blob: f00a19c1056b055d8726201543305e97cf2f902c [file] [log] [blame]
Marco Nelissen0c3be872014-05-01 10:14:44 -07001/*
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 Nelissenc7a11b22014-05-30 10:13:25 -070017//#define LOG_NDEBUG 0
Marco Nelissen0c3be872014-05-01 10:14:44 -070018#define LOG_TAG "NdkMediaExtractor"
19
20
Colin Cross7e8d4ba2017-05-04 16:17:42 -070021#include <media/NdkMediaError.h>
22#include <media/NdkMediaExtractor.h>
Marco Nelissen98603d82018-07-17 11:06:55 -070023#include <media/NdkMediaFormatPriv.h>
Robert Shih0df451b2017-12-08 14:16:50 -080024#include "NdkMediaDataSourcePriv.h"
Marco Nelissen0c3be872014-05-01 10:14:44 -070025
26
Aurimas Liutikas214c8332016-02-19 14:48:23 -080027#include <inttypes.h>
Marco Nelissen0c3be872014-05-01 10:14:44 -070028#include <utils/Log.h>
29#include <utils/StrongPointer.h>
Marco Nelissen050eb322014-05-09 15:10:23 -070030#include <media/hardware/CryptoAPI.h>
Marco Nelissen0c3be872014-05-01 10:14:44 -070031#include <media/stagefright/foundation/ABuffer.h>
32#include <media/stagefright/foundation/AMessage.h>
33#include <media/stagefright/MetaData.h>
34#include <media/stagefright/NuMediaExtractor.h>
35#include <media/IMediaHTTPService.h>
36#include <android_runtime/AndroidRuntime.h>
37#include <android_util_Binder.h>
38
39#include <jni.h>
40
41using namespace android;
42
Marco Nelissene419d7c2014-05-15 14:17:25 -070043static media_status_t translate_error(status_t err) {
Marco Nelissen0c3be872014-05-01 10:14:44 -070044 if (err == OK) {
Marco Nelissene419d7c2014-05-15 14:17:25 -070045 return AMEDIA_OK;
Robert Shihd83d4f42018-02-24 19:02:46 -080046 } else if (err == ERROR_END_OF_STREAM) {
47 return AMEDIA_ERROR_END_OF_STREAM;
48 } else if (err == ERROR_IO) {
49 return AMEDIA_ERROR_IO;
Marco Nelissen0c3be872014-05-01 10:14:44 -070050 }
Robert Shihd83d4f42018-02-24 19:02:46 -080051
Marco Nelissen0c3be872014-05-01 10:14:44 -070052 ALOGE("sf error code: %d", err);
Marco Nelissene419d7c2014-05-15 14:17:25 -070053 return AMEDIA_ERROR_UNKNOWN;
Marco Nelissen0c3be872014-05-01 10:14:44 -070054}
55
56struct AMediaExtractor {
57 sp<NuMediaExtractor> mImpl;
Marco Nelissen050eb322014-05-09 15:10:23 -070058 sp<ABuffer> mPsshBuf;
Marco Nelissen0c3be872014-05-01 10:14:44 -070059};
60
61extern "C" {
62
Marco Nelissen3425fd52014-05-14 11:12:46 -070063EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -070064AMediaExtractor* AMediaExtractor_new() {
65 ALOGV("ctor");
66 AMediaExtractor *mData = new AMediaExtractor();
67 mData->mImpl = new NuMediaExtractor();
68 return mData;
69}
70
Marco Nelissen3425fd52014-05-14 11:12:46 -070071EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -070072media_status_t AMediaExtractor_delete(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -070073 ALOGV("dtor");
74 delete mData;
Marco Nelissene419d7c2014-05-15 14:17:25 -070075 return AMEDIA_OK;
Marco Nelissen0c3be872014-05-01 10:14:44 -070076}
77
Marco Nelissen3425fd52014-05-14 11:12:46 -070078EXPORT
Glenn Kastenb187de12014-12-30 08:18:15 -080079media_status_t AMediaExtractor_setDataSourceFd(AMediaExtractor *mData, int fd, off64_t offset,
80 off64_t length) {
Aurimas Liutikas214c8332016-02-19 14:48:23 -080081 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
Marco Nelissene419d7c2014-05-15 14:17:25 -070082 return translate_error(mData->mImpl->setDataSource(fd, offset, length));
Marco Nelissen0c3be872014-05-01 10:14:44 -070083}
84
Marco Nelissen3425fd52014-05-14 11:12:46 -070085EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -070086media_status_t AMediaExtractor_setDataSource(AMediaExtractor *mData, const char *location) {
Robert Shih6a59e4a2018-08-30 13:31:28 -070087 return AMediaExtractor_setDataSourceWithHeaders(mData, location, 0, NULL, NULL);
88}
89
90EXPORT
91media_status_t AMediaExtractor_setDataSourceWithHeaders(AMediaExtractor *mData,
92 const char *uri,
93 int numheaders,
94 const char * const *keys,
95 const char * const *values) {
96
97 ALOGV("setDataSource(%s)", uri);
Marco Nelissen0c3be872014-05-01 10:14:44 -070098
Robert Shih730af222018-09-14 14:02:57 -070099 sp<MediaHTTPService> httpService = createMediaHttpService(uri, /* version = */ 1);
100 if (httpService == NULL) {
101 ALOGE("can't create http service");
Marco Nelissene419d7c2014-05-15 14:17:25 -0700102 return AMEDIA_ERROR_UNSUPPORTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700103 }
104
Robert Shih6a59e4a2018-08-30 13:31:28 -0700105 KeyedVector<String8, String8> headers;
106 for (int i = 0; i < numheaders; ++i) {
107 String8 key8(keys[i]);
108 String8 value8(values[i]);
109 headers.add(key8, value8);
110 }
111
112 status_t err;
113 err = mData->mImpl->setDataSource(httpService, uri, numheaders > 0 ? &headers : NULL);
Marco Nelissene419d7c2014-05-15 14:17:25 -0700114 return translate_error(err);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700115}
116
Marco Nelissen3425fd52014-05-14 11:12:46 -0700117EXPORT
Robert Shih0df451b2017-12-08 14:16:50 -0800118media_status_t AMediaExtractor_setDataSourceCustom(AMediaExtractor* mData, AMediaDataSource *src) {
119 return translate_error(mData->mImpl->setDataSource(new NdkDataSource(src)));
120}
121
122EXPORT
Robert Shih30e3c7d2018-01-21 17:06:12 -0800123AMediaFormat* AMediaExtractor_getFileFormat(AMediaExtractor *mData) {
124 sp<AMessage> format;
125 mData->mImpl->getFileFormat(&format);
126 return AMediaFormat_fromMsg(&format);
127}
128
129EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700130size_t AMediaExtractor_getTrackCount(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700131 return mData->mImpl->countTracks();
132}
133
Marco Nelissen3425fd52014-05-14 11:12:46 -0700134EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700135AMediaFormat* AMediaExtractor_getTrackFormat(AMediaExtractor *mData, size_t idx) {
136 sp<AMessage> format;
137 mData->mImpl->getTrackFormat(idx, &format);
138 return AMediaFormat_fromMsg(&format);
139}
140
Marco Nelissen3425fd52014-05-14 11:12:46 -0700141EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700142media_status_t AMediaExtractor_selectTrack(AMediaExtractor *mData, size_t idx) {
Mark Salyzyn98f28cd2014-06-18 16:32:50 -0700143 ALOGV("selectTrack(%zu)", idx);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700144 return translate_error(mData->mImpl->selectTrack(idx));
145}
146
Marco Nelissen3425fd52014-05-14 11:12:46 -0700147EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700148media_status_t AMediaExtractor_unselectTrack(AMediaExtractor *mData, size_t idx) {
Mark Salyzyn98f28cd2014-06-18 16:32:50 -0700149 ALOGV("unselectTrack(%zu)", idx);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700150 return translate_error(mData->mImpl->unselectTrack(idx));
151}
152
Marco Nelissen3425fd52014-05-14 11:12:46 -0700153EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700154bool AMediaExtractor_advance(AMediaExtractor *mData) {
155 //ALOGV("advance");
Robert Shih70452262016-09-16 16:43:49 -0700156 status_t err = mData->mImpl->advance();
157 if (err == ERROR_END_OF_STREAM) {
158 return false;
159 } else if (err != OK) {
160 ALOGE("sf error code: %d", err);
161 return false;
162 }
163 return true;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700164}
165
Marco Nelissen3425fd52014-05-14 11:12:46 -0700166EXPORT
Marco Nelissen79e2b622014-05-16 08:07:28 -0700167media_status_t AMediaExtractor_seekTo(AMediaExtractor *ex, int64_t seekPosUs, SeekMode mode) {
168 android::MediaSource::ReadOptions::SeekMode sfmode;
169 if (mode == AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC) {
170 sfmode = android::MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC;
171 } else if (mode == AMEDIAEXTRACTOR_SEEK_CLOSEST_SYNC) {
172 sfmode = android::MediaSource::ReadOptions::SEEK_CLOSEST_SYNC;
173 } else {
174 sfmode = android::MediaSource::ReadOptions::SEEK_NEXT_SYNC;
175 }
176
177 return translate_error(ex->mImpl->seekTo(seekPosUs, sfmode));
178}
179
180EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700181ssize_t AMediaExtractor_readSampleData(AMediaExtractor *mData, uint8_t *buffer, size_t capacity) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700182 //ALOGV("readSampleData");
183 sp<ABuffer> tmp = new ABuffer(buffer, capacity);
184 if (mData->mImpl->readSampleData(tmp) == OK) {
185 return tmp->size();
186 }
187 return -1;
188}
189
Marco Nelissen3425fd52014-05-14 11:12:46 -0700190EXPORT
Robert Shih30e3c7d2018-01-21 17:06:12 -0800191ssize_t AMediaExtractor_getSampleSize(AMediaExtractor *mData) {
192 size_t sampleSize;
193 status_t err = mData->mImpl->getSampleSize(&sampleSize);
194 if (err != OK) {
195 return -1;
196 }
197 return sampleSize;
198}
199
200EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700201uint32_t AMediaExtractor_getSampleFlags(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700202 int sampleFlags = 0;
203 sp<MetaData> meta;
204 status_t err = mData->mImpl->getSampleMeta(&meta);
205 if (err != OK) {
206 return -1;
207 }
208 int32_t val;
209 if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700210 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_SYNC;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700211 }
212
213 uint32_t type;
214 const void *data;
215 size_t size;
216 if (meta->findData(kKeyEncryptedSizes, &type, &data, &size)) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700217 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_ENCRYPTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700218 }
219 return sampleFlags;
220}
221
Marco Nelissen3425fd52014-05-14 11:12:46 -0700222EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700223int AMediaExtractor_getSampleTrackIndex(AMediaExtractor *mData) {
224 size_t idx;
225 if (mData->mImpl->getSampleTrackIndex(&idx) != OK) {
226 return -1;
227 }
228 return idx;
229}
230
Marco Nelissen3425fd52014-05-14 11:12:46 -0700231EXPORT
Marco Nelisseneb4860c2014-05-29 08:04:34 -0700232int64_t AMediaExtractor_getSampleTime(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700233 int64_t time;
234 if (mData->mImpl->getSampleTime(&time) != OK) {
235 return -1;
236 }
237 return time;
238}
239
Marco Nelissen3425fd52014-05-14 11:12:46 -0700240EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700241PsshInfo* AMediaExtractor_getPsshInfo(AMediaExtractor *ex) {
242
243 if (ex->mPsshBuf != NULL) {
244 return (PsshInfo*) ex->mPsshBuf->data();
245 }
246
247 sp<AMessage> format;
248 ex->mImpl->getFileFormat(&format);
249 sp<ABuffer> buffer;
250 if(!format->findBuffer("pssh", &buffer)) {
251 return NULL;
252 }
253
254 // the format of the buffer is 1 or more of:
255 // {
256 // 16 byte uuid
257 // 4 byte data length N
258 // N bytes of data
259 // }
260
261 // Determine the number of entries in the source data.
262 // Since we got the data from stagefright, we trust it is valid and properly formatted.
263 const uint8_t* data = buffer->data();
264 size_t len = buffer->size();
265 size_t numentries = 0;
266 while (len > 0) {
267 numentries++;
268
Marco Nelissen346bb512015-04-09 14:32:45 -0700269 if (len < 16) {
270 ALOGE("invalid PSSH data");
271 return NULL;
272 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700273 // skip uuid
274 data += 16;
275 len -= 16;
276
277 // get data length
Marco Nelissen346bb512015-04-09 14:32:45 -0700278 if (len < 4) {
279 ALOGE("invalid PSSH data");
280 return NULL;
281 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700282 uint32_t datalen = *((uint32_t*)data);
283 data += 4;
284 len -= 4;
285
Marco Nelissen346bb512015-04-09 14:32:45 -0700286 if (len < datalen) {
287 ALOGE("invalid PSSH data");
288 return NULL;
289 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700290 // skip the data
291 data += datalen;
292 len -= datalen;
293 }
294
Marco Nelissen58344bc2014-10-23 11:36:38 -0700295 // there are <numentries> in the source buffer, we need
296 // (source buffer size) - (sizeof(uint32_t) * numentries) + sizeof(size_t)
297 // + ((sizeof(void*) + sizeof(size_t)) * numentries) bytes for the PsshInfo structure
298 // Or in other words, the data lengths in the source structure are replaced by size_t
299 // (which may be the same size or larger, for 64 bit), and in addition there is an
300 // extra pointer for each entry, and an extra size_t for the entire PsshInfo.
301 size_t newsize = buffer->size() - (sizeof(uint32_t) * numentries) + sizeof(size_t)
302 + ((sizeof(void*) + sizeof(size_t)) * numentries);
Marco Nelissen346bb512015-04-09 14:32:45 -0700303 if (newsize <= buffer->size()) {
304 ALOGE("invalid PSSH data");
305 return NULL;
306 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700307 ex->mPsshBuf = new ABuffer(newsize);
308 ex->mPsshBuf->setRange(0, newsize);
309
310 // copy data
311 const uint8_t* src = buffer->data();
312 uint8_t* dst = ex->mPsshBuf->data();
Marco Nelissen58344bc2014-10-23 11:36:38 -0700313 uint8_t* dstdata = dst + sizeof(size_t) + numentries * sizeof(PsshEntry);
314 *((size_t*)dst) = numentries;
315 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700316 for (size_t i = 0; i < numentries; i++) {
317 // copy uuid
318 memcpy(dst, src, 16);
319 src += 16;
320 dst += 16;
321
322 // get/copy data length
323 uint32_t datalen = *((uint32_t*)src);
Marco Nelissen58344bc2014-10-23 11:36:38 -0700324 *((size_t*)dst) = datalen;
325 src += sizeof(uint32_t);
326 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700327
328 // the next entry in the destination is a pointer to the actual data, which we store
329 // after the array of PsshEntry
Marco Nelissen58344bc2014-10-23 11:36:38 -0700330 *((void**)dst) = dstdata;
331 dst += sizeof(void*);
Marco Nelissen050eb322014-05-09 15:10:23 -0700332
333 // copy the actual data
334 memcpy(dstdata, src, datalen);
335 dstdata += datalen;
336 src += datalen;
337 }
338
339 return (PsshInfo*) ex->mPsshBuf->data();
340}
341
Marco Nelissen3425fd52014-05-14 11:12:46 -0700342EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700343AMediaCodecCryptoInfo *AMediaExtractor_getSampleCryptoInfo(AMediaExtractor *ex) {
344 sp<MetaData> meta;
345 if(ex->mImpl->getSampleMeta(&meta) != 0) {
346 return NULL;
347 }
348
349 uint32_t type;
350 const void *crypteddata;
351 size_t cryptedsize;
352 if (!meta->findData(kKeyEncryptedSizes, &type, &crypteddata, &cryptedsize)) {
353 return NULL;
354 }
355 size_t numSubSamples = cryptedsize / sizeof(size_t);
356
357 const void *cleardata;
358 size_t clearsize;
359 if (meta->findData(kKeyPlainSizes, &type, &cleardata, &clearsize)) {
360 if (clearsize != cryptedsize) {
361 // The two must be of the same length.
362 return NULL;
363 }
364 }
365
366 const void *key;
367 size_t keysize;
Edwin Wongb2fb3c92016-08-29 14:57:47 -0700368 if (meta->findData(kKeyCryptoKey, &type, &key, &keysize)) {
Marco Nelissen050eb322014-05-09 15:10:23 -0700369 if (keysize != 16) {
Edwin Wongb2fb3c92016-08-29 14:57:47 -0700370 // Keys must be 16 bytes in length.
Marco Nelissen050eb322014-05-09 15:10:23 -0700371 return NULL;
372 }
373 }
374
375 const void *iv;
376 size_t ivsize;
377 if (meta->findData(kKeyCryptoIV, &type, &iv, &ivsize)) {
378 if (ivsize != 16) {
379 // IVs must be 16 bytes in length.
380 return NULL;
381 }
382 }
383
384 int32_t mode;
385 if (!meta->findInt32(kKeyCryptoMode, &mode)) {
386 mode = CryptoPlugin::kMode_AES_CTR;
387 }
388
389 return AMediaCodecCryptoInfo_new(
390 numSubSamples,
391 (uint8_t*) key,
392 (uint8_t*) iv,
Marco Nelissen79e2b622014-05-16 08:07:28 -0700393 (cryptoinfo_mode_t) mode,
Marco Nelissen050eb322014-05-09 15:10:23 -0700394 (size_t*) cleardata,
395 (size_t*) crypteddata);
396}
397
Robert Shih30e3c7d2018-01-21 17:06:12 -0800398EXPORT
399int64_t AMediaExtractor_getCachedDuration(AMediaExtractor *ex) {
400 bool eos;
401 int64_t durationUs;
402 if (ex->mImpl->getCachedDuration(&durationUs, &eos)) {
403 return durationUs;
404 }
405 return -1;
406}
Marco Nelissen0c3be872014-05-01 10:14:44 -0700407
Robert Shihd83d4f42018-02-24 19:02:46 -0800408EXPORT
409media_status_t AMediaExtractor_getSampleFormat(AMediaExtractor *ex, AMediaFormat *fmt) {
410 if (fmt == NULL) {
411 return AMEDIA_ERROR_INVALID_PARAMETER;
412 }
413
414 sp<MetaData> sampleMeta;
415 status_t err = ex->mImpl->getSampleMeta(&sampleMeta);
416 if (err != OK) {
417 return translate_error(err);
418 }
419
420 sp<AMessage> meta;
421 AMediaFormat_getFormat(fmt, &meta);
422 meta->clear();
423
424 int32_t layerId;
425 if (sampleMeta->findInt32(kKeyTemporalLayerId, &layerId)) {
426 meta->setInt32(AMEDIAFORMAT_KEY_TEMPORAL_LAYER_ID, layerId);
427 }
428
429 size_t trackIndex;
430 err = ex->mImpl->getSampleTrackIndex(&trackIndex);
431 if (err == OK) {
432 meta->setInt32(AMEDIAFORMAT_KEY_TRACK_INDEX, trackIndex);
433 sp<AMessage> trackFormat;
434 AString mime;
435 err = ex->mImpl->getTrackFormat(trackIndex, &trackFormat);
436 if (err == OK
437 && trackFormat != NULL
438 && trackFormat->findString(AMEDIAFORMAT_KEY_MIME, &mime)) {
439 meta->setString(AMEDIAFORMAT_KEY_MIME, mime);
440 }
441 }
442
443 int64_t durationUs;
444 if (sampleMeta->findInt64(kKeyDuration, &durationUs)) {
445 meta->setInt64(AMEDIAFORMAT_KEY_DURATION, durationUs);
446 }
447
448 uint32_t dataType; // unused
449 const void *seiData;
450 size_t seiLength;
451 if (sampleMeta->findData(kKeySEI, &dataType, &seiData, &seiLength)) {
452 sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);;
453 meta->setBuffer(AMEDIAFORMAT_KEY_SEI, sei);
454 }
455
456 const void *mpegUserDataPointer;
457 size_t mpegUserDataLength;
458 if (sampleMeta->findData(
459 kKeyMpegUserData, &dataType, &mpegUserDataPointer, &mpegUserDataLength)) {
460 sp<ABuffer> mpegUserData = ABuffer::CreateAsCopy(mpegUserDataPointer, mpegUserDataLength);
461 meta->setBuffer(AMEDIAFORMAT_KEY_MPEG_USER_DATA, mpegUserData);
462 }
463
464 return AMEDIA_OK;
465}
466
Robert Shih08eb9082018-03-09 13:57:26 -0800467EXPORT
468media_status_t AMediaExtractor_disconnect(AMediaExtractor * ex) {
469 ex->mImpl->disconnect();
470 return AMEDIA_OK;
471}
472
Marco Nelissen0c3be872014-05-01 10:14:44 -0700473} // extern "C"
474