From 14cf79559735fdfd4251b094d93007ebf597bdd1 Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Thu, 17 Oct 2024 18:54:38 +0200 Subject: [PATCH] add scaffold for ConstraintStreamSelector --- .../net/newpipe/newplayer/NewPlayerImpl.kt | 6 ++++-- .../newplayer/logic/AutoStreamSelector.kt | 18 +++++++++++++++--- .../logic/ConstraintStreamSelector.kt | 16 ++++++++++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 new-player/src/main/java/net/newpipe/newplayer/logic/ConstraintStreamSelector.kt diff --git a/new-player/src/main/java/net/newpipe/newplayer/NewPlayerImpl.kt b/new-player/src/main/java/net/newpipe/newplayer/NewPlayerImpl.kt index 8607b405..0ddbd038 100644 --- a/new-player/src/main/java/net/newpipe/newplayer/NewPlayerImpl.kt +++ b/new-player/src/main/java/net/newpipe/newplayer/NewPlayerImpl.kt @@ -207,6 +207,7 @@ class NewPlayerImpl( } override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) { + currentStreamLanguageConstraint = null super.onMediaItemTransition(mediaItem, reason) mutableCurrentlyPlaying.update { mediaItem } if (mediaItem != null) { @@ -418,6 +419,7 @@ class NewPlayerImpl( @OptIn(UnstableApi::class) private fun internalPlayStream(mediaSource: MediaSource, playMode: PlayMode) { + currentStreamLanguageConstraint = null if (exoPlayer.value?.playbackState == Player.STATE_IDLE || exoPlayer.value == null) { prepare() } @@ -469,11 +471,11 @@ class NewPlayerImpl( private suspend fun toMediaSource(item: String): MediaSource { val autoStreamSelector = AutoStreamSelector( - preferredLanguages = preferredStreamLanguages + preferredLanguages = preferredStreamLanguages, + streamLanguageConstraint = currentStreamLanguageConstraint ) val selection = autoStreamSelector.selectStreamAutomatically( - item, availableStreams = repository.getStreams(item), ) return toMediaSource(selection, item) diff --git a/new-player/src/main/java/net/newpipe/newplayer/logic/AutoStreamSelector.kt b/new-player/src/main/java/net/newpipe/newplayer/logic/AutoStreamSelector.kt index f4b31148..816604af 100644 --- a/new-player/src/main/java/net/newpipe/newplayer/logic/AutoStreamSelector.kt +++ b/new-player/src/main/java/net/newpipe/newplayer/logic/AutoStreamSelector.kt @@ -31,14 +31,25 @@ import net.newpipe.newplayer.logic.TrackUtils.tryAndGetMedianAudioOnlyTracks import net.newpipe.newplayer.logic.TrackUtils.tryAndGetMedianCombinedVideoAndAudioTracks import net.newpipe.newplayer.logic.TrackUtils.tryAndGetMedianVideoOnlyTracks +/** + * TODO: This whole class is just a concept. this should really be finished at some point. + */ internal class AutoStreamSelector( /** * Must be in IETF-BCP-47 format + * This is a soft constraint. If no language can be found in the available tracks + * that fits on of the preferred languages, a default language is picked. */ val preferredLanguages: List, + + /** + * This is a hard constraint. If not null a stream with this language must be selected, + * otherwise the automatic stream selection fails. + * Setting this will override the [preferredLanguages]. + */ + val streamLanguageConstraint: String? ) { internal fun selectStreamAutomatically( - item: String, availableStreams: List, ): StreamSelection { @@ -47,7 +58,8 @@ internal class AutoStreamSelector( val bestFittingLanguage = TrackUtils.getBestLanguageFit(availableStreams, preferredLanguages) - val availablesInPreferredLanguage = + + val availableInPreferredLanguage = if (bestFittingLanguage != null) TrackUtils.filtersByLanguage( availableStreams, bestFittingLanguage ) @@ -60,7 +72,7 @@ internal class AutoStreamSelector( if (hasVideoTracks(availableStreams)) { // first: try and get a dynamic stream variant - val dynamicStreams = getDynamicStreams(availablesInPreferredLanguage) + val dynamicStreams = getDynamicStreams(availableInPreferredLanguage) if (dynamicStreams.isNotEmpty()) { return SingleSelection(dynamicStreams[0]) } diff --git a/new-player/src/main/java/net/newpipe/newplayer/logic/ConstraintStreamSelector.kt b/new-player/src/main/java/net/newpipe/newplayer/logic/ConstraintStreamSelector.kt new file mode 100644 index 00000000..85f5eef9 --- /dev/null +++ b/new-player/src/main/java/net/newpipe/newplayer/logic/ConstraintStreamSelector.kt @@ -0,0 +1,16 @@ +package net.newpipe.newplayer.logic + +import net.newpipe.newplayer.data.Stream +import net.newpipe.newplayer.data.StreamSelection +import net.newpipe.newplayer.data.StreamTrack + +internal object ConstraintStreamSelector { + fun selectStream( + availableStreams: List, + currentStreamSelection: StreamSelection, + currentlyPlayingTracks: List + ): StreamSelection { + + return currentStreamSelection + } +} \ No newline at end of file