123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- 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<RTCRoomManager>
- {
- 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<string, AudioSource> dicAudio;
- /// <summary>
- /// 是否邀请中控
- /// </summary>
- public bool isInvite = false;
- /// <summary>
- /// 是否申请协助
- /// </summary>
- public bool isApply = false;
- private int assistanceId = -1;
- private void Start()
- {
- // this.RegisterEvent<RTCCreatRoomSuccessEvent>(CreatRoomSuccess).UnRegisterWhenGameObjectDestroyed(gameObject);
- rtcState = RTCEnum.OFF;
- question = QuestionEnum.NetWork;
- isRTCreate = false;
- RTCManager.OnRemoteVideoStateChanged += RemoteVideoStateChanged;
- }
- private void Update()
- {
- if(isRTCreate)
- {
- isRTCreate = false;
- // this.GetService<IRTCService>().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<RTCRoomInfo>(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
- {
- /// <summary>
- /// 离线
- /// </summary>
- OFF,
- /// <summary>
- /// 当前只有眼镜端在房间
- /// </summary>
- Online,
- /// <summary>
- /// 眼镜端和专家端都在房间
- /// </summary>
- ExpertOnline,
- /// <summary>
- /// 眼镜端和控制台都在房间
- /// </summary>
- ConterlOnline,
- /// <summary>
- /// 眼镜、专家、控制台 都在房间
- /// </summary>
- ALLOnline
- }
- public enum UserEnum
- {
- Player,
- Expert,
- Conterl
- }
- public enum QuestionEnum
- {
- NetWork = 1,
- Host = 2,
- Storage = 3
- }
|