public abstract class MediaCodecRenderer extends BaseRenderer
MediaCodec
to decode samples for rendering.Modifier and Type | Class and Description |
---|---|
static class |
MediaCodecRenderer.DecoderInitializationException
Thrown when a failure occurs instantiating a decoder.
|
Modifier and Type | Field and Description |
---|---|
protected DecoderCounters |
decoderCounters |
STATE_DISABLED, STATE_ENABLED, STATE_STARTED
ADAPTIVE_NOT_SEAMLESS, ADAPTIVE_NOT_SUPPORTED, ADAPTIVE_SEAMLESS, ADAPTIVE_SUPPORT_MASK, FORMAT_EXCEEDS_CAPABILITIES, FORMAT_HANDLED, FORMAT_SUPPORT_MASK, FORMAT_UNSUPPORTED_DRM, FORMAT_UNSUPPORTED_SUBTYPE, FORMAT_UNSUPPORTED_TYPE, TUNNELING_NOT_SUPPORTED, TUNNELING_SUPPORT_MASK, TUNNELING_SUPPORTED
Constructor and Description |
---|
MediaCodecRenderer(int trackType,
MediaCodecSelector mediaCodecSelector,
DrmSessionManager<FrameworkMediaCrypto> drmSessionManager,
boolean playClearSamplesWithoutKeys) |
Modifier and Type | Method and Description |
---|---|
protected boolean |
canReconfigureCodec(android.media.MediaCodec codec,
boolean codecIsAdaptive,
Format oldFormat,
Format 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 abstract void |
configureCodec(MediaCodecInfo codecInfo,
android.media.MediaCodec codec,
Format format,
android.media.MediaCrypto crypto)
Configures a newly created
MediaCodec . |
protected void |
flushCodec() |
protected android.media.MediaCodec |
getCodec() |
protected MediaCodecInfo |
getCodecInfo() |
protected MediaCodecInfo |
getDecoderInfo(MediaCodecSelector mediaCodecSelector,
Format format,
boolean requiresSecureDecoder)
Returns a
MediaCodecInfo for a given format. |
protected long |
getDequeueOutputBufferTimeoutUs()
Returns the maximum time to block whilst waiting for a decoded output buffer.
|
boolean |
isEnded()
Whether the renderer is ready for the
ExoPlayer instance to transition to
Player.STATE_ENDED . |
boolean |
isReady()
Whether the renderer is able to immediately render media from the current position.
|
protected void |
maybeInitCodec() |
protected void |
onCodecInitialized(java.lang.String name,
long initializedTimestampMs,
long initializationDurationMs)
Called when a
MediaCodec has been created and configured. |
protected void |
onDisabled()
Called when the renderer is disabled.
|
protected void |
onEnabled(boolean joining)
Called when the renderer is enabled.
|
protected void |
onInputFormatChanged(Format newFormat)
Called when a new format is read from the upstream
MediaPeriod . |
protected void |
onOutputFormatChanged(android.media.MediaCodec codec,
android.media.MediaFormat outputFormat)
Called when the output format of the
MediaCodec changes. |
protected void |
onPositionReset(long positionUs,
boolean joining)
Called when the position is reset.
|
protected void |
onProcessedOutputBuffer(long presentationTimeUs)
Called when an output buffer is successfully processed.
|
protected void |
onQueueInputBuffer(DecoderInputBuffer buffer)
Called 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,
android.media.MediaCodec codec,
java.nio.ByteBuffer buffer,
int bufferIndex,
int bufferFlags,
long bufferPresentationTimeUs,
boolean shouldSkip)
Processes an output media buffer.
|
protected void |
releaseCodec() |
void |
render(long positionUs,
long elapsedRealtimeUs)
Incrementally renders the
SampleStream . |
protected void |
renderToEndOfStream()
Incrementally renders any remaining output.
|
protected boolean |
shouldInitCodec(MediaCodecInfo codecInfo) |
int |
supportsFormat(Format format)
Returns the extent to which the
Renderer supports a given format. |
protected abstract int |
supportsFormat(MediaCodecSelector mediaCodecSelector,
DrmSessionManager<FrameworkMediaCrypto> drmSessionManager,
Format format)
Returns the extent to which the renderer is capable of supporting a given format.
|
int |
supportsMixedMimeTypeAdaptation()
Returns the extent to which the
Renderer supports adapting between supported formats
that have different mime types. |
disable, enable, getCapabilities, getConfiguration, getIndex, getMediaClock, getState, getStream, getTrackType, handleMessage, hasReadStreamToEnd, isCurrentStreamFinal, isSourceReady, maybeThrowStreamError, onStreamChanged, readSource, replaceStream, resetPosition, setCurrentStreamFinal, setIndex, skipSource, start, stop, supportsFormatDrm
protected DecoderCounters decoderCounters
public MediaCodecRenderer(int trackType, MediaCodecSelector mediaCodecSelector, @Nullable DrmSessionManager<FrameworkMediaCrypto> drmSessionManager, boolean playClearSamplesWithoutKeys)
trackType
- The track type that the renderer handles. One of the C.TRACK_TYPE_*
constants defined in C
.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.public final int supportsMixedMimeTypeAdaptation()
RendererCapabilities
Renderer
supports adapting between supported formats
that have different mime types.supportsMixedMimeTypeAdaptation
in interface RendererCapabilities
supportsMixedMimeTypeAdaptation
in class BaseRenderer
RendererCapabilities.ADAPTIVE_SEAMLESS
, RendererCapabilities.ADAPTIVE_NOT_SEAMLESS
and
RendererCapabilities.ADAPTIVE_NOT_SUPPORTED
.public final int supportsFormat(Format format) throws ExoPlaybackException
RendererCapabilities
Renderer
supports a given format. The returned value is
the bitwise OR of three properties:
RendererCapabilities.FORMAT_HANDLED
,
RendererCapabilities.FORMAT_EXCEEDS_CAPABILITIES
, RendererCapabilities.FORMAT_UNSUPPORTED_DRM
,
RendererCapabilities.FORMAT_UNSUPPORTED_SUBTYPE
and RendererCapabilities.FORMAT_UNSUPPORTED_TYPE
.RendererCapabilities.ADAPTIVE_SEAMLESS
, RendererCapabilities.ADAPTIVE_NOT_SEAMLESS
and
RendererCapabilities.ADAPTIVE_NOT_SUPPORTED
. Only set if the level of support for the format itself is
RendererCapabilities.FORMAT_HANDLED
or RendererCapabilities.FORMAT_EXCEEDS_CAPABILITIES
.RendererCapabilities.TUNNELING_SUPPORTED
and
RendererCapabilities.TUNNELING_NOT_SUPPORTED
. Only set if the level of support for the format itself is
RendererCapabilities.FORMAT_HANDLED
or RendererCapabilities.FORMAT_EXCEEDS_CAPABILITIES
.RendererCapabilities.FORMAT_SUPPORT_MASK
, RendererCapabilities.ADAPTIVE_SUPPORT_MASK
and
RendererCapabilities.TUNNELING_SUPPORT_MASK
respectively.format
- The format.ExoPlaybackException
- If an error occurs.protected abstract int supportsFormat(MediaCodecSelector mediaCodecSelector, DrmSessionManager<FrameworkMediaCrypto> drmSessionManager, Format format) throws MediaCodecUtil.DecoderQueryException
mediaCodecSelector
- The decoder selector.drmSessionManager
- The renderer's DrmSessionManager
.format
- The format.supportsFormat(Format)
for more detail.MediaCodecUtil.DecoderQueryException
- If there was an error querying decoders.protected MediaCodecInfo getDecoderInfo(MediaCodecSelector mediaCodecSelector, Format format, boolean requiresSecureDecoder) throws MediaCodecUtil.DecoderQueryException
MediaCodecInfo
for a given format.mediaCodecSelector
- The decoder selector.format
- The format for which a decoder is required.requiresSecureDecoder
- Whether a secure decoder is required.MediaCodecInfo
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(MediaCodecInfo codecInfo, android.media.MediaCodec codec, Format format, android.media.MediaCrypto crypto) throws MediaCodecUtil.DecoderQueryException
MediaCodec
.codecInfo
- Information about the MediaCodec
being configured.codec
- The MediaCodec
to configure.format
- The format for which the codec is being configured.crypto
- For drm protected playbacks, a MediaCrypto
to use for decryption.MediaCodecUtil.DecoderQueryException
- If an error occurs querying codecInfo
.protected final void maybeInitCodec() throws ExoPlaybackException
ExoPlaybackException
protected boolean shouldInitCodec(MediaCodecInfo codecInfo)
protected final android.media.MediaCodec getCodec()
protected final MediaCodecInfo getCodecInfo()
protected void onEnabled(boolean joining) throws ExoPlaybackException
BaseRenderer
The default implementation is a no-op.
onEnabled
in class BaseRenderer
joining
- Whether this renderer is being enabled to join an ongoing playback.ExoPlaybackException
- If an error occurs.protected void onPositionReset(long positionUs, boolean joining) throws ExoPlaybackException
BaseRenderer
BaseRenderer.onStreamChanged(Format[], long)
has been called, and also when a position
discontinuity is encountered.
After a position reset, the renderer's SampleStream
is guaranteed to provide samples
starting from a key frame.
The default implementation is a no-op.
onPositionReset
in class BaseRenderer
positionUs
- The new playback position in microseconds.joining
- Whether this renderer is being enabled to join an ongoing playback.ExoPlaybackException
- If an error occurs.protected void onDisabled()
BaseRenderer
The default implementation is a no-op.
onDisabled
in class BaseRenderer
protected void releaseCodec()
protected void onStarted()
BaseRenderer
The default implementation is a no-op.
onStarted
in class BaseRenderer
protected void onStopped()
BaseRenderer
The default implementation is a no-op.
onStopped
in class BaseRenderer
public void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException
Renderer
SampleStream
.
If the renderer is in the Renderer.STATE_ENABLED
state then each call to this method will do
work toward being ready to render the SampleStream
when the renderer is started. It may
also render the very start of the media, for example the first frame of a video stream. If the
renderer is in the Renderer.STATE_STARTED
state then calls to this method will render the
SampleStream
in sync with the specified media positions.
This method should return quickly, and should not block if the renderer is unable to make useful progress.
This method may be called when the renderer is in the following states:
Renderer.STATE_ENABLED
, Renderer.STATE_STARTED
.
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.ExoPlaybackException
- If an error occurs.protected void flushCodec() throws ExoPlaybackException
ExoPlaybackException
protected void onCodecInitialized(java.lang.String name, long initializedTimestampMs, long initializationDurationMs)
MediaCodec
has been created and configured.
The default implementation is a no-op.
name
- The name of the codec that was initialized.initializedTimestampMs
- SystemClock.elapsedRealtime()
when initialization
finished.initializationDurationMs
- The time taken to initialize the codec in milliseconds.protected void onInputFormatChanged(Format newFormat) throws ExoPlaybackException
MediaPeriod
.newFormat
- The new format.ExoPlaybackException
- If an error occurs reinitializing the MediaCodec
.protected void onOutputFormatChanged(android.media.MediaCodec codec, android.media.MediaFormat outputFormat) throws ExoPlaybackException
MediaCodec
changes.
The default implementation is a no-op.
codec
- The MediaCodec
instance.outputFormat
- The new output format.ExoPlaybackException
- Thrown if an error occurs handling the new output format.protected void onQueueInputBuffer(DecoderInputBuffer buffer)
The default implementation is a no-op.
buffer
- The buffer to be queued.protected void onProcessedOutputBuffer(long presentationTimeUs)
The default implementation is a no-op.
presentationTimeUs
- The timestamp associated with the output buffer.protected boolean canReconfigureCodec(android.media.MediaCodec codec, boolean codecIsAdaptive, Format oldFormat, Format 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.public boolean isEnded()
Renderer
ExoPlayer
instance to transition to
Player.STATE_ENDED
. The player will make this transition as soon as true
is
returned by all of its Renderer
s.
This method may be called when the renderer is in the following states:
Renderer.STATE_ENABLED
, Renderer.STATE_STARTED
.
public boolean isReady()
Renderer
If the renderer is in the Renderer.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 Renderer.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:
Renderer.STATE_ENABLED
, Renderer.STATE_STARTED
.
protected long getDequeueOutputBufferTimeoutUs()
protected abstract boolean processOutputBuffer(long positionUs, long elapsedRealtimeUs, android.media.MediaCodec codec, java.nio.ByteBuffer buffer, int bufferIndex, int bufferFlags, long bufferPresentationTimeUs, boolean shouldSkip) throws ExoPlaybackException
When a new ByteBuffer
is passed to this method its position and limit delineate the
data to be processed. The return value indicates whether the buffer was processed in full. If
true is returned then the next call to this method will receive a new buffer to be processed.
If false is returned then the same buffer will be passed to the next call. An implementation of
this method is free to modify the buffer and can assume that the buffer will not be externally
modified between successive calls. Hence an implementation can, for example, modify the
buffer's position to keep track of how much of the data it has processed.
Note that the first call to this method following a call to
onPositionReset(long, boolean)
will always receive a new ByteBuffer
to be
processed.
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.codec
- The MediaCodec
instance.buffer
- The output buffer to process.bufferIndex
- The index of the output buffer.bufferFlags
- The flags attached to the output buffer.bufferPresentationTimeUs
- The presentation time of the output buffer in microseconds.shouldSkip
- Whether the buffer should be skipped (i.e. not rendered).ExoPlaybackException
- If an error occurs processing the output buffer.protected void renderToEndOfStream() throws ExoPlaybackException
The default implementation is a no-op.
ExoPlaybackException
- Thrown if an error occurs rendering remaining output.