Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache computed PlayableMediaStream in QueueEntry element #3555

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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)