use crate::proc::hidden_command; use std::fmt; use std::io::Write; use std::path::PathBuf; use std::process::{Child, Stdio}; use std::thread; use std::time::{Duration, Instant}; #[derive(Debug, Clone, PartialEq, Eq)] pub enum CaptureSource { GameWindow { process_name: String, window_title: String, }, Monitor { display_id: String, }, Desktop, } #[derive(Debug, Clone, PartialEq, Eq)] pub enum EncoderPreference { HardwareH264, SoftwareH264, } #[derive(Debug, Clone, PartialEq, Eq)] pub enum CaptureMethod { DesktopDuplication, GdiGrab, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct CaptureConfig { pub source: CaptureSource, pub method: CaptureMethod, pub width: u32, pub height: u32, pub fps: u32, pub bitrate_kbps: u32, pub encoder: EncoderPreference, pub system_audio_enabled: bool, pub mic_enabled: bool, pub system_audio_device: Option, pub mic_device: Option, pub separate_audio_tracks: bool, } #[derive(Debug, Clone, PartialEq, Eq)] pub enum CaptureError { UnsupportedPlatform, DeviceUnavailable(String), PermissionDenied(String), EncoderUnavailable(String), } impl fmt::Display for CaptureError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::UnsupportedPlatform => { f.write_str("capture is currently implemented for Windows only") } Self::DeviceUnavailable(message) => write!(f, "capture device unavailable: {message}"), Self::PermissionDenied(message) => write!(f, "capture permission denied: {message}"), Self::EncoderUnavailable(message) => write!(f, "encoder unavailable: {message}"), } } } pub trait CaptureBackend { fn name(&self) -> &'static str; fn start(&mut self, config: CaptureConfig) -> Result<(), CaptureError>; fn stop(&mut self) -> Result<(), CaptureError>; } #[derive(Debug, Default)] pub struct WindowsCaptureBackend { active: bool, } impl CaptureBackend for WindowsCaptureBackend { fn name(&self) -> &'static str { "windows-graphics-capture" } fn start(&mut self, _config: CaptureConfig) -> Result<(), CaptureError> { #[cfg(target_os = "windows")] { self.active = true; Ok(()) } #[cfg(not(target_os = "windows"))] { Err(CaptureError::UnsupportedPlatform) } } fn stop(&mut self) -> Result<(), CaptureError> { self.active = false; Ok(()) } } pub fn find_ffmpeg_executable() -> Option { let bundled = std::env::current_exe() .ok() .and_then(|path| path.parent().map(|parent| parent.to_path_buf())) .map(|parent| parent.join("ffmpeg-x86_64-pc-windows-msvc.exe")); if let Some(path) = bundled { if path.exists() { return Some(path); } } let candidates = ["ffmpeg.exe", "ffmpeg"]; for candidate in candidates { if hidden_command(candidate).arg("-version").output().is_ok() { return Some(PathBuf::from(candidate)); } } None } pub fn ffmpeg_is_available() -> bool { find_ffmpeg_executable().is_some() } pub fn ffmpeg_supports_input_device(executable: &std::path::Path, device: &str) -> bool { let Ok(output) = hidden_command(executable) .args(["-hide_banner", "-devices"]) .output() else { return false; }; let combined = format!( "{}\n{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr) ); combined .lines() .any(|line| line.contains('D') && line.split_whitespace().any(|part| part == device)) } pub fn ffmpeg_supports_filter(executable: &std::path::Path, filter: &str) -> bool { let Ok(output) = hidden_command(executable) .args(["-hide_banner", "-filters"]) .output() else { return false; }; let combined = format!( "{}\n{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr) ); combined .lines() .any(|line| line.split_whitespace().any(|part| part == filter)) } /// Returns true when the single-track native WGC encoder can satisfy the requested audio /// configuration. windows-capture exposes one mixed audio channel, so distinct system/mic /// tracks are only achievable through the FFmpeg backend and route there instead. pub fn native_capture_can_honor_audio( system_audio_enabled: bool, mic_enabled: bool, separate_audio_tracks: bool, native_system_audio_available: bool, native_mic_available: bool, ) -> bool { if system_audio_enabled && mic_enabled && separate_audio_tracks { return false; } (!system_audio_enabled || native_system_audio_available) && (!mic_enabled || native_mic_available) } #[derive(Debug, Clone, PartialEq, Eq)] pub struct FfmpegRecordingPlan { pub executable: PathBuf, pub args: Vec, } impl FfmpegRecordingPlan { pub fn for_windows_desktop( executable: impl Into, output: impl Into, config: &CaptureConfig, ) -> Self { let output = output.into(); let mut args = base_desktop_input_args(config); args.extend([ "-movflags".to_string(), "+faststart".to_string(), output.display().to_string(), ]); Self { executable: executable.into(), args, } } pub fn for_windows_desktop_segments( executable: impl Into, output_pattern: impl Into, config: &CaptureConfig, segment_duration: Duration, ) -> Self { let output_pattern = output_pattern.into(); let mut args = base_desktop_input_args(config); args.extend([ "-f".to_string(), "segment".to_string(), "-segment_time".to_string(), segment_duration.as_secs().max(1).to_string(), "-reset_timestamps".to_string(), "1".to_string(), "-segment_format".to_string(), "mp4".to_string(), output_pattern.display().to_string(), ]); Self { executable: executable.into(), args, } } } fn base_desktop_input_args(config: &CaptureConfig) -> Vec { let preset = match config.encoder { EncoderPreference::HardwareH264 => "h264_mf", EncoderPreference::SoftwareH264 => "libx264", }; let size = format!("{}x{}", config.width, config.height); let fps = config.fps.to_string(); let bitrate = format!("{}k", config.bitrate_kbps); let separate_tracks = config.system_audio_enabled && config.mic_enabled && config.separate_audio_tracks; let mut args = vec![ "-y".to_string(), "-hide_banner".to_string(), "-loglevel".to_string(), "error".to_string(), ]; match config.method { CaptureMethod::DesktopDuplication => args.extend([ "-f".to_string(), "lavfi".to_string(), "-i".to_string(), format!("ddagrab=framerate={fps}:video_size={size},hwdownload,format=bgra"), ]), CaptureMethod::GdiGrab => args.extend([ "-f".to_string(), "gdigrab".to_string(), "-framerate".to_string(), fps, "-video_size".to_string(), size, "-i".to_string(), "desktop".to_string(), ]), } let mut next_input_index = 1; let mut audio_input_indices = Vec::new(); if config.system_audio_enabled { args.extend([ "-f".to_string(), "wasapi".to_string(), "-i".to_string(), config .system_audio_device .clone() .unwrap_or_else(|| "default".to_string()), ]); audio_input_indices.push(next_input_index); next_input_index += 1; } if config.mic_enabled { args.extend([ "-f".to_string(), "dshow".to_string(), "-i".to_string(), format!( "audio={}", config .mic_device .clone() .unwrap_or_else(|| "Microphone".to_string()) ), ]); audio_input_indices.push(next_input_index); } args.extend([ "-map".to_string(), "0:v:0".to_string(), "-c:v".to_string(), preset.to_string(), "-b:v".to_string(), bitrate, "-pix_fmt".to_string(), "yuv420p".to_string(), ]); if separate_tracks { // Map each audio input to a separate track with its own encoder settings for (idx, &input_idx) in audio_input_indices.iter().enumerate() { args.extend([ "-map".to_string(), format!("{}:a?", input_idx), format!("-c:a:{idx}"), "aac".to_string(), format!("-b:a:{idx}"), "160k".to_string(), ]); } } else if config.system_audio_enabled || config.mic_enabled { // Legacy: mix all audio into single track for input_idx in audio_input_indices { args.extend(["-map".to_string(), format!("{}:a?", input_idx)]); } args.extend([ "-c:a".to_string(), "aac".to_string(), "-b:a".to_string(), "160k".to_string(), ]); } args } #[derive(Debug)] pub struct FfmpegCaptureBackend { executable: PathBuf, output: PathBuf, child: Option, } impl FfmpegCaptureBackend { pub fn new(executable: impl Into, output: impl Into) -> Self { Self { executable: executable.into(), output: output.into(), child: None, } } pub fn plan(&self, config: &CaptureConfig) -> FfmpegRecordingPlan { FfmpegRecordingPlan::for_windows_desktop(&self.executable, &self.output, config) } } impl CaptureBackend for FfmpegCaptureBackend { fn name(&self) -> &'static str { "ffmpeg-gdigrab" } fn start(&mut self, config: CaptureConfig) -> Result<(), CaptureError> { if self.child.is_some() { return Ok(()); } let plan = self.plan(&config); let child = hidden_command(&plan.executable) .args(&plan.args) .stdin(Stdio::piped()) .stdout(Stdio::null()) .stderr(Stdio::null()) .spawn() .map_err(|error| CaptureError::DeviceUnavailable(error.to_string()))?; self.child = Some(child); Ok(()) } fn stop(&mut self) -> Result<(), CaptureError> { if let Some(mut child) = self.child.take() { if let Some(stdin) = child.stdin.as_mut() { let _ = stdin.write_all(b"q\n"); } let deadline = Instant::now() + Duration::from_secs(5); loop { if child .try_wait() .map_err(|error| CaptureError::DeviceUnavailable(error.to_string()))? .is_some() { break; } if Instant::now() >= deadline { let _ = child.kill(); let _ = child.wait(); break; } thread::sleep(Duration::from_millis(100)); } } Ok(()) } } impl Drop for FfmpegCaptureBackend { fn drop(&mut self) { let _ = self.stop(); } } #[derive(Debug)] pub struct FfmpegReplayCaptureBackend { executable: PathBuf, output_pattern: PathBuf, segment_duration: Duration, child: Option, } impl FfmpegReplayCaptureBackend { pub fn new( executable: impl Into, output_pattern: impl Into, segment_duration: Duration, ) -> Self { Self { executable: executable.into(), output_pattern: output_pattern.into(), segment_duration, child: None, } } pub fn plan(&self, config: &CaptureConfig) -> FfmpegRecordingPlan { FfmpegRecordingPlan::for_windows_desktop_segments( &self.executable, &self.output_pattern, config, self.segment_duration, ) } } impl CaptureBackend for FfmpegReplayCaptureBackend { fn name(&self) -> &'static str { "ffmpeg-gdigrab-segments" } fn start(&mut self, config: CaptureConfig) -> Result<(), CaptureError> { if self.child.is_some() { return Ok(()); } self.child = Some(spawn_ffmpeg(&self.plan(&config))?); if child_exited(&mut self.child)? && config.method == CaptureMethod::DesktopDuplication { let mut fallback_config = config; fallback_config.method = CaptureMethod::GdiGrab; self.child = Some(spawn_ffmpeg(&self.plan(&fallback_config))?); } Ok(()) } fn stop(&mut self) -> Result<(), CaptureError> { stop_ffmpeg_child(&mut self.child) } } impl Drop for FfmpegReplayCaptureBackend { fn drop(&mut self) { let _ = self.stop(); } } fn spawn_ffmpeg(plan: &FfmpegRecordingPlan) -> Result { hidden_command(&plan.executable) .args(&plan.args) .stdin(Stdio::piped()) .stdout(Stdio::null()) .stderr(Stdio::null()) .spawn() .map_err(|error| CaptureError::DeviceUnavailable(error.to_string())) } fn child_exited(child: &mut Option) -> Result { thread::sleep(Duration::from_millis(500)); let Some(running_child) = child.as_mut() else { return Ok(false); }; if running_child .try_wait() .map_err(|error| CaptureError::DeviceUnavailable(error.to_string()))? .is_some() { *child = None; Ok(true) } else { Ok(false) } } fn stop_ffmpeg_child(child: &mut Option) -> Result<(), CaptureError> { if let Some(mut child) = child.take() { if let Some(stdin) = child.stdin.as_mut() { let _ = stdin.write_all(b"q\n"); } let deadline = Instant::now() + Duration::from_secs(5); loop { if child .try_wait() .map_err(|error| CaptureError::DeviceUnavailable(error.to_string()))? .is_some() { break; } if Instant::now() >= deadline { let _ = child.kill(); let _ = child.wait(); break; } thread::sleep(Duration::from_millis(100)); } } Ok(()) } #[cfg(test)] mod tests { use super::*; #[test] fn ffmpeg_plan_prefers_media_foundation_h264() { let config = CaptureConfig { source: CaptureSource::Desktop, method: CaptureMethod::GdiGrab, width: 1280, height: 720, fps: 30, bitrate_kbps: 6000, encoder: EncoderPreference::HardwareH264, system_audio_enabled: false, mic_enabled: false, system_audio_device: None, mic_device: None, separate_audio_tracks: false, }; let plan = FfmpegRecordingPlan::for_windows_desktop("ffmpeg", "out.mp4", &config); assert!(plan.args.contains(&"h264_mf".to_string())); assert!(plan.args.contains(&"1280x720".to_string())); assert!(plan.args.contains(&"6000k".to_string())); } #[test] fn ffmpeg_segment_plan_uses_segment_muxer() { let config = CaptureConfig { source: CaptureSource::Desktop, method: CaptureMethod::DesktopDuplication, width: 1280, height: 720, fps: 30, bitrate_kbps: 6000, encoder: EncoderPreference::HardwareH264, system_audio_enabled: false, mic_enabled: false, system_audio_device: None, mic_device: None, separate_audio_tracks: false, }; let plan = FfmpegRecordingPlan::for_windows_desktop_segments( "ffmpeg", "segment-%05d.mp4", &config, Duration::from_secs(5), ); assert!(plan.args.contains(&"segment".to_string())); assert!(plan.args.iter().any(|arg| arg.contains("ddagrab"))); assert!(plan.args.contains(&"segment-%05d.mp4".to_string())); } #[test] fn device_parser_ignores_unknown_executable() { assert!(!ffmpeg_supports_input_device( PathBuf::from("definitely-missing-ffmpeg.exe").as_path(), "wasapi" )); } #[test] fn separate_tracks_map_distinct_audio_inputs() { let config = CaptureConfig { source: CaptureSource::Desktop, method: CaptureMethod::DesktopDuplication, width: 1280, height: 720, fps: 30, bitrate_kbps: 6000, encoder: EncoderPreference::HardwareH264, system_audio_enabled: true, mic_enabled: true, system_audio_device: None, mic_device: Some("Microphone".to_string()), separate_audio_tracks: true, }; let plan = FfmpegRecordingPlan::for_windows_desktop_segments( "ffmpeg", "segment-%05d.mp4", &config, Duration::from_secs(5), ); assert!(plan.args.iter().any(|arg| arg == "-map")); assert!(plan.args.windows(2).any(|pair| pair == ["-map", "1:a?"])); assert!(plan.args.windows(2).any(|pair| pair == ["-map", "2:a?"])); assert!(plan.args.iter().any(|arg| arg == "-c:a:0")); assert!(plan.args.iter().any(|arg| arg == "-c:a:1")); assert!(plan.args.iter().any(|arg| arg == "wasapi")); assert!(plan.args.iter().any(|arg| arg.contains("Microphone"))); assert!(!plan.args.windows(3).any(|triple| triple == ["-c:a", "aac", "-b:a"])); } #[test] fn legacy_mixed_audio_keeps_single_track_encoding_when_separate_tracks_off() { let config = CaptureConfig { source: CaptureSource::Desktop, method: CaptureMethod::GdiGrab, width: 1280, height: 720, fps: 30, bitrate_kbps: 6000, encoder: EncoderPreference::HardwareH264, system_audio_enabled: true, mic_enabled: true, system_audio_device: None, mic_device: Some("Microphone".to_string()), separate_audio_tracks: false, }; let plan = FfmpegRecordingPlan::for_windows_desktop("ffmpeg", "out.mp4", &config); assert!(plan.args.windows(2).any(|pair| pair == ["-map", "1:a?"])); assert!(plan.args.windows(2).any(|pair| pair == ["-map", "2:a?"])); assert!(plan.args.windows(3).any(|triple| triple == ["-c:a", "aac", "-b:a"])); assert!(!plan.args.iter().any(|arg| arg == "-c:a:0")); } #[test] fn native_capture_cannot_honor_separate_tracks() { assert!(!native_capture_can_honor_audio(true, true, true, true, true)); } #[test] fn native_capture_honors_single_source_combinations() { assert!(native_capture_can_honor_audio(true, false, true, true, false)); assert!(native_capture_can_honor_audio(false, true, true, false, true)); assert!(native_capture_can_honor_audio(true, true, false, true, true)); } #[test] fn native_capture_requires_available_native_sources() { assert!(!native_capture_can_honor_audio(true, false, false, false, true)); assert!(!native_capture_can_honor_audio(false, true, false, true, false)); assert!(native_capture_can_honor_audio(false, false, true, false, false)); } }