Constraint seek targetGranule within bounds + simplify tests
PiperOrigin-RevId: 261328701
This commit is contained in:
parent
173eadc70e
commit
f179feb292
@ -29,8 +29,8 @@ import java.io.IOException;
|
|||||||
/** Seeks in an Ogg stream. */
|
/** Seeks in an Ogg stream. */
|
||||||
/* package */ final class DefaultOggSeeker implements OggSeeker {
|
/* package */ final class DefaultOggSeeker implements OggSeeker {
|
||||||
|
|
||||||
@VisibleForTesting public static final int MATCH_RANGE = 72000;
|
private static final int MATCH_RANGE = 72000;
|
||||||
@VisibleForTesting public static final int MATCH_BYTE_RANGE = 100000;
|
private static final int MATCH_BYTE_RANGE = 100000;
|
||||||
private static final int DEFAULT_OFFSET = 30000;
|
private static final int DEFAULT_OFFSET = 30000;
|
||||||
|
|
||||||
private static final int STATE_SEEK_TO_END = 0;
|
private static final int STATE_SEEK_TO_END = 0;
|
||||||
@ -127,7 +127,7 @@ import java.io.IOException;
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void startSeek(long targetGranule) {
|
public void startSeek(long targetGranule) {
|
||||||
this.targetGranule = targetGranule;
|
this.targetGranule = Util.constrainValue(targetGranule, 0, totalGranules - 1);
|
||||||
state = STATE_SEEK;
|
state = STATE_SEEK;
|
||||||
start = payloadStartPosition;
|
start = payloadStartPosition;
|
||||||
end = payloadEndPosition;
|
end = payloadEndPosition;
|
||||||
@ -201,7 +201,7 @@ import java.io.IOException;
|
|||||||
private void skipToPageOfTargetGranule(ExtractorInput input)
|
private void skipToPageOfTargetGranule(ExtractorInput input)
|
||||||
throws IOException, InterruptedException {
|
throws IOException, InterruptedException {
|
||||||
pageHeader.populate(input, /* quiet= */ false);
|
pageHeader.populate(input, /* quiet= */ false);
|
||||||
while (pageHeader.granulePosition < targetGranule) {
|
while (pageHeader.granulePosition <= targetGranule) {
|
||||||
input.skipFully(pageHeader.headerSize + pageHeader.bodySize);
|
input.skipFully(pageHeader.headerSize + pageHeader.bodySize);
|
||||||
start = input.getPosition();
|
start = input.getPosition();
|
||||||
startGranule = pageHeader.granulePosition;
|
startGranule = pageHeader.granulePosition;
|
||||||
|
@ -160,7 +160,7 @@ public final class DefaultOggSeekerTest {
|
|||||||
/* payloadStartPosition= */ 0,
|
/* payloadStartPosition= */ 0,
|
||||||
/* payloadEndPosition= */ testFile.data.length,
|
/* payloadEndPosition= */ testFile.data.length,
|
||||||
/* firstPayloadPageSize= */ testFile.firstPayloadPageSize,
|
/* firstPayloadPageSize= */ testFile.firstPayloadPageSize,
|
||||||
/* firstPayloadPageGranulePosition= */ testFile.firstPayloadPageGranulePosition,
|
/* firstPayloadPageGranulePosition= */ testFile.firstPayloadPageGranuleCount,
|
||||||
/* firstPayloadPageIsLastPage= */ false);
|
/* firstPayloadPageIsLastPage= */ false);
|
||||||
OggPageHeader pageHeader = new OggPageHeader();
|
OggPageHeader pageHeader = new OggPageHeader();
|
||||||
|
|
||||||
@ -183,28 +183,12 @@ public final class DefaultOggSeekerTest {
|
|||||||
assertThat(input.getPosition()).isEqualTo(0);
|
assertThat(input.getPosition()).isEqualTo(0);
|
||||||
|
|
||||||
// Test last granule.
|
// Test last granule.
|
||||||
granule = seekTo(input, oggSeeker, testFile.lastGranule, 0);
|
granule = seekTo(input, oggSeeker, testFile.granuleCount - 1, 0);
|
||||||
long position = testFile.data.length;
|
assertThat(granule).isEqualTo(testFile.granuleCount - testFile.lastPayloadPageGranuleCount);
|
||||||
// TODO: Simplify this.
|
assertThat(input.getPosition()).isEqualTo(testFile.data.length - testFile.lastPayloadPageSize);
|
||||||
assertThat(
|
|
||||||
(testFile.lastGranule > granule && position > input.getPosition())
|
|
||||||
|| (testFile.lastGranule == granule && position == input.getPosition()))
|
|
||||||
.isTrue();
|
|
||||||
|
|
||||||
// Test exact granule.
|
|
||||||
input.setPosition(testFile.data.length / 2);
|
|
||||||
oggSeeker.skipToNextPage(input);
|
|
||||||
assertThat(pageHeader.populate(input, true)).isTrue();
|
|
||||||
position = input.getPosition() + pageHeader.headerSize + pageHeader.bodySize;
|
|
||||||
granule = seekTo(input, oggSeeker, pageHeader.granulePosition, 0);
|
|
||||||
// TODO: Simplify this.
|
|
||||||
assertThat(
|
|
||||||
(pageHeader.granulePosition > granule && position > input.getPosition())
|
|
||||||
|| (pageHeader.granulePosition == granule && position == input.getPosition()))
|
|
||||||
.isTrue();
|
|
||||||
|
|
||||||
for (int i = 0; i < 100; i += 1) {
|
for (int i = 0; i < 100; i += 1) {
|
||||||
long targetGranule = (long) (random.nextDouble() * testFile.lastGranule);
|
long targetGranule = random.nextInt(testFile.granuleCount);
|
||||||
int initialPosition = random.nextInt(testFile.data.length);
|
int initialPosition = random.nextInt(testFile.data.length);
|
||||||
granule = seekTo(input, oggSeeker, targetGranule, initialPosition);
|
granule = seekTo(input, oggSeeker, targetGranule, initialPosition);
|
||||||
long currentPosition = input.getPosition();
|
long currentPosition = input.getPosition();
|
||||||
|
@ -30,35 +30,39 @@ import java.util.Random;
|
|||||||
private static final int MAX_GRANULES_IN_PAGE = 100000;
|
private static final int MAX_GRANULES_IN_PAGE = 100000;
|
||||||
|
|
||||||
public final byte[] data;
|
public final byte[] data;
|
||||||
public final long lastGranule;
|
public final int granuleCount;
|
||||||
public final int packetCount;
|
|
||||||
public final int pageCount;
|
public final int pageCount;
|
||||||
public final int firstPayloadPageSize;
|
public final int firstPayloadPageSize;
|
||||||
public final long firstPayloadPageGranulePosition;
|
public final int firstPayloadPageGranuleCount;
|
||||||
|
public final int lastPayloadPageSize;
|
||||||
|
public final int lastPayloadPageGranuleCount;
|
||||||
|
|
||||||
private OggTestFile(
|
private OggTestFile(
|
||||||
byte[] data,
|
byte[] data,
|
||||||
long lastGranule,
|
int granuleCount,
|
||||||
int packetCount,
|
|
||||||
int pageCount,
|
int pageCount,
|
||||||
int firstPayloadPageSize,
|
int firstPayloadPageSize,
|
||||||
long firstPayloadPageGranulePosition) {
|
int firstPayloadPageGranuleCount,
|
||||||
|
int lastPayloadPageSize,
|
||||||
|
int lastPayloadPageGranuleCount) {
|
||||||
this.data = data;
|
this.data = data;
|
||||||
this.lastGranule = lastGranule;
|
this.granuleCount = granuleCount;
|
||||||
this.packetCount = packetCount;
|
|
||||||
this.pageCount = pageCount;
|
this.pageCount = pageCount;
|
||||||
this.firstPayloadPageSize = firstPayloadPageSize;
|
this.firstPayloadPageSize = firstPayloadPageSize;
|
||||||
this.firstPayloadPageGranulePosition = firstPayloadPageGranulePosition;
|
this.firstPayloadPageGranuleCount = firstPayloadPageGranuleCount;
|
||||||
|
this.lastPayloadPageSize = lastPayloadPageSize;
|
||||||
|
this.lastPayloadPageGranuleCount = lastPayloadPageGranuleCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static OggTestFile generate(Random random, int pageCount) {
|
public static OggTestFile generate(Random random, int pageCount) {
|
||||||
ArrayList<byte[]> fileData = new ArrayList<>();
|
ArrayList<byte[]> fileData = new ArrayList<>();
|
||||||
int fileSize = 0;
|
int fileSize = 0;
|
||||||
long granule = 0;
|
int granuleCount = 0;
|
||||||
int packetLength = -1;
|
|
||||||
int packetCount = 0;
|
|
||||||
int firstPayloadPageSize = 0;
|
int firstPayloadPageSize = 0;
|
||||||
long firstPayloadPageGranulePosition = 0;
|
int firstPayloadPageGranuleCount = 0;
|
||||||
|
int lastPageloadPageSize = 0;
|
||||||
|
int lastPayloadPageGranuleCount = 0;
|
||||||
|
int packetLength = -1;
|
||||||
|
|
||||||
for (int i = 0; i < pageCount; i++) {
|
for (int i = 0; i < pageCount; i++) {
|
||||||
int headerType = 0x00;
|
int headerType = 0x00;
|
||||||
@ -71,17 +75,17 @@ import java.util.Random;
|
|||||||
if (i == pageCount - 1) {
|
if (i == pageCount - 1) {
|
||||||
headerType |= 4;
|
headerType |= 4;
|
||||||
}
|
}
|
||||||
granule += random.nextInt(MAX_GRANULES_IN_PAGE - 1) + 1;
|
int pageGranuleCount = random.nextInt(MAX_GRANULES_IN_PAGE - 1) + 1;
|
||||||
int pageSegmentCount = random.nextInt(MAX_SEGMENT_COUNT);
|
int pageSegmentCount = random.nextInt(MAX_SEGMENT_COUNT);
|
||||||
byte[] header = OggTestData.buildOggHeader(headerType, granule, 0, pageSegmentCount);
|
granuleCount += pageGranuleCount;
|
||||||
|
byte[] header = OggTestData.buildOggHeader(headerType, granuleCount, 0, pageSegmentCount);
|
||||||
fileData.add(header);
|
fileData.add(header);
|
||||||
fileSize += header.length;
|
int pageSize = header.length;
|
||||||
|
|
||||||
byte[] laces = new byte[pageSegmentCount];
|
byte[] laces = new byte[pageSegmentCount];
|
||||||
int bodySize = 0;
|
int bodySize = 0;
|
||||||
for (int j = 0; j < pageSegmentCount; j++) {
|
for (int j = 0; j < pageSegmentCount; j++) {
|
||||||
if (packetLength < 0) {
|
if (packetLength < 0) {
|
||||||
packetCount++;
|
|
||||||
if (i < pageCount - 1) {
|
if (i < pageCount - 1) {
|
||||||
packetLength = random.nextInt(MAX_PACKET_LENGTH);
|
packetLength = random.nextInt(MAX_PACKET_LENGTH);
|
||||||
} else {
|
} else {
|
||||||
@ -96,14 +100,19 @@ import java.util.Random;
|
|||||||
packetLength -= 255;
|
packetLength -= 255;
|
||||||
}
|
}
|
||||||
fileData.add(laces);
|
fileData.add(laces);
|
||||||
fileSize += laces.length;
|
pageSize += laces.length;
|
||||||
|
|
||||||
byte[] payload = TestUtil.buildTestData(bodySize, random);
|
byte[] payload = TestUtil.buildTestData(bodySize, random);
|
||||||
fileData.add(payload);
|
fileData.add(payload);
|
||||||
fileSize += payload.length;
|
pageSize += payload.length;
|
||||||
|
|
||||||
|
fileSize += pageSize;
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
firstPayloadPageSize = header.length + bodySize;
|
firstPayloadPageSize = pageSize;
|
||||||
firstPayloadPageGranulePosition = granule;
|
firstPayloadPageGranuleCount = pageGranuleCount;
|
||||||
|
} else if (i == pageCount - 1) {
|
||||||
|
lastPageloadPageSize = pageSize;
|
||||||
|
lastPayloadPageGranuleCount = pageGranuleCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,11 +124,12 @@ import java.util.Random;
|
|||||||
}
|
}
|
||||||
return new OggTestFile(
|
return new OggTestFile(
|
||||||
file,
|
file,
|
||||||
granule,
|
granuleCount,
|
||||||
packetCount,
|
|
||||||
pageCount,
|
pageCount,
|
||||||
firstPayloadPageSize,
|
firstPayloadPageSize,
|
||||||
firstPayloadPageGranulePosition);
|
firstPayloadPageGranuleCount,
|
||||||
|
lastPageloadPageSize,
|
||||||
|
lastPayloadPageGranuleCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int findPreviousPageStart(long position) {
|
public int findPreviousPageStart(long position) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user