public class MediaCodecAudioRenderer extends MediaCodecRenderer implements MediaClock
MediaCodec and an AudioSink.
This renderer accepts the following messages sent via ExoPlayer#createMessage(Target)
on the playback thread:
C.MSG_SET_VOLUME to set the volume. The message payload should be
a Float with 0 being silence and 1 being unity gain.
C.MSG_SET_AUDIO_ATTRIBUTES to set the audio attributes. The
message payload should be an AudioAttributes
instance that will configure the underlying audio track.
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 |
|---|
MediaCodecAudioRenderer(MediaCodecSelector mediaCodecSelector) |
MediaCodecAudioRenderer(MediaCodecSelector mediaCodecSelector,
DrmSessionManager<FrameworkMediaCrypto> drmSessionManager,
boolean playClearSamplesWithoutKeys) |
MediaCodecAudioRenderer(MediaCodecSelector mediaCodecSelector,
DrmSessionManager<FrameworkMediaCrypto> drmSessionManager,
boolean playClearSamplesWithoutKeys,
android.os.Handler eventHandler,
AudioRendererEventListener eventListener) |
MediaCodecAudioRenderer(MediaCodecSelector mediaCodecSelector,
DrmSessionManager<FrameworkMediaCrypto> drmSessionManager,
boolean playClearSamplesWithoutKeys,
android.os.Handler eventHandler,
AudioRendererEventListener eventListener,
AudioCapabilities audioCapabilities,
AudioProcessor... audioProcessors) |
MediaCodecAudioRenderer(MediaCodecSelector mediaCodecSelector,
DrmSessionManager<FrameworkMediaCrypto> drmSessionManager,
boolean playClearSamplesWithoutKeys,
android.os.Handler eventHandler,
AudioRendererEventListener eventListener,
AudioSink audioSink) |
MediaCodecAudioRenderer(MediaCodecSelector mediaCodecSelector,
android.os.Handler eventHandler,
AudioRendererEventListener eventListener) |
| Modifier and Type | Method and Description |
|---|---|
protected boolean |
allowPassthrough(java.lang.String mimeType)
Returns whether encoded audio passthrough should be used for playing back the input format.
|
protected void |
configureCodec(MediaCodecInfo codecInfo,
android.media.MediaCodec codec,
Format format,
android.media.MediaCrypto crypto)
Configures a newly created
MediaCodec. |
protected MediaCodecInfo |
getDecoderInfo(MediaCodecSelector mediaCodecSelector,
Format format,
boolean requiresSecureDecoder)
Returns a
MediaCodecInfo for a given format. |
MediaClock |
getMediaClock()
If the renderer advances its own playback position then this method returns a corresponding
MediaClock. |
PlaybackParameters |
getPlaybackParameters()
Returns the active playback parameters.
|
long |
getPositionUs()
Returns the current media position in microseconds.
|
void |
handleMessage(int messageType,
java.lang.Object message)
Handles a message delivered to the target.
|
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 |
onAudioSessionId(int audioSessionId)
Called when the audio session id becomes known.
|
protected void |
onAudioTrackPositionDiscontinuity() |
protected void |
onAudioTrackUnderrun(int bufferSize,
long bufferSizeMs,
long elapsedSinceLastFeedMs) |
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 |
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 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 |
renderToEndOfStream()
Incrementally renders any remaining output.
|
PlaybackParameters |
setPlaybackParameters(PlaybackParameters playbackParameters)
Attempts to set the playback parameters and returns the active playback parameters, which may
differ from those passed in.
|
protected int |
supportsFormat(MediaCodecSelector mediaCodecSelector,
DrmSessionManager<FrameworkMediaCrypto> drmSessionManager,
Format format)
Returns the extent to which the renderer is capable of supporting a given format.
|
canReconfigureCodec, flushCodec, getCodec, getCodecInfo, getDequeueOutputBufferTimeoutUs, getMediaFormatForPlayback, maybeInitCodec, onProcessedOutputBuffer, releaseCodec, render, shouldInitCodec, supportsFormat, supportsMixedMimeTypeAdaptationdisable, enable, getCapabilities, getConfiguration, getIndex, getState, getStream, getTrackType, hasReadStreamToEnd, isCurrentStreamFinal, isSourceReady, maybeThrowStreamError, onStreamChanged, readSource, replaceStream, resetPosition, setCurrentStreamFinal, setIndex, skipSource, start, stop, supportsFormatDrmpublic MediaCodecAudioRenderer(MediaCodecSelector mediaCodecSelector)
mediaCodecSelector - A decoder selector.public MediaCodecAudioRenderer(MediaCodecSelector mediaCodecSelector, @Nullable DrmSessionManager<FrameworkMediaCrypto> drmSessionManager, boolean playClearSamplesWithoutKeys)
mediaCodecSelector - A decoder selector.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.public MediaCodecAudioRenderer(MediaCodecSelector mediaCodecSelector, @Nullable android.os.Handler eventHandler, @Nullable AudioRendererEventListener eventListener)
mediaCodecSelector - A decoder selector.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 MediaCodecAudioRenderer(MediaCodecSelector mediaCodecSelector, @Nullable DrmSessionManager<FrameworkMediaCrypto> drmSessionManager, boolean playClearSamplesWithoutKeys, @Nullable android.os.Handler eventHandler, @Nullable AudioRendererEventListener eventListener)
mediaCodecSelector - A decoder selector.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.public MediaCodecAudioRenderer(MediaCodecSelector mediaCodecSelector, @Nullable DrmSessionManager<FrameworkMediaCrypto> drmSessionManager, boolean playClearSamplesWithoutKeys, @Nullable android.os.Handler eventHandler, @Nullable AudioRendererEventListener eventListener, @Nullable AudioCapabilities audioCapabilities, AudioProcessor... audioProcessors)
mediaCodecSelector - A decoder selector.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.audioCapabilities - The audio capabilities for playback on this device. May be null if the
default capabilities (no encoded audio passthrough support) should be assumed.audioProcessors - Optional AudioProcessors that will process PCM audio before
output.public MediaCodecAudioRenderer(MediaCodecSelector mediaCodecSelector, @Nullable DrmSessionManager<FrameworkMediaCrypto> drmSessionManager, boolean playClearSamplesWithoutKeys, @Nullable android.os.Handler eventHandler, @Nullable AudioRendererEventListener eventListener, AudioSink audioSink)
mediaCodecSelector - A decoder selector.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.audioSink - The sink to which audio will be output.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 MediaCodecInfo getDecoderInfo(MediaCodecSelector mediaCodecSelector, Format format, boolean requiresSecureDecoder) throws MediaCodecUtil.DecoderQueryException
MediaCodecRendererMediaCodecInfo for a given format.getDecoderInfo in class MediaCodecRenderermediaCodecSelector - 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 boolean allowPassthrough(java.lang.String mimeType)
AudioSink indicates that encoded audio output
is supported.mimeType - The type of input media.protected void configureCodec(MediaCodecInfo codecInfo, android.media.MediaCodec codec, Format format, android.media.MediaCrypto crypto)
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.public MediaClock getMediaClock()
RendererMediaClock. If provided, the player will use the returned MediaClock as its
source of time during playback. A player may have at most one renderer that returns a
MediaClock from this method.getMediaClock in interface RenderergetMediaClock in class BaseRendererMediaClock tracking the playback position of the renderer, or null.protected 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.protected void onOutputFormatChanged(android.media.MediaCodec codec,
android.media.MediaFormat outputFormat)
throws ExoPlaybackException
MediaCodecRendererMediaCodec changes.
The default implementation is a no-op.
onOutputFormatChanged in class MediaCodecRenderercodec - The MediaCodec instance.outputFormat - The new output format.ExoPlaybackException - Thrown if an error occurs handling the new output format.protected void onAudioSessionId(int audioSessionId)
Virtualizer in
order to spatialize the audio channels. For this use case, any Virtualizer instances
should be released in onDisabled() (if not before).AudioSink.Listener.onAudioSessionId(int)protected void onAudioTrackPositionDiscontinuity()
protected void onAudioTrackUnderrun(int bufferSize,
long bufferSizeMs,
long elapsedSinceLastFeedMs)
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 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.protected 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 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.
isEnded in interface RendererisEnded in class MediaCodecRendererpublic 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 MediaCodecRendererpublic long getPositionUs()
MediaClockgetPositionUs in interface MediaClockpublic PlaybackParameters setPlaybackParameters(PlaybackParameters playbackParameters)
MediaClocksetPlaybackParameters in interface MediaClockplaybackParameters - The playback parameters.public PlaybackParameters getPlaybackParameters()
MediaClockgetPlaybackParameters in interface MediaClockprotected void onQueueInputBuffer(DecoderInputBuffer buffer)
MediaCodecRendererThe default implementation is a no-op.
onQueueInputBuffer in class MediaCodecRendererbuffer - The buffer to be queued.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.protected void renderToEndOfStream()
throws ExoPlaybackException
MediaCodecRendererThe default implementation is a no-op.
renderToEndOfStream in class MediaCodecRendererExoPlaybackException - Thrown if an error occurs rendering remaining output.public 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.