record-daemon: fix obs recording

This commit is contained in:
vhaudiquet committed 2026-03-20 20:34:44 +01:00
1 parent dbb224e118
commit 1166424c29
12 files changed
+3717 -515

No files matched your search

+222 -52
View File
@@ -1,4 +1,7 @@
//! Video and audio encoder configuration.
//!
//! This module provides encoder configuration types that integrate with
//! libobs-simple for hardware and software encoding.
use serde::{Deserialize, Serialize};
@@ -17,6 +20,22 @@ pub struct EncoderConfig {
pub settings: EncoderSettings,
}
impl Default for EncoderConfig {
fn default() -> Self {
Self {
encoder_id: "jim_nvenc".to_string(),
bitrate: 6000,
keyframe_interval: 2,
settings: EncoderSettings::Nvenc {
cq_level: 20,
two_pass: true,
preset: "p4".to_string(),
rate_control: NvencRateControl::Vbr,
},
}
}
}
/// Encoder-specific settings.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
@@ -133,21 +152,56 @@ impl EncoderConfig {
EncoderSettings::Nvenc { .. } | EncoderSettings::Amf { .. }
)
}
}
/// Video encoder trait for abstraction over different encoders.
pub trait VideoEncoder {
/// Get the encoder ID.
fn id(&self) -> &str;
/// Check if this is an NVIDIA encoder.
pub fn is_nvenc(&self) -> bool {
matches!(self.settings, EncoderSettings::Nvenc { .. })
}
/// Get the current bitrate.
fn bitrate(&self) -> u32;
/// Check if this is an AMD encoder.
pub fn is_amf(&self) -> bool {
matches!(self.settings, EncoderSettings::Amf { .. })
}
/// Update the bitrate.
fn set_bitrate(&mut self, bitrate: u32);
/// Check if this is a software encoder.
pub fn is_software(&self) -> bool {
matches!(self.settings, EncoderSettings::X264 { .. })
}
/// Get encoder-specific settings as JSON.
fn settings_json(&self) -> serde_json::Value;
/// Get the hardware codec type for libobs-simple.
pub fn hardware_codec(&self) -> Option<libobs_simple::output::simple::HardwareCodec> {
use libobs_simple::output::simple::HardwareCodec;
match &self.settings {
EncoderSettings::Nvenc { .. } => Some(HardwareCodec::H264),
EncoderSettings::Amf { .. } => Some(HardwareCodec::H264),
EncoderSettings::X264 { .. } => None,
}
}
/// Get the hardware preset for libobs-simple.
pub fn hardware_preset(&self) -> libobs_simple::output::simple::HardwarePreset {
use libobs_simple::output::simple::HardwarePreset;
match &self.settings {
EncoderSettings::Nvenc { preset, .. } => match preset.as_str() {
"p1" => HardwarePreset::Speed,
"p2" => HardwarePreset::Speed,
"p3" => HardwarePreset::Balanced,
"p4" => HardwarePreset::Balanced,
"p5" => HardwarePreset::Quality,
"p6" => HardwarePreset::Quality,
"p7" => HardwarePreset::Quality,
_ => HardwarePreset::Balanced,
},
EncoderSettings::Amf { quality, .. } => match quality {
AmfQuality::Speed => HardwarePreset::Speed,
AmfQuality::Balanced => HardwarePreset::Balanced,
AmfQuality::Quality => HardwarePreset::Quality,
},
EncoderSettings::X264 { .. } => HardwarePreset::Balanced,
}
}
}
/// Audio encoder configuration.
@@ -174,49 +228,14 @@ impl Default for AudioEncoderConfig {
}
}
/// 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);
impl AudioEncoderConfig {
/// Create a new audio encoder config with the specified bitrate.
pub fn with_bitrate(bitrate: u32) -> Self {
Self {
bitrate,
..Default::default()
}
}
#[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.
@@ -259,6 +278,142 @@ impl EncoderCapability {
},
}
}
/// Get a human-readable name for this encoder capability.
pub fn name(&self) -> &'static str {
match self {
EncoderCapability::Nvenc => "NVIDIA NVENC",
EncoderCapability::Amf => "AMD AMF",
EncoderCapability::QuickSync => "Intel QuickSync",
EncoderCapability::Software => "Software (x264)",
}
}
}
/// Detect available hardware encoders.
///
/// This function checks the system for available GPU encoders.
pub fn detect_hardware_encoders() -> Vec<EncoderCapability> {
use std::io::Write;
use tracing::info;
info!("[ENCODER_DETECT] Starting hardware encoder detection...");
std::io::stderr().flush().ok();
let mut capabilities = Vec::new();
#[cfg(target_os = "linux")]
{
// Check for NVIDIA
if std::path::Path::new("/dev/nvidia0").exists() {
info!("[ENCODER_DETECT] Found NVIDIA device");
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, check for NVIDIA first
// Try to load nvenc DLL
info!("[ENCODER_DETECT] Checking for NVENC...");
std::io::stderr().flush().ok();
if is_nvenc_available() {
info!("[ENCODER_DETECT] NVENC available");
capabilities.push(EncoderCapability::Nvenc);
} else {
info!("[ENCODER_DETECT] NVENC not available");
}
// Check for AMD AMF
info!("[ENCODER_DETECT] Checking for AMF...");
std::io::stderr().flush().ok();
if is_amf_available() {
info!("[ENCODER_DETECT] AMF available");
capabilities.push(EncoderCapability::Amf);
} else {
info!("[ENCODER_DETECT] AMF not available");
}
// Check for Intel QuickSync
info!("[ENCODER_DETECT] Checking for QuickSync...");
std::io::stderr().flush().ok();
if is_quicksync_available() {
info!("[ENCODER_DETECT] QuickSync available");
capabilities.push(EncoderCapability::QuickSync);
} else {
info!("[ENCODER_DETECT] QuickSync not available");
}
}
// Always available
info!("[ENCODER_DETECT] Software encoder always available");
capabilities.push(EncoderCapability::Software);
info!("[ENCODER_DETECT] Detected encoders: {:?}", capabilities);
std::io::stderr().flush().ok();
capabilities
}
/// Check if NVIDIA NVENC is available.
#[cfg(target_os = "windows")]
fn is_nvenc_available() -> bool {
// Check for NVENC DLL
std::path::Path::new("C:\\Windows\\System32\\nvEncMFTH264.dll").exists()
|| std::path::Path::new("C:\\Windows\\System32\\nvEncMFTH265.dll").exists()
}
#[cfg(not(target_os = "windows"))]
fn is_nvenc_available() -> bool {
false
}
/// Check if AMD AMF is available.
#[cfg(target_os = "windows")]
fn is_amf_available() -> bool {
// Check for AMF runtime
std::path::Path::new("C:\\Windows\\System32\\amdocl64.dll").exists()
}
#[cfg(not(target_os = "windows"))]
fn is_amf_available() -> bool {
false
}
/// Check if Intel QuickSync is available.
#[cfg(target_os = "windows")]
fn is_quicksync_available() -> bool {
// Check for Intel Media SDK
std::path::Path::new("C:\\Windows\\System32\\mfx64.dll").exists()
}
#[cfg(not(target_os = "windows"))]
fn is_quicksync_available() -> bool {
false
}
/// Get the best available encoder capability.
pub fn best_available_encoder() -> EncoderCapability {
let capabilities = detect_hardware_encoders();
// Prefer hardware encoders
for cap in &capabilities {
if matches!(
cap,
EncoderCapability::Nvenc | EncoderCapability::Amf | EncoderCapability::QuickSync
) {
return *cap;
}
}
// Fall back to software
EncoderCapability::Software
}
#[cfg(test)]
@@ -278,6 +433,7 @@ mod tests {
assert_eq!(config.encoder_id, "jim_nvenc");
assert_eq!(config.bitrate, 8000);
assert!(config.is_hardware());
assert!(config.is_nvenc());
}
#[test]
@@ -292,6 +448,7 @@ mod tests {
assert_eq!(config.encoder_id, "x264");
assert!(!config.is_hardware());
assert!(config.is_software());
}
#[test]
@@ -300,4 +457,17 @@ mod tests {
assert!(!capabilities.is_empty());
assert!(capabilities.contains(&EncoderCapability::Software));
}
#[test]
fn test_best_available_encoder() {
let best = best_available_encoder();
// Should always return something
assert!(matches!(
best,
EncoderCapability::Nvenc
| EncoderCapability::Amf
| EncoderCapability::QuickSync
| EncoderCapability::Software
));
}
}