Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/opentake-media/src/decode/pcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ pub fn extract_pcm(path: &Path, spec: &PcmSpec, range: Option<(f64, f64)>) -> Re
if !status.success() && raw.is_empty() {
return Err(MediaError::no_track("audio", path));
}
// ffmpeg can exit 0 with empty stdout when metadata says audio exists but
// no decodable samples: treat as no audio track so the waveform cache
// isn't poisoned with all-1.0 silence.
if raw.is_empty() {
return Err(MediaError::no_track("audio", path));
}

let samples = raw_to_mono_f32(&raw, spec);
Ok(PcmBuffer {
Expand Down
25 changes: 25 additions & 0 deletions crates/opentake-media/src/waveform/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ pub fn load_waveform(cache_root: &Path, key: &str) -> Option<Vec<f32>> {
while let Ok(v) = cursor.read_f32::<LittleEndian>() {
out.push(v);
}
// Reject poison files: every bucket == 1.0 silence is what an older build
// cached when extract_pcm returned an empty Vec. Force regeneration.
if !out.is_empty() && out.iter().all(|&v| v == 1.0) {
return None;
}
Some(out)
}

Expand Down Expand Up @@ -97,6 +102,26 @@ mod tests {
assert!(load_waveform(dir.path(), "bad").is_none());
}

#[test]
fn load_all_ones_poison_cache_is_none() {
// An older build cached all-1.0 buckets when extract_pcm returned an
// empty Vec for a "silent" file; treat such a file as poison so the
// waveform is regenerated instead of rendered as a flat green band.
let dir = tempfile::tempdir().unwrap();
save_waveform(dir.path(), "poison", &[1.0f32; 8]).unwrap();
assert!(load_waveform(dir.path(), "poison").is_none());
}

#[test]
fn load_mixed_values_with_some_ones_is_kept() {
// A real waveform whose peaks happen to reach 1.0 must NOT be discarded;
// only an all-1.0 file is poison.
let dir = tempfile::tempdir().unwrap();
let samples = vec![1.0f32, 0.5, 1.0, 0.0];
save_waveform(dir.path(), "real", &samples).unwrap();
assert_eq!(load_waveform(dir.path(), "real").unwrap(), samples);
}

#[test]
fn little_endian_byte_layout_is_fixed() {
let dir = tempfile::tempdir().unwrap();
Expand Down
28 changes: 28 additions & 0 deletions crates/opentake-media/tests/ffmpeg_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,34 @@ fn extract_pcm_no_audio_track_errors() {
);
}

#[test]
fn extract_pcm_empty_stdout_errors_not_silent_buffer() {
// Regression for the "ffmpeg exits 0 but emits no PCM" case: a range window
// entirely past the media end seeks past EOF, so ffmpeg succeeds yet writes
// zero bytes. extract_pcm must surface NoTrack rather than return an empty
// buffer — an empty buffer downstream poisons the waveform cache with an
// all-1.0 flat band.
if !ffmpeg_available() {
return;
}
let dir = tempfile::tempdir().unwrap();
let av = dir.path().join("av.mp4");
if !make_av(&av) {
return;
}
let spec = PcmSpec {
sample_rate: 16_000,
channels: 1,
format: PcmFormat::F32,
};
// The clip is ~2 s long; ask for [5.0, 6.0) → past EOF → empty stdout.
let res = extract_pcm(&av, &spec, Some((5.0, 6.0)));
assert!(
res.is_err(),
"expected an error for an empty PCM window, not a silent buffer"
);
}

#[test]
fn waveform_has_expected_bucket_count() {
if !ffmpeg_available() {
Expand Down
Loading