123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- 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<string, string> idList = new Dictionary<string, string>();
- 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<string> newConsumers =new Queue<string>();
- 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());
- }
-
- }
- }
- }
|