RemoteRtc.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using Agora.Util;
  2. using LitJson;
  3. using ShadowStudio.UI;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Reflection;
  8. using System.Reflection.Emit;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. using XRTool.Util;
  12. using static ScenesManager;
  13. public class RemoteRtc : SCRtcManager
  14. {
  15. public Texture2D NoView;
  16. public Texture2D NoUser;
  17. public Camera cam;
  18. public MeshRenderer meshRender;
  19. public Camera cam2;
  20. //public MeshRenderer meshRender2;
  21. public static RemoteRtc Instance;
  22. public Texture2D testTexture;
  23. public Transform[] trans;
  24. public Transform cube;
  25. // Start is called before the first frame update
  26. void Start()
  27. {
  28. PermissionHelper.RequestMicrophontPermission();
  29. PermissionHelper.RequestCameraPermission();
  30. if (DeviceType.type == "DreamGlass")
  31. {
  32. CustomInfo.mWidth = 640;
  33. CustomInfo.mHight = 360;
  34. }
  35. WSHandler.Rtc.onRtcState += onRtcState;
  36. DontDestroyOnLoad(this.gameObject);
  37. Instance = this;
  38. // InitCamera();
  39. }
  40. public void InitCamera()
  41. {
  42. switch (DeviceType.type)
  43. {
  44. case "A01":
  45. StartCoroutine(CallCamera());
  46. meshRender.transform.localEulerAngles = new Vector3(0, 0, 0);
  47. break;
  48. case "Nreal":
  49. #if !UNITY_EDITOR // 打非Nreal包的时候这段代码注释
  50. //NRKernal.NRRGBCamTexture RGBCamTexture = new NRKernal.NRRGBCamTexture();
  51. //meshRender.transform.localEulerAngles = new Vector3(180, 0, 0);
  52. //meshRender.material.mainTexture = RGBCamTexture.GetTexture();
  53. //RGBCamTexture.Play();
  54. #endif
  55. break;
  56. case "DreamGlass":
  57. StartCoroutine(CallCamera());
  58. meshRender.transform.localEulerAngles = new Vector3(0, 0, 180);
  59. break;
  60. case "Rhinox":
  61. #if !UNITY_EDITOR
  62. StartCoroutine(CallCamera(2));
  63. meshRender.transform.localEulerAngles = new Vector3(0, 0, 0);
  64. #elif UNITY_EDITOR
  65. StartCoroutine(CallCamera());
  66. #endif
  67. break;
  68. }
  69. }
  70. void onRtcState(string state)
  71. {
  72. if(state=="joinRoom")
  73. {
  74. ScenesManager.Instance.showWindow(SceneType.ShowRoom);
  75. }
  76. if(state=="joined"||state=="closed")
  77. {
  78. RoomOtherUser.Instance.UpdateList();
  79. }
  80. }
  81. public void setVolume(float f)
  82. {
  83. SCRtcFactory.Instance.mSCRtcHandle.audioMeChange(f);
  84. }
  85. public void setVolume(string cid, float f)
  86. {
  87. // SCRtcFactory.Instance.mSCRtcHandle.audioPeerChange(cid, f);
  88. }
  89. public void sendMic(bool boo)
  90. {
  91. SCRtcFactory.Instance.mSCRtcHandle.sendMic(boo);
  92. }
  93. public void sendVideo(bool boo)
  94. {
  95. SCRtcFactory.Instance.mSCRtcHandle.sendVideo(boo);
  96. }
  97. public void muteVideo(bool boo)
  98. {
  99. SCRtcFactory.Instance.mSCRtcHandle.sendVideo(boo);
  100. }
  101. public void muteAudio(bool boo)
  102. {
  103. SCRtcFactory.Instance.mSCRtcHandle.sendVideo(boo);
  104. }
  105. public void closeRev(string id)
  106. {
  107. SCRtcFactory.Instance.mSCRtcHandle.closeRev(id);
  108. }
  109. public void startRev(string id)
  110. {
  111. SCRtcFactory.Instance.mSCRtcHandle.openRev(id);
  112. }
  113. public RawImage image;
  114. public RenderTexture rt;
  115. public Texture2D tex;
  116. public void startCamera()
  117. {
  118. if (rt == null)
  119. {
  120. rt = new RenderTexture(CustomInfo.mWidth, CustomInfo.mHight, 1);
  121. }
  122. cam.targetTexture = rt;
  123. image.texture = rt;
  124. me.setMyCamera(rt);
  125. //if (tex == null)
  126. //{
  127. // tex = new Texture2D(CustomInfo.mWidth, CustomInfo.mHight, TextureFormat.RGBA32, false);
  128. //}
  129. //meshRender.material.mainTexture = tex;
  130. //SCRtcFactory.Instance.mSCRtcHandle.addCameraLocal(meshRender.material.mainTexture.GetNativeTexturePtr().ToInt32());
  131. //SCRtcFactory.Instance.mSCRtcHandle.startCamera();
  132. //meshRender.transform.localEulerAngles = new Vector3(0, 180, 180);
  133. meshRender.gameObject.SetActive(true);
  134. }
  135. public void stopCamera()
  136. {
  137. // meshRender.gameObject.SetActive(false);
  138. //SCRtcFactory.Instance.mSCRtcHandle.stopCamera();
  139. }
  140. public void openSpeaker()
  141. {
  142. SCRtcFactory.Instance.mSCRtcHandle.openSpeaker();
  143. }
  144. public CustomMe me;
  145. public CustomPeerList customPeerList;
  146. public void start(string roomID)
  147. {
  148. CustomInfo.RoomId = roomID;
  149. InitListener();
  150. SCRtcConfig sconfig = new SCRtcConfig();
  151. sconfig.Url = CustomInfo.url;
  152. sconfig.Post = CustomInfo.POST;
  153. sconfig.RoomId = CustomInfo.RoomId;
  154. sconfig.Token = UserInfo.User_Token;
  155. sconfig.disPlayName = UserInfo.Account;
  156. sconfig.roomPwd = CustomInfo.roomPwd;
  157. sconfig.isRevAllAudio = true;
  158. sconfig.isRevAllVideo = true;
  159. sconfig.isSendAudio = false;
  160. sconfig.isSendVideo = false;
  161. sconfig.mWidth = CustomInfo.mWidth;
  162. sconfig.mHight = CustomInfo.mHight;
  163. sconfig.FPS = CustomInfo.FPS;
  164. me = new CustomMe();
  165. customPeerList = new CustomPeerList();
  166. SCRtcFactory.Instance.init(sconfig, this, me, customPeerList);
  167. RenderTexture myRenderTexture = new RenderTexture(sconfig.mWidth, sconfig.mHight, 1);
  168. }
  169. //关闭RTC
  170. public void Close()
  171. {
  172. SCRtcFactory.Instance.Close();
  173. if (SCRtcFactory.Instance != null && SCRtcFactory.Instance.mSCRtcPeers != null)
  174. {
  175. SCRtcFactory.Instance.mSCRtcPeers.initPeers();
  176. }
  177. int ct = PopPeerView.Instance.bigList.Count;
  178. for (int j = ct - 1; j >= 0; j--)
  179. {
  180. PopPeerView.Instance.bigList[j].closeView();
  181. }
  182. }
  183. //public TextMesh textmesh;
  184. // Update is called once per frame
  185. void Update()
  186. {
  187. this.transform.position = OpenXRCamera.Instance.head.position;
  188. this.transform.rotation = OpenXRCamera.Instance.head.rotation;
  189. //textmesh.text = SCRtcFactory.Instance.mSCRtcHandle.getwifi();
  190. //cam.transform.position = SvrManager.Instance.head.position;
  191. //cam.transform.rotation = SvrManager.Instance.head.rotation;
  192. // SCRtcFactory.Instance.Update();
  193. /*
  194. Vector3[] v3s = CameraView.GetCorners(1,cam2);
  195. for (int i = 0; i < v3s.Length; i++)
  196. {
  197. trans[i].position = cam2.TransformPoint( v3s[i]);
  198. }
  199. cube.position = CameraView.PosChange(0.2f, 0.3f, 1, cam2);*/
  200. }
  201. public void changeViewClose()
  202. {
  203. CustomInfo.isCloseView = true;
  204. //cam.cullingMask = 1 << 9;
  205. WSHandler.Rtc.onRtcState("changeView");
  206. }
  207. public void changeViewOpen()
  208. {
  209. CustomInfo.isCloseView = false;
  210. //cam.cullingMask = -1;
  211. WSHandler.Rtc.onRtcState("changeView");
  212. }
  213. public WebCamTexture webTex;
  214. private string deviceName;
  215. IEnumerator CallCamera(int index=0)
  216. {
  217. yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
  218. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  219. {
  220. WebCamDevice[] devices = WebCamTexture.devices;
  221. Debug.Log("设备摄像头:"+devices.Length);
  222. deviceName = devices[index].name;
  223. if (webTex != null)
  224. {
  225. webTex.Stop();
  226. webTex = null;
  227. }
  228. //设置摄像机摄像的区域
  229. webTex = new WebCamTexture(deviceName, CustomInfo.mWidth, CustomInfo.mHight, CustomInfo.FPS);
  230. // webTex = new WebCamTexture(deviceName, 640, 480, CustomInfo.FPS);
  231. meshRender.material.mainTexture = webTex;
  232. webTex.Play();//开始摄像
  233. }
  234. }
  235. /// <summary>
  236. /// 过滤虚拟内容层级
  237. /// </summary>
  238. public void FiltrationCamera(bool state)
  239. {
  240. int currentCullingMask = cam.cullingMask;
  241. int layerMask = 1 << LayerMask.NameToLayer("Arrow");
  242. if (state)
  243. currentCullingMask |= layerMask;
  244. else
  245. currentCullingMask &= ~layerMask;
  246. cam.cullingMask = currentCullingMask;
  247. }
  248. public void InitCenter()
  249. {
  250. WSHandler.Office.OnInit -= Init;
  251. WSHandler.Office.OnInit += Init;
  252. WSHandler.init();
  253. }
  254. private void Init(JsonData data)
  255. {
  256. if (PopUpInfo.Instance)
  257. {
  258. PopUpInfo.Instance.HideNetErrorPanel();
  259. }
  260. if (ShowRoom.Instance && ShowRoom.Instance.gameObject.activeSelf)
  261. {
  262. if (PopUpInfo.Instance)
  263. {
  264. PopUpInfo.Instance.ShowNetErrorPanel();
  265. }
  266. OnJoinRoom(WSHandler._roomid);
  267. }
  268. WSHandler.Office.OnInit -= Init;
  269. }
  270. public void OnJoinRoom(string roomId)
  271. {
  272. RoomMainInfo.roomNum = roomId;
  273. WSHandler.Office.OnJoinRoomReveived -= joinRoom;
  274. WSHandler.Office.OnJoinRoomReveived += joinRoom;
  275. WSHandler.Office.JoinRoom(roomId);
  276. }
  277. private void joinRoom(JsonData data)
  278. {
  279. if (data.Keys.Contains("data"))
  280. {
  281. switch (data["data"]["code"].ToString())
  282. {
  283. case "200":
  284. WSHandler.roomRtcinit(RoomMainInfo.roomNum);
  285. WSHandler.Office.ChangeUserType(UserInfo.BusyType);
  286. break;
  287. case "1000":
  288. PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, "房间号无效");
  289. ShowNetError();
  290. break;
  291. case "1001":
  292. PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, "房间人数已满");
  293. ShowNetError();
  294. break;
  295. case "1003":
  296. PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, "房间不存在");
  297. ShowNetError();
  298. break;
  299. default:
  300. PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, "房间不存在");
  301. ShowNetError();
  302. break;
  303. }
  304. if (PopUpInfo.Instance)
  305. {
  306. PopUpInfo.Instance.HideNetErrorPanel();
  307. }
  308. }
  309. else
  310. {
  311. Debug.LogError("消息错误");
  312. }
  313. WSHandler.Office.OnJoinRoomReveived -= joinRoom;
  314. }
  315. private void ShowNetError()
  316. {
  317. if (ShowRoom.Instance)
  318. {
  319. ShowRoom.Instance.ExitRoom();
  320. }
  321. ScenesManager.Instance.showWindow(SceneType.ShowOffice);
  322. }
  323. }