LiveVideoPusherV2.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Vinno.FIS.TRTCClient.Common.Enum;
  9. using Vinno.IUS.Common.Log;
  10. using Vinno.vCloud.Common.FIS.Helper;
  11. using Vinno.vCloud.FIS.CrossPlatform.Common;
  12. using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
  13. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo;
  14. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo.Interface;
  15. using WingInterfaceLibrary.Enum;
  16. using WingInterfaceLibrary.Interface;
  17. using WingInterfaceLibrary.LiveConsultation;
  18. using WingInterfaceLibrary.Request.Device;
  19. namespace Vinno.vCloud.Common.FIS.LiveVideos
  20. {
  21. public class LiveVideoPusherV2 : IDisposable
  22. {
  23. private readonly object _locker = new object();
  24. private readonly object _currentDeviceInfolocker = new object();
  25. private readonly ConcurrentDictionary<EnumPusherType, Func<ILiveVideoPusherV2>> _pusherCreations;
  26. private readonly List<CPVideoDeviceOutputInfo> _currentVideoDeviceInfoList;
  27. private readonly string _token;
  28. private readonly ILiveConsultationService _liveConsultationService;
  29. private readonly IDeviceService _deviceService;
  30. private readonly IEducationService _educationService;
  31. private readonly string _deviceModel;
  32. private readonly string _deviceType;
  33. private readonly string _softwareVersion;
  34. private ManualResetEvent _manualResetEvent = new ManualResetEvent(false);
  35. private IExtendedData _currentExtendData;
  36. private EnumPusherType _currentPushType;
  37. private ILiveVideoPusherV2 _pusher;
  38. private bool _hasReportedStatus;
  39. private int _rtcRoomNo;
  40. private PushHeartRateKeeperV2 _pushHeartRateKeeper;
  41. private bool _isPaused;
  42. /// <summary>
  43. /// 是否正在推流
  44. /// </summary>
  45. public bool IsPushing { get; private set; }
  46. public ILiveVideoPusherV2 LiveVideoPusher => _pusher;
  47. /// <summary>
  48. /// 推流状态改变时触发,主要给预览用
  49. /// </summary>
  50. public event EventHandler<PusherState> PusherStateChanged;
  51. /// <summary>
  52. /// The event to notificate the US to show or hide the live video out put window
  53. /// </summary>
  54. public event EventHandler<LiveNotificationArgs> LiveNotification;
  55. public LiveVideoPusherV2(string token, ILiveConsultationService liveConsultationService, IDeviceService deviceService, IEducationService educationService, string deviceModel, string deviceType, string softwareVersion)
  56. {
  57. _token = token;
  58. _liveConsultationService = liveConsultationService;
  59. _deviceService = deviceService;
  60. _educationService = educationService;
  61. _deviceModel = deviceModel;
  62. _deviceType = deviceType;
  63. _softwareVersion = softwareVersion;
  64. _currentVideoDeviceInfoList = new List<CPVideoDeviceOutputInfo>();
  65. _pusherCreations = new ConcurrentDictionary<EnumPusherType, Func<ILiveVideoPusherV2>>();
  66. }
  67. /// <summary>
  68. /// 注册具体推流类
  69. /// </summary>
  70. /// <param name="type">推流类型</param>
  71. /// <param name="pusher">推流类的创建方法</param>
  72. public void RegisterPusher(EnumPusherType type, Func<ILiveVideoPusherV2> pusher)
  73. {
  74. _pusherCreations.TryAdd(type, pusher);
  75. }
  76. public void UpdateCurrentVideoDeviceInfoList(List<CPVideoDeviceOutputInfo> infos)
  77. {
  78. lock (_currentDeviceInfolocker)
  79. {
  80. _currentVideoDeviceInfoList.Clear();
  81. foreach (var info in infos)
  82. {
  83. var item = info.Clone() as CPVideoDeviceOutputInfo;
  84. _currentVideoDeviceInfoList.Add(item);
  85. }
  86. }
  87. }
  88. public void SetIsPaused(bool isPaused)
  89. {
  90. Logger.WriteLineInfo($"LiveVideoPusherV2 Set IsPaused:{isPaused}");
  91. _isPaused = isPaused;
  92. if (isPaused)
  93. {
  94. StopPusher(isPaused);
  95. }
  96. }
  97. public void LiveStateChanged(LiveEventArgs e)
  98. {
  99. try
  100. {
  101. if (e.IsLive && IsPushing)
  102. {
  103. Logger.WriteLineError($"LiveVideoPusherV2 LiveStateChanged Error,it is already pushing.");
  104. return;
  105. }
  106. if (e.IsLive)
  107. {
  108. _currentPushType = ConvertToPusherType(e);
  109. Logger.WriteLineInfo($"LiveVideoPusherV2 Current Push Type:{_currentPushType}");
  110. InitPusher(_currentPushType);
  111. StartPusher(e.ExtendedData);
  112. }
  113. else
  114. {
  115. StopPusher();
  116. }
  117. }
  118. catch (Exception ex)
  119. {
  120. Logger.WriteLineError($"LiveVideoPusherV2 Receive Live State Error:{ex}");
  121. }
  122. }
  123. public void SetMute(bool isMute)
  124. {
  125. lock (_locker)
  126. {
  127. _pusher?.SetMute(isMute);
  128. }
  129. }
  130. public void SwitchMic(string micId)
  131. {
  132. lock (_locker)
  133. {
  134. _pusher?.SwitchMic(micId);
  135. }
  136. }
  137. /// <summary>
  138. /// 开始推流
  139. /// </summary>
  140. /// <param name="pushParams"></param>
  141. protected virtual void StartPusher(IExtendedData pushParams)
  142. {
  143. PusherStateChanged?.Invoke(this, PusherState.Preparing);
  144. try
  145. {
  146. lock (_locker)
  147. {
  148. _hasReportedStatus = false;
  149. _manualResetEvent.Reset();
  150. _pusher.ChannelStateChanged += OnChannelStateChanged;
  151. var success = _pusher.StartPusher(pushParams, _currentVideoDeviceInfoList);
  152. if (success)
  153. {
  154. Task.Run(ReportLiveStateTask);
  155. Logger.WriteLineInfo("LiveVideoPusherV2 Start Pusher Success!");
  156. _currentExtendData = pushParams;
  157. IsPushing = true;
  158. LiveVideoStatusChecker.Instance.IsPushing = true;
  159. if (pushParams is RtcExtendedData rtcExtendedData)
  160. {
  161. _rtcRoomNo = rtcExtendedData.RoomId;
  162. if (!CommonParameter.Instance.IsSonopost)
  163. {
  164. var deviceInfo = rtcExtendedData.UserInfos.FirstOrDefault(x => x.Category == EnumLiveChannelCategory.Main);
  165. if (deviceInfo != null && deviceInfo.Width > 0 && deviceInfo.Height > 0)
  166. {
  167. LiveNotification?.Invoke(this, new LiveNotificationArgs(true, deviceInfo.Width, deviceInfo.Height));
  168. }
  169. }
  170. }
  171. else if (pushParams is RtmpExtendedData rtmpExtendedData)
  172. {
  173. _rtcRoomNo = 0;
  174. if (!CommonParameter.Instance.IsSonopost)
  175. {
  176. var deviceInfo = rtmpExtendedData.UserInfos.FirstOrDefault(x => x.Category == EnumLiveChannelCategory.Main);
  177. if (deviceInfo != null && deviceInfo.Width > 0 && deviceInfo.Height > 0)
  178. {
  179. LiveNotification?.Invoke(this, new LiveNotificationArgs(true, deviceInfo.Width, deviceInfo.Height));
  180. }
  181. }
  182. }
  183. }
  184. else
  185. {
  186. _hasReportedStatus = true;
  187. UpdateChannelState(DeviceLiveStateEnum.Error, "Terminal Start Pushing Failed");
  188. Logger.WriteLineError("LiveVideoPusherV2 Start Pusher Fail!");
  189. }
  190. }
  191. }
  192. catch (Exception e)
  193. {
  194. Logger.WriteLineError($"LiveVideoPusherV2 Start Pusher Error:{e}");
  195. }
  196. PusherStateChanged?.Invoke(this, PusherState.Prepared);
  197. }
  198. private void ReportLiveStateTask()
  199. {
  200. try
  201. {
  202. _manualResetEvent.WaitOne(30000);//30秒钟内没有上报,就认为是推流失败
  203. if (!_hasReportedStatus)
  204. {
  205. UpdateChannelState(DeviceLiveStateEnum.Error, "Start Pushing Error");
  206. _hasReportedStatus = true;
  207. }
  208. }
  209. catch (Exception ex)
  210. {
  211. Logger.WriteLineError($"ReportLiveStateTask Error:{ex}");
  212. }
  213. }
  214. /// <summary>
  215. /// 结束推流
  216. /// </summary>
  217. public void StopPusher(bool isPaused = false, bool needInform = false)
  218. {
  219. PusherStateChanged?.Invoke(this, PusherState.Preparing);
  220. try
  221. {
  222. lock (_locker)
  223. {
  224. if (_pusher == null)
  225. return;
  226. _hasReportedStatus = true;
  227. _manualResetEvent.Set();
  228. _pusher.ChannelStateChanged -= OnChannelStateChanged;
  229. var success = _pusher.StopPusher();
  230. if (success)
  231. {
  232. Logger.WriteLineInfo("LiveVideoPusherV2 Stop Pusher Success!");
  233. StopHeartRateKeeper();
  234. _pusher.Dispose();
  235. _pusher = null;
  236. IsPushing = false;
  237. LiveVideoStatusChecker.Instance.IsPushing = false;
  238. }
  239. else
  240. {
  241. Logger.WriteLineError("LiveVideoPusherV2 Stop Pusher Fail!");
  242. }
  243. UpdateChannelState(DeviceLiveStateEnum.Closed, "Terminal Close Pushing");
  244. if (!CommonParameter.Instance.IsSonopost)
  245. {
  246. LiveNotification?.Invoke(this, new LiveNotificationArgs(false, 0, 0));
  247. }
  248. if (needInform)
  249. {
  250. Task.Run(() =>
  251. {
  252. LiveVideoStatusChecker.Instance.CurrentMachineConsultationStopPushingEvent?.Invoke(this, EventArgs.Empty);
  253. });
  254. }
  255. }
  256. }
  257. catch (Exception e)
  258. {
  259. Logger.WriteLineError($"LiveVideoPusherV2 Stop Pusher Error:{e}");
  260. }
  261. if (!isPaused)
  262. {
  263. PusherStateChanged?.Invoke(this, PusherState.Prepared);
  264. }
  265. }
  266. /// <summary>
  267. /// 重新推流
  268. /// </summary>
  269. public void ReStartPusher()
  270. {
  271. if (!IsPushing)
  272. {
  273. return;
  274. }
  275. Logger.WriteLineInfo("LiveVideoPusherV2 Restart Pusher");
  276. StopPusher();
  277. InitPusher(_currentPushType);
  278. StartPusher(_currentExtendData);
  279. }
  280. private void OnChannelStateChanged(object sender, ChannelStateEventArgsV2 e)
  281. {
  282. try
  283. {
  284. Logger.WriteLineInfo($"LiveVideoPusherV2 Update Channel State:{e.Category}_{e.State}");
  285. if (!_hasReportedStatus)
  286. {
  287. UpdateChannelState((DeviceLiveStateEnum)e.State, "Terminal Start Pushing");
  288. _hasReportedStatus = true;
  289. _manualResetEvent.Set();
  290. }
  291. }
  292. catch (Exception ex)
  293. {
  294. Logger.WriteLineError($"LiveVideoPusherV2 Update Channel State Error:{ex}");
  295. }
  296. }
  297. public void UpdateChannelState(DeviceLiveStateEnum state, string message)
  298. {
  299. try
  300. {
  301. if (CommonParameter.Instance.IsFeatureReleased)
  302. {
  303. Logger.WriteLineError($"UpdateChannelState Skipped,because the feature released");
  304. return;
  305. }
  306. var reportLiveStateRequest = new ReportLiveStateRequest
  307. {
  308. Token = _token,
  309. RoomNo = _rtcRoomNo,
  310. LiveState = state,
  311. Message = message
  312. };
  313. bool result = JsonRpcHelper.ReportLiveState(_deviceService, reportLiveStateRequest);
  314. if (result)
  315. {
  316. Logger.WriteLineInfo($"JsonRPCHelper ReportLiveState Sucess: State:{state},Message:{message}");
  317. }
  318. else
  319. {
  320. Logger.WriteLineError($"JsonRPCHelper ReportLiveState Fail: State:{state},Message:{message}");
  321. }
  322. }
  323. catch (Exception ex)
  324. {
  325. Logger.WriteLineError($"LiveVideoPusherV2 UpdateChannelState Error:{ex}");
  326. }
  327. }
  328. public void StartHeartRateKeeper(string heartRateCode, int interval, EnumHeartRateType heartRateType)
  329. {
  330. lock (_locker)
  331. {
  332. if (!IsPushing)
  333. {
  334. Logger.WriteLineError($"LiveVideoPusherV2 Start PushHeartRateKeeper Fail,Because it isn't Pushing");
  335. return;
  336. }
  337. if (string.IsNullOrWhiteSpace(heartRateCode))
  338. {
  339. Logger.WriteLineError($"LiveVideoPusherV2 Start PushHeartRateKeeper Fail,Because heartRateCode is null");
  340. return;
  341. }
  342. if (_pushHeartRateKeeper != null)
  343. {
  344. Logger.WriteLineError($"LiveVideoPusherV2 Start PushHeartRateKeeper Fail,Because it is busy");
  345. return;
  346. }
  347. else
  348. {
  349. if (_currentExtendData != null)
  350. {
  351. _currentExtendData.HeartRateCode = heartRateCode;
  352. }
  353. switch (heartRateType)
  354. {
  355. case EnumHeartRateType.LiveConsultation:
  356. _pushHeartRateKeeper = new PushHeartRateKeeperV2(_token, heartRateCode, interval, _liveConsultationService);
  357. LiveVideoStatusChecker.Instance.IsConsultationLiving = true;
  358. LiveVideoStatusChecker.Instance.CurrentConsultationCode = heartRateCode;
  359. break;
  360. case EnumHeartRateType.Education:
  361. _pushHeartRateKeeper = new PushHeartRateKeeperV2(_token, heartRateCode, interval, _educationService);
  362. LiveVideoStatusChecker.Instance.IsEducationLiving = true;
  363. break;
  364. }
  365. if (_pushHeartRateKeeper != null)
  366. {
  367. _pushHeartRateKeeper.Offlined += OnOfflined;
  368. _pushHeartRateKeeper.Start();
  369. }
  370. Logger.WriteLineInfo($"LiveVideoPusherV2 Start PushHeartRateKeeper,HeartRateType:{heartRateType}");
  371. }
  372. }
  373. }
  374. public void StopHeartRateKeeper()
  375. {
  376. lock (_locker)
  377. {
  378. if (_pushHeartRateKeeper != null)
  379. {
  380. _pushHeartRateKeeper.Offlined -= OnOfflined;
  381. _pushHeartRateKeeper.Stop();
  382. _pushHeartRateKeeper = null;
  383. Logger.WriteLineInfo("LiveVideoPusherV2 Stop PushHeartRateKeeper");
  384. }
  385. }
  386. LiveVideoStatusChecker.Instance.IsConsultationLiving = false;
  387. LiveVideoStatusChecker.Instance.IsEducationLiving = false;
  388. LiveVideoStatusChecker.Instance.CurrentConsultationCode = null;
  389. }
  390. private void OnOfflined(object sender, EventArgs e)
  391. {
  392. Logger.WriteLineInfo($"LiveVideoPusherV2 Pusher is offline");
  393. }
  394. private void InitPusher(EnumPusherType type)
  395. {
  396. lock (_locker)
  397. {
  398. if (_pusherCreations.TryGetValue(type, out var creation))
  399. {
  400. if (_pusher != null)
  401. {
  402. StopPusher();
  403. }
  404. _pusher = creation.Invoke();
  405. }
  406. else
  407. {
  408. throw new ArgumentException($"LiveVideoPusherV2 Not support Mode {type}");
  409. }
  410. }
  411. }
  412. private EnumPusherType ConvertToPusherType(LiveEventArgs liveEventArgs)
  413. {
  414. if (liveEventArgs.Protocol == EnumLiveProtocol.RTC)
  415. {
  416. var rtcExtendedData = liveEventArgs.ExtendedData as RtcExtendedData;
  417. if (liveEventArgs.PushMode == EnumLiveDataMode.MergeLive)
  418. {
  419. if (liveEventArgs.ExtendedData.MergeType == EnumMergeType.Merge1280X720)
  420. {
  421. return EnumPusherType.USRtcMerge;
  422. }
  423. else if (liveEventArgs.ExtendedData.MergeType == EnumMergeType.Merge1920X1080)
  424. {
  425. return EnumPusherType.RtcMerge;
  426. }
  427. }
  428. else if (liveEventArgs.PushMode == EnumLiveDataMode.OnlyLive)
  429. {
  430. if (rtcExtendedData.UserInfos.Count() <= 1)
  431. {
  432. return EnumPusherType.RtcSingle;
  433. }
  434. else
  435. {
  436. return EnumPusherType.RtcMulti;
  437. }
  438. }
  439. }
  440. else if (liveEventArgs.Protocol == EnumLiveProtocol.Rtmp)
  441. {
  442. var rtmpExtendedData = liveEventArgs.ExtendedData as RtmpExtendedData;
  443. if (liveEventArgs.PushMode == EnumLiveDataMode.MergeLive)
  444. {
  445. if (liveEventArgs.ExtendedData.MergeType == EnumMergeType.Merge1280X720)
  446. {
  447. return EnumPusherType.USRtmpMerge;
  448. }
  449. else if (liveEventArgs.ExtendedData.MergeType == EnumMergeType.Merge1920X1080)
  450. {
  451. return EnumPusherType.RtmpMerge;
  452. }
  453. }
  454. else if (liveEventArgs.PushMode == EnumLiveDataMode.OnlyLive)
  455. {
  456. if (rtmpExtendedData.UserInfos.Count() <= 1)
  457. {
  458. return EnumPusherType.RtmpSingle;
  459. }
  460. else
  461. {
  462. return EnumPusherType.RtmpMulti;
  463. }
  464. }
  465. }
  466. throw new ArgumentException("LiveVideoPusherV2 Unknown Pusher type");
  467. }
  468. public void Dispose()
  469. {
  470. StopPusher();
  471. _pusherCreations.Clear();
  472. }
  473. public void ChangeConsultationCode(string consultationCode)
  474. {
  475. _pushHeartRateKeeper?.SetHeartRateCode(consultationCode);
  476. if (_currentExtendData != null)
  477. {
  478. _currentExtendData.HeartRateCode = consultationCode;
  479. }
  480. if (LiveVideoStatusChecker.Instance.IsConsultationLiving)
  481. {
  482. LiveVideoStatusChecker.Instance.CurrentConsultationCode = consultationCode;
  483. }
  484. }
  485. public bool StartSpeedTest()
  486. {
  487. try
  488. {
  489. var createLiveRoomInfoRequest = new CreateLiveRoomInfoRequest
  490. {
  491. DeviceModel = _deviceModel,
  492. DeviceType = _deviceType,
  493. DeviceUniqueCode = CommonParameter.Instance.MachineId,
  494. SoftwareVersion = _softwareVersion,
  495. };
  496. var result = JsonRpcHelper.CreateLiveRoomInfo(_deviceService, createLiveRoomInfoRequest);
  497. if (result == null)
  498. {
  499. throw new InvalidDataException($"JsonRPCHelper CreateLiveRoomInfo Result is null");
  500. }
  501. else
  502. {
  503. var pusher = CrossPlatformHelper.Instance.LiveVideoPusherCreatorV2.CreateRTCSinglePusherV2();
  504. return pusher.StartSpeedTest((uint)result.AppId, result.UserCode, result.UserSign);
  505. }
  506. }
  507. catch (Exception ex)
  508. {
  509. Logger.WriteLineError($"LiveVideoPusherV2 StartSpeedTest Fail:{ex}");
  510. return false;
  511. }
  512. }
  513. }
  514. }