Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
fix(macos): let a second stop wait for the shutdown already running
EtienneLescotc4f86d3faff7
diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.tsindex 01d3eef44..4e9ba5429 100644--- a/electron/ipc/handlers.ts+++ b/electron/ipc/handlers.ts@@ -93,6 +93,7 @@ import {import { patchWebmDurationOnDisk } from "../recording/webm-duration";import { reindexRecordingOnDisk } from "../recording/webm-seek-index";import { registerNativeBridgeHandlers } from "./nativeBridge";+import { createNativeMacMidCaptureErrorWatch } from "./nativeMacMidCaptureErrorWatch";import { registerRecordingPrefsHandlers } from "./recordingPrefs";import { RecordingStreamRegistry, registerRecordingStreamHandlers } from "./recordingStream";@@ -1490,7 +1491,12 @@ function attachNativeMacCaptureOutputDrain(onErrorDuringCapture: () => void,) {let lineBuffer = "";- let recordingStarted = false;+ // Hooked here rather than on `nativeMacCaptureEvents`, which the stop wait+ // replays from the buffer: the drain sees each line once, live.+ const watchForMidCaptureError = createNativeMacMidCaptureErrorWatch(+ () => nativeMacCaptureProcess === proc,+ onErrorDuringCapture,+ );const drain = (chunk: Buffer) => {const text = chunk.toString();nativeMacCaptureOutput += text;@@ -1501,18 +1507,7 @@ function attachNativeMacCaptureOutputDrain(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();- }+ watchForMidCaptureError(event);}}};diff --git a/electron/ipc/nativeMacMidCaptureErrorWatch.test.ts b/electron/ipc/nativeMacMidCaptureErrorWatch.test.tsnew file mode 100644index 000000000..c22b9c0e9--- /dev/null+++ b/electron/ipc/nativeMacMidCaptureErrorWatch.test.ts@@ -0,0 +1,49 @@+import { describe, expect, it, vi } from "vitest";+import { createNativeMacMidCaptureErrorWatch } from "./nativeMacMidCaptureErrorWatch";++describe("createNativeMacMidCaptureErrorWatch", () => {+ const error = { event: "error", code: "writer-failed-during-capture" };++ it("fires on an error raised after recording started", () => {+ const onError = vi.fn();+ const watch = createNativeMacMidCaptureErrorWatch(() => true, onError);++ watch({ event: "ready" });+ watch({ event: "recording-started" });+ watch(error);++ expect(onError).toHaveBeenCalledTimes(1);+ });++ it("leaves an error raised before recording started to the start wait", () => {+ const onError = vi.fn();+ const watch = createNativeMacMidCaptureErrorWatch(() => true, onError);++ watch(error);++ expect(onError).not.toHaveBeenCalled();+ });++ it("ignores events that are not errors", () => {+ const onError = vi.fn();+ const watch = createNativeMacMidCaptureErrorWatch(() => true, onError);++ watch({ event: "recording-started" });+ watch({ event: "warning", code: "stop-capture-failed" });+ watch({ event: "recording-stopped" });++ expect(onError).not.toHaveBeenCalled();+ });++ it("ignores a helper that is no longer the current process", () => {+ const onError = vi.fn();+ let current = true;+ const watch = createNativeMacMidCaptureErrorWatch(() => current, onError);++ watch({ event: "recording-started" });+ current = false;+ watch(error);++ expect(onError).not.toHaveBeenCalled();+ });+});diff --git a/electron/ipc/nativeMacMidCaptureErrorWatch.ts b/electron/ipc/nativeMacMidCaptureErrorWatch.tsnew file mode 100644index 000000000..4c62f6b7e--- /dev/null+++ b/electron/ipc/nativeMacMidCaptureErrorWatch.ts@@ -0,0 +1,24 @@+/**+ * Decides, one live helper event at a time, when the macOS helper raised an+ * error mid-take.+ *+ * The start and stop waits only listen while they are pending, so an error the+ * helper raises between them (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). Before `recording-started` the start wait owns the+ * error, and a helper that is no longer the current process has nobody to stop.+ */+export function createNativeMacMidCaptureErrorWatch(+ isCurrentProcess: () => boolean,+ onError: () => void,+) {+ let recordingStarted = false;+ return (event: Record<string, unknown>) => {+ if (event.event === "recording-started") {+ recordingStarted = true;+ }+ if (event.event === "error" && recordingStarted && isCurrentProcess()) {+ onError();+ }+ };+}diff --git a/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/ScreenCaptureRecorder.swift b/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/ScreenCaptureRecorder.swiftindex f9dfd547c..785c4a3a1 100644--- a/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/ScreenCaptureRecorder.swift+++ b/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/ScreenCaptureRecorder.swift@@ -130,7 +130,11 @@ final class ScreenCaptureRecorder: NSObject, SCStreamOutput, SCStreamDelegate {private var didStartWriting = falseprivate var didEmitRecordingStarted = falseprivate var didReportWriterFailure = false- private var isStopping = false+ /// The one shutdown, however many callers ask for it. A writer failure starts it on+ /// its own task, and the `stop` Electron sends right after has to wait for that run:+ /// the command loop exits the process as soon as `stop()` returns, which would cut+ /// `finishWriter()` off before its terminal event.+ private var shutdownTask: Task<Void, Never>?private var isPaused = falseprivate var pauseStartedAt: CMTime?private var totalPausedDuration = CMTime.zero@@ -184,17 +188,20 @@ final class ScreenCaptureRecorder: NSObject, SCStreamOutput, SCStreamDelegate {}func stop() async {- let shouldStop = stateQueue.sync {- if isStopping {- return false+ let task = stateQueue.sync { () -> Task<Void, Never> in+ if let shutdownTask {+ return shutdownTask}- isStopping = true- return true- }- if !shouldStop {- return+ let task = Task {+ await self.performStop()+ }+ shutdownTask = task+ return task}+ await task.value+ }+ private func performStop() async {do {try await stream?.stopCapture()} catch {@@ -210,7 +217,7 @@ final class ScreenCaptureRecorder: NSObject, SCStreamOutput, SCStreamDelegate {func pause() {let didPause = stateQueue.sync {- if isStopping || isPaused {+ if shutdownTask != nil || isPaused {return false}@@ -229,7 +236,7 @@ final class ScreenCaptureRecorder: NSObject, SCStreamOutput, SCStreamDelegate {func resume() {let didResume = stateQueue.sync {- if isStopping || !isPaused {+ if shutdownTask != nil || !isPaused {return false}