SCRtcManager.cs 7.1 KB

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