Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions electron/ipc/recording/mac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,16 @@ export function attachNativeCaptureLifecycle(process: ChildProcessWithoutNullStr
}
});

const streamStoppedMatch = nativeCaptureOutputBuffer.match(/STREAM_STOPPED:\s*(.+)/);
const reason = nativeCaptureOutputBuffer.includes("WINDOW_UNAVAILABLE")
? "window-unavailable"
: "capture-stopped";
: streamStoppedMatch
? "stream-stopped"
: "capture-stopped";
const message =
reason === "window-unavailable"
? "The selected window is no longer capturable. Please reselect a window."
: "Recording stopped unexpectedly.";
: streamStoppedMatch?.[1]?.trim() || "Recording stopped unexpectedly.";

emitRecordingInterrupted(reason, message);
});
Expand Down
40 changes: 40 additions & 0 deletions electron/ipc/recording/macLifecycle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { EventEmitter } from "node:events";
import { beforeEach, describe, expect, it, vi } from "vitest";
import {
setNativeCaptureOutputBuffer,
setNativeCaptureStopRequested,
setNativeScreenRecordingActive,
} from "../state";

const send = vi.hoisted(() => vi.fn());
vi.mock("electron", () => ({
BrowserWindow: { getAllWindows: () => [{ isDestroyed: () => false, webContents: { send } }] },
}));
vi.mock("../cursor/telemetry", () => ({}));
vi.mock("../utils", () => ({}));
vi.mock("./diagnostics", () => ({}));
vi.mock("./macCompanionAudio", () => ({}));
vi.mock("./prune", () => ({}));

import { attachNativeCaptureLifecycle } from "./mac";

describe("macOS native capture lifecycle", () => {
beforeEach(() => {
send.mockClear();
setNativeScreenRecordingActive(true);
setNativeCaptureStopRequested(false);
setNativeCaptureOutputBuffer("");
});

it("reports a stopped stream to the renderer with its actual error", () => {
const process = new EventEmitter();
attachNativeCaptureLifecycle(process as Parameters<typeof attachNativeCaptureLifecycle>[0]);
setNativeCaptureOutputBuffer("STREAM_STOPPED: Screen capture permission was revoked\n");
process.emit("close", 1);

expect(send).toHaveBeenCalledWith("recording-interrupted", {
reason: "stream-stopped",
message: "Screen capture permission was revoked",
});
});
});
18 changes: 16 additions & 2 deletions electron/native/ScreenCaptureKitRecorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ final class ScreenCaptureRecorder: NSObject, SCStreamOutput, SCStreamDelegate {
}
writesSystemAudioToSeparateTrack = capturesSystemAudio
writesMicrophoneToSeparateTrack = capturesSystemAudio && capturesMicrophone
let requestedFPS = max(targetCaptureFPS, config.fps ?? targetCaptureFPS)
let requestedFPS = config.fps.flatMap { (1...Int(Int32.max)).contains($0) ? $0 : nil } ?? targetCaptureFPS
streamConfig.minimumFrameInterval = CMTime(value: 1, timescale: CMTimeScale(requestedFPS))
streamConfig.queueDepth = 6
streamConfig.pixelFormat = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
Expand Down Expand Up @@ -513,8 +513,22 @@ final class ScreenCaptureRecorder: NSObject, SCStreamOutput, SCStreamDelegate {
}

func stream(_ stream: SCStream, didStopWithError error: Error) {
fputs("Error: \(error.localizedDescription)\n", stderr)
let reason = error.localizedDescription.replacingOccurrences(of: "\n", with: " ")
fputs("STREAM_STOPPED: \(reason)\n", stderr)
fflush(stderr)

Task { [weak self] in
guard let self else { return }
let finalization = await self.finalizeCapture(interactive: false)
if finalization.interactiveStopParticipated {
return
}
if case let .success(outputPath) = finalization.outputResult {
print("Recording stopped. Output path: \(outputPath)")
fflush(stdout)
}
exit(1)
}
}

/// Starts one finalization operation after all previously delivered samples on
Expand Down
7 changes: 4 additions & 3 deletions electron/native/ScreenCaptureKitRecorder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const recorderSource = readFileSync(
fileURLToPath(new URL("./ScreenCaptureKitRecorder.swift", import.meta.url)),
"utf8",
);

describe("ScreenCaptureKitRecorder finalization coordination", () => {
it("marks manual stops as participants in the shared finalization", () => {
expect(recorderSource).toContain("finalizeCapture(interactive: true)");
Expand Down Expand Up @@ -85,9 +84,11 @@ describe("ScreenCaptureKitRecorder window capture", () => {
});
});


describe("ScreenCaptureKitRecorder first frame timing", () => {
const callback = recorderSource.slice(recorderSource.indexOf("func stream(_ stream:"), recorderSource.indexOf("func stream(_ stream:") + 5000);
const callback = recorderSource.slice(
recorderSource.indexOf("func stream(_ stream:"),
recorderSource.indexOf("func stream(_ stream:") + 5000,
);
it("validates a complete frame and writer readiness before setting time zero", () => {
const clock = callback.indexOf("adjustedPresentationTime(for:");
expect(clock).toBeGreaterThan(callback.indexOf("status == .complete"));
Expand Down