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 interfaceCodec.DecoderFactoryA factory fordecoderinstances.static interfaceCodec.EncoderFactoryA factory forencoderinstances.
-
Constructor Summary
Constructors Constructor Description Codec(MediaCodec mediaCodec, Format configurationFormat, Surface inputSurface)Creates aCodecfrom a configured and startedMediaCodec.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description FormatgetConfigurationFormat()Returns theFormatused for configuring the codec.SurfacegetInputSurface()Returns the inputSurface, or null if the input is not a surface.ByteBuffergetOutputBuffer()Returns the current outputByteBuffer, if available.MediaCodec.BufferInfogetOutputBufferInfo()Returns theMediaCodec.BufferInfoassociated with the current output buffer, if available.FormatgetOutputFormat()Returns the current output format, if available.booleanisEnded()Returns whether the codec output stream has ended, and no more data can be dequeued.booleanmaybeDequeueInputBuffer(DecoderInputBuffer inputBuffer)Dequeues a writable input buffer, if available.voidqueueInputBuffer(DecoderInputBuffer inputBuffer)Queues an input buffer to the decoder.voidrelease()Releases the underlying codec.voidreleaseOutputBuffer()Releases the current output buffer.voidreleaseOutputBuffer(boolean render)Releases the current output buffer.voidsignalEndOfInputStream()Signals end-of-stream on input to a video encoder.
-
-
-
Constructor Detail
-
Codec
public Codec(MediaCodec mediaCodec, Format configurationFormat, @Nullable Surface inputSurface)
Creates aCodecfrom a configured and startedMediaCodec.- Parameters:
mediaCodec- The configured and startedMediaCodec.configurationFormat- SeegetConfigurationFormat().inputSurface- The inputSurfaceif theMediaCodecreceives input from a surface.
-
-
Method Detail
-
getConfigurationFormat
public Format getConfigurationFormat()
Returns theFormatused for configuring the codec.The configuration
Formatis the inputFormatused by theCodec.DecoderFactoryor outputFormatused by theCodec.EncoderFactoryfor 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 TransformationExceptionDequeues 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 underlyingMediaCodecencounters 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 streambuffer has been queued.- Parameters:
inputBuffer- Theinput buffer.- Throws:
IllegalStateException- If called again after anend of streambuffer has been queued.TransformationException- If the underlyingMediaCodecencounters a problem.
-
signalEndOfInputStream
public void signalEndOfInputStream() throws TransformationExceptionSignals 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_STREAMflag should be set on the last input bufferqueued.- Throws:
IllegalStateException- If the codec is not an encoder receiving input from aSurface.TransformationException- If the underlyingMediaCodecencounters a problem.
-
getOutputFormat
@Nullable public Format getOutputFormat() throws TransformationException
Returns the current output format, if available.- Throws:
TransformationException- If the underlyingMediaCodecencounters a problem.
-
getOutputBuffer
@Nullable public ByteBuffer getOutputBuffer() throws TransformationException
Returns the current outputByteBuffer, if available.- Throws:
TransformationException- If the underlyingMediaCodecencounters a problem.
-
getOutputBufferInfo
@Nullable public MediaCodec.BufferInfo getOutputBufferInfo() throws TransformationException
Returns theMediaCodec.BufferInfoassociated with the current output buffer, if available.- Throws:
TransformationException- If the underlyingMediaCodecencounters a problem.
-
releaseOutputBuffer
public void releaseOutputBuffer() throws TransformationExceptionReleases 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 underlyingMediaCodecencounters a problem.
-
releaseOutputBuffer
public void releaseOutputBuffer(boolean render) throws TransformationExceptionReleases the current output buffer. If theMediaCodecwas configured with an output surface, settingrendertotruewill 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 underlyingMediaCodecencounters 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.
-
-