From 6f2087b3aa092153884d69e118410e60a9391446 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 8 Sep 2023 16:42:35 +0200 Subject: [PATCH 1/2] refactor: disable video tracks in audio only player --- .../github/libretube/helpers/DashHelper.kt | 49 +++++++++---------- .../github/libretube/helpers/PlayerHelper.kt | 2 - .../services/OfflinePlayerService.kt | 9 ++++ .../libretube/services/OnlinePlayerService.kt | 6 ++- 4 files changed, 37 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/github/libretube/helpers/DashHelper.kt b/app/src/main/java/com/github/libretube/helpers/DashHelper.kt index 2abae50a9..5c2812bc6 100644 --- a/app/src/main/java/com/github/libretube/helpers/DashHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/DashHelper.kt @@ -2,13 +2,13 @@ package com.github.libretube.helpers import com.github.libretube.api.obj.PipedStream import com.github.libretube.api.obj.Streams +import org.w3c.dom.Document +import org.w3c.dom.Element import java.io.StringWriter import javax.xml.parsers.DocumentBuilderFactory import javax.xml.transform.TransformerFactory import javax.xml.transform.dom.DOMSource import javax.xml.transform.stream.StreamResult -import org.w3c.dom.Document -import org.w3c.dom.Element // Based off of https://github.com/TeamPiped/Piped/blob/master/src/utils/DashUtils.js @@ -28,7 +28,6 @@ object DashHelper { fun createManifest( streams: Streams, supportsHdr: Boolean, - audioOnly: Boolean = false, rewriteUrls: Boolean ): String { val builder = builderFactory.newDocumentBuilder() @@ -45,30 +44,28 @@ object DashHelper { val adapSetInfos = ArrayList() - if (!audioOnly) { - for ( - stream in streams.videoStreams - // used to avoid including LBRY HLS inside the streams in the manifest - .filter { !it.format.orEmpty().contains("HLS") } - .filter { supportsHdr || !it.quality.orEmpty().uppercase().contains("HDR") } - ) { - // ignore dual format and OTF streams - if (!stream.videoOnly!! || stream.indexEnd!! <= 0) { - continue - } - - val adapSetInfo = adapSetInfos.find { it.mimeType == stream.mimeType } - if (adapSetInfo != null) { - adapSetInfo.formats.add(stream) - continue - } - adapSetInfos.add( - AdapSetInfo( - stream.mimeType!!, - mutableListOf(stream) - ) - ) + for ( + stream in streams.videoStreams + // used to avoid including LBRY HLS inside the streams in the manifest + .filter { !it.format.orEmpty().contains("HLS") } + .filter { supportsHdr || !it.quality.orEmpty().uppercase().contains("HDR") } + ) { + // ignore dual format and OTF streams + if (!stream.videoOnly!! || stream.indexEnd!! <= 0) { + continue } + + val adapSetInfo = adapSetInfos.find { it.mimeType == stream.mimeType } + if (adapSetInfo != null) { + adapSetInfo.formats.add(stream) + continue + } + adapSetInfos.add( + AdapSetInfo( + stream.mimeType!!, + mutableListOf(stream) + ) + ) } for (stream in streams.audioStreams) { diff --git a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt index fa1387efc..15b0ab91e 100644 --- a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt @@ -51,13 +51,11 @@ object PlayerHelper { fun createDashSource( streams: Streams, context: Context, - audioOnly: Boolean = false, disableProxy: Boolean ): Uri { val manifest = DashHelper.createManifest( streams, DisplayHelper.supportsHdr(context), - audioOnly, disableProxy ) diff --git a/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt b/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt index 84ee5d795..17fcb390e 100644 --- a/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt @@ -6,8 +6,10 @@ import androidx.core.app.NotificationCompat import androidx.core.app.ServiceCompat import androidx.lifecycle.LifecycleService import androidx.lifecycle.lifecycleScope +import androidx.media3.common.C import androidx.media3.common.MediaItem import androidx.media3.exoplayer.ExoPlayer +import androidx.media3.exoplayer.trackselection.DefaultTrackSelector import com.github.libretube.R import com.github.libretube.constants.IntentData import com.github.libretube.constants.PLAYER_CHANNEL_ID @@ -16,6 +18,7 @@ import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.obj.DownloadWithItems import com.github.libretube.enums.FileType import com.github.libretube.extensions.toAndroidUri +import com.github.libretube.extensions.updateParameters import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.PlayerHelper.loadPlaybackParams import com.github.libretube.obj.PlayerNotificationData @@ -78,10 +81,16 @@ class OfflinePlayerService : LifecycleService() { */ @androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class) private fun startAudioPlayer(downloadWithItem: DownloadWithItems): Boolean { + val trackSelector = DefaultTrackSelector(this) + trackSelector.updateParameters { + setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, true) + } + player = ExoPlayer.Builder(this) .setUsePlatformDiagnostics(false) .setHandleAudioBecomingNoisy(true) .setAudioAttributes(PlayerHelper.getAudioAttributes(), true) + .setTrackSelector(trackSelector) .setLoadControl(PlayerHelper.getLoadControl()) .build() .loadPlaybackParams(isBackgroundMode = true) diff --git a/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt b/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt index de6b54bac..66edd1e43 100644 --- a/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt @@ -12,6 +12,7 @@ import androidx.core.app.ServiceCompat import androidx.core.net.toUri import androidx.lifecycle.LifecycleService import androidx.lifecycle.lifecycleScope +import androidx.media3.common.C import androidx.media3.common.MediaItem import androidx.media3.common.MimeTypes import androidx.media3.common.PlaybackException @@ -31,6 +32,7 @@ import com.github.libretube.db.obj.WatchPosition import com.github.libretube.extensions.parcelableExtra import com.github.libretube.extensions.setMetadata import com.github.libretube.extensions.toID +import com.github.libretube.extensions.updateParameters import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.PlayerHelper.checkForSegments import com.github.libretube.helpers.PlayerHelper.loadPlaybackParams @@ -236,6 +238,9 @@ class OnlinePlayerService : LifecycleService() { val trackSelector = DefaultTrackSelector(this) PlayerHelper.applyPreferredAudioQuality(this, trackSelector) + trackSelector.updateParameters { + setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, true) + } player = ExoPlayer.Builder(this) .setUsePlatformDiagnostics(false) @@ -312,7 +317,6 @@ class OnlinePlayerService : LifecycleService() { PlayerHelper.createDashSource( streams, this, - true, disableProxy ) to MimeTypes.APPLICATION_MPD } else { From e5e127a044a190132356d6af2f03fea7dbc0c7f6 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 8 Sep 2023 16:45:41 +0200 Subject: [PATCH 2/2] feat: disable video track loading while screen off --- .../libretube/ui/fragments/PlayerFragment.kt | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) 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 c611d92a2..d5648cad3 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 @@ -610,11 +610,16 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { } override fun onPause() { - // pauses the player if the screen is turned off - // check whether the screen is on val isInteractive = requireContext().getSystemService()!!.isInteractive + // disable video stream since it's not needed when screen off + if (!isInteractive && this::trackSelector.isInitialized) { + trackSelector.updateParameters { + setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, true) + } + } + // pause player if screen off and setting enabled if (this::exoPlayer.isInitialized && !isInteractive && PlayerHelper.pausePlayerOnScreenOffEnabled @@ -624,6 +629,17 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { super.onPause() } + override fun onResume() { + super.onResume() + + // re-enable and load video stream + if (this::trackSelector.isInitialized) { + trackSelector.updateParameters { + setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, false) + } + } + } + override fun onDestroy() { super.onDestroy()