Skip to content

Commit

Permalink
Cache computed PlayableMediaStream in QueueEntry element
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsvanvelzen committed May 5, 2024
1 parent 01642e6 commit de04539
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
19 changes: 12 additions & 7 deletions playback/core/src/main/kotlin/mediastream/MediaStreamState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions playback/core/src/main/kotlin/mediastream/QueueEntryMediaStream.kt
Original file line number Diff line number Diff line change
@@ -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<PlayableMediaStream>("MediaStream")

/**
* Get or set the [MediaStream] for this [QueueEntry].
*/
var QueueEntry.mediaStream by element(mediaStreamKey)

0 comments on commit de04539

Please sign in to comment.