Skip to content

Commit

Permalink
Avoid chunkless preparation if the codec mapping is ambiguous
Browse files Browse the repository at this point in the history
Issue: #7877
PiperOrigin-RevId: 338659937
  • Loading branch information
AquilesCanta authored and ojw28 committed Nov 2, 2020
1 parent 5b1514e commit a184924
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,18 @@ public static String getUserAgent(Context context, String applicationName) {
+ ") " + ExoPlayerLibraryInfo.VERSION_SLASHY;
}

/** Returns the number of codec strings in {@code codecs} whose type matches {@code trackType}. */
public static int getCodecCountOfType(@Nullable String codecs, int trackType) {
String[] codecArray = splitCodecs(codecs);
int count = 0;
for (String codec : codecArray) {
if (trackType == MimeTypes.getTrackTypeOfCodec(codec)) {
count++;
}
}
return count;
}

/**
* Returns a copy of {@code codecs} without the codecs whose track type doesn't match {@code
* trackType}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,12 @@ private void buildAndPrepareMainSampleStreamWrapper(
}
}
String codecs = selectedPlaylistFormats[0].codecs;
int numberOfVideoCodecs = Util.getCodecCountOfType(codecs, C.TRACK_TYPE_VIDEO);
int numberOfAudioCodecs = Util.getCodecCountOfType(codecs, C.TRACK_TYPE_AUDIO);
boolean codecsStringAllowsChunklessPreparation =
numberOfAudioCodecs <= 1
&& numberOfVideoCodecs <= 1
&& numberOfAudioCodecs + numberOfVideoCodecs > 0;
HlsSampleStreamWrapper sampleStreamWrapper =
buildSampleStreamWrapper(
C.TRACK_TYPE_DEFAULT,
Expand All @@ -614,18 +620,16 @@ private void buildAndPrepareMainSampleStreamWrapper(
positionUs);
sampleStreamWrappers.add(sampleStreamWrapper);
manifestUrlIndicesPerWrapper.add(selectedVariantIndices);
if (allowChunklessPreparation && codecs != null) {
boolean variantsContainVideoCodecs = Util.getCodecsOfType(codecs, C.TRACK_TYPE_VIDEO) != null;
boolean variantsContainAudioCodecs = Util.getCodecsOfType(codecs, C.TRACK_TYPE_AUDIO) != null;
if (allowChunklessPreparation && codecsStringAllowsChunklessPreparation) {
List<TrackGroup> muxedTrackGroups = new ArrayList<>();
if (variantsContainVideoCodecs) {
if (numberOfVideoCodecs > 0) {
Format[] videoFormats = new Format[selectedVariantsCount];
for (int i = 0; i < videoFormats.length; i++) {
videoFormats[i] = deriveVideoFormat(selectedPlaylistFormats[i]);
}
muxedTrackGroups.add(new TrackGroup(videoFormats));

if (variantsContainAudioCodecs
if (numberOfAudioCodecs > 0
&& (masterPlaylist.muxedAudioFormat != null || masterPlaylist.audios.isEmpty())) {
muxedTrackGroups.add(
new TrackGroup(
Expand All @@ -640,7 +644,7 @@ private void buildAndPrepareMainSampleStreamWrapper(
muxedTrackGroups.add(new TrackGroup(ccFormats.get(i)));
}
}
} else if (variantsContainAudioCodecs) {
} else /* numberOfAudioCodecs > 0 */ {
// Variants only contain audio.
Format[] audioFormats = new Format[selectedVariantsCount];
for (int i = 0; i < audioFormats.length; i++) {
Expand All @@ -651,9 +655,6 @@ private void buildAndPrepareMainSampleStreamWrapper(
/* isPrimaryTrackInVariant= */ true);
}
muxedTrackGroups.add(new TrackGroup(audioFormats));
} else {
// Variants contain codecs but no video or audio entries could be identified.
throw new IllegalArgumentException("Unexpected codecs attribute: " + codecs);
}

TrackGroup id3TrackGroup =
Expand Down Expand Up @@ -693,7 +694,7 @@ private void buildAndPrepareAudioSampleStreamWrappers(
continue;
}

boolean renditionsHaveCodecs = true;
boolean codecStringsAllowChunklessPreparation = true;
scratchPlaylistUrls.clear();
scratchPlaylistFormats.clear();
scratchIndicesList.clear();
Expand All @@ -704,7 +705,8 @@ private void buildAndPrepareAudioSampleStreamWrappers(
scratchIndicesList.add(renditionIndex);
scratchPlaylistUrls.add(rendition.url);
scratchPlaylistFormats.add(rendition.format);
renditionsHaveCodecs &= rendition.format.codecs != null;
codecStringsAllowChunklessPreparation &=
Util.getCodecCountOfType(rendition.format.codecs, C.TRACK_TYPE_AUDIO) == 1;
}
}

Expand All @@ -720,7 +722,7 @@ private void buildAndPrepareAudioSampleStreamWrappers(
manifestUrlsIndicesPerWrapper.add(Ints.toArray(scratchIndicesList));
sampleStreamWrappers.add(sampleStreamWrapper);

if (allowChunklessPreparation && renditionsHaveCodecs) {
if (allowChunklessPreparation && codecStringsAllowChunklessPreparation) {
Format[] renditionFormats = scratchPlaylistFormats.toArray(new Format[0]);
sampleStreamWrapper.prepareWithMasterPlaylistInfo(
new TrackGroup[] {new TrackGroup(renditionFormats)}, /* primaryTrackGroupIndex= */ 0);
Expand Down

0 comments on commit a184924

Please sign in to comment.