RemoteRtc.cs 9.5 KB

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