123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- using LitJson;
- using ShadowStudio.UI;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Reflection.Emit;
- using UnityEngine;
- using UnityEngine.UI;
- using XRTool.Util;
- using rtc;
- using static ScenesManager;
- public class RemoteRtc : SCRtcManager
- {
- public Texture2D NoView;
- public Texture2D NoUser;
- public Camera cam;
- public MeshRenderer meshRender;
- public Camera cam2;
- //public MeshRenderer meshRender2;
- public static RemoteRtc Instance;
- public Texture2D testTexture;
- public Transform[] trans;
- public Transform cube;
- // Start is called before the first frame update
- void Start()
- {
- //if (DeviceType.type == "DreamGlass")
- //{
- // CustomInfo.mWidth = 640;
- // CustomInfo.mHight = 360;
- //}
- //WSHandler.Rtc.onRtcState += onRtcState;
- //DontDestroyOnLoad(this.gameObject);
- //Instance = this;
- // InitCamera();
- }
- public void InitCamera()
- {
- switch (DeviceType.type)
- {
- case "A01":
- StartCoroutine(CallCamera());
- meshRender.transform.localEulerAngles = new Vector3(0, 0, 0);
- break;
- case "Nreal":
- #if !UNITY_EDITOR // 打非Nreal包的时候这段代码注释
- //NRKernal.NRRGBCamTexture RGBCamTexture = new NRKernal.NRRGBCamTexture();
- //meshRender.transform.localEulerAngles = new Vector3(180, 0, 0);
- //meshRender.material.mainTexture = RGBCamTexture.GetTexture();
- //RGBCamTexture.Play();
- #endif
- break;
- case "DreamGlass":
- StartCoroutine(CallCamera());
- meshRender.transform.localEulerAngles = new Vector3(0, 0, 180);
- break;
- case "Rhinox":
- #if !UNITY_EDITOR
- StartCoroutine(CallCamera(2));
- meshRender.transform.localEulerAngles = new Vector3(0, 0, 0);
- #elif UNITY_EDITOR
- StartCoroutine(CallCamera());
- #endif
- break;
- }
- }
- void onRtcState(string state)
- {
- if(state=="joinRoom")
- {
- ScenesManager.Instance.showWindow(SceneType.ShowRoom);
- }
- if(state=="joined"||state=="closed")
- {
- RoomOtherUser.Instance.UpdateList();
- }
- }
- public void setVolume(float f)
- {
- SCRtcFactory.Instance.mSCRtcHandle.audioMeChange(f);
- }
- public void setVolume(string cid, float f)
- {
- // SCRtcFactory.Instance.mSCRtcHandle.audioPeerChange(cid, f);
- }
- public void sendMic(bool boo)
- {
- SCRtcFactory.Instance.mSCRtcHandle.sendMic(boo);
- }
- public void sendVideo(bool boo)
- {
- SCRtcFactory.Instance.mSCRtcHandle.sendVideo(boo);
- }
- public void muteVideo(bool boo)
- {
- SCRtcFactory.Instance.mSCRtcHandle.sendVideo(boo);
- }
- public void muteAudio(bool boo)
- {
- SCRtcFactory.Instance.mSCRtcHandle.sendVideo(boo);
- }
- public void closeRev(string id)
- {
- SCRtcFactory.Instance.mSCRtcHandle.closeRev(id);
- }
- public void startRev(string id)
- {
- SCRtcFactory.Instance.mSCRtcHandle.openRev(id);
- }
- public RawImage image;
- public RenderTexture rt;
- public Texture2D tex;
- public void startCamera()
- {
- if (rt == null)
- {
- rt = new RenderTexture(CustomInfo.mWidth, CustomInfo.mHight, 1);
- }
- cam.targetTexture = rt;
- image.texture = rt;
- me.setMyCamera(rt);
- //if (tex == null)
- //{
- // tex = new Texture2D(CustomInfo.mWidth, CustomInfo.mHight, TextureFormat.RGBA32, false);
- //}
- //meshRender.material.mainTexture = tex;
- //SCRtcFactory.Instance.mSCRtcHandle.addCameraLocal(meshRender.material.mainTexture.GetNativeTexturePtr().ToInt32());
- //SCRtcFactory.Instance.mSCRtcHandle.startCamera();
- //meshRender.transform.localEulerAngles = new Vector3(0, 180, 180);
- meshRender.gameObject.SetActive(true);
- }
- public void stopCamera()
- {
- // meshRender.gameObject.SetActive(false);
- //SCRtcFactory.Instance.mSCRtcHandle.stopCamera();
- }
- public void openSpeaker()
- {
- SCRtcFactory.Instance.mSCRtcHandle.openSpeaker();
- }
- public CustomMe me;
- public CustomPeerList customPeerList;
- public void start(string roomID)
- {
- CustomInfo.RoomId = roomID;
- InitListener();
- SCRtcConfig sconfig = new SCRtcConfig();
- sconfig.Url = CustomInfo.url;
- sconfig.Post = CustomInfo.POST;
- sconfig.RoomId = CustomInfo.RoomId;
- sconfig.Token = UserInfo.User_Token;
- sconfig.disPlayName = UserInfo.Account;
- sconfig.roomPwd = CustomInfo.roomPwd;
- sconfig.isRevAllAudio = true;
- sconfig.isRevAllVideo = true;
- sconfig.isSendAudio = false;
- sconfig.isSendVideo = false;
- sconfig.mWidth = CustomInfo.mWidth;
- sconfig.mHight = CustomInfo.mHight;
- sconfig.FPS = CustomInfo.FPS;
- me = new CustomMe();
- customPeerList = new CustomPeerList();
- SCRtcFactory.Instance.init(sconfig, this, me, customPeerList);
- RenderTexture myRenderTexture = new RenderTexture(sconfig.mWidth, sconfig.mHight, 1);
- }
- //关闭RTC
- public void Close()
- {
- SCRtcFactory.Instance.Close();
- if (SCRtcFactory.Instance != null && SCRtcFactory.Instance.mSCRtcPeers != null)
- {
- SCRtcFactory.Instance.mSCRtcPeers.initPeers();
- }
- int ct = PopPeerView.Instance.bigList.Count;
- for (int j = ct - 1; j >= 0; j--)
- {
- PopPeerView.Instance.bigList[j].closeView();
- }
- }
- //public TextMesh textmesh;
- // Update is called once per frame
- void Update()
- {
- this.transform.position = OpenXRCamera.Instance.head.position;
- this.transform.rotation = OpenXRCamera.Instance.head.rotation;
- //textmesh.text = SCRtcFactory.Instance.mSCRtcHandle.getwifi();
- //cam.transform.position = SvrManager.Instance.head.position;
- //cam.transform.rotation = SvrManager.Instance.head.rotation;
- // SCRtcFactory.Instance.Update();
- /*
- Vector3[] v3s = CameraView.GetCorners(1,cam2);
- for (int i = 0; i < v3s.Length; i++)
- {
- trans[i].position = cam2.TransformPoint( v3s[i]);
- }
- cube.position = CameraView.PosChange(0.2f, 0.3f, 1, cam2);*/
- }
- public void changeViewClose()
- {
- CustomInfo.isCloseView = true;
- //cam.cullingMask = 1 << 9;
- WSHandler.Rtc.onRtcState("changeView");
- }
- public void changeViewOpen()
- {
- CustomInfo.isCloseView = false;
- //cam.cullingMask = -1;
- WSHandler.Rtc.onRtcState("changeView");
- }
- public WebCamTexture webTex;
- private string deviceName;
- IEnumerator CallCamera(int index=0)
- {
- yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
- if (Application.HasUserAuthorization(UserAuthorization.WebCam))
- {
- WebCamDevice[] devices = WebCamTexture.devices;
- Debug.Log("设备摄像头:"+devices.Length);
- deviceName = devices[index].name;
- if (webTex != null)
- {
- webTex.Stop();
- webTex = null;
- }
- //设置摄像机摄像的区域
- webTex = new WebCamTexture(deviceName, CustomInfo.mWidth, CustomInfo.mHight, CustomInfo.FPS);
- // webTex = new WebCamTexture(deviceName, 640, 480, CustomInfo.FPS);
- meshRender.material.mainTexture = webTex;
- webTex.Play();//开始摄像
- }
- }
- /// <summary>
- /// 过滤虚拟内容层级
- /// </summary>
- public void FiltrationCamera(bool state)
- {
- int currentCullingMask = cam.cullingMask;
- int layerMask = 1 << LayerMask.NameToLayer("Arrow");
- if (state)
- currentCullingMask |= layerMask;
- else
- currentCullingMask &= ~layerMask;
- cam.cullingMask = currentCullingMask;
- }
- public void InitCenter()
- {
- WSHandler.Office.OnInit -= Init;
- WSHandler.Office.OnInit += Init;
- WSHandler.init();
- }
- private void Init(JsonData data)
- {
- if (PopUpInfo.Instance)
- {
- PopUpInfo.Instance.HideNetErrorPanel();
- }
- if (ShowRoom.Instance && ShowRoom.Instance.gameObject.activeSelf)
- {
- if (PopUpInfo.Instance)
- {
- PopUpInfo.Instance.ShowNetErrorPanel();
- }
- OnJoinRoom(WSHandler._roomid);
- }
- WSHandler.Office.OnInit -= Init;
- }
- public void OnJoinRoom(string roomId)
- {
- RoomMainInfo.roomNum = roomId;
- WSHandler.Office.OnJoinRoomReveived -= joinRoom;
- WSHandler.Office.OnJoinRoomReveived += joinRoom;
- WSHandler.Office.JoinRoom(roomId);
- }
- private void joinRoom(JsonData data)
- {
- if (data.Keys.Contains("data"))
- {
- switch (data["data"]["code"].ToString())
- {
- case "200":
- WSHandler.roomRtcinit(RoomMainInfo.roomNum);
- WSHandler.Office.ChangeUserType(UserInfo.BusyType);
- break;
- case "1000":
- PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, "房间号无效");
- ShowNetError();
- break;
- case "1001":
- PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, "房间人数已满");
- ShowNetError();
- break;
- case "1003":
- PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, "房间不存在");
- ShowNetError();
- break;
- default:
- PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, "房间不存在");
- ShowNetError();
- break;
- }
- if (PopUpInfo.Instance)
- {
- PopUpInfo.Instance.HideNetErrorPanel();
- }
- }
- else
- {
- Debug.LogError("消息错误");
- }
- WSHandler.Office.OnJoinRoomReveived -= joinRoom;
- }
- private void ShowNetError()
- {
- if (ShowRoom.Instance)
- {
- ShowRoom.Instance.ExitRoom();
- }
- ScenesManager.Instance.showWindow(SceneType.ShowOffice);
- }
- }
|