livekit-client + @livekit/components-react, using room.switchActiveDevice('audioinput' | 'audiooutput', deviceId, true)
Issue
When the user switches the input device from the built-in MacBook Air microphone to a Bluetooth headset (and vice versa), both input and output are expected to follow the selected device. In Safari, the UI shows the switch succeeded, but audio in and out keeps flowing through the Bluetooth device — even with the MacBook mic selected.
Chrome on the same machine behaves correctly.
Repro
Connect a Bluetooth HFP headset (AirPods, etc.)
Join a LiveKit room in Safari
In the device menu, select the built-in MacBook Air microphone
Observe: ActiveDeviceChanged fires, UI checkmark moves, but remote participants still hear the Bluetooth mic, and local playback still comes out of the Bluetooth speaker
What we’ve tried
Verified at the hardware level by reading the live track’s settings:
const pub = room.localParticipant.getTrackPublication(Track.Source.Microphone);
const settings = (pub.track as any).mediaStreamTrack.getSettings?.();
console.log('requested:', requestedId, 'actual:', settings.deviceId);
// → actual deviceId does not match the requested one
The switch internally does a fresh getUserMedia({ audio: { deviceId: { exact } } }) — the call resolves, but the track still lands on the Bluetooth device
For output: HTMLMediaElement.setSinkId() + room.switchActiveDevice(‘audiooutput’, …) — same result, audio.sinkId reports the requested device but audible output stays on Bluetooth
Tried pause/resume around setSinkId (Safari sometimes needs this for active streams) — no change
navigator.mediaDevices.selectAudioOutput() works for picking the device, but routing still doesn’t move
Console diagnostics (from enumerateDevices + getSettings)
| kind | label | groupId |
|-------------|----------------------|---------|
| audioinput | MacBook Air Microphone | abc123… |
| audioinput | AirPods Pro | def456… |
| audiooutput | AirPods Pro | def456… |
// note: groupId matches between AirPods mic and speaker
setSinkId supported: true
Hypothesis
This looks like a WebKit/macOS limitation rather than a LiveKit bug: once a Bluetooth HFP headset is the OS’s active communications device, macOS CoreAudio pins the route for the WebKit process, and Safari silently ignores both the exact deviceId constraint on getUserMedia and setSinkId routing — while still reporting success. Selecting “System Settings → Sound → Output → MacBook Air Speakers” at the OS level does move the audio, which supports this.
Questions
Can anyone confirm this is a known WebKit limitation (bug link appreciated)?
Is there any workaround from the web side, or do we need to detect the mismatch and direct users to macOS Sound settings (which is our current fallback — we surface a toast when getSettings().deviceId ≠ requested)?
Does LiveKit have any recommended pattern for this Safari + Bluetooth case?
Thanks for the detailed write-up. That kind of report makes this much easier to pin down. Most of what you’re seeing is Safari and macOS deciding where audio goes. Two parts of it come from how the LiveKit SDK behaves, so I’ll take those first.
switchActiveDevice() resolving doesn’t mean the switch worked. It resolves with a boolean. For audioinput, it restarts the mic track and then returns false if mediaStreamTrack.getSettings().deviceId doesn’t match the device you asked for. That’s the same check you wrote yourself. So you can drive your toast directly from the return value:
const ok = await room.switchActiveDevice('audioinput', deviceId, true);
if (!ok) showRouteToast(); // Safari kept the old device
On Safari, the SDK treats audio output selection as unsupported, even though ‘setSinkId’ in HTMLMediaElement.prototype is true. supportsSetSinkId() returns false for every Safari-based browser. Unless you’ve set webAudioMix, switchActiveDevice(‘audiooutput’, …) throws cannot switch audio output, the current browser does not support it. If you call setSinkId() directly, sinkId will report the new device, but the sound keeps following the macOS route. That matches what you saw. We recommend hiding the output picker on Safari:
import { supportsAudioOutputSelection } from 'livekit-client';
const showOutputPicker = supportsAudioOutputSelection();
Why the mic switch doesn’t stick. While a page is capturing from a Bluetooth headset, macOS puts it into the hands-free call profile (HFP), which ties its mic and speaker together. That’s why the AirPods mic and speaker share a groupId. Safari then tends to keep both on the headset. Your test supports this: changing the device in macOS Sound settings moves the audio straight away. We don’t know of a web-side API that overrides it.
Things worth trying, though we haven’t confirmed either one works:
Release the Bluetooth capture completely before switching: await room.localParticipant.setMicrophoneEnabled(false), then re-enable it with the new device: setMicrophoneEnabled(true, { deviceId: { exact: id } }). While the old track is open, macOS may hold the headset in HFP. Stopping it first can let the route change.
For Safari with Bluetooth: use the return value to detect when the switch didn’t happen, hide the output picker, and send people to macOS Sound settings or Control Center when the route doesn’t move. That’s what you’re already doing, and it’s the right call. Chrome takes the requested device as given, which is why it behaves differently on the same machine.
Thanks for the suggestions @CWilson — here’s an update on what I’ve implemented:
What worked
Boolean result check:switchActiveDevice() resolves with a boolean, so I now drive failure off that return value instead of only catching thrown errors — a silent “kept the old device” case is now reported as a failure.
Output selection:supportsAudioOutputSelection() returns false on Safari-based browsers, so I hid the output picker entirely there and show a hint to use macOS Sound settings / Control Center instead of calling setSinkId() (which could update the reported sink ID without actually moving the audio).
What didn’t work
Disable/re-enable workaround: I tried the disable/re-enable microphone workaround (setMicrophoneEnabled(false) → true) to force a clean track restart after a Bluetooth reconnect. On Safari it made things worse: the mic ended up muted, and device selection started oscillating between the MacBook mic and the headset — the toggle and RoomEvent.ActiveDeviceChanged kept firing against each other. I removed that retry.
Current mitigation (switchMicWithFallback)
3-second timeout: Racing the switchActiveDevice('audioinput', ...) promise with a timeout so a stalled getUserMedia doesn’t hang the UI.
Duplicate-switch guard: A micSwitchLockedUntil lock drops a second switch attempt while one is pending or immediately after a failure, preventing stacked attempts.
Immediate feedback: Both the boolean result and thrown errors route into _markMicFailed() (triggering a toast and a indicator on the device row), plus a RoomEvent.MediaDevicesError listener surfaces capture failures directly.
Open Question
Is there a recommended SDK-level way to cancel or recover a pending switchActiveDevice('audioinput') request when Safari’s getUserMedia stalls during a Bluetooth HFP route change?
Right now, the timeout is purely a UI-level workaround — the underlying promise remains pending with no way to abort it, meaning the SDK’s internal getUserMedia keeps running against the old route. If LiveKit has (or would consider) exposing an abort/cancel handle for in-flight device switches, that would let us recover cleanly instead of having to guard around it.