123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Serialization;
- using Agora.Rtc;
- using Agora.Util;
- using Logger = Agora.Util.Logger;
- namespace Agora_RTC_Plugin.API_Example.Examples.Advanced.ProcessVideoRawData
- {
- public class ProcessVideoRawData : 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 IRtcEngine RtcEngine;
- internal int Count;
- internal int WriteCount;
- internal int ReadCount;
- internal byte[] VideoBuffer = new byte[0];
- private int _videoFrameWidth = 1080;
- public int VideoFrameWidth
- {
- set
- {
- if (value != _videoFrameWidth)
- {
- _videoFrameWidth = value;
- _needResize = true;
- }
- }
-
- get
- {
- return _videoFrameWidth;
- }
- }
- private int _videoFrameHeight = 720;
- public int VideoFrameHeight
- {
- set
- {
- if (value != _videoFrameHeight)
- {
- _videoFrameHeight = value;
- _needResize = true;
- }
- }
- get
- {
- return _videoFrameHeight;
- }
- }
- private bool _needResize = false;
- public GameObject VideoView;
- private Texture2D _texture;
- private bool _isTextureAttach = false;
- void Start()
- {
- LoadAssetData();
- if (CheckAppId())
- {
- SetUpUI();
- InitEngine();
- JoinChannel();
- }
- }
- // Update is called once per frame
- void Update()
- {
- PermissionHelper.RequestMicrophontPermission();
- PermissionHelper.RequestCameraPermission();
- if (!_isTextureAttach)
- {
- var rd = VideoView.GetComponent<RawImage>();
- rd.texture = _texture;
- _isTextureAttach = true;
- }
- else if (VideoBuffer != null && VideoBuffer.Length != 0 && !_needResize)
- {
- lock (VideoBuffer)
- {
- _texture.LoadRawTextureData(VideoBuffer);
- _texture.Apply();
- }
- }
- else if(_needResize)
- {
- _texture.Reinitialize(_videoFrameWidth, _videoFrameHeight);
- _texture.Apply();
- _needResize = false;
- }
- }
- 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")]
- public void LoadAssetData()
- {
- if (_appIdInput == null) return;
- _appID = _appIdInput.appID;
- _token = _appIdInput.token;
- _channelName = _appIdInput.channelName;
- }
- void SetUpUI()
- {
- var bufferLength = _videoFrameHeight * _videoFrameWidth * 4;
- _texture = new Texture2D(_videoFrameWidth, _videoFrameHeight, TextureFormat.RGBA32, false);
- _texture.Apply();
- }
- void InitEngine()
- {
- RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
- 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(handler);
- RtcEngine.RegisterVideoFrameObserver(new VideoFrameObserver(this), OBSERVER_MODE.RAW_DATA);
- }
- void JoinChannel()
- {
- VideoEncoderConfiguration config = new VideoEncoderConfiguration();
- config.dimensions = new VideoDimensions(_videoFrameWidth, _videoFrameHeight);
- RtcEngine.SetVideoEncoderConfiguration(config);
- RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
- RtcEngine.EnableAudio();
- RtcEngine.EnableVideo();
- RtcEngine.JoinChannel(_token, _channelName);
- }
- private void OnDestroy()
- {
- Debug.Log("OnDestroy");
- if (RtcEngine != null)
- {
- if (_texture != null)
- {
- GameObject.Destroy(_texture);
- _texture = null;
- }
- RtcEngine.UnRegisterVideoFrameObserver();
- RtcEngine.InitEventHandler(null);
- RtcEngine.LeaveChannel();
- RtcEngine.Dispose();
- }
- }
- private static float[] ConvertByteToFloat16(byte[] byteArray)
- {
- var floatArray = new float[byteArray.Length / 2];
- for (var i = 0; i < floatArray.Length; i++)
- {
- floatArray[i] = System.BitConverter.ToInt16(byteArray, i * 2) / 32768f; // -Int16.MinValue
- }
- return floatArray;
- }
- }
- #region -- Agora Event ---
- internal class UserEventHandler : IRtcEngineEventHandler
- {
- private readonly ProcessVideoRawData _agoraVideoRawData;
- internal UserEventHandler(ProcessVideoRawData agoraVideoRawData)
- {
- _agoraVideoRawData = agoraVideoRawData;
- }
- public override void OnError(int err, string msg)
- {
- _agoraVideoRawData.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg));
- }
- public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
- {
- int build = 0;
- _agoraVideoRawData.Log.UpdateLog(string.Format("sdk version: ${0}",
- _agoraVideoRawData.RtcEngine.GetVersion(ref build)));
- _agoraVideoRawData.Log.UpdateLog(
- string.Format("OnJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}",
- connection.channelId, connection.localUid, elapsed));
- }
- public override void OnRejoinChannelSuccess(RtcConnection connection, int elapsed)
- {
- _agoraVideoRawData.Log.UpdateLog("OnRejoinChannelSuccess");
- }
- public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
- {
- _agoraVideoRawData.Log.UpdateLog("OnLeaveChannel");
- }
- public override void OnClientRoleChanged(RtcConnection connection, CLIENT_ROLE_TYPE oldRole,
- CLIENT_ROLE_TYPE newRole)
- {
- _agoraVideoRawData.Log.UpdateLog("OnClientRoleChanged");
- }
- public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
- {
- _agoraVideoRawData.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid,
- elapsed));
- }
- public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
- {
- _agoraVideoRawData.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
- (int)reason));
- }
- }
- internal class VideoFrameObserver : IVideoFrameObserver
- {
- private readonly ProcessVideoRawData _agoraVideoRawData;
- internal VideoFrameObserver(ProcessVideoRawData agoraVideoRawData)
- {
- _agoraVideoRawData = agoraVideoRawData;
- }
- public override bool OnCaptureVideoFrame(VideoFrame videoFrame, VideoFrameBufferConfig config)
- {
- Debug.Log("OnCaptureVideoFrame-----------" + " width:" + videoFrame.width + " height:" +
- videoFrame.height);
- _agoraVideoRawData.VideoFrameWidth = videoFrame.width;
- _agoraVideoRawData.VideoFrameHeight = videoFrame.height;
- lock (_agoraVideoRawData.VideoBuffer)
- {
- _agoraVideoRawData.VideoBuffer = videoFrame.yBuffer;
- }
- return true;
- }
- public override bool OnRenderVideoFrame(string channelId, uint uid, VideoFrame videoFrame)
- {
- Debug.Log("OnRenderVideoFrameHandler-----------" + " uid:" + uid + " width:" + videoFrame.width +
- " height:" + videoFrame.height);
- return true;
- }
- public override VIDEO_OBSERVER_FRAME_TYPE GetVideoFormatPreference()
- {
- return VIDEO_OBSERVER_FRAME_TYPE.FRAME_TYPE_RGBA;
- }
- }
- #endregion
- }
|