Prefer Desktop Duplication capture when available
This commit is contained in:
1 parent
9ac5a30a17
commit
86a081b390
3 files changed
+96
-19
No files matched your search
+13
-2
@@ -1,6 +1,7 @@
|
|||||||
use clipforge::capture::{
|
use clipforge::capture::{
|
||||||
ffmpeg_is_available, ffmpeg_supports_input_device, find_ffmpeg_executable, CaptureBackend,
|
ffmpeg_is_available, ffmpeg_supports_filter, ffmpeg_supports_input_device,
|
||||||
CaptureConfig, CaptureSource, EncoderPreference, FfmpegReplayCaptureBackend,
|
find_ffmpeg_executable, CaptureBackend, CaptureConfig, CaptureMethod, CaptureSource,
|
||||||
|
EncoderPreference, FfmpegReplayCaptureBackend,
|
||||||
};
|
};
|
||||||
use clipforge::database::ClipDatabase;
|
use clipforge::database::ClipDatabase;
|
||||||
use clipforge::game_detection::{default_profiles, detect_game};
|
use clipforge::game_detection::{default_profiles, detect_game};
|
||||||
@@ -67,6 +68,7 @@ struct DesktopStatus {
|
|||||||
ffmpeg_available: bool,
|
ffmpeg_available: bool,
|
||||||
ffmpeg_path: Option<String>,
|
ffmpeg_path: Option<String>,
|
||||||
system_audio_available: bool,
|
system_audio_available: bool,
|
||||||
|
desktop_duplication_available: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
@@ -400,8 +402,14 @@ fn start_capture_inner(runtime: &AppRuntime) -> Result<DesktopStatus, String> {
|
|||||||
let segment_pattern = buffer_dir.join("segment-%05d.mp4");
|
let segment_pattern = buffer_dir.join("segment-%05d.mp4");
|
||||||
|
|
||||||
let system_audio_enabled = ffmpeg_supports_input_device(&ffmpeg, "wasapi");
|
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 {
|
let config = CaptureConfig {
|
||||||
source: CaptureSource::Desktop,
|
source: CaptureSource::Desktop,
|
||||||
|
method,
|
||||||
width: recorder.settings.quality.width,
|
width: recorder.settings.quality.width,
|
||||||
height: recorder.settings.quality.height,
|
height: recorder.settings.quality.height,
|
||||||
fps: recorder.settings.quality.fps,
|
fps: recorder.settings.quality.fps,
|
||||||
@@ -846,6 +854,9 @@ fn status_from_recorder(recorder: &RecorderService, capture: Option<&ActiveCaptu
|
|||||||
system_audio_available: find_ffmpeg_executable()
|
system_audio_available: find_ffmpeg_executable()
|
||||||
.map(|path| ffmpeg_supports_input_device(&path, "wasapi"))
|
.map(|path| ffmpeg_supports_input_device(&path, "wasapi"))
|
||||||
.unwrap_or(false),
|
.unwrap_or(false),
|
||||||
|
desktop_duplication_available: find_ffmpeg_executable()
|
||||||
|
.map(|path| ffmpeg_supports_filter(&path, "ddagrab"))
|
||||||
|
.unwrap_or(false),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+78
-17
@@ -23,9 +23,16 @@ pub enum EncoderPreference {
|
|||||||
SoftwareH264,
|
SoftwareH264,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub enum CaptureMethod {
|
||||||
|
DesktopDuplication,
|
||||||
|
GdiGrab,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct CaptureConfig {
|
pub struct CaptureConfig {
|
||||||
pub source: CaptureSource,
|
pub source: CaptureSource,
|
||||||
|
pub method: CaptureMethod,
|
||||||
pub width: u32,
|
pub width: u32,
|
||||||
pub height: u32,
|
pub height: u32,
|
||||||
pub fps: 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))
|
.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)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct FfmpegRecordingPlan {
|
pub struct FfmpegRecordingPlan {
|
||||||
pub executable: PathBuf,
|
pub executable: PathBuf,
|
||||||
@@ -202,15 +226,25 @@ fn base_desktop_input_args(config: &CaptureConfig) -> Vec<String> {
|
|||||||
"-hide_banner".to_string(),
|
"-hide_banner".to_string(),
|
||||||
"-loglevel".to_string(),
|
"-loglevel".to_string(),
|
||||||
"error".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 next_input_index = 1;
|
||||||
let mut audio_maps = Vec::new();
|
let mut audio_maps = Vec::new();
|
||||||
|
|
||||||
@@ -385,15 +419,12 @@ impl CaptureBackend for FfmpegReplayCaptureBackend {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let plan = self.plan(&config);
|
self.child = Some(spawn_ffmpeg(&self.plan(&config))?);
|
||||||
let child = Command::new(&plan.executable)
|
if child_exited(&mut self.child)? && config.method == CaptureMethod::DesktopDuplication {
|
||||||
.args(&plan.args)
|
let mut fallback_config = config;
|
||||||
.stdin(Stdio::piped())
|
fallback_config.method = CaptureMethod::GdiGrab;
|
||||||
.stdout(Stdio::null())
|
self.child = Some(spawn_ffmpeg(&self.plan(&fallback_config))?);
|
||||||
.stderr(Stdio::null())
|
}
|
||||||
.spawn()
|
|
||||||
.map_err(|error| CaptureError::DeviceUnavailable(error.to_string()))?;
|
|
||||||
self.child = Some(child);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -408,6 +439,33 @@ impl Drop for FfmpegReplayCaptureBackend {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn spawn_ffmpeg(plan: &FfmpegRecordingPlan) -> Result<Child, CaptureError> {
|
||||||
|
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<Child>) -> Result<bool, CaptureError> {
|
||||||
|
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<Child>) -> Result<(), CaptureError> {
|
fn stop_ffmpeg_child(child: &mut Option<Child>) -> Result<(), CaptureError> {
|
||||||
if let Some(mut child) = child.take() {
|
if let Some(mut child) = child.take() {
|
||||||
if let Some(stdin) = child.stdin.as_mut() {
|
if let Some(stdin) = child.stdin.as_mut() {
|
||||||
@@ -441,6 +499,7 @@ mod tests {
|
|||||||
fn ffmpeg_plan_prefers_media_foundation_h264() {
|
fn ffmpeg_plan_prefers_media_foundation_h264() {
|
||||||
let config = CaptureConfig {
|
let config = CaptureConfig {
|
||||||
source: CaptureSource::Desktop,
|
source: CaptureSource::Desktop,
|
||||||
|
method: CaptureMethod::GdiGrab,
|
||||||
width: 1280,
|
width: 1280,
|
||||||
height: 720,
|
height: 720,
|
||||||
fps: 30,
|
fps: 30,
|
||||||
@@ -463,6 +522,7 @@ mod tests {
|
|||||||
fn ffmpeg_segment_plan_uses_segment_muxer() {
|
fn ffmpeg_segment_plan_uses_segment_muxer() {
|
||||||
let config = CaptureConfig {
|
let config = CaptureConfig {
|
||||||
source: CaptureSource::Desktop,
|
source: CaptureSource::Desktop,
|
||||||
|
method: CaptureMethod::DesktopDuplication,
|
||||||
width: 1280,
|
width: 1280,
|
||||||
height: 720,
|
height: 720,
|
||||||
fps: 30,
|
fps: 30,
|
||||||
@@ -482,6 +542,7 @@ mod tests {
|
|||||||
);
|
);
|
||||||
|
|
||||||
assert!(plan.args.contains(&"segment".to_string()));
|
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()));
|
assert!(plan.args.contains(&"segment-%05d.mp4".to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ type DesktopStatus = {
|
|||||||
ffmpeg_available: boolean;
|
ffmpeg_available: boolean;
|
||||||
ffmpeg_path: string | null;
|
ffmpeg_path: string | null;
|
||||||
system_audio_available: boolean;
|
system_audio_available: boolean;
|
||||||
|
desktop_duplication_available: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
type ClipDto = {
|
type ClipDto = {
|
||||||
@@ -83,6 +84,7 @@ type AppState = {
|
|||||||
ffmpegAvailable: boolean;
|
ffmpegAvailable: boolean;
|
||||||
ffmpegPath: string | null;
|
ffmpegPath: string | null;
|
||||||
systemAudioAvailable: boolean;
|
systemAudioAvailable: boolean;
|
||||||
|
desktopDuplicationAvailable: boolean;
|
||||||
micEnabled: boolean;
|
micEnabled: boolean;
|
||||||
autoRecordEnabled: boolean;
|
autoRecordEnabled: boolean;
|
||||||
autoUpload: boolean;
|
autoUpload: boolean;
|
||||||
@@ -121,6 +123,7 @@ const state: AppState = {
|
|||||||
ffmpegAvailable: false,
|
ffmpegAvailable: false,
|
||||||
ffmpegPath: null,
|
ffmpegPath: null,
|
||||||
systemAudioAvailable: false,
|
systemAudioAvailable: false,
|
||||||
|
desktopDuplicationAvailable: false,
|
||||||
micEnabled: false,
|
micEnabled: false,
|
||||||
autoRecordEnabled: false,
|
autoRecordEnabled: false,
|
||||||
autoUpload: false,
|
autoUpload: false,
|
||||||
@@ -378,6 +381,7 @@ function renderRecordingView() {
|
|||||||
<p class="eyebrow">Capture</p>
|
<p class="eyebrow">Capture</p>
|
||||||
<h2>720p30 Performance</h2>
|
<h2>720p30 Performance</h2>
|
||||||
<p class="muted">Lightweight desktop capture uses bundled FFmpeg when system FFmpeg is missing.</p>
|
<p class="muted">Lightweight desktop capture uses bundled FFmpeg when system FFmpeg is missing.</p>
|
||||||
|
<p class="muted">${state.desktopDuplicationAvailable ? "Desktop Duplication capture available" : "Using GDI capture fallback"}</p>
|
||||||
<p class="muted">${state.ffmpegAvailable ? "FFmpeg ready" : "FFmpeg missing"}${state.ffmpegPath ? ` · ${state.ffmpegPath}` : ""}</p>
|
<p class="muted">${state.ffmpegAvailable ? "FFmpeg ready" : "FFmpeg missing"}${state.ffmpegPath ? ` · ${state.ffmpegPath}` : ""}</p>
|
||||||
<p class="muted">${state.capturePath ? `Writing ${state.capturePath}` : "No active capture file"}</p>
|
<p class="muted">${state.capturePath ? `Writing ${state.capturePath}` : "No active capture file"}</p>
|
||||||
</article>
|
</article>
|
||||||
@@ -638,6 +642,7 @@ function applyDesktopStatus(status: DesktopStatus) {
|
|||||||
state.ffmpegAvailable = status.ffmpeg_available;
|
state.ffmpegAvailable = status.ffmpeg_available;
|
||||||
state.ffmpegPath = status.ffmpeg_path;
|
state.ffmpegPath = status.ffmpeg_path;
|
||||||
state.systemAudioAvailable = status.system_audio_available;
|
state.systemAudioAvailable = status.system_audio_available;
|
||||||
|
state.desktopDuplicationAvailable = status.desktop_duplication_available;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function trimSelectedClip() {
|
async function trimSelectedClip() {
|
||||||
|
|||||||
Reference in new issue
Block a user