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.
|
protected static interface |
MediaCodecRenderer.KeepCodecResult
The possible return values for
canKeepCodec(MediaCodec, MediaCodecInfo, Format,
Format). |
Renderer.State| Modifier and Type | Field and Description |
|---|---|
protected DecoderCounters |
decoderCounters |
protected static int |
KEEP_CODEC_RESULT_NO
The codec cannot be kept.
|
protected static int |
KEEP_CODEC_RESULT_YES_WITH_RECONFIGURATION
The codec can be kept, but must be reconfigured by prefixing the next input buffer with the new
format's configuration data.
|
protected static int |
KEEP_CODEC_RESULT_YES_WITHOUT_RECONFIGURATION
The codec can be kept.
|
STATE_DISABLED, STATE_ENABLED, STATE_STARTEDADAPTIVE_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 int |
canKeepCodec(android.media.MediaCodec codec,
MediaCodecInfo codecInfo,
Format oldFormat,
Format newFormat)
Determines whether the existing
MediaCodec can be kept for a new format, and if it can
whether it requires reconfiguration. |
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, getStreamFormats, getTrackType, handleMessage, hasReadStreamToEnd, isCurrentStreamFinal, isSourceReady, maybeThrowStreamError, onStreamChanged, readSource, replaceStream, resetPosition, setCurrentStreamFinal, setIndex, skipSource, start, stop, supportsFormatDrmprotected static final int KEEP_CODEC_RESULT_NO
protected static final int KEEP_CODEC_RESULT_YES_WITHOUT_RECONFIGURATION
protected static final int KEEP_CODEC_RESULT_YES_WITH_RECONFIGURATION
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()
RendererCapabilitiesRenderer supports adapting between supported formats
that have different mime types.supportsMixedMimeTypeAdaptation in interface RendererCapabilitiessupportsMixedMimeTypeAdaptation in class BaseRendererRendererCapabilities.ADAPTIVE_SEAMLESS, RendererCapabilities.ADAPTIVE_NOT_SEAMLESS and
RendererCapabilities.ADAPTIVE_NOT_SUPPORTED.public final int supportsFormat(Format format) throws ExoPlaybackException
RendererCapabilitiesRenderer 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
ExoPlaybackExceptionprotected boolean shouldInitCodec(MediaCodecInfo codecInfo)
protected final android.media.MediaCodec getCodec()
protected final MediaCodecInfo getCodecInfo()
protected void onEnabled(boolean joining)
throws ExoPlaybackException
BaseRendererThe default implementation is a no-op.
onEnabled in class BaseRendererjoining - 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
BaseRendererBaseRenderer.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 BaseRendererpositionUs - 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()
BaseRendererThe default implementation is a no-op.
onDisabled in class BaseRendererprotected void releaseCodec()
protected void onStarted()
BaseRendererThe default implementation is a no-op.
onStarted in class BaseRendererprotected void onStopped()
BaseRendererThe default implementation is a no-op.
onStopped in class BaseRendererpublic void render(long positionUs,
long elapsedRealtimeUs)
throws ExoPlaybackException
RendererSampleStream.
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
ExoPlaybackExceptionprotected 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 int canKeepCodec(android.media.MediaCodec codec,
MediaCodecInfo codecInfo,
Format oldFormat,
Format newFormat)
MediaCodec can be kept for a new format, and if it can
whether it requires reconfiguration.
The default implementation returns KEEP_CODEC_RESULT_NO.
codec - The existing MediaCodec instance.codecInfo - A MediaCodecInfo describing the decoder.oldFormat - The format for which the existing instance is configured.newFormat - The new format.public boolean isEnded()
RendererExoPlayer instance to transition to
Player.STATE_ENDED. The player will make this transition as soon as true is
returned by all of its Renderers.
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.