Reduce calls from TsExtractor to ExtractorInput.read

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
This commit is contained in:
olly 2017-07-17 06:57:10 -07:00 committed by Oliver Woodman
parent 6041b0fe9c
commit 009369bf94

View File

@ -105,8 +105,8 @@ public final class TsExtractor implements Extractor {
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;
@ -174,10 +174,10 @@ public final class TsExtractor implements Extractor {
@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;
}
@ -216,7 +216,8 @@ public final class TsExtractor implements Extractor {
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) {
@ -224,7 +225,8 @@ public final class TsExtractor implements Extractor {
}
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);
@ -234,8 +236,7 @@ public final class TsExtractor implements Extractor {
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) {
@ -554,5 +555,4 @@ public final class TsExtractor implements Extractor {
}
}