SCRtcFactory.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using LitJson;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using XRTool.Util;
  7. public class SCRtcFactory
  8. {
  9. private static SCRtcFactory _instance;
  10. public static SCRtcFactory Instance
  11. {
  12. get
  13. {
  14. if (_instance == null)
  15. {
  16. _instance = new SCRtcFactory();
  17. }
  18. return _instance;
  19. }
  20. }
  21. public SCRtcManager mSCRtcManager;
  22. public SCRtcHandle mSCRtcHandle;
  23. public SCRtcMe mSCRtcMe;
  24. public SCRtcPeers mSCRtcPeers;
  25. public bool isClose = true;
  26. public SCRtcFactory()
  27. {
  28. mSCRtcHandle = SCRtcHandle.Instance;
  29. }
  30. public SCRtcConfig mSCRtcConfig;
  31. public void init(SCRtcConfig sRtc, SCRtcManager ms, SCRtcMe me, SCRtcPeers sPeers=null)
  32. {
  33. this.mSCRtcConfig = sRtc;
  34. if (sPeers != null)
  35. {
  36. this.mSCRtcPeers = sPeers;
  37. }
  38. else
  39. {
  40. this.mSCRtcPeers = new SCRtcPeers();
  41. }
  42. this.mSCRtcManager = ms;
  43. if (me != null)
  44. {
  45. this.mSCRtcMe = me;
  46. }
  47. else
  48. {
  49. this.mSCRtcMe = new SCRtcMe();
  50. }
  51. mSCRtcHandle.InitRTC(sRtc);
  52. isClose = false;
  53. }
  54. public void Close()
  55. {
  56. this.mSCRtcHandle.Close();
  57. //
  58. }
  59. public void Update()
  60. {
  61. if(!isClose)
  62. {
  63. mSCRtcHandle.goThread();
  64. if (this.mSCRtcManager != null)
  65. {
  66. if (this.mSCRtcManager.queue.Count > 0)
  67. this.mSCRtcManager.OnRtcListener(this.mSCRtcManager.queue.Dequeue());
  68. }
  69. if (mSCRtcMe != null)
  70. mSCRtcMe.updateTexture();
  71. if (mSCRtcPeers != null)
  72. mSCRtcPeers.updateTextures();
  73. if (WSHandler.Rtc != null && WSHandler.Rtc.newConsumers.Count > 0)
  74. {
  75. JsonData data = JsonMapper.ToObject(WSHandler.Rtc.newConsumers.Dequeue());
  76. TimerMgr.Instance.CreateTimer(() => {
  77. SCRtcPeer p = SCRtcFactory.Instance.mSCRtcPeers.getPeer(data["data"]["peerId"].ToString());
  78. if (p != null)
  79. {
  80. // p.setInfo(data["data"]["kind"].ToString(), data["data"]["id"].ToString(), !bool.Parse(data["data"]["producerPaused"].ToString()));
  81. p = SCRtcFactory.Instance.mSCRtcPeers.getPeer(data["data"]["peerId"].ToString());
  82. if (p != null)
  83. {
  84. p.setInfo(data["data"]["kind"].ToString(), data["data"]["id"].ToString(), !bool.Parse(data["data"]["producerPaused"].ToString()));
  85. }
  86. SCRtcFactory.Instance.mSCRtcHandle.onNewConsumer(data["data"].ToJson(), data["id"].ToString());
  87. }
  88. }, float.Parse(data["data"]["rtpParameters"]["mid"].ToString())/10 + 0.1f);
  89. }
  90. }
  91. }
  92. }