RemoteRtc.cs 10 KB

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