//! 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. use std::fs; use std::io::{self, ErrorKind}; use std::path::{Path, PathBuf}; use std::time::SystemTime; use serde::Serialize; use crate::audio::{mix_wavs, SAMPLE_RATE}; use crate::model::{Call, CallId, CallStatus}; /// The call's audio was deleted (retention, or the files are gone). pub const AUDIO_GONE: &str = "Audio nicht mehr vorhanden"; 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)] pub struct PlaybackInfo { pub path: PathBuf, pub duration_secs: f64, } /// Why `call` cannot be played, if it cannot: its tracks are still being /// written (recording) or read for transcription (processing), or its /// audio was deleted. pub fn refusal(call: &Call) -> Option<&'static str> { match call.status { CallStatus::Recording => Some(RECORDING), CallStatus::Processing => Some(PROCESSING), _ if call.audio_deleted => Some(AUDIO_GONE), _ => None, } } /// Whether `call` still has audio to play: not deleted, and at least one /// track holds more than a bare header. 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)) } /// Whether a cached mix last written at `cache` can be reused for tracks /// last written at `sources` (`None`: that track does not exist). It can if /// it exists and is newer than every existing track; with no track left /// there is nothing it could still stand for. pub fn is_fresh(cache: Option, sources: &[Option]) -> bool { let Some(cache) = cache else { return false; }; let mut any = false; for source in sources.iter().flatten() { if *source >= cache { return false; } any = true; } any } /// Where call `id`'s mix is cached in the playback directory `dir`. pub fn cache_path(dir: &Path, id: CallId) -> PathBuf { dir.join(format!("{id}.wav")) } /// 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. pub fn prepare(call: &Call, dir: &Path) -> Result { let path = cache_path(dir, call.id); if let Some(msg) = refusal(call) { if msg == AUDIO_GONE { remove_cached(dir, call.id); } return Err(msg.to_string()); } 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, }) } 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) { 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(e) => Err(format!("Audio konnte nicht vorbereitet werden: {e}")), } } /// Removes call `id`'s cached mix, if any (with the call or its audio). /// Failures are logged: the playback directory is emptied at every start. pub fn remove_cached(dir: &Path, id: CallId) { let path = cache_path(dir, id); match fs::remove_file(&path) { Ok(()) => {} Err(e) if e.kind() == ErrorKind::NotFound => {} Err(e) => log::warn!("could not remove {}: {e}", path.display()), } } /// Removes every cached mix (at startup, so customer audio never lingers /// in the cache). pub fn clear_cache(dir: &Path) -> io::Result<()> { match fs::remove_dir_all(dir) { Err(e) if e.kind() == ErrorKind::NotFound => Ok(()), other => other, } } fn modified(path: &Path) -> Option { fs::metadata(path).and_then(|m| m.modified()).ok() } fn duration_secs(path: &Path) -> Result { let reader = hound::WavReader::open(path)?; Ok(reader.duration() as f64 / reader.spec().sample_rate as f64) } #[cfg(test)] mod tests { use std::fs::File; use std::time::Duration; use chrono::Local; use super::*; use crate::audio::TrackWriter; fn call(dir: &Path, status: CallStatus) -> Call { Call { id: 7, app: "Microsoft Teams".to_string(), started_at: Local::now(), ended_at: Some(Local::now()), status, interrupted: false, error: None, mic_path: dir.join("7-mic.wav"), out_path: dir.join("7-out.wav"), transcribed_at: None, audio_deleted: false, title: None, crm_transcript_id: None, crm_base_url: None, uploaded_at: None, } } fn write_track(path: &Path, samples: &[f32]) { let mut writer = TrackWriter::create(path).unwrap(); writer.write(samples).unwrap(); writer.finalize().unwrap(); } /// A call with one second on the mic and half a second on the output. fn recorded_call(dir: &Path, status: CallStatus) -> Call { let call = call(dir, status); write_track(&call.mic_path, &vec![0.1; 16_000]); write_track(&call.out_path, &vec![0.2; 8_000]); call } fn set_modified(path: &Path, time: SystemTime) { File::options() .write(true) .open(path) .unwrap() .set_modified(time) .unwrap(); } #[test] fn a_cache_newer_than_every_track_is_fresh() { let t = SystemTime::UNIX_EPOCH + Duration::from_secs(1_000); let s = Duration::from_secs(1); assert!(is_fresh(Some(t), &[Some(t - s), Some(t - s)])); assert!(is_fresh(Some(t), &[None, Some(t - s)])); assert!(!is_fresh(Some(t), &[Some(t - s), Some(t + s)])); assert!(!is_fresh(Some(t), &[Some(t), Some(t - s)]), "same time"); assert!(!is_fresh(None, &[Some(t - s), Some(t - s)]), "no cache"); assert!(!is_fresh(Some(t), &[None, None]), "no tracks"); } #[test] fn recording_processing_and_deleted_calls_are_refused() { let dir = Path::new("/nonexistent"); assert_eq!(refusal(&call(dir, CallStatus::Recording)), Some(RECORDING)); assert_eq!( refusal(&call(dir, CallStatus::Processing)), Some(PROCESSING) ); let mut deleted = call(dir, CallStatus::Done); deleted.audio_deleted = true; assert_eq!(refusal(&deleted), Some(AUDIO_GONE)); for status in [ CallStatus::AwaitingDecision, CallStatus::Done, CallStatus::Failed, ] { assert_eq!(refusal(&call(dir, status)), None, "{status:?}"); } } #[test] fn audio_is_available_while_a_track_holds_audio() { let dir = tempfile::tempdir().unwrap(); let mut c = call(dir.path(), CallStatus::Done); assert!(!audio_available(&c), "no files"); write_track(&c.out_path, &[]); assert!(!audio_available(&c), "header only"); write_track(&c.mic_path, &[0.1; 10]); assert!(audio_available(&c)); c.audio_deleted = true; assert!(!audio_available(&c), "deleted by retention"); } #[test] fn the_mix_is_built_once_and_reused() { 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(); assert_eq!(info.path, playback.join("7.wav")); 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); assert_eq!(prepare(&c, &playback).unwrap(), info); assert_eq!(modified(&info.path), Some(old), "reused, not rebuilt"); } #[test] fn the_mix_is_rebuilt_when_a_track_is_newer() { 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(); let old = SystemTime::now() - Duration::from_secs(60); set_modified(&first.path, 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)); } #[test] fn a_call_with_one_track_plays_that_track() { let dir = tempfile::tempdir().unwrap(); let c = call(dir.path(), CallStatus::Failed); write_track(&c.out_path, &vec![0.2; 4_000]); let info = prepare(&c, &dir.path().join("playback")).unwrap(); assert!((info.duration_secs - 0.25).abs() < 1e-9); } #[test] fn a_refused_call_builds_nothing() { let dir = tempfile::tempdir().unwrap(); let playback = dir.path().join("playback"); for status in [CallStatus::Recording, CallStatus::Processing] { let c = recorded_call(dir.path(), status); assert!(prepare(&c, &playback).is_err()); assert!(!cache_path(&playback, c.id).exists()); } } #[test] fn deleted_audio_removes_the_cached_mix() { 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(); // 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()); } #[test] fn missing_files_remove_the_cached_mix() { 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(); 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()); } #[test] fn the_cache_is_removed_per_call_and_cleared_as_a_whole() { 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(); fs::write(playback.join("8.wav"), b"other").unwrap(); remove_cached(&playback, c.id); assert!(!info.path.exists()); assert!(playback.join("8.wav").exists()); remove_cached(&playback, c.id); // already gone: no error clear_cache(&playback).unwrap(); assert!(!playback.exists()); clear_cache(&playback).unwrap(); // already gone: no error } }