fix #14740: serve playback only to the main window

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-10 13:52:26 +01:00
co-authored by Claude Opus 5.5
parent a74cfa5864
commit 8c47388eee
14 changed files with 624 additions and 124 deletions
Generated
+1 -1
View File
@@ -449,6 +449,7 @@ version = "0.2.4"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"chrono", "chrono",
"http-range",
"log", "log",
"recorder-audio", "recorder-audio",
"recorder-core", "recorder-core",
@@ -4582,7 +4583,6 @@ dependencies = [
"gtk", "gtk",
"heck 0.5.0", "heck 0.5.0",
"http", "http",
"http-range",
"jni 0.21.1", "jni 0.21.1",
"libc", "libc",
"log", "log",
+62 -23
View File
@@ -65,28 +65,35 @@ fn soft_clip(x: f32) -> f32 {
/// One source track; `None` if the file is missing or holds no audio. /// One source track; `None` if the file is missing or holds no audio.
type Track = Option<hound::WavIntoSamples<BufReader<File>, i16>>; type Track = Option<hound::WavIntoSamples<BufReader<File>, i16>>;
/// Opens a track for [`mix_wavs`]. A missing or header-only file counts as /// Whether the WAV at `path` holds audio to play: it is a 16 kHz mono
/// no track; anything else that is not 16 kHz mono 16-bit PCM is an error. /// 16-bit PCM track whose header counts at least one sample. The one rule
fn open_track(path: &Path) -> io::Result<Track> { /// 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() { if path.as_os_str().is_empty() {
return Ok(None); return None;
} }
let reader = match WavReader::open(path) { let reader = match WavReader::open(path) {
Ok(reader) => reader, Ok(reader) => reader,
Err(hound::Error::IoError(e)) if e.kind() == ErrorKind::NotFound => return Ok(None), Err(hound::Error::IoError(e)) if e.kind() == ErrorKind::NotFound => return None,
Err(e) => return Err(invalid(path, e)), Err(e) => {
log::warn!("left out {}: {e}", path.display());
return None;
}
}; };
if reader.spec() != spec() { 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 { if reader.len() == 0 {
return Ok(None); return None;
} }
Ok(Some(reader.into_samples())) Some(reader.into_samples())
}
fn invalid(path: &Path, e: impl std::fmt::Display) -> io::Error {
io::Error::new(ErrorKind::InvalidData, format!("{}: {e}", path.display()))
} }
/// Reads up to [`CHUNK_SAMPLES`] samples of `track` into `buf` (cleared /// Reads up to [`CHUNK_SAMPLES`] samples of `track` into `buf` (cleared
@@ -104,15 +111,15 @@ fn read_chunk(track: &mut Track, buf: &mut Vec<i16>) -> io::Result<()> {
/// Mixes the WAV tracks at `a` and `b` (16 kHz mono 16-bit PCM, as written /// 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 /// 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 /// length in samples. A track without audio ([`has_audio`]: missing,
/// track holding audio nothing is written and the error is /// empty or not in that format) is left out; with neither track holding
/// [`ErrorKind::NotFound`]. /// audio nothing is written and the error is [`ErrorKind::NotFound`].
/// ///
/// Writes `dest` with a `.part` suffix first and renames it at the end, so /// Writes `dest` with a `.part` suffix first and renames it at the end, so
/// `dest` is never a half-written file. /// `dest` is never a half-written file.
pub fn mix_wavs(a: &Path, b: &Path, dest: &Path) -> io::Result<u64> { pub fn mix_wavs(a: &Path, b: &Path, dest: &Path) -> io::Result<u64> {
let mut track_a = open_track(a)?; let mut track_a = open_track(a);
let mut track_b = open_track(b)?; let mut track_b = open_track(b);
if track_a.is_none() && track_b.is_none() { if track_a.is_none() && track_b.is_none() {
return Err(io::Error::new( return Err(io::Error::new(
ErrorKind::NotFound, ErrorKind::NotFound,
@@ -296,18 +303,50 @@ mod tests {
} }
#[test] #[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 dir = tempfile::tempdir().unwrap();
let (a, junk, dest) = ( let (a, junk, other, dest) = (
dir.path().join("a.wav"), dir.path().join("a.wav"),
dir.path().join("j.wav"), dir.path().join("j.wav"),
dir.path().join("o.wav"),
dir.path().join("m.wav"), dir.path().join("m.wav"),
); );
write_track(&a, &[0.25; 10]); write_track(&a, &[0.25; 10]);
fs::write(&junk, b"not a wav file at all").unwrap(); 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!(mix_wavs(&a, &junk, &dest).unwrap(), 10);
assert_eq!(err.kind(), ErrorKind::InvalidData); assert_eq!(mix_wavs(&other, &a, &dest).unwrap(), 10);
assert!(!dest.exists()); 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")));
} }
} }
+1 -1
View File
@@ -18,5 +18,5 @@ pub use conditioner::Conditioner;
pub use flac::encode_flac; pub use flac::encode_flac;
pub use gap::GapFiller; pub use gap::GapFiller;
pub use level::rms_level; 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}; pub use wav::{repair_wav, TrackWriter, SAMPLE_RATE};
+5
View File
@@ -176,6 +176,9 @@ pub struct CallDetail {
/// ([`crate::playback::audio_available`]). Set by the app; `false` from /// ([`crate::playback::audio_available`]). Set by the app; `false` from
/// [`CallDetail::of`], which does not look at the files. /// [`CallDetail::of`], which does not look at the files.
pub audio_available: bool, 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 { impl CallDetail {
@@ -214,6 +217,7 @@ impl CallDetail {
merges: merge_views(merges, &speaker_names), merges: merge_views(merges, &speaker_names),
uploading: false, uploading: false,
audio_available: false, audio_available: false,
audio_deleted: call.audio_deleted,
speaker_names, speaker_names,
} }
} }
@@ -652,6 +656,7 @@ mod tests {
}], }],
"uploading": false, "uploading": false,
"audio_available": false, "audio_available": false,
"audio_deleted": false,
}) })
); );
} }
+55 -42
View File
@@ -1,8 +1,9 @@
//! Playback of a call in the detail view while its audio is still on disk: //! Playback of a call in the detail view while its audio is still on disk:
//! both tracks are mixed into one cached mono WAV //! both tracks are mixed into one cached mono WAV
//! (`<playback dir>/<id>.wav`), which the UI plays through the webview's //! (`<playback dir>/<id>.wav`), which the app serves to the main window
//! asset protocol. The cache is rebuilt only when a track is newer than it, //! only (its `playback` URI scheme). The cache is rebuilt only when a track
//! and removed with the call's audio. //! is newer than it, and removed with the call's audio and when the player
//! lets go of it.
use std::fs; use std::fs;
use std::io::{self, ErrorKind}; use std::io::{self, ErrorKind};
@@ -11,22 +12,23 @@ use std::time::SystemTime;
use serde::Serialize; 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}; 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"; 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 RECORDING: &str = "Das Gespräch wird gerade aufgenommen";
pub const PROCESSING: &str = "Das Gespräch wird gerade verarbeitet – Wiedergabe danach möglich"; 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). /// What the UI needs besides the call id (from which it builds the
const HEADER_LEN: u64 = 44; /// `playback` URL): the mix's length.
#[derive(Serialize, Clone, Copy, Debug, PartialEq)]
/// 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)]
pub struct PlaybackInfo { pub struct PlaybackInfo {
pub path: PathBuf,
pub duration_secs: f64, 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 /// 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 { pub fn audio_available(call: &Call) -> bool {
!call.audio_deleted !call.audio_deleted && (has_audio(&call.mic_path) || has_audio(&call.out_path))
&& [&call.mic_path, &call.out_path]
.iter()
.any(|path| fs::metadata(path).is_ok_and(|m| m.len() > HEADER_LEN))
} }
/// Whether a cached mix last written at `cache` can be reused for tracks /// 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 /// Returns call `call`'s mix in the playback directory `dir`, building it
/// first unless a fresh one is cached ([`is_fresh`]). Refused (see /// first unless a fresh one is cached ([`is_fresh`]). Refused (see
/// [`refusal`]) while the call records or is processed and once its audio /// [`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<PlaybackInfo, String> { pub fn prepare(call: &Call, dir: &Path) -> Result<PlaybackInfo, String> {
let path = cache_path(dir, call.id); let path = cache_path(dir, call.id);
if let Some(msg) = refusal(call) { if let Some(msg) = refusal(call) {
@@ -89,27 +89,25 @@ pub fn prepare(call: &Call, dir: &Path) -> Result<PlaybackInfo, String> {
let sources = [modified(&call.mic_path), modified(&call.out_path)]; let sources = [modified(&call.mic_path), modified(&call.out_path)];
if is_fresh(modified(&path), &sources) { if is_fresh(modified(&path), &sources) {
match duration_secs(&path) { match duration_secs(&path) {
Ok(duration_secs) => { Ok(duration_secs) => return Ok(PlaybackInfo { duration_secs }),
return Ok(PlaybackInfo {
path,
duration_secs,
})
}
Err(e) => log::warn!("call {}: rebuilding unreadable playback file: {e}", call.id), 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}"))?; let mixed =
match mix_wavs(&call.mic_path, &call.out_path, &path) { fs::create_dir_all(dir).and_then(|()| mix_wavs(&call.mic_path, &call.out_path, &path));
match mixed {
Ok(samples) => Ok(PlaybackInfo { Ok(samples) => Ok(PlaybackInfo {
path,
duration_secs: samples as f64 / SAMPLE_RATE as f64, duration_secs: samples as f64 / SAMPLE_RATE as f64,
}), }),
Err(e) if e.kind() == ErrorKind::NotFound => { Err(e) if e.kind() == ErrorKind::NotFound => {
remove_cached(dir, call.id); 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 c = recorded_call(dir.path(), CallStatus::Done);
let info = prepare(&c, &playback).unwrap(); 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); assert!((info.duration_secs - 1.0).abs() < 1e-9);
// Marked as written long ago: a rebuild would show a new time. // Marked as written long ago: a rebuild would show a new time.
let old = SystemTime::now() - Duration::from_secs(60); let old = SystemTime::now() - Duration::from_secs(60);
set_modified(&c.mic_path, old - 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(&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!(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] #[test]
@@ -264,15 +263,16 @@ mod tests {
let dir = tempfile::tempdir().unwrap(); let dir = tempfile::tempdir().unwrap();
let playback = dir.path().join("playback"); let playback = dir.path().join("playback");
let c = recorded_call(dir.path(), CallStatus::AwaitingDecision); 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); 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. // Two seconds now; the file is newer than the cache.
write_track(&c.mic_path, &vec![0.1; 32_000]); write_track(&c.mic_path, &vec![0.1; 32_000]);
let second = prepare(&c, &playback).unwrap(); let second = prepare(&c, &playback).unwrap();
assert!((second.duration_secs - 2.0).abs() < 1e-9); assert!((second.duration_secs - 2.0).abs() < 1e-9);
assert_ne!(modified(&second.path), Some(old)); assert_ne!(modified(&mix), Some(old));
} }
#[test] #[test]
@@ -300,14 +300,15 @@ mod tests {
let dir = tempfile::tempdir().unwrap(); let dir = tempfile::tempdir().unwrap();
let playback = dir.path().join("playback"); let playback = dir.path().join("playback");
let mut c = recorded_call(dir.path(), CallStatus::Done); 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. // Retention deleted the audio.
fs::remove_file(&c.mic_path).unwrap(); fs::remove_file(&c.mic_path).unwrap();
fs::remove_file(&c.out_path).unwrap(); fs::remove_file(&c.out_path).unwrap();
c.audio_deleted = true; c.audio_deleted = true;
assert_eq!(prepare(&c, &playback), Err(AUDIO_GONE.to_string())); assert_eq!(prepare(&c, &playback), Err(AUDIO_GONE.to_string()));
assert!(!info.path.exists()); assert!(!mix.exists());
} }
#[test] #[test]
@@ -315,12 +316,24 @@ mod tests {
let dir = tempfile::tempdir().unwrap(); let dir = tempfile::tempdir().unwrap();
let playback = dir.path().join("playback"); let playback = dir.path().join("playback");
let c = recorded_call(dir.path(), CallStatus::Done); 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.mic_path).unwrap();
fs::remove_file(&c.out_path).unwrap(); fs::remove_file(&c.out_path).unwrap();
assert_eq!(prepare(&c, &playback), Err(AUDIO_GONE.to_string())); assert_eq!(prepare(&c, &playback), Err(AUDIO_MISSING.to_string()));
assert!(!info.path.exists()); 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] #[test]
@@ -328,11 +341,11 @@ mod tests {
let dir = tempfile::tempdir().unwrap(); let dir = tempfile::tempdir().unwrap();
let playback = dir.path().join("playback"); let playback = dir.path().join("playback");
let c = recorded_call(dir.path(), CallStatus::Done); 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(); fs::write(playback.join("8.wav"), b"other").unwrap();
remove_cached(&playback, c.id); remove_cached(&playback, c.id);
assert!(!info.path.exists()); assert!(!cache_path(&playback, c.id).exists());
assert!(playback.join("8.wav").exists()); assert!(playback.join("8.wav").exists());
remove_cached(&playback, c.id); // already gone: no error remove_cached(&playback, c.id); // already gone: no error
+2 -1
View File
@@ -17,7 +17,7 @@ tauri-build = { version = "2.7.4", features = [] }
serde_json = "1.0" serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
log = "0.4" 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-log = "2"
tauri-plugin-autostart = "2" tauri-plugin-autostart = "2"
tauri-plugin-single-instance = "2" tauri-plugin-single-instance = "2"
@@ -30,3 +30,4 @@ tauri-plugin-updater = "2"
tauri-plugin-opener = "2" tauri-plugin-opener = "2"
tokio = { version = "1", features = ["sync"] } tokio = { version = "1", features = ["sync"] }
async-trait = "0.1" async-trait = "0.1"
http-range = "0.1.6"
+16 -4
View File
@@ -11,7 +11,6 @@ use std::time::Duration;
use recorder_core::call_view::{CallDetail, CallListItem}; use recorder_core::call_view::{CallDetail, CallListItem};
use recorder_core::capture::Devices; use recorder_core::capture::Devices;
use recorder_core::model::CallId; use recorder_core::model::CallId;
use recorder_core::playback::PlaybackInfo;
use recorder_core::secrets::Service; use recorder_core::secrets::Service;
use recorder_core::settings::Settings; use recorder_core::settings::Settings;
use serde::Serialize; use serde::Serialize;
@@ -20,7 +19,8 @@ use tauri_plugin_autostart::ManagerExt;
use crate::processing; use crate::processing;
use crate::state::{ 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); 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 /// 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] #[tauri::command]
pub async fn prepare_playback(app: AppHandle, id: CallId) -> CmdResult<PlaybackInfo> { pub async fn prepare_playback(app: AppHandle, id: CallId) -> CmdResult<PreparedPlayback> {
blocking(app, move |app| app.state::<AppState>().prepare_playback(id)).await blocking(app, move |app| app.state::<AppState>().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::<AppState>().release_playback(id, lease);
Ok(())
})
.await
}
/// Deletes a finished call's audio and row. Closes the prompt if no call /// 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. /// 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 /// While its DeepScript jobs are still on record, nothing is deleted and a
+18 -2
View File
@@ -1,6 +1,7 @@
mod backend; mod backend;
mod commands; mod commands;
mod detection; mod detection;
mod playback;
mod processing; mod processing;
mod state; mod state;
mod tray; mod tray;
@@ -43,6 +44,21 @@ pub fn run() {
.open_js_links_on_click(false) .open_js_links_on_click(false)
.build(), .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::<AppState>()
.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| { .setup(|app| {
// Release builds log to a file in the app log directory (on // Release builds log to a file in the app log directory (on
// Windows %LOCALAPPDATA%\eu.euvino.callrecorder\logs), debug // Windows %LOCALAPPDATA%\eu.euvino.callrecorder\logs), debug
@@ -72,8 +88,7 @@ pub fn run() {
let data_dir = app.path().app_data_dir()?; let data_dir = app.path().app_data_dir()?;
let config_dir = app.path().app_config_dir()?; let config_dir = app.path().app_config_dir()?;
// The asset protocol may read this directory, and only this one // Served to the main window only, by the `playback` scheme.
// (`app.security.assetProtocol.scope` in tauri.conf.json).
let playback_dir = app.path().app_cache_dir()?.join("playback"); let playback_dir = app.path().app_cache_dir()?.join("playback");
std::fs::create_dir_all(&data_dir)?; std::fs::create_dir_all(&data_dir)?;
let store = Store::open(&data_dir.join("calls.sqlite"))?; let store = Store::open(&data_dir.join("calls.sqlite"))?;
@@ -161,6 +176,7 @@ pub fn run() {
commands::unmerge_speaker, commands::unmerge_speaker,
commands::rename_call, commands::rename_call,
commands::prepare_playback, commands::prepare_playback,
commands::release_playback,
backend::backend_server, backend::backend_server,
backend::backend_status, backend::backend_status,
backend::backend_login, backend::backend_login,
+293
View File
@@ -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/<id>` on Linux
//! and macOS, `http://playback.localhost/<id>` 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 `<id>`'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<Vec<u8>>, dir: &Path) -> Response<Vec<u8>> {
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<Vec<u8>> {
empty(StatusCode::SERVICE_UNAVAILABLE)
}
/// The call id of a request path: exactly `/<digits>`.
fn call_id(path: &str) -> Option<CallId> {
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<Response<Vec<u8>>> {
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<u8>) -> Response<Vec<u8>> {
builder
.body(body)
.unwrap_or_else(|_| empty(StatusCode::INTERNAL_SERVER_ERROR))
}
fn empty(status: StatusCode) -> Response<Vec<u8>> {
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<Vec<u8>> {
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);
}
}
+103 -21
View File
@@ -7,7 +7,7 @@
//! independent of windows and events; the command layer turns results into //! independent of windows and events; the command layer turns results into
//! UI updates. //! UI updates.
use std::collections::{HashSet, VecDeque}; use std::collections::{HashMap, HashSet, VecDeque};
use std::fs; use std::fs;
use std::io::ErrorKind; use std::io::ErrorKind;
use std::path::{Path, PathBuf}; 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::detect::{CallDetector, DetectorEvent, MicSession};
use recorder_core::http::{HttpTransport, ReqwestTransport}; use recorder_core::http::{HttpTransport, ReqwestTransport};
use recorder_core::model::{Call, CallId, CallStatus, Speaker}; 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::recorder::Recorder;
use recorder_core::secrets::{SecretStore, Service}; use recorder_core::secrets::{SecretStore, Service};
use recorder_core::settings::Settings; use recorder_core::settings::Settings;
@@ -149,12 +149,12 @@ pub struct AppState {
settings: Mutex<Settings>, settings: Mutex<Settings>,
settings_path: PathBuf, settings_path: PathBuf,
recordings_dir: PathBuf, recordings_dir: PathBuf,
/// Where the mixes for playback are cached (`<app cache>/playback`, /// Where the mixes for playback are cached (`<app cache>/playback`),
/// the only directory the webview's asset protocol may read). /// served to the main window by [`crate::playback`].
playback_dir: PathBuf, playback_dir: PathBuf,
/// Held while a mix is built or removed, so a call deleted meanwhile /// The leases on cached mixes. Held while a mix is built or removed,
/// never leaves its mix behind. /// so a call deleted meanwhile never leaves its mix behind.
playback_lock: Mutex<()>, playback: Mutex<PlaybackLeases>,
current: Mutex<Option<CurrentRecording>>, current: Mutex<Option<CurrentRecording>>,
detector: Mutex<CallDetector>, detector: Mutex<CallDetector>,
/// Stopped calls awaiting the end-of-call prompt's decision, oldest /// Stopped calls awaiting the end-of-call prompt's decision, oldest
@@ -181,6 +181,23 @@ pub struct AppState {
upload_confirmed_unsaved: Mutex<HashSet<CallId>>, upload_confirmed_unsaved: Mutex<HashSet<CallId>>,
} }
/// 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<CallId, u64>,
}
/// An upload reserved with [`AppState::claim_upload`]; released when /// An upload reserved with [`AppState::claim_upload`]; released when
/// dropped, also if the upload fails or panics. /// dropped, also if the upload fails or panics.
pub struct UploadClaim<'a> { pub struct UploadClaim<'a> {
@@ -221,7 +238,7 @@ impl AppState {
settings_path, settings_path,
recordings_dir, recordings_dir,
playback_dir, playback_dir,
playback_lock: Mutex::new(()), playback: Mutex::new(PlaybackLeases::default()),
current: Mutex::new(None), current: Mutex::new(None),
detector: Mutex::new(detector), detector: Mutex::new(detector),
pending_prompts: Mutex::new(VecDeque::new()), pending_prompts: Mutex::new(VecDeque::new()),
@@ -672,29 +689,53 @@ impl AppState {
} }
/// Mixes call `id`'s tracks for playback (or reuses the cached mix, /// Mixes call `id`'s tracks for playback (or reuses the cached mix,
/// see [`playback::prepare`]). Refused while the call is recorded or /// see [`playback::prepare`]) under a new lease. Refused while the call
/// processed, and once its audio is gone. /// is recorded or processed, and once its audio is gone.
pub fn prepare_playback(&self, id: CallId) -> Result<PlaybackInfo, String> { pub fn prepare_playback(&self, id: CallId) -> Result<PreparedPlayback, String> {
if lock(&self.current) if lock(&self.current)
.as_ref() .as_ref()
.is_some_and(|cur| cur.call_id == id) .is_some_and(|cur| cur.call_id == id)
{ {
return Err(playback::RECORDING.to_string()); 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 // Read under the lock: a call deleted before it is not mixed, one
// deleted meanwhile has its mix removed once the lock is free. // deleted meanwhile has its mix removed once the lock is free.
let call = self.find_call(id)?; 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 /// The player of `lease` is done with call `id`'s mix: removes it,
/// deleted. /// 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) { 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); 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 /// Empties the playback cache (at startup), so mixed customer audio
/// never lingers from an earlier run. /// never lingers from an earlier run.
pub fn clear_playback_cache(&self) { pub fn clear_playback_cache(&self) {
@@ -1968,10 +2009,49 @@ mod tests {
let id = playable_call(&f, "a"); let id = playable_call(&f, "a");
assert!(f.state.call_detail(id).unwrap().audio_available); assert!(f.state.call_detail(id).unwrap().audio_available);
let info = f.state.prepare_playback(id).unwrap(); let prepared = f.state.prepare_playback(id).unwrap();
assert_eq!(info.path, playback_file(&f, id)); assert!((prepared.duration_secs - 1.0).abs() < 1e-9);
assert!((info.duration_secs - 1.0).abs() < 1e-9); assert!(playback_file(&f, id).exists());
assert!(info.path.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] #[test]
@@ -2021,7 +2101,9 @@ mod tests {
fs::remove_file(&call.out_path).unwrap(); fs::remove_file(&call.out_path).unwrap();
f.state.store.mark_audio_deleted(id).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!( assert_eq!(
f.state.prepare_playback(id), f.state.prepare_playback(id),
Err(playback::AUDIO_GONE.to_string()) Err(playback::AUDIO_GONE.to_string())
+1 -7
View File
@@ -44,13 +44,7 @@
} }
], ],
"security": { "security": {
"csp": null, "csp": null
"assetProtocol": {
"enable": true,
"scope": [
"$APPCACHE/playback/**"
]
}
} }
}, },
"bundle": { "bundle": {
+41 -13
View File
@@ -1,7 +1,6 @@
<script lang="ts"> <script lang="ts">
import { convertFileSrc } from '@tauri-apps/api/core'
import { onDestroy } from 'svelte' import { onDestroy } from 'svelte'
import { errorText, preparePlayback, type CallId } from './api' import { errorText, playbackUrl, preparePlayback, releasePlayback, type CallId } from './api'
import { formatOffset } from './format' import { formatOffset } from './format'
let { let {
@@ -31,6 +30,21 @@
let error = $state('') let error = $state('')
/** The mix held in memory where the webview cannot stream it (Linux). */ /** The mix held in memory where the webview cannot stream it (Linux). */
let blobUrl: string | null = null let blobUrl: string | null = null
/** This player's hold on the mix on disk (see `releasePlayback`). */
let lease: number | null = null
/** Set once the player is gone: a load still under way stops at the next
* step and keeps nothing (no blob, no mix on disk). */
let destroyed = false
const abort = new AbortController()
const LOAD_FAILED = 'Audio konnte nicht geladen werden.'
/** Lets go of the mix on disk (once). */
function release() {
if (lease === null) return
releasePlayback(callId, lease).catch(() => {})
lease = null
}
/** Mixes the call (first use only) and loads the mix into the player. */ /** Mixes the call (first use only) and loads the mix into the player. */
function load(): Promise<boolean> { function load(): Promise<boolean> {
@@ -38,29 +52,38 @@
preparing = true preparing = true
error = '' error = ''
try { try {
const info = await preparePlayback(callId) const prepared = await preparePlayback(callId)
duration = info.duration_secs lease = prepared.lease
const url = convertFileSrc(info.path) if (destroyed) return false
duration = prepared.duration_secs
const url = playbackUrl(callId)
try { try {
await loadSource(url) await loadSource(url)
} catch (err) { } catch (err) {
if (destroyed) return false
if (!(err instanceof MediaError) || err.code !== MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED) { if (!(err instanceof MediaError) || err.code !== MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED) {
throw err throw err
} }
// WebKitGTK (Linux) plays no media from a custom scheme such as // WebKitGTK (Linux) plays no media from a custom scheme such as
// asset:// (WebView2 gets http://asset.localhost instead), but // playback:// (WebView2 gets http://playback.localhost instead),
// fetches it fine: there the mix is played from memory. // but fetches it fine: there the mix is played from memory, and
const response = await fetch(url) // the file is not needed any more.
if (!response.ok) throw new Error(`Audio konnte nicht geladen werden (${response.status}).`) const response = await fetch(url, { signal: abort.signal })
blobUrl = URL.createObjectURL(await response.blob()) if (!response.ok) throw new Error(LOAD_FAILED)
const blob = await response.blob()
if (destroyed) return false
blobUrl = URL.createObjectURL(blob)
release()
await loadSource(blobUrl) await loadSource(blobUrl)
} }
return true return !destroyed
} catch (err) { } catch (err) {
error = err instanceof MediaError ? 'Audio konnte nicht geladen werden.' : errorText(err) if (destroyed) return false
error = err instanceof MediaError ? LOAD_FAILED : errorText(err)
loading = null // a later click tries again loading = null // a later click tries again
return false return false
} finally { } finally {
if (destroyed) release()
preparing = false preparing = false
} }
})() })()
@@ -127,11 +150,16 @@
} }
onDestroy(() => { onDestroy(() => {
// Lets go of the file, so the call can be deleted right away. destroyed = true
abort.abort()
// Lets go of the file and the memory, so the call can be deleted right
// away and no customer audio stays behind.
audio?.pause() audio?.pause()
audio?.removeAttribute('src') audio?.removeAttribute('src')
audio?.load() audio?.load()
if (blobUrl) URL.revokeObjectURL(blobUrl) if (blobUrl) URL.revokeObjectURL(blobUrl)
blobUrl = null
release()
onplaying?.(false) onplaying?.(false)
}) })
</script> </script>
+16 -6
View File
@@ -1,6 +1,6 @@
// Typed wrappers around the Tauri commands and events of the app shell. // 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' import { listen, type UnlistenFn } from '@tauri-apps/api/event'
export type CallId = number export type CallId = number
@@ -142,13 +142,16 @@ export interface CallDetail {
uploading: boolean uploading: boolean
/** The call's audio is still on this PC and can be played. */ /** The call's audio is still on this PC and can be played. */
audio_available: boolean 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. */ /** A call's two tracks mixed into one file, ready to play from
export interface PlaybackInfo { * `playbackUrl`. */
/** The mixed WAV in the playback cache; play it via `convertFileSrc`. */ export interface PreparedPlayback {
path: string
duration_secs: number duration_secs: number
/** Hand back with `releasePlayback` once the player is done. */
lease: number
} }
/** A newer version the release feed offers. */ /** A newer version the release feed offers. */
@@ -199,7 +202,14 @@ export const retryCall = (id: CallId) => invoke<void>('retry_call', { id })
export const discardCall = (id: CallId) => invoke<void>('discard_call', { id }) export const discardCall = (id: CallId) => invoke<void>('discard_call', { id })
/** Mixes the call's tracks (or reuses the cached mix); refused while the /** Mixes the call's tracks (or reuses the cached mix); refused while the
* call is recorded or processed and once its audio is gone. */ * call is recorded or processed and once its audio is gone. */
export const preparePlayback = (id: CallId) => invoke<PlaybackInfo>('prepare_playback', { id }) export const preparePlayback = (id: CallId) =>
invoke<PreparedPlayback>('prepare_playback', { id })
/** The player is done with the mix: it is removed from disk. */
export const releasePlayback = (id: CallId, lease: number) =>
invoke<void>('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<CallListItem[]>('list_calls') export const listCalls = () => invoke<CallListItem[]>('list_calls')
export const getCall = (id: CallId) => invoke<CallDetail>('get_call', { id }) export const getCall = (id: CallId) => invoke<CallDetail>('get_call', { id })
/** An empty name restores the default label. */ /** An empty name restores the default label. */
+10 -3
View File
@@ -166,9 +166,12 @@
}), }),
] ]
refreshList() 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() getSettings()
.then((settings) => (retentionDays = settings.retention_days)) .then((settings) => (retentionDays = settings.retention_days))
.catch(() => {}) .catch((err) => console.warn('settings not read:', errorText(err)))
getRecordingState() getRecordingState()
.then((state) => (recordingState = state)) .then((state) => (recordingState = state))
.catch((err) => (recordError = errorText(err))) .catch((err) => (recordError = errorText(err)))
@@ -514,11 +517,15 @@
> >
{/if} {/if}
</div> </div>
{:else} {:else if detail.audio_deleted && retentionDays !== null}
<p class="muted no-audio"> <p class="muted no-audio">
Audio nicht mehr vorhanden – Aufnahmen werden {retentionDays ?? '…'} Tage nach der Audio nicht mehr vorhanden – Aufnahmen werden {retentionDays} Tage nach der
Transkription gelöscht (<a href="#/settings">Einstellungen</a>). Transkription gelöscht (<a href="#/settings">Einstellungen</a>).
</p> </p>
{:else if detail.audio_deleted}
<p class="muted no-audio">Audio nicht mehr vorhanden.</p>
{:else}
<p class="muted no-audio">Audio nicht vorhanden.</p>
{/if} {/if}
{/if} {/if}