From 0da3691a1b4ae755ad297c4bf2e9909609201a13 Mon Sep 17 00:00:00 2001 From: lashman Date: Mon, 21 Sep 2026 06:59:30 +0300 Subject: [PATCH] let the capture rate be capped on purpose, off by default --- src-tauri/src/capture.rs | 64 +++++++++++++++++++++++++++++++++++----- src-tauri/src/config.rs | 8 +++++ src-tauri/src/lib.rs | 11 +++++-- src/lib/Settings.svelte | 15 ++++++++-- src/lib/api.ts | 1 + 5 files changed, 87 insertions(+), 12 deletions(-) diff --git a/src-tauri/src/capture.rs b/src-tauri/src/capture.rs index 2052333..dbfe627 100644 --- a/src-tauri/src/capture.rs +++ b/src-tauri/src/capture.rs @@ -794,6 +794,27 @@ fn is_hdr_output(device_name: &str) -> Option { None } +/// Resolve what the raw pipe declares, and whether WGC has to be throttled to +/// get there. Returns `(fps, throttled)`. +/// +/// With no limit the pipe declares the panel's refresh rate, which is what WGC +/// delivers on its own - and the declared rate has to match what actually +/// arrives, or `fix_intermediate_fps` rescales every timestamp by the difference +/// and changes the clip's speed rather than its smoothness. +/// +/// A limit above the refresh rate needs no throttle, and one below it caps at +/// the panel either way. The `max(1)` keeps `clamp` from panicking if a panel +/// ever reports 0 Hz, which `monitor_refresh_rate` already guards against. +fn pipe_rate(refresh_hz: u32, limit: Option) -> (u32, bool) { + match limit { + Some(requested) => { + let fps = requested.clamp(1, refresh_hz.max(1)); + (fps, fps < refresh_hz) + } + None => (refresh_hz, false), + } +} + /// Find the monitor containing the given screen point, falling back to the /// primary monitor on failure. fn monitor_at_point(x: i32, y: i32) -> Result { @@ -823,6 +844,7 @@ pub(crate) fn start( ffmpeg_path: PathBuf, capture_cursor: bool, crop: Option, + rate_limit: Option, quality: CaptureQuality, ) -> Result { // Use the center of the capture region to determine which monitor to capture. @@ -852,14 +874,18 @@ pub(crate) fn start( let frame_counter = Arc::new(std::sync::atomic::AtomicU64::new(0)); - // The pipe declares what WGC actually delivers, which is the panel's refresh - // rate - not the frame rate in the settings. Those timestamps land close to - // real time already, so `fix_intermediate_fps` only has drift to correct. - // Throttling capture to the setting would throw away frames a later export - // cannot get back; the setting is a target for the export, the thumbnails and - // the GIF estimate instead. + // Off by default, so the pipe declares the panel's refresh rate and WGC + // delivers it untouched. Only an explicit cap throttles, and then the + // declared rate follows the throttle down. let refresh_hz = monitor_refresh_rate(monitor); - let pipe_fps = refresh_hz; + let (pipe_fps, throttled) = pipe_rate(refresh_hz, rate_limit); + let update_interval = if throttled { + MinimumUpdateIntervalSettings::Custom(std::time::Duration::from_secs_f64( + 1.0 / f64::from(pipe_fps), + )) + } else { + MinimumUpdateIntervalSettings::Default + }; // A monitor showing HDR needs the tone curve; an SDR one must not go through // it, or white comes out at 0.65. Unknown falls back to the HDR path so a @@ -893,7 +919,7 @@ pub(crate) fn start( cursor_setting, DrawBorderSettings::Default, SecondaryWindowSettings::Default, - MinimumUpdateIntervalSettings::Default, + update_interval, DirtyRegionSettings::Default, ColorFormat::Rgba16F, setup, @@ -1113,4 +1139,26 @@ mod tests { assert_close(actual_fps, 60.0); assert_close(pts_scale, 2.0); } + + /// No cap is the default and must leave the rate exactly where WGC puts it. + #[test] + fn pipe_rate_follows_the_panel_without_a_cap() { + assert_eq!(pipe_rate(120, None), (120, false)); + assert_eq!(pipe_rate(60, None), (60, false)); + } + + /// An explicit cap below the refresh rate throttles, and the declared rate + /// follows it down so the two cannot disagree. + #[test] + fn pipe_rate_throttles_to_an_explicit_cap() { + assert_eq!(pipe_rate(120, Some(30)), (30, true)); + assert_eq!(pipe_rate(120, Some(60)), (60, true)); + } + + /// A cap at or above the panel has nothing to hold back. + #[test] + fn pipe_rate_needs_no_throttle_at_or_above_the_panel() { + assert_eq!(pipe_rate(120, Some(120)), (120, false)); + assert_eq!(pipe_rate(60, Some(240)), (60, false)); + } } diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index 95ba508..1cfa746 100644 --- a/src-tauri/src/config.rs +++ b/src-tauri/src/config.rs @@ -61,6 +61,11 @@ pub struct CaptureConfig { pub capture_cursor: bool, pub capture_audio: bool, pub capture_quality: CaptureQuality, + /// Opt in to recording at `default_fps` instead of the panel's refresh rate. + /// Off by default, and a config written before this field existed gets false + /// through the container `serde(default)` - so nobody's capture changes until + /// they turn it on. + pub limit_capture_fps: bool, } #[derive(Debug, Clone, Default, Serialize, Deserialize)] @@ -151,6 +156,7 @@ impl Default for CaptureConfig { capture_cursor: true, capture_audio: false, capture_quality: CaptureQuality::Auto, + limit_capture_fps: false, } } } @@ -278,6 +284,7 @@ mod tests { capture_cursor: false, capture_audio: true, capture_quality: CaptureQuality::Full, + limit_capture_fps: true, }, hotkeys: HotkeyConfig { start: Some("F9".to_string()), @@ -299,6 +306,7 @@ mod tests { let loaded = Config::load_or_create(&path).unwrap(); assert_eq!(loaded.export.default_format, Format::Webm); assert_eq!(loaded.capture.default_fps, 60); + assert!(loaded.capture.limit_capture_fps); assert_eq!(loaded.hotkeys.start, Some("F9".to_string())); assert_eq!(loaded.output_dir, Some(PathBuf::from("./custom"))); assert_eq!(loaded.theme, Theme::Light); diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 33402a8..d6d93f3 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -582,6 +582,7 @@ fn start_capture( ffmpeg, cfg.capture.capture_cursor, crop, + cfg.capture.limit_capture_fps.then_some(cfg.capture.default_fps), cfg.capture.capture_quality, ) .map_err(|e| format!("{e:#}"))?; @@ -684,13 +685,19 @@ fn resume_capture(app: AppHandle, state: State<'_, AppState>) -> Result<(), Stri (ss.capture_cursor, ss.audio, ss.crop, ss.segments.len() + 1) }; - let quality = lock(&state.config).capture.capture_quality; + let (rate_limit, quality) = { + let c = lock(&state.config); + ( + c.capture.limit_capture_fps.then_some(c.capture.default_fps), + c.capture.capture_quality, + ) + }; let output = segment_path(&state.app_dir, next_idx); let ffmpeg = bootstrap::ffmpeg_path(&state.app_dir); if !ffmpeg.is_file() { return Err("ffmpeg.exe not found".to_string()); } - let session = capture::start(output, ffmpeg, capture_cursor, crop, quality) + let session = capture::start(output, ffmpeg, capture_cursor, crop, rate_limit, quality) .map_err(|e| format!("{e:#}"))?; let audio_path = audio_segment_path(&state.app_dir, next_idx); diff --git a/src/lib/Settings.svelte b/src/lib/Settings.svelte index 45c2912..1b305cc 100644 --- a/src/lib/Settings.svelte +++ b/src/lib/Settings.svelte @@ -21,6 +21,7 @@ || cfg.capture.capture_cursor !== initialCfg.capture.capture_cursor || cfg.capture.capture_audio !== initialCfg.capture.capture_audio || cfg.capture.capture_quality !== initialCfg.capture.capture_quality + || cfg.capture.limit_capture_fps !== initialCfg.capture.limit_capture_fps || cfg.export.default_format !== initialCfg.export.default_format || cfg.export.gif_dither !== initialCfg.export.gif_dither || cfg.export.webm_codec !== initialCfg.export.webm_codec @@ -148,6 +149,16 @@ {#each [15,24,30,60] as f}{/each} + + {#if cfg.capture.limit_capture_fps} +

+ Recording stops at {cfg.capture.default_fps} fps instead of the display's refresh + rate. Smaller temp files and less CPU, but nothing above that rate is captured. +

+ {/if} {#if codecs && (!codecs.av1 || !codecs.h265)} -

Some codecs are unavailable in your ffmpeg build and disabled above.

+

Some codecs are unavailable in your ffmpeg build and disabled above.

{/if} {/if} @@ -515,7 +526,7 @@ .advanced-toggle svg { transition: transform 150ms; } .advanced-toggle svg.rotated { transform: rotate(180deg); } - .codec-note { + .field-note { margin: 0; font-size: 11px; color: var(--warn); diff --git a/src/lib/api.ts b/src/lib/api.ts index b17161f..1207d0a 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -30,6 +30,7 @@ export type CaptureConfig = { capture_cursor: boolean capture_audio: boolean capture_quality: CaptureQuality + limit_capture_fps: boolean } export type HotkeyConfig = {