diff --git a/packages/flat-server-api/src/room.ts b/packages/flat-server-api/src/room.ts index 59439f4c4d6..82a94646cfe 100644 --- a/packages/flat-server-api/src/room.ts +++ b/packages/flat-server-api/src/room.ts @@ -103,6 +103,7 @@ export interface JoinRoomResult { rtmToken: string; showGuide: boolean; region: Region; + agoraAppId?: string; billing?: { /** minutes */ limit: number; diff --git a/packages/flat-services/src/services/text-chat/text-chat.ts b/packages/flat-services/src/services/text-chat/text-chat.ts index e193384d436..e21e745e063 100644 --- a/packages/flat-services/src/services/text-chat/text-chat.ts +++ b/packages/flat-services/src/services/text-chat/text-chat.ts @@ -14,6 +14,7 @@ export interface IServiceTextChatJoinRoomConfig { ownerUUID: string; uid: string; token?: string | null; + agoraAppId?: string | null; } export abstract class IServiceTextChat implements IService { diff --git a/packages/flat-services/src/services/video-chat/video-chat.ts b/packages/flat-services/src/services/video-chat/video-chat.ts index 78ea444061b..33909a0f5ce 100644 --- a/packages/flat-services/src/services/video-chat/video-chat.ts +++ b/packages/flat-services/src/services/video-chat/video-chat.ts @@ -30,6 +30,7 @@ export interface IServiceVideoChatJoinRoomConfig { shareScreenUID: IServiceVideoChatUID; shareScreenToken: string; mirror?: boolean; + agoraAppId?: string | null; } export abstract class IServiceVideoChat implements IService { diff --git a/packages/flat-stores/src/classroom-store/index.ts b/packages/flat-stores/src/classroom-store/index.ts index 7dfe418d54e..b21e601f3cc 100644 --- a/packages/flat-stores/src/classroom-store/index.ts +++ b/packages/flat-stores/src/classroom-store/index.ts @@ -376,6 +376,7 @@ export class ClassroomStore { ownerUUID: this.ownerUUID, uid: this.userUUID, token: globalStore.rtmToken, + agoraAppId: globalStore.agoraAppId, }); const fastboard = await this.whiteboardStore.joinWhiteboardRoom(); @@ -1508,6 +1509,7 @@ export class ClassroomStore { shareScreenUID: String(globalStore.rtcShareScreen?.uid || -1), shareScreenToken: globalStore.rtcShareScreen?.token || "", mirror: preferencesStore.mirrorMode, + agoraAppId: globalStore.agoraAppId, }); if (preferencesStore.cameraId) { diff --git a/packages/flat-stores/src/global-store.ts b/packages/flat-stores/src/global-store.ts index c4e013b7d49..7b9dec6aef8 100644 --- a/packages/flat-stores/src/global-store.ts +++ b/packages/flat-stores/src/global-store.ts @@ -67,6 +67,7 @@ export class GlobalStore { * Room's region, services (currently only whiteboard) must use this value to join the room. */ public region: Region | null = null; + public agoraAppId: string | null = null; public rtcToken: string | null = null; public rtcUID: number | null = null; public rtcShareScreen: { @@ -219,6 +220,7 @@ export class GlobalStore { | "rtcUID" | "rtcShareScreen" | "region" + | "agoraAppId" > >, ): void => { @@ -230,6 +232,7 @@ export class GlobalStore { "rtcUID", "rtcShareScreen", "region", + "agoraAppId", ] as const; for (const key of keys) { const value = config[key]; diff --git a/packages/flat-stores/src/room-store.ts b/packages/flat-stores/src/room-store.ts index 05820dab37c..a990450aedb 100644 --- a/packages/flat-stores/src/room-store.ts +++ b/packages/flat-stores/src/room-store.ts @@ -57,6 +57,7 @@ export interface RoomItem { expireAt: number; vipLevel: 0 | 1; }; + agoraAppId?: string; } // Only keep sub-room ids. sub-room info are stored in ordinaryRooms. @@ -144,6 +145,7 @@ export class RoomStore { ownerUUID: data.ownerUUID, roomType: data.roomType, billing: data.billing, + agoraAppId: data.agoraAppId, }); return data; } diff --git a/service-providers/agora-rtc/agora-rtc-electron/src/agora-rtc-electron.ts b/service-providers/agora-rtc/agora-rtc-electron/src/agora-rtc-electron.ts index 999ff1b37ab..9c54ee069a5 100644 --- a/service-providers/agora-rtc/agora-rtc-electron/src/agora-rtc-electron.ts +++ b/service-providers/agora-rtc/agora-rtc-electron/src/agora-rtc-electron.ts @@ -32,11 +32,12 @@ export class AgoraRTCElectron extends IServiceVideoChat { public readonly isMac: boolean; public readonly shareScreen = new AgoraRTCElectronShareScreen({ rtc: this }); - public readonly APP_ID: string; public readonly rtcEngine: AgoraSdk; private readonly _roomSideEffect = new SideEffectManager(); + public APP_ID: string; + private _cameraID?: string; private _micID?: string; private _speakerID?: string; @@ -203,6 +204,12 @@ export class AgoraRTCElectron extends IServiceVideoChat { } this.leaveRoom(); } + + if (config.agoraAppId && config.agoraAppId !== this.APP_ID) { + this.rtcEngine.release(true); + this.rtcEngine.initialize((this.APP_ID = config.agoraAppId)); + } + return this._join(config); } diff --git a/service-providers/agora-rtc/agora-rtc-web/src/agora-rtc-web.ts b/service-providers/agora-rtc/agora-rtc-web/src/agora-rtc-web.ts index 4028fa60ac3..930ce70cab8 100644 --- a/service-providers/agora-rtc/agora-rtc-web/src/agora-rtc-web.ts +++ b/service-providers/agora-rtc/agora-rtc-web/src/agora-rtc-web.ts @@ -42,8 +42,6 @@ export interface AgoraRTCWebConfig { } export class AgoraRTCWeb extends IServiceVideoChat { - public readonly APP_ID: string; - public readonly shareScreen: AgoraRTCWebShareScreen; private readonly _roomSideEffect = new SideEffectManager(); @@ -52,6 +50,7 @@ export class AgoraRTCWeb extends IServiceVideoChat { private _pLeavingRoom?: Promise; private _testingAudio?: HTMLAudioElement; + public APP_ID: string; public client?: IAgoraRTCClient; public mode?: IServiceVideoChatMode; @@ -358,6 +357,7 @@ export class AgoraRTCWeb extends IServiceVideoChat { shareScreenUID, shareScreenToken, mirror, + agoraAppId, }: IServiceVideoChatJoinRoomConfig): Promise { this._roomSideEffect.flushAll(); @@ -494,6 +494,10 @@ export class AgoraRTCWeb extends IServiceVideoChat { }), ); + if (agoraAppId && agoraAppId !== this.APP_ID) { + this.APP_ID = agoraAppId; + } + await client.join( this.APP_ID, roomUUID, diff --git a/service-providers/agora-rtm/src/rtm.ts b/service-providers/agora-rtm/src/rtm.ts index 78729b7e5a1..4024c39482e 100644 --- a/service-providers/agora-rtm/src/rtm.ts +++ b/service-providers/agora-rtm/src/rtm.ts @@ -24,14 +24,14 @@ export class AgoraRTM extends IServiceTextChat { private _pJoiningRoom?: Promise; private _pLeavingRoom?: Promise; - public readonly client: RtmClient; + public client: RtmClient; public channel?: RtmChannel; private roomUUID?: string; private userUUID?: string; private token?: string; - public constructor(APP_ID: string) { + public constructor(public APP_ID: string) { super(); if (!APP_ID) { throw new Error("APP_ID is not set"); @@ -185,6 +185,7 @@ export class AgoraRTM extends IServiceTextChat { token, roomUUID, ownerUUID, + agoraAppId, }: IServiceTextChatJoinRoomConfig): Promise { this.token = token || (await generateRTMToken()); @@ -192,6 +193,13 @@ export class AgoraRTM extends IServiceTextChat { throw new Error("Missing Agora RTM token"); } + if (agoraAppId && agoraAppId !== this.APP_ID) { + this.APP_ID = agoraAppId; + this.client = RtmEngine.createInstance(this.APP_ID, { + logFilter: RtmEngine.LOG_FILTER_WARNING, + }); + } + this._roomSideEffect.add(() => { const handler = async (): Promise => { this.token = await generateRTMToken(); diff --git a/service-providers/agora-rtm2/src/rtm2.ts b/service-providers/agora-rtm2/src/rtm2.ts index e990127d2a1..8b45ad7aa5d 100644 --- a/service-providers/agora-rtm2/src/rtm2.ts +++ b/service-providers/agora-rtm2/src/rtm2.ts @@ -28,7 +28,7 @@ export class AgoraRTM2 extends IServiceTextChat { private userUUID?: string; private token?: string; - public constructor(private readonly APP_ID: string) { + public constructor(public APP_ID: string) { super(); if (!APP_ID) { throw new Error("APP_ID is not set"); @@ -162,6 +162,7 @@ export class AgoraRTM2 extends IServiceTextChat { token, roomUUID, ownerUUID, + agoraAppId, }: IServiceTextChatJoinRoomConfig): Promise { this.token = token || (await generateRTMToken()); @@ -169,6 +170,10 @@ export class AgoraRTM2 extends IServiceTextChat { throw new Error("Missing Agora RTM token"); } + if (agoraAppId && agoraAppId !== this.APP_ID) { + this.APP_ID = agoraAppId; + } + const client = (this.client = new RTM(this.APP_ID, uid, { logLevel: "warn", logUpload: !process.env.DEV,