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 | |
Marco Nelissen | c7a11b2 | 2014-05-30 10:13:25 -0700 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 18 | #define LOG_TAG "NdkMediaExtractor" |
| 19 | |
| 20 | |
Colin Cross | 7e8d4ba | 2017-05-04 16:17:42 -0700 | [diff] [blame] | 21 | #include <media/NdkMediaError.h> |
| 22 | #include <media/NdkMediaExtractor.h> |
Marco Nelissen | 5dcf85a | 2018-10-11 09:49:02 -0700 | [diff] [blame] | 23 | #include <media/NdkMediaErrorPriv.h> |
Marco Nelissen | 98603d8 | 2018-07-17 11:06:55 -0700 | [diff] [blame] | 24 | #include <media/NdkMediaFormatPriv.h> |
Santiago Seifert | 79c34f2 | 2020-11-11 23:49:28 +0000 | [diff] [blame] | 25 | #include "NdkJavaVMHelperPriv.h" |
Robert Shih | 0df451b | 2017-12-08 14:16:50 -0800 | [diff] [blame] | 26 | #include "NdkMediaDataSourcePriv.h" |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 27 | |
| 28 | |
Aurimas Liutikas | 214c833 | 2016-02-19 14:48:23 -0800 | [diff] [blame] | 29 | #include <inttypes.h> |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 30 | #include <utils/Log.h> |
| 31 | #include <utils/StrongPointer.h> |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 32 | #include <media/hardware/CryptoAPI.h> |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 33 | #include <media/stagefright/foundation/ABuffer.h> |
| 34 | #include <media/stagefright/foundation/AMessage.h> |
| 35 | #include <media/stagefright/MetaData.h> |
| 36 | #include <media/stagefright/NuMediaExtractor.h> |
| 37 | #include <media/IMediaHTTPService.h> |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 38 | #include <android_util_Binder.h> |
| 39 | |
| 40 | #include <jni.h> |
| 41 | |
| 42 | using namespace android; |
| 43 | |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 44 | struct AMediaExtractor { |
| 45 | sp<NuMediaExtractor> mImpl; |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 46 | sp<ABuffer> mPsshBuf; |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Robert Shih | 0b4530d | 2019-01-18 12:12:03 -0800 | [diff] [blame] | 49 | sp<ABuffer> U32ArrayToSizeBuf(size_t numSubSamples, uint32_t *data) { |
| 50 | if (numSubSamples > SIZE_MAX / sizeof(size_t)) { |
| 51 | return NULL; |
| 52 | } |
| 53 | sp<ABuffer> sizebuf = new ABuffer(numSubSamples * sizeof(size_t)); |
| 54 | size_t *sizes = (size_t *)sizebuf->data(); |
| 55 | for (size_t i = 0; sizes != NULL && i < numSubSamples; i++) { |
| 56 | sizes[i] = data[i]; |
| 57 | } |
| 58 | return sizebuf; |
| 59 | } |
| 60 | |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 61 | extern "C" { |
| 62 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 63 | EXPORT |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 64 | AMediaExtractor* AMediaExtractor_new() { |
| 65 | ALOGV("ctor"); |
| 66 | AMediaExtractor *mData = new AMediaExtractor(); |
Santiago Seifert | 79c34f2 | 2020-11-11 23:49:28 +0000 | [diff] [blame] | 67 | mData->mImpl = new NuMediaExtractor( |
| 68 | NdkJavaVMHelper::getJNIEnv() != nullptr |
| 69 | ? NuMediaExtractor::EntryPoint::NDK_WITH_JVM |
| 70 | : NuMediaExtractor::EntryPoint::NDK_NO_JVM ); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 71 | return mData; |
| 72 | } |
| 73 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 74 | EXPORT |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 75 | media_status_t AMediaExtractor_delete(AMediaExtractor *mData) { |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 76 | ALOGV("dtor"); |
| 77 | delete mData; |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 78 | return AMEDIA_OK; |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 81 | EXPORT |
Glenn Kasten | b187de1 | 2014-12-30 08:18:15 -0800 | [diff] [blame] | 82 | media_status_t AMediaExtractor_setDataSourceFd(AMediaExtractor *mData, int fd, off64_t offset, |
| 83 | off64_t length) { |
Aurimas Liutikas | 214c833 | 2016-02-19 14:48:23 -0800 | [diff] [blame] | 84 | ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length); |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 85 | return translate_error(mData->mImpl->setDataSource(fd, offset, length)); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Robert Shih | 6a59e4a | 2018-08-30 13:31:28 -0700 | [diff] [blame] | 88 | media_status_t AMediaExtractor_setDataSourceWithHeaders(AMediaExtractor *mData, |
| 89 | const char *uri, |
| 90 | int numheaders, |
| 91 | const char * const *keys, |
| 92 | const char * const *values) { |
| 93 | |
| 94 | ALOGV("setDataSource(%s)", uri); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 95 | |
Marco Nelissen | a523204 | 2019-09-24 09:27:40 -0700 | [diff] [blame] | 96 | sp<MediaHTTPService> httpService = createMediaHttpService(uri); |
Robert Shih | 730af22 | 2018-09-14 14:02:57 -0700 | [diff] [blame] | 97 | if (httpService == NULL) { |
| 98 | ALOGE("can't create http service"); |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 99 | return AMEDIA_ERROR_UNSUPPORTED; |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Robert Shih | 6a59e4a | 2018-08-30 13:31:28 -0700 | [diff] [blame] | 102 | KeyedVector<String8, String8> headers; |
| 103 | for (int i = 0; i < numheaders; ++i) { |
| 104 | String8 key8(keys[i]); |
| 105 | String8 value8(values[i]); |
| 106 | headers.add(key8, value8); |
| 107 | } |
| 108 | |
| 109 | status_t err; |
| 110 | err = mData->mImpl->setDataSource(httpService, uri, numheaders > 0 ? &headers : NULL); |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 111 | return translate_error(err); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 114 | EXPORT |
Robert Shih | 82248d1 | 2018-10-03 15:57:47 -0700 | [diff] [blame] | 115 | media_status_t AMediaExtractor_setDataSource(AMediaExtractor *mData, const char *location) { |
| 116 | return AMediaExtractor_setDataSourceWithHeaders(mData, location, 0, NULL, NULL); |
| 117 | } |
| 118 | |
| 119 | EXPORT |
Robert Shih | 0df451b | 2017-12-08 14:16:50 -0800 | [diff] [blame] | 120 | media_status_t AMediaExtractor_setDataSourceCustom(AMediaExtractor* mData, AMediaDataSource *src) { |
| 121 | return translate_error(mData->mImpl->setDataSource(new NdkDataSource(src))); |
| 122 | } |
| 123 | |
| 124 | EXPORT |
Robert Shih | 30e3c7d | 2018-01-21 17:06:12 -0800 | [diff] [blame] | 125 | AMediaFormat* AMediaExtractor_getFileFormat(AMediaExtractor *mData) { |
| 126 | sp<AMessage> format; |
| 127 | mData->mImpl->getFileFormat(&format); |
| 128 | return AMediaFormat_fromMsg(&format); |
| 129 | } |
| 130 | |
| 131 | EXPORT |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 132 | size_t AMediaExtractor_getTrackCount(AMediaExtractor *mData) { |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 133 | return mData->mImpl->countTracks(); |
| 134 | } |
| 135 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 136 | EXPORT |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 137 | AMediaFormat* AMediaExtractor_getTrackFormat(AMediaExtractor *mData, size_t idx) { |
| 138 | sp<AMessage> format; |
| 139 | mData->mImpl->getTrackFormat(idx, &format); |
| 140 | return AMediaFormat_fromMsg(&format); |
| 141 | } |
| 142 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 143 | EXPORT |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 144 | media_status_t AMediaExtractor_selectTrack(AMediaExtractor *mData, size_t idx) { |
Mark Salyzyn | 98f28cd | 2014-06-18 16:32:50 -0700 | [diff] [blame] | 145 | ALOGV("selectTrack(%zu)", idx); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 146 | return translate_error(mData->mImpl->selectTrack(idx)); |
| 147 | } |
| 148 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 149 | EXPORT |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 150 | media_status_t AMediaExtractor_unselectTrack(AMediaExtractor *mData, size_t idx) { |
Mark Salyzyn | 98f28cd | 2014-06-18 16:32:50 -0700 | [diff] [blame] | 151 | ALOGV("unselectTrack(%zu)", idx); |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 152 | return translate_error(mData->mImpl->unselectTrack(idx)); |
| 153 | } |
| 154 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 155 | EXPORT |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 156 | bool AMediaExtractor_advance(AMediaExtractor *mData) { |
| 157 | //ALOGV("advance"); |
Robert Shih | 7045226 | 2016-09-16 16:43:49 -0700 | [diff] [blame] | 158 | status_t err = mData->mImpl->advance(); |
| 159 | if (err == ERROR_END_OF_STREAM) { |
| 160 | return false; |
| 161 | } else if (err != OK) { |
| 162 | ALOGE("sf error code: %d", err); |
| 163 | return false; |
| 164 | } |
| 165 | return true; |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 168 | EXPORT |
Marco Nelissen | 79e2b62 | 2014-05-16 08:07:28 -0700 | [diff] [blame] | 169 | media_status_t AMediaExtractor_seekTo(AMediaExtractor *ex, int64_t seekPosUs, SeekMode mode) { |
| 170 | android::MediaSource::ReadOptions::SeekMode sfmode; |
| 171 | if (mode == AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC) { |
| 172 | sfmode = android::MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC; |
| 173 | } else if (mode == AMEDIAEXTRACTOR_SEEK_CLOSEST_SYNC) { |
| 174 | sfmode = android::MediaSource::ReadOptions::SEEK_CLOSEST_SYNC; |
| 175 | } else { |
| 176 | sfmode = android::MediaSource::ReadOptions::SEEK_NEXT_SYNC; |
| 177 | } |
| 178 | |
| 179 | return translate_error(ex->mImpl->seekTo(seekPosUs, sfmode)); |
| 180 | } |
| 181 | |
| 182 | EXPORT |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 183 | ssize_t AMediaExtractor_readSampleData(AMediaExtractor *mData, uint8_t *buffer, size_t capacity) { |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 184 | //ALOGV("readSampleData"); |
| 185 | sp<ABuffer> tmp = new ABuffer(buffer, capacity); |
| 186 | if (mData->mImpl->readSampleData(tmp) == OK) { |
| 187 | return tmp->size(); |
| 188 | } |
| 189 | return -1; |
| 190 | } |
| 191 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 192 | EXPORT |
Robert Shih | 30e3c7d | 2018-01-21 17:06:12 -0800 | [diff] [blame] | 193 | ssize_t AMediaExtractor_getSampleSize(AMediaExtractor *mData) { |
| 194 | size_t sampleSize; |
| 195 | status_t err = mData->mImpl->getSampleSize(&sampleSize); |
| 196 | if (err != OK) { |
| 197 | return -1; |
| 198 | } |
| 199 | return sampleSize; |
| 200 | } |
| 201 | |
| 202 | EXPORT |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 203 | uint32_t AMediaExtractor_getSampleFlags(AMediaExtractor *mData) { |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 204 | int sampleFlags = 0; |
| 205 | sp<MetaData> meta; |
| 206 | status_t err = mData->mImpl->getSampleMeta(&meta); |
| 207 | if (err != OK) { |
| 208 | return -1; |
| 209 | } |
| 210 | int32_t val; |
| 211 | if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 212 | sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_SYNC; |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | uint32_t type; |
| 216 | const void *data; |
| 217 | size_t size; |
| 218 | if (meta->findData(kKeyEncryptedSizes, &type, &data, &size)) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 219 | sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_ENCRYPTED; |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 220 | } |
| 221 | return sampleFlags; |
| 222 | } |
| 223 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 224 | EXPORT |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 225 | int AMediaExtractor_getSampleTrackIndex(AMediaExtractor *mData) { |
| 226 | size_t idx; |
| 227 | if (mData->mImpl->getSampleTrackIndex(&idx) != OK) { |
| 228 | return -1; |
| 229 | } |
| 230 | return idx; |
| 231 | } |
| 232 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 233 | EXPORT |
Marco Nelissen | eb4860c | 2014-05-29 08:04:34 -0700 | [diff] [blame] | 234 | int64_t AMediaExtractor_getSampleTime(AMediaExtractor *mData) { |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 235 | int64_t time; |
| 236 | if (mData->mImpl->getSampleTime(&time) != OK) { |
| 237 | return -1; |
| 238 | } |
| 239 | return time; |
| 240 | } |
| 241 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 242 | EXPORT |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 243 | PsshInfo* AMediaExtractor_getPsshInfo(AMediaExtractor *ex) { |
| 244 | |
| 245 | if (ex->mPsshBuf != NULL) { |
| 246 | return (PsshInfo*) ex->mPsshBuf->data(); |
| 247 | } |
| 248 | |
| 249 | sp<AMessage> format; |
| 250 | ex->mImpl->getFileFormat(&format); |
| 251 | sp<ABuffer> buffer; |
| 252 | if(!format->findBuffer("pssh", &buffer)) { |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | // the format of the buffer is 1 or more of: |
| 257 | // { |
| 258 | // 16 byte uuid |
| 259 | // 4 byte data length N |
| 260 | // N bytes of data |
| 261 | // } |
| 262 | |
| 263 | // Determine the number of entries in the source data. |
| 264 | // Since we got the data from stagefright, we trust it is valid and properly formatted. |
| 265 | const uint8_t* data = buffer->data(); |
| 266 | size_t len = buffer->size(); |
| 267 | size_t numentries = 0; |
| 268 | while (len > 0) { |
| 269 | numentries++; |
| 270 | |
Marco Nelissen | 346bb51 | 2015-04-09 14:32:45 -0700 | [diff] [blame] | 271 | if (len < 16) { |
| 272 | ALOGE("invalid PSSH data"); |
| 273 | return NULL; |
| 274 | } |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 275 | // skip uuid |
| 276 | data += 16; |
| 277 | len -= 16; |
| 278 | |
| 279 | // get data length |
Marco Nelissen | 346bb51 | 2015-04-09 14:32:45 -0700 | [diff] [blame] | 280 | if (len < 4) { |
| 281 | ALOGE("invalid PSSH data"); |
| 282 | return NULL; |
| 283 | } |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 284 | uint32_t datalen = *((uint32_t*)data); |
| 285 | data += 4; |
| 286 | len -= 4; |
| 287 | |
Marco Nelissen | 346bb51 | 2015-04-09 14:32:45 -0700 | [diff] [blame] | 288 | if (len < datalen) { |
| 289 | ALOGE("invalid PSSH data"); |
| 290 | return NULL; |
| 291 | } |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 292 | // skip the data |
| 293 | data += datalen; |
| 294 | len -= datalen; |
| 295 | } |
| 296 | |
Marco Nelissen | 58344bc | 2014-10-23 11:36:38 -0700 | [diff] [blame] | 297 | // there are <numentries> in the source buffer, we need |
| 298 | // (source buffer size) - (sizeof(uint32_t) * numentries) + sizeof(size_t) |
| 299 | // + ((sizeof(void*) + sizeof(size_t)) * numentries) bytes for the PsshInfo structure |
| 300 | // Or in other words, the data lengths in the source structure are replaced by size_t |
| 301 | // (which may be the same size or larger, for 64 bit), and in addition there is an |
| 302 | // extra pointer for each entry, and an extra size_t for the entire PsshInfo. |
| 303 | size_t newsize = buffer->size() - (sizeof(uint32_t) * numentries) + sizeof(size_t) |
| 304 | + ((sizeof(void*) + sizeof(size_t)) * numentries); |
Marco Nelissen | 346bb51 | 2015-04-09 14:32:45 -0700 | [diff] [blame] | 305 | if (newsize <= buffer->size()) { |
| 306 | ALOGE("invalid PSSH data"); |
| 307 | return NULL; |
| 308 | } |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 309 | ex->mPsshBuf = new ABuffer(newsize); |
| 310 | ex->mPsshBuf->setRange(0, newsize); |
| 311 | |
| 312 | // copy data |
| 313 | const uint8_t* src = buffer->data(); |
| 314 | uint8_t* dst = ex->mPsshBuf->data(); |
Marco Nelissen | 58344bc | 2014-10-23 11:36:38 -0700 | [diff] [blame] | 315 | uint8_t* dstdata = dst + sizeof(size_t) + numentries * sizeof(PsshEntry); |
| 316 | *((size_t*)dst) = numentries; |
| 317 | dst += sizeof(size_t); |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 318 | for (size_t i = 0; i < numentries; i++) { |
| 319 | // copy uuid |
| 320 | memcpy(dst, src, 16); |
| 321 | src += 16; |
| 322 | dst += 16; |
| 323 | |
| 324 | // get/copy data length |
| 325 | uint32_t datalen = *((uint32_t*)src); |
Marco Nelissen | 58344bc | 2014-10-23 11:36:38 -0700 | [diff] [blame] | 326 | *((size_t*)dst) = datalen; |
| 327 | src += sizeof(uint32_t); |
| 328 | dst += sizeof(size_t); |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 329 | |
| 330 | // the next entry in the destination is a pointer to the actual data, which we store |
| 331 | // after the array of PsshEntry |
Marco Nelissen | 58344bc | 2014-10-23 11:36:38 -0700 | [diff] [blame] | 332 | *((void**)dst) = dstdata; |
| 333 | dst += sizeof(void*); |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 334 | |
| 335 | // copy the actual data |
| 336 | memcpy(dstdata, src, datalen); |
| 337 | dstdata += datalen; |
| 338 | src += datalen; |
| 339 | } |
| 340 | |
| 341 | return (PsshInfo*) ex->mPsshBuf->data(); |
| 342 | } |
| 343 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 344 | EXPORT |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 345 | AMediaCodecCryptoInfo *AMediaExtractor_getSampleCryptoInfo(AMediaExtractor *ex) { |
| 346 | sp<MetaData> meta; |
| 347 | if(ex->mImpl->getSampleMeta(&meta) != 0) { |
| 348 | return NULL; |
| 349 | } |
| 350 | |
| 351 | uint32_t type; |
| 352 | const void *crypteddata; |
| 353 | size_t cryptedsize; |
| 354 | if (!meta->findData(kKeyEncryptedSizes, &type, &crypteddata, &cryptedsize)) { |
| 355 | return NULL; |
| 356 | } |
Robert Shih | 0b4530d | 2019-01-18 12:12:03 -0800 | [diff] [blame] | 357 | size_t numSubSamples = cryptedsize / sizeof(uint32_t); |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 358 | |
| 359 | const void *cleardata; |
| 360 | size_t clearsize; |
| 361 | if (meta->findData(kKeyPlainSizes, &type, &cleardata, &clearsize)) { |
| 362 | if (clearsize != cryptedsize) { |
| 363 | // The two must be of the same length. |
| 364 | return NULL; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | const void *key; |
| 369 | size_t keysize; |
Edwin Wong | b2fb3c9 | 2016-08-29 14:57:47 -0700 | [diff] [blame] | 370 | if (meta->findData(kKeyCryptoKey, &type, &key, &keysize)) { |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 371 | if (keysize != 16) { |
Edwin Wong | b2fb3c9 | 2016-08-29 14:57:47 -0700 | [diff] [blame] | 372 | // Keys must be 16 bytes in length. |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 373 | return NULL; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | const void *iv; |
| 378 | size_t ivsize; |
| 379 | if (meta->findData(kKeyCryptoIV, &type, &iv, &ivsize)) { |
| 380 | if (ivsize != 16) { |
| 381 | // IVs must be 16 bytes in length. |
| 382 | return NULL; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | int32_t mode; |
| 387 | if (!meta->findInt32(kKeyCryptoMode, &mode)) { |
| 388 | mode = CryptoPlugin::kMode_AES_CTR; |
| 389 | } |
| 390 | |
Robert Shih | 0b4530d | 2019-01-18 12:12:03 -0800 | [diff] [blame] | 391 | if (sizeof(uint32_t) != sizeof(size_t)) { |
| 392 | sp<ABuffer> clearbuf = U32ArrayToSizeBuf(numSubSamples, (uint32_t *)cleardata); |
| 393 | sp<ABuffer> cryptedbuf = U32ArrayToSizeBuf(numSubSamples, (uint32_t *)crypteddata); |
| 394 | cleardata = clearbuf == NULL ? NULL : clearbuf->data(); |
| 395 | crypteddata = crypteddata == NULL ? NULL : cryptedbuf->data(); |
| 396 | if(crypteddata == NULL || cleardata == NULL) { |
| 397 | return NULL; |
| 398 | } |
| 399 | } |
| 400 | |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 401 | return AMediaCodecCryptoInfo_new( |
| 402 | numSubSamples, |
| 403 | (uint8_t*) key, |
| 404 | (uint8_t*) iv, |
Marco Nelissen | 79e2b62 | 2014-05-16 08:07:28 -0700 | [diff] [blame] | 405 | (cryptoinfo_mode_t) mode, |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 406 | (size_t*) cleardata, |
| 407 | (size_t*) crypteddata); |
| 408 | } |
| 409 | |
Robert Shih | 30e3c7d | 2018-01-21 17:06:12 -0800 | [diff] [blame] | 410 | EXPORT |
| 411 | int64_t AMediaExtractor_getCachedDuration(AMediaExtractor *ex) { |
| 412 | bool eos; |
| 413 | int64_t durationUs; |
| 414 | if (ex->mImpl->getCachedDuration(&durationUs, &eos)) { |
| 415 | return durationUs; |
| 416 | } |
| 417 | return -1; |
| 418 | } |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 419 | |
Robert Shih | d83d4f4 | 2018-02-24 19:02:46 -0800 | [diff] [blame] | 420 | EXPORT |
| 421 | media_status_t AMediaExtractor_getSampleFormat(AMediaExtractor *ex, AMediaFormat *fmt) { |
| 422 | if (fmt == NULL) { |
| 423 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 424 | } |
| 425 | |
| 426 | sp<MetaData> sampleMeta; |
| 427 | status_t err = ex->mImpl->getSampleMeta(&sampleMeta); |
| 428 | if (err != OK) { |
| 429 | return translate_error(err); |
| 430 | } |
| 431 | |
| 432 | sp<AMessage> meta; |
| 433 | AMediaFormat_getFormat(fmt, &meta); |
| 434 | meta->clear(); |
| 435 | |
| 436 | int32_t layerId; |
| 437 | if (sampleMeta->findInt32(kKeyTemporalLayerId, &layerId)) { |
| 438 | meta->setInt32(AMEDIAFORMAT_KEY_TEMPORAL_LAYER_ID, layerId); |
| 439 | } |
| 440 | |
| 441 | size_t trackIndex; |
| 442 | err = ex->mImpl->getSampleTrackIndex(&trackIndex); |
| 443 | if (err == OK) { |
| 444 | meta->setInt32(AMEDIAFORMAT_KEY_TRACK_INDEX, trackIndex); |
| 445 | sp<AMessage> trackFormat; |
| 446 | AString mime; |
| 447 | err = ex->mImpl->getTrackFormat(trackIndex, &trackFormat); |
| 448 | if (err == OK |
| 449 | && trackFormat != NULL |
| 450 | && trackFormat->findString(AMEDIAFORMAT_KEY_MIME, &mime)) { |
| 451 | meta->setString(AMEDIAFORMAT_KEY_MIME, mime); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | int64_t durationUs; |
| 456 | if (sampleMeta->findInt64(kKeyDuration, &durationUs)) { |
| 457 | meta->setInt64(AMEDIAFORMAT_KEY_DURATION, durationUs); |
| 458 | } |
| 459 | |
| 460 | uint32_t dataType; // unused |
| 461 | const void *seiData; |
| 462 | size_t seiLength; |
| 463 | if (sampleMeta->findData(kKeySEI, &dataType, &seiData, &seiLength)) { |
| 464 | sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);; |
| 465 | meta->setBuffer(AMEDIAFORMAT_KEY_SEI, sei); |
| 466 | } |
| 467 | |
| 468 | const void *mpegUserDataPointer; |
| 469 | size_t mpegUserDataLength; |
| 470 | if (sampleMeta->findData( |
| 471 | kKeyMpegUserData, &dataType, &mpegUserDataPointer, &mpegUserDataLength)) { |
| 472 | sp<ABuffer> mpegUserData = ABuffer::CreateAsCopy(mpegUserDataPointer, mpegUserDataLength); |
| 473 | meta->setBuffer(AMEDIAFORMAT_KEY_MPEG_USER_DATA, mpegUserData); |
| 474 | } |
| 475 | |
Sampath Shetty | c6148a6 | 2018-12-18 15:50:43 +1100 | [diff] [blame] | 476 | const void *audioPresentationsPointer; |
| 477 | size_t audioPresentationsLength; |
| 478 | if (sampleMeta->findData( |
| 479 | kKeyAudioPresentationInfo, &dataType, |
| 480 | &audioPresentationsPointer, &audioPresentationsLength)) { |
| 481 | sp<ABuffer> audioPresentationsData = ABuffer::CreateAsCopy( |
| 482 | audioPresentationsPointer, audioPresentationsLength); |
| 483 | meta->setBuffer(AMEDIAFORMAT_KEY_AUDIO_PRESENTATION_INFO, audioPresentationsData); |
| 484 | } |
| 485 | |
Robert Shih | d83d4f4 | 2018-02-24 19:02:46 -0800 | [diff] [blame] | 486 | return AMEDIA_OK; |
| 487 | } |
| 488 | |
Marco Nelissen | 0c3be87 | 2014-05-01 10:14:44 -0700 | [diff] [blame] | 489 | } // extern "C" |
| 490 | |