From 009369bf94e9202f7b2500df84ea8f0f853f0005 Mon Sep 17 00:00:00 2001 From: olly Date: Mon, 17 Jul 2017 06:57:10 -0700 Subject: [PATCH] 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 --- .../exoplayer2/extractor/ts/TsExtractor.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/extractor/ts/TsExtractor.java b/library/core/src/main/java/com/google/android/exoplayer2/extractor/ts/TsExtractor.java index 1149856649..2929b8a076 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/extractor/ts/TsExtractor.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/extractor/ts/TsExtractor.java @@ -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 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 { } - }