Recordly icon indicating copy to clipboard operation
Recordly copied to clipboard

macOS: recording silently truncates when SCStream stops mid-capture — didStopWithError only writes to stderr and nothing reads it

Open pxshaaa opened this issue 2 months ago • 2 comments

Problem

On macOS, when the SCStream stops mid-capture, the recording silently truncates. Nothing in the UI changes: the recording indicator stays on, the webcam keeps recording, the cursor track keeps logging, and the timer keeps counting. You discover minutes later, in the editor, that the screen video ends at a fraction of the take.

I lost two takes to this in one session (6:46 recorded → 55 s of video; 8:23 recorded → 115 s of video) and only found the cause by probing the files.

Type

bug

Evidence

Two sessions, full-display capture of a 5K display, mic on, system audio off. Each recording produces four artefacts; here is what each contains:

Session Screen video Its inline audio Webcam Cursor track Actually recorded
A 114.89 s 114.68 s 501.84 s 503.4 s ~8:23
B 55.02 s 56.76 s 406.16 s 408.6 s ~6:48

The signature is consistent and, I think, diagnostic:

  1. The video track and its inline audio track stop at the same instant, while the webcam (renderer-side MediaRecorder) and the cursor monitor (separate process) run the full duration. So it is not the writer failing on one input — it is the whole SCStream going away, taking both .screen and microphone sample buffers with it.
  2. The file is perfectly well-formed. ftyp / mdat / moov, and the indexed packet bytes account for 204,156,468 of the mdat's 204,156,490 — there is no unindexed tail to recover. finishWriting() ran normally on stop, because as far as AVAssetWriter is concerned nothing went wrong; it simply stopped being fed. (This is what distinguishes it from #821, where the moov is missing.)
  3. The capture does not degrade into the stop, it stops dead. The last second of the truncated file is a clean 60 fps cadence:
    ... 56.605  56.588  56.788  56.655  56.805   (pts_time, video)
    
    No lengthening gaps, no tail-off — healthy frames, then nothing. That is a teardown, not starvation.

Root cause

electron/native/ScreenCaptureKitRecorder.swift:512 — the whole handler:

func stream(_ stream: SCStream, didStopWithError error: Error) {
	fputs("Error: \(error.localizedDescription)\n", stderr)
	fflush(stderr)
}

It does not clear isRecording, does not finalize, does not attempt a restart, and does not notify Electron. The error goes to stderr.

electron/ipc/register/recording.ts:800 — where stderr goes:

captProc.stderr.on("data", (chunk: Buffer) => {
	setNativeCaptureOutputBuffer(nativeCaptureOutputBuffer + chunk.toString());
});

That buffer is inspected exactly once, immediately after waitForNativeCaptureStart, to test for MICROPHONE_CAPTURE_UNAVAILABLE (line 810). After that nothing reads it for the rest of the recording. So the helper reports the failure and no one is listening, which is why the UI carries on as if all is well.

Why the stream is being torn down in the first place

ScreenCaptureKitRecorder.swift:25,106:

let targetCaptureFPS = 60
let requestedFPS = max(targetCaptureFPS, config.fps ?? targetCaptureFPS)

max() makes 60 fps a floor, not a default — a lower config.fps cannot take effect. Full-display capture then sets streamConfig to display bounds × backing scale with no ceiling, so a 2560×1440 Retina display is captured at 5120×2880 @ 60 fps with queueDepth = 6.

The encoder cannot hold that, and the file shows it: in session B, 42.6 s of the 57 s timeline is drop-gaps (gaps up to 4.5 s), 622 frames total — videoInput.isReadyForMoreMediaData is false most of the time and those frames are silently discarded. Sustained back-pressure like that is what precedes the teardown in both of my sessions.

Two things fall out of that:

  • Window capture is not a workaround, it is heavier. The windowId path still configures the stream for the entire display at full scale, switches to kCVPixelFormatType_32BGRA (vs the 4:2:0 biplanar format used for display capture), and then crops every frame on the CPU in appendCroppedVideoFrame. More bytes per frame plus per-frame work on the shared sample handler queue.
  • With the fps floor hardcoded, a user whose stream is being starved has no in-app lever at all. They have to change their display resolution in System Settings.

Expected

If the stream dies mid-recording, the app should say so and stop, rather than continue presenting a recording that is no longer being captured. Concretely:

  1. didStopWithError should mark the capture as failed and drive finalization, so the partial file is closed deliberately and the error carries a reason.
  2. The helper should emit a machine-readable line (e.g. STREAM_STOPPED: <error>), matching the existing MICROPHONE_CAPTURE_UNAVAILABLE convention.
  3. The Electron side should keep watching stderr for the whole session rather than sampling it once at start, and surface a stop + notification. A watchdog on frame arrival (no accepted frame for N seconds while unpaused) would also catch silent stalls that never produce a didStopWithError.
  4. Independently: give config.fps real authority (config.fps ?? targetCaptureFPS rather than max(...)), and consider capping capture dimensions for full-display capture so 5K panels do not default to a 885 Mpixel/s stream.

Even just #1 and #2 would convert a silently lost take into an immediate, obvious failure.

Environment

  • Recordly 1.4.0 (dev.recordly.app), installed from the release DMG
  • macOS 26.2, Apple silicon
  • Native ScreenCaptureKit backend, full-display capture of a 27" 5K display (UI looks like 2560×1440), mic on, system audio off, webcam on
  • Disk had ~4 GB free, and the truncated file is 71 MB — not a disk-space failure

Possibly related

  • #822 — Mission Control gesture stops a window recording. Likely the same didStopWithError path, but there the stop is at least visible; here it is not.
  • #821 — long recording ends with a missing moov. Different failure mode: there the writer never finalizes, here it finalizes cleanly over a short file.
  • #854 — editor preview freezes on variable-frame-rate recordings. The 75%-drop-gap timeline described above is exactly the kind of source that produces.

pxshaaa avatar Sep 17 '26 09:09 pxshaaa

Another data point with the same signature, on a different machine and a much longer take.

Environment: Recordly 1.4.0, macOS 27.2, Apple M5 Max. Screen capture with mic on, webcam off. The screen video came out at 1280×720 (nominal 50 fps).

What happened: I started a recording at about 09:17 and stopped it at about 12:10 local time, roughly 2 h 50 min. I believed it was recording the whole time and did not see any error. The screen video ends at 42:54.

Artefact Duration / span
Screen video 2573.995 s (42.9 min)
Its inline audio 2573.984 s
Cursor track (.cursor.json) 108,000 samples at a steady 33 ms cadence, t = 82.7 min to 142.0 min, no gap over 1 s
Actually recorded about 2 h 50 min

The video and its audio stop at the same instant. The file is well-formed (ftyp / mdat / moov; last video and audio packets at 2573.98 s and 2573.97 s), and the cursor monitor kept sampling for at least another 99 minutes after the video ended. No other file from that session exists on disk. This matches the teardown you describe.

Drop gaps before the stop: 15,202 video frames over 2574 s, 5.9 fps on average. 5,419 inter-frame gaps exceed 0.1 s and together cover 2,221 s (86% of the timeline); the longest is 2.04 s. The last frames keep a clean 20 ms cadence right up to the end:

2573.902  2573.922  2573.942  2573.962  2573.982  2573.998   (pts_time, video)

So the stream was starved for most of the take even at 1280×720, then stopped dead. Capture resolution alone may not explain the back-pressure.

Two smaller observations in case they help:

  • The cursor track holds exactly 108,000 samples (60 min at 30 Hz) and starts at t = 82.7 min, so it looks capped. Its last sample (t = 142.0 min) is also about 30 minutes before the recording was stopped (t ≈ 172 min).
  • The app did not restart during the session: updater.log shows launches at 09:14 and 13:41 local time only.

I'd second the expected behavior in the original report: surface didStopWithError and stop visibly. In my case the lost two hours were a live session I cannot re-record.

kiki830621 avatar Sep 30 '26 06:09 kiki830621

Follow-up to my comment above: it has happened three more times on the same setup (Recordly 1.4.0, macOS 27.2, M5 Max, mic on, webcam off), each at a different output resolution.

Take Output Take length (cursor track end ≈ stop) Screen video Inline audio Frames
2026-10-06 2648×2086 103.1 min 69.2 min 69.1 min 52.5 fps avg, 2 gaps > 0.1 s (longest 0.18 s)
2026-10-07 a 1920×1080 111.3 min 10.5 min 10.5 min mostly static slides, 1.4 fps avg
2026-10-07 b 2648×2086 41.1 min 11.4 min 11.4 min 52.2 fps avg, no gaps > 0.1 s (longest 0.07 s)

In each case the cursor track's last sample is within a few seconds of when I pressed stop, so the cursor monitor ran for the whole take, while the screen video and its audio end together much earlier. ffprobe reads all three files normally.

The two 2648×2086 takes argue against back-pressure as the trigger. The screen was changing continuously, frames arrive at a steady ~52 fps with essentially no drop gaps, and the last frames keep a normal cadence right up to the end:

2026-10-06:    4151.345  4151.362  4151.378  4151.395  4151.412  4151.428
2026-10-07 b:   685.645   685.670   685.687   685.712   685.728   685.745

So the stream stops dead without any visible starvation beforehand.

A correction to my earlier comment: that take was mostly static slides, and ScreenCaptureKit only produces new frames when the screen content changes, so the long inter-frame gaps I counted there are not good evidence of starvation.

It is also intermittent with identical settings. Of my last nine takes, four were truncated (42.9 of ~172 min, 69.2 of 103.1, 10.5 of 111.3, 11.4 of 41.1). The other five, including a 154-minute take, came out complete. I don't see a common elapsed time or file size at the cut.

+1 for #1006: a visible stop would have let me restart each of these instead of losing most of the take.

kiki830621 avatar Oct 07 '26 10:10 kiki830621