MetadataSample.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.Serialization;
  7. using Agora.Rtc;
  8. using Agora.Util;
  9. using Logger = Agora.Util.Logger;
  10. namespace Agora_RTC_Plugin.API_Example.Examples.Advanced.MetadataSample
  11. {
  12. public class MetadataSample : MonoBehaviour
  13. {
  14. [FormerlySerializedAs("appIdInput")]
  15. [SerializeField]
  16. private AppIdInput _appIdInput;
  17. [Header("_____________Basic Configuration_____________")]
  18. [FormerlySerializedAs("APP_ID")]
  19. [SerializeField]
  20. public string _appID = "";
  21. [FormerlySerializedAs("TOKEN")]
  22. [SerializeField]
  23. public string _token = "";
  24. [FormerlySerializedAs("CHANNEL_NAME")]
  25. [SerializeField]
  26. public string _channelName = "";
  27. public Text LogText;
  28. internal Logger Log;
  29. internal IRtcEngine RtcEngine;
  30. internal bool Sending = false;
  31. internal Queue<String> MetadataQueue = new Queue<string>();
  32. private void Start()
  33. {
  34. LoadAssetData();
  35. if (CheckAppId())
  36. {
  37. InitEngine();
  38. SetupUI();
  39. JoinChannel();
  40. }
  41. }
  42. [ContextMenu("ShowAgoraBasicProfileData")]
  43. private void LoadAssetData()
  44. {
  45. if (_appIdInput == null) return;
  46. _appID = _appIdInput.appID;
  47. _token = _appIdInput.token;
  48. _channelName = _appIdInput.channelName;
  49. }
  50. private bool CheckAppId()
  51. {
  52. Log = new Logger(LogText);
  53. return Log.DebugAssert(_appID.Length > 10, "Please fill in your appId in API-Example/profile/appIdInput.asset");
  54. }
  55. private void InitEngine()
  56. {
  57. RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
  58. UserEventHandler handler = new UserEventHandler(this);
  59. RtcEngineContext context = new RtcEngineContext(_appID, 0,
  60. CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING,
  61. AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_GAME_STREAMING);
  62. RtcEngine.Initialize(context);
  63. RtcEngine.InitEventHandler(handler);
  64. UserMetadataObserver metadataObserver = new UserMetadataObserver(this);
  65. RtcEngine.RegisterMediaMetadataObserver(metadataObserver, METADATA_TYPE.VIDEO_METADATA);
  66. }
  67. private void SetupUI()
  68. {
  69. var ui = this.transform.Find("UI");
  70. var btn = ui.Find("StartButton").GetComponent<Button>();
  71. btn.onClick.AddListener(OnStartButtonPress);
  72. btn = ui.Find("StopButton").GetComponent<Button>();
  73. btn.onClick.AddListener(OnStopButtonPress);
  74. }
  75. private void JoinChannel()
  76. {
  77. RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
  78. RtcEngine.EnableAudio();
  79. RtcEngine.EnableVideo();
  80. RtcEngine.JoinChannel(_token, _channelName, "");
  81. }
  82. private void Update()
  83. {
  84. PermissionHelper.RequestMicrophontPermission();
  85. lock (MetadataQueue)
  86. {
  87. while(MetadataQueue.Count > 0)
  88. {
  89. string metadataString = MetadataQueue.Dequeue();
  90. this.Log.UpdateLog(metadataString);
  91. }
  92. }
  93. }
  94. private void OnStartButtonPress()
  95. {
  96. this.Sending = true;
  97. this.Log.UpdateLog("Sending: true");
  98. }
  99. private void OnStopButtonPress()
  100. {
  101. this.Sending = false;
  102. this.Log.UpdateLog("Sending: false");
  103. }
  104. private void OnDestroy()
  105. {
  106. Debug.Log("OnDestroy");
  107. if (RtcEngine == null) return;
  108. RtcEngine.InitEventHandler(null);
  109. RtcEngine.UnregisterMediaMetadataObserver();
  110. RtcEngine.LeaveChannel();
  111. RtcEngine.Dispose();
  112. }
  113. public string GetChannelName()
  114. {
  115. return this._channelName;
  116. }
  117. #region -- Video Render UI Logic ---
  118. internal static void MakeVideoView(uint uid, string channelId = "")
  119. {
  120. var go = GameObject.Find(uid.ToString());
  121. if (!ReferenceEquals(go, null))
  122. {
  123. return; // reuse
  124. }
  125. // create a GameObject and assign to this new user
  126. var videoSurface = MakeImageSurface(uid.ToString());
  127. if (ReferenceEquals(videoSurface, null)) return;
  128. // configure videoSurface
  129. if (uid == 0)
  130. {
  131. videoSurface.SetForUser(uid, channelId);
  132. }
  133. else
  134. {
  135. videoSurface.SetForUser(uid, channelId, VIDEO_SOURCE_TYPE.VIDEO_SOURCE_REMOTE);
  136. }
  137. videoSurface.OnTextureSizeModify += (int width, int height) =>
  138. {
  139. float scale = (float)height / (float)width;
  140. videoSurface.transform.localScale = new Vector3(-5, 5 * scale, 1);
  141. Debug.Log("OnTextureSizeModify: " + width + " " + height);
  142. };
  143. videoSurface.SetEnable(true);
  144. }
  145. // VIDEO TYPE 1: 3D Object
  146. private static VideoSurface MakePlaneSurface(string goName)
  147. {
  148. var go = GameObject.CreatePrimitive(PrimitiveType.Plane);
  149. if (go == null)
  150. {
  151. return null;
  152. }
  153. go.name = goName;
  154. // set up transform
  155. go.transform.Rotate(-90.0f, 0.0f, 0.0f);
  156. go.transform.position = Vector3.zero;
  157. go.transform.localScale = new Vector3(0.25f, 0.5f, 0.5f);
  158. // configure videoSurface
  159. var videoSurface = go.AddComponent<VideoSurface>();
  160. return videoSurface;
  161. }
  162. // Video TYPE 2: RawImage
  163. private static VideoSurface MakeImageSurface(string goName)
  164. {
  165. GameObject go = new GameObject();
  166. if (go == null)
  167. {
  168. return null;
  169. }
  170. go.name = goName;
  171. // to be renderered onto
  172. go.AddComponent<RawImage>();
  173. // make the object draggable
  174. go.AddComponent<UIElementDrag>();
  175. var canvas = GameObject.Find("VideoCanvas");
  176. if (canvas != null)
  177. {
  178. go.transform.parent = canvas.transform;
  179. Debug.Log("add video view");
  180. }
  181. else
  182. {
  183. Debug.Log("Canvas is null video view");
  184. }
  185. // set up transform
  186. go.transform.Rotate(0f, 0.0f, 180.0f);
  187. go.transform.localPosition = Vector3.zero;
  188. go.transform.localScale = new Vector3(2f, 3f, 1f);
  189. // configure videoSurface
  190. var videoSurface = go.AddComponent<VideoSurface>();
  191. return videoSurface;
  192. }
  193. internal static void DestroyVideoView(uint uid)
  194. {
  195. var go = GameObject.Find(uid.ToString());
  196. if (!ReferenceEquals(go, null))
  197. {
  198. Destroy(go);
  199. }
  200. }
  201. #endregion
  202. }
  203. #region -- Agora Event ---
  204. internal class UserEventHandler : IRtcEngineEventHandler
  205. {
  206. private readonly MetadataSample _sample;
  207. internal UserEventHandler(MetadataSample sample)
  208. {
  209. _sample = sample;
  210. }
  211. public override void OnError(int err, string msg)
  212. {
  213. _sample.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg));
  214. }
  215. public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
  216. {
  217. int build = 0;
  218. Debug.Log("Agora: OnJoinChannelSuccess ");
  219. _sample.Log.UpdateLog(string.Format("sdk version: ${0}",
  220. _sample.RtcEngine.GetVersion(ref build)));
  221. _sample.Log.UpdateLog(
  222. string.Format("OnJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}",
  223. connection.channelId, connection.localUid, elapsed));
  224. MetadataSample.MakeVideoView(0);
  225. }
  226. public override void OnRejoinChannelSuccess(RtcConnection connection, int elapsed)
  227. {
  228. _sample.Log.UpdateLog("OnRejoinChannelSuccess");
  229. }
  230. public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
  231. {
  232. _sample.Log.UpdateLog("OnLeaveChannel");
  233. MetadataSample.DestroyVideoView(0);
  234. }
  235. public override void OnClientRoleChanged(RtcConnection connection, CLIENT_ROLE_TYPE oldRole, CLIENT_ROLE_TYPE newRole)
  236. {
  237. _sample.Log.UpdateLog("OnClientRoleChanged");
  238. }
  239. public override void OnStreamMessageError(RtcConnection connection, uint remoteUid, int streamId, int code, int missed, int cached)
  240. {
  241. _sample.Log.UpdateLog(string.Format("OnStreamMessageError remoteUid: {0}, streamId: {1}, code: {2}, missed: {3}, cached: {4}", remoteUid, streamId, code, missed, cached));
  242. }
  243. public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
  244. {
  245. _sample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  246. MetadataSample.MakeVideoView(uid, _sample.GetChannelName());
  247. }
  248. public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
  249. {
  250. _sample.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
  251. (int)reason));
  252. MetadataSample.DestroyVideoView(uid);
  253. }
  254. }
  255. internal class UserMetadataObserver : IMetadataObserver
  256. {
  257. MetadataSample _sample;
  258. private int tick = 0;
  259. internal UserMetadataObserver(MetadataSample sample)
  260. {
  261. _sample = sample;
  262. }
  263. public override int GetMaxMetadataSize()
  264. {
  265. return 128;
  266. }
  267. public override bool OnReadyToSendMetadata(ref Metadata metadata, VIDEO_SOURCE_TYPE source_type)
  268. {
  269. if (this._sample.Sending)
  270. {
  271. this.tick++;
  272. string str = "tick :" + tick;
  273. byte[] strByte = System.Text.Encoding.Default.GetBytes(str);
  274. Marshal.Copy(strByte, 0, metadata.buffer, strByte.Length);
  275. metadata.size = (uint)strByte.Length;
  276. Debug.Log("OnReadyToSendMetadata Sended metadatasize:" + metadata.size);
  277. }
  278. return this._sample.Sending;
  279. }
  280. public override void OnMetadataReceived(Metadata data)
  281. {
  282. //this callback not trigger in unity main thread
  283. byte[] strByte = new byte[data.size];
  284. Marshal.Copy(data.buffer, strByte, 0, (int)data.size);
  285. string str = System.Text.Encoding.Default.GetString(strByte);
  286. var str2 = string.Format("OnMetadataReceived uid:{0} buffer:{1}", data.uid, str);
  287. lock (this._sample.MetadataQueue)
  288. {
  289. this._sample.MetadataQueue.Enqueue(str2);
  290. }
  291. }
  292. }
  293. #endregion
  294. }