WSRtcHandler.cs 21 KB

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