RtcMultiPusherV2.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. using Com.Tencent.Trtc;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading;
  7. using Vinno.FIS.TRTCClient.Common;
  8. using Vinno.FIS.TRTCClient.Common.Enum;
  9. using Vinno.FIS.TRTCClient.Common.FileTransfer;
  10. using Vinno.FIS.TRTCClient.Common.Log;
  11. using Vinno.FIS.TRTCClient.Common.Models;
  12. using Vinno.vCloud.FIS.CrossPlatform.Common;
  13. using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
  14. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo;
  15. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo.Interface;
  16. namespace Vinno.vCloud.FIS.CrossPlatform.Android.LiveVideo.RTC
  17. {
  18. public class RtcMultiPusherV2 : PusherBase, ILiveVideoPusherV2
  19. {
  20. private readonly ConcurrentDictionary<EnumLiveChannelCategory, TRTCClientV2> _pushers;
  21. private readonly ConcurrentDictionary<EnumLiveChannelCategory, EnumLiveStates> _liveStatus;
  22. private TRTCPusher _pusher;
  23. private IImageSenderV2 _imageSender;
  24. private int _videoResolution;
  25. public event EventHandler<ChannelStateEventArgsV2> ChannelStateChanged;
  26. public RtcMultiPusherV2()
  27. {
  28. _pushers = new ConcurrentDictionary<EnumLiveChannelCategory, TRTCClientV2>();
  29. _liveStatus = new ConcurrentDictionary<EnumLiveChannelCategory, EnumLiveStates>();
  30. }
  31. public bool StartPusher(IExtendedData pushParams, IEnumerable<CPVideoDeviceOutputInfo> deviceInfos)
  32. {
  33. var rtcParams = pushParams as RtcExtendedData;
  34. if (rtcParams == null || rtcParams.AppId == 0)
  35. {
  36. CrossPlatformHelper.Instance.LogWriter?.WriteLineError("RTC Multi Extended Data Is Null!");
  37. return false;
  38. }
  39. var userInfos = rtcParams.UserInfos;
  40. bool hasMain = userInfos.Any(x => x.Category == EnumLiveChannelCategory.Main);
  41. for (int i = 0; i < userInfos.Count(); i++)
  42. {
  43. var deviceInfo = deviceInfos.FirstOrDefault(c => c.Category == userInfos[i].Category);
  44. if (deviceInfo != null)
  45. {
  46. if (deviceInfo.Category == EnumLiveChannelCategory.Main)
  47. {
  48. _videoResolution = GetVideoResolution(userInfos[i].Width, userInfos[i].Height);
  49. _pusher = new TRTCPusher();
  50. _pusher.FirstFrameSend += OnFirstFrameSend;
  51. if (CommonParameter.Instance.IsSonopost)
  52. {
  53. _pusher.EnterRoom(userInfos[i].UserId, rtcParams.RoomId, userInfos[i].UserSign, rtcParams.AppId, _videoResolution, userInfos[i].VideoFps, userInfos[i].VideoBitrate, userInfos[i].MinVideoBitrate, rtcParams.IsMute, rtcParams.MicDeviceId);
  54. if (_videoResolution == TRTCCloudDef.TrtcVideoResolution640480)
  55. {
  56. _imageSender = new Camera640X480SenderV2(deviceInfo.VideoDeviceId, userInfos[i].Width, userInfos[i].Height, deviceInfo.Category, false, false);
  57. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Multi PusherV2:Camera640X480SenderV2");
  58. }
  59. else if (_videoResolution == TRTCCloudDef.TrtcVideoResolution960540)
  60. {
  61. _imageSender = new Camera960X540SenderV2(deviceInfo.VideoDeviceId, userInfos[i].Width, userInfos[i].Height, deviceInfo.Category, false, false);
  62. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Multi PusherV2:Camera960X540SenderV2");
  63. }
  64. else if (_videoResolution == TRTCCloudDef.TrtcVideoResolution960720)
  65. {
  66. _imageSender = new Camera960X720SenderV2(deviceInfo.VideoDeviceId, userInfos[i].Width, userInfos[i].Height, deviceInfo.Category, false, false);
  67. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Multi PusherV2:Camera960X720SenderV2");
  68. }
  69. else if (_videoResolution == TRTCCloudDef.TrtcVideoResolution1280720)
  70. {
  71. _imageSender = new Camera1280X720SenderV2(deviceInfo.VideoDeviceId, userInfos[i].Width, userInfos[i].Height, deviceInfo.Category, false, false);
  72. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Multi PusherV2:Camera1280X720SenderV2");
  73. }
  74. else
  75. {
  76. _imageSender = new Camera1920X1080SenderV2(deviceInfo.VideoDeviceId, userInfos[i].Width, userInfos[i].Height, deviceInfo.Category, false, false);
  77. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Multi PusherV2:Camera1920X1080SenderV2");
  78. }
  79. }
  80. else
  81. {
  82. _pusher.EnterRoom(userInfos[i].UserId, rtcParams.RoomId, userInfos[i].UserSign, rtcParams.AppId, _videoResolution, userInfos[i].VideoFps, userInfos[i].VideoBitrate, userInfos[i].MinVideoBitrate, pushParams.IsMute, pushParams.MicDeviceId);
  83. if (_videoResolution == TRTCCloudDef.TrtcVideoResolution640480)
  84. {
  85. _imageSender = new Terminal640X480SenderV2(deviceInfo.VideoDeviceId, userInfos[i].Width, userInfos[i].Height);
  86. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Multi PusherV2:Terminal1280X720SenderV2");
  87. }
  88. else if (_videoResolution == TRTCCloudDef.TrtcVideoResolution960540)
  89. {
  90. _imageSender = new Terminal960X540SenderV2(deviceInfo.VideoDeviceId, userInfos[i].Width, userInfos[i].Height);
  91. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Multi PusherV2:Terminal1280X720SenderV2");
  92. }
  93. else if (_videoResolution == TRTCCloudDef.TrtcVideoResolution960720)
  94. {
  95. _imageSender = new Terminal960X720SenderV2(deviceInfo.VideoDeviceId, userInfos[i].Width, userInfos[i].Height);
  96. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Multi PusherV2:Terminal1280X720SenderV2");
  97. }
  98. else if (_videoResolution == TRTCCloudDef.TrtcVideoResolution1280720)
  99. {
  100. _imageSender = new Terminal1280X720SenderV2(deviceInfo.VideoDeviceId, userInfos[i].Width, userInfos[i].Height);
  101. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Multi PusherV2:Terminal1280X720SenderV2");
  102. }
  103. else
  104. {
  105. _imageSender = new Terminal1920X1080SenderV2(deviceInfo.VideoDeviceId, userInfos[i].Width, userInfos[i].Height);
  106. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Multi PusherV2:Terminal1920X1080SenderV2");
  107. }
  108. }
  109. _imageSender.VideoFrameDataReceived += OnVideoFrameDataReceived;
  110. _imageSender.PreviewImageFrameDataReceived += OnImageFrameReceived;
  111. _imageSender.Start();
  112. }
  113. else
  114. {
  115. var device = (CPVideoDeviceOutputInfo)deviceInfo.Clone();
  116. bool isMute = true;
  117. string micId = "";
  118. if (i == 0 && !hasMain)//没有主屏的情况
  119. {
  120. isMute = rtcParams.IsMute;
  121. micId = rtcParams.MicDeviceId;
  122. }
  123. var pusher = new TRTCClientV2(device, userInfos[i].Width, userInfos[i].Height, rtcParams.AppId, userInfos[i].UserId, rtcParams.RoomId, userInfos[i].UserSign, userInfos[i].VideoFps, userInfos[i].VideoBitrate, userInfos[i].MinVideoBitrate, isMute, micId);
  124. if (_pushers.TryAdd(userInfos[i].Category, pusher))
  125. {
  126. pusher.ImageFrameReceived += OnImageFrameReceived;
  127. pusher.FirstFrameReceived += OnFirstFrameReceived;
  128. pusher.Start();
  129. }
  130. else
  131. {
  132. pusher.Dispose();
  133. }
  134. }
  135. }
  136. else
  137. {
  138. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Rtc Multi PusherV2 Can not find {userInfos[i].Category} User Info!");
  139. }
  140. }
  141. return true;
  142. }
  143. private void OnVideoFrameDataReceived(object sender, CPVideoFrameData e)
  144. {
  145. _pusher?.SendData(e.Width, e.Height, e.Data);
  146. }
  147. private int GetVideoResolution(int width, int height)
  148. {
  149. if (width <= 640 && height <= 480)
  150. {
  151. return TRTCCloudDef.TrtcVideoResolution640480;
  152. }
  153. else if (width <= 960 && height <= 544)
  154. {
  155. return TRTCCloudDef.TrtcVideoResolution960540;
  156. }
  157. else if (width <= 960 && height <= 720)
  158. {
  159. return TRTCCloudDef.TrtcVideoResolution960720;
  160. }
  161. else if (width <= 1280 && height <= 720)
  162. {
  163. return TRTCCloudDef.TrtcVideoResolution1280720;
  164. }
  165. else
  166. {
  167. return TRTCCloudDef.TrtcVideoResolution19201080;
  168. }
  169. }
  170. public bool StopPusher()
  171. {
  172. try
  173. {
  174. if (_pusher != null)
  175. {
  176. _pusher.FirstFrameSend -= OnFirstFrameSend;
  177. _pusher.ExitRoom();
  178. _pusher = null;
  179. }
  180. if (_imageSender != null)
  181. {
  182. _imageSender.VideoFrameDataReceived -= OnVideoFrameDataReceived;
  183. _imageSender.PreviewImageFrameDataReceived -= OnImageFrameReceived;
  184. _imageSender.Stop();
  185. _imageSender = null;
  186. }
  187. foreach (var pusher in _pushers.Values)
  188. {
  189. pusher.ImageFrameReceived -= OnImageFrameReceived;
  190. pusher.FirstFrameReceived -= OnFirstFrameReceived;
  191. pusher.Stop();
  192. }
  193. _pushers.Clear();
  194. _liveStatus.Clear();
  195. return true;
  196. }
  197. catch (Exception e)
  198. {
  199. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Rtc Multi PusherV2 Stop Pusher Error:{e}");
  200. }
  201. return false;
  202. }
  203. public void SetMute(bool isMute)
  204. {
  205. if (_pusher != null)
  206. {
  207. _pusher.SetMute(isMute);
  208. }
  209. else
  210. {
  211. if (_pushers.Count > 0)
  212. {
  213. _pushers[0].SetMute(isMute);
  214. }
  215. }
  216. }
  217. public void SwitchMic(string micId)
  218. {
  219. if (_pusher != null)
  220. {
  221. _pusher?.SwitchMic(micId);
  222. }
  223. else
  224. {
  225. if (_pushers.Count > 0)
  226. {
  227. _pushers[0].SwitchMic(micId);
  228. }
  229. }
  230. }
  231. public override void StartPreview(EnumLiveChannelCategory category)
  232. {
  233. if (_pushers.TryGetValue(category, out var client))
  234. {
  235. client.StartPreview();
  236. }
  237. base.StartPreview(category);
  238. }
  239. public override void StopPreview()
  240. {
  241. foreach (var item in _pushers.Values)
  242. {
  243. item.StopPreview();
  244. }
  245. base.StopPreview();
  246. }
  247. private void OnImageFrameReceived(object sender, ImageFrameData e)
  248. {
  249. var category = (EnumLiveChannelCategory)sender;
  250. CopyPreviewData(category, e);
  251. }
  252. private void OnFirstFrameReceived(object sender, EnumLiveChannelCategory e)
  253. {
  254. if (!_liveStatus.TryGetValue(e, out var states))
  255. {
  256. if (_liveStatus.TryAdd(e, EnumLiveStates.TerminalIsPushing))
  257. {
  258. ChannelStateChanged?.Invoke(this, new ChannelStateEventArgsV2(e, EnumDeviceLiveState.Pushing));
  259. }
  260. }
  261. }
  262. private void OnFirstFrameSend(object sender, EventArgs e)
  263. {
  264. if (_liveStatus.TryAdd(EnumLiveChannelCategory.Main, EnumLiveStates.TerminalIsPushing))
  265. {
  266. ChannelStateChanged?.Invoke(this, new ChannelStateEventArgsV2(EnumLiveChannelCategory.Main, EnumDeviceLiveState.Pushing));
  267. }
  268. }
  269. public bool StartSpeedTest(uint appId, string userId, string userSign)
  270. {
  271. TRTCPusher pusher = null;
  272. try
  273. {
  274. pusher = new TRTCPusher();
  275. return pusher.StartSpeedTest((int)appId, userId, userSign);
  276. }
  277. catch (Exception ex)
  278. {
  279. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Rtc Multi PusherV2 StartSpeedTest Error:{ex}");
  280. return false;
  281. }
  282. finally
  283. {
  284. if (pusher != null)
  285. {
  286. pusher.StopSpeedTest();
  287. pusher = null;
  288. }
  289. }
  290. }
  291. public override void DoDispose()
  292. {
  293. StopPusher();
  294. base.DoDispose();
  295. }
  296. private class TRTCClientV2 : IDisposable
  297. {
  298. private readonly object _previewLocker = new object();
  299. private readonly ManualResetEvent _handlingImageEvent = new ManualResetEvent(true);
  300. private TRTCRoomInfo _roomInfo;
  301. private CPVideoDeviceOutputInfo _deviceInfo;
  302. private int _trtcVideoResolution;
  303. private IImageSenderV2 _imageSender;
  304. private FileTransferWriter _localFileWriter;
  305. private bool _isPreviewing;
  306. private bool _hasInformImageSize;
  307. public EnumLiveChannelCategory Category => _deviceInfo.Category;
  308. public event EventHandler<ImageFrameData> ImageFrameReceived;
  309. public event EventHandler<EnumLiveChannelCategory> FirstFrameReceived;
  310. public TRTCClientV2(CPVideoDeviceOutputInfo deviceInfo, int outputWidth, int outputHeight, int appid, string userId, int roomId, string userSign, int fps, int bitrate, int minBitrate, bool isMute, string micId)
  311. {
  312. _deviceInfo = deviceInfo;
  313. GetResolution(outputWidth, outputHeight);
  314. int finalOutputWidth = 640;
  315. int finalOutputHeight = 480;
  316. switch (_trtcVideoResolution)
  317. {
  318. case TRTCCloudDef.TrtcVideoResolution640480:
  319. finalOutputWidth = 640;
  320. finalOutputHeight = 480;
  321. break;
  322. case TRTCCloudDef.TrtcVideoResolution960540:
  323. finalOutputWidth = 960;
  324. finalOutputHeight = 544;
  325. break;
  326. case TRTCCloudDef.TrtcVideoResolution960720:
  327. finalOutputWidth = 960;
  328. finalOutputHeight = 720;
  329. break;
  330. case TRTCCloudDef.TrtcVideoResolution1280720:
  331. finalOutputWidth = 1280;
  332. finalOutputHeight = 720;
  333. break;
  334. case TRTCCloudDef.TrtcVideoResolution19201080:
  335. finalOutputWidth = 1920;
  336. finalOutputHeight = 1080;
  337. break;
  338. }
  339. _roomInfo = new TRTCRoomInfo
  340. {
  341. AppId = (uint)appid,
  342. UserSig = userSign,
  343. UserId = userId,
  344. RoomId = (uint)roomId,
  345. OutputWidth = finalOutputWidth,
  346. OutputHeight = finalOutputHeight,
  347. VideoFps = (uint)fps,
  348. VideoBitrate = (uint)bitrate,
  349. MinVideoBitrate = (uint)minBitrate,
  350. MicDeviceId = micId,
  351. IsMute = isMute,
  352. IsOldServerMode = false,
  353. IsLiveMode = true
  354. };
  355. switch (deviceInfo.Category)
  356. {
  357. case EnumLiveChannelCategory.Main:
  358. _roomInfo.Category = EnumLiveChannelCategory.Main;
  359. break;
  360. case EnumLiveChannelCategory.Auxiliary1:
  361. _roomInfo.Category = EnumLiveChannelCategory.Auxiliary1;
  362. break;
  363. case EnumLiveChannelCategory.Auxiliary2:
  364. _roomInfo.Category = EnumLiveChannelCategory.Auxiliary2;
  365. break;
  366. case EnumLiveChannelCategory.Auxiliary3:
  367. _roomInfo.Category = EnumLiveChannelCategory.Auxiliary3;
  368. break;
  369. }
  370. }
  371. private void GetResolution(int width, int height)
  372. {
  373. if (width <= 640 && height <= 480)
  374. {
  375. _trtcVideoResolution = TRTCCloudDef.TrtcVideoResolution640480;
  376. }
  377. else if (width <= 960 && height <= 544)
  378. {
  379. _trtcVideoResolution = TRTCCloudDef.TrtcVideoResolution960540;
  380. }
  381. else if (width <= 960 && height <= 720)
  382. {
  383. _trtcVideoResolution = TRTCCloudDef.TrtcVideoResolution960720;
  384. }
  385. else if (width <= 1280 && height <= 720)
  386. {
  387. _trtcVideoResolution = TRTCCloudDef.TrtcVideoResolution1280720;
  388. }
  389. else
  390. {
  391. _trtcVideoResolution = TRTCCloudDef.TrtcVideoResolution19201080;
  392. }
  393. }
  394. public void Start()
  395. {
  396. FISAndroid.FileReaderFromTRTCLiveVideoToCommon.DataReceived += OnMessageDataReceived;
  397. SendEnterRoomMessage();
  398. StartImageSender();
  399. StartLocalFileWriter();
  400. }
  401. private void SendEnterRoomMessage()
  402. {
  403. CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"RtcMultiPusherV2 MessageClient Send EnterRoom,{_roomInfo}");
  404. var trtcMessage = new TRTCMessage(EnumMessageType.EnterRoom, _roomInfo.ToBytes());
  405. FISAndroid.FileWrtierFromCommonToTRTCLiveVideo.WriteMessageToFileContinuous(trtcMessage.ToBytes());
  406. }
  407. private void StartImageSender()
  408. {
  409. _imageSender = GetCameraImageSenderV2(_deviceInfo.VideoDeviceId, _roomInfo.OutputWidth, _roomInfo.OutputHeight, _roomInfo.Category, true);
  410. _imageSender.VideoFrameDataReceived += OnVideoFrameDataReceived;
  411. _imageSender.PreviewImageFrameDataReceived += OnPreviewImageFrameDataReceived;
  412. _imageSender.Start();
  413. }
  414. private void StopImageSender()
  415. {
  416. if (_imageSender != null)
  417. {
  418. _imageSender.VideoFrameDataReceived -= OnVideoFrameDataReceived;
  419. _imageSender.PreviewImageFrameDataReceived -= OnPreviewImageFrameDataReceived;
  420. _imageSender.Stop();
  421. }
  422. }
  423. private void StartLocalFileWriter()
  424. {
  425. if (_localFileWriter == null)
  426. {
  427. _localFileWriter = new FileTransferWriter($"{FISTRTCConsts.ImageTransferFolderForTRTCConsultationLocalVideo}_{_roomInfo.UserId}", FISTRTCConsts.DataTransferFolderForTRTCLiveVideo, FISAndroid.FISFolderPath, null);
  428. _localFileWriter.LogMsgThrow += OnLogMsgThrow;
  429. CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"Start Local Video File Writer :{_localFileWriter.Name}");
  430. }
  431. }
  432. private void OnLogMsgThrow(object sender, LogEventArgs e)
  433. {
  434. if (e == null)
  435. {
  436. return;
  437. }
  438. switch (e.LogType)
  439. {
  440. case DeviceLogCategory.Error:
  441. CrossPlatformHelper.Instance.LogWriter?.WriteLineError(e.Msg);
  442. break;
  443. case DeviceLogCategory.Warn:
  444. CrossPlatformHelper.Instance.LogWriter?.WriteLineWarn(e.Msg);
  445. break;
  446. case DeviceLogCategory.Verb:
  447. CrossPlatformHelper.Instance.LogWriter?.WriteLineVerbose(e.Msg);
  448. break;
  449. case DeviceLogCategory.Info:
  450. default:
  451. CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo(e.Msg);
  452. break;
  453. }
  454. }
  455. public void Stop()
  456. {
  457. CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"RtcMultiPusherV2 TRTCClient Stop");
  458. FISAndroid.FileReaderFromTRTCLiveVideoToCommon.DataReceived -= OnMessageDataReceived;
  459. StopImageSender();
  460. SendExitMessage();
  461. StopLocalFileWriter();
  462. }
  463. private CameraImageSenderV2 GetCameraImageSenderV2(string id, int width, int height, EnumLiveChannelCategory category, bool isFillMode)
  464. {
  465. if (width <= 640 && height <= 480)
  466. {
  467. return new Camera640X480SenderV2(id, width, height, category, isFillMode, true);
  468. }
  469. else if (width <= 960 && height <= 544)
  470. {
  471. return new Camera960X540SenderV2(id, width, height, category, isFillMode, true); ;
  472. }
  473. else if (width <= 960 && height <= 720)
  474. {
  475. return new Camera960X720SenderV2(id, width, height, category, isFillMode, true); ;
  476. }
  477. else if (width <= 1280 && height <= 720)
  478. {
  479. return new Camera1280X720SenderV2(id, width, height, category, isFillMode, true);
  480. }
  481. else
  482. {
  483. return new Camera1920X1080SenderV2(id, width, height, category, isFillMode, true);
  484. }
  485. }
  486. private void SendExitMessage()
  487. {
  488. try
  489. {
  490. CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"RtcMultiPusherV2 MessageClient Send Exit");
  491. var trtcMessage = new TRTCMessage(EnumMessageType.ExitRoom, new byte[0]);
  492. FISAndroid.FileWrtierFromCommonToTRTCLiveVideo.WriteMessageToFileContinuous(trtcMessage.ToBytes());
  493. Thread.Sleep(50);
  494. }
  495. catch (Exception ex)
  496. {
  497. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RtcMultiPusherV2 MessageClient SendExitMessage error: {ex}");
  498. }
  499. finally
  500. {
  501. FISAndroid.FileWrtierFromCommonToTRTCLiveVideo.ClearReserveStorageFolderPath();
  502. }
  503. }
  504. private void OnMessageDataReceived(object sender, byte[] e)
  505. {
  506. if (e == null)
  507. {
  508. return;
  509. }
  510. var trtcMessage = TRTCMessage.FromBytes(e);
  511. CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"RtcMultiPusherV2 Message Pipe Server Receive PipeMessage{trtcMessage?.MessageType}");
  512. if (trtcMessage.MessageType == EnumMessageType.FirstFrameReceived)
  513. {
  514. FirstFrameReceived?.Invoke(this, Category);
  515. }
  516. }
  517. private void StopLocalFileWriter()
  518. {
  519. if (_localFileWriter != null)
  520. {
  521. _localFileWriter.Dispose();
  522. _localFileWriter.LogMsgThrow -= OnLogMsgThrow;
  523. _localFileWriter = null;
  524. }
  525. }
  526. private void OnVideoFrameDataReceived(object sender, CPVideoFrameData e)
  527. {
  528. if (!_hasInformImageSize)
  529. {
  530. var imageSize = new TRTCImageSizeData(e.Width, e.Height);
  531. var imageSizeMessage = new TRTCMessage(EnumMessageType.ImageSizeChanged, imageSize.ToBytes());
  532. FISAndroid.FileWrtierFromCommonToTRTCLiveVideo.WriteMessageToFileContinuous(imageSizeMessage.ToBytes());
  533. _hasInformImageSize = true;
  534. }
  535. _localFileWriter?.WriteToFileContinuous(e.Data);
  536. }
  537. private void OnPreviewImageFrameDataReceived(object sender, ImageFrameData e)
  538. {
  539. try
  540. {
  541. _handlingImageEvent.Reset();
  542. if (_isPreviewing)
  543. {
  544. ImageFrameReceived?.Invoke(Category, e);
  545. }
  546. }
  547. catch (Exception ex)
  548. {
  549. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RtcMultiPusherV2 OnPreviewImageFrameDataReceived error: {ex}");
  550. }
  551. finally
  552. {
  553. _handlingImageEvent.Set();
  554. }
  555. }
  556. public void StartPreview()
  557. {
  558. lock (_previewLocker)
  559. {
  560. if (_isPreviewing)
  561. return;
  562. _isPreviewing = true;
  563. }
  564. }
  565. public void SetMute(bool isMute)
  566. {
  567. CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"RtcMultiPusherV2 MessageClient Send SetMute {isMute}");
  568. var isMuteMessage = new TRTCBoolMessage(isMute);
  569. var muteMessage = new TRTCMessage(EnumMessageType.Mute, isMuteMessage.ToBytes());
  570. FISAndroid.FileWrtierFromCommonToTRTCLiveVideo.WriteMessageToFileContinuous(muteMessage.ToBytes());
  571. }
  572. public void SwitchMic(string micId)
  573. {
  574. }
  575. public void StopPreview()
  576. {
  577. lock (_previewLocker)
  578. {
  579. if (!_isPreviewing)
  580. {
  581. return;
  582. }
  583. _isPreviewing = false;
  584. }
  585. }
  586. public void Dispose()
  587. {
  588. Stop();
  589. _handlingImageEvent.WaitOne();
  590. }
  591. }
  592. }
  593. }