diff --git a/tools/voelinctl/src/bench.rs b/tools/voelinctl/src/bench.rs index b902a10..d1ebe9d 100644 --- a/tools/voelinctl/src/bench.rs +++ b/tools/voelinctl/src/bench.rs @@ -51,6 +51,16 @@ struct EncodersArgs { #[derive(Args, Debug)] struct BenchArgs { + /// What to capture: `synthetic` (the test pattern), `portal` (the + /// desktop's screen-share dialog, Wayland and X11), `monitor:` or + /// `window:` (X11). Anything but `synthetic` ignores --res and + /// --pattern and measures the real capture → convert → encode pipeline. + #[arg(long, default_value = "synthetic")] + source: String, + /// Give up if the capture has not started after this many seconds (the + /// portal opens a dialog the user has to accept). + #[arg(long, default_value_t = 60)] + start_timeout: u64, /// Size of the test pattern. #[arg(long, default_value = "1920x1080")] res: String, @@ -212,6 +222,19 @@ impl MediaSink for BenchSink { } } +/// `synthetic`, `portal`, `monitor:` or `window:`. +fn parse_source(value: &str) -> Result { + Ok(match value.split_once(':') { + Some(("monitor", n)) => SourceId::Monitor(n.parse().context("monitor index")?), + Some(("window", id)) => SourceId::Window(id.parse().context("window id")?), + _ => match value { + "synthetic" => SourceId::Synthetic, + "portal" => SourceId::Portal, + other => bail!("unknown source {other:?}: synthetic, portal, monitor:, window:"), + }, + }) +} + /// `x`. fn parse_size(size: &str) -> Result<(u32, u32)> { let parsed = size.split_once('x').and_then(|(w, h)| Some((w.parse().ok()?, h.parse().ok()?))); @@ -317,8 +340,9 @@ fn bench(args: BenchArgs) -> Result<()> { .collect::>>()?; let ids: Vec = if layers.is_empty() { vec![0] } else { layers.iter().map(|l| l.id).collect() }; + let source = parse_source(&args.source)?; let config = StreamerConfig { - source: SourceId::Synthetic, + source: source.clone(), synthetic_size: (width, height), synthetic_pattern: pattern, fps: args.fps, @@ -330,23 +354,54 @@ fn bench(args: BenchArgs) -> Result<()> { ..StreamerConfig::default() }; let runtime = tokio::runtime::Runtime::new()?; - let streamer = runtime.block_on(Streamer::start(&codecs, config)).context("streamer")?; + let timeout = Duration::from_secs(args.start_timeout.max(1)); + // The portal opens a dialog on the desktop; without an answer the start + // never returns, so it is given a deadline instead of hanging. + let started = runtime + .block_on(async { tokio::time::timeout(timeout, Streamer::start(&codecs, config)).await }); + let streamer = match started { + Ok(result) => result.context("streamer")?, + Err(_) => bail!( + "the capture did not start within {} s (the {} source was not accepted)", + timeout.as_secs(), + args.source + ), + }; let sink = Arc::new(BenchSink { layers: ids.iter().map(|id| (*id, LayerCount::default())).collect(), keyframe: AtomicBool::new(true), }); streamer.attach(sink.clone()); let backend = streamer.stats().layers.first().and_then(|l| l.backend); + let what = if source == SourceId::Synthetic { + format!("{width}x{height} {} test pattern", args.pattern) + } else { + format!("{} capture", args.source) + }; println!( - "{width}x{height} {} test pattern at {} fps, {codec} ({}), {} layer(s), {} s after {} s \ - warm-up", - args.pattern, + "{what} at {} fps, {codec} ({}), {} layer(s), {} s after {} s warm-up", args.fps, backend.map_or("?".to_owned(), |b| b.to_string()), ids.len(), args.seconds, args.warmup ); + // A real source only delivers once the user has picked something. + if source != SourceId::Synthetic { + let deadline = Instant::now() + timeout; + while streamer.stats().captured_frames == 0 { + if Instant::now() >= deadline { + bail!( + "no frame from the {} source within {} s (nobody accepted the dialog?)", + args.source, + timeout.as_secs() + ); + } + std::thread::sleep(Duration::from_millis(100)); + } + let stats = streamer.stats(); + println!("capturing {}x{}", stats.width, stats.height); + } std::thread::sleep(Duration::from_secs(args.warmup)); let start = sample(&sink, &streamer); std::thread::sleep(Duration::from_secs(args.seconds));