Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.
Kotlin alternativeandroidandroid-applicationclientglassmorphisminnertubelibrespotliquid-glassspotifyspotify-connectspotifyapiyoutube-musicyoutube-playerytdlnis
Follow the engine when another client sends it elsewhere
Lelonio10f58a612074
diff --git a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.ktindex eb04cec..401b814 100644--- a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt+++ b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt@@ -257,6 +257,15 @@ object NativeBridge {/** What the account is playing and where, as JSON; `{}` when nothing is. */fun remoteState(): String = nativeRemoteState()+ /**+ * What this device is playing, as `{"contextUri": …, "trackUri": …}`.+ *+ * Not the same question as [remoteState], which reads the account's own+ * picture: that picture is not sent to the device it describes, so while+ * another client drives this one this is the only current answer.+ */+ fun playingHere(): String = runCatching { nativePlayingHere() }.getOrDefault("{}")+/** Every device the account can see, this one included, as a JSON array. */fun remoteDevices(): String = nativeRemoteDevices()@@ -365,6 +374,7 @@ object NativeBridge {appVersion: String,): Stringprivate external fun nativeDeviceId(): String+ private external fun nativePlayingHere(): Stringprivate external fun nativeRemoteState(): Stringprivate external fun nativeRemoteDevices(): Stringprivate external fun nativeRemoteCommand(deviceId: String, body: String)diff --git a/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt b/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.ktindex c5a567c..85b94b1 100644--- a/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt+++ b/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt@@ -159,6 +159,21 @@ class LibrespotPlayer(*/private var remote: dev.lelonio.square.data.RemotePlayback? = null+ /**+ * Called with a track the engine is playing that this queue does not hold.+ *+ * Another client can point this device at anything: a track from a playlist+ * that was never opened here, with no transfer and no cluster update to+ * announce it, since the account does not describe a device to itself. The+ * engine simply starts playing something else, and this side went on+ * showing the track it thought was current. Set by the service, which is+ * the half that can resolve a context.+ */+ var onUnknownTrack: ((String) -> Unit)? = null++ /** The last one handed over, so a run of events asks for it once. */+ private var unknownAsked: String? = null+fun showRemote(playback: dev.lelonio.square.data.RemotePlayback?) {if (released) returnval was = remote@@ -928,7 +943,15 @@ class LibrespotPlayer(// device and a local one all arrive here the same way.if (uri.isNotEmpty()) {val index = queue.items.indexOfFirst { it.uri == uri }+ if (index < 0 && (type == "playing" || type == "loading") && unknownAsked != uri) {+ // Not ours: somebody else chose it. Asked for once per track,+ // and only on a track that is actually starting, so a position+ // report on a queue mid-rebuild does not set this going.+ unknownAsked = uri+ onUnknownTrack?.invoke(uri)+ }if (index >= 0) {+ unknownAsked = nullengineIndex = indexif (index == queue.currentIndex) {skipInFlight = falsediff --git a/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt b/app/src/main/java/dev/lelonio/square/playback/PlaybackService.ktindex 1829e89..e9d5905 100644--- a/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt+++ b/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt@@ -188,6 +188,10 @@ class PlaybackService : MediaLibraryService() {val built = container.spotifyBackend.createPlayer(playbackHost) as LibrespotPlayerlibrespot = built+ // Another client pointing this device at a track of its own; see+ // LibrespotPlayer.onUnknownTrack.+ built.onUnknownTrack = { uri -> scope.launch { adoptPlayingTrack(uri) } }+// Applied straight to the output rather than waiting for the UI: the// service can be running with no activity attached at all.audioOutput.setSpeedAndPitch(AudioEffects.speed.value, AudioEffects.pitch.value)@@ -327,6 +331,14 @@ class PlaybackService : MediaLibraryService() {val index = tracks.indexOfFirst { it.uri == here.uri }if (tracks.isEmpty()) return+ // A long context takes seconds to read, and an answer about a track the+ // engine has already left behind would drag the screen back to it; see+ // adoptPlayingTrack.+ val playing = engineUri()+ if (playing.isNotEmpty() && playing != here.uri) {+ android.util.Log.i(TAG, "${here.uri} is over by now, leaving the queue alone")+ return+ }engine.adopt(tracks = tracks.map(::toQueueTrack),@@ -338,6 +350,92 @@ class PlaybackService : MediaLibraryService() {)}+ /**+ * Puts the queue behind a track another client started here.+ *+ * A pause on this phone and a tap in the web player on some playlist track:+ * the engine obeys and plays it, and this side had no idea. Not a transfer,+ * so nothing arrives that way, and not a cluster update either, since the+ * account does not describe a device to that device. The Connect state the+ * engine publishes is the one place the answer exists, so it is asked+ * directly, and what comes back is resolved the same way a handover is.+ */+ private suspend fun adoptPlayingTrack(uri: String) {+ val engine = librespot ?: return++ val here = withContext(Dispatchers.IO) {+ runCatching { org.json.JSONObject(NativeBridge.playingHere()) }+ .onFailure { android.util.Log.w(TAG, "cannot read what we play: $it") }+ .getOrNull()+ }+ val contextUri = here?.optString("contextUri").orEmpty()+ val source = contextUri.takeIf {+ it.isNotEmpty() && it.startsWith("spotify:") && !it.startsWith("spotify:web-api")+ }++ val key = "$contextUri|$uri"+ if (adoptedContext == key) return+ adoptedContext = key++ // The context first, because it is the whole list in its own order: a+ // playlist read this way comes back complete, while what the account+ // hands a device is a window of the next few dozen tracks.+ var tracks = if (source == null) {+ emptyList()+ } else {+ withContext(Dispatchers.IO) {+ runCatching { container.activeBackend.tracksOf(source) }+ .onFailure { android.util.Log.w(TAG, "cannot read $source: $it") }+ .getOrDefault(emptyList())+ }+ }++ // Nothing came back, which is the normal answer for the playlists+ // Spotify makes rather than stores: a daily mix, a radio, "Pop Mix"+ // exist for the account and resolve to nothing for anyone else. The+ // account sent the list along with the order to play it, so that is+ // what is used.+ if (tracks.none { it.uri == uri }) {+ val queued = buildList {+ val array = here?.optJSONArray("tracks")+ for (i in 0 until (array?.length() ?: 0)) add(array!!.getString(i))+ }+ tracks = withContext(Dispatchers.IO) {+ runCatching { dev.lelonio.square.data.Catalog.tracks(queued) }+ .onFailure { android.util.Log.w(TAG, "cannot read the engine's queue: $it") }+ .getOrDefault(emptyList())+ }+ android.util.Log.i(TAG, "adopting ${tracks.size} tracks from the engine itself")+ } else {+ android.util.Log.i(TAG, "adopting ${tracks.size} tracks from $source")+ }++ val index = tracks.indexOfFirst { it.uri == uri }+ if (index < 0) return+ // Resolving a long context takes seconds, and in those seconds the+ // listener can have moved on: an answer about a track that is no longer+ // playing would put the screen somewhere the engine has already left.+ if (engineUri() != uri) {+ android.util.Log.i(TAG, "$uri is over by now, leaving the queue alone")+ return+ }++ engine.adopt(+ tracks = tracks.map(::toQueueTrack),+ index = index,+ positionMs = 0L,+ contextUri = source,+ contextLabel = "",+ playing = true,+ )+ }++ /** What the engine says it is playing this instant. */+ private suspend fun engineUri(): String = withContext(Dispatchers.IO) {+ runCatching { org.json.JSONObject(NativeBridge.playingHere()).optString("trackUri") }+ .getOrDefault("")+ }+/*** Rebuilds the engine so a new bitrate takes effect.*diff --git a/native/src/engine.rs b/native/src/engine.rsindex e39a49f..97e49d8 100644--- a/native/src/engine.rs+++ b/native/src/engine.rs@@ -1457,6 +1457,31 @@ pub fn elsewhere_active() -> bool {}}+/// What the Connect state says this device is playing, as JSON.+///+/// `{"contextUri", "trackUri", "index", "tracks"}`, any of which can be empty.+///+/// The track list is the queue as the account handed it over, which is the+/// only copy of it for a context this app cannot read for itself.+///+/// Asked when another client has driven this device somewhere the app did not+/// send it. The account does not push this device a cluster update about its+/// own playing, so there is nothing to read from the outside: this comes from+/// the Connect state itself, which is the same thing the other devices are+/// shown.+pub fn playing_here() -> EngineResult<String> {+ let playing = with_bundle(|bundle| bundle.spirc.playing())?;+ let escape = |value: &str| serde_json::to_string(value).unwrap_or_else(|_| "\"\"".into());+ let tracks: Vec<String> = playing.tracks.iter().map(|uri| escape(uri)).collect();+ Ok(format!(+ "{{\"contextUri\":{},\"trackUri\":{},\"index\":{},\"tracks\":[{}]}}",+ escape(&playing.context_uri),+ escape(&playing.track_uri),+ playing.index,+ tracks.join(","),+ ))+}+pub fn play() -> EngineResult<()> {// Nothing happens here while the music is somewhere else. A play that// arrives then is Android's, not the listener's: focus coming back, adiff --git a/native/src/ffi.rs b/native/src/ffi.rsindex 6c9f37a..d7957af 100644--- a/native/src/ffi.rs+++ b/native/src/ffi.rs@@ -532,6 +532,15 @@ pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeDevguard_string(&mut env, "DeviceId", crate::remote::device_id)}+/// What this device itself is playing, straight from its Connect state.+#[no_mangle]+pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativePlayingHere(+ mut env: JNIEnv,+ _class: JClass,+) -> jstring {+ guard_string(&mut env, "PlayingHere", engine::playing_here)+}+/// What the account is playing, wherever it is playing it.#[no_mangle]pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeRemoteState(diff --git a/native/src/remote.rs b/native/src/remote.rsindex 9068d11..afe31e5 100644--- a/native/src/remote.rs+++ b/native/src/remote.rs@@ -76,10 +76,16 @@ pub fn watch(session: &Session, notify: impl Fn() + Send + 'static) {continue;};log::info!(- "cluster update: active {} of {} devices, own {}",+ "cluster update: active {} of {} devices, own {} [{}]",cluster.active_device_id,cluster.device.len(),device_id().unwrap_or_default(),+ cluster+ .device+ .iter()+ .map(|(id, device)| format!("{id}={}", device.name))+ .collect::<Vec<_>>()+ .join(", "),);if let Ok(mut stored) = CLUSTER.lock() {*stored = Some(cluster);diff --git a/native/vendor/README.md b/native/vendor/README.mdindex 762f840..b13cdd6 100644--- a/native/vendor/README.md+++ b/native/vendor/README.md@@ -156,3 +156,20 @@ the one making the sound, and refused to do its job.go-librespot keeps the same answer as a single boolean it owns and gateseverything on it; this is that boolean, borrowed back out of librespot.++### The patch: what is this device playing?++The same shape one step further in. `Spirc` also carries a `PlayingHere` behind+a mutex — context uri, track uri, and the previous, current and coming tracks in+playing order — republished by the loop alongside the flag above.++Another client can point this device anywhere: a track from a playlist the app+never opened, with no transfer to announce it. Nothing arrives from outside+either, because the account does not send a device the cluster update that+describes that device, so the owner of the handle was left showing whatever it+believed before.++The track list is there for the contexts Spotify makes rather than stores. A+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.diff --git a/native/vendor/librespot-connect/src/spirc.rs b/native/vendor/librespot-connect/src/spirc.rsindex 3d29445..fa5cae6 100644--- a/native/vendor/librespot-connect/src/spirc.rs+++ b/native/vendor/librespot-connect/src/spirc.rs@@ -76,6 +76,8 @@ struct SpircTask {connect_state: ConnectState,/// LOCAL PATCH: kept in step with `connect_state.is_active()`; see [`Spirc`].active: std::sync::Arc<std::sync::atomic::AtomicBool>,+ /// LOCAL PATCH: kept in step with what this device is playing; see [`Spirc`].+ playing: std::sync::Arc<std::sync::Mutex<PlayingHere>>,connect_established: bool,play_request_id: Option<u64>,@@ -156,6 +158,31 @@ pub struct Spirc {/// caller trusting the cluster refuses to play on a device that is already/// playing.active: std::sync::Arc<std::sync::atomic::AtomicBool>,+ /// LOCAL PATCH: what this device is playing.+ ///+ /// The same problem as `active`, one step further in. When another client+ /// drives this device the tracks change here and nothing outside the loop+ /// is told: the account does not send this device a cluster update about+ /// its own playing, so reading the cluster answers with whatever was true+ /// before. The owner of the handle needs the current answer to keep a+ /// queue of its own in step with the one being played.+ playing: std::sync::Arc<std::sync::Mutex<PlayingHere>>,+}++/// LOCAL PATCH: a snapshot of what the device is playing, for its owner.+///+/// The track list matters as much as the track. A context can be one the app+/// cannot resolve for itself — the editorial and algorithmic playlists answer+/// nothing useful to a client that is not Spotify's own — while the loop has+/// the real list in hand, because the account sent it here to be played.+#[derive(Clone, Default)]+pub struct PlayingHere {+ pub context_uri: String,+ pub track_uri: String,+ /// Previous, current and coming tracks, in playing order.+ pub tracks: Vec<String>,+ /// Where the current track sits in `tracks`.+ pub index: usize,}impl Spirc {@@ -163,6 +190,14 @@ impl Spirc {pub fn is_active(&self) -> bool {self.active.load(std::sync::atomic::Ordering::SeqCst)}++ /// LOCAL PATCH: what this device is playing right now.+ pub fn playing(&self) -> PlayingHere {+ self.playing+ .lock()+ .map(|state| state.clone())+ .unwrap_or_default()+ }}impl Spirc {@@ -274,16 +309,20 @@ impl Spirc {update_state: false,// LOCAL PATCH: replaced with the handle's own the moment it exists.active: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),+ playing: std::sync::Arc::new(std::sync::Mutex::new(PlayingHere::default())),spirc_id,};let active = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));task.active = active.clone();+ let playing = std::sync::Arc::new(std::sync::Mutex::new(PlayingHere::default()));+ task.playing = playing.clone();let spirc = Spirc {commands: cmd_tx,active,+ playing,};let initial_volume = task.connect_state.device_info().volume;@@ -440,6 +479,46 @@ impl Spirc {}impl SpircTask {+ /// LOCAL PATCH: hands the loop's own view of itself to the handle.+ ///+ /// Everything here is already known to the loop and to nobody else. The+ /// track is taken from the published player state rather than from the+ /// player, so what is read outside is what the account is being told.+ fn publish_local_state(&self) {+ self.active.store(+ self.connect_state.is_active(),+ std::sync::atomic::Ordering::SeqCst,+ );+ if let Ok(mut playing) = self.playing.lock() {+ let state = self.connect_state.player();+ let track = state+ .track+ .as_ref()+ .map(|track| track.uri.clone())+ .unwrap_or_default();+ let mut tracks: Vec<String> = state+ .prev_tracks+ .iter()+ .map(|track| track.uri.clone())+ .collect();+ if !track.is_empty() {+ tracks.push(track.clone());+ }+ tracks.extend(state.next_tracks.iter().map(|track| track.uri.clone()));+ // Queued items and the odd advert are in here too, and neither is+ // something the app can look up. Counted out before the position is+ // taken, or the position would point past them.+ tracks.retain(|uri| uri.starts_with("spotify:track:"));+ let index = tracks.iter().position(|uri| *uri == track).unwrap_or(0);+ *playing = PlayingHere {+ context_uri: state.context_uri.clone(),+ track_uri: track,+ tracks,+ index,+ };+ }+ }+async fn run(mut self) {// simplify unwrapping of received item or parsed resultmacro_rules! unwrap {@@ -467,12 +546,9 @@ impl SpircTask {while !self.session.is_invalid() && !self.shutdown {// LOCAL PATCH: published every turn, so a caller outside the loop can- // ask whether this device holds playback without guessing from the- // cluster; see [`Spirc`].- self.active.store(- self.connect_state.is_active(),- std::sync::atomic::Ordering::SeqCst,- );+ // ask whether this device holds playback, and what it is playing,+ // without guessing from the cluster; see [`Spirc`].+ self.publish_local_state();let commands = self.commands.as_mut();let player_events = self.player_events.as_mut();@@ -598,15 +674,12 @@ impl SpircTask {else => break}- // LOCAL PATCH: the flag again, now that this turn's event has been+ // LOCAL PATCH: the same again, now that this turn's event has been// handled. Publishing it only before the wait meant it was one// event stale for as long as nothing else happened, which is// exactly the case after a handover: the device had taken playback// and still answered that it had not.- self.active.store(- self.connect_state.is_active(),- std::sync::atomic::Ordering::SeqCst,- );+ self.publish_local_state();}if !self.shutdown && self.connect_state.is_active() {