Skip to content

Commit

Permalink
Reduce calls from TsExtractor to ExtractorInput.read
Browse files Browse the repository at this point in the history
We currently read at most 5 packets at a time from the
extractor input. Whether this is inefficient depends on
how efficiently the underlying DataSource handles lots
of small reads. It seems likely, however, that DataSource
implementations will in general more efficiently handle
fewer larger reads, and in the case of this extractor
it's trivial to do this.

Notes:
- The change appears to make little difference in my
  testing with DefaultHttpDataSource, although analysis
  in #3040 suggests that it does help.
- This change shouldn't have any negative implications
  (i.e. at worst it should be neutral wrt performance). In
  particular it should not make buffering any more likely,
  because the underlying DataSource should return fewer
  bytes than are being requested in the case that it
  cannot fully satisfy the requested amount.

Issue: #3040

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=162206761
  • Loading branch information
ojw28 committed Jul 17, 2017
1 parent 6041b0f commit 009369b
Showing 1 changed file with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public Extractor[] createExtractors() {
private static final long E_AC3_FORMAT_IDENTIFIER = Util.getIntegerCodeForString("EAC3");
private static final long HEVC_FORMAT_IDENTIFIER = Util.getIntegerCodeForString("HEVC");

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 * 50;
private static final int SNIFF_TS_PACKET_COUNT = 5;

@Mode private final int mode;
private final List<TimestampAdjuster> timestampAdjusters;
Expand Down Expand Up @@ -174,10 +174,10 @@ public TsExtractor(@Mode int mode, TimestampAdjuster timestampAdjuster,
@Override
public boolean sniff(ExtractorInput input) throws IOException, InterruptedException {
byte[] buffer = tsPacketBuffer.data;
input.peekFully(buffer, 0, BUFFER_SIZE);
input.peekFully(buffer, 0, TS_PACKET_SIZE * SNIFF_TS_PACKET_COUNT);
for (int j = 0; j < TS_PACKET_SIZE; j++) {
for (int i = 0; true; i++) {
if (i == BUFFER_PACKET_COUNT) {
if (i == SNIFF_TS_PACKET_COUNT) {
input.skipFully(j);
return true;
}
Expand Down Expand Up @@ -216,15 +216,17 @@ public void release() {
public int read(ExtractorInput input, PositionHolder seekPosition)
throws IOException, InterruptedException {
byte[] data = tsPacketBuffer.data;
// Shift bytes to the start of the buffer if there isn't enough space left at the end

// Shift bytes to the start of the buffer if there isn't enough space left at the end.
if (BUFFER_SIZE - tsPacketBuffer.getPosition() < TS_PACKET_SIZE) {
int bytesLeft = tsPacketBuffer.bytesLeft();
if (bytesLeft > 0) {
System.arraycopy(data, tsPacketBuffer.getPosition(), data, 0, bytesLeft);
}
tsPacketBuffer.reset(data, bytesLeft);
}
// Read more bytes until there is at least one packet size

// Read more bytes until we have at least one packet.
while (tsPacketBuffer.bytesLeft() < TS_PACKET_SIZE) {
int limit = tsPacketBuffer.limit();
int read = input.read(data, limit, BUFFER_SIZE - limit);
Expand All @@ -234,8 +236,7 @@ public int read(ExtractorInput input, PositionHolder seekPosition)
tsPacketBuffer.setLimit(limit + read);
}

// Note: see ISO/IEC 13818-1, section 2.4.3.2 for detailed information on the format of
// the header.
// Note: See ISO/IEC 13818-1, section 2.4.3.2 for details of the header format.
final int limit = tsPacketBuffer.limit();
int position = tsPacketBuffer.getPosition();
while (position < limit && data[position] != TS_SYNC_BYTE) {
Expand Down Expand Up @@ -554,5 +555,4 @@ private EsInfo readEsInfo(ParsableByteArray data, int length) {

}


}

0 comments on commit 009369b

Please sign in to comment.