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 <noreply@anthropic.com>
This commit is contained in:
selikhovandClaude Opus 5.5 committed 2026-10-04 03:43:06 +03:00
1 parent 8917f7dab2
commit 475122c2a6
3 files changed
+67 -20

No files matched your search

+44 -14
View File
@@ -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();
+11 -4
View File
@@ -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()),