GHZRTCManager.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using Blue;
  2. using GHZRtc;
  3. using LitJson;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. /// <summary>
  9. /// 重新写的
  10. /// </summary>
  11. public class GHZRTCManager : SingletonMono<GHZRTCManager>, IController
  12. {
  13. /// <summary>
  14. /// 自身UID
  15. /// </summary>
  16. private string uid;
  17. /// <summary>
  18. /// 当前频道号
  19. /// </summary>
  20. private string channelName;
  21. public string UID
  22. {
  23. get => uid;
  24. set
  25. {
  26. if (value != null)
  27. this.uid = value;
  28. }
  29. }
  30. public string ChannelName
  31. {
  32. get => channelName;
  33. set
  34. {
  35. if (value != null)
  36. this.channelName = value;
  37. }
  38. }
  39. /// <summary>
  40. /// 初始化
  41. /// </summary>
  42. public void Init()
  43. {
  44. Debug.LogError("Init");
  45. GHZRtcManager.Instance.Init();
  46. }
  47. public string RoomID;
  48. public void JoinTest()
  49. {
  50. EndustryLoginUserInfo sendLogin = new EndustryLoginUserInfo()
  51. {
  52. account = "cy2",
  53. password = "1"
  54. };
  55. string jsonLogin = JsonMapper.ToJson(sendLogin);
  56. HttpTool.Instance.PostLogin("https://api-fat1.ghz-tech.com" + HttpActionLang.login, jsonLogin, mes =>
  57. {
  58. JObject obj = JObject.Parse(mes);
  59. if (obj["code"].ToString() == "200")
  60. {
  61. if (!string.IsNullOrWhiteSpace(obj["data"].ToString()) && !string.IsNullOrWhiteSpace(obj["data"]["token"].ToString()))
  62. {
  63. login.UserInfo.Instance.Token = obj["data"]["token"].ToString();
  64. JsonData data = new JsonData();
  65. data["roomId"] = RoomID;
  66. StartCoroutine(HttpTool.Instance.SendHttp("https://api-fat1.ghz-tech.com" + HttpActionLang.rtc_JoinRoom, data.ToJson(), message =>
  67. {
  68. JObject jobject = JObject.Parse(message);
  69. if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString()))
  70. {
  71. Debug.LogError($"加入房间:{jobject["data"].ToString()}");
  72. RTCRoomInfo mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(jobject["data"].ToString());
  73. Debug.LogError($"加入房间成功,房间ID:{mRTCRoomInfo.roomId}");
  74. Debug.LogError($"地址链接:https://{mRTCRoomInfo.host}");
  75. Debug.LogError($"Token:{mRTCRoomInfo.token}");
  76. GHZRtcManager.Instance.ConnectRoom(mRTCRoomInfo.host, mRTCRoomInfo.token);
  77. }
  78. }));
  79. }
  80. }
  81. });
  82. }
  83. /// <summary>
  84. /// 进入房间
  85. /// </summary>
  86. public void JoinChannel(string url, string token)
  87. {
  88. GHZRtcManager.Instance.ConnectRoom(url, token, false);
  89. }
  90. /// <summary>
  91. /// 退出房间
  92. /// </summary>
  93. public void LeaveChannel()
  94. {
  95. GHZRtcManager.Instance.DisconnectRoom();
  96. }
  97. /// <summary>
  98. /// 显示用户画面
  99. /// </summary>
  100. /// <param name="uid"> 用户的UID</param>
  101. /// <param name="rawImage"> 需要显示的RawImage</param>
  102. public void ShowViewRawImage(string uid, RawImage rawImage)
  103. {
  104. foreach (var i in GHZRtcManager.Instance.remoteParticipantViews.Keys)
  105. {
  106. if (i.sid == uid)
  107. {
  108. rawImage.texture = GHZRtcManager.Instance.remoteParticipantViews[i].VideoView;
  109. }
  110. }
  111. // var localVideoViewARF = (AspectRatioFitter)localVideoView.GetComponent<AspectRatioFitter>();
  112. // localVideoViewARF.aspectRatio = (float)videoTrack.Texture.width / videoTrack.Texture.height;
  113. }
  114. /// <summary>
  115. /// 显示用户画面
  116. /// </summary>
  117. /// <param name="uid"> 用户的UID</param>
  118. /// <param name="mesh"> 需要显示的Mesh</param>
  119. public void ShowViewMeshRenderer(string uid, MeshRenderer mesh)
  120. {
  121. foreach (var i in GHZRtcManager.Instance.remoteParticipantViews.Keys)
  122. {
  123. if (i.sid == uid)
  124. {
  125. mesh.material.mainTexture = GHZRtcManager.Instance.remoteParticipantViews[i].VideoView;
  126. }
  127. }
  128. }
  129. /// <summary>
  130. /// 开关自身音频
  131. /// </summary>
  132. /// <param name="isAudio"></param>
  133. public void MuteLocalAudioStream(bool isAudio)
  134. {
  135. GHZRtcManager.Instance.OnMicrophone(isAudio);
  136. }
  137. /// <summary>
  138. /// 开关自身视频
  139. /// </summary>
  140. /// <param name="isVideo"></param>
  141. public void MuteLocalVideoStream(bool isVideo)
  142. {
  143. GHZRtcManager.Instance.OnWebCam(isVideo);
  144. }
  145. /// <summary>
  146. /// 订阅/取订 用户音频
  147. /// </summary>
  148. /// <param name="uid"></param>
  149. /// <param name="isAudio"></param>
  150. public void MuteRemoteAudioStream(string uid, bool isAudio)
  151. {
  152. foreach (var i in GHZRtcManager.Instance.remoteParticipantViews.Keys)
  153. {
  154. if (i.sid == uid)
  155. {
  156. if (i.audioTracks.Count > 0)
  157. {
  158. for (int j = 0; j < i.audioTracks.Count; j++)
  159. {
  160. string sid = i.audioTracks[j].sid;
  161. i.Room.engine.signalClient.SendMuteTrack(sid, !isAudio);
  162. }
  163. }
  164. }
  165. }
  166. }
  167. /// <summary>
  168. /// 订阅/取订 用户视频
  169. /// </summary>
  170. /// <param name="uid"></param>
  171. /// <param name="isVideo"></param>
  172. public void MuteRemoteVideoStream(string uid, bool isVideo)
  173. {
  174. foreach (var i in GHZRtcManager.Instance.remoteParticipantViews.Keys)
  175. {
  176. if (i.sid == uid)
  177. {
  178. if (i.videoTracks.Count > 0)
  179. {
  180. for (int j = 0; j < i.videoTracks.Count; j++)
  181. {
  182. string sid = i.videoTracks[j].sid;
  183. i.Room.engine.signalClient.SendMuteTrack(sid, !isVideo);
  184. }
  185. }
  186. }
  187. }
  188. }
  189. public delegate void OnRemoteAudioStateChangedEvent(string uid, REMOTE_AUIDO_STATE_REASON_RTC state);
  190. public static OnRemoteAudioStateChangedEvent OnRemoteAudioStateChanged;
  191. /// <summary>
  192. /// 用户音频状态发生变化的回调
  193. /// </summary>
  194. /// <param name="uid"></param>
  195. /// <param name="state"> 音频状态 </param>
  196. public void RemoteAudioStateChanged(string uid, REMOTE_AUIDO_STATE_REASON_RTC state)
  197. {
  198. Debug.Log("uid=RemoteAudioStateChanged=>" + state);
  199. OnRemoteAudioStateChanged?.Invoke(uid, state);
  200. }
  201. public delegate void OnRemoteVideoStateChangedEvent(string uid, REMOTE_VIDEO_STATE_REASON_RTC state);
  202. public static OnRemoteVideoStateChangedEvent OnRemoteVideoStateChanged;
  203. /// <summary>
  204. /// 用户视频状态发生变化的回调
  205. /// </summary>
  206. /// <param name="uid"></param>
  207. /// <param name="state"> 视频状态 </param>
  208. public void RemoteVideoStateChanged(string uid, REMOTE_VIDEO_STATE_REASON_RTC state)
  209. {
  210. Debug.Log("uid=RemoteVideoStateChanged=>" + state);
  211. OnRemoteVideoStateChanged?.Invoke(uid, state);
  212. }
  213. public delegate void OnUserJoinedEvent(string uid);
  214. public static OnUserJoinedEvent OnUserJoined;
  215. /// <summary>
  216. /// 有用户进入频道的回调
  217. /// </summary>
  218. /// <param name="uid"></param>
  219. public void UserJoined(string uid)
  220. {
  221. Debug.Log("uid=UserJoined=>" + uid);
  222. OnUserJoined?.Invoke(uid);
  223. }
  224. public delegate void OnUserOfflineEvent(string uid);
  225. public static OnUserOfflineEvent OnUserOffline;
  226. /// <summary>
  227. /// 有用户退出频道的回调
  228. /// </summary>
  229. /// <param name="uid"></param>
  230. public void UserOffline(string uid)
  231. {
  232. Debug.Log("uid=UserOffline=>" + uid);
  233. OnUserOffline?.Invoke(uid);
  234. }
  235. }