RemoteRtc.cs 9.8 KB

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