# Codec

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/Codec.java+class:androidx.media3.transformer.Codec)

*** ** * ** ***


```
@UnstableApi
public interface Codec
```

<br />

Known direct subclasses [DefaultCodec](https://developer.android.com/reference/androidx/media3/transformer/DefaultCodec)

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

*** ** * ** ***

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

`https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer` are used as both decoders' and encoders' input buffers.

## Summary

| ### Nested types |
|---|
| `public interface https://developer.android.com/reference/androidx/media3/transformer/Codec.DecoderFactory` A factory for `https://developer.android.com/reference/androidx/media3/transformer/Codec` instances. |
| `public interface https://developer.android.com/reference/androidx/media3/transformer/Codec.EncoderFactory` A factory for `https://developer.android.com/reference/androidx/media3/transformer/Codec` instances. |

| ### Public methods |
|---|---|
| `abstract https://developer.android.com/reference/androidx/media3/common/Format` | `https://developer.android.com/reference/androidx/media3/transformer/Codec#getConfigurationFormat()()` Returns the `https://developer.android.com/reference/androidx/media3/common/Format` used for configuring the `Codec`. |
| `abstract https://developer.android.com/reference/androidx/media3/common/Format` | `https://developer.android.com/reference/androidx/media3/transformer/Codec#getInputFormat()()` Returns the `https://developer.android.com/reference/androidx/media3/common/Format` accepted by the codec. |
| `abstract https://developer.android.com/reference/android/view/Surface.html` | `https://developer.android.com/reference/androidx/media3/transformer/Codec#getInputSurface()()` Returns the input `https://developer.android.com/reference/android/view/Surface.html` of an underlying video encoder. |
| `default int` | `https://developer.android.com/reference/androidx/media3/transformer/Codec#getMaxPendingFrameCount()()` Returns the maximum number of frames that may be pending in the output `Codec` at a time, or `5` as a default value. |
| `abstract https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/transformer/Codec#getName()()` Returns the name of the underlying codec. |
| `abstract @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/Codec#getOutputBuffer()()` Returns the current output `https://developer.android.com/reference/java/nio/ByteBuffer.html`, or `null` if unavailable. |
| `abstract @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/Codec#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. |
| `abstract @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/Codec#getOutputFormat()()` Returns the current output format, or `null` if unavailable. |
| `abstract boolean` | `https://developer.android.com/reference/androidx/media3/transformer/Codec#isEnded()()` Returns whether the `Codec`'s output stream has ended, and no more data can be dequeued. |
| `abstract boolean` | `https://developer.android.com/reference/androidx/media3/transformer/Codec#maybeDequeueInputBuffer(androidx.media3.decoder.DecoderInputBuffer)(https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer inputBuffer)` Dequeues a writable input buffer, if available. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/transformer/Codec#queueInputBuffer(androidx.media3.decoder.DecoderInputBuffer)(https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer inputBuffer)` Queues an input buffer to the `Codec`. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/transformer/Codec#release()()` Releases the `Codec`. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/transformer/Codec#releaseOutputBuffer(boolean)(boolean render)` Releases the current output buffer. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/transformer/Codec#releaseOutputBuffer(long)(long renderPresentationTimeUs)` Renders and releases the current output buffer. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/transformer/Codec#signalEndOfInputStream()()` Signals end-of-stream on input to a video encoder. |

## Public methods

### getConfigurationFormat

```
abstract 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

```
abstract 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

```
abstract 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

```
default 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

```
abstract String getName()
```

Returns the name of the underlying codec.

### getOutputBuffer

```
abstract @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

```
abstract @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

```
abstract @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

```
abstract boolean isEnded()
```

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

### maybeDequeueInputBuffer

```
abstract 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

```
abstract 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

```
abstract void release()
```

Releases the `Codec`.

### releaseOutputBuffer

```
abstract 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

```
abstract 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

```
abstract 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. |