/**@class android.media.MediaCodec @extends java.lang.Object MediaCodec class can be used to access low-level media codecs, i.e. encoder/decoder components. It is part of the Android low-level multimedia support infrastructure (normally used together with {@link android.media.MediaExtractor}, {@link android.media.MediaSync}, {@link android.media.MediaMuxer}, {@link android.media.MediaCrypto}, {@link android.media.MediaDrm}, {@link android.media.Image}, {@link Surface}, and {@link android.media.AudioTrack}.) <p> <center><object style="width: 540px; height: 205px;" type="image/svg+xml" data="../../../images/media/mediacodec_buffers.svg"><img src="../../../images/media/mediacodec_buffers.png" style="width: 540px; height: 205px" alt="MediaCodec buffer flow diagram"></object></center> <p> In broad terms, a codec processes input data to generate output data. It processes data asynchronously and uses a set of input and output buffers. At a simplistic level, you request (or receive) an empty input buffer, fill it up with data and send it to the codec for processing. The codec uses up the data and transforms it into one of its empty output buffers. Finally, you request (or receive) a filled output buffer, consume its contents and release it back to the codec. <h3>Data Types</h3> <p> Codecs operate on three kinds of data: compressed data, raw audio data and raw video data. All three kinds of data can be processed using {@link ByteBuffer ByteBuffers}, but you should use a {@link Surface} for raw video data to improve codec performance. Surface uses native video buffers without mapping or copying them to ByteBuffers; thus, it is much more efficient. You normally cannot access the raw video data when using a Surface, but you can use the {@link android.media.ImageReader} class to access unsecured decoded (raw) video frames. This may still be more efficient than using ByteBuffers, as some native buffers may be mapped into {@linkplain ByteBuffer#isDirect direct} ByteBuffers. When using ByteBuffer mode, you can access raw video frames using the {@link android.media.Image} class and {@link #getInputImage getInput}/{@link #getOutputImage OutputImage(int)}. <h4>Compressed Buffers</h4> <p> Input buffers (for decoders) and output buffers (for encoders) contain compressed data according to the {@linkplain android.media.MediaFormat#KEY_MIME format's type}. For video types this is normally a single compressed video frame. For audio data this is normally a single access unit (an encoded audio segment typically containing a few milliseconds of audio as dictated by the format type), but this requirement is slightly relaxed in that a buffer may contain multiple encoded access units of audio. In either case, buffers do not start or end on arbitrary byte boundaries, but rather on frame/access unit boundaries unless they are flagged with {@link #BUFFER_FLAG_PARTIAL_FRAME}. <h4>Raw Audio Buffers</h4> <p> Raw audio buffers contain entire frames of PCM audio data, which is one sample for each channel in channel order. Each PCM audio sample is either a 16 bit signed integer or a float, in native byte order. Raw audio buffers in the float PCM encoding are only possible if the MediaFormat's {@linkplain android.media.MediaFormat#KEY_PCM_ENCODING} is set to {@linkplain android.media.AudioFormat#ENCODING_PCM_FLOAT} during MediaCodec {@link #configure configure(…)} and confirmed by {@link #getOutputFormat} for decoders or {@link #getInputFormat} for encoders. A sample method to check for float PCM in the MediaFormat is as follows: <pre class=prettyprint> static boolean isPcmFloat(MediaFormat format) { return format.getInteger(MediaFormat.KEY_PCM_ENCODING, AudioFormat.ENCODING_PCM_16BIT) == AudioFormat.ENCODING_PCM_FLOAT; }</pre> In order to extract, in a short array, one channel of a buffer containing 16 bit signed integer audio data, the following code may be used: <pre class=prettyprint> // Assumes the buffer PCM encoding is 16 bit. short[] getSamplesForChannel(MediaCodec codec, int bufferId, int channelIx) { ByteBuffer outputBuffer = codec.getOutputBuffer(bufferId); MediaFormat format = codec.getOutputFormat(bufferId); ShortBuffer samples = outputBuffer.order(ByteOrder.nativeOrder()).asShortBuffer(); int numChannels = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT); if (channelIx < 0 || channelIx >= numChannels) { return null; } short[] res = new short[samples.remaining() / numChannels]; for (int i = 0; i < res.length; ++i) { res[i] = samples.get(i * numChannels + channelIx); } return res; }</pre> <h4>Raw Video Buffers</h4> <p> In ByteBuffer mode video buffers are laid out according to their {@linkplain android.media.MediaFormat#KEY_COLOR_FORMAT color format}. You can get the supported color formats as an array from {@link #getCodecInfo}{@code .}{@link android.media.MediaCodecInfo#getCapabilitiesForType getCapabilitiesForType(…)}{@code .}{@link android.media.MediaCodecInfo.CodecCapabilities#colorFormats colorFormats}. Video codecs may support three kinds of color formats: <ul> <li><strong>native raw video format:</strong> This is marked by {@link android.media.MediaCodecInfo.CodecCapabilities#COLOR_FormatSurface} and it can be used with an input or output Surface.</li> <li><strong>flexible YUV buffers</strong> (such as {@link android.media.MediaCodecInfo.CodecCapabilities#COLOR_FormatYUV420Flexible}): These can be used with an input/output Surface, as well as in ByteBuffer mode, by using {@link #getInputImage getInput}/{@link #getOutputImage OutputImage(int)}.</li> <li><strong>other, specific formats:</strong> These are normally only supported in ByteBuffer mode. Some color formats are vendor specific. Others are defined in {@link android.media.MediaCodecInfo.CodecCapabilities}. For color formats that are equivalent to a flexible format, you can still use {@link #getInputImage getInput}/{@link #getOutputImage OutputImage(int)}.</li> </ul> <p> All video codecs support flexible YUV 4:2:0 buffers since {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1}. <h4>Accessing Raw Video ByteBuffers on Older Devices</h4> <p> Prior to {@link android.os.Build.VERSION_CODES#LOLLIPOP} and {@link android.media.Image} support, you need to use the {@link android.media.MediaFormat#KEY_STRIDE} and {@link android.media.MediaFormat#KEY_SLICE_HEIGHT} output format values to understand the layout of the raw output buffers. <p class=note> Note that on some devices the slice-height is advertised as 0. This could mean either that the slice-height is the same as the frame height, or that the slice-height is the frame height aligned to some value (usually a power of 2). Unfortunately, there is no standard and simple way to tell the actual slice height in this case. Furthermore, the vertical stride of the {@code U} plane in planar formats is also not specified or defined, though usually it is half of the slice height. <p> The {@link android.media.MediaFormat#KEY_WIDTH} and {@link android.media.MediaFormat#KEY_HEIGHT} keys specify the size of the video frames; however, for most encondings the video (picture) only occupies a portion of the video frame. This is represented by the 'crop rectangle'. <p> You need to use the following keys to get the crop rectangle of raw output images from the {@linkplain #getOutputFormat output format}. If these keys are not present, the video occupies the entire video frame.The crop rectangle is understood in the context of the output frame <em>before</em> applying any {@linkplain android.media.MediaFormat#KEY_ROTATION rotation}. <table style="width: 0%"> <thead> <tr> <th>Format Key</th> <th>Type</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td>{@code "crop-left"}</td> <td>Integer</td> <td>The left-coordinate (x) of the crop rectangle</td> </tr><tr> <td>{@code "crop-top"}</td> <td>Integer</td> <td>The top-coordinate (y) of the crop rectangle</td> </tr><tr> <td>{@code "crop-right"}</td> <td>Integer</td> <td>The right-coordinate (x) <strong>MINUS 1</strong> of the crop rectangle</td> </tr><tr> <td>{@code "crop-bottom"}</td> <td>Integer</td> <td>The bottom-coordinate (y) <strong>MINUS 1</strong> of the crop rectangle</td> </tr><tr> <td colspan=3> The right and bottom coordinates can be understood as the coordinates of the right-most valid column/bottom-most valid row of the cropped output image. </td> </tr> </tbody> </table> <p> The size of the video frame (before rotation) can be calculated as such: <pre class=prettyprint> MediaFormat format = decoder.getOutputFormat(…); int width = format.getInteger(MediaFormat.KEY_WIDTH); if (format.containsKey("crop-left") && format.containsKey("crop-right")) { width = format.getInteger("crop-right") + 1 - format.getInteger("crop-left"); } int height = format.getInteger(MediaFormat.KEY_HEIGHT); if (format.containsKey("crop-top") && format.containsKey("crop-bottom")) { height = format.getInteger("crop-bottom") + 1 - format.getInteger("crop-top"); } </pre> <p class=note> Also note that the meaning of {@link android.media.MediaCodec.BufferInfo#offset android.media.MediaCodec.BufferInfo.offset} was not consistent across devices. On some devices the offset pointed to the top-left pixel of the crop rectangle, while on most devices it pointed to the top-left pixel of the entire frame. <h3>States</h3> <p> During its life a codec conceptually exists in one of three states: Stopped, Executing or Released. The Stopped collective state is actually the conglomeration of three states: Uninitialized, Configured and Error, whereas the Executing state conceptually progresses through three sub-states: Flushed, Running and End-of-Stream. <p> <center><object style="width: 516px; height: 353px;" type="image/svg+xml" data="../../../images/media/mediacodec_states.svg"><img src="../../../images/media/mediacodec_states.png" style="width: 519px; height: 356px" alt="MediaCodec state diagram"></object></center> <p> When you create a codec using one of the factory methods, the codec is in the Uninitialized state. First, you need to configure it via {@link #configure configure(…)}, which brings it to the Configured state, then call {@link #start} to move it to the Executing state. In this state you can process data through the buffer queue manipulation described above. <p> The Executing state has three sub-states: Flushed, Running and End-of-Stream. Immediately after {@link #start} the codec is in the Flushed sub-state, where it holds all the buffers. As soon as the first input buffer is dequeued, the codec moves to the Running sub-state, where it spends most of its life. When you queue an input buffer with the {@linkplain #BUFFER_FLAG_END_OF_STREAM end-of-stream marker}, the codec transitions to the End-of-Stream sub-state. In this state the codec no longer accepts further input buffers, but still generates output buffers until the end-of-stream is reached on the output. You can move back to the Flushed sub-state at any time while in the Executing state using {@link #flush}. <p> Call {@link #stop} to return the codec to the Uninitialized state, whereupon it may be configured again. When you are done using a codec, you must release it by calling {@link #release}. <p> On rare occasions the codec may encounter an error and move to the Error state. This is communicated using an invalid return value from a queuing operation, or sometimes via an exception. Call {@link #reset} to make the codec usable again. You can call it from any state to move the codec back to the Uninitialized state. Otherwise, call {@link #release} to move to the terminal Released state. <h3>Creation</h3> <p> Use {@link android.media.MediaCodecList} to create a MediaCodec for a specific {@link android.media.MediaFormat}. When decoding a file or a stream, you can get the desired format from {@link android.media.MediaExtractor#getTrackFormat android.media.MediaExtractor.getTrackFormat}. Inject any specific features that you want to add using {@link android.media.MediaFormat#setFeatureEnabled android.media.MediaFormat.setFeatureEnabled}, then call {@link android.media.MediaCodecList#findDecoderForFormat android.media.MediaCodecList.findDecoderForFormat} to get the name of a codec that can handle that specific media format. Finally, create the codec using {@link #createByCodecName}. <p class=note> <strong>Note:</strong> On {@link android.os.Build.VERSION_CODES#LOLLIPOP}, the format to {@code MediaCodecList.findDecoder}/{@code EncoderForFormat} must not contain a {@linkplain android.media.MediaFormat#KEY_FRAME_RATE frame rate}. Use <code class=prettyprint>format.setString(MediaFormat.KEY_FRAME_RATE, null)</code> to clear any existing frame rate setting in the format. <p> You can also create the preferred codec for a specific MIME type using {@link #createDecoderByType createDecoder}/{@link #createEncoderByType EncoderByType(String)}. This, however, cannot be used to inject features, and may create a codec that cannot handle the specific desired media format. <h4>Creating secure decoders</h4> <p> On versions {@link android.os.Build.VERSION_CODES#KITKAT_WATCH} and earlier, secure codecs might not be listed in {@link android.media.MediaCodecList}, but may still be available on the system. Secure codecs that exist can be instantiated by name only, by appending {@code ".secure"} to the name of a regular codec (the name of all secure codecs must end in {@code ".secure"}.) {@link #createByCodecName} will throw an {@code IOException} if the codec is not present on the system. <p> From {@link android.os.Build.VERSION_CODES#LOLLIPOP} onwards, you should use the {@link android.media.MediaCodecInfo.CodecCapabilities#FEATURE_SecurePlayback} feature in the media format to create a secure decoder. <h3>Initialization</h3> <p> After creating the codec, you can set a callback using {@link #setCallback setCallback} if you want to process data asynchronously. Then, {@linkplain #configure configure} the codec using the specific media format. This is when you can specify the output {@link Surface} for video producers – codecs that generate raw video data (e.g. video decoders). This is also when you can set the decryption parameters for secure codecs (see {@link android.media.MediaCrypto}). Finally, since some codecs can operate in multiple modes, you must specify whether you want it to work as a decoder or an encoder. <p> Since {@link android.os.Build.VERSION_CODES#LOLLIPOP}, you can query the resulting input and output format in the Configured state. You can use this to verify the resulting configuration, e.g. color formats, before starting the codec. <p> If you want to process raw input video buffers natively with a video consumer – a codec that processes raw video input, such as a video encoder – create a destination Surface for your input data using {@link #createInputSurface} after configuration. Alternately, set up the codec to use a previously created {@linkplain #createPersistentInputSurface persistent input surface} by calling {@link #setInputSurface}. <h4 id=CSD><a name="CSD"></a>Codec-specific Data</h4> <p> Some formats, notably AAC audio and MPEG4, H.264 and H.265 video formats require the actual data to be prefixed by a number of buffers containing setup data, or codec specific data. When processing such compressed formats, this data must be submitted to the codec after {@link #start} and before any frame data. Such data must be marked using the flag {@link #BUFFER_FLAG_CODEC_CONFIG} in a call to {@link #queueInputBuffer queueInputBuffer}. <p> Codec-specific data can also be included in the format passed to {@link #configure configure} in ByteBuffer entries with keys "csd-0", "csd-1", etc. These keys are always included in the track {@link android.media.MediaFormat} obtained from the {@link android.media.MediaExtractor#getTrackFormat android.media.MediaExtractor}. Codec-specific data in the format is automatically submitted to the codec upon {@link #start}; you <strong>MUST NOT</strong> submit this data explicitly. If the format did not contain codec specific data, you can choose to submit it using the specified number of buffers in the correct order, according to the format requirements. In case of H.264 AVC, you can also concatenate all codec-specific data and submit it as a single codec-config buffer. <p> Android uses the following codec-specific data buffers. These are also required to be set in the track format for proper {@link android.media.MediaMuxer} track configuration. Each parameter set and the codec-specific-data sections marked with (<sup>*</sup>) must start with a start code of {@code "\x00\x00\x00\x01"}. <p> <style>td.NA { background: #ccc; } .mid > tr > td { vertical-align: middle; }</style> <table> <thead> <th>Format</th> <th>CSD buffer #0</th> <th>CSD buffer #1</th> <th>CSD buffer #2</th> </thead> <tbody class=mid> <tr> <td>AAC</td> <td>Decoder-specific information from ESDS<sup>*</sup></td> <td class=NA>Not Used</td> <td class=NA>Not Used</td> </tr> <tr> <td>VORBIS</td> <td>Identification header</td> <td>Setup header</td> <td class=NA>Not Used</td> </tr> <tr> <td>OPUS</td> <td>Identification header</td> <td>Pre-skip in nanosecs<br> (unsigned 64-bit {@linkplain ByteOrder#nativeOrder native-order} integer.)<br> This overrides the pre-skip value in the identification header.</td> <td>Seek Pre-roll in nanosecs<br> (unsigned 64-bit {@linkplain ByteOrder#nativeOrder native-order} integer.)</td> </tr> <tr> <td>FLAC</td> <td>mandatory metadata block (called the STREAMINFO block),<br> optionally followed by any number of other metadata blocks</td> <td class=NA>Not Used</td> <td class=NA>Not Used</td> </tr> <tr> <td>MPEG-4</td> <td>Decoder-specific information from ESDS<sup>*</sup></td> <td class=NA>Not Used</td> <td class=NA>Not Used</td> </tr> <tr> <td>H.264 AVC</td> <td>SPS (Sequence Parameter Sets<sup>*</sup>)</td> <td>PPS (Picture Parameter Sets<sup>*</sup>)</td> <td class=NA>Not Used</td> </tr> <tr> <td>H.265 HEVC</td> <td>VPS (Video Parameter Sets<sup>*</sup>) +<br> SPS (Sequence Parameter Sets<sup>*</sup>) +<br> PPS (Picture Parameter Sets<sup>*</sup>)</td> <td class=NA>Not Used</td> <td class=NA>Not Used</td> </tr> <tr> <td>VP9</td> <td>VP9 <a href="http://wiki.webmproject.org/vp9-codecprivate">CodecPrivate</a> Data (optional)</td> <td class=NA>Not Used</td> <td class=NA>Not Used</td> </tr> </tbody> </table> <p class=note> <strong>Note:</strong> care must be taken if the codec is flushed immediately or shortly after start, before any output buffer or output format change has been returned, as the codec specific data may be lost during the flush. You must resubmit the data using buffers marked with {@link #BUFFER_FLAG_CODEC_CONFIG} after such flush to ensure proper codec operation. <p> Encoders (or codecs that generate compressed data) will create and return the codec specific data before any valid output buffer in output buffers marked with the {@linkplain #BUFFER_FLAG_CODEC_CONFIG codec-config flag}. Buffers containing codec-specific-data have no meaningful timestamps. <h3>Data Processing</h3> <p> Each codec maintains a set of input and output buffers that are referred to by a buffer-ID in API calls. After a successful call to {@link #start} the client "owns" neither input nor output buffers. In synchronous mode, call {@link #dequeueInputBuffer dequeueInput}/{@link #dequeueOutputBuffer OutputBuffer(…)} to obtain (get ownership of) an input or output buffer from the codec. In asynchronous mode, you will automatically receive available buffers via the {@link Callback#onInputBufferAvailable android.media.MediaCodec.Callback.onInput}/{@link android.media.MediaCodec.Callback#onOutputBufferAvailable OutputBufferAvailable(…)} callbacks. <p> Upon obtaining an input buffer, fill it with data and submit it to the codec using {@link #queueInputBuffer queueInputBuffer} – or {@link #queueSecureInputBuffer queueSecureInputBuffer} if using decryption. Do not submit multiple input buffers with the same timestamp (unless it is <a href="#CSD">codec-specific data</a> marked as such). <p> The codec in turn will return a read-only output buffer via the {@link android.media.MediaCodec.Callback#onOutputBufferAvailable onOutputBufferAvailable} callback in asynchronous mode, or in response to a {@link #dequeueOutputBuffer dequeueOutputBuffer} call in synchronous mode. After the output buffer has been processed, call one of the {@link #releaseOutputBuffer releaseOutputBuffer} methods to return the buffer to the codec. <p> While you are not required to resubmit/release buffers immediately to the codec, holding onto input and/or output buffers may stall the codec, and this behavior is device dependent. <strong>Specifically, it is possible that a codec may hold off on generating output buffers until <em>all</em> outstanding buffers have been released/resubmitted.</strong> Therefore, try to hold onto to available buffers as little as possible. <p> Depending on the API version, you can process data in three ways: <table> <thead> <tr> <th>Processing Mode</th> <th>API version <= 20<br>Jelly Bean/KitKat</th> <th>API version >= 21<br>Lollipop and later</th> </tr> </thead> <tbody> <tr> <td>Synchronous API using buffer arrays</td> <td>Supported</td> <td>Deprecated</td> </tr> <tr> <td>Synchronous API using buffers</td> <td class=NA>Not Available</td> <td>Supported</td> </tr> <tr> <td>Asynchronous API using buffers</td> <td class=NA>Not Available</td> <td>Supported</td> </tr> </tbody> </table> <h4>Asynchronous Processing using Buffers</h4> <p> Since {@link android.os.Build.VERSION_CODES#LOLLIPOP}, the preferred method is to process data asynchronously by setting a callback before calling {@link #configure configure}. Asynchronous mode changes the state transitions slightly, because you must call {@link #start} after {@link #flush} to transition the codec to the Running sub-state and start receiving input buffers. Similarly, upon an initial call to {@code start} the codec will move directly to the Running sub-state and start passing available input buffers via the callback. <p> <center><object style="width: 516px; height: 353px;" type="image/svg+xml" data="../../../images/media/mediacodec_async_states.svg"><img src="../../../images/media/mediacodec_async_states.png" style="width: 516px; height: 353px" alt="MediaCodec state diagram for asynchronous operation"></object></center> <p> MediaCodec is typically used like this in asynchronous mode: <pre class=prettyprint> MediaCodec codec = MediaCodec.createByCodecName(name); MediaFormat mOutputFormat; // member variable codec.setCallback(new MediaCodec.Callback() { {@literal @Override} void onInputBufferAvailable(MediaCodec mc, int inputBufferId) { ByteBuffer inputBuffer = codec.getInputBuffer(inputBufferId); // fill inputBuffer with valid data … codec.queueInputBuffer(inputBufferId, …); } {@literal @Override} void onOutputBufferAvailable(MediaCodec mc, int outputBufferId, …) { ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferId); MediaFormat bufferFormat = codec.getOutputFormat(outputBufferId); // option A // bufferFormat is equivalent to mOutputFormat // outputBuffer is ready to be processed or rendered. … codec.releaseOutputBuffer(outputBufferId, …); } {@literal @Override} void onOutputFormatChanged(MediaCodec mc, MediaFormat format) { // Subsequent data will conform to new format. // Can ignore if using getOutputFormat(outputBufferId) mOutputFormat = format; // option B } {@literal @Override} void onError(…) { … } }); codec.configure(format, …); mOutputFormat = codec.getOutputFormat(); // option B codec.start(); // wait for processing to complete codec.stop(); codec.release();</pre> <h4>Synchronous Processing using Buffers</h4> <p> Since {@link android.os.Build.VERSION_CODES#LOLLIPOP}, you should retrieve input and output buffers using {@link #getInputBuffer getInput}/{@link #getOutputBuffer OutputBuffer(int)} and/or {@link #getInputImage getInput}/{@link #getOutputImage OutputImage(int)} even when using the codec in synchronous mode. This allows certain optimizations by the framework, e.g. when processing dynamic content. This optimization is disabled if you call {@link #getInputBuffers getInput}/{@link #getOutputBuffers OutputBuffers()}. <p class=note> <strong>Note:</strong> do not mix the methods of using buffers and buffer arrays at the same time. Specifically, only call {@code getInput}/{@code OutputBuffers} directly after {@link #start} or after having dequeued an output buffer ID with the value of {@link #INFO_OUTPUT_FORMAT_CHANGED}. <p> MediaCodec is typically used like this in synchronous mode: <pre> MediaCodec codec = MediaCodec.createByCodecName(name); codec.configure(format, …); MediaFormat outputFormat = codec.getOutputFormat(); // option B codec.start(); for (;;) { int inputBufferId = codec.dequeueInputBuffer(timeoutUs); if (inputBufferId >= 0) { ByteBuffer inputBuffer = codec.getInputBuffer(…); // fill inputBuffer with valid data … codec.queueInputBuffer(inputBufferId, …); } int outputBufferId = codec.dequeueOutputBuffer(…); if (outputBufferId >= 0) { ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferId); MediaFormat bufferFormat = codec.getOutputFormat(outputBufferId); // option A // bufferFormat is identical to outputFormat // outputBuffer is ready to be processed or rendered. … codec.releaseOutputBuffer(outputBufferId, …); } else if (outputBufferId == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) { // Subsequent data will conform to new format. // Can ignore if using getOutputFormat(outputBufferId) outputFormat = codec.getOutputFormat(); // option B } } codec.stop(); codec.release();</pre> <h4>Synchronous Processing using Buffer Arrays (deprecated)</h4> <p> In versions {@link android.os.Build.VERSION_CODES#KITKAT_WATCH} and before, the set of input and output buffers are represented by the {@code ByteBuffer[]} arrays. After a successful call to {@link #start}, retrieve the buffer arrays using {@link #getInputBuffers getInput}/{@link #getOutputBuffers OutputBuffers()}. Use the buffer ID-s as indices into these arrays (when non-negative), as demonstrated in the sample below. Note that there is no inherent correlation between the size of the arrays and the number of input and output buffers used by the system, although the array size provides an upper bound. <pre> MediaCodec codec = MediaCodec.createByCodecName(name); codec.configure(format, …); codec.start(); ByteBuffer[] inputBuffers = codec.getInputBuffers(); ByteBuffer[] outputBuffers = codec.getOutputBuffers(); for (;;) { int inputBufferId = codec.dequeueInputBuffer(…); if (inputBufferId >= 0) { // fill inputBuffers[inputBufferId] with valid data … codec.queueInputBuffer(inputBufferId, …); } int outputBufferId = codec.dequeueOutputBuffer(…); if (outputBufferId >= 0) { // outputBuffers[outputBufferId] is ready to be processed or rendered. … codec.releaseOutputBuffer(outputBufferId, …); } else if (outputBufferId == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) { outputBuffers = codec.getOutputBuffers(); } else if (outputBufferId == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) { // Subsequent data will conform to new format. MediaFormat format = codec.getOutputFormat(); } } codec.stop(); codec.release();</pre> <h4>End-of-stream Handling</h4> <p> When you reach the end of the input data, you must signal it to the codec by specifying the {@link #BUFFER_FLAG_END_OF_STREAM} flag in the call to {@link #queueInputBuffer queueInputBuffer}. You can do this on the last valid input buffer, or by submitting an additional empty input buffer with the end-of-stream flag set. If using an empty buffer, the timestamp will be ignored. <p> The codec will continue to return output buffers until it eventually signals the end of the output stream by specifying the same end-of-stream flag in the {@link android.media.MediaCodec.BufferInfo} set in {@link #dequeueOutputBuffer dequeueOutputBuffer} or returned via {@link android.media.MediaCodec.Callback#onOutputBufferAvailable onOutputBufferAvailable}. This can be set on the last valid output buffer, or on an empty buffer after the last valid output buffer. The timestamp of such empty buffer should be ignored. <p> Do not submit additional input buffers after signaling the end of the input stream, unless the codec has been flushed, or stopped and restarted. <h4>Using an Output Surface</h4> <p> The data processing is nearly identical to the ByteBuffer mode when using an output {@link Surface}; however, the output buffers will not be accessible, and are represented as {@code null} values. E.g. {@link #getOutputBuffer getOutputBuffer}/{@link #getOutputandroid.media.Image android.media.Image(int)} will return {@code null} and {@link #getOutputBuffers} will return an array containing only {@code null}-s. <p> When using an output Surface, you can select whether or not to render each output buffer on the surface. You have three choices: <ul> <li><strong>Do not render the buffer:</strong> Call {@link #releaseOutputBuffer(int, boolean) releaseOutputBuffer(bufferId, false)}.</li> <li><strong>Render the buffer with the default timestamp:</strong> Call {@link #releaseOutputBuffer(int, boolean) releaseOutputBuffer(bufferId, true)}.</li> <li><strong>Render the buffer with a specific timestamp:</strong> Call {@link #releaseOutputBuffer(int, long) releaseOutputBuffer(bufferId, timestamp)}.</li> </ul> <p> Since {@link android.os.Build.VERSION_CODES#M}, the default timestamp is the {@linkplain android.media.MediaCodec.BufferInfo#presentationTimeUs presentation timestamp} of the buffer (converted to nanoseconds). It was not defined prior to that. <p> Also since {@link android.os.Build.VERSION_CODES#M}, you can change the output Surface dynamically using {@link #setOutputSurface setOutputSurface}. <p> When rendering output to a Surface, the Surface may be configured to drop excessive frames (that are not consumed by the Surface in a timely manner). Or it may be configured to not drop excessive frames. In the latter mode if the Surface is not consuming output frames fast enough, it will eventually block the decoder. Prior to {@link android.os.Build.VERSION_CODES#Q} the exact behavior was undefined, with the exception that View surfaces (SuerfaceView or TextureView) always dropped excessive frames. Since {@link android.os.Build.VERSION_CODES#Q} the default behavior is to drop excessive frames. Applications can opt out of this behavior for non-View surfaces (such as ImageReader or SurfaceTexture) by targeting SDK {@link android.os.Build.VERSION_CODES#Q} and setting the key {@code "allow-frame-drop"} to {@code 0} in their configure format. <h4>Transformations When Rendering onto Surface</h4> If the codec is configured into Surface mode, any crop rectangle, {@linkplain android.media.MediaFormat#KEY_ROTATION rotation} and {@linkplain #setVideoScalingMode video scaling mode} will be automatically applied with one exception: <p class=note> Prior to the {@link android.os.Build.VERSION_CODES#M} release, software decoders may not have applied the rotation when being rendered onto a Surface. Unfortunately, there is no standard and simple way to identify software decoders, or if they apply the rotation other than by trying it out. <p> There are also some caveats. <p class=note> Note that the pixel aspect ratio is not considered when displaying the output onto the Surface. This means that if you are using {@link #VIDEO_SCALING_MODE_SCALE_TO_FIT} mode, you must position the output Surface so that it has the proper final display aspect ratio. Conversely, you can only use {@link #VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING} mode for content with square pixels (pixel aspect ratio or 1:1). <p class=note> Note also that as of {@link android.os.Build.VERSION_CODES#N} release, {@link #VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING} mode may not work correctly for videos rotated by 90 or 270 degrees. <p class=note> When setting the video scaling mode, note that it must be reset after each time the output buffers change. Since the {@link #INFO_OUTPUT_BUFFERS_CHANGED} event is deprecated, you can do this after each time the output format changes. <h4>Using an Input Surface</h4> <p> When using an input Surface, there are no accessible input buffers, as buffers are automatically passed from the input surface to the codec. Calling {@link #dequeueInputBuffer dequeueInputBuffer} will throw an {@code IllegalStateException}, and {@link #getInputBuffers} returns a bogus {@code ByteBuffer[]} array that <strong>MUST NOT</strong> be written into. <p> Call {@link #signalEndOfInputStream} to signal end-of-stream. The input surface will stop submitting data to the codec immediately after this call. <p> <h3>Seeking & Adaptive Playback Support</h3> <p> Video decoders (and in general codecs that consume compressed video data) behave differently regarding seek and format change whether or not they support and are configured for adaptive playback. You can check if a decoder supports {@linkplain android.media.MediaCodecInfo.CodecCapabilities#FEATURE_AdaptivePlayback adaptive playback} via {@link android.media.MediaCodecInfo.CodecCapabilities#isFeatureSupported android.media.MediaCodecInfo.CodecCapabilities.isFeatureSupported(String)}. Adaptive playback support for video decoders is only activated if you configure the codec to decode onto a {@link Surface}. <h4 id=KeyFrames><a name="KeyFrames"></a>Stream Boundary and Key Frames</h4> <p> It is important that the input data after {@link #start} or {@link #flush} starts at a suitable stream boundary: the first frame must a key frame. A <em>key frame</em> can be decoded completely on its own (for most codecs this means an I-frame), and no frames that are to be displayed after a key frame refer to frames before the key frame. <p> The following table summarizes suitable key frames for various video formats. <table> <thead> <tr> <th>Format</th> <th>Suitable key frame</th> </tr> </thead> <tbody class=mid> <tr> <td>VP9/VP8</td> <td>a suitable intraframe where no subsequent frames refer to frames prior to this frame.<br> <i>(There is no specific name for such key frame.)</i></td> </tr> <tr> <td>H.265 HEVC</td> <td>IDR or CRA</td> </tr> <tr> <td>H.264 AVC</td> <td>IDR</td> </tr> <tr> <td>MPEG-4<br>H.263<br>MPEG-2</td> <td>a suitable I-frame where no subsequent frames refer to frames prior to this frame.<br> <i>(There is no specific name for such key frame.)</td> </tr> </tbody> </table> <h4>For decoders that do not support adaptive playback (including when not decoding onto a Surface)</h4> <p> In order to start decoding data that is not adjacent to previously submitted data (i.e. after a seek) you <strong>MUST</strong> flush the decoder. Since all output buffers are immediately revoked at the point of the flush, you may want to first signal then wait for the end-of-stream before you call {@code flush}. It is important that the input data after a flush starts at a suitable stream boundary/key frame. <p class=note> <strong>Note:</strong> the format of the data submitted after a flush must not change; {@link #flush} does not support format discontinuities; for that, a full {@link #stop} - {@link #configure configure(…)} - {@link #start} cycle is necessary. <p class=note> <strong>Also note:</strong> if you flush the codec too soon after {@link #start} – generally, before the first output buffer or output format change is received – you will need to resubmit the codec-specific-data to the codec. See the <a href="#CSD">codec-specific-data section</a> for more info. <h4>For decoders that support and are configured for adaptive playback</h4> <p> In order to start decoding data that is not adjacent to previously submitted data (i.e. after a seek) it is <em>not necessary</em> to flush the decoder; however, input data after the discontinuity must start at a suitable stream boundary/key frame. <p> For some video formats - namely H.264, H.265, VP8 and VP9 - it is also possible to change the picture size or configuration mid-stream. To do this you must package the entire new codec-specific configuration data together with the key frame into a single buffer (including any start codes), and submit it as a <strong>regular</strong> input buffer. <p> You will receive an {@link #INFO_OUTPUT_FORMAT_CHANGED} return value from {@link #dequeueOutputBuffer dequeueOutputBuffer} or a {@link android.media.MediaCodec.Callback#onOutputBufferAvailable onOutputFormatChanged} callback just after the picture-size change takes place and before any frames with the new size have been returned. <p class=note> <strong>Note:</strong> just as the case for codec-specific data, be careful when calling {@link #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. <h3>Error handling</h3> <p> The factory methods {@link #createByCodecName createByCodecName} and {@link #createDecoderByType createDecoder}/{@link #createEncoderByType EncoderByType} throw {@code IOException} on failure which you must catch or declare to pass up. MediaCodec methods throw {@code 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 {@link android.media.MediaCodec.CryptoException}, which has further error information obtainable from {@link android.media.MediaCodec.CryptoException#getErrorCode}. <p> Internal codec errors result in a {@link android.media.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 {@code CodecException} can be determined by calling {@link android.media.MediaCodec.CodecException#isRecoverable} and {@link android.media.MediaCodec.CodecException#isTransient}: <ul> <li><strong>recoverable errors:</strong> If {@code isRecoverable()} returns true, then call {@link #stop}, {@link #configure configure(…)}, and {@link #start} to recover.</li> <li><strong>transient errors:</strong> If {@code isTransient()} returns true, then resources are temporarily unavailable and the method may be retried at a later time.</li> <li><strong>fatal errors:</strong> If both {@code isRecoverable()} and {@code isTransient()} return false, then the {@code CodecException} is fatal and the codec must be {@linkplain #reset reset} or {@linkplain #release released}.</li> </ul> <p> Both {@code isRecoverable()} and {@code isTransient()} do not return true at the same time. <h2 id=History><a name="History"></a>Valid API Calls and API History</h2> <p> This sections summarizes the valid API calls in each state and the API history of the MediaCodec class. For API version numbers, see {@link android.os.Build.VERSION_CODES}. <style> .api > tr > th, .api > tr > td { text-align: center; padding: 4px 4px; } .api > tr > th { vertical-align: bottom; } .api > tr > td { vertical-align: middle; } .sml > tr > th, .sml > tr > td { text-align: center; padding: 2px 4px; } .fn { text-align: left; } .fn > code > a { font: 14px/19px Roboto Condensed, sans-serif; } .deg45 { white-space: nowrap; background: none; border: none; vertical-align: bottom; width: 30px; height: 83px; } .deg45 > div { transform: skew(-45deg, 0deg) translate(1px, -67px); transform-origin: bottom left 0; width: 30px; height: 20px; } .deg45 > div > div { border: 1px solid #ddd; background: #999; height: 90px; width: 42px; } .deg45 > div > div > div { transform: skew(45deg, 0deg) translate(-55px, 55px) rotate(-45deg); } </style> <table align="right" style="width: 0%"> <thead> <tr><th>Symbol</th><th>Meaning</th></tr> </thead> <tbody class=sml> <tr><td>●</td><td>Supported</td></tr> <tr><td>⁕</td><td>Semantics changed</td></tr> <tr><td>○</td><td>Experimental support</td></tr> <tr><td>[ ]</td><td>Deprecated</td></tr> <tr><td>⎋</td><td>Restricted to surface input mode</td></tr> <tr><td>⎆</td><td>Restricted to surface output mode</td></tr> <tr><td>▧</td><td>Restricted to ByteBuffer input mode</td></tr> <tr><td>↩</td><td>Restricted to synchronous mode</td></tr> <tr><td>⇄</td><td>Restricted to asynchronous mode</td></tr> <tr><td>( )</td><td>Can be called, but shouldn't</td></tr> </tbody> </table> <table style="width: 100%;"> <thead class=api> <tr> <th class=deg45><div><div style="background:#4285f4"><div>Uninitialized</div></div></div></th> <th class=deg45><div><div style="background:#f4b400"><div>Configured</div></div></div></th> <th class=deg45><div><div style="background:#e67c73"><div>Flushed</div></div></div></th> <th class=deg45><div><div style="background:#0f9d58"><div>Running</div></div></div></th> <th class=deg45><div><div style="background:#f7cb4d"><div>End of Stream</div></div></div></th> <th class=deg45><div><div style="background:#db4437"><div>Error</div></div></div></th> <th class=deg45><div><div style="background:#666"><div>Released</div></div></div></th> <th></th> <th colspan="8">SDK Version</th> </tr> <tr> <th colspan="7">State</th> <th>Method</th> <th>16</th> <th>17</th> <th>18</th> <th>19</th> <th>20</th> <th>21</th> <th>22</th> <th>23</th> </tr> </thead> <tbody class=api> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td class=fn>{@link #createByCodecName createByCodecName}</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td class=fn>{@link #createDecoderByType createDecoderByType}</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td class=fn>{@link #createEncoderByType createEncoderByType}</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td class=fn>{@link #createPersistentInputSurface createPersistentInputSurface}</td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td>●</td> </tr> <tr> <td>16+</td> <td>-</td> <td>-</td> <td>-</td> <td>-</td> <td>-</td> <td>-</td> <td class=fn>{@link #configure configure}</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>⁕</td> <td>●</td> <td>●</td> </tr> <tr> <td>-</td> <td>18+</td> <td>-</td> <td>-</td> <td>-</td> <td>-</td> <td>-</td> <td class=fn>{@link #createInputSurface createInputSurface}</td> <td></td> <td></td> <td>⎋</td> <td>⎋</td> <td>⎋</td> <td>⎋</td> <td>⎋</td> <td>⎋</td> </tr> <tr> <td>-</td> <td>-</td> <td>16+</td> <td>16+</td> <td>(16+)</td> <td>-</td> <td>-</td> <td class=fn>{@link #dequeueInputBuffer dequeueInputBuffer}</td> <td>●</td> <td>●</td> <td>▧</td> <td>▧</td> <td>▧</td> <td>⁕▧↩</td> <td>▧↩</td> <td>▧↩</td> </tr> <tr> <td>-</td> <td>-</td> <td>16+</td> <td>16+</td> <td>16+</td> <td>-</td> <td>-</td> <td class=fn>{@link #dequeueOutputBuffer dequeueOutputBuffer}</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>⁕↩</td> <td>↩</td> <td>↩</td> </tr> <tr> <td>-</td> <td>-</td> <td>16+</td> <td>16+</td> <td>16+</td> <td>-</td> <td>-</td> <td class=fn>{@link #flush flush}</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> </tr> <tr> <td>18+</td> <td>18+</td> <td>18+</td> <td>18+</td> <td>18+</td> <td>18+</td> <td>-</td> <td class=fn>{@link #getCodecInfo getCodecInfo}</td> <td></td> <td></td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> </tr> <tr> <td>-</td> <td>-</td> <td>(21+)</td> <td>21+</td> <td>(21+)</td> <td>-</td> <td>-</td> <td class=fn>{@link #getInputBuffer getInputBuffer}</td> <td></td> <td></td> <td></td> <td></td> <td></td> <td>●</td> <td>●</td> <td>●</td> </tr> <tr> <td>-</td> <td>-</td> <td>16+</td> <td>(16+)</td> <td>(16+)</td> <td>-</td> <td>-</td> <td class=fn>{@link #getInputBuffers getInputBuffers}</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>[⁕↩]</td> <td>[↩]</td> <td>[↩]</td> </tr> <tr> <td>-</td> <td>21+</td> <td>(21+)</td> <td>(21+)</td> <td>(21+)</td> <td>-</td> <td>-</td> <td class=fn>{@link #getInputFormat getInputFormat}</td> <td></td> <td></td> <td></td> <td></td> <td></td> <td>●</td> <td>●</td> <td>●</td> </tr> <tr> <td>-</td> <td>-</td> <td>(21+)</td> <td>21+</td> <td>(21+)</td> <td>-</td> <td>-</td> <td class=fn>{@link #getInputImage getInputImage}</td> <td></td> <td></td> <td></td> <td></td> <td></td> <td>○</td> <td>●</td> <td>●</td> </tr> <tr> <td>18+</td> <td>18+</td> <td>18+</td> <td>18+</td> <td>18+</td> <td>18+</td> <td>-</td> <td class=fn>{@link #getName getName}</td> <td></td> <td></td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> </tr> <tr> <td>-</td> <td>-</td> <td>(21+)</td> <td>21+</td> <td>21+</td> <td>-</td> <td>-</td> <td class=fn>{@link #getOutputBuffer getOutputBuffer}</td> <td></td> <td></td> <td></td> <td></td> <td></td> <td>●</td> <td>●</td> <td>●</td> </tr> <tr> <td>-</td> <td>-</td> <td>16+</td> <td>16+</td> <td>16+</td> <td>-</td> <td>-</td> <td class=fn>{@link #getOutputBuffers getOutputBuffers}</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>[⁕↩]</td> <td>[↩]</td> <td>[↩]</td> </tr> <tr> <td>-</td> <td>21+</td> <td>16+</td> <td>16+</td> <td>16+</td> <td>-</td> <td>-</td> <td class=fn>{@link #getOutputFormat}()</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> </tr> <tr> <td>-</td> <td>-</td> <td>(21+)</td> <td>21+</td> <td>21+</td> <td>-</td> <td>-</td> <td class=fn>{@link #getOutputFormat}(int)</td> <td></td> <td></td> <td></td> <td></td> <td></td> <td>●</td> <td>●</td> <td>●</td> </tr> <tr> <td>-</td> <td>-</td> <td>(21+)</td> <td>21+</td> <td>21+</td> <td>-</td> <td>-</td> <td class=fn>{@link #getOutputImage getOutputImage}</td> <td></td> <td></td> <td></td> <td></td> <td></td> <td>○</td> <td>●</td> <td>●</td> </tr> <tr> <td>-</td> <td>-</td> <td>-</td> <td>16+</td> <td>(16+)</td> <td>-</td> <td>-</td> <td class=fn>{@link #queueInputBuffer queueInputBuffer}</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>⁕</td> <td>●</td> <td>●</td> </tr> <tr> <td>-</td> <td>-</td> <td>-</td> <td>16+</td> <td>(16+)</td> <td>-</td> <td>-</td> <td class=fn>{@link #queueSecureInputBuffer queueSecureInputBuffer}</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>⁕</td> <td>●</td> <td>●</td> </tr> <tr> <td>16+</td> <td>16+</td> <td>16+</td> <td>16+</td> <td>16+</td> <td>16+</td> <td>16+</td> <td class=fn>{@link #release release}</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> </tr> <tr> <td>-</td> <td>-</td> <td>-</td> <td>16+</td> <td>16+</td> <td>-</td> <td>-</td> <td class=fn>{@link #releaseOutputBuffer(int, boolean)}</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>⁕</td> <td>●</td> <td>⁕</td> </tr> <tr> <td>-</td> <td>-</td> <td>-</td> <td>21+</td> <td>21+</td> <td>-</td> <td>-</td> <td class=fn>{@link #releaseOutputBuffer(int, long)}</td> <td></td> <td></td> <td></td> <td></td> <td></td> <td>⎆</td> <td>⎆</td> <td>⎆</td> </tr> <tr> <td>21+</td> <td>21+</td> <td>21+</td> <td>21+</td> <td>21+</td> <td>21+</td> <td>-</td> <td class=fn>{@link #reset reset}</td> <td></td> <td></td> <td></td> <td></td> <td></td> <td>●</td> <td>●</td> <td>●</td> </tr> <tr> <td>21+</td> <td>-</td> <td>-</td> <td>-</td> <td>-</td> <td>-</td> <td>-</td> <td class=fn>{@link #setCallback(Callback) setCallback}</td> <td></td> <td></td> <td></td> <td></td> <td></td> <td>●</td> <td>●</td> <td>{@link #setCallback(Callback, Handler) ⁕}</td> </tr> <tr> <td>-</td> <td>23+</td> <td>-</td> <td>-</td> <td>-</td> <td>-</td> <td>-</td> <td class=fn>{@link #setInputSurface setInputSurface}</td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td>⎋</td> </tr> <tr> <td>23+</td> <td>23+</td> <td>23+</td> <td>23+</td> <td>23+</td> <td>(23+)</td> <td>(23+)</td> <td class=fn>{@link #setOnFrameRenderedListener setOnFrameRenderedListener}</td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td>○ ⎆</td> </tr> <tr> <td>-</td> <td>23+</td> <td>23+</td> <td>23+</td> <td>23+</td> <td>-</td> <td>-</td> <td class=fn>{@link #setOutputSurface setOutputSurface}</td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td>⎆</td> </tr> <tr> <td>19+</td> <td>19+</td> <td>19+</td> <td>19+</td> <td>19+</td> <td>(19+)</td> <td>-</td> <td class=fn>{@link #setParameters setParameters}</td> <td></td> <td></td> <td></td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> </tr> <tr> <td>-</td> <td>(16+)</td> <td>(16+)</td> <td>16+</td> <td>(16+)</td> <td>(16+)</td> <td>-</td> <td class=fn>{@link #setVideoScalingMode setVideoScalingMode}</td> <td>⎆</td> <td>⎆</td> <td>⎆</td> <td>⎆</td> <td>⎆</td> <td>⎆</td> <td>⎆</td> <td>⎆</td> </tr> <tr> <td>(29+)</td> <td>29+</td> <td>29+</td> <td>29+</td> <td>(29+)</td> <td>(29+)</td> <td>-</td> <td class=fn>{@link #setAudioPresentation setAudioPresentation}</td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td>-</td> <td>-</td> <td>18+</td> <td>18+</td> <td>-</td> <td>-</td> <td>-</td> <td class=fn>{@link #signalEndOfInputStream signalEndOfInputStream}</td> <td></td> <td></td> <td>⎋</td> <td>⎋</td> <td>⎋</td> <td>⎋</td> <td>⎋</td> <td>⎋</td> </tr> <tr> <td>-</td> <td>16+</td> <td>21+(⇄)</td> <td>-</td> <td>-</td> <td>-</td> <td>-</td> <td class=fn>{@link #start start}</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>⁕</td> <td>●</td> <td>●</td> </tr> <tr> <td>-</td> <td>-</td> <td>16+</td> <td>16+</td> <td>16+</td> <td>-</td> <td>-</td> <td class=fn>{@link #stop stop}</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> <td>●</td> </tr> </tbody> </table> */ var MediaCodec = { /** This indicates that the (encoded) buffer marked as such contains the data for a key frame. @deprecated Use {@link #BUFFER_FLAG_KEY_FRAME} instead. */ BUFFER_FLAG_SYNC_FRAME : "1", /** This indicates that the (encoded) buffer marked as such contains the data for a key frame. */ BUFFER_FLAG_KEY_FRAME : "1", /** This indicated that the buffer marked as such contains codec initialization / codec specific data instead of media data. */ BUFFER_FLAG_CODEC_CONFIG : "2", /** This signals the end of stream, i.e. no buffers will be available after this, unless of course, {@link #flush} follows. */ BUFFER_FLAG_END_OF_STREAM : "4", /** This indicates that the buffer only contains part of a frame, and the decoder should batch the data until a buffer without this flag appears before decoding the frame. */ BUFFER_FLAG_PARTIAL_FRAME : "8", /** This indicates that the buffer contains non-media data for the muxer to process. All muxer data should start with a FOURCC header that determines the type of data. For example, when it contains Exif data sent to a MediaMuxer track of {@link android.media.MediaFormat#MIMETYPE_IMAGE_ANDROID_HEIC} type, the data must start with Exif header ("Exif\0\0"), followed by the TIFF header (See JEITA CP-3451C Section 4.5.2.) @hide */ BUFFER_FLAG_MUXER_DATA : "16", /** If this codec is to be used as an encoder, pass this flag. */ CONFIGURE_FLAG_ENCODE : "1", /***/ CRYPTO_MODE_UNENCRYPTED : "0", /***/ CRYPTO_MODE_AES_CTR : "1", /***/ CRYPTO_MODE_AES_CBC : "2", /** If a non-negative timeout had been specified in the call to {@link #dequeueOutputBuffer}, indicates that the call timed out. */ INFO_TRY_AGAIN_LATER : "-1", /** The output format has changed, subsequent data will follow the new format. {@link #getOutputFormat}() returns the new format. Note, that you can also use the new {@link #getOutputFormat}(int) method to get the format for a specific output buffer. This frees you from having to track output format changes. */ INFO_OUTPUT_FORMAT_CHANGED : "-2", /** The output buffers have changed, the client must refer to the new set of output buffers returned by {@link #getOutputBuffers} from this point on. <p>Additionally, this event signals that the video scaling mode may have been reset to the default.</p> @deprecated This return value can be ignored as {@link #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. */ INFO_OUTPUT_BUFFERS_CHANGED : "-3", /** The content is scaled to the surface dimensions */ VIDEO_SCALING_MODE_SCALE_TO_FIT : "1", /** The content is scaled, maintaining its aspect ratio, the whole surface area is used, content may be cropped. <p class=note> This mode is only suitable for content with 1:1 pixel aspect ratio as you cannot configure the pixel aspect ratio for a {@link Surface}. <p class=note> As of {@link android.os.Build.VERSION_CODES#N} release, this mode may not work if the video is {@linkplain android.media.MediaFormat#KEY_ROTATION rotated} by 90 or 270 degrees. */ VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING : "2", /** Change a video encoder's target bitrate on the fly. The value is an Integer object containing the new bitrate in bps. @see #setParameters(Bundle) */ PARAMETER_KEY_VIDEO_BITRATE : "video-bitrate", /** 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. @see #setParameters(Bundle) */ PARAMETER_KEY_SUSPEND : "drop-input-frames", /** When {@link #PARAMETER_KEY_SUSPEND} is present, the client can also optionally use this key to specify the timestamp (in micro-second) at which the suspend/resume operation takes effect. Note that the specified timestamp must be greater than or equal to the timestamp of any previously queued suspend/resume operations. The value is a long int, indicating the timestamp to suspend/resume. @see #setParameters(Bundle) */ PARAMETER_KEY_SUSPEND_TIME : "drop-start-time-us", /** Specify an offset (in micro-second) to be added on top of the timestamps onward. A typical use case is to apply an adjust to the timestamps after a period of pause by the user. This parameter can only be used on an encoder in "surface-input" mode. The value is a long int, indicating the timestamp offset to be applied. @see #setParameters(Bundle) */ PARAMETER_KEY_OFFSET_TIME : "time-offset-us", /** Request that the encoder produce a sync frame "soon". Provide an Integer with the value 0. @see #setParameters(Bundle) */ PARAMETER_KEY_REQUEST_SYNC_FRAME : "request-sync", /** Set the HDR10+ metadata on the next queued input frame. Provide a byte array of data that's conforming to the user_data_registered_itu_t_t35() syntax of SEI message for ST 2094-40. <p> For decoders: <p> When a decoder is configured for one of the HDR10+ profiles that uses out-of-band metadata (such as {@link android.media.MediaCodecInfo.CodecProfileLevel#VP9Profile2HDR10Plus} or {@link android.media.MediaCodecInfo.CodecProfileLevel#VP9Profile3HDR10Plus}), this parameter sets the HDR10+ metadata on the next input buffer queued to the decoder. A decoder supporting these profiles must propagate the metadata to the format of the output buffer corresponding to this particular input buffer (under key {@link android.media.MediaFormat#KEY_HDR10_PLUS_INFO}). The metadata should be applied to that output buffer and the buffers following it (in display order), until the next output buffer (in display order) upon which an HDR10+ metadata is set. <p> This parameter shouldn't be set if the decoder is not configured for an HDR10+ profile that uses out-of-band metadata. In particular, it shouldn't be set for HDR10+ profiles that uses in-band metadata where the metadata is embedded in the input buffers, for example {@link android.media.MediaCodecInfo.CodecProfileLevel#HEVCProfileMain10HDR10Plus}. <p> For encoders: <p> When an encoder is configured for one of the HDR10+ profiles and the operates in byte buffer input mode (instead of surface input mode), this parameter sets the HDR10+ metadata on the next input buffer queued to the encoder. For the HDR10+ profiles that uses out-of-band metadata (such as {@link android.media.MediaCodecInfo.CodecProfileLevel#VP9Profile2HDR10Plus}, or {@link android.media.MediaCodecInfo.CodecProfileLevel#VP9Profile3HDR10Plus}), the metadata must be propagated to the format of the output buffer corresponding to this particular input buffer (under key {@link android.media.MediaFormat#KEY_HDR10_PLUS_INFO}). For the HDR10+ profiles that uses in-band metadata (such as {@link android.media.MediaCodecInfo.CodecProfileLevel#HEVCProfileMain10HDR10Plus}), the metadata info must be embedded in the corresponding output buffer itself. <p> This parameter shouldn't be set if the encoder is not configured for an HDR10+ profile, or if it's operating in surface input mode. <p> @see MediaFormat#KEY_HDR10_PLUS_INFO */ PARAMETER_KEY_HDR10_PLUS_INFO : "hdr10-plus-info", /**Instantiate the preferred decoder supporting input data of the given mime type. The following is a partial list of defined mime types and their semantics: <ul> <li>"video/x-vnd.on2.vp8" - VP8 video (i.e. video in .webm) <li>"video/x-vnd.on2.vp9" - VP9 video (i.e. video in .webm) <li>"video/avc" - H.264/AVC video <li>"video/hevc" - H.265/HEVC video <li>"video/mp4v-es" - MPEG4 video <li>"video/3gpp" - H.263 video <li>"audio/3gpp" - AMR narrowband audio <li>"audio/amr-wb" - AMR wideband audio <li>"audio/mpeg" - MPEG1/2 audio layer III <li>"audio/mp4a-latm" - AAC audio (note, this is raw AAC packets, not packaged in LATM!) <li>"audio/vorbis" - vorbis audio <li>"audio/g711-alaw" - G.711 alaw audio <li>"audio/g711-mlaw" - G.711 ulaw audio </ul> <strong>Note:</strong> It is preferred to use {@link android.media.MediaCodecList#findDecoderForFormat} and {@link #createByCodecName} to ensure that the resulting codec can handle a given format. @param {String} type The mime type of the input data. @throws IOException if the codec cannot be created. @throws IllegalArgumentException if type is not a valid mime type. @throws NullPointerException if type is null. */ createDecoderByType : function( ) {}, /**Instantiate the preferred encoder supporting output data of the given mime type. <strong>Note:</strong> It is preferred to use {@link android.media.MediaCodecList#findEncoderForFormat} and {@link #createByCodecName} to ensure that the resulting codec can handle a given format. @param {String} type The desired mime type of the output data. @throws IOException if the codec cannot be created. @throws IllegalArgumentException if type is not a valid mime type. @throws NullPointerException if type is null. */ createEncoderByType : function( ) {}, /**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 {@link android.media.MediaCodecList} @param {String} name The name of the codec to be instantiated. @throws IOException if the codec cannot be created. @throws IllegalArgumentException if name is not valid. @throws NullPointerException if name is null. */ createByCodecName : function( ) {}, /**Returns the codec to its initial (Uninitialized) state. Call this if an {@link android.media.MediaCodec.CodecException#isRecoverable unrecoverable} error has occured to reset the codec to its initial state after creation. @throws CodecException if an unrecoverable error has occured and the codec could not be reset. @throws IllegalStateException if in the Released state. */ reset : function( ) {}, /**Free up resources used by the codec instance. 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. */ release : function( ) {}, /**Configures a component. @param {Object {MediaFormat}} format The format of the input data (decoder) or the desired format of the output data (encoder). Passing {@code null} as {@code format} is equivalent to passing an {@link MediaFormat#MediaFormat an empty mediaformat}. @param {Object {Surface}} surface Specify a surface on which to render the output of this decoder. Pass {@code null} as {@code surface} if the codec does not generate raw video output (e.g. not a video decoder) and/or if you want to configure the codec for {@link ByteBuffer} output. @param {Object {MediaCrypto}} crypto Specify a crypto object to facilitate secure decryption of the media data. Pass {@code null} as {@code crypto} for non-secure codecs. Please note that {@link MediaCodec} does NOT take ownership of the {@link MediaCrypto} object; it is the application's responsibility to properly cleanup the {@link MediaCrypto} object when not in use. @param {Number} flags Specify {@link #CONFIGURE_FLAG_ENCODE} to configure the component as an encoder. @throws 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 {@link #CONFIGURE_FLAG_ENCODE} for an encoder). @throws IllegalStateException if not in the Uninitialized state. @throws CryptoException upon DRM error. @throws CodecException upon codec error. */ configure : function( ) {}, /**Configure a component to be used with a descrambler. @param {Object {MediaFormat}} format The format of the input data (decoder) or the desired format of the output data (encoder). Passing {@code null} as {@code format} is equivalent to passing an {@link MediaFormat#MediaFormat an empty mediaformat}. @param {Object {Surface}} surface Specify a surface on which to render the output of this decoder. Pass {@code null} as {@code surface} if the codec does not generate raw video output (e.g. not a video decoder) and/or if you want to configure the codec for {@link ByteBuffer} output. @param {Number} flags Specify {@link #CONFIGURE_FLAG_ENCODE} to configure the component as an encoder. @param {Object {MediaDescrambler}} descrambler Specify a descrambler object to facilitate secure descrambling of the media data, or null for non-secure codecs. @throws 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 {@link #CONFIGURE_FLAG_ENCODE} for an encoder). @throws IllegalStateException if not in the Uninitialized state. @throws CryptoException upon DRM error. @throws CodecException upon codec error. */ configure : function( ) {}, /**Dynamically sets the output surface of a codec. <p> This can only be used if the codec was configured with an output surface. The new output surface should have a compatible usage type to the original output surface. E.g. codecs may not support switching from a SurfaceTexture (GPU readable) output to ImageReader (software readable) output. @param {Object {Surface}} surface the output surface to use. It must not be {@code null}. @throws IllegalStateException if the codec does not support setting the output surface in the current state. @throws IllegalArgumentException if the new surface is not of a suitable type for the codec. */ setOutputSurface : function( ) {}, /**Create a persistent input surface that can be used with codecs that normally have an input surface, such as video encoders. A persistent input can be reused by subsequent {@link android.media.MediaCodec} or {@link android.media.MediaRecorder} instances, but can only be used by at most one codec or recorder instance concurrently. <p> The application is responsible for calling release() on the Surface when done. @return {Object {android.view.Surface}} an input surface that can be used with {@link #setInputSurface}. */ createPersistentInputSurface : function( ) {}, /**Configures the codec (e.g. encoder) to use a persistent input surface in place of input buffers. This may only be called after {@link #configure} and before {@link #start}, in lieu of {@link #createInputSurface}. @param {Object {Surface}} surface a persistent input surface created by {@link #createPersistentInputSurface} @throws IllegalStateException if not in the Configured state or does not require an input surface. @throws IllegalArgumentException if the surface was not created by {@link #createPersistentInputSurface}. */ setInputSurface : function( ) {}, /**Requests a Surface to use as the input to an encoder, in place of input buffers. This may only be called after {@link #configure} and before {@link #start}. <p> The application is responsible for calling release() on the Surface when done. <p> The Surface must be rendered with a hardware-accelerated API, such as OpenGL ES. {@link android.view.Surface#lockCanvas(android.graphics.Rect)} may fail or produce unexpected results. @throws IllegalStateException if not in the Configured state. */ createInputSurface : function( ) {}, /**After successfully configuring the component, call {@code start}. <p> Call {@code start} also if the codec is configured in asynchronous mode, and it has just been flushed, to resume requesting input buffers. @throws IllegalStateException if not in the Configured state or just after {@link #flush} for a codec that is configured in asynchronous mode. @throws MediaCodec.CodecException upon codec error. Note that some codec errors for start may be attributed to future method calls. */ start : function( ) {}, /**Finish the decode/encode session, note that the codec instance remains active and ready to be {@link #start}ed again. To ensure that it is available to other client call {@link #release} and don't just rely on garbage collection to eventually do this for you. @throws IllegalStateException if in the Released state. */ stop : function( ) {}, /**Flush both input and output ports of the component. <p> Upon return, all indices previously returned in calls to {@link #dequeueInputBuffer dequeueInputBuffer} and {@link #dequeueOutputBuffer dequeueOutputBuffer} — or obtained via {@link android.media.MediaCodec.Callback#onInputBufferAvailable onInputBufferAvailable} or {@link android.media.MediaCodec.Callback#onOutputBufferAvailable onOutputBufferAvailable} callbacks — become invalid, and all buffers are owned by the codec. <p> If the codec is configured in asynchronous mode, call {@link #start} after {@code flush} has returned to resume codec operations. The codec will not request input buffers until this has happened. <strong>Note, however, that there may still be outstanding {@code onOutputBufferAvailable} callbacks that were not handled prior to calling {@code flush}. The indices returned via these callbacks also become invalid upon calling {@code flush} and should be discarded.</strong> <p> If the codec is configured in synchronous mode, codec will resume automatically if it is configured with an input surface. Otherwise, it will resume when {@link #dequeueInputBuffer dequeueInputBuffer} is called. @throws IllegalStateException if not in the Executing state. @throws MediaCodec.CodecException upon codec error. */ flush : function( ) {}, /**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 {@link #getInputBuffer} in response to a {@link #dequeueInputBuffer} return value or a {@link android.media.MediaCodec.Callback#onInputBufferAvailable} callback. <p> 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 {@link android.media.MediaExtractor} provides codec specific data as part of the returned track format in entries named "csd-0", "csd-1" ... <p> These buffers can be submitted directly after {@link #start} or {@link #flush} by specifying the flag {@link #BUFFER_FLAG_CODEC_CONFIG}. However, if you configure the codec with a {@link android.media.MediaFormat} containing these keys, they will be automatically submitted by MediaCodec directly after start. Therefore, the use of {@link #BUFFER_FLAG_CODEC_CONFIG} flag is discouraged and is recommended only for advanced users. <p> 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 {@link #BUFFER_FLAG_END_OF_STREAM}. <p class=note> <strong>Note:</strong> Prior to {@link android.os.Build.VERSION_CODES#M}, {@code presentationTimeUs} was not propagated to the frame timestamp of (rendered) Surface output buffers, and the resulting frame timestamp was undefined. Use {@link #releaseOutputBuffer(int, long)} to ensure a specific frame timestamp is set. Similarly, since frame timestamps can be used by the destination surface for rendering synchronization, <strong>care must be taken to normalize presentationTimeUs so as to not be mistaken for a system time. (See {@linkplain #releaseOutputBuffer(int, long) SurfaceView specifics}).</strong> @param {Number} index The index of a client-owned input buffer previously returned in a call to {@link #dequeueInputBuffer}. @param {Number} offset The byte offset into the input buffer at which the data starts. @param {Number} size The number of bytes of valid input data. @param {Number} presentationTimeUs The presentation timestamp in microseconds for this buffer. This is normally the media time at which this buffer should be presented (rendered). When using an output surface, this will be propagated as the {@link SurfaceTexture#getTimestamp timestamp} for the frame (after conversion to nanoseconds). @param {Number} flags A bitmask of flags {@link #BUFFER_FLAG_CODEC_CONFIG} and {@link #BUFFER_FLAG_END_OF_STREAM}. While not prohibited, most codecs do not use the {@link #BUFFER_FLAG_KEY_FRAME} flag for input buffers. @throws IllegalStateException if not in the Executing state. @throws MediaCodec.CodecException upon codec error. @throws CryptoException if a crypto object has been specified in {@link #configure} */ queueInputBuffer : function( ) {}, /**Similar to {@link #queueInputBuffer queueInputBuffer} but submits a buffer that is potentially encrypted. <strong>Check out further notes at {@link #queueInputBuffer queueInputBuffer}.</strong> @param {Number} index The index of a client-owned input buffer previously returned in a call to {@link #dequeueInputBuffer}. @param {Number} offset The byte offset into the input buffer at which the data starts. @param {Object {MediaCodec.CryptoInfo}} info Metadata required to facilitate decryption, the object can be reused immediately after this call returns. @param {Number} presentationTimeUs The presentation timestamp in microseconds for this buffer. This is normally the media time at which this buffer should be presented (rendered). @param {Number} flags A bitmask of flags {@link #BUFFER_FLAG_CODEC_CONFIG} and {@link #BUFFER_FLAG_END_OF_STREAM}. While not prohibited, most codecs do not use the {@link #BUFFER_FLAG_KEY_FRAME} flag for input buffers. @throws IllegalStateException if not in the Executing state. @throws MediaCodec.CodecException upon codec error. @throws 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. */ queueSecureInputBuffer : function( ) {}, /**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. @param {Number} timeoutUs The timeout in microseconds, a negative timeout indicates "infinite". @throws IllegalStateException if not in the Executing state, or codec is configured in asynchronous mode. @throws MediaCodec.CodecException upon codec error. */ dequeueInputBuffer : function( ) {}, /**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. @param {Object {MediaCodec.BufferInfo}} info Will be filled with buffer meta data. @param {Number} timeoutUs The timeout in microseconds, a negative timeout indicates "infinite". @throws IllegalStateException if not in the Executing state, or codec is configured in asynchronous mode. @throws MediaCodec.CodecException upon codec error. */ dequeueOutputBuffer : function( ) {}, /**If you are done with a buffer, use this call to return the buffer to the codec or to render it on the output surface. If you configured the codec with an output surface, setting {@code render} to {@code true} will first send the buffer to that output surface. The surface will release the buffer back to the codec once it is no longer used/displayed. Once an output buffer is released to the codec, it MUST NOT be used until it is later retrieved by {@link #getOutputBuffer} in response to a {@link #dequeueOutputBuffer} return value or a {@link android.media.MediaCodec.Callback#onOutputBufferAvailable} callback. @param {Number} index The index of a client-owned output buffer previously returned from a call to {@link #dequeueOutputBuffer}. @param {Boolean} render If a valid surface was specified when configuring the codec, passing true renders this output buffer to the surface. @throws IllegalStateException if not in the Executing state. @throws MediaCodec.CodecException upon codec error. */ releaseOutputBuffer : function( ) {}, /**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.<p> The timestamp may have special meaning depending on the destination surface. <table> <tr><th>SurfaceView specifics</th></tr> <tr><td> If you render your buffer on a {@link android.view.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 <i>reasonably close</i> to the current {@link System#nanoTime}. Currently, this is set as within one (1) second. A few notes: <ul> <li>the buffer will not be returned to the codec until the timestamp has passed and the buffer is no longer used by the {@link android.view.Surface}. <li>buffers are processed sequentially, so you may block subsequent buffers to be displayed on the {@link android.view.Surface}. This is important if you want to react to user action, e.g. stop the video or seek. <li>if multiple buffers are sent to the {@link android.view.Surface} to be rendered at the same VSYNC, the last one will be shown, and the other ones will be dropped. <li>if the timestamp is <em>not</em> "reasonably close" to the current system time, the {@link android.view.Surface} will ignore the timestamp, and display the buffer at the earliest feasible time. In this mode it will not drop frames. <li>for best performance and quality, call this method when you are about two VSYNCs' time before the desired render time. For 60Hz displays, this is about 33 msec. </ul> </td></tr> </table> Once an output buffer is released to the codec, it MUST NOT be used until it is later retrieved by {@link #getOutputBuffer} in response to a {@link #dequeueOutputBuffer} return value or a {@link android.media.MediaCodec.Callback#onOutputBufferAvailable} callback. @param {Number} index The index of a client-owned output buffer previously returned from a call to {@link #dequeueOutputBuffer}. @param {Number} renderTimestampNs The timestamp to associate with this buffer when it is sent to the Surface. @throws IllegalStateException if not in the Executing state. @throws MediaCodec.CodecException upon codec error. */ releaseOutputBuffer : function( ) {}, /**Signals end-of-stream on input. Equivalent to submitting an empty buffer with {@link #BUFFER_FLAG_END_OF_STREAM} set. This may only be used with encoders receiving input from a Surface created by {@link #createInputSurface}. @throws IllegalStateException if not in the Executing state. @throws MediaCodec.CodecException upon codec error. */ signalEndOfInputStream : function( ) {}, /**Call this after dequeueOutputBuffer signals a format change by returning {@link #INFO_OUTPUT_FORMAT_CHANGED}. You can also call this after {@link #configure} 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. @throws IllegalStateException if not in the Executing or Configured state. @throws MediaCodec.CodecException upon codec error. */ getOutputFormat : function( ) {}, /**Call this after {@link #configure} returns successfully to get the input format accepted by the codec. Do this to determine what optional configuration parameters were supported by the codec. @throws IllegalStateException if not in the Executing or Configured state. @throws MediaCodec.CodecException upon codec error. */ getInputFormat : function( ) {}, /**Returns the output format for a specific output buffer. @param {Number} index The index of a client-owned input buffer previously returned from a call to {@link #dequeueInputBuffer}. @return {Object {android.media.MediaFormat}} the format for the output buffer, or null if the index is not a dequeued output buffer. */ getOutputFormat : function( ) {}, /**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. @deprecated Use the new {@link #getInputBuffer} method instead each time an input buffer is dequeued. <b>Note:</b> As of API 21, dequeued input buffers are automatically {@link java.nio.Buffer#clear cleared}. <em>Do not use this method if using an input surface.</em> @throws IllegalStateException if not in the Executing state, or codec is configured in asynchronous mode. @throws MediaCodec.CodecException upon codec error. */ getInputBuffers : function( ) {}, /**Retrieve the set of output buffers. Call this after start() returns and whenever dequeueOutputBuffer signals an output buffer change by returning {@link #INFO_OUTPUT_BUFFERS_CHANGED}. After calling this method, any ByteBuffers previously returned by an earlier call to this method MUST no longer be used. @deprecated Use the new {@link #getOutputBuffer} method instead each time an output buffer is dequeued. This method is not supported if codec is configured in asynchronous mode. <b>Note:</b> As of API 21, the position and limit of output buffers that are dequeued will be set to the valid data range. <em>Do not use this method if using an output surface.</em> @throws IllegalStateException if not in the Executing state, or codec is configured in asynchronous mode. @throws MediaCodec.CodecException upon codec error. */ getOutputBuffers : function( ) {}, /**Returns a {@link java.nio.Buffer#clear 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. @param {Number} index The index of a client-owned input buffer previously returned from a call to {@link #dequeueInputBuffer}, or received via an onInputBufferAvailable callback. @return {Object {java.nio.ByteBuffer}} the input buffer, or null if the index is not a dequeued input buffer, or if the codec is configured for surface input. @throws IllegalStateException if not in the Executing state. @throws MediaCodec.CodecException upon codec error. */ getInputBuffer : function( ) {}, /**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. @param {Number} index The index of a client-owned input buffer previously returned from a call to {@link #dequeueInputBuffer}, or received via an onInputBufferAvailable callback. @return {Object {android.media.Image}} the input image, or null if the index is not a dequeued input buffer, or not a ByteBuffer that contains a raw image. @throws IllegalStateException if not in the Executing state. @throws MediaCodec.CodecException upon codec error. */ getInputImage : function( ) {}, /**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. @param {Number} index The index of a client-owned output buffer previously returned from a call to {@link #dequeueOutputBuffer}, or received via an onOutputBufferAvailable callback. @return {Object {java.nio.ByteBuffer}} the output buffer, or null if the index is not a dequeued output buffer, or the codec is configured with an output surface. @throws IllegalStateException if not in the Executing state. @throws MediaCodec.CodecException upon codec error. */ getOutputBuffer : function( ) {}, /**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. @param {Number} index The index of a client-owned output buffer previously returned from a call to {@link #dequeueOutputBuffer}, or received via an onOutputBufferAvailable callback. @return {Object {android.media.Image}} the output image, or null if the index is not a dequeued output buffer, not a raw video frame, or if the codec was configured with an output surface. @throws IllegalStateException if not in the Executing state. @throws MediaCodec.CodecException upon codec error. */ getOutputImage : function( ) {}, /**If a surface has been specified in a previous call to {@link #configure} specifies the scaling mode to use. The default is "scale to fit". <p class=note> The scaling mode may be reset to the <strong>default</strong> each time an {@link #INFO_OUTPUT_BUFFERS_CHANGED} event is received from the codec; therefore, the client must call this method after every buffer change event (and before the first output buffer is released for rendering) to ensure consistent scaling mode. <p class=note> Since the {@link #INFO_OUTPUT_BUFFERS_CHANGED} event is deprecated, this can also be done after each {@link #INFO_OUTPUT_FORMAT_CHANGED} event. @throws IllegalArgumentException if mode is not recognized. @throws IllegalStateException if in the Released state. */ setVideoScalingMode : function( ) {}, /**Sets the audio presentation. @param {Object {AudioPresentation}} presentation see {@link AudioPresentation}. In particular, id should be set. */ setAudioPresentation : function( ) {}, /**Retrieve the codec name. If the codec was created by createDecoderByType or createEncoderByType, what component is chosen is not known beforehand. This method returns the name of the codec that was selected by the platform. <strong>Note:</strong> Implementations may provide multiple aliases (codec names) for the same underlying codec, any of which can be used to instantiate the same underlying codec in {@link android.media.MediaCodec#createByCodecName}. This method returns the name used to create the codec in this case. @throws IllegalStateException if in the Released state. */ getName : function( ) {}, /**Retrieve the underlying codec name. This method is similar to {@link #getName}, except that it returns the underlying component name even if an alias was used to create this MediaCodec object by name, @throws IllegalStateException if in the Released state. */ getCanonicalName : function( ) {}, /**Return Metrics data about the current codec instance. @return {Object {android.os.PersistableBundle}} a {@link PersistableBundle} containing the set of attributes and values available for the media being handled by this instance of MediaCodec The attributes are descibed in {@link MetricsConstants}. Additional vendor-specific fields may also be present in the return value. */ getMetrics : function( ) {}, /**Communicate additional parameter changes to the component instance. <b>Note:</b> Some of these parameter changes may silently fail to apply. @param {Object {Bundle}} params The bundle of parameters to set. @throws IllegalStateException if in the Released state. */ setParameters : function( ) {}, /**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 {@link #configure} is called. When asynchronous callback is enabled, the client should not call {@link #getInputBuffers}, {@link #getOutputBuffers}, {@link #dequeueInputBuffer}(long) or {@link #dequeueOutputBuffer(BufferInfo, long)}. <p> Also, {@link #flush} behaves differently in asynchronous mode. After calling {@code flush}, you must call {@link #start} to "resume" receiving input buffers, even if an input surface was created. @param {Object {MediaCodec.Callback}} cb The callback that will run. Use {@code null} to clear a previously set callback (before {@link #configure configure} is called and run in synchronous mode). @param {Object {Handler}} handler Callbacks will happen on the handler's thread. If {@code null}, callbacks are done on the default thread (the caller's thread or the main thread.) */ setCallback : function( ) {}, /**Sets an asynchronous callback for actionable MediaCodec events on the default looper. <p> Same as {@link #setCallback(Callback, Handler)} with handler set to null. @param {Object {MediaCodec.Callback}} cb The callback that will run. Use {@code null} to clear a previously set callback (before {@link #configure configure} is called and run in synchronous mode). @see #setCallback(Callback, Handler) */ setCallback : function( ) {}, /**Registers a callback to be invoked when an output frame is rendered on the output surface. <p> This method can be called in any codec state, but will only have an effect in the Executing state for codecs that render buffers to the output surface. <p> <strong>Note:</strong> This callback is for informational purposes only: to get precise render timing samples, and can be significantly delayed and batched. Some frames may have been rendered even if there was no callback generated. @param {Object {MediaCodec.OnFrameRenderedListener}} listener the callback that will be run @param {Object {Handler}} handler the callback will be run on the handler's thread. If {@code null}, the callback will be run on the default thread, which is the looper from which the codec was created, or a new thread if there was none. */ setOnFrameRenderedListener : function( ) {}, /**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. @throws IllegalStateException if in the Released state. */ getCodecInfo : function( ) {}, };