SCRtcManager.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using BeinLab.Util;
  2. using LitJson;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  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. WSHandler.Rtc.RtcResponse(data["data"]["data"]);
  48. WSHandler.Rtc.isnewConsumer = false;
  49. break;
  50. case "addVideo":
  51. pid = data["data"]["data"]["peerID"].ToString();
  52. SCRtcFactory.Instance.mSCRtcPeers.getPeer(pid).setTexture(data);
  53. break;
  54. case "changeResolution":
  55. pid = data["data"]["data"]["peerID"].ToString();
  56. if (pid != "Local")
  57. {
  58. SCRtcFactory.Instance.mSCRtcPeers.getPeer(pid).changeTexture(data);
  59. }
  60. break;
  61. }
  62. break;
  63. case "onNotification":
  64. switch (data["method"].ToString())
  65. {
  66. case "closed":
  67. pid = data["data"]["peerId"].ToString();
  68. Debug.Log("删除===>" + pid);
  69. SCRtcFactory.Instance.mSCRtcPeers.removePeers(pid);
  70. break;
  71. case "joined":
  72. p = SCRtcFactory.Instance.mSCRtcPeers.addPeers(data["data"]["peerId"].ToString());
  73. p.setName(data["data"]["displayName"].ToString());
  74. Debug.Log("添加===>" + p.name);
  75. break;
  76. case "joinRoom":
  77. if (data["data"]["room"]["users"].Count > 0)
  78. {
  79. for (int i = 0; i < data["data"]["room"]["users"].Count; i++)
  80. {
  81. SCRtcFactory.Instance.mSCRtcPeers.addPeers(data["data"]["room"]["users"][i]["peerId"].ToString()).setName(data["data"]["room"]["users"][i]["displayName"].ToString()); ;
  82. Debug.Log("添加===>" + data["data"]["room"]["users"][i]["displayName"].ToString());
  83. }
  84. }
  85. break;
  86. case "init":
  87. SCRtcFactory.Instance.mSCRtcConfig.routerRtpCapabilities = data["data"].ToJson();
  88. SCRtcFactory.Instance.mSCRtcHandle.joinRoom(SCRtcFactory.Instance.mSCRtcConfig.routerRtpCapabilities);
  89. break;
  90. }
  91. break;
  92. }
  93. }
  94. }
  95. }