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.

Stop a refused key from emptying the queue in silence

Lelonio6144d646f857
diff --git a/native/vendor/README.md b/native/vendor/README.md
index 58b4295..b68f726 100644
--- a/native/vendor/README.md
+++ b/native/vendor/README.md
@@ -172,6 +172,25 @@ device that has to be handed the queue again.
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.
+### The patch: a refused key ends the load
+
+Spotify hands out a decryption key per file, and it refuses them when it is
+throttling a session — which says nothing about the track being asked for.
+Upstream then loads the file anyway, without a key, on the grounds that a file
+which turns out to be unencrypted still plays and an encrypted one will simply
+fail to parse.
+
+What that failure looks like from the outside is the whole queue flicking past
+in silence: the decoder dies on the first frame, the track "ends" in half a
+second, the engine moves to the next one, and that one is refused too. Nothing
+in the log says the word key.
+
+A refusal or a timeout now ends the load, which puts it on the retry path with
+the rest, and says so if it still cannot be played. Any other key failure keeps
+upstream's behaviour, since a genuinely unencrypted file has to go on playing.
+See also the retry in `librespot-core`'s `audio_key.rs`, which is what tries a
+refused key again before it gets this far.
+
### Maintenance
The markers are `LOCAL PATCH` in `src/player.rs` and `src/config.rs`.
diff --git a/native/vendor/librespot-playback/src/player.rs b/native/vendor/librespot-playback/src/player.rs
index adf73d4..e2bb350 100644
--- a/native/vendor/librespot-playback/src/player.rs
+++ b/native/vendor/librespot-playback/src/player.rs
@@ -1153,9 +1153,35 @@ impl PlayerTrackLoader {
// Not all audio files are encrypted. If we can't get a key, try loading the track
// without decryption. If the file was encrypted after all, the decoder will fail
// parsing and bail out, so we should be safe from outputting ear-piercing noise.
+ //
+ // LOCAL PATCH: a refused key ends the load instead.
+ //
+ // Spotify refuses keys when it is throttling a session, and the
+ // refusal says nothing about the track. Carrying on without one
+ // means handing the decoder an encrypted file: it dies on the first
+ // frame, the track "ends" in half a second, and the engine moves to
+ // the next one — which is refused too. The listener sees the whole
+ // queue flick past in silence, with nothing in the log but a
+ // decoder complaining about the end of the stream.
+ //
+ // Failing the load here puts it on the retry path instead (see
+ // MAX_LOAD_ATTEMPTS above), which waits and tries again, and says
+ // so plainly if it still cannot. Only a refusal or a timeout is
+ // treated this way; any other key failure keeps upstream's
+ // behaviour, since a file that is genuinely unencrypted has to go
+ // on playing.
let key = match self.session.audio_key().request(track_id, file_id).await {
Ok(key) => Some(key),
Err(e) => {
+ let refused = matches!(
+ e.error.downcast_ref::<librespot_core::audio_key::AudioKeyError>(),
+ Some(librespot_core::audio_key::AudioKeyError::AesKey)
+ | Some(librespot_core::audio_key::AudioKeyError::Timeout)
+ );
+ if refused {
+ warn!("no key for <{track_id:?}>, not playing it undecrypted: {e}");
+ return None;
+ }
warn!("Unable to load key, continuing without decryption: {e}");
None
}