6524a1ade3f9fbd9b0909232f80b1f9a5f9b4dbf.svn-base 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package io.agora.media;
  2. public class RtcTokenBuilder2 {
  3. public enum Role {
  4. ROLE_PUBLISHER(1),
  5. ROLE_SUBSCRIBER(2),
  6. ;
  7. public int initValue;
  8. Role(int initValue) {
  9. this.initValue = initValue;
  10. }
  11. }
  12. /**
  13. * Build the RTC token with uid.
  14. *
  15. * @param appId: The App ID issued to you by Agora. Apply for a new App ID from
  16. * Agora Dashboard if it is missing from your kit. See Get an App ID.
  17. * @param appCertificate: Certificate of the application that you registered in
  18. * the Agora Dashboard. See Get an App Certificate.
  19. * @param channelName: Unique channel name for the AgoraRTC session in the string format
  20. * @param uid: User ID. A 32-bit unsigned integer with a value ranging from 1 to (232-1).
  21. * optionalUid must be unique.
  22. * @param role: ROLE_PUBLISHER: A broadcaster/host in a live-broadcast profile.
  23. * ROLE_SUBSCRIBER: An audience(default) in a live-broadcast profile.
  24. * @param expire: represented by the number of seconds elapsed since now. If, for example, you want to access the
  25. * Agora Service within 10 minutes after the token is generated, set expireTimestamp as 600(seconds).
  26. * @return The RTC token.
  27. */
  28. public String buildTokenWithUid(String appId, String appCertificate, String channelName, int uid, Role role, int expire) {
  29. return buildTokenWithAccount(appId, appCertificate, channelName, AccessToken2.getUidStr(uid), role, expire);
  30. }
  31. /**
  32. * Build the RTC token with account.
  33. *
  34. * @param appId: The App ID issued to you by Agora. Apply for a new App ID from
  35. * Agora Dashboard if it is missing from your kit. See Get an App ID.
  36. * @param appCertificate: Certificate of the application that you registered in
  37. * the Agora Dashboard. See Get an App Certificate.
  38. * @param channelName: Unique channel name for the AgoraRTC session in the string format
  39. * @param account: The user's account, max length is 255 Bytes.
  40. * @param role: ROLE_PUBLISHER: A broadcaster/host in a live-broadcast profile.
  41. * ROLE_SUBSCRIBER: An audience(default) in a live-broadcast profile.
  42. * @param expire: represented by the number of seconds elapsed since now. If, for example, you want to access the
  43. * Agora Service within 10 minutes after the token is generated, set expireTimestamp as 600(seconds).
  44. * @return The RTC token.
  45. */
  46. public String buildTokenWithAccount(String appId, String appCertificate, String channelName, String account, Role role, int expire) {
  47. AccessToken2 accessToken = new AccessToken2(appId, appCertificate, expire);
  48. AccessToken2.Service serviceRtc = new AccessToken2.ServiceRtc(channelName, account);
  49. serviceRtc.addPrivilegeRtc(AccessToken2.PrivilegeRtc.PRIVILEGE_JOIN_CHANNEL, expire);
  50. if (role == Role.ROLE_PUBLISHER) {
  51. serviceRtc.addPrivilegeRtc(AccessToken2.PrivilegeRtc.PRIVILEGE_PUBLISH_AUDIO_STREAM, expire);
  52. serviceRtc.addPrivilegeRtc(AccessToken2.PrivilegeRtc.PRIVILEGE_PUBLISH_VIDEO_STREAM, expire);
  53. serviceRtc.addPrivilegeRtc(AccessToken2.PrivilegeRtc.PRIVILEGE_PUBLISH_DATA_STREAM, expire);
  54. }
  55. accessToken.addService(serviceRtc);
  56. try {
  57. return accessToken.build();
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. return "";
  61. }
  62. }
  63. }