RemoteRtc.cs 8.7 KB

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