Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: disable video track loading while screen off #4719

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 22 additions & 25 deletions app/src/main/java/com/github/libretube/helpers/DashHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -28,7 +28,6 @@ object DashHelper {
fun createManifest(
streams: Streams,
supportsHdr: Boolean,
audioOnly: Boolean = false,
rewriteUrls: Boolean
): String {
val builder = builderFactory.newDocumentBuilder()
Expand All @@ -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
}
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)
)
)
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -312,7 +317,6 @@ class OnlinePlayerService : LifecycleService() {
PlayerHelper.createDashSource(
streams,
this,
true,
disableProxy
) to MimeTypes.APPLICATION_MPD
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand Down
Loading