|
@@ -1,538 +0,0 @@
|
|
|
-using Agora.Rtc;
|
|
|
-using Agora.Util;
|
|
|
-using SC.XR.Unity;
|
|
|
-using System.Collections;
|
|
|
-using System.Collections.Generic;
|
|
|
-using UnityEngine;
|
|
|
-using UnityEngine.Rendering;
|
|
|
-using UnityEngine.UI;
|
|
|
-
|
|
|
-public class RTCManager : SingletonMono<RTCManager>
|
|
|
-{
|
|
|
-
|
|
|
- public Camera ca;
|
|
|
- public RawImage img;
|
|
|
- Texture2D screenShot;
|
|
|
- byte[] bts;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- private uint uid;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- private string channelName;
|
|
|
-
|
|
|
- [SerializeField]
|
|
|
- private string _appID = "";
|
|
|
-
|
|
|
- [SerializeField]
|
|
|
- private string _token = "";
|
|
|
-
|
|
|
- internal IRtcEngine RtcEngine = null;
|
|
|
-
|
|
|
- private Dictionary<string, uint> dicPeeridAndUid;
|
|
|
- public List<CustomPeer> listCustomPeer;
|
|
|
- private string mainViewPeerId;
|
|
|
-
|
|
|
- private bool isSwitchCamera;
|
|
|
-
|
|
|
- public delegate void OnInitDelegate();
|
|
|
- public delegate void OnJoinChannelDelegate();
|
|
|
- public delegate void OnLeaveChannelDelegate();
|
|
|
- public delegate void OnShowViewRawImageDelegate(string uid, RawImage rawImage);
|
|
|
- public delegate void OnShowViewMeshRendererDelegate(string uid, MeshRenderer mesh);
|
|
|
- public delegate void OnShowOneViewRawImageDelegate(RawImage rawImage);
|
|
|
- public delegate void OnMuteLocalAudioStreamDelegate(bool isAudio);
|
|
|
- public delegate void OnMuteLocalVideoStreamDelegate(bool isVideo);
|
|
|
- public delegate void OnMuteRemoteAudioStreamDelegate(string uid, bool isAudio);
|
|
|
- public delegate void OnMuteRemoteVideoStreamDelegate(string uid, bool isVideo);
|
|
|
- public delegate void OnRemoteAudioStateChangedDelegate(string uid,REMOTE_AUDIO_STATE_RTC state, REMOTE_AUIDO_STATE_REASON_RTC reason);
|
|
|
- public delegate void OnRemoteVideoStateChangedDelegate(string uid,REMOTE_VIDEO_STATE_RTC state, REMOTE_VIDEO_STATE_REASON_RTC reason);
|
|
|
- public delegate void OnUserJoinedDelegate(string uid);
|
|
|
- public delegate void OnUserOfflineDelegate(string uid);
|
|
|
- public delegate void OnSetUidDelegate(string uid);
|
|
|
- public delegate void OnSetChannelNameDelegate(string channelName);
|
|
|
- public delegate void OnAddPeeridUidDelegate(string peerid, string uid);
|
|
|
- public delegate void OnRemAtPeeridUidDelegate(string peerid);
|
|
|
-
|
|
|
- public OnInitDelegate OnInit;
|
|
|
- public OnJoinChannelDelegate OnJoinChannel;
|
|
|
- public OnLeaveChannelDelegate OnLeaveChannel;
|
|
|
- public OnShowViewRawImageDelegate OnShowViewRawImage;
|
|
|
- public OnShowViewMeshRendererDelegate OnShowViewMeshRenderer;
|
|
|
- public OnShowOneViewRawImageDelegate OnShowOneViewRawImage;
|
|
|
- public OnMuteLocalAudioStreamDelegate OnMuteLocalAudioStream;
|
|
|
- public OnMuteLocalVideoStreamDelegate OnMuteLocalVideoStream;
|
|
|
- public OnMuteRemoteAudioStreamDelegate OnMuteRemoteAudioStream;
|
|
|
- public OnMuteRemoteVideoStreamDelegate OnMuteRemoteVideoStream;
|
|
|
- public OnRemoteAudioStateChangedDelegate OnRemoteAudioStateChanged;
|
|
|
- public OnRemoteVideoStateChangedDelegate OnRemoteVideoStateChanged;
|
|
|
- public OnUserJoinedDelegate OnUserJoined;
|
|
|
- public OnUserOfflineDelegate OnUserOffline;
|
|
|
- public OnSetUidDelegate OnSetUid;
|
|
|
- public OnSetChannelNameDelegate OnSetChannelName;
|
|
|
- public OnAddPeeridUidDelegate OnAddPeeridUid;
|
|
|
- public OnRemAtPeeridUidDelegate OnRemAtPeeridUid;
|
|
|
-
|
|
|
- private void Update()
|
|
|
- {
|
|
|
- PermissionHelper.RequestMicrophontPermission();
|
|
|
- PermissionHelper.RequestCameraPermission();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public void Init()
|
|
|
- {
|
|
|
-
|
|
|
- isSwitchCamera = false;
|
|
|
- dicPeeridAndUid = new Dictionary<string, uint>();
|
|
|
- listCustomPeer = new List<CustomPeer>();
|
|
|
- InitEngine();
|
|
|
- }
|
|
|
-
|
|
|
- private void InitEngine()
|
|
|
- {
|
|
|
-
|
|
|
- RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
|
|
|
-
|
|
|
- RTCManagerHandler handler = new RTCManagerHandler(this);
|
|
|
- RtcEngineContext context = new RtcEngineContext(_appID, 0,
|
|
|
- CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_COMMUNICATION,
|
|
|
- AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT);
|
|
|
-
|
|
|
- RtcEngine.Initialize(context);
|
|
|
- RtcEngine.InitEventHandler(handler);
|
|
|
- }
|
|
|
-
|
|
|
- public void SetUid(string uid)
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
- this.uid = StringToUint(uid);
|
|
|
- }
|
|
|
-
|
|
|
- public void SetChannelName(string channelName)
|
|
|
- {
|
|
|
-
|
|
|
- this.channelName = channelName;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public void JoinChannel()
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
- RtcEngine.EnableAudio();
|
|
|
- RtcEngine.EnableVideo();
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- VideoEncoderConfiguration config = new VideoEncoderConfiguration();
|
|
|
- config.dimensions = new VideoDimensions(1280, 720);
|
|
|
- config.frameRate = 15;
|
|
|
- config.bitrate = 0;
|
|
|
-
|
|
|
- RtcEngine.SetVideoEncoderConfiguration(config);
|
|
|
- RtcEngine.SetChannelProfile(CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_COMMUNICATION);
|
|
|
- RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
|
|
|
-
|
|
|
- RtcEngine.SetExternalVideoSource(true, true, EXTERNAL_VIDEO_SOURCE_TYPE.VIDEO_FRAME, new SenderOptions());
|
|
|
-
|
|
|
- if (DeviceType.type == "DreamGlass")
|
|
|
- RtcEngine.SetAudioProfile(0, AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_GAME_STREAMING);
|
|
|
-
|
|
|
- RtcEngine.JoinChannel(_token, channelName, "", uid);
|
|
|
- Debug.Log("uid " + uid);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- StartCoroutine(RenderTexturesScreenCapture());
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- IEnumerator RenderTexturesScreenCapture()
|
|
|
- {
|
|
|
-
|
|
|
- Debug.Log("RenderTexturesScreenCapture发送图片1");
|
|
|
- yield return new WaitForEndOfFrame();
|
|
|
- if (screenShot == null)
|
|
|
- {
|
|
|
- screenShot = new Texture2D(1280, 720, TextureFormat.RGBA32, false);
|
|
|
- StartCoroutine(GetRenederFPS());
|
|
|
- }
|
|
|
- while (true)
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- yield return new WaitForSeconds(0.05f);
|
|
|
- if (bts != null)
|
|
|
- {
|
|
|
-
|
|
|
- var timetick = System.DateTime.Now.Ticks / 10000;
|
|
|
- ExternalVideoFrame externalVideoFrame = new ExternalVideoFrame();
|
|
|
- externalVideoFrame.type = VIDEO_BUFFER_TYPE.VIDEO_BUFFER_RAW_DATA;
|
|
|
- externalVideoFrame.format = VIDEO_PIXEL_FORMAT.VIDEO_PIXEL_RGBA;
|
|
|
- externalVideoFrame.buffer = bts;
|
|
|
- externalVideoFrame.stride = (int)screenShot.width;
|
|
|
- externalVideoFrame.height = (int)screenShot.height;
|
|
|
- externalVideoFrame.rotation = 180;
|
|
|
- externalVideoFrame.cropLeft = 1;
|
|
|
- externalVideoFrame.cropRight = 1;
|
|
|
- externalVideoFrame.timestamp = timetick;
|
|
|
- RtcEngine.PushVideoFrame(externalVideoFrame);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- IEnumerator GetRenederFPS()
|
|
|
- {
|
|
|
- while (true)
|
|
|
- {
|
|
|
- var req = AsyncGPUReadback.Request(RemoteRtc.Instance.cam2.activeTexture);
|
|
|
- yield return new WaitUntil(() => req.done);
|
|
|
- if (!req.hasError)
|
|
|
- {
|
|
|
-
|
|
|
- if (bts == null)
|
|
|
- {
|
|
|
- bts = new byte[req.layerDataSize];
|
|
|
- }
|
|
|
- req.GetData<byte>().CopyTo(bts);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Debug.LogError("Error AsyncGPUReadbackRequest.hasError");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public void LeaveChannel()
|
|
|
- {
|
|
|
-
|
|
|
- int msg = RtcEngine.LeaveChannel();
|
|
|
- switch (msg)
|
|
|
- {
|
|
|
- case 0:
|
|
|
- Debug.Log( "成功退出频道: " + channelName);
|
|
|
- break;
|
|
|
- default:
|
|
|
- Debug.Log("退出频道失败: " + msg);
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public void ShowViewRawImage( string peerId, RawImage rawImage)
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
- MakeVideoView(dicPeeridAndUid[peerId], rawImage, this.channelName);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public void ShowViewMeshRenderer(string uid, MeshRenderer mesh)
|
|
|
- {
|
|
|
- OnShowViewMeshRenderer(uid, mesh);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public void MuteLocalAudioStream(bool isAudio)
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
- int msg = RtcEngine.MuteLocalAudioStream(!isAudio);
|
|
|
-
|
|
|
-
|
|
|
- switch (msg)
|
|
|
- {
|
|
|
- case 0:
|
|
|
- Debug.Log(isAudio ? "打开本地音频成功" : "关闭本地音频成功 ");
|
|
|
- break;
|
|
|
- default:
|
|
|
- Debug.LogError("开关本地音频失败: " + msg);
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public void MuteLocalVideoStream(bool isVideo)
|
|
|
- {
|
|
|
-
|
|
|
- int msg = RtcEngine.MuteLocalVideoStream(!isVideo);
|
|
|
- switch (msg)
|
|
|
- {
|
|
|
- case 0:
|
|
|
- Debug.Log(isVideo ? "打开本地视频成功 " : "关闭本地视频成功 ");
|
|
|
- break;
|
|
|
- default:
|
|
|
- Debug.LogError("开关本地视频失败: " + msg);
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public void MuteRemoteAudioStream(string peerid, bool isAudio)
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
- int msg = RtcEngine.MuteRemoteAudioStream(StringToUint(peerid), !isAudio);
|
|
|
-
|
|
|
- switch (msg)
|
|
|
- {
|
|
|
- case 0:
|
|
|
- Debug.Log(isAudio ? "订阅远端音频成功" : "取订远端音频成功 ");
|
|
|
- break;
|
|
|
- default:
|
|
|
- Debug.LogError("远端音频失败: " + msg);
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public void MuteRemoteVideoStream(string peerid, bool isVideo)
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
- int msg = RtcEngine.MuteRemoteVideoStream(StringToUint(peerid), !isVideo);
|
|
|
- switch (msg)
|
|
|
- {
|
|
|
- case 0:
|
|
|
- Debug.Log(isVideo ? "订阅远端视频成功" : "取订远端视频成功 ");
|
|
|
- break;
|
|
|
- default:
|
|
|
- Debug.LogError("远端视频失败: " + msg);
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public void RemoteAudioStateChanged(string uid ,REMOTE_AUDIO_STATE state, REMOTE_AUDIO_STATE_REASON reason)
|
|
|
- {
|
|
|
- OnRemoteAudioStateChanged(uid, (REMOTE_AUDIO_STATE_RTC)state, (REMOTE_AUIDO_STATE_REASON_RTC)reason);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public void RemoteVideoStateChanged(string uid ,REMOTE_AUDIO_STATE state ,REMOTE_AUDIO_STATE_REASON reason)
|
|
|
- {
|
|
|
- OnRemoteVideoStateChanged(uid, (REMOTE_VIDEO_STATE_RTC)state, (REMOTE_VIDEO_STATE_REASON_RTC)reason);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public void UserJoined(string uid)
|
|
|
- {
|
|
|
- OnUserJoined(uid);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public void UserOffline(string uid)
|
|
|
- {
|
|
|
- OnUserOffline(uid);
|
|
|
- }
|
|
|
-
|
|
|
- private uint StringToUint(string value)
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
- return (uint)int.Parse(value);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- internal static void MakeVideoView(uint uid, RawImage rawImage, string channelId = "")
|
|
|
- {
|
|
|
- Debug.Log("MakeVideoView " + uid);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- var videoSurface = MakeImageSurface(rawImage);
|
|
|
-
|
|
|
- if (ReferenceEquals(videoSurface, null)) return;
|
|
|
-
|
|
|
- if (uid == 0)
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- videoSurface.SetForUser(uid, channelId, VIDEO_SOURCE_TYPE.VIDEO_SOURCE_REMOTE);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- videoSurface.SetEnable(true);
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private static VideoSurface MakeImageSurface(RawImage rawImage)
|
|
|
- {
|
|
|
-
|
|
|
- if (rawImage.gameObject.GetComponent<UIElementDrag>() != null)
|
|
|
- Destroy(rawImage.gameObject.GetComponent<UIElementDrag>());
|
|
|
- rawImage.gameObject.AddComponent<UIElementDrag>();
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- if (rawImage.gameObject.GetComponent<VideoSurface>() != null)
|
|
|
- Destroy(rawImage.gameObject.GetComponent<VideoSurface>());
|
|
|
- var videoSurface = rawImage.gameObject.AddComponent<VideoSurface>();
|
|
|
-
|
|
|
- return videoSurface;
|
|
|
- }
|
|
|
-}
|
|
|
-public class RTCManagerHandler : IRtcEngineEventHandler
|
|
|
-{
|
|
|
- private readonly RTCManager _videoSample;
|
|
|
-
|
|
|
- internal RTCManagerHandler(RTCManager videoSample)
|
|
|
- {
|
|
|
- _videoSample = videoSample;
|
|
|
- }
|
|
|
-
|
|
|
- public override void OnError(int err, string msg)
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
|
|
|
- {
|
|
|
- int build = 0;
|
|
|
- Debug.Log("Agora: OnJoinChannelSuccess ");
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
|
|
|
- {
|
|
|
-
|
|
|
- AgoraVideoAudioManager.DestroyVideoView(0);
|
|
|
- }
|
|
|
-
|
|
|
- public override void OnClientRoleChanged(RtcConnection connection, CLIENT_ROLE_TYPE oldRole, CLIENT_ROLE_TYPE newRole)
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
|
|
|
- {
|
|
|
- Debug.Log(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
|
|
|
-
|
|
|
- AgoraVideoAudioManager.OnUserJoined(uid);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public override void OnUserInfoUpdated(uint uid, Agora.Rtc.UserInfo info)
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
- AgoraVideoAudioManager.OnUserInfoUpdated(uid, info);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public override void OnUplinkNetworkInfoUpdated(UplinkNetworkInfo info)
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public override void OnDownlinkNetworkInfoUpdated(DownlinkNetworkInfo info)
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- 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);
|
|
|
- }
|
|
|
-}
|