/// <summary> /// 启动摄像头 /// </summary> /// <param name="cil"></param> private static void liveCamera(CameraInstance cil) { try { CameraSession cameraSession = null !; if (cil != null && cil.EnableLive) { cameraSession = GetCameraCurrentSession(cil); if (cameraSession == null || (cameraSession != null && cameraSession.IsOnline == false) ) //camera没有,或者isonline是false要推流 { CameraType ctype = cameraSession != null ? cameraSession.CameraType : cil.CameraType; switch (ctype) { case CameraType.Rtsp: liveRtsp(cil); break; case CameraType.GB28181: bool found = false; lock (Common.SipProcess.SipDeviceLock) { var dev = Common.SipProcess.SipDeviceList.FindLast(x => x.DeviceId.Equals(cil.CameraDeviceLable)); if (dev != null && dev.CameraExList != null && dev.CameraExList.Count > 0) { var camera = dev.CameraExList.FindLast(x => x.Camera != null && x.Camera.DeviceID.Equals(cil.CameraChannelLable)); if (camera != null) { found = true; //break; } } } if (found == true) { LiveGb28181(cil); } break; } } else if (cameraSession != null && string.IsNullOrEmpty(cameraSession.CameraId) && cameraSession.IsOnline == true ) //当camera不为空,但camera.cameraid为空时,不需要重新推,但要补全这个id { CameraSession sessionsub = null !; lock (Common.CameraSessionLock) { sessionsub = Common.CameraSessions.FindLast(x => x.App !.Equals(cameraSession.App) && x.Vhost !.Equals(cameraSession.Vhost) && x.StreamId !.Equals(cameraSession.StreamId)) !; } if (sessionsub != null) { lock (Common.CameraSessionLock) { cameraSession.CameraId = cil.CameraId; } } } } } catch (Exception ex) { Logger.Logger.Error("liveCamera Except -> " + ex.Message + "->" + ex.StackTrace); } }
/// <summary> /// 启动rtsp摄像头 /// </summary> /// <param name="cil"></param> private static void liveRtsp(CameraInstance cil) { try { ResponseStruct rs = null !; var mediaObj = Common.MediaServerList.FindLast(x => x.MediaServerId.Equals(cil.PushMediaServerId)); if (mediaObj == null || mediaObj.IsRunning == false) { return; } bool useFFmpeg = true; if (cil.RetryTimes < 2) { //rtsp方式,先跳过三次,因为zlmediakit会自动维护掉线的ffmpeg,要延迟处理一下,不然会重复创建ffmpeg cil.RetryTimes++; return; } cil.RetryTimes = 0; ResZLMediaKitAddFFmpegProxy ret = null !; if (useFFmpeg) { ret = MediaServerApis.AddFFmpegProxy( mediaObj.MediaServerId, cil.IfRtspUrl, out rs); } else { ret = MediaServerApis.AddStreamProxy(mediaObj.MediaServerId, cil.IfRtspUrl, out rs); } if (ret != null && rs.Code == ErrorNumber.None) { Logger.Logger.Info("Rtsp推流成功->" + cil.CameraId + "->" + cil.IfRtspUrl); CameraSession sessionsub = null !; lock (Common.CameraSessionLock) { sessionsub = Common.CameraSessions.FindLast(x => x.App !.Equals(ret.App) && x.Vhost !.Equals(ret.Vhost) && x.StreamId !.Equals(ret.StreamId)) !; } if (sessionsub != null) { lock (Common.CameraSessionLock) { sessionsub.CameraId = cil.CameraId; } } } else { Logger.Logger.Warn( "Rtsp推流失败->" + cil.CameraId + "->" + cil.IfRtspUrl + "->" + JsonHelper.ToJson(rs)); } } catch (Exception ex) { Logger.Logger.Error("liveRtsp Except ->" + ex.Message + "->" + ex.StackTrace); } }