WSRtcHandler.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LitJson;
  5. using XRTool.Util;
  6. using System;
  7. public class WSRtcHandler {
  8. public delegate void OnIceCandidateDelegate(JsonData data);
  9. public delegate void OnOfferDelegate(JsonData data);
  10. public delegate void OnAnswerDelegate(JsonData data);
  11. public delegate void OnSpeekingDelegate (JsonData data);
  12. public delegate void OnProhibitSpeekDelegate (JsonData data);
  13. public delegate void OnRtcState(string str);
  14. public OnRtcState onRtcState;
  15. public OnIceCandidateDelegate OnIceCandidate;
  16. public OnOfferDelegate OnOffer;
  17. public OnAnswerDelegate OnAnswer;
  18. public OnSpeekingDelegate OnSpeeking;
  19. public OnProhibitSpeekDelegate OnProhibitSpeek;
  20. public event Action<JsonData> OnReceiveFile;
  21. public event Action<JsonData> OnDeleteFile;
  22. public event Action<JsonData> OnClearFile;
  23. public event Action<JsonData> OnCoordinate;
  24. public event Action<JsonData> OnClearFileSucess;
  25. public event Action<JsonData> OnDeleteFileSucess;
  26. public event Action<JsonData> OnSharePrintscreen;
  27. public event Action<JsonData> OnActiveSpeaker;
  28. public event Action<JsonData> OnChangeOwner;
  29. private Dictionary<string, string> idList = new Dictionary<string, string>();
  30. string roomRtcId;
  31. string rtcToken;
  32. string rtcConfig;
  33. public bool isnewConsumer = false;
  34. public Queue<string> newConsumers = new Queue<string>();
  35. public void getRtpCapabilities(string pwd)
  36. {
  37. roomRtcId = WSHandler._roomid;
  38. JsonData message = new JsonData();
  39. JsonData data = new JsonData();
  40. message["request"] = true;
  41. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  42. message["method"] = "init";
  43. data["roomId"] = roomRtcId;
  44. data["roomPwd"] = pwd;
  45. message["data"] = data;
  46. WSHandler.SendRtcMessage(message);
  47. idList.Add(message["id"].ToString(), message["method"].ToString());
  48. }
  49. public void createWebRtcTransport()
  50. {
  51. JsonData message = new JsonData();
  52. JsonData data = new JsonData();
  53. message["request"] = true;
  54. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  55. message["method"] = "createWebRtcTransport";
  56. data["forceTcp"] = false;
  57. data["roomId"] = roomRtcId;
  58. data["token"] = rtcToken;
  59. data["producing"] = true;
  60. data["consuming"] = false;
  61. data["sctpCapabilities"] = "";
  62. message["data"] = data;
  63. WSHandler.SendRtcMessage(message);
  64. idList.Add(message["id"].ToString(), "createSendTransport");
  65. }
  66. public void createRecvTransport()
  67. {
  68. JsonData message = new JsonData();
  69. JsonData data = new JsonData();
  70. message["request"] = true;
  71. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  72. message["method"] = "createWebRtcTransport";
  73. data["roomId"] = roomRtcId;
  74. data["token"] = rtcToken;
  75. data["producing"] = false;
  76. data["consuming"] = true;
  77. data["sctpCapabilities"] = "";
  78. message["data"] = data;
  79. WSHandler.SendRtcMessage(message);
  80. idList.Add(message["id"].ToString(), "createRecvTransport");
  81. }
  82. public void joinRoom(string DeviceConfig)
  83. {
  84. JsonData message = new JsonData();
  85. JsonData data = new JsonData();
  86. message["request"] = true;
  87. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  88. message["method"] = "joinRoom";
  89. data["roomId"] = roomRtcId;
  90. data["token"] = rtcToken;
  91. data["displayName"] = UserInfo.User_Token;
  92. data["device"] = JsonMapper.ToObject(DeviceConfig);
  93. data["routerRtpCapabilities"] = JsonMapper.ToObject(rtcConfig)["data"];
  94. data["sctpCapabilities"] = "";
  95. message["data"] = data;
  96. WSHandler.SendRtcMessage(message);
  97. idList.Add(message["id"].ToString(), message["method"].ToString());
  98. }
  99. public void changeDisplayName()
  100. {
  101. JsonData message = new JsonData();
  102. message["request"] = true;
  103. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  104. message["method"] = "changeDisplayName";
  105. JsonData data = new JsonData();
  106. data["displayName"] = UserInfo.Account;
  107. data["token"] = rtcToken;
  108. message["data"] = data;
  109. WSHandler.SendRtcMessage(message);
  110. idList.Add(message["id"].ToString(), message["method"].ToString());
  111. }
  112. public void connectWebRtcTransport(JsonData data)
  113. {
  114. JsonData message = new JsonData();
  115. message["request"] = true;
  116. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  117. message["method"] = "connectWebRtcTransport";
  118. message["data"] = data;
  119. WSHandler.SendRtcMessage(message);
  120. idList.Add(message["id"].ToString(), message["method"].ToString());
  121. }
  122. public void RtcResponse(JsonData data)
  123. {
  124. JsonData requestData = new JsonData();
  125. requestData["response"] = true;
  126. requestData["id"] = data["id"];
  127. requestData["ok"] = true;
  128. requestData["data"] = "{}";
  129. WSHandler.SendRtcMessage(requestData);
  130. }
  131. public void leave()
  132. {
  133. JsonData message = new JsonData();
  134. message["request"] = true;
  135. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  136. message["method"] = "leave";
  137. JsonData data = new JsonData();
  138. data["roomId"] = roomRtcId;
  139. data["token"] = rtcToken;
  140. message["data"] = data;
  141. WSHandler.SendRtcMessage(message);
  142. idList.Add(message["id"].ToString(), message["method"].ToString());
  143. }
  144. public void unsubscribe(string consumerId)
  145. {
  146. JsonData message = new JsonData();
  147. message["request"] = true;
  148. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  149. message["method"] = "pauseConsumer";
  150. JsonData data = new JsonData();
  151. data["roomId"] = roomRtcId;
  152. data["token"] = rtcToken;
  153. data["consumerId"] = consumerId;
  154. message["data"] = data;
  155. WSHandler.SendRtcMessage(message);
  156. idList.Add(message["id"].ToString(), message["method"].ToString());
  157. }
  158. public void subscribe(string consumerId)
  159. {
  160. JsonData message = new JsonData();
  161. message["request"] = true;
  162. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  163. message["method"] = "resumeConsumer";
  164. JsonData data = new JsonData();
  165. data["roomId"] = roomRtcId;
  166. data["token"] = rtcToken;
  167. data["consumerId"] = consumerId;
  168. message["data"] = data;
  169. WSHandler.SendRtcMessage(message);
  170. idList.Add(message["id"].ToString(), message["method"].ToString());
  171. }
  172. public void closeProducer(string consumerId)
  173. {
  174. JsonData message = new JsonData();
  175. message["request"] = true;
  176. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  177. message["method"] = "closeProducer";
  178. JsonData data = new JsonData();
  179. data["roomId"] = roomRtcId;
  180. data["token"] = rtcToken;
  181. data["producerId"] = consumerId;
  182. message["data"] = data;
  183. WSHandler.SendRtcMessage(message);
  184. idList.Add(message["id"].ToString(), message["method"].ToString());
  185. }
  186. public void pauseProducer(string consumerId)
  187. {
  188. JsonData message = new JsonData();
  189. message["request"] = true;
  190. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  191. message["method"] = "pauseProducer";
  192. JsonData data = new JsonData();
  193. data["roomId"] = roomRtcId;
  194. data["token"] = rtcToken;
  195. data["producerId"] = consumerId;
  196. message["data"] = data;
  197. WSHandler.SendRtcMessage(message);
  198. idList.Add(message["id"].ToString(), message["method"].ToString());
  199. }
  200. public void resumeProducer(string consumerId)
  201. {
  202. JsonData message = new JsonData();
  203. message["request"] = true;
  204. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  205. message["method"] = "resumeProducer";
  206. JsonData data = new JsonData();
  207. data["roomId"] = roomRtcId;
  208. data["token"] = rtcToken;
  209. data["producerId"] = consumerId;
  210. message["data"] = data;
  211. WSHandler.SendRtcMessage(message);
  212. idList.Add(message["id"].ToString(), message["method"].ToString());
  213. }
  214. public static string produceid;
  215. public void onProduce(JsonData data)
  216. {
  217. JsonData message = new JsonData();
  218. message["request"] = true;
  219. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  220. produceid = message["id"].ToString();
  221. message["method"] = "produce";
  222. message["data"] = data;
  223. WSHandler.SendRtcMessage(message);
  224. idList.Add(message["id"].ToString(), message["method"].ToString());
  225. }
  226. public void responseOk(string id)
  227. {
  228. JsonData message = new JsonData();
  229. message["response"] = true;
  230. message["id"] = id;
  231. message["ok"] = true;
  232. message["data"] = "{}";
  233. WSHandler.SendRtcMessage(message);
  234. }
  235. //从文件列表中删除指定文件
  236. public void deleteFile(string path)
  237. {
  238. JsonData message = new JsonData();
  239. message["request"] = true;
  240. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  241. message["ok"] = true;
  242. message["method"] = "deleteFile";
  243. JsonData data = new JsonData();
  244. data["path"] = path;
  245. message["data"] = data;
  246. WSHandler.SendRtcMessage(message);
  247. idList.Add(message["id"].ToString(), message["method"].ToString());
  248. }
  249. //清空文件列表
  250. public void clearFile()
  251. {
  252. JsonData message = new JsonData();
  253. message["request"] = true;
  254. message["id"] = UnityEngine.Random.Range(10000000, 99999999);
  255. message["ok"] = true;
  256. message["method"] = "clearFile";
  257. WSHandler.SendRtcMessage(message);
  258. idList.Add(message["id"].ToString(), message["method"].ToString());
  259. }
  260. public void OnMessageReceived(JsonData data)
  261. {
  262. SCRtcPeer p;
  263. if (data.Keys.Contains("method"))
  264. {
  265. switch (data["method"].ToString())
  266. {
  267. case "closed":
  268. sendText(data["method"].ToString(), data["data"]);
  269. string pid = data["data"]["peerId"].ToString();
  270. SCRtcFactory.Instance.mSCRtcPeers.removePeers(pid);
  271. string isCreator = data["data"]["isCreator"].ToString();
  272. if (isCreator == "1")
  273. {
  274. if (RoomMain.Instance)
  275. {
  276. RoomMain.Instance.ShowRoomissolvePop();
  277. RoomMain.Instance.LeaveRoom();
  278. }
  279. }
  280. AgoraVideoAudioManager.Instance.RemAtPeeridUid(data["data"]["peerId"].ToString());
  281. break;
  282. case "chatMessage":
  283. sendText(data["method"].ToString(), data["data"]);
  284. break;
  285. case "connect":
  286. getRtpCapabilities("");
  287. Debug.Log(data["data"][0]+ " "+ data["data"][1]);
  288. AgoraVideoAudioManager.Instance.AddPeeridUid(data["data"][0].ToString(), (uint)int.Parse(data["data"][1].ToString()));
  289. AgoraVideoAudioManager.Instance.uid = (uint)int.Parse(data["data"][1].ToString());
  290. break;
  291. case "newConsumer":
  292. newConsumers.Enqueue(data.ToJson());
  293. // SCRtcFactory.Instance.mSCRtcHandle.onNewConsumer(data["data"].ToJson(), data["id"].ToString());
  294. break;
  295. case "joined":
  296. data["action"] = "onNotification";
  297. SCRtcFactory.Instance.mSCRtcManager.OnRtcListener(data);
  298. sendText(data["method"].ToString(), data["data"]);
  299. SoundMgr._instance.PlayMusicByName("tishi", false);
  300. AgoraVideoAudioManager.Instance.AddPeeridUid(data["data"]["peerId"].ToString(), (uint)int.Parse(data["data"]["uid"].ToString()));
  301. break;
  302. case "consumerPaused":
  303. p = SCRtcFactory.Instance.mSCRtcPeers.getPeer(data["data"]["peerId"].ToString());
  304. if (p != null)
  305. {
  306. if (p.cIdA == data["data"]["consumerId"].ToString())
  307. {
  308. p.setInfo("audio", data["data"]["consumerId"].ToString(), false);
  309. }
  310. else
  311. {
  312. p.setInfo("video", data["data"]["consumerId"].ToString(), false);
  313. }
  314. }
  315. break;
  316. case "consumerResumed":
  317. p = SCRtcFactory.Instance.mSCRtcPeers.getPeer(data["data"]["peerId"].ToString());
  318. if (p != null)
  319. {
  320. if (p.cIdA == data["data"]["consumerId"].ToString())
  321. {
  322. p.setInfo("audio", data["data"]["consumerId"].ToString(), true);
  323. }
  324. else
  325. {
  326. p.setInfo("video", data["data"]["consumerId"].ToString(), true);
  327. }
  328. }
  329. break;
  330. case "consumerClosed":
  331. p = SCRtcFactory.Instance.mSCRtcPeers.getPeer(data["data"]["peerId"].ToString());
  332. if(p!=null)
  333. {
  334. if (p.cIdA == data["data"]["consumerId"].ToString() || data["data"]["kind"].ToString()=="audio")
  335. {
  336. p.setInfo("audio", "",false);
  337. }
  338. else if (p.cIdV == data["data"]["consumerId"].ToString() || data["data"]["kind"].ToString() == "video")
  339. {
  340. p.setInfo("video", "", false);
  341. }
  342. }
  343. break;
  344. case "activeSpeaker":
  345. OnActiveSpeaker?.Invoke(data);
  346. break;
  347. case "receiveFile":
  348. OnReceiveFile?.Invoke(data);
  349. break;
  350. case "deleteFile":
  351. OnDeleteFile?.Invoke(data);
  352. break;
  353. case "clearFile":
  354. OnClearFile?.Invoke(data);
  355. break;
  356. case "coordinate":
  357. OnCoordinate?.Invoke(data);
  358. break;
  359. case "sharePrintscreen":
  360. OnSharePrintscreen?.Invoke(data);
  361. break;
  362. case "changeOwner":
  363. OnChangeOwner?.Invoke(data);
  364. break;
  365. default:
  366. break;
  367. }
  368. if (onRtcState != null)
  369. {
  370. onRtcState(data["method"].ToString());
  371. }
  372. }
  373. if (data.Keys.Contains("id"))
  374. {
  375. if (idList.ContainsKey(data["id"].ToString()))
  376. {
  377. switch (idList[data["id"].ToString()])
  378. {
  379. case "init":
  380. rtcConfig = data.ToJson();
  381. // Debug.Log("init=========>");
  382. if (data["data"].Keys.Contains("token"))
  383. {
  384. rtcToken = data["data"]["token"].ToString();
  385. }
  386. else
  387. {
  388. rtcToken = "";
  389. }
  390. createWebRtcTransport();
  391. SCRtcFactory.Instance.mSCRtcHandle.initRtpCapabilities(data["data"].ToJson());
  392. // joinRoom(data.ToJson());
  393. break;
  394. case "createSendTransport":
  395. // Debug.Log("createSendTransport=========>");
  396. SCRtcFactory.Instance.mSCRtcHandle.createSendTransport(data["data"].ToJson());
  397. createRecvTransport();
  398. break;
  399. case "createRecvTransport":
  400. // Debug.Log("createRecvTransport=========>");
  401. SCRtcFactory.Instance.mSCRtcHandle.createRecvTransport(data["data"].ToJson());
  402. // changeDisplayName();
  403. SCRtcFactory.Instance.mSCRtcHandle.getDevice();
  404. // #if UNITY_EDITOR || !UNITY_ANDROID
  405. joinRoom("{}");
  406. // #endif
  407. break;
  408. case "joinRoom":
  409. // Debug.Log("joinRoom=========>");
  410. SCRtcFactory.Instance.mSCRtcHandle.joinRoom(data["data"].ToJson());
  411. data["action"] = "onNotification";
  412. data["method"] = "joinRoom";
  413. SCRtcFactory.Instance.mSCRtcManager.OnRtcListener(data);
  414. sendText(idList[data["id"].ToString()], data["data"]);
  415. break;
  416. case "changeDisplayName":
  417. // Debug.Log("changeDisplayName=========>");
  418. break;
  419. case "closeProducer":
  420. onRtcState(idList[data["id"].ToString()]);
  421. break;
  422. case "clearFile":
  423. OnClearFileSucess?.Invoke(data);
  424. break;
  425. case "deleteFile":
  426. OnDeleteFileSucess?.Invoke(data);
  427. break;
  428. default:
  429. break;
  430. }
  431. if (onRtcState != null)
  432. {
  433. onRtcState(idList[data["id"].ToString()]);
  434. }
  435. idList.Remove(data["id"].ToString());
  436. }
  437. if (data["data"].Keys.Contains("room"))
  438. {
  439. for (int i = 0; i < data["data"]["room"]["users"].Count; i++)
  440. {
  441. AgoraVideoAudioManager.Instance.AddPeeridUid(data["data"]["room"]["users"][i]["peerId"].ToString(), (uint)int.Parse(data["data"]["room"]["users"][i]["uid"].ToString()));
  442. }
  443. AgoraVideoAudioManager.Instance.ShowOneView(RoomMain.Instance.agoraRawImage);
  444. }
  445. }
  446. }
  447. void sendText(string md,JsonData data)
  448. {
  449. if(RoomInfo.Instance)
  450. {
  451. RoomInfo.Instance.ctManager.SetTextManager(md, data);
  452. }
  453. }
  454. }