From c13a4053ac433ca9a462917fb6ff28ab8b604702 Mon Sep 17 00:00:00 2001 From: Yuriy Panov Date: Fri, 23 Oct 2026 13:26:43 +0600 Subject: [PATCH] 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 --- .../java/it/panov/musyx/player/Buffering.kt | 47 ++++++++++ .../java/it/panov/musyx/player/ExoEngine.kt | 85 ++++++++++++++++++- .../panov/musyx/player/PlaybackController.kt | 3 +- .../java/it/panov/musyx/player/PlayerCore.kt | 7 +- .../java/it/panov/musyx/state/AppState.kt | 4 + .../main/java/it/panov/musyx/mobile/Musyx.kt | 3 + 6 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 core/src/main/java/it/panov/musyx/player/Buffering.kt diff --git a/core/src/main/java/it/panov/musyx/player/Buffering.kt b/core/src/main/java/it/panov/musyx/player/Buffering.kt new file mode 100644 index 0000000..90c22b0 --- /dev/null +++ b/core/src/main/java/it/panov/musyx/player/Buffering.kt @@ -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, + ) + } +} diff --git a/core/src/main/java/it/panov/musyx/player/ExoEngine.kt b/core/src/main/java/it/panov/musyx/player/ExoEngine.kt index 3179180..bdbc038 100644 --- a/core/src/main/java/it/panov/musyx/player/ExoEngine.kt +++ b/core/src/main/java/it/panov/musyx/player/ExoEngine.kt @@ -7,6 +7,7 @@ import android.content.Context import android.net.Uri import android.os.Handler import android.os.Looper +import android.util.Log import androidx.media3.common.AudioAttributes import androidx.media3.common.C import androidx.media3.common.MediaItem @@ -14,15 +15,25 @@ import androidx.media3.common.PlaybackException import androidx.media3.common.Player import androidx.annotation.OptIn 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.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.source.DefaultMediaSourceFactory import it.panov.musyx.api.CLIENT_HEADER import it.panov.musyx.api.Stream import it.panov.musyx.api.Track import it.panov.musyx.api.isYandexHost +import java.io.InterruptedIOException import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.Executors interface EngineEvents { fun loaded(generation: Long, durationMs: Long?) @@ -34,10 +45,22 @@ interface EngineEvents { } @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. */ private val keys = ConcurrentHashMap() + /** 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 private var generation = 0L @@ -68,9 +91,29 @@ class ExoEngine(context: Context, private val token: () -> String?, private val token()?.let { headers["Authorization"] = "OAuth $it" } 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) .setMediaSourceFactory(DefaultMediaSourceFactory(decrypting)) + .setLoadControl(loadControl) .setAudioAttributes( AudioAttributes.Builder().setUsage(C.USAGE_MEDIA).setContentType(C.AUDIO_CONTENT_TYPE_MUSIC).build(), /* 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) { this.generation = generation ready = false + // Whatever was preloading is either this track, whose cached start is used now, or stale. + cancelPreload() mirrors = stream.urls mirror = 0 keys.clear() @@ -112,10 +157,42 @@ class ExoEngine(context: Context, private val token: () -> String?, private val override fun stop() { ready = false + cancelPreload() exo.stop() 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 { override fun onPlaybackStateChanged(state: Int) { 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) { // Another mirror of the same file may still answer. if (!ready && mirror + 1 < mirrors.size) { diff --git a/core/src/main/java/it/panov/musyx/player/PlaybackController.kt b/core/src/main/java/it/panov/musyx/player/PlaybackController.kt index 05862fe..83820be 100644 --- a/core/src/main/java/it/panov/musyx/player/PlaybackController.kt +++ b/core/src/main/java/it/panov/musyx/player/PlaybackController.kt @@ -50,9 +50,10 @@ class PlaybackController( private val waveSettings: () -> WaveSettings?, private val token: () -> String?, private val onSignedOut: () -> Unit, + buffering: Buffering, ) : EngineEvents { 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. */ val sessionPlayer = SessionPlayer(engine.exo, this) diff --git a/core/src/main/java/it/panov/musyx/player/PlayerCore.kt b/core/src/main/java/it/panov/musyx/player/PlayerCore.kt index 097017f..9df8149 100644 --- a/core/src/main/java/it/panov/musyx/player/PlayerCore.kt +++ b/core/src/main/java/it/panov/musyx/player/PlayerCore.kt @@ -57,6 +57,8 @@ interface Engine { fun pause() fun seek(positionMs: Long) 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. */ @@ -280,7 +282,10 @@ class PlayerCore(private val random: Random = Random.Default) { } fun prefetched(trackId: String, result: Result) { - result.onSuccess { prefetched = trackId to it } + result.onSuccess { + prefetched = trackId to it + engine?.preload(it) + } .onFailure { android.util.Log.w("Musyx", "prefetch $trackId: $it") } } diff --git a/core/src/main/java/it/panov/musyx/state/AppState.kt b/core/src/main/java/it/panov/musyx/state/AppState.kt index 32a1d00..9337489 100644 --- a/core/src/main/java/it/panov/musyx/state/AppState.kt +++ b/core/src/main/java/it/panov/musyx/state/AppState.kt @@ -41,6 +41,7 @@ import it.panov.musyx.data.Locale import it.panov.musyx.data.Quality import it.panov.musyx.data.Settings import it.panov.musyx.data.SettingsStore +import it.panov.musyx.player.Buffering import it.panov.musyx.player.PlaybackController import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch @@ -77,6 +78,8 @@ class AppState( private val systemLanguage: () -> String, /** Coil's disk cache, cleared from Settings. */ 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) private set @@ -145,6 +148,7 @@ class AppState( waveSettings = { waveShown }, token = { credentials?.accessToken }, onSignedOut = ::signOut, + buffering = buffering, ) val client: YmClient? get() = (session as? Session.SignedIn)?.client diff --git a/mobile/src/main/java/it/panov/musyx/mobile/Musyx.kt b/mobile/src/main/java/it/panov/musyx/mobile/Musyx.kt index 3d818d5..f1502a8 100644 --- a/mobile/src/main/java/it/panov/musyx/mobile/Musyx.kt +++ b/mobile/src/main/java/it/panov/musyx/mobile/Musyx.kt @@ -16,6 +16,7 @@ import coil3.request.crossfade import it.panov.musyx.api.Lrclib import it.panov.musyx.data.CredentialStore import it.panov.musyx.data.SettingsStore +import it.panov.musyx.player.Buffering import it.panov.musyx.state.AppState import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -41,6 +42,8 @@ class Musyx : Application(), SingletonImageLoader.Factory { clearCovers = { 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, ) }