Repository navigation
Conversation
|
Thanks a lot for this. Linux hardware encoding is something we really want, and the PR clearly took a lot of care (device validation, smoke tests, 1. Blocker: FFmpeg 6.x (libavcodec 60) never loadsOn Ubuntu 24.04 ( Cause:
Suggested fix: drop the 2. VA-API hwframe canary rejects a valid surface ID
3. Bitrate changes produce IDRs
4. Proposal: use GStreamer instead of FFmpegThe .deb already hard-depends on
Why I think it's the better base:
Caveats I hit or found in the source:
The NVENC pipeline I used: Forced keyframes: send Would you be open to reworking the encoder part on top of GStreamer? The device selection, config keys and tests in this PR would mostly carry 5. Smaller items
Thanks again! Happy to share the benchmark harness if it helps. |
958a7a1 to
0b28790
Compare
|
Thanks a lot @sqlpxc for the thorough review, the clear GStreamer direction, and for sharing I accepted all of your suggestions, rebased onto latest Summary of changes addressing your comments
Real-world testing & benchmarks performedTest machine: Linux Mint 22.3 (Ubuntu 24.04 base, X11
1.
|
| Encoder | Latency median / p95 / p99 | CPU/frame | 1-in-1-out | Forced IDR @150 (missing hdr) | 20→5 Mbps @300 | Avg P-frame KiB (budget 40.7 → 10.2) |
|---|---|---|---|---|---|---|
NVENC H.264 (nvcudah264enc) |
3.80 / 4.09 / 4.19 ms | 1.49 ms | ✓ (0 extra) | ✓ [150] (0) |
0 IDR, same frame (39.1→18.9K) |
36.4 → 12.4 KiB |
NVENC HEVC (nvcudah265enc) |
4.57 / 5.00 / 5.16 ms | 1.62 ms | ✓ (0 extra) | ✓ [150] (0) |
0 IDR, same frame (38.9→17.3K) |
38.7 → 11.3 KiB |
VA-API H.264 (vah264enc) |
5.97 / 8.07 / 8.38 ms | 3.77 ms | ✓ (0 extra) | ✓ [150] (0) |
Hysteresis @421 (+2s, IDR+hdr) | 40.8 → 10.2 KiB |
VA-API HEVC (vah265enc) |
6.18 / 8.36 / 10.03 ms | 4.26 ms | ✓ (0 extra) | ✓ [150] (0) |
Hysteresis @420 (+2s, IDR+hdr) | 43.8 → 11.9 KiB |
VA-API AV1 (vaav1enc) |
6.09 / 8.10 / 8.73 ms | 3.82 ms | ✓ (0 extra) | ✓ [150] (0) |
Hysteresis @420 (+2s, IDR+hdr) | 39.7 → 10.2 KiB |
2. Unit, Debian 12 CI, & live hardware test suites
- Debian 12 Bookworm CI (
debian:bookworm, GStreamer 1.22): built cleanly and passed 121 passed, 0 failed (cargo test --locked --lib, including the hardware-free GStreamerappsrc ! identity ! appsinkbuffer pool / 4-byte aligned NV12 stride / flush PTS / fallible property / config header tests) +rustfmt --edition 2024 --check. - Live GPU suite (
AIRHAND_REQUIRE_HW_TEST=1 cargo test --locked --lib): 121 passed, 0 failed (cargo clippy --locked --all-targets -- -W clippy::all: 0 warnings in changed files). Verified live VA-API (H.264,HEVC,AV1) and NVENC (H.264,HEVC) across1280×720, non-4/16-aligned strides (1366×768,1590×994,1728×1080), and2560×1440(VA-API AV1), with strictffmpeg -err_detect explodeframe-count decode checks, BT.709 red/blue RGB color round-trip assertions, NVENC zero-IDR bitrate drop (verifying no raw H.264 IDR NAL5after frame 0), and VA-API hysteresis + AV1READY -> PLAYINGrate transitions.
3. Live X11 capture benchmark (encode_bench) & real-world simulated-client E2E (e2e_roundtrip)
- Ran
encode_benchcapturing the live 2560×1440 X11 desktop (NVENC H.2642560×1440 p506.35 ms,VA-API H.264p5012.10 ms,VA-API AV1p5012.85 ms). - Started a local
airhand-serverrelay (udp://0.0.0.0:4433) and rane2e_roundtripwith the protocol-level simulated controller over QUIC + E2E encryption in all three modes:AIRHAND_HW_ENCODER=auto: lazily probed Intel VA-API ([H264, H265, AV1], skipping secondary NVENC), streamedVA-API H.264 (Intel)1080p30 (OpenH264 decode verified), then switched live mid-session toVA-API AV1 (Intel)720p60 — passed.AIRHAND_HW_ENCODER=nvenc: lazily probed RTX 4090 NVENC ([H264, H265]), streamedNVENC H.2641080p30 (OpenH264 decode verified), then handled mid-session quality/codec switch cleanly — passed.AIRHAND_HW_ENCODER=software: skipped GStreamer init, advertised[H264], and streamedOpenH264(took_ms=0) — passed.
(Note: Debian 12 / GStreamer 1.22 build & hardware-free tests passed in CI, and GStreamer 1.22 plugin behavior was audited against upstream source; live GPU hardware runs were executed on GStreamer 1.24.2.)
Summary
Adds optional Linux hardware video encoding (VA-API and NVENC for H.264, HEVC, and AV1) via system GStreamer (
gstreamer-rs0.23,v1_22feature floor for Debian 12 Bookworm compatibility), replacing the earlier FFmpegdlopen/ABI approach and addressing all review feedback from@sqlpxc.What's changed
client/src-tauri/src/host/encoder/linux.rs,#![forbid(unsafe_code)]):gstreamer1.0-plugins-bad, already a Linux package dependency) — no C FFmpeg ABI glue, nobuild.rsprobe, and nolibavcodec*.debrecommends.nvcudah264enc,nvcudah265enc, plusnvcudaav1enc/nvav1encon GStreamer 1.26+): feeds BT.709 limited-range NV12 through an encoder-ownedGstBufferPool(honoring GStreamer's 4-byte row alignment for widths like1366and1590), sets explicit downstream caps, keepsvbv-buffer-sizefixed at init, and updates onlybitrate/max-bitrateat runtime for same-frame, zero-IDR dynamic bitrate changes (with fallible property probing for GStreamer 1.22 / 1.24 / 1.26+).vapluginvah264enc,vah265enc,vaav1enc, multi-GPUvarenderD*and*lpencvariants): resolves DRM render nodes across GStreamer 1.22 (rate-controlenum type /READYfallback) and 1.24+ (device-path), setsb-frames=0,ref-frames=1(prevents Intel iHD 24.1.0vaEndPicturefailure),key-int-max=1024, andhierarchical-level=1/gf-group-size=1for 1-in-1-out AV1. UsesFlushStart+FlushStop(false)onappsrcfor reliable forced IDRs with full headers (avoiding GStreamer 1.24.2GstVaBaseEncforce-key-unit bugs), rejects CQP-only drivers, detects sync keyframes + in-band headers strictly from the bitstream, and retains VA-API-only bitrate hysteresis (READY -> set -> PLAYINGwhenbitrateis notMUTABLE_PLAYING, e.g.vaav1encon 1.24.2).warm_up(); caches validated candidates lazily inOnceLock<ProbeCache>shared byavailable_codecs()andLinuxHwEncoder::new()(so hosts without working hardware fail fast with zero retries); bypasses PCI sysfs gating whenhw_encoder = "nvenc"(and checks/dev/nvidiactl//dev/dxginauto); uses non-blocking pool acquisition and bounded worker-thread teardown.annexb_nal_typesuntouched (no Windows/macOS HEVC path changes).host/mod.rs,host/pipeline.rs,build.rs, andtauri.linux.conf.jsonunchanged relative tomain.VideoEncoder::set_bitratedocumentation inencoder/mod.rs.hw_encoderandvaapi_deviceinconfig.rsviaput_setting(keeping the# Airhandfile header banner intact).libgstreamer1.0-devandlibgstreamer-plugins-base1.0-devto.github/workflows/release.ymlanddocs/building*.md..github/workflows/check.ymlrunningrustfmt --edition 2024 --checkon changed Rust files andcargo test --locked --libin a Debian 12 Bookworm container (including hardware-free GStreamerappsrc ! identity ! appsinkpadded-NV12/pool/flush/property/cache unit tests that never silently skip).README*.md,docs/architecture*.md,docs/limitations*.md, anddocs/usage*.md.