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:
+103
-21
@@ -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>,
|
||||
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).
|
||||
/// Where the mixes for playback are cached (`<app cache>/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<PlaybackLeases>,
|
||||
current: Mutex<Option<CurrentRecording>>,
|
||||
detector: Mutex<CallDetector>,
|
||||
/// Stopped calls awaiting the end-of-call prompt's decision, oldest
|
||||
@@ -181,6 +181,23 @@ pub struct AppState {
|
||||
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
|
||||
/// 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<PlaybackInfo, String> {
|
||||
/// 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<PreparedPlayback, 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);
|
||||
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())
|
||||
|
||||
Reference in New Issue
Block a user