mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 22:30:30 +05:30
Merge pull request #4719 from Bnyro/master
feat: disable video track loading while screen off
This commit is contained in:
commit
f268b6dec7
@ -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<AdapSetInfo>()
|
||||
|
||||
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) {
|
||||
|
@ -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
|
||||
)
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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 {
|
||||
|
@ -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<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
|
||||
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()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user