Merge pull request #4719 from Bnyro/master

feat: disable video track loading while screen off
This commit is contained in:
Bnyro 2023-09-08 16:44:56 +02:00 committed by GitHub
commit f268b6dec7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 31 deletions

View File

@ -2,13 +2,13 @@ package com.github.libretube.helpers
import com.github.libretube.api.obj.PipedStream import com.github.libretube.api.obj.PipedStream
import com.github.libretube.api.obj.Streams import com.github.libretube.api.obj.Streams
import org.w3c.dom.Document
import org.w3c.dom.Element
import java.io.StringWriter import java.io.StringWriter
import javax.xml.parsers.DocumentBuilderFactory import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.transform.TransformerFactory import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult 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 // Based off of https://github.com/TeamPiped/Piped/blob/master/src/utils/DashUtils.js
@ -28,7 +28,6 @@ object DashHelper {
fun createManifest( fun createManifest(
streams: Streams, streams: Streams,
supportsHdr: Boolean, supportsHdr: Boolean,
audioOnly: Boolean = false,
rewriteUrls: Boolean rewriteUrls: Boolean
): String { ): String {
val builder = builderFactory.newDocumentBuilder() val builder = builderFactory.newDocumentBuilder()
@ -45,7 +44,6 @@ object DashHelper {
val adapSetInfos = ArrayList<AdapSetInfo>() val adapSetInfos = ArrayList<AdapSetInfo>()
if (!audioOnly) {
for ( for (
stream in streams.videoStreams stream in streams.videoStreams
// used to avoid including LBRY HLS inside the streams in the manifest // used to avoid including LBRY HLS inside the streams in the manifest
@ -69,7 +67,6 @@ object DashHelper {
) )
) )
} }
}
for (stream in streams.audioStreams) { for (stream in streams.audioStreams) {
val adapSetInfo = val adapSetInfo =

View File

@ -51,13 +51,11 @@ object PlayerHelper {
fun createDashSource( fun createDashSource(
streams: Streams, streams: Streams,
context: Context, context: Context,
audioOnly: Boolean = false,
disableProxy: Boolean disableProxy: Boolean
): Uri { ): Uri {
val manifest = DashHelper.createManifest( val manifest = DashHelper.createManifest(
streams, streams,
DisplayHelper.supportsHdr(context), DisplayHelper.supportsHdr(context),
audioOnly,
disableProxy disableProxy
) )

View File

@ -6,8 +6,10 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.ServiceCompat import androidx.core.app.ServiceCompat
import androidx.lifecycle.LifecycleService import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.media3.common.C
import androidx.media3.common.MediaItem import androidx.media3.common.MediaItem
import androidx.media3.exoplayer.ExoPlayer import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PLAYER_CHANNEL_ID 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.db.obj.DownloadWithItems
import com.github.libretube.enums.FileType import com.github.libretube.enums.FileType
import com.github.libretube.extensions.toAndroidUri import com.github.libretube.extensions.toAndroidUri
import com.github.libretube.extensions.updateParameters
import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.PlayerHelper
import com.github.libretube.helpers.PlayerHelper.loadPlaybackParams import com.github.libretube.helpers.PlayerHelper.loadPlaybackParams
import com.github.libretube.obj.PlayerNotificationData import com.github.libretube.obj.PlayerNotificationData
@ -78,10 +81,16 @@ class OfflinePlayerService : LifecycleService() {
*/ */
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class) @androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
private fun startAudioPlayer(downloadWithItem: DownloadWithItems): Boolean { private fun startAudioPlayer(downloadWithItem: DownloadWithItems): Boolean {
val trackSelector = DefaultTrackSelector(this)
trackSelector.updateParameters {
setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, true)
}
player = ExoPlayer.Builder(this) player = ExoPlayer.Builder(this)
.setUsePlatformDiagnostics(false) .setUsePlatformDiagnostics(false)
.setHandleAudioBecomingNoisy(true) .setHandleAudioBecomingNoisy(true)
.setAudioAttributes(PlayerHelper.getAudioAttributes(), true) .setAudioAttributes(PlayerHelper.getAudioAttributes(), true)
.setTrackSelector(trackSelector)
.setLoadControl(PlayerHelper.getLoadControl()) .setLoadControl(PlayerHelper.getLoadControl())
.build() .build()
.loadPlaybackParams(isBackgroundMode = true) .loadPlaybackParams(isBackgroundMode = true)

View File

@ -12,6 +12,7 @@ import androidx.core.app.ServiceCompat
import androidx.core.net.toUri import androidx.core.net.toUri
import androidx.lifecycle.LifecycleService import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.media3.common.C
import androidx.media3.common.MediaItem import androidx.media3.common.MediaItem
import androidx.media3.common.MimeTypes import androidx.media3.common.MimeTypes
import androidx.media3.common.PlaybackException 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.parcelableExtra
import com.github.libretube.extensions.setMetadata import com.github.libretube.extensions.setMetadata
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.extensions.updateParameters
import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.PlayerHelper
import com.github.libretube.helpers.PlayerHelper.checkForSegments import com.github.libretube.helpers.PlayerHelper.checkForSegments
import com.github.libretube.helpers.PlayerHelper.loadPlaybackParams import com.github.libretube.helpers.PlayerHelper.loadPlaybackParams
@ -236,6 +238,9 @@ class OnlinePlayerService : LifecycleService() {
val trackSelector = DefaultTrackSelector(this) val trackSelector = DefaultTrackSelector(this)
PlayerHelper.applyPreferredAudioQuality(this, trackSelector) PlayerHelper.applyPreferredAudioQuality(this, trackSelector)
trackSelector.updateParameters {
setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, true)
}
player = ExoPlayer.Builder(this) player = ExoPlayer.Builder(this)
.setUsePlatformDiagnostics(false) .setUsePlatformDiagnostics(false)
@ -312,7 +317,6 @@ class OnlinePlayerService : LifecycleService() {
PlayerHelper.createDashSource( PlayerHelper.createDashSource(
streams, streams,
this, this,
true,
disableProxy disableProxy
) to MimeTypes.APPLICATION_MPD ) to MimeTypes.APPLICATION_MPD
} else { } else {

View File

@ -610,11 +610,16 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
} }
override fun onPause() { override fun onPause() {
// pauses the player if the screen is turned off
// check whether the screen is on // check whether the screen is on
val isInteractive = requireContext().getSystemService<PowerManager>()!!.isInteractive val isInteractive = requireContext().getSystemService<PowerManager>()!!.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 // pause player if screen off and setting enabled
if (this::exoPlayer.isInitialized && !isInteractive && if (this::exoPlayer.isInitialized && !isInteractive &&
PlayerHelper.pausePlayerOnScreenOffEnabled PlayerHelper.pausePlayerOnScreenOffEnabled
@ -624,6 +629,17 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
super.onPause() 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() { override fun onDestroy() {
super.onDestroy() super.onDestroy()