RtcSinglePusherV2.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using Com.Tencent.Trtc;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Vinno.FIS.TRTCClient.Common.Enum;
  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. namespace Vinno.vCloud.FIS.CrossPlatform.Android.LiveVideo.RTC
  11. {
  12. public class RtcSinglePusherV2 : PusherBase, ILiveVideoPusherV2
  13. {
  14. private TRTCPusher _pusher;
  15. private IImageSenderV2 _imageSender;
  16. private int _videoResolution;
  17. public event EventHandler<ChannelStateEventArgsV2> ChannelStateChanged;
  18. public RtcSinglePusherV2()
  19. {
  20. }
  21. public bool StartPusher(IExtendedData pushParams, IEnumerable<CPVideoDeviceOutputInfo> deviceInfos)
  22. {
  23. try
  24. {
  25. var rtcParams = pushParams as RtcExtendedData;
  26. if (rtcParams == null || rtcParams.AppId == 0 || rtcParams.UserInfos.Count == 0)
  27. {
  28. CrossPlatformHelper.Instance.LogWriter?.WriteLineError("RTC Single PusherV2: Extended Data Is Null!");
  29. return false;
  30. }
  31. if (deviceInfos.Count() == 0)
  32. {
  33. CrossPlatformHelper.Instance.LogWriter?.WriteLineError("RTC Single PusherV2: No device set!");
  34. return false;
  35. }
  36. var userInfo = rtcParams.UserInfos.FirstOrDefault();
  37. var deviceInfo = deviceInfos.FirstOrDefault(x => x.Category == userInfo.Category);
  38. if (deviceInfo == null)
  39. {
  40. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single Pusher Category {userInfo.Category} No device found!");
  41. return false;
  42. }
  43. _videoResolution = GetVideoResolution(userInfo.Width, userInfo.Height);
  44. _pusher = new TRTCPusher();
  45. _pusher.FirstFrameSend += OnFirstFrameSend;
  46. if (CommonParameter.Instance.IsSonopost)
  47. {
  48. _pusher.EnterRoom(userInfo.UserId, rtcParams.RoomId, userInfo.UserSign, rtcParams.AppId, _videoResolution, userInfo.VideoFps, userInfo.VideoBitrate, userInfo.MinVideoBitrate, pushParams.IsMute, pushParams.MicDeviceId);
  49. if (_videoResolution == TRTCCloudDef.TrtcVideoResolution640480)
  50. {
  51. _imageSender = new Camera640X480SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height, deviceInfo.Category, deviceInfo.Category == EnumLiveChannelCategory.Main ? false : true, false);
  52. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Camera640X480SenderV2");
  53. }
  54. else if (_videoResolution == TRTCCloudDef.TrtcVideoResolution960540)
  55. {
  56. _imageSender = new Camera960X540SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height, deviceInfo.Category, deviceInfo.Category == EnumLiveChannelCategory.Main ? false : true, false);
  57. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Camera960X540SenderV2");
  58. }
  59. else if (_videoResolution == TRTCCloudDef.TrtcVideoResolution960720)
  60. {
  61. _imageSender = new Camera960X720SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height, deviceInfo.Category, deviceInfo.Category == EnumLiveChannelCategory.Main ? false : true, false);
  62. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Camera960X720SenderV2");
  63. }
  64. else if (_videoResolution == TRTCCloudDef.TrtcVideoResolution1280720)
  65. {
  66. _imageSender = new Camera1280X720SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height, deviceInfo.Category, deviceInfo.Category == EnumLiveChannelCategory.Main ? false : true, false);
  67. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Camera1280X720SenderV2");
  68. }
  69. else
  70. {
  71. _imageSender = new Camera1920X1080SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height, deviceInfo.Category, deviceInfo.Category == EnumLiveChannelCategory.Main ? false : true, false);
  72. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Camera1920X1080SenderV2");
  73. }
  74. }
  75. else
  76. {
  77. _pusher.EnterRoom(userInfo.UserId, rtcParams.RoomId, userInfo.UserSign, rtcParams.AppId, _videoResolution, userInfo.VideoFps, userInfo.VideoBitrate, userInfo.MinVideoBitrate, pushParams.IsMute, pushParams.MicDeviceId);
  78. if (_videoResolution == TRTCCloudDef.TrtcVideoResolution640480)
  79. {
  80. _imageSender = new Terminal640X480SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height);
  81. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Terminal1280X720SenderV2");
  82. }
  83. else if (_videoResolution == TRTCCloudDef.TrtcVideoResolution960540)
  84. {
  85. _imageSender = new Terminal960X540SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height);
  86. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Terminal1280X720SenderV2");
  87. }
  88. else if (_videoResolution == TRTCCloudDef.TrtcVideoResolution960720)
  89. {
  90. _imageSender = new Terminal960X720SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height);
  91. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Terminal1280X720SenderV2");
  92. }
  93. else if (_videoResolution == TRTCCloudDef.TrtcVideoResolution1280720)
  94. {
  95. _imageSender = new Terminal1280X720SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height);
  96. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Terminal1280X720SenderV2");
  97. }
  98. else
  99. {
  100. _imageSender = new Terminal1920X1080SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height);
  101. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Terminal1920X1080SenderV2");
  102. }
  103. }
  104. _imageSender.VideoFrameDataReceived += OnVideoFrameDataReceived;
  105. _imageSender.PreviewImageFrameDataReceived += OnPreviewImageFrameDataReceived;
  106. _imageSender.Start();
  107. return true;
  108. }
  109. catch (Exception ex)
  110. {
  111. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2 Start Push Error:{ex}");
  112. }
  113. return false;
  114. }
  115. private int GetVideoResolution(int width, int height)
  116. {
  117. if (width <= 640 && height <= 480)
  118. {
  119. return TRTCCloudDef.TrtcVideoResolution640480;
  120. }
  121. else if (width <= 960 && height <= 544)
  122. {
  123. return TRTCCloudDef.TrtcVideoResolution960540;
  124. }
  125. else if (width <= 960 && height <= 720)
  126. {
  127. return TRTCCloudDef.TrtcVideoResolution960720;
  128. }
  129. else if (width <= 1280 && height <= 720)
  130. {
  131. return TRTCCloudDef.TrtcVideoResolution1280720;
  132. }
  133. else
  134. {
  135. return TRTCCloudDef.TrtcVideoResolution19201080;
  136. }
  137. }
  138. private void OnVideoFrameDataReceived(object sender, CPVideoFrameData e)
  139. {
  140. _pusher?.SendData(e.Width, e.Height, e.Data);
  141. }
  142. public void SetMute(bool isMute)
  143. {
  144. _pusher?.SetMute(isMute);
  145. }
  146. public void SwitchMic(string micId)
  147. {
  148. _pusher?.SwitchMic(micId);
  149. }
  150. public bool StopPusher()
  151. {
  152. try
  153. {
  154. if (_pusher != null)
  155. {
  156. _pusher.FirstFrameSend -= OnFirstFrameSend;
  157. _pusher.ExitRoom();
  158. _pusher = null;
  159. }
  160. if (_imageSender != null)
  161. {
  162. _imageSender.VideoFrameDataReceived -= OnVideoFrameDataReceived;
  163. _imageSender.PreviewImageFrameDataReceived -= OnPreviewImageFrameDataReceived;
  164. _imageSender.Stop();
  165. _imageSender = null;
  166. }
  167. return true;
  168. }
  169. catch (Exception e)
  170. {
  171. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Rtc Single PusherV2 Stop Pusher Error:{e}");
  172. }
  173. return false;
  174. }
  175. private void OnPreviewImageFrameDataReceived(object sender, ImageFrameData e)
  176. {
  177. var category = (EnumLiveChannelCategory)sender;
  178. CopyPreviewData(category, e);
  179. }
  180. private void OnFirstFrameSend(object sender, EventArgs e)
  181. {
  182. ChannelStateChanged?.Invoke(this, new ChannelStateEventArgsV2(EnumLiveChannelCategory.Main, EnumDeviceLiveState.Pushing));
  183. }
  184. public bool StartSpeedTest(uint appId, string userId, string userSign)
  185. {
  186. TRTCPusher pusher = null;
  187. try
  188. {
  189. pusher = new TRTCPusher();
  190. return pusher.StartSpeedTest((int)appId, userId, userSign);
  191. }
  192. catch (Exception ex)
  193. {
  194. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Rtc Single PusherV2 StartSpeedTest Error:{ex}");
  195. return false;
  196. }
  197. finally
  198. {
  199. if (pusher != null)
  200. {
  201. pusher.StopSpeedTest();
  202. pusher = null;
  203. }
  204. }
  205. }
  206. public override void DoDispose()
  207. {
  208. StopPusher();
  209. base.DoDispose();
  210. }
  211. }
  212. }