Skip to content

Commit

Permalink
rename TrackSelector to StreamSelector and introduce TrackUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
theScrabi committed Oct 15, 2024
1 parent 1f101d8 commit a7bb30d
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 181 deletions.
10 changes: 5 additions & 5 deletions new-player/src/main/java/net/newpipe/newplayer/NewPlayerImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import net.newpipe.newplayer.utils.StreamExceptionResponse
import net.newpipe.newplayer.utils.StreamSelection
import net.newpipe.newplayer.utils.StreamSelectionResponse
import net.newpipe.newplayer.utils.StreamTrack
import net.newpipe.newplayer.utils.TrackSelector
import net.newpipe.newplayer.utils.StreamSelector
import kotlin.random.Random

private const val TAG = "NewPlayerImpl"
Expand Down Expand Up @@ -207,7 +207,7 @@ class NewPlayerImpl(
uniqueIdToStreamSelectionLookup[mediaItem.mediaId.toLong()]!!
launchJobAndCollectError {
mutableCurrentlyAvailableTracks.update {
TrackSelector.getNonDynamicTracksNonDuplicated(repository.getStreams(streamSelection.item))
StreamSelector.getNonDynamicTracksNonDuplicated(repository.getStreams(streamSelection.item))
}
}
} else {
Expand Down Expand Up @@ -344,7 +344,7 @@ class NewPlayerImpl(
override fun playStream(item: String, playMode: PlayMode) {
launchJobAndCollectError {
mutableCurrentlyAvailableTracks.update {
TrackSelector.getNonDynamicTracksNonDuplicated(repository.getStreams(item))
StreamSelector.getNonDynamicTracksNonDuplicated(repository.getStreams(item))
}

val mediaSource = toMediaSource(item)
Expand Down Expand Up @@ -416,11 +416,11 @@ class NewPlayerImpl(
@OptIn(UnstableApi::class)
private suspend
fun toMediaSource(item: String): MediaSource {
val trackSelector = TrackSelector(
val streamSelector = StreamSelector(
preferredLanguages = preferredStreamLanguages
)

val selection = trackSelector.selectStreamAutomatically(
val selection = streamSelector.selectStreamAutomatically(
item,
availableStreams = repository.getStreams(item),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import net.newpipe.newplayer.utils.NewPlayerException
import net.newpipe.newplayer.PlayMode
import net.newpipe.newplayer.RepeatMode
import net.newpipe.newplayer.ui.ContentScale
import net.newpipe.newplayer.utils.TrackSelector
import java.util.LinkedList
import kotlin.math.abs

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* NewPlayer
*
* @author Christian Schabesberger
*
* Copyright (C) NewPipe e.V. 2024 <code(at)newpipe-ev.de>
*
* NewPlayer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NewPlayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NewPlayer. If not, see <http://www.gnu.org/licenses/>.
*/

package net.newpipe.newplayer.utils

import net.newpipe.newplayer.utils.TrackUtils.getDynamicStreams
import net.newpipe.newplayer.utils.TrackUtils.hasVideoStreams
import net.newpipe.newplayer.utils.TrackUtils.tryAndGetMedianAudioOnlyStream
import net.newpipe.newplayer.utils.TrackUtils.tryAndGetMedianCombinedVideoAndAudioStream
import net.newpipe.newplayer.utils.TrackUtils.tryAndGetMedianVideoOnlyStream

internal class StreamSelector(
val preferredLanguages: List<LanguageIdentifier>,
) {
internal fun selectStreamAutomatically(
item: String,
availableStreams: List<Stream>,
): StreamSelection {


// filter for best fitting language stream variants

val bestFittingLanguage =
TrackUtils.getBestLanguageFit(availableStreams, preferredLanguages)
val availablesInPreferredLanguage =
if (bestFittingLanguage != null) TrackUtils.filtersByLanguage(
availableStreams, bestFittingLanguage
)
else {
emptyList()
}


// is it a video stream or a pure audio stream?
if (hasVideoStreams(availableStreams)) {

// first: try and get a dynamic stream variant
val dynamicStreams = getDynamicStreams(availablesInPreferredLanguage)
if (dynamicStreams.isNotEmpty()) {
return SingleSelection(dynamicStreams[0])
}

// second: try and get separate audio and video stream variants

val videoOnlyStream = tryAndGetMedianVideoOnlyStream(availableStreams)


if (videoOnlyStream != null) {

val audioStream = tryAndGetMedianAudioOnlyStream(availableStreams)

if (videoOnlyStream != null && audioStream != null) {
return MultiSelection(listOf(videoOnlyStream, audioStream))
}
} /* if (vdeioOnlyStream != null) */

// fourth: try to get a video and audio stream variant with the best fitting identifier

tryAndGetMedianCombinedVideoAndAudioStream(availableStreams)?.let {
return SingleSelection(it)
}

} else { /* if(!hasVideoStreams(availableStreams)) */

// first: try to get an audio stream variant with the best fitting identifier

tryAndGetMedianAudioOnlyStream(availableStreams)?.let {
return SingleSelection(it)
}
}

throw NewPlayerException("StreamSelector: No suitable Stream found that.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ data class VideoStreamTrack(
override val fileFormat: String,
) : StreamTrack {

override fun toShortIdentifierString() = "${width}p${if (frameRate != 30) frameRate else ""}"
override fun toShortIdentifierString() =
"${if (width < height) width else height}p${if (frameRate != 30) frameRate else ""}"

override fun toLongIdentifierString() = "$fileFormat ${toShortIdentifierString()}"

Expand Down Expand Up @@ -68,15 +69,15 @@ data class AudioStreamTrack(
val bitrate: Int,
override val fileFormat: String,
val language: LanguageIdentifier? = null
) : StreamTrack{
) : StreamTrack {

override fun toShortIdentifierString() =
if (bitrate < 1000) "${bitrate}bps" else "${bitrate / 1000}kbps"

override fun toLongIdentifierString() = "$fileFormat ${toShortIdentifierString()}"

override fun compareTo(other: StreamTrack) =
if(other is AudioStreamTrack) {
if (other is AudioStreamTrack) {
bitrate - other.bitrate
} else {
-1
Expand Down
172 changes: 0 additions & 172 deletions new-player/src/main/java/net/newpipe/newplayer/utils/TrackSelector.kt

This file was deleted.

Loading

0 comments on commit a7bb30d

Please sign in to comment.