webcam-loopback-manipulation-screensharing-and-stuff git · main
vcam: v4l2 loopback with overlays, filters, OpenCV and RVM matting
C 51.3% C++ 40.7% Markdown 6.9% Makefile 1%git clone https://git.christianimmanuel.de/linux-system/webcam-loopback-manipulation-screensharing-and-stuff.gitwget https://git.christianimmanuel.de/linux-system/webcam-loopback-manipulation-screensharing-and-stuff/archive/webcam-loopback-manipulation-screensharing-and-stuff.tar.gzFail attempt to implement recording
Makefile | 33 +++-- README.md | 93 +++++++++++++- scripts/vcam-ctl | 376 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ipc.c | 2 +- src/overlay.c | 36 +++--- 5 files changed, 505 insertions(+), 35 deletions(-) diff --git a/Makefile b/Makefile index 5ba85ba..be114de 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,4 @@ CC := gcc - -PREFIX ?= /usr/local -BINDIR := $(PREFIX)/bin - CFLAGS := -std=c11 -O2 -Wall -Wextra -Wpedantic \ -D_POSIX_C_SOURCE=200809L \ -I include \ @@ -13,13 +9,21 @@ LDFLAGS := $(shell pkg-config --libs libavcodec libavformat libavutil libswscale $(shell pkg-config --libs libpng) \ -lpthread -lm +# Installation paths — overridable on the command line: +# make install (default: /usr/local) +# make install PREFIX=$HOME/.local (user-local) +# make install PREFIX=/usr (system-wide) +# make install DESTDIR=/tmp/stage (staged install for packaging) +PREFIX ?= /usr/local +BINDIR ?= $(PREFIX)/bin + SRCS := src/main.c src/frame.c src/capture.c src/output.c \ src/overlay.c src/registry.c src/ipc.c src/filter.c OBJS := $(SRCS:.c=.o) BIN := vcam TEST := test_screen -.PHONY: all clean install +.PHONY: all clean install uninstall all: $(BIN) $(TEST) @@ -34,11 +38,20 @@ src/%.o: src/%.c include/vcam.h $(CC) $(CFLAGS) -c -o $@ $< install: $(BIN) - install -d "$(DESTDIR)$(BINDIR)" - install -m 755 $(BIN) "$(DESTDIR)$(BINDIR)/" - install -m 755 scripts/vcam-ctl "$(DESTDIR)$(BINDIR)/" - install -m 755 scripts/vcam-setup "$(DESTDIR)$(BINDIR)/" - install -m 755 scripts/vcam-tune "$(DESTDIR)$(BINDIR)/" + install -d $(DESTDIR)$(BINDIR) + install -m 755 $(BIN) $(DESTDIR)$(BINDIR)/ + install -m 755 scripts/vcam-ctl $(DESTDIR)$(BINDIR)/ + install -m 755 scripts/vcam-setup $(DESTDIR)$(BINDIR)/ + install -m 755 scripts/vcam-tune $(DESTDIR)$(BINDIR)/ + @echo + @echo "Installed to $(DESTDIR)$(BINDIR)/" + @echo "Make sure that's on your PATH." + +uninstall: + rm -f $(DESTDIR)$(BINDIR)/$(BIN) + rm -f $(DESTDIR)$(BINDIR)/vcam-ctl + rm -f $(DESTDIR)$(BINDIR)/vcam-setup + rm -f $(DESTDIR)$(BINDIR)/vcam-tune clean: rm -f $(OBJS) $(BIN) $(TEST) diff --git a/README.md b/README.md index 8e0a892..df9724c 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,10 @@ # vcam — virtual webcam pipeline +> 🤖 **This is 100% Vibecode by ClaudeCode.** Every line of C, every shell +> script, and every word of this README was written by Claude through a +> long iterative back-and-forth. No human authored any of it directly — +> only the prompts that shaped it. + Real-time virtual webcam written in C. Reads from a physical webcam, runs your frames through a chain of pixel effects, composites images / looping videos / live screen regions on top, and writes the result to a @@ -44,7 +49,14 @@ sudo apt install \ # 2. Build & install git clone <repo-url> vcam && cd vcam make -sudo make install # vcam, vcam-ctl, vcam-setup, vcam-tune +make +sudo make install # default → /usr/local/bin +# or pick your own destination: +# make install PREFIX=$HOME/.local # ~/.local/bin (no sudo) +# make install PREFIX=/usr # system-wide +# make install DESTDIR=/tmp/pkg PREFIX=/usr # staged for packaging +# Uninstall: +# sudo make uninstall # uses the same PREFIX/DESTDIR # 3. ONE-TIME: make v4l2loopback land at /dev/video0 (Chromium needs this) sudo vcam-setup --install @@ -410,6 +422,82 @@ vcam-ctl filter add voronoi 30 && vcam-ctl filter add xor 0.4 # math art --- +## Recording the vcam output + +`vcam-ctl record` drives a detached `ffmpeg` child to capture whatever +vcam is currently outputting on `/dev/video0`. Because v4l2loopback +allows multiple readers, you can record at the same time a browser or +OBS or whatever is also using the cam — no contention. + +```bash +vcam-ctl record start # auto-picks a mic if one's + # available (ALSA "default" + # if present, else first + # hw:N,M from arecord -l). + # Falls back to video-only. +vcam-ctl record start --no-audio # force video-only +vcam-ctl record start -m hw:1,0 -o ~/Videos/out.mkv # explicit mic + filename +vcam-ctl record start --crf 18 --preset slower # higher quality +vcam-ctl record pause # SIGSTOP ffmpeg +vcam-ctl record resume # SIGCONT ffmpeg +vcam-ctl record stop # flush + close cleanly +vcam-ctl record status # → "recording" | "paused" | "stopped" +vcam-ctl record info # detailed + elapsed time +vcam-ctl record mics # list available ALSA devices +``` + +Audio policy: ALSA only by design (no Pulse/PipeWire daemon dependency). +On systems running PipeWire, the pipewire-alsa compatibility plugin +generally exposes `default` as a working capture device, which `record +start` will pick automatically. If you have multiple capture cards or a +preferred one, list them with `record mics` and pass `-m hw:N,M`. + +The recording lives in a separate process from vcam, so it survives a +vcam restart. State is published in three plain files under `/tmp` so any +status bar can read them with `cat`: + +| File | Contents | +|---|---| +| `/tmp/vcam-record.pid` | PID of the ffmpeg child | +| `/tmp/vcam-record.status` | `recording` or `paused` (absent when stopped) | +| `/tmp/vcam-record.info` | `file=`, `mic=`, `started=`, `format=`, etc. | +| `/tmp/vcam-record.log` | ffmpeg's own stderr | + +### Swaybar integration + +`vcam-ctl record bar` emits one line of swaybar-compatible JSON: + +```json +{"text":"● REC 00:01:23","class":"recording","color":"#ff5555"} +``` + +Drop into `~/.config/sway/config`: + +``` +bar { + status_command while date +'%T'; do + printf '%s | %s\n' "$(vcam-ctl record bar)" "$(date +%T)" + sleep 1 + done +} +``` + +Or feed it into [waybar](https://github.com/Alexays/Waybar) / i3blocks / +[i3status-rust](https://github.com/greshake/i3status-rust). The +simplest possible integration is `vcam-ctl record status` — exits 0 if +recording, 1 if not. + +### Pause caveat + +`pause` sends `SIGSTOP` to the ffmpeg process — it literally halts in +place. Video resumes cleanly because v4l2loopback just queues frames, +but the ALSA capture device may buffer a small amount of audio data +while stopped and produce a tiny pop or skip on resume. For long pauses +prefer `stop` + a fresh `start` (the auto-generated filename includes a +timestamp so you won't collide). + +--- + ## vcam-tune — interactive parameter dial Walk mode (no args) navigates every active filter; the highlighted one is @@ -500,7 +588,8 @@ scripts/ ```bash make # build vcam and test_screen with -Wall -Wextra -Wpedantic -O2 make clean -sudo make install # all four scripts + vcam to /usr/local/bin +sudo make install # → /usr/local/bin +make install PREFIX=$HOME/.local # user-local, no root needed ``` To add a new source file, drop it in `src/` and append to `SRCS` in the diff --git a/scripts/vcam-ctl b/scripts/vcam-ctl index b958bda..15f1044 100755 --- a/scripts/vcam-ctl +++ b/scripts/vcam-ctl @@ -76,6 +76,16 @@ # "full" stretches it to the full output. # webcam off Remove the "cam" overlay. # +# Recording (capture the vcam output to a file): +# record start [-o <file>] [-m <alsa-dev>] [--no-audio] [--crf N] +# record stop Flush + close file cleanly. +# record pause / record resume Pause/resume in place (SIGSTOP/SIGCONT). +# record status "recording" / "paused" / "stopped". +# record info Detailed status + elapsed duration. +# record mics List ALSA capture devices. +# record bar Print swaybar JSON for status_command. +# record help Full record usage. +# # Other: # help IPC command list # --help, -h this message @@ -418,5 +428,371 @@ if [[ "$1" == "webcam" ]]; then exit 0 fi +# ────────────────────────────────────────────────────────────── +# record subcommand — drive an ffmpeg child to capture the vcam +# output (and optionally a microphone). +# +# State lives in three small files under /tmp so any tool (swaybar, +# polybar, conky, custom scripts) can read them without going through +# IPC: +# +# /tmp/vcam-record.pid ← PID of the running ffmpeg child +# /tmp/vcam-record.status ← "recording" or "paused" +# /tmp/vcam-record.info ← key=value lines: file, mic, started, format +# /tmp/vcam-record.log ← ffmpeg stderr (for debugging) +# +# Recording survives a vcam restart — it's an independent ffmpeg +# process. State files self-clean when stale. +# ────────────────────────────────────────────────────────────── +REC_PID="/tmp/vcam-record.pid" +REC_STATUS="/tmp/vcam-record.status" +REC_INFO="/tmp/vcam-record.info" +REC_LOG="/tmp/vcam-record.log" + +rec_is_active() { + [[ -f "$REC_PID" ]] || return 1 + local pid; pid=$(cat "$REC_PID" 2>/dev/null) || return 1 + [[ -z "$pid" ]] && return 1 + kill -0 "$pid" 2>/dev/null +} + +rec_cleanup_stale() { + if ! rec_is_active; then + rm -f "$REC_PID" "$REC_STATUS" "$REC_INFO" + fi +} + +rec_list_mics() { + if ! command -v arecord >/dev/null 2>&1; then + echo "arecord not installed (sudo apt install alsa-utils)" >&2 + return 1 + fi + echo "Hardware capture devices:" + arecord -l 2>/dev/null | grep -E "^card|^ Subdev" \ + | awk -F: '/^card/{c=$1$2; printf " %s\n", $0} /^ Subdev/{print " " $0}' + echo "" + echo "ALSA PCM names (use with --mic):" + # Filter to interesting ones — skip null/iec/loopback/dmix internals + arecord -L 2>/dev/null | awk ' + /^[a-zA-Z]/ && !/^null$/ && !/^iec/ && !/^Loopback/ \ + && !/^dmix/ && !/^dsnoop/ && !/^plugequal/ \ + { name=$0; getline desc; printf " %-25s %s\n", name, desc }' +} + +rec_default_output() { + local dir="${XDG_VIDEOS_DIR:-$HOME/Videos}" + [[ -d "$dir" ]] || dir="$HOME" + echo "$dir/vcam-$(date +%Y-%m-%d_%H-%M-%S).mkv" +} + +if [[ "$1" == "record" ]]; then + shift + ACTION="${1:-help}" + shift || true + + case "$ACTION" in + # ── start ──────────────────────────────────────────── + start) + rec_cleanup_stale + if rec_is_active; then + echo "Already recording (pid $(cat $REC_PID))." >&2 + echo "Use 'vcam-ctl record stop' first, or pass --replace." >&2 + # Allow --replace + if [[ " $* " != *" --replace "* && " $* " != *" -r "* ]]; then + exit 1 + fi + kill -TERM "$(cat $REC_PID)" 2>/dev/null + sleep 0.5 + rm -f "$REC_PID" "$REC_STATUS" "$REC_INFO" + fi + + OUT=""; MIC=""; NO_AUDIO=0; CRF=23 + VCODEC="libx264"; VPRESET="veryfast" + VCAM_OUT="${VCAM_OUTPUT:-/dev/video0}" + + while [[ $# -gt 0 ]]; do + case "$1" in + -o|--output) OUT="$2"; shift 2 ;; + -m|--mic) MIC="$2"; shift 2 ;; + --no-audio) NO_AUDIO=1; shift ;; + --crf) CRF="$2"; shift 2 ;; + --vcodec) VCODEC="$2"; shift 2 ;; + --preset) VPRESET="$2"; shift 2 ;; + -r|--replace) shift ;; # already handled + *) echo "Unknown record start option: $1" >&2; exit 1 ;; + esac + done + + OUT="${OUT:-$(rec_default_output)}" + mkdir -p "$(dirname "$OUT")" + + if ! command -v ffmpeg >/dev/null 2>&1; then + echo "ffmpeg not installed. (sudo apt install ffmpeg)" >&2 + exit 1 + fi + + # Auto-pick a microphone if user didn't ask for --no-audio and + # didn't supply --mic. We try the ALSA "default" PCM first + # (works on most systems including PipeWire's ALSA shim), then + # fall back to the first hardware capture card we find via + # arecord -l. Pass --no-audio to skip this entirely. + if [[ $NO_AUDIO -eq 0 && -z "$MIC" ]]; then + if arecord -L 2>/dev/null | grep -q '^default$'; then + MIC="default" + elif command -v arecord >/dev/null 2>&1; then + # Parse "card N: ..., device M: ..." → "hw:N,M" + FIRST_HW=$(arecord -l 2>/dev/null \ + | grep -E '^card [0-9]+' \ + | head -1 \ + | sed -E 's/^card ([0-9]+).* device ([0-9]+).*/hw:\1,\2/') + if [[ -n "$FIRST_HW" && "$FIRST_HW" =~ ^hw: ]]; then + MIC="$FIRST_HW" + fi + fi + if [[ -n "$MIC" ]]; then + echo "[record] auto-selected mic: $MIC" \ + "(override with --mic, or skip with --no-audio)" + else + echo "[record] no ALSA capture device found — recording video only" + fi + fi + + # Query the loopback's CURRENT format so ffmpeg doesn't try to + # change it (which v4l2loopback rejects while vcam is writing + # to it — that's what was making recording fail while ffplay/ + # browsers had the cam open). Passing matching parameters + # turns ffmpeg's S_FMT call into a no-op. + FMT_W=""; FMT_H=""; FMT_PIX=""; FMT_FPS="" + if command -v v4l2-ctl >/dev/null 2>&1; then + FMT_LINE=$(v4l2-ctl -d "$VCAM_OUT" --get-fmt-video 2>/dev/null \ + | tr -d ' ') + FMT_W=$(echo "$FMT_LINE" | awk -F'Width/Height:' '{print $2}' \ + | awk -F'/' '{print $1}') + FMT_H=$(echo "$FMT_LINE" | awk -F'Width/Height:' '{print $2}' \ + | awk -F'/' '{print $2}' | awk -F',' '{print $1}') + # PixelFormat is like "PixelFormat:'YUYV'(YUYV4:2:2)" + FMT_RAW=$(v4l2-ctl -d "$VCAM_OUT" --get-fmt-video 2>/dev/null \ + | awk -F"'" '/Pixel Format/{print $2}') + # ffmpeg names: YUYV -> yuyv422, NV12 -> nv12, etc. + case "$FMT_RAW" in + YUYV) FMT_PIX="yuyv422" ;; + UYVY) FMT_PIX="uyvy422" ;; + NV12) FMT_PIX="nv12" ;; + RGB3) FMT_PIX="rgb24" ;; + BGR3) FMT_PIX="bgr24" ;; + *) FMT_PIX="" ;; # let ffmpeg figure it out + esac + # Frame rate + FMT_FPS=$(v4l2-ctl -d "$VCAM_OUT" --get-parm 2>/dev/null \ + | awk '/Frames per second/{print $4; exit}') + fi + + # Build ffmpeg command — read the vcam loopback output. + # v4l2loopback supports multiple readers, so this doesn't + # interfere with browsers/OBS/ffplay also reading the cam, + # as long as we don't ask for a format change. + CMD=(ffmpeg -y -hide_banner -loglevel warning) + + # Pre-input options must come BEFORE -i + [[ -n "$FMT_PIX" ]] && CMD+=(-input_format "$FMT_PIX") + [[ -n "$FMT_W" && -n "$FMT_H" ]] && CMD+=(-video_size "${FMT_W}x${FMT_H}") + [[ -n "$FMT_FPS" ]] && CMD+=(-framerate "$FMT_FPS") + + CMD+=(-f v4l2 -i "$VCAM_OUT") + + if [[ $NO_AUDIO -eq 0 && -n "$MIC" ]]; then + # Try ALSA first. Many "PipeWire" systems still expose + # ALSA via the pipewire-alsa plugin, so "default" usually + # works without going to pulse/pipewire-pulse. + CMD+=(-f alsa -ac 2 -i "$MIC") + fi + + CMD+=(-c:v "$VCODEC" -preset "$VPRESET" -crf "$CRF" -pix_fmt yuv420p) + if [[ $NO_AUDIO -eq 0 && -n "$MIC" ]]; then + CMD+=(-c:a aac -b:a 160k) + fi + CMD+=("$OUT") + + # Spawn detached so it survives this shell exiting + nohup "${CMD[@]}" </dev/null >"$REC_LOG" 2>&1 & + PID=$! + disown 2>/dev/null || true + + # Give it a moment to fail fast (bad device etc.) + sleep 0.3 + if ! kill -0 "$PID" 2>/dev/null; then + echo "ffmpeg exited immediately — see $REC_LOG:" >&2 + tail -n 5 "$REC_LOG" >&2 + exit 1 + fi + + echo "$PID" > "$REC_PID" + echo "recording" > "$REC_STATUS" + { + echo "file=$OUT" + echo "mic=${MIC:-none}" + echo "started=$(date +%s)" + echo "started_human=$(date '+%Y-%m-%d %H:%M:%S')" + echo "format=${OUT##*.}" + echo "vcodec=$VCODEC" + echo "crf=$CRF" + } > "$REC_INFO" + + echo "● Recording started" + echo " file: $OUT" + echo " pid: $PID" + [[ -n "$MIC" ]] && echo " mic: $MIC" + ;; + + # ── stop ───────────────────────────────────────────── + stop) + if ! rec_is_active; then + echo "Not currently recording." >&2 + rec_cleanup_stale + exit 1 + fi + PID=$(cat "$REC_PID") + # Send 'q' on stdin? We redirected to /dev/null so use SIGINT, + # which makes ffmpeg flush + write the trailer cleanly. + kill -INT "$PID" 2>/dev/null || true + for _ in $(seq 1 50); do + kill -0 "$PID" 2>/dev/null || break + sleep 0.1 + done + if kill -0 "$PID" 2>/dev/null; then + kill -TERM "$PID" 2>/dev/null + sleep 0.5 + kill -KILL "$PID" 2>/dev/null + fi + OUT=$(grep '^file=' "$REC_INFO" 2>/dev/null | cut -d= -f2-) + rm -f "$REC_PID" "$REC_STATUS" "$REC_INFO" + echo "■ Recording stopped" + [[ -n "$OUT" ]] && echo " saved: $OUT" + ;; + + # ── pause ──────────────────────────────────────────── + # SIGSTOP halts ffmpeg in place. Audio device may buffer a bit + # of data while stopped; expect a tiny glitch on resume. + pause) + if ! rec_is_active; then echo "Not recording." >&2; exit 1; fi + PID=$(cat "$REC_PID") + kill -STOP "$PID" + echo "paused" > "$REC_STATUS" + echo "⏸ Recording paused (pid $PID)" + ;; + + # ── resume ─────────────────────────────────────────── + resume|continue) + if [[ ! -f "$REC_PID" ]]; then echo "No paused recording." >&2; exit 1; fi + PID=$(cat "$REC_PID") + kill -CONT "$PID" + echo "recording" > "$REC_STATUS" + echo "● Recording resumed (pid $PID)" + ;; + + # ── status ─────────────────────────────────────────── + # Outputs one of: "recording", "paused", "stopped". + # Designed for swaybar / polybar polling — exit code mirrors + # the state so 'vcam-ctl record status >/dev/null' is enough. + status) + if rec_is_active; then + cat "$REC_STATUS" 2>/dev/null || echo "recording" + exit 0 + else + rec_cleanup_stale + echo "stopped" + exit 1 + fi + ;; + + # ── info ───────────────────────────────────────────── + info) + if ! rec_is_active; then + rec_cleanup_stale + echo "Not currently recording." + exit 1 + fi + echo "state=$(cat $REC_STATUS 2>/dev/null || echo recording)" + echo "pid=$(cat $REC_PID)" + [[ -f "$REC_INFO" ]] && cat "$REC_INFO" + # Duration on the fly + if [[ -f "$REC_INFO" ]]; then + START=$(grep '^started=' "$REC_INFO" | cut -d= -f2) + if [[ -n "$START" ]]; then + NOW=$(date +%s) + DUR=$((NOW - START)) + printf "duration=%02d:%02d:%02d\n" \ + $((DUR/3600)) $(((DUR%3600)/60)) $((DUR%60)) + fi + fi + ;; + + # ── mics: list available ALSA capture devices ───────── + mics|devices) + rec_list_mics + ;; + + # ── bar: emit swaybar-compatible JSON (for status_command) ─ + bar) + if rec_is_active; then + STATE=$(cat "$REC_STATUS" 2>/dev/null || echo recording) + START=$(grep '^started=' "$REC_INFO" 2>/dev/null | cut -d= -f2) + NOW=$(date +%s) + DUR=$((NOW - ${START:-NOW})) + LBL=$(printf "%02d:%02d:%02d" $((DUR/3600)) $(((DUR%3600)/60)) $((DUR%60))) + if [[ "$STATE" == "paused" ]]; then + printf '{"text":"⏸ REC %s","class":"paused","color":"#ffaa00"}\n' "$LBL" + else + printf '{"text":"● REC %s","class":"recording","color":"#ff5555"}\n' "$LBL" + fi + else + printf '{"text":"","class":"idle"}\n' + fi + ;; + + help|--help|-h|"") + cat <<'HLP' +vcam-ctl record — capture the vcam loopback output to a file. + + record start [options] Start recording. + -o, --output <path> Output file (default: $XDG_VIDEOS_DIR/vcam-<ts>.mkv) + -m, --mic <device> ALSA capture device (e.g. "default", "hw:1,0"). + If omitted, vcam-ctl auto-picks: ALSA "default" + if present, otherwise the first hw:N,M from + 'arecord -l'. Use 'vcam-ctl record mics' to see + available devices. + --no-audio Record video only (skip mic auto-detect). + --crf <n> x264 quality 0–51 (default 23, lower = better) + --vcodec <name> video codec (default libx264) + --preset <name> x264 preset (default veryfast) + -r, --replace kill any existing recording first + + record stop Flush & close, write the file out cleanly. + record pause SIGSTOP the ffmpeg process (audio may glitch on resume). + record resume SIGCONT the ffmpeg process. + record status Print "recording" / "paused" / "stopped". Exit code + 0 if active, 1 if not — handy for shell scripts. + record info Detailed info about the current recording. + record mics List available ALSA capture devices. + record bar Emit one line of swaybar JSON for status_command. + +State files (live under /tmp, readable by anything): + /tmp/vcam-record.pid PID of ffmpeg + /tmp/vcam-record.status "recording" or "paused" + /tmp/vcam-record.info file=, mic=, started=, format= + /tmp/vcam-record.log ffmpeg's own stderr + +Swaybar (~/.config/sway/config): + bar { status_command while date +'%T'; do vcam-ctl record bar; sleep 1; done } + +Or simpler, use a custom status block that polls 'vcam-ctl record status'. +HLP + ;; + + *) echo "Unknown record action: $ACTION (try 'vcam-ctl record help')" >&2; exit 1 ;; + esac + exit 0 +fi + # ── default: pass everything else straight to the IPC server ─ ipc_send "$*" diff --git a/src/ipc.c b/src/ipc.c index 7382be1..9f80f7b 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -358,7 +358,7 @@ static void handle_command(int client_fd, char *line, OverlayRegistry *reg) int ox, oy, ow, oh; overlay_get_rect(s->overlay, &ox, &oy, &ow, &oh); snprintf(buf, sizeof buf, - "OK z=%d: [%s] id=%s pos=(%d,%d) size=%dx%d%s", + "OK %d: [%s] id=%s pos=(%d,%d) size=%dx%d%s", i, type_str, s->id, ox, oy, ow, oh, (i == reg->count - 1) ? " (top)" : (i == 0) ? " (bottom)" : ""); diff --git a/src/overlay.c b/src/overlay.c index 224ba01..f5575bd 100644 --- a/src/overlay.c +++ b/src/overlay.c @@ -1,4 +1,3 @@ -#define _GNU_SOURCE /* for F_SETPIPE_SZ */ #include "vcam.h" #include <stdio.h> @@ -424,21 +423,27 @@ Overlay *overlay_create_screen(const char *geometry, o->src_w = gw; o->src_h = gh; resolve_dims(gw, gh, dst_w, dst_h, &o->dst_w, &o->dst_h); - /* Clamp to output dimensions if known — otherwise overlay_blit would - * just clip and the user would only see a corner of their screen. */ - if (g_output_w > 0 && g_output_h > 0 && + /* If the user didn't specify dst dims (dst_w == dst_h == 0) and the + * source is bigger than the output, scale down preserving aspect so + * the user actually sees the whole capture rather than a clipped + * corner. Only triggers when output dims are known AND no explicit + * dst was given — explicit sizes are always respected. */ + if (dst_w == 0 && dst_h == 0 && + g_output_w > 0 && g_output_h > 0 && (o->dst_w > g_output_w || o->dst_h > g_output_h)) { - /* Scale down preserving aspect so it fits the output */ double sx = (double)g_output_w / o->dst_w; double sy = (double)g_output_h / o->dst_h; double s = sx < sy ? sx : sy; int new_w = (int)(o->dst_w * s); int new_h = (int)(o->dst_h * s); - /* Snap to multiples of 2 for stride safety */ + /* Snap to multiples of 2 for stride safety; clamp to a sane min */ new_w &= ~1; new_h &= ~1; - fprintf(stderr, "[screen] clamping %dx%d → %dx%d to fit %dx%d output\n", - o->dst_w, o->dst_h, new_w, new_h, g_output_w, g_output_h); - o->dst_w = new_w; o->dst_h = new_h; + if (new_w >= 16 && new_h >= 16) { + fprintf(stderr, + "[screen] scaling %dx%d → %dx%d to fit %dx%d output\n", + o->dst_w, o->dst_h, new_w, new_h, g_output_w, g_output_h); + o->dst_w = new_w; o->dst_h = new_h; + } } bool wayland = getenv("WAYLAND_DISPLAY") != NULL; @@ -464,19 +469,6 @@ Overlay *overlay_create_screen(const char *geometry, unlink(o->fifo_path); free(o); return NULL; } - /* Tune the kernel pipe buffer to ~one frame. - * Default is 64KB, way smaller than a raw frame (~3-8MB), which - * forces wf-recorder to issue many partial writes per frame. - * Larger than 1 frame would let wf-recorder build up a backlog - * → visible lag. Sizing to exactly one frame means wf-recorder - * can write a single frame in one shot, blocks on the next - * until we read — tight coupling, minimal latency. */ - size_t one_frame = (size_t)gw * gh * 4; /* worst case: BGR0 */ - if (fcntl(o->pipe_fd, F_SETPIPE_SZ, (int)one_frame) < 0) { - /* F_SETPIPE_SZ may fail past /proc/sys/fs/pipe-max-size; - * non-fatal, just means we use the kernel's default. */ - } - char wfr_geo[64]; snprintf(wfr_geo, sizeof wfr_geo, "%d,%d %dx%d", gx, gy, gw, gh);