@UnstableApi
public final class DefaultCodec implements Codec


A default Codec implementation that uses MediaCodec.

Summary

Constants

static final int

Public constructors

DefaultCodec(
    Context context,
    Format configurationFormat,
    MediaFormat configurationMediaFormat,
    String mediaCodecName,
    boolean isDecoder,
    @Nullable Surface outputSurface
)

Creates a DefaultCodec.

Public methods

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.

@Nullable ByteBuffer

Returns the current output ByteBuffer, or null if unavailable.

@Nullable MediaCodec.BufferInfo

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

@Nullable 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
@EnsuresNonNullIf(expression = "#1.data", result = true)
maybeDequeueInputBuffer(DecoderInputBuffer inputBuffer)

Dequeues a writable input buffer, if available.

void

Queues an input buffer to the Codec.

void

Releases the Codec.

void
releaseOutputBuffer(boolean render)

Releases the current output buffer.

void
releaseOutputBuffer(long renderPresentationTimeUs)

Renders and releases the current output buffer.

void

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

Protected methods

void
@VisibleForTesting
releaseOutputBuffer(boolean render, long renderPresentationTimeUs)

Releases the output buffer at renderPresentationTimeUs if render is true, otherwise release the buffer without rendering.

Constants

DEFAULT_PCM_ENCODING

public static final int DEFAULT_PCM_ENCODING = 2

Public fields

configurationFormat

public final Format configurationFormat

inputSurface

public final @Nullable Surface inputSurface

maxPendingFrameCount

public final int maxPendingFrameCount

outputBuffer

public @Nullable ByteBuffer outputBuffer

outputBufferInfo

public final MediaCodec.BufferInfo outputBufferInfo

Public constructors

DefaultCodec

public DefaultCodec(
    Context context,
    Format configurationFormat,
    MediaFormat configurationMediaFormat,
    String mediaCodecName,
    boolean isDecoder,
    @Nullable Surface outputSurface
)

Creates a DefaultCodec.

Parameters
Context context

The Context.

Format configurationFormat

The Format to configure the DefaultCodec. See getConfigurationFormat. The sampleMimeType must not be null.

MediaFormat configurationMediaFormat

The MediaFormat to configure the underlying .

String mediaCodecName

The name of a specific MediaCodec to instantiate.

boolean isDecoder

Whether the DefaultCodec is intended as a decoder.

@Nullable Surface outputSurface

The output Surface if the MediaCodec outputs to a surface.

Public methods

getConfigurationFormat

public Format getConfigurationFormat()

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

public Surface getInputSurface()

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

public int getMaxPendingFrameCount()

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

getName

public String getName()

Returns the name of the underlying codec.

This name is of the actual codec, which may not be the same as the mediaCodecName passed to DefaultCodec.

See also
getCanonicalName

getOutputBuffer

public @Nullable ByteBuffer getOutputBuffer()

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

public @Nullable MediaCodec.BufferInfo getOutputBufferInfo()

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

public @Nullable Format getOutputFormat()

Returns the current output format, or null if unavailable.

Throws
androidx.media3.transformer.ExportException

If the underlying decoder or encoder encounters a problem.

isEnded

public boolean isEnded()

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

maybeDequeueInputBuffer

@EnsuresNonNullIf(expression = "#1.data", result = true)
public boolean maybeDequeueInputBuffer(DecoderInputBuffer inputBuffer)

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
DecoderInputBuffer inputBuffer

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

public void queueInputBuffer(DecoderInputBuffer inputBuffer)

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
DecoderInputBuffer inputBuffer

The input buffer.

Throws
androidx.media3.transformer.ExportException

If the underlying decoder or encoder encounters a problem.

release

public void release()

Releases the Codec.

releaseOutputBuffer

public void releaseOutputBuffer(boolean render)

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
boolean render

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

public void releaseOutputBuffer(long renderPresentationTimeUs)

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
long renderPresentationTimeUs

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

public void signalEndOfInputStream()

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.

Protected methods

releaseOutputBuffer

@VisibleForTesting
protected void releaseOutputBuffer(boolean render, long renderPresentationTimeUs)

Releases the output buffer at renderPresentationTimeUs if render is true, otherwise release the buffer without rendering.