@UnstableApi
interface Codec

Known direct subclasses
DefaultCodec

A default Codec implementation that uses MediaCodec.


Provides a layer of abstraction for interacting with decoders and encoders.

DecoderInputBuffers are used as both decoders' and encoders' input buffers.

Summary

Nested types

A factory for decoder instances.

A factory for encoder instances.

Public functions

Format!

Returns the Format used for configuring the Codec.

Surface!

Returns the input Surface of an underlying video encoder.

Int

Returns the maximum number of frames that may be pending in the output Codec at a time, or 5 as a default value.

String!

Returns the name of the underlying codec.

ByteBuffer?

Returns the current output ByteBuffer, or null if unavailable.

MediaCodec.BufferInfo?

Returns the BufferInfo associated with the current output buffer, or null if there is no output buffer available.

Format?

Returns the current output format, or null if unavailable.

Boolean

Returns whether the Codec's output stream has ended, and no more data can be dequeued.

Boolean

Dequeues a writable input buffer, if available.

Unit

Queues an input buffer to the Codec.

Unit

Releases the Codec.

Unit

Releases the current output buffer.

Unit
releaseOutputBuffer(renderPresentationTimeUs: Long)

Renders and releases the current output buffer.

Unit

Signals end-of-stream on input to a video encoder.

Public functions

getConfigurationFormat

fun getConfigurationFormat(): Format!

Returns the Format used for configuring the Codec.

The configuration Format is the input Format used by the or output Format used by the EncoderFactory for selecting and configuring the underlying decoder or encoder.

getInputSurface

fun getInputSurface(): Surface!

Returns the input Surface of an underlying video encoder.

This method must only be called on video encoders because audio/video decoders and audio encoders don't use a Surface as input.

getMaxPendingFrameCount

fun getMaxPendingFrameCount(): Int

Returns the maximum number of frames that may be pending in the output Codec at a time, or 5 as a default value.

getName

fun getName(): String!

Returns the name of the underlying codec.

getOutputBuffer

fun getOutputBuffer(): ByteBuffer?

Returns the current output ByteBuffer, or null if unavailable.

This method must not be called on video decoders because they must output to a .

Throws
androidx.media3.transformer.ExportException

If the underlying decoder or encoder encounters a problem.

getOutputBufferInfo

fun getOutputBufferInfo(): MediaCodec.BufferInfo?

Returns the BufferInfo associated with the current output buffer, or null if there is no output buffer available.

This method returns null if and only if getOutputBuffer returns null.

Throws
androidx.media3.transformer.ExportException

If the underlying decoder or encoder encounters a problem.

getOutputFormat

fun getOutputFormat(): Format?

Returns the current output format, or null if unavailable.

Throws
androidx.media3.transformer.ExportException

If the underlying decoder or encoder encounters a problem.

isEnded

fun isEnded(): Boolean

Returns whether the Codec's output stream has ended, and no more data can be dequeued.

maybeDequeueInputBuffer

fun maybeDequeueInputBuffer(inputBuffer: DecoderInputBuffer!): Boolean

Dequeues a writable input buffer, if available.

This method must not be called from video encoders because they must use a Surface to receive input.

Parameters
inputBuffer: DecoderInputBuffer!

The buffer where the dequeued buffer data is stored, at inputBuffer.data.

Returns
Boolean

Whether an input buffer is ready to be used.

Throws
androidx.media3.transformer.ExportException

If the underlying decoder or encoder encounters a problem.

queueInputBuffer

fun queueInputBuffer(inputBuffer: DecoderInputBuffer!): Unit

Queues an input buffer to the Codec. No buffers may be queued after end of stream buffer has been queued.

This method must not be called from video encoders because they must use a Surface to receive input.

Parameters
inputBuffer: DecoderInputBuffer!

The input buffer.

Throws
androidx.media3.transformer.ExportException

If the underlying decoder or encoder encounters a problem.

release

fun release(): Unit

Releases the Codec.

releaseOutputBuffer

fun releaseOutputBuffer(render: Boolean): Unit

Releases the current output buffer.

Only set render to true when the Codec is a video decoder. Setting render to true will first render the buffer to the output surface. In this case, the surface will release the buffer back to the Codec once it is no longer used/displayed.

This should be called after the buffer has been processed. The next output buffer will not be available until the current output buffer has been released.

Calling this method with render set to true is equivalent to calling releaseOutputBuffer with the presentation timestamp of the output buffer info.

Parameters
render: Boolean

Whether the buffer needs to be rendered to the output Surface.

Throws
androidx.media3.transformer.ExportException

If the underlying decoder or encoder encounters a problem.

releaseOutputBuffer

fun releaseOutputBuffer(renderPresentationTimeUs: Long): Unit

Renders and releases the current output buffer.

This method must only be called on video decoders.

This method will first render the buffer to the output surface. The surface will then release the buffer back to the Codec once it is no longer used/displayed.

This should be called after the buffer has been processed. The next output buffer will not be available until the current output buffer has been released.

Parameters
renderPresentationTimeUs: Long

The presentation timestamp to associate with this buffer, in microseconds.

Throws
androidx.media3.transformer.ExportException

If the underlying decoder or encoder encounters a problem.

signalEndOfInputStream

fun signalEndOfInputStream(): Unit

Signals end-of-stream on input to a video encoder.

This method must only be called on video encoders because they must use a Surface as input. For audio/video decoders or audio encoders, the BUFFER_FLAG_END_OF_STREAM flag should be set on the last input buffer queued.

Throws
androidx.media3.transformer.ExportException

If the underlying video encoder encounters a problem.