Skip to content

Conversation

@mfairley
Copy link

@mfairley mfairley commented Jan 24, 2026

When using { ideal: 'default' } device constraints (the default), unmuting
would trigger unnecessary track restarts because the comparison between the
constraint value ('default') and actual device ID (e.g. 'audio') would always
fail.

This adds a pendingDeviceChange flag that is only set when setDeviceId()
is explicitly called while the track is muted. The unmute logic now checks
this flag instead of comparing constraint values against the actual device,
ensuring restarts only happen when the user actually requested a device change.

Related issues

Test plan

  • Tested muting and unmuting microphone on iOS with default device constraints
  • Verified no unnecessary restarts occur on unmute
  • Verified setDeviceId() while muted still triggers restart on unmute

Summary by CodeRabbit

  • Bug Fixes
    • Fixed unnecessary audio track restarts when unmuting with ideal device constraints; device changes made while muted now apply on unmute without forcing a restart.
  • Chores
    • Added release changeset entry for this patch.

✏️ Tip: You can customize this high-level summary in your review settings.

@changeset-bot
Copy link

changeset-bot bot commented Jan 24, 2026

🦋 Changeset detected

Latest commit: bc2915b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
livekit-client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai
Copy link

coderabbitai bot commented Jan 24, 2026

📝 Walkthrough

Walkthrough

Adds a pendingDeviceChange flag to LocalTrack and updates LocalAudioTrack to use this flag (instead of comparing device constraints) to decide whether to restart a track on unmute; changeset file added for a patch release.

Changes

Cohort / File(s) Summary
Changeset Documentation
.changeset/funny-masks-strive.md
Added patch release entry describing the fix for unnecessary track restarts on unmute when using ideal device constraints
LocalTrack state
src/room/track/LocalTrack.ts
Introduced protected boolean pendingDeviceChange; setDeviceId now sets this flag when called while muted; restartTrack clears the flag once acquisition completes
LocalAudioTrack unmute logic
src/room/track/LocalAudioTrack.ts
Removed unwrapConstraint import and legacy deviceHasChanged comparison; unmute path now uses pendingDeviceChange to decide whether to reacquire/restart the mic track

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A little flag beneath the log,

waits while silence hugs the bog.
Set in quiet, cleared with care,
no needless restarts in the air.
Hopping fixes, light as flair. ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: preventing unnecessary track restarts on unmute when using ideal device constraints, which is the core problem being addressed in the PR.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ac7b855 and bc2915b.

📒 Files selected for processing (3)
  • .changeset/funny-masks-strive.md
  • src/room/track/LocalAudioTrack.ts
  • src/room/track/LocalTrack.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • .changeset/funny-masks-strive.md
  • src/room/track/LocalAudioTrack.ts
🔇 Additional comments (2)
src/room/track/LocalTrack.ts (2)

68-69: LGTM!

The protected field with false default is appropriate. It follows the existing pattern of protected state fields in this class and allows subclasses to implement unmute behavior based on this flag.


369-372: LGTM!

The flag is correctly reset after successfully acquiring and setting the new media stream track. This ensures the pending state is only cleared when the device change has been fully applied.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/room/track/LocalTrack.ts`:
- Around line 250-253: LocalVideoTrack.unmute should mirror
LocalAudioTrack.unmute by clearing the pendingDeviceChange flag when unmuting so
it doesn't remain true across cycles; update the LocalVideoTrack.unmute method
to check this.pendingDeviceChange at the start (or before any restart logic for
camera source), set this.pendingDeviceChange = false and return true (or
otherwise short-circuit the deferred device-change flow) so state is consistent
with LocalAudioTrack.unmute.
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1744ee4 and ac7b855.

📒 Files selected for processing (3)
  • .changeset/funny-masks-strive.md
  • src/room/track/LocalAudioTrack.ts
  • src/room/track/LocalTrack.ts
🔇 Additional comments (4)
src/room/track/LocalTrack.ts (1)

68-69: LGTM!

The new pendingDeviceChange flag is correctly initialized and appropriately scoped as protected to allow subclass access.

src/room/track/LocalAudioTrack.ts (2)

77-87: Consider resetting pendingDeviceChange in a finally block or before restartTrack().

If restartTrack() throws, pendingDeviceChange will still be reset to false on line 86, which is correct. However, the current placement means if an exception occurs between line 85 and 86 (unlikely but possible), the flag may not be reset.

More importantly, if the restart fails and the user calls unmute() again, the restart will be attempted again (due to readyState === 'ended'), so this is likely fine. The current implementation is acceptable.


6-6: LGTM!

Clean removal of unwrapConstraint from imports now that device change detection uses the pendingDeviceChange flag instead of constraint comparison.

.changeset/funny-masks-strive.md (1)

1-5: LGTM!

Changeset correctly documents this as a patch release with a clear description of the fix.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.

Comment on lines 250 to 253
if (this.isMuted) {
this.pendingDeviceChange = true;
return true;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check LocalVideoTrack unmute logic for device change handling

# Search for unmute method in LocalVideoTrack
echo "=== Searching for unmute method in LocalVideoTrack ==="
ast-grep --pattern $'class LocalVideoTrack {
  $$$
  unmute() {
    $$$
  }
  $$$
}'

# Also check for any pendingDeviceChange references
echo -e "\n=== Searching for pendingDeviceChange references ==="
rg -n "pendingDeviceChange" --type ts

# Search for LocalVideoTrack class definition
echo -e "\n=== Searching for LocalVideoTrack class ==="
rg -n "class LocalVideoTrack" --type ts

# Search for unmute in LocalVideoTrack context
echo -e "\n=== Searching for unmute method ==="
rg -n "unmute\(\)" --type ts -A 5 -B 2

Repository: livekit/client-sdk-js

Length of output: 5452


🏁 Script executed:

#!/bin/bash
# Get full unmute implementations in both tracks

echo "=== LocalAudioTrack unmute method (full) ==="
sed -n '69,93p' src/room/track/LocalAudioTrack.ts

echo -e "\n=== LocalVideoTrack unmute method (full) ==="
sed -n '161,178p' src/room/track/LocalVideoTrack.ts

echo -e "\n=== LocalTrack device change handling context (lines 245-260) ==="
sed -n '245,260p' src/room/track/LocalTrack.ts

Repository: livekit/client-sdk-js

Length of output: 1829


LocalVideoTrack should also check and reset pendingDeviceChange on unmute for consistency.

The flag is set when a device change is deferred due to muting, but only LocalAudioTrack explicitly resets it in its unmute handler. LocalVideoTrack's unmute always restarts the track (if source is Camera), so it implicitly handles the pending change—but does not reset the flag. This can cause pendingDeviceChange to remain true on subsequent mute/unmute cycles, creating inconsistent state. Consider adding the same check and reset logic to LocalVideoTrack.unmute() as found in LocalAudioTrack.unmute() (lines 81–86).

🤖 Prompt for AI Agents
In `@src/room/track/LocalTrack.ts` around lines 250 - 253, LocalVideoTrack.unmute
should mirror LocalAudioTrack.unmute by clearing the pendingDeviceChange flag
when unmuting so it doesn't remain true across cycles; update the
LocalVideoTrack.unmute method to check this.pendingDeviceChange at the start (or
before any restart logic for camera source), set this.pendingDeviceChange =
false and return true (or otherwise short-circuit the deferred device-change
flow) so state is consistent with LocalAudioTrack.unmute.

…vice constraints

When using `{ ideal: 'default' }` device constraints (the default), unmuting
would trigger unnecessary track restarts because the comparison between the
constraint value ('default') and actual device ID (e.g. 'audio') would always
fail.

This adds a `pendingDeviceChange` flag that is only set when `setDeviceId()`
is explicitly called while the track is muted. The unmute logic now checks
this flag instead of comparing constraint values against the actual device,
ensuring restarts only happen when the user actually requested a device change.
Copy link
Contributor

@davidliu davidliu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! @lukasIO do you see anything affecting the web side? Tested web and seems to mute/unmute fine, without reacquiring the mic track.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants