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:
@@ -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<hound::WavIntoSamples<BufReader<File>, 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<Track> {
|
||||
/// 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<i16>) -> 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<u64> {
|
||||
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")));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
+55
-42
@@ -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
|
||||
//! (`<playback dir>/<id>.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.
|
||||
//! (`<playback dir>/<id>.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<PlaybackInfo, String> {
|
||||
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<PlaybackInfo, 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,
|
||||
})
|
||||
}
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user