SampleQueue
@UnstableApi
class SampleQueue : TrackOutput
A queue of media samples.
Summary
Nested types |
|---|
|
A listener for changes to the upstream format. |
Protected constructors |
|---|
SampleQueue( |
Public functions |
|
|---|---|
java-static SampleQueue! |
createWithDrm(Creates a sample queue with DRM resource management. |
java-static SampleQueue! |
This function is deprecated. Use |
java-static SampleQueue! |
createWithoutDrm(allocator: Allocator!)Creates a sample queue without DRM resource management. |
synchronized Long |
|
Unit |
Discards up to but not including the sample immediately before or at the specified time. |
Unit |
Discards all samples in the queue and advances the read position. |
Unit |
Discards up to but not including the read position. |
Unit |
discardUpstreamFrom(timeUs: Long)Discards samples from the write side of the queue. |
Unit |
discardUpstreamSamples(discardFromIndex: Int)Discards samples from the write side of the queue. |
Unit |
Called when the |
Int |
Returns the current absolute start index. |
synchronized Long |
Returns the timestamp of the first sample, or |
synchronized Long |
Returns the largest sample timestamp that has been read since the last reset. |
Int |
Returns the current absolute read index. |
synchronized Int |
getSkipCount(timeUs: Long, allowEndOfQueue: Boolean)Returns the number of samples that need to be |
Int |
Returns the current absolute write index. |
synchronized Boolean |
Returns whether there is data available for reading. |
Unit |
Throws an error that's preventing data from being read. |
synchronized Long |
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. |
Unit |
Calls |
Int |
@CallSuperAttempts to read from the queue. |
Unit |
Calls |
Unit |
reset()Convenience method for |
Unit |
Clears all samples from the queue. |
Unit |
sampleData(Called to write sample data to the output. |
Int |
sampleData(Called to write sample data to the output. |
Unit |
sampleMetadata(Called when metadata associated with a sample has been extracted from the stream. |
synchronized Boolean |
Attempts to seek the read position to the specified sample index. |
synchronized Boolean |
Attempts to seek the read position to the keyframe before or at the specified time. |
Unit |
setSampleOffsetUs(sampleOffsetUs: Long)Sets an offset that will be added to the timestamps (and sub-sample timestamps) of samples that are subsequently queued. |
Unit |
setStartTimeUs(startTimeUs: Long)Sets the start time for the queue. |
Unit |
Sets a listener to be notified of changes to the upstream format. |
synchronized Unit |
Advances the read position by the specified number of samples. |
Unit |
Sets a source identifier for subsequent samples. |
Unit |
splice()Indicates samples that are subsequently queued should be spliced into those already queued. |
Protected functions |
|
|---|---|
Format! |
@CallSuperAdjusts the upstream |
Unit |
Invalidates the last upstream format adjustment. |
Inherited Constants |
|---|
Inherited functions |
||||||
|---|---|---|---|---|---|---|
|
Protected constructors
SampleQueue
protected SampleQueue(
allocator: Allocator!,
drmSessionManager: DrmSessionManager?,
drmEventDispatcher: DrmSessionEventListener.EventDispatcher?
)
Public functions
createWithDrm
java-static fun createWithDrm(
allocator: Allocator!,
drmSessionManager: DrmSessionManager!,
drmEventDispatcher: DrmSessionEventListener.EventDispatcher!
): SampleQueue!
Creates a sample queue with DRM resource management.
For each sample added to the queue, a DrmSession will be attached containing the keys needed to decrypt it.
| Parameters | |
|---|---|
allocator: Allocator! |
An |
drmSessionManager: DrmSessionManager! |
The |
drmEventDispatcher: DrmSessionEventListener.EventDispatcher! |
A |
createWithDrm
java-static funcreateWithDrm(
allocator: Allocator!,
playbackLooper: Looper!,
drmSessionManager: DrmSessionManager!,
drmEventDispatcher: DrmSessionEventListener.EventDispatcher!
): SampleQueue!
createWithoutDrm
java-static fun createWithoutDrm(allocator: Allocator!): SampleQueue!
Creates a sample queue without DRM resource management.
discardTo
fun discardTo(timeUs: Long, toKeyframe: Boolean, stopAtReadPosition: Boolean): Unit
Discards up to but not including the sample immediately before or at the specified time.
| Parameters | |
|---|---|
timeUs: Long |
The time to discard up to. |
toKeyframe: Boolean |
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: Boolean |
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. |
discardToEnd
fun discardToEnd(): Unit
Discards all samples in the queue and advances the read position.
discardUpstreamFrom
fun discardUpstreamFrom(timeUs: Long): Unit
Discards samples from the write side of the queue.
| Parameters | |
|---|---|
timeUs: Long |
Samples will be discarded from the write end of the queue until a sample with a timestamp smaller than timeUs is encountered (this sample is not discarded). Must be larger than |
discardUpstreamSamples
fun discardUpstreamSamples(discardFromIndex: Int): Unit
Discards samples from the write side of the queue.
| Parameters | |
|---|---|
discardFromIndex: Int |
The absolute index of the first sample to be discarded. Must be in the range [ |
format
fun format(format: Format!): Unit
Called when the Format of the track has been extracted from the stream.
getFirstTimestampUs
synchronized fun getFirstTimestampUs(): Long
Returns the timestamp of the first sample, or MIN_VALUE if the queue is empty.
getLargestReadTimestampUs
synchronized fun getLargestReadTimestampUs(): Long
Returns the largest sample timestamp that has been read since the last reset.
getSkipCount
synchronized fun getSkipCount(timeUs: Long, allowEndOfQueue: Boolean): Int
Returns the number of samples that need to be skipped to advance the read position to the keyframe before or at the specified time.
| Parameters | |
|---|---|
timeUs: Long |
The time to advance to. |
allowEndOfQueue: Boolean |
Whether the end of the queue is considered a keyframe when |
| Returns | |
|---|---|
Int |
The number of samples that need to be skipped, which may be equal to 0. |
isReady
@CallSuper
synchronized fun isReady(loadingFinished: Boolean): Boolean
Returns whether there is data available for reading.
Note: If the stream has ended then a buffer with the end of stream flag can always be read from read. Hence an ended stream is always ready.
| Parameters | |
|---|---|
loadingFinished: Boolean |
Whether no more samples will be written to the sample queue. When true, this method returns true if the sample queue is empty, because an empty sample queue means the end of stream has been reached. When false, this method returns false if the sample queue is empty. |
maybeThrowError
@CallSuper
fun maybeThrowError(): Unit
Throws an error that's preventing data from being read. Does nothing if no such error exists.
| Throws | |
|---|---|
java.io.IOException |
The underlying error. |
peekSourceId
synchronized fun peekSourceId(): Long
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.
| Returns | |
|---|---|
Long |
The source id. |
preRelease
@CallSuper
fun preRelease(): Unit
Calls discardToEnd and releases any resources owned by the queue.
read
@CallSuper
fun read(
formatHolder: FormatHolder!,
buffer: DecoderInputBuffer!,
@SampleStream.ReadFlags readFlags: Int,
loadingFinished: Boolean
): Int
Attempts to read from the queue.
Formats read from this method may be associated to a DrmSession through drmSession.
| Parameters | |
|---|---|
formatHolder: FormatHolder! |
A |
buffer: DecoderInputBuffer! |
A |
@SampleStream.ReadFlags readFlags: Int |
Flags controlling the behavior of this read operation. |
loadingFinished: Boolean |
True if an empty queue should be considered the end of the stream. |
| Returns | |
|---|---|
Int |
The result, which can be |
| Throws | |
|---|---|
androidx.media3.decoder.DecoderInputBuffer.InsufficientCapacityException |
If the |
release
@CallSuper
fun release(): Unit
Calls reset(true) and releases any resources owned by the queue.
reset
@CallSuper
fun reset(resetUpstreamFormat: Boolean): Unit
Clears all samples from the queue.
| Parameters | |
|---|---|
resetUpstreamFormat: Boolean |
Whether the upstream format should be cleared. If set to false, samples queued after the reset (and before a subsequent call to |
sampleData
fun sampleData(
data: ParsableByteArray!,
length: Int,
@TrackOutput.SampleDataPart sampleDataPart: Int
): Unit
Called to write sample data to the output.
| Parameters | |
|---|---|
data: ParsableByteArray! |
A |
length: Int |
The number of bytes to read, starting from |
@TrackOutput.SampleDataPart sampleDataPart: Int |
The part of the sample data to which this call corresponds. |
sampleData
fun sampleData(
input: DataReader!,
length: Int,
allowEndOfInput: Boolean,
@TrackOutput.SampleDataPart sampleDataPart: Int
): Int
Called to write sample data to the output.
| Parameters | |
|---|---|
input: DataReader! |
A |
length: Int |
The maximum length to read from the input. |
allowEndOfInput: Boolean |
True if encountering the end of the input having read no data is allowed, and should result in |
@TrackOutput.SampleDataPart sampleDataPart: Int |
The part of the sample data to which this call corresponds. |
| Returns | |
|---|---|
Int |
The number of bytes appended. |
| Throws | |
|---|---|
java.io.IOException |
If an error occurred reading from the input. |
sampleMetadata
fun sampleMetadata(
timeUs: Long,
@C.BufferFlags flags: Int,
size: Int,
offset: Int,
cryptoData: TrackOutput.CryptoData?
): Unit
Called when metadata associated with a sample has been extracted from the stream.
The corresponding sample data will have already been passed to the output via calls to sampleData or sampleData.
| Parameters | |
|---|---|
timeUs: Long |
The media timestamp associated with the sample, in microseconds. |
@C.BufferFlags flags: Int |
Flags associated with the sample. See |
size: Int |
The size of the sample data, in bytes. |
offset: Int |
The number of bytes that have been passed to |
cryptoData: TrackOutput.CryptoData? |
The encryption data required to decrypt the sample. May be null. |
seekTo
synchronized fun seekTo(sampleIndex: Int): Boolean
Attempts to seek the read position to the specified sample index.
| Parameters | |
|---|---|
sampleIndex: Int |
The sample index. |
| Returns | |
|---|---|
Boolean |
Whether the seek was successful. |
seekTo
synchronized fun seekTo(timeUs: Long, allowTimeBeyondBuffer: Boolean): Boolean
Attempts to seek the read position to the keyframe before or at the specified time.
For formats where all samples are sync samples, it seeks the read position to the first sample at or after the specified time.
| Parameters | |
|---|---|
timeUs: Long |
The time to seek to. |
allowTimeBeyondBuffer: Boolean |
Whether the operation can succeed if |
| Returns | |
|---|---|
Boolean |
Whether the seek was successful. |
setSampleOffsetUs
fun setSampleOffsetUs(sampleOffsetUs: Long): Unit
Sets an offset that will be added to the timestamps (and sub-sample timestamps) of samples that are subsequently queued.
| Parameters | |
|---|---|
sampleOffsetUs: Long |
The timestamp offset in microseconds. |
setStartTimeUs
fun setStartTimeUs(startTimeUs: Long): Unit
Sets the start time for the queue. Samples with earlier timestamps will be discarded if all samples are sync samples in the given input format.
| Parameters | |
|---|---|
startTimeUs: Long |
The start time, in microseconds. |
setUpstreamFormatChangeListener
fun setUpstreamFormatChangeListener(
listener: SampleQueue.UpstreamFormatChangedListener?
): Unit
Sets a listener to be notified of changes to the upstream format.
| Parameters | |
|---|---|
listener: SampleQueue.UpstreamFormatChangedListener? |
The listener. |
skip
synchronized fun skip(count: Int): Unit
Advances the read position by the specified number of samples.
| Parameters | |
|---|---|
count: Int |
The number of samples to advance the read position by. Must be at least 0 and at most |
Protected functions
getAdjustedUpstreamFormat
@CallSuper
protected fun getAdjustedUpstreamFormat(format: Format!): Format!
Adjusts the upstream Format (i.e., the Format that was most recently passed to format).
The default implementation incorporates the sample offset passed to setSampleOffsetUs into subsampleOffsetUs.
invalidateUpstreamFormatAdjustment
protected fun invalidateUpstreamFormatAdjustment(): Unit
Invalidates the last upstream format adjustment. getAdjustedUpstreamFormat will be called to adjust the upstream Format again before the next sample is queued.