Skip to content

Commit

Permalink
Add SampleQueue.peek
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 352779870
  • Loading branch information
AquilesCanta authored and kim-vde committed Jan 22, 2021
1 parent c40d1c6 commit 4eaa611
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,45 +115,25 @@ public void rewind() {
}

/**
* Reads data from the rolling buffer to populate a decoder input buffer.
* Reads data from the rolling buffer to populate a decoder input buffer, and advances the read
* position.
*
* @param buffer The buffer to populate.
* @param extrasHolder The extras holder whose offset should be read and subsequently adjusted.
*/
public void readToBuffer(DecoderInputBuffer buffer, SampleExtrasHolder extrasHolder) {
// Read encryption data if the sample is encrypted.
AllocationNode readAllocationNode = this.readAllocationNode;
if (buffer.isEncrypted()) {
readAllocationNode = readEncryptionData(readAllocationNode, buffer, extrasHolder, scratch);
}
// Read sample data, extracting supplemental data into a separate buffer if needed.
if (buffer.hasSupplementalData()) {
// If there is supplemental data, the sample data is prefixed by its size.
scratch.reset(4);
readAllocationNode = readData(readAllocationNode, extrasHolder.offset, scratch.getData(), 4);
int sampleSize = scratch.readUnsignedIntToInt();
extrasHolder.offset += 4;
extrasHolder.size -= 4;

// Write the sample data.
buffer.ensureSpaceForWrite(sampleSize);
readAllocationNode =
readData(readAllocationNode, extrasHolder.offset, buffer.data, sampleSize);
extrasHolder.offset += sampleSize;
extrasHolder.size -= sampleSize;
readAllocationNode = readSampleData(readAllocationNode, buffer, extrasHolder, scratch);
}

// Write the remaining data as supplemental data.
buffer.resetSupplementalData(extrasHolder.size);
readAllocationNode =
readData(
readAllocationNode, extrasHolder.offset, buffer.supplementalData, extrasHolder.size);
} else {
// Write the sample data.
buffer.ensureSpaceForWrite(extrasHolder.size);
readAllocationNode =
readData(readAllocationNode, extrasHolder.offset, buffer.data, extrasHolder.size);
}
this.readAllocationNode = readAllocationNode;
/**
* Peeks data from the rolling buffer to populate a decoder input buffer, without advancing the
* read position.
*
* @param buffer The buffer to populate.
* @param extrasHolder The extras holder whose offset should be read and subsequently adjusted.
*/
public void peekToBuffer(DecoderInputBuffer buffer, SampleExtrasHolder extrasHolder) {
readSampleData(readAllocationNode, buffer, extrasHolder, scratch);
}

/**
Expand Down Expand Up @@ -270,6 +250,52 @@ private void postAppend(int length) {
}
}

/**
* Reads data from the rolling buffer to populate a decoder input buffer.
*
* @param allocationNode The first {@link AllocationNode} containing data yet to be read.
* @param buffer The buffer to populate.
* @param extrasHolder The extras holder whose offset should be read and subsequently adjusted.
* @param scratch A scratch {@link ParsableByteArray}.
* @return The first {@link AllocationNode} that contains unread bytes after the last byte that
* the invocation read.
*/
private static AllocationNode readSampleData(
AllocationNode allocationNode,
DecoderInputBuffer buffer,
SampleExtrasHolder extrasHolder,
ParsableByteArray scratch) {
if (buffer.isEncrypted()) {
allocationNode = readEncryptionData(allocationNode, buffer, extrasHolder, scratch);
}
// Read sample data, extracting supplemental data into a separate buffer if needed.
if (buffer.hasSupplementalData()) {
// If there is supplemental data, the sample data is prefixed by its size.
scratch.reset(4);
allocationNode = readData(allocationNode, extrasHolder.offset, scratch.getData(), 4);
int sampleSize = scratch.readUnsignedIntToInt();
extrasHolder.offset += 4;
extrasHolder.size -= 4;

// Write the sample data.
buffer.ensureSpaceForWrite(sampleSize);
allocationNode = readData(allocationNode, extrasHolder.offset, buffer.data, sampleSize);
extrasHolder.offset += sampleSize;
extrasHolder.size -= sampleSize;

// Write the remaining data as supplemental data.
buffer.resetSupplementalData(extrasHolder.size);
allocationNode =
readData(allocationNode, extrasHolder.offset, buffer.supplementalData, extrasHolder.size);
} else {
// Write the sample data.
buffer.ensureSpaceForWrite(extrasHolder.size);
allocationNode =
readData(allocationNode, extrasHolder.offset, buffer.data, extrasHolder.size);
}
return allocationNode;
}

/**
* Reads encryption data for the sample described by {@code extrasHolder}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,20 @@ public synchronized boolean isReady(boolean loadingFinished) {
return mayReadSample(relativeReadIndex);
}

/** Equivalent to {@link #read}, except it never advances the read position. */
public final int peek(
FormatHolder formatHolder,
DecoderInputBuffer buffer,
boolean formatRequired,
boolean loadingFinished) {
int result =
peekSampleMetadata(formatHolder, buffer, formatRequired, loadingFinished, extrasHolder);
if (result == C.RESULT_BUFFER_READ && !buffer.isEndOfStream() && !buffer.isFlagsOnly()) {
sampleDataQueue.peekToBuffer(buffer, extrasHolder);
}
return result;
}

/**
* Attempts to read from the queue.
*
Expand Down

0 comments on commit 4eaa611

Please sign in to comment.