Commit 02066223 by olly Committed by Ian Baker

DownloadHelper: Support multiple track selection overrides

addTrackSelectionForSingleRenderer takes a list of legacy overrides,
which are then set on the supplied parameters one at a time to run
track selection. This allows multiple overrides for a single track
type to be applied in the download use case, despite it not being
possible to place such overrides directly into a single parameters.

For new style overrides, multiple overrides for the same track type
can be placed directly into a single parameters. Therefore we'll be
able to replace use of addTrackSelectionForSingleRenderer with use
of addTrackSelection, which is a much cleaner API. For this to work,
we need to make DownloadHelper apply multiple overrides in this case.

PiperOrigin-RevId: 432459834
parent 0316c033
......@@ -32,6 +32,7 @@ import androidx.media3.common.StreamKey;
import androidx.media3.common.Timeline;
import androidx.media3.common.TrackGroup;
import androidx.media3.common.TrackGroupArray;
import androidx.media3.common.TrackSelectionOverride;
import androidx.media3.common.TrackSelectionParameters;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.UnstableApi;
......@@ -605,8 +606,9 @@ public final class DownloadHelper {
*/
public void replaceTrackSelections(
int periodIndex, TrackSelectionParameters trackSelectionParameters) {
assertPreparedWithMedia();
clearTrackSelections(periodIndex);
addTrackSelection(periodIndex, trackSelectionParameters);
addTrackSelectionInternal(periodIndex, trackSelectionParameters);
}
/**
......@@ -620,8 +622,7 @@ public final class DownloadHelper {
public void addTrackSelection(
int periodIndex, TrackSelectionParameters trackSelectionParameters) {
assertPreparedWithMedia();
trackSelector.setParameters(trackSelectionParameters);
runTrackSelection(periodIndex);
addTrackSelectionInternal(periodIndex, trackSelectionParameters);
}
/**
......@@ -652,7 +653,7 @@ public final class DownloadHelper {
TrackSelectionParameters parameters =
parametersBuilder.setPreferredAudioLanguage(language).build();
for (int periodIndex = 0; periodIndex < periodCount; periodIndex++) {
addTrackSelection(periodIndex, parameters);
addTrackSelectionInternal(periodIndex, parameters);
}
}
}
......@@ -689,7 +690,7 @@ public final class DownloadHelper {
TrackSelectionParameters parameters =
parametersBuilder.setPreferredTextLanguage(language).build();
for (int periodIndex = 0; periodIndex < periodCount; periodIndex++) {
addTrackSelection(periodIndex, parameters);
addTrackSelectionInternal(periodIndex, parameters);
}
}
}
......@@ -716,12 +717,12 @@ public final class DownloadHelper {
builder.setRendererDisabled(/* rendererIndex= */ i, /* disabled= */ i != rendererIndex);
}
if (overrides.isEmpty()) {
addTrackSelection(periodIndex, builder.build());
addTrackSelectionInternal(periodIndex, builder.build());
} else {
TrackGroupArray trackGroupArray = mappedTrackInfos[periodIndex].getTrackGroups(rendererIndex);
for (int i = 0; i < overrides.size(); i++) {
builder.setSelectionOverride(rendererIndex, trackGroupArray, overrides.get(i));
addTrackSelection(periodIndex, builder.build());
addTrackSelectionInternal(periodIndex, builder.build());
}
}
}
......@@ -773,8 +774,28 @@ public final class DownloadHelper {
return requestBuilder.setStreamKeys(streamKeys).build();
}
// Initialization of array of Lists.
@SuppressWarnings("unchecked")
@RequiresNonNull({
"trackGroupArrays",
"trackSelectionsByPeriodAndRenderer",
"mediaPreparer",
"mediaPreparer.timeline"
})
private void addTrackSelectionInternal(
int periodIndex, TrackSelectionParameters trackSelectionParameters) {
trackSelector.setParameters(trackSelectionParameters);
runTrackSelection(periodIndex);
// TrackSelectionParameters can contain multiple overrides for each track type. The track
// selector will only use one of them (because it's designed for playback), but for downloads we
// want to use all of them. Run selection again with each override being the only one of its
// type, to ensure that all of the desired tracks are included.
for (TrackSelectionOverride override : trackSelectionParameters.overrides.values()) {
trackSelector.setParameters(
trackSelectionParameters.buildUpon().setOverrideForType(override).build());
runTrackSelection(periodIndex);
}
}
@SuppressWarnings("unchecked") // Initialization of array of Lists.
private void onMediaPrepared() {
checkNotNull(mediaPreparer);
checkNotNull(mediaPreparer.mediaPeriods);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment