Skip to content

Commit b3422a0

Browse files
committed
Address API review comments on custom track format comparator
1 parent 5168797 commit b3422a0

5 files changed

Lines changed: 20 additions & 12 deletions

File tree

RELEASENOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
* Transformer:
5656
* Fix an issue where `ExportResult.fileSizeBytes` may be over-reported.
5757
* Track selection:
58+
* Expose `BaseTrackSelection.DEFAULT_FORMAT_COMPARATOR` and add
59+
`AdaptiveTrackSelection.Factory.setTrackFormatComparator` to allow
60+
custom format ordering and ABR selection priority beyond bitrate-only
61+
ordering.
5862
* Extractors:
5963
* MP4: Add support for extracting ITU-T T.35 (`it35`) timed metadata
6064
tracks.

libraries/exoplayer/src/main/java/androidx/media3/exoplayer/trackselection/AdaptiveTrackSelection.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.google.common.collect.Iterables;
3939
import com.google.common.collect.Multimap;
4040
import com.google.common.collect.MultimapBuilder;
41+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
4142
import java.util.ArrayList;
4243
import java.util.Arrays;
4344
import java.util.Comparator;
@@ -237,19 +238,18 @@ public Factory(
237238
* Sets the comparator used to order formats in adaptive track selections. The comparator order
238239
* controls which formats are considered first during adaptation.
239240
*
241+
* <p>The default is {@link BaseTrackSelection#DEFAULT_FORMAT_COMPARATOR}, which decides the
242+
* priority based on the bitrate.
243+
*
240244
* @param trackFormatComparator Comparator used to order selected formats.
241245
* @return This factory, for convenience.
242246
*/
247+
@CanIgnoreReturnValue
243248
public Factory setTrackFormatComparator(Comparator<Format> trackFormatComparator) {
244249
this.trackFormatComparator = checkNotNull(trackFormatComparator);
245250
return this;
246251
}
247252

248-
/** Returns the comparator used to order formats in adaptive track selections. */
249-
protected final Comparator<Format> getTrackFormatComparator() {
250-
return trackFormatComparator;
251-
}
252-
253253
@Override
254254
public final @NullableType ExoTrackSelection[] createTrackSelections(
255255
@NullableType Definition[] definitions,
@@ -311,7 +311,7 @@ protected AdaptiveTrackSelection createAdaptiveTrackSelection(
311311
bufferedFractionToLiveEdgeForQualityIncrease,
312312
adaptationCheckpoints,
313313
clock,
314-
getTrackFormatComparator());
314+
trackFormatComparator);
315315
}
316316
}
317317

libraries/exoplayer/src/main/java/androidx/media3/exoplayer/trackselection/BaseTrackSelection.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
@UnstableApi
3737
public abstract class BaseTrackSelection implements ExoTrackSelection {
3838

39-
static final Comparator<Format> DEFAULT_FORMAT_COMPARATOR =
39+
/** The default {@link Format} comparator, which sorts formats by bitrate in descending order. */
40+
public static final Comparator<Format> DEFAULT_FORMAT_COMPARATOR =
4041
(firstFormat, secondFormat) -> Integer.compare(secondFormat.bitrate, firstFormat.bitrate);
4142

4243
/** The selected {@link TrackGroup}. */
@@ -87,7 +88,8 @@ public BaseTrackSelection(TrackGroup group, int[] tracks, @Type int type) {
8788
* @param tracks The indices of the selected tracks within the {@link TrackGroup}. Must not be
8889
* null or empty. May be in any order.
8990
* @param type The type that will be returned from {@link TrackSelection#getType()}.
90-
* @param formatComparator Comparator that determines the order of selected {@link Format}s.
91+
* @param formatComparator Comparator that determines the order of selected {@linkplain Format
92+
* formats}.
9193
*/
9294
protected BaseTrackSelection(
9395
TrackGroup group, int[] tracks, @Type int type, Comparator<Format> formatComparator) {

libraries/exoplayer/src/main/java/androidx/media3/exoplayer/trackselection/TrackSelection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
* A track selection consisting of a static subset of selected tracks belonging to a {@link
3232
* TrackGroup}.
3333
*
34-
* <p>Tracks belonging to the subset are exposed in selection order, which by default is decreasing
35-
* bandwidth order.
34+
* <p>Tracks belonging to the subset are exposed in selection order with the highest priority track
35+
* first, for example in decreasing bandwidth order.
3636
*/
3737
@UnstableApi
3838
public interface TrackSelection {

libraries/exoplayer/src/test/java/androidx/media3/exoplayer/trackselection/AdaptiveTrackSelectionTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package androidx.media3.exoplayer.trackselection;
1717

18+
import static com.google.common.base.Preconditions.checkState;
1819
import static com.google.common.truth.Truth.assertThat;
1920
import static org.mockito.Mockito.when;
2021

@@ -588,7 +589,7 @@ public void updateSelectedTrack_withCustomFormatOrder_defersSwitchDownWhenBuffer
588589
/* playbackPositionUs= */ 0,
589590
/* bufferedDurationUs= */ 25_000_000,
590591
/* availableDurationUs= */ C.TIME_UNSET,
591-
/* queue= */ Collections.emptyList(),
592+
/* queue= */ ImmutableList.of(),
592593
createMediaChunkIterators(trackGroup, TEST_CHUNK_DURATION_US));
593594

594595
// With sufficient buffer (bufferedDurationUs >= maxDurationForQualityDecreaseUs), the
@@ -1029,6 +1030,7 @@ private static Comparator<Format> formatComparatorInOrder(List<Format> orderedFo
10291030

10301031
private static int getFormatOrderIndex(List<Format> orderedFormats, Format format) {
10311032
int index = orderedFormats.indexOf(format);
1032-
return index == C.INDEX_UNSET ? Integer.MAX_VALUE : index;
1033+
checkState(index != C.INDEX_UNSET);
1034+
return index;
10331035
}
10341036
}

0 commit comments

Comments
 (0)