Skip to content

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

fix(macos): release a half-built system-audio tap, and say when it failed

Etienne Lescot05ba5dd0c165
diff --git a/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/SystemAudioTap.swift b/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/SystemAudioTap.swift
index aac3c067e..4b7eafc63 100644
--- a/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/SystemAudioTap.swift
+++ b/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/SystemAudioTap.swift
@@ -43,7 +43,17 @@ final class SystemAudioTap {
}
/// Starts the tap. Blocks while macOS' permission prompt is on screen.
+ ///
+ /// All or nothing: a failure part-way releases whatever was already created, so a tap
+ /// that never started holds no Core Audio objects, whoever does or does not stop it later.
func start() throws {
+ var didStart = false
+ defer {
+ if !didStart {
+ stop()
+ }
+ }
+
// Nothing to exclude: the helper plays no sound, which is all ScreenCaptureKit's
// `excludesCurrentProcessAudio` ever took out.
let description = CATapDescription(stereoGlobalTapButExcludeProcesses: [])
@@ -80,6 +90,7 @@ final class SystemAudioTap {
"AudioDeviceCreateIOProcIDWithBlock"
)
try Self.check(AudioDeviceStart(aggregateID, procID), "AudioDeviceStart")
+ didStart = true
}
func stop() {
diff --git a/electron/recording/nativeMacCaptureStop.test.ts b/electron/recording/nativeMacCaptureStop.test.ts
index 9855b7c2d..725856799 100644
--- a/electron/recording/nativeMacCaptureStop.test.ts
+++ b/electron/recording/nativeMacCaptureStop.test.ts
@@ -7,6 +7,7 @@ import {
nativeMacDiscardTargets,
readNativeMacHelperEvents,
readNativeMacStopOutcome,
+ SYSTEM_AUDIO_LOST_WARNING,
sendNativeMacStopCommand,
waitForNativeMacCaptureStop,
} from "./nativeMacCaptureStop";
@@ -143,6 +144,31 @@ describe("readNativeMacStopOutcome", () => {
});
});
+ it("tells the user when the system audio they asked for could not be recorded", () => {
+ const tapFailed = line({
+ event: "warning",
+ code: "system-audio-unavailable",
+ message: "System audio could not be captured: AudioDeviceStart failed (OSStatus -1)",
+ });
+ expect(readNativeMacStopOutcome(started + tapFailed + stopped, null, TARGET)).toEqual({
+ ok: true,
+ screenVideoPath: TARGET,
+ warning: SYSTEM_AUDIO_LOST_WARNING,
+ });
+ });
+
+ it("keeps both the early end and the lost system audio in one warning", () => {
+ const tapFailed = line({ event: "warning", code: "system-audio-unavailable" });
+ const outcome = readNativeMacStopOutcome(
+ started + tapFailed + captureStopped + stopped,
+ null,
+ TARGET,
+ );
+ expect(outcome).toMatchObject({ ok: true, screenVideoPath: TARGET });
+ expect(outcome?.ok && outcome.warning).toContain("Recording ended early");
+ expect(outcome?.ok && outcome.warning).toContain(SYSTEM_AUDIO_LOST_WARNING);
+ });
+
it("says why the take ended rather than quoting the raw writer error", () => {
expect(readNativeMacStopOutcome(started + writerDied + writerFailed, null, TARGET)).toEqual({
ok: false,
diff --git a/electron/recording/nativeMacCaptureStop.ts b/electron/recording/nativeMacCaptureStop.ts
index 42e665f47..3104cd850 100644
--- a/electron/recording/nativeMacCaptureStop.ts
+++ b/electron/recording/nativeMacCaptureStop.ts
@@ -55,6 +55,9 @@ const INTERRUPTION_ERROR_CODES = new Set([
"capture-stopped-with-error",
]);
+/** Shown when a finished take could not capture the system audio it was asked for. */
+export const SYSTEM_AUDIO_LOST_WARNING = "System audio could not be recorded for this take.";
+
type HelperEvent = Record<string, unknown>;
function parseHelperEvent(line: string): HelperEvent | null {
@@ -136,6 +139,12 @@ export function readNativeMacStopOutcome(
const errors = events.filter((event) => event.event === "error");
const interruption = errors.find(isInterruption);
const failure = lastWhere(errors, (event) => !isInterruption(event));
+ // Not a failure of the take: the video and every other track are fine. But the system
+ // audio the user asked for is silence, and a finished take with no word about it would
+ // only be found out on playback.
+ const systemAudioLost = events.some(
+ (event) => event.event === "warning" && event.code === "system-audio-unavailable",
+ );
const exited = exit !== null;
// A finalized file outranks anything said before it: this is the take the
@@ -152,15 +161,17 @@ export function readNativeMacStopOutcome(
exited,
};
}
- if (!interruption) {
- return { ok: true, screenVideoPath };
+ const warnings: string[] = [];
+ if (interruption) {
+ const reason = messageOf(interruption).replace(/[.\s]+$/, "");
+ warnings.push(`Recording ended early (${reason}). The part recorded until then was saved.`);
}
- const reason = messageOf(interruption).replace(/[.\s]+$/, "");
- return {
- ok: true,
- screenVideoPath,
- warning: `Recording ended early (${reason}). The part recorded until then was saved.`,
- };
+ if (systemAudioLost) {
+ warnings.push(SYSTEM_AUDIO_LOST_WARNING);
+ }
+ return warnings.length > 0
+ ? { ok: true, screenVideoPath, warning: warnings.join(" ") }
+ : { ok: true, screenVideoPath };
}
if (failure) {