RTCRoomManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Blue;
  6. using Newtonsoft.Json;
  7. using LitJson;
  8. using Newtonsoft.Json.Linq;
  9. public class RTCRoomManager : MonoSingleton<RTCRoomManager>, IController
  10. {
  11. public ParticipantView expertView;
  12. public RawImage rawImage;
  13. public RTCRoomInfo mRTCRoomInfo;
  14. public QuestionEnum question;
  15. public RTCEnum rtcState;
  16. public string controlId;
  17. public JObject expertData;
  18. public JObject controlData;
  19. public bool isRTCreate;
  20. public GameObject audio1;
  21. public GameObject audio2;
  22. public Dictionary<string, AudioSource> dicAudio;
  23. /// <summary>
  24. /// 是否邀请中控
  25. /// </summary>
  26. public bool isInvite = false;
  27. /// <summary>
  28. /// 是否申请协助
  29. /// </summary>
  30. public bool isApply = false;
  31. private int assistanceId = -1;
  32. private void Start()
  33. {
  34. this.RegisterEvent<RTCCreatRoomSuccessEvent>(CreatRoomSuccess).UnRegisterWhenGameObjectDestroyed(gameObject);
  35. rtcState = RTCEnum.OFF;
  36. question = QuestionEnum.NetWork;
  37. isRTCreate = false;
  38. RTCManager.OnRemoteVideoStateChanged += RemoteVideoStateChanged;
  39. }
  40. private void Update()
  41. {
  42. if(isRTCreate)
  43. {
  44. isRTCreate = false;
  45. this.GetService<IRTCService>().CreatRoom();
  46. }
  47. // Debug.Log(" GETWindow Scenes " + ScenesManager.Instance.getWindow());
  48. }
  49. private void RemoteVideoStateChanged(string uid, REMOTE_VIDEO_STATE_REASON_RTC state)
  50. {
  51. Debug.Log("RemoteVideoStateChanged " + uid +" "+ state.ToString());
  52. switch (state)
  53. {
  54. case REMOTE_VIDEO_STATE_REASON_RTC.REMOTE_VIDEO_STATE_REASON_NETWORK_CONGESTION:
  55. break;
  56. case REMOTE_VIDEO_STATE_REASON_RTC.REMOTE_VIDEO_STATE_REASON_NETWORK_RECOVERY:
  57. break;
  58. case REMOTE_VIDEO_STATE_REASON_RTC.REMOTE_VIDEO_STATE_REASON_LOCAL_MUTED:
  59. break;
  60. case REMOTE_VIDEO_STATE_REASON_RTC.REMOTE_VIDEO_STATE_REASON_LOCAL_UNMUTED:
  61. break;
  62. case REMOTE_VIDEO_STATE_REASON_RTC.REMOTE_VIDEO_STATE_REASON_REMOTE_MUTED:
  63. LiveRTCRoomMain.Instance.rtcRawImage.gameObject.SetActive(false);
  64. break;
  65. case REMOTE_VIDEO_STATE_REASON_RTC.REMOTE_VIDEO_STATE_REASON_REMOTE_UNMUTED:
  66. LiveRTCRoomMain.Instance.rtcRawImage.gameObject.SetActive(true);
  67. break;
  68. case REMOTE_VIDEO_STATE_REASON_RTC.REMOTE_VIDEO_STATE_REASON_REMOTE_OFFLINE:
  69. break;
  70. default:
  71. break;
  72. }
  73. }
  74. private void CreatRoomSuccess(RTCCreatRoomSuccessEvent e)
  75. {
  76. Debug.Log("DGJ =====> JoinRoomSuccess");
  77. mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(e.rtcRoomInfo);
  78. UILogManager.Instance.text2.text = mRTCRoomInfo.roomId.ToString();
  79. UICallManager.Instance.SetMQTTRoomId(mRTCRoomInfo.roomId.ToString());
  80. }
  81. public void OnLocalUserJoined()
  82. {
  83. // UserJoined(UserEnum.Player);
  84. Debug.Log("DGJ ===>OnLocalUserJoined " + isInvite + isApply);
  85. if(isInvite) // 邀请中控
  86. {
  87. isInvite = false;
  88. JsonData obj = new JsonData();
  89. obj["action"] = "invite";
  90. obj["rtcRoom"] = mRTCRoomInfo.roomId.ToString();
  91. obj["personnel"] = new JsonData();
  92. obj["personnel"]["id"] = MQTTManager.Instance.id;
  93. obj["personnel"]["name"] = MQTTManager.Instance._username;
  94. // obj["personnel"]["resourcePoo1"] = MQTTManager.Instance.resourcePool;
  95. obj["personnel"]["resourcePoolId"] = MQTTManager.Instance.resourcePoolId;
  96. obj["personnel"]["room"] = MQTTManager.Instance.room;
  97. obj["personnel"]["phone"] = MQTTManager.Instance.phone;
  98. // 用MQTT 向中考发送邀请
  99. MQTTManager.Instance.PushMsg(MQTTManager.Instance.front + "online/" + controlId, obj.ToJson());
  100. }
  101. if (isApply) // 当前在RTC 频道内
  102. {
  103. isApply = false;
  104. JsonData data = new JsonData();
  105. data["roomId"] = mRTCRoomInfo.roomId.ToString();
  106. data["questionCategory"] = (int)question;
  107. data["deviceUniqueId"] = GetAndroidSNIEMI.GetDeviceUniqueIdMD5();
  108. Debug.Log("DGJ =====> " + data.ToJson());
  109. StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.apply, data.ToJson(), message =>
  110. {
  111. Debug.Log("DGJ ===> HttpActionLang.apply " + HttpActionLang.apply + " " + message);
  112. JObject jobject = JObject.Parse(message);
  113. if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString()))
  114. {
  115. assistanceId = int.Parse(jobject["data"]["assistanceId"].ToString());
  116. Debug.Log("DGJ ===> " + assistanceId);
  117. // Debug.Log("DGJ ===> "+ transform.position.sqrMagnitude+ transform.CompareTag(""));
  118. // ScenesManager.Instance.showWindow(ScenesManager.SceneType.ShowRoom); // 等待收到专家入场
  119. }
  120. }));
  121. }
  122. }
  123. public void Apply()
  124. {
  125. JsonData data = new JsonData();
  126. data["roomId"] = mRTCRoomInfo.roomId.ToString();
  127. data["questionCategory"] = (int)question;
  128. data["deviceUniqueId"] = GetAndroidSNIEMI.GetDeviceUniqueIdMD5();
  129. Debug.Log("DGJ =====> " + data.ToJson());
  130. StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.apply, data.ToJson(), message =>
  131. {
  132. Debug.Log("DGJ ===> HttpActionLang.apply " + HttpActionLang.apply + " " + message);
  133. JObject jobject = JObject.Parse(message);
  134. if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString()))
  135. {
  136. assistanceId = int.Parse(jobject["data"]["assistanceId"].ToString());
  137. Debug.Log("DGJ ===> " + assistanceId);
  138. // Debug.Log("DGJ ===> "+ transform.position.sqrMagnitude+ transform.CompareTag(""));
  139. // ScenesManager.Instance.showWindow(ScenesManager.SceneType.ShowRoom); // 等待收到专家入场
  140. ScenesManager.Instance.showWindow(ScenesManager.SceneType.ShowRoom);
  141. }
  142. }));
  143. }
  144. public void CancelApply()
  145. {
  146. if(assistanceId!=-1)
  147. {
  148. JsonData data = new JsonData();
  149. data["assistanceId"] = assistanceId;
  150. StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.cancel_Apply, data.ToJson(), message =>
  151. {
  152. Debug.Log("DGJ ===> HttpActionLang.cancel_Apply " + HttpActionLang.cancel_Apply + " " + message);
  153. }));
  154. assistanceId = -1;
  155. }
  156. }
  157. public void ExpertJonin(string identity)
  158. {
  159. RTCManager.Instance.ShowViewRawImage(identity, rawImage);
  160. }
  161. public void UserJoined( UserEnum userEnum , JObject metadata = null)
  162. {
  163. Debug.Log("DGJ ===> " + rtcState);
  164. switch (userEnum)
  165. {
  166. case UserEnum.Player:
  167. if (rtcState == RTCEnum.OFF)
  168. OnLocalUserJoined();
  169. rtcState = RTCEnum.Online;
  170. Debug.Log(" DGJ ===> UserJoined 自己 ");
  171. break;
  172. case UserEnum.Expert:
  173. expertData = metadata;
  174. switch (rtcState)
  175. {
  176. case RTCEnum.OFF:
  177. case RTCEnum.Online:
  178. case RTCEnum.ExpertOnline:
  179. rtcState = RTCEnum.ExpertOnline;
  180. break;
  181. case RTCEnum.ConterlOnline:
  182. rtcState = RTCEnum.ALLOnline;
  183. break;
  184. case RTCEnum.ALLOnline:
  185. Debug.LogError("RTC 房间状态有误 " );
  186. break;
  187. default:
  188. break;
  189. }
  190. Debug.Log(" DGJ ===> UserJoined 专家 ");
  191. break;
  192. case UserEnum.Conterl:
  193. controlData = metadata;
  194. switch (rtcState)
  195. {
  196. case RTCEnum.OFF:
  197. case RTCEnum.Online:
  198. rtcState = RTCEnum.ConterlOnline;
  199. break;
  200. case RTCEnum.ExpertOnline:
  201. rtcState = RTCEnum.ALLOnline;
  202. break;
  203. case RTCEnum.ConterlOnline:
  204. rtcState = RTCEnum.ConterlOnline;
  205. break;
  206. case RTCEnum.ALLOnline:
  207. Debug.LogError("RTC 房间状态有误 ");
  208. break;
  209. default:
  210. break;
  211. }
  212. Debug.Log(" DGJ ===> UserJoined 中控 ");
  213. break;
  214. default:
  215. break;
  216. }
  217. Debug.Log("DGJ ===> " + rtcState);
  218. }
  219. public void UserOffline(string uid)
  220. {
  221. Participant participant = RTCManager.Instance.QuesUser(uid);
  222. if(expertData!=null&& participant.identity == expertData["name"].ToString() )
  223. {
  224. UserOffline(UserEnum.Expert);
  225. Debug.Log("DGJ ===> UserOffline 专家 "+ uid);
  226. }else if(controlData!=null && participant.identity == controlData["name"].ToString())
  227. {
  228. UserOffline(UserEnum.Conterl);
  229. Debug.Log("DGJ ===> UserOffline 中控 " + uid);
  230. }
  231. }
  232. public void UserOffline(UserEnum userEnum)
  233. {
  234. switch (userEnum)
  235. {
  236. case UserEnum.Player:
  237. rtcState = RTCEnum.OFF;
  238. break;
  239. case UserEnum.Expert:
  240. switch (rtcState)
  241. {
  242. case RTCEnum.OFF:
  243. case RTCEnum.Online:
  244. case RTCEnum.ExpertOnline:
  245. rtcState = RTCEnum.Online;
  246. break;
  247. case RTCEnum.ConterlOnline:
  248. Debug.LogError("RTC 房间状态有误 ");
  249. break;
  250. case RTCEnum.ALLOnline:
  251. rtcState = RTCEnum.ConterlOnline;
  252. break;
  253. default:
  254. break;
  255. }
  256. break;
  257. case UserEnum.Conterl:
  258. switch (rtcState)
  259. {
  260. case RTCEnum.OFF:
  261. case RTCEnum.Online:
  262. case RTCEnum.ConterlOnline:
  263. rtcState = RTCEnum.Online;
  264. break;
  265. case RTCEnum.ExpertOnline:
  266. Debug.LogError("RTC 房间状态有误 ");
  267. break;
  268. case RTCEnum.ALLOnline:
  269. rtcState = RTCEnum.ExpertOnline;
  270. break;
  271. default:
  272. break;
  273. }
  274. break;
  275. default:
  276. break;
  277. }
  278. if (rtcState != RTCEnum.OFF && !(ScenesManager.Instance.getWindow() == ScenesManager.SceneType.ShowRoom || ScenesManager.Instance.getWindow() == ScenesManager.SceneType.ShowRTC))
  279. {
  280. Debug.Log(" GETWindow Scenes " + ScenesManager.Instance.getWindow());
  281. // 退出房间
  282. // LeaveChannel();
  283. }
  284. //if(rtcState == RTCEnum.Online && !(ScenesManager.Instance.getWindow() == ScenesManager.SceneType.ShowRoom || ScenesManager.Instance.getWindow() == ScenesManager.SceneType.ShowRTC))
  285. //{
  286. // Debug.Log(" GETWindow Scenes " + ScenesManager.Instance.getWindow());
  287. // // 退出房间
  288. // LeaveChannel();
  289. //}
  290. }
  291. public void LeaveChannel()
  292. {
  293. Debug.LogError(" RTCRoomManager 退出房间");
  294. Debug.Log("DGJ ===> MQTT Test 退出房间 ");
  295. MQTTManager.Instance.UnSubscribe(MQTTManager.Instance.roomId);
  296. RTCManager.Instance.LeaveChannel();
  297. rtcState = RTCEnum.OFF;
  298. ClearRoomData();
  299. }
  300. private void ClearRoomData()
  301. {
  302. expertView = null;
  303. mRTCRoomInfo = null;
  304. controlId = "";
  305. // question = QuestionEnum.NetWork;
  306. expertData = null;
  307. controlData = null;
  308. }
  309. }
  310. public enum RTCEnum
  311. {
  312. /// <summary>
  313. /// 离线
  314. /// </summary>
  315. OFF,
  316. /// <summary>
  317. /// 当前只有眼镜端在房间
  318. /// </summary>
  319. Online,
  320. /// <summary>
  321. /// 眼镜端和专家端都在房间
  322. /// </summary>
  323. ExpertOnline,
  324. /// <summary>
  325. /// 眼镜端和控制台都在房间
  326. /// </summary>
  327. ConterlOnline,
  328. /// <summary>
  329. /// 眼镜、专家、控制台 都在房间
  330. /// </summary>
  331. ALLOnline
  332. }
  333. public enum UserEnum
  334. {
  335. Player,
  336. Expert,
  337. Conterl
  338. }