123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- 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<JsonData> OnReceiveFile;
- public event Action<JsonData> OnDeleteFile;
- public event Action<JsonData> OnClearFile;
- public event Action<JsonData> OnCoordinate;
- public event Action<JsonData> OnClearFileSucess;
- public event Action<JsonData> OnDeleteFileSucess;
- public event Action<JsonData> OnSharePrintscreen;
- public event Action<JsonData> OnActiveSpeaker;
- public event Action<JsonData> OnChangeOwner;
- private Dictionary<string, string> idList = new Dictionary<string, string>();
- string roomRtcId;
- string rtcToken;
- string rtcConfig;
- public bool isnewConsumer = false;
- public Queue<string> newConsumers = new Queue<string>();
- 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 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 (RoomMain.Instance)
- {
- RoomMain.Instance.ShowRoomissolvePop();
- RoomMain.Instance.LeaveRoom();
- }
- }
- AgoraVideoAudioManager.Instance.RemAtPeeridUid(data["data"]["peerId"].ToString());
- break;
- case "chatMessage":
- sendText(data["method"].ToString(), data["data"]);
- break;
- case "connect":
- getRtpCapabilities("");
- 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"]);
- SoundMgr._instance.PlayMusicByName("tishi", false);
- AgoraVideoAudioManager.Instance.AddPeeridUid(data["data"]["peerId"].ToString(), (uint)int.Parse(data["data"]["uid"].ToString()));
- 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;
- default:
- break;
- }
- if (onRtcState != null)
- {
- onRtcState(idList[data["id"].ToString()]);
- }
- idList.Remove(data["id"].ToString());
- }
- 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()));
- }
- AgoraVideoAudioManager.Instance.ShowOneView(RoomMain.Instance.agoraRawImage);
- }
- }
- }
- void sendText(string md,JsonData data)
- {
- if(RoomInfo.Instance)
- {
- RoomInfo.Instance.ctManager.SetTextManager(md, data);
- }
- }
- }
|