From 43837055ab19c95032c48a0a3a0bb3aa1868584b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 16 Nov 2026 06:20:05 -0800 Subject: [PATCH 1/3] chore(quest): claim quest/m2/native-media-config.md Quest-Claim: 419329c9e63906ab From 4a1edaaaef6ac67a5e0445bfef8ba1a364694c37 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 16 Nov 2026 07:01:42 -0800 Subject: [PATCH 2/3] feat(video)!: one typed device selector per config, and a cursor enum encode::Config, decode::Config, resize::Config, encode::Capture, and moq_transcode::Config pick hardware with a moq_video::Device present on every platform and feature set, in place of cfg-gated VA-API and V4L2 paths. A render node also places NVENC and NVDEC on the NVIDIA GPU behind it, matched by PCI address. capture::Config::cursor is a non-exhaustive Cursor enum, and capture::Mode is non-exhaustive. Co-Authored-By: Claude Opus 5.5 --- doc/lib/rs/moq-video.md | 11 ++ quest/m2/README.md | 1 - quest/m2/native-media-config.md | 84 -------- quest/m2/video-producer.md | 1 - quest/m3/vaapi-render-tests.md | 2 +- quest/m4/capture-cursor.md | 8 +- quest/m5/vaapi-vulkan-import.md | 4 +- rs/moq-cli/src/main.rs | 11 ++ rs/moq-cli/src/play/args.rs | 11 +- rs/moq-cli/src/play/headless.rs | 6 - rs/moq-cli/src/play/media.rs | 11 +- rs/moq-cli/src/play/video.rs | 2 - rs/moq-cli/src/publish.rs | 25 +-- rs/moq-cli/src/transcode.rs | 31 ++- rs/moq-transcode/CHANGELOG.md | 1 + rs/moq-transcode/src/config.rs | 44 ++--- rs/moq-video/CHANGELOG.md | 2 + rs/moq-video/src/capture/mod.rs | 30 ++- rs/moq-video/src/capture/pipewire.rs | 2 +- rs/moq-video/src/capture/screencapture.rs | 2 +- rs/moq-video/src/capture/wgc/native.rs | 4 +- rs/moq-video/src/capture/x11.rs | 4 +- rs/moq-video/src/decode/backend/mod.rs | 87 ++++++++ rs/moq-video/src/decode/backend/nvdec.rs | 3 +- rs/moq-video/src/decode/backend/v4l2.rs | 2 +- rs/moq-video/src/decode/backend/vaapi.rs | 4 +- rs/moq-video/src/decode/decoder.rs | 17 +- rs/moq-video/src/device.rs | 230 ++++++++++++++++++++++ rs/moq-video/src/encode/backend/mod.rs | 49 +++++ rs/moq-video/src/encode/backend/nvenc.rs | 22 ++- rs/moq-video/src/encode/backend/v4l2.rs | 2 +- rs/moq-video/src/encode/backend/vaapi.rs | 2 +- rs/moq-video/src/encode/encoder.rs | 38 ++-- rs/moq-video/src/encode/producer.rs | 17 +- rs/moq-video/src/frame.rs | 2 +- rs/moq-video/src/frame/cuda.rs | 33 ++++ rs/moq-video/src/lib.rs | 2 + rs/moq-video/src/resize.rs | 13 +- 38 files changed, 564 insertions(+), 256 deletions(-) delete mode 100644 quest/m2/native-media-config.md create mode 100644 rs/moq-video/src/device.rs diff --git a/doc/lib/rs/moq-video.md b/doc/lib/rs/moq-video.md index 956747e5956..05b8efc8bea 100644 --- a/doc/lib/rs/moq-video.md +++ b/doc/lib/rs/moq-video.md @@ -48,6 +48,17 @@ cameras; links libpipewire), and `vpx` (links libvpx). `vaapi` (Intel and AMD on Linux), `v4l2` (ARM SoC codecs such as a Raspberry Pi's), and `nvidia` are on by default, since none needs anything on the build host. +Every config that opens hardware (`encode::Config`, `decode::Config`, +`resize::Config`, `encode::Capture`) has a `device` field, on every platform +and feature set. `Device::Auto`, the default, lets each backend choose. +`Device::Render(path)` names a DRM render node: VA-API opens it, and NVENC and +NVDEC open the NVIDIA GPU behind it, so a host with several GPUs places each +codec on the one it names. `Device::Video(path)` names a V4L2 codec node. A +named node steers automatic backend selection to the backends that drive it, +and one that fails to open is an error rather than a search for another. +macOS, Windows, and Android pick their own device and ignore the field. +`capture::Config::cursor` is `Cursor::Drawn` (the default) or `Cursor::Hidden`. + On Wayland, the `pipewire` feature lets Rust callers request a screen or window through the system picker with `capture::Source::Portal`. Keep the selection for automatic reopens; create a new one to choose another source. diff --git a/quest/m2/README.md b/quest/m2/README.md index 42df4d4cfa6..564d3cd3ae2 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -63,7 +63,6 @@ removal, a rename, or a wire, catalog, or format change that is free until and [moq-json writes](/quest/m2/json-write-at.md). - **Native media and gateways.** [The video producer](/quest/m2/video-producer.md), [the hole wait](/quest/m2/delay-hole-wait.md), - [device and cursor](/quest/m2/native-media-config.md), [sink names](/quest/m2/audio-sink-names.md), [moq-vaapi's exports](/quest/m2/vaapi-exports.md), [the WebRTC bind](/quest/m2/webrtc-bind.md), diff --git a/quest/m2/native-media-config.md b/quest/m2/native-media-config.md deleted file mode 100644 index d94378de62d..00000000000 --- a/quest/m2/native-media-config.md +++ /dev/null @@ -1,84 +0,0 @@ -# [M] moq-video configs pick a device and a cursor mode with one typed field each - -## Goal - -Every moq-video config that opens hardware carries one `device` selector: a -typed value, present on every platform and feature set, honoured by whichever -backend opens and ignored otherwise, NVIDIA included. A caller sets it -without repeating moq-video's `cfg` gates or naming a backend. -`capture::Config::cursor` is an open enum (`Drawn`, `Hidden`) instead of a -bool, so a third mode is a new variant. - -## Plan - -Decided 2026-11-16 (maintainer, /quest-plan of the pre-cut audit): - -- ✅ One typed, always-present `device` selector per config (rejected: keep - the backend-named path fields and only drop their `cfg` gates, which - leaves NVIDIA unselectable; keep them and correct the crate doc). The - reference user is a transcode host with more than one GPU, which cannot - place NVIDIA work on a chosen GPU today. -- ✅ `capture::Config::cursor` becomes a `#[non_exhaustive]` enum with - `Drawn` and `Hidden` (rejected: keep the bool and add a second field for - the planned third mode). [The cursor apart from the frame](/quest/m4/capture-cursor.md) - adds a mode the portal already has, and a bool plus a second field can - say "drawn and also apart". - -Facts found by the audit: - -- [moq-dev/moq#8305](https://github.com/moq-dev/moq/pull/8305) replaced - environment reads with fields, and left backend-named, `cfg`-gated paths - on five public structs: `encode::Config` - (`rs/moq-video/src/encode/encoder.rs:210`, `:215`), `decode::Config` - (`rs/moq-video/src/decode/decoder.rs:94`, `:99`), `resize::Config` - (`rs/moq-video/src/resize.rs:24`), `encode::Capture` - (`rs/moq-video/src/encode/producer.rs:315`, `:318`), and - `moq_transcode::Config` (`rs/moq-transcode/src/config.rs:61`, `:66`, `:71`). -- A field that exists only under - `cfg(all(target_os = "linux", feature = "vaapi"))` makes every caller - repeat the gate: `grep -rn 'feature = "vaapi"\|feature = "v4l2"'` finds 23 - lines in `rs/moq-cli/src` and 10 in `rs/moq-transcode/src`. A dependent - crate has to mirror moq-video's feature names to compile. -- The crate doc promises the opposite: no public type, signature, or error - variant names a backend (`rs/moq-video/src/lib.rs:58`). -- NVIDIA has no selector: NVDEC opens device 0 - (`rs/moq-video/src/decode/backend/nvdec.rs:140`), and NVENC opens device 0 - (`rs/moq-video/src/encode/backend/nvenc.rs:467`) unless `Config::input`, a - `frame::vulkan::Device`, picks one by UUID (`:471`-`:478`). -- [GPU health](/quest/m3/gpu-health.md) already decided the vendor-neutral - identity (2026-10-05): the render node, carried on `frame::vulkan::Device`. -- `pub cursor: bool` is at `rs/moq-video/src/capture/mod.rs:305`, with five - readers: `capture/pipewire.rs:72`, `screencapture.rs:161`, `x11.rs:296`, - `wgc/native.rs:263`, and `rs/moq-cli/src/publish.rs:1135`. - -For the PR: - -- Shape the selector as a `#[non_exhaustive]` enum, such as a DRM render - node path and a V4L2 node path, defaulting to automatic choice. Settle in - the PR how a config that needs two nodes says so (moq-transcode names a - V4L2 decoder and a V4L2 encoder node separately today): one selector per - direction is the likely answer, since decode and encode configs are - already separate. -- NVIDIA maps a render node to its CUDA ordinal the way `Config::input` - already maps a Vulkan device, so NVDEC and NVENC both honour it. Test it - where two devices exist, or with the mapping function fed two fake - identities; skip, as the other NVIDIA tests do, on a host with none. -- A named device that fails to open is an error, as a named VA-API node is - today; only the automatic choice falls back. -- CLI flags keep their names (`--vaapi-device`, `--v4l2-encoder`, - `--v4l2-decoder`, `--no-cursor`) and lose their `cfg` gates where the - selector makes that possible. -- Recommended in the same PR, not decided by the maintainer: fence - `capture::Mode` (`rs/moq-video/src/capture/mod.rs:203`), the one listing - struct [moq-dev/moq#8742](https://github.com/moq-dev/moq/pull/8742) left - without `#[non_exhaustive]`. -- Update `doc/lib/rs/moq-video.md` and `rs/moq-video/MIGRATE.md`. - -Public API: moq-video and moq-transcode break (device fields on five -configs, `capture::Config::cursor`). CLI: none. Wire: none. - -## Related - -- [moq-video has a producer that takes frames](/quest/m2/video-producer.md) - changes `encode::Capture` and its driver in the same crate -- [GPU health](/quest/m3/gpu-health.md) - the render-node identity the selector reuses -- [The cursor apart from the frame](/quest/m4/capture-cursor.md) - adds the third cursor variant diff --git a/quest/m2/video-producer.md b/quest/m2/video-producer.md index 35a7d2ee21c..ee2f8960fd3 100644 --- a/quest/m2/video-producer.md +++ b/quest/m2/video-producer.md @@ -70,5 +70,4 @@ publisher is renamed). moq-ffi and moq-c keep their names. Wire: none. ## Related - [Terminal operations consume](/quest/m2/finish-sweep.md) - gives the renamed type `finish(self)`; whichever lands second rebases -- [One device selector](/quest/m2/native-media-config.md) - changes `encode::Config` in the same crate - [A published capture exposes its grant](/quest/m3/video-publish-grant.md) - adds a getter to the capture handle this runs on diff --git a/quest/m3/vaapi-render-tests.md b/quest/m3/vaapi-render-tests.md index 7031370f099..e67989e27aa 100644 --- a/quest/m3/vaapi-render-tests.md +++ b/quest/m3/vaapi-render-tests.md @@ -46,7 +46,7 @@ which covers only NVIDIA. - Added 2026-11-06 (maintainer, /quest-unblock) from [moq-dev/moq#8013](https://github.com/moq-dev/moq/pull/8013)'s follow-ups: - The VA-API tests open the first suitable render node, so a second GPU - is covered only when `Config::vaapi` names it, which moq-video no longer + is covered only when `Config::device` names it, which moq-video no longer reads from the environment. `just rs gpu` runs the VA-API tests once for each AMD or Intel render node, passing it to the tests through a test-only knob ([env-knobs](/quest/m4/env-knobs.md) owns its name); a node of either vendor without its VA-API diff --git a/quest/m4/capture-cursor.md b/quest/m4/capture-cursor.md index ff3888289a0..300e869563a 100644 --- a/quest/m4/capture-cursor.md +++ b/quest/m4/capture-cursor.md @@ -21,9 +21,11 @@ Decided: ✅ a separate cursor (rejected: drawn into the frame, with the viewer's own pointer shown on top). Milestone: ✅ m4 (rejected: m3), since the consumer is Kixel's later (m2) plans. -Today `capture::Config::cursor` is a bool that draws the cursor in or leaves -it out: the portal's embedded mode, `SetIsCursorCaptureEnabled` on WGC, -`setShowsCursor` on macOS, and a CPU blend of the XFixes cursor on X11. +Today `capture::Config::cursor` is a `#[non_exhaustive]` enum whose +`Drawn` and `Hidden` draw the cursor in or leave it out: the portal's +embedded mode, `SetIsCursorCaptureEnabled` on WGC, `setShowsCursor` on +macOS, and a CPU blend of the XFixes cursor on X11. The separate cursor is a +third variant, which each backend's `Cursor::drawn` match must then decide. What each system offers, to confirm in the PR: diff --git a/quest/m5/vaapi-vulkan-import.md b/quest/m5/vaapi-vulkan-import.md index 70207f8a2e7..b98c5bdd829 100644 --- a/quest/m5/vaapi-vulkan-import.md +++ b/quest/m5/vaapi-vulkan-import.md @@ -40,7 +40,7 @@ sync_file import, so an explicit-sync producer races it. half-written buffer. - Device: VA-API has no device UUID, and today moq-video's `frame::vaapi::device()` picks one process-wide render node (the first that - probes) in a `OnceLock`, unless `Config::vaapi` names one. The encoder, decoder, and + probes) in a `OnceLock`, unless `Config::device` names one. The encoder, decoder, and resize share it so a DMA-BUF moves between them without a copy. Choosing a node per surface makes `device()` per-node, so keep decode, resize, and encode on one node per DMA-BUF. Decided 2026-10-05: the producer puts the @@ -61,7 +61,7 @@ Test: an ignored hardware test in `just rs gpu`'s Intel branch renders on the iGPU with Vulkan, exports a DMA-BUF image and an explicit-sync timeline, and encodes H.264 at two sizes; the output decodes, and the slots recycle. On the two-node host, also check that a surface carrying `renderD129`'s -`dev_t`, or `Config::vaapi` naming it, opens that node rather than the +`dev_t`, or `Config::device` naming it, opens that node rather than the first render node: the second-node check moved here from [Video hardware validation](/quest/m5/video-hardware.md) (2026-10-06 audit). diff --git a/rs/moq-cli/src/main.rs b/rs/moq-cli/src/main.rs index 5210131b3b4..981cdbc6620 100644 --- a/rs/moq-cli/src/main.rs +++ b/rs/moq-cli/src/main.rs @@ -40,6 +40,17 @@ use tokio_util::sync::CancellationToken; #[global_allocator] static ALLOC: moq_tokio::jemalloc::Allocator = moq_tokio::jemalloc::Allocator; +/// The device a render node flag (`--vaapi-device`) and a V4L2 node flag name. +/// Each command declares the two in conflict, so at most one is set. +#[cfg(any(feature = "capture", feature = "transcode", feature = "play"))] +fn device(render: &Option, video: &Option) -> moq_video::Device { + match (render, video) { + (Some(node), _) => moq_video::Device::Render(node.clone()), + (None, Some(node)) => moq_video::Device::Video(node.clone()), + (None, None) => moq_video::Device::Auto, + } +} + /// Everything needed to build MoQ clients/servers. #[derive(Clone)] struct Net { diff --git a/rs/moq-cli/src/play/args.rs b/rs/moq-cli/src/play/args.rs index c8583d00490..95a0799d84a 100644 --- a/rs/moq-cli/src/play/args.rs +++ b/rs/moq-cli/src/play/args.rs @@ -93,15 +93,14 @@ pub struct Args { #[usage(long, value_name = "PATH", value_hint = usage_rs::ValueHint::FilePath, extensions("ndjson"))] pub metrics: Option, - /// The VA-API render node, such as `/dev/dri/renderD129`. Defaults to the - /// first that encodes, decodes, and scales. - #[cfg(all(target_os = "linux", feature = "vaapi"))] - #[usage(long, env = "MOQ_VAAPI_DEVICE", value_hint = usage_rs::ValueHint::FilePath)] + /// The render node the decoder opens, such as `/dev/dri/renderD129`: VA-API + /// opens it, and NVDEC the NVIDIA GPU behind it. Defaults to the first that + /// encodes, decodes, and scales. Linux only. + #[usage(long, env = "MOQ_VAAPI_DEVICE", value_hint = usage_rs::ValueHint::FilePath, conflicts = "--v4l2-decoder")] pub vaapi_device: Option, /// The V4L2 M2M decoder node, such as `/dev/video10`. Defaults to the first - /// that decodes H.264. - #[cfg(all(target_os = "linux", feature = "v4l2"))] + /// that decodes H.264. Linux only. #[usage(long, env = "MOQ_V4L2_DECODER", value_hint = usage_rs::ValueHint::FilePath)] pub v4l2_decoder: Option, } diff --git a/rs/moq-cli/src/play/headless.rs b/rs/moq-cli/src/play/headless.rs index 56bdabd08ea..5cfa5dd62ff 100644 --- a/rs/moq-cli/src/play/headless.rs +++ b/rs/moq-cli/src/play/headless.rs @@ -144,9 +144,7 @@ mod tests { let args = Args { catalog_format: None, metrics: None, - #[cfg(all(target_os = "linux", feature = "vaapi"))] vaapi_device: None, - #[cfg(all(target_os = "linux", feature = "v4l2"))] v4l2_decoder: None, delay: super::super::args::Delay::Auto, stretch: std::time::Duration::from_millis(200).into(), @@ -172,9 +170,7 @@ mod tests { let args = Args { catalog_format: None, metrics: Some(path.clone()), - #[cfg(all(target_os = "linux", feature = "vaapi"))] vaapi_device: None, - #[cfg(all(target_os = "linux", feature = "v4l2"))] v4l2_decoder: None, delay: super::super::args::Delay::Auto, stretch: std::time::Duration::from_millis(200).into(), @@ -203,9 +199,7 @@ mod tests { let args = Args { catalog_format: None, metrics: None, - #[cfg(all(target_os = "linux", feature = "vaapi"))] vaapi_device: None, - #[cfg(all(target_os = "linux", feature = "v4l2"))] v4l2_decoder: None, delay: super::super::args::Delay::Auto, stretch: std::time::Duration::from_millis(200).into(), diff --git a/rs/moq-cli/src/play/media.rs b/rs/moq-cli/src/play/media.rs index c2444d3a923..57ab5f8eebe 100644 --- a/rs/moq-cli/src/play/media.rs +++ b/rs/moq-cli/src/play/media.rs @@ -284,14 +284,7 @@ impl Media { // that imports none. let mut decode = moq_video::decode::Config::new(); decode.output = self.output.render().await; - #[cfg(all(target_os = "linux", feature = "vaapi"))] - { - decode.vaapi = self.args.vaapi_device.clone(); - } - #[cfg(all(target_os = "linux", feature = "v4l2"))] - { - decode.v4l2 = self.args.v4l2_decoder.clone(); - } + decode.device = crate::device(&self.args.vaapi_device, &self.args.v4l2_decoder); let decoder = moq_video::decode::Sink::open(config, &decode).await?; let delay = moq_net::track::Delay { min: Some(self.args.video_delay()), @@ -2132,9 +2125,7 @@ mod tests { args: Args { catalog_format: None, metrics: None, - #[cfg(all(target_os = "linux", feature = "vaapi"))] vaapi_device: None, - #[cfg(all(target_os = "linux", feature = "v4l2"))] v4l2_decoder: None, delay: Delay::Fixed(delay), stretch: Duration::ZERO.into(), diff --git a/rs/moq-cli/src/play/video.rs b/rs/moq-cli/src/play/video.rs index 43fea509254..5c8d6b7a5bc 100644 --- a/rs/moq-cli/src/play/video.rs +++ b/rs/moq-cli/src/play/video.rs @@ -533,9 +533,7 @@ mod tests { stretch: Duration::ZERO.into(), catalog_format: None, metrics: None, - #[cfg(all(target_os = "linux", feature = "vaapi"))] vaapi_device: None, - #[cfg(all(target_os = "linux", feature = "v4l2"))] v4l2_decoder: None, select: Default::default(), }, diff --git a/rs/moq-cli/src/publish.rs b/rs/moq-cli/src/publish.rs index c5d42c93109..a1b07b03ac4 100644 --- a/rs/moq-cli/src/publish.rs +++ b/rs/moq-cli/src/publish.rs @@ -158,15 +158,14 @@ pub struct CaptureArgs { #[usage(long = "no-audio", hide = true)] no_audio: bool, - /// The VA-API render node, such as `/dev/dri/renderD129`. Defaults to the - /// first that encodes, decodes, and scales. - #[cfg(all(target_os = "linux", feature = "vaapi"))] - #[usage(long, env = "MOQ_VAAPI_DEVICE", value_hint = usage_rs::ValueHint::FilePath)] + /// The render node the encoder opens, such as `/dev/dri/renderD129`: VA-API + /// opens it, and NVENC the NVIDIA GPU behind it. Defaults to the first that + /// encodes, decodes, and scales. Linux only. + #[usage(long, env = "MOQ_VAAPI_DEVICE", value_hint = usage_rs::ValueHint::FilePath, conflicts = "--v4l2-encoder")] pub vaapi_device: Option, /// The V4L2 M2M encoder node, such as `/dev/video11`. Defaults to the first - /// that encodes H.264. - #[cfg(all(target_os = "linux", feature = "v4l2"))] + /// that encodes H.264. Linux only. #[usage(long, env = "MOQ_V4L2_ENCODER", value_hint = usage_rs::ValueHint::FilePath)] pub v4l2_encoder: Option, } @@ -1132,7 +1131,10 @@ impl CaptureArgs { .map(|fps| moq_video::FrameRate::new(fps, 1)) .transpose() .map_err(anyhow::Error::from)?; - config.cursor = !self.no_cursor; + config.cursor = match self.no_cursor { + true => moq_video::capture::Cursor::Hidden, + false => moq_video::capture::Cursor::Drawn, + }; Ok(config) } @@ -1150,14 +1152,7 @@ impl CaptureArgs { }; options.bandwidth = bandwidth; options.on_demand = self.video == Mode::Demand; - #[cfg(all(target_os = "linux", feature = "vaapi"))] - { - options.vaapi = self.vaapi_device.clone(); - } - #[cfg(all(target_os = "linux", feature = "v4l2"))] - { - options.v4l2 = self.v4l2_encoder.clone(); - } + options.device = crate::device(&self.vaapi_device, &self.v4l2_encoder); Ok(options) } diff --git a/rs/moq-cli/src/transcode.rs b/rs/moq-cli/src/transcode.rs index 59fc09df171..df7b3598d3b 100644 --- a/rs/moq-cli/src/transcode.rs +++ b/rs/moq-cli/src/transcode.rs @@ -49,21 +49,25 @@ pub struct Args { #[usage(long)] encoder_sessions: Option, - /// The VA-API render node, such as `/dev/dri/renderD129`. Defaults to the - /// first that encodes, decodes, and scales. - #[cfg(all(target_os = "linux", feature = "vaapi"))] - #[usage(long, env = "MOQ_VAAPI_DEVICE", value_hint = usage_rs::ValueHint::FilePath)] + /// The render node the decoder and encoders open, such as + /// `/dev/dri/renderD129`: VA-API opens it, and NVDEC and NVENC the NVIDIA + /// GPU behind it. Defaults to the first that encodes, decodes, and scales. + /// Linux only. + #[usage( + long, + env = "MOQ_VAAPI_DEVICE", + value_hint = usage_rs::ValueHint::FilePath, + conflicts("--v4l2-encoder", "--v4l2-decoder") + )] pub vaapi_device: Option, /// The V4L2 M2M encoder node, such as `/dev/video11`. Defaults to the first - /// that encodes H.264. - #[cfg(all(target_os = "linux", feature = "v4l2"))] + /// that encodes H.264. Linux only. #[usage(long, env = "MOQ_V4L2_ENCODER", value_hint = usage_rs::ValueHint::FilePath)] pub v4l2_encoder: Option, /// The V4L2 M2M decoder node, such as `/dev/video10`. Defaults to the first - /// that decodes H.264. - #[cfg(all(target_os = "linux", feature = "v4l2"))] + /// that decodes H.264. Linux only. #[usage(long, env = "MOQ_V4L2_DECODER", value_hint = usage_rs::ValueHint::FilePath)] pub v4l2_decoder: Option, } @@ -131,15 +135,8 @@ pub async fn run(moq: MoqSide, args: Args, net: Net) -> anyhow::Result<()> { if let Some(serving) = args.encoder_sessions { config.sessions = moq_transcode::Sessions::new(serving); } - #[cfg(all(target_os = "linux", feature = "vaapi"))] - { - config.vaapi = args.vaapi_device.clone(); - } - #[cfg(all(target_os = "linux", feature = "v4l2"))] - { - config.v4l2_encoder = args.v4l2_encoder.clone(); - config.v4l2_decoder = args.v4l2_decoder.clone(); - } + config.decoder_device = crate::device(&args.vaapi_device, &args.v4l2_decoder); + config.encoder_device = crate::device(&args.vaapi_device, &args.v4l2_encoder); let source_path = moq_net::Path::try_from( moq.broadcast diff --git a/rs/moq-transcode/CHANGELOG.md b/rs/moq-transcode/CHANGELOG.md index 385385d261d..062412c2157 100644 --- a/rs/moq-transcode/CHANGELOG.md +++ b/rs/moq-transcode/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- [**breaking**] `Config::decoder_device` and `Config::encoder_device`, each a `moq_video::Device` present on every platform and feature set, replace the `cfg`-gated `vaapi`, `v4l2_decoder`, and `v4l2_encoder` paths, and also place NVIDIA work on a chosen GPU. - `active::Consumer` stops reporting a rendition the ladder retired before it encoded a frame, so a source that keeps resizing no longer grows every cursor. ### Fixed diff --git a/rs/moq-transcode/src/config.rs b/rs/moq-transcode/src/config.rs index 73875caced4..8afd5e67354 100644 --- a/rs/moq-transcode/src/config.rs +++ b/rs/moq-transcode/src/config.rs @@ -55,52 +55,32 @@ pub struct Config { /// one to close instead. The default sets no budget. pub sessions: Sessions, - /// The VA-API render node the decoder and encoders open; see - /// [`encode::Config::vaapi`](moq_video::encode::Config::vaapi). - #[cfg(all(target_os = "linux", feature = "vaapi"))] - pub vaapi: Option, - - /// The V4L2 M2M node the decoder opens; see - /// [`decode::Config::v4l2`](moq_video::decode::Config::v4l2). - #[cfg(all(target_os = "linux", feature = "v4l2"))] - pub v4l2_decoder: Option, - - /// The V4L2 M2M node the encoders open; see - /// [`encode::Config::v4l2`](moq_video::encode::Config::v4l2). - #[cfg(all(target_os = "linux", feature = "v4l2"))] - pub v4l2_encoder: Option, + /// The hardware the decoder opens; see + /// [`decode::Config::device`](moq_video::decode::Config::device). Name the + /// encoders' device too, so a decoded frame reaches them without a copy. + pub decoder_device: moq_video::Device, + + /// The hardware the encoders open; see + /// [`encode::Config::device`](moq_video::encode::Config::device). + pub encoder_device: moq_video::Device, } impl Config { - /// The source decoder: the configured implementation and devices, + /// The source decoder: the configured implementation and device, /// delivering frames where the resize expects them, at native size. pub(crate) fn decoder(&self) -> moq_video::decode::Config { let mut decoder = moq_video::decode::Config::new(); decoder.kind = self.decoder.clone(); decoder.output = self.resize.output; - #[cfg(all(target_os = "linux", feature = "vaapi"))] - { - decoder.vaapi = self.vaapi.clone(); - } - #[cfg(all(target_os = "linux", feature = "v4l2"))] - { - decoder.v4l2 = self.v4l2_decoder.clone(); - } + decoder.device = self.decoder_device.clone(); decoder } - /// A rung encoder at `size` and `framerate`: the configured implementation and devices. + /// A rung encoder at `size` and `framerate`: the configured implementation and device. pub(crate) fn encoder(&self, size: moq_video::Size, framerate: moq_video::FrameRate) -> moq_video::encode::Config { let mut encoder = moq_video::encode::Config::new(size.width, size.height, framerate); encoder.kind = self.encoder.clone(); - #[cfg(all(target_os = "linux", feature = "vaapi"))] - { - encoder.vaapi = self.vaapi.clone(); - } - #[cfg(all(target_os = "linux", feature = "v4l2"))] - { - encoder.v4l2 = self.v4l2_encoder.clone(); - } + encoder.device = self.encoder_device.clone(); encoder } } diff --git a/rs/moq-video/CHANGELOG.md b/rs/moq-video/CHANGELOG.md index 3bed76e8db6..76a31204976 100644 --- a/rs/moq-video/CHANGELOG.md +++ b/rs/moq-video/CHANGELOG.md @@ -28,6 +28,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - With the `pipewire` feature, `capture::cameras()` lists a webcam that a PipeWire node fronts as `pipewire:`, ahead of the V4L2 devices, instead of as `/dev/videoN`, since PipeWire shares the camera while V4L2 holds it exclusively. A `/dev/videoN` id still opens V4L2. - With the `pipewire` feature, `Source::Camera(None)` opens PipeWire's default camera outside a sandbox when PipeWire lists a camera, and `/dev/video0` otherwise. - [**breaking**] `encode::Producer::new` and `with_track` take a `moq_mux::catalog::Reserved` instead of the catalog, so a caller declares the encoder's GOP with `reserve().with_timeline_hint(gop)`. +- [**breaking**] `encode::Config`, `decode::Config`, `resize::Config`, and `encode::Capture` pick hardware with one `device: Device` field, present on every platform and feature set, in place of the `cfg`-gated `vaapi` and `v4l2` paths. `Device::Render` also places NVENC and NVDEC on the NVIDIA GPU behind the node, where they used to open CUDA device 0, and a named node steers automatic backend selection to the backends that drive it. +- [**breaking**] `capture::Config::cursor` is a `capture::Cursor` (`Drawn`, the default, or `Hidden`) instead of a bool, and `capture::Mode` is `#[non_exhaustive]`. ## [0.1.10](https://github.com/moq-dev/moq/compare/moq-video-v0.1.9...moq-video-v0.1.10) - 2026-10-05 diff --git a/rs/moq-video/src/capture/mod.rs b/rs/moq-video/src/capture/mod.rs index b94b294a7f2..c155782ea0e 100644 --- a/rs/moq-video/src/capture/mod.rs +++ b/rs/moq-video/src/capture/mod.rs @@ -200,6 +200,7 @@ impl Camera { /// A capture mode a source reports: one frame size and its exact frame rates. #[derive(Clone, Debug, PartialEq, Eq)] +#[non_exhaustive] pub struct Mode { /// Frame width in pixels. pub width: u32, @@ -300,9 +301,9 @@ pub struct Config { pub height: Option, /// Preferred exact frame rate. pub framerate: Option, - /// Draw the mouse cursor into captured frames. Screen/window/app sources - /// only; ignored by cameras. Defaults to `true`. - pub cursor: bool, + /// What happens to the mouse cursor. Screen/window/app sources only; + /// ignored by cameras. Defaults to [`Cursor::Drawn`]. + pub cursor: Cursor, /// Where captured pixels live. Defaults to [`Output::Native`]: whatever the /// platform captures into, a GPU surface or CPU pixels. pub output: Output, @@ -315,12 +316,33 @@ impl Default for Config { width: None, height: None, framerate: None, - cursor: true, + cursor: Cursor::default(), output: Output::Native, } } } +/// What a screen, window, or app capture does with the mouse cursor. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +#[non_exhaustive] +pub enum Cursor { + /// Drawn into each frame where it sits. + #[default] + Drawn, + /// Left out of the frames. + Hidden, +} + +impl Cursor { + /// Whether the platform should draw the cursor into the frame. + pub(crate) fn drawn(self) -> bool { + match self { + Self::Drawn => true, + Self::Hidden => false, + } + } +} + /// A live, async frame source opened via [`open`]. /// /// Every backend delivers frames through the same latest-frame channel, so the diff --git a/rs/moq-video/src/capture/pipewire.rs b/rs/moq-video/src/capture/pipewire.rs index 905184f33cb..4f152e0a581 100644 --- a/rs/moq-video/src/capture/pipewire.rs +++ b/rs/moq-video/src/capture/pipewire.rs @@ -69,7 +69,7 @@ pub(super) mod camera; /// Open a system-picked screen or window and stream frames from a PipeWire thread. pub(super) async fn open(config: &Config, selection: &portal::Selection) -> Result { - let (node_id, fd, session) = portal_negotiate(config.cursor, selection).await?; + let (node_id, fd, session) = portal_negotiate(config.cursor.drawn(), selection).await?; start( config, Capture { diff --git a/rs/moq-video/src/capture/screencapture.rs b/rs/moq-video/src/capture/screencapture.rs index c8e33acb592..386a340f1c4 100644 --- a/rs/moq-video/src/capture/screencapture.rs +++ b/rs/moq-video/src/capture/screencapture.rs @@ -158,7 +158,7 @@ async fn open(config: &Config, filter: &SCContentFilter, size: (u32, u32)) -> Re configuration.setHeight(height as usize); configuration.setPixelFormat(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange); configuration.setMinimumFrameInterval(CMTime::new(1, fps)); - configuration.setShowsCursor(config.cursor); + configuration.setShowsCursor(config.cursor.drawn()); } let chan = FrameChannel::new(); diff --git a/rs/moq-video/src/capture/wgc/native.rs b/rs/moq-video/src/capture/wgc/native.rs index 21e3ccfee73..defb035cb13 100644 --- a/rs/moq-video/src/capture/wgc/native.rs +++ b/rs/moq-video/src/capture/wgc/native.rs @@ -260,7 +260,7 @@ impl Capture { .session .as_ref() .unwrap() - .SetIsCursorCaptureEnabled(config.cursor) + .SetIsCursorCaptureEnabled(config.cursor.drawn()) .map_err(|e| error("configure WGC cursor", e))?; Ok(capture) } @@ -652,7 +652,7 @@ mod tests { } async fn capture_and_release(source: Source) { - for cursor in [true, false] { + for cursor in [crate::capture::Cursor::Drawn, crate::capture::Cursor::Hidden] { let config = Config { source: source.clone(), cursor, diff --git a/rs/moq-video/src/capture/x11.rs b/rs/moq-video/src/capture/x11.rs index 92247cf4eff..c71e81b89a5 100644 --- a/rs/moq-video/src/capture/x11.rs +++ b/rs/moq-video/src/capture/x11.rs @@ -293,7 +293,7 @@ impl Capture { tracing::debug!(source = %name, shared_memory = shm.is_some(), "X11 capture opened"); let framerate = config.framerate.unwrap_or(crate::FrameRate::integer(DEFAULT_FRAMERATE)); let interval = Duration::from_secs_f64(1.0 / framerate.as_f64()); - let cursor = config.cursor + let cursor = config.cursor.drawn() && connection .xfixes_query_version(5, 0) .ok() @@ -1195,7 +1195,7 @@ mod tests { "the X11 benchmark needs a screen of at least 1920x1080" ); let config = Config { - cursor: false, + cursor: crate::capture::Cursor::Hidden, ..Default::default() }; for (width, height) in [(640u16, 480u16), (1280, 720), (1920, 1080)] { diff --git a/rs/moq-video/src/decode/backend/mod.rs b/rs/moq-video/src/decode/backend/mod.rs index b0fd8c60912..11835bbb979 100644 --- a/rs/moq-video/src/decode/backend/mod.rs +++ b/rs/moq-video/src/decode/backend/mod.rs @@ -25,6 +25,7 @@ use bytes::Bytes; use moq_net::Timestamp; use super::decoder::{Config, Kind}; +use crate::device::Drives; use crate::{Error, Frame, Size}; #[cfg(feature = "openh264")] @@ -208,6 +209,8 @@ struct Candidate { name: &'static str, supports: fn(Codec) -> bool, open: Open, + /// The kind of [`Device`](crate::Device) it opens, which a named one steers selection by. + drives: Drives, } /// Hardware backends, in priority order. Platform-gated so only the ones that @@ -218,30 +221,35 @@ const HARDWARE: &[Candidate] = &[ name: videotoolbox::NAME, supports: |c| matches!(c, Codec::H264 | Codec::H265), open: videotoolbox::VideoToolbox::open, + drives: Drives::Own, }, #[cfg(target_os = "windows")] Candidate { name: mediafoundation::NAME, supports: |c| matches!(c, Codec::H264 | Codec::H265), open: mediafoundation::MediaFoundation::open, + drives: Drives::Own, }, #[cfg(all(target_os = "android", feature = "mediacodec"))] Candidate { name: mediacodec::NAME, supports: |c| matches!(c, Codec::H264 | Codec::H265 | Codec::Av1), open: mediacodec::MediaCodec::open, + drives: Drives::Own, }, #[cfg(all(target_os = "linux", feature = "nvidia"))] Candidate { name: nvdec::NAME, supports: |c| matches!(c, Codec::H264 | Codec::H265 | Codec::Av1), open: nvdec::Nvdec::open, + drives: Drives::Nvidia, }, #[cfg(all(target_os = "linux", feature = "vaapi"))] Candidate { name: vaapi::NAME, supports: |c| matches!(c, Codec::H264 | Codec::H265), open: vaapi::Vaapi::open, + drives: Drives::Render, }, // Last of the Linux hardware decoders, for the same reason as its encode // counterpart: the SoC blocks it drives are the only hardware on a board that @@ -251,6 +259,7 @@ const HARDWARE: &[Candidate] = &[ name: v4l2::NAME, supports: |c| matches!(c, Codec::H264), open: v4l2::V4l2::open, + drives: Drives::Video, }, ]; @@ -260,12 +269,14 @@ const SOFTWARE: &[Candidate] = &[ name: openh264::NAME, supports: |c| matches!(c, Codec::H264), open: openh264::Openh264::open, + drives: Drives::Own, }, #[cfg(feature = "vpx")] Candidate { name: vpx::NAME, supports: |c| matches!(c, Codec::Vp8 | Codec::Vp9), open: vpx::Vpx::open, + drives: Drives::Own, }, ]; @@ -278,42 +289,50 @@ const NAMED_ONLY: &[Candidate] = &[ name: probe::NAME, supports: |c| matches!(c, Codec::H264), open: probe::Probe::open, + drives: Drives::Own, }, Candidate { name: probe::BUFFERED_NAME, supports: |c| matches!(c, Codec::H264 | Codec::H265 | Codec::Vp9), open: probe::Buffered::open, + drives: Drives::Own, }, Candidate { name: probe::REORDER_NAME, supports: |c| matches!(c, Codec::H264), open: probe::Reorder::open, + drives: Drives::Own, }, Candidate { name: probe::HOLDING_NAME, supports: |c| matches!(c, Codec::H264), open: probe::Reorder::open_holding, + drives: Drives::Own, }, Candidate { name: probe::IMMEDIATE_NAME, supports: |c| matches!(c, Codec::H264), open: probe::Immediate::open, + drives: Drives::Own, }, Candidate { name: probe::NATIVE_NAME, supports: |c| matches!(c, Codec::H264), open: probe::Native::open, + drives: Drives::Own, }, Candidate { name: probe::REFUSING_NAME, supports: |c| matches!(c, Codec::H264), open: probe::Refusing::open, + drives: Drives::Own, }, #[cfg(not(apple))] Candidate { name: probe::BLOCKING_FLUSH_NAME, supports: |c| matches!(c, Codec::H264), open: probe::BlockingFlush::open, + drives: Drives::Own, }, ]; @@ -384,6 +403,9 @@ fn select(stream: &Stream, attempts: Vec, config: &Config) -> Result = Vec::new(); let mut skipped = Vec::new(); + // Passed over unopened for driving another device than the named one, which + // says nothing about the stream, so neither a refusal nor a fault. + let mut passed = Vec::new(); let mut faulted = false; for attempt in attempts { @@ -392,6 +414,10 @@ fn select(stream: &Stream, attempts: Vec, config: &Config) -> Result { @@ -428,6 +454,10 @@ fn select(stream: &Stream, attempts: Vec, config: &Config) -> Result assert!(reason.contains("/dev/video10"), "{reason}"), + Err(other) => panic!("expected NoDecoder, got {other:?}"), + Ok(backend) => panic!("expected NoDecoder, opened {}", backend.name()), + } + } + + /// The backend that drives the named device refusing the stream is a refusal, + /// even beside backends passed over for driving another device. + #[test] + fn the_named_devices_refusal_is_the_streams() { + const RENDER: Candidate = Candidate { + name: "render", + supports: |c| matches!(c, Codec::H264), + open: |_, _| Err(Error::UnsupportedCodec("too large".into())), + drives: Drives::Render, + }; + const VIDEO: Candidate = Candidate { + drives: Drives::Video, + ..WORKING + }; + + let mut config = Config::new(); + config.device = crate::Device::Render("/dev/dri/renderD129".into()); + let attempts = vec![Attempt::hardware(&VIDEO), Attempt::hardware(&RENDER)]; + assert!(matches!( + select(&Codec::H264.into(), attempts, &config), + Err(Error::UnsupportedCodec(_)) + )); + } + /// A name no candidate answers to has to say so, and say what it could have /// been asked for instead. #[test] diff --git a/rs/moq-video/src/decode/backend/nvdec.rs b/rs/moq-video/src/decode/backend/nvdec.rs index 0bb48d4d38e..9201fe9a921 100644 --- a/rs/moq-video/src/decode/backend/nvdec.rs +++ b/rs/moq-video/src/decode/backend/nvdec.rs @@ -137,7 +137,8 @@ impl Nvdec { // `CudaContext::new` retains the device's primary context, the same one // the NVENC backend uses, so frames pass between them without a copy. - let ctx = CudaContext::new(0).map_err(|e| codec_err(format!("CUDA init: {e:?}")))?; + let ordinal = cuda::ordinal(&config.device)?.unwrap_or(0); + let ctx = CudaContext::new(ordinal).map_err(|e| codec_err(format!("CUDA init: {e:?}")))?; ctx.bind_to_thread() .map_err(|e| codec_err(format!("CUDA bind: {e:?}")))?; // The decoder itself waits for the first sequence header, so ask now diff --git a/rs/moq-video/src/decode/backend/v4l2.rs b/rs/moq-video/src/decode/backend/v4l2.rs index 27fe3301458..a38e9998480 100644 --- a/rs/moq-video/src/decode/backend/v4l2.rs +++ b/rs/moq-video/src/decode/backend/v4l2.rs @@ -146,7 +146,7 @@ impl V4l2 { return Err(Error::UnsupportedCodec(format!("{NAME} decodes H.264 only"))); } - let device = v4l2::open(&ROLE, config.v4l2.as_deref())?; + let device = v4l2::open(&ROLE, config.device.video())?; // Before the first access unit, so the announcement of the stream's size // cannot be missed. device.subscribe_source_change()?; diff --git a/rs/moq-video/src/decode/backend/vaapi.rs b/rs/moq-video/src/decode/backend/vaapi.rs index 3a445656a4a..b7adc0f2abe 100644 --- a/rs/moq-video/src/decode/backend/vaapi.rs +++ b/rs/moq-video/src/decode/backend/vaapi.rs @@ -120,14 +120,14 @@ impl Vaapi { other => return Err(Error::codec(anyhow::anyhow!("VAAPI cannot decode {}", other.label()))), }; if let Some((profile, name)) = required - && offered(codec, config.vaapi.as_deref(), profile) == Some(false) + && offered(codec, config.device.render(), profile) == Some(false) { return Err(Error::UnsupportedCodec(format!("no VA-API driver here decodes {name}"))); } let vaapi = VaapiConfig { codec, - device: vaapi::device(codec, config.vaapi.as_deref()), + device: vaapi::device(codec, config.device.render()), }; let decoder = Decoder::new(vaapi).map_err(|e| Error::codec(anyhow::anyhow!("VAAPI decoder init: {e:?}")))?; diff --git a/rs/moq-video/src/decode/decoder.rs b/rs/moq-video/src/decode/decoder.rs index 9708b1eaad7..5516d65d3ae 100644 --- a/rs/moq-video/src/decode/decoder.rs +++ b/rs/moq-video/src/decode/decoder.rs @@ -87,16 +87,13 @@ pub struct Config { /// [`Frame::resize`](crate::Frame::resize) to the rest; the hint only lets a /// decoder that can make that a no-op do so. pub scale_hint: Option, - /// The VA-API render node, such as `/dev/dri/renderD129`. `None` picks the - /// first that encodes, decodes, and scales, so a DMA-BUF moves between them - /// without a copy. A named node is used even when it fails to open. - #[cfg(all(target_os = "linux", feature = "vaapi"))] - pub vaapi: Option, - /// The V4L2 M2M decoder node, such as `/dev/video10`. `None` searches every - /// node for one that decodes H.264, which a driver whose format enumeration - /// is wrong can defeat. - #[cfg(all(target_os = "linux", feature = "v4l2"))] - pub v4l2: Option, + /// The hardware the decoder opens. [`Device::Auto`](crate::Device::Auto) + /// (the default) picks one: VA-API the first render node that encodes, + /// decodes, and scales, so a DMA-BUF moves between them without a copy; + /// NVDEC the first CUDA device; V4L2 the first node that decodes H.264, + /// which a driver whose format enumeration is wrong can defeat. Name the + /// encoder's device so a frame passes to it without a copy. + pub device: crate::Device, } impl Config { diff --git a/rs/moq-video/src/device.rs b/rs/moq-video/src/device.rs new file mode 100644 index 00000000000..3f4d994900e --- /dev/null +++ b/rs/moq-video/src/device.rs @@ -0,0 +1,230 @@ +//! Which hardware a codec or scaler opens. + +use std::path::{Path, PathBuf}; + +use crate::Error; + +/// The hardware a decoder, encoder, or scaler opens. +/// +/// Present on every platform and honoured by whichever backend opens. A Linux +/// hardware backend that drives another kind of device is passed over, so a +/// named node also steers automatic backend selection; software and a platform +/// API that picks its own device (VideoToolbox, Media Foundation, MediaCodec) +/// ignore it. A named device that fails to open is an error rather than a +/// search for another one: only [`Auto`](Self::Auto) chooses. Whether software +/// may take over when no hardware opens is the backend `Kind`'s call, not this. +#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] +#[non_exhaustive] +pub enum Device { + /// Let each backend pick its device. + #[default] + Auto, + /// A DRM render node, such as `/dev/dri/renderD129`: VA-API opens it, and + /// NVENC and NVDEC open the CUDA device behind it when an NVIDIA driver runs it. + Render(PathBuf), + /// A V4L2 memory-to-memory codec node, such as `/dev/video11`. + Video(PathBuf), +} + +impl Device { + /// The named render node, if any. + #[cfg_attr( + not(all(target_os = "linux", any(feature = "vaapi", feature = "nvidia"))), + allow(dead_code, reason = "only the VA-API and NVIDIA backends open one") + )] + pub(crate) fn render(&self) -> Option<&Path> { + match self { + Self::Render(path) => Some(path), + _ => None, + } + } + + /// The named V4L2 codec node, if any. + #[cfg_attr( + not(all(target_os = "linux", feature = "v4l2")), + allow(dead_code, reason = "only the V4L2 backends open one") + )] + pub(crate) fn video(&self) -> Option<&Path> { + match self { + Self::Video(path) => Some(path), + _ => None, + } + } + + /// Whether a backend that drives `drives` opens this device, so selection + /// passes over one that would open another. + /// + /// Fails when telling needs the named node's driver and that cannot be read. + pub(crate) fn takes(&self, drives: Drives) -> Result { + match (self, drives) { + (Self::Auto, _) | (_, Drives::Own) => Ok(true), + (Self::Render(_), Drives::Render) | (Self::Video(_), Drives::Video) => Ok(true), + (Self::Render(path), Drives::Nvidia) => nvidia(path), + _ => Ok(false), + } + } +} + +impl std::fmt::Display for Device { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Auto => f.write_str("any device"), + Self::Render(path) => write!(f, "render node {}", path.display()), + Self::Video(path) => write!(f, "codec node {}", path.display()), + } + } +} + +/// The kind of [`Device`] a backend opens. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[allow(dead_code, reason = "a build without a backend of some kind never names that kind")] +pub(crate) enum Drives { + /// None a [`Device`] names: software, or a platform API that picks its own. + Own, + /// Any DRM render node (VA-API). + Render, + /// A DRM render node an NVIDIA driver runs (NVENC, NVDEC). + Nvidia, + /// A V4L2 codec node. + Video, +} + +/// Whether the NVIDIA driver runs the render node at `path`. +#[cfg(target_os = "linux")] +fn nvidia(path: &Path) -> Result { + Ok(field(&uevent(path)?, "DRIVER") == Some("nvidia")) +} + +#[cfg(not(target_os = "linux"))] +fn nvidia(_path: &Path) -> Result { + Ok(false) +} + +/// The sysfs `uevent` of the device behind the character node at `path`. +#[cfg(target_os = "linux")] +fn uevent(path: &Path) -> Result { + use std::os::unix::fs::{FileTypeExt, MetadataExt}; + + let failed = |err: &dyn std::fmt::Display| Error::codec(anyhow::anyhow!("{}: {err}", path.display())); + let meta = std::fs::metadata(path).map_err(|err| failed(&err))?; + if !meta.file_type().is_char_device() { + return Err(failed(&"not a device node")); + } + let rdev = meta.rdev(); + let sysfs = format!( + "/sys/dev/char/{}:{}/device/uevent", + libc::major(rdev), + libc::minor(rdev) + ); + std::fs::read_to_string(&sysfs).map_err(|err| failed(&format_args!("{sysfs}: {err}"))) +} + +/// The value of `key` in a sysfs `uevent`'s `KEY=value` lines. +#[cfg(target_os = "linux")] +fn field<'a>(uevent: &'a str, key: &str) -> Option<&'a str> { + uevent + .lines() + .find_map(|line| line.strip_prefix(key)?.strip_prefix('=')) +} + +/// A PCI device's address, without the function: what CUDA reports per device. +#[cfg(all(target_os = "linux", feature = "nvidia"))] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct Pci { + pub domain: u32, + pub bus: u32, + pub device: u32, +} + +#[cfg(all(target_os = "linux", feature = "nvidia"))] +impl Pci { + /// The PCI address of the device behind the node at `path`. + pub(crate) fn of(path: &Path) -> Result { + let uevent = uevent(path)?; + let slot = field(&uevent, "PCI_SLOT_NAME") + .ok_or_else(|| Error::codec(anyhow::anyhow!("{} is not a PCI device", path.display())))?; + Self::parse(slot) + .ok_or_else(|| Error::codec(anyhow::anyhow!("{}: malformed PCI address {slot}", path.display()))) + } + + /// Parses `domain:bus:device.function`, all hex, as sysfs spells it. + fn parse(slot: &str) -> Option { + let (domain, rest) = slot.split_once(':')?; + let (bus, rest) = rest.split_once(':')?; + let (device, _function) = rest.split_once('.')?; + let hex = |s: &str| u32::from_str_radix(s, 16).ok(); + Some(Self { + domain: hex(domain)?, + bus: hex(bus)?, + device: hex(device)?, + }) + } + + /// The ordinal of the device at this address among `devices`, each an ordinal and its address. + pub(crate) fn find(self, devices: impl IntoIterator) -> Option { + devices + .into_iter() + .find_map(|(ordinal, pci)| (pci == self).then_some(ordinal)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_named_node_steers_to_the_backends_that_drive_its_kind() { + let render = Device::Render("/dev/dri/renderD129".into()); + let video = Device::Video("/dev/video11".into()); + for drives in [Drives::Own, Drives::Render, Drives::Nvidia, Drives::Video] { + assert!(Device::Auto.takes(drives).unwrap(), "{drives:?} refused Auto"); + } + assert!(render.takes(Drives::Own).unwrap()); + assert!(render.takes(Drives::Render).unwrap()); + assert!(!render.takes(Drives::Video).unwrap()); + assert!(video.takes(Drives::Own).unwrap()); + assert!(video.takes(Drives::Video).unwrap()); + assert!(!video.takes(Drives::Render).unwrap()); + assert!(!video.takes(Drives::Nvidia).unwrap()); + } + + /// The NVIDIA check reads the node, so a node that is not there is an error, + /// not a quiet pass to another device. + #[cfg(target_os = "linux")] + #[test] + fn a_missing_render_node_fails_the_nvidia_check() { + let missing = Device::Render("/dev/dri/renderD999-missing".into()); + assert!(missing.takes(Drives::Nvidia).is_err()); + let file = Device::Render("/proc/self/status".into()); + assert!(file.takes(Drives::Nvidia).is_err()); + } + + #[cfg(target_os = "linux")] + #[test] + fn uevent_fields_are_read_by_exact_key() { + let uevent = "DRIVER=nvidia\nPCI_CLASS=30000\nPCI_SLOT_NAME=0000:01:00.0\n"; + assert_eq!(field(uevent, "DRIVER"), Some("nvidia")); + assert_eq!(field(uevent, "PCI_SLOT_NAME"), Some("0000:01:00.0")); + assert_eq!(field(uevent, "PCI"), None); + } + + #[cfg(all(target_os = "linux", feature = "nvidia"))] + #[test] + fn a_render_node_maps_to_the_cuda_device_at_its_pci_address() { + let first = Pci::parse("0000:01:00.0").unwrap(); + let second = Pci::parse("0001:a1:1f.0").unwrap(); + assert_eq!( + second, + Pci { + domain: 1, + bus: 0xa1, + device: 0x1f + } + ); + let devices = [(0, first), (1, second)]; + assert_eq!(first.find(devices), Some(0)); + assert_eq!(second.find(devices), Some(1)); + assert_eq!(Pci::parse("0000:02:00.0").unwrap().find(devices), None); + assert_eq!(Pci::parse("0000:01:00"), None); + } +} diff --git a/rs/moq-video/src/encode/backend/mod.rs b/rs/moq-video/src/encode/backend/mod.rs index 013f49949c7..902c3f0c96f 100644 --- a/rs/moq-video/src/encode/backend/mod.rs +++ b/rs/moq-video/src/encode/backend/mod.rs @@ -18,6 +18,7 @@ //! when this build enables it. use super::encoder::{Applied, Codec, Config, Kind}; +use crate::device::Drives; use crate::encode::Encoded; use crate::{Error, Frame}; @@ -240,6 +241,8 @@ struct Candidate { allow(dead_code, reason = "only the `capture` feature uses it") )] cuts: bool, + /// The kind of [`Device`](crate::Device) it opens, which a named one steers selection by. + drives: Drives, #[cfg(target_os = "linux")] vulkan: bool, } @@ -253,6 +256,7 @@ const HARDWARE: &[Candidate] = &[ codecs: &[Codec::H264, Codec::H265], open: videotoolbox::VideoToolbox::open, cuts: true, + drives: Drives::Own, #[cfg(target_os = "linux")] vulkan: false, }, @@ -262,6 +266,7 @@ const HARDWARE: &[Candidate] = &[ codecs: &[Codec::H264, Codec::H265], open: mediafoundation::MediaFoundation::open, cuts: true, + drives: Drives::Own, #[cfg(target_os = "linux")] vulkan: false, }, @@ -271,6 +276,7 @@ const HARDWARE: &[Candidate] = &[ codecs: &[Codec::H264, Codec::H265], open: mediacodec::MediaCodec::open, cuts: true, + drives: Drives::Own, #[cfg(target_os = "linux")] vulkan: false, }, @@ -280,6 +286,7 @@ const HARDWARE: &[Candidate] = &[ codecs: &[Codec::H264, Codec::H265], open: nvenc::Nvenc::open, cuts: true, + drives: Drives::Nvidia, #[cfg(target_os = "linux")] vulkan: true, }, @@ -289,6 +296,7 @@ const HARDWARE: &[Candidate] = &[ codecs: &[Codec::H264, Codec::H265], open: vaapi::Vaapi::open, cuts: true, + drives: Drives::Render, #[cfg(target_os = "linux")] vulkan: false, }, @@ -301,6 +309,7 @@ const HARDWARE: &[Candidate] = &[ codecs: &[Codec::H264], open: v4l2::V4l2::open, cuts: false, + drives: Drives::Video, #[cfg(target_os = "linux")] vulkan: false, }, @@ -316,6 +325,7 @@ const SOFTWARE: &[Candidate] = &[ codecs: &[Codec::H264], open: openh264::Openh264::open, cuts: true, + drives: Drives::Own, #[cfg(target_os = "linux")] vulkan: false, }, @@ -331,6 +341,7 @@ const NAMED_ONLY: &[Candidate] = &[ codecs: &[Codec::H264], open: probe::Probe::open, cuts: true, + drives: Drives::Own, #[cfg(target_os = "linux")] vulkan: false, }, @@ -339,6 +350,7 @@ const NAMED_ONLY: &[Candidate] = &[ codecs: &[Codec::H264], open: probe::Probe::open_no_cut, cuts: false, + drives: Drives::Own, #[cfg(target_os = "linux")] vulkan: false, }, @@ -440,6 +452,10 @@ fn select(attempts: Vec, config: &Config) -> Result, E tried.push(format!("{name}: cannot import external Vulkan {device}")); continue; } + if !config.device.takes(attempt.candidate.drives)? { + tried.push(format!("{name}: does not open {}", config.device)); + continue; + } match (attempt.candidate.open)(config) { Ok(backend) => { // `Auto` returning a software encoder is otherwise invisible except for @@ -650,6 +666,7 @@ mod tests { codecs: &[Codec::H264], open: Stub::open, cuts: true, + drives: Drives::Own, #[cfg(target_os = "linux")] vulkan: false, }; @@ -661,6 +678,7 @@ mod tests { codecs: &[Codec::H264], open: |_| Err(Error::codec(anyhow::anyhow!("driver libraries not found"))), cuts: true, + drives: Drives::Own, #[cfg(target_os = "linux")] vulkan: false, }; @@ -686,6 +704,37 @@ mod tests { } } + /// A named node passes over the hardware that drives another kind of device + /// without opening it, and says so when nothing else opens. + #[test] + fn a_named_device_passes_over_other_hardware() { + const RENDER: Candidate = Candidate { + name: "render", + open: |_| panic!("opened a render backend for a codec node"), + drives: Drives::Render, + ..WORKING + }; + const VIDEO: Candidate = Candidate { + name: "video", + drives: Drives::Video, + ..WORKING + }; + + let mut config = config(); + config.device = crate::Device::Video("/dev/video11".into()); + let opened = select(vec![Attempt::hardware(&RENDER), Attempt::hardware(&VIDEO)], &config).unwrap(); + assert_eq!(opened.name(), "stub"); + + match select(vec![Attempt::hardware(&RENDER)], &config) { + Err(Error::NoEncoder(reason)) => assert!( + reason.contains("render: does not open codec node /dev/video11"), + "{reason}" + ), + Err(other) => panic!("expected NoEncoder, got {other:?}"), + Ok(backend) => panic!("expected NoEncoder, opened {}", backend.name()), + } + } + #[tracing_test::traced_test] #[test] fn falling_past_hardware_warns() { diff --git a/rs/moq-video/src/encode/backend/nvenc.rs b/rs/moq-video/src/encode/backend/nvenc.rs index 26b58227834..43ef50d90c4 100644 --- a/rs/moq-video/src/encode/backend/nvenc.rs +++ b/rs/moq-video/src/encode/backend/nvenc.rs @@ -144,7 +144,7 @@ impl Nvenc { // cudarc 0.19's DriverError is Debug-only (no Display), so format with `{e:?}`. let codec_guid = codec_guid(config.codec); - let cuda = input_context(config.input)?; + let cuda = input_context(config.input, &config.device)?; let encoder = Encoder::initialize_with_cuda(cuda.clone()) .map_err(|e| Error::codec(anyhow::anyhow!("NVENC init: {e}")))?; @@ -461,11 +461,22 @@ fn drain_output(submission: moq_nvenc::Submission) -> Result<(Vec, I, .map_err(|e| Error::codec(anyhow::anyhow!("NVENC lock output: {e}"))) } -/// Match the declared Vulkan device before allocating an NVENC session. -fn input_context(input: Option) -> Result, Error> { +/// The CUDA device NVENC opens: the declared Vulkan device's, the named render +/// node's, or the first, refusing a render node that is not the Vulkan device. +fn input_context(input: Option, named: &crate::Device) -> Result, Error> { + let ordinal = cuda::ordinal(named)?; let Some(device) = input else { - return CudaContext::new(0).map_err(|e| Error::codec(anyhow::anyhow!("CUDA init: {e:?}"))); + return CudaContext::new(ordinal.unwrap_or(0)).map_err(|e| Error::codec(anyhow::anyhow!("CUDA init: {e:?}"))); }; + let input = vulkan_ordinal(device)?; + if ordinal.is_some_and(|ordinal| ordinal != input) { + return Err(Error::Unsupported(format!("{named} is not external Vulkan {device}"))); + } + CudaContext::new(input).map_err(|e| Error::codec(anyhow::anyhow!("CUDA input device {input}: {e:?}"))) +} + +/// The CUDA ordinal of the declared Vulkan device, matched by UUID. +fn vulkan_ordinal(device: vulkan::Device) -> Result { matching_driver(device)?; let count = CudaContext::device_count().map_err(|e| Error::codec(anyhow::anyhow!("CUDA enumerate: {e:?}")))?; for ordinal in 0..count { @@ -474,8 +485,7 @@ fn input_context(input: Option) -> Result, Erro let uuid = cudarc::driver::result::device::get_uuid(physical) .map_err(|e| Error::codec(anyhow::anyhow!("CUDA UUID: {e:?}")))?; if uuid.bytes.map(|byte| u8::from_ne_bytes(byte.to_ne_bytes())) == device.device_uuid { - return CudaContext::new(ordinal as usize) - .map_err(|e| Error::codec(anyhow::anyhow!("CUDA input device {ordinal}: {e:?}"))); + return Ok(ordinal as usize); } } Err(Error::Unsupported(format!( diff --git a/rs/moq-video/src/encode/backend/v4l2.rs b/rs/moq-video/src/encode/backend/v4l2.rs index 0305b8c55ed..20df8cec1ae 100644 --- a/rs/moq-video/src/encode/backend/v4l2.rs +++ b/rs/moq-video/src/encode/backend/v4l2.rs @@ -157,7 +157,7 @@ impl V4l2 { let size = config.size(); size.validate("V4L2 encode of")?; - let device = v4l2::open(&ROLE, config.v4l2.as_deref())?; + let device = v4l2::open(&ROLE, config.device.video())?; // Before `VIDIOC_S_FMT`: bcm2835-codec picks its encoder configuration // from the profile and level at format time, and a level set afterwards diff --git a/rs/moq-video/src/encode/backend/vaapi.rs b/rs/moq-video/src/encode/backend/vaapi.rs index 84567aa0dc7..7e34e4a7c0f 100644 --- a/rs/moq-video/src/encode/backend/vaapi.rs +++ b/rs/moq-video/src/encode/backend/vaapi.rs @@ -93,7 +93,7 @@ impl Vaapi { Codec::H265 => moq_vaapi::Codec::H265, }; let vaapi = VaapiConfig { - device: vaapi::device(codec, config.vaapi.as_deref()), + device: vaapi::device(codec, config.device.render()), color: vaapi::color(color)?, codec, ..VaapiConfig::new( diff --git a/rs/moq-video/src/encode/encoder.rs b/rs/moq-video/src/encode/encoder.rs index 0728ffbfe26..fd3e4c0fbfc 100644 --- a/rs/moq-video/src/encode/encoder.rs +++ b/rs/moq-video/src/encode/encoder.rs @@ -10,7 +10,7 @@ use std::rc::Rc; use super::Encoded; use super::backend::{self, Backend}; -use crate::{Color, Error, Frame, FrameRate, Size}; +use crate::{Color, Device, Error, Frame, FrameRate, Size}; /// Output video codec. `#[non_exhaustive]` so new codecs can be added without /// breaking external `match`es. @@ -203,16 +203,15 @@ pub struct Config { /// `None` retains selection for CPU and native decoder/capture surfaces. #[cfg(target_os = "linux")] pub input: Option, - /// The VA-API render node, such as `/dev/dri/renderD129`. `None` picks the - /// first that encodes, decodes, and scales, so a DMA-BUF moves between them - /// without a copy. A named node is used even when it fails to open. - #[cfg(all(target_os = "linux", feature = "vaapi"))] - pub vaapi: Option, - /// The V4L2 M2M encoder node, such as `/dev/video11`. `None` searches every - /// node for one that encodes H.264, which a driver whose format enumeration - /// is wrong can defeat. - #[cfg(all(target_os = "linux", feature = "v4l2"))] - pub v4l2: Option, + /// The hardware the encoder opens. [`Device::Auto`] (the default) picks + /// one: VA-API the first render node that encodes, decodes, and scales, so a + /// DMA-BUF moves between them without a copy; NVENC the first CUDA device, or + /// the one behind [`input`](Self::input); V4L2 the first node that encodes + /// H.264, which a driver whose format enumeration is wrong can defeat. + /// + /// A render node an NVIDIA driver runs that is not the device behind + /// `input` is refused. + pub device: Device, } impl Config { @@ -232,10 +231,7 @@ impl Config { resize: None, #[cfg(target_os = "linux")] input: None, - #[cfg(all(target_os = "linux", feature = "vaapi"))] - vaapi: None, - #[cfg(all(target_os = "linux", feature = "v4l2"))] - v4l2: None, + device: Device::Auto, } } @@ -339,14 +335,12 @@ impl Config { self.color.unwrap_or_else(|| Color::infer(self.size())) } - /// The resize options, scaling on this encoder's VA-API node unless they name one. + /// The resize options, scaling on this encoder's device unless they name one. fn resolved_resize(&self) -> Option { - let resize = self.resize.clone()?; - #[cfg(all(target_os = "linux", feature = "vaapi"))] - let resize = crate::resize::Config { - vaapi: resize.vaapi.or_else(|| self.vaapi.clone()), - ..resize - }; + let mut resize = self.resize.clone()?; + if resize.device == Device::Auto { + resize.device = self.device.clone(); + } Some(resize) } diff --git a/rs/moq-video/src/encode/producer.rs b/rs/moq-video/src/encode/producer.rs index f9de903fd9a..3556578ccf3 100644 --- a/rs/moq-video/src/encode/producer.rs +++ b/rs/moq-video/src/encode/producer.rs @@ -310,12 +310,8 @@ pub struct Capture { /// the recent past in the track's cache. On demand saves the capture and encode while nobody /// watches, and leaves nothing to fetch from before the first viewer arrived. pub on_demand: bool, - /// The VA-API render node the encoder opens; see [`Config::vaapi`](encoder::Config::vaapi). - #[cfg(all(target_os = "linux", feature = "vaapi"))] - pub vaapi: Option, - /// The V4L2 M2M node the encoder opens; see [`Config::v4l2`](encoder::Config::v4l2). - #[cfg(all(target_os = "linux", feature = "v4l2"))] - pub v4l2: Option, + /// The hardware the encoder opens; see [`Config::device`](encoder::Config::device). + pub device: crate::Device, } #[cfg(feature = "capture")] @@ -326,14 +322,7 @@ impl Capture { config.bitrate = self.bitrate; config.codec = self.codec; config.kind = self.kind.clone(); - #[cfg(all(target_os = "linux", feature = "vaapi"))] - { - config.vaapi = self.vaapi.clone(); - } - #[cfg(all(target_os = "linux", feature = "v4l2"))] - { - config.v4l2 = self.v4l2.clone(); - } + config.device = self.device.clone(); config } } diff --git a/rs/moq-video/src/frame.rs b/rs/moq-video/src/frame.rs index 74026681f35..9fe82f4113e 100644 --- a/rs/moq-video/src/frame.rs +++ b/rs/moq-video/src/frame.rs @@ -613,7 +613,7 @@ impl Surface { // download and resize on the CPU. #[cfg(all(target_os = "linux", feature = "vaapi"))] Surface::DmaBuf(buffer) if output == crate::Output::Native => { - match vaapi::resize(buffer, size, config.vaapi.as_deref()) { + match vaapi::resize(buffer, size, config.device.render()) { Ok(scaled) => Surface::DmaBuf(scaled), Err(err) => { static WARN_ONCE: std::sync::Once = std::sync::Once::new(); diff --git a/rs/moq-video/src/frame/cuda.rs b/rs/moq-video/src/frame/cuda.rs index f805244132c..7b80fbf7ced 100644 --- a/rs/moq-video/src/frame/cuda.rs +++ b/rs/moq-video/src/frame/cuda.rs @@ -97,6 +97,39 @@ pub(crate) fn scales(ctx: &Arc) -> bool { kernels(ctx).is_ok() } +/// The CUDA ordinal of the device behind `device`'s render node, matched by PCI +/// address, or `None` when it names no render node. +/// +/// Fails when no CUDA device sits at the node's address, which a node another +/// driver runs, or one `CUDA_VISIBLE_DEVICES` hides, cannot satisfy. +pub(crate) fn ordinal(device: &crate::Device) -> Result, Error> { + let Some(node) = device.render() else { + return Ok(None); + }; + let pci = crate::device::Pci::of(node)?; + let cuda = |e: result::DriverError| Error::codec(anyhow::anyhow!("CUDA enumerate: {e:?}")); + let count = CudaContext::device_count().map_err(cuda)?; + let mut devices = Vec::new(); + for ordinal in 0..count { + let physical = result::device::get(ordinal).map_err(cuda)?; + let attribute = |attribute| { + // SAFETY: `physical` came from `device::get` just above. + let value = unsafe { result::device::get_attribute(physical, attribute) }.map_err(cuda)?; + u32::try_from(value).map_err(|_| Error::codec(anyhow::anyhow!("CUDA device {ordinal}: negative PCI field"))) + }; + use cudarc::driver::sys::CUdevice_attribute as A; + let address = crate::device::Pci { + domain: attribute(A::CU_DEVICE_ATTRIBUTE_PCI_DOMAIN_ID)?, + bus: attribute(A::CU_DEVICE_ATTRIBUTE_PCI_BUS_ID)?, + device: attribute(A::CU_DEVICE_ATTRIBUTE_PCI_DEVICE_ID)?, + }; + devices.push((ordinal as usize, address)); + } + pci.find(devices) + .map(Some) + .ok_or_else(|| Error::Unsupported(format!("no CUDA device is {device}"))) +} + /// An owned device allocation. Plain `cuMemAlloc` on purpose: NVENC's /// resource registration rejects stream-ordered pool memory /// (`cuMemAllocAsync`), which is what cudarc's `CudaSlice` uses on any GPU diff --git a/rs/moq-video/src/lib.rs b/rs/moq-video/src/lib.rs index 195dce50964..03ea2b334d4 100644 --- a/rs/moq-video/src/lib.rs +++ b/rs/moq-video/src/lib.rs @@ -82,6 +82,7 @@ pub mod encode; pub mod render; pub mod resize; +mod device; mod error; pub mod frame; mod frame_rate; @@ -100,6 +101,7 @@ mod mf; mod v4l2; pub use color::Color; +pub use device::Device; pub use error::Error; #[cfg(target_os = "linux")] pub use frame::{DmaBuf, DmaBufExport, DmaBufPlane, DrmFormat}; diff --git a/rs/moq-video/src/resize.rs b/rs/moq-video/src/resize.rs index 54387da2093..43d2f4ea072 100644 --- a/rs/moq-video/src/resize.rs +++ b/rs/moq-video/src/resize.rs @@ -1,6 +1,6 @@ //! Frame resizing options. -use crate::Output; +use crate::{Device, Output}; /// Options for [`Frame::resize`](crate::Frame::resize). /// @@ -16,10 +16,9 @@ pub struct Config { /// downloads first and always scales on the CPU. [`Output::Render`] is /// `Native` for a surface `render::Renderer` imports and `Cpu` otherwise. pub output: Output, - /// The VA-API render node that scales a DMA-BUF, such as `/dev/dri/renderD129`. - /// - /// `None` uses the node the VA-API codecs share. Name the encoder's node so - /// the scaled buffer imports into it without a copy. - #[cfg(all(target_os = "linux", feature = "vaapi"))] - pub vaapi: Option, + /// The render node that scales a DMA-BUF. [`Device::Auto`] (the default) + /// uses the node the VA-API codecs share; name the encoder's node so the + /// scaled buffer imports into it without a copy. Every other surface scales + /// on the device that holds it, so this names nothing for them. + pub device: Device, } From 217aa5774e3aaae356fd608c87b38abb83fbc587 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 16 Nov 2026 13:55:14 -0800 Subject: [PATCH 3/3] fix(video): a failed NVIDIA driver check moves on to the next backend An unreadable or missing render node counted against every backend: the driver check's error left select through `?` before VA-API or software was tried. It now counts against the NVIDIA candidate alone, as a failed open would, and its reason still reaches NoEncoder/NoDecoder. Also points the VA-API module docs at Config::device. Co-Authored-By: Claude Opus 5.5 --- rs/moq-video/src/decode/backend/mod.rs | 52 ++++++++++++++++++++++-- rs/moq-video/src/decode/backend/vaapi.rs | 2 +- rs/moq-video/src/encode/backend/mod.rs | 51 +++++++++++++++++++++-- rs/moq-video/src/encode/backend/vaapi.rs | 2 +- 4 files changed, 99 insertions(+), 8 deletions(-) diff --git a/rs/moq-video/src/decode/backend/mod.rs b/rs/moq-video/src/decode/backend/mod.rs index 11835bbb979..850aa2472c6 100644 --- a/rs/moq-video/src/decode/backend/mod.rs +++ b/rs/moq-video/src/decode/backend/mod.rs @@ -414,9 +414,23 @@ fn select(stream: &Stream, attempts: Vec, config: &Config) -> Result {} + Ok(false) => { + passed.push(format!("{name}: does not open {}", config.device)); + continue; + } + // Telling whether this backend drives the named node failed, which + // counts against this candidate alone, as a failed open would. + Err(e) => { + tracing::debug!(decoder = name, error = %e, "decoder unavailable, trying next"); + faulted = true; + tried.push(format!("{name}: {e}")); + if attempt.hardware { + skipped.push(format!("{name}: {e}")); + } + continue; + } } match (attempt.candidate.open)(stream, config) { @@ -582,6 +596,38 @@ mod tests { assert!(!logs_contain("no hardware decoder available")); } + /// A driver check that cannot read the named node counts against the NVIDIA + /// candidate alone: selection goes on to the next, and with none left the + /// failure is a fault that names the node, not a refusal of the stream. + #[cfg(target_os = "linux")] + #[test] + fn a_failed_driver_check_moves_on() { + const NVIDIA: Candidate = Candidate { + name: "nvidia", + supports: |c| matches!(c, Codec::H264), + open: |_, _| panic!("opened NVIDIA for an unreadable node"), + drives: Drives::Nvidia, + }; + const RENDER: Candidate = Candidate { + drives: Drives::Render, + ..WORKING + }; + + let mut config = Config::new(); + config.device = crate::Device::Render("/dev/dri/renderD199-absent".into()); + let attempts = vec![Attempt::hardware(&NVIDIA), Attempt::hardware(&RENDER)]; + assert_eq!(select(&Codec::H264.into(), attempts, &config).unwrap().name(), "stub"); + + match select(&Codec::H264.into(), vec![Attempt::hardware(&NVIDIA)], &config) { + Err(Error::NoDecoder(reason)) => assert!( + reason.contains("nvidia: ") && reason.contains("renderD199-absent"), + "{reason}" + ), + Err(other) => panic!("expected NoDecoder, got {other:?}"), + Ok(backend) => panic!("expected NoDecoder, opened {}", backend.name()), + } + } + /// A named node passes over the backends that drive another kind of device /// without opening them, and opens the one that drives it. #[test] diff --git a/rs/moq-video/src/decode/backend/vaapi.rs b/rs/moq-video/src/decode/backend/vaapi.rs index b7adc0f2abe..5daf271bbf9 100644 --- a/rs/moq-video/src/decode/backend/vaapi.rs +++ b/rs/moq-video/src/decode/backend/vaapi.rs @@ -16,7 +16,7 @@ //! that is a refusal of the stream rather than a fault. For H.265, which has no //! software decoder behind this one, that refusal is what lets a player pick //! another rendition. The render node is the one the encoder and the GPU resize -//! share, unless that one cannot decode H.265, and `Config::vaapi` can name +//! share, unless that one cannot decode H.265, and `Config::device` can name //! it; see `frame::vaapi::device`. //! //! Progressive 8-bit 4:2:0 only, which is everything a browser's `VideoEncoder`, diff --git a/rs/moq-video/src/encode/backend/mod.rs b/rs/moq-video/src/encode/backend/mod.rs index 902c3f0c96f..33f608a1008 100644 --- a/rs/moq-video/src/encode/backend/mod.rs +++ b/rs/moq-video/src/encode/backend/mod.rs @@ -452,9 +452,22 @@ fn select(attempts: Vec, config: &Config) -> Result, E tried.push(format!("{name}: cannot import external Vulkan {device}")); continue; } - if !config.device.takes(attempt.candidate.drives)? { - tried.push(format!("{name}: does not open {}", config.device)); - continue; + match config.device.takes(attempt.candidate.drives) { + Ok(true) => {} + Ok(false) => { + tried.push(format!("{name}: does not open {}", config.device)); + continue; + } + // Telling whether this backend drives the named node failed, which + // counts against this candidate alone, as a failed open would. + Err(e) => { + tracing::debug!(encoder = name, error = %e, "encoder unavailable, trying next"); + tried.push(format!("{name}: {e}")); + if attempt.hardware { + refused.push(format!("{name}: {e}")); + } + continue; + } } match (attempt.candidate.open)(config) { Ok(backend) => { @@ -735,6 +748,38 @@ mod tests { } } + /// A driver check that cannot read the named node counts against the NVIDIA + /// candidate alone: selection goes on, and the reason reaches the error. + #[cfg(target_os = "linux")] + #[test] + fn a_failed_driver_check_moves_on() { + const NVIDIA: Candidate = Candidate { + name: "nvidia", + open: |_| panic!("opened NVIDIA for an unreadable node"), + drives: Drives::Nvidia, + ..WORKING + }; + const RENDER: Candidate = Candidate { + name: "render", + drives: Drives::Render, + ..WORKING + }; + + let mut config = config(); + config.device = crate::Device::Render("/dev/dri/renderD199-absent".into()); + let opened = select(vec![Attempt::hardware(&NVIDIA), Attempt::hardware(&RENDER)], &config).unwrap(); + assert_eq!(opened.name(), "stub"); + + match select(vec![Attempt::hardware(&NVIDIA)], &config) { + Err(Error::NoEncoder(reason)) => assert!( + reason.contains("nvidia: ") && reason.contains("renderD199-absent"), + "{reason}" + ), + Err(other) => panic!("expected NoEncoder, got {other:?}"), + Ok(backend) => panic!("expected NoEncoder, opened {}", backend.name()), + } + } + #[tracing_test::traced_test] #[test] fn falling_past_hardware_warns() { diff --git a/rs/moq-video/src/encode/backend/vaapi.rs b/rs/moq-video/src/encode/backend/vaapi.rs index 7e34e4a7c0f..f8f407a3ed3 100644 --- a/rs/moq-video/src/encode/backend/vaapi.rs +++ b/rs/moq-video/src/encode/backend/vaapi.rs @@ -11,7 +11,7 @@ //! driver), makes `Encoder::new` return an error; under automatic selection //! [`backend::open`](super::open) then moves on to openh264, like the NVENC backend. //! The render node is the one the decoder and the GPU resize share, unless that -//! one cannot encode H.265, and `Config::vaapi` can name it; see +//! one cannot encode H.265, and `Config::device` can name it; see //! `frame::vaapi::device`. //! //! A [`Surface::DmaBuf`] is encoded without touching the CPU. An NV12 buffer at