SCRtcManager.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using LitJson;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public abstract class SCRtcManager : MonoBehaviour
  7. {
  8. public Queue<JsonData> queue;
  9. private void Start()
  10. {
  11. }
  12. public void InitListener()
  13. {
  14. queue = new Queue<JsonData>();
  15. /*
  16. StopCoroutine("onUpdateRtc");
  17. StartCoroutine("onUpdateRtc");*/
  18. Debug.Log("StartListener");
  19. }
  20. private void Update()
  21. {
  22. SCRtcFactory.Instance.Update();
  23. }
  24. IEnumerator onUpdateRtc()
  25. {
  26. while (true)
  27. {
  28. yield return new WaitForEndOfFrame();
  29. SCRtcFactory.Instance.Update();
  30. yield return new WaitForEndOfFrame();
  31. }
  32. }
  33. public virtual void OnRtcListener(JsonData data)
  34. {
  35. string pid;
  36. SCRtcPeer p;
  37. if (data != null)
  38. {
  39. switch (data["action"].ToString())
  40. {
  41. case "OnListener":
  42. switch (data["data"]["action"].ToString())
  43. {
  44. case "addPeer":
  45. SCRtcFactory.Instance.mSCRtcPeers.getPeer(data["data"]["data"]["pid"].ToString()).setInfo(data["data"]["data"]["kind"].ToString(), data["data"]["data"]["cid"].ToString(),true);
  46. break;
  47. case "addVideo":
  48. pid = data["data"]["data"]["peerID"].ToString();
  49. SCRtcFactory.Instance.mSCRtcPeers.getPeer(pid).setTexture(data);
  50. break;
  51. case "changeResolution":
  52. pid = data["data"]["data"]["peerID"].ToString();
  53. if (pid != "Local")
  54. {
  55. SCRtcFactory.Instance.mSCRtcPeers.getPeer(pid).changeTexture(data);
  56. }
  57. break;
  58. }
  59. break;
  60. case "onNotification":
  61. switch (data["method"].ToString())
  62. {
  63. case "closed":
  64. pid = data["data"]["peerId"].ToString();
  65. SCRtcFactory.Instance.mSCRtcPeers.removePeers(pid);
  66. break;
  67. case "joined":
  68. p = SCRtcFactory.Instance.mSCRtcPeers.addPeers(data["data"]["peerId"].ToString());
  69. p.setName(data["data"]["nickName"].ToString());
  70. p.avater = data["data"]["avatar"].ToString();
  71. p.isSn =bool.Parse(data["data"]["sn"].ToString());
  72. break;
  73. case "joinRoom":
  74. if (data["data"]["room"]["users"].Count > 0)
  75. {
  76. for (int i = 0; i < data["data"]["room"]["users"].Count; i++)
  77. {
  78. SCRtcPeer sp = SCRtcFactory.Instance.mSCRtcPeers.addPeers(data["data"]["room"]["users"][i]["peerId"].ToString());
  79. sp.setName(data["data"]["room"]["users"][i]["nickName"].ToString()); ;
  80. sp.avater = data["data"]["room"]["users"][i]["avatar"].ToString();
  81. sp.isSn = bool.Parse(data["data"]["room"]["users"][i]["sn"].ToString());
  82. if (int.Parse(data["data"]["room"]["users"][i]["isCreator"].ToString()) == 1)
  83. {
  84. switch (data["data"]["room"]["users"][i]["sn"].ToString())
  85. {
  86. case "false":
  87. RoomMainInfo.isSN = false;
  88. break;
  89. case "true":
  90. RoomMainInfo.isSN = true;
  91. break;
  92. }
  93. RoomMainInfo.isCreator = "1";
  94. RoomMainInfo.RoomOwner = "用户" + data["data"]["room"]["users"][i]["nickName"].ToString() + "的房间";
  95. RoomMainInfo.RoomCreatorActivateType = int.Parse(data["data"]["room"]["users"][i]["activateType"].ToString());
  96. }
  97. }
  98. }
  99. else
  100. {
  101. if (!string.IsNullOrEmpty(RoomMainInfo.roomName))
  102. {
  103. RoomMainInfo.isCreator = "0";
  104. RoomMainInfo.RoomOwner = RoomMainInfo.roomName;
  105. RoomMainInfo.isSN = UserInfo.isSN;
  106. RoomMainInfo.RoomCreatorActivateType = UserInfo.activateType;
  107. }
  108. }
  109. if (data["data"]["room"].Keys.Contains("fileHistory"))
  110. {
  111. if (data["data"]["room"]["fileHistory"].Count > 0)
  112. {
  113. for (int i = 0; i < data["data"]["room"]["fileHistory"].Count; i++)
  114. {
  115. FileConfig fileConfig = new FileConfig();
  116. fileConfig.PeerId = data["data"]["room"]["fileHistory"][i]["peerId"].ToString();
  117. fileConfig.From = data["data"]["room"]["fileHistory"][i]["from"].ToString();
  118. fileConfig.CreateTime = data["data"]["room"]["fileHistory"][i]["createTime"].ToString();
  119. fileConfig.Url = data["data"]["room"]["fileHistory"][i]["url"].ToString();
  120. fileConfig.Path = data["data"]["room"]["fileHistory"][i]["path"].ToString();
  121. fileConfig.FileName = data["data"]["room"]["fileHistory"][i]["fileName"].ToString();
  122. if (ShowRoom.Instance)
  123. {
  124. ShowRoom.Instance.fileConfiglist.Add(fileConfig);
  125. }
  126. }
  127. }
  128. }
  129. break;
  130. case "init":
  131. SCRtcFactory.Instance.mSCRtcConfig.routerRtpCapabilities = data["data"].ToJson();
  132. SCRtcFactory.Instance.mSCRtcHandle.joinRoom(SCRtcFactory.Instance.mSCRtcConfig.routerRtpCapabilities);
  133. break;
  134. }
  135. break;
  136. }
  137. }
  138. }
  139. }