Files
Remus/src/player.rs
T
Ansho Rei 6997e7cd93 fix: show the picture in the playing track's file on the now-playing screen
Nextcloud Music gives every song of an album the album's cover id, so
tracks with their own art all showed the album cover. The player now
reads the picture embedded in each file it decodes and reports it once
per track, or reports that there is none. The now-playing art, backdrop
and palette use it, and wait for that answer so they do not show the
album cover first and then change. Lists and the queue keep the
server's cover.

Pictures are read where each file's decoder hands on its stream:
further down, a file without a picture still carries the previous
file's tags after a gapless switch.
2026-11-04 20:22:03 +01:00

726 lines
28 KiB
Rust

//! Playback through GStreamer's playbin. The queue lives here so the next
//! track can be handed to playbin in `about-to-finish`, which keeps playback
//! gapless.
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};
use anyhow::{Context, Result};
use gstreamer as gst;
use gstreamer::prelude::*;
#[derive(Debug, Clone)]
pub enum PlayerEvent {
Position(f64),
Duration(f64),
Paused(bool),
/// Index into the queue, None when playback has stopped.
Track(Option<usize>),
/// The picture embedded in a track's file, or None once it is clear the
/// file has none. Sent once per track started, after its `Track`. A
/// picture that turns up late is still sent, after the None.
Art(usize, Option<Vec<u8>>),
}
#[derive(Default)]
struct Queue {
urls: Vec<String>,
current: Option<usize>,
/// Handed to playbin, becomes `current` at its stream-start.
pending: Option<usize>,
/// Loaded paused by `load_paused` and not played since: a track that
/// fails is skipped without starting playback.
hold: bool,
/// Seconds to seek to once the paused track has prerolled.
seek_to: Option<f64>,
}
/// Pictures found in the files being decoded, by URI.
type Pictures = Arc<Mutex<HashMap<String, Vec<u8>>>>;
pub struct Player {
playbin: gst::Element,
queue: Arc<Mutex<Queue>>,
pictures: Pictures,
stop: Arc<AtomicBool>,
bus_thread: Option<JoinHandle<()>>,
}
const TICK: Duration = Duration::from_millis(250);
/// How long after a track starts its tags can still bring a picture. Tags
/// come ahead of the audio, so this is mostly a slow server's margin.
const ART_WAIT: Duration = Duration::from_millis(600);
impl Player {
pub fn spawn(on_event: impl Fn(PlayerEvent) + Send + 'static) -> Result<Player> {
gst::init().context("could not initialise GStreamer")?;
let playbin = gst::ElementFactory::make("playbin")
.build()
.context("GStreamer's playbin is missing (gst-plugins-base)")?;
if let Ok(sink) = gst::ElementFactory::make("fakesink").build() {
playbin.set_property("video-sink", &sink);
}
// Tests and headless runs: REMUS_AUDIO_SINK=fakesink plays in real time without a device.
if let Ok(name) = std::env::var("REMUS_AUDIO_SINK")
&& let Ok(sink) = gst::ElementFactory::make(&name).property("sync", true).build()
{
playbin.set_property("audio-sink", &sink);
}
let queue = Arc::new(Mutex::new(Queue::default()));
let q = queue.clone();
playbin.connect("about-to-finish", false, move |args| {
let playbin = args[0].get::<gst::Element>().ok()?;
let mut q = lock(&q);
let next = q.current.map_or(0, |c| c + 1);
if let Some(url) = q.urls.get(next).cloned() {
q.pending = Some(next);
playbin.set_property("uri", url);
}
None
});
let pictures = Pictures::default();
watch_pictures(&playbin, &pictures);
let stop = Arc::new(AtomicBool::new(false));
let bus_thread = std::thread::Builder::new().name("gst-bus".into()).spawn({
let (playbin, queue, pictures, stop) = (playbin.clone(), queue.clone(), pictures.clone(), stop.clone());
move || watch_bus(&playbin, &queue, &pictures, &stop, &on_event)
})?;
Ok(Player {
playbin,
queue,
pictures,
stop,
bus_thread: Some(bus_thread),
})
}
/// Replaces the queue with `urls` and starts at `start`.
pub fn play_list(&self, urls: &[String], start: usize) {
{
let mut q = lock(&self.queue);
q.urls = urls.to_vec();
q.current = None;
q.pending = None;
}
self.play_index(start);
}
/// Adds to the end of the queue, starting playback if it had stopped.
pub fn append(&self, url: &str) {
let start = {
let mut q = lock(&self.queue);
q.urls.push(url.to_string());
(q.current.is_none() && q.pending.is_none()).then(|| q.urls.len() - 1)
};
if let Some(i) = start {
self.play_index(i);
}
}
/// Replaces the queue with `urls` and readies `index` paused at
/// `position` seconds, so nothing plays until `toggle_pause`. With no
/// index the queue is only stored, as after it played to the end.
pub fn load_paused(&self, urls: &[String], index: Option<usize>, position: f64) {
let url = {
let mut q = lock(&self.queue);
q.urls = urls.to_vec();
q.current = None;
q.pending = None;
q.hold = false;
q.seek_to = None;
let Some(i) = index else { return };
let Some(url) = q.urls.get(i).cloned() else { return };
q.pending = Some(i);
q.hold = true;
q.seek_to = (position > 0.0).then_some(position);
url
};
start_paused(&self.playbin, &url);
}
pub fn play_index(&self, index: usize) {
let url = {
let mut q = lock(&self.queue);
let Some(url) = q.urls.get(index).cloned() else { return };
q.pending = Some(index);
q.hold = false;
q.seek_to = None;
url
};
// Not under the lock: a state change waits for the streaming threads,
// and about-to-finish runs on one of them and takes the lock.
start(&self.playbin, &url);
}
/// Stops playback and forgets the queue, as when switching server. Going
/// to Null flushes the bus, so nothing from the old track reports later.
pub fn stop(&self) {
*lock(&self.queue) = Queue::default();
lock(&self.pictures).clear();
let _ = self.playbin.set_state(gst::State::Null);
}
pub fn toggle_pause(&self) {
// Toggles where the pipeline is heading, so play pressed while a
// paused track is still loading is not lost.
let (_, current, pending) = self.playbin.state(gst::ClockTime::ZERO);
let heading = if pending == gst::State::VoidPending {
current
} else {
pending
};
let target = match heading {
gst::State::Playing => gst::State::Paused,
gst::State::Paused => gst::State::Playing,
_ => return,
};
if target == gst::State::Playing {
lock(&self.queue).hold = false;
}
let _ = self.playbin.set_state(target);
}
pub fn next(&self) {
let next = lock(&self.queue).current.map(|c| c + 1);
if let Some(i) = next {
self.play_index(i);
}
}
pub fn prev(&self) {
let prev = lock(&self.queue).current.map(|c| c.saturating_sub(1));
if let Some(i) = prev {
self.play_index(i);
}
}
/// Sets remus's own playback volume, 0 to 100. Cubic, so equal steps
/// sound equally loud apart.
pub fn set_volume(&self, level: u8) {
let v = level.min(100) as f64 / 100.0;
self.playbin.set_property("volume", v * v * v);
}
pub fn set_muted(&self, muted: bool) {
self.playbin.set_property("mute", muted);
}
pub fn seek(&self, seconds: f64) {
let Some(pos) = self.playbin.query_position::<gst::ClockTime>() else {
return;
};
seek_to(&self.playbin, pos.seconds_f64() + seconds);
}
}
impl Drop for Player {
fn drop(&mut self) {
self.stop.store(true, Ordering::Relaxed);
let _ = self.playbin.set_state(gst::State::Null);
if let Some(t) = self.bus_thread.take() {
let _ = t.join();
}
}
}
fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
m.lock().unwrap_or_else(|e| e.into_inner())
}
/// Records the picture in each file playbin decodes, keyed by its URI. Read
/// where each file's decoder hands on its stream, because further down the
/// tags of a file without a picture still carry the previous file's, once
/// playback moves on gaplessly.
fn watch_pictures(playbin: &gst::Element, pictures: &Pictures) {
let Some(bin) = playbin.downcast_ref::<gst::Bin>() else {
return;
};
let pictures = pictures.clone();
bin.connect_deep_element_added(move |_, _, element| {
let decoder = element
.factory()
.is_some_and(|f| matches!(f.name().as_str(), "uridecodebin" | "uridecodebin3"));
// playbin adds the same decoder again for later tracks.
// SAFETY: the key is only ever set to a bool, here.
if !decoder || unsafe { element.data::<bool>("remus-pictures") }.is_some() {
return;
}
unsafe { element.set_data("remus-pictures", true) };
let pictures = pictures.clone();
element.connect_pad_added(move |decoder, pad| {
let decoder = decoder.downgrade();
let pictures = pictures.clone();
pad.add_probe(gst::PadProbeType::EVENT_DOWNSTREAM, move |_, info| {
if let Some(gst::PadProbeData::Event(ev)) = &info.data
&& let gst::EventView::Tag(t) = ev.view()
&& let Some(bytes) = embedded_picture(t.tag())
&& let Some(uri) = decoder.upgrade().and_then(|d| d.property::<Option<String>>("uri"))
{
lock(&pictures).entry(uri).or_insert(bytes);
}
gst::PadProbeReturn::Ok
});
});
});
}
/// The track whose picture is still being looked for.
struct ArtWait {
index: usize,
since: Instant,
/// Already reported as without a picture; one turning up later is still sent.
told_none: bool,
}
fn start(playbin: &gst::Element, url: &str) {
let _ = playbin.set_state(gst::State::Ready);
playbin.set_property("uri", url);
if let Err(e) = playbin.set_state(gst::State::Playing) {
log::warn!("could not start playback: {e}");
}
}
fn start_paused(playbin: &gst::Element, url: &str) {
let _ = playbin.set_state(gst::State::Ready);
playbin.set_property("uri", url);
if let Err(e) = playbin.set_state(gst::State::Paused) {
log::warn!("could not load the track: {e}");
}
}
/// Seeks to `seconds`, kept a second short of the end so a stale position
/// does not land past a track that has since changed.
fn seek_to(playbin: &gst::Element, seconds: f64) {
let mut target = seconds.max(0.0);
if let Some(dur) = playbin.query_duration::<gst::ClockTime>() {
target = target.min((dur.seconds_f64() - 1.0).max(0.0));
}
let flags = gst::SeekFlags::FLUSH | gst::SeekFlags::KEY_UNIT;
if let Err(e) = playbin.seek_simple(flags, gst::ClockTime::from_seconds_f64(target)) {
log::debug!("seek failed: {e}");
}
}
fn watch_bus(
playbin: &gst::Element,
queue: &Mutex<Queue>,
pictures: &Pictures,
stop: &AtomicBool,
emit: &dyn Fn(PlayerEvent),
) {
let Some(bus) = playbin.bus() else { return };
let mut last_tick = Instant::now();
let mut duration = 0.0;
// playbin can post stream-start more than once for one track, so a repeat
// of the last report is dropped. A track that was just started is always
// reported, even at the same index: a new queue, or the same track again.
let mut last_track: Option<Option<usize>> = None;
let mut report_track = |t: Option<usize>, started: bool| {
if started || last_track != Some(t) {
last_track = Some(t);
emit(PlayerEvent::Track(t));
emit(PlayerEvent::Position(0.0));
true
} else {
false
}
};
let mut art_wait: Option<ArtWait> = None;
while !stop.load(Ordering::Relaxed) {
if let Some(msg) = bus.timed_pop(gst::ClockTime::from_mseconds(TICK.as_millis() as u64)) {
use gst::MessageView;
match msg.view() {
MessageView::StreamStart(_) => {
let (current, started) = {
let mut q = lock(queue);
let started = match q.pending.take() {
Some(p) => {
q.current = Some(p);
true
}
None => false,
};
(q.current, started)
};
duration = 0.0;
if report_track(current, started) {
art_wait = current.map(|index| ArtWait {
index,
since: Instant::now(),
told_none: false,
});
// Keep only what this track and the next may still need.
let keep: Vec<String> = {
let q = lock(queue);
current
.into_iter()
.flat_map(|i| q.urls.get(i..=i + 1))
.flatten()
.cloned()
.collect()
};
lock(pictures).retain(|uri, _| keep.contains(uri));
}
}
MessageView::StateChanged(s) if s.src() == Some(playbin.upcast_ref()) => match s.current() {
gst::State::Playing => emit(PlayerEvent::Paused(false)),
gst::State::Paused if s.pending() == gst::State::VoidPending => emit(PlayerEvent::Paused(true)),
_ => {}
},
// Prerolled: the first moment a paused track can seek, and the
// ticks below report no position while paused, so report it here.
MessageView::AsyncDone(_) => {
let target = lock(queue).seek_to.take();
if let Some(s) = target {
seek_to(playbin, s);
} else if let Some(p) = playbin.query_position::<gst::ClockTime>() {
emit(PlayerEvent::Position(p.seconds_f64()));
}
}
MessageView::Eos(_) => {
{
let mut q = lock(queue);
q.current = None;
q.pending = None;
}
let _ = playbin.set_state(gst::State::Ready);
report_track(None, false);
art_wait = None;
}
// A failing source posts several errors. Once the first has moved
// on to the next track, the source is out of playbin and the rest
// would count as the next track failing too.
MessageView::Error(err) if err.src().is_some_and(|s| !s.has_as_ancestor(playbin)) => {
log::debug!("dropping an error from a track already skipped: {}", err.error());
}
MessageView::Error(err) => {
let detail = err.debug().map(|d| d.to_string()).unwrap_or_default();
log::warn!("playback error: {} ({})", err.error(), strip_queries(&detail));
// Skip the track that failed rather than stopping the queue.
let (next, len, hold) = {
let mut q = lock(queue);
let failed = q.pending.take().or(q.current);
q.current = None;
// The position belonged to the track that failed.
q.seek_to = None;
(failed.map_or(usize::MAX, |i| i + 1), q.urls.len(), q.hold)
};
if next < len {
let url = {
let mut q = lock(queue);
q.pending = Some(next);
q.urls[next].clone()
};
if hold {
start_paused(playbin, &url);
} else {
start(playbin, &url);
}
} else {
let _ = playbin.set_state(gst::State::Ready);
report_track(None, false);
art_wait = None;
}
}
_ => {}
}
}
if let Some(w) = &mut art_wait {
let url = lock(queue).urls.get(w.index).cloned();
let picture = url.and_then(|u| lock(pictures).get(&u).cloned());
if picture.is_some() {
emit(PlayerEvent::Art(w.index, picture));
art_wait = None;
} else if !w.told_none && w.since.elapsed() >= ART_WAIT {
w.told_none = true;
emit(PlayerEvent::Art(w.index, None));
}
}
if last_tick.elapsed() >= TICK {
last_tick = Instant::now();
if playbin.current_state() == gst::State::Playing {
if let Some(p) = playbin.query_position::<gst::ClockTime>() {
emit(PlayerEvent::Position(p.seconds_f64()));
}
if let Some(d) = playbin.query_duration::<gst::ClockTime>()
&& (d.seconds_f64() - duration).abs() > 0.5
{
duration = d.seconds_f64();
emit(PlayerEvent::Duration(duration));
}
}
}
}
}
/// The front cover among the pictures in `tags`, else the first picture.
fn embedded_picture(tags: &gst::TagListRef) -> Option<Vec<u8>> {
// GST_TAG_IMAGE_TYPE_FRONT_COVER in gst-tag's GstTagImageType.
const FRONT_COVER: i32 = 1;
let is_front = |s: &gst::Sample| {
s.info()
.and_then(|i| i.value("image-type").ok())
.and_then(|v| gst::glib::EnumValue::from_value(v))
.is_some_and(|(_, v)| v.value() == FRONT_COVER)
};
let pictures: Vec<gst::Sample> = tags.iter_tag::<gst::tags::Image>().map(|v| v.get()).collect();
let sample = pictures.iter().find(|s| is_front(s)).or(pictures.first())?;
let map = sample.buffer()?.map_readable().ok()?;
Some(map.as_slice().to_vec())
}
/// Removes the query string from any URL in `text`. Stream URLs carry the
/// Subsonic auth token, and GStreamer's HTTP errors quote the URL.
fn strip_queries(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut in_query = false;
let mut in_url = false;
for (i, c) in text.char_indices() {
let ends_url = c.is_whitespace()
|| matches!(c, '"' | '\'' | ')' | '>')
// "…?a=b, next" in prose: the comma closes the URL.
|| (c == ',' && text[i + 1..].starts_with(char::is_whitespace));
if ends_url {
in_url = false;
in_query = false;
} else if !in_url && (text[i..].starts_with("http://") || text[i..].starts_with("https://")) {
in_url = true;
} else if in_url && c == '?' {
in_query = true;
out.push_str("?…");
continue;
}
if !in_query {
out.push(c);
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::mpsc;
#[test]
fn queries_are_stripped_from_urls() {
let s = strip_queries("Not Found (404), URL: https://h/rest/stream?u=a&t=secret&s=salt, Redirect to: (NULL)");
assert_eq!(s, "Not Found (404), URL: https://h/rest/stream?…, Redirect to: (NULL)");
assert!(!strip_queries("\"http://x/y?t=secret\" next").contains("secret"));
assert_eq!(strip_queries("what? no url here"), "what? no url here");
let s = strip_queries("URL: https://h/rest/stream?u=a&p=enc%3A736573616d65&v=1 failed");
assert!(!s.contains("736573616d65"), "{s}");
}
/// Writes `secs` of silence as a 16-bit mono WAV and returns its file:// URI.
fn silent_wav(dir: &std::path::Path, name: &str, secs: f32) -> String {
let rate = 8000u32;
let samples = (rate as f32 * secs) as u32;
let data_len = samples * 2;
let mut b = Vec::new();
b.extend_from_slice(b"RIFF");
b.extend_from_slice(&(36 + data_len).to_le_bytes());
b.extend_from_slice(b"WAVEfmt ");
b.extend_from_slice(&16u32.to_le_bytes());
b.extend_from_slice(&1u16.to_le_bytes());
b.extend_from_slice(&1u16.to_le_bytes());
b.extend_from_slice(&rate.to_le_bytes());
b.extend_from_slice(&(rate * 2).to_le_bytes());
b.extend_from_slice(&2u16.to_le_bytes());
b.extend_from_slice(&16u16.to_le_bytes());
b.extend_from_slice(b"data");
b.extend_from_slice(&data_len.to_le_bytes());
b.resize(b.len() + data_len as usize, 0);
let path = dir.join(name);
std::fs::write(&path, b).unwrap();
format!("file://{}", path.display())
}
/// Puts an ID3v2.3 tag holding `picture` as the front cover before the
/// file at `uri`, as a tagged MP3 would carry it.
fn with_id3_picture(uri: &str, picture: &[u8]) {
let path = uri.strip_prefix("file://").unwrap();
let mut body = vec![0u8]; // Latin-1 text
body.extend_from_slice(b"image/png\0");
body.push(3); // front cover
body.push(0); // empty description
body.extend_from_slice(picture);
let mut frame = b"APIC".to_vec();
frame.extend_from_slice(&(body.len() as u32).to_be_bytes());
frame.extend_from_slice(&[0, 0]);
frame.extend_from_slice(&body);
let n = frame.len() as u32;
let syncsafe = [(n >> 21) & 0x7f, (n >> 14) & 0x7f, (n >> 7) & 0x7f, n & 0x7f].map(|b| b as u8);
let mut tagged = b"ID3\x03\x00\x00".to_vec();
tagged.extend_from_slice(&syncsafe);
tagged.extend_from_slice(&frame);
tagged.extend_from_slice(&std::fs::read(path).unwrap());
std::fs::write(path, tagged).unwrap();
}
#[test]
fn reports_the_picture_in_each_file() {
// SAFETY: set before any thread reads the environment in this test.
unsafe { std::env::set_var("REMUS_AUDIO_SINK", "fakesink") };
let dir = std::env::temp_dir().join(format!("remus-player-art-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let urls = vec![silent_wav(&dir, "a.wav", 1.0), silent_wav(&dir, "b.wav", 1.0)];
let mut png = Vec::new();
image::RgbImage::from_pixel(4, 4, image::Rgb([200, 30, 30]))
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
.unwrap();
with_id3_picture(&urls[0], &png);
let (tx, rx) = mpsc::channel();
let player = Player::spawn(move |ev| {
if let PlayerEvent::Art(i, bytes) = ev {
let _ = tx.send((i, bytes));
}
})
.unwrap();
player.play_list(&urls, 0);
let first = rx.recv_timeout(Duration::from_secs(5));
let second = rx.recv_timeout(Duration::from_secs(5));
drop(player);
let _ = std::fs::remove_dir_all(&dir);
assert_eq!(first, Ok((0, Some(png))), "the tagged file's picture");
assert_eq!(second, Ok((1, None)), "the plain file has none");
}
#[test]
fn plays_a_queue_through_to_the_end() {
// SAFETY: set before any thread reads the environment in this test.
unsafe { std::env::set_var("REMUS_AUDIO_SINK", "fakesink") };
let dir = std::env::temp_dir().join(format!("remus-player-test-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let urls = vec![silent_wav(&dir, "a.wav", 0.6), silent_wav(&dir, "b.wav", 0.6)];
let (tx, rx) = mpsc::channel();
let player = Player::spawn(move |ev| {
if let PlayerEvent::Track(t) = ev {
let _ = tx.send(t);
}
})
.unwrap();
player.play_list(&urls, 0);
let mut tracks = Vec::new();
while let Ok(t) = rx.recv_timeout(Duration::from_secs(5)) {
tracks.push(t);
if t.is_none() {
break;
}
}
drop(player);
let _ = std::fs::remove_dir_all(&dir);
assert_eq!(tracks, vec![Some(0), Some(1), None]);
}
#[test]
fn starting_at_the_playing_index_reports_the_track_again() {
// SAFETY: set before any thread reads the environment in this test.
unsafe { std::env::set_var("REMUS_AUDIO_SINK", "fakesink") };
let dir = std::env::temp_dir().join(format!("remus-player-restart-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
// Long enough that nothing finishes while the test runs.
let album_a = vec![silent_wav(&dir, "a1.wav", 10.0), silent_wav(&dir, "a2.wav", 10.0)];
let album_b = vec![silent_wav(&dir, "b1.wav", 10.0), silent_wav(&dir, "b2.wav", 10.0)];
let (tx, rx) = mpsc::channel();
let player = Player::spawn(move |ev| {
if let PlayerEvent::Track(t) = ev {
let _ = tx.send(t);
}
})
.unwrap();
let next = || rx.recv_timeout(Duration::from_secs(3)).ok();
player.play_list(&album_a, 0);
assert_eq!(next(), Some(Some(0)), "first album started");
// Another album, started at the same index as the track playing.
player.play_list(&album_b, 0);
assert_eq!(next(), Some(Some(0)), "second album started at the same index");
// The playing track started again from the queue.
player.play_index(0);
assert_eq!(next(), Some(Some(0)), "the same track restarted");
drop(player);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_paused_load_seeks_and_waits_for_play() {
// SAFETY: set before any thread reads the environment in this test.
unsafe { std::env::set_var("REMUS_AUDIO_SINK", "fakesink") };
let dir = std::env::temp_dir().join(format!("remus-player-paused-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
// The saved track no longer exists, so the next one is readied instead.
let missing = format!("file://{}", dir.join("gone.wav").display());
let urls = vec![
silent_wav(&dir, "a.wav", 10.0),
missing,
silent_wav(&dir, "c.wav", 10.0),
];
let (tx, rx) = mpsc::channel();
let player = Player::spawn(move |ev| {
let _ = tx.send(ev);
})
.unwrap();
player.load_paused(&urls, Some(1), 5.0);
let mut track = None;
let mut started = false;
let deadline = Instant::now() + Duration::from_secs(2);
while let Some(left) = deadline.checked_duration_since(Instant::now()) {
match rx.recv_timeout(left) {
Ok(PlayerEvent::Track(t)) => track = t,
Ok(PlayerEvent::Paused(false)) => started = true,
Ok(PlayerEvent::Position(p)) if p > 0.0 => started = true,
Ok(_) => {}
Err(_) => break,
}
}
assert_eq!(track, Some(2), "skipped the missing track");
assert!(!started, "nothing plays before play is pressed");
player.toggle_pause();
let mut played = false;
while let Ok(ev) = rx.recv_timeout(Duration::from_secs(3)) {
if let PlayerEvent::Position(p) = ev
&& p > 0.0
{
played = true;
// The seek was dropped with the track it belonged to.
assert!(p < 4.0, "position {p}");
break;
}
}
assert!(played, "plays once play is pressed");
player.load_paused(&urls, Some(0), 5.0);
player.toggle_pause();
let mut pos = None;
while let Ok(ev) = rx.recv_timeout(Duration::from_secs(3)) {
if let PlayerEvent::Position(p) = ev
&& p > 0.0
{
pos = Some(p);
break;
}
}
let p = pos.expect("a position once playing");
assert!((4.5..7.0).contains(&p), "resumed at {p}");
drop(player);
let _ = std::fs::remove_dir_all(&dir);
}
}