-
-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84a1680
commit 59153d4
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
playback/jellyfin/src/main/kotlin/mediastream/VideoMediaStreamResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package org.jellyfin.playback.jellyfin.mediastream | ||
|
||
import org.jellyfin.playback.core.mediastream.BasicMediaStream | ||
import org.jellyfin.playback.core.mediastream.MediaConversionMethod | ||
import org.jellyfin.playback.core.mediastream.MediaStream | ||
import org.jellyfin.playback.core.mediastream.MediaStreamContainer | ||
import org.jellyfin.playback.core.mediastream.PlayableMediaStream | ||
import org.jellyfin.playback.core.queue.QueueEntry | ||
import org.jellyfin.playback.core.support.PlaySupportReport | ||
import org.jellyfin.playback.jellyfin.queue.baseItem | ||
import org.jellyfin.sdk.api.client.ApiClient | ||
import org.jellyfin.sdk.api.client.extensions.dynamicHlsApi | ||
import org.jellyfin.sdk.api.client.extensions.videosApi | ||
import org.jellyfin.sdk.model.api.DeviceProfile | ||
import org.jellyfin.sdk.model.constant.MediaType | ||
|
||
class VideoMediaStreamResolver( | ||
val api: ApiClient, | ||
val profile: DeviceProfile, | ||
) : JellyfinStreamResolver(api, profile) { | ||
companion object { | ||
private val REMUX_CONTAINERS = arrayOf("mp4", "mkv") | ||
private const val REMUX_SEGMENT_CONTAINER = "mp4" | ||
} | ||
|
||
private fun MediaInfo.getDirectPlayStream() = BasicMediaStream( | ||
identifier = playSessionId, | ||
conversionMethod = MediaConversionMethod.None, | ||
container = getMediaStreamContainer(), | ||
tracks = getTracks() | ||
) | ||
|
||
private fun MediaInfo.getRemuxStream(container: String) = BasicMediaStream( | ||
identifier = playSessionId, | ||
conversionMethod = MediaConversionMethod.Remux, | ||
container = MediaStreamContainer( | ||
format = container | ||
), | ||
tracks = getTracks() | ||
) | ||
|
||
private fun MediaInfo.getTranscodeStream() = BasicMediaStream( | ||
identifier = playSessionId, | ||
conversionMethod = MediaConversionMethod.Transcode, | ||
// The server doesn't provide us with the transcode information so we return mock data | ||
container = MediaStreamContainer(format = "unknown"), | ||
tracks = emptyList() | ||
) | ||
|
||
override suspend fun getStream( | ||
queueEntry: QueueEntry, | ||
testStream: (stream: MediaStream) -> PlaySupportReport, | ||
): PlayableMediaStream? { | ||
val baseItem = queueEntry.baseItem | ||
if (baseItem == null || baseItem.mediaType != MediaType.Video) return null | ||
|
||
val mediaInfo = getPlaybackInfo(baseItem) | ||
|
||
// Test for direct play support | ||
val directPlayStream = mediaInfo.getDirectPlayStream() | ||
if (testStream(directPlayStream).canPlay) { | ||
return directPlayStream.toPlayableMediaStream( | ||
queueEntry = queueEntry, | ||
url = api.videosApi.getVideoStreamUrl( | ||
itemId = baseItem.id, | ||
mediaSourceId = mediaInfo.mediaSource.id, | ||
playSessionId = mediaInfo.playSessionId, | ||
static = true, | ||
) | ||
) | ||
} | ||
|
||
// Try remuxing | ||
if (mediaInfo.mediaSource.supportsDirectStream) { | ||
for (container in REMUX_CONTAINERS) { | ||
val remuxStream = mediaInfo.getRemuxStream(container) | ||
if (testStream(remuxStream).canPlay) { | ||
return remuxStream.toPlayableMediaStream( | ||
queueEntry = queueEntry, | ||
url = api.videosApi.getVideoStreamByContainerUrl( | ||
itemId = baseItem.id, | ||
mediaSourceId = mediaInfo.mediaSource.id, | ||
playSessionId = mediaInfo.playSessionId, | ||
container = container, | ||
) | ||
) | ||
} | ||
} | ||
} | ||
|
||
// Fallback to provided transcode | ||
if (mediaInfo.mediaSource.supportsTranscoding) { | ||
val transcodeStream = mediaInfo.getTranscodeStream() | ||
|
||
// Skip testing transcode stream because we lack the information to do so | ||
return transcodeStream.toPlayableMediaStream( | ||
queueEntry = queueEntry, | ||
url = api.dynamicHlsApi.getMasterHlsVideoPlaylistUrl( | ||
itemId = baseItem.id, | ||
mediaSourceId = requireNotNull(mediaInfo.mediaSource.id), | ||
playSessionId = mediaInfo.playSessionId, | ||
tag = mediaInfo.mediaSource.eTag, | ||
segmentContainer = REMUX_SEGMENT_CONTAINER, | ||
) | ||
) | ||
} | ||
|
||
// Unable to find a suitable stream, return | ||
return null | ||
} | ||
} |