diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 1446b92..5f7a675 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,6 +1,7 @@ use clipforge::capture::{ - ffmpeg_is_available, ffmpeg_supports_input_device, find_ffmpeg_executable, CaptureBackend, - CaptureConfig, CaptureSource, EncoderPreference, FfmpegReplayCaptureBackend, + ffmpeg_is_available, ffmpeg_supports_filter, ffmpeg_supports_input_device, + find_ffmpeg_executable, CaptureBackend, CaptureConfig, CaptureMethod, CaptureSource, + EncoderPreference, FfmpegReplayCaptureBackend, }; use clipforge::database::ClipDatabase; use clipforge::game_detection::{default_profiles, detect_game}; @@ -67,6 +68,7 @@ struct DesktopStatus { ffmpeg_available: bool, ffmpeg_path: Option, system_audio_available: bool, + desktop_duplication_available: bool, } #[derive(Debug, Serialize)] @@ -400,8 +402,14 @@ fn start_capture_inner(runtime: &AppRuntime) -> Result { let segment_pattern = buffer_dir.join("segment-%05d.mp4"); let system_audio_enabled = ffmpeg_supports_input_device(&ffmpeg, "wasapi"); + let method = if ffmpeg_supports_filter(&ffmpeg, "ddagrab") { + CaptureMethod::DesktopDuplication + } else { + CaptureMethod::GdiGrab + }; let config = CaptureConfig { source: CaptureSource::Desktop, + method, width: recorder.settings.quality.width, height: recorder.settings.quality.height, fps: recorder.settings.quality.fps, @@ -846,6 +854,9 @@ fn status_from_recorder(recorder: &RecorderService, capture: Option<&ActiveCaptu system_audio_available: find_ffmpeg_executable() .map(|path| ffmpeg_supports_input_device(&path, "wasapi")) .unwrap_or(false), + desktop_duplication_available: find_ffmpeg_executable() + .map(|path| ffmpeg_supports_filter(&path, "ddagrab")) + .unwrap_or(false), } } diff --git a/src/capture.rs b/src/capture.rs index bb77650..a3d7026 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -23,9 +23,16 @@ pub enum EncoderPreference { 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, @@ -135,6 +142,23 @@ pub fn ffmpeg_supports_input_device(executable: &std::path::Path, device: &str) .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) = Command::new(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)) +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct FfmpegRecordingPlan { pub executable: PathBuf, @@ -202,15 +226,25 @@ fn base_desktop_input_args(config: &CaptureConfig) -> Vec { "-hide_banner".to_string(), "-loglevel".to_string(), "error".to_string(), - "-f".to_string(), - "gdigrab".to_string(), - "-framerate".to_string(), - fps, - "-video_size".to_string(), - size, - "-i".to_string(), - "desktop".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_maps = Vec::new(); @@ -385,15 +419,12 @@ impl CaptureBackend for FfmpegReplayCaptureBackend { return Ok(()); } - let plan = self.plan(&config); - let child = Command::new(&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); + 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(()) } @@ -408,6 +439,33 @@ impl Drop for FfmpegReplayCaptureBackend { } } +fn spawn_ffmpeg(plan: &FfmpegRecordingPlan) -> Result { + Command::new(&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() { @@ -441,6 +499,7 @@ mod tests { fn ffmpeg_plan_prefers_media_foundation_h264() { let config = CaptureConfig { source: CaptureSource::Desktop, + method: CaptureMethod::GdiGrab, width: 1280, height: 720, fps: 30, @@ -463,6 +522,7 @@ mod tests { fn ffmpeg_segment_plan_uses_segment_muxer() { let config = CaptureConfig { source: CaptureSource::Desktop, + method: CaptureMethod::DesktopDuplication, width: 1280, height: 720, fps: 30, @@ -482,6 +542,7 @@ mod tests { ); 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())); } diff --git a/ui/src/main.ts b/ui/src/main.ts index 9bb5cc5..a47b6d7 100644 --- a/ui/src/main.ts +++ b/ui/src/main.ts @@ -50,6 +50,7 @@ type DesktopStatus = { ffmpeg_available: boolean; ffmpeg_path: string | null; system_audio_available: boolean; + desktop_duplication_available: boolean; }; type ClipDto = { @@ -83,6 +84,7 @@ type AppState = { ffmpegAvailable: boolean; ffmpegPath: string | null; systemAudioAvailable: boolean; + desktopDuplicationAvailable: boolean; micEnabled: boolean; autoRecordEnabled: boolean; autoUpload: boolean; @@ -121,6 +123,7 @@ const state: AppState = { ffmpegAvailable: false, ffmpegPath: null, systemAudioAvailable: false, + desktopDuplicationAvailable: false, micEnabled: false, autoRecordEnabled: false, autoUpload: false, @@ -378,6 +381,7 @@ function renderRecordingView() {

Capture

720p30 Performance

Lightweight desktop capture uses bundled FFmpeg when system FFmpeg is missing.

+

${state.desktopDuplicationAvailable ? "Desktop Duplication capture available" : "Using GDI capture fallback"}

${state.ffmpegAvailable ? "FFmpeg ready" : "FFmpeg missing"}${state.ffmpegPath ? ` ยท ${state.ffmpegPath}` : ""}

${state.capturePath ? `Writing ${state.capturePath}` : "No active capture file"}

@@ -638,6 +642,7 @@ function applyDesktopStatus(status: DesktopStatus) { state.ffmpegAvailable = status.ffmpeg_available; state.ffmpegPath = status.ffmpeg_path; state.systemAudioAvailable = status.system_audio_available; + state.desktopDuplicationAvailable = status.desktop_duplication_available; } async function trimSelectedClip() {