voelin-media: stop probing absent GPUs and keep the encoder libraries quiet

The startup probe opened every encoder in the table, which took 1.77 s here
and froze the app's first use for that long, and it let the libraries behind
them print about thirty lines to standard error on every run.

Most of the time went on hardware that is not in the machine: a self-test of
a family whose vendor is absent still runs its driver's initialisation
before failing, 1.7 s for av1_nvenc and 1.2 s for vp9_qsv here. Read the PCI
vendors of the DRM render nodes once and skip those families outright; when
the vendors cannot be read nothing is skipped, so an unusual setup only pays
what it paid before. The report gains a plain reason ("no NVIDIA GPU in this
machine") in place of a CUDA error code.

FFmpeg's own log already goes to tracing, but SVT-AV1's build banner, AMF's
GetProperty warnings and Mesa's RADV_PERFTEST notice are written to the
descriptor by the libraries themselves. Point standard error at a temporary
file while the self-tests run and log what landed there as one debug record;
VOELIN_FFMPEG_PROBE_STDERR=1 leaves it alone.

Measured with `voelinctl stream encoders` (release): 1.77 s before, 0.93-1.12
s after, 12 backends tested instead of 20, and nothing on standard error.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Opus 5 committed 2026-09-29 18:35:39 +03:00
1 parent b541ec8093
commit e599eab0ac
2 files changed
+173 -17

No files matched your search

+2 -2
View File
@@ -20,7 +20,7 @@ openh264-download = ["openh264", "dep:reqwest", "dep:bzip2", "dep:sha2"]
# Video encoders through FFmpeg loaded at runtime (never linked or shipped):
# VA-API, NVENC, Quick Sync, AMF, Media Foundation, VideoToolbox, x264,
# SVT-AV1, ... whatever the installed FFmpeg has (see docs/media.md).
ffmpeg = ["dep:libloading"]
ffmpeg = ["dep:libloading", "dep:rustix"]
# AV1 decoding through the system libdav1d (pkg-config `dav1d` >= 1.3).
av1 = ["dep:dav1d"]
# X11 screen capture (Linux).
@@ -54,7 +54,7 @@ yuv = "0.8.19"
ashpd = { version = "0.13.13", default-features = false, features = ["screencast", "tokio"], optional = true }
memmap2 = { version = "0.9.11", optional = true }
pipewire = { version = "0.10.1", optional = true }
rustix = { version = "1.1.5", features = ["event", "fs"], optional = true }
rustix = { version = "1.1.5", features = ["event", "fs", "stdio"], optional = true }
wayland-client = { version = "0.31.15", optional = true }
wayland-protocols = { version = "0.32.13", features = ["client", "staging"], optional = true }
wayland-protocols-wlr = { version = "0.3.12", features = ["client"], optional = true }
+171 -15
View File
@@ -1316,6 +1316,9 @@ pub struct BackendStatus {
pub spec: &'static BackendSpec,
/// `Ok` if it encoded the test frame, else why not.
pub available: std::result::Result<(), String>,
/// How long its self-test took (the probe runs them in parallel, so the
/// startup cost is the slowest one).
pub took: Duration,
}
/// Size of the self-test frame.
@@ -1341,42 +1344,195 @@ fn in_build(ffmpeg: &Ffmpeg, name: &str) -> bool {
!unsafe { (ffmpeg.api.avcodec_find_encoder_by_name)(name.as_ptr()) }.is_null()
}
/// PCI vendor of a GPU, as `/sys/class/drm/*/device/vendor` gives it.
#[cfg(target_os = "linux")]
mod vendor {
pub const AMD: u32 = 0x1002;
pub const NVIDIA: u32 = 0x10de;
pub const INTEL: u32 = 0x8086;
}
/// The PCI vendors of this machine's DRM render nodes, read once; empty if
/// `/sys/class/drm` says nothing (a container without it, say), which is
/// taken as "unknown" and skips nothing.
#[cfg(target_os = "linux")]
fn drm_vendors() -> &'static [u32] {
static VENDORS: OnceLock<Vec<u32>> = OnceLock::new();
VENDORS.get_or_init(|| {
let Ok(entries) = std::fs::read_dir("/sys/class/drm") else { return Vec::new() };
let mut found: Vec<u32> = entries
.flatten()
.filter(|e| e.file_name().to_string_lossy().starts_with("renderD"))
.filter_map(|e| std::fs::read_to_string(e.path().join("device/vendor")).ok())
.filter_map(|text| u32::from_str_radix(text.trim().trim_start_matches("0x"), 16).ok())
.collect();
found.sort_unstable();
found.dedup();
found
})
}
/// Sends the process's standard error to a temporary file while it lives,
/// and logs whatever landed there when it is dropped.
///
/// FFmpeg's own messages already go to `tracing` (see
/// [`Ffmpeg::capture_log`](super::Ffmpeg::capture_log)), but the libraries
/// behind the encoders write to the descriptor themselves and cannot be
/// asked not to: SVT-AV1 prints a build banner and its allocation totals,
/// AMD's AMF runtime prints `GetProperty(...) not found` warnings, and Mesa
/// prints a `RADV_PERFTEST` deprecation notice when AMF brings up Vulkan.
/// Opening every encoder at startup meant about thirty such lines on every
/// run of the app.
///
/// This moves the descriptor, so anything else the process writes to
/// standard error meanwhile lands in the file too and comes back as one
/// `debug` record. `VOELIN_FFMPEG_PROBE_STDERR=1` leaves it alone.
struct QuietStderr {
#[cfg(unix)]
saved: std::os::fd::OwnedFd,
#[cfg(unix)]
file: std::path::PathBuf,
}
impl QuietStderr {
#[cfg(unix)]
fn start() -> Option<Self> {
use std::os::fd::AsFd;
if std::env::var_os("VOELIN_FFMPEG_PROBE_STDERR").is_some() {
return None;
}
let file =
std::env::temp_dir().join(format!("voelin-encoder-probe-{}.log", std::process::id()));
let sink = std::fs::File::create(&file).ok()?;
let stderr = std::io::stderr();
let saved = rustix::io::dup(stderr.as_fd()).ok()?;
// Anything already buffered belongs on the real descriptor.
let _ = std::io::Write::flush(&mut std::io::stderr());
rustix::stdio::dup2_stderr(sink.as_fd()).ok()?;
Some(Self { saved, file })
}
#[cfg(not(unix))]
fn start() -> Option<Self> {
None
}
}
#[cfg(unix)]
impl Drop for QuietStderr {
fn drop(&mut self) {
use std::os::fd::AsFd;
let _ = std::io::Write::flush(&mut std::io::stderr());
let _ = rustix::stdio::dup2_stderr(self.saved.as_fd());
if let Ok(text) = std::fs::read_to_string(&self.file) {
let text = text.trim();
if !text.is_empty() {
tracing::debug!(target: "ffmpeg", "the encoder libraries wrote:\n{text}");
}
}
let _ = std::fs::remove_file(&self.file);
}
}
/// Whether the hardware `spec` needs can be in this machine at all.
///
/// A self-test of a family whose vendor is absent still costs 0.4-1.7 s of
/// driver initialisation before it fails (measured here: `av1_nvenc` 1.7 s,
/// `vp9_qsv` 1.2 s), and the probe's wall time is its slowest test. Nothing
/// is skipped when the vendors cannot be read, so an unusual setup only
/// pays the time it used to.
fn vendor_may_be_present(spec: &BackendSpec) -> std::result::Result<(), String> {
#[cfg(target_os = "linux")]
{
let vendors = drm_vendors();
if vendors.is_empty() {
return Ok(());
}
let (wanted, what) = match spec.family() {
"nvenc" => (vendor::NVIDIA, "no NVIDIA GPU"),
"qsv" => (vendor::INTEL, "no Intel GPU"),
"amf" => (vendor::AMD, "no AMD GPU"),
_ => return Ok(()),
};
// NVIDIA's driver can run without a DRM node (`nvidia-drm.modeset=0`).
if wanted == vendor::NVIDIA && std::path::Path::new("/dev/nvidiactl").exists() {
return Ok(());
}
if !vendors.contains(&wanted) {
return Err(format!("{what} in this machine"));
}
}
let _ = spec;
Ok(())
}
/// Every backend with its self-test result (run once per process, the
/// backends in parallel). Empty without FFmpeg.
///
/// A backend whose vendor is not in the machine is not opened at all
/// ([`vendor_may_be_present`]): its driver would spend up to 1.7 s failing,
/// and the probe costs as much as its slowest test.
pub fn probe() -> &'static [BackendStatus] {
static PROBE: OnceLock<Vec<BackendStatus>> = OnceLock::new();
PROBE.get_or_init(|| {
let Ok(ffmpeg) = Ffmpeg::get() else { return Vec::new() };
let started = Instant::now();
let quiet = QuietStderr::start();
let statuses: Vec<BackendStatus> = std::thread::scope(|scope| {
let tests: Vec<_> = BACKENDS
.iter()
.map(|spec| {
let present = in_build(ffmpeg, spec.name);
let test = present.then(|| scope.spawn(move || test_backend(spec)));
(spec, test)
let absent = vendor_may_be_present(spec).err();
let present = absent.is_none() && in_build(ffmpeg, spec.name);
let test = present.then(|| {
scope.spawn(move || {
let started = Instant::now();
(test_backend(spec), started.elapsed())
})
});
(spec, absent, test)
})
.collect();
tests
.into_iter()
.map(|(spec, test)| BackendStatus {
spec,
available: match test {
None => Err("not in this FFmpeg build".into()),
Some(handle) => {
handle.join().unwrap_or_else(|_| Err("the self-test panicked".into()))
}
},
.map(|(spec, absent, test)| {
let (available, took) = match test {
None => (
Err(absent.unwrap_or_else(|| "not in this FFmpeg build".into())),
Duration::ZERO,
),
Some(handle) => handle.join().unwrap_or_else(|_| {
(Err("the self-test panicked".into()), Duration::ZERO)
}),
};
BackendStatus { spec, available, took }
})
.collect()
});
// Standard error comes back (and what the libraries wrote is logged)
// before our own report.
drop(quiet);
for status in &statuses {
match &status.available {
Ok(()) => tracing::info!(backend = status.spec.name, "FFmpeg encoder available"),
Err(e) => {
tracing::debug!(backend = status.spec.name, "FFmpeg encoder unusable: {e}")
}
Ok(()) => tracing::info!(
backend = status.spec.name,
ms = status.took.as_millis() as u64,
"FFmpeg encoder available"
),
Err(e) => tracing::debug!(
backend = status.spec.name,
ms = status.took.as_millis() as u64,
"FFmpeg encoder unusable: {e}"
),
}
}
tracing::info!(
ms = started.elapsed().as_millis() as u64,
tested = statuses.iter().filter(|s| !s.took.is_zero()).count(),
"FFmpeg encoders probed"
);
statuses
})
}