/* Copyright 2026 Lotus Guild SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial Please see LICENSE in the repository root for full details. */ import { type IWidgetApiRequest } from "matrix-widget-api"; import { logger } from "matrix-js-sdk/lib/logger"; import { type MediaDevices } from "../state/MediaDevices"; import { widget } from "../widget"; import { LotusWidgetActions } from "./lotusActions"; import { lotusSendToHost } from "./lotusWidget"; /** * [Gitea #119] Let the host switch the audio output device (headset ↔ * speakers) from its own call bar: `io.lotus.set_audio_output { deviceId }` * selects the output; the fork answers each change (and the initial state) * with `io.lotus.audio_output_state { deviceId, available: [{id,label}] }`. * No effect unless the host sends the action. Returns a teardown function. */ export function startLotusAudioOutput(mediaDevices: MediaDevices): () => void { const w = widget; if (!w) return () => undefined; const handler = (ev: CustomEvent): void => { w.api.transport.reply(ev.detail, {}); const data = ev.detail.data as { deviceId?: string } | undefined; if (typeof data?.deviceId !== "string") return; logger.debug(`[lotus] set_audio_output: ${data.deviceId}`); mediaDevices.audioOutput.select(data.deviceId); }; w.lazyActions.on(LotusWidgetActions.SetAudioOutput, handler); const sub = mediaDevices.audioOutput.selected$.subscribe((selected) => { lotusSendToHost(LotusWidgetActions.AudioOutputState, { deviceId: selected?.id ?? null, }); }); return () => { sub.unsubscribe(); w.lazyActions.off(LotusWidgetActions.SetAudioOutput, handler); }; }