RTCService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System.Collections.Generic;
  2. using Blue;
  3. using Newtonsoft.Json;
  4. using UnityEngine;
  5. public class RTCService : IRTCService
  6. {
  7. private string mToken;
  8. private RTCRoomInfo mRTCRoomInfo;
  9. private List<RTCUserInfo> mUserList =new List<RTCUserInfo>(); // 房间用户列表
  10. public Dictionary<int, RTCUserInfo> mUserDic{ get; private set; } = new Dictionary<int, RTCUserInfo>(); // 房间用户字典
  11. public void OnInit()
  12. {
  13. // 获取 token
  14. this.RegisterEvent<RTCGetTokenSuccessEvent>(RTCGetTokenSuccess);
  15. this.RegisterEvent<RTCGetTokenFailEvent>(RTCGetTokenFail);
  16. // 创建房间
  17. this.RegisterEvent<RTCCreatRoomSuccessEvent>(RTCCreatRoomSuccess);
  18. this.RegisterEvent<RTCCreatRoomFailEvent>(RTCCreatRoomFail);
  19. // 连接RTC
  20. this.RegisterEvent<RTCConnectSuccessEvent>(RTCConnectSuccess);
  21. this.RegisterEvent<RTCConnectFailEvent>(RTCConnectFail);
  22. // 加入房间
  23. this.RegisterEvent<JoinRoomSuccessEvent>(JoinRoomSuccess);
  24. this.RegisterEvent<JoinRoomFailEvent>(JoinRoomFail);
  25. // 其他用户加入房间
  26. this.RegisterEvent<OtherUserJoinRoomEvent>(OtherUserJoinRoom);
  27. this.RegisterEvent<OtherLeaveRoomEvent>(OtherLeaveRoom);
  28. }
  29. /// <summary>
  30. /// 关闭RTC
  31. /// </summary>
  32. public void CloseRTC()
  33. {
  34. }
  35. /// <summary>
  36. /// 登录后获取账号Token
  37. /// </summary>
  38. public void GetToken()
  39. {
  40. mToken = "...token...";
  41. this.SendEvent(new RTCGetTokenSuccessEvent() { token = mToken });
  42. //this.SendEvent<RTCGetTokenFailEvent>();
  43. }
  44. /// <summary>
  45. /// 根据Token创建房间
  46. /// </summary>
  47. public void CreatRoom()
  48. {
  49. var mRTCUrl = "...RTCURL...";
  50. RTCUserInfo RTCUserInfo = new RTCUserInfo()
  51. {
  52. UserID = 123,UserName="我是123",Head=321,Audio = false,Video = false
  53. };
  54. mUserDic.Add(RTCUserInfo.UserID,RTCUserInfo);
  55. mUserList.Add(RTCUserInfo);
  56. mRTCRoomInfo = new RTCRoomInfo()
  57. {
  58. RoomID=111111,RoomName="我是111111",UserList = mUserList
  59. };
  60. string currentRoomInfo = JsonConvert.SerializeObject(mRTCRoomInfo);
  61. this.SendEvent(new RTCCreatRoomSuccessEvent() { RTCUrl = mRTCUrl, roomInfo = currentRoomInfo });
  62. //this.SendEvent<RTCCreatRoomFailEvent>();
  63. }
  64. /// <summary>
  65. /// 根据url连接rtc
  66. /// </summary>
  67. public void ConnectByRTCUrl(string URL)
  68. {
  69. this.SendEvent<RTCConnectSuccessEvent>();
  70. //this.SendEvent<RTCConnectFailEvent>();
  71. }
  72. /// <summary>s
  73. /// 加入房间
  74. /// </summary>
  75. public void JoinRoom(int roomId)
  76. {
  77. // ToDo:
  78. this.SendEvent<JoinRoomSuccessEvent>();
  79. //this.SendEvent<JoinRoomFailEvent>();
  80. }
  81. /// <summary>
  82. /// 退出房间
  83. /// </summary>
  84. public void LeaveRoom()
  85. {
  86. mUserList.Clear();
  87. mUserDic.Clear();
  88. Debug.LogError($"离开房间");
  89. }
  90. /// <summary>
  91. /// 控制摄像头
  92. /// </summary>
  93. public void ActiveVideo(bool active)
  94. {
  95. Debug.LogError($"ActiveVideo:{active}");
  96. }
  97. /// <summary>
  98. /// 控制麦克风
  99. /// </summary>
  100. public void ActiveAudio(bool active)
  101. {
  102. Debug.LogError($"ActiveAudio:{active}");
  103. }
  104. /// <summary>
  105. /// 将远程视频流静音
  106. /// </summary>
  107. public void MuteRemoteVideoStream(string userID, bool active)
  108. {
  109. Debug.LogError($"远程视频流===> 用户:{userID},开关:{active}");
  110. }
  111. /// <summary>
  112. /// 将远程音频流静音
  113. /// </summary>
  114. public void MuteRemoteAudioStream(string userID, bool active)
  115. {
  116. Debug.LogError($"远程视频流===> 用户:{userID},开关:{active}");
  117. }
  118. /// <summary>
  119. /// 视频静音
  120. /// </summary>
  121. public void MuteVideo(bool active)
  122. {
  123. Debug.LogError($"视频静音:{active}");
  124. }
  125. /// <summary>
  126. /// 音频静音
  127. /// </summary>
  128. public void MuteAudio(bool active)
  129. {
  130. Debug.LogError($"音频静音{active}");
  131. }
  132. /// <summary>
  133. /// 打开本地视频
  134. /// </summary>
  135. public void LocalVideo(bool active)
  136. {
  137. Debug.LogError($"打开本地视频{active}");
  138. }
  139. /// <summary>
  140. /// 打开本地音频
  141. /// </summary>
  142. public void LoacalAudio(bool active)
  143. {
  144. Debug.LogError($"打开本地音频{active}");
  145. }
  146. #region 事件
  147. // 获取 token
  148. private void RTCGetTokenSuccess(RTCGetTokenSuccessEvent e)
  149. {
  150. Debug.LogError($"获取token成功:{e.token}");
  151. this.UnRegisterEvent<RTCGetTokenSuccessEvent>(RTCGetTokenSuccess);
  152. }
  153. private void RTCGetTokenFail(RTCGetTokenFailEvent e)
  154. {
  155. Debug.LogError($"RTC:RTCGetTokenFail");
  156. this.UnRegisterEvent<RTCGetTokenFailEvent>(RTCGetTokenFail);
  157. }
  158. // 创建房间
  159. private void RTCCreatRoomSuccess(RTCCreatRoomSuccessEvent e)
  160. {
  161. Debug.LogError($"创建房间成功:{e.roomInfo}");
  162. ConnectByRTCUrl(e.RTCUrl);
  163. this.UnRegisterEvent<RTCCreatRoomSuccessEvent>(RTCCreatRoomSuccess);
  164. }
  165. private void RTCCreatRoomFail(RTCCreatRoomFailEvent e)
  166. {
  167. Debug.LogError($"RTC:RTCGetTokenFail");
  168. this.UnRegisterEvent<RTCCreatRoomFailEvent>(RTCCreatRoomFail);
  169. }
  170. // 连接RTC
  171. private void RTCConnectSuccess(RTCConnectSuccessEvent e)
  172. {
  173. Debug.LogError($"RTC:RTCConnectSuccess");
  174. this.UnRegisterEvent<RTCConnectSuccessEvent>(RTCConnectSuccess);
  175. }
  176. private void RTCConnectFail(RTCConnectFailEvent e)
  177. {
  178. Debug.LogError($"RTC:RTCConnectFail");
  179. this.UnRegisterEvent<RTCConnectFailEvent>(RTCConnectFail);
  180. }
  181. // 加入房间
  182. private void JoinRoomSuccess(JoinRoomSuccessEvent e)
  183. {
  184. mUserList = JsonConvert.DeserializeObject<List<RTCUserInfo>>(e.roomInfo);
  185. foreach(RTCUserInfo user in mUserList) mUserDic.Add(user.UserID,user);
  186. Debug.LogError($"RTC:JoinRoomSuccess");
  187. this.UnRegisterEvent<JoinRoomSuccessEvent>(JoinRoomSuccess);
  188. }
  189. private void JoinRoomFail(JoinRoomFailEvent e)
  190. {
  191. Debug.LogError($"RTC:JoinRoomFail");
  192. this.UnRegisterEvent<JoinRoomFailEvent>(JoinRoomFail);
  193. }
  194. // 其他用户
  195. private void OtherUserJoinRoom(OtherUserJoinRoomEvent e)
  196. {
  197. RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject<RTCUserInfo>(e.rtcUserInfoJsonString);
  198. if(!mUserDic.ContainsKey(RTCUserInfo.UserID))
  199. {
  200. mUserDic.Add(RTCUserInfo.UserID,RTCUserInfo);
  201. mUserList.Add(RTCUserInfo);
  202. }
  203. this.UnRegisterEvent<OtherUserJoinRoomEvent>(OtherUserJoinRoom);
  204. }
  205. private void OtherLeaveRoom(OtherLeaveRoomEvent e)
  206. {
  207. RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject<RTCUserInfo>(e.rtcUserInfoJsonString);
  208. if(mUserDic.ContainsKey(RTCUserInfo.UserID))
  209. {
  210. mUserDic.Remove(RTCUserInfo.UserID);
  211. mUserList.Remove(RTCUserInfo);
  212. }
  213. this.UnRegisterEvent<OtherLeaveRoomEvent>(OtherLeaveRoom);
  214. }
  215. #endregion
  216. }