CgwMonitorManage.Tiandy.TiandyVideoMonitor.StartReceiveVideo C# (CSharp) Method

StartReceiveVideo() public method

启动摄像头预览
public StartReceiveVideo ( string cameraNo ) : SmcErr
cameraNo string 摄像头编号
return CgwMonitorManage.SmcError.SmcErr
        public SmcErr StartReceiveVideo(string cameraNo)
        {
            NLogEx.LoggerEx logEx = new NLogEx.LoggerEx(log);
            logEx.Trace("Enter: TiandyVideoMonitor.StartReceiveVideo({0}).", cameraNo);
            SmcErr err = new CgwError();

            TiandyCamera camera = null;
            Host host = null;

            //摄像头所连接的流媒体服务器
            MediaServer cameraMediaServer = null;
            if (this.cameraOperateLock.TryEnterReadLock(CgwConst.ENTER_LOCK_WAIT_TIME))
            {
                try
                {
                    if (!this.tiandyCameraDictionary.ContainsKey(cameraNo))
                    {
                        err.SetErrorNo(CgwError.CAMERA_NOT_FOUND);
                        logEx.Error("Start Receive camera video data failed.Camera No is not found.Camera No:{0}", cameraNo);
                        return err;
                    }
                    camera = this.tiandyCameraDictionary[cameraNo];
                    host = this.hostDictionary[camera.HostNo];
                    cameraMediaServer = this.mediaServerDictionary[host.MediaServerNo];
                }
                catch (Exception e)
                {
                    err.SetErrorNo(CgwError.START_RECEIVE_VIDEO_FAILED);
                    logEx.Error("Start Receive camera video data failed.Camera No:{0},Execption message:{1}", cameraNo, e.Message);
                    return err;
                }
                finally
                {
                    this.cameraOperateLock.ExitReadLock();
                }
            }

            if (camera == null)
            {
                err.SetErrorNo(CgwError.START_RECEIVE_VIDEO_FAILED);
                logEx.Error("Start Receive camera video data failed.Camera No is not found.Camera No:{0}", cameraNo);
                return err;
            }

            RealPlayInfo real = new RealPlayInfo();
            real.ch = camera.Channel;
            real.data_type = (int)TiandyDateType.PRIMARY;
            real.hPlayWnd = null;

            int result = this.sdkClient.StartReceiveVideo(host.No, cameraMediaServer, ref real);

            //如果为负数,表示预览失败
            if (result < 0)
            {
                err.SetErrorNo(CgwError.START_RECEIVE_VIDEO_FAILED);
                logEx.Error("Start Receive camera video data failed.Camera No:{0}.", cameraNo);
                return err;
            }
            else
            {
                //需要停止的预览句柄
                int needToStopHandel = CgwConst.START_RECEIVE_VIDEO_DATA_FAILED;
                if (this.handelOperateLock.TryEnterWriteLock(CgwConst.ENTER_LOCK_WAIT_TIME))
                {
                    try
                    {
                        //如果预览句柄已经存在,删除掉原来的句柄,用新的句柄替换
                        if (this.cameraVideoHandeDic.ContainsKey(cameraNo))
                        {
                            needToStopHandel = this.cameraVideoHandeDic[cameraNo];
                            this.videoHandleCameraDic.Remove(needToStopHandel);
                            this.cameraVideoHandeDic.Remove(cameraNo);
                        }
                        this.cameraVideoHandeDic.Add(cameraNo, result);
                        this.videoHandleCameraDic.Add(result, cameraNo);
                    }
                    finally
                    {
                        this.handelOperateLock.ExitWriteLock();
                    }
                }
                logEx.Info("Start Receive camera video data success.Camera No:{0},Handle:{1}.", cameraNo, result);
                //重新预览后,更新了预览句柄,需要将原来的预览停止,放在handelOperateLock外面,防止长时间占用锁
                if (needToStopHandel != CgwConst.START_RECEIVE_VIDEO_DATA_FAILED)
                {
                    this.sdkClient.StopReceiveVideo(needToStopHandel);
                }
            }

            return err;
        }