Commit 9779f2c3 by tonihei Committed by Oliver Woodman

Add DashMediaPeriod getStreamKeys implementation and test.

PiperOrigin-RevId: 231385518
parent 92bf8e91
......@@ -22,6 +22,7 @@ import android.util.SparseIntArray;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.SeekParameters;
import com.google.android.exoplayer2.offline.StreamKey;
import com.google.android.exoplayer2.source.CompositeSequenceableLoaderFactory;
import com.google.android.exoplayer2.source.EmptySampleStream;
import com.google.android.exoplayer2.source.MediaPeriod;
......@@ -193,6 +194,49 @@ import java.util.List;
}
@Override
public List<StreamKey> getStreamKeys(List<TrackSelection> trackSelections) {
List<AdaptationSet> manifestAdaptationSets = manifest.getPeriod(periodIndex).adaptationSets;
List<StreamKey> streamKeys = new ArrayList<>();
for (TrackSelection trackSelection : trackSelections) {
int trackGroupIndex = trackGroups.indexOf(trackSelection.getTrackGroup());
TrackGroupInfo trackGroupInfo = trackGroupInfos[trackGroupIndex];
if (trackGroupInfo.trackGroupCategory != TrackGroupInfo.CATEGORY_PRIMARY) {
// Ignore non-primary tracks.
continue;
}
int[] adaptationSetIndices = trackGroupInfo.adaptationSetIndices;
int[] trackIndices = new int[trackSelection.length()];
for (int i = 0; i < trackSelection.length(); i++) {
trackIndices[i] = trackSelection.getIndexInTrackGroup(i);
}
Arrays.sort(trackIndices);
int currentAdaptationSetIndex = 0;
int totalTracksInPreviousAdaptationSets = 0;
int tracksInCurrentAdaptationSet =
manifestAdaptationSets.get(adaptationSetIndices[0]).representations.size();
for (int i = 0; i < trackIndices.length; i++) {
while (trackIndices[i]
>= totalTracksInPreviousAdaptationSets + tracksInCurrentAdaptationSet) {
currentAdaptationSetIndex++;
totalTracksInPreviousAdaptationSets += tracksInCurrentAdaptationSet;
tracksInCurrentAdaptationSet =
manifestAdaptationSets
.get(adaptationSetIndices[currentAdaptationSetIndex])
.representations
.size();
}
streamKeys.add(
new StreamKey(
periodIndex,
adaptationSetIndices[currentAdaptationSetIndex],
trackIndices[i] - totalTracksInPreviousAdaptationSets));
}
}
return streamKeys;
}
@Override
public long selectTracks(TrackSelection[] selections, boolean[] mayRetainStreamFlags,
SampleStream[] streams, boolean[] streamResetFlags, long positionUs) {
int[] streamIndexToTrackGroupIndex = getStreamIndexToTrackGroupIndex(selections);
......@@ -697,7 +741,7 @@ import java.util.List;
public final int[] adaptationSetIndices;
public final int trackType;
public @TrackGroupCategory final int trackGroupCategory;
@TrackGroupCategory public final int trackGroupCategory;
public final int eventStreamGroupIndex;
public final int primaryTrackGroupIndex;
......@@ -748,7 +792,7 @@ import java.util.List;
return new TrackGroupInfo(
C.TRACK_TYPE_METADATA,
CATEGORY_MANIFEST_EVENTS,
null,
new int[0],
-1,
C.INDEX_UNSET,
C.INDEX_UNSET,
......
......@@ -77,7 +77,7 @@ public class SsMediaPeriodTest {
mock(Allocator.class));
MediaPeriodAsserts.assertGetStreamKeysAndManifestFilterIntegration(
mediaPeriodFactory, testManifest, /* periodIndex= */ 0);
mediaPeriodFactory, testManifest);
}
private static Format createVideoFormat(int bitrate) {
......
......@@ -58,11 +58,30 @@ public final class MediaPeriodAsserts {
*
* @param mediaPeriodFactory A factory to create a {@link MediaPeriod} based on a manifest.
* @param manifest The manifest which is to be tested.
*/
public static <T extends FilterableManifest<T>>
void assertGetStreamKeysAndManifestFilterIntegration(
FilterableManifestMediaPeriodFactory<T> mediaPeriodFactory, T manifest) {
assertGetStreamKeysAndManifestFilterIntegration(
mediaPeriodFactory, manifest, /* periodIndex= */ 0, /* ignoredMimeType= */ null);
}
/**
* Asserts that the values returns by {@link MediaPeriod#getStreamKeys(List)} are compatible with
* a {@link FilterableManifest} using these stream keys.
*
* @param mediaPeriodFactory A factory to create a {@link MediaPeriod} based on a manifest.
* @param manifest The manifest which is to be tested.
* @param periodIndex The index of period in the manifest.
* @param ignoredMimeType Optional mime type whose existence in the filtered track groups is not
* asserted.
*/
public static <T extends FilterableManifest<T>>
void assertGetStreamKeysAndManifestFilterIntegration(
FilterableManifestMediaPeriodFactory<T> mediaPeriodFactory, T manifest, int periodIndex) {
FilterableManifestMediaPeriodFactory<T> mediaPeriodFactory,
T manifest,
int periodIndex,
@Nullable String ignoredMimeType) {
MediaPeriod mediaPeriod = mediaPeriodFactory.createMediaPeriod(manifest, periodIndex);
TrackGroupArray trackGroupArray = getTrackGroups(mediaPeriod);
......@@ -94,13 +113,17 @@ public final class MediaPeriodAsserts {
}
}
if (trackGroupArray.length > 1) {
for (int i = 0; i < trackGroupArray.length - 1; i++) {
for (int j = i + 1; j < trackGroupArray.length; j++) {
testSelections.add(
Arrays.asList(
new TrackSelection[] {
new TestTrackSelection(trackGroupArray.get(0), 0),
new TestTrackSelection(trackGroupArray.get(1), 0)
new TestTrackSelection(trackGroupArray.get(i), 0),
new TestTrackSelection(trackGroupArray.get(j), 0)
}));
}
}
}
if (trackGroupArray.length > 2) {
List<TrackSelection> selectionsFromAllGroups = new ArrayList<>();
for (int i = 0; i < trackGroupArray.length; i++) {
......@@ -113,12 +136,20 @@ public final class MediaPeriodAsserts {
// contain at least all requested formats.
for (List<TrackSelection> testSelection : testSelections) {
List<StreamKey> streamKeys = mediaPeriod.getStreamKeys(testSelection);
if (streamKeys.isEmpty()) {
// Manifests won't be filtered if stream key is empty.
continue;
}
T filteredManifest = manifest.copy(streamKeys);
// The filtered manifest should only have one period left.
MediaPeriod filteredMediaPeriod =
mediaPeriodFactory.createMediaPeriod(filteredManifest, /* periodIndex= */ 0);
TrackGroupArray filteredTrackGroupArray = getTrackGroups(filteredMediaPeriod);
for (TrackSelection trackSelection : testSelection) {
if (ignoredMimeType != null
&& ignoredMimeType.equals(trackSelection.getFormat(0).sampleMimeType)) {
continue;
}
Format[] expectedFormats = new Format[trackSelection.length()];
for (int k = 0; k < trackSelection.length(); k++) {
expectedFormats[k] = trackSelection.getFormat(k);
......
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