Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

Let a bad connection be slow rather than empty

Lelonio35d5248d3d7e
diff --git a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
index 9163df3..4a6f36c 100644
--- a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
+++ b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
@@ -179,6 +179,16 @@ object NativeBridge {
*/
fun setQuality(bitrateKbps: Int, crossfadeMs: Int) = nativeSetQuality(bitrateKbps, crossfadeMs)
+ /**
+ * Changes the quality asked for from the next track on.
+ *
+ * Unlike [setQuality] this rebuilds nothing: the bitrate is only read while
+ * a track loads, so a message is enough. What is playing keeps the file it
+ * started with, which is also why there is nothing to hear when this is
+ * called — no gap, no restart.
+ */
+ fun setBitrate(bitrateKbps: Int) = nativeSetBitrate(bitrateKbps)
+
/**
* Builds a new session, player and Connect device, keeping everything else.
*
@@ -384,6 +394,8 @@ object NativeBridge {
private external fun nativeIsConnected(): Boolean
private external fun nativeSpircLost(): Boolean
private external fun nativeSetQuality(bitrateKbps: Int, crossfadeMs: Int)
+
+ private external fun nativeSetBitrate(bitrateKbps: Int)
private external fun nativeReconnect()
private external fun nativePlaybackElsewhere(): Boolean
private external fun nativePublishContext(positionMs: Int): Boolean
diff --git a/native/src/engine.rs b/native/src/engine.rs
index 4db6230..0c4a3d1 100644
--- a/native/src/engine.rs
+++ b/native/src/engine.rs
@@ -757,6 +757,33 @@ pub fn set_quality(bitrate_kbps: i32, crossfade_ms: i32) -> EngineResult<()> {
reconnect()
}
+/// Changes the quality of what is asked for next, without rebuilding anything.
+///
+/// The bitrate is read while a track is being loaded, to put the formats it
+/// exists in into order of preference, and nowhere else. Rebuilding the session
+/// for it — which is what [`set_quality`] does, because the crossfade beside it
+/// really is fixed for the life of a player — costs a second of silence, so
+/// following a connection as it changes was never worth doing. This is the same
+/// change at the cost of a message.
+///
+/// What is playing keeps the file it started with. The next track gets this.
+pub fn set_bitrate(bitrate_kbps: i32) -> EngineResult<()> {
+ let bitrate = match bitrate_kbps {
+ 96 => Bitrate::Bitrate96,
+ 160 => Bitrate::Bitrate160,
+ _ => Bitrate::Bitrate320,
+ };
+ let mut guard = ENGINE.lock().map_err(|_| "engine mutex poisoned")?;
+ let engine = guard.as_mut().ok_or("engine not started")?;
+ // Kept on the recipe as well, so a session rebuilt for any other reason
+ // starts where the listening left it rather than back at the setting.
+ engine.recipe.player_config.bitrate = bitrate;
+ if let Some(bundle) = engine.bundle.as_ref() {
+ bundle.player.set_bitrate(bitrate);
+ }
+ Ok(())
+}
+
/// Throws away a bundle and builds another one, leaving the runtime and the
/// audio output alone.
///
@@ -1581,6 +1608,17 @@ pub fn play() -> EngineResult<()> {
}
pub fn pause() -> EngineResult<()> {
+ // The sound stops here, before anything is asked of the network.
+ //
+ // A pause is a message to the Connect task, and that task also talks to
+ // Spotify: while it is waiting on a request it does not read its own
+ // channel, so on a weak signal the music went on playing for seconds after
+ // the button. The player is the thing making the noise and it is right
+ // here, so it is told first and the account is told whenever it can be.
+ // Pausing a player that is already paused, or one that was never playing
+ // because the music is on another device, costs nothing.
+ let _ = with_bundle(|engine| engine.player.pause());
+
transport("pause", |e| e.spirc.pause(), |e| e.player.pause())
}
diff --git a/native/src/ffi.rs b/native/src/ffi.rs
index 5b52f29..9faf465 100644
--- a/native/src/ffi.rs
+++ b/native/src/ffi.rs
@@ -474,6 +474,16 @@ pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeSet
});
}
+/// Changes the quality asked for from the next track on, with no rebuild.
+#[no_mangle]
+pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeSetBitrate(
+ mut env: JNIEnv,
+ _class: JClass,
+ bitrate_kbps: jint,
+) {
+ guard(&mut env, "SetBitrate", || engine::set_bitrate(bitrate_kbps));
+}
+
#[no_mangle]
pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeReconnect(
mut env: JNIEnv,
diff --git a/native/vendor/README.md b/native/vendor/README.md
index be2fc94..58b4295 100644
--- a/native/vendor/README.md
+++ b/native/vendor/README.md
@@ -131,6 +131,47 @@ wraps to the bottom and is heard as a click.
A track shorter than the fade simply drains early: the incoming track goes on
rising, over silence rather than over music.
+### The patch: a slow connection is not a missing track
+
+Upstream gives a load one attempt. If it fails, the player sends `Unavailable`,
+and Connect answers that by skipping to the next track and dropping the failed
+one out of the queue. That is right for a track the account cannot play, and
+wrong for every other reason a load fails: on a weak signal each track failed in
+turn, so the queue ran itself out in seconds without a note being heard, and the
+songs it gave up on were gone from the queue for the rest of the session.
+
+The loading state now retries the same track, three times, waiting 0.9s, 1.8s
+and 3.6s, and sends a fresh `Loading` each time so the app can go on saying it
+is working. `Unavailable` is only sent once all of them have failed, which
+leaves its meaning intact for the case it was written for.
+
+The counter and the position it retries from live on `PlayerInternal` and are
+reset by `handle_command_load`, so every new load starts with a full set of
+attempts.
+
+### The patch: a failed head start is not a missing track
+
+The player fetches the next track while the current one plays, and upstream
+answers a failed fetch by telling Spirc the track is unavailable, which takes it
+out of the queue. On a weak link that head start is the first thing to fail, and
+it fails while the connection is busy carrying the song being listened to: songs
+were vanishing from the queue before anything had tried to play them.
+
+A failed preload now just forgets the head start. The track is loaded normally
+when its turn comes, with the retries that path has.
+
+### The patch: the quality can change without a new player
+
+The bitrate is read in one place, while a track is being loaded, to put the
+formats it exists in into order of preference. Upstream keeps it in the
+player's immutable config all the same, so following a connection as it changes
+meant building a new session, which costs a second of silence and a Connect
+device that has to be handed the queue again.
+
+`PlayerCommand::SetBitrate` and `Player::set_bitrate` change it in place, and
+drop whatever was preloaded, since that was fetched at the old quality. What is
+playing keeps the file it started with; the next track gets the new one.
+
### Maintenance
The markers are `LOCAL PATCH` in `src/player.rs` and `src/config.rs`.
@@ -174,6 +215,19 @@ daily mix, a radio, "Pop Mix" resolve to nothing for a client that is not
Spotify's own, and the only copy of that queue in existence here is the one the
account handed the device to play.
+### The patch: a weak signal must not hold the buttons
+
+The task's `select!` is the only thing reading the command channel, and one of
+its branches tells the account what this device is doing, which is an HTTP
+request. Awaiting it inline suspends the whole loop, so a pause sent from the
+app sat unread until the request came back: on a weak signal, seconds of music
+under a finger that had already asked for silence.
+
+The state update is now bounded by `STATE_UPDATE_TIMEOUT`, one and a half
+seconds, after which the loop goes back to reading commands and leaves the
+update flagged so the next turn tries again. The account can be told late. The
+listener cannot.
+
### The patch: a new running order, without a reload
`Spirc::set_queue_tracks(prev, next)` replaces what comes before and after the
diff --git a/native/vendor/librespot-connect/src/spirc.rs b/native/vendor/librespot-connect/src/spirc.rs
index 783baf3..f124445 100644
--- a/native/vendor/librespot-connect/src/spirc.rs
+++ b/native/vendor/librespot-connect/src/spirc.rs
@@ -153,6 +153,10 @@ const VOLUME_UPDATE_DELAY: Duration = Duration::from_millis(500);
// to reduce updates to remote, we group some request by waiting for a set amount of time
const UPDATE_STATE_DELAY: Duration = Duration::from_millis(200);
+/// LOCAL PATCH: how long the loop will wait for the account to acknowledge a
+/// state update before going back to reading commands. See the use site.
+const STATE_UPDATE_TIMEOUT: Duration = Duration::from_millis(1500);
+
/// The spotify connect handle
pub struct Spirc {
commands: mpsc::UnboundedSender<SpircCommand>,
@@ -649,8 +653,26 @@ impl SpircTask {
_ = async { sleep(UPDATE_STATE_DELAY).await }, if self.update_state => {
self.update_state = false;
- if let Err(why) = self.notify().await {
- error!("state update: {why}")
+ // LOCAL PATCH: bounded, because this loop is the only thing
+ // reading the command channel.
+ //
+ // Telling the account what this device is doing is an HTTP
+ // request, and awaiting it here suspends the whole select:
+ // the pause the listener just pressed sits unread in its
+ // channel until the request comes back. On a weak signal
+ // that is many seconds of music playing under a finger that
+ // has already asked for silence. The account can be told
+ // late; the listener cannot be answered late.
+ match tokio::time::timeout(STATE_UPDATE_TIMEOUT, self.notify()).await {
+ Ok(Err(why)) => error!("state update: {why}"),
+ Err(_) => {
+ warn!("state update timed out, telling the account later");
+ // Left set, so the next turn of the loop tries
+ // again rather than leaving the account with a
+ // state this device has moved on from.
+ self.update_state = true;
+ }
+ Ok(Ok(())) => (),
}
},
_ = async { sleep(VOLUME_UPDATE_DELAY).await }, if self.update_volume => {
diff --git a/native/vendor/librespot-playback/src/player.rs b/native/vendor/librespot-playback/src/player.rs
index 616c655..adf73d4 100644
--- a/native/vendor/librespot-playback/src/player.rs
+++ b/native/vendor/librespot-playback/src/player.rs
@@ -31,7 +31,7 @@ use crate::{
mixer::VolumeGetter,
};
use futures_util::{
- StreamExt, TryFutureExt, future, future::FusedFuture,
+ FutureExt, StreamExt, TryFutureExt, future, future::FusedFuture,
stream::futures_unordered::FuturesUnordered,
};
use librespot_metadata::{audio::UniqueFields, track::Tracks};
@@ -42,6 +42,15 @@ use tokio::sync::{mpsc, oneshot};
use crate::{NUM_CHANNELS, SAMPLE_RATE, SAMPLES_PER_SECOND};
+/// LOCAL PATCH: how many times a failed load is tried again before the track is
+/// called unavailable, and how long the first wait is (it doubles each time).
+///
+/// Four attempts over about seven seconds. Long enough to ride out a lift, a
+/// tunnel or a handover between cells, short enough that a track which really
+/// is gone does not hold the queue for a minute.
+const MAX_LOAD_ATTEMPTS: u8 = 3;
+const LOAD_RETRY_BACKOFF: Duration = Duration::from_millis(900);
+
const PRELOAD_NEXT_TRACK_BEFORE_END_DURATION_MS: u32 = 30000;
/// LOCAL PATCH: the largest value that survives conversion to signed 16 bit.
@@ -106,6 +115,11 @@ struct PlayerInternal {
fade_out: Option<FadeOut>,
/// LOCAL PATCH: the play request whose end has already been announced early.
early_end: Option<u64>,
+ /// LOCAL PATCH: how many times the track being loaded has been retried, and
+ /// from what position, so a retry resumes the same load. See the failure
+ /// branch of the loading state in `poll`.
+ load_attempt: u8,
+ load_position_ms: u32,
}
/// LOCAL PATCH: the outgoing half of a crossfade.
@@ -152,6 +166,8 @@ enum PlayerCommand {
SetSinkEventCallback(Option<SinkEventCallback>),
EmitVolumeChangedEvent(u16),
SetAutoNormaliseAsAlbum(bool),
+ /// LOCAL PATCH: the quality to ask for from the next load onwards.
+ SetBitrate(Bitrate),
EmitSessionDisconnectedEvent {
connection_id: String,
user_name: String,
@@ -535,6 +551,8 @@ impl Player {
fade_out: None,
early_end: None,
+ load_attempt: 0,
+ load_position_ms: 0,
normalisation_peaks: [0.0; 2],
normalisation_integrators: [0.0; 2],
normalisation_channel: 0,
@@ -640,6 +658,18 @@ impl Player {
self.command(PlayerCommand::SetAutoNormaliseAsAlbum(setting));
}
+ /// LOCAL PATCH: change the quality without building another player.
+ ///
+ /// The bitrate is only ever read while a track is being loaded, to put the
+ /// available formats in order of preference, so there is nothing about it
+ /// that has to be fixed for the life of the player. Upstream keeps it in
+ /// the immutable config anyway, which meant that following a connection as
+ /// it changed cost a whole new session and a second of silence. What is
+ /// playing keeps the file it started with; the next track gets this.
+ pub fn set_bitrate(&self, bitrate: Bitrate) {
+ self.command(PlayerCommand::SetBitrate(bitrate));
+ }
+
pub fn emit_filter_explicit_content_changed_event(&self, filter: bool) {
self.command(PlayerCommand::EmitFilterExplicitContentChangedEvent(filter));
}
@@ -1091,6 +1121,16 @@ impl PlayerTrackLoader {
}
};
+ // LOCAL PATCH: says which file is actually being streamed.
+ //
+ // The bitrate setting picks an order of preference, not a file: what
+ // arrives depends on what the track is offered in, and a track already
+ // in the cache is played from disk whatever the setting now says. With
+ // nothing in the log naming the format, "the quality setting does
+ // nothing" could not be told apart from "this track only exists at
+ // that quality" or "you have heard this one before".
+ info!("<{}> streaming as {:?}", audio_item.name, format);
+
let bytes_per_second = self.stream_data_rate(format)?;
// This is only a loop to be able to reload the file if an error occurred
@@ -1407,13 +1447,57 @@ impl Future for PlayerInternal {
}
}
Poll::Ready(Err(e)) => {
- error!(
- "Skipping to next track, unable to load track <{track_id:?}>: {e:?}"
- );
- self.send_event(PlayerEvent::Unavailable {
- track_id,
- play_request_id,
- })
+ // LOCAL PATCH: try again before declaring the track
+ // gone.
+ //
+ // Upstream treats one failed load as "this track
+ // cannot be played" and Spirc answers that by
+ // skipping to the next one. On a bad connection
+ // every track fails, so the queue ran itself out in
+ // a few seconds, one skip per song, and nothing
+ // played at all — and each failure also marks the
+ // track unavailable for the rest of the session, so
+ // songs kept being skipped long after the signal
+ // came back. A slow network should mean the music
+ // takes longer to start, not that the queue empties.
+ if self.load_attempt < MAX_LOAD_ATTEMPTS {
+ let backoff = LOAD_RETRY_BACKOFF * (1 << self.load_attempt);
+ self.load_attempt += 1;
+ warn!(
+ "retrying load of <{track_id:?}> in {backoff:?}, attempt {} of {MAX_LOAD_ATTEMPTS}: {e:?}",
+ self.load_attempt + 1,
+ );
+ // Says it is still working on this track rather
+ // than leaving the listener at a dead screen.
+ let position_ms = self.load_position_ms;
+ self.send_event(PlayerEvent::Loading {
+ track_id: track_id.clone(),
+ play_request_id,
+ position_ms,
+ });
+ let again = self.load_track(track_id.clone(), position_ms);
+ let loader = Box::pin(
+ async move {
+ tokio::time::sleep(backoff).await;
+ again.await
+ }
+ .fuse(),
+ );
+ self.state = PlayerState::Loading {
+ track_id,
+ play_request_id,
+ start_playback,
+ loader,
+ };
+ } else {
+ error!(
+ "Skipping to next track, unable to load track <{track_id:?}>: {e:?}"
+ );
+ self.send_event(PlayerEvent::Unavailable {
+ track_id,
+ play_request_id,
+ })
+ }
}
Poll::Pending => (),
}
@@ -1438,21 +1522,21 @@ impl Future for PlayerInternal {
};
}
Poll::Ready(Err(_)) => {
- debug!("Unable to preload {track_id:?}");
+ // LOCAL PATCH: a preload that failed is not a track
+ // that cannot be played.
+ //
+ // Upstream tells Spirc the track is unavailable, and
+ // Spirc answers by taking it out of the queue. But this
+ // is the *next* track being fetched early, while the
+ // current one is still playing and the connection is
+ // busy carrying it: on a weak link the head start is
+ // the first thing to fail, and songs were disappearing
+ // from the queue before anyone had tried to play them.
+ // Forgetting the head start is enough. The track is
+ // loaded normally when it is reached, with the retries
+ // that path has.
+ warn!("could not preload {track_id:?}, leaving it for its turn");
self.preload = PlayerPreload::None;
- // Let Spirc know that the track was unavailable.
- if let PlayerState::Playing {
- play_request_id, ..
- }
- | PlayerState::Paused {
- play_request_id, ..
- } = self.state
- {
- self.send_event(PlayerEvent::Unavailable {
- track_id,
- play_request_id,
- });
- }
}
Poll::Pending => (),
}
@@ -2328,6 +2412,11 @@ impl PlayerInternal {
self.preload = PlayerPreload::None;
+ // LOCAL PATCH: a fresh load is a fresh set of attempts, and a retry has
+ // to know where this one was starting from.
+ self.load_attempt = 0;
+ self.load_position_ms = position_ms;
+
// If we don't have a loader yet, create one from scratch.
let loader =
loader.unwrap_or_else(|| Box::pin(self.load_track(track_id.clone(), position_ms)));
@@ -2533,6 +2622,18 @@ impl PlayerInternal {
self.auto_normalise_as_album = setting
}
+ // LOCAL PATCH: see Player::set_bitrate.
+ PlayerCommand::SetBitrate(bitrate) => {
+ if self.config.bitrate != bitrate {
+ info!("bitrate is now {bitrate:?}, from the next track");
+ self.config.bitrate = bitrate;
+ // What was preloaded was fetched at the old quality, and
+ // keeping it would silently undo the change for exactly one
+ // track — the next one, which is the one this was asked for.
+ self.preload = PlayerPreload::None;
+ }
+ }
+
PlayerCommand::EmitFilterExplicitContentChangedEvent(filter) => {
self.send_event(PlayerEvent::FilterExplicitContentChanged { filter });
@@ -2685,6 +2786,9 @@ impl fmt::Debug for PlayerCommand {
.debug_tuple("EmitVolumeChangedEvent")
.field(&volume)
.finish(),
+ PlayerCommand::SetBitrate(bitrate) => {
+ f.debug_tuple("SetBitrate").field(bitrate).finish()
+ }
PlayerCommand::SetAutoNormaliseAsAlbum(setting) => f
.debug_tuple("SetAutoNormaliseAsAlbum")
.field(&setting)