diff --git a/app/README.md b/app/README.md index f237929..9ec784b 100644 --- a/app/README.md +++ b/app/README.md @@ -100,9 +100,10 @@ reading (`history::tests`), highlight clipping (`highlight::tests`) and formatti the channels left on. Recordings made before the split play as one mixed track. 8. OS integration: tray icon with open/hide/record/quit, launch at login through the Run key, single instance, and toasts on recording start and stop. -9. Installer: `packager.toml` targets NSIS and copies the daemon and its OBS files beside the - exe. `cargo install cargo-packager --locked && cargo packager --release`. The installer has - not been run on a clean VM yet. +9. Installer: `packager.toml` targets NSIS and copies the daemon, its OBS files and the VC++ + runtime beside the exe. `cargo install cargo-packager --locked && cargo packager --release`. + On the first run the app downloads ffmpeg (`--fetch-ffmpeg` does it from a shell), so clip + export is ready before the first clip. The installer has not been run on a clean VM yet. 10. Parity: `tauri-app` is still in the tree. Delete it once the items above are verified on a machine with a live League Client. diff --git a/app/src/app.rs b/app/src/app.rs index 3d5ff15..3581c06 100644 --- a/app/src/app.rs +++ b/app/src/app.rs @@ -7,6 +7,7 @@ use std::time::Duration; use rune::tok::type_; use rune::{Cx, El, Key, KeyEvent, Tx, col, el, row, text}; +use crate::export; use crate::history::{self, QueueCategory, Summary, Timeline}; use crate::ipc::{self, Daemon}; use crate::os; @@ -145,6 +146,9 @@ pub struct App { pub highlights_scroll: ui::ScrollBar, pub events_scroll: ui::ScrollBar, pub notice: Option, + /// ffmpeg being downloaded on the first run, and the whole percent it last reported. + pub ffmpeg_rx: Option>, + pub ffmpeg_pct: Option, pub autostart: bool, pub logs: String, pub pending_review: bool, @@ -189,6 +193,8 @@ impl App { highlights_scroll: ui::ScrollBar::new(), events_scroll: ui::ScrollBar::new(), notice: None, + ffmpeg_rx: None, + ffmpeg_pct: None, autostart, logs: String::new(), pending_review: false, @@ -201,6 +207,19 @@ impl App { if headless && !live { app.demo(); } + // Clip export runs on ffmpeg. Fetch it now, on the run that follows an install, so the + // first export does not sit behind a download. A hidden start (the login item) leaves it + // to the export itself. + let hidden = std::env::args().any(|arg| arg == "--hidden"); + if !headless && !hidden && export::missing() && !export::fetching() { + let (tx, rx) = channel(); + std::thread::spawn(move || { + let _ = export::fetch(move |event| { + let _ = tx.send(event); + }); + }); + app.ffmpeg_rx = Some(rx); + } app } @@ -299,6 +318,15 @@ impl App { }); } + /// Whether the notice on screen belongs to the download. It is updated in place; a notice + /// the app is showing for something else is not replaced by the progress. + fn ffmpeg_banner_showing(&self) -> bool { + match &self.notice { + None => true, + Some(notice) => notice.text.starts_with("Downloading ffmpeg"), + } + } + pub fn refresh_settings(&self) { self.daemon.get_settings(); } @@ -661,6 +689,44 @@ impl App { } } + // Taken out of the field so the notice helpers below can borrow the app. + if let Some(rx) = self.ffmpeg_rx.take() { + let mut finished = false; + while let Ok(event) = rx.try_recv() { + match event { + export::FetchEvent::Bytes { done, total } => { + let pct = if total > 0 { done * 100 / total } else { 0 }; + if self.ffmpeg_pct != Some(pct) { + self.ffmpeg_pct = Some(pct); + if self.ffmpeg_banner_showing() { + self.notify(format!("Downloading ffmpeg for clip exports: {pct}%")); + } + } + } + export::FetchEvent::Unpacking => { + if self.ffmpeg_banner_showing() { + self.notify("Unpacking ffmpeg…"); + } + } + export::FetchEvent::Finished(result) => { + finished = true; + match result { + Ok(()) => { + if self.ffmpeg_banner_showing() { + self.notify("ffmpeg is ready: clip export works now"); + } + } + Err(e) => self.notify_error(e), + } + } + } + changed = true; + } + if !finished { + self.ffmpeg_rx = Some(rx); + } + } + if let Some(rx) = &self.review_rx { match rx.try_recv() { Ok(None) => { diff --git a/app/src/export.rs b/app/src/export.rs index 1b5056e..b93cdec 100644 --- a/app/src/export.rs +++ b/app/src/export.rs @@ -2,10 +2,15 @@ //! `tauri-app/src-tauri/src/lib.rs`. use std::path::PathBuf; +use std::sync::atomic::{AtomicBool, Ordering}; use ffmpeg_sidecar::command::{FfmpegCommand, ffmpeg_is_installed}; -use ffmpeg_sidecar::download::auto_download; +use ffmpeg_sidecar::download::{ + FfmpegDownloadProgressEvent, download_ffmpeg_package_with_progress, ffmpeg_download_url, + unpack_ffmpeg_without_extras, +}; use ffmpeg_sidecar::event::{FfmpegEvent, LogLevel}; +use ffmpeg_sidecar::paths::{sidecar_dir, sidecar_path}; use crate::daemon; @@ -16,12 +21,80 @@ pub fn clips_dir() -> PathBuf { dir } +/// Set while `fetch` runs, so an export during the download does not start a second one that +/// unpacks over the first. +static FETCHING: AtomicBool = AtomicBool::new(false); + +/// What `fetch` reports while it runs. +pub enum FetchEvent { + /// Bytes of the download so far, of the total the server announced. + Bytes { done: u64, total: u64 }, + /// The archive is complete and being unpacked beside the exe. + Unpacking, + /// The work is over, with its outcome. + Finished(Result<(), String>), +} + +/// Whether ffmpeg still has to be downloaded: it lives beside the exe. The presence of the file +/// is enough here; `ffmpeg -version` is left to the export that needs it. +pub fn missing() -> bool { + sidecar_path().map(|path| !path.exists()).unwrap_or(true) +} + +/// Whether a download is running right now. +pub fn fetching() -> bool { + FETCHING.load(Ordering::SeqCst) +} + +/// Download ffmpeg beside the exe, reporting progress. Blocks: call it on a thread. +/// +/// Only `ffmpeg.exe` is unpacked. The archive carries `ffplay` and `ffprobe` too, and the +/// export paths call neither; unpacking all three would add 200 MB to every install. +pub fn fetch(report: impl Fn(FetchEvent)) -> Result<(), String> { + if ffmpeg_is_installed() { + return Ok(()); + } + FETCHING.store(true, Ordering::SeqCst); + let result = download(&report); + FETCHING.store(false, Ordering::SeqCst); + report(FetchEvent::Finished(result.clone())); + result +} + +fn download(report: &impl Fn(FetchEvent)) -> Result<(), String> { + let url = ffmpeg_download_url().map_err(|e| format!("cannot find a build to download: {e}"))?; + let dir = sidecar_dir().map_err(|e| format!("cannot find where to put ffmpeg: {e}"))?; + let archive = download_ffmpeg_package_with_progress(url, &dir, |event| { + if let FfmpegDownloadProgressEvent::Downloading { + total_bytes, + downloaded_bytes, + } = event + { + report(FetchEvent::Bytes { + done: downloaded_bytes, + total: total_bytes, + }); + } + }) + .map_err(|e| format!("cannot download ffmpeg: {e}"))?; + report(FetchEvent::Unpacking); + unpack_ffmpeg_without_extras(&archive, &dir) + .map_err(|e| format!("cannot unpack ffmpeg: {e}"))?; + if !ffmpeg_is_installed() { + return Err("ffmpeg did not end up beside the app".to_string()); + } + Ok(()) +} + /// Download the ffmpeg sidecar on first use. pub fn ensure_ffmpeg() -> Result<(), String> { if ffmpeg_is_installed() { return Ok(()); } - auto_download().map_err(|e| format!("cannot download ffmpeg: {e}")) + if fetching() { + return Err("ffmpeg is still downloading; try the export again in a moment".to_string()); + } + fetch(|_| {}) } /// Export `start..end` of `video`. Stream copy is fast and keyframe-aligned; `precise` re-encodes diff --git a/app/src/main.rs b/app/src/main.rs index 7a05a06..79d8783 100644 --- a/app/src/main.rs +++ b/app/src/main.rs @@ -52,6 +52,12 @@ fn main() { probe(); return; } + // `--fetch-ffmpeg`: download ffmpeg beside the exe and exit, 0 when it is ready. The app + // does this itself on the first run; this is the same fetch, for a shortcut or a script. + if std::env::args().any(|arg| arg == "--fetch-ffmpeg") { + fetch_ffmpeg(); + return; + } // A second launch hands its arguments to the first and exits; the tooling modes // (`--snap`, `--tree`, that sort of thing) render or print and exit, so they never forward. if !app::headless() && !rune::single_instance(os::APP_ID) { @@ -125,6 +131,29 @@ fn main() { .run(); } +/// `--fetch-ffmpeg`: download ffmpeg with the same code the first run uses, printing progress. +fn fetch_ffmpeg() { + let shown = std::cell::Cell::new(u64::MAX); + let result = export::fetch(|event| match event { + export::FetchEvent::Bytes { done, total } => { + let pct = if total > 0 { done * 100 / total } else { 0 }; + if pct != shown.get() { + shown.set(pct); + println!("ffmpeg: {pct}%"); + } + } + export::FetchEvent::Unpacking => println!("ffmpeg: unpacking"), + export::FetchEvent::Finished(_) => {} + }); + match result { + Ok(()) => println!("ffmpeg: ready"), + Err(e) => { + eprintln!("{e}"); + std::process::exit(1); + } + } +} + /// `--probe`: bring the daemon up and print what it answers, then exit. A quick check that the /// IPC client and the daemon agree without opening a window. fn probe() {