From e403bd379a3d306c38bfe46206cba3caa6949f7e Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 16 Nov 2022 18:14:47 +0100 Subject: [PATCH] reimplement quality selection and pref --- .../github/libretube/obj/VideoResolution.kt | 7 ++ .../libretube/ui/fragments/PlayerFragment.kt | 84 +++++++++++-------- 2 files changed, 54 insertions(+), 37 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/obj/VideoResolution.kt diff --git a/app/src/main/java/com/github/libretube/obj/VideoResolution.kt b/app/src/main/java/com/github/libretube/obj/VideoResolution.kt new file mode 100644 index 000000000..b234cfda2 --- /dev/null +++ b/app/src/main/java/com/github/libretube/obj/VideoResolution.kt @@ -0,0 +1,7 @@ +package com.github.libretube.obj + +data class VideoResolution( + val name: String, + val resolution: Int? = null, + val adaptiveSourceUrl: String? = null +) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 92b018eea..93e13620f 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -60,6 +60,7 @@ import com.github.libretube.extensions.query import com.github.libretube.extensions.toID import com.github.libretube.extensions.toStreamItem import com.github.libretube.obj.ShareData +import com.github.libretube.obj.VideoResolution import com.github.libretube.services.BackgroundMode import com.github.libretube.services.DownloadService import com.github.libretube.ui.activities.MainActivity @@ -166,9 +167,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { private lateinit var shareData: ShareData - private var selectedAudioSourceUrl: String? = null - private var selectedVideoSourceUrl: String? = null - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) arguments?.let { @@ -1081,16 +1079,19 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { exoPlayer.setMediaItem(mediaItem) } - private fun getAvailableResolutions(): Pair, Array> { - if (!this::streams.isInitialized) return Pair(arrayOf(), arrayOf()) + private fun getAvailableResolutions(): List { + if (!this::streams.isInitialized) return listOf() - var videosNameArray: Array = arrayOf() - var videosUrlArray: Array = arrayOf() + val resolutions = mutableListOf() // append hls to list if available if (streams.hls != null) { - videosNameArray += getString(R.string.hls) - videosUrlArray += streams.hls!! + resolutions.add( + VideoResolution( + name = getString(R.string.hls), + adaptiveSourceUrl = streams.hls!! + ) + ) } val videoStreams = try { @@ -1112,20 +1113,26 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { // append quality to list if it has the preferred format (e.g. MPEG) val preferredMimeType = "video/${PlayerHelper.videoFormatPreference}" if (vid.url != null && vid.mimeType == preferredMimeType) { // preferred format - videosNameArray += vid.quality.toString() - videosUrlArray += vid.url!! + resolutions.add( + 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 - videosNameArray += "LBRY MP4" - videosUrlArray += vid.url!! + resolutions.add( + VideoResolution( + name = "LBRY MP4", + adaptiveSourceUrl = vid.url, + resolution = Int.MAX_VALUE + ) + ) } } - return Pair(videosNameArray, videosUrlArray) + return resolutions } private fun setResolutionAndSubtitles() { - // get the available resolutions - val (videosNameArray, videosUrlArray) = getAvailableResolutions() - // create a list of subtitles subtitles = mutableListOf() val subtitlesNamesList = mutableListOf(context?.getString(R.string.none)!!) @@ -1159,12 +1166,13 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { ) } - private fun setStreamSource( - streams: Streams - ) { + private fun setStreamSource(streams: Streams) { val defaultResolution = PlayerHelper.getDefaultResolution(requireContext()) 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) @@ -1176,15 +1184,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { this.setMediaSource(mediaItem.toUri(), MimeTypes.APPLICATION_MPD) } - private fun getAudioSource(audioStreams: List?): 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() { val cronetEngine: CronetEngine = CronetHelper.getCronetEngine() val cronetDataSourceFactory: CronetDataSource.Factory = @@ -1216,6 +1215,11 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { // control for the track sources like subtitles and audio source trackSelector = DefaultTrackSelector(requireContext()) + val params = trackSelector.buildUponParameters().setPreferredAudioLanguage( + Locale.getDefault().language.lowercase().substring(0, 2) + ) + trackSelector.setParameters(params) + // limit hls to full hd if ( PreferenceHelper.getBoolean( @@ -1348,22 +1352,25 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { override fun onQualityClicked() { // get the available resolutions - val (videosNameArray, videosUrlArray) = getAvailableResolutions() + val resolutions = getAvailableResolutions() // Dialog for quality selection val lastPosition = exoPlayer.currentPosition BaseBottomSheet() .setSimpleItems( - videosNameArray.toList() + resolutions.map { it.name } ) { which -> if ( - videosNameArray[which] == getString(R.string.hls) || - videosNameArray[which] == "LBRY HLS" + resolutions[which].adaptiveSourceUrl != null ) { // set the progressive media source - setMediaSource(videosUrlArray[which].toUri(), MimeTypes.APPLICATION_M3U8) + setMediaSource(resolutions[which].adaptiveSourceUrl!!.toUri(), MimeTypes.APPLICATION_M3U8) } 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) } @@ -1382,7 +1389,10 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { BaseBottomSheet() .setSimpleItems(audioLanguages) { 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) }