Commit 0b1a6fe9 by aquilescanta Committed by Oliver Woodman

Ignore discontinuities in TSs for HLS

Issue:#1921
Issue:#1978
Issue:#2163
Issue:#2172

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=141797474
parent 2a79931e
...@@ -78,7 +78,7 @@ public final class TsExtractor implements Extractor { ...@@ -78,7 +78,7 @@ public final class TsExtractor implements Extractor {
private static final int BUFFER_PACKET_COUNT = 5; // Should be at least 2 private static final int BUFFER_PACKET_COUNT = 5; // Should be at least 2
private static final int BUFFER_SIZE = TS_PACKET_SIZE * BUFFER_PACKET_COUNT; private static final int BUFFER_SIZE = TS_PACKET_SIZE * BUFFER_PACKET_COUNT;
private final boolean mapByType; private final boolean hlsMode;
private final TimestampAdjuster timestampAdjuster; private final TimestampAdjuster timestampAdjuster;
private final ParsableByteArray tsPacketBuffer; private final ParsableByteArray tsPacketBuffer;
private final ParsableBitArray tsScratch; private final ParsableBitArray tsScratch;
...@@ -106,14 +106,14 @@ public final class TsExtractor implements Extractor { ...@@ -106,14 +106,14 @@ public final class TsExtractor implements Extractor {
/** /**
* @param timestampAdjuster A timestamp adjuster for offsetting and scaling sample timestamps. * @param timestampAdjuster A timestamp adjuster for offsetting and scaling sample timestamps.
* @param payloadReaderFactory Factory for injecting a custom set of payload readers. * @param payloadReaderFactory Factory for injecting a custom set of payload readers.
* @param mapByType True if {@link TrackOutput}s should be mapped by their type, false to map them * @param hlsMode Whether the extractor should be used in HLS mode. If true, {@link TrackOutput}s
* by their PID. * are mapped by their type (instead of PID) and continuity counters are ignored.
*/ */
public TsExtractor(TimestampAdjuster timestampAdjuster, public TsExtractor(TimestampAdjuster timestampAdjuster,
TsPayloadReader.Factory payloadReaderFactory, boolean mapByType) { TsPayloadReader.Factory payloadReaderFactory, boolean hlsMode) {
this.timestampAdjuster = timestampAdjuster; this.timestampAdjuster = timestampAdjuster;
this.payloadReaderFactory = Assertions.checkNotNull(payloadReaderFactory); this.payloadReaderFactory = Assertions.checkNotNull(payloadReaderFactory);
this.mapByType = mapByType; this.hlsMode = hlsMode;
tsPacketBuffer = new ParsableByteArray(BUFFER_SIZE); tsPacketBuffer = new ParsableByteArray(BUFFER_SIZE);
tsScratch = new ParsableBitArray(new byte[3]); tsScratch = new ParsableBitArray(new byte[3]);
trackIds = new SparseBooleanArray(); trackIds = new SparseBooleanArray();
...@@ -211,18 +211,22 @@ public final class TsExtractor implements Extractor { ...@@ -211,18 +211,22 @@ public final class TsExtractor implements Extractor {
tsScratch.skipBits(2); // transport_scrambling_control tsScratch.skipBits(2); // transport_scrambling_control
boolean adaptationFieldExists = tsScratch.readBit(); boolean adaptationFieldExists = tsScratch.readBit();
boolean payloadExists = tsScratch.readBit(); boolean payloadExists = tsScratch.readBit();
// Discontinuity check.
boolean discontinuityFound = false; boolean discontinuityFound = false;
int continuityCounter = tsScratch.readBits(4); int continuityCounter = tsScratch.readBits(4);
int previousCounter = continuityCounters.get(pid, continuityCounter - 1); if (!hlsMode) {
continuityCounters.put(pid, continuityCounter); int previousCounter = continuityCounters.get(pid, continuityCounter - 1);
if (previousCounter == continuityCounter) { continuityCounters.put(pid, continuityCounter);
if (payloadExists) { if (previousCounter == continuityCounter) {
// Duplicate packet found. if (payloadExists) {
tsPacketBuffer.setPosition(endOfPacket); // Duplicate packet found.
return RESULT_CONTINUE; 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. // Skip the adaptation field.
...@@ -354,7 +358,7 @@ public final class TsExtractor implements Extractor { ...@@ -354,7 +358,7 @@ public final class TsExtractor implements Extractor {
// Skip the descriptors. // Skip the descriptors.
sectionData.skipBytes(programInfoLength); sectionData.skipBytes(programInfoLength);
if (mapByType && id3Reader == null) { if (hlsMode && id3Reader == null) {
// Setup an ID3 track regardless of whether there's a corresponding entry, in case one // Setup an ID3 track regardless of whether there's a corresponding entry, in case one
// appears intermittently during playback. See [Internal: b/20261500]. // appears intermittently during playback. See [Internal: b/20261500].
EsInfo dummyEsInfo = new EsInfo(TS_STREAM_TYPE_ID3, null, new byte[0]); EsInfo dummyEsInfo = new EsInfo(TS_STREAM_TYPE_ID3, null, new byte[0]);
...@@ -377,14 +381,14 @@ public final class TsExtractor implements Extractor { ...@@ -377,14 +381,14 @@ public final class TsExtractor implements Extractor {
} }
remainingEntriesLength -= esInfoLength + 5; remainingEntriesLength -= esInfoLength + 5;
int trackId = mapByType ? streamType : elementaryPid; int trackId = hlsMode ? streamType : elementaryPid;
if (trackIds.get(trackId)) { if (trackIds.get(trackId)) {
continue; continue;
} }
trackIds.put(trackId, true); trackIds.put(trackId, true);
TsPayloadReader reader; TsPayloadReader reader;
if (mapByType && streamType == TS_STREAM_TYPE_ID3) { if (hlsMode && streamType == TS_STREAM_TYPE_ID3) {
reader = id3Reader; reader = id3Reader;
} else { } else {
reader = payloadReaderFactory.createPayloadReader(streamType, esInfo); reader = payloadReaderFactory.createPayloadReader(streamType, esInfo);
...@@ -397,7 +401,7 @@ public final class TsExtractor implements Extractor { ...@@ -397,7 +401,7 @@ public final class TsExtractor implements Extractor {
tsPayloadReaders.put(elementaryPid, reader); tsPayloadReaders.put(elementaryPid, reader);
} }
} }
if (mapByType) { if (hlsMode) {
if (!tracksEnded) { if (!tracksEnded) {
output.endTracks(); output.endTracks();
} }
......
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