Skip to content

Native Android client for Spotify, built with Jetpack Compose

Playback back to 2.0.1 on the page side: the play telemetry leaves from the WebView again, and Widevine is probed only after a failed play

kota-zof11e844dfadb
diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt
index 3d2f0fbf..cdd41db7 100644
--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt
+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt
@@ -555,6 +555,7 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
* then the user is told.
*/
private fun onRefused(late: Boolean = false) {
+ probeDrm()
val last = lastPlay ?: return
val now = System.currentTimeMillis()
if (now - last.third > if (late) LATE_REFUSED_WINDOW_MS else REFUSED_WINDOW_MS) return
@@ -1182,16 +1183,30 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScop
pausedByUser = false
repeatRestoreUntil = System.currentTimeMillis() + REPEAT_RESTORE_MS
withFade {
- command("player.play", timeoutMs = PLAY_TIMEOUT_MS, args = buildJsonObject {
- put("contextUri", contextUri)
- order?.let { uris -> putJsonArray("order") { uris.forEach { add(kotlinx.serialization.json.JsonPrimitive(it)) } } }
- trackUri?.let { put("trackUri", it) }
- index?.let { put("index", it) }
- positionMs?.let { put("positionMs", it) }
- })
+ try {
+ command("player.play", timeoutMs = PLAY_TIMEOUT_MS, args = buildJsonObject {
+ put("contextUri", contextUri)
+ order?.let { uris -> putJsonArray("order") { uris.forEach { add(kotlinx.serialization.json.JsonPrimitive(it)) } } }
+ trackUri?.let { put("trackUri", it) }
+ index?.let { put("index", it) }
+ positionMs?.let { put("positionMs", it) }
+ })
+ } catch (e: app.myspoti.spotify.bridge.BridgeException) {
+ probeDrm()
+ throw e
+ }
}
}
+ /**
+ * Asks the page whether its WebView has a working Widevine ([drmMissing]), after a play failed:
+ * never at boot, where the probe ran alongside the player's own EME set-up and the page of
+ * 2.0.2-beta.1 never registered its device. The page asks once; later calls only repeat the answer.
+ */
+ private fun probeDrm() {
+ scope.launch { runCatching { bridge.call("drm.probe", timeoutMs = 5_000, quiet = true) } }
+ }
+
/**
* A playback command, sent again when the page was not able to take it yet: a page just
* (re)loaded says "ready" a moment before Spotify Connect knows its device ("No such
diff --git a/base/api-spotify/src/main/resources/bridge/player-bridge.js b/base/api-spotify/src/main/resources/bridge/player-bridge.js
index d9996881..98f2eff5 100644
--- a/base/api-spotify/src/main/resources/bridge/player-bridge.js
+++ b/base/api-spotify/src/main/resources/bridge/player-bridge.js
@@ -684,8 +684,13 @@
// outdated or stripped Android System WebView, a device without a licence) plays nothing, which
// looks like "no sound", "refused" or "stuck at 0:00". Asked once per page, from the page's own
// EME; an empty robustness is the weakest level every Widevine device has (L3).
+ // Only on the host's request after a play failed ("drm.probe"), never at boot: asked while the
+ // player sets up its own EME (2.0.2-beta.1), the page never registered its playback device.
let drm = null;
+ let drmAsked = false;
const probeDrm = () => {
+ if (drmAsked) return;
+ drmAsked = true;
const report = (ok, error) => { drm = { type: 'drm', ok, error: error || '' }; post(drm); };
try {
if (!navigator.requestMediaKeySystemAccess) return report(false, 'no EME in this WebView');
@@ -695,6 +700,7 @@
}]).then(() => report(true), e => report(false, String((e && e.message) || e).slice(0, 200)));
} catch (e) { report(false, String((e && e.message) || e).slice(0, 200)); }
};
+ commands['drm.probe'] = () => { probeDrm(); return drm; };
const announce = () => {
if (!services) return;
@@ -731,7 +737,6 @@
park();
post({ type: 'ready', version: VERSION });
pushState();
- probeDrm();
return;
}
if (Date.now() - started > READY_TIMEOUT_MS) {
diff --git a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/PlaybackFollowUpTest.kt b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/PlaybackFollowUpTest.kt
index 1d5448ab..120281b8 100644
--- a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/PlaybackFollowUpTest.kt
+++ b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/PlaybackFollowUpTest.kt
@@ -121,6 +121,16 @@ class PlaybackFollowUpTest {
assertTrue(player.drmMissing.value)
}
+ // Asked at boot in 2.0.2-beta.1, the probe ran beside the player's own EME set-up and the page
+ // never registered its device: it is asked only once a play has gone wrong.
+ @Test fun `widevine is probed after a refusal, not at boot`() = runBlocking {
+ host.post("""{"type":"ready","version":"t"}""")
+ delay(300)
+ assertEquals(0, host.calls.count { it.first == "drm.probe" })
+ host.post("""{"type":"refused","status":402}""")
+ waitFor("drm probe") { host.calls.any { it.first == "drm.probe" } }
+ }
+
@Test fun `a refusal told late is dropped while another device plays`() = runBlocking {
host.mediaActive = false
host.mediaPaused = true
diff --git a/base/host-android/src/main/kotlin/app/myspoti/host/android/ControlRelay.kt b/base/host-android/src/main/kotlin/app/myspoti/host/android/ControlRelay.kt
index be9c3346..aa200885 100644
--- a/base/host-android/src/main/kotlin/app/myspoti/host/android/ControlRelay.kt
+++ b/base/host-android/src/main/kotlin/app/myspoti/host/android/ControlRelay.kt
@@ -279,17 +279,16 @@ internal class ControlRelay(private val view: WebView, private val fingerprint:
* removes it; the playback commands were only the first ones (that header alone was enough to
* silence them). One rule on Spotify's side is all it takes to flag every install.
*
- * Only the text endpoints are on the list, the ones whose bodies survive a `String`:
- * the playback commands and states, and the telemetry that reports what was played
- * (`melody`: the plays and the milliseconds really heard, base64 JSON; `gabo-receiver-service`:
- * the event batches). That report is what counts a listen in the history and in Wrapped,
- * and it left the WebView with the package name on it while the commands did not. Widening
- * this to everything else broke the page (protobuf and binary bodies, see the notes).
+ * Only the playback commands and states are on the list. The play telemetry (`melody`,
+ * `gabo-receiver-service`) was added in 2.0.2-beta.1 and that beta's page never registered
+ * its playback device on a free account ("Device not found", nothing plays): it leaves from
+ * the WebView again, as in 2.0.1, until it is proven harmless on a phone. Widening this to
+ * everything else broke the page too (protobuf and binary bodies, see the notes).
*
* Login and consent hosts stay out: their flows post forms and redirect, and a relay would
* have to reproduce every step exactly to sign in.
*/
- private val RELAYED = Regex("^https://[a-z0-9-]+(\\.[a-z0-9-]+)*\\.spotify\\.com/((connect-state|track-playback|melody)/v1/|gabo-receiver-service/)")
+ private val RELAYED = Regex("^https://[a-z0-9-]+(\\.[a-z0-9-]+)*\\.spotify\\.com/(connect-state|track-playback)/v1/")
private val NOT_RELAYED = Regex("^https://(accounts|challenge|www)\\.spotify\\.com/")
fun isRelayed(url: String) = RELAYED.containsMatchIn(url) && !NOT_RELAYED.containsMatchIn(url)
@@ -329,7 +328,7 @@ internal class ControlRelay(private val view: WebView, private val fingerprint:
if (window.__mspMuted) { this.muted = false; this.volume = 0.001; }
return play.apply(this, arguments);
};
- const relayed = /^https:\/\/([a-z0-9-]+\.)*spotify\.com\/((connect-state|track-playback|melody)\/v1\/|gabo-receiver-service\/)/;
+ const relayed = /^https:\/\/([a-z0-9-]+\.)*spotify\.com\/(connect-state|track-playback)\/v1\//;
const notRelayed = /^https:\/\/(accounts|challenge|www)\.spotify\.com\//;
const pending = new Map();
let seq = 0;
diff --git a/base/host-android/src/test/kotlin/app/myspoti/host/android/ControlRelayTest.kt b/base/host-android/src/test/kotlin/app/myspoti/host/android/ControlRelayTest.kt
index 20d276c2..6d187ac8 100644
--- a/base/host-android/src/test/kotlin/app/myspoti/host/android/ControlRelayTest.kt
+++ b/base/host-android/src/test/kotlin/app/myspoti/host/android/ControlRelayTest.kt
@@ -11,11 +11,12 @@ class ControlRelayTest {
assertTrue(ControlRelay.isRelayed("https://gew4-spclient.spotify.com/track-playback/v1/devices/a/state"))
}
- // What reports a listen (the milliseconds really played) must not leave with the package name.
- @Test fun `play telemetry leaves from the app`() {
- assertTrue(ControlRelay.isRelayed("https://gew4-spclient.spotify.com/melody/v1/msg/batch"))
- assertTrue(ControlRelay.isRelayed("https://gew4-spclient.spotify.com/gabo-receiver-service/v3/events"))
- assertTrue(ControlRelay.isRelayed("https://gew4-spclient.spotify.com/gabo-receiver-service/public/v3/events"))
+ // Relayed in 2.0.2-beta.1, whose page then never registered its device: back with the WebView.
+ @Test fun `play telemetry stays with the WebView, as in 2_0_1`() {
+ assertFalse(ControlRelay.isRelayed("https://gew4-spclient.spotify.com/melody/v1/msg/batch"))
+ assertFalse(ControlRelay.isRelayed("https://gew4-spclient.spotify.com/melody/v1/time"))
+ assertFalse(ControlRelay.isRelayed("https://gew4-spclient.spotify.com/gabo-receiver-service/v3/events"))
+ assertFalse(ControlRelay.isRelayed("https://gew4-spclient.spotify.com/gabo-receiver-service/public/v3/events"))
}
@Test fun `sign-in, audio and pages stay with the WebView`() {