Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * This file defines an NDK API. |
| 19 | * Do not remove methods. |
| 20 | * Do not change method signatures. |
| 21 | * Do not change the value of constants. |
| 22 | * Do not change the size of any of the classes defined in here. |
| 23 | * Do not reference types that are not part of the NDK. |
| 24 | * Do not #include files that aren't part of the NDK. |
| 25 | */ |
| 26 | |
| 27 | #ifndef _NDK_MEDIA_CODEC_H |
| 28 | #define _NDK_MEDIA_CODEC_H |
| 29 | |
| 30 | #include <android/native_window.h> |
| 31 | |
| 32 | #include "NdkMediaFormat.h" |
| 33 | |
| 34 | #ifdef __cplusplus |
| 35 | extern "C" { |
| 36 | #endif |
| 37 | |
| 38 | |
| 39 | struct AMediaCodec; |
| 40 | typedef struct AMediaCodec AMediaCodec; |
| 41 | |
| 42 | struct AMediaCodecBufferInfo { |
| 43 | int32_t offset; |
| 44 | int32_t size; |
| 45 | int64_t presentationTimeUs; |
| 46 | uint32_t flags; |
| 47 | }; |
| 48 | typedef struct AMediaCodecBufferInfo AMediaCodecBufferInfo; |
| 49 | |
| 50 | enum { |
| 51 | AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM = 4, |
Marco Nelissen | 86aa02c | 2014-05-07 16:03:54 -0700 | [diff] [blame] | 52 | AMEDIACODEC_CONFIGURE_FLAG_ENCODE = 1, |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 53 | AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED = -3, |
| 54 | AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED = -2, |
| 55 | AMEDIACODEC_INFO_TRY_AGAIN_LATER = -1 |
| 56 | }; |
| 57 | |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 58 | /** |
Marco Nelissen | 86aa02c | 2014-05-07 16:03:54 -0700 | [diff] [blame] | 59 | * Create codec by name. Use this if you know the exact codec you want to use. |
| 60 | * When configuring, you will need to specify whether to use the codec as an |
| 61 | * encoder or decoder. |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 62 | */ |
Marco Nelissen | 86aa02c | 2014-05-07 16:03:54 -0700 | [diff] [blame] | 63 | AMediaCodec* AMediaCodec_createCodecByName(const char *name); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Create codec by mime type. Most applications will use this, specifying a |
| 67 | * mime type obtained from media extractor. |
| 68 | */ |
Marco Nelissen | 86aa02c | 2014-05-07 16:03:54 -0700 | [diff] [blame] | 69 | AMediaCodec* AMediaCodec_createDecoderByType(const char *mime_type); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Create encoder by name. |
| 73 | */ |
Marco Nelissen | 86aa02c | 2014-05-07 16:03:54 -0700 | [diff] [blame] | 74 | AMediaCodec* AMediaCodec_createEncoderByType(const char *mime_type); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * delete the codec and free its resources |
| 78 | */ |
| 79 | int AMediaCodec_delete(AMediaCodec*); |
| 80 | |
| 81 | /** |
| 82 | * Configure the codec. For decoding you would typically get the format from an extractor. |
| 83 | */ |
Marco Nelissen | 86aa02c | 2014-05-07 16:03:54 -0700 | [diff] [blame] | 84 | int AMediaCodec_configure(AMediaCodec*, const AMediaFormat* format, |
| 85 | ANativeWindow* surface, uint32_t flags); // TODO: other args |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Start the codec. A codec must be configured before it can be started, and must be started |
| 89 | * before buffers can be sent to it. |
| 90 | */ |
| 91 | int AMediaCodec_start(AMediaCodec*); |
| 92 | |
| 93 | /** |
| 94 | * Stop the codec. |
| 95 | */ |
| 96 | int AMediaCodec_stop(AMediaCodec*); |
| 97 | |
| 98 | /* |
| 99 | * Flush the codec's input and output. All indices previously returned from calls to |
| 100 | * AMediaCodec_dequeueInputBuffer and AMediaCodec_dequeueOutputBuffer become invalid. |
| 101 | */ |
| 102 | int AMediaCodec_flush(AMediaCodec*); |
| 103 | |
| 104 | /** |
| 105 | * Get an input buffer. The specified buffer index must have been previously obtained from |
| 106 | * dequeueInputBuffer, and not yet queued. |
| 107 | */ |
| 108 | uint8_t* AMediaCodec_getInputBuffer(AMediaCodec*, size_t idx, size_t *out_size); |
| 109 | |
| 110 | /** |
| 111 | * Get an output buffer. The specified buffer index must have been previously obtained from |
| 112 | * dequeueOutputBuffer, and not yet queued. |
| 113 | */ |
| 114 | uint8_t* AMediaCodec_getOutputBuffer(AMediaCodec*, size_t idx, size_t *out_size); |
| 115 | |
| 116 | /** |
| 117 | * Get the index of the next available input buffer. An app will typically use this with |
| 118 | * getInputBuffer() to get a pointer to the buffer, then copy the data to be encoded or decoded |
| 119 | * into the buffer before passing it to the codec. |
| 120 | */ |
| 121 | ssize_t AMediaCodec_dequeueInputBuffer(AMediaCodec*, int64_t timeoutUs); |
| 122 | |
| 123 | /** |
| 124 | * Send the specified buffer to the codec for processing. |
| 125 | */ |
| 126 | int AMediaCodec_queueInputBuffer(AMediaCodec*, |
| 127 | size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags); |
| 128 | |
| 129 | /** |
| 130 | * Get the index of the next available buffer of processed data. |
| 131 | */ |
| 132 | ssize_t AMediaCodec_dequeueOutputBuffer(AMediaCodec*, AMediaCodecBufferInfo *info, int64_t timeoutUs); |
| 133 | AMediaFormat* AMediaCodec_getOutputFormat(AMediaCodec*); |
| 134 | |
| 135 | /** |
| 136 | * Release and optionally render the specified buffer. |
| 137 | */ |
| 138 | int AMediaCodec_releaseOutputBuffer(AMediaCodec*, size_t idx, bool render); |
| 139 | |
| 140 | |
| 141 | |
Marco Nelissen | cdb42cd | 2014-05-08 14:46:05 -0700 | [diff] [blame] | 142 | typedef void (*OnCodecEvent)(AMediaCodec *codec, void *userdata); |
| 143 | |
| 144 | /** |
| 145 | * Set a callback to be called when a new buffer is available, or there was a format |
| 146 | * or buffer change. |
| 147 | * Note that you cannot perform any operations on the mediacodec from within the callback. |
| 148 | * If you need to perform mediacodec operations, you must do so on a different thread. |
| 149 | */ |
| 150 | int AMediaCodec_setNotificationCallback(AMediaCodec*, OnCodecEvent callback, void *userdata); |
| 151 | |
| 152 | |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 153 | #ifdef __cplusplus |
| 154 | } // extern "C" |
| 155 | #endif |
| 156 | |
| 157 | #endif //_NDK_MEDIA_CODEC_H |