reimplement quality selection and pref

This commit is contained in:
Bnyro 2022-11-16 18:14:47 +01:00
parent 3e5681bae3
commit e403bd379a
2 changed files with 54 additions and 37 deletions

View File

@ -0,0 +1,7 @@
package com.github.libretube.obj
data class VideoResolution(
val name: String,
val resolution: Int? = null,
val adaptiveSourceUrl: String? = null
)

View File

@ -60,6 +60,7 @@ import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toStreamItem import com.github.libretube.extensions.toStreamItem
import com.github.libretube.obj.ShareData import com.github.libretube.obj.ShareData
import com.github.libretube.obj.VideoResolution
import com.github.libretube.services.BackgroundMode import com.github.libretube.services.BackgroundMode
import com.github.libretube.services.DownloadService import com.github.libretube.services.DownloadService
import com.github.libretube.ui.activities.MainActivity import com.github.libretube.ui.activities.MainActivity
@ -166,9 +167,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
private lateinit var shareData: ShareData private lateinit var shareData: ShareData
private var selectedAudioSourceUrl: String? = null
private var selectedVideoSourceUrl: String? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
arguments?.let { arguments?.let {
@ -1081,16 +1079,19 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
exoPlayer.setMediaItem(mediaItem) exoPlayer.setMediaItem(mediaItem)
} }
private fun getAvailableResolutions(): Pair<Array<String>, Array<String>> { private fun getAvailableResolutions(): List<VideoResolution> {
if (!this::streams.isInitialized) return Pair(arrayOf(), arrayOf()) if (!this::streams.isInitialized) return listOf()
var videosNameArray: Array<String> = arrayOf() val resolutions = mutableListOf<VideoResolution>()
var videosUrlArray: Array<String> = arrayOf()
// append hls to list if available // append hls to list if available
if (streams.hls != null) { if (streams.hls != null) {
videosNameArray += getString(R.string.hls) resolutions.add(
videosUrlArray += streams.hls!! VideoResolution(
name = getString(R.string.hls),
adaptiveSourceUrl = streams.hls!!
)
)
} }
val videoStreams = try { val videoStreams = try {
@ -1112,20 +1113,26 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
// append quality to list if it has the preferred format (e.g. MPEG) // append quality to list if it has the preferred format (e.g. MPEG)
val preferredMimeType = "video/${PlayerHelper.videoFormatPreference}" val preferredMimeType = "video/${PlayerHelper.videoFormatPreference}"
if (vid.url != null && vid.mimeType == preferredMimeType) { // preferred format if (vid.url != null && vid.mimeType == preferredMimeType) { // preferred format
videosNameArray += vid.quality.toString() resolutions.add(
videosUrlArray += vid.url!! VideoResolution(
name = vid.quality!!,
resolution = vid.quality.toString().split("p").first().toInt()
)
)
} else if (vid.quality.equals("LBRY") && vid.format.equals("MP4")) { // LBRY MP4 format } else if (vid.quality.equals("LBRY") && vid.format.equals("MP4")) { // LBRY MP4 format
videosNameArray += "LBRY MP4" resolutions.add(
videosUrlArray += vid.url!! VideoResolution(
name = "LBRY MP4",
adaptiveSourceUrl = vid.url,
resolution = Int.MAX_VALUE
)
)
} }
} }
return Pair(videosNameArray, videosUrlArray) return resolutions
} }
private fun setResolutionAndSubtitles() { private fun setResolutionAndSubtitles() {
// get the available resolutions
val (videosNameArray, videosUrlArray) = getAvailableResolutions()
// create a list of subtitles // create a list of subtitles
subtitles = mutableListOf() subtitles = mutableListOf()
val subtitlesNamesList = mutableListOf(context?.getString(R.string.none)!!) val subtitlesNamesList = mutableListOf(context?.getString(R.string.none)!!)
@ -1159,12 +1166,13 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
) )
} }
private fun setStreamSource( private fun setStreamSource(streams: Streams) {
streams: Streams
) {
val defaultResolution = PlayerHelper.getDefaultResolution(requireContext()) val defaultResolution = PlayerHelper.getDefaultResolution(requireContext())
if (defaultResolution != "") { if (defaultResolution != "") {
// TODO: Fix this, we need to set it from the player! val params = trackSelector.buildUponParameters()
.setMaxVideoSize(Int.MAX_VALUE, defaultResolution.toInt())
.setMinVideoSize(Int.MAX_VALUE, defaultResolution.toInt())
trackSelector.setParameters(params)
} }
val manifest = DashHelper.createManifest(streams) val manifest = DashHelper.createManifest(streams)
@ -1176,15 +1184,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
this.setMediaSource(mediaItem.toUri(), MimeTypes.APPLICATION_MPD) this.setMediaSource(mediaItem.toUri(), MimeTypes.APPLICATION_MPD)
} }
private fun getAudioSource(audioStreams: List<PipedStream>?): String {
val appLanguage = Locale.getDefault().language.lowercase().substring(0, 2)
val filteredStreams = audioStreams.orEmpty().filter { it.audioTrackId?.contains(appLanguage) ?: false }
return PlayerHelper.getAudioSource(
requireContext(),
filteredStreams.ifEmpty { audioStreams!! }
)
}
private fun createExoPlayer() { private fun createExoPlayer() {
val cronetEngine: CronetEngine = CronetHelper.getCronetEngine() val cronetEngine: CronetEngine = CronetHelper.getCronetEngine()
val cronetDataSourceFactory: CronetDataSource.Factory = val cronetDataSourceFactory: CronetDataSource.Factory =
@ -1216,6 +1215,11 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
// control for the track sources like subtitles and audio source // control for the track sources like subtitles and audio source
trackSelector = DefaultTrackSelector(requireContext()) trackSelector = DefaultTrackSelector(requireContext())
val params = trackSelector.buildUponParameters().setPreferredAudioLanguage(
Locale.getDefault().language.lowercase().substring(0, 2)
)
trackSelector.setParameters(params)
// limit hls to full hd // limit hls to full hd
if ( if (
PreferenceHelper.getBoolean( PreferenceHelper.getBoolean(
@ -1348,22 +1352,25 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
override fun onQualityClicked() { override fun onQualityClicked() {
// get the available resolutions // get the available resolutions
val (videosNameArray, videosUrlArray) = getAvailableResolutions() val resolutions = getAvailableResolutions()
// Dialog for quality selection // Dialog for quality selection
val lastPosition = exoPlayer.currentPosition val lastPosition = exoPlayer.currentPosition
BaseBottomSheet() BaseBottomSheet()
.setSimpleItems( .setSimpleItems(
videosNameArray.toList() resolutions.map { it.name }
) { which -> ) { which ->
if ( if (
videosNameArray[which] == getString(R.string.hls) || resolutions[which].adaptiveSourceUrl != null
videosNameArray[which] == "LBRY HLS"
) { ) {
// set the progressive media source // set the progressive media source
setMediaSource(videosUrlArray[which].toUri(), MimeTypes.APPLICATION_M3U8) setMediaSource(resolutions[which].adaptiveSourceUrl!!.toUri(), MimeTypes.APPLICATION_M3U8)
} else { } else {
// TODO: Fix this val resolution = resolutions[which].resolution!!
val params = trackSelector.buildUponParameters()
.setMaxVideoSize(Int.MAX_VALUE, resolution)
.setMinVideoSize(Int.MAX_VALUE, resolution)
trackSelector.setParameters(params)
} }
exoPlayer.seekTo(lastPosition) exoPlayer.seekTo(lastPosition)
} }
@ -1382,7 +1389,10 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
BaseBottomSheet() BaseBottomSheet()
.setSimpleItems(audioLanguages) { index -> .setSimpleItems(audioLanguages) { index ->
val audioStreams = audioGroups.values.elementAt(index) val audioStreams = audioGroups.values.elementAt(index)
// TODO: Fix this val lang = audioStreams.first().audioTrackId!!.substring(0, 2)
val newParams = trackSelector.buildUponParameters()
.setPreferredAudioLanguage(lang)
trackSelector.setParameters(newParams)
} }
.show(childFragmentManager) .show(childFragmentManager)
} }