using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Newtonsoft.Json; using LitJson; using Newtonsoft.Json.Linq; public class RTCRoomManager : MonoSingleton { public ParticipantView expertView; public RawImage rawImage; public RTCRoomInfo mRTCRoomInfo; public string roomId; public QuestionEnum question; public RTCEnum rtcState; public string controlId; public JObject expertData; public JObject controlData; public bool isRTCreate; public GameObject audio1; public GameObject audio2; public Dictionary dicAudio; /// /// 是否邀请中控 /// public bool isInvite = false; /// /// 是否申请协助 /// public bool isApply = false; private int assistanceId = -1; private void Start() { // this.RegisterEvent(CreatRoomSuccess).UnRegisterWhenGameObjectDestroyed(gameObject); rtcState = RTCEnum.OFF; question = QuestionEnum.NetWork; isRTCreate = false; RTCManager.OnRemoteVideoStateChanged += RemoteVideoStateChanged; } private void Update() { if(isRTCreate) { isRTCreate = false; // this.GetService().CreatRoom(); } // Debug.Log(" GETWindow Scenes " + ScenesManager.Instance.getWindow()); } private void RemoteVideoStateChanged(string uid, REMOTE_VIDEO_STATE_REASON_RTC state) { Debug.Log("RemoteVideoStateChanged " + uid +" "+ state.ToString()); switch (state) { case REMOTE_VIDEO_STATE_REASON_RTC.REMOTE_VIDEO_STATE_REASON_NETWORK_CONGESTION: break; case REMOTE_VIDEO_STATE_REASON_RTC.REMOTE_VIDEO_STATE_REASON_NETWORK_RECOVERY: break; case REMOTE_VIDEO_STATE_REASON_RTC.REMOTE_VIDEO_STATE_REASON_LOCAL_MUTED: break; case REMOTE_VIDEO_STATE_REASON_RTC.REMOTE_VIDEO_STATE_REASON_LOCAL_UNMUTED: break; case REMOTE_VIDEO_STATE_REASON_RTC.REMOTE_VIDEO_STATE_REASON_REMOTE_MUTED: LiveRTCRoomMain.Instance.rtcRawImage.gameObject.SetActive(false); break; case REMOTE_VIDEO_STATE_REASON_RTC.REMOTE_VIDEO_STATE_REASON_REMOTE_UNMUTED: LiveRTCRoomMain.Instance.rtcRawImage.gameObject.SetActive(true); break; case REMOTE_VIDEO_STATE_REASON_RTC.REMOTE_VIDEO_STATE_REASON_REMOTE_OFFLINE: break; default: break; } } private void CreatRoomSuccess() { Debug.Log("DGJ =====> JoinRoomSuccess"); // mRTCRoomInfo = JsonConvert.DeserializeObject(e.rtcRoomInfo); // UILogManager.Instance.text2.text = mRTCRoomInfo.roomId.ToString(); UICallMachineRoomData.Instance.SetMQTTRoomId(mRTCRoomInfo.roomId.ToString()); } public void CreateRoomSuccess(string roomId) { this.roomId = roomId; UICallMachineRoomData.Instance.SetMQTTRoomId(roomId); } public void OnLocalUserJoined() { // UserJoined(UserEnum.Player); Debug.Log("DGJ ===>OnLocalUserJoined " + MQTTManager.Instance.front + "online/" + controlId); if(isInvite) // 邀请中控 { isInvite = false; JsonData obj = new JsonData(); obj["action"] = "invite"; obj["rtcRoom"] = roomId; obj["personnel"] = new JsonData(); obj["personnel"]["id"] = MQTTManager.Instance.id; obj["personnel"]["name"] = MQTTManager.Instance._username; // obj["personnel"]["resourcePoo1"] = MQTTManager.Instance.resourcePool; obj["personnel"]["resourcePoolId"] = MQTTManager.Instance.resourcePoolId; obj["personnel"]["room"] = MQTTManager.Instance.room; obj["personnel"]["phone"] = MQTTManager.Instance.phone; obj["device"] = new JsonData(); obj["device"]["deviceCode"] = "ROOM"; obj["roomId"] = roomId; // 用MQTT 向中考发送邀请 MQTTManager.Instance.PushMsg(MQTTManager.Instance.front + "room/RTC" , obj.ToJson()); } if (isApply) // 当前在RTC 频道内 { isApply = false; JsonData data = new JsonData(); data["roomId"] = roomId; data["questionCategory"] = (int)question; data["deviceUniqueId"] = GetAndroidSNIEMI.GetDeviceUniqueIdMD5(); Debug.Log("DGJ =====> " + data.ToJson()); StartCoroutine(HttpToolLangChao.Instance.SendHttp(HttpActionLang.apply, data.ToJson(), message => { Debug.Log("DGJ ===> HttpActionLang.apply " + HttpActionLang.apply + " " + message); JObject jobject = JObject.Parse(message); if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString())) { assistanceId = int.Parse(jobject["data"]["assistanceId"].ToString()); Debug.Log("DGJ ===> " + assistanceId); // Debug.Log("DGJ ===> "+ transform.position.sqrMagnitude+ transform.CompareTag("")); // ScenesManager.Instance.showWindow(ScenesManager.SceneType.ShowRoom); // 等待收到专家入场 } })); } } public void CancelApply() { if(assistanceId!=-1) { JsonData data = new JsonData(); data["assistanceId"] = assistanceId; StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.cancel_Apply, data.ToJson(), message => { Debug.Log("DGJ ===> HttpActionLang.cancel_Apply " + HttpActionLang.cancel_Apply + " " + message); })); assistanceId = -1; } } public void ExpertJonin(string identity) { RTCManager.Instance.ShowViewRawImage(identity, rawImage); } public void UserJoined( UserEnum userEnum , JObject metadata = null) { Debug.Log("DGJ ===> " + rtcState); switch (userEnum) { case UserEnum.Player: if (rtcState == RTCEnum.OFF) OnLocalUserJoined(); rtcState = RTCEnum.Online; Debug.Log(" DGJ ===> UserJoined 自己 "); break; case UserEnum.Expert: expertData = metadata; switch (rtcState) { case RTCEnum.OFF: case RTCEnum.Online: case RTCEnum.ExpertOnline: rtcState = RTCEnum.ExpertOnline; break; case RTCEnum.ConterlOnline: rtcState = RTCEnum.ALLOnline; break; case RTCEnum.ALLOnline: Debug.LogError("RTC 房间状态有误 " ); break; default: break; } Debug.Log(" DGJ ===> UserJoined 专家 "); break; case UserEnum.Conterl: controlData = metadata; switch (rtcState) { case RTCEnum.OFF: case RTCEnum.Online: rtcState = RTCEnum.ConterlOnline; break; case RTCEnum.ExpertOnline: rtcState = RTCEnum.ALLOnline; break; case RTCEnum.ConterlOnline: rtcState = RTCEnum.ConterlOnline; break; case RTCEnum.ALLOnline: Debug.LogError("RTC 房间状态有误 "); break; default: break; } Debug.Log(" DGJ ===> UserJoined 中控 "); break; default: break; } Debug.Log("DGJ ===> " + rtcState); } public void UserOffline(string uid) { Participant participant = RTCMainManager.Instance.QuesUser(uid); if(expertData!=null&& participant.identity == expertData["name"].ToString() ) { UserOffline(UserEnum.Expert); Debug.Log("DGJ ===> UserOffline 专家 "+ uid); }else if(controlData!=null && participant.identity == controlData["name"].ToString()) { UserOffline(UserEnum.Conterl); Debug.Log("DGJ ===> UserOffline 中控 " + uid); } } public void UserOffline(UserEnum userEnum) { switch (userEnum) { case UserEnum.Player: rtcState = RTCEnum.OFF; break; case UserEnum.Expert: switch (rtcState) { case RTCEnum.OFF: case RTCEnum.Online: case RTCEnum.ExpertOnline: rtcState = RTCEnum.Online; break; case RTCEnum.ConterlOnline: Debug.LogError("RTC 房间状态有误 "); break; case RTCEnum.ALLOnline: rtcState = RTCEnum.ConterlOnline; break; default: break; } break; case UserEnum.Conterl: switch (rtcState) { case RTCEnum.OFF: case RTCEnum.Online: case RTCEnum.ConterlOnline: rtcState = RTCEnum.Online; break; case RTCEnum.ExpertOnline: Debug.LogError("RTC 房间状态有误 "); break; case RTCEnum.ALLOnline: rtcState = RTCEnum.ExpertOnline; break; default: break; } break; default: break; } if (rtcState != RTCEnum.OFF && !(ScenesManager.Instance.getWindow() == ScenesManager.SceneType.ShowRoom || ScenesManager.Instance.getWindow() == ScenesManager.SceneType.ShowRTC)) { Debug.Log(" GETWindow Scenes " + ScenesManager.Instance.getWindow()); // 退出房间 // LeaveChannel(); } //if(rtcState == RTCEnum.Online && !(ScenesManager.Instance.getWindow() == ScenesManager.SceneType.ShowRoom || ScenesManager.Instance.getWindow() == ScenesManager.SceneType.ShowRTC)) //{ // Debug.Log(" GETWindow Scenes " + ScenesManager.Instance.getWindow()); // // 退出房间 // LeaveChannel(); //} } public void LeaveChannel() { Debug.LogError(" RTCRoomManager 退出房间"); Debug.Log("DGJ ===> MQTT Test 退出房间 "); MQTTManager.Instance.UnSubscribe(MQTTManager.Instance.front + "room/" +MQTTManager.Instance.roomId); RTCManager.Instance.LeaveChannel(); rtcState = RTCEnum.OFF; ClearRoomData(); } private void ClearRoomData() { expertView = null; mRTCRoomInfo = null; controlId = ""; // question = QuestionEnum.NetWork; roomId = null; expertData = null; controlData = null; } } public enum RTCEnum { /// /// 离线 /// OFF, /// /// 当前只有眼镜端在房间 /// Online, /// /// 眼镜端和专家端都在房间 /// ExpertOnline, /// /// 眼镜端和控制台都在房间 /// ConterlOnline, /// /// 眼镜、专家、控制台 都在房间 /// ALLOnline } public enum UserEnum { Player, Expert, Conterl } public enum QuestionEnum { NetWork = 1, Host = 2, Storage = 3 }