Skip to content

Commit

Permalink
Add container format sniffing in HLS
Browse files Browse the repository at this point in the history
Issue:#2025

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=211977802
  • Loading branch information
AquilesCanta authored and ojw28 committed Sep 12, 2018
1 parent da88b34 commit b14e093
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 69 deletions.
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
[VR180](https://github.com/google/spatial-media/blob/master/docs/vr180.md).
* HLS:
* Support PlayReady.
* Add container format sniffing
([#2025](https://github.com/google/ExoPlayer/issues/2025)).
* Support alternative `EXT-X-KEY` tags.
* Support `EXT-X-INDEPENDENT-SEGMENTS` in the master playlist.
* Support variable substitution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.drm.DrmInitData;
import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.extractor.mp3.Mp3Extractor;
import com.google.android.exoplayer2.extractor.mp4.FragmentedMp4Extractor;
import com.google.android.exoplayer2.extractor.ts.Ac3Extractor;
import com.google.android.exoplayer2.extractor.ts.AdtsExtractor;
import com.google.android.exoplayer2.extractor.ts.DefaultTsPayloadReaderFactory;
import com.google.android.exoplayer2.extractor.ts.TsExtractor;
import com.google.android.exoplayer2.source.UnrecognizedInputFormatException;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.TimestampAdjuster;
import java.io.EOFException;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -56,74 +60,187 @@ public Pair<Extractor, Boolean> createExtractor(
List<Format> muxedCaptionFormats,
DrmInitData drmInitData,
TimestampAdjuster timestampAdjuster,
Map<String, List<String>> responseHeaders) {
Map<String, List<String>> responseHeaders,
ExtractorInput extractorInput)
throws InterruptedException, IOException {

if (previousExtractor != null) {
// A extractor has already been successfully used. Return one of the same type.
if (previousExtractor instanceof TsExtractor
|| previousExtractor instanceof FragmentedMp4Extractor) {
// TS and fMP4 extractors can be reused.
return buildResult(previousExtractor);
} else if (previousExtractor instanceof WebvttExtractor) {
return buildResult(new WebvttExtractor(format.language, timestampAdjuster));
} else if (previousExtractor instanceof AdtsExtractor) {
return buildResult(new AdtsExtractor());
} else if (previousExtractor instanceof Ac3Extractor) {
return buildResult(new Ac3Extractor());
} else if (previousExtractor instanceof Mp3Extractor) {
return buildResult(new Mp3Extractor());
} else {
throw new IllegalArgumentException(
"Unexpected previousExtractor type: " + previousExtractor.getClass().getSimpleName());
}
}

// Try selecting the extractor by the file extension.
Extractor extractorByFileExtension =
createExtractorByFileExtension(
uri, format, muxedCaptionFormats, drmInitData, timestampAdjuster);
extractorInput.resetPeekPosition();
if (sniffQuietly(extractorByFileExtension, extractorInput)) {
return buildResult(extractorByFileExtension);
}

// We need to manually sniff each known type, without retrying the one selected by file
// extension.

if (!(extractorByFileExtension instanceof WebvttExtractor)) {
WebvttExtractor webvttExtractor = new WebvttExtractor(format.language, timestampAdjuster);
if (sniffQuietly(webvttExtractor, extractorInput)) {
return buildResult(webvttExtractor);
}
}

if (!(extractorByFileExtension instanceof AdtsExtractor)) {
AdtsExtractor adtsExtractor = new AdtsExtractor();
if (sniffQuietly(adtsExtractor, extractorInput)) {
return buildResult(adtsExtractor);
}
}

if (!(extractorByFileExtension instanceof Ac3Extractor)) {
Ac3Extractor ac3Extractor = new Ac3Extractor();
if (sniffQuietly(ac3Extractor, extractorInput)) {
return buildResult(ac3Extractor);
}
}

if (!(extractorByFileExtension instanceof Mp3Extractor)) {
Mp3Extractor mp3Extractor =
new Mp3Extractor(/* flags= */ 0, /* forcedFirstSampleTimestampUs= */ 0);
if (sniffQuietly(mp3Extractor, extractorInput)) {
return buildResult(mp3Extractor);
}
}

if (!(extractorByFileExtension instanceof FragmentedMp4Extractor)) {
FragmentedMp4Extractor fragmentedMp4Extractor =
new FragmentedMp4Extractor(
/* flags= */ 0,
timestampAdjuster,
/* sideloadedTrack= */ null,
drmInitData,
muxedCaptionFormats != null ? muxedCaptionFormats : Collections.emptyList());
if (sniffQuietly(fragmentedMp4Extractor, extractorInput)) {
return buildResult(fragmentedMp4Extractor);
}
}

if (!(extractorByFileExtension instanceof TsExtractor)) {
TsExtractor tsExtractor = createTsExtractor(format, muxedCaptionFormats, timestampAdjuster);
if (sniffQuietly(tsExtractor, extractorInput)) {
return buildResult(tsExtractor);
}
}

throw new UnrecognizedInputFormatException(
"The segment does not seem to conform to any of the known HLS segment formats", uri);
}

private Extractor createExtractorByFileExtension(
Uri uri,
Format format,
List<Format> muxedCaptionFormats,
DrmInitData drmInitData,
TimestampAdjuster timestampAdjuster) {
String lastPathSegment = uri.getLastPathSegment();
if (lastPathSegment == null) {
lastPathSegment = "";
}
boolean isPackedAudioExtractor = false;
Extractor extractor;
if (MimeTypes.TEXT_VTT.equals(format.sampleMimeType)
|| lastPathSegment.endsWith(WEBVTT_FILE_EXTENSION)
|| lastPathSegment.endsWith(VTT_FILE_EXTENSION)) {
extractor = new WebvttExtractor(format.language, timestampAdjuster);
return new WebvttExtractor(format.language, timestampAdjuster);
} else if (lastPathSegment.endsWith(AAC_FILE_EXTENSION)) {
isPackedAudioExtractor = true;
extractor = new AdtsExtractor();
return new AdtsExtractor();
} else if (lastPathSegment.endsWith(AC3_FILE_EXTENSION)
|| lastPathSegment.endsWith(EC3_FILE_EXTENSION)) {
isPackedAudioExtractor = true;
extractor = new Ac3Extractor();
return new Ac3Extractor();
} else if (lastPathSegment.endsWith(MP3_FILE_EXTENSION)) {
isPackedAudioExtractor = true;
extractor = new Mp3Extractor(0, 0);
} else if (previousExtractor != null) {
// Only reuse TS and fMP4 extractors.
extractor = previousExtractor;
return new Mp3Extractor(/* flags= */ 0, /* forcedFirstSampleTimestampUs= */ 0);
} else if (lastPathSegment.endsWith(MP4_FILE_EXTENSION)
|| lastPathSegment.startsWith(M4_FILE_EXTENSION_PREFIX, lastPathSegment.length() - 4)
|| lastPathSegment.startsWith(MP4_FILE_EXTENSION_PREFIX, lastPathSegment.length() - 5)) {
extractor =
new FragmentedMp4Extractor(
/* flags= */ 0,
timestampAdjuster,
/* sideloadedTrack= */ null,
drmInitData,
muxedCaptionFormats != null ? muxedCaptionFormats : Collections.emptyList());
return new FragmentedMp4Extractor(
/* flags= */ 0,
timestampAdjuster,
/* sideloadedTrack= */ null,
drmInitData,
muxedCaptionFormats != null ? muxedCaptionFormats : Collections.emptyList());
} else {
// For any other file extension, we assume TS format.
@DefaultTsPayloadReaderFactory.Flags
int esReaderFactoryFlags = DefaultTsPayloadReaderFactory.FLAG_IGNORE_SPLICE_INFO_STREAM;
if (muxedCaptionFormats != null) {
// The playlist declares closed caption renditions, we should ignore descriptors.
esReaderFactoryFlags |= DefaultTsPayloadReaderFactory.FLAG_OVERRIDE_CAPTION_DESCRIPTORS;
} else {
// The playlist does not provide any closed caption information. We preemptively declare a
// closed caption track on channel 0.
muxedCaptionFormats =
Collections.singletonList(
Format.createTextSampleFormat(
/* id= */ null,
MimeTypes.APPLICATION_CEA608,
/* selectionFlags= */ 0,
/* language= */ null));
return createTsExtractor(format, muxedCaptionFormats, timestampAdjuster);
}
}

private static TsExtractor createTsExtractor(
Format format, List<Format> muxedCaptionFormats, TimestampAdjuster timestampAdjuster) {
@DefaultTsPayloadReaderFactory.Flags
int esReaderFactoryFlags = DefaultTsPayloadReaderFactory.FLAG_IGNORE_SPLICE_INFO_STREAM;
if (muxedCaptionFormats != null) {
// The playlist declares closed caption renditions, we should ignore descriptors.
esReaderFactoryFlags |= DefaultTsPayloadReaderFactory.FLAG_OVERRIDE_CAPTION_DESCRIPTORS;
} else {
// The playlist does not provide any closed caption information. We preemptively declare a
// closed caption track on channel 0.
muxedCaptionFormats =
Collections.singletonList(
Format.createTextSampleFormat(
/* id= */ null,
MimeTypes.APPLICATION_CEA608,
/* selectionFlags= */ 0,
/* language= */ null));
}
String codecs = format.codecs;
if (!TextUtils.isEmpty(codecs)) {
// Sometimes AAC and H264 streams are declared in TS chunks even though they don't really
// exist. If we know from the codec attribute that they don't exist, then we can
// explicitly ignore them even if they're declared.
if (!MimeTypes.AUDIO_AAC.equals(MimeTypes.getAudioMediaMimeType(codecs))) {
esReaderFactoryFlags |= DefaultTsPayloadReaderFactory.FLAG_IGNORE_AAC_STREAM;
}
String codecs = format.codecs;
if (!TextUtils.isEmpty(codecs)) {
// Sometimes AAC and H264 streams are declared in TS chunks even though they don't really
// exist. If we know from the codec attribute that they don't exist, then we can
// explicitly ignore them even if they're declared.
if (!MimeTypes.AUDIO_AAC.equals(MimeTypes.getAudioMediaMimeType(codecs))) {
esReaderFactoryFlags |= DefaultTsPayloadReaderFactory.FLAG_IGNORE_AAC_STREAM;
}
if (!MimeTypes.VIDEO_H264.equals(MimeTypes.getVideoMediaMimeType(codecs))) {
esReaderFactoryFlags |= DefaultTsPayloadReaderFactory.FLAG_IGNORE_H264_STREAM;
}
if (!MimeTypes.VIDEO_H264.equals(MimeTypes.getVideoMediaMimeType(codecs))) {
esReaderFactoryFlags |= DefaultTsPayloadReaderFactory.FLAG_IGNORE_H264_STREAM;
}
extractor = new TsExtractor(TsExtractor.MODE_HLS, timestampAdjuster,
new DefaultTsPayloadReaderFactory(esReaderFactoryFlags, muxedCaptionFormats));
}
return Pair.create(extractor, isPackedAudioExtractor);

return new TsExtractor(
TsExtractor.MODE_HLS,
timestampAdjuster,
new DefaultTsPayloadReaderFactory(esReaderFactoryFlags, muxedCaptionFormats));
}

private static Pair<Extractor, Boolean> buildResult(Extractor extractor) {
return new Pair<>(
extractor,
extractor instanceof AdtsExtractor
|| extractor instanceof Ac3Extractor
|| extractor instanceof Mp3Extractor);
}

private static boolean sniffQuietly(Extractor extractor, ExtractorInput input)
throws InterruptedException, IOException {
boolean result = false;
try {
result = extractor.sniff(input);
} catch (EOFException e) {
// Do nothing.
} finally {
input.resetPeekPosition();
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.drm.DrmInitData;
import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.extractor.PositionHolder;
import com.google.android.exoplayer2.util.TimestampAdjuster;
import java.io.IOException;
import java.util.List;
import java.util.Map;

Expand All @@ -45,9 +48,14 @@ public interface HlsExtractorFactory {
* @param timestampAdjuster Adjuster corresponding to the provided discontinuity sequence number.
* @param responseHeaders The HTTP response headers associated with the media segment or
* initialization section to extract.
* @param sniffingExtractorInput The first extractor input that will be passed to the returned
* extractor's {@link Extractor#read(ExtractorInput, PositionHolder)}. Must only be used to
* call {@link Extractor#sniff(ExtractorInput)}.
* @return A pair containing the {@link Extractor} and a boolean that indicates whether it is a
* packed audio extractor. The first element may be {@code previousExtractor} if the factory
* has determined it can be re-used.
* @throws InterruptedException If the thread is interrupted while sniffing.
* @throws IOException If an I/O error is encountered while sniffing.
*/
Pair<Extractor, Boolean> createExtractor(
Extractor previousExtractor,
Expand All @@ -56,5 +64,7 @@ Pair<Extractor, Boolean> createExtractor(
List<Format> muxedCaptionFormats,
DrmInitData drmInitData,
TimestampAdjuster timestampAdjuster,
Map<String, List<String>> responseHeaders);
Map<String, List<String>> responseHeaders,
ExtractorInput sniffingExtractorInput)
throws InterruptedException, IOException;
}
Loading

0 comments on commit b14e093

Please sign in to comment.