public SmcError.SmcErr StartReceiveVideo(string cameraNo)
{
NLogEx.LoggerEx logEx = new NLogEx.LoggerEx(log);
logEx.Trace("Enter: eLTEVideoMonitor.StartReceiveVideo({0}).", cameraNo);
SmcErr err = new CgwError();
//预览成功,需要停止原来的预览,并将预览句柄添加到缓存
//需要停止的预览句柄
UInt32 needToStopChannel = CgwConst.T28181_ERROR_HANDLE;
if (this.handelOperateLock.TryEnterWriteLock(CgwConst.ENTER_LOCK_WAIT_TIME))
{
try
{
//如果预览句柄已经存在,删除掉原来的句柄,用新的句柄替换
if (this.cameraVideoChannelDic.ContainsKey(cameraNo.ToString()))
{
needToStopChannel = this.cameraVideoChannelDic[cameraNo.ToString()];
this.videoChannelDataSenderDic.Remove(needToStopChannel);
this.cameraVideoChannelDic.Remove(cameraNo.ToString());
//用户参数,4字节整数
IntPtr pUser = Marshal.AllocHGlobal(4);
NetSourcedInterface.IVS_NETSOURCE_SetDataCallBack(needToStopChannel, null, pUser);
NetSourcedInterface.IVS_NETSOURCE_CloseNetStream(needToStopChannel);
//释放NETSOURCE通道资源
NetSourcedInterface.IVS_NETSOURCE_FreeChannel(needToStopChannel);
//关闭rtp回调
rtpAdapter.ESDK_RTP_CloseChannel(needToStopChannel);
//释放用户数据内存
foreach (KeyValuePair<UInt32, IntPtr> kvp in channelInPtrDic)
{
if (kvp.Key == needToStopChannel)
{
Marshal.FreeHGlobal(kvp.Value);
channelInPtrDic.Remove(kvp.Key);
break;
}
}
}
}
finally
{
this.handelOperateLock.ExitWriteLock();
}
}
//重新预览后,更新了预览句柄,需要将原来的预览停止
if (needToStopChannel != CgwConst.T28181_ERROR_HANDLE)
{
ELTE_RESULT result = (ELTE_RESULT)this.lteSdkClient.StopP2PVideo(Convert.ToInt32(cameraNo));
//如果不为0,表示停止原来的预览失败,只记录日志,不返回错误,不设置错误码
if (result != ELTE_RESULT.RET_SUCCESS)
{
logEx.Error("Get a new preview success. But stop old preview failed.CameraNo:{0},Ivs sdk error code:{0}", cameraNo, result);
}
}
//设置实况参数
eLTEVideoParameter para = new eLTEVideoParameter();
para.cameratype = cameraType;
para.fmtvalue = fmtValue;
para.mutetype = muteType;
para.user_confirm_type = userConfirmType;
if (this.videoAutOperateLock.TryEnterWriteLock(CgwConst.ENTER_LOCK_WAIT_TIME))
{
try
{
if (!videoAutoEvents.ContainsKey(cameraNo))
{
AutoResetEvent startAutoEvent = new AutoResetEvent(false);
startAutoEvent.Reset();
eLTEAutoReset elTEAutoEvent = new eLTEAutoReset();
elTEAutoEvent.eLTEVideoEvent = startAutoEvent;
elTEAutoEvent.VideoSuccess = false;
logEx.Trace("StartReceiveVideo add CameraNo");
videoAutoEvents.Add(cameraNo, elTEAutoEvent);
}
//else
//{
// cameraNoExists = true;
// logEx.Info("StartReceiveVideo Contains CameraNo");
//}
}
finally
{
this.videoAutOperateLock.ExitWriteLock();
}
}
ELTE_RESULT iRet = (ELTE_RESULT)lteSdkClient.StartP2PVideoMonitor(Convert.ToInt32(cameraNo), para);
if (iRet != ELTE_RESULT.RET_SUCCESS)
{
err.SetErrorNo(CgwError.START_RECEIVE_VIDEO_FAILED);
//logEx.Error("StartReceiveVideo.StartP2PVideoMonitor failed.CameraNo:{0},ELTE_RESULT:{1}", cameraNo, Enum.GetName(typeof(ELTE_RESULT), iRet));
logEx.Error("StartReceiveVideo.StartP2PVideoMonitor failed.CameraNo:{0},ELTE_RESULT:{1}", cameraNo, iRet.ToString());
}
else
{
if (!videoAutoEvents[cameraNo].eLTEVideoEvent.WaitOne(start_Wait_Time))
{
//等待播放超时,设置错误码
err.SetErrorNo(CgwError.START_RECEIVE_VIDEO_FAILED);
}
else
{
if (!videoAutoEvents[cameraNo].VideoSuccess)//未超时,但播放失败
{
err.SetErrorNo(CgwError.START_RECEIVE_VIDEO_FAILED);
}
}
}
if (this.videoAutOperateLock.TryEnterWriteLock(CgwConst.ENTER_LOCK_WAIT_TIME))
{
try
{
if (videoAutoEvents.ContainsKey(cameraNo))
{
videoAutoEvents.Remove(cameraNo);
logEx.Trace("StartReceiveVideo Remove CameraNo:{0} Success", cameraNo);
}
}
finally
{
this.videoAutOperateLock.ExitWriteLock();
}
}
return err;
}