public class SampleQueue extends java.lang.Object implements TrackOutput
| Modifier and Type | Class and Description |
|---|---|
static interface |
SampleQueue.UpstreamFormatChangedListener
A listener for changes to the upstream format.
|
TrackOutput.CryptoData| Modifier and Type | Field and Description |
|---|---|
static int |
ADVANCE_FAILED |
| Constructor and Description |
|---|
SampleQueue(Allocator allocator) |
| Modifier and Type | Method and Description |
|---|---|
int |
advanceTo(long timeUs,
boolean toKeyframe,
boolean allowTimeBeyondBuffer)
Attempts to advance the read position to the sample before or at the specified time.
|
int |
advanceToEnd()
Advances the read position to the end of the queue.
|
void |
discardTo(long timeUs,
boolean toKeyframe,
boolean stopAtReadPosition)
Discards up to but not including the sample immediately before or at the specified time.
|
void |
discardToEnd()
Discards to the end of the queue.
|
void |
discardToRead()
Discards up to but not including the read position.
|
void |
discardUpstreamSamples(int discardFromIndex)
Discards samples from the write side of the queue.
|
void |
format(Format format)
Called when the
Format of the track has been extracted from the stream. |
int |
getFirstIndex()
Returns the absolute index of the first sample.
|
long |
getFirstTimestampUs()
Returns the timestamp of the first sample, or
Long.MIN_VALUE if the queue is empty. |
long |
getLargestQueuedTimestampUs()
Returns the largest sample timestamp that has been queued since the last
reset(). |
int |
getReadIndex()
Returns the current absolute read index.
|
Format |
getUpstreamFormat()
Returns the upstream
Format in which samples are being queued. |
int |
getWriteIndex()
Returns the current absolute write index.
|
boolean |
hasNextSample()
Returns whether a sample is available to be read.
|
int |
peekSourceId()
Peeks the source id of the next sample to be read, or the current upstream source id if the
queue is empty or if the read position is at the end of the queue.
|
int |
read(FormatHolder formatHolder,
DecoderInputBuffer buffer,
boolean formatRequired,
boolean loadingFinished,
long decodeOnlyUntilUs)
Attempts to read from the queue.
|
void |
reset()
Resets the output without clearing the upstream format.
|
void |
reset(boolean resetUpstreamFormat)
Resets the output.
|
void |
rewind()
Rewinds the read position to the first sample in the queue.
|
int |
sampleData(ExtractorInput input,
int length,
boolean allowEndOfInput)
Called to write sample data to the output.
|
void |
sampleData(ParsableByteArray buffer,
int length)
Called to write sample data to the output.
|
void |
sampleMetadata(long timeUs,
int flags,
int size,
int offset,
TrackOutput.CryptoData cryptoData)
Called when metadata associated with a sample has been extracted from the stream.
|
boolean |
setReadPosition(int sampleIndex)
Attempts to set the read position to the specified sample index.
|
void |
setSampleOffsetUs(long sampleOffsetUs)
Sets an offset that will be added to the timestamps (and sub-sample timestamps) of samples
that are subsequently queued.
|
void |
setUpstreamFormatChangeListener(SampleQueue.UpstreamFormatChangedListener listener)
Sets a listener to be notified of changes to the upstream format.
|
void |
sourceId(int sourceId)
Sets a source identifier for subsequent samples.
|
void |
splice()
Indicates samples that are subsequently queued should be spliced into those already queued.
|
public static final int ADVANCE_FAILED
public void reset()
reset(false).public void reset(boolean resetUpstreamFormat)
resetUpstreamFormat - Whether the upstream format should be cleared. If set to false,
samples queued after the reset (and before a subsequent call to format(Format))
are assumed to have the current upstream format. If set to true, format(Format)
must be called after the reset before any more samples can be queued.public void sourceId(int sourceId)
sourceId - The source identifier.public void splice()
public int getWriteIndex()
public void discardUpstreamSamples(int discardFromIndex)
discardFromIndex - The absolute index of the first sample to be discarded. Must be in the
range [getReadIndex(), getWriteIndex()].public boolean hasNextSample()
public int getFirstIndex()
public int getReadIndex()
public int peekSourceId()
public Format getUpstreamFormat()
Format in which samples are being queued.public long getLargestQueuedTimestampUs()
reset().
Samples that were discarded by calling discardUpstreamSamples(int) are not
considered as having been queued. Samples that were dequeued from the front of the queue are
considered as having been queued.
Long.MIN_VALUE if no
samples have been queued.public long getFirstTimestampUs()
Long.MIN_VALUE if the queue is empty.public void rewind()
public void discardTo(long timeUs,
boolean toKeyframe,
boolean stopAtReadPosition)
timeUs - The time to discard to.toKeyframe - If true then discards samples up to the keyframe before or at the specified
time, rather than any sample before or at that time.stopAtReadPosition - If true then samples are only discarded if they're before the
read position. If false then samples at and beyond the read position may be discarded, in
which case the read position is advanced to the first remaining sample.public void discardToRead()
public void discardToEnd()
public int advanceToEnd()
public int advanceTo(long timeUs,
boolean toKeyframe,
boolean allowTimeBeyondBuffer)
timeUs - The time to advance to.toKeyframe - If true then attempts to advance to the keyframe before or at the specified
time, rather than to any sample before or at that time.allowTimeBeyondBuffer - Whether the operation can succeed if timeUs is beyond the
end of the queue, by advancing the read position to the last sample (or keyframe).ADVANCE_FAILED if the operation was not successful. A successful
advance is one in which the read position was unchanged or advanced, and is now at a sample
meeting the specified criteria.public boolean setReadPosition(int sampleIndex)
sampleIndex - The sample index.public int read(FormatHolder formatHolder, DecoderInputBuffer buffer, boolean formatRequired, boolean loadingFinished, long decodeOnlyUntilUs)
formatHolder - A FormatHolder to populate in the case of reading a format.buffer - A DecoderInputBuffer to populate in the case of reading a sample or the
end of the stream. If the end of the stream has been reached, the
C.BUFFER_FLAG_END_OF_STREAM flag will be set on the buffer.formatRequired - Whether the caller requires that the format of the stream be read even if
it's not changing. A sample will never be read if set to true, however it is still possible
for the end of stream or nothing to be read.loadingFinished - True if an empty queue should be considered the end of the stream.decodeOnlyUntilUs - If a buffer is read, the C.BUFFER_FLAG_DECODE_ONLY flag will
be set if the buffer's timestamp is less than this value.C.RESULT_NOTHING_READ, C.RESULT_FORMAT_READ or
C.RESULT_BUFFER_READ.public void setUpstreamFormatChangeListener(SampleQueue.UpstreamFormatChangedListener listener)
listener - The listener.public void setSampleOffsetUs(long sampleOffsetUs)
sampleOffsetUs - The timestamp offset in microseconds.public void format(Format format)
TrackOutputFormat of the track has been extracted from the stream.format in interface TrackOutputformat - The extracted Format.public int sampleData(ExtractorInput input, int length, boolean allowEndOfInput) throws java.io.IOException, java.lang.InterruptedException
TrackOutputsampleData in interface TrackOutputinput - An ExtractorInput from which to read the sample data.length - The maximum length to read from the input.allowEndOfInput - True if encountering the end of the input having read no data is
allowed, and should result in C.RESULT_END_OF_INPUT being returned. False if it
should be considered an error, causing an EOFException to be thrown.java.io.IOException - If an error occurred reading from the input.java.lang.InterruptedException - If the thread was interrupted.public void sampleData(ParsableByteArray buffer, int length)
TrackOutputsampleData in interface TrackOutputbuffer - A ParsableByteArray from which to read the sample data.length - The number of bytes to read.public void sampleMetadata(long timeUs,
@C.BufferFlags
int flags,
int size,
int offset,
@Nullable
TrackOutput.CryptoData cryptoData)
TrackOutputThe corresponding sample data will have already been passed to the output via calls to
TrackOutput.sampleData(ExtractorInput, int, boolean) or TrackOutput.sampleData(ParsableByteArray,
int).
sampleMetadata in interface TrackOutputtimeUs - The media timestamp associated with the sample, in microseconds.flags - Flags associated with the sample. See C.BUFFER_FLAG_*.size - The size of the sample data, in bytes.offset - The number of bytes that have been passed to TrackOutput.sampleData(ExtractorInput,
int, boolean) or TrackOutput.sampleData(ParsableByteArray, int) since the last byte belonging
to the sample whose metadata is being passed.cryptoData - The encryption data required to decrypt the sample. May be null.