public class ChunkSampleSource extends Object implements SampleSource, SampleSource.SampleSourceReader, Loader.Callback
Modifier and Type | Class and Description |
---|---|
static interface |
ChunkSampleSource.EventListener
Interface definition for a callback to be notified of
ChunkSampleSource events. |
SampleSource.SampleSourceReader
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_MIN_LOADABLE_RETRY_COUNT
The default minimum number of times to retry loading data prior to failing.
|
protected DefaultTrackOutput |
sampleQueue |
END_OF_STREAM, FORMAT_READ, NO_DISCONTINUITY, NOTHING_READ, SAMPLE_READ
Constructor and Description |
---|
ChunkSampleSource(ChunkSource chunkSource,
LoadControl loadControl,
int bufferSizeContribution) |
ChunkSampleSource(ChunkSource chunkSource,
LoadControl loadControl,
int bufferSizeContribution,
Handler eventHandler,
ChunkSampleSource.EventListener eventListener,
int eventSourceId) |
ChunkSampleSource(ChunkSource chunkSource,
LoadControl loadControl,
int bufferSizeContribution,
Handler eventHandler,
ChunkSampleSource.EventListener eventListener,
int eventSourceId,
int minLoadableRetryCount) |
Modifier and Type | Method and Description |
---|---|
boolean |
continueBuffering(int track,
long positionUs)
Indicates to the source that it should still be buffering data for the specified track.
|
void |
disable(int track)
Disable the specified track.
|
void |
enable(int track,
long positionUs)
Enable the specified track.
|
long |
getBufferedPositionUs()
Returns an estimate of the position up to which data is buffered.
|
MediaFormat |
getFormat(int track)
Returns the format of the specified track.
|
int |
getTrackCount()
Returns the number of tracks exposed by the source.
|
void |
maybeThrowError()
If the source is currently having difficulty preparing or loading samples, then this method
throws the underlying error.
|
void |
onLoadCanceled(Loader.Loadable loadable)
Invoked when loading has been canceled.
|
void |
onLoadCompleted(Loader.Loadable loadable)
Invoked when the data source has been fully loaded.
|
void |
onLoadError(Loader.Loadable loadable,
IOException e)
Invoked when the data source is stopped due to an error.
|
protected void |
onSampleRead(MediaChunk mediaChunk,
SampleHolder sampleHolder)
Called when a sample has been read.
|
boolean |
prepare(long positionUs)
Prepares the source.
|
int |
readData(int track,
long positionUs,
MediaFormatHolder formatHolder,
SampleHolder sampleHolder)
Attempts to read a sample or a new format from the source.
|
long |
readDiscontinuity(int track)
Attempts to read a pending discontinuity from the source.
|
SampleSource.SampleSourceReader |
register()
A consumer of samples should call this method to register themselves and gain access to the
source through the returned
SampleSource.SampleSourceReader . |
void |
release()
Releases the
SampleSource.SampleSourceReader . |
void |
seekToUs(long positionUs)
Seeks to the specified time in microseconds.
|
protected long |
usToMs(long timeUs) |
public static final int DEFAULT_MIN_LOADABLE_RETRY_COUNT
protected final DefaultTrackOutput sampleQueue
public ChunkSampleSource(ChunkSource chunkSource, LoadControl loadControl, int bufferSizeContribution)
chunkSource
- A ChunkSource
from which chunks to load are obtained.loadControl
- Controls when the source is permitted to load data.bufferSizeContribution
- The contribution of this source to the media buffer, in bytes.public ChunkSampleSource(ChunkSource chunkSource, LoadControl loadControl, int bufferSizeContribution, Handler eventHandler, ChunkSampleSource.EventListener eventListener, int eventSourceId)
chunkSource
- A ChunkSource
from which chunks to load are obtained.loadControl
- Controls when the source is permitted to load data.bufferSizeContribution
- The contribution of this source to the media buffer, in bytes.eventHandler
- A handler to use when delivering events to eventListener
. May be
null if delivery of events is not required.eventListener
- A listener of events. May be null if delivery of events is not required.eventSourceId
- An identifier that gets passed to eventListener
methods.public ChunkSampleSource(ChunkSource chunkSource, LoadControl loadControl, int bufferSizeContribution, Handler eventHandler, ChunkSampleSource.EventListener eventListener, int eventSourceId, int minLoadableRetryCount)
chunkSource
- A ChunkSource
from which chunks to load are obtained.loadControl
- Controls when the source is permitted to load data.bufferSizeContribution
- The contribution of this source to the media buffer, in bytes.eventHandler
- A handler to use when delivering events to eventListener
. May be
null if delivery of events is not required.eventListener
- A listener of events. May be null if delivery of events is not required.eventSourceId
- An identifier that gets passed to eventListener
methods.minLoadableRetryCount
- The minimum number of times that the source should retry a load
before propagating an error.public SampleSource.SampleSourceReader register()
SampleSource
SampleSource.SampleSourceReader
.
SampleSource.SampleSourceReader.release()
should be called on the returned object when access is no
longer required.
register
in interface SampleSource
SampleSource.SampleSourceReader
that provides access to the source.public boolean prepare(long positionUs)
SampleSource.SampleSourceReader
Preparation may require reading from the data source (e.g. to determine the available tracks
and formats). If insufficient data is available then the call will return false
rather than block. The method can be called repeatedly until the return value indicates
success.
prepare
in interface SampleSource.SampleSourceReader
positionUs
- The player's current playback position.public int getTrackCount()
SampleSource.SampleSourceReader
This method should only be called after the source has been prepared.
getTrackCount
in interface SampleSource.SampleSourceReader
public MediaFormat getFormat(int track)
SampleSource.SampleSourceReader
Note that whilst the format of a track will remain constant, the format of the actual media
stream may change dynamically. An example of this is where the track is adaptive
(i.e. @link MediaFormat.adaptive
is true). Hence the track formats returned through
this method should not be used to configure decoders. Decoder configuration should be
performed using the formats obtained when reading the media stream through calls to
SampleSource.SampleSourceReader.readData(int, long, MediaFormatHolder, SampleHolder)
.
This method should only be called after the source has been prepared.
getFormat
in interface SampleSource.SampleSourceReader
track
- The track index.public void enable(int track, long positionUs)
SampleSource.SampleSourceReader
SampleSource.SampleSourceReader.readData(int, long, MediaFormatHolder, SampleHolder)
.
This method should only be called after the source has been prepared, and when the specified track is disabled.
enable
in interface SampleSource.SampleSourceReader
track
- The track to enable.positionUs
- The player's current playback position.public void disable(int track)
SampleSource.SampleSourceReader
This method should only be called when the specified track is enabled.
disable
in interface SampleSource.SampleSourceReader
track
- The track to disable.public boolean continueBuffering(int track, long positionUs)
SampleSource.SampleSourceReader
This method should only be called when the specified track is enabled.
continueBuffering
in interface SampleSource.SampleSourceReader
track
- The track to continue buffering.positionUs
- The current playback position.public long readDiscontinuity(int track)
SampleSource.SampleSourceReader
This method should only be called when the specified track is enabled.
readDiscontinuity
in interface SampleSource.SampleSourceReader
track
- The track from which to read.SampleSource.NO_DISCONTINUITY
.public int readData(int track, long positionUs, MediaFormatHolder formatHolder, SampleHolder sampleHolder)
SampleSource.SampleSourceReader
This method should only be called when the specified track is enabled.
Note that where multiple tracks are enabled, SampleSource.NOTHING_READ
may be returned if the
next piece of data to be read from the SampleSource
corresponds to a different track
than the one for which data was requested.
This method will always return SampleSource.NOTHING_READ
in the case that there's a pending
discontinuity to be read from SampleSource.SampleSourceReader.readDiscontinuity(int)
for the specified track.
readData
in interface SampleSource.SampleSourceReader
track
- The track from which to read.positionUs
- The current playback position.formatHolder
- A MediaFormatHolder
object to populate in the case of a new
format.sampleHolder
- A SampleHolder
object to populate in the case of a new sample.
If the caller requires the sample data then it must ensure that SampleHolder.data
references a valid output buffer.SampleSource.SAMPLE_READ
, SampleSource.FORMAT_READ
,
SampleSource.NOTHING_READ
or SampleSource.END_OF_STREAM
.public void seekToUs(long positionUs)
SampleSource.SampleSourceReader
This method should only be called when at least one track is enabled.
seekToUs
in interface SampleSource.SampleSourceReader
positionUs
- The seek position in microseconds.public void maybeThrowError() throws IOException
SampleSource.SampleSourceReader
maybeThrowError
in interface SampleSource.SampleSourceReader
IOException
- The underlying error.public long getBufferedPositionUs()
SampleSource.SampleSourceReader
This method should only be called when at least one track is enabled.
getBufferedPositionUs
in interface SampleSource.SampleSourceReader
TrackRenderer.END_OF_TRACK_US
if data is buffered to the end of the stream,
or TrackRenderer.UNKNOWN_TIME_US
if no estimate is available.public void release()
SampleSource.SampleSourceReader
SampleSource.SampleSourceReader
.
This method should be called when access to the SampleSource
is no longer required.
release
in interface SampleSource.SampleSourceReader
public void onLoadCompleted(Loader.Loadable loadable)
Loader.Callback
onLoadCompleted
in interface Loader.Callback
loadable
- The loadable whose load has completed.public void onLoadCanceled(Loader.Loadable loadable)
Loader.Callback
onLoadCanceled
in interface Loader.Callback
loadable
- The loadable whose load has been canceled.public void onLoadError(Loader.Loadable loadable, IOException e)
Loader.Callback
onLoadError
in interface Loader.Callback
loadable
- The loadable whose load has failed.protected void onSampleRead(MediaChunk mediaChunk, SampleHolder sampleHolder)
mediaChunk
- The chunk from which the sample was obtained.sampleHolder
- Holds the read sample.protected final long usToMs(long timeUs)