Skip to content

Commit

Permalink
Document and remove check for C.TIME_UNSET when consuming data in TS
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 595647795
  • Loading branch information
rohitjoins authored and copybara-github committed Jan 4, 2024
1 parent c230414 commit 5970d2d
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package androidx.media3.extractor;

import static androidx.media3.common.util.Assertions.checkState;

import androidx.media3.common.C;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.ParsableByteArray;
Expand Down Expand Up @@ -104,14 +106,13 @@ public static void consumeCcData(
for (TrackOutput output : outputs) {
ccDataBuffer.setPosition(sampleStartPosition);
output.sampleData(ccDataBuffer, sampleLength);
if (presentationTimeUs != C.TIME_UNSET) {
output.sampleMetadata(
presentationTimeUs,
C.BUFFER_FLAG_KEY_FRAME,
sampleLength,
/* offset= */ 0,
/* cryptoData= */ null);
}
checkState(presentationTimeUs != C.TIME_UNSET);
output.sampleMetadata(
presentationTimeUs,
C.BUFFER_FLAG_KEY_FRAME,
sampleLength,
/* offset= */ 0,
/* cryptoData= */ null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package androidx.media3.extractor.ts;

import static androidx.media3.common.util.Assertions.checkState;
import static java.lang.Math.min;
import static java.lang.annotation.ElementType.TYPE_USE;

Expand Down Expand Up @@ -112,9 +113,7 @@ public void createTracks(ExtractorOutput extractorOutput, TrackIdGenerator idGen

@Override
public void packetStarted(long pesTimeUs, @TsPayloadReader.Flags int flags) {
if (pesTimeUs != C.TIME_UNSET) {
timeUs = pesTimeUs;
}
timeUs = pesTimeUs;
}

@Override
Expand Down Expand Up @@ -143,10 +142,10 @@ public void consume(ParsableByteArray data) {
output.sampleData(data, bytesToRead);
bytesRead += bytesToRead;
if (bytesRead == sampleSize) {
if (timeUs != C.TIME_UNSET) {
output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
timeUs += sampleDurationUs;
}
// packetStarted method must be called before reading samples.
checkState(timeUs != C.TIME_UNSET);
output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
timeUs += sampleDurationUs;
state = STATE_FINDING_SYNC;
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package androidx.media3.extractor.ts;

import static androidx.media3.common.util.Assertions.checkState;
import static java.lang.Math.min;
import static java.lang.annotation.ElementType.TYPE_USE;

Expand Down Expand Up @@ -114,9 +115,7 @@ public void createTracks(ExtractorOutput extractorOutput, TrackIdGenerator idGen

@Override
public void packetStarted(long pesTimeUs, @TsPayloadReader.Flags int flags) {
if (pesTimeUs != C.TIME_UNSET) {
timeUs = pesTimeUs;
}
timeUs = pesTimeUs;
}

@Override
Expand Down Expand Up @@ -145,10 +144,10 @@ public void consume(ParsableByteArray data) {
output.sampleData(data, bytesToRead);
bytesRead += bytesToRead;
if (bytesRead == sampleSize) {
if (timeUs != C.TIME_UNSET) {
output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
timeUs += sampleDurationUs;
}
// packetStarted method must be called before reading samples.
checkState(timeUs != C.TIME_UNSET);
output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
timeUs += sampleDurationUs;
state = STATE_FINDING_SYNC;
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package androidx.media3.extractor.ts;

import static androidx.media3.common.util.Assertions.checkState;
import static java.lang.Math.min;

import androidx.annotation.Nullable;
Expand Down Expand Up @@ -155,9 +156,7 @@ public void createTracks(ExtractorOutput extractorOutput, TrackIdGenerator idGen

@Override
public void packetStarted(long pesTimeUs, @TsPayloadReader.Flags int flags) {
if (pesTimeUs != C.TIME_UNSET) {
timeUs = pesTimeUs;
}
timeUs = pesTimeUs;
}

@Override
Expand Down Expand Up @@ -537,10 +536,10 @@ private void readSample(ParsableByteArray data) {
currentOutput.sampleData(data, bytesToRead);
bytesRead += bytesToRead;
if (bytesRead == sampleSize) {
if (timeUs != C.TIME_UNSET) {
currentOutput.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
timeUs += currentSampleDuration;
}
// packetStarted method must be called before reading samples.
checkState(timeUs != C.TIME_UNSET);
currentOutput.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
timeUs += currentSampleDuration;
setFindingSampleState();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package androidx.media3.extractor.ts;

import static androidx.media3.common.util.Assertions.checkState;
import static java.lang.Math.min;

import androidx.annotation.Nullable;
Expand Down Expand Up @@ -89,9 +90,7 @@ public void createTracks(ExtractorOutput extractorOutput, TrackIdGenerator idGen

@Override
public void packetStarted(long pesTimeUs, @TsPayloadReader.Flags int flags) {
if (pesTimeUs != C.TIME_UNSET) {
timeUs = pesTimeUs;
}
timeUs = pesTimeUs;
}

@Override
Expand All @@ -117,10 +116,10 @@ public void consume(ParsableByteArray data) {
output.sampleData(data, bytesToRead);
bytesRead += bytesToRead;
if (bytesRead == sampleSize) {
if (timeUs != C.TIME_UNSET) {
output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
timeUs += sampleDurationUs;
}
// packetStarted method must be called before consuming samples.
checkState(timeUs != C.TIME_UNSET);
output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
timeUs += sampleDurationUs;
state = STATE_FINDING_SYNC;
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package androidx.media3.extractor.ts;

import static androidx.media3.common.util.Assertions.checkState;
import static androidx.media3.extractor.ts.TsPayloadReader.FLAG_DATA_ALIGNMENT_INDICATOR;

import androidx.media3.common.C;
Expand Down Expand Up @@ -79,20 +80,18 @@ public void packetStarted(long pesTimeUs, @TsPayloadReader.Flags int flags) {
return;
}
writingSample = true;
if (pesTimeUs != C.TIME_UNSET) {
sampleTimeUs = pesTimeUs;
}
sampleTimeUs = pesTimeUs;
sampleBytesWritten = 0;
bytesToCheck = 2;
}

@Override
public void packetFinished(boolean isEndOfInput) {
if (writingSample) {
if (sampleTimeUs != C.TIME_UNSET) {
for (TrackOutput output : outputs) {
output.sampleMetadata(sampleTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleBytesWritten, 0, null);
}
// packetStarted method must be called before reading sample.
checkState(sampleTimeUs != C.TIME_UNSET);
for (TrackOutput output : outputs) {
output.sampleMetadata(sampleTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleBytesWritten, 0, null);
}
writingSample = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,20 @@
import androidx.media3.extractor.ExtractorOutput;
import androidx.media3.extractor.TrackOutput;

/** Extracts individual samples from an elementary media stream, preserving original order. */
/**
* Extracts individual samples from an elementary media stream, preserving original order.
*
* <p>The expected sequence of method calls is as follows:
*
* <ol>
* <li>{@link #createTracks(ExtractorOutput, PesReader.TrackIdGenerator)} (once at initialization)
* <li>{@link #seek()} (optional, to reset the state)
* <li>{@link #packetStarted(long, int)} (to signal the start of a new packet)
* <li>{@link #consume(ParsableByteArray)} (zero or more times, to provide packet data)
* <li>{@link #packetFinished(boolean)} (to signal the end of the current packet)
* <li>Repeat steps 3-5 for subsequent packets
* </ol>
*/
@UnstableApi
public interface ElementaryStreamReader {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package androidx.media3.extractor.ts;

import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Assertions.checkState;
import static androidx.media3.common.util.Assertions.checkStateNotNull;
import static androidx.media3.common.util.Util.castNonNull;
import static java.lang.annotation.ElementType.TYPE_USE;
Expand Down Expand Up @@ -130,9 +131,7 @@ public void createTracks(ExtractorOutput extractorOutput, TrackIdGenerator idGen
@Override
public void packetStarted(long pesTimeUs, @TsPayloadReader.Flags int flags) {
// TODO (Internal b/32267012): Consider using random access indicator.
if (pesTimeUs != C.TIME_UNSET) {
this.pesTimeUs = pesTimeUs;
}
this.pesTimeUs = pesTimeUs;
}

@Override
Expand Down Expand Up @@ -479,10 +478,9 @@ public void onData(byte[] data, int offset, int limit) {
}

public void onDataEnd(long position, int bytesWrittenPastPosition, boolean hasOutputFormat) {
if (startCodeValue == START_CODE_VALUE_VOP
&& hasOutputFormat
&& readingSample
&& sampleTimeUs != C.TIME_UNSET) {
// packetStarted method must be called before reading sample.
checkState(sampleTimeUs != C.TIME_UNSET);
if (startCodeValue == START_CODE_VALUE_VOP && hasOutputFormat && readingSample) {
int size = (int) (position - samplePosition);
@C.BufferFlags int flags = sampleIsKeyframe ? C.BUFFER_FLAG_KEY_FRAME : 0;
output.sampleMetadata(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ public void createTracks(ExtractorOutput extractorOutput, TrackIdGenerator idGen

@Override
public void packetStarted(long pesTimeUs, @TsPayloadReader.Flags int flags) {
if (pesTimeUs != C.TIME_UNSET) {
this.pesTimeUs = pesTimeUs;
}
this.pesTimeUs = pesTimeUs;
randomAccessIndicator |= (flags & FLAG_RANDOM_ACCESS_INDICATOR) != 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ public void createTracks(ExtractorOutput extractorOutput, TrackIdGenerator idGen
@Override
public void packetStarted(long pesTimeUs, @TsPayloadReader.Flags int flags) {
// TODO (Internal b/32267012): Consider using random access indicator.
if (pesTimeUs != C.TIME_UNSET) {
this.pesTimeUs = pesTimeUs;
}
this.pesTimeUs = pesTimeUs;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package androidx.media3.extractor.ts;

import static androidx.media3.common.util.Assertions.checkState;
import static androidx.media3.extractor.metadata.id3.Id3Decoder.ID3_HEADER_LENGTH;
import static androidx.media3.extractor.ts.TsPayloadReader.FLAG_DATA_ALIGNMENT_INDICATOR;
import static java.lang.Math.min;
Expand Down Expand Up @@ -77,9 +78,7 @@ public void packetStarted(long pesTimeUs, @TsPayloadReader.Flags int flags) {
return;
}
writingSample = true;
if (pesTimeUs != C.TIME_UNSET) {
sampleTimeUs = pesTimeUs;
}
sampleTimeUs = pesTimeUs;
sampleSize = 0;
sampleBytesRead = 0;
}
Expand Down Expand Up @@ -126,9 +125,9 @@ public void packetFinished(boolean isEndOfInput) {
if (!writingSample || sampleSize == 0 || sampleBytesRead != sampleSize) {
return;
}
if (sampleTimeUs != C.TIME_UNSET) {
output.sampleMetadata(sampleTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
}
// packetStarted method must be called before consuming samples.
checkState(sampleTimeUs != C.TIME_UNSET);
output.sampleMetadata(sampleTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
writingSample = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package androidx.media3.extractor.ts;

import static androidx.media3.common.util.Assertions.checkState;
import static java.lang.Math.min;

import androidx.annotation.Nullable;
Expand Down Expand Up @@ -101,9 +102,7 @@ public void createTracks(ExtractorOutput extractorOutput, TrackIdGenerator idGen

@Override
public void packetStarted(long pesTimeUs, @TsPayloadReader.Flags int flags) {
if (pesTimeUs != C.TIME_UNSET) {
timeUs = pesTimeUs;
}
timeUs = pesTimeUs;
}

@Override
Expand Down Expand Up @@ -314,10 +313,10 @@ private void parsePayloadMux(ParsableBitArray data, int muxLengthBytes) {
sampleDataBuffer.setPosition(0);
}
output.sampleData(sampleDataBuffer, muxLengthBytes);
if (timeUs != C.TIME_UNSET) {
output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, muxLengthBytes, 0, null);
timeUs += sampleDurationUs;
}
// packetStarted method must be called before consuming samples.
checkState(timeUs != C.TIME_UNSET);
output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, muxLengthBytes, 0, null);
timeUs += sampleDurationUs;
}

private void resetBufferForSize(int newSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package androidx.media3.extractor.ts;

import static androidx.media3.common.util.Assertions.checkState;
import static java.lang.Math.min;

import androidx.annotation.Nullable;
Expand Down Expand Up @@ -92,9 +93,7 @@ public void createTracks(ExtractorOutput extractorOutput, TrackIdGenerator idGen

@Override
public void packetStarted(long pesTimeUs, @TsPayloadReader.Flags int flags) {
if (pesTimeUs != C.TIME_UNSET) {
timeUs = pesTimeUs;
}
timeUs = pesTimeUs;
}

@Override
Expand Down Expand Up @@ -233,10 +232,10 @@ private void readFrameRemainder(ParsableByteArray source) {
return;
}

if (timeUs != C.TIME_UNSET) {
output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, frameSize, 0, null);
timeUs += frameDurationUs;
}
// packetStarted method must be called before consuming samples.
checkState(timeUs != C.TIME_UNSET);
output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, frameSize, 0, null);
timeUs += frameDurationUs;
frameBytesRead = 0;
state = STATE_FINDING_HEADER;
}
Expand Down

0 comments on commit 5970d2d

Please sign in to comment.