PusherManager.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System.Runtime.Versioning;
  2. using fis.media.Library.Media.Rooms;
  3. using fis.media.Library.Players;
  4. using fis.media.ThirdPartLibrary.Daniu.Common;
  5. using fis.media.ThirdPartLibrary.Daniu.Publisher;
  6. using fis.media.ThirdPartLibrary.Tencent;
  7. namespace fis.media.Managers
  8. {
  9. [SupportedOSPlatform("windows")]
  10. public class PusherManager : IDisposable
  11. {
  12. private static PusherManager? _instance;
  13. public static PusherManager Instance => _instance ?? (_instance = new PusherManager());
  14. public EventHandler<LocalPreviewVideoFrameData> PreviewImageReceived;
  15. private SmartPublisher? _smartPublisher;
  16. private string _userId;
  17. private int _appId;
  18. private string _userSign;
  19. private int _rtcRoomId;
  20. private RoomTypeEnum _roomType;
  21. private string _rtmpurl;
  22. private int _screenWidth;
  23. private int _screenHeight;
  24. private LocalPreviewVideoFrameData _localPreviewFrame;
  25. private PusherManager() { }
  26. /// <summary>
  27. /// 注册SmartPublisherSDK
  28. /// </summary>
  29. public static void RegistSmartPublisherSDK()
  30. {
  31. SmartPublisher.RegistSmartPublisherSDK();
  32. }
  33. public void Start(string[] args)
  34. {
  35. if (SetRoomPara(args))
  36. {
  37. if (_roomType == RoomTypeEnum.TRTCRoom)
  38. {
  39. StartTRTC();
  40. }
  41. if (_roomType == RoomTypeEnum.RTMPVirtualRoom)
  42. {
  43. StartRtmp();
  44. }
  45. }
  46. }
  47. /// <summary>
  48. /// 开始推流
  49. /// </summary>
  50. /// <param name="url">推流地址</param>
  51. private void StartRtmp()
  52. {
  53. if (_smartPublisher == null)
  54. {
  55. _smartPublisher = new SmartPublisher(0, AudioMode.Mix);
  56. SetScreenSize(_screenWidth, _screenHeight);
  57. _smartPublisher.InitLayers();
  58. _smartPublisher.PreviewImageReceived += OnRTMPFrameReceived;
  59. }
  60. _smartPublisher?.SetEnablePreview(true);
  61. _smartPublisher?.Start(_rtmpurl);
  62. }
  63. private void OnRTMPFrameReceived(object? sender, VideoFrameData e)
  64. {
  65. throw new NotImplementedException();
  66. }
  67. /// <summary>
  68. /// 开始推流(TRTC进入房间即开始推流)
  69. /// </summary>
  70. /// <param name="url">推流地址</param>
  71. private void StartTRTC()
  72. {
  73. TRTCChatRoom.Instance.Enter(_userId, _rtcRoomId, true, _userSign, _appId);
  74. TRTCChatRoom.Instance.LocalVideoFrameArrived += OnLocalVideoFrameArrived;
  75. TRTCChatRoom.Instance.LocalVideoScreenFrameArrived += OnLocalVideoScreenFrameArrived;
  76. }
  77. private void OnLocalVideoScreenFrameArrived(object? sender, VideoFrameData e)
  78. {
  79. ;
  80. }
  81. private void OnLocalVideoFrameArrived(object? sender, VideoFrameData e)
  82. {
  83. if (_localPreviewFrame == null)
  84. {
  85. _localPreviewFrame = new LocalPreviewVideoFrameData(_userId);
  86. }
  87. _localPreviewFrame.Data = e;
  88. PreviewImageReceived?.Invoke(this, _localPreviewFrame);
  89. }
  90. private bool SetRoomPara(string[] args)
  91. {
  92. if (args == null||args.Length<1)
  93. {
  94. return false;
  95. }
  96. _roomType= (RoomTypeEnum)Enum.Parse(typeof(RoomTypeEnum), args[0], true);
  97. if (_roomType == RoomTypeEnum.TRTCRoom && args.Length == 5)
  98. {
  99. if (int.TryParse(args[1], out int appId))
  100. {
  101. return false;
  102. }
  103. var userId=args[2];
  104. var userSign = args[3];
  105. if (int.TryParse(args[4], out int intergeRoomId))
  106. {
  107. return false;
  108. }
  109. _userId = userId;
  110. _appId = appId;
  111. _userSign = userSign;
  112. _rtcRoomId = intergeRoomId;
  113. }
  114. if (_roomType == RoomTypeEnum.TRTCRoom && args.Length == 4)
  115. {
  116. if (int.TryParse(args[1], out int screenWidth))
  117. {
  118. return false;
  119. }
  120. if (int.TryParse(args[2], out int screenHeight))
  121. {
  122. return false;
  123. }
  124. var url = args[3];
  125. _rtmpurl = url;
  126. _screenWidth = screenWidth;
  127. _screenHeight = screenHeight;
  128. }
  129. return true;
  130. }
  131. public void EnableLayer(int index, bool isEnable)
  132. {
  133. _smartPublisher?.SetEnableLayer(index, isEnable);
  134. }
  135. public void SetEnablePreview(bool enablePreview)
  136. {
  137. _smartPublisher?.SetEnablePreview(enablePreview);
  138. }
  139. public void SetScreenSize(int screenWidth, int screenHeight)
  140. {
  141. _smartPublisher?.SetScreenSize(screenWidth, screenHeight);
  142. }
  143. /// <summary>
  144. /// 设置背景图路径
  145. /// </summary>
  146. /// <param name="path"></param>
  147. public void SetBackgrountPath(string path)
  148. {
  149. _smartPublisher?.SetBackgrountPath(path);
  150. }
  151. /// <summary>
  152. /// 设置是否静音
  153. /// </summary>
  154. /// <param name="isMute"></param>
  155. public void SetMute(bool isMute)
  156. {
  157. _smartPublisher?.SetMute(isMute);
  158. }
  159. /// <summary>
  160. /// 停止推流
  161. /// </summary>
  162. public void Stop()
  163. {
  164. if (_roomType == RoomTypeEnum.RTMPVirtualRoom)
  165. {
  166. _smartPublisher?.Close();
  167. }
  168. if (_roomType == RoomTypeEnum.TRTCRoom)
  169. {
  170. TRTCChatRoom.Instance.LocalVideoFrameArrived -= OnLocalVideoFrameArrived;
  171. TRTCChatRoom.Instance.LocalVideoScreenFrameArrived -= OnLocalVideoScreenFrameArrived;
  172. TRTCChatRoom.Instance.Exit();
  173. }
  174. }
  175. public void Dispose()
  176. {
  177. Stop();
  178. if (_smartPublisher != null)
  179. {
  180. _smartPublisher.PreviewImageReceived -= OnRTMPFrameReceived;
  181. _smartPublisher?.Dispose();
  182. _smartPublisher = null;
  183. }
  184. }
  185. }
  186. }