ProcessVideoRawData.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Serialization;
  4. using Agora.Rtc;
  5. using Agora.Util;
  6. using Logger = Agora.Util.Logger;
  7. namespace Agora_RTC_Plugin.API_Example.Examples.Advanced.ProcessVideoRawData
  8. {
  9. public class ProcessVideoRawData : MonoBehaviour
  10. {
  11. [FormerlySerializedAs("appIdInput")]
  12. [SerializeField]
  13. private AppIdInput _appIdInput;
  14. [Header("_____________Basic Configuration_____________")]
  15. [FormerlySerializedAs("APP_ID")]
  16. [SerializeField]
  17. private string _appID = "";
  18. [FormerlySerializedAs("TOKEN")]
  19. [SerializeField]
  20. private string _token = "";
  21. [FormerlySerializedAs("CHANNEL_NAME")]
  22. [SerializeField]
  23. private string _channelName = "";
  24. public Text LogText;
  25. internal Logger Log;
  26. internal IRtcEngine RtcEngine;
  27. internal int Count;
  28. internal int WriteCount;
  29. internal int ReadCount;
  30. internal byte[] VideoBuffer = new byte[0];
  31. private int _videoFrameWidth = 1080;
  32. public int VideoFrameWidth
  33. {
  34. set
  35. {
  36. if (value != _videoFrameWidth)
  37. {
  38. _videoFrameWidth = value;
  39. _needResize = true;
  40. }
  41. }
  42. get
  43. {
  44. return _videoFrameWidth;
  45. }
  46. }
  47. private int _videoFrameHeight = 720;
  48. public int VideoFrameHeight
  49. {
  50. set
  51. {
  52. if (value != _videoFrameHeight)
  53. {
  54. _videoFrameHeight = value;
  55. _needResize = true;
  56. }
  57. }
  58. get
  59. {
  60. return _videoFrameHeight;
  61. }
  62. }
  63. private bool _needResize = false;
  64. public GameObject VideoView;
  65. private Texture2D _texture;
  66. private bool _isTextureAttach = false;
  67. void Start()
  68. {
  69. LoadAssetData();
  70. if (CheckAppId())
  71. {
  72. SetUpUI();
  73. InitEngine();
  74. JoinChannel();
  75. }
  76. }
  77. // Update is called once per frame
  78. void Update()
  79. {
  80. PermissionHelper.RequestMicrophontPermission();
  81. PermissionHelper.RequestCameraPermission();
  82. if (!_isTextureAttach)
  83. {
  84. var rd = VideoView.GetComponent<RawImage>();
  85. rd.texture = _texture;
  86. _isTextureAttach = true;
  87. }
  88. else if (VideoBuffer != null && VideoBuffer.Length != 0 && !_needResize)
  89. {
  90. lock (VideoBuffer)
  91. {
  92. _texture.LoadRawTextureData(VideoBuffer);
  93. _texture.Apply();
  94. }
  95. }
  96. else if(_needResize)
  97. {
  98. _texture.Reinitialize(_videoFrameWidth, _videoFrameHeight);
  99. _texture.Apply();
  100. _needResize = false;
  101. }
  102. }
  103. bool CheckAppId()
  104. {
  105. Log = new Logger(LogText);
  106. return Log.DebugAssert(_appID.Length > 10, "Please fill in your appId in API-Example/profile/appIdInput.asset");
  107. }
  108. //Show data in AgoraBasicProfile
  109. [ContextMenu("ShowAgoraBasicProfileData")]
  110. public void LoadAssetData()
  111. {
  112. if (_appIdInput == null) return;
  113. _appID = _appIdInput.appID;
  114. _token = _appIdInput.token;
  115. _channelName = _appIdInput.channelName;
  116. }
  117. void SetUpUI()
  118. {
  119. var bufferLength = _videoFrameHeight * _videoFrameWidth * 4;
  120. _texture = new Texture2D(_videoFrameWidth, _videoFrameHeight, TextureFormat.RGBA32, false);
  121. _texture.Apply();
  122. }
  123. void InitEngine()
  124. {
  125. RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
  126. UserEventHandler handler = new UserEventHandler(this);
  127. RtcEngineContext context = new RtcEngineContext(_appID, 0,
  128. CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING,
  129. AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT);
  130. RtcEngine.Initialize(context);
  131. RtcEngine.InitEventHandler(handler);
  132. RtcEngine.RegisterVideoFrameObserver(new VideoFrameObserver(this), OBSERVER_MODE.RAW_DATA);
  133. }
  134. void JoinChannel()
  135. {
  136. VideoEncoderConfiguration config = new VideoEncoderConfiguration();
  137. config.dimensions = new VideoDimensions(_videoFrameWidth, _videoFrameHeight);
  138. RtcEngine.SetVideoEncoderConfiguration(config);
  139. RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
  140. RtcEngine.EnableAudio();
  141. RtcEngine.EnableVideo();
  142. RtcEngine.JoinChannel(_token, _channelName);
  143. }
  144. private void OnDestroy()
  145. {
  146. Debug.Log("OnDestroy");
  147. if (RtcEngine != null)
  148. {
  149. if (_texture != null)
  150. {
  151. GameObject.Destroy(_texture);
  152. _texture = null;
  153. }
  154. RtcEngine.UnRegisterVideoFrameObserver();
  155. RtcEngine.InitEventHandler(null);
  156. RtcEngine.LeaveChannel();
  157. RtcEngine.Dispose();
  158. }
  159. }
  160. private static float[] ConvertByteToFloat16(byte[] byteArray)
  161. {
  162. var floatArray = new float[byteArray.Length / 2];
  163. for (var i = 0; i < floatArray.Length; i++)
  164. {
  165. floatArray[i] = System.BitConverter.ToInt16(byteArray, i * 2) / 32768f; // -Int16.MinValue
  166. }
  167. return floatArray;
  168. }
  169. }
  170. #region -- Agora Event ---
  171. internal class UserEventHandler : IRtcEngineEventHandler
  172. {
  173. private readonly ProcessVideoRawData _agoraVideoRawData;
  174. internal UserEventHandler(ProcessVideoRawData agoraVideoRawData)
  175. {
  176. _agoraVideoRawData = agoraVideoRawData;
  177. }
  178. public override void OnError(int err, string msg)
  179. {
  180. _agoraVideoRawData.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg));
  181. }
  182. public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
  183. {
  184. int build = 0;
  185. _agoraVideoRawData.Log.UpdateLog(string.Format("sdk version: ${0}",
  186. _agoraVideoRawData.RtcEngine.GetVersion(ref build)));
  187. _agoraVideoRawData.Log.UpdateLog(
  188. string.Format("OnJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}",
  189. connection.channelId, connection.localUid, elapsed));
  190. }
  191. public override void OnRejoinChannelSuccess(RtcConnection connection, int elapsed)
  192. {
  193. _agoraVideoRawData.Log.UpdateLog("OnRejoinChannelSuccess");
  194. }
  195. public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
  196. {
  197. _agoraVideoRawData.Log.UpdateLog("OnLeaveChannel");
  198. }
  199. public override void OnClientRoleChanged(RtcConnection connection, CLIENT_ROLE_TYPE oldRole,
  200. CLIENT_ROLE_TYPE newRole)
  201. {
  202. _agoraVideoRawData.Log.UpdateLog("OnClientRoleChanged");
  203. }
  204. public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
  205. {
  206. _agoraVideoRawData.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid,
  207. elapsed));
  208. }
  209. public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
  210. {
  211. _agoraVideoRawData.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
  212. (int)reason));
  213. }
  214. }
  215. internal class VideoFrameObserver : IVideoFrameObserver
  216. {
  217. private readonly ProcessVideoRawData _agoraVideoRawData;
  218. internal VideoFrameObserver(ProcessVideoRawData agoraVideoRawData)
  219. {
  220. _agoraVideoRawData = agoraVideoRawData;
  221. }
  222. public override bool OnCaptureVideoFrame(VideoFrame videoFrame, VideoFrameBufferConfig config)
  223. {
  224. Debug.Log("OnCaptureVideoFrame-----------" + " width:" + videoFrame.width + " height:" +
  225. videoFrame.height);
  226. _agoraVideoRawData.VideoFrameWidth = videoFrame.width;
  227. _agoraVideoRawData.VideoFrameHeight = videoFrame.height;
  228. lock (_agoraVideoRawData.VideoBuffer)
  229. {
  230. _agoraVideoRawData.VideoBuffer = videoFrame.yBuffer;
  231. }
  232. return true;
  233. }
  234. public override bool OnRenderVideoFrame(string channelId, uint uid, VideoFrame videoFrame)
  235. {
  236. Debug.Log("OnRenderVideoFrameHandler-----------" + " uid:" + uid + " width:" + videoFrame.width +
  237. " height:" + videoFrame.height);
  238. return true;
  239. }
  240. public override VIDEO_OBSERVER_FRAME_TYPE GetVideoFormatPreference()
  241. {
  242. return VIDEO_OBSERVER_FRAME_TYPE.FRAME_TYPE_RGBA;
  243. }
  244. }
  245. #endregion
  246. }