feat #14740: play back calls

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-10 13:37:47 +01:00
co-authored by Claude Opus 5.5
parent 8d757fd639
commit a74cfa5864
16 changed files with 1335 additions and 12 deletions
+162 -3
View File
@@ -2,9 +2,10 @@
//!
//! Holds the store, the settings, the secret store, the HTTP transport, the
//! recording that is currently running, the call detector, the calls
//! awaiting a decision in the end-of-call prompt and the calls being
//! processed. Everything here is independent of windows and
//! events; the command layer turns results into UI updates.
//! awaiting a decision in the end-of-call prompt, the calls being
//! processed and the cached mixes for playback. Everything here is
//! independent of windows and events; the command layer turns results into
//! UI updates.
use std::collections::{HashSet, VecDeque};
use std::fs;
@@ -23,6 +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::recorder::Recorder;
use recorder_core::secrets::{SecretStore, Service};
use recorder_core::settings::Settings;
@@ -147,6 +149,12 @@ pub struct AppState {
settings: Mutex<Settings>,
settings_path: PathBuf,
recordings_dir: PathBuf,
/// Where the mixes for playback are cached (`<app cache>/playback`,
/// the only directory the webview's asset protocol may read).
playback_dir: PathBuf,
/// Held while a mix is built or removed, so a call deleted meanwhile
/// never leaves its mix behind.
playback_lock: Mutex<()>,
current: Mutex<Option<CurrentRecording>>,
detector: Mutex<CallDetector>,
/// Stopped calls awaiting the end-of-call prompt's decision, oldest
@@ -201,6 +209,7 @@ impl AppState {
secrets: Box<dyn SecretStore>,
settings_path: PathBuf,
recordings_dir: PathBuf,
playback_dir: PathBuf,
) -> Self {
let settings = Settings::load(&settings_path);
let detector = CallDetector::new(detector_config(&settings));
@@ -211,6 +220,8 @@ impl AppState {
settings: Mutex::new(settings),
settings_path,
recordings_dir,
playback_dir,
playback_lock: Mutex::new(()),
current: Mutex::new(None),
detector: Mutex::new(detector),
pending_prompts: Mutex::new(VecDeque::new()),
@@ -656,9 +667,44 @@ impl AppState {
&self.settings().language,
);
detail.uploading = self.is_uploading(id);
detail.audio_available = playback::audio_available(&call);
Ok(detail)
}
/// 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<PlaybackInfo, String> {
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);
// 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)
}
/// Removes call `id`'s cached mix, once the call or its audio is
/// deleted.
pub fn remove_playback(&self, id: CallId) {
let _building = lock(&self.playback_lock);
playback::remove_cached(&self.playback_dir, id);
}
/// Empties the playback cache (at startup), so mixed customer audio
/// never lingers from an earlier run.
pub fn clear_playback_cache(&self) {
let dir = &self.playback_dir;
match playback::clear_cache(dir) {
Ok(()) => log::info!("cleared the playback cache {}", dir.display()),
Err(e) => log::warn!("could not empty {}: {e}", dir.display()),
}
}
/// Renames a speaker of call `id`; a blank `name` restores the default
/// label ("Ich", "Sprecher N").
pub fn rename_speaker(&self, id: CallId, speaker_key: &str, name: &str) -> Result<(), String> {
@@ -789,6 +835,7 @@ impl AppState {
}
drop(uploading);
drop(processing);
self.remove_playback(id);
Ok(self.prompts_waiting())
}
@@ -967,6 +1014,7 @@ mod tests {
Box::new(MemorySecrets::default()),
dir.path().join("settings.json"),
dir.path().join("recordings"),
dir.path().join("playback"),
);
Fixture { state, dir }
}
@@ -1897,4 +1945,115 @@ mod tests {
OutputTarget::Device("id-Lautsprecher".into())
);
}
/// A finished call whose tracks hold real audio: one second on the mic,
/// half a second on the output.
fn playable_call(f: &Fixture, name: &str) -> CallId {
let (id, mic, out) = finished_call(f, name);
for (path, samples) in [(&mic, 16_000), (&out, 8_000)] {
let mut writer = recorder_core::audio::TrackWriter::create(path).unwrap();
writer.write(&vec![0.1; samples]).unwrap();
writer.finalize().unwrap();
}
id
}
fn playback_file(f: &Fixture, id: CallId) -> PathBuf {
f.dir.path().join("playback").join(format!("{id}.wav"))
}
#[test]
fn a_finished_call_is_mixed_into_the_playback_cache() {
let f = fixture();
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());
}
#[test]
fn recording_and_processing_calls_are_not_played() {
let f = fixture();
let dir = f.dir.path();
let recording = f
.state
.store
.create_call(
"Microsoft Teams",
at(10, 0, 0),
&dir.join("r-mic.wav"),
&dir.join("r-out.wav"),
)
.unwrap();
assert_eq!(
f.state.prepare_playback(recording),
Err(playback::RECORDING.to_string())
);
let processing = playable_call(&f, "p");
f.state
.store
.set_status(processing, CallStatus::Processing, None)
.unwrap();
assert_eq!(
f.state.prepare_playback(processing),
Err(playback::PROCESSING.to_string())
);
assert!(!playback_file(&f, processing).exists());
assert_eq!(
f.state.prepare_playback(999),
Err("Gespräch nicht gefunden".to_string())
);
}
#[test]
fn deleted_audio_is_not_played_and_its_mix_is_removed() {
let f = fixture();
let id = playable_call(&f, "a");
f.state.prepare_playback(id).unwrap();
// What the retention sweep does to a transcribed call's audio.
let call = f.state.store.get_call(id).unwrap().unwrap();
fs::remove_file(&call.mic_path).unwrap();
fs::remove_file(&call.out_path).unwrap();
f.state.store.mark_audio_deleted(id).unwrap();
assert!(!f.state.call_detail(id).unwrap().audio_available);
assert_eq!(
f.state.prepare_playback(id),
Err(playback::AUDIO_GONE.to_string())
);
assert!(!playback_file(&f, id).exists());
}
#[test]
fn deleting_a_call_removes_its_mix() {
let f = fixture();
let id = playable_call(&f, "a");
let other = playable_call(&f, "b");
f.state.prepare_playback(id).unwrap();
f.state.prepare_playback(other).unwrap();
f.state.discard_call(id).unwrap();
assert!(!playback_file(&f, id).exists());
assert!(playback_file(&f, other).exists());
assert_eq!(
f.state.prepare_playback(id),
Err("Gespräch nicht gefunden".to_string())
);
}
#[test]
fn the_playback_cache_is_emptied_at_startup() {
let f = fixture();
let id = playable_call(&f, "a");
f.state.prepare_playback(id).unwrap();
f.state.clear_playback_cache();
assert!(!f.dir.path().join("playback").exists());
f.state.clear_playback_cache(); // nothing left: no error
}
}