public final class ExtractorSampleSource extends Object implements SampleSource, SampleSource.SampleSourceReader, ExtractorOutput, Loader.Callback
SampleSource
that extracts sample data using an Extractor
.
If no Extractor
instances are passed to the constructor, the input stream container
format will be detected automatically from the following supported formats:
Mp4Extractor
)FragmentedMp4Extractor
)WebmExtractor
)OggExtractor
Mp3Extractor
)AdtsExtractor
)TsExtractor
)PsExtractor
)FlvExtractor
)WavExtractor
)Seeking in AAC, MPEG TS and FLV streams is not supported.
To override the default extractors, pass one or more Extractor
instances to the
constructor. When reading a new stream, the first Extractor
that returns true
from Extractor.sniff(ExtractorInput)
will be used.
Modifier and Type | Class and Description |
---|---|
static interface |
ExtractorSampleSource.EventListener
Interface definition for a callback to be notified of
ExtractorSampleSource events. |
static class |
ExtractorSampleSource.UnrecognizedInputFormatException
Thrown if the input format could not recognized.
|
SampleSource.SampleSourceReader
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_MIN_LOADABLE_RETRY_COUNT_LIVE
The default minimum number of times to retry loading prior to failing for live streams.
|
static int |
DEFAULT_MIN_LOADABLE_RETRY_COUNT_ON_DEMAND
The default minimum number of times to retry loading prior to failing for on-demand streams.
|
END_OF_STREAM, FORMAT_READ, NO_DISCONTINUITY, NOTHING_READ, SAMPLE_READ
Constructor and Description |
---|
ExtractorSampleSource(Uri uri,
DataSource dataSource,
Allocator allocator,
int requestedBufferSize,
Extractor... extractors) |
ExtractorSampleSource(Uri uri,
DataSource dataSource,
Allocator allocator,
int requestedBufferSize,
Handler eventHandler,
ExtractorSampleSource.EventListener eventListener,
int eventSourceId,
Extractor... extractors) |
ExtractorSampleSource(Uri uri,
DataSource dataSource,
Allocator allocator,
int requestedBufferSize,
int minLoadableRetryCount,
Extractor... extractors) |
ExtractorSampleSource(Uri uri,
DataSource dataSource,
Allocator allocator,
int requestedBufferSize,
int minLoadableRetryCount,
Handler eventHandler,
ExtractorSampleSource.EventListener eventListener,
int eventSourceId,
Extractor... extractors) |
Modifier and Type | Method and Description |
---|---|
boolean |
continueBuffering(int track,
long playbackPositionUs)
Indicates to the source that it should still be buffering data for the specified track.
|
void |
disable(int track)
Disable the specified track.
|
void |
drmInitData(DrmInitData drmInitData)
Invoked when
DrmInitData has been extracted from the stream. |
void |
enable(int track,
long positionUs)
Enable the specified track.
|
void |
endTracks()
Invoked when all tracks have been identified, meaning that
ExtractorOutput.track(int) will not be
invoked again. |
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 ignored,
IOException e)
Invoked when the data source is stopped due to an error.
|
boolean |
prepare(long positionUs)
Prepares the source.
|
int |
readData(int track,
long playbackPositionUs,
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 |
seekMap(SeekMap seekMap)
Invoked when a
SeekMap has been extracted from the stream. |
void |
seekToUs(long positionUs)
Seeks to the specified time in microseconds.
|
TrackOutput |
track(int id)
Called by the
Extractor to get the TrackOutput for a specific track. |
public static final int DEFAULT_MIN_LOADABLE_RETRY_COUNT_ON_DEMAND
public static final int DEFAULT_MIN_LOADABLE_RETRY_COUNT_LIVE
public ExtractorSampleSource(Uri uri, DataSource dataSource, Allocator allocator, int requestedBufferSize, Extractor... extractors)
uri
- The Uri
of the media stream.dataSource
- A data source to read the media stream.allocator
- An Allocator
from which to obtain memory allocations.requestedBufferSize
- The requested total buffer size for storing sample data, in bytes.
The actual allocated size may exceed the value passed in if the implementation requires it.extractors
- Extractor
s to extract the media stream, in order of decreasing
priority. If omitted, the default extractors will be used.public ExtractorSampleSource(Uri uri, DataSource dataSource, Allocator allocator, int requestedBufferSize, Handler eventHandler, ExtractorSampleSource.EventListener eventListener, int eventSourceId, Extractor... extractors)
uri
- The Uri
of the media stream.dataSource
- A data source to read the media stream.allocator
- An Allocator
from which to obtain memory allocations.requestedBufferSize
- The requested total buffer size for storing sample data, in bytes.
The actual allocated size may exceed the value passed in if the implementation requires it.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.extractors
- Extractor
s to extract the media stream, in order of decreasing
priority. If omitted, the default extractors will be used.public ExtractorSampleSource(Uri uri, DataSource dataSource, Allocator allocator, int requestedBufferSize, int minLoadableRetryCount, Extractor... extractors)
uri
- The Uri
of the media stream.dataSource
- A data source to read the media stream.allocator
- An Allocator
from which to obtain memory allocations.requestedBufferSize
- The requested total buffer size for storing sample data, in bytes.
The actual allocated size may exceed the value passed in if the implementation requires it.minLoadableRetryCount
- The minimum number of times that the sample source will retry
if a loading error occurs.extractors
- Extractor
s to extract the media stream, in order of decreasing
priority. If omitted, the default extractors will be used.public ExtractorSampleSource(Uri uri, DataSource dataSource, Allocator allocator, int requestedBufferSize, int minLoadableRetryCount, Handler eventHandler, ExtractorSampleSource.EventListener eventListener, int eventSourceId, Extractor... extractors)
uri
- The Uri
of the media stream.dataSource
- A data source to read the media stream.allocator
- An Allocator
from which to obtain memory allocations.requestedBufferSize
- The requested total buffer size for storing sample data, in bytes.
The actual allocated size may exceed the value passed in if the implementation requires it.minLoadableRetryCount
- The minimum number of times that the sample source will retry
if a loading error occurs.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.extractors
- Extractor
s to extract the media stream, in order of decreasing
priority. If omitted, the default extractors will be used.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 playbackPositionUs)
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.playbackPositionUs
- 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 playbackPositionUs, 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.playbackPositionUs
- 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 maybeThrowError() throws IOException
SampleSource.SampleSourceReader
maybeThrowError
in interface SampleSource.SampleSourceReader
IOException
- The underlying error.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 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 ignored, IOException e)
Loader.Callback
onLoadError
in interface Loader.Callback
ignored
- The loadable whose load has failed.public TrackOutput track(int id)
ExtractorOutput
Extractor
to get the TrackOutput
for a specific track.
The same TrackOutput
is returned if multiple calls are made with the same
trackId
.
track
in interface ExtractorOutput
id
- A track identifier.TrackOutput
for the given track identifier.public void endTracks()
ExtractorOutput
ExtractorOutput.track(int)
will not be
invoked again.endTracks
in interface ExtractorOutput
public void seekMap(SeekMap seekMap)
ExtractorOutput
SeekMap
has been extracted from the stream.seekMap
in interface ExtractorOutput
seekMap
- The extracted SeekMap
.public void drmInitData(DrmInitData drmInitData)
ExtractorOutput
DrmInitData
has been extracted from the stream.drmInitData
in interface ExtractorOutput
drmInitData
- The extracted DrmInitData
.