1
1
import { SideEffectManager } from "side-effect-manager" ;
2
2
import { combine , Val } from "value-enhancer" ;
3
3
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" ;
5
5
6
6
export interface RTCRemoteAvatarConfig {
7
7
rtcRemoteUser ?: IAgoraRTCRemoteUser ;
@@ -15,7 +15,8 @@ export class RTCRemoteAvatar implements FlatRTCAvatar {
15
15
private readonly _shouldMic$ = new Val ( false ) ;
16
16
17
17
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 > ;
19
20
20
21
public enableCamera ( enabled : boolean ) : void {
21
22
this . _shouldCamera$ . setValue ( enabled ) ;
@@ -30,25 +31,27 @@ export class RTCRemoteAvatar implements FlatRTCAvatar {
30
31
}
31
32
32
33
public updateUser ( rtcRemoteUser : IAgoraRTCRemoteUser ) : void {
33
- this . _user$ . setValue ( rtcRemoteUser ) ;
34
+ this . _videoTrack$ . setValue ( rtcRemoteUser . videoTrack ) ;
35
+ this . _audioTrack$ . setValue ( rtcRemoteUser . audioTrack ) ;
34
36
}
35
37
36
38
public getVolumeLevel ( ) : number {
37
- return this . _user $. value ?. audioTrack ?. getVolumeLevel ( ) || 0 ;
39
+ return this . _audioTrack $. value ?. getVolumeLevel ( ) || 0 ;
38
40
}
39
41
40
42
public constructor ( config : RTCRemoteAvatarConfig = { } ) {
41
43
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 ) ;
43
46
44
47
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 ) {
47
50
try {
48
51
if ( shouldMic ) {
49
- user . audioTrack . play ( ) ;
52
+ audioTrack . play ( ) ;
50
53
} else {
51
- user . audioTrack . stop ( ) ;
54
+ audioTrack . stop ( ) ;
52
55
}
53
56
} catch ( e ) {
54
57
console . error ( e ) ;
@@ -58,14 +61,14 @@ export class RTCRemoteAvatar implements FlatRTCAvatar {
58
61
) ;
59
62
60
63
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 ) {
64
67
try {
65
68
if ( shouldCamera ) {
66
- user . videoTrack . play ( el ) ;
69
+ videoTrack . play ( el ) ;
67
70
} else {
68
- user . videoTrack . stop ( ) ;
71
+ videoTrack . stop ( ) ;
69
72
}
70
73
} catch ( e ) {
71
74
console . error ( e ) ;
@@ -74,18 +77,19 @@ export class RTCRemoteAvatar implements FlatRTCAvatar {
74
77
} ,
75
78
) ,
76
79
) ;
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
- } ) ;
86
80
}
87
81
88
82
public destroy ( ) : void {
89
83
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 ) ;
90
94
}
91
95
}
0 commit comments