record-daemon: initial commit
This commit is contained in:
commit
d6c0334369
30 files changed
+9486
No files matched your search
@@ -0,0 +1,303 @@
|
||||
//! Video and audio encoder configuration.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::config::{AmfQuality, EncoderPreset, QualityLevel};
|
||||
|
||||
/// Video encoder configuration.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct EncoderConfig {
|
||||
/// Encoder ID (e.g., "jim_nvenc", "x264").
|
||||
pub encoder_id: String,
|
||||
/// Target bitrate in kbps.
|
||||
pub bitrate: u32,
|
||||
/// Keyframe interval in seconds.
|
||||
pub keyframe_interval: u32,
|
||||
/// Encoder-specific settings.
|
||||
pub settings: EncoderSettings,
|
||||
}
|
||||
|
||||
/// Encoder-specific settings.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "lowercase")]
|
||||
pub enum EncoderSettings {
|
||||
/// NVIDIA NVENC settings.
|
||||
Nvenc {
|
||||
/// Constant Quality level (0-51, lower = better).
|
||||
cq_level: u32,
|
||||
/// Use two-pass encoding.
|
||||
two_pass: bool,
|
||||
/// Preset (p1-p7, lower = faster).
|
||||
preset: String,
|
||||
/// Rate control mode.
|
||||
rate_control: NvencRateControl,
|
||||
},
|
||||
/// AMD AMF settings.
|
||||
Amf {
|
||||
/// Quality preset.
|
||||
quality: AmfQuality,
|
||||
/// Rate control method.
|
||||
rate_control: AmfRateControl,
|
||||
},
|
||||
/// x264 settings.
|
||||
X264 {
|
||||
/// Preset name.
|
||||
preset: String,
|
||||
/// Constant Rate Factor (if using CRF).
|
||||
crf: Option<u32>,
|
||||
/// Use threads.
|
||||
threads: u32,
|
||||
},
|
||||
}
|
||||
|
||||
/// NVENC rate control modes.
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum NvencRateControl {
|
||||
/// Constant bitrate.
|
||||
Cbr,
|
||||
/// Variable bitrate.
|
||||
#[default]
|
||||
Vbr,
|
||||
/// Constant quality.
|
||||
Cqp,
|
||||
}
|
||||
|
||||
/// AMF rate control modes.
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum AmfRateControl {
|
||||
/// Constant bitrate.
|
||||
Cbr,
|
||||
/// Variable bitrate.
|
||||
#[default]
|
||||
Vbr,
|
||||
/// Constant QP.
|
||||
Cqp,
|
||||
}
|
||||
|
||||
impl EncoderConfig {
|
||||
/// Create encoder config from preset.
|
||||
pub fn from_preset(preset: &EncoderPreset) -> Self {
|
||||
match preset {
|
||||
EncoderPreset::Nvenc {
|
||||
bitrate,
|
||||
cq_level,
|
||||
two_pass,
|
||||
} => Self {
|
||||
encoder_id: "jim_nvenc".to_string(),
|
||||
bitrate: *bitrate,
|
||||
keyframe_interval: 2,
|
||||
settings: EncoderSettings::Nvenc {
|
||||
cq_level: *cq_level,
|
||||
two_pass: *two_pass,
|
||||
preset: "p4".to_string(), // Balanced preset
|
||||
rate_control: NvencRateControl::Vbr,
|
||||
},
|
||||
},
|
||||
EncoderPreset::Amf { bitrate, quality } => Self {
|
||||
encoder_id: "amd_amf_h264".to_string(),
|
||||
bitrate: *bitrate,
|
||||
keyframe_interval: 2,
|
||||
settings: EncoderSettings::Amf {
|
||||
quality: *quality,
|
||||
rate_control: AmfRateControl::Vbr,
|
||||
},
|
||||
},
|
||||
EncoderPreset::X264 {
|
||||
preset,
|
||||
bitrate,
|
||||
crf,
|
||||
} => Self {
|
||||
encoder_id: "x264".to_string(),
|
||||
bitrate: *bitrate,
|
||||
keyframe_interval: 2,
|
||||
settings: EncoderSettings::X264 {
|
||||
preset: preset.clone(),
|
||||
crf: *crf,
|
||||
threads: 0, // Auto
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the encoder name for display.
|
||||
pub fn encoder_name(&self) -> &str {
|
||||
&self.encoder_id
|
||||
}
|
||||
|
||||
/// Check if this is a hardware encoder.
|
||||
pub fn is_hardware(&self) -> bool {
|
||||
matches!(
|
||||
self.settings,
|
||||
EncoderSettings::Nvenc { .. } | EncoderSettings::Amf { .. }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Video encoder trait for abstraction over different encoders.
|
||||
pub trait VideoEncoder {
|
||||
/// Get the encoder ID.
|
||||
fn id(&self) -> &str;
|
||||
|
||||
/// Get the current bitrate.
|
||||
fn bitrate(&self) -> u32;
|
||||
|
||||
/// Update the bitrate.
|
||||
fn set_bitrate(&mut self, bitrate: u32);
|
||||
|
||||
/// Get encoder-specific settings as JSON.
|
||||
fn settings_json(&self) -> serde_json::Value;
|
||||
}
|
||||
|
||||
/// Audio encoder configuration.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct AudioEncoderConfig {
|
||||
/// Encoder ID (usually "ffmpeg_aac").
|
||||
pub encoder_id: String,
|
||||
/// Bitrate in kbps.
|
||||
pub bitrate: u32,
|
||||
/// Sample rate in Hz.
|
||||
pub sample_rate: u32,
|
||||
/// Number of channels.
|
||||
pub channels: u32,
|
||||
}
|
||||
|
||||
impl Default for AudioEncoderConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
encoder_id: "ffmpeg_aac".to_string(),
|
||||
bitrate: 192,
|
||||
sample_rate: 48000,
|
||||
channels: 2,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Audio encoder trait.
|
||||
pub trait AudioEncoder {
|
||||
/// Get the encoder ID.
|
||||
fn id(&self) -> &str;
|
||||
|
||||
/// Get the current bitrate.
|
||||
fn bitrate(&self) -> u32;
|
||||
|
||||
/// Update the bitrate.
|
||||
fn set_bitrate(&mut self, bitrate: u32);
|
||||
}
|
||||
|
||||
/// Detect available hardware encoders.
|
||||
pub fn detect_hardware_encoders() -> Vec<EncoderCapability> {
|
||||
let mut capabilities = Vec::new();
|
||||
|
||||
// Note: Actual detection would query the system for GPU availability
|
||||
// For now, we check environment variables and common indicators
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
// Check for NVIDIA
|
||||
if std::path::Path::new("/dev/nvidia0").exists() {
|
||||
capabilities.push(EncoderCapability::Nvenc);
|
||||
}
|
||||
|
||||
// Check for AMD
|
||||
if std::path::Path::new("/sys/class/drm").exists() {
|
||||
// Would check for AMD GPU
|
||||
// capabilities.push(EncoderCapability::Amf);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
// On Windows, would use DXGI to detect GPUs
|
||||
// For now, assume software encoding
|
||||
}
|
||||
|
||||
// Always available
|
||||
capabilities.push(EncoderCapability::Software);
|
||||
|
||||
capabilities
|
||||
}
|
||||
|
||||
/// Encoder capability.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum EncoderCapability {
|
||||
/// NVIDIA NVENC.
|
||||
Nvenc,
|
||||
/// AMD AMF.
|
||||
Amf,
|
||||
/// Intel QuickSync.
|
||||
QuickSync,
|
||||
/// Software (x264).
|
||||
Software,
|
||||
}
|
||||
|
||||
impl EncoderCapability {
|
||||
/// Get the recommended encoder preset for this capability.
|
||||
pub fn recommended_preset(&self, quality: QualityLevel) -> EncoderPreset {
|
||||
let bitrate = quality.recommended_bitrate();
|
||||
|
||||
match self {
|
||||
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_encoder_config_from_nvenc_preset() {
|
||||
let preset = EncoderPreset::Nvenc {
|
||||
bitrate: 8000,
|
||||
cq_level: 20,
|
||||
two_pass: true,
|
||||
};
|
||||
|
||||
let config = EncoderConfig::from_preset(&preset);
|
||||
|
||||
assert_eq!(config.encoder_id, "jim_nvenc");
|
||||
assert_eq!(config.bitrate, 8000);
|
||||
assert!(config.is_hardware());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encoder_config_from_x264_preset() {
|
||||
let preset = EncoderPreset::X264 {
|
||||
preset: "fast".to_string(),
|
||||
bitrate: 6000,
|
||||
crf: Some(23),
|
||||
};
|
||||
|
||||
let config = EncoderConfig::from_preset(&preset);
|
||||
|
||||
assert_eq!(config.encoder_id, "x264");
|
||||
assert!(!config.is_hardware());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detect_hardware_encoders() {
|
||||
let capabilities = detect_hardware_encoders();
|
||||
assert!(!capabilities.is_empty());
|
||||
assert!(capabilities.contains(&EncoderCapability::Software));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user