LiveClient.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. using System;
  2. using System.Threading.Tasks;
  3. using Vinno.IUS.Common.Log;
  4. using Vinno.IUS.Common.Network.Leaf;
  5. using Vinno.IUS.Common.Network.Transfer;
  6. using Vinno.vCloud.FIS.CrossPlatform.Common;
  7. using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
  8. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo;
  9. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo.Interface;
  10. using Vinno.vCloud.Protocol.Infrastructures;
  11. using Vinno.vCloud.Protocol.Messages.Client.Live;
  12. using Vinno.vCloud.Protocol.Messages.Live;
  13. namespace Vinno.vCloud.Common.FIS.LiveVideos
  14. {
  15. internal class LiveClient : IDisposable
  16. {
  17. private ILiveVideoPusher _pusher;
  18. private ClientLeaf _clientLeaf;
  19. private PushHeartRateKeeper _pushHeartRateKeeper;
  20. private EnumLiveStates _liveState;
  21. private object _getLiveProtocolLock = new object();
  22. private LiveProtocol _liveProtocol;
  23. private int _usScreenWidth;
  24. private int _usScreenHeight;
  25. /// <summary>
  26. /// Preview local camera capured changed
  27. /// </summary>
  28. public event EventHandler<CPVideoFrameData> PreviewCameraCapured;
  29. /// <summary>
  30. /// Raised when live changed to protocol
  31. /// </summary>
  32. public event EventHandler<LiveProtocol> LiveProtocolChanged;
  33. /// <summary>
  34. /// The event to notificate the US to show or hide the live video out put window
  35. /// </summary>
  36. public event EventHandler<LiveNotificationArgs> LiveNotification;
  37. public LiveClient(ClientLeaf clientLeaf, int usScreenWidth, int usScreenHeight)
  38. {
  39. _clientLeaf = clientLeaf;
  40. _liveState = EnumLiveStates.Idle;
  41. _liveProtocol = LiveProtocol.RTC;
  42. _usScreenWidth = usScreenWidth;
  43. _usScreenHeight = usScreenHeight;
  44. }
  45. /// <summary>
  46. /// Start Pusher
  47. /// </summary>
  48. public void Start(LiveProtocol liveProtocol)
  49. {
  50. try
  51. {
  52. var pushConfiguration = CPCombineSmartPushConfiguration.Instance;
  53. if (_liveState == EnumLiveStates.TerminalIsPushing)
  54. {
  55. Logger.WriteLineInfo($"LiveClient is pushing ,not do start");
  56. return;
  57. }
  58. bool isStartPush = false;
  59. pushConfiguration.IsPushOnce = true;
  60. if (liveProtocol == LiveProtocol.RTC)
  61. {
  62. //直播推流
  63. if (pushConfiguration.ShouldPushData)
  64. {
  65. if (_pusher == null)
  66. {
  67. _pusher = CrossPlatformHelper.Instance.LiveVideoPusherCreator.CreateRtcPusher(
  68. pushConfiguration.RoomId,
  69. pushConfiguration.TerminalId,
  70. pushConfiguration.UserSign,
  71. pushConfiguration.Resolution,
  72. pushConfiguration.PushDataSourceType,
  73. _usScreenWidth,
  74. _usScreenHeight,
  75. UpdateLiveState);
  76. _pusher.PreviewCameraCapured += OnPreviewCameraCapured;
  77. _pusher.LiveNotification += OnLiveNotification;
  78. }
  79. isStartPush = _pusher.Start(pushConfiguration);
  80. }
  81. }
  82. else
  83. {
  84. //直播推流
  85. if (pushConfiguration.ShouldPushData)
  86. {
  87. if (_pusher == null)
  88. {
  89. _pusher = CrossPlatformHelper.Instance.LiveVideoPusherCreator.CreateRtmpPusher(pushConfiguration.TerminalId, _usScreenWidth, _usScreenHeight, UpdateLiveState);
  90. _pusher.PreviewCameraCapured += OnPreviewCameraCapured;
  91. _pusher.LiveNotification += OnLiveNotification;
  92. }
  93. isStartPush = _pusher.Start(pushConfiguration);
  94. }
  95. }
  96. if (isStartPush)
  97. {
  98. LivePushingStatusChecker.Instance.IsPushing = true;
  99. _pushHeartRateKeeper = new PushHeartRateKeeper(pushConfiguration.TerminalId, pushConfiguration.LiveServiceUrl, _clientLeaf);
  100. _pushHeartRateKeeper.Offlined += OnOfflined;
  101. _pushHeartRateKeeper.Start();
  102. }
  103. }
  104. catch (Exception ex)
  105. {
  106. Logger.WriteLineError($"LiveClient start failed ex;{ex}");
  107. _liveState = EnumLiveStates.Idle;
  108. }
  109. }
  110. /// <summary>
  111. /// Start only Preview capture camera
  112. /// </summary>
  113. public async void StartOnlyPreviewImage()
  114. {
  115. await Task.Run(() =>
  116. {
  117. try
  118. {
  119. Logger.WriteLineInfo($"LiveClient start Preview Camera begin");
  120. var liveProtocol = GetLiveProtocol();
  121. var pushConfiguration = CPCombineSmartPushConfiguration.Instance;
  122. pushConfiguration.IsPushOnce = true;
  123. if (liveProtocol == LiveProtocol.RTC)
  124. {
  125. //only show local camera data
  126. if (_pusher == null)
  127. {
  128. _pusher = CrossPlatformHelper.Instance.LiveVideoPusherCreator.CreateRtcPusher(
  129. uint.MinValue,
  130. pushConfiguration.TerminalId,
  131. string.Empty,
  132. pushConfiguration.Resolution,
  133. pushConfiguration.PushDataSourceType,
  134. _usScreenWidth,
  135. _usScreenHeight,
  136. UpdateLiveState);
  137. _pusher.PreviewCameraCapured += OnPreviewCameraCapured;
  138. _pusher.LiveNotification += OnLiveNotification;
  139. }
  140. _pusher.Start(pushConfiguration);
  141. }
  142. else
  143. {
  144. if (_pusher == null)
  145. {
  146. _pusher = CrossPlatformHelper.Instance.LiveVideoPusherCreator.CreateRtmpPusher(pushConfiguration.TerminalId, _usScreenWidth, _usScreenHeight, UpdateLiveState);
  147. _pusher.PreviewCameraCapured += OnPreviewCameraCapured;
  148. _pusher.LiveNotification += OnLiveNotification;
  149. }
  150. _pusher.Start(pushConfiguration);
  151. }
  152. Logger.WriteLineInfo($"LiveClient start Preview Camera end");
  153. }
  154. catch (Exception ex)
  155. {
  156. Logger.WriteLineError($"LiveClient start only preview image failed ex;{ex}");
  157. }
  158. });
  159. }
  160. /// <summary>
  161. /// Stop pusher
  162. /// </summary>
  163. public void Stop()
  164. {
  165. if (_pushHeartRateKeeper != null)
  166. {
  167. _pushHeartRateKeeper.Offlined -= OnOfflined;
  168. _pushHeartRateKeeper.Stop();
  169. _pushHeartRateKeeper = null;
  170. }
  171. CPCombineSmartPushConfiguration.Instance.ShouldPushData = false;
  172. if (_pusher != null)
  173. {
  174. _pusher.Stop();
  175. }
  176. _liveState = EnumLiveStates.Idle;
  177. LivePushingStatusChecker.Instance.IsPushing = false;
  178. Logger.WriteLineInfo("Live Client Stop Push");
  179. }
  180. /// <summary>
  181. /// Set push live status ,but _pusher is exist
  182. /// </summary>
  183. /// <param name="livePushStatus"></param>
  184. public void SetPushStatus(EnumLivePushStatus livePushStatus)
  185. {
  186. if (_pusher != null)
  187. {
  188. CPCombineSmartPushConfiguration.Instance.SetPushStatus(livePushStatus);
  189. Logger.WriteLineInfo($"LiveClient SetPushStatus:{livePushStatus}");
  190. }
  191. }
  192. /// <summary>
  193. /// Set enable preview camera capture data
  194. /// </summary>
  195. /// <param name="isShowPreview"></param>
  196. public void SetPreviewCamera(bool isShowPreview)
  197. {
  198. if (_pusher != null)
  199. {
  200. _pusher.SetPreviewCamera(isShowPreview);
  201. Logger.WriteLineInfo($"LiveClient set preview camera :{isShowPreview}");
  202. }
  203. }
  204. public void ChangeCamera(string cameraId)
  205. {
  206. if (_pusher != null)
  207. {
  208. _pusher.ChangeCamera(cameraId);
  209. Logger.WriteLineInfo($"LiveClient change camera id:{cameraId}");
  210. }
  211. }
  212. public void ChangeMic(string micId)
  213. {
  214. if (_pusher != null)
  215. {
  216. _pusher.ChangeMic(micId);
  217. Logger.WriteLineInfo($"LiveClient change mic id:{micId}");
  218. }
  219. }
  220. /// <summary>
  221. /// Set live mute
  222. /// </summary>
  223. /// <param name="isMute"></param>
  224. public void SetMute(bool isMute)
  225. {
  226. if (_pusher != null)
  227. {
  228. _pusher.SetMute(isMute);
  229. Logger.WriteLineInfo($"LiveClient set mute :{isMute}");
  230. }
  231. }
  232. private async void UpdateLiveState(EnumLiveStates liveState)
  233. {
  234. try
  235. {
  236. await Task.Run(() =>
  237. {
  238. if (_liveState != liveState)
  239. {
  240. try
  241. {
  242. _liveState = liveState;
  243. if (_liveState == EnumLiveStates.TerminalIsPushing)
  244. {
  245. SetPushStatus(EnumLivePushStatus.Pushing);
  246. }
  247. _clientLeaf?.Send(new UpdateTerminalLiveStateRequest()
  248. {
  249. TerminalId = CPCombineSmartPushConfiguration.Instance.TerminalId,
  250. State = (LiveStates)liveState
  251. });
  252. Logger.WriteLineInfo($"LiveClient UpdateLiveState: {liveState},TerminalId:{CPCombineSmartPushConfiguration.Instance.TerminalId}");
  253. }
  254. catch (Exception ex)
  255. {
  256. Logger.WriteLineError($"UpdateLiveState {_liveState} Error:{ex}");
  257. _liveState = EnumLiveStates.Idle;
  258. }
  259. }
  260. });
  261. }
  262. catch (Exception e)
  263. {
  264. Logger.WriteLineInfo($"LiveClient UpdateLiveState failed ex:{e}");
  265. }
  266. }
  267. private void OnPreviewCameraCapured(object sender, CPVideoFrameData data)
  268. {
  269. PreviewCameraCapured?.Invoke(this, data);
  270. }
  271. private void OnOfflined(object sender, EventArgs e)
  272. {
  273. Logger.WriteLineInfo($"LiveClient Push OnOfflined TerminalId:{CPCombineSmartPushConfiguration.Instance.TerminalId}");
  274. //Retry();
  275. }
  276. /// <summary>
  277. /// Disposed when live video feature disposed
  278. /// </summary>
  279. public void Dispose()
  280. {
  281. CPCombineSmartPushConfiguration.Instance.ShowPreviewImage = false;
  282. Stop();
  283. if (_pusher != null)
  284. {
  285. _pusher.PreviewCameraCapured -= OnPreviewCameraCapured;
  286. _pusher.LiveNotification -= OnLiveNotification;
  287. _pusher = null;
  288. }
  289. }
  290. /// <summary>
  291. /// retry live pushing
  292. /// </summary>
  293. public void Retry(LiveProtocol liveProtocol)
  294. {
  295. try
  296. {
  297. if (_clientLeaf != null && _clientLeaf.Online && CPCombineSmartPushConfiguration.Instance.PushStatus == EnumLivePushStatus.Pushing)// network connected and Pushing
  298. {
  299. // check server live room exist
  300. var request = MessagePool.GetMessage<CheckTerminalRoomRequest>();
  301. request.TerminalId = CPCombineSmartPushConfiguration.Instance.TerminalId;
  302. var result = _clientLeaf.Send(request);
  303. var checkTerminalRoomResult = CheckTerminalRoomResult.Convert(result);
  304. if (checkTerminalRoomResult == null || !checkTerminalRoomResult.IsExist)
  305. {
  306. Logger.WriteLineError($"LiveClient Retry, Check server live room no exist");
  307. SetPushStatus(EnumLivePushStatus.Ide);
  308. CPCombineSmartPushConfiguration.Instance.PushUrl = string.Empty;
  309. return;
  310. }
  311. //server live room exist ,do next code
  312. lock (CPCombineSmartPushConfiguration.Instance)
  313. {
  314. Logger.WriteLineInfo($"LiveClient RetryLive begin");
  315. Stop();
  316. CPCombineSmartPushConfiguration.Instance.ShouldPushData = true;
  317. SetPushStatus(EnumLivePushStatus.Ide);
  318. Start(liveProtocol);
  319. Logger.WriteLineInfo($"LiveClient RetryLive end");
  320. }
  321. }
  322. else
  323. {
  324. //Logger.WriteLineInfo($"LiveClient RetryLive:_clientLeafOnline:{_clientLeaf.Online} PushStatus:{CombineSmartPushConfiguration.Instance.PushStatus}");
  325. }
  326. }
  327. catch (Exception ex)
  328. {
  329. Logger.WriteLineError($"LiveClient Retry failed:{ex}");
  330. }
  331. }
  332. /// <summary>
  333. /// Speed live test network and auto select good network to server
  334. /// </summary>
  335. /// <param name="configuration"></param>
  336. /// <returns></returns>
  337. public bool StartSpeedTest()
  338. {
  339. bool result = false;
  340. try
  341. {
  342. var liveProtocol = GetLiveProtocol();
  343. if (liveProtocol == LiveProtocol.RTC) // only test live protocol RTC
  344. {
  345. var pusher = CrossPlatformHelper.Instance.LiveVideoPusherCreator.CreateRtcPusher(uint.MinValue, string.Empty, string.Empty, CPCombineSmartPushConfiguration.Instance.Resolution, CPCombineSmartPushConfiguration.Instance.PushDataSourceType, _usScreenWidth,
  346. _usScreenHeight, null);
  347. if (pusher != null)
  348. {
  349. using (var getLiveRoomSignRequest = MessagePool.GetMessage<GetLiveRoomSignRequest>())
  350. {
  351. var userId = Guid.NewGuid().ToString();
  352. getLiveRoomSignRequest.RoomId = userId;
  353. var getLiveRoomSignResult =
  354. GetLiveRoomSignResult.Convert(_clientLeaf?.Send(getLiveRoomSignRequest));
  355. if (getLiveRoomSignResult != null)
  356. {
  357. var userSign = getLiveRoomSignResult.UserSign;
  358. var appId = (uint)getLiveRoomSignResult.AppId;
  359. CPCombineSmartPushConfiguration.Instance.IsPushOnce = true;
  360. result = pusher.StartSpeedTest(appId, userId, userSign);
  361. }
  362. }
  363. }
  364. }
  365. else
  366. {
  367. // 内网 不需要处理
  368. Logger.WriteLineInfo($"LiveClient StartSpeedTest LiveProtocol:{liveProtocol}, do not speedTest ");
  369. return true;
  370. }
  371. }
  372. catch (Exception ex)
  373. {
  374. Logger.WriteLineError($"LiveClient StartSpeedTest ex:{ex}");
  375. }
  376. return result;
  377. }
  378. /// <summary>
  379. /// Notify server satatus and resolutions
  380. /// </summary>
  381. /// <param name="terminalName">The terminal id.</param>
  382. /// <param name="isCameraLiveEnabled"></param>
  383. /// <param name="serialNumber">device dongledId to fix hainan project, if vbox should ingnore</param>
  384. internal async void NotifyStatusAndResolutions(string terminalId)
  385. {
  386. await Task.Run(() =>
  387. {
  388. try
  389. {
  390. var liveProtocol = GetLiveProtocol();
  391. if (liveProtocol == LiveProtocol.RTC)
  392. {
  393. //获取Terminal 和 Camera Resolution
  394. var pusher = CrossPlatformHelper.Instance.LiveVideoPusherCreator.CreateRtcPusher(uint.MinValue, string.Empty, string.Empty, CPCombineSmartPushConfiguration.Instance.Resolution, CPCombineSmartPushConfiguration.Instance.PushDataSourceType, _usScreenWidth,
  395. _usScreenHeight, null);
  396. if (pusher != null)
  397. {
  398. using (var changeCameraSettingRequest = new ChangeCameraSettingRequest()
  399. {
  400. TerminalId = terminalId,
  401. IsMergeChannel = true,
  402. TerminalWidth = pusher.TerminalWidth,
  403. TerminalHeight = pusher.TerminalHeight,
  404. CameraWidth = pusher.CameraWidth,
  405. CameraHeight = pusher.CameraHeight
  406. })
  407. {
  408. var result = _clientLeaf?.Send(changeCameraSettingRequest);
  409. var resultMsg = ChangeCameraSettingResult.Convert(result);
  410. if (resultMsg != null && resultMsg.IsSuccess)
  411. {
  412. Logger.WriteLineInfo($"UpdateTerminalRelatedInfoRequest success RTC, PushDataSourceType :{CPCombineSmartPushConfiguration.Instance.PushDataSourceType}, " +
  413. $"TerminalSize:{changeCameraSettingRequest.TerminalWidth}*{changeCameraSettingRequest.TerminalHeight} " +
  414. $"CameraSize: {changeCameraSettingRequest.CameraWidth}*{changeCameraSettingRequest.CameraHeight}");
  415. }
  416. else
  417. {
  418. Logger.WriteLineError($"ChangeCameraSettingRequest failed");
  419. }
  420. }
  421. }
  422. }
  423. else
  424. {
  425. var pusher = CrossPlatformHelper.Instance.LiveVideoPusherCreator.CreateRtmpPusher(terminalId, _usScreenWidth, _usScreenHeight, null);
  426. pusher.UpdateResolution(CPCombineSmartPushConfiguration.Instance);
  427. if (pusher != null)
  428. {
  429. using (var changeCameraSettingRequest = new ChangeCameraSettingRequest()
  430. {
  431. TerminalId = terminalId,
  432. IsMergeChannel = true,
  433. TerminalWidth = pusher.TerminalWidth,
  434. TerminalHeight = pusher.TerminalHeight,
  435. CameraWidth = pusher.CameraWidth,
  436. CameraHeight = pusher.CameraHeight
  437. })
  438. {
  439. var result = _clientLeaf?.Send(changeCameraSettingRequest);
  440. var resultMsg = ChangeCameraSettingResult.Convert(result);
  441. if (resultMsg != null && resultMsg.IsSuccess)
  442. {
  443. Logger.WriteLineInfo($"UpdateTerminalRelatedInfoRequest success ,RTMP PushDataSourceType :{CPCombineSmartPushConfiguration.Instance.PushDataSourceType}" +
  444. $"TerminalWidth:{pusher.TerminalWidth} TerminalHeight:{pusher.TerminalHeight} " +
  445. $"CameraWidth: {pusher.CameraWidth} CameraHeight:{pusher.CameraHeight}");
  446. }
  447. else
  448. {
  449. Logger.WriteLineError($"UpdateTerminalRelatedInfoRequest failed");
  450. }
  451. }
  452. }
  453. }
  454. Retry(liveProtocol);
  455. }
  456. catch (Exception e)
  457. {
  458. Logger.WriteLineError($"Get resolutions ex:{e}");
  459. }
  460. });
  461. }
  462. /// <summary>
  463. /// get live Protocol Rtmp or Rtc
  464. /// </summary>
  465. private LiveProtocol GetLiveProtocol()
  466. {
  467. lock (_getLiveProtocolLock)
  468. {
  469. LiveProtocol liveProtocol = LiveProtocol.RTC;
  470. try
  471. {
  472. using (var request = MessagePool.GetMessage<GetLiveProtocolRequest>())
  473. {
  474. var result = _clientLeaf?.Send(request);
  475. var getLiveProtocolResult = GetLiveProtocolResult.Convert(result);
  476. if (getLiveProtocolResult != null)
  477. {
  478. liveProtocol = getLiveProtocolResult.LiveProtocol;
  479. }
  480. }
  481. }
  482. catch (Exception ex)
  483. {
  484. Logger.WriteLineError($"Terminal get live protocol failed {ex}");
  485. }
  486. if (_liveProtocol != liveProtocol)
  487. {
  488. _liveProtocol = liveProtocol;
  489. Logger.WriteLineInfo($"Terminal get live protocol:{liveProtocol}");
  490. LiveProtocolChanged?.Invoke(this, _liveProtocol);
  491. }
  492. return liveProtocol;
  493. }
  494. }
  495. private void OnLiveNotification(object sender, LiveNotificationArgs e)
  496. {
  497. LiveNotification?.Invoke(this, e);
  498. }
  499. }
  500. }