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.

Resuming no longer waits behind Connect

Lelonio1c2688b36bb8
diff --git a/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt b/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt
index 1c055f0..3187743 100644
--- a/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt
+++ b/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt
@@ -333,17 +333,25 @@ class AudioOutput {
* makes. This is the pause the *player* knows about.
*/
fun setPlaybackActive(active: Boolean) {
- synchronized(this) {
+ val output = synchronized(this) {
+ if (active) applyReverb() else suspendReverb()
+ track?.takeIf { it.state == AudioTrack.STATE_INITIALIZED }
+ } ?: return
+
+ // Outside the lock. Both are calls into the audio server, and the
+ // writer takes this lock for every packet: held across a slow one, it
+ // held up the very audio that was being resumed.
+ //
+ // The pause is what makes the button feel instant, since the engine's
+ // own stop fades for a fifth of a second first. The play is for a
+ // pause the engine never turned into a stop, a track still loading
+ // when it was pressed, say: the sink is then never started again, and
+ // a track left paused here would keep the writer waiting for good.
+ output.runCatching {
if (active) {
- applyReverb()
- track?.takeIf { it.state == AudioTrack.STATE_INITIALIZED }?.runCatching {
- if (playState != AudioTrack.PLAYSTATE_PLAYING) play()
- }
+ if (playState != AudioTrack.PLAYSTATE_PLAYING) play()
} else {
- suspendReverb()
- track?.takeIf { it.state == AudioTrack.STATE_INITIALIZED }?.runCatching {
- if (playState == AudioTrack.PLAYSTATE_PLAYING) pause()
- }
+ if (playState == AudioTrack.PLAYSTATE_PLAYING) pause()
}
}
}
diff --git a/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt b/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
index 2aaeadc..8a252dc 100644
--- a/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
+++ b/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
@@ -752,7 +752,6 @@ class LibrespotPlayer(
invalidateState()
return Futures.immediateVoidFuture()
}
- onPlaybackActive(true)
// Play is where a reconnection is worth waiting for. The engine has
// no queue of its own, so a rebuilt device knows nothing until the
// queue is handed over again, and resuming is exactly the moment
@@ -762,6 +761,11 @@ class LibrespotPlayer(
} else {
engine("play") { NativeBridge.play() }
}
+ // After the engine, not before: waking the output is a call into
+ // the audio server that took most of a tenth of a second here, and
+ // it stood between the button and the command that brings the
+ // music back.
+ onPlaybackActive(true)
} else {
wantPlay = false
engine("pause") { NativeBridge.pause() }
diff --git a/native/src/engine.rs b/native/src/engine.rs
index e1d0aaf..10e24d8 100644
--- a/native/src/engine.rs
+++ b/native/src/engine.rs
@@ -1877,6 +1877,19 @@ pub fn play() -> EngineResult<()> {
log::info!("play: another device has playback, ignoring");
return Ok(());
}
+
+ // The music starts here, for the same reason it stops here in [`pause`].
+ //
+ // After a pause the Connect task is still telling Spotify about it, and
+ // while that request is out it does not read its channel: a play pressed a
+ // second after a pause waited behind it, about a third of a second on a
+ // good line and ten on one that was answering 429. Pause already skipped
+ // that queue, so the button stopped the music at once and then took its
+ // time to bring it back. The account hears about it whenever it can, and
+ // the player answers that late play with where the track really is; see
+ // `handle_play` in player.rs.
+ let _ = with_bundle(|engine| engine.player.play());
+
transport("play", |e| e.spirc()?.play(), |e| e.player.play())
}
diff --git a/native/vendor/librespot-playback/src/player.rs b/native/vendor/librespot-playback/src/player.rs
index c9333f8..032a08f 100644
--- a/native/vendor/librespot-playback/src/player.rs
+++ b/native/vendor/librespot-playback/src/player.rs
@@ -2426,6 +2426,25 @@ impl PlayerInternal {
});
self.ensure_sink_running();
}
+ // LOCAL PATCH: already playing, because the engine told this player
+ // directly before it told the Connect task; see `play` in the
+ // engine. The Connect task's own play comes later, sometimes
+ // seconds later, and restarts its clock from the position it
+ // paused at. Saying where the track really is lets it correct that
+ // rather than report a position that lags by the whole delay.
+ PlayerState::Playing {
+ ref track_id,
+ play_request_id,
+ stream_position_ms,
+ ..
+ } => {
+ let track_id = track_id.clone();
+ self.send_event(PlayerEvent::PositionCorrection {
+ play_request_id,
+ track_id,
+ position_ms: stream_position_ms,
+ });
+ }
PlayerState::Loading {
ref mut start_playback,
..