RTCService.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. private Dictionary<int, RTCUserInfo> mUserDic = 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. /// <summary>
  27. /// 关闭RTC
  28. /// </summary>
  29. public void CloseRTC()
  30. {
  31. }
  32. /// <summary>
  33. /// 登录后获取账号Token
  34. /// </summary>
  35. public void GetToken()
  36. {
  37. mToken = "...token...";
  38. this.SendEvent(new RTCGetTokenSuccessEvent() { token = mToken });
  39. //this.SendEvent<RTCGetTokenFailEvent>();
  40. }
  41. /// <summary>
  42. /// 根据Token创建房间
  43. /// </summary>
  44. public void CreatRoom()
  45. {
  46. var mRTCUrl = "...RTCURL...";
  47. RTCUserInfo RTCUserInfo = new RTCUserInfo()
  48. {
  49. UserID = 123,UserName="我是123",Head=321,Audio = false,Video = false
  50. };
  51. mUserDic.Add(RTCUserInfo.UserID,RTCUserInfo);
  52. mUserList.Add(RTCUserInfo);
  53. mRTCRoomInfo = new RTCRoomInfo()
  54. {
  55. RoomID=111111,RoomName="我是111111",UserList = mUserList
  56. };
  57. string currentRoomInfo = JsonConvert.SerializeObject(mRTCRoomInfo);
  58. this.SendEvent(new RTCCreatRoomSuccessEvent() { RTCUrl = mRTCUrl, roomInfo = currentRoomInfo });
  59. //this.SendEvent<RTCCreatRoomFailEvent>();
  60. }
  61. /// <summary>
  62. /// 根据url连接rtc
  63. /// </summary>
  64. public void ConnectByRTCUrl(string URL)
  65. {
  66. this.SendEvent<RTCConnectSuccessEvent>();
  67. //this.SendEvent<RTCConnectFailEvent>();
  68. }
  69. /// <summary>s
  70. /// 加入房间
  71. /// </summary>
  72. public void JoinRoom(int roomId)
  73. {
  74. this.SendEvent<JoinRoomSuccessEvent>();
  75. //this.SendEvent<JoinRoomFailEvent>();
  76. }
  77. /// <summary>
  78. /// 退出房间
  79. /// </summary>
  80. public void LeaveRoom()
  81. {
  82. mUserList.Clear();
  83. mUserDic.Clear();
  84. Debug.LogError($"离开房间");
  85. }
  86. /// <summary>
  87. /// 控制摄像头
  88. /// </summary>
  89. public void ActiveVideo(bool active)
  90. {
  91. Debug.LogError($"ActiveCamera:{active}");
  92. }
  93. /// <summary>
  94. /// 控制麦克风
  95. /// </summary>
  96. public void ActiveAudio(bool active)
  97. {
  98. Debug.LogError($"ActiveCamera:{active}");
  99. }
  100. /// <summary>
  101. /// 将远程视频流静音
  102. /// </summary>
  103. public void MuteRemoteVideoStream(string userID, bool active)
  104. {
  105. Debug.LogError($"远程视频流===> 用户:{userID},开关:{active}");
  106. }
  107. /// <summary>
  108. /// 将远程音频流静音
  109. /// </summary>
  110. public void MuteRemoteAudioStream(string userID, bool active)
  111. {
  112. Debug.LogError($"远程视频流===> 用户:{userID},开关:{active}");
  113. }
  114. /// <summary>
  115. /// 视频静音
  116. /// </summary>
  117. public void MuteVideo(bool active)
  118. {
  119. Debug.LogError($"视频静音:{active}");
  120. }
  121. /// <summary>
  122. /// 音频静音
  123. /// </summary>
  124. public void MuteAudio(bool active)
  125. {
  126. Debug.LogError($"音频静音{active}");
  127. }
  128. /// <summary>
  129. /// 打开本地视频
  130. /// </summary>
  131. public void LocalVideo(bool active)
  132. {
  133. Debug.LogError($"打开本地视频{active}");
  134. }
  135. /// <summary>
  136. /// 打开本地音频
  137. /// </summary>
  138. public void LoacalAudio(bool active)
  139. {
  140. Debug.LogError($"打开本地音频{active}");
  141. }
  142. #region 事件
  143. // 获取 token
  144. private void RTCGetTokenSuccess(RTCGetTokenSuccessEvent e)
  145. {
  146. Debug.LogError($"获取token成功:{e.token}");
  147. this.UnRegisterEvent<RTCGetTokenSuccessEvent>(RTCGetTokenSuccess);
  148. }
  149. private void RTCGetTokenFail(RTCGetTokenFailEvent e)
  150. {
  151. Debug.LogError($"RTC:RTCGetTokenFail");
  152. this.UnRegisterEvent<RTCGetTokenFailEvent>(RTCGetTokenFail);
  153. }
  154. // 创建房间
  155. private void RTCCreatRoomSuccess(RTCCreatRoomSuccessEvent e)
  156. {
  157. Debug.LogError($"创建房间成功:{e.roomInfo}");
  158. ConnectByRTCUrl(e.RTCUrl);
  159. this.UnRegisterEvent<RTCCreatRoomSuccessEvent>(RTCCreatRoomSuccess);
  160. }
  161. private void RTCCreatRoomFail(RTCCreatRoomFailEvent e)
  162. {
  163. Debug.LogError($"RTC:RTCGetTokenFail");
  164. this.UnRegisterEvent<RTCCreatRoomFailEvent>(RTCCreatRoomFail);
  165. }
  166. // 连接RTC
  167. private void RTCConnectSuccess(RTCConnectSuccessEvent e)
  168. {
  169. Debug.LogError($"RTC:RTCConnectSuccess");
  170. this.UnRegisterEvent<RTCConnectSuccessEvent>(RTCConnectSuccess);
  171. }
  172. private void RTCConnectFail(RTCConnectFailEvent e)
  173. {
  174. Debug.LogError($"RTC:RTCConnectFail");
  175. this.UnRegisterEvent<RTCConnectFailEvent>(RTCConnectFail);
  176. }
  177. // 加入房间
  178. private void JoinRoomSuccess(JoinRoomSuccessEvent e)
  179. {
  180. mUserList = JsonConvert.DeserializeObject<List<RTCUserInfo>>(e.roomInfo);
  181. foreach(RTCUserInfo user in mUserList) mUserDic.Add(user.UserID,user);
  182. Debug.LogError($"RTC:JoinRoomSuccess");
  183. this.UnRegisterEvent<JoinRoomSuccessEvent>(JoinRoomSuccess);
  184. }
  185. private void JoinRoomFail(JoinRoomFailEvent e)
  186. {
  187. Debug.LogError($"RTC:JoinRoomFail");
  188. this.UnRegisterEvent<JoinRoomFailEvent>(JoinRoomFail);
  189. }
  190. #endregion
  191. }