Class Codec
- java.lang.Object
-
- com.google.android.exoplayer2.transformer.Codec
-
public final class Codec extends Object
A wrapper aroundMediaCodec
.Provides a layer of abstraction for callers that need to interact with
MediaCodec
. This is done by simplifying the calls needed to queue and dequeue buffers, removing the need to track buffer indices and codec events.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Codec.DecoderFactory
A factory fordecoder
instances.static interface
Codec.EncoderFactory
A factory forencoder
instances.
-
Constructor Summary
Constructors Constructor Description Codec(MediaCodec mediaCodec, Format configurationFormat, Surface inputSurface)
Creates aCodec
from a configured and startedMediaCodec
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Format
getConfigurationFormat()
Returns theFormat
used for configuring the codec.Surface
getInputSurface()
Returns the inputSurface
, or null if the input is not a surface.ByteBuffer
getOutputBuffer()
Returns the current outputByteBuffer
, if available.MediaCodec.BufferInfo
getOutputBufferInfo()
Returns theMediaCodec.BufferInfo
associated with the current output buffer, if available.Format
getOutputFormat()
Returns the current output format, if available.boolean
isEnded()
Returns whether the codec output stream has ended, and no more data can be dequeued.boolean
maybeDequeueInputBuffer(DecoderInputBuffer inputBuffer)
Dequeues a writable input buffer, if available.void
queueInputBuffer(DecoderInputBuffer inputBuffer)
Queues an input buffer to the decoder.void
release()
Releases the underlying codec.void
releaseOutputBuffer()
Releases the current output buffer.void
releaseOutputBuffer(boolean render)
Releases the current output buffer.void
signalEndOfInputStream()
Signals end-of-stream on input to a video encoder.
-
-
-
Constructor Detail
-
Codec
public Codec(MediaCodec mediaCodec, Format configurationFormat, @Nullable Surface inputSurface)
Creates aCodec
from a configured and startedMediaCodec
.- Parameters:
mediaCodec
- The configured and startedMediaCodec
.configurationFormat
- SeegetConfigurationFormat()
.inputSurface
- The inputSurface
if theMediaCodec
receives input from a surface.
-
-
Method Detail
-
getConfigurationFormat
public Format getConfigurationFormat()
Returns theFormat
used for configuring the codec.The configuration
Format
is the inputFormat
used by theCodec.DecoderFactory
or outputFormat
used by theCodec.EncoderFactory
for selecting and configuring the underlyingMediaCodec
.
-
getInputSurface
@Nullable public Surface getInputSurface()
Returns the inputSurface
, or null if the input is not a surface.
-
maybeDequeueInputBuffer
@EnsuresNonNullIf(expression="#1.data", result=true) public boolean maybeDequeueInputBuffer(DecoderInputBuffer inputBuffer) throws TransformationException
Dequeues a writable input buffer, if available.- Parameters:
inputBuffer
- The buffer where the dequeued buffer data is stored.- Returns:
- Whether an input buffer is ready to be used.
- Throws:
TransformationException
- If the underlyingMediaCodec
encounters a problem.
-
queueInputBuffer
public void queueInputBuffer(DecoderInputBuffer inputBuffer) throws TransformationException
Queues an input buffer to the decoder. No buffers may be queued after anend of stream
buffer has been queued.- Parameters:
inputBuffer
- Theinput buffer
.- Throws:
IllegalStateException
- If called again after anend of stream
buffer has been queued.TransformationException
- If the underlyingMediaCodec
encounters a problem.
-
signalEndOfInputStream
public void signalEndOfInputStream() throws TransformationException
Signals end-of-stream on input to a video encoder.This method does not need to be called for audio/video decoders or audio encoders. For these the
MediaCodec.BUFFER_FLAG_END_OF_STREAM
flag should be set on the last input bufferqueued
.- Throws:
IllegalStateException
- If the codec is not an encoder receiving input from aSurface
.TransformationException
- If the underlyingMediaCodec
encounters a problem.
-
getOutputFormat
@Nullable public Format getOutputFormat() throws TransformationException
Returns the current output format, if available.- Throws:
TransformationException
- If the underlyingMediaCodec
encounters a problem.
-
getOutputBuffer
@Nullable public ByteBuffer getOutputBuffer() throws TransformationException
Returns the current outputByteBuffer
, if available.- Throws:
TransformationException
- If the underlyingMediaCodec
encounters a problem.
-
getOutputBufferInfo
@Nullable public MediaCodec.BufferInfo getOutputBufferInfo() throws TransformationException
Returns theMediaCodec.BufferInfo
associated with the current output buffer, if available.- Throws:
TransformationException
- If the underlyingMediaCodec
encounters a problem.
-
releaseOutputBuffer
public void releaseOutputBuffer() throws TransformationException
Releases the current output buffer.This should be called after the buffer has been processed. The next output buffer will not be available until the previous has been released.
- Throws:
TransformationException
- If the underlyingMediaCodec
encounters a problem.
-
releaseOutputBuffer
public void releaseOutputBuffer(boolean render) throws TransformationException
Releases the current output buffer. If theMediaCodec
was configured with an output surface, settingrender
totrue
will first send the buffer to the output surface. 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 previous has been released.
- Parameters:
render
- Whether the buffer needs to be sent to the outputSurface
.- Throws:
TransformationException
- If the underlyingMediaCodec
encounters a problem.
-
isEnded
public boolean isEnded()
Returns whether the codec output stream has ended, and no more data can be dequeued.
-
release
public void release()
Releases the underlying codec.
-
-