CI / Security audit (RUSTSEC) (pull_request) Has been cancelled
CI / Build + Clippy + Test (pull_request) Has been cancelled
Update the 'Runtime architecture' section to reflect that state.rs /
cap_portal.rs / state_portal.rs / webrtc.rs are now parent modules of
directory trees:
- src/state.rs -> src/state/mod.rs (+ src/state/dispatch/ for the 13
Wayland Dispatch impls)
- src/state_portal.rs still exists; helpers split into
src/state_portal/{bitrate,threads}.rs
- src/cap_portal.rs holds the struct; setup/token_fs/pipewire_thread
split into src/cap_portal/
- src/webrtc.rs gains src/webrtc/html_page.rs sibling
No content changes beyond the path references; the rest of AGENTS.md
remains accurate.
4.5 KiB
4.5 KiB
AGENTS.md
Sources of truth
- Trust current Rust source,
Cargo.toml,shell.nix, and tests overdocs/superpowers/*oranalysis.md; those docs contain historical/aspirational modules that are not in the tree. - Crate edition is 2021, not the default Rust 2024. The package exposes a library, three binaries (
wl-webrtc,vaapi_import_bench,sw_encode_bench), and two examples (list_globals,test_portal).
Setup and build
- Native prerequisites are FFmpeg 6+ dev libs with VAAPI, Wayland protocols/libs, libdrm, PipeWire, and libclang.
shell.nixprovides FFmpeg/Wayland/libdrm/Mesa/libva/clang andLIBCLANG_PATH, but does not currently list PipeWire. - Normal build:
cargo build. Release binary required by README and integration tests:cargo build --release. Cargo.tomlsetsclippy::undocumented_unsafe_blocks = "deny"; everyunsafeblock andunsafe implmust carry a// SAFETY:comment or the build fails. Forunsafe impl Sendon FFmpeg wrappers, see the convention insrc/avhw/mod.rs— justification must be at the C-API level (atomic refcounts, libvaVADisplaythread safety), not Rust borrow level.
Testing and verification
- Unit/focused tests can run with filters, e.g.
cargo test transform,cargo test fps_limit,cargo test backend_detect. - Full
cargo testincludestests/integration_test.rs, which shells out totarget/release/wl-webrtc; runcargo build --releasefirst or those tests fail before reaching code behavior. - Hardware/live tests are marked ignored; run them only on a Wayland session with VAAPI-capable GPU and writable output:
cargo test -- --ignored. - CLI smoke test surface:
target/release/wl-webrtc --helpand invalid-argument rejection. Real capture needs Wayland plus either wlr-screencopy or XDG Portal/PipeWire. - The README CLI table is stale for
--backendand--no-persist;src/args.rsis authoritative for flags.
Runtime architecture
src/main.rsis the real entrypoint: parseArgs, initialize tracing fromRUST_LOGor-v, reject non-H.264, require--outputor--port, detect backend, then run one of two loops.- Backend detection in
src/backend_detect.rs: explicit--backend portal|screencopywins; otherwise wlr-screencopy is preferred when the Wayland globalzwlr_screencopy_manager_v1exists, else Portal/PipeWire is used if D-Bus ScreenCast is available. - Do not use
ashpdfor backend availability checks;backend_detect.rsintentionally uses rawzbusbecauseashpdcaches azbus::Connectionin a global and can hang after its owning Tokio runtime is dropped. src/state/mod.rsdrives the wlroots path using a mio Wayland fd loop andState<CapWlrScreencopy>(WaylandDispatchimpls live undersrc/state/dispatch/);src/state_portal.rsdrives the Portal/PipeWire path throughCapPortalframe channels (bitrate helpers and encode/webrtc thread loops are split intosrc/state_portal/{bitrate,threads}.rs).src/cap_portal.rsholds theCapPortalstruct itself; its setup logic, token filesystem helpers, and PipeWire capture thread live undersrc/cap_portal/.src/webrtc.rsis a small embedded HTTP/WebRTC signaling server usingstr0m; the embedded HTML test page is insrc/webrtc/html_page.rs.--port 0means file-output mode,--port > 0enables WebRTC mode.
Unsafe and FFI work
- FFmpeg/VAAPI/PipeWire code relies on raw FFI and many
unsafeblocks. Preserve nearby// SAFETY:explanations and add one for any new unsafe block. src/avhw/(split from the formersrc/avhw.rsin commitd53e881) owns FFmpegAVBufferRef/ frame / codec contexts. Five types (AvHwDevCtx,AvHwFrameCtx,EncState,SwEncState,SwEncEncode) carryunsafe impl Send; soundness was Oracle-audited on 2026-07-09 against FFmpeg/libva threading semantics. Moving them across threads is sound because the C APIs use atomic refcounts, not because of any Rust-side exclusivity — seesrc/avhw/mod.rsfor the full convention.CapPortalstores the portal restore token under the user cache directory (wl-webrtc/portal-restore-token); use--no-persistwhen manually testing fresh authorization behavior.
Useful manual commands
- List Wayland globals:
cargo run --example list_globals. - Portal permission smoke test:
cargo run --example test_portal. - Portal/VAAPI benchmarks require a screen-share dialog and hardware:
cargo run --bin vaapi_import_bench -- --output /tmp/vaapi_bench.mp4andcargo run --bin sw_encode_bench -- --output /tmp/bench_test.mp4.