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 | |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 32 | #include "NdkMediaCrypto.h" |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 33 | #include "NdkMediaFormat.h" |
| 34 | |
| 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
| 39 | |
| 40 | struct AMediaCodec; |
| 41 | typedef struct AMediaCodec AMediaCodec; |
| 42 | |
| 43 | struct AMediaCodecBufferInfo { |
| 44 | int32_t offset; |
| 45 | int32_t size; |
| 46 | int64_t presentationTimeUs; |
| 47 | uint32_t flags; |
| 48 | }; |
| 49 | typedef struct AMediaCodecBufferInfo AMediaCodecBufferInfo; |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 50 | typedef struct AMediaCodecCryptoInfo AMediaCodecCryptoInfo; |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 51 | |
| 52 | enum { |
| 53 | AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM = 4, |
Marco Nelissen | 86aa02c | 2014-05-07 16:03:54 -0700 | [diff] [blame] | 54 | AMEDIACODEC_CONFIGURE_FLAG_ENCODE = 1, |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 55 | AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED = -3, |
| 56 | AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED = -2, |
| 57 | AMEDIACODEC_INFO_TRY_AGAIN_LATER = -1 |
| 58 | }; |
| 59 | |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 60 | /** |
Marco Nelissen | 86aa02c | 2014-05-07 16:03:54 -0700 | [diff] [blame] | 61 | * Create codec by name. Use this if you know the exact codec you want to use. |
| 62 | * When configuring, you will need to specify whether to use the codec as an |
| 63 | * encoder or decoder. |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 64 | */ |
Marco Nelissen | 86aa02c | 2014-05-07 16:03:54 -0700 | [diff] [blame] | 65 | AMediaCodec* AMediaCodec_createCodecByName(const char *name); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Create codec by mime type. Most applications will use this, specifying a |
| 69 | * mime type obtained from media extractor. |
| 70 | */ |
Marco Nelissen | 86aa02c | 2014-05-07 16:03:54 -0700 | [diff] [blame] | 71 | AMediaCodec* AMediaCodec_createDecoderByType(const char *mime_type); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Create encoder by name. |
| 75 | */ |
Marco Nelissen | 86aa02c | 2014-05-07 16:03:54 -0700 | [diff] [blame] | 76 | AMediaCodec* AMediaCodec_createEncoderByType(const char *mime_type); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * delete the codec and free its resources |
| 80 | */ |
| 81 | int AMediaCodec_delete(AMediaCodec*); |
| 82 | |
| 83 | /** |
| 84 | * Configure the codec. For decoding you would typically get the format from an extractor. |
| 85 | */ |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 86 | int AMediaCodec_configure( |
| 87 | AMediaCodec*, |
| 88 | const AMediaFormat* format, |
| 89 | ANativeWindow* surface, |
| 90 | AMediaCrypto *crypto, |
| 91 | uint32_t flags); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Start the codec. A codec must be configured before it can be started, and must be started |
| 95 | * before buffers can be sent to it. |
| 96 | */ |
| 97 | int AMediaCodec_start(AMediaCodec*); |
| 98 | |
| 99 | /** |
| 100 | * Stop the codec. |
| 101 | */ |
| 102 | int AMediaCodec_stop(AMediaCodec*); |
| 103 | |
| 104 | /* |
| 105 | * Flush the codec's input and output. All indices previously returned from calls to |
| 106 | * AMediaCodec_dequeueInputBuffer and AMediaCodec_dequeueOutputBuffer become invalid. |
| 107 | */ |
| 108 | int AMediaCodec_flush(AMediaCodec*); |
| 109 | |
| 110 | /** |
| 111 | * Get an input buffer. The specified buffer index must have been previously obtained from |
| 112 | * dequeueInputBuffer, and not yet queued. |
| 113 | */ |
| 114 | uint8_t* AMediaCodec_getInputBuffer(AMediaCodec*, size_t idx, size_t *out_size); |
| 115 | |
| 116 | /** |
| 117 | * Get an output buffer. The specified buffer index must have been previously obtained from |
| 118 | * dequeueOutputBuffer, and not yet queued. |
| 119 | */ |
| 120 | uint8_t* AMediaCodec_getOutputBuffer(AMediaCodec*, size_t idx, size_t *out_size); |
| 121 | |
| 122 | /** |
| 123 | * Get the index of the next available input buffer. An app will typically use this with |
| 124 | * getInputBuffer() to get a pointer to the buffer, then copy the data to be encoded or decoded |
| 125 | * into the buffer before passing it to the codec. |
| 126 | */ |
| 127 | ssize_t AMediaCodec_dequeueInputBuffer(AMediaCodec*, int64_t timeoutUs); |
| 128 | |
| 129 | /** |
| 130 | * Send the specified buffer to the codec for processing. |
| 131 | */ |
| 132 | int AMediaCodec_queueInputBuffer(AMediaCodec*, |
| 133 | size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags); |
| 134 | |
| 135 | /** |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 136 | * Send the specified buffer to the codec for processing. |
| 137 | */ |
| 138 | int AMediaCodec_queueSecureInputBuffer(AMediaCodec*, |
| 139 | size_t idx, off_t offset, AMediaCodecCryptoInfo*, uint64_t time, uint32_t flags); |
| 140 | |
| 141 | /** |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 142 | * Get the index of the next available buffer of processed data. |
| 143 | */ |
| 144 | ssize_t AMediaCodec_dequeueOutputBuffer(AMediaCodec*, AMediaCodecBufferInfo *info, int64_t timeoutUs); |
| 145 | AMediaFormat* AMediaCodec_getOutputFormat(AMediaCodec*); |
| 146 | |
| 147 | /** |
| 148 | * Release and optionally render the specified buffer. |
| 149 | */ |
| 150 | int AMediaCodec_releaseOutputBuffer(AMediaCodec*, size_t idx, bool render); |
| 151 | |
| 152 | |
Marco Nelissen | cdb42cd | 2014-05-08 14:46:05 -0700 | [diff] [blame] | 153 | typedef void (*OnCodecEvent)(AMediaCodec *codec, void *userdata); |
| 154 | |
| 155 | /** |
| 156 | * Set a callback to be called when a new buffer is available, or there was a format |
| 157 | * or buffer change. |
| 158 | * Note that you cannot perform any operations on the mediacodec from within the callback. |
| 159 | * If you need to perform mediacodec operations, you must do so on a different thread. |
| 160 | */ |
| 161 | int AMediaCodec_setNotificationCallback(AMediaCodec*, OnCodecEvent callback, void *userdata); |
| 162 | |
| 163 | |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 164 | enum { |
| 165 | AMEDIACODECRYPTOINFO_MODE_CLEAR = 0, |
| 166 | AMEDIACODECRYPTOINFO_MODE_AES_CTR = 1 |
| 167 | }; |
| 168 | |
| 169 | /** |
| 170 | * create an AMediaCodecCryptoInfo from scratch. Use this if you need to use custom |
| 171 | * crypto info, rather than one obtained from AMediaExtractor. |
| 172 | */ |
| 173 | AMediaCodecCryptoInfo *AMediaCodecCryptoInfo_new( |
| 174 | int numsubsamples, |
| 175 | uint8_t key[16], |
| 176 | uint8_t iv[16], |
| 177 | uint32_t mode, |
| 178 | size_t *clearbytes, |
| 179 | size_t *encryptedbytes); |
| 180 | |
| 181 | /** |
Marco Nelissen | 829e097 | 2014-05-13 16:22:19 -0700 | [diff] [blame] | 182 | * delete an AMediaCodecCryptoInfo created previously with AMediaCodecCryptoInfo_new, or |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 183 | * obtained from AMediaExtractor |
| 184 | */ |
| 185 | int AMediaCodecCryptoInfo_delete(AMediaCodecCryptoInfo*); |
| 186 | |
| 187 | size_t AMediaCodecCryptoInfo_getNumSubSamples(AMediaCodecCryptoInfo*); |
| 188 | int AMediaCodecCryptoInfo_getKey(AMediaCodecCryptoInfo*, uint8_t *dst); |
| 189 | int AMediaCodecCryptoInfo_getIV(AMediaCodecCryptoInfo*, uint8_t *dst); |
| 190 | uint32_t AMediaCodecCryptoInfo_getMode(AMediaCodecCryptoInfo*); |
| 191 | int AMediaCodecCryptoInfo_getClearBytes(AMediaCodecCryptoInfo*, size_t *dst); |
| 192 | int AMediaCodecCryptoInfo_getEncryptedBytes(AMediaCodecCryptoInfo*, size_t *dst); |
| 193 | |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 194 | #ifdef __cplusplus |
| 195 | } // extern "C" |
| 196 | #endif |
| 197 | |
| 198 | #endif //_NDK_MEDIA_CODEC_H |