Symphonia now has all its pure-Rust codecs and formats. What it can't decode (Opus, WMA, AC-3, the sound of video files) goes through the ffmpeg CLI: ffprobe for rate and length, raw f32 PCM piped into the same output, seeking by restarting with -ss, the child killed on stop. Missing ffmpeg keeps the unsupported error. A Handle lets another thread close the playing file while it is rewritten and resume it in place (D-041). Co-Authored-By: Claude Opus 5.5 <[email protected]>
131 lines
4.6 KiB
Rust
131 lines
4.6 KiB
Rust
//! The ffmpeg fallback on files symphonia can't decode; skipped without ffmpeg. No audio device
|
|
//! is needed: opening a track and computing its waveform both decode without output.
|
|
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::Command;
|
|
use std::sync::mpsc;
|
|
use std::time::Duration;
|
|
|
|
use tunal_player::{Event, Player, Waveforms, ffmpeg_available};
|
|
|
|
/// Runs ffmpeg with `args` to make `name` in `dir` from a one-second 440 Hz tone (plus a
|
|
/// test picture when `video`).
|
|
fn make(dir: &Path, name: &str, video: bool, args: &[&str]) -> PathBuf {
|
|
let out = dir.join(name);
|
|
let mut cmd = Command::new("ffmpeg");
|
|
cmd.args(["-nostdin", "-v", "error", "-y"]);
|
|
if video {
|
|
cmd.args(["-f", "lavfi", "-i", "testsrc=duration=1:size=64x48:rate=10"]);
|
|
}
|
|
cmd.args(["-f", "lavfi", "-i", "sine=frequency=440:duration=1"])
|
|
.args(args)
|
|
.arg(&out);
|
|
assert!(cmd.status().unwrap().success(), "ffmpeg made {name}");
|
|
out
|
|
}
|
|
|
|
fn peaks_and_length(path: &Path) -> (f32, f64) {
|
|
let (tx, rx) = mpsc::channel();
|
|
Waveforms::new().request(path, 16, move |r| tx.send(r).unwrap());
|
|
let wave = rx.recv_timeout(Duration::from_secs(30)).unwrap().unwrap();
|
|
let top = wave.peaks.iter().map(|p| p.1).fold(0.0, f32::max);
|
|
(top, wave.duration.as_secs_f64())
|
|
}
|
|
|
|
/// Opens `path` in a player and returns the events up to the first position.
|
|
fn open_events(player: &Player, rx: &mpsc::Receiver<Event>, path: &Path) -> Vec<Event> {
|
|
player.open(path);
|
|
let mut events = Vec::new();
|
|
loop {
|
|
let event = rx.recv_timeout(Duration::from_secs(10)).unwrap();
|
|
let last = matches!(event, Event::Position(_) | Event::Error(_));
|
|
events.push(event);
|
|
if last {
|
|
return events;
|
|
}
|
|
}
|
|
}
|
|
|
|
fn opened_duration(events: &[Event]) -> Option<Duration> {
|
|
events.iter().find_map(|e| match e {
|
|
Event::Opened { duration, .. } => *duration,
|
|
_ => None,
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn opus_and_video_containers_decode_through_ffmpeg() {
|
|
if !ffmpeg_available() {
|
|
eprintln!("skipped: no ffmpeg");
|
|
return;
|
|
}
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let files = [
|
|
make(dir.path(), "t.opus", false, &["-c:a", "libopus"]),
|
|
make(dir.path(), "-dash t.opus", false, &["-c:a", "libopus"]),
|
|
make(
|
|
dir.path(),
|
|
"v.mkv",
|
|
true,
|
|
&["-c:v", "mpeg4", "-c:a", "libopus"],
|
|
),
|
|
make(dir.path(), "v.avi", true, &["-c:v", "mpeg4", "-c:a", "ac3"]),
|
|
];
|
|
let (tx, rx) = mpsc::channel();
|
|
let player = Player::new(move |e| {
|
|
let _ = tx.send(e);
|
|
});
|
|
for path in &files {
|
|
let name = path.file_name().unwrap().to_string_lossy();
|
|
let (top, secs) = peaks_and_length(path);
|
|
// lavfi's sine is at 1/8 of full scale.
|
|
assert!(top > 0.05, "{name}: peak {top}");
|
|
assert!((0.9..=1.2).contains(&secs), "{name}: {secs} s of samples");
|
|
let events = open_events(&player, &rx, path);
|
|
let duration = opened_duration(&events).unwrap_or_else(|| panic!("{name}: {events:?}"));
|
|
assert!(
|
|
(0.9..=1.2).contains(&duration.as_secs_f64()),
|
|
"{name}: {duration:?}"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_released_track_comes_back_where_it_was() {
|
|
if !ffmpeg_available() {
|
|
eprintln!("skipped: no ffmpeg");
|
|
return;
|
|
}
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = make(dir.path(), "t.opus", false, &["-c:a", "libopus"]);
|
|
let other = make(dir.path(), "o.opus", false, &["-c:a", "libopus"]);
|
|
let (tx, rx) = mpsc::channel();
|
|
let player = Player::new(move |e| {
|
|
let _ = tx.send(e);
|
|
});
|
|
open_events(&player, &rx, &path);
|
|
player.seek(Duration::from_millis(400));
|
|
assert!(matches!(
|
|
rx.recv_timeout(Duration::from_secs(10)).unwrap(),
|
|
Event::Position(p) if p == Duration::from_millis(400)
|
|
));
|
|
let handle = player.handle();
|
|
// Another file: nothing to let go of.
|
|
assert_eq!(handle.without(&other, || 1), 1);
|
|
// The open track: closed while `f` runs (it may rewrite the file), then reopened.
|
|
let copy = dir.path().join("copy.opus");
|
|
handle.without(&path, || std::fs::copy(&path, ©).unwrap());
|
|
let mut events = Vec::new();
|
|
while let Ok(event) = rx.recv_timeout(Duration::from_secs(10)) {
|
|
let last = matches!(event, Event::Position(_));
|
|
events.push(event);
|
|
if last {
|
|
break;
|
|
}
|
|
}
|
|
assert!(opened_duration(&events).is_some(), "{events:?}");
|
|
assert!(
|
|
matches!(events.last(), Some(Event::Position(p)) if *p == Duration::from_millis(400)),
|
|
"{events:?}"
|
|
);
|
|
}
|