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 | #define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "NdkMediaFormat" |
| 19 | |
| 20 | |
| 21 | #include "NdkMediaFormat.h" |
| 22 | |
| 23 | #include <utils/Log.h> |
| 24 | #include <utils/StrongPointer.h> |
| 25 | #include <media/stagefright/foundation/ABuffer.h> |
| 26 | #include <media/stagefright/foundation/AMessage.h> |
| 27 | #include <media/stagefright/MetaData.h> |
| 28 | #include <android_runtime/AndroidRuntime.h> |
| 29 | #include <android_util_Binder.h> |
| 30 | |
| 31 | #include <jni.h> |
| 32 | |
| 33 | using namespace android; |
| 34 | |
| 35 | struct AMediaFormat { |
| 36 | sp<AMessage> mFormat; |
Marco Nelissen | 7c9b141 | 2014-05-06 10:33:07 -0700 | [diff] [blame] | 37 | String8 mDebug; |
| 38 | KeyedVector<String8, String8> mStringCache; |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | extern "C" { |
| 42 | |
| 43 | // private functions for conversion to/from AMessage |
Marco Nelissen | 08aaabe | 2014-05-06 16:08:19 -0700 | [diff] [blame] | 44 | AMediaFormat* AMediaFormat_fromMsg(const void* data) { |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 45 | ALOGV("private ctor"); |
| 46 | AMediaFormat* mData = new AMediaFormat(); |
| 47 | mData->mFormat = *((sp<AMessage>*)data); |
| 48 | return mData; |
| 49 | } |
| 50 | |
Marco Nelissen | 08aaabe | 2014-05-06 16:08:19 -0700 | [diff] [blame] | 51 | void AMediaFormat_getFormat(const AMediaFormat* mData, void* dest) { |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 52 | *((sp<AMessage>*)dest) = mData->mFormat; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /* |
| 57 | * public function follow |
| 58 | */ |
| 59 | AMediaFormat *AMediaFormat_new() { |
| 60 | ALOGV("ctor"); |
| 61 | sp<AMessage> msg = new AMessage(); |
| 62 | return AMediaFormat_fromMsg(&msg); |
| 63 | } |
| 64 | |
| 65 | int AMediaFormat_delete(AMediaFormat *mData) { |
| 66 | ALOGV("dtor"); |
| 67 | delete mData; |
| 68 | return OK; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | const char* AMediaFormat_toString(AMediaFormat *mData) { |
| 73 | sp<AMessage> f = mData->mFormat; |
| 74 | String8 ret; |
| 75 | int num = f->countEntries(); |
| 76 | for (int i = 0; i < num; i++) { |
| 77 | if (i != 0) { |
| 78 | ret.append(", "); |
| 79 | } |
| 80 | AMessage::Type t; |
| 81 | const char *name = f->getEntryNameAt(i, &t); |
| 82 | ret.append(name); |
| 83 | ret.append(": "); |
| 84 | switch (t) { |
| 85 | case AMessage::kTypeInt32: |
| 86 | { |
| 87 | int32_t val; |
| 88 | f->findInt32(name, &val); |
| 89 | ret.appendFormat("int32(%d)", val); |
| 90 | break; |
| 91 | } |
| 92 | case AMessage::kTypeInt64: |
| 93 | { |
| 94 | int64_t val; |
| 95 | f->findInt64(name, &val); |
| 96 | ret.appendFormat("int64(%lld)", val); |
| 97 | break; |
| 98 | } |
| 99 | case AMessage::kTypeSize: |
| 100 | { |
| 101 | size_t val; |
| 102 | f->findSize(name, &val); |
| 103 | ret.appendFormat("size_t(%d)", val); |
| 104 | break; |
| 105 | } |
| 106 | case AMessage::kTypeFloat: |
| 107 | { |
| 108 | float val; |
| 109 | f->findFloat(name, &val); |
| 110 | ret.appendFormat("float(%f)", val); |
| 111 | break; |
| 112 | } |
| 113 | case AMessage::kTypeDouble: |
| 114 | { |
| 115 | double val; |
| 116 | f->findDouble(name, &val); |
| 117 | ret.appendFormat("double(%f)", val); |
| 118 | break; |
| 119 | } |
| 120 | case AMessage::kTypeString: |
| 121 | { |
| 122 | AString val; |
| 123 | f->findString(name, &val); |
| 124 | ret.appendFormat("string(%s)", val.c_str()); |
| 125 | break; |
| 126 | } |
| 127 | case AMessage::kTypeBuffer: |
| 128 | { |
| 129 | ret.appendFormat("data"); |
| 130 | break; |
| 131 | } |
| 132 | default: |
| 133 | { |
| 134 | ret.appendFormat("unknown(%d)", t); |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | ret.append("}"); |
Marco Nelissen | 7c9b141 | 2014-05-06 10:33:07 -0700 | [diff] [blame] | 140 | mData->mDebug = ret; |
| 141 | return mData->mDebug.string(); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Marco Nelissen | 34d4972 | 2014-05-07 12:55:18 -0700 | [diff] [blame^] | 144 | bool AMediaFormat_getInt32(AMediaFormat* format, const char *name, int32_t *out) { |
| 145 | return format->mFormat->findInt32(name, out); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Marco Nelissen | 34d4972 | 2014-05-07 12:55:18 -0700 | [diff] [blame^] | 148 | bool AMediaFormat_getInt64(AMediaFormat* format, const char *name, int64_t *out) { |
| 149 | return format->mFormat->findInt64(name, out); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Marco Nelissen | 34d4972 | 2014-05-07 12:55:18 -0700 | [diff] [blame^] | 152 | bool AMediaFormat_getFloat(AMediaFormat* format, const char *name, float *out) { |
| 153 | return format->mFormat->findFloat(name, out); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Marco Nelissen | 34d4972 | 2014-05-07 12:55:18 -0700 | [diff] [blame^] | 156 | bool AMediaFormat_getSize(AMediaFormat* format, const char *name, size_t *out) { |
| 157 | return format->mFormat->findSize(name, out); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Marco Nelissen | 34d4972 | 2014-05-07 12:55:18 -0700 | [diff] [blame^] | 160 | bool AMediaFormat_getBuffer(AMediaFormat* format, const char *name, void** data, size_t *outsize) { |
| 161 | sp<ABuffer> buf; |
| 162 | if (format->mFormat->findBuffer(name, &buf)) { |
| 163 | *data = buf->data() + buf->offset(); |
| 164 | *outsize = buf->size(); |
| 165 | return true; |
| 166 | } |
| 167 | return false; |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | bool AMediaFormat_getString(AMediaFormat* mData, const char *name, const char **out) { |
Marco Nelissen | 7c9b141 | 2014-05-06 10:33:07 -0700 | [diff] [blame] | 171 | |
| 172 | for (size_t i = 0; i < mData->mStringCache.size(); i++) { |
| 173 | if (strcmp(mData->mStringCache.keyAt(i).string(), name) == 0) { |
| 174 | mData->mStringCache.removeItemsAt(i, 1); |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 179 | AString tmp; |
| 180 | if (mData->mFormat->findString(name, &tmp)) { |
Marco Nelissen | 7c9b141 | 2014-05-06 10:33:07 -0700 | [diff] [blame] | 181 | String8 ret(tmp.c_str()); |
| 182 | mData->mStringCache.add(String8(name), ret); |
| 183 | *out = ret.string(); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 184 | return true; |
| 185 | } |
| 186 | return false; |
| 187 | } |
| 188 | |
Marco Nelissen | 34d4972 | 2014-05-07 12:55:18 -0700 | [diff] [blame^] | 189 | void AMediaFormat_setInt32(AMediaFormat* format, const char *name, int32_t value) { |
| 190 | format->mFormat->setInt32(name, value); |
| 191 | } |
| 192 | |
| 193 | void AMediaFormat_setInt64(AMediaFormat* format, const char *name, int64_t value) { |
| 194 | format->mFormat->setInt64(name, value); |
| 195 | } |
| 196 | |
| 197 | void AMediaFormat_setFloat(AMediaFormat* format, const char* name, float value) { |
| 198 | format->mFormat->setFloat(name, value); |
| 199 | } |
| 200 | |
| 201 | void AMediaFormat_setString(AMediaFormat* format, const char* name, const char* value) { |
| 202 | // AMessage::setString() makes a copy of the string |
| 203 | format->mFormat->setString(name, value, strlen(value)); |
| 204 | } |
| 205 | |
| 206 | void AMediaFormat_setBuffer(AMediaFormat* format, const char* name, void* data, size_t size) { |
| 207 | // the ABuffer(void*, size_t) constructor doesn't take ownership of the data, so create |
| 208 | // a new buffer and copy the data into it |
| 209 | sp<ABuffer> buf = new ABuffer(size); |
| 210 | memcpy(buf->data(), data, size); |
| 211 | buf->setRange(0, size); |
| 212 | // AMessage::setBuffer() increases the refcount of the buffer |
| 213 | format->mFormat->setBuffer(name, buf); |
| 214 | } |
| 215 | |
| 216 | |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 217 | const char* AMEDIAFORMAT_KEY_AAC_PROFILE = "aac-profile"; |
| 218 | const char* AMEDIAFORMAT_KEY_BIT_RATE = "bitrate"; |
| 219 | const char* AMEDIAFORMAT_KEY_CHANNEL_COUNT = "channel-count"; |
| 220 | const char* AMEDIAFORMAT_KEY_CHANNEL_MASK = "channel-mask"; |
| 221 | const char* AMEDIAFORMAT_KEY_COLOR_FORMAT = "color-format"; |
| 222 | const char* AMEDIAFORMAT_KEY_DURATION = "durationUs"; |
| 223 | const char* AMEDIAFORMAT_KEY_FLAC_COMPRESSION_LEVEL = "flac-compression-level"; |
| 224 | const char* AMEDIAFORMAT_KEY_FRAME_RATE = "frame-rate"; |
| 225 | const char* AMEDIAFORMAT_KEY_HEIGHT = "height"; |
| 226 | const char* AMEDIAFORMAT_KEY_IS_ADTS = "is-adts"; |
| 227 | const char* AMEDIAFORMAT_KEY_IS_AUTOSELECT = "is-autoselect"; |
| 228 | const char* AMEDIAFORMAT_KEY_IS_DEFAULT = "is-default"; |
| 229 | const char* AMEDIAFORMAT_KEY_IS_FORCED_SUBTITLE = "is-forced-subtitle"; |
| 230 | const char* AMEDIAFORMAT_KEY_I_FRAME_INTERVAL = "i-frame-interval"; |
| 231 | const char* AMEDIAFORMAT_KEY_LANGUAGE = "language"; |
| 232 | const char* AMEDIAFORMAT_KEY_MAX_HEIGHT = "max-height"; |
| 233 | const char* AMEDIAFORMAT_KEY_MAX_INPUT_SIZE = "max-input-size"; |
| 234 | const char* AMEDIAFORMAT_KEY_MAX_WIDTH = "max-width"; |
| 235 | const char* AMEDIAFORMAT_KEY_MIME = "mime"; |
| 236 | const char* AMEDIAFORMAT_KEY_PUSH_BLANK_BUFFERS_ON_STOP = "push-blank-buffers-on-shutdown"; |
| 237 | const char* AMEDIAFORMAT_KEY_REPEAT_PREVIOUS_FRAME_AFTER = "repeat-previous-frame-after"; |
| 238 | const char* AMEDIAFORMAT_KEY_SAMPLE_RATE = "sample-rate"; |
| 239 | const char* AMEDIAFORMAT_KEY_WIDTH = "width"; |
| 240 | const char* AMEDIAFORMAT_KEY_STRIDE = "stride"; |
| 241 | |
| 242 | |
| 243 | } // extern "C" |
| 244 | |
| 245 | |