From c9b67977e08e658e7f30c1a341350771238129a4 Mon Sep 17 00:00:00 2001 From: Erica Z Date: Tue, 15 Oct 2024 22:14:37 +0200 Subject: [PATCH] improve async queue logic hopefully --- src/playbin.vala | 42 +++++++++--------------------------------- 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/src/playbin.vala b/src/playbin.vala index 1f648b8..1ace5cf 100644 --- a/src/playbin.vala +++ b/src/playbin.vala @@ -46,7 +46,7 @@ class Playbin : Object { public Playbin (ListModel play_queue) { this.play_queue = play_queue; - this.next_uri = new AsyncQueue (); + this.next_uri = new AsyncQueue (); this.next_uri.push (""); // gstreamer docs: GNOME-based applications, for example, will usually @@ -78,30 +78,21 @@ class Playbin : Object { } else if (this.playing_index+1 == position) { // next track was changed // try to fix up gapless transition - this.next_uri.lock (); - switch (this.next_uri.length_unlocked ()) { - case 1: + string? next_uri = this.next_uri.try_pop (); + if (next_uri != null) { // we're in luck, about-to-finish hasn't been triggered yet // we can get away with replacing it - this.next_uri.try_pop_unlocked (); if (this.playing_index+1 < play_queue.get_n_items ()) { Song song = (Song) play_queue.get_item (this.playing_index+1); - this.next_uri.push_unlocked (this.api.stream_uri (song.id)); + this.next_uri.push (this.api.stream_uri (song.id)); } else { - this.next_uri.push_unlocked (""); + this.next_uri.push (""); } - break; - - case 0: + } else { // about-to-finish already triggered // we'll need to stop the new track when it starts playing assert (false); // TODO - break; - - default: - error ("invalid next_uri queue length %u\n", this.next_uri.length_unlocked ()); } - this.next_uri.unlock (); } }); @@ -193,29 +184,14 @@ class Playbin : Object { this.playbin.set_state (Gst.State.PLAYING); this.next_gapless = false; - this.next_uri.lock (); - switch (this.next_uri.length_unlocked ()) { - case 1: + string? next_uri = this.next_uri.try_pop (); + if (next_uri != null) { // we're in luck, about-to-finish hasn't been triggered yet - this.next_uri.try_pop_unlocked (); - break; - - case 0: + } else { // about-to-finish already triggered // we'll need to stop the new track when it starts playing assert (false); // TODO - break; - - case -1: - // about-to-finish is blocked - // extremely stupid edge case - assert (false); // TODO - break; - - default: - error ("invalid next_uri queue length %u\n", this.next_uri.length_unlocked ()); } - this.next_uri.unlock (); } // ASSUMPTION: about-to-finish will be signalled exactly once per track