import { RecorderBusyError } from './recorder/recorder-errors' import type { RecorderOwner, RecorderSession, RecordingRuntimeSnapshot, } from './recorder/recorder-types' /** The part of the shared recorder facade this policy needs. */ export interface CaptureRecorderPort { acquire(owner: RecorderOwner): Promise } function isLive(snapshot: RecordingRuntimeSnapshot): boolean { return snapshot.state === 'recording' || snapshot.state === 'paused' } function isReattachable(snapshot: RecordingRuntimeSnapshot): boolean { return (snapshot.state === 'stopped' || snapshot.state === 'recoverable') && snapshot.recording !== null } /** * Stops the microphone when the signed-in session is lost involuntarily, but * keeps the meeting capture's file so its owner can process it after signing * back in (Record restores a stopped capture as a recoverable recording). * * - A Talk turn is transient conversation input, not durable work: discarded. * - A capture the backend cannot hand back after the screen unmounts (the * in-process iOS recorder reports no reattachable snapshot) is discarded as * before, because a stranded stopped capture would block every new * recording. */ export async function stopLiveCaptureKeepingFile(recorder: CaptureRecorderPort): Promise { let session: RecorderSession try { session = await recorder.acquire('record') } catch (error) { if (!(error instanceof RecorderBusyError)) throw error const holder = await recorder.acquire(error.holder) await holder.cancel() return } try { await session.stop() } catch { // Nothing is driven by this JS process (idle, already stopped, or a // foreground-service capture that outlived a restart): inspect it below. } let snapshot = await session.restore() if (isLive(snapshot)) { await session.stop() snapshot = await session.restore() } if (isReattachable(snapshot)) return await session.cancel() }