Skip to content

Commit

Permalink
Filter unsupported tracks.
Browse files Browse the repository at this point in the history
  • Loading branch information
StaehliJ committed Aug 16, 2023
1 parent abda2b9 commit 6820dac
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
*/
package ch.srgssr.pillarbox.player.extension

import android.annotation.SuppressLint
import androidx.media3.common.C
import androidx.media3.common.C.TrackType
import androidx.media3.common.Format
import androidx.media3.common.TrackGroup
import androidx.media3.common.Tracks

Expand All @@ -19,40 +21,47 @@ val Tracks.text: List<Tracks.Group>
* Audio tracks.
*/
val Tracks.audio: List<Tracks.Group>
get() = filterByTrackType(C.TRACK_TYPE_AUDIO)
get() = filterByTrackType(C.TRACK_TYPE_AUDIO).mapNotNull { it.filterUnsupported() }

/**
* Video tracks.
*/
val Tracks.video: List<Tracks.Group>
get() = filterByTrackType(C.TRACK_TYPE_VIDEO)
get() = filterByTrackType(C.TRACK_TYPE_VIDEO).mapNotNull { it.filterUnsupported() }

private fun Tracks.filterByTrackType(trackType: @TrackType Int): List<Tracks.Group> {
return groups.filter { it.type == trackType }
}

@Suppress("SpreadOperator", "ReturnCount")
private fun Tracks.Group.filterForced(): Tracks.Group? {
if (type != C.TRACK_TYPE_TEXT) return this
if (length == 1 && getTrackFormat(0).isForced()) return null
val listIndexNotForced = ArrayList<Int>(length)
return filterBy { group, i -> group.getTrackFormat(i).isForced() }
}

internal fun Tracks.Group.filterUnsupported(): Tracks.Group? {
return filterBy { group, i -> !group.isTrackSupported(i) }
}

@SuppressLint("WrongConstant")
@Suppress("SpreadOperator", "ReturnCount")
internal fun Tracks.Group.filterBy(filter: (Tracks.Group, Int) -> Boolean): Tracks.Group? {
if (length == 1 && filter(this, 0)) return null
val listIndexToKeep = ArrayList<Int>(length)
for (i in 0 until length) {
val track = getTrackFormat(i)
if (!track.isForced()) {
listIndexNotForced.add(i)
if (!filter(this, i)) {
listIndexToKeep.add(i)
}
}
if (listIndexNotForced.size == length) return this
val count = listIndexNotForced.size
val formats = Array(count) {
getTrackFormat(listIndexNotForced[it])
}
val trackSupport = Array(count) {
getTrackSupport(listIndexNotForced[it])
}
val trackEnabled = Array(count) {
isTrackSelected(listIndexNotForced[it])
if (listIndexToKeep.isEmpty()) return null
if (listIndexToKeep.size == length) return this
val count = listIndexToKeep.size
val formats = ArrayList<Format>(count)
val trackSupport = IntArray(count)
val trackSelect = BooleanArray(count)
for (i in 0 until count) {
val trackIndex = listIndexToKeep[i]
formats.add(getTrackFormat(trackIndex))
trackSupport[i] = getTrackSupport(trackIndex)
trackSelect[i] = isTrackSelected(trackIndex)
}
val trackGroup = TrackGroup(mediaTrackGroup.id, *formats)
return Tracks.Group(trackGroup, isAdaptiveSupported, trackSupport.toIntArray(), trackEnabled.toBooleanArray())
return Tracks.Group(TrackGroup(mediaTrackGroup.id, *formats.toTypedArray()), isAdaptiveSupported, trackSupport, trackSelect)
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ class TestTracksExtension {

fun createTrackGroup(listFormat: List<Format>): Tracks.Group {
val trackGroup = TrackGroup(*listFormat.toTypedArray())
val trackSupport = IntArray(listFormat.size)
val trackSupport = IntArray(listFormat.size){
C.FORMAT_HANDLED
}
val selected = BooleanArray(listFormat.size)
return Tracks.Group(trackGroup, false, trackSupport, selected)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2023. SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.player

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 ch.srgssr.pillarbox.player.extension.filterUnsupported
import org.junit.Assert
import org.junit.Test

class TestUnsupportedTracks {

@Test
fun test1() {
val format1 = createSampleFormat("F1")
val format2 = createSampleFormat("F2")
val formatUnsupportedTrack = createSampleFormat("Unsupported")
val listFormat = listOf(format1, format2, formatUnsupportedTrack)
val trackGroup = TrackGroup(*listFormat.toTypedArray())
val selected = BooleanArray(listFormat.size)
val trackSupport = IntArray(listFormat.size)
trackSupport[0] = C.FORMAT_HANDLED
trackSupport[1] = C.FORMAT_HANDLED
trackSupport[2] = C.FORMAT_UNSUPPORTED_SUBTYPE
val tracks = Tracks(listOf(Tracks.Group(trackGroup, false, trackSupport, selected)))

val expectedTracksGroups = listOf(
Tracks.Group(
TrackGroup(*listOf(format1, format2).toTypedArray()), false, IntArray(2){
C.FORMAT_HANDLED
}, BooleanArray
(2)
)
)
Assert.assertEquals(expectedTracksGroups, tracks.groups.mapNotNull { it.filterUnsupported() })
}

companion object {
private fun createSampleFormat(
label: String
):
Format {
return Format.Builder()
.setId("id:${label}")
.setLabel(label)
.setContainerMimeType(MimeTypes.AUDIO_MP4)
.build()
}
}

}

0 comments on commit 6820dac

Please sign in to comment.