439e13c714285af4807bd3ac4b49f1c8277f41c6.svn-base 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package io.agora.media;
  2. public class RtcTokenBuilder {
  3. public enum Role {
  4. /**
  5. * DEPRECATED. Role_Attendee has the same privileges as Role_Publisher.
  6. */
  7. Role_Attendee(0),
  8. /**
  9. * RECOMMENDED. Use this role for a voice/video call or a live broadcast, if your scenario does not require authentication for [Hosting-in](https://docs.agora.io/en/Agora%20Platform/terms?platform=All%20Platforms#hosting-in).
  10. */
  11. Role_Publisher(1),
  12. /**
  13. * Only use this role if your scenario require authentication for [Hosting-in](https://docs.agora.io/en/Agora%20Platform/terms?platform=All%20Platforms#hosting-in).
  14. *
  15. * @note In order for this role to take effect, please contact our support team to enable authentication for Hosting-in for you. Otherwise, Role_Subscriber still has the same privileges as Role_Publisher.
  16. */
  17. Role_Subscriber(2),
  18. /**
  19. * DEPRECATED. Role_Attendee has the same privileges as Role_Publisher.
  20. */
  21. Role_Admin(101);
  22. public int initValue;
  23. Role(int initValue) {
  24. this.initValue = initValue;
  25. }
  26. }
  27. /**
  28. * Builds an RTC token using an int uid.
  29. *
  30. * @param appId The App ID issued to you by Agora.
  31. * @param appCertificate Certificate of the application that you registered in
  32. * the Agora Dashboard.
  33. * @param channelName The unique channel name for the AgoraRTC session in the string format. The string length must be less than 64 bytes. Supported character scopes are:
  34. * <ul>
  35. * <li> The 26 lowercase English letters: a to z.</li>
  36. * <li> The 26 uppercase English letters: A to Z.</li>
  37. * <li> The 10 digits: 0 to 9.</li>
  38. * <li> The space.</li>
  39. * <li> "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">", "?", "@", "[", "]", "^", "_", " {", "}", "|", "~", ",".
  40. * </ul>
  41. * @param uid User ID. A 32-bit unsigned integer with a value ranging from
  42. * 1 to (2^32-1).
  43. * @param role The user role.
  44. * <ul>
  45. * <li> Role_Publisher = 1: RECOMMENDED. Use this role for a voice/video call or a live broadcast.</li>
  46. * <li> Role_Subscriber = 2: ONLY use this role if your live-broadcast scenario requires authentication for [Hosting-in](https://docs.agora.io/en/Agora%20Platform/terms?platform=All%20Platforms#hosting-in). In order for this role to take effect, please contact our support team to enable authentication for Hosting-in for you. Otherwise, Role_Subscriber still has the same privileges as Role_Publisher.</li>
  47. * </ul>
  48. * @param privilegeTs Represented by the number of seconds elapsed since 1/1/1970.
  49. * If, for example, you want to access the Agora Service within 10 minutes
  50. * after the token is generated, set expireTimestamp as the current time stamp
  51. * + 600 (seconds).
  52. */
  53. public String buildTokenWithUid(String appId, String appCertificate,
  54. String channelName, int uid, Role role, int privilegeTs) {
  55. String account = uid == 0 ? "" : String.valueOf(uid & 0xFFFFFFFFL);
  56. return buildTokenWithUserAccount(appId, appCertificate, channelName,
  57. account, role, privilegeTs);
  58. }
  59. /**
  60. * Builds an RTC token using a string userAccount.
  61. *
  62. * @param appId The App ID issued to you by Agora.
  63. * @param appCertificate Certificate of the application that you registered in
  64. * the Agora Dashboard.
  65. * @param channelName The unique channel name for the AgoraRTC session in the string format. The string length must be less than 64 bytes. Supported character scopes are:
  66. * <ul>
  67. * <li> The 26 lowercase English letters: a to z.</li>
  68. * <li> The 26 uppercase English letters: A to Z.</li>
  69. * <li> The 10 digits: 0 to 9.</li>
  70. * <li> The space.</li>
  71. * <li> "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">", "?", "@", "[", "]", "^", "_", " {", "}", "|", "~", ",".
  72. * </ul>
  73. * @param account The user account.
  74. * @param role The user role.
  75. * <ul>
  76. * <li> Role_Publisher = 1: RECOMMENDED. Use this role for a voice/video call or a live broadcast.</li>
  77. * <li> Role_Subscriber = 2: ONLY use this role if your live-broadcast scenario requires authentication for [Hosting-in](https://docs.agora.io/en/Agora%20Platform/terms?platform=All%20Platforms#hosting-in). In order for this role to take effect, please contact our support team to enable authentication for Hosting-in for you. Otherwise, Role_Subscriber still has the same privileges as Role_Publisher.</li>
  78. * </ul>
  79. * @param privilegeTs represented by the number of seconds elapsed since 1/1/1970.
  80. * If, for example, you want to access the Agora Service within 10 minutes
  81. * after the token is generated, set expireTimestamp as the current time stamp
  82. * + 600 (seconds).
  83. */
  84. public String buildTokenWithUserAccount(String appId, String appCertificate,
  85. String channelName, String account, Role role, int privilegeTs) {
  86. // Assign appropriate access privileges to each role.
  87. AccessToken builder = new AccessToken(appId, appCertificate, channelName, account);
  88. builder.addPrivilege(AccessToken.Privileges.kJoinChannel, privilegeTs);
  89. if (role == Role.Role_Publisher || role == Role.Role_Attendee || role == Role.Role_Admin) {
  90. builder.addPrivilege(AccessToken.Privileges.kPublishAudioStream, privilegeTs);
  91. builder.addPrivilege(AccessToken.Privileges.kPublishVideoStream, privilegeTs);
  92. builder.addPrivilege(AccessToken.Privileges.kPublishDataStream, privilegeTs);
  93. }
  94. try {
  95. return builder.build();
  96. } catch (Exception e) {
  97. e.printStackTrace();
  98. return "";
  99. }
  100. }
  101. /**
  102. * Generates an RTC token with the specified privilege.
  103. *
  104. * This method supports generating a token with the following privileges:
  105. * - Joining an RTC channel.
  106. * - Publishing audio in an RTC channel.
  107. * - Publishing video in an RTC channel.
  108. * - Publishing data streams in an RTC channel.
  109. *
  110. * The privileges for publishing audio, video, and data streams in an RTC channel apply only if you have
  111. * enabled co-host authentication.
  112. *
  113. * A user can have multiple privileges. Each privilege is valid for a maximum of 24 hours.
  114. * The SDK triggers the onTokenPrivilegeWillExpire and onRequestToken callbacks when the token is about to expire
  115. * or has expired. The callbacks do not report the specific privilege affected, and you need to maintain
  116. * the respective timestamp for each privilege in your app logic. After receiving the callback, you need
  117. * to generate a new token, and then call renewToken to pass the new token to the SDK, or call joinChannel to re-join
  118. * the channel.
  119. *
  120. * @note
  121. * Agora recommends setting a reasonable timestamp for each privilege according to your scenario.
  122. * Suppose the expiration timestamp for joining the channel is set earlier than that for publishing audio.
  123. * When the token for joining the channel expires, the user is immediately kicked off the RTC channel
  124. * and cannot publish any audio stream, even though the timestamp for publishing audio has not expired.
  125. *
  126. * @param appId The App ID of your Agora project.
  127. * @param appCertificate The App Certificate of your Agora project.
  128. * @param channelName The unique channel name for the Agora RTC session in string format. The string length must be less than 64 bytes. The channel name may contain the following characters:
  129. * - All lowercase English letters: a to z.
  130. * - All uppercase English letters: A to Z.
  131. * - All numeric characters: 0 to 9.
  132. * - The space character.
  133. * - "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">", "?", "@", "[", "]", "^", "_", " {", "}", "|", "~", ",".
  134. * @param uid The user ID. A 32-bit unsigned integer with a value range from 1 to (232 - 1). It must be unique. Set uid as 0, if you do not want to authenticate the user ID, that is, any uid from the app client can join the channel.
  135. * @param joinChannelPrivilegeExpiredTs The Unix timestamp when the privilege for joining the channel expires, represented
  136. * by the sum of the current timestamp plus the valid time period of the token. For example, if you set joinChannelPrivilegeExpiredTs as the
  137. * current timestamp plus 600 seconds, the token expires in 10 minutes.
  138. * @param pubAudioPrivilegeExpiredTs The Unix timestamp when the privilege for publishing audio expires, represented
  139. * by the sum of the current timestamp plus the valid time period of the token. For example, if you set pubAudioPrivilegeExpiredTs as the
  140. * current timestamp plus 600 seconds, the token expires in 10 minutes. If you do not want to enable this privilege,
  141. * set pubAudioPrivilegeExpiredTs as the current Unix timestamp.
  142. * @param pubVideoPrivilegeExpiredTs The Unix timestamp when the privilege for publishing video expires, represented
  143. * by the sum of the current timestamp plus the valid time period of the token. For example, if you set pubVideoPrivilegeExpiredTs as the
  144. * current timestamp plus 600 seconds, the token expires in 10 minutes. If you do not want to enable this privilege,
  145. * set pubVideoPrivilegeExpiredTs as the current Unix timestamp.
  146. * @param pubDataStreamPrivilegeExpiredTs The Unix timestamp when the privilege for publishing data streams expires, represented
  147. * by the sum of the current timestamp plus the valid time period of the token. For example, if you set pubDataStreamPrivilegeExpiredTs as the
  148. * current timestamp plus 600 seconds, the token expires in 10 minutes. If you do not want to enable this privilege,
  149. * set pubDataStreamPrivilegeExpiredTs as the current Unix timestamp.
  150. */
  151. public String buildTokenWithUid(String appId, String appCertificate,
  152. String channelName, int uid, int joinChannelPrivilegeExpiredTs,
  153. int pubAudioPrivilegeExpiredTs, int pubVideoPrivilegeExpiredTs,
  154. int pubDataStreamPrivilegeExpiredTs) {
  155. String account = uid == 0 ? "" : String.valueOf(uid & 0xFFFFFFFFL);
  156. return buildTokenWithUserAccount(appId, appCertificate, channelName,
  157. account, joinChannelPrivilegeExpiredTs, pubAudioPrivilegeExpiredTs,
  158. pubVideoPrivilegeExpiredTs, pubDataStreamPrivilegeExpiredTs);
  159. }
  160. /**
  161. * Generates an RTC token with the specified privilege.
  162. *
  163. * This method supports generating a token with the following privileges:
  164. * - Joining an RTC channel.
  165. * - Publishing audio in an RTC channel.
  166. * - Publishing video in an RTC channel.
  167. * - Publishing data streams in an RTC channel.
  168. *
  169. * The privileges for publishing audio, video, and data streams in an RTC channel apply only if you have
  170. * enabled co-host authentication.
  171. *
  172. * A user can have multiple privileges. Each privilege is valid for a maximum of 24 hours.
  173. * The SDK triggers the onTokenPrivilegeWillExpire and onRequestToken callbacks when the token is about to expire
  174. * or has expired. The callbacks do not report the specific privilege affected, and you need to maintain
  175. * the respective timestamp for each privilege in your app logic. After receiving the callback, you need
  176. * to generate a new token, and then call renewToken to pass the new token to the SDK, or call joinChannel to re-join
  177. * the channel.
  178. *
  179. * @note
  180. * Agora recommends setting a reasonable timestamp for each privilege according to your scenario.
  181. * Suppose the expiration timestamp for joining the channel is set earlier than that for publishing audio.
  182. * When the token for joining the channel expires, the user is immediately kicked off the RTC channel
  183. * and cannot publish any audio stream, even though the timestamp for publishing audio has not expired.
  184. *
  185. * @param appId The App ID of your Agora project.
  186. * @param appCertificate The App Certificate of your Agora project.
  187. * @param channelName The unique channel name for the Agora RTC session in string format. The string length must be less than 64 bytes. The channel name may contain the following characters:
  188. * - All lowercase English letters: a to z.
  189. * - All uppercase English letters: A to Z.
  190. * - All numeric characters: 0 to 9.
  191. * - The space character.
  192. * - "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">", "?", "@", "[", "]", "^", "_", " {", "}", "|", "~", ",".
  193. * @param account The user account.
  194. * @param joinChannelPrivilegeExpiredTs The Unix timestamp when the privilege for joining the channel expires, represented
  195. * by the sum of the current timestamp plus the valid time period of the token. For example, if you set joinChannelPrivilegeExpiredTs as the
  196. * current timestamp plus 600 seconds, the token expires in 10 minutes.
  197. * @param pubAudioPrivilegeExpiredTs The Unix timestamp when the privilege for publishing audio expires, represented
  198. * by the sum of the current timestamp plus the valid time period of the token. For example, if you set pubAudioPrivilegeExpiredTs as the
  199. * current timestamp plus 600 seconds, the token expires in 10 minutes. If you do not want to enable this privilege,
  200. * set pubAudioPrivilegeExpiredTs as the current Unix timestamp.
  201. * @param pubVideoPrivilegeExpiredTs The Unix timestamp when the privilege for publishing video expires, represented
  202. * by the sum of the current timestamp plus the valid time period of the token. For example, if you set pubVideoPrivilegeExpiredTs as the
  203. * current timestamp plus 600 seconds, the token expires in 10 minutes. If you do not want to enable this privilege,
  204. * set pubVideoPrivilegeExpiredTs as the current Unix timestamp.
  205. * @param pubDataStreamPrivilegeExpiredTs The Unix timestamp when the privilege for publishing data streams expires, represented
  206. * by the sum of the current timestamp plus the valid time period of the token. For example, if you set pubDataStreamPrivilegeExpiredTs as the
  207. * current timestamp plus 600 seconds, the token expires in 10 minutes. If you do not want to enable this privilege,
  208. * set pubDataStreamPrivilegeExpiredTs as the current Unix timestamp.
  209. */
  210. public String buildTokenWithUserAccount(String appId, String appCertificate,
  211. String channelName, String account, int joinChannelPrivilegeExpiredTs,
  212. int pubAudioPrivilegeExpiredTs, int pubVideoPrivilegeExpiredTs,
  213. int pubDataStreamPrivilegeExpiredTs) {
  214. // Assign appropriate access privileges to each role.
  215. AccessToken builder = new AccessToken(appId, appCertificate, channelName, account);
  216. builder.addPrivilege(AccessToken.Privileges.kJoinChannel, joinChannelPrivilegeExpiredTs);
  217. builder.addPrivilege(AccessToken.Privileges.kPublishAudioStream, pubAudioPrivilegeExpiredTs);
  218. builder.addPrivilege(AccessToken.Privileges.kPublishVideoStream, pubVideoPrivilegeExpiredTs);
  219. builder.addPrivilege(AccessToken.Privileges.kPublishDataStream, pubDataStreamPrivilegeExpiredTs);
  220. try {
  221. return builder.build();
  222. } catch (Exception e) {
  223. e.printStackTrace();
  224. return "";
  225. }
  226. }
  227. }