WSRtcHandler.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LitJson;
  5. using XRTool.Util;
  6. public class WSRtcHandler {
  7. public delegate void OnIceCandidateDelegate(JsonData data);
  8. public delegate void OnOfferDelegate(JsonData data);
  9. public delegate void OnAnswerDelegate(JsonData data);
  10. public delegate void OnSpeekingDelegate (JsonData data);
  11. public delegate void OnProhibitSpeekDelegate (JsonData data);
  12. public OnIceCandidateDelegate OnIceCandidate;
  13. public OnOfferDelegate OnOffer;
  14. public OnAnswerDelegate OnAnswer;
  15. public OnSpeekingDelegate OnSpeeking;
  16. public OnProhibitSpeekDelegate OnProhibitSpeek;
  17. public delegate void OnRtcInit(JsonData data);
  18. public OnRtcInit onRtcInit;
  19. private Dictionary<string, string> idList = new Dictionary<string, string>();
  20. string roomRtcId;
  21. string rtcToken;
  22. string rtcConfig;
  23. public void getRtpCapabilities(string pwd)
  24. {
  25. WSHandler.Rtc.isnewConsumer = false;
  26. roomRtcId = WSHandler._roomid;
  27. JsonData message = new JsonData();
  28. JsonData data = new JsonData();
  29. message["request"] = true;
  30. message["id"] = Random.Range(10000000, 99999999);
  31. message["method"] = "init";
  32. data["roomId"] = roomRtcId;
  33. data["roomPwd"] = pwd;
  34. message["data"] = data;
  35. WSHandler.SendRtcMessage(message);
  36. idList.Add(message["id"].ToString(), message["method"].ToString());
  37. }
  38. public void createWebRtcTransport()
  39. {
  40. JsonData message = new JsonData();
  41. JsonData data = new JsonData();
  42. message["request"] = true;
  43. message["id"] = Random.Range(10000000, 99999999);
  44. message["method"] = "createWebRtcTransport";
  45. data["forceTcp"] = false;
  46. data["roomId"] = roomRtcId;
  47. data["token"] = rtcToken;
  48. data["producing"] = true;
  49. data["consuming"] = false;
  50. data["sctpCapabilities"] = "";
  51. message["data"] = data;
  52. WSHandler.SendRtcMessage(message);
  53. idList.Add(message["id"].ToString(), "createSendTransport");
  54. }
  55. public void createRecvTransport()
  56. {
  57. JsonData message = new JsonData();
  58. JsonData data = new JsonData();
  59. message["request"] = true;
  60. message["id"] = Random.Range(10000000, 99999999);
  61. message["method"] = "createWebRtcTransport";
  62. data["roomId"] = roomRtcId;
  63. data["token"] = rtcToken;
  64. data["producing"] = false;
  65. data["consuming"] = true;
  66. data["sctpCapabilities"] = "";
  67. message["data"] = data;
  68. WSHandler.SendRtcMessage(message);
  69. idList.Add(message["id"].ToString(), "createRecvTransport");
  70. }
  71. public void joinRoom(string DeviceConfig)
  72. {
  73. JsonData message = new JsonData();
  74. JsonData data = new JsonData();
  75. message["request"] = true;
  76. message["id"] = Random.Range(10000000, 99999999);
  77. message["method"] = "joinRoom";
  78. data["roomId"] = roomRtcId;
  79. data["token"] = rtcToken;
  80. data["displayName"] = UserInfo.User_Token;
  81. data["device"] = JsonMapper.ToObject(DeviceConfig);
  82. data["routerRtpCapabilities"] = JsonMapper.ToObject(rtcConfig)["data"];
  83. data["sctpCapabilities"] = "";
  84. message["data"] = data;
  85. WSHandler.SendRtcMessage(message);
  86. idList.Add(message["id"].ToString(), message["method"].ToString());
  87. }
  88. public void changeDisplayName()
  89. {
  90. JsonData message = new JsonData();
  91. message["request"] = true;
  92. message["id"] = Random.Range(10000000, 99999999);
  93. message["method"] = "changeDisplayName";
  94. JsonData data = new JsonData();
  95. data["displayName"] = CommonMethod.MyPeer.PeerId;
  96. data["token"] = rtcToken;
  97. message["data"] = data;
  98. WSHandler.SendRtcMessage(message);
  99. idList.Add(message["id"].ToString(), message["method"].ToString());
  100. }
  101. public void connectWebRtcTransport(JsonData data)
  102. {
  103. JsonData message = new JsonData();
  104. message["request"] = true;
  105. message["id"] = Random.Range(10000000, 99999999);
  106. message["method"] = "connectWebRtcTransport";
  107. message["data"] = data;
  108. WSHandler.SendRtcMessage(message);
  109. idList.Add(message["id"].ToString(), message["method"].ToString());
  110. }
  111. public void RtcResponse(JsonData data)
  112. {
  113. JsonData requestData = new JsonData();
  114. requestData["response"] = true;
  115. requestData["id"] = data["id"];
  116. requestData["ok"] = true;
  117. requestData["data"] = "{}";
  118. WSHandler.SendRtcMessage(requestData);
  119. }
  120. public void leave()
  121. {
  122. JsonData message = new JsonData();
  123. message["request"] = true;
  124. message["id"] = Random.Range(10000000, 99999999);
  125. message["method"] = "leave";
  126. JsonData data = new JsonData();
  127. data["roomId"] = roomRtcId;
  128. data["token"] = rtcToken;
  129. message["data"] = data;
  130. WSHandler.SendRtcMessage(message);
  131. idList.Add(message["id"].ToString(), message["method"].ToString());
  132. }
  133. public void unsubscribe(string consumerId)
  134. {
  135. JsonData message = new JsonData();
  136. message["request"] = true;
  137. message["id"] = Random.Range(10000000, 99999999);
  138. message["method"] = "pauseConsumer";
  139. JsonData data = new JsonData();
  140. data["roomId"] = roomRtcId;
  141. data["token"] = rtcToken;
  142. data["consumerId"] = consumerId;
  143. message["data"] = data;
  144. WSHandler.SendRtcMessage(message);
  145. idList.Add(message["id"].ToString(), message["method"].ToString());
  146. }
  147. public void subscribe(string consumerId)
  148. {
  149. JsonData message = new JsonData();
  150. message["request"] = true;
  151. message["id"] = Random.Range(10000000, 99999999);
  152. message["method"] = "resumeConsumer";
  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 closeProducer(string consumerId)
  162. {
  163. JsonData message = new JsonData();
  164. message["request"] = true;
  165. message["id"] = Random.Range(10000000, 99999999);
  166. message["method"] = "closeProducer";
  167. JsonData data = new JsonData();
  168. data["roomId"] = roomRtcId;
  169. data["token"] = rtcToken;
  170. data["producerId"] = consumerId;
  171. message["data"] = data;
  172. WSHandler.SendRtcMessage(message);
  173. idList.Add(message["id"].ToString(), message["method"].ToString());
  174. }
  175. public void pauseProducer(string consumerId)
  176. {
  177. JsonData message = new JsonData();
  178. message["request"] = true;
  179. message["id"] = Random.Range(10000000, 99999999);
  180. message["method"] = "pauseProducer";
  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 resumeProducer(string consumerId)
  190. {
  191. JsonData message = new JsonData();
  192. message["request"] = true;
  193. message["id"] = Random.Range(10000000, 99999999);
  194. message["method"] = "resumeProducer";
  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 static string produceid;
  204. public void onProduce(JsonData data)
  205. {
  206. JsonData message = new JsonData();
  207. message["request"] = true;
  208. message["id"] = Random.Range(10000000, 99999999);
  209. produceid = message["id"].ToString();
  210. message["method"] = "produce";
  211. message["data"] = data;
  212. WSHandler.SendRtcMessage(message);
  213. idList.Add(message["id"].ToString(), message["method"].ToString());
  214. }
  215. public bool isnewConsumer = false;
  216. public Queue<string> newConsumers =new Queue<string>();
  217. public void OnMessageReceived(JsonData data)
  218. {
  219. SCRtcPeer p;
  220. if (data.Keys.Contains("method"))
  221. {
  222. switch (data["method"].ToString())
  223. {
  224. case "closed":
  225. string pid = data["data"]["peerId"].ToString();
  226. SCRtcFactory.Instance.mSCRtcPeers.removePeers(pid);
  227. break;
  228. case "connect":
  229. getRtpCapabilities("");
  230. break;
  231. case "newConsumer":
  232. newConsumers.Enqueue(data.ToJson());
  233. break;
  234. case "joined":
  235. data["action"] = "onNotification";
  236. SCRtcFactory.Instance.mSCRtcManager.OnRtcListener(data);
  237. break;
  238. case "consumerPaused":
  239. p = SCRtcFactory.Instance.mSCRtcPeers.getPeer(data["data"]["peerId"].ToString());
  240. if (p != null)
  241. {
  242. if (p.cIdA == data["data"]["consumerId"].ToString())
  243. {
  244. p.setInfo("audio", data["data"]["consumerId"].ToString(), false);
  245. }
  246. else
  247. {
  248. p.setInfo("video", data["data"]["consumerId"].ToString(), false);
  249. }
  250. }
  251. break;
  252. case "consumerResumed":
  253. p = SCRtcFactory.Instance.mSCRtcPeers.getPeer(data["data"]["peerId"].ToString());
  254. if (p != null)
  255. {
  256. if (p.cIdA == data["data"]["consumerId"].ToString())
  257. {
  258. p.setInfo("audio", data["data"]["consumerId"].ToString(), true);
  259. }
  260. else
  261. {
  262. p.setInfo("video", data["data"]["consumerId"].ToString(), true);
  263. }
  264. }
  265. break;
  266. case "consumerClosed":
  267. p = SCRtcFactory.Instance.mSCRtcPeers.getPeer(data["data"]["peerId"].ToString());
  268. if(p!=null)
  269. {
  270. if (p.cIdA == data["data"]["consumerId"].ToString())
  271. {
  272. p.setInfo("audio", "",false);
  273. }
  274. else
  275. {
  276. p.setInfo("video", "", false);
  277. }
  278. }
  279. break;
  280. case "activeSpeaker":
  281. if(data["data"]["peerId"]!=null)
  282. {
  283. p = SCRtcFactory.Instance.mSCRtcPeers.getPeer(data["data"]["peerId"].ToString());
  284. if (p != null)
  285. {
  286. p.changeVolumn(int.Parse(data["data"]["volume"].ToString()));
  287. }
  288. }
  289. break;
  290. default:
  291. break;
  292. }
  293. }
  294. if (data.Keys.Contains("id"))
  295. {
  296. if (idList.ContainsKey(data["id"].ToString()))
  297. {
  298. switch (idList[data["id"].ToString()])
  299. {
  300. case "init":
  301. rtcConfig = data.ToJson();
  302. // Debug.Log("init=========>");
  303. if (data["data"].Keys.Contains("token"))
  304. {
  305. rtcToken = data["data"]["token"].ToString();
  306. }
  307. else
  308. {
  309. rtcToken = "";
  310. }
  311. createWebRtcTransport();
  312. SCRtcFactory.Instance.mSCRtcHandle.initRtpCapabilities(data["data"].ToJson());
  313. // joinRoom(data.ToJson());
  314. break;
  315. case "createSendTransport":
  316. // Debug.Log("createSendTransport=========>");
  317. SCRtcFactory.Instance.mSCRtcHandle.createSendTransport(data["data"].ToJson());
  318. createRecvTransport();
  319. break;
  320. case "createRecvTransport":
  321. // Debug.Log("createRecvTransport=========>");
  322. SCRtcFactory.Instance.mSCRtcHandle.createRecvTransport(data["data"].ToJson());
  323. changeDisplayName();
  324. break;
  325. case "joinRoom":
  326. // Debug.Log("joinRoom=========>");
  327. SCRtcFactory.Instance.mSCRtcHandle.joinRoom(data["data"].ToJson());
  328. data["action"] = "onNotification";
  329. data["method"] = "joinRoom";
  330. SCRtcFactory.Instance.mSCRtcManager.OnRtcListener(data);
  331. if(onRtcInit!=null)
  332. onRtcInit(data);
  333. break;
  334. case "changeDisplayName":
  335. Debug.Log("changeDisplayName=========>");
  336. SCRtcFactory.Instance.mSCRtcHandle.getDevice();
  337. // #if UNITY_EDITOR || !UNITY_ANDROID
  338. joinRoom("{}");
  339. //#endif
  340. break;
  341. default:
  342. break;
  343. }
  344. idList.Remove(data["id"].ToString());
  345. }
  346. }
  347. }
  348. }