//! 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::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, Copy)] pub enum PlayerEvent { Position(f64), Duration(f64), Paused(bool), /// Index into the queue, None when playback has stopped. Track(Option), } #[derive(Default)] struct Queue { urls: Vec, current: Option, /// Handed to playbin, becomes `current` at its stream-start. pending: Option, /// 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, } pub struct Player { playbin: gst::Element, queue: Arc>, stop: Arc, bus_thread: Option>, } const TICK: Duration = Duration::from_millis(250); impl Player { pub fn spawn(on_event: impl Fn(PlayerEvent) + Send + 'static) -> Result { 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::().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 stop = Arc::new(AtomicBool::new(false)); let bus_thread = std::thread::Builder::new().name("gst-bus".into()).spawn({ let (playbin, queue, stop) = (playbin.clone(), queue.clone(), stop.clone()); move || watch_bus(&playbin, &queue, &stop, &on_event) })?; Ok(Player { playbin, queue, 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, 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); } 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); } } pub fn seek(&self, seconds: f64) { let Some(pos) = self.playbin.query_position::() 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(q: &Mutex) -> MutexGuard<'_, Queue> { q.lock().unwrap_or_else(|e| e.into_inner()) } 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::() { 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, 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> = None; let mut report_track = |t: Option, started: bool| { if started || last_track != Some(t) { last_track = Some(t); emit(PlayerEvent::Track(t)); emit(PlayerEvent::Position(0.0)); } }; 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; report_track(current, started); } 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::() { 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); } // 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); } } _ => {} } } if last_tick.elapsed() >= TICK { last_tick = Instant::now(); if playbin.current_state() == gst::State::Playing { if let Some(p) = playbin.query_position::() { emit(PlayerEvent::Position(p.seconds_f64())); } if let Some(d) = playbin.query_duration::() && (d.seconds_f64() - duration).abs() > 0.5 { duration = d.seconds_f64(); emit(PlayerEvent::Duration(duration)); } } } } } /// 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()) } #[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); } }