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.

Pause the player directly when the Connect device is gone

Lelonio76be1a26c568
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 72ba2db..a38f9d1 100644
--- a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
+++ b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
@@ -129,6 +129,17 @@ object NativeBridge {
val isConnected: Boolean get() = nativeIsConnected()
+ /**
+ * Whether the Connect device is gone and the engine wants rebuilding.
+ *
+ * Distinct from [isConnected], which asks about the session: a session can
+ * be perfectly valid while the Spirc task behind it has died, and that is
+ * the state where transport commands stop landing and the player carries on
+ * making sound. Set the first time a command has to go around Spirc to
+ * reach the player, and cleared by the next [start].
+ */
+ val spircLost: Boolean get() = nativeSpircLost()
+
fun shutdown() = nativeShutdown()
// --- Catalogue, served by the access point rather than api.spotify.com ---
@@ -204,6 +215,7 @@ object NativeBridge {
private external fun nativeSetVolume(volume: Int)
private external fun nativeVolume(): Int
private external fun nativeIsConnected(): Boolean
+ private external fun nativeSpircLost(): Boolean
private external fun nativeShutdown()
private external fun nativeUsername(): String
private external fun nativeCollectionUri(): String
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 6c2dc26..eec6771 100644
--- a/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
+++ b/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
@@ -219,9 +219,21 @@ class LibrespotPlayer(
*
* A failed transport command should cost the command.
*/
+ /**
+ * Sends one command to the engine, and says so loudly when it does not land.
+ *
+ * At error level rather than warning, because there is no such thing as a
+ * harmless transport command that failed: the user pressed something and it
+ * did not happen. This was a warning, which is how "pause does nothing and
+ * the music keeps playing" stayed invisible in the log for so long.
+ *
+ * The engine itself now falls back to the player when the Connect layer
+ * refuses a command, so reaching this line means both paths failed and
+ * playback is genuinely beyond the app's reach.
+ */
private inline fun engine(what: String, command: () -> Unit) {
runCatching(command)
- .onFailure { android.util.Log.w("SquarePlayer", "$what failed: ${it.message}") }
+ .onFailure { android.util.Log.e("SquarePlayer", "$what did not reach the engine", it) }
}
/** Call after any queue mutation, before [invalidateState]. */
diff --git a/native/src/engine.rs b/native/src/engine.rs
index 5832bc3..fc051a2 100644
--- a/native/src/engine.rs
+++ b/native/src/engine.rs
@@ -29,6 +29,14 @@ use tokio::runtime::Runtime;
pub(crate) static JAVA_VM: OnceCell<JavaVM> = OnceCell::new();
static ENGINE: Mutex<Option<Engine>> = Mutex::new(None);
static CONTEXT_INITIALIZED: AtomicBool = AtomicBool::new(false);
+/// Set when a transport command had to go around Spirc; see [`transport`].
+///
+/// The engine is still making sound at that point, but the Connect device is
+/// gone: the account has a stale idea of this phone, and nothing arriving from
+/// another client will be obeyed. Only a rebuild fixes that, and only the Kotlin
+/// side can decide when to do it, so this is a flag it can ask about rather than
+/// something acted on here.
+static SPIRC_LOST: AtomicBool = AtomicBool::new(false);
pub fn store_java_vm(vm: JavaVM) {
let _ = JAVA_VM.set(vm);
@@ -302,7 +310,16 @@ pub fn start(
// dealer, so without it the device appears once and then goes stale.
rt.spawn(async move {
spirc_task.await;
+ // The authoritative moment the Connect device is gone.
+ //
+ // Not a failed command, which is what this used to key off and why the
+ // repair never fired: when the task ends, its channel still accepts
+ // messages and nobody reads them. A load sent afterwards returns Ok,
+ // nothing loads, and the previous track plays on. Watching the task
+ // itself is the only signal that does not depend on being lucky enough
+ // to get an error back.
log::info!("connect device stopped");
+ SPIRC_LOST.store(true, Ordering::SeqCst);
});
spawn_event_pump(
@@ -314,6 +331,10 @@ pub fn start(
cache_dir.to_string(),
);
+ // A fresh engine has a live Connect device again, whatever the last one
+ // ended up as.
+ SPIRC_LOST.store(false, Ordering::SeqCst);
+
*guard = Some(Engine {
rt,
session,
@@ -879,22 +900,73 @@ pub fn load_queue(
e.spirc.activate()?;
e.spirc.load(request)
})?
+ .inspect_err(|_| {
+ // A load has no way around Spirc: choosing what to decode next is the
+ // Connect device's job and nothing else can do it. A refused one is a
+ // second, weaker sign of the same loss the task watcher above catches
+ // properly, kept because it costs nothing.
+ SPIRC_LOST.store(true, Ordering::SeqCst);
+ })
.map_err(|e| format!("load failed: {e}"))
}
+/// Runs a transport command through Spirc, and through the player if Spirc is gone.
+///
+/// Spirc and the player are separate things: the first is the Connect device,
+/// the second is what decodes audio and writes it to the sink. Every transport
+/// command goes through Spirc so the account sees it, but a Spirc command is a
+/// message into a task, and when that task has died the channel is closed and
+/// the command comes back an error while the player carries on making sound.
+///
+/// That is the "pause does nothing and only killing the app stops the music"
+/// failure. It cannot be fixed by reporting the error, because the audio is
+/// still playing either way: something has to reach the thing making it. So the
+/// player is told directly, and the account is left with a stale idea of what
+/// this device is doing until the engine is rebuilt. Silence under the user's
+/// finger is worth more than a tidy Connect state.
+fn transport(
+ what: &str,
+ via_spirc: impl FnOnce(&Engine) -> Result<(), librespot_core::Error>,
+ via_player: impl FnOnce(&Engine),
+) -> EngineResult<()> {
+ // Asked before trying, not after failing.
+ //
+ // A command sent to a Spirc task that has already ended does not come back
+ // an error: the channel still accepts the message and nobody ever reads it.
+ // Waiting for a failure meant the fallback almost never ran, which is why
+ // pause went on doing nothing with the network gone.
+ if spirc_lost() {
+ return with_engine(|engine| {
+ log::warn!("{what}: the connect device is gone, going straight to the player");
+ via_player(engine);
+ });
+ }
+
+ with_engine(|engine| match via_spirc(engine) {
+ Ok(()) => Ok(()),
+ Err(e) => {
+ log::warn!("{what}: spirc refused it ({e}), going straight to the player");
+ SPIRC_LOST.store(true, Ordering::SeqCst);
+ via_player(engine);
+ Ok(())
+ }
+ })?
+}
+
pub fn play() -> EngineResult<()> {
- with_engine(|e| e.spirc.play())?.map_err(|e| format!("play failed: {e}"))
+ transport("play", |e| e.spirc.play(), |e| e.player.play())
}
pub fn pause() -> EngineResult<()> {
- with_engine(|e| e.spirc.pause())?.map_err(|e| format!("pause failed: {e}"))
+ transport("pause", |e| e.spirc.pause(), |e| e.player.pause())
}
pub fn stop() -> EngineResult<()> {
// Disconnecting rather than stopping the player: it tells the account this
// device has given up playback, which is what makes it disappear from the
- // device list instead of lingering as a paused phantom.
- with_engine(|e| e.spirc.disconnect(true))?.map_err(|e| format!("stop failed: {e}"))
+ // device list instead of lingering as a paused phantom. The fallback does
+ // stop the player, because a stop that leaves audio running is not a stop.
+ transport("stop", |e| e.spirc.disconnect(true), |e| e.player.stop())
}
pub fn seek(position_ms: u32) -> EngineResult<()> {
@@ -931,6 +1003,25 @@ pub fn volume() -> EngineResult<u16> {
with_engine(|e| e.mixer.volume())
}
+/// Whether the Connect device is gone and the engine wants rebuilding.
+///
+/// Two signals, because one of them is late. The flag is authoritative but only
+/// rises when the Spirc task has actually finished, and that took ten seconds
+/// after a dropped network in the case this was written for: for those ten
+/// seconds the device is already unreachable, every command is accepted and
+/// discarded, and the listener is pressing skip at a player that will not move.
+///
+/// An invalid session is the earlier sign of the same thing. It is checked at
+/// the moment a command is issued rather than watched, so a connection that
+/// drops and comes back on its own between two commands costs nothing: the
+/// question is only ever asked when somebody is waiting for an answer.
+pub fn spirc_lost() -> bool {
+ if SPIRC_LOST.load(Ordering::SeqCst) {
+ return true;
+ }
+ with_engine(|e| e.session.is_invalid()).unwrap_or(false)
+}
+
pub fn is_connected() -> bool {
with_engine(|e| !e.session.is_invalid()).unwrap_or(false)
}
diff --git a/native/src/ffi.rs b/native/src/ffi.rs
index 9d39e34..e82917e 100644
--- a/native/src/ffi.rs
+++ b/native/src/ffi.rs
@@ -415,6 +415,14 @@ pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeTra
}
+#[no_mangle]
+pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeSpircLost(
+ _env: JNIEnv,
+ _class: JClass,
+) -> jboolean {
+ engine::spirc_lost() as jboolean
+}
+
#[no_mangle]
pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeShutdown(
_env: JNIEnv,