WSRtcHandler.cs 19 KB

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