ChatRoomManager.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using fis.media.Library.Media.Members;
  2. using fis.media.Library.Media.Players;
  3. using fis.media.Library.Media.Publisher;
  4. using fis.media.Library.Media.Rooms;
  5. using fis.media.Library.OutPut.FisVideoFrameOutput;
  6. using fis.media.Library.Players;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace fis.media.Managers
  13. {
  14. public class ChatRoomManager
  15. {
  16. private ChatRoomManager()
  17. {
  18. Pusher =PusherManager.Instance;
  19. Player = PlayerManager.Instance;
  20. //默认为TRTC房间
  21. RoomType = RoomTypeEnum.TRTCRoom;
  22. _videoStreamStructre = new VideoStreamStructre();
  23. }
  24. private static ChatRoomManager _instance;
  25. private RoomTypeEnum _roomType;
  26. private VideoStreamStructre _videoStreamStructre;
  27. public static ChatRoomManager Instance => _instance ?? (_instance = new ChatRoomManager());
  28. public IRoom Room { get; private set; }
  29. public PusherManager Pusher { get; set; }
  30. public PlayerManager Player { get; set; }
  31. public event EventHandler<VideoStreamStructre> VideoStreamOutputInvoked;
  32. /// <summary>
  33. /// 进入房间
  34. /// </summary>
  35. /// <param name="roomType"></param>
  36. /// <param name="members"></param>
  37. /// <param name="args"></param>
  38. /// <returns></returns>
  39. public bool Enter(RoomTypeEnum roomType, List<AbstractMember> members,string[] args)
  40. {
  41. RoomType = roomType;
  42. foreach (var c in members)
  43. {
  44. Room.EnterRoom(c);
  45. }
  46. var memebers = Room.Members;
  47. foreach (var m in memebers)
  48. {
  49. var channels = m.Player.PlayerChannels;
  50. foreach (var channel in channels)
  51. {
  52. channel.FrameRecieved += OnChannelFrameRevieved;
  53. }
  54. }
  55. var commonStartupArgs= new List<string> { $"{RoomType}" };
  56. foreach (var arg in args)
  57. {
  58. commonStartupArgs.Add(arg);
  59. }
  60. Pusher.Start(commonStartupArgs.ToArray());
  61. Pusher.PreviewImageReceived += OnPreviewImageReceived;
  62. Player.Start(commonStartupArgs.ToArray());
  63. return true;
  64. }
  65. private void OnPreviewImageReceived(object? sender, LocalPreviewVideoFrameData e)
  66. {
  67. var selfMember = Room.Members.FirstOrDefault(x => x.Id == e.UserId);
  68. var activeMember= selfMember as ActiveMember;
  69. var channelVideoFrame = new ChannelVideoFrame(e.Data,activeMember.Id, ChannelNameEnum.Main,activeMember.RoleType);
  70. OnChannelFrameRevieved(this, channelVideoFrame);
  71. }
  72. private void OnChannelFrameRevieved(object? sender, ChannelVideoFrame e)
  73. {
  74. var existOuput = _videoStreamStructre.VideoChannels.FirstOrDefault(x => x.Id == e.ChannelOwnerId
  75. && x.ChannelDisplayName == e.ChannelNameEnum.ToString());
  76. if (existOuput == null)
  77. {
  78. var outPut = GenerateOutput(e);
  79. outPut.UpdataFrameData(e.VideoFrameData.ToBase64());
  80. _videoStreamStructre.VideoChannels.Add(outPut);
  81. }
  82. else
  83. {
  84. existOuput.UpdataFrameData(e.VideoFrameData.ToBase64());
  85. }
  86. VideoStreamOutputInvoked?.Invoke(this,_videoStreamStructre);
  87. }
  88. /// <summary>
  89. /// 更新成员状态
  90. /// </summary>
  91. /// <param name="abstractMember"></param>
  92. /// <returns></returns>
  93. public bool UpdateMember(AbstractMember abstractMember)
  94. {
  95. var memeber = Room.Members.FirstOrDefault(x => x.Id == abstractMember.Id);
  96. if (memeber == null)
  97. {
  98. return false;
  99. }
  100. var activeExistMemeber= memeber as ActiveMember;
  101. var activeMemeber = abstractMember as ActiveMember;
  102. if (activeMemeber != null && activeExistMemeber != null)
  103. {
  104. activeExistMemeber.IsOnline = activeMemeber.IsOnline;
  105. activeExistMemeber.IsMute = activeMemeber.IsMute;
  106. activeExistMemeber.IsCameraOn = activeExistMemeber.IsCameraOn;
  107. activeExistMemeber.IsMergeChannel = activeExistMemeber.IsMergeChannel;
  108. return activeExistMemeber.AdaptMemberUpdate();
  109. }
  110. return true;
  111. }
  112. /// <summary>
  113. /// 删除成员
  114. /// </summary>
  115. /// <param name="abstractMember"></param>
  116. /// <returns></returns>
  117. public bool RemoveMember(AbstractMember abstractMember)
  118. {
  119. var memeber = Room.Members.FirstOrDefault(x => x.Id == abstractMember.Id);
  120. if (memeber == null)
  121. {
  122. return false;
  123. }
  124. memeber.Dispose();
  125. Room.Members.Remove(memeber);
  126. return true;
  127. }
  128. public bool Exit()
  129. {
  130. Pusher.Dispose();
  131. Player.Dispose();
  132. var memebers = Room.Members;
  133. foreach (var m in memebers)
  134. {
  135. var channels = m.Player.PlayerChannels;
  136. foreach (var channel in channels)
  137. {
  138. channel.FrameRecieved -= OnChannelFrameRevieved;
  139. }
  140. }
  141. Room.Dispose();
  142. return true;
  143. }
  144. private RoomTypeEnum RoomType
  145. {
  146. get
  147. {
  148. return _roomType;
  149. }
  150. set
  151. {
  152. if (_roomType != value)
  153. {
  154. _roomType = value;
  155. SwithRoom();
  156. }
  157. }
  158. }
  159. private void SwithRoom()
  160. {
  161. Room?.Dispose();
  162. Room = null;
  163. if (RoomType == RoomTypeEnum.TRTCRoom)
  164. {
  165. Room = TRTCRoom.Instance;
  166. }
  167. if (RoomType == RoomTypeEnum.RTMPVirtualRoom)
  168. {
  169. Room = RTMPRoom.Instance;
  170. }
  171. if (RoomType == RoomTypeEnum.VRTCRoom)
  172. {
  173. Room = VRTCRoom.Instance;
  174. }
  175. }
  176. private Channelbase GenerateOutput(ChannelVideoFrame e)
  177. {
  178. if (e.OwnerRoleType == MemeberTypeEnum.LocalVideo)
  179. {
  180. return new LocalCameraChannel(e.ChannelOwnerId, e.ChannelNameEnum.ToString());
  181. }
  182. if (e.OwnerRoleType == MemeberTypeEnum.RemoteVideo)
  183. {
  184. return new RemoteCameraChannel(e.ChannelOwnerId, e.ChannelNameEnum.ToString());
  185. }
  186. if (e.OwnerRoleType == MemeberTypeEnum.MainDeviceVideo)
  187. {
  188. if (e.ChannelNameEnum == ChannelNameEnum.Main)
  189. {
  190. return new MainChannel(e.ChannelOwnerId, e.ChannelNameEnum.ToString());
  191. }
  192. if (e.ChannelNameEnum == ChannelNameEnum.SideOne)
  193. {
  194. return new SideChannelOne(e.ChannelOwnerId, e.ChannelNameEnum.ToString());
  195. }
  196. if (e.ChannelNameEnum == ChannelNameEnum.SideTwo)
  197. {
  198. return new SideChannelTwo(e.ChannelOwnerId, e.ChannelNameEnum.ToString());
  199. }
  200. }
  201. return null;
  202. }
  203. }
  204. }