diff --git a/app/src/settings.rs b/app/src/settings.rs index c3160f9..417a1b5 100644 --- a/app/src/settings.rs +++ b/app/src/settings.rs @@ -171,12 +171,13 @@ impl Quality { } } + /// What a quality level records at: the screen's resolution, at this bitrate. pub fn detail(self) -> &'static str { match self { - Quality::Low => "720p @ 30fps", - Quality::Medium => "1080p @ 30fps", - Quality::High => "1080p @ 60fps", - Quality::Ultra => "1440p @ 60fps", + Quality::Low => "Your screen's resolution at 4.5 Mbps", + Quality::Medium => "Your screen's resolution at 6 Mbps", + Quality::High => "Your screen's resolution at 8 Mbps", + Quality::Ultra => "Your screen's resolution at 12 Mbps", } } } diff --git a/plans/record-daemon-architecture.md b/plans/record-daemon-architecture.md index 583e8ec..ae4578e 100644 --- a/plans/record-daemon-architecture.md +++ b/plans/record-daemon-architecture.md @@ -201,11 +201,13 @@ enum EncoderPreset { } enum QualityLevel { - Low, // 720p30, lower bitrate - Medium, // 1080p30, moderate bitrate - High, // 1080p60, high bitrate - Ultra, // 1440p60, max quality + Low, // 4.5 Mbps bitrate tier + Medium, // 6 Mbps + High, // 8 Mbps + Ultra, // 12 Mbps } +// Recordings always run at the screen resolution: the daemon sizes the OBS +// canvas from the primary monitor, and quality only trades bitrate. ``` ### 2. LQP Client diff --git a/record-daemon/src/config/presets.rs b/record-daemon/src/config/presets.rs index 2950f7e..4527df1 100644 --- a/record-daemon/src/config/presets.rs +++ b/record-daemon/src/config/presets.rs @@ -89,43 +89,26 @@ pub enum AmfQuality { Quality, } -/// Quality level presets that configure resolution and other parameters. +/// Quality level presets as bitrate tiers. +/// +/// Recordings always run at the screen's resolution; these only trade file size +/// against detail. Frame rate comes from `VideoSettings::frame_rate`. #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)] #[serde(rename_all = "lowercase")] pub enum QualityLevel { - /// 720p @ 30fps, lower bitrate. + /// 4500 kbps, smaller files. Low, - /// 1080p @ 30fps, moderate bitrate. + /// 6000 kbps. Medium, - /// 1080p @ 60fps, high bitrate. + /// 8000 kbps. #[default] High, - /// 1440p @ 60fps, maximum quality. + /// 12000 kbps, largest files. Ultra, } impl QualityLevel { - /// Get the target resolution for this quality level. - pub fn resolution(&self) -> (u32, u32) { - match self { - QualityLevel::Low => (1280, 720), - QualityLevel::Medium => (1920, 1080), - QualityLevel::High => (1920, 1080), - QualityLevel::Ultra => (2560, 1440), - } - } - - /// Get the recommended frame rate for this quality level. - pub fn frame_rate(&self) -> u32 { - match self { - QualityLevel::Low => 30, - QualityLevel::Medium => 30, - QualityLevel::High => 60, - QualityLevel::Ultra => 60, - } - } - - /// Get the recommended bitrate in kbps for this quality level. + /// Get the bitrate in kbps this quality level records at. pub fn recommended_bitrate(&self) -> u32 { match self { QualityLevel::Low => 4500, @@ -342,15 +325,6 @@ impl EncoderPreset { } } - /// Get the effective bitrate for this preset. - pub fn effective_bitrate(&self) -> u32 { - match self { - EncoderPreset::Nvenc { bitrate, .. } => *bitrate, - EncoderPreset::Amf { bitrate, .. } => *bitrate, - EncoderPreset::X264 { bitrate, .. } => *bitrate, - } - } - /// Create a preset optimized for the given quality level. pub fn for_quality(quality: QualityLevel, capability: EncoderCapability) -> Self { let bitrate = quality.recommended_bitrate(); @@ -384,10 +358,10 @@ mod tests { use super::*; #[test] - fn test_quality_level_resolution() { - assert_eq!(QualityLevel::Low.resolution(), (1280, 720)); - assert_eq!(QualityLevel::High.resolution(), (1920, 1080)); - assert_eq!(QualityLevel::Ultra.resolution(), (2560, 1440)); + fn test_quality_level_bitrate() { + assert_eq!(QualityLevel::Low.recommended_bitrate(), 4500); + assert_eq!(QualityLevel::High.recommended_bitrate(), 8000); + assert_eq!(QualityLevel::Ultra.recommended_bitrate(), 12000); } #[test] @@ -410,7 +384,14 @@ mod tests { #[test] fn test_preset_for_quality() { let preset = EncoderPreset::for_quality(QualityLevel::High, EncoderCapability::Nvenc); - assert_eq!(preset.effective_bitrate(), 8000); + assert_eq!( + preset, + EncoderPreset::Nvenc { + bitrate: 8000, + cq_level: 20, + two_pass: true + } + ); assert!(preset.is_hardware()); } } diff --git a/record-daemon/src/config/settings.rs b/record-daemon/src/config/settings.rs index acc8f8b..bdee391 100644 --- a/record-daemon/src/config/settings.rs +++ b/record-daemon/src/config/settings.rs @@ -46,6 +46,13 @@ pub struct VideoSettings { pub hardware_acceleration: bool, } +impl VideoSettings { + /// The bitrate a recording runs at: the quality tier's. + pub fn video_bitrate(&self) -> u32 { + self.quality.recommended_bitrate() + } +} + impl Default for VideoSettings { fn default() -> Self { Self { diff --git a/record-daemon/src/recording/obs_context.rs b/record-daemon/src/recording/obs_context.rs index 66b9faa..ab5e63e 100644 --- a/record-daemon/src/recording/obs_context.rs +++ b/record-daemon/src/recording/obs_context.rs @@ -127,6 +127,7 @@ impl ObsContextBuilder { recording: false, pending_audio: None, encoder_capability: None, + canvas: (0, 0), tracks: Vec::new(), }) } @@ -157,6 +158,9 @@ pub struct ObsContext { /// Settings that arrived while a recording was running, applied once it stops. pending_audio: Option<(VideoSettings, AudioSettings)>, encoder_capability: Option, + /// The canvas the video pipeline currently runs at, so a settings change or + /// a new recording can tell whether the screen resolution moved under it. + canvas: (u32, u32), /// What each audio track of the running recording carries, in file order. tracks: Vec, } @@ -191,7 +195,7 @@ impl ObsContext { self.encoder_capability = Some(encoder); info!("OBS encoder: {:?}", encoder); - let (width, height) = self.video_settings.quality.resolution(); + let (width, height) = canvas_resolution(); let fps = self.video_settings.frame_rate; let video_info = ObsVideoInfoBuilder::new() @@ -232,6 +236,7 @@ impl ObsContext { )); self.scene = Some(scene); self.context = Some(context); + self.canvas = (width, height); info!( "OBS context initialized ({}x{} @ {}fps)", @@ -260,31 +265,7 @@ impl ObsContext { return self.start(); } - if video.frame_rate != self.video_settings.frame_rate - || video.quality != self.video_settings.quality - { - let encoder = self - .encoder_capability - .unwrap_or_else(best_available_encoder); - let (width, height) = video.quality.resolution(); - let video_info = ObsVideoInfoBuilder::new() - .fps_num(video.frame_rate) - .fps_den(1) - .base_width(width) - .base_height(height) - .output_width(width) - .output_height(height) - .build(); - if let Some(context) = self.context.as_mut() { - context.reset_video(video_info).map_err(|e| { - RecordingError::StartError(format!("failed to reset video: {:?}", e)) - })?; - } - info!( - "video reset to {}x{} @ {}fps ({:?})", - width, height, video.frame_rate, encoder - ); - } + self.reset_video_if_changed(video.frame_rate)?; if audio != self.audio_settings { let context = self @@ -319,6 +300,7 @@ impl ObsContext { self.apply_now(video, audio)?; } self.start()?; + self.reset_video_if_changed(self.video_settings.frame_rate)?; let context = self .context @@ -332,7 +314,7 @@ impl ObsContext { let encoder = self .encoder_capability .unwrap_or_else(best_available_encoder); - let bitrate = self.video_settings.encoder_preset.effective_bitrate(); + let bitrate = self.video_settings.video_bitrate(); let obs_path = ObsPath::new(&output_path.to_string_lossy()); let mut builder = @@ -447,6 +429,39 @@ impl ObsContext { self.context = None; Ok(()) } + + /// Rebuild the video pipeline when the frame rate or the screen moved. + /// + /// Both a settings change and a recording start go through here, so the canvas + /// always matches the capture and the recording never crops the game. + fn reset_video_if_changed(&mut self, fps: u32) -> Result<()> { + let (width, height) = canvas_resolution(); + if fps == self.video_settings.frame_rate && (width, height) == self.canvas { + return Ok(()); + } + let encoder = self + .encoder_capability + .unwrap_or_else(best_available_encoder); + let video_info = ObsVideoInfoBuilder::new() + .fps_num(fps) + .fps_den(1) + .base_width(width) + .base_height(height) + .output_width(width) + .output_height(height) + .build(); + if let Some(context) = self.context.as_mut() { + context.reset_video(video_info).map_err(|e| { + RecordingError::StartError(format!("failed to reset video: {:?}", e)) + })?; + } + self.canvas = (width, height); + info!( + "video reset to {}x{} @ {}fps ({:?})", + width, height, fps, encoder + ); + Ok(()) + } } impl Drop for ObsContext { @@ -479,6 +494,31 @@ fn audio_source( Ok(source) } +/// The resolution to record at: the primary monitor's, in device pixels. +/// +/// The capture fills the canvas one to one, so a recording never crops or rescales +/// the game, whatever preset is set. Falls back to 1080p if the display query fails. +fn canvas_resolution() -> (u32, u32) { + const FALLBACK: (u32, u32) = (1920, 1080); + let displays = match display_info::DisplayInfo::all() { + Ok(displays) if !displays.is_empty() => displays, + _ => { + warn!( + "could not list displays, recording at {}x{}", + FALLBACK.0, FALLBACK.1 + ); + return FALLBACK; + } + }; + let screen = displays + .iter() + .find(|d| d.is_primary) + .unwrap_or_else(|| displays.iter().max_by_key(|d| d.width * d.height).unwrap()); + // H.264 encoders want even dimensions. + let even = |v: u32| v & !1; + (even(screen.width), even(screen.height)) +} + /// Audio that comes from a device: the microphone and any configured second device. /// /// These live in the scene for the whole session. Process captures are in the scene as well,