Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
A cold start no longer throws the player's page away, nor fetches the same segments again and again
kota-zobd0287dd8ffc
diff --git a/base/app/src/main/java/com/music/spotui/data/offline/DashLive.kt b/base/app/src/main/java/com/music/spotui/data/offline/DashLive.ktindex 75702a00..bb4f4bcd 100644--- a/base/app/src/main/java/com/music/spotui/data/offline/DashLive.kt+++ b/base/app/src/main/java/com/music/spotui/data/offline/DashLive.kt@@ -27,16 +27,22 @@ internal class DashLive(var at = head.size.toLong()for (i in segmentSizes.indices) { s[i] = at; at += segmentSizes[i] }}- /** Guards [cachedIndex], [cached] and [loading]; never held during a fetch. */+ /** Guards [cached] and [loading]; never held during a fetch. */private val lock = Any()- private var cachedIndex = -1- private var cached: ByteArray = ByteArray(0)+ /**+ * The last segments read, for readers at different places: with only one kept, the player and the+ * song's analysis took it from each other and fetched the same segments again and again (seen: 381+ * fetches, 253 of them repeats, 155 MB in the first minute, while the page was trying to load).+ */+ private val cached = object : LinkedHashMap<Int, ByteArray>(KEPT_SEGMENTS + 1, 0.75f, true) {+ override fun removeEldestEntry(eldest: MutableMap.MutableEntry<Int, ByteArray>?) = size > KEPT_SEGMENTS+ }/** The segments being fetched: a read of one waits for that fetch instead of starting another. */private val loading = HashMap<Int, FutureTask<ByteArray>>()/** Fetches the first segment ahead, so the song starts without waiting for it. */fun warm() {- if (segmentUrls.isNotEmpty() && synchronized(lock) { cachedIndex < 0 }) segment(0)+ if (segmentUrls.isNotEmpty() && synchronized(lock) { cached.isEmpty() }) segment(0)}/**@@ -46,7 +52,7 @@ internal class DashLive(*/private fun segment(i: Int): ByteArray {val task = synchronized(lock) {- if (cachedIndex == i) return cached+ cached[i]?.let { return it }loading.getOrPut(i) { FutureTask { fetchChecked(i) } }}task.run() // nothing when another thread runs or ran it: then only waits for it below@@ -61,8 +67,7 @@ internal class DashLive(}synchronized(lock) {loading.remove(i, task)- cached = bytes- cachedIndex = i+ cached[i] = bytes}return bytes}@@ -96,6 +101,8 @@ internal class DashLive(data class Plan(val initUrl: String, val segmentUrls: List<String>, val timescale: Long, val durations: LongArray)companion object {+ /** Segments kept per song: two readers, each with the one it is on and the one before. */+ private const val KEPT_SEGMENTS = 4// Segment sizes are asked all at once (same CDN host, kept-alive connections): ~4 rounds for a song.private const val PARALLEL = 16private val cache = object : LinkedHashMap<String, Pair<Long, DashLive>>(8, 0.75f, true) {diff --git a/base/app/src/test/java/com/music/spotui/data/offline/DashLiveTest.kt b/base/app/src/test/java/com/music/spotui/data/offline/DashLiveTest.ktindex ec2e1695..d4d4a958 100644--- a/base/app/src/test/java/com/music/spotui/data/offline/DashLiveTest.kt+++ b/base/app/src/test/java/com/music/spotui/data/offline/DashLiveTest.kt@@ -36,6 +36,14 @@ class DashLiveTest {assertEquals(400, b.int); assertEquals(20, b.int)}+ @Test fun twoReadersAtDifferentPlacesDoNotFetchTheSameSegmentsAgain() {+ val fetched = ArrayList<String>()+ val live = DashLive(byteArrayOf(), listOf("s1", "s2", "s3"), longArrayOf(4, 4, 4)) { fetched += it; ByteArray(4) }+ val b = ByteArray(1)+ for (i in 0 until 4) { live.read(i.toLong(), b, 0, 1); live.read(8L + i, b, 0, 1) }+ assertEquals(listOf("s1", "s3"), fetched)+ }+@Test fun theLiveFileReadsAcrossSegmentsAndFromAnyPosition() {val segs = mapOf("s1" to ByteArray(5) { (10 + it).toByte() }, "s2" to ByteArray(3) { (20 + it).toByte() })val fetched = mutableListOf<String>()diff --git a/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.kt b/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.ktindex 87d017a4..aaba7871 100644--- a/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.kt+++ b/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.kt@@ -350,6 +350,9 @@ class WebPlayerHost(private val context: Context) : BridgeHost {override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {Logbook.log("page start ${Logbook.noQuery(url).take(120)}")mainFrameFailed = false+ // A "finished" comes just before the player's document starts: its wait must not run out on a+ // page still loading (a page thrown away at 10 s, and a cold start of 30 s, on the test phone).+ main.removeCallbacks(drmWatch)// A load that never ends (measured: a reload's document request answered nothing// for 9 minutes, the page stayed "loading", empty, without the bridge).main.removeCallbacks(loadTimeout)