RemoteRtc.cs 8.4 KB

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