From 7efc7aef43365e9da4c6a8e86e58676b85dd54b9 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Mon, 27 Oct 2025 15:30:22 -0700 Subject: [PATCH 1/7] Improve refresh rate handling in screen capture config Adds validation and fallback logic for display refresh rates when initializing ScreenCaptureConfig. If the reported refresh rate is invalid, the code now falls back to 60Hz and logs a warning, ensuring a reasonable FPS is always selected. --- .../src/sources/screen_capture/mod.rs | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/crates/recording/src/sources/screen_capture/mod.rs b/crates/recording/src/sources/screen_capture/mod.rs index 78c7a2c22cc..04b7803d8b3 100644 --- a/crates/recording/src/sources/screen_capture/mod.rs +++ b/crates/recording/src/sources/screen_capture/mod.rs @@ -295,7 +295,26 @@ impl ScreenCaptureConfig { ) -> Result { cap_fail::fail!("ScreenCaptureSource::init"); - let fps = max_fps.min(display.refresh_rate() as u32); + let reported_refresh_rate = display.refresh_rate(); + let fallback_refresh = 60; + let rounded_refresh = reported_refresh_rate.round(); + let is_invalid_refresh = !rounded_refresh.is_finite() || rounded_refresh <= 0.0; + let capped_refresh = if is_invalid_refresh { + fallback_refresh as f64 + } else { + rounded_refresh.min(500.0) + }; + let target_refresh = if is_invalid_refresh { + warn!( + ?reported_refresh_rate, + fallback = fallback_refresh, + "Display reported invalid refresh rate; falling back to default" + ); + fallback_refresh + } else { + capped_refresh as u32 + }; + let fps = std::cmp::max(1, std::cmp::min(max_fps, target_refresh)); let output_size = crop_bounds .clone() From e8291296c0898db400672ec535302a5d08bd7df8 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Mon, 27 Oct 2025 15:43:22 -0700 Subject: [PATCH 2/7] Coderabbit suggestions --- .../src/sources/screen_capture/mod.rs | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/crates/recording/src/sources/screen_capture/mod.rs b/crates/recording/src/sources/screen_capture/mod.rs index 04b7803d8b3..199f3fa86bd 100644 --- a/crates/recording/src/sources/screen_capture/mod.rs +++ b/crates/recording/src/sources/screen_capture/mod.rs @@ -378,15 +378,43 @@ impl ScreenCaptureConfig { } } +fn validated_refresh_rate(reported_refresh_rate: T) -> u32 +where + T: Into, +{ + let reported_refresh_rate = reported_refresh_rate.into(); + let fallback_refresh = 60; + let rounded_refresh = reported_refresh_rate.round(); + let is_invalid_refresh = !rounded_refresh.is_finite() || rounded_refresh <= 0.0; + let capped_refresh = if is_invalid_refresh { + fallback_refresh as f64 + } else { + rounded_refresh.min(500.0) + }; + + if is_invalid_refresh { + warn!( + ?reported_refresh_rate, + fallback = fallback_refresh, + "Display reported invalid refresh rate; falling back to default" + ); + fallback_refresh + } else { + capped_refresh as u32 + } +} + pub fn list_displays() -> Vec<(CaptureDisplay, Display)> { scap_targets::Display::list() .into_iter() .filter_map(|display| { + let refresh_rate = validated_refresh_rate(display.raw_handle().refresh_rate()); + Some(( CaptureDisplay { id: display.id(), name: display.name()?, - refresh_rate: display.raw_handle().refresh_rate() as u32, + refresh_rate, }, display, )) @@ -428,13 +456,17 @@ pub fn list_windows() -> Vec<(CaptureWindow, Window)> { #[cfg(not(target_os = "macos"))] let bundle_identifier = None; + let refresh_rate = v + .display() + .map(|display| validated_refresh_rate(display.raw_handle().refresh_rate()))?; + Some(( CaptureWindow { id: v.id(), name, owner_name, bounds: v.display_relative_logical_bounds()?, - refresh_rate: v.display()?.raw_handle().refresh_rate() as u32, + refresh_rate, bundle_identifier, }, v, From 3a921d0c074406795f34c7ad88b6402ebc31244b Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Mon, 27 Oct 2025 15:51:50 -0700 Subject: [PATCH 3/7] Coderabbit suggestions --- .../src/sources/screen_capture/mod.rs | 20 +------------------ 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/crates/recording/src/sources/screen_capture/mod.rs b/crates/recording/src/sources/screen_capture/mod.rs index 199f3fa86bd..658f2ea41ff 100644 --- a/crates/recording/src/sources/screen_capture/mod.rs +++ b/crates/recording/src/sources/screen_capture/mod.rs @@ -295,25 +295,7 @@ impl ScreenCaptureConfig { ) -> Result { cap_fail::fail!("ScreenCaptureSource::init"); - let reported_refresh_rate = display.refresh_rate(); - let fallback_refresh = 60; - let rounded_refresh = reported_refresh_rate.round(); - let is_invalid_refresh = !rounded_refresh.is_finite() || rounded_refresh <= 0.0; - let capped_refresh = if is_invalid_refresh { - fallback_refresh as f64 - } else { - rounded_refresh.min(500.0) - }; - let target_refresh = if is_invalid_refresh { - warn!( - ?reported_refresh_rate, - fallback = fallback_refresh, - "Display reported invalid refresh rate; falling back to default" - ); - fallback_refresh - } else { - capped_refresh as u32 - }; + let target_refresh = validated_refresh_rate(display.refresh_rate()); let fps = std::cmp::max(1, std::cmp::min(max_fps, target_refresh)); let output_size = crop_bounds From b7e464123018d0f1a3492d6ba207a76b371f003b Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Mon, 27 Oct 2025 16:35:02 -0700 Subject: [PATCH 4/7] Add encoder preferences for Windows pipelines Introduces the EncoderPreferences struct to control hardware/software encoder selection on Windows. Passes encoder preferences through capture and output pipeline creation, enabling fallback to software encoding when needed. Updates relevant pipeline and factory interfaces to support this configuration. --- crates/recording/src/capture_pipeline.rs | 32 ++++++++ crates/recording/src/output_pipeline/win.rs | 87 ++++++++++++--------- crates/recording/src/studio_recording.rs | 9 +++ 3 files changed, 90 insertions(+), 38 deletions(-) diff --git a/crates/recording/src/capture_pipeline.rs b/crates/recording/src/capture_pipeline.rs index b9f372e73d5..eea5d2d770e 100644 --- a/crates/recording/src/capture_pipeline.rs +++ b/crates/recording/src/capture_pipeline.rs @@ -8,11 +8,38 @@ use anyhow::anyhow; use cap_timestamp::Timestamps; use std::{path::PathBuf, sync::Arc}; +#[cfg(windows)] +use std::sync::atomic::{AtomicBool, Ordering}; + +#[cfg(windows)] +#[derive(Clone, Debug)] +pub struct EncoderPreferences { + force_software: Arc, +} + +#[cfg(windows)] +impl EncoderPreferences { + pub fn new() -> Self { + Self { + force_software: Arc::new(AtomicBool::new(false)), + } + } + + pub fn should_force_software(&self) -> bool { + self.force_software.load(Ordering::Relaxed) + } + + pub fn force_software_only(&self) { + self.force_software.store(true, Ordering::Relaxed); + } +} + pub trait MakeCapturePipeline: ScreenCaptureFormat + std::fmt::Debug + 'static { async fn make_studio_mode_pipeline( screen_capture: screen_capture::VideoSourceConfig, output_path: PathBuf, start_time: Timestamps, + #[cfg(windows)] encoder_preferences: EncoderPreferences, ) -> anyhow::Result where Self: Sized; @@ -23,6 +50,7 @@ pub trait MakeCapturePipeline: ScreenCaptureFormat + std::fmt::Debug + 'static { mic_feed: Option>, output_path: PathBuf, output_resolution: (u32, u32), + #[cfg(windows)] encoder_preferences: EncoderPreferences, ) -> anyhow::Result where Self: Sized; @@ -76,6 +104,7 @@ impl MakeCapturePipeline for screen_capture::Direct3DCapture { screen_capture: screen_capture::VideoSourceConfig, output_path: PathBuf, start_time: Timestamps, + encoder_preferences: EncoderPreferences, ) -> anyhow::Result { let d3d_device = screen_capture.d3d_device.clone(); @@ -88,6 +117,7 @@ impl MakeCapturePipeline for screen_capture::Direct3DCapture { bitrate_multiplier: 0.15f32, frame_rate: 30u32, output_size: None, + encoder_preferences, }) .await } @@ -98,6 +128,7 @@ impl MakeCapturePipeline for screen_capture::Direct3DCapture { mic_feed: Option>, output_path: PathBuf, output_resolution: (u32, u32), + encoder_preferences: EncoderPreferences, ) -> anyhow::Result { let d3d_device = screen_capture.d3d_device.clone(); let mut output_builder = OutputPipeline::builder(output_path.clone()) @@ -122,6 +153,7 @@ impl MakeCapturePipeline for screen_capture::Direct3DCapture { Width: output_resolution.0 as i32, Height: output_resolution.1 as i32, }), + encoder_preferences, }) .await } diff --git a/crates/recording/src/output_pipeline/win.rs b/crates/recording/src/output_pipeline/win.rs index 1f6204d26ae..b3547f5db68 100644 --- a/crates/recording/src/output_pipeline/win.rs +++ b/crates/recording/src/output_pipeline/win.rs @@ -32,6 +32,7 @@ pub struct WindowsMuxerConfig { pub frame_rate: u32, pub bitrate_multiplier: f32, pub output_size: Option, + pub encoder_preferences: crate::capture_pipeline::EncoderPreferences, } impl Muxer for WindowsMuxer { @@ -71,21 +72,52 @@ impl Muxer for WindowsMuxer { tasks.spawn_thread("windows-encoder", move || { cap_mediafoundation_utils::thread_init(); + let encoder_preferences = &config.encoder_preferences; + let encoder = (|| { let mut output = output.lock().unwrap(); - let native_encoder = - cap_enc_mediafoundation::H264Encoder::new_with_scaled_output( - &config.d3d_device, - config.pixel_format, - input_size, - output_size, - config.frame_rate, - config.bitrate_multiplier, - ); - - match native_encoder { - Ok(encoder) => cap_mediafoundation_ffmpeg::H264StreamMuxer::new( + let fallback = |reason: Option| { + use tracing::{error, info}; + + encoder_preferences.force_software_only(); + if let Some(reason) = reason.as_ref() { + error!("Falling back to software H264 encoder: {reason}"); + } else { + info!("Falling back to software H264 encoder"); + } + + let fallback_width = if output_size.Width > 0 { + output_size.Width as u32 + } else { + video_config.width + }; + let fallback_height = if output_size.Height > 0 { + output_size.Height as u32 + } else { + video_config.height + }; + + cap_enc_ffmpeg::H264Encoder::builder(video_config) + .with_output_size(fallback_width, fallback_height) + .and_then(|builder| builder.build(&mut output)) + .map(either::Right) + .map_err(|e| anyhow!("ScreenSoftwareEncoder/{e}")) + }; + + if encoder_preferences.should_force_software() { + return fallback(None); + } + + match cap_enc_mediafoundation::H264Encoder::new_with_scaled_output( + &config.d3d_device, + config.pixel_format, + input_size, + output_size, + config.frame_rate, + config.bitrate_multiplier, + ) { + Ok(encoder) => match cap_mediafoundation_ffmpeg::H264StreamMuxer::new( &mut output, cap_mediafoundation_ffmpeg::MuxerConfig { width: output_size.Width as u32, @@ -93,32 +125,11 @@ impl Muxer for WindowsMuxer { fps: config.frame_rate, bitrate: encoder.bitrate(), }, - ) - .map(|muxer| either::Left((encoder, muxer))) - .map_err(|e| anyhow!("{e}")), - Err(e) => { - use tracing::{error, info}; - - error!("Failed to create native encoder: {e}"); - info!("Falling back to software H264 encoder"); - - let fallback_width = if output_size.Width > 0 { - output_size.Width as u32 - } else { - video_config.width - }; - let fallback_height = if output_size.Height > 0 { - output_size.Height as u32 - } else { - video_config.height - }; - - cap_enc_ffmpeg::H264Encoder::builder(video_config) - .with_output_size(fallback_width, fallback_height) - .and_then(|builder| builder.build(&mut output)) - .map(either::Right) - .map_err(|e| anyhow!("ScreenSoftwareEncoder/{e}")) - } + ) { + Ok(muxer) => Ok(either::Left((encoder, muxer))), + Err(err) => fallback(Some(err.to_string())), + }, + Err(err) => fallback(Some(err.to_string())), } })(); diff --git a/crates/recording/src/studio_recording.rs b/crates/recording/src/studio_recording.rs index 146c235ac02..85c4bfa9fe1 100644 --- a/crates/recording/src/studio_recording.rs +++ b/crates/recording/src/studio_recording.rs @@ -587,6 +587,8 @@ struct SegmentPipelineFactory { start_time: Timestamps, index: u32, completion_tx: watch::Sender>>, + #[cfg(windows)] + encoder_preferences: crate::capture_pipeline::EncoderPreferences, } impl SegmentPipelineFactory { @@ -607,6 +609,8 @@ impl SegmentPipelineFactory { start_time, index: 0, completion_tx, + #[cfg(windows)] + encoder_preferences: crate::capture_pipeline::EncoderPreferences::new(), } } @@ -624,6 +628,8 @@ impl SegmentPipelineFactory { next_cursors_id, self.custom_cursor_capture, self.start_time, + #[cfg(windows)] + self.encoder_preferences.clone(), ) .await?; @@ -682,6 +688,7 @@ async fn create_segment_pipeline( next_cursors_id: u32, custom_cursor_capture: bool, start_time: Timestamps, + #[cfg(windows)] encoder_preferences: crate::capture_pipeline::EncoderPreferences, ) -> anyhow::Result { #[cfg(windows)] let d3d_device = crate::capture_pipeline::create_d3d_device().unwrap(); @@ -718,6 +725,8 @@ async fn create_segment_pipeline( capture_source, screen_output_path.clone(), start_time, + #[cfg(windows)] + encoder_preferences, ) .instrument(error_span!("screen-out")) .await From 390d10d49d515947249fdf8d45943a4489ff96e5 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Mon, 27 Oct 2025 16:50:42 -0700 Subject: [PATCH 5/7] Fix encoder error handling and output locking on Windows Improves error propagation in MP4 export by mapping encoder errors to strings. Refactors Windows output pipeline to ensure proper locking of output streams and corrects the fallback logic for encoder selection. Also removes an unused import and minor variable cleanup in Windows screen capture source. --- crates/export/src/mp4.rs | 10 +++-- crates/recording/src/instant_recording.rs | 2 + crates/recording/src/output_pipeline/win.rs | 39 +++++++++++-------- .../src/sources/screen_capture/windows.rs | 3 +- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/crates/export/src/mp4.rs b/crates/export/src/mp4.rs index b54e52177a8..e50a5441b18 100644 --- a/crates/export/src/mp4.rs +++ b/crates/export/src/mp4.rs @@ -97,10 +97,12 @@ impl Mp4ExportSettings { let mut encoded_frames = 0; while let Ok(frame) = frame_rx.recv() { - encoder.queue_video_frame( - frame.video, - Duration::from_secs_f32(encoded_frames as f32 / fps as f32), - ); + encoder + .queue_video_frame( + frame.video, + Duration::from_secs_f32(encoded_frames as f32 / fps as f32), + ) + .map_err(|err| err.to_string())?; encoded_frames += 1; if let Some(audio) = frame.audio { encoder.queue_audio_frame(audio); diff --git a/crates/recording/src/instant_recording.rs b/crates/recording/src/instant_recording.rs index ae325891995..138d2dfb280 100644 --- a/crates/recording/src/instant_recording.rs +++ b/crates/recording/src/instant_recording.rs @@ -224,6 +224,8 @@ async fn create_pipeline( mic_feed, output_path.clone(), output_resolution, + #[cfg(windows)] + crate::capture_pipeline::EncoderPreferences::new(), ) .await?; diff --git a/crates/recording/src/output_pipeline/win.rs b/crates/recording/src/output_pipeline/win.rs index b3547f5db68..44d2971986a 100644 --- a/crates/recording/src/output_pipeline/win.rs +++ b/crates/recording/src/output_pipeline/win.rs @@ -75,9 +75,7 @@ impl Muxer for WindowsMuxer { let encoder_preferences = &config.encoder_preferences; let encoder = (|| { - let mut output = output.lock().unwrap(); - - let fallback = |reason: Option| { + let mut fallback = |reason: Option| { use tracing::{error, info}; encoder_preferences.force_software_only(); @@ -98,9 +96,11 @@ impl Muxer for WindowsMuxer { video_config.height }; + let mut output = output.lock().unwrap(); + cap_enc_ffmpeg::H264Encoder::builder(video_config) .with_output_size(fallback_width, fallback_height) - .and_then(|builder| builder.build(&mut output)) + .and_then(|builder| builder.build(&mut *output)) .map(either::Right) .map_err(|e| anyhow!("ScreenSoftwareEncoder/{e}")) }; @@ -117,18 +117,25 @@ impl Muxer for WindowsMuxer { config.frame_rate, config.bitrate_multiplier, ) { - Ok(encoder) => match cap_mediafoundation_ffmpeg::H264StreamMuxer::new( - &mut output, - cap_mediafoundation_ffmpeg::MuxerConfig { - width: output_size.Width as u32, - height: output_size.Height as u32, - fps: config.frame_rate, - bitrate: encoder.bitrate(), - }, - ) { - Ok(muxer) => Ok(either::Left((encoder, muxer))), - Err(err) => fallback(Some(err.to_string())), - }, + Ok(encoder) => { + let muxer = { + let mut output = output.lock().unwrap(); + cap_mediafoundation_ffmpeg::H264StreamMuxer::new( + &mut *output, + cap_mediafoundation_ffmpeg::MuxerConfig { + width: output_size.Width as u32, + height: output_size.Height as u32, + fps: config.frame_rate, + bitrate: encoder.bitrate(), + }, + ) + }; + + match muxer { + Ok(muxer) => Ok(either::Left((encoder, muxer))), + Err(err) => fallback(Some(err.to_string())), + } + } Err(err) => fallback(Some(err.to_string())), } })(); diff --git a/crates/recording/src/sources/screen_capture/windows.rs b/crates/recording/src/sources/screen_capture/windows.rs index d14eb84f6ba..d1fadede0da 100644 --- a/crates/recording/src/sources/screen_capture/windows.rs +++ b/crates/recording/src/sources/screen_capture/windows.rs @@ -13,7 +13,6 @@ use futures::{ FutureExt, SinkExt, StreamExt, channel::{mpsc, oneshot}, }; -use kameo::prelude::*; use scap_direct3d::StopCapturerError; use scap_ffmpeg::*; use scap_targets::{Display, DisplayId}; @@ -179,7 +178,7 @@ impl output_pipeline::VideoSource for VideoSource { where Self: Sized, { - let (mut error_tx, mut error_rx) = mpsc::channel(1); + let (error_tx, mut error_rx) = mpsc::channel(1); let (ctrl_tx, ctrl_rx) = std::sync::mpsc::sync_channel::(1); let tokio_rt = tokio::runtime::Handle::current(); From ed4f837606edc2261f8b76366052de0186033f60 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Mon, 27 Oct 2025 16:58:59 -0700 Subject: [PATCH 6/7] Coderabbit suggestions --- crates/recording/src/output_pipeline/win.rs | 51 ++++++++++++++++++--- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/crates/recording/src/output_pipeline/win.rs b/crates/recording/src/output_pipeline/win.rs index 44d2971986a..976a48fa432 100644 --- a/crates/recording/src/output_pipeline/win.rs +++ b/crates/recording/src/output_pipeline/win.rs @@ -75,7 +75,7 @@ impl Muxer for WindowsMuxer { let encoder_preferences = &config.encoder_preferences; let encoder = (|| { - let mut fallback = |reason: Option| { + let fallback = |reason: Option| { use tracing::{error, info}; encoder_preferences.force_software_only(); @@ -96,11 +96,19 @@ impl Muxer for WindowsMuxer { video_config.height }; - let mut output = output.lock().unwrap(); + let mut output_guard = match output.lock() { + Ok(guard) => guard, + Err(poisoned) => { + return Err(anyhow!( + "ScreenSoftwareEncoder: failed to lock output mutex: {}", + poisoned + )) + } + }; cap_enc_ffmpeg::H264Encoder::builder(video_config) .with_output_size(fallback_width, fallback_height) - .and_then(|builder| builder.build(&mut *output)) + .and_then(|builder| builder.build(&mut *output_guard)) .map(either::Right) .map_err(|e| anyhow!("ScreenSoftwareEncoder/{e}")) }; @@ -118,13 +126,42 @@ impl Muxer for WindowsMuxer { config.bitrate_multiplier, ) { Ok(encoder) => { + let width = match u32::try_from(output_size.Width) { + Ok(width) if width > 0 => width, + _ => { + return fallback(Some(format!( + "Invalid output width: {}", + output_size.Width + ))) + } + }; + + let height = match u32::try_from(output_size.Height) { + Ok(height) if height > 0 => height, + _ => { + return fallback(Some(format!( + "Invalid output height: {}", + output_size.Height + ))) + } + }; + let muxer = { - let mut output = output.lock().unwrap(); + let mut output_guard = match output.lock() { + Ok(guard) => guard, + Err(poisoned) => { + return fallback(Some(format!( + "Failed to lock output mutex: {}", + poisoned + ))) + } + }; + cap_mediafoundation_ffmpeg::H264StreamMuxer::new( - &mut *output, + &mut *output_guard, cap_mediafoundation_ffmpeg::MuxerConfig { - width: output_size.Width as u32, - height: output_size.Height as u32, + width, + height, fps: config.frame_rate, bitrate: encoder.bitrate(), }, From 8f11c001cc7e2a13b7b9550c74ee7670a55cc03c Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Mon, 27 Oct 2025 17:02:22 -0700 Subject: [PATCH 7/7] fmt --- crates/recording/src/output_pipeline/win.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/recording/src/output_pipeline/win.rs b/crates/recording/src/output_pipeline/win.rs index 976a48fa432..0ab39862da1 100644 --- a/crates/recording/src/output_pipeline/win.rs +++ b/crates/recording/src/output_pipeline/win.rs @@ -102,7 +102,7 @@ impl Muxer for WindowsMuxer { return Err(anyhow!( "ScreenSoftwareEncoder: failed to lock output mutex: {}", poisoned - )) + )); } }; @@ -132,7 +132,7 @@ impl Muxer for WindowsMuxer { return fallback(Some(format!( "Invalid output width: {}", output_size.Width - ))) + ))); } }; @@ -142,7 +142,7 @@ impl Muxer for WindowsMuxer { return fallback(Some(format!( "Invalid output height: {}", output_size.Height - ))) + ))); } }; @@ -153,7 +153,7 @@ impl Muxer for WindowsMuxer { return fallback(Some(format!( "Failed to lock output mutex: {}", poisoned - ))) + ))); } };