java.lang.Object | |
↳ | android.media.MediaCodec |
MediaCodec class can be used to access low-level media codec, i.e. encoder/decoder components.
MediaCodec is generally used like this:
MediaCodec codec = MediaCodec.createDecoderByType(type); codec.configure(format, ...); codec.start(); // if API level <= 20, get input and output buffer arrays here ByteBuffer[] inputBuffers = codec.getInputBuffers(); ByteBuffer[] outputBuffers = codec.getOutputBuffers(); for (;;) { int inputBufferIndex = codec.dequeueInputBuffer(timeoutUs); if (inputBufferIndex >= 0) { // if API level >= 21, get input buffer here ByteBuffer inputBuffer = codec.getInputBuffer(inputBufferIndex); // fill inputBuffers[inputBufferIndex] with valid data ... codec.queueInputBuffer(inputBufferIndex, ...); } int outputBufferIndex = codec.dequeueOutputBuffer(timeoutUs); if (outputBufferIndex >= 0) { // if API level >= 21, get output buffer here ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferIndex); // outputBuffer is ready to be processed or rendered. ... codec.releaseOutputBuffer(outputBufferIndex, ...); } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) { // no needed to handle if API level >= 21 and using getOutputBuffer(int) outputBuffers = codec.getOutputBuffers(); } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) { // Subsequent data will conform to new format. // can ignore if API level >= 21 and using getOutputFormat(outputBufferIndex) MediaFormat format = codec.getOutputFormat(); ... } } codec.stop(); codec.release(); codec = null;Each codec maintains a number of input and output buffers that are referred to by index in API calls.
For API levels 20 and below:
The contents of these buffers are represented by the ByteBuffer[] arrays
accessible through getInputBuffers()
and getOutputBuffers()
.
After a successful call to start()
the client "owns" neither
input nor output buffers, subsequent calls to dequeueInputBuffer(long)
and dequeueOutputBuffer(MediaCodec.BufferInfo, long)
then transfer ownership from the codec
to the client.
The client is not required to resubmit/release buffers immediately to the codec, the sample code above simply does this for simplicity's sake. Nonetheless, it is possible that a codec may hold off on generating output buffers until all outstanding buffers have been released/resubmitted.
Once the client has an input buffer available it can fill it with data
and submit it it to the codec via a call to queueInputBuffer(int, int, int, long, int)
.
Do not submit multiple input buffers with the same timestamp (unless
it is codec-specific data marked as such using the flag
BUFFER_FLAG_CODEC_CONFIG
).
The codec in turn will return an output buffer to the client in response
to dequeueOutputBuffer(MediaCodec.BufferInfo, long)
. After the output buffer has been processed
a call to releaseOutputBuffer(int, boolean)
will return it to the codec.
If a video surface has been provided in the call to configure(MediaFormat, Surface, MediaCrypto, int)
,
releaseOutputBuffer(int, boolean)
optionally allows rendering of the buffer
to the surface.
Input buffers (for decoders) and Output buffers (for encoders) contain encoded data according to the format's type. For video types this data is all the encoded data representing a single moment in time, for audio data this is slightly relaxed in that a buffer may contain multiple encoded frames of audio. In either case, buffers do not start and end on arbitrary byte boundaries, this is not a stream of bytes, it's a stream of access units.
Most formats also require the actual data to be prefixed by a number
of buffers containing setup data, or codec specific data, i.e. the
first few buffers submitted to the codec object after starting it must
be codec specific data marked as such using the flag BUFFER_FLAG_CODEC_CONFIG
in a call to queueInputBuffer(int, int, int, long, int)
.
Codec specific data included in the format passed to configure(MediaFormat, Surface, MediaCrypto, int)
(in ByteBuffer entries with keys "csd-0", "csd-1", ...) is automatically
submitted to the codec, this data MUST NOT be submitted explicitly by the
client.
Once the client reaches the end of the input data it signals the end of
the input stream by specifying a flag of BUFFER_FLAG_END_OF_STREAM
in the call to
queueInputBuffer(int, int, int, long, int)
. The codec will continue to return output buffers
until it eventually signals the end of the output stream by specifying
the same flag (BUFFER_FLAG_END_OF_STREAM
) on the BufferInfo returned in
dequeueOutputBuffer(MediaCodec.BufferInfo, long)
. Do not submit additional input buffers after
signaling the end of the input stream, unless the codec has been flushed,
or stopped and restarted.
isFeatureSupported(String)
. Adaptive playback
is only supported if you configure the codec to decode onto a Surface
.
flush()
the decoder.
Any input or output buffers the client may own at the point of the flush are
immediately revoked, i.e. after a call to flush()
the client does not
own any buffers anymore.
It is important that the input data after a flush starts at a suitable
stream boundary. The first frame must be able to be decoded completely on
its own (for most codecs this means an I-frame), and that no frames should
refer to frames before that first new frame.
Note that the format of the data submitted after a flush must not change,
flush does not support format discontinuities,
for this a full stop()
, configure()
, start()
cycle is necessary.
flush()
the
decoder.
It is still important that the input data after the discontinuity starts at a suitable stream boundary (e.g. I-frame), and that no new frames refer to frames before the first frame of the new input data segment.
For some video formats it is also possible to change the picture size
mid-stream. To do this for H.264, the new Sequence Parameter Set (SPS) and
Picture Parameter Set (PPS) values must be packaged together with an
Instantaneous Decoder Refresh (IDR) frame in a single buffer, which then
can be enqueued as a regular input buffer.
The client will receive an INFO_OUTPUT_FORMAT_CHANGED
return
value from dequeueOutputBuffer()
or
onOutputBufferAvailable()
just after the picture-size change takes place and before any
frames with the new size have been returned.
Be careful when calling flush()
shortly after you have changed
the picture size. If you have not received confirmation of the picture
size change, you will need to repeat the request for the new picture size.
E.g. for H.264 you will need to prepend the PPS/SPS to the new IDR
frame to ensure that the codec receives the picture size change request.
During its life, a codec conceptually exists in one of the following states:
Initialized, Configured, Executing, Error, Uninitialized, (omitting transitory states
between them). When created by one of the factory methods,
the codec is in the Initialized state; configure(MediaFormat, Surface, MediaCrypto, int)
brings it to the
Configured state; start()
brings it to the Executing state.
In the Executing state, decoding or encoding occurs through the buffer queue
manipulation described above. The method stop()
returns the codec to the Initialized state, whereupon it may be configured again,
and release()
brings the codec to the terminal Uninitialized state. When
a codec error occurs, the codec moves to the Error state. Use reset()
to
bring the codec back to the Initialized state, or release()
to move it
to the Uninitialized state.
The factory methods
createByCodecName(String)
,
createDecoderByType(String)
,
and createEncoderByType(String)
throw IOException
on failure which
the caller must catch or declare to pass up.
MediaCodec methods throw IllegalStateException
when the method is called from a codec state that does not allow it;
this is typically due to incorrect application API usage.
Methods involving secure buffers may throw
MediaCodec.CryptoException(int, String)
, which
has further error information obtainable from getErrorCode()
.
Internal codec errors result in a MediaCodec.CodecException
,
which may be due to media content corruption, hardware failure, resource exhaustion,
and so forth, even when the application is correctly using the API.
The recommended action when receiving a MediaCodec.CodecException
can be determined by
calling isRecoverable()
and
isTransient()
.
If isRecoverable()
returns true,
then a stop()
, configure(MediaFormat, Surface, MediaCrypto, int)
, and start()
can be performed to recover.
If isTransient()
returns true,
then resources are temporarily unavailable and the method may be retried at a later time.
If both isRecoverable()
and isTransient()
return false,
then the MediaCodec.CodecException
is fatal and the codec must be
reset
or released
.
Both isRecoverable()
and
isTransient()
do not return true at the same time.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
MediaCodec.BufferInfo | Per buffer metadata includes an offset and size specifying the range of valid data in the associated codec (output) buffer. | ||||||||||
MediaCodec.Callback | MediaCodec callback interface. | ||||||||||
MediaCodec.CodecException | Thrown when an internal codec error occurs. | ||||||||||
MediaCodec.CryptoException | Thrown when a crypto error occurs while queueing a secure input buffer. | ||||||||||
MediaCodec.CryptoInfo | Metadata describing the structure of a (at least partially) encrypted input sample. |
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | BUFFER_FLAG_CODEC_CONFIG | This indicated that the buffer marked as such contains codec initialization / codec specific data instead of media data. | |||||||||
int | BUFFER_FLAG_END_OF_STREAM | This signals the end of stream, i.e. | |||||||||
int | BUFFER_FLAG_KEY_FRAME | This indicates that the (encoded) buffer marked as such contains the data for a key frame. | |||||||||
int | BUFFER_FLAG_SYNC_FRAME |
This constant was deprecated
in API level 21.
Use BUFFER_FLAG_KEY_FRAME instead.
|
|||||||||
int | CONFIGURE_FLAG_ENCODE | If this codec is to be used as an encoder, pass this flag. | |||||||||
int | CRYPTO_MODE_AES_CTR | ||||||||||
int | CRYPTO_MODE_UNENCRYPTED | ||||||||||
int | INFO_OUTPUT_BUFFERS_CHANGED |
This constant was deprecated
in API level 21.
This return value can be ignored as getOutputBuffers() has been deprecated. Client should
request a current buffer using on of the get-buffer or
get-image methods each time one has been dequeued.
|
|||||||||
int | INFO_OUTPUT_FORMAT_CHANGED | The output format has changed, subsequent data will follow the new format. | |||||||||
int | INFO_TRY_AGAIN_LATER |
If a non-negative timeout had been specified in the call
to dequeueOutputBuffer(MediaCodec.BufferInfo, long) , indicates that the call timed out.
|
|||||||||
String | PARAMETER_KEY_REQUEST_SYNC_FRAME | Request that the encoder produce a sync frame "soon". | |||||||||
String | PARAMETER_KEY_SUSPEND | Temporarily suspend/resume encoding of input data. | |||||||||
String | PARAMETER_KEY_VIDEO_BITRATE | Change a video encoder's target bitrate on the fly. | |||||||||
int | VIDEO_SCALING_MODE_SCALE_TO_FIT | The content is scaled to the surface dimensions | |||||||||
int | VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING | The content is scaled, maintaining its aspect ratio, the whole surface area is used, content may be cropped |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Configures a component.
| |||||||||||
If you know the exact name of the component you want to instantiate
use this method to instantiate it.
| |||||||||||
Instantiate a decoder supporting input data of the given mime type.
| |||||||||||
Instantiate an encoder supporting output data of the given mime type.
| |||||||||||
Requests a Surface to use as the input to an encoder, in place of input buffers.
| |||||||||||
Returns the index of an input buffer to be filled with valid data
or -1 if no such buffer is currently available.
| |||||||||||
Dequeue an output buffer, block at most "timeoutUs" microseconds.
| |||||||||||
Flush both input and output ports of the component, all indices
previously returned in calls to
dequeueInputBuffer(long) and
dequeueOutputBuffer(MediaCodec.BufferInfo, long) become invalid.
| |||||||||||
Get the codec info.
| |||||||||||
Returns a
cleared , writable ByteBuffer
object for a dequeued input buffer index to contain the input data.
| |||||||||||
This method was deprecated
in API level 21.
Use the new
getInputBuffer(int) method instead
each time an input buffer is dequeued.
Note:As of API 21, dequeued input buffers are
automatically cleared .
| |||||||||||
Call this after
configure(MediaFormat, Surface, MediaCrypto, int) returns successfully to
get the input format accepted by the codec.
| |||||||||||
Returns a writable Image object for a dequeued input buffer
index to contain the raw input video frame.
| |||||||||||
Get the component name.
| |||||||||||
Returns a read-only ByteBuffer for a dequeued output buffer
index.
| |||||||||||
This method was deprecated
in API level 21.
Use the new
getOutputBuffer(int) method instead
each time an output buffer is dequeued. This method is not
supported if codec is configured in asynchronous mode.
Note:As of API 21, the position and limit of output
buffers that are dequeued will be set to the valid data
range.
| |||||||||||
Call this after dequeueOutputBuffer signals a format change by returning
INFO_OUTPUT_FORMAT_CHANGED .
| |||||||||||
Returns the output format for a specific output buffer.
| |||||||||||
Returns a read-only Image object for a dequeued output buffer
index that contains the raw video frame.
| |||||||||||
After filling a range of the input buffer at the specified index
submit it to the component.
| |||||||||||
Similar to
queueInputBuffer(int, int, int, long, int) but submits a buffer that is
potentially encrypted.
| |||||||||||
Make sure you call this when you're done to free up any opened
component instance instead of relying on the garbage collector
to do this for you at some point in the future.
| |||||||||||
If you are done with a buffer, use this call to return the buffer to
the codec.
| |||||||||||
If you are done with a buffer, use this call to update its surface timestamp
and return it to the codec to render it on the output surface.
| |||||||||||
Returns the codec to its initial (Initialized) state.
| |||||||||||
Sets an asynchronous callback for actionable MediaCodec events.
| |||||||||||
Communicate additional parameter changes to the component instance.
| |||||||||||
If a surface has been specified in a previous call to
configure(MediaFormat, Surface, MediaCrypto, int)
specifies the scaling mode to use.
| |||||||||||
Signals end-of-stream on input.
| |||||||||||
After successfully configuring the component, call
start .
| |||||||||||
Finish the decode/encode session, note that the codec instance
remains active and ready to be
start() ed again.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
This indicated that the buffer marked as such contains codec initialization / codec specific data instead of media data.
This signals the end of stream, i.e. no buffers will be available
after this, unless of course, flush()
follows.
This indicates that the (encoded) buffer marked as such contains the data for a key frame.
This constant was deprecated
in API level 21.
Use BUFFER_FLAG_KEY_FRAME
instead.
This indicates that the (encoded) buffer marked as such contains the data for a key frame.
If this codec is to be used as an encoder, pass this flag.
This constant was deprecated
in API level 21.
This return value can be ignored as getOutputBuffers()
has been deprecated. Client should
request a current buffer using on of the get-buffer or
get-image methods each time one has been dequeued.
The output buffers have changed, the client must refer to the new
set of output buffers returned by getOutputBuffers()
from
this point on.
The output format has changed, subsequent data will follow the new
format. getOutputFormat()
returns the new format. Note, that
you can also use the new getOutputFormat(int)
method to
get the format for a specific output buffer. This frees you from
having to track output format changes.
If a non-negative timeout had been specified in the call
to dequeueOutputBuffer(MediaCodec.BufferInfo, long)
, indicates that the call timed out.
Request that the encoder produce a sync frame "soon". Provide an Integer with the value 0.
Temporarily suspend/resume encoding of input data. While suspended input data is effectively discarded instead of being fed into the encoder. This parameter really only makes sense to use with an encoder in "surface-input" mode, as the client code has no control over the input-side of the encoder in that case. The value is an Integer object containing the value 1 to suspend or the value 0 to resume.
Change a video encoder's target bitrate on the fly. The value is an Integer object containing the new bitrate in bps.
The content is scaled to the surface dimensions
The content is scaled, maintaining its aspect ratio, the whole surface area is used, content may be cropped
Configures a component.
format | The format of the input data (decoder) or the desired format of the output data (encoder). |
---|---|
surface | Specify a surface on which to render the output of this decoder. |
crypto | Specify a crypto object to facilitate secure decryption of the media data. |
flags | Specify CONFIGURE_FLAG_ENCODE to configure the
component as an encoder. |
IllegalArgumentException | if the surface has been released (or is invalid),
or the format is unacceptable (e.g. missing a mandatory key),
or the flags are not set properly
(e.g. missing CONFIGURE_FLAG_ENCODE for an encoder). |
---|---|
IllegalStateException | if not in the Initialized state. |
If you know the exact name of the component you want to instantiate
use this method to instantiate it. Use with caution.
Likely to be used with information obtained from MediaCodecList
name | The name of the codec to be instantiated. |
---|
IOException | if the codec cannot be created. |
---|---|
IllegalArgumentException | if name is not valid. |
NullPointerException | if name is null. |
Instantiate a decoder supporting input data of the given mime type. The following is a partial list of defined mime types and their semantics:
type | The mime type of the input data. |
---|
IOException | if the codec cannot be created. |
---|---|
IllegalArgumentException | if type is not a valid mime type. |
NullPointerException | if type is null. |
Instantiate an encoder supporting output data of the given mime type.
type | The desired mime type of the output data. |
---|
IOException | if the codec cannot be created. |
---|---|
IllegalArgumentException | if type is not a valid mime type. |
NullPointerException | if type is null. |
Requests a Surface to use as the input to an encoder, in place of input buffers. This
may only be called after configure(MediaFormat, Surface, MediaCrypto, int)
and before start()
.
The application is responsible for calling release() on the Surface when done.
The Surface must be rendered with a hardware-accelerated API, such as OpenGL ES.
lockCanvas(android.graphics.Rect)
may fail or produce
unexpected results.
IllegalStateException | if not in the Configured state. |
---|
Returns the index of an input buffer to be filled with valid data or -1 if no such buffer is currently available. This method will return immediately if timeoutUs == 0, wait indefinitely for the availability of an input buffer if timeoutUs < 0 or wait up to "timeoutUs" microseconds if timeoutUs > 0.
timeoutUs | The timeout in microseconds, a negative timeout indicates "infinite". |
---|
IllegalStateException | if not in the Executing state, or codec is configured in asynchronous mode. |
---|---|
MediaCodec.CodecException | upon codec error. |
Dequeue an output buffer, block at most "timeoutUs" microseconds. Returns the index of an output buffer that has been successfully decoded or one of the INFO_* constants below.
info | Will be filled with buffer meta data. |
---|---|
timeoutUs | The timeout in microseconds, a negative timeout indicates "infinite". |
IllegalStateException | if not in the Executing state, or codec is configured in asynchronous mode. |
---|---|
MediaCodec.CodecException | upon codec error. |
Flush both input and output ports of the component, all indices
previously returned in calls to dequeueInputBuffer(long)
and
dequeueOutputBuffer(MediaCodec.BufferInfo, long)
become invalid.
If codec is configured in asynchronous mode, call start()
after flush
has returned to resume codec operations. The
codec will not request input buffers until this has happened.
If codec is configured in synchronous mode, codec will resume
automatically if an input surface was created. Otherwise, it
will resume when dequeueInputBuffer(long)
is called.
IllegalStateException | if not in the Executing state. |
---|---|
MediaCodec.CodecException | upon codec error. |
Get the codec info. If the codec was created by createDecoderByType or createEncoderByType, what component is chosen is not known beforehand, and thus the caller does not have the MediaCodecInfo.
IllegalStateException | if in the Uninitialized state. |
---|
Returns a cleared
, writable ByteBuffer
object for a dequeued input buffer index to contain the input data.
After calling this method any ByteBuffer or Image object
previously returned for the same input index MUST no longer
be used.
index | The index of a client-owned input buffer previously
returned from a call to dequeueInputBuffer(long) ,
or received via an onInputBufferAvailable callback. |
---|
IllegalStateException | if not in the Executing state. |
---|---|
MediaCodec.CodecException | upon codec error. |
This method was deprecated
in API level 21.
Use the new getInputBuffer(int)
method instead
each time an input buffer is dequeued.
Note:As of API 21, dequeued input buffers are
automatically cleared
.
Retrieve the set of input buffers. Call this after start() returns. After calling this method, any ByteBuffers previously returned by an earlier call to this method MUST no longer be used.
IllegalStateException | if not in the Executing state, or codec is configured in asynchronous mode. |
---|---|
MediaCodec.CodecException | upon codec error. |
Call this after configure(MediaFormat, Surface, MediaCrypto, int)
returns successfully to
get the input format accepted by the codec. Do this to
determine what optional configuration parameters were
supported by the codec.
IllegalStateException | if not in the Executing or Configured state. |
---|---|
MediaCodec.CodecException | upon codec error. |
Returns a writable Image object for a dequeued input buffer index to contain the raw input video frame. After calling this method any ByteBuffer or Image object previously returned for the same input index MUST no longer be used.
index | The index of a client-owned input buffer previously
returned from a call to dequeueInputBuffer(long) ,
or received via an onInputBufferAvailable callback. |
---|
IllegalStateException | if not in the Executing state. |
---|---|
MediaCodec.CodecException | upon codec error. |
Get the component name. If the codec was created by createDecoderByType or createEncoderByType, what component is chosen is not known beforehand.
IllegalStateException | if in the Uninitialized state. |
---|
Returns a read-only ByteBuffer for a dequeued output buffer index. The position and limit of the returned buffer are set to the valid output data. After calling this method, any ByteBuffer or Image object previously returned for the same output index MUST no longer be used.
index | The index of a client-owned output buffer previously
returned from a call to dequeueOutputBuffer(MediaCodec.BufferInfo, long) ,
or received via an onOutputBufferAvailable callback. |
---|
IllegalStateException | if not in the Executing state. |
---|---|
MediaCodec.CodecException | upon codec error. |
This method was deprecated
in API level 21.
Use the new getOutputBuffer(int)
method instead
each time an output buffer is dequeued. This method is not
supported if codec is configured in asynchronous mode.
Note:As of API 21, the position and limit of output
buffers that are dequeued will be set to the valid data
range.
Retrieve the set of output buffers. Call this after start()
returns and whenever dequeueOutputBuffer signals an output
buffer change by returning INFO_OUTPUT_BUFFERS_CHANGED
. After calling this method, any
ByteBuffers previously returned by an earlier call to this
method MUST no longer be used.
IllegalStateException | if not in the Executing state, or codec is configured in asynchronous mode. |
---|---|
MediaCodec.CodecException | upon codec error. |
Call this after dequeueOutputBuffer signals a format change by returning
INFO_OUTPUT_FORMAT_CHANGED
.
You can also call this after configure(MediaFormat, Surface, MediaCrypto, int)
returns
successfully to get the output format initially configured
for the codec. Do this to determine what optional
configuration parameters were supported by the codec.
IllegalStateException | if not in the Executing or Configured state. |
---|---|
MediaCodec.CodecException | upon codec error. |
Returns the output format for a specific output buffer.
index | The index of a client-owned input buffer previously
returned from a call to dequeueInputBuffer(long) . |
---|
Returns a read-only Image object for a dequeued output buffer index that contains the raw video frame. After calling this method, any ByteBuffer or Image object previously returned for the same output index MUST no longer be used.
index | The index of a client-owned output buffer previously
returned from a call to dequeueOutputBuffer(MediaCodec.BufferInfo, long) ,
or received via an onOutputBufferAvailable callback. |
---|
IllegalStateException | if not in the Executing state. |
---|---|
MediaCodec.CodecException | upon codec error. |
After filling a range of the input buffer at the specified index
submit it to the component. Once an input buffer is queued to
the codec, it MUST NOT be used until it is later retrieved by
getInputBuffer(int)
in response to a dequeueInputBuffer(long)
return value or a onInputBufferAvailable(MediaCodec, int)
callback.
Many decoders require the actual compressed data stream to be
preceded by "codec specific data", i.e. setup data used to initialize
the codec such as PPS/SPS in the case of AVC video or code tables
in the case of vorbis audio.
The class MediaExtractor
provides codec
specific data as part of
the returned track format in entries named "csd-0", "csd-1" ...
These buffers can be submitted directly after start()
or
flush()
by specifying the flag BUFFER_FLAG_CODEC_CONFIG
. However, if you configure the
codec with a MediaFormat
containing these keys, they
will be automatically submitted by MediaCodec directly after
start. Therefore, the use of BUFFER_FLAG_CODEC_CONFIG
flag is discouraged and is
recommended only for advanced users.
To indicate that this is the final piece of input data (or rather that
no more input data follows unless the decoder is subsequently flushed)
specify the flag BUFFER_FLAG_END_OF_STREAM
.
index | The index of a client-owned input buffer previously returned
in a call to dequeueInputBuffer(long) . |
---|---|
offset | The byte offset into the input buffer at which the data starts. |
size | The number of bytes of valid input data. |
presentationTimeUs | The presentation timestamp in microseconds for this buffer. This is normally the media time at which this buffer should be presented (rendered). |
flags | A bitmask of flags
BUFFER_FLAG_CODEC_CONFIG and BUFFER_FLAG_END_OF_STREAM .
While not prohibited, most codecs do not use the
BUFFER_FLAG_KEY_FRAME flag for input buffers. |
IllegalStateException | if not in the Executing state. |
---|---|
MediaCodec.CodecException | upon codec error. |
MediaCodec.CryptoException | if a crypto object has been specified in
configure(MediaFormat, Surface, MediaCrypto, int)
|
Similar to queueInputBuffer(int, int, int, long, int)
but submits a buffer that is
potentially encrypted.
index | The index of a client-owned input buffer previously returned
in a call to dequeueInputBuffer(long) . |
---|---|
offset | The byte offset into the input buffer at which the data starts. |
info | Metadata required to facilitate decryption, the object can be reused immediately after this call returns. |
presentationTimeUs | The presentation timestamp in microseconds for this buffer. This is normally the media time at which this buffer should be presented (rendered). |
flags | A bitmask of flags
BUFFER_FLAG_CODEC_CONFIG and BUFFER_FLAG_END_OF_STREAM .
While not prohibited, most codecs do not use the
BUFFER_FLAG_KEY_FRAME flag for input buffers. |
IllegalStateException | if not in the Executing state. |
---|---|
MediaCodec.CodecException | upon codec error. |
MediaCodec.CryptoException | if an error occurs while attempting to decrypt the buffer. An error code associated with the exception helps identify the reason for the failure. |
Make sure you call this when you're done to free up any opened component instance instead of relying on the garbage collector to do this for you at some point in the future.
If you are done with a buffer, use this call to return the buffer to
the codec. If you previously specified a surface when configuring this
video decoder you can optionally render the buffer.
Once an output buffer is released to the codec, it MUST NOT
be used until it is later retrieved by getOutputBuffer(int)
in response
to a dequeueOutputBuffer(MediaCodec.BufferInfo, long)
return value or a
onOutputBufferAvailable(MediaCodec, int, MediaCodec.BufferInfo)
callback.
index | The index of a client-owned output buffer previously returned
from a call to dequeueOutputBuffer(MediaCodec.BufferInfo, long) . |
---|---|
render | If a valid surface was specified when configuring the codec, passing true renders this output buffer to the surface. |
IllegalStateException | if not in the Executing state. |
---|---|
MediaCodec.CodecException | upon codec error. |
If you are done with a buffer, use this call to update its surface timestamp and return it to the codec to render it on the output surface. If you have not specified an output surface when configuring this video codec, this call will simply return the buffer to the codec.
The timestamp may have special meaning depending on the destination surface.
SurfaceView specifics |
---|
If you render your buffer on a SurfaceView ,
you can use the timestamp to render the buffer at a specific time (at the
VSYNC at or after the buffer timestamp). For this to work, the timestamp
needs to be reasonably close to the current nanoTime() .
Currently, this is set as within one (1) second. A few notes:
|
getOutputBuffer(int)
in response
to a dequeueOutputBuffer(MediaCodec.BufferInfo, long)
return value or a
onOutputBufferAvailable(MediaCodec, int, MediaCodec.BufferInfo)
callback.index | The index of a client-owned output buffer previously returned
from a call to dequeueOutputBuffer(MediaCodec.BufferInfo, long) . |
---|---|
renderTimestampNs | The timestamp to associate with this buffer when it is sent to the Surface. |
IllegalStateException | if not in the Executing state. |
---|---|
MediaCodec.CodecException | upon codec error. |
Returns the codec to its initial (Initialized) state.
Call this if an unrecoverable
error has occured to reset the codec to its initial state after creation.
MediaCodec.CodecException | if an unrecoverable error has occured and the codec could not be reset. |
---|---|
IllegalStateException | if in the Uninitialized state. |
Sets an asynchronous callback for actionable MediaCodec events.
If the client intends to use the component in asynchronous mode,
a valid callback should be provided before configure(MediaFormat, Surface, MediaCrypto, int)
is called.
When asynchronous callback is enabled, the client should not call
getInputBuffers()
, getOutputBuffers()
,
dequeueInputBuffer(long)
or dequeueOutputBuffer(BufferInfo, long)
.
Also, flush()
behaves differently in asynchronous mode. After calling
flush
, you must call start()
to "resume" receiving input buffers,
even if an input surface was created.
cb | The callback that will run. |
---|
Communicate additional parameter changes to the component instance.
IllegalStateException | if in the Uninitialized state. |
---|
If a surface has been specified in a previous call to configure(MediaFormat, Surface, MediaCrypto, int)
specifies the scaling mode to use. The default is "scale to fit".
IllegalArgumentException | if mode is not recognized. |
---|---|
IllegalStateException | if in the Uninitialized state. |
Signals end-of-stream on input. Equivalent to submitting an empty buffer with
BUFFER_FLAG_END_OF_STREAM
set. This may only be used with
encoders receiving input from a Surface created by createInputSurface()
.
IllegalStateException | if not in the Executing state. |
---|---|
MediaCodec.CodecException | upon codec error. |
After successfully configuring the component, call start
.
Call start
also if the codec is configured in asynchronous mode,
and it has just been flushed, to resume requesting input buffers.
IllegalStateException | if not in the Configured state
or just after flush() for a codec that is configured
in asynchronous mode. |
---|---|
MediaCodec.CodecException | upon codec error. Note that some codec errors for start may be attributed to future method calls. |
Finish the decode/encode session, note that the codec instance
remains active and ready to be start()
ed again.
To ensure that it is available to other client call release()
and don't just rely on garbage collection to eventually do this for you.
IllegalStateException | if in the Uninitialized state. |
---|
Invoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.
Note that objects that override finalize
are significantly more expensive than
objects that don't. Finalizers may be run a long time after the object is no longer
reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup.
Note also that finalizers are run on a single VM-wide finalizer thread,
so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary
for a class that has a native peer and needs to call a native method to destroy that peer.
Even then, it's better to provide an explicit close
method (and implement
Closeable
), and insist that callers manually dispose of instances. This
works well for something like files, but less well for something like a BigInteger
where typical calling code would have to deal with lots of temporaries. Unfortunately,
code that creates lots of temporaries is the worst kind of code from the point of view of
the single finalizer thread.
If you must use finalizers, consider at least providing your own
ReferenceQueue
and having your own thread process that queue.
Unlike constructors, finalizers are not automatically chained. You are responsible for
calling super.finalize()
yourself.
Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.