using Agora.Rtc; using Agora.Util; using SC.XR.Unity; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.Serialization; using UnityEngine.UI; using XRTool.Util; using Logger = Agora.Util.Logger; public class AgoraVideoAudioManager : SingletonMono { [FormerlySerializedAs("appIdInput")] [SerializeField] private AppIdInput _appIdInput; [Header("_____________Basic Configuration_____________")] [FormerlySerializedAs("APP_ID")] [SerializeField] private string _appID = ""; [FormerlySerializedAs("TOKEN")] [SerializeField] private string _token = ""; [FormerlySerializedAs("CHANNEL_NAME")] [SerializeField] private string _channelName = ""; internal IRtcEngine RtcEngine = null; internal Logger Log; private bool isAudio; private bool isVideo; //private static List list_UserInfo; private Dictionary dicPeeridAndUid; public Text LogText; private bool isRoom; private CustomPeer myPeer; public uint uid; public string userAccount; // private Dictionary list_ShowView; // public MenuIcon menuIcon; public bool isSwitchCamera; public string mainViewPeerId; public List listCustomPeer; public Texture bg; private bool isOneShow; // Use this for initialization public bool isVideoStream; private void Start() { LoadAssetData(); if (CheckAppId()) { InitEngine(); //SetChinnelName("0003003"); //JoinChannel(); //SetupUI(); } //list_UserInfo = new List(); // list_ShowView = new Dictionary(); dicPeeridAndUid = new Dictionary(); isRoom = false; isSwitchCamera = false; listCustomPeer = new List(); isOneShow = true; } // Update is called once per frame private void Update() { PermissionHelper.RequestMicrophontPermission(); PermissionHelper.RequestCameraPermission(); //if(isRoom&&CommonMethod.MyPeer!=null&&list_ShowView.Count>0) //{ // if(myPeer==null) // { // myPeer = (CustomPeer)StudioRtc.Instance.customPeerList.getPeerName(CommonMethod.MyPeer.PeerId); // Debug.Log(" Get MyCustomPeer"); // } // if (myPeer == null) return; // if (isAudio != myPeer.isAudio) // { // isAudio = myPeer.isAudio; // EnableLoacalAudio(isAudio); // } // if(isVideo!= myPeer.isVideo) // { // isVideo = myPeer.isVideo; // EnableLocalVideo(isVideo); // } //} } //Show data in AgoraBasicProfile [ContextMenu("ShowAgoraBasicProfileData")] private void LoadAssetData() { if (_appIdInput == null) return; _appID = _appIdInput.appID; _token = _appIdInput.token; _channelName = _appIdInput.channelName; } private bool CheckAppId() { Log = new Logger(LogText); return Log.DebugAssert(_appID.Length > 10, "Please fill in your appId in API-Example/profile/appIdInput.asset"); } private void InitEngine() { RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine(); AgoraVideoManagerHandler handler = new AgoraVideoManagerHandler(this); RtcEngineContext context = new RtcEngineContext(_appID, 0, CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_COMMUNICATION, AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT); // RtcEngine.SetCameraDeviceOrientation(VIDEO_SOURCE_TYPE.VIDEO_SOURCE_CAMERA, VIDEO_ORIENTATION.VIDEO_ORIENTATION_90); RtcEngine.Initialize(context); RtcEngine.InitEventHandler(handler); } public void RegisterLocalUserAccount(string peerId) { RtcEngine.RegisterLocalUserAccount(_appID, peerId); } public void SetChinnelName(string roomid) { _channelName = roomid; } public void JoinChannel( ) { Debug.Log(" JoinChannel " + _channelName); RtcEngine.EnableAudio(); RtcEngine.EnableVideo(); // RtcEngine.DisableAudio(); // RtcEngine.DisableVideo(); VideoEncoderConfiguration config = new VideoEncoderConfiguration(); config.dimensions = new VideoDimensions(720,1280); config.frameRate = 15; config.bitrate = 0; // config.orientationMode = ORIENTATION_MODE.ORIENTATION_MODE_ADAPTIVE; RtcEngine.SetVideoEncoderConfiguration(config); RtcEngine.SetChannelProfile(CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_COMMUNICATION); RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER); // _channelName = roomid; RtcEngine.SetLocalVideoMirrorMode(VIDEO_MIRROR_MODE_TYPE.VIDEO_MIRROR_MODE_ENABLED); RtcEngine.JoinChannel(_token, _channelName,"", uid); Debug.Log("uid "+uid); //RtcEngine.MuteLocalVideoStream(false); //RtcEngine.MuteLocalAudioStream(false); //RtcEngine.MuteAllRemoteAudioStreams(true); //RtcEngine.MuteAllRemoteVideoStreams(true); isRoom = true; if (!isSwitchCamera) { RtcEngine.SwitchCamera(); isSwitchCamera = !isSwitchCamera; } if (_channelName== "0003003") { StartCoroutine(CloseChannel(10f)); } // RtcEngine.SetCameraDeviceOrientation(VIDEO_SOURCE_TYPE.VIDEO_SOURCE_CAMERA, VIDEO_ORIENTATION.VIDEO_ORIENTATION_90); //RtcEngine.SetCameraDeviceOrientation(VIDEO_SOURCE_TYPE.VIDEO_SOURCE_CAMERA_PRIMARY, VIDEO_ORIENTATION.VIDEO_ORIENTATION_90); //RtcEngine.SetCameraDeviceOrientation(VIDEO_SOURCE_TYPE.VIDEO_SOURCE_CAMERA_SECONDARY, VIDEO_ORIENTATION.VIDEO_ORIENTATION_90); RtcEngine.SetLocalVideoMirrorMode(VIDEO_MIRROR_MODE_TYPE.VIDEO_MIRROR_MODE_DISABLED); StartCoroutine(SetSystem(0.3f)); } IEnumerator SetSystem(float times) { yield return new WaitForSeconds(times); EnableLocalVideo(CustomInfo.isSendVideo); EnableLoacalAudio(CustomInfo.isSendAudio); } public void OpenAgoraAudio() { Debug.Log(" 打开 OpenAgoraAudio "); // RtcEngine.SwitchCamera(); //RtcEngine.MuteAllRemoteAudioStreams(false); //RtcEngine.MuteAllRemoteVideoStreams(false); // RtcEngine.EnableAudio(); } public uint GetUID( string peerId) { if (dicPeeridAndUid.ContainsKey(peerId)) return dicPeeridAndUid[peerId]; return 0; } private IEnumerator CloseChannel( float times) { yield return new WaitForSeconds(times); LeaveChannel(); } public void LeaveChannel() { Debug.Log("LeaveChannel " ); int msg = RtcEngine.LeaveChannel(); switch (msg) { case 0: LogText.text = "成功退出频道: " + _channelName; break; default: LogText.text = "退出频道失败: " + msg; break; } isRoom = false; myPeer = null; dicPeeridAndUid.Clear(); dicPeeridAndUid = new Dictionary(); //list_ShowView.Clear(); //list_ShowView = new Dictionary(); AgoraVideoAudioManager.Instance.listCustomPeer.Clear(); AgoraVideoAudioManager.Instance.listCustomPeer = new List(); mainViewPeerId = ""; CloseAgoraMainImage(mainViewPeerId, false); } public void VuforiaLeaveChannel() { int msg = RtcEngine.LeaveChannel(); switch (msg) { case 0: LogText.text = "成功退出频道: " + _channelName; break; default: LogText.text = "退出频道失败: " + msg; break; } } public void AddPeeridUid(string peerid, uint uid) { Debug.Log(" AddPeeridUid " + peerid + " " + uid); if (dicPeeridAndUid.ContainsKey(peerid)) return; dicPeeridAndUid.Add(peerid, uid); } public void RemAtPeeridUid(string peerid) { if (!dicPeeridAndUid.ContainsKey(peerid)) return; Debug.Log(" RemAtPeeridUid " + peerid + " " + dicPeeridAndUid[peerid]); dicPeeridAndUid.Remove(peerid); CloseAgoraMainImage(peerid, false); for (int i = 0; i //{ // float scale = (float)height / (float)width; // videoSurface.transform.localScale = new Vector3(-5, 5 * scale, 1); // Debug.Log("OnTextureSizeModify: " + width + " " + height); //}; videoSurface.SetEnable(true); } //internal static void OnUserInfoUpdated(uint uid, Agora.Rtc.UserInfo info) //{ // Debug.Log(info.uid); // disUserPeer_Uid.Add("", info); //} internal static void OnUserJoined(uint uid) { // _videoSample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed)); // Debug.Log(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed)) //Agora.Rtc.UserInfo userInfo = new Agora.Rtc.UserInfo(); //AgoraVideoAudioManager.Instance.RtcEngine.GetUserInfoByUid(uid, ref userInfo); AgoraVideoAudioManager.Instance.UserJoined(uid); } internal static void OnRemoteVideoStateChanged(uint uid, REMOTE_VIDEO_STATE state, REMOTE_VIDEO_STATE_REASON reason) { AgoraVideoAudioManager.Instance.RemoteVideoStateChanged(uid, state, reason); } internal static void OnRemoteAudioStateChanged(uint uid, REMOTE_AUDIO_STATE state, REMOTE_AUDIO_STATE_REASON reason) { AgoraVideoAudioManager.Instance.RemoteAudioStateChanged(uid, state, reason); } #region -- Video Render UI Logic --- // VIDEO TYPE 1: 3D Object private static VideoSurface MakePlaneSurface(string goName) { var go = GameObject.CreatePrimitive(PrimitiveType.Quad); //for (int i = 0; i < list_UserInfo.Count; i++) //{ // if (list_UserInfo[i].uid.ToString() == goName) // { // string userAccount = list_UserInfo[i].userAccount; // if (AgoraVideoAudioManager.Instance.list_ShowView.ContainsKey(list_UserInfo[i].userAccount)) // { // go = AgoraVideoAudioManager.Instance.list_ShowView[list_UserInfo[i].userAccount].gameObject; // } // else // Debug.LogError(" Agora ShowView is NULL "); // } //} if (go == null) { go = GameObject.CreatePrimitive(PrimitiveType.Plane); return null; } go.name = goName; // set up transform go.transform.Rotate(-90.0f, 0.0f, 0.0f); var yPos = Random.Range(3.0f, 5.0f); var xPos = Random.Range(-2.0f, 2.0f); go.transform.position = Vector3.zero; go.transform.localScale = new Vector3(0.25f, 0.5f, 0.5f); // configure videoSurface var videoSurface = go.AddComponent(); return videoSurface; } // Video TYPE 2: RawImage private static VideoSurface MakeImageSurface(RawImage rawImage) { //GameObject go = rawImage.gameObject; //if (go == null) //{ // go = new GameObject(); // //go.name = goName; // // to be renderered onto // go.AddComponent(); // // set up transform // //go.transform.Rotate(0f, 0.0f, 180.0f); // //go.transform.localPosition = Vector3.zero; // //go.transform.localScale = new Vector3(2f, 3f, 1f); //} //if (go == null) //{ // return null; //} // make the object draggable if (rawImage.gameObject.GetComponent() != null) Destroy(rawImage.gameObject.GetComponent()); rawImage.gameObject.AddComponent(); //var canvas = GameObject.Find("VideoCanvas"); //if (canvas != null) //{ // go.transform.parent = canvas.transform; // Debug.Log("add video view"); //} //else //{ // Debug.Log("Canvas is null video view"); //} // configure videoSurface if (rawImage.gameObject.GetComponent() != null) Destroy(rawImage.gameObject.GetComponent()); var videoSurface = rawImage.gameObject.AddComponent(); return videoSurface; } internal static void DestroyVideoView(uint uid) { var go = GameObject.Find(uid.ToString()); if (!ReferenceEquals(go, null)) { Destroy(go); } } #endregion } #region -- Agora Event --- public class AgoraVideoManagerHandler : IRtcEngineEventHandler { private readonly AgoraVideoAudioManager _videoSample; internal AgoraVideoManagerHandler(AgoraVideoAudioManager videoSample) { _videoSample = videoSample; } public override void OnError(int err, string msg) { _videoSample.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg)); } public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed) { int build = 0; Debug.Log("Agora: OnJoinChannelSuccess "); _videoSample.Log.UpdateLog(string.Format("sdk version: ${0}", _videoSample.RtcEngine.GetVersion(ref build))); _videoSample.Log.UpdateLog(string.Format("sdk build: ${0}", build)); _videoSample.Log.UpdateLog( string.Format("OnJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}", connection.channelId, connection.localUid, elapsed)); // _videoSample.ClickSelf(); // AgoraVideoAudioManager.MakeVideoView(0); } public override void OnRejoinChannelSuccess(RtcConnection connection, int elapsed) { _videoSample.Log.UpdateLog("OnRejoinChannelSuccess"); } public override void OnLeaveChannel(RtcConnection connection, RtcStats stats) { _videoSample.Log.UpdateLog("OnLeaveChannel"); AgoraVideoAudioManager.DestroyVideoView(0); } public override void OnClientRoleChanged(RtcConnection connection, CLIENT_ROLE_TYPE oldRole, CLIENT_ROLE_TYPE newRole) { _videoSample.Log.UpdateLog("OnClientRoleChanged"); } public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed) { Debug.Log(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed)); _videoSample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed)); AgoraVideoAudioManager.OnUserJoined(uid); // AgoraVideoAudioManager.MakeVideoView(uid, _videoSample.GetChannelName()); } public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason) { _videoSample.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid, (int)reason)); AgoraVideoAudioManager.DestroyVideoView(uid); } //public override void OnUserInfoUpdated(uint uid, Agora.Rtc.UserInfo info) //{ // _videoSample.Log.UpdateLog(string.Format(" 用户 :${0} 加入房间", uid)); // AgoraVideoAudioManager.OnUserInfoUpdated(uid, info); //} public override void OnUplinkNetworkInfoUpdated(UplinkNetworkInfo info) { _videoSample.Log.UpdateLog("OnUplinkNetworkInfoUpdated"); } public override void OnDownlinkNetworkInfoUpdated(DownlinkNetworkInfo info) { _videoSample.Log.UpdateLog("OnDownlinkNetworkInfoUpdated"); } public override void OnRemoteVideoStateChanged(RtcConnection connection, uint remoteUid, REMOTE_VIDEO_STATE state, REMOTE_VIDEO_STATE_REASON reason, int elapsed) { AgoraVideoAudioManager.OnRemoteVideoStateChanged(remoteUid, state, reason); } public override void OnRemoteAudioStateChanged(RtcConnection connection, uint remoteUid, REMOTE_AUDIO_STATE state, REMOTE_AUDIO_STATE_REASON reason, int elapsed) { AgoraVideoAudioManager.OnRemoteAudioStateChanged(remoteUid, state, reason); } } #endregion