dont call setSinkId with controlled devices

This commit is contained in:
Timo
2025-05-23 16:50:25 +02:00
parent 0971a15c40
commit b7e6722cc1
3 changed files with 14 additions and 11 deletions

View File

@@ -48,6 +48,8 @@ export interface MediaDeviceHandle {
*/ */
available: Map<string, DeviceLabel>; available: Map<string, DeviceLabel>;
selectedId: string | undefined; selectedId: string | undefined;
selectedWebDeviceId: string | undefined;
/** /**
* An additional device configuration that makes us use only one channel of the * An additional device configuration that makes us use only one channel of the
* output device and a reduced volume. * output device and a reduced volume.
@@ -202,6 +204,7 @@ function useMediaDeviceHandle(
return useMemo( return useMemo(
() => ({ () => ({
available, available,
selectedWebDeviceId: selectedId,
selectedId, selectedId,
useAsEarpiece: false, useAsEarpiece: false,
selectedGroupId, selectedGroupId,
@@ -214,6 +217,7 @@ function useMediaDeviceHandle(
export const deviceStub: MediaDeviceHandle = { export const deviceStub: MediaDeviceHandle = {
available: new Map(), available: new Map(),
selectedId: undefined, selectedId: undefined,
selectedWebDeviceId: undefined,
selectedGroupId: undefined, selectedGroupId: undefined,
select: () => {}, select: () => {},
useAsEarpiece: false, useAsEarpiece: false,
@@ -379,6 +383,7 @@ function useControlledOutput(): MediaDeviceHandle {
() => ({ () => ({
available: available, available: available,
selectedId, selectedId,
selectedWebDeviceId: undefined,
selectedGroupId: undefined, selectedGroupId: undefined,
select: setPreferredId, select: setPreferredId,
useAsEarpiece: asEarpiece, useAsEarpiece: asEarpiece,

View File

@@ -308,7 +308,7 @@ export function useLivekit(
kind: MediaDeviceKind, kind: MediaDeviceKind,
device: MediaDeviceHandle, device: MediaDeviceHandle,
): void => { ): void => {
const id = device.selectedId; const id = device.selectedWebDeviceId;
// Detect if we're trying to use chrome's default device, in which case // Detect if we're trying to use chrome's default device, in which case
// we need to to see if the default device has changed to a different device // we need to to see if the default device has changed to a different device
@@ -348,14 +348,12 @@ export function useLivekit(
logger.error(`Failed to restart audio device track`, e); logger.error(`Failed to restart audio device track`, e);
}); });
} }
} else { } else if (id !== undefined && room.getActiveDevice(kind) !== id) {
if (id !== undefined && room.getActiveDevice(kind) !== id) { room
room .switchActiveDevice(kind, id)
.switchActiveDevice(kind, id) .catch((e) =>
.catch((e) => logger.error(`Failed to sync ${kind} device with LiveKit`, e),
logger.error(`Failed to sync ${kind} device with LiveKit`, e), );
);
}
} }
}; };

View File

@@ -113,11 +113,11 @@ export function useAudioContext<S extends string>(
if (audioContext && "setSinkId" in audioContext) { if (audioContext && "setSinkId" in audioContext) {
// https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/setSinkId // https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/setSinkId
// @ts-expect-error - setSinkId doesn't exist yet in types, maybe because it's not supported everywhere. // @ts-expect-error - setSinkId doesn't exist yet in types, maybe because it's not supported everywhere.
audioContext.setSinkId(audioOutput.selectedId).catch((ex) => { audioContext.setSinkId(audioOutput.selectedWebDeviceId).catch((ex) => {
logger.warn("Unable to change sink for audio context", ex); logger.warn("Unable to change sink for audio context", ex);
}); });
} }
}, [audioContext, audioOutput.selectedId]); }, [audioContext, audioOutput.selectedWebDeviceId]);
const { pan: earpiecePan, volume: earpieceVolume } = useEarpieceAudioConfig(); const { pan: earpiecePan, volume: earpieceVolume } = useEarpieceAudioConfig();
// Don't return a function until we're ready. // Don't return a function until we're ready.