make streamselect reflect reality better

This commit is contained in:
Christian Schabesberger 2024-09-13 14:17:21 +02:00
parent c37e44a56e
commit 632407fa69
1 changed files with 43 additions and 20 deletions

View File

@ -114,6 +114,14 @@ object StreamSelect {
private fun getNonDynamicAudioVariants(availableStreamVariants: List<StreamVariant>) = private fun getNonDynamicAudioVariants(availableStreamVariants: List<StreamVariant>) =
availableStreamVariants.filter { it.streamType == StreamType.AUDIO } availableStreamVariants.filter { it.streamType == StreamType.AUDIO }
private fun hasVideoStreamVariants(availableStreamVariants: List<StreamVariant>): Boolean {
for (variant in availableStreamVariants) {
if (variant.streamType == StreamType.AUDIO_AND_VIDEO || variant.streamType == StreamType.VIDEO || variant.streamType == StreamType.DYNAMIC)
return true
}
return false
}
fun selectStream( fun selectStream(
item: String, item: String,
playMode: PlayMode, playMode: PlayMode,
@ -123,6 +131,8 @@ object StreamSelect {
preferredLanguage: List<String> preferredLanguage: List<String>
): StreamSelection { ): StreamSelection {
// filter for best fitting language stream variants
val bestFittingLanguage = getBestLanguageFit(availableStreamVariants, preferredLanguage) val bestFittingLanguage = getBestLanguageFit(availableStreamVariants, preferredLanguage)
val availableVariantsInPreferredLanguage = val availableVariantsInPreferredLanguage =
if (bestFittingLanguage != null) filterVariantsByLanguage( if (bestFittingLanguage != null) filterVariantsByLanguage(
@ -133,10 +143,11 @@ object StreamSelect {
emptyList() emptyList()
} }
if (playMode == PlayMode.FULLSCREEN_VIDEO
|| playMode == PlayMode.EMBEDDED_VIDEO // is it a video stream or a pure audio stream?
|| playMode == PlayMode.PIP if (hasVideoStreamVariants(availableStreamVariants)) {
) {
// first: try and get a dynamic stream variant
getDynamicStream(availableVariantsInPreferredLanguage) getDynamicStream(availableVariantsInPreferredLanguage)
?: getDynamicStream( ?: getDynamicStream(
availableStreamVariants availableStreamVariants
@ -144,7 +155,9 @@ object StreamSelect {
return SingleSelection(it) return SingleSelection(it)
} }
val bestIdentifier = // second: try and get seperate audio and video stream variants
val bestVideoIdentifier =
getBestFittingVideoIdentifier( getBestFittingVideoIdentifier(
availableVariantsInPreferredLanguage, availableVariantsInPreferredLanguage,
preferredVideoIdentifier preferredVideoIdentifier
@ -164,10 +177,10 @@ object StreamSelect {
val videoOnlyStream = val videoOnlyStream =
getVideoOnlyVariantWithMatchingIdentifier( getVideoOnlyVariantWithMatchingIdentifier(
availableVariantsInPreferredLanguage, availableVariantsInPreferredLanguage,
bestIdentifier bestVideoIdentifier
) ?: getVideoOnlyVariantWithMatchingIdentifier( ) ?: getVideoOnlyVariantWithMatchingIdentifier(
availableStreamVariants, availableStreamVariants,
bestIdentifier bestVideoIdentifier
) )
if (videoOnlyStream != null) { if (videoOnlyStream != null) {
@ -180,20 +193,27 @@ object StreamSelect {
} }
} }
getFirstVariantMatchingIdentifier(availableVariantsInPreferredLanguage, bestIdentifier) // fourth: try to get a video and audio stream variant with the best fitting identifier
?: getFirstVariantMatchingIdentifier(availableStreamVariants, bestIdentifier)?.let {
getFirstVariantMatchingIdentifier(
availableVariantsInPreferredLanguage,
bestVideoIdentifier
)
?: getFirstVariantMatchingIdentifier(
availableStreamVariants,
bestVideoIdentifier
)?.let {
return SingleSelection(it) return SingleSelection(it)
} }
// fifth: try and get the median video and audio stream variant
return SingleSelection(run { return SingleSelection(run {
val videoVariants = val videoVariants =
getNonDynamicVideoVariants(availableVariantsInPreferredLanguage).let { getNonDynamicVideoVariants(availableVariantsInPreferredLanguage).ifEmpty {
if (it.isNotEmpty()) { getNonDynamicVideoVariants(availableStreamVariants)
it
} else {
getNonDynamicVideoVariants(availableStreamVariants)
}
} }
if (videoVariants.isNotEmpty()) { if (videoVariants.isNotEmpty()) {
return@run videoVariants[videoVariants.size / 2] return@run videoVariants[videoVariants.size / 2]
} else { } else {
@ -201,7 +221,10 @@ object StreamSelect {
} }
}) })
} else { } else { /* if(hasVideoStreamVariants(availableStreamVariants)) */
// first: try to get an audio stream variant with the best fitting identifier
getBestFittingAudioVariant( getBestFittingAudioVariant(
availableVariantsInPreferredLanguage, availableVariantsInPreferredLanguage,
preferredAudioIdentifier preferredAudioIdentifier
@ -213,10 +236,12 @@ object StreamSelect {
return SingleSelection(it) return SingleSelection(it)
} }
// second: try and get the median audio stream variant
return SingleSelection(run { return SingleSelection(run {
val audioVariants = val audioVariants =
getNonDynamicAudioVariants(availableVariantsInPreferredLanguage).let{ getNonDynamicAudioVariants(availableVariantsInPreferredLanguage).let {
if(it.isNotEmpty()) { if (it.isNotEmpty()) {
it it
} else { } else {
getNonDynamicAudioVariants(availableStreamVariants) getNonDynamicAudioVariants(availableStreamVariants)
@ -229,7 +254,5 @@ object StreamSelect {
} }
}) })
} }
} }
} }