Skip to content

Commit

Permalink
add scaffold for ConstraintStreamSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
theScrabi committed Oct 17, 2024
1 parent fa0f162 commit 14cf795
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

/**
* 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<Stream>,
): StreamSelection {

Expand All @@ -47,7 +58,8 @@ internal class AutoStreamSelector(

val bestFittingLanguage =
TrackUtils.getBestLanguageFit(availableStreams, preferredLanguages)
val availablesInPreferredLanguage =

val availableInPreferredLanguage =
if (bestFittingLanguage != null) TrackUtils.filtersByLanguage(
availableStreams, bestFittingLanguage
)
Expand All @@ -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])
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Stream>,
currentStreamSelection: StreamSelection,
currentlyPlayingTracks: List<StreamTrack>
): StreamSelection {

return currentStreamSelection
}
}

0 comments on commit 14cf795

Please sign in to comment.