RTCRoomManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Newtonsoft.Json;
  6. using LitJson;
  7. using Newtonsoft.Json.Linq;
  8. public class RTCRoomManager : MonoSingleton<RTCRoomManager>
  9. {
  10. public ParticipantView expertView;
  11. public RawImage rawImage;
  12. public RTCRoomInfo mRTCRoomInfo;
  13. public string roomId;
  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()
  75. {
  76. Debug.Log("DGJ =====> JoinRoomSuccess");
  77. // mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(e.rtcRoomInfo);
  78. // UILogManager.Instance.text2.text = mRTCRoomInfo.roomId.ToString();
  79. UICallMachineRoomData.Instance.SetMQTTRoomId(mRTCRoomInfo.roomId.ToString());
  80. }
  81. public void CreateRoomSuccess(string roomId)
  82. {
  83. this.roomId = roomId;
  84. UICallMachineRoomData.Instance.SetMQTTRoomId(roomId);
  85. }
  86. public void OnLocalUserJoined()
  87. {
  88. // UserJoined(UserEnum.Player);
  89. Debug.Log("DGJ ===>OnLocalUserJoined " + MQTTManager.Instance.front + "online/" + controlId);
  90. if(isInvite) // 邀请中控
  91. {
  92. isInvite = false;
  93. JsonData obj = new JsonData();
  94. obj["action"] = "invite";
  95. obj["rtcRoom"] = roomId;
  96. obj["personnel"] = new JsonData();
  97. obj["personnel"]["id"] = MQTTManager.Instance.id;
  98. obj["personnel"]["name"] = MQTTManager.Instance._username;
  99. // obj["personnel"]["resourcePoo1"] = MQTTManager.Instance.resourcePool;
  100. obj["personnel"]["resourcePoolId"] = MQTTManager.Instance.resourcePoolId;
  101. obj["personnel"]["room"] = MQTTManager.Instance.room;
  102. obj["personnel"]["phone"] = MQTTManager.Instance.phone;
  103. obj["device"] = new JsonData();
  104. obj["device"]["deviceCode"] = "ROOM";
  105. obj["roomId"] = roomId;
  106. // 用MQTT 向中考发送邀请
  107. MQTTManager.Instance.PushMsg(MQTTManager.Instance.front + "room/RTC" , obj.ToJson());
  108. }
  109. if (isApply) // 当前在RTC 频道内
  110. {
  111. isApply = false;
  112. JsonData data = new JsonData();
  113. data["roomId"] = roomId;
  114. data["questionCategory"] = (int)question;
  115. data["deviceUniqueId"] = GetAndroidSNIEMI.GetDeviceUniqueIdMD5();
  116. Debug.Log("DGJ =====> " + data.ToJson());
  117. StartCoroutine(HttpToolLangChao.Instance.SendHttp(HttpActionLang.apply, data.ToJson(), message =>
  118. {
  119. Debug.Log("DGJ ===> HttpActionLang.apply " + HttpActionLang.apply + " " + message);
  120. JObject jobject = JObject.Parse(message);
  121. if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString()))
  122. {
  123. assistanceId = int.Parse(jobject["data"]["assistanceId"].ToString());
  124. Debug.Log("DGJ ===> " + assistanceId);
  125. // Debug.Log("DGJ ===> "+ transform.position.sqrMagnitude+ transform.CompareTag(""));
  126. // ScenesManager.Instance.showWindow(ScenesManager.SceneType.ShowRoom); // 等待收到专家入场
  127. }
  128. }));
  129. }
  130. }
  131. public void CancelApply()
  132. {
  133. if(assistanceId!=-1)
  134. {
  135. JsonData data = new JsonData();
  136. data["assistanceId"] = assistanceId;
  137. StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.cancel_Apply, data.ToJson(), message =>
  138. {
  139. Debug.Log("DGJ ===> HttpActionLang.cancel_Apply " + HttpActionLang.cancel_Apply + " " + message);
  140. }));
  141. assistanceId = -1;
  142. }
  143. }
  144. public void ExpertJonin(string identity)
  145. {
  146. RTCManager.Instance.ShowViewRawImage(identity, rawImage);
  147. }
  148. public void UserJoined( UserEnum userEnum , JObject metadata = null)
  149. {
  150. Debug.Log("DGJ ===> " + rtcState);
  151. switch (userEnum)
  152. {
  153. case UserEnum.Player:
  154. if (rtcState == RTCEnum.OFF)
  155. OnLocalUserJoined();
  156. rtcState = RTCEnum.Online;
  157. Debug.Log(" DGJ ===> UserJoined 自己 ");
  158. break;
  159. case UserEnum.Expert:
  160. expertData = metadata;
  161. switch (rtcState)
  162. {
  163. case RTCEnum.OFF:
  164. case RTCEnum.Online:
  165. case RTCEnum.ExpertOnline:
  166. rtcState = RTCEnum.ExpertOnline;
  167. break;
  168. case RTCEnum.ConterlOnline:
  169. rtcState = RTCEnum.ALLOnline;
  170. break;
  171. case RTCEnum.ALLOnline:
  172. Debug.LogError("RTC 房间状态有误 " );
  173. break;
  174. default:
  175. break;
  176. }
  177. Debug.Log(" DGJ ===> UserJoined 专家 ");
  178. break;
  179. case UserEnum.Conterl:
  180. controlData = metadata;
  181. switch (rtcState)
  182. {
  183. case RTCEnum.OFF:
  184. case RTCEnum.Online:
  185. rtcState = RTCEnum.ConterlOnline;
  186. break;
  187. case RTCEnum.ExpertOnline:
  188. rtcState = RTCEnum.ALLOnline;
  189. break;
  190. case RTCEnum.ConterlOnline:
  191. rtcState = RTCEnum.ConterlOnline;
  192. break;
  193. case RTCEnum.ALLOnline:
  194. Debug.LogError("RTC 房间状态有误 ");
  195. break;
  196. default:
  197. break;
  198. }
  199. Debug.Log(" DGJ ===> UserJoined 中控 ");
  200. break;
  201. default:
  202. break;
  203. }
  204. Debug.Log("DGJ ===> " + rtcState);
  205. }
  206. public void UserOffline(string uid)
  207. {
  208. Participant participant = RTCMainManager.Instance.QuesUser(uid);
  209. if(expertData!=null&& participant.identity == expertData["name"].ToString() )
  210. {
  211. UserOffline(UserEnum.Expert);
  212. Debug.Log("DGJ ===> UserOffline 专家 "+ uid);
  213. }else if(controlData!=null && participant.identity == controlData["name"].ToString())
  214. {
  215. UserOffline(UserEnum.Conterl);
  216. Debug.Log("DGJ ===> UserOffline 中控 " + uid);
  217. }
  218. }
  219. public void UserOffline(UserEnum userEnum)
  220. {
  221. switch (userEnum)
  222. {
  223. case UserEnum.Player:
  224. rtcState = RTCEnum.OFF;
  225. break;
  226. case UserEnum.Expert:
  227. switch (rtcState)
  228. {
  229. case RTCEnum.OFF:
  230. case RTCEnum.Online:
  231. case RTCEnum.ExpertOnline:
  232. rtcState = RTCEnum.Online;
  233. break;
  234. case RTCEnum.ConterlOnline:
  235. Debug.LogError("RTC 房间状态有误 ");
  236. break;
  237. case RTCEnum.ALLOnline:
  238. rtcState = RTCEnum.ConterlOnline;
  239. break;
  240. default:
  241. break;
  242. }
  243. break;
  244. case UserEnum.Conterl:
  245. switch (rtcState)
  246. {
  247. case RTCEnum.OFF:
  248. case RTCEnum.Online:
  249. case RTCEnum.ConterlOnline:
  250. rtcState = RTCEnum.Online;
  251. break;
  252. case RTCEnum.ExpertOnline:
  253. Debug.LogError("RTC 房间状态有误 ");
  254. break;
  255. case RTCEnum.ALLOnline:
  256. rtcState = RTCEnum.ExpertOnline;
  257. break;
  258. default:
  259. break;
  260. }
  261. break;
  262. default:
  263. break;
  264. }
  265. if (rtcState != RTCEnum.OFF && !(ScenesManager.Instance.getWindow() == ScenesManager.SceneType.ShowRoom || ScenesManager.Instance.getWindow() == ScenesManager.SceneType.ShowRTC))
  266. {
  267. Debug.Log(" GETWindow Scenes " + ScenesManager.Instance.getWindow());
  268. // 退出房间
  269. // LeaveChannel();
  270. }
  271. //if(rtcState == RTCEnum.Online && !(ScenesManager.Instance.getWindow() == ScenesManager.SceneType.ShowRoom || ScenesManager.Instance.getWindow() == ScenesManager.SceneType.ShowRTC))
  272. //{
  273. // Debug.Log(" GETWindow Scenes " + ScenesManager.Instance.getWindow());
  274. // // 退出房间
  275. // LeaveChannel();
  276. //}
  277. }
  278. public void LeaveChannel()
  279. {
  280. Debug.LogError(" RTCRoomManager 退出房间");
  281. Debug.Log("DGJ ===> MQTT Test 退出房间 ");
  282. MQTTManager.Instance.UnSubscribe(MQTTManager.Instance.front + "room/" +MQTTManager.Instance.roomId);
  283. RTCManager.Instance.LeaveChannel();
  284. rtcState = RTCEnum.OFF;
  285. ClearRoomData();
  286. }
  287. private void ClearRoomData()
  288. {
  289. expertView = null;
  290. mRTCRoomInfo = null;
  291. controlId = "";
  292. // question = QuestionEnum.NetWork;
  293. roomId = null;
  294. expertData = null;
  295. controlData = null;
  296. }
  297. }
  298. public enum RTCEnum
  299. {
  300. /// <summary>
  301. /// 离线
  302. /// </summary>
  303. OFF,
  304. /// <summary>
  305. /// 当前只有眼镜端在房间
  306. /// </summary>
  307. Online,
  308. /// <summary>
  309. /// 眼镜端和专家端都在房间
  310. /// </summary>
  311. ExpertOnline,
  312. /// <summary>
  313. /// 眼镜端和控制台都在房间
  314. /// </summary>
  315. ConterlOnline,
  316. /// <summary>
  317. /// 眼镜、专家、控制台 都在房间
  318. /// </summary>
  319. ALLOnline
  320. }
  321. public enum UserEnum
  322. {
  323. Player,
  324. Expert,
  325. Conterl
  326. }
  327. public enum QuestionEnum
  328. {
  329. NetWork = 1,
  330. Host = 2,
  331. Storage = 3
  332. }