diff --git a/playback/core/src/main/kotlin/mediastream/MediaStreamState.kt b/playback/core/src/main/kotlin/mediastream/MediaStreamState.kt index 4bc06a321d..e9b547f776 100644 --- a/playback/core/src/main/kotlin/mediastream/MediaStreamState.kt +++ b/playback/core/src/main/kotlin/mediastream/MediaStreamState.kt @@ -11,6 +11,7 @@ import kotlinx.coroutines.plus import org.jellyfin.playback.core.PlayerState import org.jellyfin.playback.core.backend.BackendService import org.jellyfin.playback.core.backend.PlayerBackend +import org.jellyfin.playback.core.queue.QueueEntry import timber.log.Timber interface MediaStreamState { @@ -38,13 +39,7 @@ class DefaultMediaStreamState( if (entry == null) { backend.setCurrent(null) } else { - val stream = mediaStreamResolvers.firstNotNullOfOrNull { resolver -> - runCatching { - resolver.getStream(entry, backend::supportsStream) - }.onFailure { - Timber.e(it, "Media stream resolver failed for $entry") - }.getOrNull() - } + val stream = entry.getOrComputeMediaStream(backend) if (stream == null) { Timber.e("Unable to resolve stream for entry $entry") @@ -64,6 +59,16 @@ class DefaultMediaStreamState( // TODO Register some kind of event when $current item is at -30 seconds to setNext() } + private suspend fun QueueEntry.getOrComputeMediaStream( + backend: PlayerBackend, + ): PlayableMediaStream? = mediaStream ?: mediaStreamResolvers.firstNotNullOfOrNull { resolver -> + runCatching { + resolver.getStream(this, backend::supportsStream) + }.onFailure { + Timber.e(it, "Media stream resolver failed for $this") + }.getOrNull() + }.also { mediaStream = it } + private fun PlayerBackend.setCurrent(stream: PlayableMediaStream?) { Timber.d("Current stream changed to $stream") _current.value = stream diff --git a/playback/core/src/main/kotlin/mediastream/QueueEntryMediaStream.kt b/playback/core/src/main/kotlin/mediastream/QueueEntryMediaStream.kt new file mode 100644 index 0000000000..d15974e068 --- /dev/null +++ b/playback/core/src/main/kotlin/mediastream/QueueEntryMediaStream.kt @@ -0,0 +1,12 @@ +package org.jellyfin.playback.core.mediastream + +import org.jellyfin.playback.core.element.ElementKey +import org.jellyfin.playback.core.element.element +import org.jellyfin.playback.core.queue.QueueEntry + +private val mediaStreamKey = ElementKey("MediaStream") + +/** + * Get or set the [MediaStream] for this [QueueEntry]. + */ +var QueueEntry.mediaStream by element(mediaStreamKey)