JoinChannelWithUserAccount.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.JoinChannelWithUserAccount
  8. {
  9. public class JoinChannelWithUserAccount : 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. private const string USER_ACCOUNT = "Unity";
  28. // Use this for initialization
  29. private void Start()
  30. {
  31. LoadAssetData();
  32. if (CheckAppId())
  33. {
  34. InitEngine();
  35. JoinChannel();
  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.JoinChannelWithUserAccount(_token, _channelName, USER_ACCOUNT);
  74. }
  75. public void GetUserInfoByUserAccount()
  76. {
  77. Agora.Rtc.UserInfo info = new Agora.Rtc.UserInfo();
  78. RtcEngine.GetUserInfoByUserAccount(USER_ACCOUNT, ref info);
  79. Log.UpdateLog(string.Format("GetUserInfoByUserAccount account: {0}, uid: {1}", info.userAccount, info.uid));
  80. }
  81. private void OnDestroy()
  82. {
  83. Debug.Log("OnDestroy");
  84. if (RtcEngine == null) return;
  85. RtcEngine.InitEventHandler(null);
  86. RtcEngine.LeaveChannel();
  87. RtcEngine.Dispose();
  88. }
  89. internal string GetChannelName()
  90. {
  91. return _channelName;
  92. }
  93. #region -- Video Render UI Logic ---
  94. internal static void MakeVideoView(uint uid, string channelId = "")
  95. {
  96. var go = GameObject.Find(uid.ToString());
  97. if (!ReferenceEquals(go, null))
  98. {
  99. return; // reuse
  100. }
  101. // create a GameObject and assign to this new user
  102. var videoSurface = MakeImageSurface(uid.ToString());
  103. if (ReferenceEquals(videoSurface, null)) return;
  104. // configure videoSurface
  105. if (uid == 0)
  106. {
  107. videoSurface.SetForUser(uid, channelId);
  108. }
  109. else
  110. {
  111. videoSurface.SetForUser(uid, channelId, VIDEO_SOURCE_TYPE.VIDEO_SOURCE_REMOTE);
  112. }
  113. videoSurface.OnTextureSizeModify += (int width, int height) =>
  114. {
  115. float scale = (float)height / (float)width;
  116. videoSurface.transform.localScale = new Vector3(-5, 5 * scale, 1);
  117. Debug.Log("OnTextureSizeModify: " + width + " " + height);
  118. };
  119. videoSurface.SetEnable(true);
  120. }
  121. // VIDEO TYPE 1: 3D Object
  122. private VideoSurface MakePlaneSurface(string goName)
  123. {
  124. var go = GameObject.CreatePrimitive(PrimitiveType.Plane);
  125. if (go == null)
  126. {
  127. return null;
  128. }
  129. go.name = goName;
  130. // set up transform
  131. go.transform.Rotate(-90.0f, 0.0f, 0.0f);
  132. go.transform.position = Vector3.zero;
  133. go.transform.localScale = new Vector3(0.25f, 0.5f, 0.5f);
  134. // configure videoSurface
  135. var videoSurface = go.AddComponent<VideoSurface>();
  136. return videoSurface;
  137. }
  138. // Video TYPE 2: RawImage
  139. private static VideoSurface MakeImageSurface(string goName)
  140. {
  141. GameObject go = new GameObject();
  142. if (go == null)
  143. {
  144. return null;
  145. }
  146. go.name = goName;
  147. // to be renderered onto
  148. go.AddComponent<RawImage>();
  149. // make the object draggable
  150. go.AddComponent<UIElementDrag>();
  151. var canvas = GameObject.Find("VideoCanvas");
  152. if (canvas != null)
  153. {
  154. go.transform.parent = canvas.transform;
  155. Debug.Log("add video view");
  156. }
  157. else
  158. {
  159. Debug.Log("Canvas is null video view");
  160. }
  161. // set up transform
  162. go.transform.Rotate(0f, 0.0f, 180.0f);
  163. go.transform.localPosition = Vector3.zero;
  164. go.transform.localScale = new Vector3(2f, 3f, 1f);
  165. // configure videoSurface
  166. var videoSurface = go.AddComponent<VideoSurface>();
  167. return videoSurface;
  168. }
  169. internal static void DestroyVideoView(uint uid)
  170. {
  171. var go = GameObject.Find(uid.ToString());
  172. if (!ReferenceEquals(go, null))
  173. {
  174. Destroy(go);
  175. }
  176. }
  177. #endregion
  178. }
  179. #region -- Agora Event ---
  180. internal class UserEventHandler : IRtcEngineEventHandler
  181. {
  182. private readonly JoinChannelWithUserAccount _videoSample;
  183. internal UserEventHandler(JoinChannelWithUserAccount videoSample)
  184. {
  185. _videoSample = videoSample;
  186. }
  187. public override void OnError(int err, string msg)
  188. {
  189. _videoSample.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg));
  190. }
  191. public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
  192. {
  193. int build = 0;
  194. Debug.Log("Agora: OnJoinChannelSuccess ");
  195. _videoSample.Log.UpdateLog(string.Format("sdk version: ${0}",
  196. _videoSample.RtcEngine.GetVersion(ref build)));
  197. _videoSample.Log.UpdateLog(
  198. string.Format("OnJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}",
  199. connection.channelId, connection.localUid, elapsed));
  200. JoinChannelWithUserAccount.MakeVideoView(0);
  201. }
  202. public override void OnRejoinChannelSuccess(RtcConnection connection, int elapsed)
  203. {
  204. _videoSample.Log.UpdateLog("OnRejoinChannelSuccess");
  205. }
  206. public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
  207. {
  208. _videoSample.Log.UpdateLog("OnLeaveChannel");
  209. JoinChannelWithUserAccount.DestroyVideoView(0);
  210. }
  211. public override void OnClientRoleChanged(RtcConnection connection, CLIENT_ROLE_TYPE oldRole, CLIENT_ROLE_TYPE newRole)
  212. {
  213. _videoSample.Log.UpdateLog("OnClientRoleChanged");
  214. }
  215. public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
  216. {
  217. _videoSample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  218. JoinChannelWithUserAccount.MakeVideoView(uid, _videoSample.GetChannelName());
  219. }
  220. public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
  221. {
  222. _videoSample.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
  223. (int)reason));
  224. JoinChannelWithUserAccount.DestroyVideoView(uid);
  225. }
  226. }
  227. #endregion
  228. }