StudioRtc.cs 5.8 KB

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