From 475122c2a6d089fa022602a877be2576eacf5d57 Mon Sep 17 00:00:00 2001 From: selikhov Date: Sun, 4 Oct 2026 03:43:06 +0300 Subject: [PATCH] Capture: ask the compositor for the stream's frame rate, up to its own The ScreenCast portal was offered a frame rate range up to the stream's cap but no `maxFramerate`, so the compositor's own maximum (its screen's refresh rate) applied whatever the cap was: a 30 fps stream of a 144 Hz screen got 144 frames a second, of which the sink dropped most. It is now offered `maxFramerate` up to the cap, preferably the cap: PipeWire keeps the consumer's default where the producer's range allows it, so the compositor sends at the lower of the cap and what it can, up to 320 fps or more on a screen that fast. Renegotiated with the cap as before. Windows Graphics Capture is reported to deliver about 60 frames a second without a minimum update interval (or with one under 1 ms); it now gets 1 ms where Windows has the setting, so the sink's cap decides there too. Type-checked only; it has not run on Windows. Co-Authored-By: Claude Opus 5.5 --- crates/voelin-media/src/capture/portal.rs | 58 ++++++++++++++++------ crates/voelin-media/src/capture/windows.rs | 15 ++++-- docs/media.md | 14 +++++- 3 files changed, 67 insertions(+), 20 deletions(-) diff --git a/crates/voelin-media/src/capture/portal.rs b/crates/voelin-media/src/capture/portal.rs index 631a5b7..e171b47 100644 --- a/crates/voelin-media/src/capture/portal.rs +++ b/crates/voelin-media/src/capture/portal.rs @@ -388,17 +388,26 @@ fn size_range() -> Property { ) } -/// Up to `fps`, as the compositor can (it sends frames on damage only). -fn framerate_range(fps: u32) -> Property { - property!( - FormatProperties::VideoFramerate, - Choice, - Range, - Fraction, - Fraction { num: fps, denom: 1 }, - Fraction { num: 0, denom: 1 }, - Fraction { num: fps, denom: 1 } - ) +/// Up to `fps`, as the compositor can (it sends frames on damage only): +/// the frame rate (compositors offer 0, variable) and the most frames it +/// may send, preferably `fps`. Compositors offer their screen's refresh rate +/// as the maximum (Mutter, KWin, xdg-desktop-portal-hyprland, whose +/// `screencopy:max_fps` defaults to 120), so the intersection is the +/// lower of the two: the compositor neither sends frames the sink would +/// drop nor fewer than its screen shows. +fn framerate_range(fps: u32) -> [Property; 2] { + let up_to = |key: FormatProperties, min: u32| { + property!( + key, + Choice, + Range, + Fraction, + Fraction { num: fps, denom: 1 }, + Fraction { num: min, denom: 1 }, + Fraction { num: fps, denom: 1 } + ) + }; + [up_to(FormatProperties::VideoFramerate, 0), up_to(FormatProperties::VideoMaxFramerate, 1)] } /// The modifiers we take, best first: the sink's tiled ones, then LINEAR, @@ -471,7 +480,7 @@ fn enum_formats( properties.push(property!(FormatProperties::VideoFormat, Id, format)); properties.push(modifier_property(&modifiers, false)); properties.push(size_range()); - properties.push(framerate_range(fps)); + properties.extend(framerate_range(fps)); formats.push(serialize(format_object(properties))?); } } @@ -488,7 +497,7 @@ fn enum_formats( FORMATS[3] )); properties.push(size_range()); - properties.push(framerate_range(fps)); + properties.extend(framerate_range(fps)); formats.push(serialize(format_object(properties))?); Ok(formats) } @@ -543,7 +552,7 @@ fn format_changed( Rectangle { width: size.width, height: size.height } ), ]; - properties.push(framerate_range(state.fps)); + properties.extend(framerate_range(state.fps)); let fixed = serialize(format_object(properties))?; stream.update_params(&mut [pod(&fixed)?]).map_err(|e| e.to_string())?; return Ok(()); @@ -808,6 +817,27 @@ mod tests { assert!(modifier.is_none(), "shared memory has no modifier"); } } + // The frame rate and the most frames a second: 320 preferred, as + // much as the compositor can. + for bytes in enum_formats(320, true, &[]).unwrap() { + let object = pod(&bytes).unwrap().as_object().unwrap(); + for (key, min) in + [(FormatProperties::VideoFramerate, 0), (FormatProperties::VideoMaxFramerate, 1)] + { + use pw::spa::pod::deserialize::PodDeserializer; + let prop = object.find_prop(Id(key.as_raw())).expect("a frame rate"); + let value = + PodDeserializer::deserialize_any_from(prop.value().as_bytes()).unwrap().1; + let Value::Choice(ChoiceValue::Fraction(Choice( + _, + ChoiceEnum::Range { default, min: low, max }, + ))) = value + else { + panic!("{value:?}"); + }; + assert_eq!((default.num, low.num, max.num), (320, min, 320)); + } + } let linear_only = enum_formats(60, true, &[]).unwrap(); let object = pod(&linear_only[0]).unwrap().as_object().unwrap(); let prop = object.find_prop(Id(FormatProperties::VideoModifier.as_raw())).unwrap(); diff --git a/crates/voelin-media/src/capture/windows.rs b/crates/voelin-media/src/capture/windows.rs index 2ce053a..f6067c1 100644 --- a/crates/voelin-media/src/capture/windows.rs +++ b/crates/voelin-media/src/capture/windows.rs @@ -12,12 +12,12 @@ //! Type-checked on Linux (`--target x86_64-pc-windows-gnu`), not run yet. use std::sync::atomic::{AtomicBool, Ordering}; -use std::time::Instant; +use std::time::{Duration, Instant}; use tracing::{debug, warn}; use windows_capture::capture::{CaptureControl, Context, GraphicsCaptureApiHandler}; use windows_capture::frame::Frame; -use windows_capture::graphics_capture_api::InternalCaptureControl; +use windows_capture::graphics_capture_api::{GraphicsCaptureApi, InternalCaptureControl}; use windows_capture::monitor::Monitor; use windows_capture::settings::{ ColorFormat, CursorCaptureSettings, DirtyRegionSettings, DrawBorderSettings, @@ -105,13 +105,20 @@ where } else { CursorCaptureSettings::WithoutCursor }; + // Left alone, Windows delivers about 60 frames a second (reported for + // a default and for anything under 1 ms: robmikh/Win32CaptureSample#82). + // 1 ms lets the sink's frame-rate cap decide, which can change while + // capturing. Windows 10 has no such setting. + let interval = match GraphicsCaptureApi::is_minimum_update_interval_supported() { + Ok(true) => MinimumUpdateIntervalSettings::Custom(Duration::from_millis(1)), + _ => MinimumUpdateIntervalSettings::Default, + }; let settings = Settings::new( item, cursor, DrawBorderSettings::WithoutBorder, SecondaryWindowSettings::Default, - // No minimum: the frame rate can change while capturing. - MinimumUpdateIntervalSettings::Default, + interval, DirtyRegionSettings::Default, ColorFormat::Bgra8, (sink, Instant::now()), diff --git a/docs/media.md b/docs/media.md index 578d456..0749b59 100644 --- a/docs/media.md +++ b/docs/media.md @@ -733,8 +733,13 @@ Notes: and end; if that is slower than 1 ns per pixel over the first 30 frames (uncached VRAM), the stream renegotiates to shared memory. `VOELIN_PORTAL_DMABUF=0` or `with_dmabuf(false)` skips DMA-BUFs. The frame rate - asked of the compositor is the sink's cap, renegotiated when it changes; - compositors send frames only when the screen changes. Without a session + asked of the compositor is the sink's cap, renegotiated when it changes: + `framerate` up to it and `maxFramerate` preferably it. Compositors offer + their screen's refresh rate as the most (Mutter, KWin, + xdg-desktop-portal-hyprland, which also caps at its `screencopy:max_fps`, + 120 unless configured), so a capture runs at the lower of the cap and + what the compositor can, and the compositor sends no frame the sink + would drop. Compositors send frames only when the screen changes. Without a session bus or portal, `start` returns `Error::CaptureUnavailable`; a cancelled dialog returns `Error::Cancelled`. - wlroots: `WlrootsCapture::new()` (`$WAYLAND_DISPLAY`) or `with_display`; @@ -752,6 +757,11 @@ Notes: stream of ours ([Stream audio](#stream-audio)). Without a PipeWire daemon, both return `Error::CaptureUnavailable`. - Windows process loopback needs Windows 10 2004 or later. +- Windows Graphics Capture is given a minimum update interval of 1 ms where + Windows has the setting (Windows 11): left alone, or below 1 ms, it is + reported to deliver about 60 frames a second (robmikh/Win32CaptureSample + issue 82), and the sink's frame-rate cap decides instead. Not run on + Windows yet. ## Performance and benchmarks