Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/await-mixed-audio-output-routing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

Await `AudioContext.setSinkId()` when switching the audio output with `webAudioMix` enabled, so the switch only resolves (and emits `ActiveDeviceChanged`) once routing succeeds, and routing failures reject instead of becoming unhandled rejections
2 changes: 1 addition & 1 deletion src/room/CoreRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1492,7 +1492,7 @@ export class CoreRoom extends (EventEmitter as new () => TypedEmitter<RoomEventC
try {
if (this.options.webAudioMix) {
// @ts-expect-error setSinkId is not yet in the typescript type of AudioContext
this.audioContext?.setSinkId(deviceId);
await this.audioContext?.setSinkId(deviceId);
}

// also set audio output on all audio elements, even if webAudioMix is enabled in order to workaround echo cancellation not working on chrome with non-default output devices
Expand Down
48 changes: 48 additions & 0 deletions src/room/Room.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,54 @@ describe('Active device switch', () => {
expect(deviceId).toBe('test');
expect(kind).toBe('audioinput');
});

describe('audiooutput with webAudioMix', () => {
function roomWithMixedAudioContext(setSinkId: (deviceId: string) => Promise<void>) {
const room = new Room({ webAudioMix: true });
(room as unknown as { audioContext: { setSinkId: typeof setSinkId } }).audioContext = {
setSinkId,
};
return room;
}

it('waits for the audio context to finish routing before confirming the switch', async () => {
const route = new Future<void, never>();
const room = roomWithMixedAudioContext(() => route.promise);
const deviceChanged = vi.fn();
room.on(RoomEvent.ActiveDeviceChanged, deviceChanged);

let settled = false;
const switchPromise = room.switchActiveDevice('audiooutput', 'speaker').then(() => {
settled = true;
});
await new Promise((resolve) => setTimeout(resolve, 0));
expect(settled).toBe(false);
expect(deviceChanged).not.toHaveBeenCalled();

route.resolve?.();
await switchPromise;
expect(deviceChanged).toHaveBeenCalledWith('audiooutput', 'speaker');
expect(room.getActiveDevice('audiooutput')).toBe('speaker');
});

it('rejects and keeps the previous output when the audio context fails to route', async () => {
const room = roomWithMixedAudioContext(async (deviceId) => {
if (deviceId === 'broken') {
throw new Error('setSinkId failed');
}
});
await room.switchActiveDevice('audiooutput', 'speaker');

const deviceChanged = vi.fn();
room.on(RoomEvent.ActiveDeviceChanged, deviceChanged);
await expect(room.switchActiveDevice('audiooutput', 'broken')).rejects.toThrow(
'setSinkId failed',
);
expect(deviceChanged).not.toHaveBeenCalled();
expect(room.getActiveDevice('audiooutput')).toBe('speaker');
expect(room.options.audioOutput?.deviceId).toBe('speaker');
});
});
});

describe('Room signaling options', () => {
Expand Down
Loading