StudioRtc.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using BeinLab.Util;
  2. using SC.XR.Unity.Module_InputSystem;
  3. using ShadowStudio.UI;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using Vuforia;
  8. using XRTool.Util;
  9. public class StudioRtc : SCRtcManager
  10. {
  11. public Camera cam;
  12. public MeshRenderer meshRender;
  13. public static StudioRtc Instance;
  14. // Start is called before the first frame update
  15. void Start()
  16. {
  17. DontDestroyOnLoad(this.gameObject);
  18. Instance = this;
  19. }
  20. public void setVolume(float f)
  21. {
  22. SCRtcFactory.Instance.mSCRtcHandle.audioMeChange(f);
  23. }
  24. public void setVolume(string cid,float f)
  25. {
  26. SCRtcFactory.Instance.mSCRtcHandle.audioPeerChange(cid,f);
  27. }
  28. public void sendMic(bool boo)
  29. {
  30. #if UNITY_ANDROID && !UNITY_EDITOR
  31. //if (boo && StudioRtc.Instance.me.cIdA == "")
  32. //CommonMethod.ShowNetLoading("音視頻_socket", "正在开启麦克风");
  33. #endif
  34. SCRtcFactory.Instance.mSCRtcHandle.sendMic(boo);
  35. }
  36. public void sendVideo(bool boo)
  37. {
  38. #if UNITY_ANDROID && !UNITY_EDITOR
  39. //if (boo&& StudioRtc.Instance.me.cIdV == "")
  40. //CommonMethod.ShowNetLoading("音視頻_socket", "正在开启摄像头");
  41. #endif
  42. SCRtcFactory.Instance.mSCRtcHandle.sendVideo(boo);
  43. }
  44. public void muteVideo(bool boo)
  45. {
  46. SCRtcFactory.Instance.mSCRtcHandle.sendVideo(boo);
  47. }
  48. public void muteAudio(bool boo)
  49. {
  50. SCRtcFactory.Instance.mSCRtcHandle.sendVideo(boo);
  51. }
  52. public void closeRev(string id)
  53. {
  54. SCRtcFactory.Instance.mSCRtcHandle.closeRev(id);
  55. }
  56. public void startRev(string id)
  57. {
  58. SCRtcFactory.Instance.mSCRtcHandle.openRev(id);
  59. }
  60. public RenderTexture rt;
  61. public Texture2D tex;
  62. public void startCamera()
  63. {
  64. if (!CheckDeviceType.checkIsPhone())
  65. {
  66. ConsoleDlg.Instance.vufora_camera.enabled = false;
  67. Vuforia.VuforiaBehaviour.Instance.enabled = false;
  68. }
  69. if (rt == null)
  70. {
  71. rt = new RenderTexture(CustomInfo.mWidth, CustomInfo.mHight, 1);
  72. }
  73. //cam.targetTexture = rt;
  74. //me.setMyCamera(rt);
  75. //if (tex == null)
  76. //{
  77. // tex = new Texture2D(CustomInfo.mWidth, CustomInfo.mHight, TextureFormat.RGBA32, false);
  78. //}
  79. //if (!CheckDeviceType.checkIsPhone())
  80. //{
  81. // meshRender.material.mainTexture = tex;
  82. // SCRtcFactory.Instance.mSCRtcHandle.addCameraLocal(meshRender.material.mainTexture.GetNativeTexturePtr().ToInt32());
  83. // SCRtcFactory.Instance.mSCRtcHandle.startCamera();
  84. // meshRender.transform.localEulerAngles = new Vector3(0, 180, 180);
  85. // meshRender.gameObject.SetActive(true);
  86. //}
  87. }
  88. public void stopCamera()
  89. {
  90. meshRender.gameObject.SetActive(false);
  91. SCRtcFactory.Instance.mSCRtcHandle.stopCamera();
  92. }
  93. public void openSpeaker()
  94. {
  95. SCRtcFactory.Instance.mSCRtcHandle.openSpeaker();
  96. }
  97. public CustomMe me;
  98. public CustomPeerList customPeerList;
  99. public void start(string roomID)
  100. {
  101. CustomInfo.RoomId = roomID;
  102. InitListener();
  103. SCRtcConfig sconfig = new SCRtcConfig();
  104. sconfig.Url = CustomInfo.url;
  105. sconfig.Post = CustomInfo.POST;
  106. sconfig.RoomId = CustomInfo.RoomId;
  107. sconfig.Token = UserInfo.User_Token;
  108. sconfig.disPlayName = CommonMethod.MyPeer.PeerId;
  109. sconfig.roomPwd = CustomInfo.roomPwd;
  110. sconfig.isRevAllAudio = true;
  111. sconfig.isRevAllVideo = true;
  112. sconfig.isSendAudio = CustomInfo.isSendAudio;
  113. sconfig.isSendVideo = CustomInfo.isSendVideo;
  114. sconfig.mWidth = CustomInfo.mWidth;
  115. sconfig.mHight = CustomInfo.mHight;
  116. sconfig.FPS = CustomInfo.FPS;
  117. me = new CustomMe();
  118. customPeerList =new CustomPeerList();
  119. SCRtcFactory.Instance.init(sconfig, this, me, customPeerList);
  120. // RenderTexture myRenderTexture = new RenderTexture(sconfig.mWidth, sconfig.mHight, 1);
  121. Debug.Log("rtcinit==================================>");
  122. }
  123. //关闭RTC
  124. public void Close()
  125. {
  126. Debug.Log("退出房间CloseStart");
  127. if (SCRtcFactory.Instance != null)
  128. {
  129. SCRtcFactory.Instance.Close();
  130. if (SCRtcFactory.Instance.mSCRtcPeers != null)
  131. {
  132. SCRtcFactory.Instance.mSCRtcPeers.initPeers();
  133. }
  134. }
  135. Debug.Log("退出房间CloseEnd");
  136. }
  137. Timer timeCreator;
  138. int netCount;
  139. // Update is called once per frame
  140. void Update()
  141. {
  142. cam.transform.position = GSXRManager.Instance.head.position;
  143. cam.transform.rotation = GSXRManager.Instance.head.rotation;
  144. SCRtcFactory.Instance.Update();
  145. if(CommonMethod.NetLoading&&CommonMethod.NetLoading.activeSelf)
  146. {
  147. if(timeCreator==null)
  148. {
  149. timeCreator = TimerMgr.Instance.CreateTimer(() => {
  150. int ct = CommonMethod.queueList.Count;
  151. for (int i = 0; i < ct; i++)
  152. {
  153. CommonMethod.queueList.Dequeue();
  154. }
  155. SCInputModule.Instance.CanProcessEvent = true;
  156. netCount = 0;
  157. }, 30f);
  158. }
  159. }
  160. else
  161. {
  162. if(timeCreator!=null)
  163. {
  164. timeCreator.CleanUp();
  165. timeCreator = null;
  166. }
  167. netCount = 0;
  168. }
  169. }
  170. }