public abstract class MediaCodecTrackRenderer extends SampleSourceTrackRenderer
TrackRenderer
that uses MediaCodec
to decode samples for rendering.Modifier and Type | Class and Description |
---|---|
static class |
MediaCodecTrackRenderer.DecoderInitializationException
Thrown when a failure occurs instantiating a decoder.
|
static interface |
MediaCodecTrackRenderer.EventListener
Interface definition for a callback to be notified of
MediaCodecTrackRenderer events. |
Modifier and Type | Field and Description |
---|---|
CodecCounters |
codecCounters |
protected Handler |
eventHandler |
protected static int |
SOURCE_STATE_NOT_READY
Value returned by
getSourceState() when the source is not ready. |
protected static int |
SOURCE_STATE_READY
Value returned by
getSourceState() when the source is ready and we're able to read
from it. |
protected static int |
SOURCE_STATE_READY_READ_MAY_FAIL
Value returned by
getSourceState() when the source is ready but we might not be able
to read from it. |
END_OF_TRACK_US, MATCH_LONGEST_US, STATE_ENABLED, STATE_PREPARED, STATE_RELEASED, STATE_STARTED, STATE_UNPREPARED, UNKNOWN_TIME_US
Constructor and Description |
---|
MediaCodecTrackRenderer(SampleSource[] sources,
MediaCodecSelector mediaCodecSelector,
DrmSessionManager<FrameworkMediaCrypto> drmSessionManager,
boolean playClearSamplesWithoutKeys,
Handler eventHandler,
MediaCodecTrackRenderer.EventListener eventListener) |
MediaCodecTrackRenderer(SampleSource source,
MediaCodecSelector mediaCodecSelector,
DrmSessionManager<FrameworkMediaCrypto> drmSessionManager,
boolean playClearSamplesWithoutKeys,
Handler eventHandler,
MediaCodecTrackRenderer.EventListener eventListener) |
Modifier and Type | Method and Description |
---|---|
protected boolean |
canReconfigureCodec(MediaCodec codec,
boolean codecIsAdaptive,
MediaFormat oldFormat,
MediaFormat newFormat)
Determines whether the existing
MediaCodec should be reconfigured for a new format by
sending codec specific initialization data at the start of the next input buffer. |
protected boolean |
codecInitialized() |
protected abstract void |
configureCodec(MediaCodec codec,
boolean codecIsAdaptive,
MediaFormat format,
MediaCrypto crypto)
Configures a newly created
MediaCodec . |
protected void |
doSomeWork(long positionUs,
long elapsedRealtimeUs,
boolean sourceIsReady)
|
protected void |
flushCodec() |
protected DecoderInfo |
getDecoderInfo(MediaCodecSelector mediaCodecSelector,
String mimeType,
boolean requiresSecureDecoder)
Returns a
DecoderInfo for a given format. |
protected long |
getDequeueOutputBufferTimeoutUs()
Returns the maximum time to block whilst waiting for a decoded output buffer.
|
protected int |
getSourceState()
Gets the source state.
|
protected abstract boolean |
handlesTrack(MediaCodecSelector mediaCodecSelector,
MediaFormat mediaFormat)
Returns whether this renderer is capable of handling the provided track.
|
protected boolean |
handlesTrack(MediaFormat mediaFormat)
Returns whether this renderer is capable of handling the provided track.
|
protected boolean |
haveFormat() |
protected boolean |
isEnded()
Whether the renderer is ready for the
ExoPlayer instance to transition to
ExoPlayer.STATE_ENDED . |
protected boolean |
isReady()
Whether the renderer is able to immediately render media from the current position.
|
protected void |
maybeInitCodec() |
protected void |
onDisabled()
Called when the renderer is disabled.
|
protected void |
onDiscontinuity(long positionUs)
Invoked when a discontinuity is encountered.
|
protected void |
onInputFormatChanged(MediaFormatHolder formatHolder)
Invoked when a new format is read from the upstream
SampleSource . |
protected void |
onOutputFormatChanged(MediaCodec codec,
MediaFormat outputFormat)
Invoked when the output format of the
MediaCodec changes. |
protected void |
onOutputStreamEnded()
Invoked when the output stream ends, meaning that the last output buffer has been processed
and the
MediaCodec.BUFFER_FLAG_END_OF_STREAM flag has been propagated through the
decoder. |
protected void |
onProcessedOutputBuffer(long presentationTimeUs)
Invoked when an output buffer is successfully processed.
|
protected void |
onQueuedInputBuffer(long presentationTimeUs,
ByteBuffer buffer,
int bufferSize,
boolean sampleEncrypted)
Invoked immediately before an input buffer is queued into the codec.
|
protected void |
onStarted()
Called when the renderer is started.
|
protected void |
onStopped()
Called when the renderer is stopped.
|
protected abstract boolean |
processOutputBuffer(long positionUs,
long elapsedRealtimeUs,
MediaCodec codec,
ByteBuffer buffer,
MediaCodec.BufferInfo bufferInfo,
int bufferIndex,
boolean shouldSkip)
Processes the provided output buffer.
|
protected void |
releaseCodec() |
protected boolean |
shouldInitCodec() |
doPrepare, doSomeWork, getBufferedPositionUs, getDurationUs, getFormat, getTrackCount, maybeThrowError, onEnabled, onReleased, readSource, seekTo, shiftInputPosition
getMediaClock, getState, handleMessage
protected static final int SOURCE_STATE_NOT_READY
getSourceState()
when the source is not ready.protected static final int SOURCE_STATE_READY
getSourceState()
when the source is ready and we're able to read
from it.protected static final int SOURCE_STATE_READY_READ_MAY_FAIL
getSourceState()
when the source is ready but we might not be able
to read from it. We transition to this state when an attempt to read a sample fails despite the
source reporting that samples are available. This can occur when the next sample to be provided
by the source is for another renderer.public final CodecCounters codecCounters
protected final Handler eventHandler
public MediaCodecTrackRenderer(SampleSource source, MediaCodecSelector mediaCodecSelector, DrmSessionManager<FrameworkMediaCrypto> drmSessionManager, boolean playClearSamplesWithoutKeys, Handler eventHandler, MediaCodecTrackRenderer.EventListener eventListener)
source
- The upstream source from which the renderer obtains samples.mediaCodecSelector
- A decoder selector.drmSessionManager
- For use with encrypted media. May be null if support for encrypted
media is not required.playClearSamplesWithoutKeys
- Encrypted media may contain clear (un-encrypted) regions.
For example a media file may start with a short clear region so as to allow playback to
begin in parallel with key acquisition. This parameter specifies whether the renderer is
permitted to play clear regions of encrypted media files before drmSessionManager
has obtained the keys necessary to decrypt encrypted regions of the media.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.public MediaCodecTrackRenderer(SampleSource[] sources, MediaCodecSelector mediaCodecSelector, DrmSessionManager<FrameworkMediaCrypto> drmSessionManager, boolean playClearSamplesWithoutKeys, Handler eventHandler, MediaCodecTrackRenderer.EventListener eventListener)
sources
- The upstream sources from which the renderer obtains samples.mediaCodecSelector
- A decoder selector.drmSessionManager
- For use with encrypted media. May be null if support for encrypted
media is not required.playClearSamplesWithoutKeys
- Encrypted media may contain clear (un-encrypted) regions.
For example a media file may start with a short clear region so as to allow playback to
begin in parallel with key acquisition. This parameter specifies whether the renderer is
permitted to play clear regions of encrypted media files before drmSessionManager
has obtained the keys necessary to decrypt encrypted regions of the media.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.protected final boolean handlesTrack(MediaFormat mediaFormat) throws MediaCodecUtil.DecoderQueryException
SampleSourceTrackRenderer
handlesTrack
in class SampleSourceTrackRenderer
mediaFormat
- The format of the track.MediaCodecUtil.DecoderQueryException
- Thrown if there was an error querying decoders.protected abstract boolean handlesTrack(MediaCodecSelector mediaCodecSelector, MediaFormat mediaFormat) throws MediaCodecUtil.DecoderQueryException
mediaCodecSelector
- The decoder selector.mediaFormat
- The format of the track.MediaCodecUtil.DecoderQueryException
- Thrown if there was an error querying decoders.protected DecoderInfo getDecoderInfo(MediaCodecSelector mediaCodecSelector, String mimeType, boolean requiresSecureDecoder) throws MediaCodecUtil.DecoderQueryException
DecoderInfo
for a given format.mediaCodecSelector
- The decoder selector.mimeType
- The mime type for which a decoder is required.requiresSecureDecoder
- Whether a secure decoder is required.DecoderInfo
describing the decoder to instantiate, or null if no suitable
decoder exists.MediaCodecUtil.DecoderQueryException
- Thrown if there was an error querying decoders.protected abstract void configureCodec(MediaCodec codec, boolean codecIsAdaptive, MediaFormat format, MediaCrypto crypto)
MediaCodec
.codec
- The MediaCodec
to configure.codecIsAdaptive
- Whether the codec is adaptive.format
- The format for which the codec is being configured.crypto
- For drm protected playbacks, a MediaCrypto
to use for decryption.protected final void maybeInitCodec() throws ExoPlaybackException
ExoPlaybackException
protected boolean shouldInitCodec()
protected final boolean codecInitialized()
protected final boolean haveFormat()
protected void onDisabled() throws ExoPlaybackException
TrackRenderer
The default implementation is a no-op.
onDisabled
in class SampleSourceTrackRenderer
ExoPlaybackException
- If an error occurs.protected void releaseCodec()
protected void onDiscontinuity(long positionUs) throws ExoPlaybackException
SampleSourceTrackRenderer
onDiscontinuity
in class SampleSourceTrackRenderer
positionUs
- The playback position after the discontinuity, or the position at which
the renderer is being enabled.ExoPlaybackException
- If an error occurs handling the discontinuity.protected void onStarted()
TrackRenderer
The default implementation is a no-op.
onStarted
in class TrackRenderer
protected void onStopped()
TrackRenderer
The default implementation is a no-op.
onStopped
in class TrackRenderer
protected void doSomeWork(long positionUs, long elapsedRealtimeUs, boolean sourceIsReady) throws ExoPlaybackException
SampleSourceTrackRenderer
doSomeWork
in class SampleSourceTrackRenderer
positionUs
- The current media time in microseconds, measured at the start of the
current iteration of the rendering loop.elapsedRealtimeUs
- SystemClock.elapsedRealtime()
in microseconds,
measured at the start of the current iteration of the rendering loop.sourceIsReady
- The result of the most recent call to
SampleSource.SampleSourceReader.continueBuffering(int, long)
.ExoPlaybackException
- If an error occurs.protected void flushCodec() throws ExoPlaybackException
ExoPlaybackException
protected void onInputFormatChanged(MediaFormatHolder formatHolder) throws ExoPlaybackException
SampleSource
.formatHolder
- Holds the new format.ExoPlaybackException
- If an error occurs reinitializing the MediaCodec
.protected void onOutputFormatChanged(MediaCodec codec, MediaFormat outputFormat) throws ExoPlaybackException
MediaCodec
changes.
The default implementation is a no-op.
codec
- The MediaCodec
instance.outputFormat
- The new output format.ExoPlaybackException
- If an error occurs on output format change.protected void onOutputStreamEnded()
MediaCodec.BUFFER_FLAG_END_OF_STREAM
flag has been propagated through the
decoder.
The default implementation is a no-op.
protected void onQueuedInputBuffer(long presentationTimeUs, ByteBuffer buffer, int bufferSize, boolean sampleEncrypted)
The default implementation is a no-op.
presentationTimeUs
- The timestamp associated with the input buffer.buffer
- The buffer to be queued.bufferSize
- the size of the sample data stored in the buffer.sampleEncrypted
- Whether the sample data is encrypted.protected void onProcessedOutputBuffer(long presentationTimeUs)
The default implementation is a no-op.
presentationTimeUs
- The timestamp associated with the output buffer.protected boolean canReconfigureCodec(MediaCodec codec, boolean codecIsAdaptive, MediaFormat oldFormat, MediaFormat newFormat)
MediaCodec
should be reconfigured for a new format by
sending codec specific initialization data at the start of the next input buffer. If true is
returned then the MediaCodec
instance will be reconfigured in this way. If false is
returned then the instance will be released, and a new instance will be created for the new
format.
The default implementation returns false.
codec
- The existing MediaCodec
instance.codecIsAdaptive
- Whether the codec is adaptive.oldFormat
- The format for which the existing instance is configured.newFormat
- The new format.protected boolean isEnded()
TrackRenderer
ExoPlayer
instance to transition to
ExoPlayer.STATE_ENDED
. The player will make this transition as soon as true
is
returned by all of its TrackRenderer
s.
This method may be called when the renderer is in the following states:
TrackRenderer.STATE_ENABLED
, TrackRenderer.STATE_STARTED
isEnded
in class TrackRenderer
protected boolean isReady()
TrackRenderer
If the renderer is in the TrackRenderer.STATE_STARTED
state then returning true indicates that the
renderer has everything that it needs to continue playback. Returning false indicates that
the player should pause until the renderer is ready.
If the renderer is in the TrackRenderer.STATE_ENABLED
state then returning true indicates that the
renderer is ready for playback to be started. Returning false indicates that it is not.
This method may be called when the renderer is in the following states:
TrackRenderer.STATE_ENABLED
, TrackRenderer.STATE_STARTED
isReady
in class TrackRenderer
protected final int getSourceState()
SOURCE_STATE_NOT_READY
, SOURCE_STATE_READY
and
SOURCE_STATE_READY_READ_MAY_FAIL
.protected long getDequeueOutputBufferTimeoutUs()
protected abstract boolean processOutputBuffer(long positionUs, long elapsedRealtimeUs, MediaCodec codec, ByteBuffer buffer, MediaCodec.BufferInfo bufferInfo, int bufferIndex, boolean shouldSkip) throws ExoPlaybackException
ExoPlaybackException
- If an error occurs processing the output buffer.