Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
Add resolution downscaling for hardware encoder limits
Richie McIlroy8abd14fd0d9f
diff --git a/crates/recording/src/capture_pipeline.rs b/crates/recording/src/capture_pipeline.rsindex f8e7671d0..379e01e28 100644--- a/crates/recording/src/capture_pipeline.rs+++ b/crates/recording/src/capture_pipeline.rs@@ -56,6 +56,7 @@ pub trait MakeCapturePipeline: ScreenCaptureFormat + std::fmt::Debug + 'static {start_time: Timestamps,fragmented: bool,shared_pause_state: Option<SharedPauseState>,+ output_size: Option<(u32, u32)>,#[cfg(windows)] encoder_preferences: EncoderPreferences,) -> anyhow::Result<OutputPipeline>where@@ -83,6 +84,7 @@ impl MakeCapturePipeline for screen_capture::CMSampleBufferCapture {start_time: Timestamps,fragmented: bool,shared_pause_state: Option<SharedPauseState>,+ output_size: Option<(u32, u32)>,) -> anyhow::Result<OutputPipeline> {if fragmented {let fragments_dir = output_path@@ -94,6 +96,7 @@ impl MakeCapturePipeline for screen_capture::CMSampleBufferCapture {.with_video::<screen_capture::VideoSource>(screen_capture).with_timestamps(start_time).build::<MacOSFragmentedM4SMuxer>(MacOSFragmentedM4SMuxerConfig {+ output_size,shared_pause_state,..Default::default()})@@ -102,7 +105,9 @@ impl MakeCapturePipeline for screen_capture::CMSampleBufferCapture {OutputPipeline::builder(output_path.clone()).with_video::<screen_capture::VideoSource>(screen_capture).with_timestamps(start_time)- .build::<AVFoundationMp4Muxer>(Default::default())+ .build::<AVFoundationMp4Muxer>(AVFoundationMp4MuxerConfig {+ output_height: output_size.map(|(_, h)| h),+ }).await}}@@ -141,6 +146,7 @@ impl MakeCapturePipeline for screen_capture::Direct3DCapture {start_time: Timestamps,fragmented: bool,shared_pause_state: Option<SharedPauseState>,+ output_size: Option<(u32, u32)>,encoder_preferences: EncoderPreferences,) -> anyhow::Result<OutputPipeline> {if fragmented {@@ -155,7 +161,7 @@ impl MakeCapturePipeline for screen_capture::Direct3DCapture {.build::<WindowsFragmentedM4SMuxer>(WindowsFragmentedM4SMuxerConfig {segment_duration: std::time::Duration::from_secs(3),preset: H264Preset::Ultrafast,- output_size: None,+ output_size,shared_pause_state,disk_space_callback: None,})@@ -170,7 +176,10 @@ impl MakeCapturePipeline for screen_capture::Direct3DCapture {d3d_device,bitrate_multiplier: 0.15f32,frame_rate: 30u32,- output_size: None,+ output_size: output_size.map(|(w, h)| windows::Graphics::SizeInt32 {+ Width: w as i32,+ Height: h as i32,+ }),encoder_preferences,fragmented: false,frag_duration_us: 2_000_000,diff --git a/crates/recording/src/lib.rs b/crates/recording/src/lib.rsindex 96f6991a2..95bc7b1df 100644--- a/crates/recording/src/lib.rs+++ b/crates/recording/src/lib.rs@@ -7,11 +7,14 @@ pub mod fragmentation;pub mod instant_recording;mod output_pipeline;pub mod recovery;+mod resolution_limits;pub mod screenshot;pub mod sources;pub mod studio_recording;pub mod sync_calibration;+pub use resolution_limits::{H264_MAX_DIMENSION, calculate_gpu_compatible_size};+#[cfg(any(test, feature = "test-utils"))]pub mod test_sources;diff --git a/crates/recording/src/resolution_limits.rs b/crates/recording/src/resolution_limits.rsnew file mode 100644index 000000000..309ff737f--- /dev/null+++ b/crates/recording/src/resolution_limits.rs@@ -0,0 +1,129 @@+pub const H264_MAX_DIMENSION: u32 = 4096;++pub fn calculate_gpu_compatible_size(+ width: u32,+ height: u32,+ max_dimension: u32,+) -> Option<(u32, u32)> {+ if width <= max_dimension && height <= max_dimension {+ return None;+ }++ let aspect_ratio = width as f64 / height as f64;++ let (target_width, target_height) = if width >= height {+ let mut target_width = max_dimension.min(width);+ let mut target_height = (target_width as f64 / aspect_ratio).round() as u32;++ if target_height > max_dimension {+ target_height = max_dimension;+ target_width = (target_height as f64 * aspect_ratio).round() as u32;+ target_width = target_width.min(max_dimension);+ }++ (target_width, target_height)+ } else {+ let mut target_height = max_dimension.min(height);+ let mut target_width = (target_height as f64 * aspect_ratio).round() as u32;++ if target_width > max_dimension {+ target_width = max_dimension;+ target_height = (target_width as f64 / aspect_ratio).round() as u32;+ target_height = target_height.min(max_dimension);+ }++ (target_width, target_height)+ };++ let final_width = ensure_even(target_width);+ let final_height = ensure_even(target_height);++ tracing::debug!(+ input_width = width,+ input_height = height,+ output_width = final_width,+ output_height = final_height,+ max_dimension = max_dimension,+ "Studio mode auto-downscaling applied for hardware encoder compatibility"+ );++ Some((final_width, final_height))+}++fn ensure_even(value: u32) -> u32 {+ let adjusted = value - (value % 2);+ if adjusted == 0 { 2 } else { adjusted }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn test_below_limits_returns_none() {+ assert_eq!(calculate_gpu_compatible_size(3840, 2160, 4096), None);+ assert_eq!(calculate_gpu_compatible_size(1920, 1080, 4096), None);+ }++ #[test]+ fn test_exactly_at_limit_returns_none() {+ assert_eq!(calculate_gpu_compatible_size(4096, 4096, 4096), None);+ assert_eq!(calculate_gpu_compatible_size(4096, 2160, 4096), None);+ assert_eq!(calculate_gpu_compatible_size(3840, 4096, 4096), None);+ }++ #[test]+ fn test_5k_landscape_downscales() {+ let result = calculate_gpu_compatible_size(5120, 2880, 4096);+ assert!(result.is_some());+ let (w, h) = result.unwrap();+ assert!(w <= 4096);+ assert!(h <= 4096);+ assert_eq!(w % 2, 0);+ assert_eq!(h % 2, 0);+ let original_aspect = 5120.0 / 2880.0;+ let result_aspect = w as f64 / h as f64;+ assert!((original_aspect - result_aspect).abs() < 0.02);+ }++ #[test]+ fn test_5k_ultrawide_downscales() {+ let result = calculate_gpu_compatible_size(5120, 2160, 4096);+ assert!(result.is_some());+ let (w, h) = result.unwrap();+ assert!(w <= 4096);+ assert!(h <= 4096);+ assert_eq!(w % 2, 0);+ assert_eq!(h % 2, 0);+ }++ #[test]+ fn test_portrait_mode_downscales() {+ let result = calculate_gpu_compatible_size(2880, 5120, 4096);+ assert!(result.is_some());+ let (w, h) = result.unwrap();+ assert!(w <= 4096);+ assert!(h <= 4096);+ assert_eq!(w % 2, 0);+ assert_eq!(h % 2, 0);+ }++ #[test]+ fn test_extreme_resolution() {+ let result = calculate_gpu_compatible_size(7680, 4320, 4096);+ assert!(result.is_some());+ let (w, h) = result.unwrap();+ assert!(w <= 4096);+ assert!(h <= 4096);+ assert_eq!(w % 2, 0);+ assert_eq!(h % 2, 0);+ }++ #[test]+ fn test_ensure_even() {+ assert_eq!(ensure_even(100), 100);+ assert_eq!(ensure_even(101), 100);+ assert_eq!(ensure_even(1), 2);+ assert_eq!(ensure_even(0), 2);+ }+}diff --git a/crates/recording/src/studio_recording.rs b/crates/recording/src/studio_recording.rsindex 4c4c4a358..d8415782b 100644--- a/crates/recording/src/studio_recording.rs+++ b/crates/recording/src/studio_recording.rs@@ -1,5 +1,6 @@use crate::{- ActorError, MediaError, RecordingBaseInputs, RecordingError, SharedPauseState,+ ActorError, H264_MAX_DIMENSION, MediaError, RecordingBaseInputs, RecordingError,+ SharedPauseState, calculate_gpu_compatible_size,capture_pipeline::{MakeCapturePipeline, ScreenCaptureMethod, Stop, target_to_display_and_crop,},@@ -957,6 +958,10 @@ async fn create_segment_pipeline(.await.context("screen capture init")?;+ let screen_info = screen_config.info();+ let output_size =+ calculate_gpu_compatible_size(screen_info.width, screen_info.height, H264_MAX_DIMENSION);+let (capture_source, system_audio) = screen_config.to_sources().await?;let dir = ensure_dir(&segments_dir.join(format!("segment-{index}")))?;@@ -989,6 +994,7 @@ async fn create_segment_pipeline(start_time,fragmented,shared_pause_state.clone(),+ output_size,#[cfg(windows)]encoder_preferences.clone(),)