From 74651b761709a5295f65ed18a96feb01bdb75953 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 16 Nov 2022 18:42:57 +0100 Subject: [PATCH] option to force hls --- .../libretube/constants/PreferenceKeys.kt | 1 + .../libretube/ui/fragments/PlayerFragment.kt | 57 +++++++++---------- app/src/main/res/values/array.xml | 2 +- app/src/main/res/values/strings.xml | 5 +- app/src/main/res/xml/audio_video_settings.xml | 10 ++++ 5 files changed, 44 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt index 9f0f69781..a46100d37 100644 --- a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt +++ b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt @@ -81,6 +81,7 @@ object PreferenceKeys { const val LIMIT_HLS = "limit_hls" const val PROGRESSIVE_LOADING_INTERVAL_SIZE = "progressive_loading_interval" const val ALTERNATIVE_PLAYER_LAYOUT = "alternative_player_layout" + const val USE_HLS_OVER_DASH = "use_hls" /** * Background mode 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 93e13620f..89cd89da5 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 @@ -1084,16 +1084,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { val resolutions = mutableListOf() - // append hls to list if available - if (streams.hls != null) { - resolutions.add( - VideoResolution( - name = getString(R.string.hls), - adaptiveSourceUrl = streams.hls!! - ) - ) - } - val videoStreams = try { // attempt to sort the qualities, catch if there was an error ih parsing streams.videoStreams?.sortedBy { @@ -1129,6 +1119,15 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { ) } } + + if (resolutions.isEmpty()) { + return listOf( + VideoResolution(getString(R.string.hls), adaptiveSourceUrl = streams.hls) + ) + } + + resolutions.add(0, VideoResolution(getString(R.string.auto_quality), Int.MAX_VALUE)) + return resolutions } @@ -1175,13 +1174,21 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { trackSelector.setParameters(params) } - val manifest = DashHelper.createManifest(streams) + if (!PreferenceHelper.getBoolean(PreferenceKeys.USE_HLS_OVER_DASH, false) && + streams.videoStreams.orEmpty().isNotEmpty() + ) { + val manifest = DashHelper.createManifest(streams) - // encode to base64 - val encoded = Base64.encodeToString(manifest.toByteArray(), Base64.DEFAULT) - val mediaItem = "data:application/dash+xml;charset=utf-8;base64,$encoded" + // encode to base64 + val encoded = Base64.encodeToString(manifest.toByteArray(), Base64.DEFAULT) + val mediaItem = "data:application/dash+xml;charset=utf-8;base64,$encoded" - this.setMediaSource(mediaItem.toUri(), MimeTypes.APPLICATION_MPD) + this.setMediaSource(mediaItem.toUri(), MimeTypes.APPLICATION_MPD) + } else if (streams.hls != null) { + setMediaSource(streams.hls.toUri(), MimeTypes.APPLICATION_M3U8) + } else { + Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show() + } } private fun createExoPlayer() { @@ -1355,24 +1362,16 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { val resolutions = getAvailableResolutions() // Dialog for quality selection - val lastPosition = exoPlayer.currentPosition BaseBottomSheet() .setSimpleItems( resolutions.map { it.name } ) { which -> - if ( - resolutions[which].adaptiveSourceUrl != null - ) { - // set the progressive media source - setMediaSource(resolutions[which].adaptiveSourceUrl!!.toUri(), MimeTypes.APPLICATION_M3U8) - } else { - 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) + val resolution = resolutions[which].resolution!! + + val params = trackSelector.buildUponParameters() + .setMaxVideoSize(Int.MAX_VALUE, resolution) + .setMinVideoSize(Int.MAX_VALUE, resolution) + trackSelector.setParameters(params) } .show(childFragmentManager) } diff --git a/app/src/main/res/values/array.xml b/app/src/main/res/values/array.xml index f48a87cb5..d48ec1b73 100644 --- a/app/src/main/res/values/array.xml +++ b/app/src/main/res/values/array.xml @@ -188,7 +188,7 @@ - @string/hls + @string/auto_quality 2160p 1440p 1080p diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 04d59f718..62b315ad8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -208,7 +208,7 @@ Authentication instance Use a different instance for authenticated calls. Choose an auth instance - Auto + HLS GitHub Audio and video Fullscreen orientation @@ -372,6 +372,9 @@ Show the related videos as a row above the comments instead of below. Audio track Default + Use HLS + Use HLS instead of DASH (will be slower, not recommended) + Auto Download Service diff --git a/app/src/main/res/xml/audio_video_settings.xml b/app/src/main/res/xml/audio_video_settings.xml index 9d140d99b..fdeb60a2c 100644 --- a/app/src/main/res/xml/audio_video_settings.xml +++ b/app/src/main/res/xml/audio_video_settings.xml @@ -46,6 +46,7 @@ + @@ -62,10 +64,18 @@ app:defaultValue="all" app:entries="@array/playerAudioFormat" app:entryValues="@array/playerAudioFormatValues" + app:isPreferenceVisible="false" app:key="player_audio_format" app:title="@string/playerAudioFormat" app:useSimpleSummaryProvider="true" /> + +