From bde7d78c3dcdacae5cbb0a2706a06324b0aee526 Mon Sep 17 00:00:00 2001 From: Alvin-Zilverstand <524715@vistacollege.nl> Date: Mon, 14 Sep 2026 13:51:31 +0200 Subject: [PATCH] fix: stop status polling from spawning FFmpeg and hide subprocess console windows --- docs/implementation-status.md | 1 + src-tauri/src/main.rs | 92 +++++++++++++++++++++++------------ src/audio.rs | 4 +- src/capture.rs | 13 ++--- src/lib.rs | 1 + src/media.rs | 8 +-- src/proc.rs | 16 ++++++ src/screenshot.rs | 4 +- ui/src/main.ts | 4 +- 9 files changed, 95 insertions(+), 48 deletions(-) create mode 100644 src/proc.rs diff --git a/docs/implementation-status.md b/docs/implementation-status.md index cb4c320..ccf2694 100644 --- a/docs/implementation-status.md +++ b/docs/implementation-status.md @@ -52,6 +52,7 @@ - The Settings screen shows the current version, checks for updates, and offers a Download & install button when a newer release exists. - First-run onboarding wizard walks new installs through privacy/audio, capture-source, storage, and hotkey setup before showing the main app; it uses the defaults already saved in `settings.json` so existing users are unaffected. - The default clip library root is `~/Documents/ClipForge` (rather than the process working directory), and a first-run migration moves any legacy `clipforge-library` folder found in the CWD into the new location while rewriting stored clip paths in the SQLite database. +- Capture capabilities (bundled FFmpeg discovery, WASAPI/ddagrab support) are detected once at startup and cached, so background workers (`refresh_detected_game` every 2s, UI status poll every 3s) never spawn FFmpeg; all FFmpeg/console subprocesses are launched with a hidden console window on Windows, preventing the app from freezing and flashing a terminal repeatedly. - Vite TypeScript UI builds and has interactive Library, Recording, Auto Clip, Uploads, and Settings views. - Library search, event/upload filters, and grid/list switching are functional in the UI. - User-visible notices are shown for major actions and failures instead of relying only on console output. diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 6f89780..bffd826 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -3,7 +3,7 @@ use clipforge::audio::{list_ffmpeg_dshow_audio_inputs, AudioDeviceKind}; use clipforge::capture::{ - ffmpeg_is_available, ffmpeg_supports_filter, ffmpeg_supports_input_device, + ffmpeg_supports_filter, ffmpeg_supports_input_device, find_ffmpeg_executable, CaptureBackend, CaptureConfig, CaptureMethod, CaptureSource, EncoderPreference, FfmpegReplayCaptureBackend, }; @@ -25,6 +25,7 @@ use clipforge::native_audio::{ NativeAudioSource, }; use clipforge::native_wgc::NativeWgcReplayCaptureBackend; +use clipforge::proc::hidden_command; use clipforge::recorder::{RecorderAction, RecorderService}; use clipforge::settings::{load_or_create_settings, save_settings, AppSettings}; use clipforge::storage::{clip_path, is_inside_root, resolve_library_root, LibraryPaths}; @@ -52,6 +53,30 @@ struct AppRuntime { valve_events: Mutex>, hotkey_shortcuts: Mutex>, app_handle: Mutex>, + capture_capabilities: CaptureCapabilities, +} + +#[derive(Clone)] +struct CaptureCapabilities { + ffmpeg_path: Option, + system_audio_available: bool, + desktop_duplication_available: bool, +} + +fn detect_capture_capabilities() -> CaptureCapabilities { + let ffmpeg_path = find_ffmpeg_executable(); + let (system_audio_available, desktop_duplication_available) = match &ffmpeg_path { + Some(path) => ( + native_system_loopback_available() || ffmpeg_supports_input_device(path, "wasapi"), + ffmpeg_supports_filter(path, "ddagrab"), + ), + None => (native_system_loopback_available(), false), + }; + CaptureCapabilities { + ffmpeg_path, + system_audio_available, + desktop_duplication_available, + } } struct HotkeyShortcuts { @@ -194,7 +219,7 @@ struct AutoClipEventDto { fn get_status(runtime: State<'_, AppRuntime>) -> Result { let recorder = runtime.recorder.lock().map_err(|error| error.to_string())?; let capture = runtime.capture.lock().map_err(|error| error.to_string())?; - Ok(status_from_recorder(&recorder, capture.as_ref())) + Ok(status_from_recorder(&recorder, capture.as_ref(), &runtime.capture_capabilities)) } #[tauri::command] @@ -233,7 +258,7 @@ fn refresh_detected_game_inner(runtime: &AppRuntime) -> Result) -> Result) -> Result u32 { @@ -686,7 +711,7 @@ fn start_capture_inner(runtime: &AppRuntime) -> Result { let mut capture = runtime.capture.lock().map_err(|error| error.to_string())?; if capture.is_some() { - return Ok(status_from_recorder(&recorder, capture.as_ref())); + return Ok(status_from_recorder(&recorder, capture.as_ref(), &runtime.capture_capabilities)); } let storage = clipforge::storage::check_storage_space(&recorder.paths.buffer_root); @@ -780,7 +805,7 @@ fn start_capture_inner(runtime: &AppRuntime) -> Result { backend, }); - Ok(status_from_recorder(&recorder, capture.as_ref())) + Ok(status_from_recorder(&recorder, capture.as_ref(), &runtime.capture_capabilities)) } #[tauri::command] @@ -823,7 +848,7 @@ fn stop_capture_inner(runtime: &AppRuntime) -> Result { if matches!(recorder.state.status, RecordingStatus::RecordingSession) { recorder.toggle_session_recording(); } - Ok(status_from_recorder(&recorder, capture.as_ref())) + Ok(status_from_recorder(&recorder, capture.as_ref(), &runtime.capture_capabilities)) } #[tauri::command] @@ -864,7 +889,7 @@ fn reveal_clip(clip_id: String, runtime: State<'_, AppRuntime>) -> Result<(), St return Err(format!("Clip file does not exist: {}", clip.path.display())); } - Command::new("explorer") + hidden_command("explorer") .arg(format!("/select,{}", clip.path.display())) .spawn() .map_err(|error| error.to_string())?; @@ -1102,7 +1127,7 @@ fn take_screenshot(runtime: State<'_, AppRuntime>) -> Result Result<(), String> { if !path.exists() { return Err(format!("Screenshot file does not exist: {}", path.display())); } - Command::new("explorer") + hidden_command("explorer") .arg(format!("/select,{}", path.display())) .spawn() .map_err(|error| error.to_string())?; @@ -1130,7 +1155,7 @@ fn list_screenshots() -> Result, String> { fn reveal_crash_logs(runtime: State<'_, AppRuntime>) -> Result<(), String> { let logs_dir = runtime.library_root.join("logs"); fs::create_dir_all(&logs_dir).map_err(|error| error.to_string())?; - Command::new("explorer") + hidden_command("explorer") .arg(logs_dir.display().to_string()) .spawn() .map_err(|error| error.to_string())?; @@ -1554,7 +1579,7 @@ fn set_hotkeys( register_hotkeys(&app_handle, runtime.inner())?; } - Ok(status_from_recorder(&recorder, capture.as_ref())) + Ok(status_from_recorder(&recorder, capture.as_ref(), &runtime.capture_capabilities)) } fn main() { @@ -1573,6 +1598,7 @@ fn main() { valve_events: Mutex::new(Vec::new()), hotkey_shortcuts: Mutex::new(None), app_handle: Mutex::new(None), + capture_capabilities: detect_capture_capabilities(), }) .setup(|app| { let handle = app.handle().clone(); @@ -1864,7 +1890,11 @@ fn enforce_storage_cap(runtime: &AppRuntime) -> Result<(), String> { Ok(()) } -fn status_from_recorder(recorder: &RecorderService, capture: Option<&ActiveCapture>) -> DesktopStatus { +fn status_from_recorder( + recorder: &RecorderService, + capture: Option<&ActiveCapture>, + capabilities: &CaptureCapabilities, +) -> DesktopStatus { let game_id = recorder.state.detected_game_id.clone().unwrap_or_default(); let quality = recorder.settings.effective_quality_for(&game_id).clone(); DesktopStatus { @@ -1894,15 +1924,13 @@ fn status_from_recorder(recorder: &RecorderService, capture: Option<&ActiveCaptu capture_path: capture.map(|active| active.session_output_path.display().to_string()), clip_count: recorder.library.all().len(), library_root: recorder.paths.clip_root.display().to_string(), - ffmpeg_available: ffmpeg_is_available(), - ffmpeg_path: find_ffmpeg_executable().map(|path| path.display().to_string()), - system_audio_available: native_system_loopback_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), + ffmpeg_available: capabilities.ffmpeg_path.is_some(), + ffmpeg_path: capabilities + .ffmpeg_path + .as_ref() + .map(|path| path.display().to_string()), + system_audio_available: capabilities.system_audio_available, + desktop_duplication_available: capabilities.desktop_duplication_available, auto_clip_enabled_events: auto_clip_event_dtos(&recorder.settings), hotkey_clip_last_60s: recorder.settings.hotkeys.clip_last_60s.clone(), hotkey_clip_last_30s: recorder.settings.hotkeys.clip_last_30s.clone(), @@ -1970,7 +1998,7 @@ fn set_game_quality_override( .insert(game_id, preset); save_settings(&runtime.library_root, &recorder.settings) .map_err(|error| format!("Could not save quality override: {error}"))?; - Ok(status_from_recorder(&recorder, capture.as_ref())) + Ok(status_from_recorder(&recorder, capture.as_ref(), &runtime.capture_capabilities)) } #[tauri::command] @@ -1983,7 +2011,7 @@ fn clear_game_quality_override( recorder.settings.quality_overrides_by_game.remove(&game_id); save_settings(&runtime.library_root, &recorder.settings) .map_err(|error| format!("Could not save quality override: {error}"))?; - Ok(status_from_recorder(&recorder, capture.as_ref())) + Ok(status_from_recorder(&recorder, capture.as_ref(), &runtime.capture_capabilities)) } fn clip_to_dto(clip: &Clip) -> ClipDto { diff --git a/src/audio.rs b/src/audio.rs index 92fe45a..d1ebfb7 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -1,5 +1,5 @@ +use crate::proc::hidden_command; use std::path::Path; -use std::process::Command; #[derive(Debug, Clone, PartialEq, Eq)] pub enum AudioDeviceKind { @@ -14,7 +14,7 @@ pub struct AudioDevice { } pub fn list_ffmpeg_dshow_audio_inputs(ffmpeg: &Path) -> Vec { - let Ok(output) = Command::new(ffmpeg) + let Ok(output) = hidden_command(ffmpeg) .args([ "-hide_banner", "-list_devices", diff --git a/src/capture.rs b/src/capture.rs index 1ecfbdb..871d3c3 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -1,7 +1,8 @@ +use crate::proc::hidden_command; use std::fmt; use std::io::Write; use std::path::PathBuf; -use std::process::{Child, Command, Stdio}; +use std::process::{Child, Stdio}; use std::thread; use std::time::{Duration, Instant}; @@ -114,7 +115,7 @@ pub fn find_ffmpeg_executable() -> Option { let candidates = ["ffmpeg.exe", "ffmpeg"]; for candidate in candidates { - if Command::new(candidate).arg("-version").output().is_ok() { + if hidden_command(candidate).arg("-version").output().is_ok() { return Some(PathBuf::from(candidate)); } } @@ -127,7 +128,7 @@ pub fn ffmpeg_is_available() -> bool { } pub fn ffmpeg_supports_input_device(executable: &std::path::Path, device: &str) -> bool { - let Ok(output) = Command::new(executable) + let Ok(output) = hidden_command(executable) .args(["-hide_banner", "-devices"]) .output() else { @@ -144,7 +145,7 @@ pub fn ffmpeg_supports_input_device(executable: &std::path::Path, device: &str) } pub fn ffmpeg_supports_filter(executable: &std::path::Path, filter: &str) -> bool { - let Ok(output) = Command::new(executable) + let Ok(output) = hidden_command(executable) .args(["-hide_banner", "-filters"]) .output() else { @@ -368,7 +369,7 @@ impl CaptureBackend for FfmpegCaptureBackend { } let plan = self.plan(&config); - let child = Command::new(&plan.executable) + let child = hidden_command(&plan.executable) .args(&plan.args) .stdin(Stdio::piped()) .stdout(Stdio::null()) @@ -474,7 +475,7 @@ impl Drop for FfmpegReplayCaptureBackend { } fn spawn_ffmpeg(plan: &FfmpegRecordingPlan) -> Result { - Command::new(&plan.executable) + hidden_command(&plan.executable) .args(&plan.args) .stdin(Stdio::piped()) .stdout(Stdio::null()) diff --git a/src/lib.rs b/src/lib.rs index 1332544..871d3c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,7 @@ pub mod media; pub mod models; pub mod native_audio; pub mod native_wgc; +pub mod proc; pub mod recorder; pub mod settings; pub mod storage; diff --git a/src/media.rs b/src/media.rs index feb7075..1f6592d 100644 --- a/src/media.rs +++ b/src/media.rs @@ -1,7 +1,7 @@ +use crate::proc::hidden_command; use std::fs; use std::io; use std::path::{Path, PathBuf}; -use std::process::Command; use std::time::{Duration, SystemTime, UNIX_EPOCH}; #[derive(Debug, Clone, PartialEq, Eq)] @@ -96,7 +96,7 @@ pub fn concat_segments( } fs::write(&list_path, list)?; - let status = Command::new(ffmpeg) + let status = hidden_command(ffmpeg) .args([ "-y", "-hide_banner", @@ -137,7 +137,7 @@ pub fn trim_clip( fs::create_dir_all(parent)?; } - let status = Command::new(ffmpeg) + let status = hidden_command(ffmpeg) .args([ "-y", "-hide_banner", @@ -168,7 +168,7 @@ pub fn generate_thumbnail(ffmpeg: &Path, input: &Path, output: &Path) -> Result< fs::create_dir_all(parent)?; } - let status = Command::new(ffmpeg) + let status = hidden_command(ffmpeg) .args([ "-y", "-hide_banner", diff --git a/src/proc.rs b/src/proc.rs new file mode 100644 index 0000000..7cab6ee --- /dev/null +++ b/src/proc.rs @@ -0,0 +1,16 @@ +use std::path::Path; +use std::process::Command; + +/// Builds a `Command` that never shows a console window on Windows. FFmpeg and other +/// console subprocesses otherwise flash a terminal window each time they start from a +/// GUI (Tauri) process, which looks like the app constantly opening and closing. +pub fn hidden_command(program: impl AsRef) -> Command { + let mut command = Command::new(program.as_ref()); + #[cfg(windows)] + { + use std::os::windows::process::CommandExt; + const CREATE_NO_WINDOW: u32 = 0x0800_0000; + command.creation_flags(CREATE_NO_WINDOW); + } + command +} \ No newline at end of file diff --git a/src/screenshot.rs b/src/screenshot.rs index d0d98a6..9c96970 100644 --- a/src/screenshot.rs +++ b/src/screenshot.rs @@ -1,7 +1,7 @@ use crate::capture::{CaptureConfig, CaptureError, CaptureMethod, CaptureSource, EncoderPreference}; +use crate::proc::hidden_command; use dirs; use std::path::PathBuf; -use std::process::Command; use std::time::{SystemTime, UNIX_EPOCH}; impl Default for ScreenshotCapture { @@ -145,7 +145,7 @@ impl ScreenshotCapture { output_path.display().to_string(), ]); - let status = Command::new(&self.executable) + let status = hidden_command(&self.executable) .args(&args) .status() .map_err(|e| CaptureError::DeviceUnavailable(e.to_string()))?; diff --git a/ui/src/main.ts b/ui/src/main.ts index b5f1fd9..224df48 100644 --- a/ui/src/main.ts +++ b/ui/src/main.ts @@ -1673,7 +1673,7 @@ async function saveAutoRecordSetting(enabled: boolean) { } async function refreshStatus() { - if (!tauriInvoke) { + if (!tauriInvoke || !state.onboardingComplete) { return; } @@ -1700,7 +1700,7 @@ async function refreshAudioDevices() { } async function pollAutoClips() { - if (!tauriInvoke || !state.captureActive) { + if (!tauriInvoke || !state.captureActive || !state.onboardingComplete) { return; }