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): stop the take when the writer dies mid-capture, and tell the user

EtienneLescot4ee0bff7f85b
diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.ts
index 7f837ae19..01d3eef44 100644
--- a/electron/ipc/handlers.ts
+++ b/electron/ipc/handlers.ts
@@ -1485,8 +1485,12 @@ function inspectNativeMacCaptureOutput() {
}
}
-function attachNativeMacCaptureOutputDrain(proc: ChildProcessWithoutNullStreams) {
+function attachNativeMacCaptureOutputDrain(
+ proc: ChildProcessWithoutNullStreams,
+ onErrorDuringCapture: () => void,
+) {
let lineBuffer = "";
+ let recordingStarted = false;
const drain = (chunk: Buffer) => {
const text = chunk.toString();
nativeMacCaptureOutput += text;
@@ -1497,6 +1501,18 @@ function attachNativeMacCaptureOutputDrain(proc: ChildProcessWithoutNullStreams)
const event = tryParseNativeHelperEvent(line.trim());
if (event) {
dispatchNativeMacHelperEvent(event);
+ if (event.event === "recording-started") {
+ recordingStarted = true;
+ }
+ // The start and stop waits only listen while they are pending, so an
+ // error the helper raises mid-take (a dead writer, a stream that
+ // stopped) used to sit in the buffer until the user pressed stop, while
+ // the HUD counted on for minutes (issue #621). Hooked here rather than
+ // on `nativeMacCaptureEvents`, which the stop wait replays from the
+ // buffer: this sees each line once, live.
+ if (event.event === "error" && recordingStarted && nativeMacCaptureProcess === proc) {
+ onErrorDuringCapture();
+ }
}
}
};
@@ -2825,7 +2841,14 @@ export function registerIpcHandlers(
stdio: ["pipe", "pipe", "pipe"],
});
nativeMacCaptureProcess = proc;
- attachNativeMacCaptureOutputDrain(proc);
+ // Drives the renderer's own stop, the same one the tray's Stop Recording
+ // sends: it clears the HUD and surfaces the helper's error as the result.
+ attachNativeMacCaptureOutputDrain(proc, () => {
+ const hudWindow = getMainWindow();
+ if (hudWindow && !hudWindow.isDestroyed()) {
+ hudWindow.webContents.send("stop-recording-from-tray");
+ }
+ });
await waitForNativeMacCaptureStart(proc);
const captureStartedAtMs = Date.now();
diff --git a/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/ScreenCaptureRecorder.swift b/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/ScreenCaptureRecorder.swift
index 9d549c724..f9dfd547c 100644
--- a/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/ScreenCaptureRecorder.swift
+++ b/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/ScreenCaptureRecorder.swift
@@ -312,6 +312,13 @@ final class ScreenCaptureRecorder: NSObject, SCStreamOutput, SCStreamDelegate {
audioMixer?.beginTimeline(at: presentationTime)
startAudioTicker()
}
+ // Checked before the readiness gate, not only at a false append: a failed
+ // writer is not guaranteed to call itself ready, and an append that is never
+ // attempted can never report the failure (issue #621).
+ if writer.status == .failed {
+ reportWriterFailure("writer status")
+ return
+ }
if videoInput.isReadyForMoreMediaData {
videoTimestampGate.record(presentationTime)
@@ -346,6 +353,12 @@ final class ScreenCaptureRecorder: NSObject, SCStreamOutput, SCStreamDelegate {
/// settles and every failure becomes the "Saving..." hang instead of an error.
/// This event answers "when did the writer die"; that one answers "did stopping
/// work". Two questions, two codes.
+ ///
+ /// It also ends the capture, the way `didStopWithError` does. A failed writer
+ /// never recovers, so every frame after it is dropped; issue #621 is a take
+ /// whose writer died at 75 s while capture ran on for 22 more minutes. The
+ /// process stays up and still answers `stop`, so the Electron stop path works
+ /// unchanged and reads this event back as the reason.
private func reportWriterFailure(_ stage: String) {
guard !didReportWriterFailure, let writer else {
return
@@ -353,10 +366,14 @@ final class ScreenCaptureRecorder: NSObject, SCStreamOutput, SCStreamDelegate {
didReportWriterFailure = true
emitError(
code: "writer-failed-during-capture",
- message: "\(stage): "
+ message: "Recording stopped: the video file could not be written (\(stage): "
+ (writer.error.map { "\($0)" }
- ?? "AVAssetWriter status \(writer.status.rawValue)"),
+ ?? "AVAssetWriter status \(writer.status.rawValue)")
+ + ").",
)
+ Task {
+ await stop()
+ }
}
/// Once per take, with the numbers that tell the cause apart. A refusal with a non-zero pause
@@ -638,7 +655,16 @@ final class ScreenCaptureRecorder: NSObject, SCStreamOutput, SCStreamDelegate {
leeway: .milliseconds(5)
)
timer.setEventHandler { [weak self] in
- self?.audioMixer?.tick()
+ guard let self else {
+ return
+ }
+ // A still screen delivers no complete frames, so the frame path alone
+ // could sit on a dead writer for as long as nothing on screen moves.
+ if self.writer?.status == .failed {
+ self.reportWriterFailure("writer status")
+ return
+ }
+ self.audioMixer?.tick()
}
audioTicker = timer
timer.resume()