Class MediaCodecAudioRenderer

    • Constructor Detail

      • MediaCodecAudioRenderer

        public MediaCodecAudioRenderer​(Context context,
                                       MediaCodecSelector mediaCodecSelector)
        Parameters:
        context - A context.
        mediaCodecSelector - A decoder selector.
      • MediaCodecAudioRenderer

        public MediaCodecAudioRenderer​(Context context,
                                       MediaCodecSelector mediaCodecSelector,
                                       @Nullable
                                       Handler eventHandler,
                                       @Nullable
                                       AudioRendererEventListener eventListener)
        Parameters:
        context - A context.
        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.
      • MediaCodecAudioRenderer

        public MediaCodecAudioRenderer​(Context context,
                                       MediaCodecSelector mediaCodecSelector,
                                       @Nullable
                                       Handler eventHandler,
                                       @Nullable
                                       AudioRendererEventListener eventListener,
                                       @Nullable
                                       AudioCapabilities audioCapabilities,
                                       AudioProcessor... audioProcessors)
        Parameters:
        context - A context.
        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.
        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.
      • MediaCodecAudioRenderer

        public MediaCodecAudioRenderer​(Context context,
                                       MediaCodecSelector mediaCodecSelector,
                                       @Nullable
                                       Handler eventHandler,
                                       @Nullable
                                       AudioRendererEventListener eventListener,
                                       AudioSink audioSink)
        Parameters:
        context - A context.
        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.
        audioSink - The sink to which audio will be output.
      • MediaCodecAudioRenderer

        public MediaCodecAudioRenderer​(Context context,
                                       MediaCodecSelector mediaCodecSelector,
                                       boolean enableDecoderFallback,
                                       @Nullable
                                       Handler eventHandler,
                                       @Nullable
                                       AudioRendererEventListener eventListener,
                                       AudioSink audioSink)
        Parameters:
        context - A context.
        mediaCodecSelector - A decoder selector.
        enableDecoderFallback - Whether to enable fallback to lower-priority decoders if decoder initialization fails. This may result in using a decoder that is slower/less efficient than the primary decoder.
        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.
      • MediaCodecAudioRenderer

        public MediaCodecAudioRenderer​(Context context,
                                       MediaCodecAdapter.Factory codecAdapterFactory,
                                       MediaCodecSelector mediaCodecSelector,
                                       boolean enableDecoderFallback,
                                       @Nullable
                                       Handler eventHandler,
                                       @Nullable
                                       AudioRendererEventListener eventListener,
                                       AudioSink audioSink)
        Creates a new instance.
        Parameters:
        context - A context.
        codecAdapterFactory - The MediaCodecAdapter.Factory used to create MediaCodecAdapter instances.
        mediaCodecSelector - A decoder selector.
        enableDecoderFallback - Whether to enable fallback to lower-priority decoders if decoder initialization fails. This may result in using a decoder that is slower/less efficient than the primary decoder.
        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.
    • Method Detail

      • getName

        public String getName()
        Description copied from interface: Renderer
        Returns the name of this renderer, for logging and debugging purposes. Should typically be the renderer's (un-obfuscated) class name.
        Specified by:
        getName in interface Renderer
        Specified by:
        getName in interface RendererCapabilities
        Returns:
        The name of this renderer.
      • experimentalSetEnableKeepAudioTrackOnSeek

        public void experimentalSetEnableKeepAudioTrackOnSeek​(boolean enableKeepAudioTrackOnSeek)
        Sets whether to enable the experimental feature that keeps and flushes the AudioTrack when a seek occurs, as opposed to releasing and reinitialising. Off by default.

        This method is experimental, and will be renamed or removed in a future release.

        Parameters:
        enableKeepAudioTrackOnSeek - Whether to keep the AudioTrack on seek.
      • shouldUseBypass

        protected boolean shouldUseBypass​(Format format)
        Description copied from class: MediaCodecRenderer
        Returns whether buffers in the input format can be processed without a codec.

        This method is only called if the content is not DRM protected, because if the content is DRM protected use of bypass is never possible.

        Overrides:
        shouldUseBypass in class MediaCodecRenderer
        Parameters:
        format - The input Format.
        Returns:
        Whether playback bypassing MediaCodec is supported.
      • getMediaClock

        @Nullable
        public MediaClock getMediaClock()
        Description copied from interface: Renderer
        If the renderer advances its own playback position then this method returns a corresponding MediaClock. 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.
        Specified by:
        getMediaClock in interface Renderer
        Overrides:
        getMediaClock in class BaseRenderer
        Returns:
        The MediaClock tracking the playback position of the renderer, or null.
      • onCodecInitialized

        protected void onCodecInitialized​(String name,
                                          long initializedTimestampMs,
                                          long initializationDurationMs)
        Description copied from class: MediaCodecRenderer
        Called when a MediaCodec has been created and configured.

        The default implementation is a no-op.

        Overrides:
        onCodecInitialized in class MediaCodecRenderer
        Parameters:
        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.
      • onCodecError

        protected void onCodecError​(Exception codecError)
        Description copied from class: MediaCodecRenderer
        Called when a codec error has occurred.

        The default implementation is a no-op.

        Overrides:
        onCodecError in class MediaCodecRenderer
        Parameters:
        codecError - The error.
      • onOutputFormatChanged

        protected void onOutputFormatChanged​(Format format,
                                             @Nullable
                                             MediaFormat mediaFormat)
                                      throws ExoPlaybackException
        Description copied from class: MediaCodecRenderer
        Called when one of the output formats changes.

        The default implementation is a no-op.

        Overrides:
        onOutputFormatChanged in class MediaCodecRenderer
        Parameters:
        format - The input Format to which future output now corresponds. If the renderer is in bypass mode, this is also the output format.
        mediaFormat - The codec output MediaFormat, or null if the renderer is in bypass mode.
        Throws:
        ExoPlaybackException - Thrown if an error occurs configuring the output.
      • onEnabled

        protected void onEnabled​(boolean joining,
                                 boolean mayRenderStartOfStream)
                          throws ExoPlaybackException
        Description copied from class: BaseRenderer
        Called when the renderer is enabled.

        The default implementation is a no-op.

        Overrides:
        onEnabled in class MediaCodecRenderer
        Parameters:
        joining - Whether this renderer is being enabled to join an ongoing playback.
        mayRenderStartOfStream - Whether this renderer is allowed to render the start of the stream even if the state is not Renderer.STATE_STARTED yet.
        Throws:
        ExoPlaybackException - If an error occurs.
      • onPositionReset

        protected void onPositionReset​(long positionUs,
                                       boolean joining)
                                throws ExoPlaybackException
        Description copied from class: BaseRenderer
        Called when the position is reset. This occurs when the renderer is enabled after BaseRenderer.onStreamChanged(Format[], long, 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.

        Overrides:
        onPositionReset in class MediaCodecRenderer
        Parameters:
        positionUs - The new playback position in microseconds.
        joining - Whether this renderer is being enabled to join an ongoing playback.
        Throws:
        ExoPlaybackException - If an error occurs.
      • onStarted

        protected void onStarted()
        Description copied from class: BaseRenderer
        Called when the renderer is started.

        The default implementation is a no-op.

        Overrides:
        onStarted in class MediaCodecRenderer
      • onStopped

        protected void onStopped()
        Description copied from class: BaseRenderer
        Called when the renderer is stopped.

        The default implementation is a no-op.

        Overrides:
        onStopped in class MediaCodecRenderer
      • onDisabled

        protected void onDisabled()
        Description copied from class: BaseRenderer
        Called when the renderer is disabled.

        The default implementation is a no-op.

        Overrides:
        onDisabled in class MediaCodecRenderer
      • onReset

        protected void onReset()
        Description copied from class: BaseRenderer
        Called when the renderer is reset.

        The default implementation is a no-op.

        Overrides:
        onReset in class MediaCodecRenderer
      • isReady

        public boolean isReady()
        Description copied from interface: Renderer
        Whether the renderer is able to immediately render media from the current position.

        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.

        Specified by:
        isReady in interface Renderer
        Overrides:
        isReady in class MediaCodecRenderer
        Returns:
        Whether the renderer is ready to render media.
      • getPositionUs

        public long getPositionUs()
        Description copied from interface: MediaClock
        Returns the current media position in microseconds.
        Specified by:
        getPositionUs in interface MediaClock
      • setPlaybackParameters

        public void setPlaybackParameters​(PlaybackParameters playbackParameters)
        Description copied from interface: MediaClock
        Attempts to set the playback parameters. The media clock may override the speed if changing the playback parameters is not supported.
        Specified by:
        setPlaybackParameters in interface MediaClock
        Parameters:
        playbackParameters - The playback parameters to attempt to set.
      • processOutputBuffer

        protected boolean processOutputBuffer​(long positionUs,
                                              long elapsedRealtimeUs,
                                              @Nullable
                                              MediaCodecAdapter codec,
                                              @Nullable
                                              ByteBuffer buffer,
                                              int bufferIndex,
                                              int bufferFlags,
                                              int sampleCount,
                                              long bufferPresentationTimeUs,
                                              boolean isDecodeOnlyBuffer,
                                              boolean isLastBuffer,
                                              Format format)
                                       throws ExoPlaybackException
        Description copied from class: MediaCodecRenderer
        Processes an output media buffer.

        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.

        Specified by:
        processOutputBuffer in class MediaCodecRenderer
        Parameters:
        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 MediaCodecAdapter instance, or null in bypass mode were no codec is used.
        buffer - The output buffer to process, or null if the buffer data is not made available to the application layer (see MediaCodec.getOutputBuffer(int)). This buffer can only be null for video data. Note that the buffer data can still be rendered in this case by using the bufferIndex.
        bufferIndex - The index of the output buffer.
        bufferFlags - The flags attached to the output buffer.
        sampleCount - The number of samples extracted from the sample queue in the buffer. This allows handling multiple samples as a batch for efficiency.
        bufferPresentationTimeUs - The presentation time of the output buffer in microseconds.
        isDecodeOnlyBuffer - Whether the buffer was marked with C.BUFFER_FLAG_DECODE_ONLY by the source.
        isLastBuffer - Whether the buffer is known to contain the last sample of the current stream. This flag is set on a best effort basis, and any logic relying on it should degrade gracefully to handle cases where it's not set.
        format - The Format associated with the buffer.
        Returns:
        Whether the output buffer was fully processed (for example, rendered or skipped).
        Throws:
        ExoPlaybackException - If an error occurs processing the output buffer.
      • getCodecMaxInputSize

        protected int getCodecMaxInputSize​(MediaCodecInfo codecInfo,
                                           Format format,
                                           Format[] streamFormats)
        Returns a maximum input size suitable for configuring a codec for format in a way that will allow possible adaptation to other compatible formats in streamFormats.
        Parameters:
        codecInfo - A MediaCodecInfo describing the decoder.
        format - The Format for which the codec is being configured.
        streamFormats - The possible stream formats.
        Returns:
        A suitable maximum input size.
      • getMediaFormat

        protected MediaFormat getMediaFormat​(Format format,
                                             String codecMimeType,
                                             int codecMaxInputSize,
                                             float codecOperatingRate)
        Returns the framework MediaFormat that can be used to configure a MediaCodec for decoding the given Format for playback.
        Parameters:
        format - The Format of the media.
        codecMimeType - The MIME type handled by the codec.
        codecMaxInputSize - The maximum input size supported by the codec.
        codecOperatingRate - The codec operating rate, or MediaCodecRenderer.CODEC_OPERATING_RATE_UNSET if no codec operating rate should be set.
        Returns:
        The framework MediaFormat.