DefaultCodec
@UnstableApi
public final class DefaultCodec implements Codec
A default Codec implementation that uses MediaCodec.
Summary
Constants |
|
|---|---|
static final int |
Public fields |
|
|---|---|
final Format |
|
final @Nullable Surface |
|
final int |
|
@Nullable ByteBuffer |
|
final MediaCodec.BufferInfo |
|
@MonotonicNonNull Format |
Public constructors |
|---|
DefaultCodec(Creates a |
Public methods |
|
|---|---|
Format |
Returns the |
Format |
Returns the |
Surface |
Returns the input |
int |
Returns the maximum number of frames that may be pending in the output |
String |
getName()Returns the name of the underlying codec. |
@Nullable ByteBuffer |
Returns the current output |
@Nullable MediaCodec.BufferInfo |
Returns the |
@Nullable Format |
Returns the current output format, or |
boolean |
isEnded()Returns whether the |
boolean |
@EnsuresNonNullIf(expression = "#1.data", result = true)Dequeues a writable input buffer, if available. |
void |
queueInputBuffer(DecoderInputBuffer inputBuffer)Queues an input buffer to the |
void |
release()Releases the |
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 |
@VisibleForTestingReleases the output buffer at |
Constants
Public fields
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 |
Format configurationFormat |
The |
MediaFormat configurationMediaFormat |
The |
String mediaCodecName |
The name of a specific |
boolean isDecoder |
Whether the |
@Nullable Surface outputSurface |
The output |
Public methods
getConfigurationFormat
public Format getConfigurationFormat()
Returns the Format used for configuring the Codec.
The configuration Format is the input Format used by the DecoderFactory or output Format used by the EncoderFactory for selecting and configuring the underlying decoder or encoder.
getInputFormat
public Format getInputFormat()
Returns the Format accepted by the codec.
This format may differ from the Format returned by getConfigurationFormat, depending on the underlying codec and configuration format requested.
| Throws | |
|---|---|
androidx.media3.transformer.ExportException |
If the underlying decoder or encoder encounters a problem. |
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 Surface.
| 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 |
| 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 |
| Throws | |
|---|---|
androidx.media3.transformer.ExportException |
If the underlying decoder or encoder encounters a problem. |
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 |
| 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.