Skip to content

Commit

Permalink
fix(web): should check remote tracks when creating avatar (#954)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrious authored Sep 17, 2021
1 parent 068683f commit 332cd17
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
29 changes: 29 additions & 0 deletions web/flat-web/src/apiMiddleware/rtc/avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class RtcAvatar extends EventEmitter {
this.avatarUser = avatarUser;
this.isLocal = userUUID === avatarUser.userUUID;
this.rtc.addAvatar(this);
this.prepareRemoteTracks();
this.observeVolumeId = window.setInterval(this.checkVolume, 500);
}

Expand All @@ -66,6 +67,18 @@ export class RtcAvatar extends EventEmitter {
return this.rtc.client!;
}

public prepareRemoteTracks(): void {
this.remoteUser = this.client.remoteUsers.find(user => user.uid === this.avatarUser.rtcUID);
if (this.remoteUser) {
if (this.remoteUser.videoTrack) {
this.pendingSetCamera = { promise: Promise.resolve() };
}
if (this.remoteUser.audioTrack) {
this.pendingSetMic = { promise: Promise.resolve() };
}
}
}

public refreshRemoteTracks(): void {
this.remoteUser = this.client.remoteUsers.find(user => user.uid === this.avatarUser.rtcUID);
if (!this.remoteUser) {
Expand Down Expand Up @@ -130,6 +143,22 @@ export class RtcAvatar extends EventEmitter {
private isPendingCamera = false;
private isPendingMic = false;

public onSubscribeCamera(): void {
if (!this.pendingSetCamera) {
this.pendingSetCamera = { promise: Promise.resolve() };
} else {
this.pendingSetCamera.resolve?.();
}
}

public onSubscribeMic(): void {
if (!this.pendingSetMic) {
this.pendingSetMic = { promise: Promise.resolve() };
} else {
this.pendingSetMic.resolve?.();
}
}

public async setCamera(enable: boolean): Promise<void> {
if (this.isPendingCamera) {
return;
Expand Down
30 changes: 14 additions & 16 deletions web/flat-web/src/apiMiddleware/rtc/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,22 +147,7 @@ export class RtcRoom {
}
console.log("[rtc] subscribe uid=%O, media=%O", user.uid, mediaType);
await this.client?.subscribe(user, mediaType);
this.avatars.forEach(avatar => {
if (mediaType === "video") {
if (!avatar.pendingSetCamera) {
avatar.pendingSetCamera = { promise: Promise.resolve() };
} else {
avatar.pendingSetCamera.resolve?.();
}
}
if (mediaType === "audio") {
if (!avatar.pendingSetMic) {
avatar.pendingSetMic = { promise: Promise.resolve() };
} else {
avatar.pendingSetMic.resolve?.();
}
}
});
this.refreshAvatar(user, mediaType);
};

private onUserUnpublished = async (
Expand All @@ -184,4 +169,17 @@ export class RtcRoom {
await this.client.renewToken(token);
}
};

private refreshAvatar(user: IAgoraRTCRemoteUser, mediaType: string): void {
this.avatars.forEach(avatar => {
if (avatar.avatarUser.rtcUID === user.uid) {
if (mediaType === "video") {
avatar.onSubscribeCamera();
}
if (mediaType === "audio") {
avatar.onSubscribeMic();
}
}
});
}
}

0 comments on commit 332cd17

Please sign in to comment.