Skip to content

Commit

Permalink
Ignore discontinuities in TSs for HLS
Browse files Browse the repository at this point in the history
Issue:google#1921
Issue:google#1978
Issue:google#2163
Issue:google#2172

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=141797474
  • Loading branch information
AquilesCanta authored and ojw28 committed Dec 14, 2016
1 parent eee9bf5 commit 9812aa7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class TsExtractor implements Extractor {
public static final int WORKAROUND_IGNORE_AAC_STREAM = 2;
public static final int WORKAROUND_IGNORE_H264_STREAM = 4;
public static final int WORKAROUND_DETECT_ACCESS_UNITS = 8;
public static final int WORKAROUND_MAP_BY_TYPE = 16;
public static final int WORKAROUND_HLS_MODE = 16;

private static final String TAG = "TsExtractor";

Expand Down Expand Up @@ -191,18 +191,22 @@ public int read(ExtractorInput input, PositionHolder seekPosition)
tsScratch.skipBits(2); // transport_scrambling_control
boolean adaptationFieldExists = tsScratch.readBit();
boolean payloadExists = tsScratch.readBit();

// Discontinuity check.
boolean discontinuityFound = false;
int continuityCounter = tsScratch.readBits(4);
int previousCounter = continuityCounters.get(pid, continuityCounter - 1);
continuityCounters.put(pid, continuityCounter);
if (previousCounter == continuityCounter) {
if (payloadExists) {
// Duplicate packet found.
tsPacketBuffer.setPosition(endOfPacket);
return RESULT_CONTINUE;
if ((workaroundFlags & WORKAROUND_HLS_MODE) == 0) {
int previousCounter = continuityCounters.get(pid, continuityCounter - 1);
continuityCounters.put(pid, continuityCounter);
if (previousCounter == continuityCounter) {
if (payloadExists) {
// Duplicate packet found.
tsPacketBuffer.setPosition(endOfPacket);
return RESULT_CONTINUE;
}
} else if (continuityCounter != (previousCounter + 1) % 16) {
discontinuityFound = true;
}
} else if (continuityCounter != (previousCounter + 1) % 16) {
discontinuityFound = true;
}

// Skip the adaptation field.
Expand Down Expand Up @@ -410,7 +414,7 @@ public void consume(ParsableByteArray data, boolean payloadUnitStartIndicator,
// Skip the descriptors.
sectionData.skipBytes(programInfoLength);

if ((workaroundFlags & WORKAROUND_MAP_BY_TYPE) != 0 && id3Reader == null) {
if ((workaroundFlags & WORKAROUND_HLS_MODE) != 0 && id3Reader == null) {
// Setup an ID3 track regardless of whether there's a corresponding entry, in case one
// appears intermittently during playback. See b/20261500.
id3Reader = new Id3Reader(output.track(TS_STREAM_TYPE_ID3));
Expand All @@ -432,7 +436,7 @@ public void consume(ParsableByteArray data, boolean payloadUnitStartIndicator,
sectionData.skipBytes(esInfoLength);
}
remainingEntriesLength -= esInfoLength + 5;
int trackId = (workaroundFlags & WORKAROUND_MAP_BY_TYPE) != 0 ? streamType : elementaryPid;
int trackId = (workaroundFlags & WORKAROUND_HLS_MODE) != 0 ? streamType : elementaryPid;
if (trackIds.get(trackId)) {
continue;
}
Expand Down Expand Up @@ -473,7 +477,7 @@ public void consume(ParsableByteArray data, boolean payloadUnitStartIndicator,
new SeiReader(output.track(nextEmbeddedTrackId++)));
break;
case TS_STREAM_TYPE_ID3:
if ((workaroundFlags & WORKAROUND_MAP_BY_TYPE) != 0) {
if ((workaroundFlags & WORKAROUND_HLS_MODE) != 0) {
pesPayloadReader = id3Reader;
} else {
pesPayloadReader = new Id3Reader(output.track(nextEmbeddedTrackId++));
Expand All @@ -490,7 +494,7 @@ public void consume(ParsableByteArray data, boolean payloadUnitStartIndicator,
new PesReader(pesPayloadReader, ptsTimestampAdjuster));
}
}
if ((workaroundFlags & WORKAROUND_MAP_BY_TYPE) != 0) {
if ((workaroundFlags & WORKAROUND_HLS_MODE) != 0) {
if (!tracksEnded) {
output.endTracks();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,8 @@ public void getChunkOperation(TsChunk previousTsChunk, long playbackPositionUs,
// The master source has yet to instantiate an adjuster for the discontinuity sequence.
return;
}
// This flag ensures the change of pid between streams does not affect the sample queues.
int workaroundFlags = TsExtractor.WORKAROUND_MAP_BY_TYPE;
// Enable HLS workarounds for the extractor.
int workaroundFlags = TsExtractor.WORKAROUND_HLS_MODE;
String codecs = format.codecs;
if (!TextUtils.isEmpty(codecs)) {
// Sometimes AAC and H264 streams are declared in TS chunks even though they don't really
Expand Down

0 comments on commit 9812aa7

Please sign in to comment.