Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

3.6 KB
Raw Blame
import type { Rectangle } from "electron";
import type { CursorCaptureMode } from "./recordingSession";

export type NativeMacSourceType = "display" | "window";

export type NativeMacRecordingRequest = {
	schemaVersion: 1;
	recordingId?: number;
	/** Main-process injected native BrowserWindow IDs excluded from display capture. */
	excludedWindowIds?: number[];
	/** Display captures: leave Finder's desktop icons out. Notifications are always left out. */
	hideDesktopIcons?: boolean;
	source: {
		type: NativeMacSourceType;
		sourceId: string;
		displayId?: number;
		windowId?: number;
		bounds?: Rectangle;
	};
	video: {
		fps: number;
		width: number;
		height: number;
		bitrate?: number;
		hideSystemCursor: boolean;
	};
	audio: {
		system: {
			enabled: boolean;
		};
		microphone: {
			enabled: boolean;
			deviceId?: string;
			deviceName?: string;
			gain: number;
		};
	};
	webcam: {
		enabled: boolean;
		deviceId?: string;
		deviceName?: string;
		width: number;
		height: number;
		fps: number;
	};
	cursor: {
		mode: CursorCaptureMode;
	};
	outputs: {
		screenPath: string;
		manifestPath?: string;
	};
};

export type NativeMacHelperReadyEvent = {
	event: "ready";
	schemaVersion: 1;
};

export type NativeMacHelperRecordingStartedEvent = {
	event: "recording-started";
	timestampMs: number;
	captureBounds?: Rectangle;
};

export type NativeMacHelperRecordingStoppedEvent = {
	event: "recording-stopped";
	screenPath: string;
	/** Frames the writer was not ready for or refused. Absent from helpers built before #937. */
	droppedVideoFrames?: number;
};

export type NativeMacHelperWarningEvent = {
	event: "warning";
	code: string;
	message: string;
};

export type NativeMacHelperErrorEvent = {
	event: "error";
	code: string;
	message: string;
};

export type NativeMacHelperEvent =
	| NativeMacHelperReadyEvent
	| NativeMacHelperRecordingStartedEvent
	| NativeMacHelperRecordingStoppedEvent
	| NativeMacHelperWarningEvent
	| NativeMacHelperErrorEvent;

export type NativeMacRecordingStartResult = {
	success: boolean;
	recordingId?: number;
	path?: string;
	helperPath?: string;
	/** Wall-clock ms of the take's first appended frame: the file's t=0. */
	startedAtMs?: number;
	/** The helper could not resolve the selected device and is using the system default. */
	microphoneDefaulted?: boolean;
	/** The microphone was asked for, but this macOS cannot capture it: the take has no voice. */
	microphoneUnavailable?: boolean;
	/** The microphone was asked for, but macOS lists no audio input: the take has no voice. */
	microphoneNotFound?: boolean;
	error?: string;
};

export function collectMacCaptureExcludedWindowIds(
	mediaSourceIds: ReadonlyArray<string | null | undefined>,
) {
	const windowIds: number[] = [];
	const seen = new Set<number>();
	for (const sourceId of mediaSourceIds) {
		const windowId = parseMacWindowIdFromSourceId(sourceId);
		if (
			windowId === null ||
			!Number.isSafeInteger(windowId) ||
			windowId <= 0 ||
			windowId > 0xffff_ffff ||
			seen.has(windowId)
		) {
			continue;
		}

		seen.add(windowId);
		windowIds.push(windowId);
	}
	return windowIds;
}

export function parseMacWindowIdFromSourceId(sourceId?: string | null) {
	if (!sourceId?.startsWith("window:")) {
		return null;
	}

	const windowIdPart = sourceId.split(":")[1];
	if (!windowIdPart || !/^\d+$/.test(windowIdPart)) {
		return null;
	}

	return Number(windowIdPart);
}

export function parseMacDisplayIdFromSourceId(sourceId?: string | null) {
	if (!sourceId?.startsWith("screen:")) {
		return null;
	}

	const displayIdPart = sourceId.split(":")[1];
	if (!displayIdPart || !/^\d+$/.test(displayIdPart)) {
		return null;
	}

	return Number(displayIdPart);
}