# DefaultCodec

Artifact: [androidx.media3:media3-transformer](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/transformer/DefaultCodec.java+class:androidx.media3.transformer.DefaultCodec)

*** ** * ** ***


```
@UnstableApi
public final class DefaultCodec implements Codec
```

<br />

*** ** * ** ***

A default `https://developer.android.com/reference/androidx/media3/transformer/Codec` implementation that uses `https://developer.android.com/reference/android/media/MediaCodec.html`.

## Summary

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#DEFAULT_PCM_ENCODING() = 2` |

| ### Public fields |
|---|---|
| `final https://developer.android.com/reference/androidx/media3/common/Format` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#configurationFormat()` |
| `final @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/view/Surface.html` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#inputSurface()` |
| `final int` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#maxPendingFrameCount()` |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/nio/ByteBuffer.html` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#outputBuffer()` |
| `final https://developer.android.com/reference/android/media/MediaCodec.BufferInfo.html` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#outputBufferInfo()` |
| `@https://checkerframework.org/api/org/checkerframework/checker/nullness/qual/MonotonicNonNull.html https://developer.android.com/reference/androidx/media3/common/Format` | `@https://checkerframework.org/api/org/checkerframework/checker/nullness/qual/MonotonicNonNull.html https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#outputFormat()` |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#DefaultCodec(android.content.Context,androidx.media3.common.Format,android.media.MediaFormat,java.lang.String,boolean,android.view.Surface)( https://developer.android.com/reference/android/content/Context.html context, https://developer.android.com/reference/androidx/media3/common/Format configurationFormat, https://developer.android.com/reference/android/media/MediaFormat.html configurationMediaFormat, https://developer.android.com/reference/java/lang/String.html mediaCodecName, boolean isDecoder, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/view/Surface.html outputSurface )` Creates a `DefaultCodec`. |

| ### Public methods |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/Format` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#getConfigurationFormat()()` Returns the `https://developer.android.com/reference/androidx/media3/common/Format` used for configuring the `Codec`. |
| `https://developer.android.com/reference/androidx/media3/common/Format` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#getInputFormat()()` Returns the `https://developer.android.com/reference/androidx/media3/common/Format` accepted by the codec. |
| `https://developer.android.com/reference/android/view/Surface.html` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#getInputSurface()()` Returns the input `https://developer.android.com/reference/android/view/Surface.html` of an underlying video encoder. |
| `int` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#getMaxPendingFrameCount()()` Returns the maximum number of frames that may be pending in the output `Codec` at a time, or `5` as a default value. |
| `https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#getName()()` Returns the name of the underlying codec. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/nio/ByteBuffer.html` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#getOutputBuffer()()` Returns the current output `https://developer.android.com/reference/java/nio/ByteBuffer.html`, or `null` if unavailable. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/media/MediaCodec.BufferInfo.html` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#getOutputBufferInfo()()` Returns the `https://developer.android.com/reference/android/media/MediaCodec.BufferInfo.html` associated with the current output buffer, or `null` if there is no output buffer available. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/common/Format` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#getOutputFormat()()` Returns the current output format, or `null` if unavailable. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#isEnded()()` Returns whether the `Codec`'s output stream has ended, and no more data can be dequeued. |
| `boolean` | `@https://checkerframework.org/api/org/checkerframework/checker/nullness/qual/EnsuresNonNullIf.html(expression = "#1.data", result = true) https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#maybeDequeueInputBuffer(androidx.media3.decoder.DecoderInputBuffer)(https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer inputBuffer)` Dequeues a writable input buffer, if available. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#queueInputBuffer(androidx.media3.decoder.DecoderInputBuffer)(https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer inputBuffer)` Queues an input buffer to the `Codec`. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#release()()` Releases the `Codec`. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#releaseOutputBuffer(boolean)(boolean render)` Releases the current output buffer. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#releaseOutputBuffer(long)(long renderPresentationTimeUs)` Renders and releases the current output buffer. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#signalEndOfInputStream()()` Signals end-of-stream on input to a video encoder. |

| ### Protected methods |
|---|---|
| `void` | `@https://developer.android.com/reference/androidx/annotation/VisibleForTesting https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#releaseOutputBuffer(boolean,long)(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
```

### outputFormat

```
@MonotonicNonNull
public @MonotonicNonNull Format outputFormat
```

## Public constructors

### DefaultCodec

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

Creates a `DefaultCodec`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | The `https://developer.android.com/reference/android/content/Context.html`. |
| `https://developer.android.com/reference/androidx/media3/common/Format configurationFormat` | The `https://developer.android.com/reference/androidx/media3/common/Format` to configure the `DefaultCodec`. See `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#getConfigurationFormat()`. The `https://developer.android.com/reference/androidx/media3/common/Format#sampleMimeType()` must not be `null`. |
| `https://developer.android.com/reference/android/media/MediaFormat.html configurationMediaFormat` | The `https://developer.android.com/reference/android/media/MediaFormat.html` to configure the underlying `https://developer.android.com/reference/android/media/MediaCodec.html`. |
| `https://developer.android.com/reference/java/lang/String.html mediaCodecName` | The name of a specific `https://developer.android.com/reference/android/media/MediaCodec.html` to instantiate. |
| `boolean isDecoder` | Whether the `DefaultCodec` is intended as a decoder. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/view/Surface.html outputSurface` | The output `https://developer.android.com/reference/android/view/Surface.html` if the `https://developer.android.com/reference/android/media/MediaCodec.html` outputs to a surface. |

## Public methods

### getConfigurationFormat

```
public Format getConfigurationFormat()
```

Returns the `https://developer.android.com/reference/androidx/media3/common/Format` used for configuring the `Codec`.

The configuration `https://developer.android.com/reference/androidx/media3/common/Format` is the input `https://developer.android.com/reference/androidx/media3/common/Format` used by the `https://developer.android.com/reference/androidx/media3/transformer/Codec.DecoderFactory` or output `https://developer.android.com/reference/androidx/media3/common/Format` used by the `https://developer.android.com/reference/androidx/media3/transformer/Codec.EncoderFactory` for selecting and configuring the underlying decoder or encoder.

### getInputFormat

```
public Format getInputFormat()
```

Returns the `https://developer.android.com/reference/androidx/media3/common/Format` accepted by the codec.

This format may differ from the `https://developer.android.com/reference/androidx/media3/common/Format` returned by `https://developer.android.com/reference/androidx/media3/transformer/Codec#getConfigurationFormat()`, depending on the underlying codec and configuration format requested.

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/media3/transformer/ExportException` | If the underlying decoder or encoder encounters a problem. |

### getInputSurface

```
public Surface getInputSurface()
```

Returns the input `https://developer.android.com/reference/android/view/Surface.html` 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 `https://developer.android.com/reference/android/view/Surface.html` 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 `https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec#DefaultCodec(android.content.Context,androidx.media3.common.Format,android.media.MediaFormat,java.lang.String,boolean,android.view.Surface)`.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodec.html#getCanonicalName()` |   |

### getOutputBuffer

```
public @Nullable ByteBuffer getOutputBuffer()
```

Returns the current output `https://developer.android.com/reference/java/nio/ByteBuffer.html`, or `null` if unavailable.

This method must not be called on video decoders because they must output to a `https://developer.android.com/reference/android/view/Surface.html`.

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/media3/transformer/ExportException` | If the underlying decoder or encoder encounters a problem. |

### getOutputBufferInfo

```
public @Nullable MediaCodec.BufferInfo getOutputBufferInfo()
```

Returns the `https://developer.android.com/reference/android/media/MediaCodec.BufferInfo.html` associated with the current output buffer, or `null` if there is no output buffer available.

This method returns `null` if and only if `https://developer.android.com/reference/androidx/media3/transformer/Codec#getOutputBuffer()` returns null.

| Throws |
|---|---|
| `https://developer.android.com/reference/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 |
|---|---|
| `https://developer.android.com/reference/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 `https://developer.android.com/reference/android/view/Surface.html` to receive input.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer inputBuffer` | The buffer where the dequeued buffer data is stored, at `https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer#data()`. |

| Returns |
|---|---|
| `boolean` | Whether an input buffer is ready to be used. |

| Throws |
|---|---|
| `https://developer.android.com/reference/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 `https://developer.android.com/reference/androidx/media3/decoder/Buffer#isEndOfStream()` buffer has been queued.

This method must not be called from video encoders because they must use a `https://developer.android.com/reference/android/view/Surface.html` to receive input.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer inputBuffer` | The `https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer`. |

| Throws |
|---|---|
| `https://developer.android.com/reference/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 `https://developer.android.com/reference/androidx/media3/transformer/Codec#releaseOutputBuffer(long)` with the presentation timestamp of the `https://developer.android.com/reference/androidx/media3/transformer/Codec#getOutputBufferInfo()`.

| Parameters |
|---|---|
| `boolean render` | Whether the buffer needs to be rendered to the output `https://developer.android.com/reference/android/view/Surface.html`. |

| Throws |
|---|---|
| `https://developer.android.com/reference/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 |
|---|---|
| `https://developer.android.com/reference/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 `https://developer.android.com/reference/android/view/Surface.html` as input. For audio/video decoders or audio encoders, the `https://developer.android.com/reference/androidx/media3/common/C#BUFFER_FLAG_END_OF_STREAM()` flag should be set on the last input buffer `https://developer.android.com/reference/androidx/media3/transformer/Codec#queueInputBuffer(androidx.media3.decoder.DecoderInputBuffer)`.

| Throws |
|---|---|
| `https://developer.android.com/reference/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.

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/media3/transformer/ExportException` |   |