fix: preserve audio capture across device reconfiguration

This commit is contained in:
illusia committed 2026-10-01 11:30:40 +08:00
1 parent 797fc135a9
commit 0785f82386
5 files changed
+382 -45

No files matched your search

+22
View File
@@ -0,0 +1,22 @@
interface AudioSink {
setSinkId?(deviceId: string): Promise<void>;
}
// Browser sink changes cannot be cancelled once started. Serialize each
// endpoint so an older completion cannot leave it on the wrong device.
export function createAudioSinkRouter() {
const pending = new WeakMap<AudioSink, Promise<void>>();
function set(sink: AudioSink, deviceId: string, isCurrent: () => boolean): Promise<void> {
const previous = pending.get(sink) ?? Promise.resolve();
const task = previous.catch(() => {}).then(async () => {
if (isCurrent()) await sink.setSinkId?.(deviceId || "default");
});
pending.set(sink, task);
const cleanup = () => { if (pending.get(sink) === task) pending.delete(sink); };
void task.then(cleanup, cleanup);
return task;
}
return { set };
}