SetBeautyEffectOptions.cs 12 KB

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