blob: 5f90dfe12fa496b46d0a58374c512489cb30856a [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
Robert Shih6a59e4a2018-08-30 13:31:28 -070085media_status_t AMediaExtractor_setDataSourceWithHeaders(AMediaExtractor *mData,
86 const char *uri,
87 int numheaders,
88 const char * const *keys,
89 const char * const *values) {
90
91 ALOGV("setDataSource(%s)", uri);
Marco Nelissen0c3be872014-05-01 10:14:44 -070092
Robert Shih730af222018-09-14 14:02:57 -070093 sp<MediaHTTPService> httpService = createMediaHttpService(uri, /* version = */ 1);
94 if (httpService == NULL) {
95 ALOGE("can't create http service");
Marco Nelissene419d7c2014-05-15 14:17:25 -070096 return AMEDIA_ERROR_UNSUPPORTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -070097 }
98
Robert Shih6a59e4a2018-08-30 13:31:28 -070099 KeyedVector<String8, String8> headers;
100 for (int i = 0; i < numheaders; ++i) {
101 String8 key8(keys[i]);
102 String8 value8(values[i]);
103 headers.add(key8, value8);
104 }
105
106 status_t err;
107 err = mData->mImpl->setDataSource(httpService, uri, numheaders > 0 ? &headers : NULL);
Marco Nelissene419d7c2014-05-15 14:17:25 -0700108 return translate_error(err);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700109}
110
Marco Nelissen3425fd52014-05-14 11:12:46 -0700111EXPORT
Robert Shih82248d12018-10-03 15:57:47 -0700112media_status_t AMediaExtractor_setDataSource(AMediaExtractor *mData, const char *location) {
113 return AMediaExtractor_setDataSourceWithHeaders(mData, location, 0, NULL, NULL);
114}
115
116EXPORT
Robert Shih0df451b2017-12-08 14:16:50 -0800117media_status_t AMediaExtractor_setDataSourceCustom(AMediaExtractor* mData, AMediaDataSource *src) {
118 return translate_error(mData->mImpl->setDataSource(new NdkDataSource(src)));
119}
120
121EXPORT
Robert Shih30e3c7d2018-01-21 17:06:12 -0800122AMediaFormat* AMediaExtractor_getFileFormat(AMediaExtractor *mData) {
123 sp<AMessage> format;
124 mData->mImpl->getFileFormat(&format);
125 return AMediaFormat_fromMsg(&format);
126}
127
128EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700129size_t AMediaExtractor_getTrackCount(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700130 return mData->mImpl->countTracks();
131}
132
Marco Nelissen3425fd52014-05-14 11:12:46 -0700133EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700134AMediaFormat* AMediaExtractor_getTrackFormat(AMediaExtractor *mData, size_t idx) {
135 sp<AMessage> format;
136 mData->mImpl->getTrackFormat(idx, &format);
137 return AMediaFormat_fromMsg(&format);
138}
139
Marco Nelissen3425fd52014-05-14 11:12:46 -0700140EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700141media_status_t AMediaExtractor_selectTrack(AMediaExtractor *mData, size_t idx) {
Mark Salyzyn98f28cd2014-06-18 16:32:50 -0700142 ALOGV("selectTrack(%zu)", idx);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700143 return translate_error(mData->mImpl->selectTrack(idx));
144}
145
Marco Nelissen3425fd52014-05-14 11:12:46 -0700146EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700147media_status_t AMediaExtractor_unselectTrack(AMediaExtractor *mData, size_t idx) {
Mark Salyzyn98f28cd2014-06-18 16:32:50 -0700148 ALOGV("unselectTrack(%zu)", idx);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700149 return translate_error(mData->mImpl->unselectTrack(idx));
150}
151
Marco Nelissen3425fd52014-05-14 11:12:46 -0700152EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700153bool AMediaExtractor_advance(AMediaExtractor *mData) {
154 //ALOGV("advance");
Robert Shih70452262016-09-16 16:43:49 -0700155 status_t err = mData->mImpl->advance();
156 if (err == ERROR_END_OF_STREAM) {
157 return false;
158 } else if (err != OK) {
159 ALOGE("sf error code: %d", err);
160 return false;
161 }
162 return true;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700163}
164
Marco Nelissen3425fd52014-05-14 11:12:46 -0700165EXPORT
Marco Nelissen79e2b622014-05-16 08:07:28 -0700166media_status_t AMediaExtractor_seekTo(AMediaExtractor *ex, int64_t seekPosUs, SeekMode mode) {
167 android::MediaSource::ReadOptions::SeekMode sfmode;
168 if (mode == AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC) {
169 sfmode = android::MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC;
170 } else if (mode == AMEDIAEXTRACTOR_SEEK_CLOSEST_SYNC) {
171 sfmode = android::MediaSource::ReadOptions::SEEK_CLOSEST_SYNC;
172 } else {
173 sfmode = android::MediaSource::ReadOptions::SEEK_NEXT_SYNC;
174 }
175
176 return translate_error(ex->mImpl->seekTo(seekPosUs, sfmode));
177}
178
179EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700180ssize_t AMediaExtractor_readSampleData(AMediaExtractor *mData, uint8_t *buffer, size_t capacity) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700181 //ALOGV("readSampleData");
182 sp<ABuffer> tmp = new ABuffer(buffer, capacity);
183 if (mData->mImpl->readSampleData(tmp) == OK) {
184 return tmp->size();
185 }
186 return -1;
187}
188
Marco Nelissen3425fd52014-05-14 11:12:46 -0700189EXPORT
Robert Shih30e3c7d2018-01-21 17:06:12 -0800190ssize_t AMediaExtractor_getSampleSize(AMediaExtractor *mData) {
191 size_t sampleSize;
192 status_t err = mData->mImpl->getSampleSize(&sampleSize);
193 if (err != OK) {
194 return -1;
195 }
196 return sampleSize;
197}
198
199EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700200uint32_t AMediaExtractor_getSampleFlags(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700201 int sampleFlags = 0;
202 sp<MetaData> meta;
203 status_t err = mData->mImpl->getSampleMeta(&meta);
204 if (err != OK) {
205 return -1;
206 }
207 int32_t val;
208 if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700209 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_SYNC;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700210 }
211
212 uint32_t type;
213 const void *data;
214 size_t size;
215 if (meta->findData(kKeyEncryptedSizes, &type, &data, &size)) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700216 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_ENCRYPTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700217 }
218 return sampleFlags;
219}
220
Marco Nelissen3425fd52014-05-14 11:12:46 -0700221EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700222int AMediaExtractor_getSampleTrackIndex(AMediaExtractor *mData) {
223 size_t idx;
224 if (mData->mImpl->getSampleTrackIndex(&idx) != OK) {
225 return -1;
226 }
227 return idx;
228}
229
Marco Nelissen3425fd52014-05-14 11:12:46 -0700230EXPORT
Marco Nelisseneb4860c2014-05-29 08:04:34 -0700231int64_t AMediaExtractor_getSampleTime(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700232 int64_t time;
233 if (mData->mImpl->getSampleTime(&time) != OK) {
234 return -1;
235 }
236 return time;
237}
238
Marco Nelissen3425fd52014-05-14 11:12:46 -0700239EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700240PsshInfo* AMediaExtractor_getPsshInfo(AMediaExtractor *ex) {
241
242 if (ex->mPsshBuf != NULL) {
243 return (PsshInfo*) ex->mPsshBuf->data();
244 }
245
246 sp<AMessage> format;
247 ex->mImpl->getFileFormat(&format);
248 sp<ABuffer> buffer;
249 if(!format->findBuffer("pssh", &buffer)) {
250 return NULL;
251 }
252
253 // the format of the buffer is 1 or more of:
254 // {
255 // 16 byte uuid
256 // 4 byte data length N
257 // N bytes of data
258 // }
259
260 // Determine the number of entries in the source data.
261 // Since we got the data from stagefright, we trust it is valid and properly formatted.
262 const uint8_t* data = buffer->data();
263 size_t len = buffer->size();
264 size_t numentries = 0;
265 while (len > 0) {
266 numentries++;
267
Marco Nelissen346bb512015-04-09 14:32:45 -0700268 if (len < 16) {
269 ALOGE("invalid PSSH data");
270 return NULL;
271 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700272 // skip uuid
273 data += 16;
274 len -= 16;
275
276 // get data length
Marco Nelissen346bb512015-04-09 14:32:45 -0700277 if (len < 4) {
278 ALOGE("invalid PSSH data");
279 return NULL;
280 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700281 uint32_t datalen = *((uint32_t*)data);
282 data += 4;
283 len -= 4;
284
Marco Nelissen346bb512015-04-09 14:32:45 -0700285 if (len < datalen) {
286 ALOGE("invalid PSSH data");
287 return NULL;
288 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700289 // skip the data
290 data += datalen;
291 len -= datalen;
292 }
293
Marco Nelissen58344bc2014-10-23 11:36:38 -0700294 // there are <numentries> in the source buffer, we need
295 // (source buffer size) - (sizeof(uint32_t) * numentries) + sizeof(size_t)
296 // + ((sizeof(void*) + sizeof(size_t)) * numentries) bytes for the PsshInfo structure
297 // Or in other words, the data lengths in the source structure are replaced by size_t
298 // (which may be the same size or larger, for 64 bit), and in addition there is an
299 // extra pointer for each entry, and an extra size_t for the entire PsshInfo.
300 size_t newsize = buffer->size() - (sizeof(uint32_t) * numentries) + sizeof(size_t)
301 + ((sizeof(void*) + sizeof(size_t)) * numentries);
Marco Nelissen346bb512015-04-09 14:32:45 -0700302 if (newsize <= buffer->size()) {
303 ALOGE("invalid PSSH data");
304 return NULL;
305 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700306 ex->mPsshBuf = new ABuffer(newsize);
307 ex->mPsshBuf->setRange(0, newsize);
308
309 // copy data
310 const uint8_t* src = buffer->data();
311 uint8_t* dst = ex->mPsshBuf->data();
Marco Nelissen58344bc2014-10-23 11:36:38 -0700312 uint8_t* dstdata = dst + sizeof(size_t) + numentries * sizeof(PsshEntry);
313 *((size_t*)dst) = numentries;
314 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700315 for (size_t i = 0; i < numentries; i++) {
316 // copy uuid
317 memcpy(dst, src, 16);
318 src += 16;
319 dst += 16;
320
321 // get/copy data length
322 uint32_t datalen = *((uint32_t*)src);
Marco Nelissen58344bc2014-10-23 11:36:38 -0700323 *((size_t*)dst) = datalen;
324 src += sizeof(uint32_t);
325 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700326
327 // the next entry in the destination is a pointer to the actual data, which we store
328 // after the array of PsshEntry
Marco Nelissen58344bc2014-10-23 11:36:38 -0700329 *((void**)dst) = dstdata;
330 dst += sizeof(void*);
Marco Nelissen050eb322014-05-09 15:10:23 -0700331
332 // copy the actual data
333 memcpy(dstdata, src, datalen);
334 dstdata += datalen;
335 src += datalen;
336 }
337
338 return (PsshInfo*) ex->mPsshBuf->data();
339}
340
Marco Nelissen3425fd52014-05-14 11:12:46 -0700341EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700342AMediaCodecCryptoInfo *AMediaExtractor_getSampleCryptoInfo(AMediaExtractor *ex) {
343 sp<MetaData> meta;
344 if(ex->mImpl->getSampleMeta(&meta) != 0) {
345 return NULL;
346 }
347
348 uint32_t type;
349 const void *crypteddata;
350 size_t cryptedsize;
351 if (!meta->findData(kKeyEncryptedSizes, &type, &crypteddata, &cryptedsize)) {
352 return NULL;
353 }
354 size_t numSubSamples = cryptedsize / sizeof(size_t);
355
356 const void *cleardata;
357 size_t clearsize;
358 if (meta->findData(kKeyPlainSizes, &type, &cleardata, &clearsize)) {
359 if (clearsize != cryptedsize) {
360 // The two must be of the same length.
361 return NULL;
362 }
363 }
364
365 const void *key;
366 size_t keysize;
Edwin Wongb2fb3c92016-08-29 14:57:47 -0700367 if (meta->findData(kKeyCryptoKey, &type, &key, &keysize)) {
Marco Nelissen050eb322014-05-09 15:10:23 -0700368 if (keysize != 16) {
Edwin Wongb2fb3c92016-08-29 14:57:47 -0700369 // Keys must be 16 bytes in length.
Marco Nelissen050eb322014-05-09 15:10:23 -0700370 return NULL;
371 }
372 }
373
374 const void *iv;
375 size_t ivsize;
376 if (meta->findData(kKeyCryptoIV, &type, &iv, &ivsize)) {
377 if (ivsize != 16) {
378 // IVs must be 16 bytes in length.
379 return NULL;
380 }
381 }
382
383 int32_t mode;
384 if (!meta->findInt32(kKeyCryptoMode, &mode)) {
385 mode = CryptoPlugin::kMode_AES_CTR;
386 }
387
388 return AMediaCodecCryptoInfo_new(
389 numSubSamples,
390 (uint8_t*) key,
391 (uint8_t*) iv,
Marco Nelissen79e2b622014-05-16 08:07:28 -0700392 (cryptoinfo_mode_t) mode,
Marco Nelissen050eb322014-05-09 15:10:23 -0700393 (size_t*) cleardata,
394 (size_t*) crypteddata);
395}
396
Robert Shih30e3c7d2018-01-21 17:06:12 -0800397EXPORT
398int64_t AMediaExtractor_getCachedDuration(AMediaExtractor *ex) {
399 bool eos;
400 int64_t durationUs;
401 if (ex->mImpl->getCachedDuration(&durationUs, &eos)) {
402 return durationUs;
403 }
404 return -1;
405}
Marco Nelissen0c3be872014-05-01 10:14:44 -0700406
Robert Shihd83d4f42018-02-24 19:02:46 -0800407EXPORT
408media_status_t AMediaExtractor_getSampleFormat(AMediaExtractor *ex, AMediaFormat *fmt) {
409 if (fmt == NULL) {
410 return AMEDIA_ERROR_INVALID_PARAMETER;
411 }
412
413 sp<MetaData> sampleMeta;
414 status_t err = ex->mImpl->getSampleMeta(&sampleMeta);
415 if (err != OK) {
416 return translate_error(err);
417 }
418
419 sp<AMessage> meta;
420 AMediaFormat_getFormat(fmt, &meta);
421 meta->clear();
422
423 int32_t layerId;
424 if (sampleMeta->findInt32(kKeyTemporalLayerId, &layerId)) {
425 meta->setInt32(AMEDIAFORMAT_KEY_TEMPORAL_LAYER_ID, layerId);
426 }
427
428 size_t trackIndex;
429 err = ex->mImpl->getSampleTrackIndex(&trackIndex);
430 if (err == OK) {
431 meta->setInt32(AMEDIAFORMAT_KEY_TRACK_INDEX, trackIndex);
432 sp<AMessage> trackFormat;
433 AString mime;
434 err = ex->mImpl->getTrackFormat(trackIndex, &trackFormat);
435 if (err == OK
436 && trackFormat != NULL
437 && trackFormat->findString(AMEDIAFORMAT_KEY_MIME, &mime)) {
438 meta->setString(AMEDIAFORMAT_KEY_MIME, mime);
439 }
440 }
441
442 int64_t durationUs;
443 if (sampleMeta->findInt64(kKeyDuration, &durationUs)) {
444 meta->setInt64(AMEDIAFORMAT_KEY_DURATION, durationUs);
445 }
446
447 uint32_t dataType; // unused
448 const void *seiData;
449 size_t seiLength;
450 if (sampleMeta->findData(kKeySEI, &dataType, &seiData, &seiLength)) {
451 sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);;
452 meta->setBuffer(AMEDIAFORMAT_KEY_SEI, sei);
453 }
454
455 const void *mpegUserDataPointer;
456 size_t mpegUserDataLength;
457 if (sampleMeta->findData(
458 kKeyMpegUserData, &dataType, &mpegUserDataPointer, &mpegUserDataLength)) {
459 sp<ABuffer> mpegUserData = ABuffer::CreateAsCopy(mpegUserDataPointer, mpegUserDataLength);
460 meta->setBuffer(AMEDIAFORMAT_KEY_MPEG_USER_DATA, mpegUserData);
461 }
462
463 return AMEDIA_OK;
464}
465
Marco Nelissen0c3be872014-05-01 10:14:44 -0700466} // extern "C"
467