The AI-native screen recorder. Record your screen and talk; the people you share it with and your AI agents both get it.
Rust agent-readableai-agentsarecclaude-codecodexcursorlocal-firstmacosprivacyrustscreen-recorderscreen-recordingtauritranscriptiontypescriptwindows
import type { AudioDeviceDto, AudioOptionsDto, PermissionStatus } from "./bridge";
/**
* Microphone selection has two layers, the same split Cap uses: the saved
* PREFERENCE (what the user picked, persisted verbatim and never rewritten
* because a device happened to be unplugged) and the RESOLVED value (what
* this recording actually uses). A preference for a mic that is currently
* missing resolves to the system default for that recording only, and the
* mic is selected again the moment it is back.
*/
export type NoMicReason =
| "off"
| "permissionDenied"
| "noDevice"
| "deviceUnavailable";
export type PreStartMicDecision =
| {
kind: "start";
audioOptions: AudioOptionsDto;
signature: string;
}
| {
kind: "prompt";
reason: NoMicReason;
unavailableDeviceId: string | null;
signature: string;
};
/** Stable fingerprint of the current audio setup. When this changes, a
* remembered "Start without microphone" choice resets. */
export function audioSetupSignature(
options: Pick<AudioOptionsDto, "includeMic" | "micDeviceId">,
devices: AudioDeviceDto[],
micPermission: PermissionStatus | null,
): string {
const deviceIds = devices
.map((device) => `${device.id}:${device.is_default ? "1" : "0"}`)
.sort()
.join(",");
return JSON.stringify({
includeMic: options.includeMic,
micDeviceId: options.includeMic ? options.micDeviceId : null,
micPermission,
devices: deviceIds,
});
}
/** Decide whether a take can start immediately or should first confirm
* starting without a working microphone. */
export function decidePreStartMic({
options,
devices,
micPermission,
rememberedSignature,
}: {
options: AudioOptionsDto;
devices: AudioDeviceDto[];
micPermission: PermissionStatus | null;
rememberedSignature: string | null;
}): PreStartMicDecision {
const signature = audioSetupSignature(options, devices, micPermission);
const remembered = rememberedSignature !== null && rememberedSignature === signature;
if (!options.includeMic) {
if (remembered) {
return { kind: "start", audioOptions: { ...options, micDeviceId: null }, signature };
}
return { kind: "prompt", reason: "off", unavailableDeviceId: null, signature };
}
let reason: NoMicReason | null = null;
let unavailableDeviceId: string | null = null;
if (micPermission === "denied") {
reason = "permissionDenied";
} else if (devices.length === 0) {
reason = "noDevice";
} else if (options.micDeviceId !== null && !micDeviceConnected(options, devices)) {
reason = "deviceUnavailable";
unavailableDeviceId = options.micDeviceId;
}
if (reason === null) {
return { kind: "start", audioOptions: resolveAudioOptions(options, devices), signature };
}
if (remembered) {
return {
kind: "start",
audioOptions: { ...options, includeMic: false, micDeviceId: null },
signature,
};
}
return { kind: "prompt", reason, unavailableDeviceId, signature };
}
export function defaultAudioDeviceId(devices: AudioDeviceDto[]): string | null {
return devices.find((device) => device.is_default)?.id ?? devices[0]?.id ?? null;
}
export function micDeviceConnected(
options: AudioOptionsDto,
devices: AudioDeviceDto[],
): boolean {
return (
!!options.micDeviceId && devices.some((device) => device.id === options.micDeviceId)
);
}
/** Options for a recording: the preferred mic when connected, else system
* default. Never persist the result — it would erase the preference. */
export function resolveAudioOptions(
options: AudioOptionsDto,
devices: AudioDeviceDto[],
): AudioOptionsDto {
if (!options.includeMic) return { ...options, micDeviceId: null };
if (micDeviceConnected(options, devices)) return options;
return { ...options, micDeviceId: null };
}
/** The preferred mic is turned on, saved, and not currently connected. */
export function disconnectedMicDeviceId(
options: AudioOptionsDto,
devices: AudioDeviceDto[],
): string | null {
if (!options.includeMic || !options.micDeviceId) return null;
return micDeviceConnected(options, devices) ? null : options.micDeviceId;
}
/** Concrete device to open for a signal check (the default's real id, so
* the check names the device it actually listened to). */
export function micDeviceIdForCheck(
options: AudioOptionsDto,
devices: AudioDeviceDto[],
): string | null {
if (!options.includeMic) return null;
if (micDeviceConnected(options, devices)) return options.micDeviceId;
return defaultAudioDeviceId(devices);
}