Match MergingMediaPeriod track selection by period index in id

MergingMediaPeriod creates its track groups with ids concatenating position in its periods array and the underlying child track group id. The ids can be used in selectTracks for matching to periods list.

Issue: google/ExoPlayer#10930
PiperOrigin-RevId: 505074653
This commit is contained in:
michaelkatz 2023-01-27 11:25:33 +00:00 committed by christosts
parent b3e7696ba7
commit 542a1ef03f
2 changed files with 38 additions and 9 deletions

View File

@ -118,17 +118,13 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
for (int i = 0; i < selections.length; i++) {
Integer streamChildIndex = streams[i] == null ? null : streamPeriodIndices.get(streams[i]);
streamChildIndices[i] = streamChildIndex == null ? C.INDEX_UNSET : streamChildIndex;
selectionChildIndices[i] = C.INDEX_UNSET;
if (selections[i] != null) {
TrackGroup mergedTrackGroup = selections[i].getTrackGroup();
TrackGroup childTrackGroup =
checkNotNull(childTrackGroupByMergedTrackGroup.get(mergedTrackGroup));
for (int j = 0; j < periods.length; j++) {
if (periods[j].getTrackGroups().indexOf(childTrackGroup) != C.INDEX_UNSET) {
selectionChildIndices[i] = j;
break;
}
}
// mergedTrackGroup.id is 'periods array index' + ":" + childTrackGroup.id
selectionChildIndices[i] =
Integer.parseInt(mergedTrackGroup.id.substring(0, mergedTrackGroup.id.indexOf(":")));
} else {
selectionChildIndices[i] = C.INDEX_UNSET;
}
}
streamPeriodIndices.clear();

View File

@ -198,6 +198,39 @@ public final class MergingMediaPeriodTest {
assertThat(firstSelectionChild2).isEqualTo(secondSelectionChild2);
}
// https://github.com/google/ExoPlayer/issues/10930
@Test
public void selectTracks_withIdenticalFormats_selectsMatchingPeriod() throws Exception {
MergingMediaPeriod mergingMediaPeriod =
prepareMergingPeriod(
new MergingPeriodDefinition(
/* timeOffsetUs= */ 0, /* singleSampleTimeUs= */ 123_000, childFormat11),
new MergingPeriodDefinition(
/* timeOffsetUs= */ -3000, /* singleSampleTimeUs= */ 456_000, childFormat11));
ExoTrackSelection[] selectionArray = {
new FixedTrackSelection(mergingMediaPeriod.getTrackGroups().get(1), /* track= */ 0)
};
SampleStream[] streams = new SampleStream[1];
mergingMediaPeriod.selectTracks(
selectionArray,
/* mayRetainStreamFlags= */ new boolean[2],
streams,
/* streamResetFlags= */ new boolean[2],
/* positionUs= */ 0);
mergingMediaPeriod.continueLoading(/* positionUs= */ 0);
FormatHolder formatHolder = new FormatHolder();
DecoderInputBuffer inputBuffer =
new DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_NORMAL);
streams[0].readData(formatHolder, inputBuffer, FLAG_REQUIRE_FORMAT);
assertThat(streams[0].readData(formatHolder, inputBuffer, /* readFlags= */ 0))
.isEqualTo(C.RESULT_BUFFER_READ);
assertThat(inputBuffer.timeUs).isEqualTo(456_000 - 3000);
}
private MergingMediaPeriod prepareMergingPeriod(MergingPeriodDefinition... definitions)
throws Exception {
MediaPeriod[] mediaPeriods = new MediaPeriod[definitions.length];