RTCService.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System.Collections.Generic;
  2. using Blue;
  3. using LitJson;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using UnityEngine;
  7. public class RTCService : IRTCService
  8. {
  9. private RTCRoomInfo mRTCRoomInfo;
  10. public Dictionary<int, RTCUserInfo> mUserDic { get; private set; } = new Dictionary<int, RTCUserInfo>(); // 房间用户字典
  11. public void OnInit()
  12. {
  13. // 创建房间
  14. this.RegisterEvent<RTCCreatRoomSuccessEvent>(RTCCreatRoomSuccess);
  15. // 连接RTC
  16. this.RegisterEvent<RTCConnectSuccessEvent>(RTCConnectSuccess);
  17. this.RegisterEvent<RTCConnectFailEvent>(RTCConnectFail);
  18. // 加入房间
  19. this.RegisterEvent<JoinRoomSuccessEvent>(JoinRoomSuccess);
  20. // 其他用户加入房间
  21. this.RegisterEvent<OtherUserJoinRoomEvent>(OtherUserJoinRoom);
  22. this.RegisterEvent<OtherLeaveRoomEvent>(OtherLeaveRoom);
  23. }
  24. /// <summary>
  25. /// 根据Token创建房间
  26. /// </summary>
  27. public void CreatRoom()
  28. {
  29. CoroutineSystem.Instance.StartCoroutine(
  30. HttpTool.Instance.SendHttp(HttpActionLang.rtc_CreateRoom, "", message =>
  31. {
  32. JObject jobject = JObject.Parse(message);
  33. if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString()))
  34. {
  35. this.SendEvent(new RTCCreatRoomSuccessEvent() { rtcRoomInfo = jobject["data"].ToString() });
  36. }
  37. }, "application/x-www-form-urlencoded"));
  38. }
  39. /// <summary>s
  40. /// 加入房间
  41. /// </summary>
  42. public void JoinRoom(string roomId)
  43. {
  44. JsonData data = new JsonData();
  45. data["roomId"] = roomId;
  46. CoroutineSystem.Instance.StartCoroutine(
  47. HttpTool.Instance.SendHttp(HttpActionLang.rtc_JoinRoom, data.ToJson(), message =>
  48. {
  49. JObject jobject = JObject.Parse(message);
  50. if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString()))
  51. {
  52. this.SendEvent(new JoinRoomSuccessEvent() { rtcRoomInfo = jobject["data"].ToString() });
  53. }
  54. }));
  55. }
  56. /// <summary>
  57. /// 根据url连接rtc
  58. /// </summary>
  59. public void ConnectByRTCUrl(string URL)
  60. {
  61. GHZRtcManager.Instance.ConnectRoom("https://"+mRTCRoomInfo.host,mRTCRoomInfo.token);
  62. this.GetService<ISendLogService>().SendLog("Blue","RoomID:"+mRTCRoomInfo.roomId);
  63. this.GetService<ISendLogService>().SendLog("Blue","Host:"+mRTCRoomInfo.host);
  64. this.GetService<ISendLogService>().SendLog("Blue","Token:"+mRTCRoomInfo.token);
  65. //this.SendEvent<RTCConnectSuccessEvent>();
  66. //this.SendEvent<RTCConnectFailEvent>();
  67. }
  68. /// <summary>
  69. /// 退出房间
  70. /// </summary>
  71. public void LeaveRoom()
  72. {
  73. mUserDic.Clear();
  74. GHZRtcManager.Instance.DisconnectRoom();
  75. Debug.LogError($"离开房间");
  76. }
  77. /// <summary>
  78. /// 控制摄像头
  79. /// </summary>
  80. public void ActiveVideo(bool active)
  81. {
  82. GHZRtcManager.Instance.OnWebCam(active);
  83. }
  84. /// <summary>
  85. /// 控制麦克风
  86. /// </summary>
  87. public void ActiveAudio(bool active)
  88. {
  89. GHZRtcManager.Instance.OnMicrophone(active);
  90. Debug.LogError($"ActiveAudio:{active}");
  91. }
  92. /// <summary>
  93. /// 将远程视频流静音
  94. /// </summary>
  95. public void MuteRemoteVideoStream(string userID, bool active)
  96. {
  97. Debug.LogError($"远程视频流===> 用户:{userID},开关:{active}");
  98. }
  99. /// <summary>
  100. /// 将远程音频流静音
  101. /// </summary>
  102. public void MuteRemoteAudioStream(string userID, bool active)
  103. {
  104. Debug.LogError($"远程视频流===> 用户:{userID},开关:{active}");
  105. }
  106. /// <summary>
  107. /// 视频静音
  108. /// </summary>
  109. public void MuteVideo(bool active)
  110. {
  111. Debug.LogError($"视频静音:{active}");
  112. }
  113. /// <summary>
  114. /// 音频静音
  115. /// </summary>
  116. public void MuteAudio(bool active)
  117. {
  118. Debug.LogError($"音频静音{active}");
  119. }
  120. /// <summary>
  121. /// 打开本地视频
  122. /// </summary>
  123. public void LocalVideo(bool active)
  124. {
  125. Debug.LogError($"打开本地视频{active}");
  126. }
  127. /// <summary>
  128. /// 打开本地音频
  129. /// </summary>
  130. public void LoacalAudio(bool active)
  131. {
  132. Debug.LogError($"打开本地音频{active}");
  133. }
  134. #region 事件
  135. // 创建房间
  136. private void RTCCreatRoomSuccess(RTCCreatRoomSuccessEvent e)
  137. {
  138. mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(e.rtcRoomInfo);
  139. Debug.LogError($"创建房间成功,房间ID:{mRTCRoomInfo.roomId}");
  140. Debug.LogError($"地址链接:https://{mRTCRoomInfo.host}");
  141. Debug.LogError($"Token:{mRTCRoomInfo.token}");
  142. ConnectByRTCUrl(mRTCRoomInfo.host);
  143. }
  144. // 加入房间
  145. private void JoinRoomSuccess(JoinRoomSuccessEvent e)
  146. {
  147. mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(e.rtcRoomInfo);
  148. Debug.LogError($"创建房间成功,房间ID:{mRTCRoomInfo.roomId}");
  149. Debug.LogError($"地址链接:https://{mRTCRoomInfo.host}");
  150. Debug.LogError($"Token:{mRTCRoomInfo.token}");
  151. ConnectByRTCUrl(mRTCRoomInfo.host);
  152. }
  153. // 连接RTC
  154. private void RTCConnectSuccess(RTCConnectSuccessEvent e)
  155. {
  156. Debug.LogError($"RTC:RTCConnectSuccess");
  157. this.UnRegisterEvent<RTCConnectSuccessEvent>(RTCConnectSuccess);
  158. }
  159. private void RTCConnectFail(RTCConnectFailEvent e)
  160. {
  161. Debug.LogError($"RTC:RTCConnectFail");
  162. this.UnRegisterEvent<RTCConnectFailEvent>(RTCConnectFail);
  163. }
  164. // 其他用户
  165. private void OtherUserJoinRoom(OtherUserJoinRoomEvent e)
  166. {
  167. RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject<RTCUserInfo>(e.rtcUserInfoJsonString);
  168. if (!mUserDic.ContainsKey(RTCUserInfo.UserID))
  169. {
  170. mUserDic.Add(RTCUserInfo.UserID, RTCUserInfo);
  171. }
  172. this.UnRegisterEvent<OtherUserJoinRoomEvent>(OtherUserJoinRoom);
  173. }
  174. private void OtherLeaveRoom(OtherLeaveRoomEvent e)
  175. {
  176. RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject<RTCUserInfo>(e.rtcUserInfoJsonString);
  177. if (mUserDic.ContainsKey(RTCUserInfo.UserID))
  178. {
  179. mUserDic.Remove(RTCUserInfo.UserID);
  180. }
  181. this.UnRegisterEvent<OtherLeaveRoomEvent>(OtherLeaveRoom);
  182. }
  183. #endregion
  184. }