Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Record a v4l2loopback camera on X11 in the Linux CI (#3297)

Richie McIlroy548e04a42261
diff --git a/.github/workflows/linux-desktop-reliability.yml b/.github/workflows/linux-desktop-reliability.yml
index 3122784b9..589aa7b1f 100644
--- a/.github/workflows/linux-desktop-reliability.yml
+++ b/.github/workflows/linux-desktop-reliability.yml
@@ -346,6 +346,18 @@ jobs:
grep -qx "$line" "$RUNNER_TEMP/out/hyprland-screenshot/result.txt"
done
+ # The hosted runner can build v4l2loopback; Blacksmith's kernel cannot.
+ - name: Install Linux desktop dependencies
+ uses: ./.github/actions/install-desktop-deps
+
+ - name: X11 recording with a camera
+ run: |
+ sudo apt-get install -y -qq "linux-headers-$(uname -r)" v4l2loopback-dkms xvfb xdotool ffmpeg dbus >/dev/null
+ chmod +x "$RUNNER_TEMP/build/cap-gpui"
+ sudo install -d -o "$(id -u)" -g "$(id -g)" "$RUNNER_TEMP/out/x11-camera"
+ bash scripts/linux-desktop-harness/x11-camera.sh "$RUNNER_TEMP/build/cap-gpui" "$RUNNER_TEMP/build/libs" \
+ "$RUNNER_TEMP/out/x11-camera"
+
- name: Hyprland clipboard (Ctrl+V into and Ctrl+C out of a text field)
env:
SCENARIO: clipboard
diff --git a/scripts/linux-desktop-harness/README.md b/scripts/linux-desktop-harness/README.md
index 971b86217..31faf33ec 100644
--- a/scripts/linux-desktop-harness/README.md
+++ b/scripts/linux-desktop-harness/README.md
@@ -75,6 +75,15 @@ real login activates.
## Checks without a desktop
+- `x11-camera.sh <cap-gpui> <ffmpeg lib dir> <out-dir>` records the display on
+ Xvfb (X11 capture, no portal) with a camera: a v4l2loopback device that
+ ffmpeg feeds a 1280x720 test pattern. Cap waits for the Stop shortcut to start
+ and to stop, pressed with real X input (xdotool), well before its 60 s
+ automatic stop. It checks the camera track is 1280x720 and as long as the
+ display track, about five seconds, and fails if any result is missing. It
+ needs root for modprobe, and runs in the Hyprland job, whose hosted runner
+ can build the module.
+
- `niri-screencast/run.sh` records through Cap's PipeWire capture loop from a
producer that negotiates and shares frames the way niri 26.04 does: a
mandatory DMA-BUF modifier, DMA-BUF buffers the consumer must map itself,
diff --git a/scripts/linux-desktop-harness/x11-camera.sh b/scripts/linux-desktop-harness/x11-camera.sh
new file mode 100755
index 000000000..d98f21f83
--- /dev/null
+++ b/scripts/linux-desktop-harness/x11-camera.sh
@@ -0,0 +1,92 @@
+#!/usr/bin/env bash
+# Usage: x11-camera.sh <cap-gpui> <ffmpeg lib dir> <out-dir>
+set -uo pipefail
+for tool in Xvfb xdotool ffmpeg ffprobe; do
+ command -v "$tool" >/dev/null || { echo "install xvfb, xdotool and ffmpeg" >&2; exit 2; }
+done
+if [[ -z ${X11_CAMERA_BUS:-} ]]; then
+ exec env X11_CAMERA_BUS=1 dbus-run-session -- bash "$0" "$@"
+fi
+bin=$1 libs=$2 out=$3
+mkdir -p "$out" && : >"$out/result.txt" || { echo "x11-camera: cannot write to $out" >&2; exit 2; }
+result() { printf '%s=%s\n' "$1" "$2" >>"$out/result.txt"; }
+
+device=/dev/video42
+sudo modprobe v4l2loopback video_nr=42 card_label="Cap Test Camera" exclusive_caps=1 || exit 2
+sudo chmod 666 "$device"
+ffmpeg -hide_banner -loglevel error -nostdin -re -f lavfi -i testsrc2=size=1280x720:rate=30 -pix_fmt yuyv422 \
+ -f v4l2 "$device" >"$out/feeder.log" 2>&1 &
+feeder=$!
+unset WAYLAND_DISPLAY
+export DISPLAY=:73 XDG_SESSION_TYPE=x11
+Xvfb "$DISPLAY" -screen 0 1920x1080x24 >/dev/null 2>&1 &
+xvfb=$!
+trap 'kill $feeder $xvfb 2>/dev/null' EXIT
+sleep 2
+echo "feeder: $(kill -0 $feeder 2>/dev/null && echo running || echo exited) $(head -c 300 "$out/feeder.log")" >>"$out/actions.log"
+
+profile=$out/profile
+log=$out/cap.log
+mkdir -p "$profile/recordings" "$profile/logs"
+printf '%s' '{"general_settings":{"hasCompletedStartup":true,"hasCompletedOnboarding":true,"enableNotifications":false,"enableTelemetry":false,"recordingCountdown":0},"recording_start_safety":{"confirmBeforeRecordingWithoutMicrophone":false}}' >"$profile/store"
+env LD_LIBRARY_PATH="$libs" CAP_GPUI_APP_DATA_DIR="$profile" CAP_GPUI_RECORDINGS_DIR="$profile/recordings" \
+ CAP_GPUI_LOG_DIR="$profile/logs" CAP_GPUI_NO_TRAY=1 CAP_GPUI_AUTO_NO_MIC=1 RUST_LOG=info \
+ CAP_GPUI_AUTO_CAMERA=1 CAP_GPUI_AUTO_RECORD=studio:60 "$bin" >"$log" 2>&1 &
+pid=$!
+
+wait_log() {
+ for _ in $(seq $(($2 * 4))); do
+ grep -aq -- "$1" "$log" 2>/dev/null && return 0
+ sleep 0.25
+ done
+ return 1
+}
+
+camera=no started=no finished=no
+wait_log "auto-selecting camera" 30 && camera=yes
+wait_log "auto-record start requested" 30
+sleep 2
+xdotool key ctrl+shift+F9
+wait_log "recording started" 30 && started=yes
+if [[ $started == yes ]]; then
+ sleep 5
+ xdotool key ctrl+shift+F9
+ # Well before the 60 s automatic stop, so a Stop shortcut that does not
+ # work fails here.
+ wait_log "recording finished" 15 && finished=yes
+fi
+sleep 2
+result camera_selected "$camera"
+result started "$started"
+result finished "$finished"
+
+probe() {
+ ffprobe -v error -select_streams v:0 -count_packets \
+ -show_entries stream=width,height,nb_read_packets:format=duration -of csv=p=0 "$1" 2>/dev/null |
+ tr ',\n' ' '
+}
+display=$(find "$profile/recordings" -path "*segment-0/display.mp4" | head -1)
+camera_video=$(find "$profile/recordings" -path "*segment-0/camera.mp4" | head -1)
+read -r _ _ display_frames display_seconds <<<"$( [[ -n $display ]] && probe "$display")"
+read -r width height frames seconds <<<"$( [[ -n $camera_video ]] && probe "$camera_video")"
+echo "display: ${display:-none} frames=${display_frames:-} seconds=${display_seconds:-}" >>"$out/actions.log"
+echo "camera: ${camera_video:-none} ${width:-}x${height:-} frames=${frames:-} seconds=${seconds:-}" >>"$out/actions.log"
+result camera_size "$([[ $width == 1280 && $height == 720 ]] && echo yes || echo no)"
+result camera_matches_display "$(awk -v c="${seconds:-0}" -v d="${display_seconds:-0}" \
+ 'BEGIN { x = c - d; if (x < 0) x = -x; print (c >= 4 && c <= 8 && x <= 0.5 ? "yes" : "no") }')"
+result cap_alive "$(kill -0 "$pid" 2>/dev/null && echo yes || echo no)"
+result panics "$(grep -ac 'panicked at' "$log")"
+kill "$pid" 2>/dev/null
+
+cat "$out/result.txt" "$out/actions.log"
+failed=0
+for key in camera_selected started finished camera_size camera_matches_display cap_alive panics; do
+ grep -q "^$key=" "$out/result.txt" || { echo "x11-camera: no $key result" >&2; failed=1; }
+done
+while IFS== read -r key value; do
+ case $key in
+ panics) [[ $value == 0 ]] || failed=1 ;;
+ *) [[ $value == yes ]] || failed=1 ;;
+ esac
+done <"$out/result.txt"
+exit $failed