using System; using System.Linq; using UnityEngine; using UnityEngine.UI; using Agora.Rtc; using Agora.Util; using UnityEngine.Serialization; using Logger = Agora.Util.Logger; namespace Agora_RTC_Plugin.API_Example.Examples.Advanced.ScreenShareWhileVideoCall { public class ScreenShareWhileVideoCall : MonoBehaviour { [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 = ""; public Text LogText; internal Logger Log; internal IRtcEngineEx RtcEngine = null; public uint Uid1 = 123; public uint Uid2 = 456; private Dropdown _winIdSelect; private Button _startShareBtn; private Button _stopShareBtn; // Use this for initialization private void Start() { LoadAssetData(); if (CheckAppId()) { InitEngine(); #if UNITY_ANDROID || UNITY_IPHONE GameObject.Find("winIdSelect").SetActive(false); #else PrepareScreenCapture(); #endif EnableUI(); JoinChannel(); } } private bool CheckAppId() { Log = new Logger(LogText); return Log.DebugAssert(_appID.Length > 10, "Please fill in your appId in API-Example/profile/appIdInput.asset"); } //Show data in AgoraBasicProfile [ContextMenu("ShowAgoraBasicProfileData")] private void LoadAssetData() { if (_appIdInput == null) return; _appID = _appIdInput.appID; _token = _appIdInput.token; _channelName = _appIdInput.channelName; } private void JoinChannel() { RtcEngine.EnableAudio(); RtcEngine.EnableVideo(); RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER); ChannelMediaOptions options = new ChannelMediaOptions(); options.autoSubscribeAudio.SetValue(true); options.autoSubscribeVideo.SetValue(true); options.publishCameraTrack.SetValue(true); options.publishScreenTrack.SetValue(false); options.enableAudioRecordingOrPlayout.SetValue(true); options.clientRoleType.SetValue(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER); RtcEngine.JoinChannel(_token, _channelName, this.Uid1, options); } private void ScreenShareJoinChannel() { ChannelMediaOptions options = new ChannelMediaOptions(); options.autoSubscribeAudio.SetValue(false); options.autoSubscribeVideo.SetValue(false); options.publishCameraTrack.SetValue(false); options.publishScreenTrack.SetValue(true); options.enableAudioRecordingOrPlayout.SetValue(false); #if UNITY_ANDROID || UNITY_IPHONE options.publishScreenCaptureAudio.SetValue(true); options.publishScreenCaptureVideo.SetValue(true); #endif options.clientRoleType.SetValue(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER); var ret = RtcEngine.JoinChannelEx(_token, new RtcConnection(_channelName, this.Uid2), options); Debug.Log("JoinChannelEx returns: " + ret); } private void ScreenShareLeaveChannel() { RtcEngine.LeaveChannelEx(new RtcConnection(_channelName, Uid2)); } private void UpdateChannelMediaOptions() { ChannelMediaOptions options = new ChannelMediaOptions(); options.autoSubscribeAudio.SetValue(false); options.autoSubscribeVideo.SetValue(false); options.publishCameraTrack.SetValue(false); options.publishScreenTrack.SetValue(true); #if UNITY_ANDROID || UNITY_IPHONE options.publishScreenCaptureAudio.SetValue(true); options.publishScreenCaptureVideo.SetValue(true); #endif options.clientRoleType.SetValue(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER); var ret = RtcEngine.UpdateChannelMediaOptions(options); Debug.Log("UpdateChannelMediaOptions returns: " + ret); } private void InitEngine() { RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngineEx(); UserEventHandler handler = new UserEventHandler(this); RtcEngineContext context = new RtcEngineContext(_appID, 0, CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING, AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT); RtcEngine.Initialize(context); RtcEngine.InitEventHandler(new UserEventHandler(this)); } private void PrepareScreenCapture() { _winIdSelect = GameObject.Find("winIdSelect").GetComponent(); if (_winIdSelect == null || RtcEngine == null) return; _winIdSelect.ClearOptions(); SIZE t = new SIZE(); t.width = 360; t.height = 240; SIZE s = new SIZE(); s.width = 360; s.height = 240; var info = RtcEngine.GetScreenCaptureSources(t, s, true); _winIdSelect.AddOptions(info.Select(w => new Dropdown.OptionData( string.Format("{0}: {1}-{2} | {3}", w.type, w.sourceName, w.sourceTitle, w.sourceId))) .ToList()); } private void EnableUI() { _startShareBtn = GameObject.Find("startShareBtn").GetComponent