diff --git a/Cargo.lock b/Cargo.lock index 3c91de7..65ad08d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -449,6 +449,7 @@ version = "0.2.4" dependencies = [ "async-trait", "chrono", + "http-range", "log", "recorder-audio", "recorder-core", @@ -4582,7 +4583,6 @@ dependencies = [ "gtk", "heck 0.5.0", "http", - "http-range", "jni 0.21.1", "libc", "log", diff --git a/crates/core/src/audio/mix.rs b/crates/core/src/audio/mix.rs index 69a4ff5..0ef5b29 100644 --- a/crates/core/src/audio/mix.rs +++ b/crates/core/src/audio/mix.rs @@ -65,28 +65,35 @@ fn soft_clip(x: f32) -> f32 { /// One source track; `None` if the file is missing or holds no audio. type Track = Option, i16>>; -/// Opens a track for [`mix_wavs`]. A missing or header-only file counts as -/// no track; anything else that is not 16 kHz mono 16-bit PCM is an error. -fn open_track(path: &Path) -> io::Result { +/// Whether the WAV at `path` holds audio to play: it is a 16 kHz mono +/// 16-bit PCM track whose header counts at least one sample. The one rule +/// both [`mix_wavs`] and the detail view's "audio available" go by. +pub fn has_audio(path: &Path) -> bool { + open_track(path).is_some() +} + +/// Opens a track for [`mix_wavs`]; `None` unless it [has audio](has_audio). +/// Why an existing file is left out is logged. +fn open_track(path: &Path) -> Track { if path.as_os_str().is_empty() { - return Ok(None); + return None; } let reader = match WavReader::open(path) { Ok(reader) => reader, - Err(hound::Error::IoError(e)) if e.kind() == ErrorKind::NotFound => return Ok(None), - Err(e) => return Err(invalid(path, e)), + Err(hound::Error::IoError(e)) if e.kind() == ErrorKind::NotFound => return None, + Err(e) => { + log::warn!("left out {}: {e}", path.display()); + return None; + } }; if reader.spec() != spec() { - return Err(invalid(path, "not 16 kHz mono 16-bit PCM")); + log::warn!("left out {}: not 16 kHz mono 16-bit PCM", path.display()); + return None; } if reader.len() == 0 { - return Ok(None); + return None; } - Ok(Some(reader.into_samples())) -} - -fn invalid(path: &Path, e: impl std::fmt::Display) -> io::Error { - io::Error::new(ErrorKind::InvalidData, format!("{}: {e}", path.display())) + Some(reader.into_samples()) } /// Reads up to [`CHUNK_SAMPLES`] samples of `track` into `buf` (cleared @@ -104,15 +111,15 @@ fn read_chunk(track: &mut Track, buf: &mut Vec) -> io::Result<()> { /// Mixes the WAV tracks at `a` and `b` (16 kHz mono 16-bit PCM, as written /// by [`super::TrackWriter`]) into a new WAV at `dest` and returns its -/// length in samples. A missing or empty track is left out; with neither -/// track holding audio nothing is written and the error is -/// [`ErrorKind::NotFound`]. +/// length in samples. A track without audio ([`has_audio`]: missing, +/// empty or not in that format) is left out; with neither track holding +/// audio nothing is written and the error is [`ErrorKind::NotFound`]. /// /// Writes `dest` with a `.part` suffix first and renames it at the end, so /// `dest` is never a half-written file. pub fn mix_wavs(a: &Path, b: &Path, dest: &Path) -> io::Result { - let mut track_a = open_track(a)?; - let mut track_b = open_track(b)?; + let mut track_a = open_track(a); + let mut track_b = open_track(b); if track_a.is_none() && track_b.is_none() { return Err(io::Error::new( ErrorKind::NotFound, @@ -296,18 +303,50 @@ mod tests { } #[test] - fn a_file_that_is_not_a_track_is_an_error() { + fn a_file_that_is_not_a_track_is_left_out() { let dir = tempfile::tempdir().unwrap(); - let (a, junk, dest) = ( + let (a, junk, other, dest) = ( dir.path().join("a.wav"), dir.path().join("j.wav"), + dir.path().join("o.wav"), dir.path().join("m.wav"), ); write_track(&a, &[0.25; 10]); fs::write(&junk, b"not a wav file at all").unwrap(); + let stereo = WavSpec { + channels: 2, + ..spec() + }; + let mut writer = WavWriter::create(&other, stereo).unwrap(); + writer.write_sample(1i16).unwrap(); + writer.write_sample(1i16).unwrap(); + writer.finalize().unwrap(); - let err = mix_wavs(&a, &junk, &dest).unwrap_err(); - assert_eq!(err.kind(), ErrorKind::InvalidData); - assert!(!dest.exists()); + assert_eq!(mix_wavs(&a, &junk, &dest).unwrap(), 10); + assert_eq!(mix_wavs(&other, &a, &dest).unwrap(), 10); + let err = mix_wavs(&junk, &other, &dest).unwrap_err(); + assert_eq!(err.kind(), ErrorKind::NotFound); + } + + #[test] + fn a_track_has_audio_only_with_samples_in_the_track_format() { + let dir = tempfile::tempdir().unwrap(); + let path = |name: &str| dir.path().join(name); + assert!(!has_audio(&path("gone.wav")), "missing"); + assert!(!has_audio(Path::new("")), "no path"); + write_track(&path("empty.wav"), &[]); + assert!(!has_audio(&path("empty.wav")), "header only"); + // Larger than a header, but its header says it holds nothing. + let mut bytes = fs::read(path("empty.wav")).unwrap(); + bytes.extend([0u8; 100]); + fs::write(path("unrepaired.wav"), bytes).unwrap(); + assert!( + !has_audio(&path("unrepaired.wav")), + "data not in the header" + ); + fs::write(path("junk.wav"), vec![7u8; 100]).unwrap(); + assert!(!has_audio(&path("junk.wav")), "not a wav"); + write_track(&path("one.wav"), &[0.1]); + assert!(has_audio(&path("one.wav"))); } } diff --git a/crates/core/src/audio/mod.rs b/crates/core/src/audio/mod.rs index e552b46..2297f06 100644 --- a/crates/core/src/audio/mod.rs +++ b/crates/core/src/audio/mod.rs @@ -18,5 +18,5 @@ pub use conditioner::Conditioner; pub use flac::encode_flac; pub use gap::GapFiller; pub use level::rms_level; -pub use mix::{mix_samples, mix_wavs}; +pub use mix::{has_audio, mix_samples, mix_wavs}; pub use wav::{repair_wav, TrackWriter, SAMPLE_RATE}; diff --git a/crates/core/src/call_view.rs b/crates/core/src/call_view.rs index d7fbfa4..49688ce 100644 --- a/crates/core/src/call_view.rs +++ b/crates/core/src/call_view.rs @@ -176,6 +176,9 @@ pub struct CallDetail { /// ([`crate::playback::audio_available`]). Set by the app; `false` from /// [`CallDetail::of`], which does not look at the files. pub audio_available: bool, + /// Whether retention deleted the call's audio (as opposed to the files + /// being missing for another reason). + pub audio_deleted: bool, } impl CallDetail { @@ -214,6 +217,7 @@ impl CallDetail { merges: merge_views(merges, &speaker_names), uploading: false, audio_available: false, + audio_deleted: call.audio_deleted, speaker_names, } } @@ -652,6 +656,7 @@ mod tests { }], "uploading": false, "audio_available": false, + "audio_deleted": false, }) ); } diff --git a/crates/core/src/playback.rs b/crates/core/src/playback.rs index 89bb6e2..4a46c5b 100644 --- a/crates/core/src/playback.rs +++ b/crates/core/src/playback.rs @@ -1,8 +1,9 @@ //! Playback of a call in the detail view while its audio is still on disk: //! both tracks are mixed into one cached mono WAV -//! (`/.wav`), which the UI plays through the webview's -//! asset protocol. The cache is rebuilt only when a track is newer than it, -//! and removed with the call's audio. +//! (`/.wav`), which the app serves to the main window +//! only (its `playback` URI scheme). The cache is rebuilt only when a track +//! is newer than it, and removed with the call's audio and when the player +//! lets go of it. use std::fs; use std::io::{self, ErrorKind}; @@ -11,22 +12,23 @@ use std::time::SystemTime; use serde::Serialize; -use crate::audio::{mix_wavs, SAMPLE_RATE}; +use crate::audio::{has_audio, mix_wavs, SAMPLE_RATE}; use crate::model::{Call, CallId, CallStatus}; -/// The call's audio was deleted (retention, or the files are gone). +/// The call's audio was deleted (by retention). pub const AUDIO_GONE: &str = "Audio nicht mehr vorhanden"; +/// Neither track holds audio (files missing or empty), though nothing +/// deleted them. +pub const AUDIO_MISSING: &str = "Audio nicht vorhanden"; +/// Mixing failed; the details (with file paths) only go to the log. +pub const PREPARE_FAILED: &str = "Audio konnte nicht vorbereitet werden."; pub const RECORDING: &str = "Das Gespräch wird gerade aufgenommen"; pub const PROCESSING: &str = "Das Gespräch wird gerade verarbeitet – Wiedergabe danach möglich"; -/// A WAV file of this size or less holds no audio (canonical header only). -const HEADER_LEN: u64 = 44; - -/// What the UI needs to play a call: the mixed file (turned into an asset -/// URL by the UI) and its length. -#[derive(Serialize, Clone, Debug, PartialEq)] +/// What the UI needs besides the call id (from which it builds the +/// `playback` URL): the mix's length. +#[derive(Serialize, Clone, Copy, Debug, PartialEq)] pub struct PlaybackInfo { - pub path: PathBuf, pub duration_secs: f64, } @@ -43,12 +45,9 @@ pub fn refusal(call: &Call) -> Option<&'static str> { } /// Whether `call` still has audio to play: not deleted, and at least one -/// track holds more than a bare header. +/// track [has audio](has_audio) — the same rule the mix goes by. pub fn audio_available(call: &Call) -> bool { - !call.audio_deleted - && [&call.mic_path, &call.out_path] - .iter() - .any(|path| fs::metadata(path).is_ok_and(|m| m.len() > HEADER_LEN)) + !call.audio_deleted && (has_audio(&call.mic_path) || has_audio(&call.out_path)) } /// Whether a cached mix last written at `cache` can be reused for tracks @@ -77,7 +76,8 @@ pub fn cache_path(dir: &Path, id: CallId) -> PathBuf { /// Returns call `call`'s mix in the playback directory `dir`, building it /// first unless a fresh one is cached ([`is_fresh`]). Refused (see /// [`refusal`]) while the call records or is processed and once its audio -/// is gone; a cached mix of a call whose audio is gone is removed. +/// is gone; a cached mix of a call whose audio is gone is removed. Errors +/// are fit for the UI: they never contain a file path. pub fn prepare(call: &Call, dir: &Path) -> Result { let path = cache_path(dir, call.id); if let Some(msg) = refusal(call) { @@ -89,27 +89,25 @@ pub fn prepare(call: &Call, dir: &Path) -> Result { let sources = [modified(&call.mic_path), modified(&call.out_path)]; if is_fresh(modified(&path), &sources) { match duration_secs(&path) { - Ok(duration_secs) => { - return Ok(PlaybackInfo { - path, - duration_secs, - }) - } + Ok(duration_secs) => return Ok(PlaybackInfo { duration_secs }), Err(e) => log::warn!("call {}: rebuilding unreadable playback file: {e}", call.id), } } - fs::create_dir_all(dir).map_err(|e| format!("Wiedergabe-Ordner fehlt: {e}"))?; - match mix_wavs(&call.mic_path, &call.out_path, &path) { + let mixed = + fs::create_dir_all(dir).and_then(|()| mix_wavs(&call.mic_path, &call.out_path, &path)); + match mixed { Ok(samples) => Ok(PlaybackInfo { - path, duration_secs: samples as f64 / SAMPLE_RATE as f64, }), Err(e) if e.kind() == ErrorKind::NotFound => { remove_cached(dir, call.id); - Err(AUDIO_GONE.to_string()) + Err(AUDIO_MISSING.to_string()) + } + Err(e) => { + log::error!("call {}: could not mix for playback: {e}", call.id); + Err(PREPARE_FAILED.to_string()) } - Err(e) => Err(format!("Audio konnte nicht vorbereitet werden: {e}")), } } @@ -247,16 +245,17 @@ mod tests { let c = recorded_call(dir.path(), CallStatus::Done); let info = prepare(&c, &playback).unwrap(); - assert_eq!(info.path, playback.join("7.wav")); + let mix = playback.join("7.wav"); + assert!(mix.exists()); assert!((info.duration_secs - 1.0).abs() < 1e-9); // Marked as written long ago: a rebuild would show a new time. let old = SystemTime::now() - Duration::from_secs(60); set_modified(&c.mic_path, old - Duration::from_secs(60)); set_modified(&c.out_path, old - Duration::from_secs(60)); - set_modified(&info.path, old); + set_modified(&mix, old); assert_eq!(prepare(&c, &playback).unwrap(), info); - assert_eq!(modified(&info.path), Some(old), "reused, not rebuilt"); + assert_eq!(modified(&mix), Some(old), "reused, not rebuilt"); } #[test] @@ -264,15 +263,16 @@ mod tests { let dir = tempfile::tempdir().unwrap(); let playback = dir.path().join("playback"); let c = recorded_call(dir.path(), CallStatus::AwaitingDecision); - let first = prepare(&c, &playback).unwrap(); + prepare(&c, &playback).unwrap(); + let mix = cache_path(&playback, c.id); let old = SystemTime::now() - Duration::from_secs(60); - set_modified(&first.path, old); + set_modified(&mix, old); // Two seconds now; the file is newer than the cache. write_track(&c.mic_path, &vec![0.1; 32_000]); let second = prepare(&c, &playback).unwrap(); assert!((second.duration_secs - 2.0).abs() < 1e-9); - assert_ne!(modified(&second.path), Some(old)); + assert_ne!(modified(&mix), Some(old)); } #[test] @@ -300,14 +300,15 @@ mod tests { let dir = tempfile::tempdir().unwrap(); let playback = dir.path().join("playback"); let mut c = recorded_call(dir.path(), CallStatus::Done); - let info = prepare(&c, &playback).unwrap(); + prepare(&c, &playback).unwrap(); + let mix = cache_path(&playback, c.id); // Retention deleted the audio. fs::remove_file(&c.mic_path).unwrap(); fs::remove_file(&c.out_path).unwrap(); c.audio_deleted = true; assert_eq!(prepare(&c, &playback), Err(AUDIO_GONE.to_string())); - assert!(!info.path.exists()); + assert!(!mix.exists()); } #[test] @@ -315,12 +316,24 @@ mod tests { let dir = tempfile::tempdir().unwrap(); let playback = dir.path().join("playback"); let c = recorded_call(dir.path(), CallStatus::Done); - let info = prepare(&c, &playback).unwrap(); + prepare(&c, &playback).unwrap(); + let mix = cache_path(&playback, c.id); fs::remove_file(&c.mic_path).unwrap(); fs::remove_file(&c.out_path).unwrap(); - assert_eq!(prepare(&c, &playback), Err(AUDIO_GONE.to_string())); - assert!(!info.path.exists()); + assert_eq!(prepare(&c, &playback), Err(AUDIO_MISSING.to_string())); + assert!(!mix.exists()); + } + + #[test] + fn a_failed_mix_reports_no_file_paths() { + let dir = tempfile::tempdir().unwrap(); + let c = recorded_call(dir.path(), CallStatus::Done); + // The playback directory cannot be created: a file is in the way. + let blocked = dir.path().join("playback"); + fs::write(&blocked, b"").unwrap(); + + assert_eq!(prepare(&c, &blocked), Err(PREPARE_FAILED.to_string())); } #[test] @@ -328,11 +341,11 @@ mod tests { let dir = tempfile::tempdir().unwrap(); let playback = dir.path().join("playback"); let c = recorded_call(dir.path(), CallStatus::Done); - let info = prepare(&c, &playback).unwrap(); + prepare(&c, &playback).unwrap(); fs::write(playback.join("8.wav"), b"other").unwrap(); remove_cached(&playback, c.id); - assert!(!info.path.exists()); + assert!(!cache_path(&playback, c.id).exists()); assert!(playback.join("8.wav").exists()); remove_cached(&playback, c.id); // already gone: no error diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 527b0de..e0ee7aa 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -17,7 +17,7 @@ tauri-build = { version = "2.7.4", features = [] } serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } log = "0.4" -tauri = { version = "2.12.5", features = ["tray-icon", "protocol-asset"] } +tauri = { version = "2.12.5", features = ["tray-icon"] } tauri-plugin-log = "2" tauri-plugin-autostart = "2" tauri-plugin-single-instance = "2" @@ -30,3 +30,4 @@ tauri-plugin-updater = "2" tauri-plugin-opener = "2" tokio = { version = "1", features = ["sync"] } async-trait = "0.1" +http-range = "0.1.6" diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index d205428..62b317f 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -11,7 +11,6 @@ use std::time::Duration; use recorder_core::call_view::{CallDetail, CallListItem}; use recorder_core::capture::Devices; use recorder_core::model::CallId; -use recorder_core::playback::PlaybackInfo; use recorder_core::secrets::Service; use recorder_core::settings::Settings; use serde::Serialize; @@ -20,7 +19,8 @@ use tauri_plugin_autostart::ManagerExt; use crate::processing; use crate::state::{ - AppState, CallBrief, DeviceLevels, DiscardError, Ended, PendingPrompt, RecordingState, + AppState, CallBrief, DeviceLevels, DiscardError, Ended, PendingPrompt, PreparedPlayback, + RecordingState, }; const LEVELS_INTERVAL: Duration = Duration::from_millis(100); @@ -210,12 +210,24 @@ pub fn rename_call( } /// Mixes the call's two tracks into one file to play in the detail view -/// (or reuses the cached mix); the UI plays it through the asset protocol. +/// (or reuses the cached mix); the main window plays it from the +/// `playback` scheme ([`crate::playback`]). #[tauri::command] -pub async fn prepare_playback(app: AppHandle, id: CallId) -> CmdResult { +pub async fn prepare_playback(app: AppHandle, id: CallId) -> CmdResult { blocking(app, move |app| app.state::().prepare_playback(id)).await } +/// The player is done with the mix of `lease`: it is removed from disk +/// unless a newer player of the same call holds it. +#[tauri::command] +pub async fn release_playback(app: AppHandle, id: CallId, lease: u64) -> CmdResult<()> { + blocking(app, move |app| { + app.state::().release_playback(id, lease); + Ok(()) + }) + .await +} + /// Deletes a finished call's audio and row. Closes the prompt if no call /// waits for it any more; otherwise the prompt reloads and shows the next. /// While its DeepScript jobs are still on record, nothing is deleted and a diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 0ed25a2..2d0a208 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,6 +1,7 @@ mod backend; mod commands; mod detection; +mod playback; mod processing; mod state; mod tray; @@ -43,6 +44,21 @@ pub fn run() { .open_js_links_on_click(false) .build(), ) + // A call's mix for the player: only for the main window, whatever + // other webviews (the remote backend login) ask for. + .register_asynchronous_uri_scheme_protocol(playback::SCHEME, |ctx, request, responder| { + let webview = ctx.webview_label().to_string(); + let dir = ctx + .app_handle() + .try_state::() + .map(|state| state.playback_dir().to_path_buf()); + tauri::async_runtime::spawn_blocking(move || { + responder.respond(match dir { + Some(dir) => playback::respond(&webview, &request, &dir), + None => playback::unavailable(), + }) + }); + }) .setup(|app| { // Release builds log to a file in the app log directory (on // Windows %LOCALAPPDATA%\eu.euvino.callrecorder\logs), debug @@ -72,8 +88,7 @@ pub fn run() { let data_dir = app.path().app_data_dir()?; let config_dir = app.path().app_config_dir()?; - // The asset protocol may read this directory, and only this one - // (`app.security.assetProtocol.scope` in tauri.conf.json). + // Served to the main window only, by the `playback` scheme. let playback_dir = app.path().app_cache_dir()?.join("playback"); std::fs::create_dir_all(&data_dir)?; let store = Store::open(&data_dir.join("calls.sqlite"))?; @@ -161,6 +176,7 @@ pub fn run() { commands::unmerge_speaker, commands::rename_call, commands::prepare_playback, + commands::release_playback, backend::backend_server, backend::backend_status, backend::backend_login, diff --git a/src-tauri/src/playback.rs b/src-tauri/src/playback.rs new file mode 100644 index 0000000..3263893 --- /dev/null +++ b/src-tauri/src/playback.rs @@ -0,0 +1,293 @@ +//! The `playback` URI scheme: serves a call's cached mix +//! ([`recorder_core::playback`]) to the main window, and to no other +//! webview — the backend-login window shows a remote page, which must +//! never read customer audio. +//! +//! The URL carries only the call id: `playback://localhost/` on Linux +//! and macOS, `http://playback.localhost/` on Windows (what the UI's +//! `convertFileSrc(id, 'playback')` builds). Range requests are answered +//! so the player can seek without loading the whole file. + +use std::fs::File; +use std::io::{self, ErrorKind, Read, Seek, SeekFrom}; +use std::path::Path; + +use http_range::HttpRange; +use recorder_core::model::CallId; +use recorder_core::playback::cache_path; +use tauri::http::header::{ + ACCEPT_RANGES, ACCESS_CONTROL_ALLOW_ORIGIN, ACCESS_CONTROL_EXPOSE_HEADERS, CACHE_CONTROL, + CONTENT_LENGTH, CONTENT_RANGE, CONTENT_TYPE, RANGE, +}; +use tauri::http::{response, Method, Request, Response, StatusCode}; + +/// The scheme's name (see the module docs for its URLs). +pub const SCHEME: &str = "playback"; +/// The only webview the scheme answers. +pub const PLAYER_WEBVIEW: &str = "main"; +/// The most bytes one range response carries (like Tauri's asset protocol). +const MAX_RANGE_LEN: u64 = 1024 * 1024; + +/// Answers `request` from webview `webview` with call ``'s mix in the +/// playback directory `dir`: 403 for any webview but the main window, 404 +/// for anything but a plain numeric id or a mix that is not there, 206 for +/// a satisfiable range (at most [`MAX_RANGE_LEN`] bytes), 416 otherwise. +pub fn respond(webview: &str, request: &Request>, dir: &Path) -> Response> { + if webview != PLAYER_WEBVIEW { + log::warn!("playback refused to webview {webview}"); + return empty(StatusCode::FORBIDDEN); + } + let method = request.method(); + if method != Method::GET && method != Method::HEAD { + return empty(StatusCode::METHOD_NOT_ALLOWED); + } + let Some(id) = call_id(request.uri().path()) else { + return empty(StatusCode::NOT_FOUND); + }; + let range = request + .headers() + .get(RANGE) + .map(|value| value.to_str().unwrap_or_default()); + match serve(&cache_path(dir, id), range, method == Method::HEAD) { + Ok(response) => response, + Err(e) if e.kind() == ErrorKind::NotFound => empty(StatusCode::NOT_FOUND), + Err(e) => { + log::error!("call {id}: could not serve the playback file: {e}"); + empty(StatusCode::INTERNAL_SERVER_ERROR) + } + } +} + +/// The answer while the app state is not set up yet (no window can play +/// before it is, so this is never expected). +pub fn unavailable() -> Response> { + empty(StatusCode::SERVICE_UNAVAILABLE) +} + +/// The call id of a request path: exactly `/`. +fn call_id(path: &str) -> Option { + let digits = path.strip_prefix('/')?; + if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + digits.parse().ok() +} + +fn serve(path: &Path, range: Option<&str>, head: bool) -> io::Result>> { + let mut file = File::open(path)?; + let len = file.metadata()?.len(); + let builder = headers(); + let Some(range) = range else { + let mut body = Vec::new(); + if !head { + file.read_to_end(&mut body)?; + } + return Ok(build(builder.header(CONTENT_LENGTH, len), body)); + }; + // A media element asks for one range at a time; of several, the first. + let Some(range) = HttpRange::parse(range, len) + .ok() + .and_then(|r| r.first().copied()) + else { + return Ok(build( + builder + .status(StatusCode::RANGE_NOT_SATISFIABLE) + .header(CONTENT_RANGE, format!("bytes */{len}")), + Vec::new(), + )); + }; + let start = range.start; + let count = range.length.min(MAX_RANGE_LEN); + let mut body = Vec::new(); + if !head { + file.seek(SeekFrom::Start(start))?; + file.take(count).read_to_end(&mut body)?; + } + Ok(build( + builder + .status(StatusCode::PARTIAL_CONTENT) + .header( + CONTENT_RANGE, + format!("bytes {start}-{}/{len}", start + count - 1), + ) + .header(CONTENT_LENGTH, count), + body, + )) +} + +/// Headers of every answer that carries the mix. Any origin may read it: +/// only the main window's own page can reach the scheme at all (the +/// webview check above), and on Linux it fetches the mix cross-origin. +fn headers() -> response::Builder { + Response::builder() + .header(CONTENT_TYPE, "audio/wav") + .header(CACHE_CONTROL, "no-store") + .header(ACCEPT_RANGES, "bytes") + .header(ACCESS_CONTROL_ALLOW_ORIGIN, "*") + .header(ACCESS_CONTROL_EXPOSE_HEADERS, "content-range") +} + +fn build(builder: response::Builder, body: Vec) -> Response> { + builder + .body(body) + .unwrap_or_else(|_| empty(StatusCode::INTERNAL_SERVER_ERROR)) +} + +fn empty(status: StatusCode) -> Response> { + let mut response = Response::new(Vec::new()); + *response.status_mut() = status; + response + .headers_mut() + .insert(CACHE_CONTROL, "no-store".parse().unwrap()); + response +} + +#[cfg(test)] +mod tests { + use std::fs; + + use super::*; + + /// A playback directory with a 10-byte "mix" for call 7. + fn dir() -> tempfile::TempDir { + let dir = tempfile::tempdir().unwrap(); + fs::write(cache_path(dir.path(), 7), b"0123456789").unwrap(); + dir + } + + fn get(uri: &str, range: Option<&str>) -> Request> { + let mut builder = Request::builder().uri(uri); + if let Some(range) = range { + builder = builder.header(RANGE, range); + } + builder.body(Vec::new()).unwrap() + } + + #[test] + fn only_the_main_window_gets_the_mix() { + let dir = dir(); + let request = get("playback://localhost/7", None); + for webview in ["backend-login", "prompt", "recorder", "", "Main"] { + let response = respond(webview, &request, dir.path()); + assert_eq!(response.status(), StatusCode::FORBIDDEN, "{webview}"); + assert!(response.body().is_empty()); + } + + let response = respond("main", &request, dir.path()); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(response.body(), b"0123456789"); + assert_eq!(response.headers()[CONTENT_TYPE], "audio/wav"); + assert_eq!(response.headers()[CACHE_CONTROL], "no-store"); + assert_eq!(response.headers()[CONTENT_LENGTH], "10"); + } + + #[test] + fn both_platform_url_forms_name_the_call() { + let dir = dir(); + for uri in ["playback://localhost/7", "http://playback.localhost/7"] { + let response = respond("main", &get(uri, None), dir.path()); + assert_eq!(response.status(), StatusCode::OK, "{uri}"); + } + } + + #[test] + fn nothing_but_a_numeric_call_id_is_served() { + let dir = dir(); + fs::write(dir.path().join("secret.wav"), b"x").unwrap(); + fs::write(dir.path().join("7.wav.part"), b"x").unwrap(); + for path in [ + "/", + "/7.wav", + "/secret", + "/../7", + "/%2e%2e%2f7", + "/7/", + "/7.wav.part", + "/-7", + "/+7", + "/%EF%BC%97", + "/99999999999999999999999", + "/8", + ] { + let request = get(&format!("playback://localhost{path}"), None); + let response = respond("main", &request, dir.path()); + assert_eq!(response.status(), StatusCode::NOT_FOUND, "{path}"); + } + assert_eq!(call_id("/7"), Some(7)); + assert_eq!(call_id("/0012"), Some(12)); + } + + #[test] + fn a_range_is_served_partially() { + let dir = dir(); + let response = respond( + "main", + &get("playback://localhost/7", Some("bytes=2-5")), + dir.path(), + ); + assert_eq!(response.status(), StatusCode::PARTIAL_CONTENT); + assert_eq!(response.body(), b"2345"); + assert_eq!(response.headers()[CONTENT_RANGE], "bytes 2-5/10"); + assert_eq!(response.headers()[CONTENT_LENGTH], "4"); + + let open_ended = respond( + "main", + &get("playback://localhost/7", Some("bytes=8-")), + dir.path(), + ); + assert_eq!(open_ended.body(), b"89"); + assert_eq!(open_ended.headers()[CONTENT_RANGE], "bytes 8-9/10"); + } + + #[test] + fn a_range_beyond_the_end_is_not_satisfiable() { + let dir = dir(); + let response = respond( + "main", + &get("playback://localhost/7", Some("bytes=20-30")), + dir.path(), + ); + assert_eq!(response.status(), StatusCode::RANGE_NOT_SATISFIABLE); + assert_eq!(response.headers()[CONTENT_RANGE], "bytes */10"); + assert!(response.body().is_empty()); + } + + #[test] + fn a_long_range_is_cut_to_the_maximum() { + let dir = tempfile::tempdir().unwrap(); + let len = MAX_RANGE_LEN as usize * 2 + 3; + fs::write(cache_path(dir.path(), 1), vec![1u8; len]).unwrap(); + let response = respond( + "main", + &get("playback://localhost/1", Some("bytes=1-")), + dir.path(), + ); + assert_eq!(response.body().len(), MAX_RANGE_LEN as usize); + assert_eq!( + response.headers()[CONTENT_RANGE], + format!("bytes 1-{}/{len}", MAX_RANGE_LEN).as_str() + ); + } + + #[test] + fn head_has_no_body_and_other_methods_are_refused() { + let dir = dir(); + let head = Request::builder() + .method(Method::HEAD) + .uri("playback://localhost/7") + .body(Vec::new()) + .unwrap(); + let response = respond("main", &head, dir.path()); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(response.headers()[CONTENT_LENGTH], "10"); + assert!(response.body().is_empty()); + + let post = Request::builder() + .method(Method::POST) + .uri("playback://localhost/7") + .body(Vec::new()) + .unwrap(); + let response = respond("main", &post, dir.path()); + assert_eq!(response.status(), StatusCode::METHOD_NOT_ALLOWED); + } +} diff --git a/src-tauri/src/state.rs b/src-tauri/src/state.rs index d1efd60..c63956a 100644 --- a/src-tauri/src/state.rs +++ b/src-tauri/src/state.rs @@ -7,7 +7,7 @@ //! independent of windows and events; the command layer turns results into //! UI updates. -use std::collections::{HashSet, VecDeque}; +use std::collections::{HashMap, HashSet, VecDeque}; use std::fs; use std::io::ErrorKind; use std::path::{Path, PathBuf}; @@ -24,7 +24,7 @@ use recorder_core::capture::{CaptureSource, OutputTarget}; use recorder_core::detect::{CallDetector, DetectorEvent, MicSession}; use recorder_core::http::{HttpTransport, ReqwestTransport}; use recorder_core::model::{Call, CallId, CallStatus, Speaker}; -use recorder_core::playback::{self, PlaybackInfo}; +use recorder_core::playback; use recorder_core::recorder::Recorder; use recorder_core::secrets::{SecretStore, Service}; use recorder_core::settings::Settings; @@ -149,12 +149,12 @@ pub struct AppState { settings: Mutex, settings_path: PathBuf, recordings_dir: PathBuf, - /// Where the mixes for playback are cached (`/playback`, - /// the only directory the webview's asset protocol may read). + /// Where the mixes for playback are cached (`/playback`), + /// served to the main window by [`crate::playback`]. playback_dir: PathBuf, - /// Held while a mix is built or removed, so a call deleted meanwhile - /// never leaves its mix behind. - playback_lock: Mutex<()>, + /// The leases on cached mixes. Held while a mix is built or removed, + /// so a call deleted meanwhile never leaves its mix behind. + playback: Mutex, current: Mutex>, detector: Mutex, /// Stopped calls awaiting the end-of-call prompt's decision, oldest @@ -181,6 +181,23 @@ pub struct AppState { upload_confirmed_unsaved: Mutex>, } +/// A mix ready to play, from [`AppState::prepare_playback`]. +#[derive(Serialize, Clone, Copy, Debug, PartialEq)] +pub struct PreparedPlayback { + pub duration_secs: f64, + /// Hand back to [`AppState::release_playback`] when the player is done. + pub lease: u64, +} + +/// Which player holds each cached mix: the lease of the latest +/// [`AppState::prepare_playback`] per call. A player that lets go of an +/// older lease (it was replaced by a newer one meanwhile) removes nothing. +#[derive(Default)] +struct PlaybackLeases { + next: u64, + latest: HashMap, +} + /// An upload reserved with [`AppState::claim_upload`]; released when /// dropped, also if the upload fails or panics. pub struct UploadClaim<'a> { @@ -221,7 +238,7 @@ impl AppState { settings_path, recordings_dir, playback_dir, - playback_lock: Mutex::new(()), + playback: Mutex::new(PlaybackLeases::default()), current: Mutex::new(None), detector: Mutex::new(detector), pending_prompts: Mutex::new(VecDeque::new()), @@ -672,29 +689,53 @@ impl AppState { } /// Mixes call `id`'s tracks for playback (or reuses the cached mix, - /// see [`playback::prepare`]). Refused while the call is recorded or - /// processed, and once its audio is gone. - pub fn prepare_playback(&self, id: CallId) -> Result { + /// see [`playback::prepare`]) under a new lease. Refused while the call + /// is recorded or processed, and once its audio is gone. + pub fn prepare_playback(&self, id: CallId) -> Result { if lock(&self.current) .as_ref() .is_some_and(|cur| cur.call_id == id) { return Err(playback::RECORDING.to_string()); } - let _building = lock(&self.playback_lock); + let mut leases = lock(&self.playback); // Read under the lock: a call deleted before it is not mixed, one // deleted meanwhile has its mix removed once the lock is free. let call = self.find_call(id)?; - playback::prepare(&call, &self.playback_dir) + let info = playback::prepare(&call, &self.playback_dir)?; + leases.next += 1; + let lease = leases.next; + leases.latest.insert(id, lease); + Ok(PreparedPlayback { + duration_secs: info.duration_secs, + lease, + }) } - /// Removes call `id`'s cached mix, once the call or its audio is - /// deleted. + /// The player of `lease` is done with call `id`'s mix: removes it, + /// unless a newer lease (another player of the same call) holds it. + pub fn release_playback(&self, id: CallId, lease: u64) { + let mut leases = lock(&self.playback); + if leases.latest.get(&id) == Some(&lease) { + leases.latest.remove(&id); + playback::remove_cached(&self.playback_dir, id); + } + } + + /// Removes call `id`'s cached mix whoever holds it: with the call, and + /// once its audio is deleted. The retention job, when it deletes a + /// call's audio, calls this too. pub fn remove_playback(&self, id: CallId) { - let _building = lock(&self.playback_lock); + let mut leases = lock(&self.playback); + leases.latest.remove(&id); playback::remove_cached(&self.playback_dir, id); } + /// Where the mixes are cached ([`crate::playback`] serves them). + pub fn playback_dir(&self) -> &Path { + &self.playback_dir + } + /// Empties the playback cache (at startup), so mixed customer audio /// never lingers from an earlier run. pub fn clear_playback_cache(&self) { @@ -1968,10 +2009,49 @@ mod tests { let id = playable_call(&f, "a"); assert!(f.state.call_detail(id).unwrap().audio_available); - let info = f.state.prepare_playback(id).unwrap(); - assert_eq!(info.path, playback_file(&f, id)); - assert!((info.duration_secs - 1.0).abs() < 1e-9); - assert!(info.path.exists()); + let prepared = f.state.prepare_playback(id).unwrap(); + assert!((prepared.duration_secs - 1.0).abs() < 1e-9); + assert!(playback_file(&f, id).exists()); + } + + #[test] + fn the_player_holding_the_latest_lease_removes_the_mix() { + let f = fixture(); + let id = playable_call(&f, "a"); + let first = f.state.prepare_playback(id).unwrap().lease; + // The call was shown again before the first player let go. + let second = f.state.prepare_playback(id).unwrap().lease; + assert_ne!(first, second); + + f.state.release_playback(id, first); + assert!( + playback_file(&f, id).exists(), + "the second player still plays" + ); + f.state.release_playback(id, second); + assert!(!playback_file(&f, id).exists()); + f.state.release_playback(id, second); // already gone: no error + + // A lease of one call never removes another call's mix. + let other = playable_call(&f, "b"); + let lease = f.state.prepare_playback(other).unwrap().lease; + f.state.release_playback(id, lease); + assert!(playback_file(&f, other).exists()); + } + + #[test] + fn missing_files_are_no_audio_but_not_deleted_audio() { + let f = fixture(); + let (id, mic, out) = finished_call(&f, "a"); // a few junk bytes each + let detail = f.state.call_detail(id).unwrap(); + assert!(!detail.audio_available); + assert!(!detail.audio_deleted); + fs::remove_file(mic).unwrap(); + fs::remove_file(out).unwrap(); + assert_eq!( + f.state.prepare_playback(id), + Err(playback::AUDIO_MISSING.to_string()) + ); } #[test] @@ -2021,7 +2101,9 @@ mod tests { fs::remove_file(&call.out_path).unwrap(); f.state.store.mark_audio_deleted(id).unwrap(); - assert!(!f.state.call_detail(id).unwrap().audio_available); + let detail = f.state.call_detail(id).unwrap(); + assert!(!detail.audio_available); + assert!(detail.audio_deleted); assert_eq!( f.state.prepare_playback(id), Err(playback::AUDIO_GONE.to_string()) diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index d184d37..cebf6a9 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -44,13 +44,7 @@ } ], "security": { - "csp": null, - "assetProtocol": { - "enable": true, - "scope": [ - "$APPCACHE/playback/**" - ] - } + "csp": null } }, "bundle": { diff --git a/ui/src/lib/Player.svelte b/ui/src/lib/Player.svelte index 2691e04..0f2da95 100644 --- a/ui/src/lib/Player.svelte +++ b/ui/src/lib/Player.svelte @@ -1,7 +1,6 @@ diff --git a/ui/src/lib/api.ts b/ui/src/lib/api.ts index 120e897..85079fa 100644 --- a/ui/src/lib/api.ts +++ b/ui/src/lib/api.ts @@ -1,6 +1,6 @@ // Typed wrappers around the Tauri commands and events of the app shell. -import { invoke } from '@tauri-apps/api/core' +import { convertFileSrc, invoke } from '@tauri-apps/api/core' import { listen, type UnlistenFn } from '@tauri-apps/api/event' export type CallId = number @@ -142,13 +142,16 @@ export interface CallDetail { uploading: boolean /** The call's audio is still on this PC and can be played. */ audio_available: boolean + /** Retention deleted the call's audio (not just: the files are missing). */ + audio_deleted: boolean } -/** A call's two tracks mixed into one file for playback. */ -export interface PlaybackInfo { - /** The mixed WAV in the playback cache; play it via `convertFileSrc`. */ - path: string +/** A call's two tracks mixed into one file, ready to play from + * `playbackUrl`. */ +export interface PreparedPlayback { duration_secs: number + /** Hand back with `releasePlayback` once the player is done. */ + lease: number } /** A newer version the release feed offers. */ @@ -199,7 +202,14 @@ export const retryCall = (id: CallId) => invoke('retry_call', { id }) export const discardCall = (id: CallId) => invoke('discard_call', { id }) /** Mixes the call's tracks (or reuses the cached mix); refused while the * call is recorded or processed and once its audio is gone. */ -export const preparePlayback = (id: CallId) => invoke('prepare_playback', { id }) +export const preparePlayback = (id: CallId) => + invoke('prepare_playback', { id }) +/** The player is done with the mix: it is removed from disk. */ +export const releasePlayback = (id: CallId, lease: number) => + invoke('release_playback', { id, lease }) +/** Where the main window plays a prepared mix from (the app's `playback` + * scheme; other windows are refused). */ +export const playbackUrl = (id: CallId) => convertFileSrc(String(id), 'playback') export const listCalls = () => invoke('list_calls') export const getCall = (id: CallId) => invoke('get_call', { id }) /** An empty name restores the default label. */ diff --git a/ui/src/routes/Main.svelte b/ui/src/routes/Main.svelte index baf2e0d..970ede4 100644 --- a/ui/src/routes/Main.svelte +++ b/ui/src/routes/Main.svelte @@ -166,9 +166,12 @@ }), ] refreshList() + // Read on every visit: the settings page is a route of this window, so + // a changed retention period is shown on the way back. Without it the + // "no audio" line just leaves the period out. getSettings() .then((settings) => (retentionDays = settings.retention_days)) - .catch(() => {}) + .catch((err) => console.warn('settings not read:', errorText(err))) getRecordingState() .then((state) => (recordingState = state)) .catch((err) => (recordError = errorText(err))) @@ -514,11 +517,15 @@ > {/if} - {:else} + {:else if detail.audio_deleted && retentionDays !== null}

- Audio nicht mehr vorhanden – Aufnahmen werden {retentionDays ?? '…'} Tage nach der + Audio nicht mehr vorhanden – Aufnahmen werden {retentionDays} Tage nach der Transkription gelöscht (Einstellungen).

+ {:else if detail.audio_deleted} +

Audio nicht mehr vorhanden.

+ {:else} +

Audio nicht vorhanden.

{/if} {/if}