using System.Collections; using System.Collections.Generic; using UnityEngine; using LitJson; using XRTool.Util; 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 OnIceCandidateDelegate OnIceCandidate; public OnOfferDelegate OnOffer; public OnAnswerDelegate OnAnswer; public OnSpeekingDelegate OnSpeeking; public OnProhibitSpeekDelegate OnProhibitSpeek; public delegate void OnRtcInit(JsonData data); public OnRtcInit onRtcInit; private Dictionary idList = new Dictionary(); string roomRtcId; string rtcToken; string rtcConfig; public void getRtpCapabilities(string pwd) { WSHandler.Rtc.isnewConsumer = false; roomRtcId = WSHandler._roomid; JsonData message = new JsonData(); JsonData data = new JsonData(); message["request"] = true; message["id"] = 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"] = 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"] = 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"] = 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"] = Random.Range(10000000, 99999999); message["method"] = "changeDisplayName"; JsonData data = new JsonData(); data["displayName"] = CommonMethod.MyPeer.PeerId; 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"] = 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"] = 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"] = 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"] = 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"] = 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"] = 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"] = 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"] = 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 bool isnewConsumer = false; public Queue newConsumers =new Queue(); public void OnMessageReceived(JsonData data) { SCRtcPeer p; if (data.Keys.Contains("method")) { switch (data["method"].ToString()) { case "closed": string pid = data["data"]["peerId"].ToString(); SCRtcFactory.Instance.mSCRtcPeers.removePeers(pid); break; case "connect": getRtpCapabilities(""); break; case "newConsumer": newConsumers.Enqueue(data.ToJson()); break; case "joined": data["action"] = "onNotification"; SCRtcFactory.Instance.mSCRtcManager.OnRtcListener(data); 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()) { p.setInfo("audio", "",false); } else { p.setInfo("video", "", false); } } break; case "activeSpeaker": if(data["data"]["peerId"]!=null) { p = SCRtcFactory.Instance.mSCRtcPeers.getPeer(data["data"]["peerId"].ToString()); if (p != null) { p.changeVolumn(int.Parse(data["data"]["volume"].ToString())); } } break; default: break; } } 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(); break; case "joinRoom": // Debug.Log("joinRoom=========>"); SCRtcFactory.Instance.mSCRtcHandle.joinRoom(data["data"].ToJson()); data["action"] = "onNotification"; data["method"] = "joinRoom"; SCRtcFactory.Instance.mSCRtcManager.OnRtcListener(data); if(onRtcInit!=null) onRtcInit(data); break; case "changeDisplayName": Debug.Log("changeDisplayName=========>"); SCRtcFactory.Instance.mSCRtcHandle.getDevice(); // #if UNITY_EDITOR || !UNITY_ANDROID joinRoom("{}"); //#endif break; default: break; } idList.Remove(data["id"].ToString()); } } } }