StudioRtc.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. Debug.Log("startCamera===>");
  65. if (!CheckDeviceType.checkIsPhone())
  66. {
  67. if (DeviceType.type == "Phone")
  68. {
  69. }
  70. else
  71. {
  72. ConsoleDlg.Instance.vufora_camera.enabled = false;
  73. Vuforia.VuforiaBehaviour.Instance.enabled = false;
  74. }
  75. }
  76. if (rt == null)
  77. {
  78. rt = new RenderTexture(CustomInfo.mWidth, CustomInfo.mHight, 1);
  79. }
  80. XRRGBCamera.Instance.playCamera();
  81. //cam.targetTexture = rt;
  82. //me.setMyCamera(rt);
  83. //if (tex == null)
  84. //{
  85. // tex = new Texture2D(CustomInfo.mWidth, CustomInfo.mHight, TextureFormat.RGBA32, false);
  86. //}
  87. //if (!CheckDeviceType.checkIsPhone())
  88. //{
  89. // meshRender.material.mainTexture = tex;
  90. // SCRtcFactory.Instance.mSCRtcHandle.addCameraLocal(meshRender.material.mainTexture.GetNativeTexturePtr().ToInt32());
  91. // SCRtcFactory.Instance.mSCRtcHandle.startCamera();
  92. // meshRender.transform.localEulerAngles = new Vector3(0, 180, 180);
  93. // meshRender.gameObject.SetActive(true);
  94. //}
  95. }
  96. public void stopCamera()
  97. {
  98. meshRender.gameObject.SetActive(false);
  99. SCRtcFactory.Instance.mSCRtcHandle.stopCamera();
  100. }
  101. public void openSpeaker()
  102. {
  103. SCRtcFactory.Instance.mSCRtcHandle.openSpeaker();
  104. }
  105. public CustomMe me;
  106. public CustomPeerList customPeerList;
  107. public void start(string roomID)
  108. {
  109. CustomInfo.RoomId = roomID;
  110. InitListener();
  111. SCRtcConfig sconfig = new SCRtcConfig();
  112. sconfig.Url = CustomInfo.url;
  113. sconfig.Post = CustomInfo.POST;
  114. sconfig.RoomId = CustomInfo.RoomId;
  115. sconfig.Token = UserInfo.User_Token;
  116. sconfig.disPlayName = CommonMethod.MyPeer.PeerId;
  117. sconfig.roomPwd = CustomInfo.roomPwd;
  118. sconfig.isRevAllAudio = true;
  119. sconfig.isRevAllVideo = true;
  120. sconfig.isSendAudio = CustomInfo.isSendAudio;
  121. sconfig.isSendVideo = CustomInfo.isSendVideo;
  122. sconfig.mWidth = CustomInfo.mWidth;
  123. sconfig.mHight = CustomInfo.mHight;
  124. sconfig.FPS = CustomInfo.FPS;
  125. me = new CustomMe();
  126. customPeerList =new CustomPeerList();
  127. SCRtcFactory.Instance.init(sconfig, this, me, customPeerList);
  128. // RenderTexture myRenderTexture = new RenderTexture(sconfig.mWidth, sconfig.mHight, 1);
  129. Debug.Log("rtcinit==================================>");
  130. }
  131. //关闭RTC
  132. public void Close()
  133. {
  134. Debug.Log("退出房间CloseStart");
  135. if (SCRtcFactory.Instance != null)
  136. {
  137. SCRtcFactory.Instance.Close();
  138. if (SCRtcFactory.Instance.mSCRtcPeers != null)
  139. {
  140. SCRtcFactory.Instance.mSCRtcPeers.initPeers();
  141. }
  142. }
  143. Debug.Log("退出房间CloseEnd");
  144. }
  145. Timer timeCreator;
  146. int netCount;
  147. // Update is called once per frame
  148. void Update()
  149. {
  150. cam.transform.position = OpenXRCamera.Instance.head.position;
  151. cam.transform.rotation = OpenXRCamera.Instance.head.rotation;
  152. SCRtcFactory.Instance.Update();
  153. if(CommonMethod.NetLoading&&CommonMethod.NetLoading.activeSelf)
  154. {
  155. if(timeCreator==null)
  156. {
  157. timeCreator = TimerMgr.Instance.CreateTimer(() => {
  158. int ct = CommonMethod.queueList.Count;
  159. for (int i = 0; i < ct; i++)
  160. {
  161. CommonMethod.queueList.Dequeue();
  162. }
  163. SCInputModule.Instance.CanProcessEvent = true;
  164. netCount = 0;
  165. }, 30f);
  166. }
  167. }
  168. else
  169. {
  170. if(timeCreator!=null)
  171. {
  172. timeCreator.CleanUp();
  173. timeCreator = null;
  174. }
  175. netCount = 0;
  176. }
  177. }
  178. }