From 165189acce74df4dcdda6ef3bb26dd33c6aa6043 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Mon, 6 Jul 2026 11:00:42 -0500 Subject: fix(recording): stop wf-recorder start-race producing 0.5s fragment files On Wayland the video start path signalled any running wf-recorder with pkill -INT and then waited a fixed 0.1s before launching a new one. Releasing the compositor capture takes longer than that, so the new wf-recorder launched into a still-held grab, died immediately, and ffmpeg finalized a ~0.5-second fragment .mkv. Seeing nothing record, I'd press the key again and burst out a pile of fragments until the capture finally freed. I replaced the fixed wait with a poll that waits until no wf-recorder remains (capped at 2s), the same move the stop path already made when it swapped its own fixed wait for cj/recording--wait-for-exit. Two supporting changes came with it. wf-recorder stderr no longer goes to /dev/null, so a failed grab shows up in the process buffer. And the sentinel now tells a ~0.5s failed start from a real stop, saying "failed to start, try again" instead of silently clearing, so one failure doesn't snowball into a blind-retry burst. The poll and the fail-fast timing are unit-tested. The live capture needs a manual check. --- ...t-video-audio-recording--build-video-command.el | 8 ++++ tests/test-video-audio-recording--start-race.el | 56 ++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 tests/test-video-audio-recording--start-race.el (limited to 'tests') diff --git a/tests/test-video-audio-recording--build-video-command.el b/tests/test-video-audio-recording--build-video-command.el index 4f290978..1ffce95b 100644 --- a/tests/test-video-audio-recording--build-video-command.el +++ b/tests/test-video-audio-recording--build-video-command.el @@ -27,6 +27,14 @@ (should (string-match-p "-i pipe:0" cmd)) (should (string-match-p "-c:v copy" cmd)))))) +(ert-deftest test-video-audio-recording--build-video-command-normal-wayland-keeps-wf-recorder-stderr () + "Wayland command does not discard wf-recorder stderr, so a failed grab is diagnosable." + (let ((cj/recording-mic-boost 2.0) + (cj/recording-system-volume 1.0)) + (cl-letf (((symbol-function 'executable-find) (lambda (_prog &rest _) t))) + (let ((cmd (cj/recording--build-video-command "mic" "sys" "/tmp/out.mkv" t))) + (should-not (string-match-p "2>/dev/null" cmd)))))) + (ert-deftest test-video-audio-recording--build-video-command-normal-x11-uses-x11grab () "X11 command uses ffmpeg with x11grab, no wf-recorder." (let ((cj/recording-mic-boost 2.0) diff --git a/tests/test-video-audio-recording--start-race.el b/tests/test-video-audio-recording--start-race.el new file mode 100644 index 00000000..36ea8595 --- /dev/null +++ b/tests/test-video-audio-recording--start-race.el @@ -0,0 +1,56 @@ +;;; test-video-audio-recording--start-race.el --- start-race fix tests -*- lexical-binding: t; -*- + +;;; Commentary: +;; Tests for the wf-recorder start-race fix: the poll that waits for a dying +;; wf-recorder to release the compositor capture before launching a new one, and +;; the fail-fast timing predicate that tells a 0.5s failed start from a real +;; recording. The pgrep wrapper and the live start/stop wiring are exercised in +;; the daemon, not here; the poll is tested with an injected predicate so no real +;; process is needed. + +;;; Code: + +(require 'ert) + +;; Stub dependencies before loading the module. +(defvar cj/custom-keymap (make-sparse-keymap) + "Stub keymap for testing.") + +(require 'video-audio-recording) + +(declare-function cj/recording--start-failed-p "video-audio-recording-capture" (elapsed threshold)) +(declare-function cj/recording--wait-for-no-wf-recorder "video-audio-recording-capture" (timeout-secs &optional running-p)) + +;;; ------------------------- cj/recording--start-failed-p --------------------- + +(ert-deftest test-recording-start-failed-p-short-exit-is-failure () + "Normal: an exit well before the threshold is a failed start." + (should (cj/recording--start-failed-p 0.5 1.5))) + +(ert-deftest test-recording-start-failed-p-long-run-is-not-failure () + "Normal: a long-running recording that ends is not a failed start." + (should-not (cj/recording--start-failed-p 30.0 1.5))) + +(ert-deftest test-recording-start-failed-p-at-threshold-is-not-failure () + "Boundary: an exit exactly at the threshold is not counted as failed." + (should-not (cj/recording--start-failed-p 1.5 1.5))) + +;;; -------------------- cj/recording--wait-for-no-wf-recorder ------------------ + +(ert-deftest test-recording-wait-for-no-wf-recorder-clears () + "Normal: returns t once the injected predicate reports wf-recorder gone." + (let ((n 0)) + (should (cj/recording--wait-for-no-wf-recorder + 2.0 + (lambda () (setq n (1+ n)) (< n 3)))))) + +(ert-deftest test-recording-wait-for-no-wf-recorder-already-clear () + "Boundary: an already-clear predicate returns t immediately." + (should (cj/recording--wait-for-no-wf-recorder 2.0 (lambda () nil)))) + +(ert-deftest test-recording-wait-for-no-wf-recorder-times-out () + "Error: a predicate that never clears returns nil at the timeout." + (should-not (cj/recording--wait-for-no-wf-recorder 0.15 (lambda () t)))) + +(provide 'test-video-audio-recording--start-race) +;;; test-video-audio-recording--start-race.el ends here -- cgit v1.2.3