using System.Collections; using System.Collections.Generic; using UnityEngine; using LitJson; using XRTool.Util; using System; public class WSRtcHandler { public delegate void OnIceCandidateDelegate(JsonData data); public delegate void OnOfferDelegate(JsonData data); public delegate void OnAnswerDelegate(JsonData data); public delegate void OnSpeekingDelegate (JsonData data); public delegate void OnProhibitSpeekDelegate (JsonData data); public delegate void OnRtcState(string str); public OnRtcState onRtcState; public OnIceCandidateDelegate OnIceCandidate; public OnOfferDelegate OnOffer; public OnAnswerDelegate OnAnswer; public OnSpeekingDelegate OnSpeeking; public OnProhibitSpeekDelegate OnProhibitSpeek; public event Action OnReceiveFile; public event Action OnDeleteFile; public event Action OnClearFile; public event Action OnCoordinate; public event Action OnClearFileSucess; public event Action OnDeleteFileSucess; //public event Action OnSharePrintscreen; public event Action OnGetuploadCert; public event Action OnSendFile; public event Action OnResponseCoordinate; public event Action OnActiveSpeaker; public event Action OnChangeOwner; private Dictionary idList = new Dictionary(); string roomRtcId; string rtcToken; string rtcConfig; public bool isnewConsumer = false; public Queue newConsumers = new Queue(); public void getRtpCapabilities(string pwd) { roomRtcId = WSHandler._roomid; JsonData message = new JsonData(); JsonData data = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "init"; data["roomId"] = roomRtcId; data["roomPwd"] = pwd; message["data"] = data; WSHandler.SendRtcMessage(message); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void createWebRtcTransport() { JsonData message = new JsonData(); JsonData data = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "createWebRtcTransport"; data["forceTcp"] = false; data["roomId"] = roomRtcId; data["token"] = rtcToken; data["producing"] = true; data["consuming"] = false; data["sctpCapabilities"] = ""; message["data"] = data; WSHandler.SendRtcMessage(message); idList.Add(message["id"].ToString(), "createSendTransport"); } public void createRecvTransport() { JsonData message = new JsonData(); JsonData data = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "createWebRtcTransport"; data["roomId"] = roomRtcId; data["token"] = rtcToken; data["producing"] = false; data["consuming"] = true; data["sctpCapabilities"] = ""; message["data"] = data; WSHandler.SendRtcMessage(message); idList.Add(message["id"].ToString(), "createRecvTransport"); } public void joinRoom(string DeviceConfig) { JsonData message = new JsonData(); JsonData data = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "joinRoom"; data["roomId"] = roomRtcId; data["token"] = rtcToken; data["displayName"] = UserInfo.User_Token; data["device"] = JsonMapper.ToObject(DeviceConfig); data["routerRtpCapabilities"] = JsonMapper.ToObject(rtcConfig)["data"]; data["sctpCapabilities"] = ""; message["data"] = data; WSHandler.SendRtcMessage(message); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void changeDisplayName() { JsonData message = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "changeDisplayName"; JsonData data = new JsonData(); data["displayName"] = UserInfo.Account; data["token"] = rtcToken; message["data"] = data; WSHandler.SendRtcMessage(message); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void connectWebRtcTransport(JsonData data) { JsonData message = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "connectWebRtcTransport"; message["data"] = data; WSHandler.SendRtcMessage(message); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void RtcResponse(JsonData data) { JsonData requestData = new JsonData(); requestData["response"] = true; requestData["id"] = data["id"]; requestData["ok"] = true; requestData["data"] = "{}"; WSHandler.SendRtcMessage(requestData); } public void leave() { JsonData message = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "leave"; JsonData data = new JsonData(); data["roomId"] = roomRtcId; data["token"] = rtcToken; message["data"] = data; WSHandler.SendRtcMessage(message); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void unsubscribe(string consumerId) { JsonData message = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "pauseConsumer"; JsonData data = new JsonData(); data["roomId"] = roomRtcId; data["token"] = rtcToken; data["consumerId"] = consumerId; message["data"] = data; WSHandler.SendRtcMessage(message); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void subscribe(string consumerId) { JsonData message = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "resumeConsumer"; JsonData data = new JsonData(); data["roomId"] = roomRtcId; data["token"] = rtcToken; data["consumerId"] = consumerId; message["data"] = data; WSHandler.SendRtcMessage(message); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void closeProducer(string consumerId) { JsonData message = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "closeProducer"; JsonData data = new JsonData(); data["roomId"] = roomRtcId; data["token"] = rtcToken; data["producerId"] = consumerId; message["data"] = data; WSHandler.SendRtcMessage(message); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void pauseProducer(string consumerId) { JsonData message = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "pauseProducer"; JsonData data = new JsonData(); data["roomId"] = roomRtcId; data["token"] = rtcToken; data["producerId"] = consumerId; message["data"] = data; WSHandler.SendRtcMessage(message); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void resumeProducer(string consumerId) { JsonData message = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "resumeProducer"; JsonData data = new JsonData(); data["roomId"] = roomRtcId; data["token"] = rtcToken; data["producerId"] = consumerId; message["data"] = data; WSHandler.SendRtcMessage(message); idList.Add(message["id"].ToString(), message["method"].ToString()); } public static string produceid; public void onProduce(JsonData data) { JsonData message = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); produceid = message["id"].ToString(); message["method"] = "produce"; message["data"] = data; WSHandler.SendRtcMessage(message); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void responseOk(string id) { JsonData message = new JsonData(); message["response"] = true; message["id"] = id; message["ok"] = true; message["data"] = "{}"; WSHandler.SendRtcMessage(message); } //从文件列表中删除指定文件 public void deleteFile(string path) { JsonData message = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["ok"] = true; message["method"] = "deleteFile"; JsonData data = new JsonData(); data["path"] = path; message["data"] = data; WSHandler.SendRtcMessage(message); idList.Add(message["id"].ToString(), message["method"].ToString()); } //清空文件列表 public void clearFile() { JsonData message = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["ok"] = true; message["method"] = "clearFile"; WSHandler.SendRtcMessage(message); idList.Add(message["id"].ToString(), message["method"].ToString()); } //获取上传凭证 public void uploadCert() { JsonData message = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["ok"] = true; message["method"] = "uploadCert"; WSHandler.SendRtcMessage(message); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void sendFile(string url, string path, string fileName) { JsonData message = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["ok"] = true; message["method"] = "sendFile"; JsonData data = new JsonData(); data["url"] = url; data["path"] = path; data["fileName"] = fileName; message["data"] = data; WSHandler.SendRtcMessage(message); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void coordinate(int action, string paramx, string paramy,string otherPeerId) { Debug.Log(otherPeerId); JsonData message = new JsonData(); message["request"] = true; message["id"] = UnityEngine.Random.Range(10000000, 99999999); message["method"] = "coordinate"; JsonData data = new JsonData(); JsonData Param = new JsonData(); Param["x"] = paramx; Param["y"] = paramy; data["params"] = Param; data["action"] = action; data["to"] = new JsonData(); data["to"].Add(otherPeerId); message["data"] = data; WSHandler.SendRtcMessage(message); idList.Add(message["id"].ToString(), message["method"].ToString()); } public void OnMessageReceived(JsonData data) { SCRtcPeer p; if (data.Keys.Contains("method")) { switch (data["method"].ToString()) { case "closed": sendText(data["method"].ToString(), data["data"]); string pid = data["data"]["peerId"].ToString(); SCRtcFactory.Instance.mSCRtcPeers.removePeers(pid); string isCreator = data["data"]["isCreator"].ToString(); if (isCreator == "1") { if (RoomMainForms.Instance) { RoomMainForms.Instance.ShowRoomissolvePop(); RoomMainForms.Instance.LeaveRoom(); } } AgoraVideoAudioManager.Instance.RemAtPeeridUid(data["data"]["peerId"].ToString()); break; case "chatMessage": sendText(data["method"].ToString(), data["data"]); break; case "connect": getRtpCapabilities(""); // 添加自己UID Debug.Log(data["data"][0] + " " + data["data"][1]); AgoraVideoAudioManager.Instance.AddPeeridUid(data["data"][0].ToString(), (uint)int.Parse(data["data"][1].ToString())); AgoraVideoAudioManager.Instance.uid = (uint)int.Parse(data["data"][1].ToString()); break; case "newConsumer": newConsumers.Enqueue(data.ToJson()); // SCRtcFactory.Instance.mSCRtcHandle.onNewConsumer(data["data"].ToJson(), data["id"].ToString()); break; case "joined": data["action"] = "onNotification"; SCRtcFactory.Instance.mSCRtcManager.OnRtcListener(data); sendText(data["method"].ToString(), data["data"]); AgoraVideoAudioManager.Instance.AddPeeridUid(data["data"]["peerId"].ToString(), (uint)int.Parse(data["data"]["uid"].ToString())); // RoomMainForms.Instance.ShowOneVideo(); break; case "consumerPaused": p = SCRtcFactory.Instance.mSCRtcPeers.getPeer(data["data"]["peerId"].ToString()); if (p != null) { if (p.cIdA == data["data"]["consumerId"].ToString()) { p.setInfo("audio", data["data"]["consumerId"].ToString(), false); } else { p.setInfo("video", data["data"]["consumerId"].ToString(), false); } } break; case "consumerResumed": p = SCRtcFactory.Instance.mSCRtcPeers.getPeer(data["data"]["peerId"].ToString()); if (p != null) { if (p.cIdA == data["data"]["consumerId"].ToString()) { p.setInfo("audio", data["data"]["consumerId"].ToString(), true); } else { p.setInfo("video", data["data"]["consumerId"].ToString(), true); } } break; case "consumerClosed": p = SCRtcFactory.Instance.mSCRtcPeers.getPeer(data["data"]["peerId"].ToString()); if(p!=null) { if (p.cIdA == data["data"]["consumerId"].ToString() || data["data"]["kind"].ToString()=="audio") { p.setInfo("audio", "",false); } else if (p.cIdV == data["data"]["consumerId"].ToString() || data["data"]["kind"].ToString() == "video") { p.setInfo("video", "", false); } } break; case "activeSpeaker": OnActiveSpeaker?.Invoke(data); break; case "receiveFile": OnReceiveFile?.Invoke(data); break; case "deleteFile": OnDeleteFile?.Invoke(data); break; case "clearFile": OnClearFile?.Invoke(data); break; case "coordinate": OnCoordinate?.Invoke(data); break; //case "sharePrintscreen": // OnSharePrintscreen?.Invoke(data); // break; case "changeOwner": OnChangeOwner?.Invoke(data); break; default: break; } if (onRtcState != null) { onRtcState(data["method"].ToString()); } } if (data.Keys.Contains("id")) { if (idList.ContainsKey(data["id"].ToString())) { switch (idList[data["id"].ToString()]) { case "init": rtcConfig = data.ToJson(); // Debug.Log("init=========>"); if (data["data"].Keys.Contains("token")) { rtcToken = data["data"]["token"].ToString(); } else { rtcToken = ""; } createWebRtcTransport(); SCRtcFactory.Instance.mSCRtcHandle.initRtpCapabilities(data["data"].ToJson()); // joinRoom(data.ToJson()); break; case "createSendTransport": // Debug.Log("createSendTransport=========>"); SCRtcFactory.Instance.mSCRtcHandle.createSendTransport(data["data"].ToJson()); createRecvTransport(); break; case "createRecvTransport": // Debug.Log("createRecvTransport=========>"); SCRtcFactory.Instance.mSCRtcHandle.createRecvTransport(data["data"].ToJson()); // changeDisplayName(); SCRtcFactory.Instance.mSCRtcHandle.getDevice(); #if UNITY_EDITOR || !UNITY_ANDROID joinRoom("{}"); #endif break; case "joinRoom": // Debug.Log("joinRoom=========>"); SCRtcFactory.Instance.mSCRtcHandle.joinRoom(data["data"].ToJson()); data["action"] = "onNotification"; data["method"] = "joinRoom"; SCRtcFactory.Instance.mSCRtcManager.OnRtcListener(data); sendText(idList[data["id"].ToString()], data["data"]); break; case "changeDisplayName": // Debug.Log("changeDisplayName=========>"); break; case "closeProducer": onRtcState(idList[data["id"].ToString()]); break; case "clearFile": OnClearFileSucess?.Invoke(data); break; case "deleteFile": OnDeleteFileSucess?.Invoke(data); break; case "uploadCert": OnGetuploadCert?.Invoke(data); break; case "sendFile": OnSendFile?.Invoke(data); break; case "coordinate": OnResponseCoordinate?.Invoke(data); break; default: break; } if (onRtcState != null) { onRtcState(idList[data["id"].ToString()]); } idList.Remove(data["id"].ToString()); try { if (data["data"].Keys.Contains("room")) { for (int i = 0; i < data["data"]["room"]["users"].Count; i++) { AgoraVideoAudioManager.Instance.AddPeeridUid(data["data"]["room"]["users"][i]["peerId"].ToString(), (uint)int.Parse(data["data"]["room"]["users"][i]["uid"].ToString())); } } } catch (Exception) { } } } } void sendText(string md,JsonData data) { //if(RoomInfo.Instance) //{ // RoomInfo.Instance.ctManager.SetTextManager(md, data); //} } }