Mulberry/tests/playback.rs

69 lines
2.6 KiB
Rust

//! Real playback through libmpv (null audio output): the displayed position must never jump backwards.
use eframe::egui;
use mulberry::bus::{Bus, Msg};
use mulberry::library::Library;
use mulberry::player::Player;
use mulberry::playlist::Manager;
use mulberry::song::Song;
use std::sync::mpsc::channel;
use std::time::{Duration, Instant};
#[test]
fn position_is_monotonic_and_smooth() {
let sc = std::env::temp_dir().join(format!("mulberry-pbtest-{}", std::process::id()));
std::fs::create_dir_all(&sc).unwrap();
std::env::set_var("MULBERRY_TEST", "1");
std::env::set_var("XDG_DATA_HOME", sc.join("data"));
std::env::set_var("XDG_CONFIG_HOME", sc.join("config"));
std::env::set_var("XDG_CACHE_HOME", sc.join("cache"));
let file = sc.join("tone.flac");
assert!(std::process::Command::new("ffmpeg").args(["-v", "quiet", "-y", "-f", "lavfi", "-i", "sine=frequency=330:duration=20"]).arg(&file).status().unwrap().success());
let ctx = egui::Context::default();
let (tx, rx) = channel();
let bus = Bus::new(tx, ctx.clone());
let lib = Library::new();
let mut mgr = Manager::new();
let mut s = Song::new(file.to_string_lossy());
s.length = 20.0;
let pl = mgr.new_playlist(&lib, Some("t"), vec![s], true);
let uid = mgr.pl(pl).unwrap().entries[0].uid;
let mut player = Player::new(bus);
player.play_song(&mut mgr, &lib, (pl, uid), 0.0, false);
let start = Instant::now();
let mut last = -1.0f64;
let mut max_step = 0.0f64;
let mut backwards = 0;
let mut samples = 0;
let mut seeked = false;
while start.elapsed() < Duration::from_secs(8) {
while let Ok(m) = rx.try_recv() {
if let Msg::Engine(id, ev) = m {
player.on_engine(&mut mgr, &lib, id, ev);
}
}
player.tick(&mut mgr, &lib);
if start.elapsed() > Duration::from_secs(4) && !seeked {
seeked = true;
player.seek(14.0);
last = -1.0;
}
let p = player.pos();
if p < last - 1e-9 {
backwards += 1;
eprintln!("BACKWARDS {last:.4} -> {p:.4} at {:?}", start.elapsed());
}
if last >= 0.0 {
max_step = max_step.max(p - last);
}
last = p;
samples += 1;
std::thread::sleep(Duration::from_millis(33));
}
eprintln!("samples={samples} backwards={backwards} max_step={max_step:.3} final={last:.2} state={}", player.state);
assert_eq!(backwards, 0, "position went backwards");
assert!(max_step < 0.25, "position jumped by {max_step}");
assert!(last > 14.5, "did not advance after seek: {last}");
}