SetVideoEncodeConfiguration.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using Agora.Rtc;
  4. using Agora.Util;
  5. using UnityEngine.Serialization;
  6. using Logger = Agora.Util.Logger;
  7. namespace Agora_RTC_Plugin.API_Example.Examples.Advanced.SetVideoEncodeConfiguration
  8. {
  9. public class SetVideoEncodeConfiguration : 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. // A list of dimensions for swithching
  28. private VideoDimensions[] _dimensions = new VideoDimensions[]
  29. {
  30. new VideoDimensions {width = 640, height = 480},
  31. new VideoDimensions {width = 480, height = 480},
  32. new VideoDimensions {width = 480, height = 240}
  33. };
  34. // Start is called before the first frame update
  35. private void Start()
  36. {
  37. LoadAssetData();
  38. if (CheckAppId())
  39. {
  40. InitEngine();
  41. JoinChannel();
  42. SetVideoEncoderConfiguration();
  43. }
  44. }
  45. // Update is called once per frame
  46. private void Update()
  47. {
  48. PermissionHelper.RequestMicrophontPermission();
  49. PermissionHelper.RequestCameraPermission();
  50. }
  51. private bool CheckAppId()
  52. {
  53. Log = new Logger(LogText);
  54. return Log.DebugAssert(_appID.Length > 10, "Please fill in your appId in API-Example/profile/appIdInput.asset");
  55. }
  56. //Show data in AgoraBasicProfile
  57. [ContextMenu("ShowAgoraBasicProfileData")]
  58. private void LoadAssetData()
  59. {
  60. if (_appIdInput == null) return;
  61. _appID = _appIdInput.appID;
  62. _token = _appIdInput.token;
  63. _channelName = _appIdInput.channelName;
  64. }
  65. private void InitEngine()
  66. {
  67. RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
  68. UserEventHandler handler = new UserEventHandler(this);
  69. RtcEngineContext context = new RtcEngineContext(_appID, 0,
  70. CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING,
  71. AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT);
  72. RtcEngine.Initialize(context);
  73. RtcEngine.InitEventHandler(handler);
  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 SetVideoEncoderConfiguration(int dim = 0)
  83. {
  84. if (dim >= _dimensions.Length)
  85. {
  86. Debug.LogError("Invalid dimension choice!");
  87. return;
  88. }
  89. VideoEncoderConfiguration config = new VideoEncoderConfiguration
  90. {
  91. dimensions = _dimensions[dim],
  92. frameRate = 15,
  93. codecType = VIDEO_CODEC_TYPE.VIDEO_CODEC_H264,
  94. bitrate = 0,
  95. minBitrate = 1,
  96. orientationMode = ORIENTATION_MODE.ORIENTATION_MODE_ADAPTIVE,
  97. degradationPreference = DEGRADATION_PREFERENCE.MAINTAIN_FRAMERATE,
  98. mirrorMode = VIDEO_MIRROR_MODE_TYPE.VIDEO_MIRROR_MODE_AUTO
  99. };
  100. RtcEngine.SetVideoEncoderConfiguration(config);
  101. }
  102. private void OnDestroy()
  103. {
  104. Debug.Log("OnDestroy");
  105. if (RtcEngine == null) return;
  106. RtcEngine.InitEventHandler(null);
  107. RtcEngine.LeaveChannel();
  108. RtcEngine.Dispose();
  109. }
  110. internal string GetChannelName()
  111. {
  112. return _channelName;
  113. }
  114. #region -- Video Render UI Logic ---
  115. internal static void MakeVideoView(uint uid, string channelId = "")
  116. {
  117. var go = GameObject.Find(uid.ToString());
  118. if (!ReferenceEquals(go, null))
  119. {
  120. return; // reuse
  121. }
  122. // create a GameObject and assign to this new user
  123. var videoSurface = MakeImageSurface(uid.ToString());
  124. if (ReferenceEquals(videoSurface, null)) return;
  125. // configure videoSurface
  126. if (uid == 0)
  127. {
  128. videoSurface.SetForUser(uid, channelId);
  129. }
  130. else
  131. {
  132. videoSurface.SetForUser(uid, channelId, VIDEO_SOURCE_TYPE.VIDEO_SOURCE_REMOTE);
  133. }
  134. videoSurface.OnTextureSizeModify += (int width, int height) =>
  135. {
  136. float scale = (float)height / (float)width;
  137. videoSurface.transform.localScale = new Vector3(-5, 5 * scale, 1);
  138. Debug.Log("OnTextureSizeModify: " + width + " " + height);
  139. };
  140. videoSurface.SetEnable(true);
  141. }
  142. // VIDEO TYPE 1: 3D Object
  143. private static VideoSurface MakePlaneSurface(string goName)
  144. {
  145. var go = GameObject.CreatePrimitive(PrimitiveType.Plane);
  146. if (go == null)
  147. {
  148. return null;
  149. }
  150. go.name = goName;
  151. // set up transform
  152. go.transform.Rotate(-90.0f, 0.0f, 0.0f);
  153. go.transform.position = Vector3.zero;
  154. go.transform.localScale = new Vector3(0.25f, 0.5f, 0.5f);
  155. // configure videoSurface
  156. var videoSurface = go.AddComponent<VideoSurface>();
  157. return videoSurface;
  158. }
  159. // Video TYPE 2: RawImage
  160. private static VideoSurface MakeImageSurface(string goName)
  161. {
  162. GameObject go = new GameObject();
  163. if (go == null)
  164. {
  165. return null;
  166. }
  167. go.name = goName;
  168. // to be renderered onto
  169. go.AddComponent<RawImage>();
  170. // make the object draggable
  171. go.AddComponent<UIElementDrag>();
  172. var canvas = GameObject.Find("VideoCanvas");
  173. if (canvas != null)
  174. {
  175. go.transform.parent = canvas.transform;
  176. Debug.Log("add video view");
  177. }
  178. else
  179. {
  180. Debug.Log("Canvas is null video view");
  181. }
  182. // set up transform
  183. go.transform.Rotate(0f, 0.0f, 180.0f);
  184. go.transform.localPosition = Vector3.zero;
  185. go.transform.localScale = new Vector3(2f, 3f, 1f);
  186. // configure videoSurface
  187. var videoSurface = go.AddComponent<VideoSurface>();
  188. return videoSurface;
  189. }
  190. internal static void DestroyVideoView(uint uid)
  191. {
  192. var go = GameObject.Find(uid.ToString());
  193. if (!ReferenceEquals(go, null))
  194. {
  195. Destroy(go);
  196. }
  197. }
  198. #endregion
  199. }
  200. #region -- Agora Event ---
  201. internal class UserEventHandler : IRtcEngineEventHandler
  202. {
  203. private readonly SetVideoEncodeConfiguration _videoEncoderConfiguration;
  204. internal UserEventHandler(SetVideoEncodeConfiguration videoEncoderConfiguration)
  205. {
  206. _videoEncoderConfiguration = videoEncoderConfiguration;
  207. }
  208. public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
  209. {
  210. int build = 0;
  211. _videoEncoderConfiguration.Log.UpdateLog(string.Format("sdk version: ${0}",
  212. _videoEncoderConfiguration.RtcEngine.GetVersion(ref build)));
  213. _videoEncoderConfiguration.Log.UpdateLog(string.Format(
  214. "onJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}", connection.channelId,
  215. connection.localUid, elapsed));
  216. SetVideoEncodeConfiguration.MakeVideoView(0);
  217. }
  218. public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
  219. {
  220. _videoEncoderConfiguration.Log.UpdateLog("OnLeaveChannelSuccess");
  221. SetVideoEncodeConfiguration.DestroyVideoView(0);
  222. }
  223. public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
  224. {
  225. _videoEncoderConfiguration.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid,
  226. elapsed));
  227. SetVideoEncodeConfiguration.MakeVideoView(uid, _videoEncoderConfiguration.GetChannelName());
  228. }
  229. public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
  230. {
  231. _videoEncoderConfiguration.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
  232. (int)reason));
  233. SetVideoEncodeConfiguration.DestroyVideoView(uid);
  234. }
  235. public override void OnError(int error, string msg)
  236. {
  237. _videoEncoderConfiguration.Log.UpdateLog(
  238. string.Format("OnSDKError error: {0}, msg: {1}", error, msg));
  239. }
  240. public override void OnConnectionLost(RtcConnection connection)
  241. {
  242. _videoEncoderConfiguration.Log.UpdateLog(string.Format("OnConnectionLost "));
  243. }
  244. }
  245. #endregion
  246. }