LiveVideo.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. using System;
  2. using System.Collections.Generic;
  3. using Vinno.FIS.TRTCClient.Common.Enum;
  4. using Vinno.IUS.Common.Log;
  5. using Vinno.IUS.Common.Network.Leaf;
  6. using Vinno.IUS.Common.Network.Transfer;
  7. using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
  8. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo;
  9. using Vinno.vCloud.Protocol.Infrastructures;
  10. using Vinno.vCloud.Protocol.Messages.Client;
  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 LiveVideo : ILiveVideo, IDisposable
  16. {
  17. private readonly string _terminalId;
  18. private readonly string _terminalName;
  19. private readonly ClientLeaf _leaf;
  20. private bool _disposed;
  21. private bool _isCameraLiveEnabled;
  22. private bool _isLiveVideoEnabled;
  23. private LiveClient _liveClient;
  24. private bool _cameraMute;
  25. private bool _previewCameraEnabled;
  26. private LiveProtocol _liveProtocol;
  27. private string _cameraId;
  28. private string _micId;
  29. private int _cameraPreviewWidth;
  30. private int _cameraPreviewHeight;
  31. private LiveClient LiveClient
  32. {
  33. get { return _liveClient; }
  34. set
  35. {
  36. if (_liveClient != value)
  37. {
  38. if (_liveClient != null)
  39. {
  40. _liveClient.PreviewCameraCapured -= OnPreviewCameraCaptured;
  41. _liveClient.LiveProtocolChanged -= OnLiveProtocolChanged;
  42. _liveClient.LiveNotification -= OnLiveNotification;
  43. _liveClient.Dispose();
  44. }
  45. _liveClient = value;
  46. if (_liveClient != null)
  47. {
  48. _liveClient.PreviewCameraCapured += OnPreviewCameraCaptured;
  49. _liveClient.LiveProtocolChanged += OnLiveProtocolChanged;
  50. _liveClient.LiveNotification += OnLiveNotification;
  51. _cameraPreviewWidth = 0;
  52. _cameraPreviewHeight = 0;
  53. }
  54. }
  55. }
  56. }
  57. /// <summary>
  58. /// Raised when live changed to protocol
  59. /// </summary>
  60. public event EventHandler<LiveProtocol> LiveProtocolChanged;
  61. /// <summary>
  62. /// The event to notificate the US to show or hide the live video out put window
  63. /// </summary>
  64. public event EventHandler<LiveNotificationArgs> LiveNotification;
  65. /// <summary>
  66. /// Raised when camera Preview Excute
  67. /// </summary>
  68. public event EventHandler<byte[]> PreviewCameraCaptured;
  69. /// <summary>
  70. /// Raised when Camera Preview Image Size Changed
  71. /// </summary>
  72. public event EventHandler<ImageSize> CameraPreviewImageSizeChanged;
  73. /// <summary>
  74. /// Create the liveVideo module.
  75. /// </summary>
  76. /// <param name="terminalId"></param>
  77. /// <param name="terminalName"></param>
  78. /// <param name="leaf"></param>
  79. /// <param name="liveVideoInfo"></param>
  80. public LiveVideo(string terminalId, string terminalName, ClientLeaf leaf, int usScreenWidth, int usScreenHeight)
  81. {
  82. _terminalId = terminalId;
  83. _terminalName = terminalName;
  84. _leaf = leaf;
  85. _liveProtocol = LiveProtocol.RTC;
  86. LiveClient = new LiveClient(_leaf, usScreenWidth, usScreenHeight);
  87. _leaf.MessageArrived += OnMessageArrived;
  88. }
  89. ~LiveVideo()
  90. {
  91. DoDispose();
  92. }
  93. /// <summary>
  94. /// Handle the message from network.
  95. /// </summary>
  96. /// <param name="sender"></param>
  97. /// <param name="e"></param>
  98. private void OnMessageArrived(object sender, Message e)
  99. {
  100. var startLiveNotification = StartLiveNotification.Convert(e);
  101. if (startLiveNotification != null)
  102. {
  103. HandleStatLiveNotification(startLiveNotification);
  104. }
  105. var closeLiveNotification = CloseLiveNotification.Convert(e);
  106. if (closeLiveNotification != null)
  107. {
  108. HandleCloseLiveNotification(closeLiveNotification);
  109. }
  110. }
  111. private void HandleCloseLiveNotification(CloseLiveNotification closeLiveNotification)
  112. {
  113. lock (CPCombineSmartPushConfiguration.Instance)
  114. {
  115. try
  116. {
  117. Logger.WriteLineInfo($"Handle CloseLiveNotification begin");
  118. CPCombineSmartPushConfiguration.Instance.PushUrl = string.Empty;
  119. if (LiveClient != null)
  120. {
  121. LiveClient.SetPushStatus(EnumLivePushStatus.Ide);
  122. LiveClient.Stop();
  123. }
  124. Logger.WriteLineInfo($"Handle CloseLiveNotification end");
  125. }
  126. catch (Exception ex)
  127. {
  128. Logger.WriteLineError($"Handle CloseLiveNotification ex:{ex}");
  129. }
  130. finally
  131. {
  132. CPCombineSmartPushConfiguration.Instance.LiveServiceUrl = string.Empty;
  133. }
  134. }
  135. }
  136. private void HandleStatLiveNotification(StartLiveNotification startLiveNotification)
  137. {
  138. lock (CPCombineSmartPushConfiguration.Instance)
  139. {
  140. try
  141. {
  142. var pushConfiguration = CPCombineSmartPushConfiguration.Instance;
  143. var notificationDetails = $"LiveProtocol:{startLiveNotification.LiveProtocol},TerminalId:{startLiveNotification.TerminalId},IntegerRoomId:{startLiveNotification.IntegerRoomId},UserSign:{startLiveNotification.UserSign},TerminalPushUrl:{startLiveNotification.TerminalPushUrl},LiveServiceUrl:{startLiveNotification.LiveServiceUrl},AppId:{startLiveNotification.AppId}";
  144. Logger.WriteLineInfo($"Handle StartLiveNotification begin,{notificationDetails}");
  145. var terminalUrl = startLiveNotification.TerminalPushUrl;
  146. if (_isLiveVideoEnabled || _isCameraLiveEnabled)
  147. {
  148. if (_isLiveVideoEnabled && !_isCameraLiveEnabled)
  149. {
  150. pushConfiguration.PushDataSourceType = EnumPushDataSourceType.Screen;
  151. pushConfiguration.Mode = EnumPushMode.Screen;
  152. }
  153. else if (!_isLiveVideoEnabled && _isCameraLiveEnabled)
  154. {
  155. pushConfiguration.PushDataSourceType = EnumPushDataSourceType.Camera;
  156. pushConfiguration.Mode = EnumPushMode.Camera;
  157. }
  158. else if (_isLiveVideoEnabled && _isCameraLiveEnabled)
  159. {
  160. pushConfiguration.PushDataSourceType = EnumPushDataSourceType.ScreenAndCamera;
  161. pushConfiguration.Mode = EnumPushMode.CombineTerminal;
  162. }
  163. var protocol = startLiveNotification.LiveProtocol;
  164. if (protocol == LiveProtocol.Rtmp)
  165. {
  166. pushConfiguration.PushUrl = terminalUrl;
  167. }
  168. pushConfiguration.ShouldPushData = true;
  169. pushConfiguration.LiveServiceUrl = startLiveNotification.LiveServiceUrl;
  170. pushConfiguration.AppId = startLiveNotification.AppId;
  171. var roomId = startLiveNotification.IntegerRoomId;
  172. var terminalId = startLiveNotification.TerminalId;
  173. var userSign = startLiveNotification.UserSign;
  174. pushConfiguration.RoomId = (uint)roomId;
  175. pushConfiguration.TerminalId = terminalId;
  176. pushConfiguration.UserSign = userSign;
  177. LiveClient.Start(startLiveNotification.LiveProtocol);
  178. if (_liveProtocol != protocol)
  179. {
  180. _liveProtocol = protocol;
  181. InvokeLiveProtocolChanged(_liveProtocol);
  182. }
  183. }
  184. Logger.WriteLineInfo($"Handle StartLiveNotification end");
  185. }
  186. catch (Exception ex)
  187. {
  188. Logger.WriteLineError($"Handle StartLiveNotification ex:{ex}");
  189. }
  190. }
  191. }
  192. private void OnPreviewCameraCaptured(object sender, CPVideoFrameData data)
  193. {
  194. if (data == null || data.Data == null)
  195. {
  196. return;
  197. }
  198. if (_cameraPreviewHeight != data.Height || _cameraPreviewWidth != data.Width)
  199. {
  200. _cameraPreviewHeight = data.Height;
  201. _cameraPreviewWidth = data.Width;
  202. CameraPreviewImageSizeChanged?.Invoke(this, new ImageSize(_cameraPreviewWidth, _cameraPreviewHeight));
  203. }
  204. PreviewCameraCaptured?.Invoke(this, data.Data);
  205. }
  206. private void DoDispose()
  207. {
  208. if (!_disposed)
  209. {
  210. lock (CPCombineSmartPushConfiguration.Instance)
  211. {
  212. try
  213. {
  214. Logger.WriteLineInfo($"DoDispose:Handle CloseLiveNotification begin");
  215. CPCombineSmartPushConfiguration.Instance.PushUrl = string.Empty;
  216. if (LiveClient != null)
  217. {
  218. LiveClient.SetPushStatus(EnumLivePushStatus.Ide);
  219. LiveClient.Stop();
  220. LiveClient.Dispose();
  221. LiveClient = null;
  222. }
  223. Logger.WriteLineInfo($"DoDispose:Handle CloseLiveNotification end");
  224. }
  225. catch (Exception ex)
  226. {
  227. Logger.WriteLineError($"Handle CloseLiveNotification ex:{ex}");
  228. }
  229. finally
  230. {
  231. CPCombineSmartPushConfiguration.Instance.LiveServiceUrl = string.Empty;
  232. }
  233. }
  234. _leaf.MessageArrived -= OnMessageArrived;
  235. _disposed = true;
  236. }
  237. }
  238. public void Dispose()
  239. {
  240. DoDispose();
  241. GC.SuppressFinalize(this);
  242. }
  243. private void NotifyLiveEnabledChanged()
  244. {
  245. try
  246. {
  247. using (var changeTerminalSupportLiveRequest = new ChangeTerminalLiveEnabledRequest
  248. { TerminalLiveEnabled = _isLiveVideoEnabled, TerminalName = _terminalName, CameraLiveEnabled = _isCameraLiveEnabled })
  249. {
  250. var changeTerminalSupportLiveServerResult = _leaf.Send(changeTerminalSupportLiveRequest);
  251. var changeTerminalSupportLiveResult = ResultMessage.Convert(changeTerminalSupportLiveServerResult);
  252. if (changeTerminalSupportLiveResult != CCR.OK)
  253. {
  254. Logger.WriteLineError($"On_isCameraLiveEnabledChanged notification failed, _isCameraLiveEnabled:{_isCameraLiveEnabled}");
  255. }
  256. }
  257. }
  258. catch (Exception e)
  259. {
  260. Logger.WriteLineError($"On_isCameraLiveEnabledChanged notification, _isCameraLiveEnabled:{_isCameraLiveEnabled}, ex: {e}");
  261. }
  262. }
  263. /// <summary>
  264. /// Change CameraSettings to upload server hardware info
  265. /// </summary>
  266. /// <param name="enableCameraLive"></param>
  267. /// <param name="cameraId"></param>
  268. /// <param name="micId"></param>
  269. /// <param name="showPreviewImage"></param>
  270. /// <param name="enableLiveVideo"></param>
  271. /// <param name="isMute"></param>
  272. public void ChangeCameraSettings(bool enableCameraLive, string cameraId, string micId, bool showPreviewImage,
  273. bool enableLiveVideo, bool isMute)
  274. {
  275. lock (CPCombineSmartPushConfiguration.Instance)
  276. {
  277. var pushConfiguration = CPCombineSmartPushConfiguration.Instance;
  278. var needNotifyToServer = false;
  279. var needNotifyLiveStatus = false;
  280. if (enableCameraLive && enableLiveVideo)
  281. {
  282. pushConfiguration.Mode = EnumPushMode.CombineTerminal;
  283. pushConfiguration.PushDataSourceType = EnumPushDataSourceType.ScreenAndCamera;
  284. }
  285. else if (enableLiveVideo)
  286. {
  287. pushConfiguration.Mode = EnumPushMode.Screen;
  288. pushConfiguration.PushDataSourceType = EnumPushDataSourceType.Screen;
  289. }
  290. else
  291. {
  292. pushConfiguration.Mode = EnumPushMode.Camera;
  293. pushConfiguration.PushDataSourceType = EnumPushDataSourceType.Camera;
  294. }
  295. pushConfiguration.VideoDevicePath = cameraId;
  296. if (!enableCameraLive)
  297. {
  298. cameraId = null;
  299. showPreviewImage = false;
  300. }
  301. pushConfiguration.AudioDevicePath = micId;
  302. //是否停止推送
  303. bool stopliveClient = false;
  304. if (_isLiveVideoEnabled != enableLiveVideo && !enableLiveVideo)
  305. {
  306. stopliveClient = true;
  307. }
  308. else if (_isCameraLiveEnabled != enableCameraLive && enableCameraLive && enableLiveVideo) //单路切换双路
  309. {
  310. stopliveClient = true;
  311. Logger.WriteLineInfo($"LiveVideo Screen to ScreenAndCamera");
  312. }
  313. else if (_isLiveVideoEnabled != enableLiveVideo && enableCameraLive && enableLiveVideo) //单路切换双路
  314. {
  315. stopliveClient = true;
  316. Logger.WriteLineInfo($"LiveVideo Camera to ScreenAndCamera");
  317. }
  318. else if (_isCameraLiveEnabled != enableCameraLive && !enableCameraLive && enableLiveVideo) //双路切换单路
  319. {
  320. stopliveClient = true;
  321. Logger.WriteLineInfo($"LiveVideo ScreenAndCamera to Screen");
  322. }
  323. if (_cameraId != pushConfiguration.VideoDevicePath && pushConfiguration.ShouldPushData)//切换摄像头
  324. {
  325. stopliveClient = true;
  326. Logger.WriteLineInfo($"LiveVideo Change Camera");
  327. }
  328. if (_micId != pushConfiguration.AudioDevicePath && pushConfiguration.ShouldPushData)//切换麦克风
  329. {
  330. stopliveClient = true;
  331. Logger.WriteLineInfo($"LiveVideo Change Mic");
  332. }
  333. if (stopliveClient)
  334. {
  335. pushConfiguration.ShowPreviewImage = false;
  336. pushConfiguration.IsMute = true;
  337. LiveClient?.Stop(); // stop and set default
  338. LiveClient?.SetPushStatus(EnumLivePushStatus.Ide);
  339. }
  340. if (!enableCameraLive)//未开摄像头默认静音 true
  341. {
  342. isMute = true;
  343. }
  344. var cameraMute = isMute; //isMute true 静音
  345. pushConfiguration.IsMute = cameraMute;
  346. if (!cameraMute)
  347. {
  348. pushConfiguration.AudioMode = EnumAudioMode.Mic;
  349. }
  350. if (enableCameraLive && enableLiveVideo && showPreviewImage)
  351. {
  352. pushConfiguration.ShowPreviewImage = true;
  353. LiveVideoStatusChecker.Instance.IsPreviewing = true;
  354. }
  355. else
  356. {
  357. pushConfiguration.ShowPreviewImage = false;
  358. LiveVideoStatusChecker.Instance.IsPreviewing = false;
  359. }
  360. if (_previewCameraEnabled != pushConfiguration.ShowPreviewImage)//是否预览
  361. {
  362. _previewCameraEnabled = pushConfiguration.ShowPreviewImage;
  363. if (!pushConfiguration.ShouldPushData)//不在推流
  364. {
  365. if (_previewCameraEnabled)//只是本地预览
  366. {
  367. LiveClient?.StartOnlyPreviewImage();
  368. }
  369. else
  370. {
  371. if (!stopliveClient)
  372. {
  373. LiveClient?.SetPreviewCamera(_previewCameraEnabled);
  374. }
  375. }
  376. }
  377. else
  378. {
  379. if (!stopliveClient)
  380. {
  381. LiveClient?.SetPreviewCamera(_previewCameraEnabled);
  382. }
  383. }
  384. }
  385. if (_cameraId != pushConfiguration.VideoDevicePath)//切换摄像头
  386. {
  387. _cameraId = pushConfiguration.VideoDevicePath;
  388. needNotifyToServer = true;
  389. if (!stopliveClient)
  390. {
  391. LiveClient?.ChangeCamera(_cameraId);
  392. }
  393. Logger.WriteLineInfo($"Change Camera Setting: Camera Id Changed:{_cameraId}");
  394. }
  395. if (_micId != pushConfiguration.AudioDevicePath)
  396. {
  397. _micId = pushConfiguration.AudioDevicePath;
  398. if (!stopliveClient)
  399. {
  400. LiveClient?.ChangeMic(_micId);
  401. }
  402. Logger.WriteLineInfo($"Change Camera Setting: Audio Id Changed:{_micId}");
  403. }
  404. if (_cameraMute != cameraMute)
  405. {
  406. _cameraMute = cameraMute;
  407. _liveClient?.SetMute(_cameraMute);
  408. Logger.WriteLineInfo($"Change Camera Setting: Camera Mute Changed:{_cameraMute}");
  409. }
  410. if (_isLiveVideoEnabled != enableLiveVideo)
  411. {
  412. needNotifyToServer = true;
  413. needNotifyLiveStatus = true;
  414. _isLiveVideoEnabled = enableLiveVideo;
  415. Logger.WriteLineInfo($"Change Camera Setting: Live Video Enabled Changed:{_isLiveVideoEnabled}");
  416. }
  417. if (_isCameraLiveEnabled != enableCameraLive)
  418. {
  419. needNotifyLiveStatus = true;
  420. needNotifyToServer = true;
  421. _isCameraLiveEnabled = enableCameraLive;
  422. Logger.WriteLineInfo($"Change Camera Setting: Camera Live Enabled Changed:{_isCameraLiveEnabled}");
  423. }
  424. if (needNotifyLiveStatus)
  425. {
  426. NotifyLiveEnabledChanged();
  427. }
  428. if (needNotifyToServer && _isLiveVideoEnabled)
  429. {
  430. _liveClient?.NotifyStatusAndResolutions(_terminalId);
  431. }
  432. }
  433. }
  434. /// <summary>
  435. /// Speed live test network and auto select good network to server
  436. /// </summary>
  437. /// <returns></returns>
  438. public bool StartSpeedTest()
  439. {
  440. bool speedTest = false;
  441. if (_liveClient != null)
  442. {
  443. speedTest = _liveClient.StartSpeedTest();
  444. }
  445. return speedTest;
  446. }
  447. private void InvokeLiveProtocolChanged(LiveProtocol liveProtocol)
  448. {
  449. LiveProtocolChanged?.Invoke(this, liveProtocol);
  450. }
  451. private void OnLiveProtocolChanged(object sender, LiveProtocol liveProtocol)
  452. {
  453. _liveProtocol = liveProtocol;
  454. InvokeLiveProtocolChanged(_liveProtocol);
  455. }
  456. private void OnLiveNotification(object sender, LiveNotificationArgs e)
  457. {
  458. LiveNotification?.Invoke(this, e);
  459. }
  460. public void ChangeCameraSettingsForSonopost(bool showPreviewImage, EnumLiveChannelCategory previewLiveChannel, IEnumerable<CPVideoDeviceInfo> infos, RainbowImageDetectConfig rainbowImageCheckConfig, string micId, bool isMute)
  461. {
  462. throw new NotImplementedException();
  463. }
  464. /// <summary>
  465. /// 获取品牌列表
  466. /// </summary>
  467. /// <returns></returns>
  468. public List<string> GetBrandList()
  469. {
  470. return new List<string>();
  471. }
  472. /// <summary>
  473. /// 获取型号列表
  474. /// </summary>
  475. /// <param name="brand"></param>
  476. /// <param name="model"></param>
  477. /// <returns></returns>
  478. public List<string> GetModelList(string brand)
  479. {
  480. return new List<string>();
  481. }
  482. /// <summary>
  483. /// 获取推荐分辨率
  484. /// </summary>
  485. /// <param name="brand"></param>
  486. /// <param name="model"></param>
  487. /// <returns></returns>
  488. public DeviceRecommandResolution GetRecommandResolution(string brand, string model)
  489. {
  490. return null;
  491. }
  492. }
  493. }