ContentInspect.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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.ContentInspect
  8. {
  9. public class ContentInspect : 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 = null;
  27. // Use this for initialization
  28. private void Start()
  29. {
  30. LoadAssetData();
  31. if (CheckAppId())
  32. {
  33. InitEngine();
  34. JoinChannel();
  35. SetupUI();
  36. }
  37. }
  38. // Update is called once per frame
  39. private void Update()
  40. {
  41. PermissionHelper.RequestMicrophontPermission();
  42. PermissionHelper.RequestCameraPermission();
  43. }
  44. //Show data in AgoraBasicProfile
  45. [ContextMenu("ShowAgoraBasicProfileData")]
  46. private void LoadAssetData()
  47. {
  48. if (_appIdInput == null) return;
  49. _appID = _appIdInput.appID;
  50. _token = _appIdInput.token;
  51. _channelName = _appIdInput.channelName;
  52. }
  53. private bool CheckAppId()
  54. {
  55. Log = new Logger(LogText);
  56. return Log.DebugAssert(_appID.Length > 10, "Please fill in your appId in API-Example/profile/appIdInput.asset");
  57. }
  58. private void InitEngine()
  59. {
  60. RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
  61. UserEventHandler handler = new UserEventHandler(this);
  62. RtcEngineContext context = new RtcEngineContext(_appID, 0,
  63. CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING,
  64. AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT);
  65. RtcEngine.Initialize(context);
  66. RtcEngine.InitEventHandler(handler);
  67. }
  68. private void JoinChannel()
  69. {
  70. RtcEngine.EnableAudio();
  71. RtcEngine.EnableVideo();
  72. RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
  73. RtcEngine.JoinChannel(_token, _channelName);
  74. }
  75. private void SetupUI()
  76. {
  77. var ui = this.transform.Find("UI");
  78. var btn = ui.Find("StartButton").GetComponent<Button>();
  79. btn.onClick.AddListener(OnStartButtonClick);
  80. btn = ui.Find("StopButton").GetComponent<Button>();
  81. btn.onClick.AddListener(OnStopButtonClick);
  82. }
  83. private void OnStartButtonClick()
  84. {
  85. var config = new ContentInspectConfig();
  86. config.modules = new ContentInspectModule[1];
  87. config.modules[0] = new ContentInspectModule
  88. {
  89. type = CONTENT_INSPECT_TYPE.CONTENT_INSPECT_MODERATION,
  90. interval = 1
  91. };
  92. config.moduleCount = 1;
  93. var nRet = RtcEngine.EnableContentInspect(true, config);
  94. this.Log.UpdateLog("StartContentInspect: " + nRet);
  95. }
  96. private void OnStopButtonClick()
  97. {
  98. var config = new ContentInspectConfig();
  99. config.modules = new ContentInspectModule[1];
  100. config.modules[0] = new ContentInspectModule
  101. {
  102. type = CONTENT_INSPECT_TYPE.CONTENT_INSPECT_MODERATION,
  103. interval = 1
  104. };
  105. config.moduleCount = 1;
  106. var nRet = RtcEngine.EnableContentInspect(false, config);
  107. this.Log.UpdateLog("StopContentInspect: " + nRet);
  108. }
  109. private void OnDestroy()
  110. {
  111. Debug.Log("OnDestroy");
  112. if (RtcEngine == null) return;
  113. RtcEngine.InitEventHandler(null);
  114. RtcEngine.LeaveChannel();
  115. RtcEngine.Dispose();
  116. }
  117. internal string GetChannelName()
  118. {
  119. return _channelName;
  120. }
  121. #region -- Video Render UI Logic ---
  122. internal static void MakeVideoView(uint uid, string channelId = "")
  123. {
  124. var go = GameObject.Find(uid.ToString());
  125. if (!ReferenceEquals(go, null))
  126. {
  127. return; // reuse
  128. }
  129. // create a GameObject and assign to this new user
  130. var videoSurface = MakeImageSurface(uid.ToString());
  131. if (ReferenceEquals(videoSurface, null)) return;
  132. // configure videoSurface
  133. if (uid == 0)
  134. {
  135. videoSurface.SetForUser(uid, channelId);
  136. }
  137. else
  138. {
  139. videoSurface.SetForUser(uid, channelId, VIDEO_SOURCE_TYPE.VIDEO_SOURCE_REMOTE);
  140. }
  141. videoSurface.OnTextureSizeModify += (int width, int height) =>
  142. {
  143. float scale = (float)height / (float)width;
  144. videoSurface.transform.localScale = new Vector3(-5, 5 * scale, 1);
  145. Debug.Log("OnTextureSizeModify: " + width + " " + height);
  146. };
  147. videoSurface.SetEnable(true);
  148. }
  149. // VIDEO TYPE 1: 3D Object
  150. private static VideoSurface MakePlaneSurface(string goName)
  151. {
  152. var go = GameObject.CreatePrimitive(PrimitiveType.Plane);
  153. if (go == null)
  154. {
  155. return null;
  156. }
  157. go.name = goName;
  158. // set up transform
  159. go.transform.Rotate(-90.0f, 0.0f, 0.0f);
  160. var yPos = Random.Range(3.0f, 5.0f);
  161. var xPos = Random.Range(-2.0f, 2.0f);
  162. go.transform.position = Vector3.zero;
  163. go.transform.localScale = new Vector3(0.25f, 0.5f, 0.5f);
  164. // configure videoSurface
  165. var videoSurface = go.AddComponent<VideoSurface>();
  166. return videoSurface;
  167. }
  168. // Video TYPE 2: RawImage
  169. private static VideoSurface MakeImageSurface(string goName)
  170. {
  171. GameObject go = new GameObject();
  172. if (go == null)
  173. {
  174. return null;
  175. }
  176. go.name = goName;
  177. // to be renderered onto
  178. go.AddComponent<RawImage>();
  179. // make the object draggable
  180. go.AddComponent<UIElementDrag>();
  181. var canvas = GameObject.Find("VideoCanvas");
  182. if (canvas != null)
  183. {
  184. go.transform.parent = canvas.transform;
  185. Debug.Log("add video view");
  186. }
  187. else
  188. {
  189. Debug.Log("Canvas is null video view");
  190. }
  191. // set up transform
  192. go.transform.Rotate(0f, 0.0f, 180.0f);
  193. go.transform.localPosition = Vector3.zero;
  194. go.transform.localScale = new Vector3(2f, 3f, 1f);
  195. // configure videoSurface
  196. var videoSurface = go.AddComponent<VideoSurface>();
  197. return videoSurface;
  198. }
  199. internal static void DestroyVideoView(uint uid)
  200. {
  201. var go = GameObject.Find(uid.ToString());
  202. if (!ReferenceEquals(go, null))
  203. {
  204. Destroy(go);
  205. }
  206. }
  207. #endregion
  208. }
  209. #region -- Agora Event ---
  210. internal class UserEventHandler : IRtcEngineEventHandler
  211. {
  212. private readonly ContentInspect _sample;
  213. internal UserEventHandler(ContentInspect sample)
  214. {
  215. _sample = sample;
  216. }
  217. public override void OnError(int err, string msg)
  218. {
  219. _sample.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg));
  220. }
  221. public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
  222. {
  223. int build = 0;
  224. Debug.Log("Agora: OnJoinChannelSuccess ");
  225. _sample.Log.UpdateLog(string.Format("sdk version: ${0}",
  226. _sample.RtcEngine.GetVersion(ref build)));
  227. _sample.Log.UpdateLog(
  228. string.Format("OnJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}",
  229. connection.channelId, connection.localUid, elapsed));
  230. ContentInspect.MakeVideoView(0);
  231. }
  232. public override void OnRejoinChannelSuccess(RtcConnection connection, int elapsed)
  233. {
  234. _sample.Log.UpdateLog("OnRejoinChannelSuccess");
  235. }
  236. public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
  237. {
  238. _sample.Log.UpdateLog("OnLeaveChannel");
  239. ContentInspect.DestroyVideoView(0);
  240. }
  241. public override void OnClientRoleChanged(RtcConnection connection, CLIENT_ROLE_TYPE oldRole, CLIENT_ROLE_TYPE newRole)
  242. {
  243. _sample.Log.UpdateLog("OnClientRoleChanged");
  244. }
  245. public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
  246. {
  247. _sample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  248. ContentInspect.MakeVideoView(uid, _sample.GetChannelName());
  249. }
  250. public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
  251. {
  252. _sample.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
  253. (int)reason));
  254. ContentInspect.DestroyVideoView(uid);
  255. }
  256. public override void OnContentInspectResult(CONTENT_INSPECT_RESULT result)
  257. {
  258. _sample.Log.UpdateLog("OnContentInspectResult :" + result);
  259. }
  260. }
  261. #endregion
  262. }