PlayerManager.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using fis.media.Library.Media.Players;
  7. using fis.media.Library.Media.Rooms;
  8. using fis.media.Library.Players;
  9. using fis.media.ThirdPartLibrary.Daniu.Player;
  10. using fis.media.ThirdPartLibrary.Tencent;
  11. namespace fis.media.Managers
  12. {
  13. public class PlayerManager:IDisposable
  14. {
  15. private static PlayerManager? _instance;
  16. private SmartPlayer? _smartPlayer { get; set; }
  17. public EventHandler<RemoteVideoFrameData>? VideoFrameReceived;
  18. private RoomTypeEnum _roomType;
  19. private string _rtmpPullUrl;
  20. private PlayerManager() {
  21. }
  22. public static PlayerManager Instance => _instance ?? (_instance = new PlayerManager());
  23. /// <summary>
  24. /// 注册SmartPlayerSDK
  25. /// </summary>
  26. public static void RegistSmartPlayerSDK()
  27. {
  28. SmartPlayer.RegistSmartPlayerSDK();
  29. }
  30. /// <summary>
  31. /// 开始RTMP拉流
  32. /// </summary>
  33. /// <param name="url"></param>
  34. private void StartRTMP()
  35. {
  36. if (_smartPlayer == null)
  37. {
  38. _smartPlayer = new SmartPlayer();
  39. //_smartPlayer.VideoFrameReceived += VideoFrameReceived;
  40. }
  41. _smartPlayer.Play(_rtmpPullUrl);
  42. }
  43. /// <summary>
  44. /// 开始TRTC拉流
  45. /// </summary>
  46. /// <param name="url"></param>
  47. private void StartTRTC()
  48. {
  49. TRTCChatRoom.Instance.RemoteVideoFrameArrived += OnRemoteVideoFrameArrived;
  50. TRTCChatRoom.Instance.RemoteVideoScreenFrameArrived += OnRemoteVideoScreenFrameArrived;
  51. }
  52. public void Start(string[] args)
  53. {
  54. if (SetRoomPara(args))
  55. {
  56. if (_roomType == RoomTypeEnum.TRTCRoom)
  57. {
  58. StartTRTC();
  59. }
  60. if (_roomType == RoomTypeEnum.RTMPVirtualRoom)
  61. {
  62. StartRTMP();
  63. }
  64. }
  65. }
  66. private bool SetRoomPara(string[] args)
  67. {
  68. if (args == null || args.Length < 1)
  69. {
  70. return false;
  71. }
  72. _roomType = (RoomTypeEnum)Enum.Parse(typeof(RoomTypeEnum), args[0], true);
  73. if (_roomType == RoomTypeEnum.RTMPVirtualRoom && args.Length == 2)
  74. {
  75. var url = args[1];
  76. _rtmpPullUrl = url;
  77. }
  78. return true;
  79. }
  80. private void OnRemoteVideoScreenFrameArrived(object? sender, RemoteVideoFrameData e)
  81. {
  82. throw new NotImplementedException();
  83. }
  84. private void OnRemoteVideoFrameArrived(object? sender, RemoteVideoFrameData e)
  85. {
  86. VideoFrameReceived?.Invoke(this,e);
  87. }
  88. /// <summary>
  89. /// 停止拉流
  90. /// </summary>
  91. public void Stop()
  92. {
  93. _smartPlayer?.Stop();
  94. TRTCChatRoom.Instance.RemoteVideoFrameArrived -= OnRemoteVideoFrameArrived;
  95. TRTCChatRoom.Instance.RemoteVideoScreenFrameArrived -= OnRemoteVideoScreenFrameArrived;
  96. }
  97. /// <summary>
  98. /// 切换拉流地址
  99. /// </summary>
  100. /// <param name="url"></param>
  101. public void SwitchUrl(string url)
  102. {
  103. _smartPlayer?.SwitchUrl(url);
  104. }
  105. /// <summary>
  106. /// 设置录制参数
  107. /// </summary>
  108. /// <param name="recordingDirectory"></param>
  109. /// <param name="maxFileSize"></param>
  110. /// <param name="filePrefix"></param>
  111. /// <param name="attachDateTime"></param>
  112. public void SetRecordParameter(string recordingDirectory, int maxFileSize, string filePrefix, bool attachDateTime)
  113. {
  114. _smartPlayer?.SetRecordParameter(recordingDirectory, maxFileSize, filePrefix, attachDateTime);
  115. }
  116. /// <summary>
  117. /// 开始录制
  118. /// </summary>
  119. public void StartRecord()
  120. {
  121. _smartPlayer?.StartRecord();
  122. }
  123. /// <summary>
  124. /// 停止录制
  125. /// </summary>
  126. public void StopRecord()
  127. {
  128. _smartPlayer?.StopRecord();
  129. }
  130. /// <summary>
  131. /// 设置静音
  132. /// </summary>
  133. /// <param name="isMute"></param>
  134. public void SetMute(bool isMute)
  135. {
  136. _smartPlayer?.SetMute(isMute);
  137. }
  138. public void Dispose()
  139. {
  140. Stop();
  141. if (_smartPlayer != null)
  142. {
  143. //_smartPlayer.VideoFrameReceived -= VideoFrameReceived;
  144. }
  145. }
  146. }
  147. }