Skip to content

Commit 1f583c3

Browse files
committed
fix(macos): release a half-built system-audio tap, and say when it failed
Two gaps in the Core Audio tap, from review: - SystemAudioTap.start() creates the tap, then the aggregate device, then the IO proc, and any of the later steps can throw. A failure part-way left the objects already created allocated, and whether anything stopped the tap later depended on who held it. start() is now all or nothing: a failure releases what it had built. - A tap that could not start only emitted a warning, which the stop outcome never read. The take finished 'successfully' with silent system audio and nothing said. The stop outcome now carries that as its warning, next to an early-end one when both apply, which the editor already shows as a toast when the take opens (and the CLI lists).
1 parent 6533052 commit 1f583c3

3 files changed

Lines changed: 56 additions & 8 deletions

File tree

‎electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/SystemAudioTap.swift‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,17 @@ final class SystemAudioTap {
4343
}
4444

4545
/// Starts the tap. Blocks while macOS' permission prompt is on screen.
46+
///
47+
/// All or nothing: a failure part-way releases whatever was already created, so a tap
48+
/// that never started holds no Core Audio objects, whoever does or does not stop it later.
4649
func start() throws {
50+
var didStart = false
51+
defer {
52+
if !didStart {
53+
stop()
54+
}
55+
}
56+
4757
// Nothing to exclude: the helper plays no sound, which is all ScreenCaptureKit's
4858
// `excludesCurrentProcessAudio` ever took out.
4959
let description = CATapDescription(stereoGlobalTapButExcludeProcesses: [])
@@ -80,6 +90,7 @@ final class SystemAudioTap {
8090
"AudioDeviceCreateIOProcIDWithBlock"
8191
)
8292
try Self.check(AudioDeviceStart(aggregateID, procID), "AudioDeviceStart")
93+
didStart = true
8394
}
8495

8596
func stop() {

‎electron/recording/nativeMacCaptureStop.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
nativeMacDiscardTargets,
88
readNativeMacHelperEvents,
99
readNativeMacStopOutcome,
10+
SYSTEM_AUDIO_LOST_WARNING,
1011
sendNativeMacStopCommand,
1112
waitForNativeMacCaptureStop,
1213
} from "./nativeMacCaptureStop";
@@ -143,6 +144,31 @@ describe("readNativeMacStopOutcome", () => {
143144
});
144145
});
145146

147+
it("tells the user when the system audio they asked for could not be recorded", () => {
148+
const tapFailed = line({
149+
event: "warning",
150+
code: "system-audio-unavailable",
151+
message: "System audio could not be captured: AudioDeviceStart failed (OSStatus -1)",
152+
});
153+
expect(readNativeMacStopOutcome(started + tapFailed + stopped, null, TARGET)).toEqual({
154+
ok: true,
155+
screenVideoPath: TARGET,
156+
warning: SYSTEM_AUDIO_LOST_WARNING,
157+
});
158+
});
159+
160+
it("keeps both the early end and the lost system audio in one warning", () => {
161+
const tapFailed = line({ event: "warning", code: "system-audio-unavailable" });
162+
const outcome = readNativeMacStopOutcome(
163+
started + tapFailed + captureStopped + stopped,
164+
null,
165+
TARGET,
166+
);
167+
expect(outcome).toMatchObject({ ok: true, screenVideoPath: TARGET });
168+
expect(outcome?.ok && outcome.warning).toContain("Recording ended early");
169+
expect(outcome?.ok && outcome.warning).toContain(SYSTEM_AUDIO_LOST_WARNING);
170+
});
171+
146172
it("says why the take ended rather than quoting the raw writer error", () => {
147173
expect(readNativeMacStopOutcome(started + writerDied + writerFailed, null, TARGET)).toEqual({
148174
ok: false,

‎electron/recording/nativeMacCaptureStop.ts‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ const INTERRUPTION_ERROR_CODES = new Set([
5555
"capture-stopped-with-error",
5656
]);
5757

58+
/** Shown when a finished take could not capture the system audio it was asked for. */
59+
export const SYSTEM_AUDIO_LOST_WARNING = "System audio could not be recorded for this take.";
60+
5861
type HelperEvent = Record<string, unknown>;
5962

6063
function parseHelperEvent(line: string): HelperEvent | null {
@@ -136,6 +139,12 @@ export function readNativeMacStopOutcome(
136139
const errors = events.filter((event) => event.event === "error");
137140
const interruption = errors.find(isInterruption);
138141
const failure = lastWhere(errors, (event) => !isInterruption(event));
142+
// Not a failure of the take: the video and every other track are fine. But the system
143+
// audio the user asked for is silence, and a finished take with no word about it would
144+
// only be found out on playback.
145+
const systemAudioLost = events.some(
146+
(event) => event.event === "warning" && event.code === "system-audio-unavailable",
147+
);
139148
const exited = exit !== null;
140149

141150
// A finalized file outranks anything said before it: this is the take the
@@ -152,15 +161,17 @@ export function readNativeMacStopOutcome(
152161
exited,
153162
};
154163
}
155-
if (!interruption) {
156-
return { ok: true, screenVideoPath };
164+
const warnings: string[] = [];
165+
if (interruption) {
166+
const reason = messageOf(interruption).replace(/[.\s]+$/, "");
167+
warnings.push(`Recording ended early (${reason}). The part recorded until then was saved.`);
157168
}
158-
const reason = messageOf(interruption).replace(/[.\s]+$/, "");
159-
return {
160-
ok: true,
161-
screenVideoPath,
162-
warning: `Recording ended early (${reason}). The part recorded until then was saved.`,
163-
};
169+
if (systemAudioLost) {
170+
warnings.push(SYSTEM_AUDIO_LOST_WARNING);
171+
}
172+
return warnings.length > 0
173+
? { ok: true, screenVideoPath, warning: warnings.join(" ") }
174+
: { ok: true, screenVideoPath };
164175
}
165176

166177
if (failure) {

0 commit comments

Comments
 (0)