MediaCodecAdapter


@UnstableApi
public interface MediaCodecAdapter

Known direct subclasses
SynchronousMediaCodecAdapter

A MediaCodecAdapter that operates the underlying MediaCodec in synchronous mode.


Abstracts MediaCodec operations.

MediaCodecAdapter offers a common interface to interact with a MediaCodec regardless of the mode the MediaCodec is operating in.

Summary

Nested types

Configuration parameters for a MediaCodecAdapter.

public interface MediaCodecAdapter.Factory

A factory for MediaCodecAdapter instances.

Listener to be called when an input or output buffer becomes available.

Listener to be called when an output frame has rendered on the output surface.

Public methods

abstract int

Returns the next available input buffer index from the underlying MediaCodec or INFO_TRY_AGAIN_LATER if no such buffer exists.

abstract int

Returns the next available output buffer index from the underlying MediaCodec.

abstract void

Detaches the current output surface.

abstract void

Flushes the adapter and the underlying MediaCodec.

abstract @Nullable ByteBuffer
getInputBuffer(int index)

Returns a writable ByteBuffer object for a dequeued input buffer index.

abstract PersistableBundle
@RequiresApi(value = 26)
getMetrics()

Returns metrics data about the current codec instance.

abstract @Nullable ByteBuffer
getOutputBuffer(int index)

Returns a read-only ByteBuffer for a dequeued output buffer index.

abstract MediaFormat

Gets the MediaFormat that was output from the MediaCodec.

abstract boolean

Whether the adapter needs to be reconfigured before it is used.

abstract void
queueInputBuffer(
    int index,
    int offset,
    int size,
    long presentationTimeUs,
    int flags
)

Submit an input buffer for decoding.

abstract void
queueSecureInputBuffer(
    int index,
    int offset,
    CryptoInfo info,
    long presentationTimeUs,
    int flags
)

Submit an input buffer that is potentially encrypted for decoding.

default boolean

Registers a listener that will be called when an input or output buffer becomes available.

abstract void

Releases the adapter and the underlying MediaCodec.

abstract void
releaseOutputBuffer(int index, boolean render)

Returns the buffer to the MediaCodec.

abstract void
releaseOutputBuffer(int index, long renderTimeStampNs)

Updates the output buffer's surface timestamp and sends it to the MediaCodec to render it on the output surface.

abstract void

Registers a callback to be invoked when an output frame is rendered on the output surface.

abstract void
@RequiresApi(value = 23)
setOutputSurface(Surface surface)

Dynamically sets the output surface of a MediaCodec.

abstract void

Communicate additional parameter changes to the MediaCodec instance.

abstract void

Specifies the scaling mode to use, if a surface was specified when the codec was created.

Public methods

dequeueInputBufferIndex

abstract int dequeueInputBufferIndex()

Returns the next available input buffer index from the underlying MediaCodec or INFO_TRY_AGAIN_LATER if no such buffer exists.

Throws
java.lang.IllegalStateException

If the underlying MediaCodec raised an error.

dequeueOutputBufferIndex

abstract int dequeueOutputBufferIndex(MediaCodec.BufferInfo bufferInfo)

Returns the next available output buffer index from the underlying MediaCodec. If the next available output is a MediaFormat change, it will return INFO_OUTPUT_FORMAT_CHANGED and you should call getOutputFormat to get the format. If there is no available output, this method will return INFO_TRY_AGAIN_LATER.

Throws
java.lang.IllegalStateException

If the underlying MediaCodec raised an error.

detachOutputSurface

@RequiresApi(value = 35)
abstract void detachOutputSurface()

Detaches the current output surface.

See also
MediaCodec

flush

abstract void flush()

Flushes the adapter and the underlying MediaCodec.

getInputBuffer

abstract @Nullable ByteBuffer getInputBuffer(int index)

Returns a writable ByteBuffer object for a dequeued input buffer index.

See also
getInputBuffer

getMetrics

@RequiresApi(value = 26)
abstract PersistableBundle getMetrics()

Returns metrics data about the current codec instance.

See also
getMetrics

getOutputBuffer

abstract @Nullable ByteBuffer getOutputBuffer(int index)

Returns a read-only ByteBuffer for a dequeued output buffer index.

See also
getOutputBuffer

getOutputFormat

abstract MediaFormat getOutputFormat()

Gets the MediaFormat that was output from the MediaCodec.

Call this method if a previous call to dequeueOutputBufferIndex returned INFO_OUTPUT_FORMAT_CHANGED.

needsReconfiguration

abstract boolean needsReconfiguration()

Whether the adapter needs to be reconfigured before it is used.

queueInputBuffer

abstract void queueInputBuffer(
    int index,
    int offset,
    int size,
    long presentationTimeUs,
    int flags
)

Submit an input buffer for decoding.

The index must be an input buffer index that has been obtained from a previous call to dequeueInputBufferIndex.

See also
queueInputBuffer

queueSecureInputBuffer

abstract void queueSecureInputBuffer(
    int index,
    int offset,
    CryptoInfo info,
    long presentationTimeUs,
    int flags
)

Submit an input buffer that is potentially encrypted for decoding.

The index must be an input buffer index that has been obtained from a previous call to dequeueInputBufferIndex.

This method behaves like queueSecureInputBuffer, with the difference that info is of type CryptoInfo and not android.media.MediaCodec.CryptoInfo.

registerOnBufferAvailableListener

default boolean registerOnBufferAvailableListener(
    MediaCodecAdapter.OnBufferAvailableListener listener
)

Registers a listener that will be called when an input or output buffer becomes available.

Returns false if listener was not successfully registered for callbacks.

Returns
boolean

Whether listener was successfully registered.

release

abstract void release()

Releases the adapter and the underlying MediaCodec.

releaseOutputBuffer

abstract void releaseOutputBuffer(int index, boolean render)

Returns the buffer to the MediaCodec. If the MediaCodec was configured with an output surface, setting render to true will first send the buffer to the output surface. The surface will release the buffer back to the codec once it is no longer used/displayed.

releaseOutputBuffer

abstract void releaseOutputBuffer(int index, long renderTimeStampNs)

Updates the output buffer's surface timestamp and sends it to the MediaCodec to render it on the output surface. If the MediaCodec is not configured with an output surface, this call will simply return the buffer to the MediaCodec.

setOnFrameRenderedListener

@RequiresApi(value = 23)
abstract void setOnFrameRenderedListener(
    MediaCodecAdapter.OnFrameRenderedListener listener,
    Handler handler
)

Registers a callback to be invoked when an output frame is rendered on the output surface.

setOutputSurface

@RequiresApi(value = 23)
abstract void setOutputSurface(Surface surface)

Dynamically sets the output surface of a MediaCodec.

See also
setOutputSurface

setParameters

abstract void setParameters(Bundle params)

Communicate additional parameter changes to the MediaCodec instance.

See also
setParameters

setVideoScalingMode

abstract void setVideoScalingMode(@C.VideoScalingMode int scalingMode)

Specifies the scaling mode to use, if a surface was specified when the codec was created.