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