SCRtcFactory.cs 2.8 KB

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