Skip to content

Commit a101cdd

Browse files
authored
fix: prevent unnecessary track restarts on unmute when using ideal device constraints (#1794)
1 parent 911ad53 commit a101cdd

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

.changeset/funny-masks-strive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"livekit-client": patch
3+
---
4+
5+
Fix unnecessary track restarts on unmute when using ideal device constraints

src/room/track/LocalAudioTrack.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { TrackEvent } from '../events';
33
import { computeBitrate, monitorFrequency } from '../stats';
44
import type { AudioSenderStats } from '../stats';
55
import type { LoggerOptions } from '../types';
6-
import { isReactNative, isWeb, unwrapConstraint } from '../utils';
6+
import { isReactNative, isWeb } from '../utils';
77
import LocalTrack from './LocalTrack';
88
import { Track } from './Track';
99
import type { AudioCaptureOptions } from './options';
@@ -74,14 +74,11 @@ export default class LocalAudioTrack extends LocalTrack<Track.Kind.Audio> {
7474
return this;
7575
}
7676

77-
const deviceHasChanged =
78-
this._constraints.deviceId &&
79-
this._mediaStreamTrack.getSettings().deviceId !==
80-
unwrapConstraint(this._constraints.deviceId);
81-
8277
if (
8378
this.source === Track.Source.Microphone &&
84-
(this.stopOnMute || this._mediaStreamTrack.readyState === 'ended' || deviceHasChanged) &&
79+
(this.stopOnMute ||
80+
this._mediaStreamTrack.readyState === 'ended' ||
81+
this.pendingDeviceChange) &&
8582
!this.isUserProvided
8683
) {
8784
this.log.debug('reacquiring mic track', this.logContext);

src/room/track/LocalTrack.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ export default abstract class LocalTrack<
6565

6666
protected trackChangeLock: Mutex;
6767

68+
protected pendingDeviceChange: boolean = false;
69+
6870
/**
6971
*
7072
* @param mediaTrack
@@ -246,6 +248,7 @@ export default abstract class LocalTrack<
246248
// when track is muted, underlying media stream track is stopped and
247249
// will be restarted later
248250
if (this.isMuted) {
251+
this.pendingDeviceChange = true;
249252
return true;
250253
}
251254

@@ -365,6 +368,7 @@ export default abstract class LocalTrack<
365368

366369
await this.setMediaStreamTrack(newTrack);
367370
this._constraints = constraints;
371+
this.pendingDeviceChange = false;
368372
this.emit(TrackEvent.Restarted, this);
369373
if (this.manuallyStopped) {
370374
this.log.warn(

0 commit comments

Comments
 (0)