fix: show a new track started at the index that was playing
The player reported a track only when the queue index changed, to drop the repeat stream-start messages playbin posts for one track. Starting another album at the same index (track 1 after track 1) therefore sent no event: the app kept the old track on screen, and pause said nothing was playing, because it had cleared the current track while waiting for that event. A stream-start that begins a track the player was asked to start is now always reported. Only repeats within one track are dropped, and the app treats every report as a new play, so replaying the current track resets its position and scrobbles again.
This commit is contained in:
+49
-11
@@ -182,10 +182,12 @@ fn watch_bus(playbin: &gst::Element, queue: &Mutex<Queue>, stop: &AtomicBool, em
|
||||
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.
|
||||
// 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>| {
|
||||
if last_track != Some(t) {
|
||||
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));
|
||||
@@ -196,15 +198,19 @@ fn watch_bus(playbin: &gst::Element, queue: &Mutex<Queue>, stop: &AtomicBool, em
|
||||
use gst::MessageView;
|
||||
match msg.view() {
|
||||
MessageView::StreamStart(_) => {
|
||||
let current = {
|
||||
let (current, started) = {
|
||||
let mut q = lock(queue);
|
||||
if let Some(p) = q.pending.take() {
|
||||
q.current = Some(p);
|
||||
}
|
||||
q.current
|
||||
let started = match q.pending.take() {
|
||||
Some(p) => {
|
||||
q.current = Some(p);
|
||||
true
|
||||
}
|
||||
None => false,
|
||||
};
|
||||
(q.current, started)
|
||||
};
|
||||
duration = 0.0;
|
||||
report_track(current);
|
||||
report_track(current, started);
|
||||
}
|
||||
MessageView::StateChanged(s) if s.src() == Some(playbin.upcast_ref()) => match s.current() {
|
||||
gst::State::Playing => emit(PlayerEvent::Paused(false)),
|
||||
@@ -218,7 +224,7 @@ fn watch_bus(playbin: &gst::Element, queue: &Mutex<Queue>, stop: &AtomicBool, em
|
||||
q.pending = None;
|
||||
}
|
||||
let _ = playbin.set_state(gst::State::Ready);
|
||||
report_track(None);
|
||||
report_track(None, false);
|
||||
}
|
||||
MessageView::Error(err) => {
|
||||
let detail = err.debug().map(|d| d.to_string()).unwrap_or_default();
|
||||
@@ -239,7 +245,7 @@ fn watch_bus(playbin: &gst::Element, queue: &Mutex<Queue>, stop: &AtomicBool, em
|
||||
start(playbin, &url);
|
||||
} else {
|
||||
let _ = playbin.set_state(gst::State::Ready);
|
||||
report_track(None);
|
||||
report_track(None, false);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
@@ -357,4 +363,36 @@ mod tests {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user