RemoteRtc.cs 10 KB

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