Files
leaguerecorder/record-daemon/src/config/presets.rs
T
vhaudiquet b1cde7e783 record: size recordings from the screen, not the quality preset
The quality preset chose the OBS canvas while the capture ran at the
game's real size, and the scene item carries no transform, so picking
1080p on a 1440p screen recorded only the top-left 1920x1080 crop of
the game.

Recordings now always run at the primary monitor's resolution, queried
in device pixels so display scaling cannot skew them, and a recording
start re-checks the screen so a display change mid-session cannot
bring the crop back. Quality presets keep their bitrate meaning and
nothing else; they never reached the encoder before, so High, Ultra
and friends now actually change what a recording costs on disk.
2026-11-21 18:15:52 +01:00

398 lines
10 KiB
Rust

//! Encoding presets and quality levels for video recording.
use serde::{Deserialize, Serialize};
/// Video encoder preset configuration.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum EncoderPreset {
/// NVIDIA NVENC hardware encoder.
Nvenc {
/// Target bitrate in kbps.
#[serde(default = "default_nvenc_bitrate")]
bitrate: u32,
/// Constant Quality level (lower = better quality).
#[serde(default = "default_nvenc_cq")]
cq_level: u32,
/// Use two-pass encoding.
#[serde(default = "default_two_pass")]
two_pass: bool,
},
/// AMD AMF hardware encoder.
Amf {
/// Target bitrate in kbps.
#[serde(default = "default_amf_bitrate")]
bitrate: u32,
/// Quality preset (speed, balanced, quality).
#[serde(default = "default_amf_quality")]
quality: AmfQuality,
},
/// Software x264 encoder (fallback).
X264 {
/// x264 preset (ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow).
#[serde(default = "default_x264_preset")]
preset: String,
/// Target bitrate in kbps.
#[serde(default = "default_x264_bitrate")]
bitrate: u32,
/// Use constant rate factor instead of bitrate.
#[serde(default)]
crf: Option<u32>,
},
}
impl Default for EncoderPreset {
fn default() -> Self {
Self::Nvenc {
bitrate: default_nvenc_bitrate(),
cq_level: default_nvenc_cq(),
two_pass: default_two_pass(),
}
}
}
fn default_nvenc_bitrate() -> u32 {
8000
}
fn default_nvenc_cq() -> u32 {
20
}
fn default_two_pass() -> bool {
true
}
fn default_amf_bitrate() -> u32 {
8000
}
fn default_amf_quality() -> AmfQuality {
AmfQuality::Balanced
}
fn default_x264_preset() -> String {
"veryfast".to_string()
}
fn default_x264_bitrate() -> u32 {
6000
}
/// AMD AMF quality preset.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "lowercase")]
pub enum AmfQuality {
Speed,
#[default]
Balanced,
Quality,
}
/// 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 {
/// 4500 kbps, smaller files.
Low,
/// 6000 kbps.
Medium,
/// 8000 kbps.
#[default]
High,
/// 12000 kbps, largest files.
Ultra,
}
impl QualityLevel {
/// Get the bitrate in kbps this quality level records at.
pub fn recommended_bitrate(&self) -> u32 {
match self {
QualityLevel::Low => 4500,
QualityLevel::Medium => 6000,
QualityLevel::High => 8000,
QualityLevel::Ultra => 12000,
}
}
}
fn preset_default_true() -> bool {
true
}
/// Audio capture settings.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AudioSettings {
/// Enable audio recording.
#[serde(default = "preset_default_true")]
pub enabled: bool,
/// Audio bitrate in kbps.
#[serde(default = "default_audio_bitrate")]
pub bitrate: u32,
/// Sample rate in Hz.
#[serde(default = "default_sample_rate")]
pub sample_rate: u32,
/// Number of audio channels.
#[serde(default = "default_channels")]
pub channels: u32,
/// Capture game audio (desktop audio).
#[serde(default = "preset_default_true")]
pub capture_game: bool,
/// Game audio volume (0-100).
#[serde(default = "default_volume")]
pub game_volume: u32,
/// Capture microphone.
#[serde(default)]
pub capture_mic: bool,
/// Microphone device ID (if capture_mic is true).
#[serde(default)]
pub mic_device: Option<String>,
/// Microphone volume (0-100).
#[serde(default = "default_volume")]
pub mic_volume: u32,
/// Capture application audio (e.g., Discord, Spotify).
#[serde(default)]
pub capture_apps: Vec<AudioSourceConfig>,
/// Application audio volume (0-100) - global for all app sources.
#[serde(default = "default_volume")]
pub apps_volume: u32,
}
impl Default for AudioSettings {
fn default() -> Self {
Self {
enabled: true,
bitrate: default_audio_bitrate(),
sample_rate: default_sample_rate(),
channels: default_channels(),
capture_game: true,
game_volume: default_volume(),
capture_mic: false,
mic_device: None,
mic_volume: default_volume(),
capture_apps: Vec::new(),
apps_volume: default_volume(),
}
}
}
fn default_volume() -> u32 {
100
}
/// Configuration for an individual audio source (e.g., Discord, Spotify).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AudioSourceConfig {
/// Unique identifier for this source.
#[serde(default)]
pub id: String,
/// Display name for the source.
pub name: String,
/// Type of audio source.
#[serde(rename = "type")]
pub source_type: AudioSourceType,
/// The device ID or process name to capture.
pub device: String,
/// Whether this source is enabled.
#[serde(default = "preset_default_true")]
pub enabled: bool,
/// Individual volume for this source (0-100), if None uses global apps_volume.
#[serde(skip_serializing_if = "Option::is_none")]
pub volume: Option<u32>,
}
impl AudioSourceConfig {
/// Create a new application audio source configuration.
pub fn new(name: &str, source_type: AudioSourceType, device: &str) -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
name: name.to_string(),
source_type,
device: device.to_string(),
enabled: true,
volume: None,
}
}
/// Create a Discord audio source.
pub fn discord() -> Self {
Self::new("Discord", AudioSourceType::Application, "Discord.exe")
}
/// Create a Spotify audio source.
pub fn spotify() -> Self {
Self::new("Spotify", AudioSourceType::Application, "Spotify.exe")
}
}
/// Type of audio source.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "lowercase")]
pub enum AudioSourceType {
/// Application audio (captures specific process on Windows via WASAPI loopback).
#[default]
Application,
/// Audio output device (captures all audio from a specific output device).
DeviceOutput,
/// Audio input device (microphone, line-in, etc.).
DeviceInput,
}
/// Information about an available audio device.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AudioDeviceInfo {
/// Device ID (used for device selection).
pub id: String,
/// Display name of the device.
pub name: String,
/// Type of the device.
pub device_type: AudioDeviceType,
/// Whether this is the default device for its type.
pub is_default: bool,
}
/// Type of audio device.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum AudioDeviceType {
/// Input device (microphone, line-in).
Input,
/// Output device (speakers, headphones).
Output,
}
fn default_audio_bitrate() -> u32 {
192
}
fn default_sample_rate() -> u32 {
48000
}
fn default_channels() -> u32 {
2
}
/// Encoder capabilities detection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EncoderCapability {
/// NVIDIA NVENC available.
Nvenc,
/// AMD AMF available.
Amf,
/// Intel QuickSync available.
QuickSync,
/// Software encoding only.
Software,
}
impl EncoderPreset {
/// Check if this preset uses hardware encoding.
pub fn is_hardware(&self) -> bool {
matches!(
self,
EncoderPreset::Nvenc { .. } | EncoderPreset::Amf { .. }
)
}
/// Get the encoder name for OBS.
pub fn encoder_name(&self) -> &'static str {
match self {
EncoderPreset::Nvenc { .. } => "jim_nvenc",
EncoderPreset::Amf { .. } => "amd_amf_h264",
EncoderPreset::X264 { .. } => "x264",
}
}
/// Create a preset optimized for the given quality level.
pub fn for_quality(quality: QualityLevel, capability: EncoderCapability) -> Self {
let bitrate = quality.recommended_bitrate();
match capability {
EncoderCapability::Nvenc => EncoderPreset::Nvenc {
bitrate,
cq_level: 20,
two_pass: true,
},
EncoderCapability::Amf => EncoderPreset::Amf {
bitrate,
quality: AmfQuality::Balanced,
},
EncoderCapability::QuickSync => EncoderPreset::X264 {
preset: "veryfast".to_string(),
bitrate,
crf: None,
},
EncoderCapability::Software => EncoderPreset::X264 {
preset: "superfast".to_string(),
bitrate: (bitrate as f32 * 0.8) as u32,
crf: Some(23),
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
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]
fn test_encoder_preset_hardware_detection() {
let nvenc = EncoderPreset::Nvenc {
bitrate: 8000,
cq_level: 20,
two_pass: true,
};
assert!(nvenc.is_hardware());
let x264 = EncoderPreset::X264 {
preset: "fast".to_string(),
bitrate: 6000,
crf: None,
};
assert!(!x264.is_hardware());
}
#[test]
fn test_preset_for_quality() {
let preset = EncoderPreset::for_quality(QualityLevel::High, EncoderCapability::Nvenc);
assert_eq!(
preset,
EncoderPreset::Nvenc {
bitrate: 8000,
cq_level: 20,
two_pass: true
}
);
assert!(preset.is_hardware());
}
}