public final class DefaultTrackOutput extends Object implements TrackOutput
TrackOutput
that buffers extracted samples in a queue and allows for consumption from
that queue.Modifier and Type | Class and Description |
---|---|
static interface |
DefaultTrackOutput.UpstreamFormatChangedListener
A listener for changes to the upstream format.
|
Constructor and Description |
---|
DefaultTrackOutput(Allocator allocator) |
Modifier and Type | Method and Description |
---|---|
void |
disable()
Disables buffering of sample data and metadata.
|
void |
discardUpstreamSamples(int discardFromIndex)
Discards samples from the write side of the buffer.
|
void |
format(Format format)
Called when the
Format of the track has been extracted from the stream. |
void |
formatWithOffset(Format format,
long sampleOffsetUs)
Like
format(Format) , but with an offset that will be added to the timestamps of
samples subsequently queued to the buffer. |
long |
getLargestQueuedTimestampUs()
Returns the largest sample timestamp that has been queued since the last
reset(boolean) . |
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 |
isEmpty()
Returns whether the buffer is empty.
|
int |
peekSourceId()
Peeks the source id of the next sample, or the current upstream source id if the buffer is
empty.
|
int |
readData(FormatHolder formatHolder,
DecoderInputBuffer buffer,
boolean loadingFinished,
long decodeOnlyUntilUs)
Attempts to read from the queue.
|
void |
reset(boolean enable)
Resets the output.
|
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,
byte[] encryptionKey)
Called when metadata associated with a sample has been extracted from the stream.
|
void |
setUpstreamFormatChangeListener(DefaultTrackOutput.UpstreamFormatChangedListener listener)
Sets a listener to be notified of changes to the upstream format.
|
boolean |
skipToKeyframeBefore(long timeUs)
Attempts to skip to the keyframe before the specified time, if it's present in the buffer.
|
void |
sourceId(int sourceId)
Sets a source identifier for subsequent samples.
|
void |
splice()
Indicates that samples subsequently queued to the buffer should be spliced into those already
queued.
|
public void reset(boolean enable)
enable
- Whether the output should be enabled. False if it should be disabled.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.public void disable()
public boolean isEmpty()
public int getReadIndex()
public int peekSourceId()
public Format getUpstreamFormat()
Format
in which samples are being queued.public long getLargestQueuedTimestampUs()
reset(boolean)
.
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 boolean skipToKeyframeBefore(long timeUs)
timeUs
- The seek time.public int readData(FormatHolder formatHolder, DecoderInputBuffer buffer, 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.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(DefaultTrackOutput.UpstreamFormatChangedListener listener)
listener
- The listener.public void formatWithOffset(Format format, long sampleOffsetUs)
format(Format)
, but with an offset that will be added to the timestamps of
samples subsequently queued to the buffer. The offset is also used to adjust
Format.subsampleOffsetUs
for both the Format
passed and those subsequently
passed to format(Format)
.format
- The format.sampleOffsetUs
- The timestamp offset in microseconds.public void format(Format format)
TrackOutput
Format
of the track has been extracted from the stream.format
in interface TrackOutput
format
- The extracted Format
.public int sampleData(ExtractorInput input, int length, boolean allowEndOfInput) throws IOException, InterruptedException
TrackOutput
sampleData
in interface TrackOutput
input
- 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.IOException
- If an error occurred reading from the input.InterruptedException
- If the thread was interrupted.public void sampleData(ParsableByteArray buffer, int length)
TrackOutput
sampleData
in interface TrackOutput
buffer
- A ParsableByteArray
from which to read the sample data.length
- The number of bytes to read.public void sampleMetadata(long timeUs, int flags, int size, int offset, byte[] encryptionKey)
TrackOutput
The 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 TrackOutput
timeUs
- 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.encryptionKey
- The encryption key associated with the sample. May be null.