From 81bde6d97acac1dcd26b984daf94d71b0e56d16d Mon Sep 17 00:00:00 2001 From: lukasIO Date: Tue, 27 Feb 2024 17:57:33 +0100 Subject: [PATCH] Only perform mute/unmute actions if necessary (#1048) --- .changeset/unlucky-planets-trade.md | 5 +++++ src/room/track/LocalAudioTrack.ts | 10 ++++++++++ src/room/track/LocalVideoTrack.ts | 10 ++++++++++ 3 files changed, 25 insertions(+) create mode 100644 .changeset/unlucky-planets-trade.md diff --git a/.changeset/unlucky-planets-trade.md b/.changeset/unlucky-planets-trade.md new file mode 100644 index 0000000000..b23e476fa1 --- /dev/null +++ b/.changeset/unlucky-planets-trade.md @@ -0,0 +1,5 @@ +--- +"livekit-client": patch +--- + +Only perform mute/unmute actions if necessary diff --git a/src/room/track/LocalAudioTrack.ts b/src/room/track/LocalAudioTrack.ts index 69785600e8..3fb00a4b0d 100644 --- a/src/room/track/LocalAudioTrack.ts +++ b/src/room/track/LocalAudioTrack.ts @@ -51,6 +51,11 @@ export default class LocalAudioTrack extends LocalTrack { async mute(): Promise { const unlock = await this.muteLock.lock(); try { + if (this.isMuted) { + this.log.debug('Track already muted', this.logContext); + return this; + } + // disabled special handling as it will cause BT headsets to switch communication modes if (this.source === Track.Source.Microphone && this.stopOnMute && !this.isUserProvided) { this.log.debug('stopping mic track', this.logContext); @@ -67,6 +72,11 @@ export default class LocalAudioTrack extends LocalTrack { async unmute(): Promise { const unlock = await this.muteLock.lock(); try { + if (!this.isMuted) { + this.log.debug('Track already unmuted', this.logContext); + return this; + } + const deviceHasChanged = this._constraints.deviceId && this._mediaStreamTrack.getSettings().deviceId !== diff --git a/src/room/track/LocalVideoTrack.ts b/src/room/track/LocalVideoTrack.ts index eee3bd8569..1ab3585c05 100644 --- a/src/room/track/LocalVideoTrack.ts +++ b/src/room/track/LocalVideoTrack.ts @@ -118,6 +118,11 @@ export default class LocalVideoTrack extends LocalTrack { async mute(): Promise { const unlock = await this.muteLock.lock(); try { + if (this.isMuted) { + this.log.debug('Track already muted', this.logContext); + return this; + } + if (this.source === Track.Source.Camera && !this.isUserProvided) { this.log.debug('stopping camera track', this.logContext); // also stop the track, so that camera indicator is turned off @@ -133,6 +138,11 @@ export default class LocalVideoTrack extends LocalTrack { async unmute(): Promise { const unlock = await this.muteLock.lock(); try { + if (!this.isMuted) { + this.log.debug('Track already unmuted', this.logContext); + return this; + } + if (this.source === Track.Source.Camera && !this.isUserProvided) { this.log.debug('reacquiring camera track', this.logContext); await this.restartTrack();