Skip to content

Commit

Permalink
Fixes calling start() on a remote track multiple times (#393)
Browse files Browse the repository at this point in the history
* Prevent multiple redundant monitors from being started if start is called multiple times on a RemoteTrack #392

* Fix linter issue

* Changeset

* Switch to setInterval to make code more readable

* Run prettier

* Clear interval on stop
  • Loading branch information
wcarle committed Aug 22, 2022
1 parent 2983939 commit bfad4b3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-crabs-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

Prevent multiple redundant monitors from being started if start is called multiple times on a RemoteTrack
5 changes: 1 addition & 4 deletions src/room/track/RemoteAudioTrack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AudioReceiverStats, computeBitrate, monitorFrequency } from '../stats';
import { AudioReceiverStats, computeBitrate } from '../stats';
import RemoteTrack from './RemoteTrack';
import { Track } from './Track';

Expand Down Expand Up @@ -63,9 +63,6 @@ export default class RemoteAudioTrack extends RemoteTrack {
}

this.prevStats = stats;
setTimeout(() => {
this.monitorReceiver();
}, monitorFrequency);
};

protected async getReceiverStats(): Promise<AudioReceiverStats | undefined> {
Expand Down
16 changes: 13 additions & 3 deletions src/room/track/RemoteTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export default abstract class RemoteTrack extends Track {
/** @internal */
receiver?: RTCRtpReceiver;

monitorInterval?: ReturnType<typeof setInterval>;

constructor(
mediaTrack: MediaStreamTrack,
sid: string,
Expand Down Expand Up @@ -46,15 +48,23 @@ export default abstract class RemoteTrack extends Track {
}

stop() {
this.stopMonitor();
// use `enabled` of track to enable re-use of transceiver
super.disable();
}

/* @internal */
startMonitor() {
setTimeout(() => {
this.monitorReceiver();
}, monitorFrequency);
if (!this.monitorInterval) {
this.monitorInterval = setInterval(() => this.monitorReceiver(), monitorFrequency);
}
}

/* @internal */
stopMonitor() {
if (this.monitorInterval) {
clearInterval(this.monitorInterval);
}
}

protected abstract monitorReceiver(): void;
Expand Down
5 changes: 1 addition & 4 deletions src/room/track/RemoteVideoTrack.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { debounce } from 'ts-debounce';
import log from '../../logger';
import { TrackEvent } from '../events';
import { computeBitrate, monitorFrequency, VideoReceiverStats } from '../stats';
import { computeBitrate, VideoReceiverStats } from '../stats';
import { getIntersectionObserver, getResizeObserver, ObservableMediaElement } from '../utils';
import RemoteTrack from './RemoteTrack';
import { attachToElement, detachTrack, Track } from './Track';
Expand Down Expand Up @@ -160,9 +160,6 @@ export default class RemoteVideoTrack extends RemoteTrack {
}

this.prevStats = stats;
setTimeout(() => {
this.monitorReceiver();
}, monitorFrequency);
};

private async getReceiverStats(): Promise<VideoReceiverStats | undefined> {
Expand Down

0 comments on commit bfad4b3

Please sign in to comment.