SCRtcManager.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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());
  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"]["displayName"].ToString());
  70. Debug.Log("joinedjoinedjoinedjoinedjoined");
  71. break;
  72. case "joinRoom":
  73. if (data["data"]["room"]["users"].Count > 0)
  74. {
  75. for (int i = 0; i < data["data"]["room"]["users"].Count; i++)
  76. {
  77. SCRtcFactory.Instance.mSCRtcPeers.addPeers(data["data"]["room"]["users"][i]["peerId"].ToString()).setName(data["data"]["room"]["users"][i]["displayName"].ToString()); ;
  78. }
  79. }
  80. break;
  81. case "init":
  82. SCRtcFactory.Instance.mSCRtcConfig.routerRtpCapabilities = data["data"].ToJson();
  83. break;
  84. }
  85. break;
  86. }
  87. }
  88. }
  89. }