public class MediaCodecVideoRenderer extends MediaCodecRenderer
MediaCodec.
This renderer accepts the following messages sent via ExoPlayer#createMessage(Target)
on the playback thread:
C.MSG_SET_SURFACE to set the output surface. The message payload
should be the target Surface, or null.
C.MSG_SET_SCALING_MODE to set the video scaling mode. The message
payload should be one of the integer scaling modes in C.VideoScalingMode. Note that
the scaling mode only applies if the Surface targeted by this renderer is owned by
a SurfaceView.
| Modifier and Type | Class and Description |
|---|---|
protected static class |
MediaCodecVideoRenderer.CodecMaxValues |
MediaCodecRenderer.DecoderInitializationExceptiondecoderCountersSTATE_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 |
|---|
MediaCodecVideoRenderer(android.content.Context context,
MediaCodecSelector mediaCodecSelector) |
MediaCodecVideoRenderer(android.content.Context context,
MediaCodecSelector mediaCodecSelector,
long allowedJoiningTimeMs) |
MediaCodecVideoRenderer(android.content.Context context,
MediaCodecSelector mediaCodecSelector,
long allowedJoiningTimeMs,
DrmSessionManager<FrameworkMediaCrypto> drmSessionManager,
boolean playClearSamplesWithoutKeys,
android.os.Handler eventHandler,
VideoRendererEventListener eventListener,
int maxDroppedFramesToNotify) |
MediaCodecVideoRenderer(android.content.Context context,
MediaCodecSelector mediaCodecSelector,
long allowedJoiningTimeMs,
android.os.Handler eventHandler,
VideoRendererEventListener eventListener,
int maxDroppedFrameCountToNotify) |
| 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 void |
configureCodec(MediaCodecInfo codecInfo,
android.media.MediaCodec codec,
Format format,
android.media.MediaCrypto crypto)
Configures a newly created
MediaCodec. |
protected void |
dropOutputBuffer(android.media.MediaCodec codec,
int index,
long presentationTimeUs)
Drops the output buffer with the specified index.
|
protected void |
flushCodec() |
protected MediaCodecVideoRenderer.CodecMaxValues |
getCodecMaxValues(MediaCodecInfo codecInfo,
Format format,
Format[] streamFormats)
Returns
MediaCodecVideoRenderer.CodecMaxValues suitable for configuring a codec for format in a way
that will allow possible adaptation to other compatible formats in streamFormats. |
protected android.media.MediaFormat |
getMediaFormat(Format format,
MediaCodecVideoRenderer.CodecMaxValues codecMaxValues,
boolean deviceNeedsAutoFrcWorkaround,
int tunnelingAudioSessionId)
Returns the framework
MediaFormat that should be used to configure the decoder when
playing media in the specified input format. |
void |
handleMessage(int messageType,
java.lang.Object message)
Handles a message delivered to the target.
|
boolean |
isReady()
Whether the renderer is able to immediately render media from the current position.
|
protected boolean |
maybeDropBuffersToKeyframe(android.media.MediaCodec codec,
int index,
long presentationTimeUs,
long positionUs)
Drops frames from the current output buffer to the next keyframe at or before the playback
position.
|
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 void |
onStreamChanged(Format[] formats,
long offsetUs)
Called when the renderer's stream has changed.
|
protected 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() |
protected void |
renderOutputBuffer(android.media.MediaCodec codec,
int index,
long presentationTimeUs)
Renders the output buffer with the specified index.
|
protected void |
renderOutputBufferV21(android.media.MediaCodec codec,
int index,
long presentationTimeUs,
long releaseTimeNs)
Renders the output buffer with the specified index.
|
protected boolean |
shouldDropBuffersToKeyframe(long earlyUs,
long elapsedRealtimeUs)
Returns whether to drop all buffers from the buffer being processed to the keyframe at or after
the current playback position, if possible.
|
protected boolean |
shouldDropOutputBuffer(long earlyUs,
long elapsedRealtimeUs)
Returns whether the buffer being processed should be dropped.
|
protected boolean |
shouldForceRenderOutputBuffer(long earlyUs,
long elapsedSinceLastRenderUs)
Returns whether to force rendering an output buffer.
|
protected boolean |
shouldInitCodec(MediaCodecInfo codecInfo) |
protected void |
skipOutputBuffer(android.media.MediaCodec codec,
int index,
long presentationTimeUs)
Skips the output buffer with the specified index.
|
protected int |
supportsFormat(MediaCodecSelector mediaCodecSelector,
DrmSessionManager<FrameworkMediaCrypto> drmSessionManager,
Format format)
Returns the extent to which the renderer is capable of supporting a given format.
|
protected void |
updateDroppedBufferCounters(int droppedBufferCount)
Updates decoder counters to reflect that
droppedBufferCount additional buffers were
dropped. |
getCodec, getCodecInfo, getDecoderInfo, getDequeueOutputBufferTimeoutUs, getMediaFormatForPlayback, isEnded, maybeInitCodec, render, renderToEndOfStream, supportsFormat, supportsMixedMimeTypeAdaptationdisable, enable, getCapabilities, getConfiguration, getIndex, getMediaClock, getState, getStream, getTrackType, hasReadStreamToEnd, isCurrentStreamFinal, isSourceReady, maybeThrowStreamError, readSource, replaceStream, resetPosition, setCurrentStreamFinal, setIndex, skipSource, start, stop, supportsFormatDrmpublic MediaCodecVideoRenderer(android.content.Context context,
MediaCodecSelector mediaCodecSelector)
context - A context.mediaCodecSelector - A decoder selector.public MediaCodecVideoRenderer(android.content.Context context,
MediaCodecSelector mediaCodecSelector,
long allowedJoiningTimeMs)
context - A context.mediaCodecSelector - A decoder selector.allowedJoiningTimeMs - The maximum duration in milliseconds for which this video renderer
can attempt to seamlessly join an ongoing playback.public MediaCodecVideoRenderer(android.content.Context context,
MediaCodecSelector mediaCodecSelector,
long allowedJoiningTimeMs,
@Nullable
android.os.Handler eventHandler,
@Nullable
VideoRendererEventListener eventListener,
int maxDroppedFrameCountToNotify)
context - A context.mediaCodecSelector - A decoder selector.allowedJoiningTimeMs - The maximum duration in milliseconds for which this video renderer
can attempt to seamlessly join an ongoing playback.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.maxDroppedFrameCountToNotify - The maximum number of frames that can be dropped between
invocations of VideoRendererEventListener.onDroppedFrames(int, long).public MediaCodecVideoRenderer(android.content.Context context,
MediaCodecSelector mediaCodecSelector,
long allowedJoiningTimeMs,
@Nullable
DrmSessionManager<FrameworkMediaCrypto> drmSessionManager,
boolean playClearSamplesWithoutKeys,
@Nullable
android.os.Handler eventHandler,
@Nullable
VideoRendererEventListener eventListener,
int maxDroppedFramesToNotify)
context - A context.mediaCodecSelector - A decoder selector.allowedJoiningTimeMs - The maximum duration in milliseconds for which this video renderer
can attempt to seamlessly join an ongoing playback.drmSessionManager - For use with encrypted content. May be null if support for encrypted
content 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.maxDroppedFramesToNotify - The maximum number of frames that can be dropped between
invocations of VideoRendererEventListener.onDroppedFrames(int, long).protected int supportsFormat(MediaCodecSelector mediaCodecSelector, DrmSessionManager<FrameworkMediaCrypto> drmSessionManager, Format format) throws MediaCodecUtil.DecoderQueryException
MediaCodecRenderersupportsFormat in class MediaCodecRenderermediaCodecSelector - The decoder selector.drmSessionManager - The renderer's DrmSessionManager.format - The format.MediaCodecRenderer.supportsFormat(Format) for more detail.MediaCodecUtil.DecoderQueryException - If there was an error querying decoders.protected void onEnabled(boolean joining)
throws ExoPlaybackException
BaseRendererThe default implementation is a no-op.
onEnabled in class MediaCodecRendererjoining - Whether this renderer is being enabled to join an ongoing playback.ExoPlaybackException - If an error occurs.protected void onStreamChanged(Format[] formats, long offsetUs) throws ExoPlaybackException
BaseRendererBaseRenderer.onEnabled(boolean) has been called, and also when the stream has been replaced whilst
the renderer is enabled or started.
The default implementation is a no-op.
onStreamChanged in class BaseRendererformats - The enabled formats.offsetUs - The offset that will be added to the timestamps of buffers read via
BaseRenderer.readSource(FormatHolder, DecoderInputBuffer, boolean) so that decoder input
buffers have monotonically increasing timestamps.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 MediaCodecRendererpositionUs - The new playback position in microseconds.joining - Whether this renderer is being enabled to join an ongoing playback.ExoPlaybackException - If an error occurs.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.
isReady in interface RendererisReady in class MediaCodecRendererprotected void onStarted()
BaseRendererThe default implementation is a no-op.
onStarted in class MediaCodecRendererprotected void onStopped()
BaseRendererThe default implementation is a no-op.
onStopped in class MediaCodecRendererprotected void onDisabled()
BaseRendererThe default implementation is a no-op.
onDisabled in class MediaCodecRendererpublic void handleMessage(int messageType,
java.lang.Object message)
throws ExoPlaybackException
PlayerMessage.TargethandleMessage in interface PlayerMessage.TargethandleMessage in class BaseRenderermessageType - The message type.message - The message payload.ExoPlaybackException - If an error occurred whilst handling the message. Should only be
thrown by targets that handle messages on the playback thread.protected boolean shouldInitCodec(MediaCodecInfo codecInfo)
shouldInitCodec in class MediaCodecRendererprotected void configureCodec(MediaCodecInfo codecInfo, android.media.MediaCodec codec, Format format, android.media.MediaCrypto crypto) throws MediaCodecUtil.DecoderQueryException
MediaCodecRendererMediaCodec.configureCodec in class MediaCodecRenderercodecInfo - 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.@CallSuper protected void releaseCodec()
releaseCodec in class MediaCodecRenderer@CallSuper
protected void flushCodec()
throws ExoPlaybackException
flushCodec in class MediaCodecRendererExoPlaybackExceptionprotected void onCodecInitialized(java.lang.String name,
long initializedTimestampMs,
long initializationDurationMs)
MediaCodecRendererMediaCodec has been created and configured.
The default implementation is a no-op.
onCodecInitialized in class MediaCodecRenderername - 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
MediaCodecRendererMediaPeriod.onInputFormatChanged in class MediaCodecRenderernewFormat - The new format.ExoPlaybackException - If an error occurs reinitializing the MediaCodec.@CallSuper protected void onQueueInputBuffer(DecoderInputBuffer buffer)
onQueueInputBuffer in class MediaCodecRendererbuffer - The buffer to be queued.protected void onOutputFormatChanged(android.media.MediaCodec codec,
android.media.MediaFormat outputFormat)
MediaCodecRendererMediaCodec changes.
The default implementation is a no-op.
onOutputFormatChanged in class MediaCodecRenderercodec - The MediaCodec instance.outputFormat - The new output format.protected boolean canReconfigureCodec(android.media.MediaCodec codec,
boolean codecIsAdaptive,
Format oldFormat,
Format newFormat)
MediaCodecRendererMediaCodec 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.
canReconfigureCodec in class MediaCodecRenderercodec - 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 processOutputBuffer(long positionUs,
long elapsedRealtimeUs,
android.media.MediaCodec codec,
java.nio.ByteBuffer buffer,
int bufferIndex,
int bufferFlags,
long bufferPresentationTimeUs,
boolean shouldSkip)
throws ExoPlaybackException
MediaCodecRenderer
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
MediaCodecRenderer.onPositionReset(long, boolean) will always receive a new ByteBuffer to be
processed.
processOutputBuffer in class MediaCodecRendererpositionUs - 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.@CallSuper protected void onProcessedOutputBuffer(long presentationTimeUs)
onProcessedOutputBuffer in class MediaCodecRendererpresentationTimeUs - The timestamp associated with the output buffer.protected boolean shouldDropOutputBuffer(long earlyUs,
long elapsedRealtimeUs)
earlyUs - The time until the buffer should be presented in microseconds. A negative value
indicates that the buffer is late.elapsedRealtimeUs - SystemClock.elapsedRealtime() in microseconds,
measured at the start of the current iteration of the rendering loop.protected boolean shouldDropBuffersToKeyframe(long earlyUs,
long elapsedRealtimeUs)
earlyUs - The time until the current buffer should be presented in microseconds. A
negative value indicates that the buffer is late.elapsedRealtimeUs - SystemClock.elapsedRealtime() in microseconds,
measured at the start of the current iteration of the rendering loop.protected boolean shouldForceRenderOutputBuffer(long earlyUs,
long elapsedSinceLastRenderUs)
earlyUs - The time until the current buffer should be presented in microseconds. A
negative value indicates that the buffer is late.elapsedSinceLastRenderUs - The elapsed time since the last output buffer was rendered, in
microseconds.protected void skipOutputBuffer(android.media.MediaCodec codec,
int index,
long presentationTimeUs)
codec - The codec that owns the output buffer.index - The index of the output buffer to skip.presentationTimeUs - The presentation time of the output buffer, in microseconds.protected void dropOutputBuffer(android.media.MediaCodec codec,
int index,
long presentationTimeUs)
codec - The codec that owns the output buffer.index - The index of the output buffer to drop.presentationTimeUs - The presentation time of the output buffer, in microseconds.protected boolean maybeDropBuffersToKeyframe(android.media.MediaCodec codec,
int index,
long presentationTimeUs,
long positionUs)
throws ExoPlaybackException
false. Returns true otherwise.codec - The codec that owns the output buffer.index - The index of the output buffer to drop.presentationTimeUs - The presentation time of the output buffer, in microseconds.positionUs - The current playback position, in microseconds.ExoPlaybackException - If an error occurs flushing the codec.protected void updateDroppedBufferCounters(int droppedBufferCount)
droppedBufferCount additional buffers were
dropped.droppedBufferCount - The number of additional dropped buffers.protected void renderOutputBuffer(android.media.MediaCodec codec,
int index,
long presentationTimeUs)
codec - The codec that owns the output buffer.index - The index of the output buffer to drop.presentationTimeUs - The presentation time of the output buffer, in microseconds.protected void renderOutputBufferV21(android.media.MediaCodec codec,
int index,
long presentationTimeUs,
long releaseTimeNs)
codec - The codec that owns the output buffer.index - The index of the output buffer to drop.presentationTimeUs - The presentation time of the output buffer, in microseconds.releaseTimeNs - The wallclock time at which the frame should be displayed, in nanoseconds.protected MediaCodecVideoRenderer.CodecMaxValues getCodecMaxValues(MediaCodecInfo codecInfo, Format format, Format[] streamFormats) throws MediaCodecUtil.DecoderQueryException
MediaCodecVideoRenderer.CodecMaxValues suitable for configuring a codec for format in a way
that will allow possible adaptation to other compatible formats in streamFormats.codecInfo - Information about the MediaCodec being configured.format - The format for which the codec is being configured.streamFormats - The possible stream formats.MediaCodecVideoRenderer.CodecMaxValues.MediaCodecUtil.DecoderQueryException - If an error occurs querying codecInfo.protected android.media.MediaFormat getMediaFormat(Format format, MediaCodecVideoRenderer.CodecMaxValues codecMaxValues, boolean deviceNeedsAutoFrcWorkaround, int tunnelingAudioSessionId)
MediaFormat that should be used to configure the decoder when
playing media in the specified input format.format - The format of input media.codecMaxValues - The codec's maximum supported values.deviceNeedsAutoFrcWorkaround - Whether the device is known to enable frame-rate conversion
logic that negatively impacts ExoPlayer.tunnelingAudioSessionId - The audio session id to use for tunneling, or
C.AUDIO_SESSION_ID_UNSET if tunneling should not be enabled.MediaFormat that should be used to configure the decoder.