RemoteRtc.cs 10 KB

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