Buffer further ahead on the phone for slow networks

The phone app now keeps up to ten minutes buffered (capped at 48 MB), and after
a stall waits for ten seconds of audio before resuming, so a weak link doesn't
stutter. Once the current track has finished loading, the next one is fetched
into a 256 MB disk cache, still encrypted, so it starts at once.

Both are set by a Buffering passed to AppState; the TV keeps ExoPlayer's defaults
and no audio cache.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Yuriy PanovandClaude Opus 5.5 committed 2026-10-23 13:26:43 +06:00
1 parent 39ab994a02
commit c13a4053ac
6 files changed
+145 -4

No files matched your search

@@ -0,0 +1,47 @@
// How much audio the engine keeps ahead. The phone plays over mobile data that slows and
// drops out, so it buffers whole tracks and fetches the next one to disk before it's needed;
// the TV sits on a steady connection and keeps ExoPlayer's defaults.
package it.panov.musyx.player
import androidx.annotation.OptIn
import androidx.media3.common.C
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.DefaultLoadControl
@OptIn(UnstableApi::class)
data class Buffering(
/** Loading resumes once less than this is buffered ahead. */
val minMs: Int,
/** Loading pauses once this much is buffered ahead. */
val maxMs: Int,
/** Buffered before a track starts. */
val startMs: Int,
/** Buffered before playback resumes after running dry. */
val resumeMs: Int,
/** A cap on the memory the buffer takes, or [C.LENGTH_UNSET] for ExoPlayer's own. */
val maxBytes: Int,
/** Disk for the next track, fetched while the current one plays; 0 turns that off. */
val preloadBytes: Long,
) {
companion object {
val Default = Buffering(
minMs = DefaultLoadControl.DEFAULT_MIN_BUFFER_MS,
maxMs = DefaultLoadControl.DEFAULT_MAX_BUFFER_MS,
startMs = DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS,
resumeMs = DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS,
maxBytes = C.LENGTH_UNSET,
preloadBytes = 0,
)
/** A whole track in memory, and a longer wait after a stall so a slow link doesn't stutter. */
val Mobile = Buffering(
minMs = 120_000,
maxMs = 600_000,
startMs = 2_500,
resumeMs = 10_000,
// Ten minutes of 320 kbps, or four of lossless.
maxBytes = 48 * 1024 * 1024,
preloadBytes = 256L * 1024 * 1024,
)
}
}
@@ -7,6 +7,7 @@ import android.content.Context
import android.net.Uri import android.net.Uri
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import android.util.Log
import androidx.media3.common.AudioAttributes import androidx.media3.common.AudioAttributes
import androidx.media3.common.C import androidx.media3.common.C
import androidx.media3.common.MediaItem import androidx.media3.common.MediaItem
@@ -14,15 +15,25 @@ import androidx.media3.common.PlaybackException
import androidx.media3.common.Player import androidx.media3.common.Player
import androidx.annotation.OptIn import androidx.annotation.OptIn
import androidx.media3.common.util.UnstableApi import androidx.media3.common.util.UnstableApi
import androidx.media3.database.StandaloneDatabaseProvider
import androidx.media3.datasource.DataSource
import androidx.media3.datasource.DataSpec
import androidx.media3.datasource.DefaultHttpDataSource import androidx.media3.datasource.DefaultHttpDataSource
import androidx.media3.datasource.ResolvingDataSource import androidx.media3.datasource.ResolvingDataSource
import androidx.media3.datasource.cache.CacheDataSource
import androidx.media3.datasource.cache.CacheWriter
import androidx.media3.datasource.cache.LeastRecentlyUsedCacheEvictor
import androidx.media3.datasource.cache.SimpleCache
import androidx.media3.exoplayer.DefaultLoadControl
import androidx.media3.exoplayer.ExoPlayer import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
import it.panov.musyx.api.CLIENT_HEADER import it.panov.musyx.api.CLIENT_HEADER
import it.panov.musyx.api.Stream import it.panov.musyx.api.Stream
import it.panov.musyx.api.Track import it.panov.musyx.api.Track
import it.panov.musyx.api.isYandexHost import it.panov.musyx.api.isYandexHost
import java.io.InterruptedIOException
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executors
interface EngineEvents { interface EngineEvents {
fun loaded(generation: Long, durationMs: Long?) fun loaded(generation: Long, durationMs: Long?)
@@ -34,10 +45,22 @@ interface EngineEvents {
} }
@OptIn(UnstableApi::class) @OptIn(UnstableApi::class)
class ExoEngine(context: Context, private val token: () -> String?, private val events: EngineEvents) : Engine { class ExoEngine(
context: Context,
private val token: () -> String?,
private val events: EngineEvents,
buffering: Buffering = Buffering.Default,
) : Engine {
/** Keys for encrypted files, by URL. Read on ExoPlayer's loader threads. */ /** Keys for encrypted files, by URL. Read on ExoPlayer's loader threads. */
private val keys = ConcurrentHashMap<String, ByteArray>() private val keys = ConcurrentHashMap<String, ByteArray>()
/** Reads through the disk cache the next track is preloaded into; null when preloading is off. */
private val cached: CacheDataSource.Factory?
private val preloader = Executors.newSingleThreadExecutor()
private var preloading: CacheWriter? = null
/** The next track's URL, to preload once the current track has stopped loading. */
private var upcoming: String? = null
val exo: ExoPlayer val exo: ExoPlayer
private var generation = 0L private var generation = 0L
@@ -68,9 +91,29 @@ class ExoEngine(context: Context, private val token: () -> String?, private val
token()?.let { headers["Authorization"] = "OAuth $it" } token()?.let { headers["Authorization"] = "OAuth $it" }
spec.withAdditionalHeaders(headers) spec.withAdditionalHeaders(headers)
} }
val decrypting = AesCtrDataSource.Factory(identified) { uri: Uri -> keys[uri.toString()] } // Files are cached as they come, still encrypted; keys stay in memory only.
cached = if (buffering.preloadBytes > 0) {
val cache = SimpleCache(
context.cacheDir.resolve("audio"),
LeastRecentlyUsedCacheEvictor(buffering.preloadBytes),
StandaloneDatabaseProvider(context),
)
CacheDataSource.Factory()
.setCache(cache)
.setUpstreamDataSourceFactory(identified)
.setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
} else {
null
}
val source: DataSource.Factory = cached ?: identified
val decrypting = AesCtrDataSource.Factory(source) { uri: Uri -> keys[uri.toString()] }
val loadControl = DefaultLoadControl.Builder()
.setBufferDurationsMs(buffering.minMs, buffering.maxMs, buffering.startMs, buffering.resumeMs)
.setTargetBufferBytes(buffering.maxBytes)
.build()
exo = ExoPlayer.Builder(context) exo = ExoPlayer.Builder(context)
.setMediaSourceFactory(DefaultMediaSourceFactory(decrypting)) .setMediaSourceFactory(DefaultMediaSourceFactory(decrypting))
.setLoadControl(loadControl)
.setAudioAttributes( .setAudioAttributes(
AudioAttributes.Builder().setUsage(C.USAGE_MEDIA).setContentType(C.AUDIO_CONTENT_TYPE_MUSIC).build(), AudioAttributes.Builder().setUsage(C.USAGE_MEDIA).setContentType(C.AUDIO_CONTENT_TYPE_MUSIC).build(),
/* handleAudioFocus = */ true, /* handleAudioFocus = */ true,
@@ -85,6 +128,8 @@ class ExoEngine(context: Context, private val token: () -> String?, private val
override fun load(generation: Long, track: Track, stream: Stream, play: Boolean) { override fun load(generation: Long, track: Track, stream: Stream, play: Boolean) {
this.generation = generation this.generation = generation
ready = false ready = false
// Whatever was preloading is either this track, whose cached start is used now, or stale.
cancelPreload()
mirrors = stream.urls mirrors = stream.urls
mirror = 0 mirror = 0
keys.clear() keys.clear()
@@ -112,10 +157,42 @@ class ExoEngine(context: Context, private val token: () -> String?, private val
override fun stop() { override fun stop() {
ready = false ready = false
cancelPreload()
exo.stop() exo.stop()
exo.clearMediaItems() exo.clearMediaItems()
} }
override fun preload(stream: Stream) {
if (cached == null) return
cancelPreload()
upcoming = stream.urls.firstOrNull()
if (ready && !exo.isLoading) startPreload()
}
/** Fetches the next track to disk, after the current one so the two don't share the link. */
private fun startPreload() {
val url = upcoming ?: return
val factory = cached ?: return
upcoming = null
val writer = CacheWriter(factory.createDataSourceForDownloading(), DataSpec(Uri.parse(url)), null, null)
preloading = writer
preloader.execute {
try {
writer.cache()
} catch (_: InterruptedIOException) {
// Cancelled: another track started.
} catch (e: Exception) {
Log.w("Musyx", "preload: $e")
}
}
}
private fun cancelPreload() {
upcoming = null
preloading?.cancel()
preloading = null
}
private inner class Listener : Player.Listener { private inner class Listener : Player.Listener {
override fun onPlaybackStateChanged(state: Int) { override fun onPlaybackStateChanged(state: Int) {
when (state) { when (state) {
@@ -131,6 +208,10 @@ class ExoEngine(context: Context, private val token: () -> String?, private val
} }
} }
override fun onIsLoadingChanged(isLoading: Boolean) {
if (!isLoading && ready) startPreload()
}
override fun onPlayerError(error: PlaybackException) { override fun onPlayerError(error: PlaybackException) {
// Another mirror of the same file may still answer. // Another mirror of the same file may still answer.
if (!ready && mirror + 1 < mirrors.size) { if (!ready && mirror + 1 < mirrors.size) {
@@ -50,9 +50,10 @@ class PlaybackController(
private val waveSettings: () -> WaveSettings?, private val waveSettings: () -> WaveSettings?,
private val token: () -> String?, private val token: () -> String?,
private val onSignedOut: () -> Unit, private val onSignedOut: () -> Unit,
buffering: Buffering,
) : EngineEvents { ) : EngineEvents {
private val core = PlayerCore() private val core = PlayerCore()
private val engine = ExoEngine(context, token, this) private val engine = ExoEngine(context, token, this, buffering)
/** The ExoPlayer, wrapped so the media session's next/previous reach the queue. */ /** The ExoPlayer, wrapped so the media session's next/previous reach the queue. */
val sessionPlayer = SessionPlayer(engine.exo, this) val sessionPlayer = SessionPlayer(engine.exo, this)
@@ -57,6 +57,8 @@ interface Engine {
fun pause() fun pause()
fun seek(positionMs: Long) fun seek(positionMs: Long)
fun stop() fun stop()
/** The stream that plays next, to fetch ahead if the engine can. */
fun preload(stream: Stream) {}
} }
/** A station being played, endlessly topped up. */ /** A station being played, endlessly topped up. */
@@ -280,7 +282,10 @@ class PlayerCore(private val random: Random = Random.Default) {
} }
fun prefetched(trackId: String, result: Result<Stream>) { fun prefetched(trackId: String, result: Result<Stream>) {
result.onSuccess { prefetched = trackId to it } result.onSuccess {
prefetched = trackId to it
engine?.preload(it)
}
.onFailure { android.util.Log.w("Musyx", "prefetch $trackId: $it") } .onFailure { android.util.Log.w("Musyx", "prefetch $trackId: $it") }
} }
@@ -41,6 +41,7 @@ import it.panov.musyx.data.Locale
import it.panov.musyx.data.Quality import it.panov.musyx.data.Quality
import it.panov.musyx.data.Settings import it.panov.musyx.data.Settings
import it.panov.musyx.data.SettingsStore import it.panov.musyx.data.SettingsStore
import it.panov.musyx.player.Buffering
import it.panov.musyx.player.PlaybackController import it.panov.musyx.player.PlaybackController
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@@ -77,6 +78,8 @@ class AppState(
private val systemLanguage: () -> String, private val systemLanguage: () -> String,
/** Coil's disk cache, cleared from Settings. */ /** Coil's disk cache, cleared from Settings. */
private val clearCovers: suspend () -> Unit, private val clearCovers: suspend () -> Unit,
/** How far ahead playback loads; the phone loads further than the TV. */
buffering: Buffering = Buffering.Default,
) { ) {
var session: Session by mutableStateOf(Session.Restoring) var session: Session by mutableStateOf(Session.Restoring)
private set private set
@@ -145,6 +148,7 @@ class AppState(
waveSettings = { waveShown }, waveSettings = { waveShown },
token = { credentials?.accessToken }, token = { credentials?.accessToken },
onSignedOut = ::signOut, onSignedOut = ::signOut,
buffering = buffering,
) )
val client: YmClient? get() = (session as? Session.SignedIn)?.client val client: YmClient? get() = (session as? Session.SignedIn)?.client
@@ -16,6 +16,7 @@ import coil3.request.crossfade
import it.panov.musyx.api.Lrclib import it.panov.musyx.api.Lrclib
import it.panov.musyx.data.CredentialStore import it.panov.musyx.data.CredentialStore
import it.panov.musyx.data.SettingsStore import it.panov.musyx.data.SettingsStore
import it.panov.musyx.player.Buffering
import it.panov.musyx.state.AppState import it.panov.musyx.state.AppState
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@@ -41,6 +42,8 @@ class Musyx : Application(), SingletonImageLoader.Factory {
clearCovers = { clearCovers = {
withContext(Dispatchers.IO) { SingletonImageLoader.get(this@Musyx).diskCache?.clear() } withContext(Dispatchers.IO) { SingletonImageLoader.get(this@Musyx).diskCache?.clear() }
}, },
// Mobile data slows and drops out: buffer whole tracks and preload the next one.
buffering = Buffering.Mobile,
) )
} }