123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900 |
- 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<AgoraVideoAudioManager>
- {
- [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<Agora.Rtc.UserInfo> list_UserInfo;
- private Dictionary<string, uint> dicPeeridAndUid;
- public Text LogText;
- private bool isRoom;
- private CustomPeer myPeer;
- public uint uid;
- public string userAccount;
- // private Dictionary<string, RawImage> list_ShowView;
-
- // public MenuIcon menuIcon;
- public bool isSwitchCamera;
- public string mainViewPeerId;
- public List<CustomPeer> listCustomPeer;
- public Texture bg;
- private bool isOneShow;
- // Use this for initialization
- private void Start()
- {
- LoadAssetData();
- if (CheckAppId())
- {
- InitEngine();
- //SetChinnelName("0003003");
- //JoinChannel();
- //SetupUI();
- }
- //list_UserInfo = new List<Agora.Rtc.UserInfo>();
- // list_ShowView = new Dictionary<string, RawImage>();
- dicPeeridAndUid = new Dictionary<string, uint>();
- isRoom = false;
- isSwitchCamera = false;
- listCustomPeer = new List<CustomPeer>();
- 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);
- }
- 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<string, uint>();
- //list_ShowView.Clear();
- //list_ShowView = new Dictionary<string, RawImage>();
- AgoraVideoAudioManager.Instance.listCustomPeer.Clear();
- AgoraVideoAudioManager.Instance.listCustomPeer = new List<CustomPeer>();
- 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 <listCustomPeer.Count; i++)
- {
- if (listCustomPeer[i].peerId == peerid)
- listCustomPeer.RemoveAt(i);
- }
- }
- private void CloseAgoraMainImage(string peerid, bool isOpen)
- {
- if (peerid == mainViewPeerId)
- {
- if(isOpen)
- {
- AddListShowView(mainViewPeerId, RoomMainForms.Instance.bigView);
- }
- else
- {
- StartCoroutine(CloseBG(0.05f));
- }
-
- }
- }
- IEnumerator CloseBG(float times)
- {
- yield return new WaitForSeconds(times);
- RoomMainForms.Instance.bigView.rectTransform.localEulerAngles = new Vector3(0, 0, 0);
- RoomMainForms.Instance.bigView.texture = bg;
- }
- public void ShowLocalView(RawImage rawImage)
- {
- rawImage.gameObject.SetActive(true);
- rawImage.rectTransform.localEulerAngles = new Vector3(0, 180, 180);
-
- MakeVideoView(0, rawImage);
- }
- public void AddListShowView( string peerId, RawImage rawImage)
- {
-
- if (!dicPeeridAndUid.ContainsKey(peerId))
- return;
-
-
-
- for (int i = 0; i < listCustomPeer.Count; i++)
- {
- if(listCustomPeer[i].peerId == peerId )
- {
- if( listCustomPeer[i].isVideo)
- {
- Debug.Log(listCustomPeer[i].isVideo + " " + peerId);
- rawImage.rectTransform.localEulerAngles = new Vector3(0, 180, 180);
- MakeVideoView(dicPeeridAndUid[peerId], rawImage, this._channelName);
-
- }
- else
- {
- rawImage.texture = bg;
- rawImage.rectTransform.localEulerAngles = new Vector3(0, 0, 0);
- }
- break;
- }
-
- }
- mainViewPeerId = peerId;
- }
- public void ShowOneView(RawImage rawImage)
- {
- if (dicPeeridAndUid.Count < 2)
- return;
- if (!isOneShow)
- return;
- isOneShow = true;
- rawImage.gameObject.SetActive(true);
- rawImage.texture = null;
- rawImage.rectTransform.localEulerAngles = new Vector3(0, 180, 180);
-
- MakeVideoView(dicPeeridAndUid.Values.Skip(1).First(), rawImage, this._channelName);
- }
- public void UserJoined(uint uid)
- {
- if (uid == dicPeeridAndUid.Values.Skip(1).First())
- {
- RoomMainForms.Instance.bigView.rectTransform.localEulerAngles = new Vector3(0, 0, 0);
- RoomMainForms.Instance.bigView.gameObject.SetActive(true);
- MakeVideoView(dicPeeridAndUid.Values.Skip(1).First(), RoomMainForms.Instance.bigView, this._channelName);
- foreach (var item in dicPeeridAndUid)
- {
- if (item.Value == uid)
- mainViewPeerId = item.Key;
- }
- }
- }
- public void SwitchCamera(bool state)
- {
- int msg = RtcEngine.SwitchCamera();
- switch (msg)
- {
- case 0:
- Debug.Log("切换摄像头成功");
- break;
- default:
- Debug.LogError("切换摄像头失败: " + msg);
- break;
- }
- }
- public void EnableLoacalAudio( bool isAudio)
- {
- int msg = RtcEngine.MuteLocalAudioStream(!isAudio);
- // int msg = RtcEngine.EnableLocalAudio(isAudio);
- switch (msg)
- {
- case 0:
- Debug.Log( isAudio ? "打开本地音频成功" : "关闭本地音频成功 ");
- break;
- default:
- Debug.LogError("开关本地音频失败: " + msg);
- break;
- }
- }
- public void MuteRemoteAudioStream( string peerid , bool isAudio)
- {
- if (!dicPeeridAndUid.ContainsKey(peerid))
- return;
- int msg = RtcEngine.MuteRemoteAudioStream(dicPeeridAndUid[peerid], isAudio);
- switch (msg)
- {
- case 0:
- Debug.Log( !isAudio ? "订阅远端音频成功" : "取订远端音频成功 ");
- break;
- default:
- Debug.LogError( "开关本地音频失败: " + msg);
- break;
- }
- }
- public void MuteRemoteVideoStream(string peerid , bool isVideo)
- {
- if (!dicPeeridAndUid.ContainsKey(peerid))
- return;
- int msg = RtcEngine.MuteRemoteVideoStream(dicPeeridAndUid[peerid], isVideo);
- switch (msg)
- {
- case 0:
- Debug.Log(!isVideo ? "订阅远端视频成功" : "取订远端视频成功 ");
- break;
- default:
- Debug.LogError("开关本地视频失败: " + msg);
- break;
- }
-
- // CloseAgoraMainImage(peerid, !isVideo);
- }
- public void EnableLocalVideo( bool isVideo)
- {
- int msg = RtcEngine.MuteLocalVideoStream(!isVideo);
- //RtcEngine.EnableVideo();
- // int msg = RtcEngine.EnableLocalVideo(isVideo);
- switch (msg)
- {
- case 0:
- Debug.Log( isVideo ? "打开本地视频成功 " : "关闭本地视频成功 ");
- break;
- default:
- Debug.LogError( "开关本地视频失败: " + msg);
- break;
- }
- if (!isVideo)
- RoomMainForms.Instance.selfVideo.texture = null;
- else
- ShowLocalView( RoomMainForms.Instance.selfVideo);
- }
- public void OnClickEnableLocalVideo( bool isVideo)
- {
- int msg = RtcEngine.EnableLocalVideo(isVideo);
- switch (msg)
- {
- case 0:
- LogText.text = isVideo ? "打开本地视频成功 " : "关闭本地视频成功 ";
- break;
- default:
- LogText.text = "开关本地视频失败: " + msg;
- break;
- }
- }
- public void RemoteVideoStateChanged(uint uid, REMOTE_VIDEO_STATE state, REMOTE_VIDEO_STATE_REASON
- reason)
- {
- Debug.Log("RemoteVideoStateChanged " + uid);
- if (!dicPeeridAndUid.ContainsValue(uid))
- return;
- for (int i = 0; i < listCustomPeer.Count; i++)
- {
- if (dicPeeridAndUid[listCustomPeer[i].peerId] == uid)
- {
- switch (reason)
- {
- case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_INTERNAL:
- break;
- case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_NETWORK_CONGESTION://网络阻塞。
- // listCustomPeer[i].isVideo = false;
- break;
- case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_NETWORK_RECOVERY:// 网络恢复正常。
- // listCustomPeer[i].isVideo = true;
- break;
- case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_LOCAL_MUTED://本地用户停止接收远端视频流或本地用户禁用视频模块
- CloseAgoraMainImage(listCustomPeer[i].peerId, false);
- // listCustomPeer[i].isCloseVideo = false;
- break;
- case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_LOCAL_UNMUTED://本地用户恢复接收远端视频流或本地用户启动视频模块
- CloseAgoraMainImage(listCustomPeer[i].peerId, true);
- // listCustomPeer[i].isCloseVideo = true;
- break;
- case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_REMOTE_MUTED://远端用户停止发送视频流或远端用户禁用视频模块。
- listCustomPeer[i].isVideo = false;
- CloseAgoraMainImage(listCustomPeer[i].peerId, false);
- break;
- case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_REMOTE_UNMUTED://远端用户恢复发送视频流或远端用户启用视频模块。
- listCustomPeer[i].isVideo = true;
- CloseAgoraMainImage(listCustomPeer[i].peerId, true);
- break;
- case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_REMOTE_OFFLINE: //远端用户离开频道。
- listCustomPeer[i].isVideo = false;
- break;
- case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_AUDIO_FALLBACK:
- break;
- case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_AUDIO_FALLBACK_RECOVERY:
- break;
- case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_VIDEO_STREAM_TYPE_CHANGE_TO_LOW:
- break;
- case REMOTE_VIDEO_STATE_REASON.REMOTE_VIDEO_STATE_REASON_VIDEO_STREAM_TYPE_CHANGE_TO_HIGH:
- break;
- default:
- break;
- }
- switch (state)
- {
- case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_STOPPED:
- break;
- case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_STARTING:
- break;
- case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_DECODING:
- RoomMainForms.Instance.bigView.gameObject.SetActive(true);
- // RoomMainForms.Instance.bigView.rectTransform.localEulerAngles = new Vector3(0, 180, 180);
- break;
- case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_FROZEN:
- break;
- case REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_FAILED:
- RoomMainForms.Instance.bigView.rectTransform.localEulerAngles = new Vector3(0, 0, 0);
- RoomMainForms.Instance.bigView.texture = bg;
- break;
- default:
- break;
- }
- break;
- }
- }
-
- }
- public void RemoteAudioStateChanged(uint uid, REMOTE_AUDIO_STATE state, REMOTE_AUDIO_STATE_REASON reason)
- {
- Debug.Log("RemoteAudioStateChanged " + uid);
- if (!dicPeeridAndUid.ContainsValue(uid))
- return;
- for (int i = 0; i < listCustomPeer.Count; i++)
- {
- if (dicPeeridAndUid[listCustomPeer[i].peerId] == uid)
- {
- switch (reason)
- {
- case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_INTERNAL:
- break;
- case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_NETWORK_CONGESTION:
- // listCustomPeer[i].isAudio = false;
- break;
- case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_NETWORK_RECOVERY:
- // listCustomPeer[i].isAudio = true;
- break;
- case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_LOCAL_MUTED:
- listCustomPeer[i].isCloseAudio = true;
- break;
- case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_LOCAL_UNMUTED:
- listCustomPeer[i].isCloseAudio = false;
- break;
- case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_REMOTE_MUTED:
- listCustomPeer[i].isAudio = false;
- break;
- case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_REMOTE_UNMUTED:
- listCustomPeer[i].isAudio = true;
- break;
- case REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_REMOTE_OFFLINE:
- listCustomPeer[i].isAudio = false;
- break;
- default:
- break;
- }
- break;
- }
- }
- }
- private void StopPublish()
- {
- var options = new ChannelMediaOptions();
- options.publishMicrophoneTrack.SetValue(false);
- options.publishCameraTrack.SetValue(false);
- var nRet = RtcEngine.UpdateChannelMediaOptions(options);
- this.Log.UpdateLog("UpdateChannelMediaOptions: " + nRet);
- }
- private void StartPublish()
- {
- var options = new ChannelMediaOptions();
- options.publishMicrophoneTrack.SetValue(true);
- options.publishCameraTrack.SetValue(true);
- var nRet = RtcEngine.UpdateChannelMediaOptions(options);
- this.Log.UpdateLog("UpdateChannelMediaOptions: " + nRet);
- }
- private void OnDestroy()
- {
- Debug.Log("OnDestroy");
- if (RtcEngine == null) return;
- RtcEngine.InitEventHandler(null);
- RtcEngine.LeaveChannel();
- RtcEngine.Dispose();
- }
-
- internal string GetChannelName()
- {
- return _channelName;
- }
- internal static void MakeVideoView(uint uid, RawImage rawImage, string channelId = "")
- {
- Debug.Log("MakeVideoView " + uid);
- //var go = GameObject.Find(uid.ToString());
- //if (!ReferenceEquals(go, null))
- //{
- // return; // reuse
- //}
- // create a GameObject and assign to this new user
- var videoSurface = MakeImageSurface(rawImage);
- // var videoSurface = MakePlaneSurface(uid.ToString());
- if (ReferenceEquals(videoSurface, null)) return;
- // configure videoSurface
- if (uid == 0)
- {
- videoSurface.SetForUser(uid, channelId);
- }
- else
- {
-
- videoSurface.SetForUser(uid, channelId, VIDEO_SOURCE_TYPE.VIDEO_SOURCE_REMOTE);
- }
-
- //videoSurface.OnTextureSizeModify += (int width, int height) =>
- //{
- // 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<VideoSurface>();
- 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<RawImage>();
- // // 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<UIElementDrag>() != null)
- Destroy(rawImage.gameObject.GetComponent<UIElementDrag>());
- rawImage.gameObject.AddComponent<UIElementDrag>();
- //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<VideoSurface>() != null)
- Destroy(rawImage.gameObject.GetComponent<VideoSurface>());
- var videoSurface = rawImage.gameObject.AddComponent<VideoSurface>();
- 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
|