From 055f0d5c5bf7415853e77f8fa13d0ae484e1079a Mon Sep 17 00:00:00 2001 From: gxz Date: Mon, 19 Aug 2024 20:14:22 +0800 Subject: [PATCH] chore: optimize --- packages/core/src/IrisApiEngine.ts | 2 +- packages/rtc/src/engine/IrisClientManager.ts | 11 +- packages/rtc/src/engine/IrisClientObserver.ts | 35 ++- .../event_handler/IrisClientEventHandler.ts | 206 +++++++----------- .../rtc/src/impl/IAgoraRtcEngineExImpl.ts | 60 ++--- packages/rtc/src/impl/IAgoraRtcEngineImpl.ts | 8 +- packages/rtc/src/util/AgoraTool.ts | 14 ++ .../rtc/test/impl/IAgoraRtcEngineImpl.test.ts | 7 +- .../test/impl/IAgoraRtcEngineImplEx.test.ts | 140 ++++++------ packages/rtc/test/utils/index.ts | 12 +- 10 files changed, 216 insertions(+), 279 deletions(-) diff --git a/packages/core/src/IrisApiEngine.ts b/packages/core/src/IrisApiEngine.ts index 8a53a94..eb621fa 100644 --- a/packages/core/src/IrisApiEngine.ts +++ b/packages/core/src/IrisApiEngine.ts @@ -72,7 +72,7 @@ export interface ApiInterceptor { dispose(): Promise; } -class EventParam { +export class EventParam { constructor( event: string, data: string, diff --git a/packages/rtc/src/engine/IrisClientManager.ts b/packages/rtc/src/engine/IrisClientManager.ts index 99f7d4b..2989193 100644 --- a/packages/rtc/src/engine/IrisClientManager.ts +++ b/packages/rtc/src/engine/IrisClientManager.ts @@ -418,10 +418,7 @@ export class IrisClientManager { getIrisClientByConnection(connection: NATIVE_RTC.RtcConnection): IrisClient { if (connection) { return this.irisClientList.filter((irisClient: IrisClient) => { - if ( - irisClient.connection?.channelId == connection.channelId && - irisClient.connection?.localUid == connection.localUid - ) { + if (irisClient.connection?.channelId == connection.channelId) { return irisClient; } })[0]; @@ -540,10 +537,8 @@ export class IrisClientManager { } } - removeUserInfoByUserAccount(userAccount: string) { - const index = this.userInfoList.findIndex( - (user) => user.userAccount === userAccount - ); + removeUserInfoByUid(uid: number) { + const index = this.userInfoList.findIndex((user) => user.uid === uid); if (index !== -1) { this.userInfoList.splice(index, 1); } diff --git a/packages/rtc/src/engine/IrisClientObserver.ts b/packages/rtc/src/engine/IrisClientObserver.ts index b37bc5f..713d66a 100644 --- a/packages/rtc/src/engine/IrisClientObserver.ts +++ b/packages/rtc/src/engine/IrisClientObserver.ts @@ -1,5 +1,5 @@ import * as NATIVE_RTC from '@iris/native-rtc'; -import { ILocalTrack, IMicrophoneAudioTrack, ITrack } from 'agora-rtc-sdk-ng'; +import { ILocalTrack, IMicrophoneAudioTrack } from 'agora-rtc-sdk-ng'; import { IrisAudioSourceType } from '../base/BaseType'; import { @@ -374,7 +374,7 @@ export class IrisClientObserver { } } - subscribeVideoTrack(userPackage: RemoteUserPackage, force: boolean) { + async subscribeVideoTrack(userPackage: RemoteUserPackage, force: boolean) { let irisClient = this._engine.irisClientManager.getIrisClientByConnection( userPackage.connection ); @@ -392,15 +392,8 @@ export class IrisClientObserver { if (!user || !user.hasVideo) { return; } - irisClient.agoraRTCClient.subscribe(user, 'video').then(() => { + await irisClient.agoraRTCClient.subscribe(user, 'video').then(() => { AgoraConsole.debug('onEventUserPublished subscribe video success'); - if (userPackage.element) { - this._engine.trackHelper.play( - user!.videoTrack as ITrack, - userPackage.element, - userPackage.videoPlayerConfig - ); - } let param: IrisTrackEventHandlerParam = { client: irisClient.agoraRTCClient, remoteUser: user!, @@ -413,7 +406,7 @@ export class IrisClientObserver { }); } } - subscribeAudioTrack(userPackage: RemoteUserPackage, force: boolean) { + async subscribeAudioTrack(userPackage: RemoteUserPackage, force: boolean) { let irisClient = this._engine.irisClientManager.getIrisClientByConnection( userPackage.connection ); @@ -431,7 +424,7 @@ export class IrisClientObserver { if (!user || !user.hasAudio) { return; } - irisClient.agoraRTCClient.subscribe(user, 'audio').then(() => { + await irisClient.agoraRTCClient.subscribe(user, 'audio').then(() => { AgoraConsole.debug('onEventUserPublished subscribe audio success'); this._engine.trackHelper.play(user!.audioTrack!); let param: IrisTrackEventHandlerParam = { @@ -447,7 +440,7 @@ export class IrisClientObserver { } } - unsubscribeVideoTrack(userPackage: RemoteUserPackage, force: boolean) { + async unsubscribeVideoTrack(userPackage: RemoteUserPackage, force: boolean) { let irisClient = this._engine.irisClientManager.getIrisClientByConnection( userPackage.connection ); @@ -458,7 +451,7 @@ export class IrisClientObserver { if (!user || !user.videoTrack) { return; } - irisClient.agoraRTCClient.unsubscribe(user, 'video').then(() => { + await irisClient.agoraRTCClient.unsubscribe(user, 'video').then(() => { AgoraConsole.debug('onEventUserPublished unsubscribe video success'); this._engine.irisClientManager.removetrackEventHandlerByRemoteUser( user!, @@ -468,7 +461,7 @@ export class IrisClientObserver { } } - unsubscribeAudioTrack(userPackage: RemoteUserPackage, force: boolean) { + async unsubscribeAudioTrack(userPackage: RemoteUserPackage, force: boolean) { let irisClient = this._engine.irisClientManager.getIrisClientByConnection( userPackage.connection ); @@ -479,7 +472,7 @@ export class IrisClientObserver { if (!user || !user.audioTrack) { return; } - irisClient.agoraRTCClient.unsubscribe(user, 'audio').then(() => { + await irisClient.agoraRTCClient.unsubscribe(user, 'audio').then(() => { AgoraConsole.debug('onEventUserPublished unsubscribe audio success'); this._engine.irisClientManager.removetrackEventHandlerByRemoteUser( user!, @@ -489,7 +482,7 @@ export class IrisClientObserver { } } - notifyRemote( + async notifyRemote( type: NotifyRemoteType, scopePackages: RemoteUserPackage[], force: boolean = true @@ -498,22 +491,22 @@ export class IrisClientObserver { switch (type) { case NotifyRemoteType.SUBSCRIBE_VIDEO_TRACK: if (scopePackage) { - this.subscribeVideoTrack(scopePackage, force); + await this.subscribeVideoTrack(scopePackage, force); } break; case NotifyRemoteType.SUBSCRIBE_AUDIO_TRACK: if (scopePackage) { - this.subscribeAudioTrack(scopePackage, force); + await this.subscribeAudioTrack(scopePackage, force); } break; case NotifyRemoteType.UNSUBSCRIBE_AUDIO_TRACK: if (scopePackage) { - this.unsubscribeAudioTrack(scopePackage, force); + await this.unsubscribeAudioTrack(scopePackage, force); } break; case NotifyRemoteType.UNSUBSCRIBE_VIDEO_TRACK: if (scopePackage) { - this.unsubscribeVideoTrack(scopePackage, force); + await this.unsubscribeVideoTrack(scopePackage, force); } break; } diff --git a/packages/rtc/src/event_handler/IrisClientEventHandler.ts b/packages/rtc/src/event_handler/IrisClientEventHandler.ts index d6df1aa..23278fd 100644 --- a/packages/rtc/src/event_handler/IrisClientEventHandler.ts +++ b/packages/rtc/src/event_handler/IrisClientEventHandler.ts @@ -7,6 +7,7 @@ import { IAgoraRTCRemoteUser, NetworkQuality, RemoteStreamType, + UID, } from 'agora-rtc-sdk-ng'; import { IrisAudioSourceType } from '../base/BaseType'; @@ -16,7 +17,7 @@ import { RemoteUserPackage } from '../engine/IrisClientManager'; import { NotifyRemoteType } from '../engine/IrisClientObserver'; import { IrisRtcEngine } from '../engine/IrisRtcEngine'; -import { AgoraTranslate } from '../util/AgoraTranslate'; +import { AgoraTranslate, getUidFromRemoteUser } from '../util'; export class IrisClientEventHandler { private _irisClient: IrisClient; @@ -41,10 +42,6 @@ export class IrisClientEventHandler { 'user-unpublished', this.onEventUserUnpublished.bind(this) ); - this.agoraRTCClient.on( - 'user-info-updated', - this.onEventUserInfoUpdated.bind(this) - ); this.agoraRTCClient.on('stream-message', this.onStreamMessage.bind(this)); this.agoraRTCClient.on( 'media-reconnect-start', @@ -167,16 +164,15 @@ export class IrisClientEventHandler { channelId: this.agoraRTCClient.channelName, localUid: this.agoraRTCClient.uid as number, }; - let remoteUid = user.uid as number; + let remoteUid: number = getUidFromRemoteUser(user); let elapsed = 0; this._engine.rtcEngineEventHandler.onUserJoined_c5499bd( connection, remoteUid, elapsed ); - //@ts-ignore websdk的私有属性 - //如果是string uid 登录 - if (this.agoraRTCClient.isStringUID) { + //if joinchannel with string uid + if (typeof user.uid === 'string') { //@ts-ignore websdk的私有属性 let _uintid = user._uintid; let userInfo: NATIVE_RTC.UserInfo = { @@ -210,32 +206,33 @@ export class IrisClientEventHandler { } } - onEventUserLeft(user: IAgoraRTCRemoteUser, reason: string): void { + async onEventUserLeft( + user: IAgoraRTCRemoteUser, + reason: string + ): Promise { + let remoteUid: number = getUidFromRemoteUser(user); let remoteUser = this._engine.irisClientManager.getRemoteUserPackageByUid( - user.uid as number + remoteUid ); let reason2 = AgoraTranslate.string2NATIVE_RTCUSER_OFFLINE_REASON_TYPE( reason ); this._engine.rtcEngineEventHandler.onUserOffline_0a32aac( this._irisClient.connection, - user.uid as number, + remoteUid, reason2 ); - //@ts-ignore websdk的私有属性 - //如果是string uid 登录 - if (this.agoraRTCClient.isStringUID) { - this._engine.irisClientManager.removeUserInfoByUserAccount( - user.uid as string - ); + //if joinchannel with string uid + if (typeof user.uid === 'string') { + this._engine.irisClientManager.removeUserInfoByUid(remoteUid); } - this._engine.irisClientManager.irisClientObserver.notifyRemote( + await this._engine.irisClientManager.irisClientObserver.notifyRemote( NotifyRemoteType.UNSUBSCRIBE_AUDIO_TRACK, [remoteUser] ); - this._engine.irisClientManager.irisClientObserver.notifyRemote( + await this._engine.irisClientManager.irisClientObserver.notifyRemote( NotifyRemoteType.UNSUBSCRIBE_AUDIO_TRACK, [remoteUser] ); @@ -246,163 +243,118 @@ export class IrisClientEventHandler { ); } - onEventUserPublished( + async onEventUserPublished( user: IAgoraRTCRemoteUser, mediaType: 'audio' | 'video' - ): void { + ): Promise { + let remoteUid: number = getUidFromRemoteUser(user); + let isLocal = user.uid === this.agoraRTCClient.uid; let remoteUser = this._engine.irisClientManager.getRemoteUserPackageByUid( - user.uid as number + remoteUid ); if (remoteUser) { if (mediaType == 'audio') { - this._engine.irisClientManager.irisClientObserver.notifyRemote( + await this._engine.irisClientManager.irisClientObserver.notifyRemote( NotifyRemoteType.SUBSCRIBE_AUDIO_TRACK, [remoteUser], false ); + this._engine.rtcEngineEventHandler.onUserMuteAudio_0aac2fe( + this._irisClient.connection, + remoteUid, + false + ); + if (!isLocal) { + this._engine.rtcEngineEventHandler.onRemoteAudioStateChanged_056772e( + this._irisClient.connection, + remoteUid, + NATIVE_RTC.REMOTE_AUDIO_STATE.REMOTE_AUDIO_STATE_STARTING, + NATIVE_RTC.REMOTE_AUDIO_STATE_REASON + .REMOTE_AUDIO_REASON_REMOTE_UNMUTED, + 0 + ); + } } else if (mediaType == 'video') { - this._engine.irisClientManager.irisClientObserver.notifyRemote( + await this._engine.irisClientManager.irisClientObserver.notifyRemote( NotifyRemoteType.SUBSCRIBE_VIDEO_TRACK, [remoteUser], false ); + this._engine.rtcEngineEventHandler.onUserMuteVideo_0aac2fe( + this._irisClient.connection, + remoteUid, + false + ); + if (!isLocal) { + this._engine.rtcEngineEventHandler.onRemoteVideoStateChanged_a14e9d1( + this._irisClient.connection, + remoteUid, + NATIVE_RTC.REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_STARTING, + NATIVE_RTC.REMOTE_VIDEO_STATE_REASON + .REMOTE_VIDEO_STATE_REASON_REMOTE_UNMUTED, + 0 + ); + } } } } - onEventUserUnpublished( + async onEventUserUnpublished( user: IAgoraRTCRemoteUser, mediaType: 'audio' | 'video' - ): void { + ): Promise { + let remoteUid: number = getUidFromRemoteUser(user); let remoteUser = this._engine.irisClientManager.getRemoteUserPackageByUid( - user.uid as number + remoteUid ); if (remoteUser) { if (mediaType == 'audio') { - this._engine.irisClientManager.irisClientObserver.notifyRemote( + await this._engine.irisClientManager.irisClientObserver.notifyRemote( NotifyRemoteType.UNSUBSCRIBE_AUDIO_TRACK, [remoteUser] ); - } else if (mediaType == 'video') { - this._engine.irisClientManager.irisClientObserver.notifyRemote( - NotifyRemoteType.UNSUBSCRIBE_VIDEO_TRACK, - [remoteUser] - ); - } - } - } - - onEventUserInfoUpdated( - uid: number, - msg: - | 'mute-audio' - | 'mute-video' - | 'enable-local-video' - | 'unmute-audio' - | 'unmute-video' - | 'disable-local-video' - ): void { - //@ts-ignore websdk的私有属性 - //如果是string uid 登录 - if (this.agoraRTCClient.isStringUID) { - uid = this._engine.irisClientManager.getUserInfoByUid(uid)?.uid!; - } - switch (msg) { - case 'mute-audio': this._engine.rtcEngineEventHandler.onUserMuteAudio_0aac2fe( this._irisClient.connection, - uid, + remoteUid, true ); - this._engine.rtcEngineEventHandler.onUserStateChanged_65f95a7( - this._irisClient.connection, - uid, - NATIVE_RTC.REMOTE_USER_STATE.USER_STATE_MUTE_AUDIO - ); this._engine.rtcEngineEventHandler.onRemoteAudioStateChanged_056772e( this._irisClient.connection, - uid, + remoteUid, NATIVE_RTC.REMOTE_AUDIO_STATE.REMOTE_AUDIO_STATE_STOPPED, NATIVE_RTC.REMOTE_AUDIO_STATE_REASON.REMOTE_AUDIO_REASON_REMOTE_MUTED, 0 ); - break; - case 'mute-video': + } else if (mediaType == 'video') { + await this._engine.irisClientManager.irisClientObserver.notifyRemote( + NotifyRemoteType.UNSUBSCRIBE_VIDEO_TRACK, + [remoteUser] + ); this._engine.rtcEngineEventHandler.onUserMuteVideo_0aac2fe( this._irisClient.connection, - uid, + remoteUid, true ); - this._engine.rtcEngineEventHandler.onUserStateChanged_65f95a7( - this._irisClient.connection, - uid, - NATIVE_RTC.REMOTE_USER_STATE.USER_STATE_MUTE_VIDEO - ); this._engine.rtcEngineEventHandler.onRemoteVideoStateChanged_a14e9d1( this._irisClient.connection, - uid, + remoteUid, NATIVE_RTC.REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_STOPPED, NATIVE_RTC.REMOTE_VIDEO_STATE_REASON .REMOTE_VIDEO_STATE_REASON_REMOTE_MUTED, 0 ); - break; - case 'unmute-audio': - this._engine.rtcEngineEventHandler.onUserMuteAudio_0aac2fe( - this._irisClient.connection, - uid, - false - ); - this._engine.rtcEngineEventHandler.onRemoteAudioStateChanged_056772e( - this._irisClient.connection, - uid, - NATIVE_RTC.REMOTE_AUDIO_STATE.REMOTE_AUDIO_STATE_STARTING, - NATIVE_RTC.REMOTE_AUDIO_STATE_REASON - .REMOTE_AUDIO_REASON_REMOTE_UNMUTED, - 0 - ); - break; - case 'unmute-video': - this._engine.rtcEngineEventHandler.onUserMuteVideo_0aac2fe( - this._irisClient.connection, - uid, - false - ); - this._engine.rtcEngineEventHandler.onRemoteVideoStateChanged_a14e9d1( - this._irisClient.connection, - uid, - NATIVE_RTC.REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_STARTING, - NATIVE_RTC.REMOTE_VIDEO_STATE_REASON - .REMOTE_VIDEO_STATE_REASON_REMOTE_UNMUTED, - 0 - ); - break; - case 'enable-local-video': - this._engine.rtcEngineEventHandler.onUserEnableLocalVideo_0aac2fe( - this._irisClient.connection, - uid, - true - ); - this._engine.rtcEngineEventHandler.onUserStateChanged_65f95a7( - this._irisClient.connection, - uid, - NATIVE_RTC.REMOTE_USER_STATE.USER_STATE_ENABLE_LOCAL_VIDEO - ); - break; - case 'disable-local-video': - this._engine.rtcEngineEventHandler.onUserEnableLocalVideo_0aac2fe( - this._irisClient.connection, - uid, - false - ); - break; + } } } - onStreamMessage(uid: number, payload: Uint8Array): void { + onStreamMessage(uid: UID, payload: Uint8Array): void { + //if joinchannel with string uid + if (typeof uid === 'string') { + uid = this._engine.irisClientManager.getUserInfoByUserAccount(uid)?.uid!; + } this._engine.rtcEngineEventHandler.onStreamMessage_99898cb( this._irisClient.connection, - uid as number, + uid, this._engine.globalState.streamMessageStreamId, payload, payload.length, @@ -422,11 +374,15 @@ export class IrisClientEventHandler { } onEventStreamFallback( - uid: number, + uid: UID, isFallbackOrRecover: 'fallback' | 'recover' ): void { + //if joinchannel with string uid + if (typeof uid === 'string') { + uid = this._engine.irisClientManager.getUserInfoByUserAccount(uid)?.uid!; + } this._engine.rtcEngineEventHandler.onRemoteSubscribeFallbackToAudioOnly_dbdc15a( - uid as number, + uid, isFallbackOrRecover == 'fallback' ? true : false ); } diff --git a/packages/rtc/src/impl/IAgoraRtcEngineExImpl.ts b/packages/rtc/src/impl/IAgoraRtcEngineExImpl.ts index dcf2256..00f6279 100644 --- a/packages/rtc/src/impl/IAgoraRtcEngineExImpl.ts +++ b/packages/rtc/src/impl/IAgoraRtcEngineExImpl.ts @@ -2,11 +2,8 @@ import * as NATIVE_RTC from '@iris/native-rtc'; import { IMicrophoneAudioTrack, VideoPlayerConfig } from 'agora-rtc-sdk-ng'; import { CallApiReturnType, CallIrisApiResult } from 'iris-web-core'; -import { IrisAudioSourceType } from '../base/BaseType'; - import { defaultLeaveChannelOptions } from '../base/DefaultValue'; import { IrisClient } from '../engine/IrisClient'; -import { RemoteUserPackage } from '../engine/IrisClientManager'; import { NotifyRemoteType, NotifyType } from '../engine/IrisClientObserver'; @@ -155,40 +152,27 @@ export class IRtcEngineExImpl implements NATIVE_RTC.IRtcEngineEx { connection: NATIVE_RTC.RtcConnection ): CallApiReturnType { let processVideoTrack = async (): Promise => { - if (isDefined(canvas.uid)) { - let remoteUser = this._engine.irisClientManager.getRemoteUserPackageByUid( + if (isDefined(canvas.uid) && isDefined(canvas.view)) { + let remoteUserPackage = this._engine.irisClientManager.getRemoteUserPackageByUid( canvas.uid ); - if (remoteUser) { - remoteUser.element = canvas.view; - } else { - let config: VideoPlayerConfig = { - fit: AgoraTranslate.NATIVE_RTC_RENDER_MODE_TYPE2Fit( - canvas.renderMode - ? canvas.renderMode - : NATIVE_RTC.RENDER_MODE_TYPE.RENDER_MODE_FIT - ), - mirror: - canvas.mirrorMode === - NATIVE_RTC.VIDEO_MIRROR_MODE_TYPE.VIDEO_MIRROR_MODE_AUTO || - canvas.mirrorMode === - NATIVE_RTC.VIDEO_MIRROR_MODE_TYPE.VIDEO_MIRROR_MODE_ENABLED, - }; - let userPackage = new RemoteUserPackage( - connection, - canvas.view, - config, - canvas.uid, - NATIVE_RTC.VIDEO_SOURCE_TYPE.VIDEO_SOURCE_REMOTE, - IrisAudioSourceType.kAudioSourceTypeMicrophonePrimary - ); - let irisClient = this._engine.irisClientManager.getIrisClientByConnection( - connection - ); - this._engine.irisClientManager.addRemoteUserPackage( - userPackage, - irisClient?.agoraRTCClient! + if (remoteUserPackage) { + remoteUserPackage.element = canvas.view; + } + let irisClient = this._engine.irisClientManager.getIrisClientByConnection( + connection + ); + if (irisClient) { + let remoteUser = irisClient.agoraRTCClient?.remoteUsers.find( + (user) => user.uid === canvas.uid ); + if (remoteUser) { + this._engine.trackHelper.play( + remoteUser.videoTrack!, + remoteUserPackage.element, + remoteUserPackage.videoPlayerConfig + ); + } } return this._engine.returnResult(); } else { @@ -213,7 +197,7 @@ export class IRtcEngineExImpl implements NATIVE_RTC.IRtcEngineEx { let remoteUsers = remoteUserPackages.filter((userPackage) => { return userPackage.uid == uid; }); - this._engine.irisClientManager.irisClientObserver.notifyRemote( + await this._engine.irisClientManager.irisClientObserver.notifyRemote( mute ? NotifyRemoteType.UNSUBSCRIBE_AUDIO_TRACK : NotifyRemoteType.SUBSCRIBE_AUDIO_TRACK, @@ -251,7 +235,7 @@ export class IRtcEngineExImpl implements NATIVE_RTC.IRtcEngineEx { let remoteUserPackages = this._engine.irisClientManager.getRemoteUserPackagesByConnection( connection ); - this._engine.irisClientManager.irisClientObserver.notifyRemote( + await this._engine.irisClientManager.irisClientObserver.notifyRemote( mute ? NotifyRemoteType.UNSUBSCRIBE_AUDIO_TRACK : NotifyRemoteType.SUBSCRIBE_AUDIO_TRACK, @@ -276,7 +260,7 @@ export class IRtcEngineExImpl implements NATIVE_RTC.IRtcEngineEx { let remoteUsers = remoteUserPackages.filter((userPackage) => { return userPackage.uid == uid; }); - this._engine.irisClientManager.irisClientObserver.notifyRemote( + await this._engine.irisClientManager.irisClientObserver.notifyRemote( mute ? NotifyRemoteType.UNSUBSCRIBE_VIDEO_TRACK : NotifyRemoteType.SUBSCRIBE_VIDEO_TRACK, @@ -314,7 +298,7 @@ export class IRtcEngineExImpl implements NATIVE_RTC.IRtcEngineEx { let remoteUserPackages = this._engine.irisClientManager.getRemoteUserPackagesByConnection( connection ); - this._engine.irisClientManager.irisClientObserver.notifyRemote( + await this._engine.irisClientManager.irisClientObserver.notifyRemote( mute ? NotifyRemoteType.UNSUBSCRIBE_VIDEO_TRACK : NotifyRemoteType.SUBSCRIBE_VIDEO_TRACK, diff --git a/packages/rtc/src/impl/IAgoraRtcEngineImpl.ts b/packages/rtc/src/impl/IAgoraRtcEngineImpl.ts index 73f2ae5..1ff0174 100644 --- a/packages/rtc/src/impl/IAgoraRtcEngineImpl.ts +++ b/packages/rtc/src/impl/IAgoraRtcEngineImpl.ts @@ -710,7 +710,7 @@ export class IRtcEngineImpl implements IRtcEngineExtensions { muteAllRemoteVideoStreams_5039d15(mute: boolean): CallApiReturnType { let processFunc = async (): Promise => { - this._engine.irisClientManager.irisClientObserver.notifyRemote( + await this._engine.irisClientManager.irisClientObserver.notifyRemote( mute ? NotifyRemoteType.UNSUBSCRIBE_VIDEO_TRACK : NotifyRemoteType.SUBSCRIBE_VIDEO_TRACK, @@ -727,7 +727,7 @@ export class IRtcEngineImpl implements IRtcEngineExtensions { let remoteUserPackage = this._engine.irisClientManager.getRemoteUserPackageByUid( uid ); - this._engine.irisClientManager.irisClientObserver.notifyRemote( + await this._engine.irisClientManager.irisClientObserver.notifyRemote( mute ? NotifyRemoteType.UNSUBSCRIBE_VIDEO_TRACK : NotifyRemoteType.SUBSCRIBE_VIDEO_TRACK, @@ -742,7 +742,7 @@ export class IRtcEngineImpl implements IRtcEngineExtensions { muteAllRemoteAudioStreams_5039d15(mute: boolean): CallApiReturnType { let processFunc = async (): Promise => { - this._engine.irisClientManager.irisClientObserver.notifyRemote( + await this._engine.irisClientManager.irisClientObserver.notifyRemote( mute ? NotifyRemoteType.UNSUBSCRIBE_AUDIO_TRACK : NotifyRemoteType.SUBSCRIBE_AUDIO_TRACK, @@ -759,7 +759,7 @@ export class IRtcEngineImpl implements IRtcEngineExtensions { let remoteUserPackage = this._engine.irisClientManager.getRemoteUserPackageByUid( uid ); - this._engine.irisClientManager.irisClientObserver.notifyRemote( + await this._engine.irisClientManager.irisClientObserver.notifyRemote( mute ? NotifyRemoteType.UNSUBSCRIBE_AUDIO_TRACK : NotifyRemoteType.SUBSCRIBE_AUDIO_TRACK, diff --git a/packages/rtc/src/util/AgoraTool.ts b/packages/rtc/src/util/AgoraTool.ts index fde71db..f39a0ee 100644 --- a/packages/rtc/src/util/AgoraTool.ts +++ b/packages/rtc/src/util/AgoraTool.ts @@ -1,3 +1,5 @@ +import { IAgoraRTCRemoteUser } from 'agora-rtc-sdk-ng'; + export function mergeArray(dest: Array, src: Array) { for (let i = 0; i < src.length; i++) { dest.push(src[i]); @@ -32,3 +34,15 @@ export function downloadCanvasAsImage(canvas: any, fileName: string) { export function isDefined(value: T | undefined | null): value is T { return value !== undefined && value !== null; } + +export function getUidFromRemoteUser(user: IAgoraRTCRemoteUser): number { + let remoteUid: number; + //if joinchannel with string uid + if (typeof user.uid === 'string') { + //@ts-ignore websdk private property + remoteUid = user._uintid; + } else { + remoteUid = user.uid; + } + return remoteUid; +} diff --git a/packages/rtc/test/impl/IAgoraRtcEngineImpl.test.ts b/packages/rtc/test/impl/IAgoraRtcEngineImpl.test.ts index 3c9b0fa..4facf70 100644 --- a/packages/rtc/test/impl/IAgoraRtcEngineImpl.test.ts +++ b/packages/rtc/test/impl/IAgoraRtcEngineImpl.test.ts @@ -3,12 +3,7 @@ import { FakeAgoraRTCWrapper, } from '@agoraio-extensions/agora-rtc-sdk-ng-fake'; import * as NATIVE_RTC from '@iris/native-rtc'; -import { - AREAS, - IAgoraRTC, - ILocalAudioTrack, - ILocalTrack, -} from 'agora-rtc-sdk-ng'; +import { AREAS, IAgoraRTC, ILocalTrack } from 'agora-rtc-sdk-ng'; import { IrisApiEngine, IrisCore } from 'iris-web-core'; diff --git a/packages/rtc/test/impl/IAgoraRtcEngineImplEx.test.ts b/packages/rtc/test/impl/IAgoraRtcEngineImplEx.test.ts index e13740c..7d48306 100644 --- a/packages/rtc/test/impl/IAgoraRtcEngineImplEx.test.ts +++ b/packages/rtc/test/impl/IAgoraRtcEngineImplEx.test.ts @@ -5,7 +5,7 @@ import { import * as NATIVE_RTC from '@iris/native-rtc'; import { AREAS, IAgoraRTC, ILocalTrack } from 'agora-rtc-sdk-ng'; -import { IrisApiEngine, IrisCore } from 'iris-web-core'; +import { EventParam, IrisApiEngine, IrisCore } from 'iris-web-core'; import { IrisWebRtc } from '../../src/IrisRtcApi'; import { IrisAudioSourceType } from '../../src/base/BaseType'; @@ -31,6 +31,7 @@ let rtcEngineExImpl: IRtcEngineExImpl; beforeEach(async () => { apiEnginePtr = IrisCore.createIrisApiEngine(); + IrisCore.createIrisEventHandler({} as any); IrisWebRtc.initIrisRtc(apiEnginePtr, { agoraRTC: FakeAgoraRTCWrapper.getFakeAgoraRTC(), }); @@ -176,51 +177,53 @@ describe('IAgoraRtcEngineImpl', () => { expect(agoraRTCClient?.setClientRole).toBeCalledTimes(1); }); test('setupRemoteVideoEx_522a409', async () => { - let param = { - token: '1234', - connection: { - channelId: FAKE_CHANNEL_NAME, - localUid: TEST_UID, - }, - options: { - channelProfile: - NATIVE_RTC.CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING, - clientRoleType: NATIVE_RTC.CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER, - }, - }; - let connection = await callIris( + await callIris( apiEnginePtr, - 'RtcEngineEx_joinChannelEx_a3cd08c', - param + 'RtcEngine_registerEventHandler_5fc0465', + null, + [ + { + onEvent: async (e: EventParam) => { + if ( + e.event === + 'RtcEngineEventHandler_onRemoteVideoStateChanged_a14e9d1' + ) { + await callIris( + apiEnginePtr, + 'RtcEngineEx_setupRemoteVideoEx_522a409', + param2 + ); + expect( + irisRtcEngine.irisClientManager.remoteUserPackages.length + ).toBe(1); + expect( + irisRtcEngine.irisClientManager.remoteUserPackages[0].element + ).toBe(param2.canvas.view); + expect( + irisRtcEngine.irisClientManager.remoteUserPackages[0].uid + ).toBe(param2.canvas.uid); + expect( + irisRtcEngine.irisClientManager.remoteUserPackages[0].connection + .channelId + ).toBe(param2.connection.channelId); + expect( + irisRtcEngine.irisClientManager.remoteUserPackages[0].connection + .localUid + ).toBe(param2.connection.localUid); + } + }, + }, + ] ); + let connection = await joinChannelEx(apiEnginePtr); let param2 = { canvas: { uid: TEST_REMOTE_UID, view: 'test-view', sourceType: NATIVE_RTC.VIDEO_SOURCE_TYPE.VIDEO_SOURCE_REMOTE, }, - connection: param.connection, + connection: connection, }; - await callIris( - apiEnginePtr, - 'RtcEngineEx_setupRemoteVideoEx_522a409', - param2 - ); - expect(irisRtcEngine.irisClientManager.remoteUserPackages.length).toBe(1); - expect(irisRtcEngine.irisClientManager.remoteUserPackages[0].element).toBe( - param2.canvas.view - ); - expect(irisRtcEngine.irisClientManager.remoteUserPackages[0].uid).toBe( - param2.canvas.uid - ); - expect( - irisRtcEngine.irisClientManager.remoteUserPackages[0].connection.channelId - ).toBe(param2.connection.channelId); - expect( - irisRtcEngine.irisClientManager.remoteUserPackages[0].connection.localUid - ).toBe(param2.connection.localUid); - await callIris(apiEnginePtr, 'RtcEngineEx_leaveChannelEx_c81e1a4', param); - expect(irisRtcEngine.irisClientManager.remoteUserPackages.length).toBe(0); }); test('muteLocalAudioStreamEx_3cf17a4', async () => { let connection = await joinChannelEx(apiEnginePtr); @@ -329,45 +332,39 @@ describe('IAgoraRtcEngineImpl', () => { }); expect((localVideoTrackPackage[0].track as ILocalTrack).muted).toBe(false); }); - test('muteAllRemoteVideoStreamsEx_3cf17a4', async () => { + test('muteRemoteVideoStreamEx_6d93082', async () => { await callIris(apiEnginePtr, 'RtcEngine_enableVideo', null); let connection = await joinChannelEx(apiEnginePtr); await setupRemoteVideoEx(apiEnginePtr, null); - let irisClient = irisRtcEngine.irisClientManager.getIrisClientByConnection( - connection - ); - let remoteUsers = irisClient.agoraRTCClient!.remoteUsers; - expect(remoteUsers[0].videoTrack?.isPlaying).toBe(false); - - await callIris( - apiEnginePtr, - 'RtcEngineEx_muteAllRemoteVideoStreamsEx_3cf17a4', - { - mute: true, - connection, - } - ); - expect(remoteUsers[0].videoTrack).toBeUndefined(); await callIris( apiEnginePtr, - 'RtcEngineEx_muteAllRemoteVideoStreamsEx_3cf17a4', - { - mute: false, - connection, - } + 'RtcEngine_registerEventHandler_5fc0465', + null, + [ + { + onEvent: async (e: EventParam) => { + if ( + e.event === + 'RtcEngineEventHandler_onRemoteVideoStateChanged_a14e9d1' + ) { + let irisClient = irisRtcEngine.irisClientManager.getIrisClientByConnection( + connection + ); + let remoteUsers = irisClient.agoraRTCClient!.remoteUsers; + if ( + (JSON.parse(e.data).state as NATIVE_RTC.REMOTE_VIDEO_STATE) === + NATIVE_RTC.REMOTE_VIDEO_STATE.REMOTE_VIDEO_STATE_STARTING + ) { + expect(remoteUsers[0].videoTrack).toBeUndefined(); + } else { + expect(remoteUsers[0].videoTrack).not.toBeUndefined(); + expect(remoteUsers[0].videoTrack?.isPlaying).toBe(true); + } + } + }, + }, + ] ); - expect(remoteUsers[0].videoTrack).not.toBeUndefined(); - expect(remoteUsers[0].videoTrack?.isPlaying).toBe(true); - }); - test('muteRemoteVideoStreamEx_6d93082', async () => { - await callIris(apiEnginePtr, 'RtcEngine_enableVideo', null); - let connection = await joinChannelEx(apiEnginePtr); - await setupRemoteVideoEx(apiEnginePtr, null); - let irisClient = irisRtcEngine.irisClientManager.getIrisClientByConnection( - connection - ); - let remoteUsers = irisClient.agoraRTCClient!.remoteUsers; - expect(remoteUsers[0].videoTrack?.isPlaying).toBe(false); await callIris( apiEnginePtr, @@ -378,7 +375,6 @@ describe('IAgoraRtcEngineImpl', () => { connection, } ); - expect(remoteUsers[0].videoTrack).toBeUndefined(); await callIris( apiEnginePtr, 'RtcEngineEx_muteRemoteVideoStreamEx_6d93082', @@ -388,8 +384,6 @@ describe('IAgoraRtcEngineImpl', () => { connection, } ); - expect(remoteUsers[0].videoTrack).not.toBeUndefined(); - expect(remoteUsers[0].videoTrack?.isPlaying).toBe(true); }); test('createDataStreamEx_9f641b6', async () => { diff --git a/packages/rtc/test/utils/index.ts b/packages/rtc/test/utils/index.ts index 3f9252a..3b415ee 100644 --- a/packages/rtc/test/utils/index.ts +++ b/packages/rtc/test/utils/index.ts @@ -26,7 +26,8 @@ export const TEST_REMOTE_UID = 456; export async function callIris( apiEnginePtr: IrisApiEngine, apiName: string, - options: any = null + options: any = null, + buffers: any[] = ['test'] ) { let result = await IrisCore.callIrisApi( apiEnginePtr, @@ -35,7 +36,7 @@ export async function callIris( JSON.stringify(options), 0, '', - ['test'], + buffers, [], 1 ) @@ -115,6 +116,8 @@ export async function joinChannel(apiEnginePtr: IrisApiEngine, options?: any) { } as IAgoraRTCRemoteUser, ]; dispatchRTCEvent(agoraRTCClient, 'user-joined', user[0]); + dispatchRTCEvent(agoraRTCClient, 'user-published', user[0], 'video'); + dispatchRTCEvent(agoraRTCClient, 'user-published', user[0], 'audio'); agoraRTCClient.remoteUsers = user; } @@ -163,6 +166,8 @@ export async function joinChannelWithUserAccount( } as IAgoraRTCRemoteUser, ]; dispatchRTCEvent(agoraRTCClient, 'user-joined', user[0]); + dispatchRTCEvent(agoraRTCClient, 'user-published', user[0], 'video'); + dispatchRTCEvent(agoraRTCClient, 'user-published', user[0], 'audio'); agoraRTCClient.remoteUsers = user; } @@ -200,8 +205,9 @@ export async function joinChannelEx( videoTrack: FakeLocalVideoTrack.create(), } as IAgoraRTCRemoteUser, ]; - // triggerCallback(irisRtcEngine, 'onEventUserJoined', user); dispatchRTCEvent(agoraRTCClient, 'user-joined', user[0]); + dispatchRTCEvent(agoraRTCClient, 'user-published', user[0], 'video'); + dispatchRTCEvent(agoraRTCClient, 'user-published', user[0], 'audio'); agoraRTCClient.remoteUsers = user; irisClient.connection.localUid = agoraRTCClient.uid; return irisClient.connection;