-
Notifications
You must be signed in to change notification settings - Fork 137
/
RemoteTrack.ts
62 lines (53 loc) · 1.48 KB
/
RemoteTrack.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { TrackEvent } from '../events';
import { monitorFrequency } from '../stats';
import { Track } from './Track';
export default abstract class RemoteTrack extends Track {
/** @internal */
receiver?: RTCRtpReceiver;
streamState: Track.StreamState = Track.StreamState.Active;
constructor(
mediaTrack: MediaStreamTrack,
sid: string,
kind: Track.Kind,
receiver?: RTCRtpReceiver,
) {
super(mediaTrack, kind);
this.sid = sid;
this.receiver = receiver;
}
/** @internal */
setMuted(muted: boolean) {
if (this.isMuted !== muted) {
this.isMuted = muted;
this.emit(muted ? TrackEvent.Muted : TrackEvent.Unmuted, this);
}
}
/** @internal */
setMediaStream(stream: MediaStream) {
// this is needed to determine when the track is finished
// we send each track down in its own MediaStream, so we can assume the
// current track is the only one that can be removed.
this.mediaStream = stream;
stream.onremovetrack = () => {
this.receiver = undefined;
this._currentBitrate = 0;
this.emit(TrackEvent.Ended, this);
};
}
start() {
this.startMonitor();
// use `enabled` of track to enable re-use of transceiver
super.enable();
}
stop() {
// use `enabled` of track to enable re-use of transceiver
super.disable();
}
/* @internal */
startMonitor() {
setTimeout(() => {
this.monitorReceiver();
}, monitorFrequency);
}
protected abstract monitorReceiver(): void;
}