/// <summary> /// 重发I帧 /// </summary> /// <param name="hostNo">主机编号</param> /// <param name="channel">摄像头对应的主机通道</param> /// <param name="devMediaServerId">设备接入服务器id</param> /// <returns></returns> public bool MakeIFrame(string hostNo, int channel, MediaServer cameraMediaServer) { MediaServer clientMediaServer = this.sdkMediaServer; if (clientMediaServer == null) { clientMediaServer = cameraMediaServer; } NLogEx.LoggerEx logEx = new NLogEx.LoggerEx(log); logEx.Trace("Enter: TiandySdkClient.MakeIFrame({0},{1},{2}).", hostNo, channel, cameraMediaServer.No); try { //调用SDK接口,登陆主机(编码器)() int loginResult = client_sdk_login2_by_id(hostNo, clientMediaServer.User, clientMediaServer.Pwd, clientMediaServer.No, cameraMediaServer.No, clientMediaServer.Ip, clientMediaServer.Port, 0); if (loginResult == CgwConst.START_RECEIVE_VIDEO_DATA_FAILED) { logEx.Error("Call Tiandy sdk function client_sdk_login2_by_id failed.dev_id:{0},username:{1},client_sup_id:{2},dev_sup_id:{3},client_sup_ip:{4},client_sup_port:{5}.", hostNo, clientMediaServer.User, clientMediaServer.No, cameraMediaServer.No, clientMediaServer.Port); return(false); } else { logEx.Info("Call Tiandy sdk function client_sdk_login2_by_id success."); } //生成I帧 if (client_sdk_make_key_frame(loginResult, channel)) { logEx.Info("Call Tiandy sdk function client_sdk_make_key_frame success."); return(true); } else { logEx.Error("Call Tiandy sdk function client_sdk_make_key_frame failed.channel:{0}", channel); return(false); } //发完关键帧后,要退出登录 } catch (Exception e) { logEx.Error("Maker I frame failed.channel:{0},Exception message:{1}.", channel, e.Message); return(false); } }
/// <summary> /// 递归天地伟业返回的设备树,获取所有通道(一个通道对应一个摄像头)、主机、流媒体(中间件)服务器 /// </summary> /// <param name="resourceTree">天地伟业返回的设备树</param> /// <param name="tiandyCameraDictionary">摄像头键值对,key为摄像头编号</param> /// <param name="hostDictionary">主机键值对,key为主机编号</param> /// <param name="mediaServerDictionary">流媒体(中间件)服务键值对,key为编号</param> private void RecursionCamera(List<Resource> resourceTree, Dictionary<string, TiandyCamera> tiandyCameraDictionary, Dictionary<string, Host> hostDictionary, Dictionary<string, MediaServer> mediaServerDictionary) { NLogEx.LoggerEx logEx = new NLogEx.LoggerEx(log); if (resourceTree == null) { logEx.Error("Recursion camera failed.ResourceTree is null."); return; } if (tiandyCameraDictionary == null) { tiandyCameraDictionary = new Dictionary<string, TiandyCamera>(); } if (hostDictionary == null) { hostDictionary = new Dictionary<string, Host>(); } if (mediaServerDictionary == null) { mediaServerDictionary = new Dictionary<string, MediaServer>(); } foreach (Resource resource in resourceTree) { //TypeId为5时,表示该节点为通道,对应一个摄像头 if (((int)NodeType.CAMERA).ToString().Equals(resource.TypeId)) { TiandyCamera camera = new TiandyCamera(resource.Id, resource.Caption); camera.Channel = resource.Ch; //摄像头的父节点为主机 camera.HostNo = resource.ParentId; tiandyCameraDictionary.Add(camera.No, camera); //如果是摄像头节点,不需要递归 continue; } else if (((int)NodeType.HOST).ToString().Equals(resource.TypeId)) { //将主机加入缓存,主机的父节点为流媒体(中间件)服务器 Host host = new Host( resource.Id, resource.Username, resource.Pwd, resource.ParentId); hostDictionary.Add(host.No, host); } else if (((int)NodeType.MEDIASERVER).ToString().Equals(resource.TypeId)) { //将流媒体(中间件)服务器加入缓存 MediaServer mediaServer = new MediaServer( resource.Id, resource.Ip, resource.Port, resource.Username, resource.Pwd); mediaServerDictionary.Add(mediaServer.No, mediaServer); } //递归处理 RecursionCamera(resource.items, tiandyCameraDictionary, hostDictionary, mediaServerDictionary); } }
/// <summary> /// 递归天地伟业返回的设备树,获取所有通道(一个通道对应一个摄像头)、主机、流媒体(中间件)服务器 /// </summary> /// <param name="resourceTree">天地伟业返回的设备树</param> /// <param name="tiandyCameraDictionary">摄像头键值对,key为摄像头编号</param> /// <param name="hostDictionary">主机键值对,key为主机编号</param> /// <param name="mediaServerDictionary">流媒体(中间件)服务键值对,key为编号</param> private void RecursionCamera(List <Resource> resourceTree, Dictionary <string, TiandyCamera> tiandyCameraDictionary, Dictionary <string, Host> hostDictionary, Dictionary <string, MediaServer> mediaServerDictionary) { NLogEx.LoggerEx logEx = new NLogEx.LoggerEx(log); if (resourceTree == null) { logEx.Error("Recursion camera failed.ResourceTree is null."); return; } if (tiandyCameraDictionary == null) { tiandyCameraDictionary = new Dictionary <string, TiandyCamera>(); } if (hostDictionary == null) { hostDictionary = new Dictionary <string, Host>(); } if (mediaServerDictionary == null) { mediaServerDictionary = new Dictionary <string, MediaServer>(); } foreach (Resource resource in resourceTree) { //TypeId为5时,表示该节点为通道,对应一个摄像头 if (((int)NodeType.CAMERA).ToString().Equals(resource.TypeId)) { TiandyCamera camera = new TiandyCamera(resource.Id, resource.Caption); camera.Channel = resource.Ch; //摄像头的父节点为主机 camera.HostNo = resource.ParentId; tiandyCameraDictionary.Add(camera.No, camera); //如果是摄像头节点,不需要递归 continue; } else if (((int)NodeType.HOST).ToString().Equals(resource.TypeId)) { //将主机加入缓存,主机的父节点为流媒体(中间件)服务器 Host host = new Host( resource.Id, resource.Username, resource.Pwd, resource.ParentId); hostDictionary.Add(host.No, host); } else if (((int)NodeType.MEDIASERVER).ToString().Equals(resource.TypeId)) { //将流媒体(中间件)服务器加入缓存 MediaServer mediaServer = new MediaServer( resource.Id, resource.Ip, resource.Port, resource.Username, resource.Pwd); mediaServerDictionary.Add(mediaServer.No, mediaServer); } //递归处理 RecursionCamera(resource.items, tiandyCameraDictionary, hostDictionary, mediaServerDictionary); } }
/// <summary> /// 启动摄像头预览 /// </summary> /// <param name="cameraNo">摄像头编号</param> /// <returns></returns> 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); }
/// <summary> /// 启动实时预览 /// </summary> /// <param name="dev_id">主机ID </param> /// <param name="cameraMediaServer">设备端接入的流媒体服务器信息 </param> /// <param name="info">实时播放信息</param> /// <returns>-1 失败,其他则是返回预览句柄</returns> public int StartReceiveVideo(string dev_id, MediaServer cameraMediaServer, ref RealPlayInfo info) { NLogEx.LoggerEx logEx = new NLogEx.LoggerEx(log); logEx.Trace("Enter: TiandySdkClient.StartReceiveVideo({0},{1},{2}).", dev_id, cameraMediaServer.No, info); int iPlayFd = CgwConst.START_RECEIVE_VIDEO_DATA_FAILED; //如果摄像机的流媒体服务器为空,那么启动码流失败 if (cameraMediaServer == null) { logEx.Error("cameraMediaServer can't be null"); return(CgwConst.START_RECEIVE_VIDEO_DATA_FAILED); } MediaServer clientMediaServer = this.sdkMediaServer; if (clientMediaServer == null) { clientMediaServer = cameraMediaServer; } try { iPlayFd = client_sdk_realplay2_ex( dev_id, clientMediaServer.User, clientMediaServer.Pwd, clientMediaServer.No, cameraMediaServer.No, clientMediaServer.Ip, clientMediaServer.Port, ref info, null, 0); if (iPlayFd == CgwConst.START_RECEIVE_VIDEO_DATA_FAILED) { logEx.Error("Call Tiandy sdk function client_sdk_realplay2_ex failed."); return(iPlayFd); } else { logEx.Info("Call Tiandy sdk function client_sdk_realplay2_ex success.dev_id:{0},username:{1},client_sup_id:{2},dev_sup_id{3},client_sup_ip:{4},client_sup_port:{5}.", dev_id, clientMediaServer.User, clientMediaServer.No, cameraMediaServer.No, clientMediaServer.Ip, clientMediaServer.Port); } //预览成功设置码流回调 //if (!client_sdk_set_realdata_callback(iPlayFd, func, 0)) if (!client_sdk_set_h264_callback(iPlayFd, this.sdkRealDataCallBackFunc, 0)) { logEx.Error("Start Receive camera video data failed.Call Tiandy sdk function client_sdk_set_h264_callback failed.playfd:{0}", iPlayFd); return(CgwConst.START_RECEIVE_VIDEO_DATA_FAILED); } else { logEx.Info("Call Tiandy sdk function client_sdk_set_h264_callback success.playfd:{0}", iPlayFd); } //因天地伟业的预览接口是异步的,还需要通过消息回调判断是否成功 int count = 0; bool isSuccess = false; //循环5次 while (count < 5) { //等待500ms Thread.Sleep(CgwConst.CHECK_RECEIVE_STATUS_WAIT_TIME); if (this.handelOperateLock.TryEnterReadLock(CgwConst.ENTER_LOCK_WAIT_TIME)) { try { if (this.receiveSuccHandle.Contains(iPlayFd)) { logEx.Info("Start Receive camera video data success."); isSuccess = true; break; } } finally { this.handelOperateLock.ExitReadLock(); } } //等待500ms Thread.Sleep(CgwConst.CHECK_RECEIVE_STATUS_WAIT_TIME); count++; } if (!isSuccess) { logEx.Error("Set callback function success. But not receive any video data.Try to stop receive."); client_sdk_set_h264_callback(iPlayFd, null, 0); client_sdk_stop_realplay(iPlayFd); iPlayFd = CgwConst.START_RECEIVE_VIDEO_DATA_FAILED; } } catch (Exception e) { iPlayFd = CgwConst.START_RECEIVE_VIDEO_DATA_FAILED; logEx.Error("Start Receive camera video data failed.Exception message:{0}.", e.Message); } return(iPlayFd); }
/// <summary> /// 启动实时预览 /// </summary> /// <param name="dev_id">主机ID </param> /// <param name="cameraMediaServer">设备端接入的流媒体服务器信息 </param> /// <param name="info">实时播放信息</param> /// <returns>-1 失败,其他则是返回预览句柄</returns> public int StartReceiveVideo(string dev_id, MediaServer cameraMediaServer, ref RealPlayInfo info) { NLogEx.LoggerEx logEx = new NLogEx.LoggerEx(log); logEx.Trace("Enter: TiandySdkClient.StartReceiveVideo({0},{1},{2}).", dev_id, cameraMediaServer.No, info); int iPlayFd = CgwConst.START_RECEIVE_VIDEO_DATA_FAILED; //如果摄像机的流媒体服务器为空,那么启动码流失败 if (cameraMediaServer == null) { logEx.Error("cameraMediaServer can't be null"); return CgwConst.START_RECEIVE_VIDEO_DATA_FAILED; } MediaServer clientMediaServer = this.sdkMediaServer; if (clientMediaServer == null) { clientMediaServer = cameraMediaServer; } try { iPlayFd = client_sdk_realplay2_ex( dev_id, clientMediaServer.User, clientMediaServer.Pwd, clientMediaServer.No, cameraMediaServer.No, clientMediaServer.Ip, clientMediaServer.Port, ref info, null, 0); if (iPlayFd == CgwConst.START_RECEIVE_VIDEO_DATA_FAILED) { logEx.Error("Call Tiandy sdk function client_sdk_realplay2_ex failed."); return iPlayFd; } else { logEx.Info("Call Tiandy sdk function client_sdk_realplay2_ex success.dev_id:{0},username:{1},client_sup_id:{2},dev_sup_id{3},client_sup_ip:{4},client_sup_port:{5}.", dev_id, clientMediaServer.User, clientMediaServer.No, cameraMediaServer.No, clientMediaServer.Ip, clientMediaServer.Port); } //预览成功设置码流回调 //if (!client_sdk_set_realdata_callback(iPlayFd, func, 0)) if (!client_sdk_set_h264_callback(iPlayFd, this.sdkRealDataCallBackFunc, 0)) { logEx.Error("Start Receive camera video data failed.Call Tiandy sdk function client_sdk_set_h264_callback failed.playfd:{0}", iPlayFd); return CgwConst.START_RECEIVE_VIDEO_DATA_FAILED; } else { logEx.Info("Call Tiandy sdk function client_sdk_set_h264_callback success.playfd:{0}", iPlayFd); } //因天地伟业的预览接口是异步的,还需要通过消息回调判断是否成功 int count = 0; bool isSuccess = false; //循环5次 while (count < 5) { //等待500ms Thread.Sleep(CgwConst.CHECK_RECEIVE_STATUS_WAIT_TIME); if (this.handelOperateLock.TryEnterReadLock(CgwConst.ENTER_LOCK_WAIT_TIME)) { try { if (this.receiveSuccHandle.Contains(iPlayFd)) { logEx.Info("Start Receive camera video data success."); isSuccess = true; break; } } finally { this.handelOperateLock.ExitReadLock(); } } //等待500ms Thread.Sleep(CgwConst.CHECK_RECEIVE_STATUS_WAIT_TIME); count++; } if (!isSuccess) { logEx.Error("Set callback function success. But not receive any video data.Try to stop receive."); client_sdk_set_h264_callback(iPlayFd, null, 0); client_sdk_stop_realplay(iPlayFd); iPlayFd = CgwConst.START_RECEIVE_VIDEO_DATA_FAILED; } } catch (Exception e) { iPlayFd = CgwConst.START_RECEIVE_VIDEO_DATA_FAILED; logEx.Error("Start Receive camera video data failed.Exception message:{0}.", e.Message); } return iPlayFd; }
/// <summary> /// 重发I帧 /// </summary> /// <param name="hostNo">主机编号</param> /// <param name="channel">摄像头对应的主机通道</param> /// <param name="devMediaServerId">设备接入服务器id</param> /// <returns></returns> public bool MakeIFrame(string hostNo, int channel, MediaServer cameraMediaServer) { MediaServer clientMediaServer = this.sdkMediaServer; if (clientMediaServer == null) { clientMediaServer = cameraMediaServer; } NLogEx.LoggerEx logEx = new NLogEx.LoggerEx(log); logEx.Trace("Enter: TiandySdkClient.MakeIFrame({0},{1},{2}).", hostNo, channel, cameraMediaServer.No); try { //调用SDK接口,登陆主机(编码器)() int loginResult = client_sdk_login2_by_id(hostNo, clientMediaServer.User, clientMediaServer.Pwd, clientMediaServer.No, cameraMediaServer.No, clientMediaServer.Ip, clientMediaServer.Port, 0); if (loginResult == CgwConst.START_RECEIVE_VIDEO_DATA_FAILED) { logEx.Error("Call Tiandy sdk function client_sdk_login2_by_id failed.dev_id:{0},username:{1},client_sup_id:{2},dev_sup_id:{3},client_sup_ip:{4},client_sup_port:{5}.", hostNo, clientMediaServer.User, clientMediaServer.No, cameraMediaServer.No, clientMediaServer.Port); return false; } else { logEx.Info("Call Tiandy sdk function client_sdk_login2_by_id success."); } //生成I帧 if (client_sdk_make_key_frame(loginResult, channel)) { logEx.Info("Call Tiandy sdk function client_sdk_make_key_frame success."); return true; } else { logEx.Error("Call Tiandy sdk function client_sdk_make_key_frame failed.channel:{0}", channel); return false; } //发完关键帧后,要退出登录 } catch (Exception e) { logEx.Error("Maker I frame failed.channel:{0},Exception message:{1}.", channel, e.Message); return false; } }