ChunkSampleStream
@UnstableApi
public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, SequenceableLoader, Loader.Callback, Loader.ReleaseCallback
A SampleStream that loads media in Chunks, obtained from a ChunkSource. May also be configured to expose additional embedded SampleStreams.
Summary
Nested types |
|---|
public final inner class ChunkSampleStream.EmbeddedSampleStream implements SampleStreamA |
public interface ChunkSampleStream.ReleaseCallback<T extends ChunkSource>A callback to be notified when a sample stream has finished being released. |
Public fields |
|
|---|---|
final T |
|
final int |
Public constructors |
|---|
ChunkSampleStream(Constructs an instance. |
Public methods |
|
|---|---|
boolean |
Consumes a pending initial discontinuity. |
boolean |
continueLoading(LoadingInfo loadingInfo)Attempts to continue loading. |
void |
discardBuffer(long positionUs, boolean toKeyframe)Discards buffered media up to the specified position. |
void |
discardUpstreamSamplesForClippedDuration(long clippedDurationUs)Discards upstream samples that exceed the given clipped duration of the stream. |
long |
getAdjustedSeekPositionUs(Adjusts a seek position given the specified |
long |
Returns an estimate of the position up to which data is buffered. |
T |
Returns the |
long |
Returns the next load time, or |
boolean |
Returns whether the loader is currently loading. |
boolean |
isReady()Returns whether data is available to be read. |
void |
Throws an error that's preventing data from being read. |
void |
onLoadCanceled(Called when a load has been canceled. |
void |
onLoadCompleted(Called when a load has completed. |
Loader.LoadErrorAction |
onLoadError(Called when a load encounters an error. |
void |
onLoadStarted(Called when a load has started for the first time or through a retry. |
void |
Called when the |
int |
readData(Attempts to read from the stream. |
void |
reevaluateBuffer(long positionUs)Re-evaluates the buffer given the playback position. |
void |
release()Releases the stream. |
void |
release(@Nullable ChunkSampleStream.ReleaseCallback<T> callback)Releases the stream. |
void |
seekToUs(long positionUs)Seeks to the specified position in microseconds. |
ChunkSampleStream.EmbeddedSampleStream |
selectEmbeddedTrack(long positionUs, int trackType)Selects the embedded track, returning a new |
int |
skipData(long positionUs)Attempts to skip to the keyframe before the specified position, or to the end of the stream if |
Inherited Constants |
||||||
|---|---|---|---|---|---|---|
|
Public fields
Public constructors
ChunkSampleStream
public ChunkSampleStream(
@C.TrackType int primaryTrackType,
@Nullable int[] embeddedTrackTypes,
@Nullable Format[] embeddedTrackFormats,
T chunkSource,
SequenceableLoader.Callback<ChunkSampleStream<T>> callback,
Allocator allocator,
long positionUs,
DrmSessionManager drmSessionManager,
DrmSessionEventListener.EventDispatcher drmEventDispatcher,
LoadErrorHandlingPolicy loadErrorHandlingPolicy,
MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher,
boolean canReportInitialDiscontinuity,
@Nullable ReleasableExecutor downloadExecutor
)
Constructs an instance.
| Parameters | |
|---|---|
@C.TrackType int primaryTrackType |
The |
@Nullable int[] embeddedTrackTypes |
The types of any embedded tracks, or null. |
@Nullable Format[] embeddedTrackFormats |
The formats of the embedded tracks, or null. |
T chunkSource |
A |
SequenceableLoader.Callback<ChunkSampleStream<T>> callback |
An |
Allocator allocator |
An |
long positionUs |
The position from which to start loading media. |
DrmSessionManager drmSessionManager |
The |
DrmSessionEventListener.EventDispatcher drmEventDispatcher |
A dispatcher to notify of |
LoadErrorHandlingPolicy loadErrorHandlingPolicy |
|
MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher |
A dispatcher to notify of |
boolean canReportInitialDiscontinuity |
Whether the stream can report an initial discontinuity if the first chunk can't start at the beginning and needs to preroll data. |
@Nullable ReleasableExecutor downloadExecutor |
An optional externally provided |
Public methods
consumeInitialDiscontinuity
public boolean consumeInitialDiscontinuity()
Consumes a pending initial discontinuity.
| Returns | |
|---|---|
boolean |
Whether the stream had an initial discontinuity. |
continueLoading
public boolean continueLoading(LoadingInfo loadingInfo)
Attempts to continue loading.
| Parameters | |
|---|---|
LoadingInfo loadingInfo |
The |
| Returns | |
|---|---|
boolean |
True if progress was made, meaning that |
discardBuffer
public void discardBuffer(long positionUs, boolean toKeyframe)
Discards buffered media up to the specified position.
| Parameters | |
|---|---|
long positionUs |
The position to discard up to, in microseconds. |
boolean toKeyframe |
If true then for each track discards samples up to the keyframe before or at the specified position, rather than any sample before or at that position. |
discardUpstreamSamplesForClippedDuration
public void discardUpstreamSamplesForClippedDuration(long clippedDurationUs)
Discards upstream samples that exceed the given clipped duration of the stream.
| Parameters | |
|---|---|
long clippedDurationUs |
The clipped duration of the stream in microseconds, or |
getAdjustedSeekPositionUs
public long getAdjustedSeekPositionUs(
long positionUs,
SeekParameters seekParameters
)
Adjusts a seek position given the specified SeekParameters. Chunk boundaries are used as sync points.
| Parameters | |
|---|---|
long positionUs |
The seek position in microseconds. |
SeekParameters seekParameters |
Parameters that control how the seek is performed. |
| Returns | |
|---|---|
long |
The adjusted seek position, in microseconds. |
getBufferedPositionUs
public long getBufferedPositionUs()
Returns an estimate of the position up to which data is buffered.
| Returns | |
|---|---|
long |
An estimate of the absolute position in microseconds up to which data is buffered, or |
getNextLoadPositionUs
public long getNextLoadPositionUs()
Returns the next load time, or TIME_END_OF_SOURCE if loading has finished.
isReady
public boolean isReady()
Returns whether data is available to be read.
Note: If the stream has ended then a buffer with the end of stream flag can always be read from readData. Hence an ended stream is always ready.
| Returns | |
|---|---|
boolean |
Whether data is available to be read. |
maybeThrowError
public void maybeThrowError()
Throws an error that's preventing data from being read. Does nothing if no such error exists.
| Throws | |
|---|---|
java.io.IOException |
The underlying error. |
onLoadCanceled
public void onLoadCanceled(
Chunk loadable,
long elapsedRealtimeMs,
long loadDurationMs,
boolean released
)
Called when a load has been canceled.
Note: If the Loader has not been released then there is guaranteed to be a memory barrier between load exiting and this callback being called. If the Loader has been released then this callback may be called before load exits.
| Parameters | |
|---|---|
Chunk loadable |
The loadable whose load has been canceled. |
long elapsedRealtimeMs |
|
long loadDurationMs |
The duration in milliseconds of the load since |
boolean released |
True if the load was canceled because the |
onLoadCompleted
public void onLoadCompleted(
Chunk loadable,
long elapsedRealtimeMs,
long loadDurationMs
)
Called when a load has completed.
Note: There is guaranteed to be a memory barrier between load exiting and this callback being called.
| Parameters | |
|---|---|
Chunk loadable |
The loadable whose load has completed. |
long elapsedRealtimeMs |
|
long loadDurationMs |
The duration in milliseconds of the load since |
onLoadError
public Loader.LoadErrorAction onLoadError(
Chunk loadable,
long elapsedRealtimeMs,
long loadDurationMs,
IOException error,
int errorCount
)
Called when a load encounters an error.
Note: There is guaranteed to be a memory barrier between load exiting and this callback being called.
| Parameters | |
|---|---|
Chunk loadable |
The loadable whose load has encountered an error. |
long elapsedRealtimeMs |
|
long loadDurationMs |
The duration in milliseconds of the load since |
IOException error |
The load error. |
int errorCount |
The number of errors this load has encountered, including this one. |
| Returns | |
|---|---|
Loader.LoadErrorAction |
The desired error handling action. One of |
onLoadStarted
public void onLoadStarted(
Chunk loadable,
long elapsedRealtimeMs,
long loadDurationMs,
int retryCount
)
Called when a load has started for the first time or through a retry.
This is called for the first time with retryCount == 0 just before the load is started.
| Parameters | |
|---|---|
Chunk loadable |
The loadable whose load has started. |
long elapsedRealtimeMs |
|
long loadDurationMs |
The duration in milliseconds of the load since |
int retryCount |
The number of failed attempts since |
readData
public int readData(
FormatHolder formatHolder,
DecoderInputBuffer buffer,
@SampleStream.ReadFlags int readFlags
)
Attempts to read from the stream.
If the stream has ended then BUFFER_FLAG_END_OF_STREAM flag is set on
buffer and RESULT_BUFFER_READ is returned. Else if no data is available then RESULT_NOTHING_READ is returned. Else if the format of the media is changing or if
formatRequired is set then formatHolder is populated and RESULT_FORMAT_READ is returned. Else buffer is populated and RESULT_BUFFER_READ is returned.
| Parameters | |
|---|---|
FormatHolder formatHolder |
A |
DecoderInputBuffer buffer |
A |
@SampleStream.ReadFlags int readFlags |
Flags controlling the behavior of this read operation. |
| Returns | |
|---|---|
int |
The |
| Throws | |
|---|---|
androidx.media3.decoder.DecoderInputBuffer.InsufficientCapacityException |
If the |
reevaluateBuffer
public void reevaluateBuffer(long positionUs)
Re-evaluates the buffer given the playback position.
Re-evaluation may discard buffered media or cancel ongoing loads so that media can be re-buffered in a different quality.
| Parameters | |
|---|---|
long positionUs |
The current playback position in microseconds. If playback of this period has not yet started, the value will be the starting position in this period minus the duration of any media in previous periods still to be played. |
release
public void release()
Releases the stream.
This method should be called when the stream is no longer required. Either this method or release can be used to release this stream.
release
public void release(@Nullable ChunkSampleStream.ReleaseCallback<T> callback)
Releases the stream.
This method should be called when the stream is no longer required. Either this method or release can be used to release this stream.
| Parameters | |
|---|---|
@Nullable ChunkSampleStream.ReleaseCallback<T> callback |
An optional callback to be called on the loading thread once the loader has been released. |
seekToUs
public void seekToUs(long positionUs)
Seeks to the specified position in microseconds.
| Parameters | |
|---|---|
long positionUs |
The seek position in microseconds. |
selectEmbeddedTrack
public ChunkSampleStream.EmbeddedSampleStream selectEmbeddedTrack(long positionUs, int trackType)
Selects the embedded track, returning a new EmbeddedSampleStream from which the track's samples can be consumed. release must be called on the returned stream when the track is no longer required, and before calling this method again to obtain another stream for the same track.
| Parameters | |
|---|---|
long positionUs |
The current playback position in microseconds. |
int trackType |
The type of the embedded track to enable. |
| Returns | |
|---|---|
ChunkSampleStream.EmbeddedSampleStream |
The |
skipData
public int skipData(long positionUs)
Attempts to skip to the keyframe before the specified position, or to the end of the stream if positionUs is beyond it.
| Parameters | |
|---|---|
long positionUs |
The specified time. |
| Returns | |
|---|---|
int |
The number of samples that were skipped. |