Skip to content

Commit 650a4e1

Browse files
authored
fix(flat-rtc): track web rtc delayed remote video track (#1540)
1 parent f7ddba8 commit 650a4e1

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

services/rtc/flat-rtc-agora-web/src/flat-rtc-agora-web.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,15 @@ export class FlatRTCAgoraWeb extends FlatRTC<FlatRTCAgoraWebUIDType> {
141141
}
142142

143143
if (this.client) {
144-
this._remoteAvatars.forEach(avatar => avatar.destroy());
145-
this._remoteAvatars.clear();
144+
try {
145+
this._remoteAvatars.forEach(avatar => avatar.destroy());
146+
this._remoteAvatars.clear();
146147

147-
if (this._localAvatar) {
148-
this._localAvatar.destroy();
149-
this._localAvatar = undefined;
150-
}
148+
if (this._localAvatar) {
149+
this._localAvatar.destroy();
150+
this._localAvatar = undefined;
151+
}
151152

152-
try {
153153
if (this.localCameraTrack) {
154154
this.localCameraTrack.stop();
155155
this.localCameraTrack.close();
@@ -160,12 +160,12 @@ export class FlatRTCAgoraWeb extends FlatRTC<FlatRTCAgoraWebUIDType> {
160160
this.localMicTrack.close();
161161
this.localMicTrack = undefined;
162162
}
163+
164+
this._roomSideEffect.flushAll();
163165
} catch (e) {
164-
console.error(e);
166+
// ignored
165167
}
166168

167-
this._roomSideEffect.flushAll();
168-
169169
this._pLeavingRoom = this.client.leave();
170170
await this._pLeavingRoom;
171171
this._pLeavingRoom = undefined;
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SideEffectManager } from "side-effect-manager";
22
import { combine, Val } from "value-enhancer";
33
import type { FlatRTCAvatar } from "@netless/flat-rtc";
4-
import type { IAgoraRTCRemoteUser } from "agora-rtc-sdk-ng";
4+
import type { IAgoraRTCRemoteUser, IRemoteAudioTrack, IRemoteVideoTrack } from "agora-rtc-sdk-ng";
55

66
export interface RTCRemoteAvatarConfig {
77
rtcRemoteUser?: IAgoraRTCRemoteUser;
@@ -15,7 +15,8 @@ export class RTCRemoteAvatar implements FlatRTCAvatar {
1515
private readonly _shouldMic$ = new Val(false);
1616

1717
private readonly _el$: Val<HTMLElement | undefined | null>;
18-
private readonly _user$: Val<IAgoraRTCRemoteUser | undefined>;
18+
private readonly _videoTrack$: Val<IRemoteVideoTrack | undefined>;
19+
private readonly _audioTrack$: Val<IRemoteAudioTrack | undefined>;
1920

2021
public enableCamera(enabled: boolean): void {
2122
this._shouldCamera$.setValue(enabled);
@@ -30,25 +31,27 @@ export class RTCRemoteAvatar implements FlatRTCAvatar {
3031
}
3132

3233
public updateUser(rtcRemoteUser: IAgoraRTCRemoteUser): void {
33-
this._user$.setValue(rtcRemoteUser);
34+
this._videoTrack$.setValue(rtcRemoteUser.videoTrack);
35+
this._audioTrack$.setValue(rtcRemoteUser.audioTrack);
3436
}
3537

3638
public getVolumeLevel(): number {
37-
return this._user$.value?.audioTrack?.getVolumeLevel() || 0;
39+
return this._audioTrack$.value?.getVolumeLevel() || 0;
3840
}
3941

4042
public constructor(config: RTCRemoteAvatarConfig = {}) {
4143
this._el$ = new Val(config.element);
42-
this._user$ = new Val(config.rtcRemoteUser);
44+
this._videoTrack$ = new Val(config.rtcRemoteUser?.videoTrack);
45+
this._audioTrack$ = new Val(config.rtcRemoteUser?.audioTrack);
4346

4447
this._sideEffect.addDisposer(
45-
combine([this._user$, this._shouldMic$]).subscribe(([user, shouldMic]) => {
46-
if (user && user.audioTrack) {
48+
combine([this._audioTrack$, this._shouldMic$]).subscribe(([audioTrack, shouldMic]) => {
49+
if (audioTrack) {
4750
try {
4851
if (shouldMic) {
49-
user.audioTrack.play();
52+
audioTrack.play();
5053
} else {
51-
user.audioTrack.stop();
54+
audioTrack.stop();
5255
}
5356
} catch (e) {
5457
console.error(e);
@@ -58,14 +61,14 @@ export class RTCRemoteAvatar implements FlatRTCAvatar {
5861
);
5962

6063
this._sideEffect.addDisposer(
61-
combine([this._el$, this._user$, this._shouldCamera$]).subscribe(
62-
([el, user, shouldCamera]) => {
63-
if (el && user && user.videoTrack) {
64+
combine([this._el$, this._videoTrack$, this._shouldCamera$]).subscribe(
65+
([el, videoTrack, shouldCamera]) => {
66+
if (el && videoTrack) {
6467
try {
6568
if (shouldCamera) {
66-
user.videoTrack.play(el);
69+
videoTrack.play(el);
6770
} else {
68-
user.videoTrack.stop();
71+
videoTrack.stop();
6972
}
7073
} catch (e) {
7174
console.error(e);
@@ -74,18 +77,19 @@ export class RTCRemoteAvatar implements FlatRTCAvatar {
7477
},
7578
),
7679
);
77-
78-
this._sideEffect.addDisposer(() => {
79-
try {
80-
this._user$.value?.videoTrack?.stop();
81-
this._user$.value?.audioTrack?.stop();
82-
} catch (e) {
83-
console.error(e);
84-
}
85-
});
8680
}
8781

8882
public destroy(): void {
8983
this._sideEffect.flushAll();
84+
85+
try {
86+
this._videoTrack$.value?.stop();
87+
this._audioTrack$.value?.stop();
88+
} catch (e) {
89+
console.error(e);
90+
}
91+
92+
this._videoTrack$.setValue(undefined);
93+
this._audioTrack$.setValue(undefined);
9094
}
9195
}

0 commit comments

Comments
 (0)