blob: ff1c8a9db0bedaf4851bc2ec15a5202b11c27400 [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 Shiha303ec82018-09-19 16:29:43 -070024#include "NdkMediaErrorPriv.h"
Robert Shih0df451b2017-12-08 14:16:50 -080025#include "NdkMediaDataSourcePriv.h"
Marco Nelissen0c3be872014-05-01 10:14:44 -070026
27
Aurimas Liutikas214c8332016-02-19 14:48:23 -080028#include <inttypes.h>
Marco Nelissen0c3be872014-05-01 10:14:44 -070029#include <utils/Log.h>
30#include <utils/StrongPointer.h>
Marco Nelissen050eb322014-05-09 15:10:23 -070031#include <media/hardware/CryptoAPI.h>
Marco Nelissen0c3be872014-05-01 10:14:44 -070032#include <media/stagefright/foundation/ABuffer.h>
33#include <media/stagefright/foundation/AMessage.h>
34#include <media/stagefright/MetaData.h>
35#include <media/stagefright/NuMediaExtractor.h>
36#include <media/IMediaHTTPService.h>
37#include <android_runtime/AndroidRuntime.h>
38#include <android_util_Binder.h>
39
40#include <jni.h>
41
42using namespace android;
43
Marco Nelissen0c3be872014-05-01 10:14:44 -070044struct AMediaExtractor {
45 sp<NuMediaExtractor> mImpl;
Marco Nelissen050eb322014-05-09 15:10:23 -070046 sp<ABuffer> mPsshBuf;
Marco Nelissen0c3be872014-05-01 10:14:44 -070047};
48
49extern "C" {
50
Marco Nelissen3425fd52014-05-14 11:12:46 -070051EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -070052AMediaExtractor* AMediaExtractor_new() {
53 ALOGV("ctor");
54 AMediaExtractor *mData = new AMediaExtractor();
55 mData->mImpl = new NuMediaExtractor();
56 return mData;
57}
58
Marco Nelissen3425fd52014-05-14 11:12:46 -070059EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -070060media_status_t AMediaExtractor_delete(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -070061 ALOGV("dtor");
62 delete mData;
Marco Nelissene419d7c2014-05-15 14:17:25 -070063 return AMEDIA_OK;
Marco Nelissen0c3be872014-05-01 10:14:44 -070064}
65
Marco Nelissen3425fd52014-05-14 11:12:46 -070066EXPORT
Glenn Kastenb187de12014-12-30 08:18:15 -080067media_status_t AMediaExtractor_setDataSourceFd(AMediaExtractor *mData, int fd, off64_t offset,
68 off64_t length) {
Aurimas Liutikas214c8332016-02-19 14:48:23 -080069 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
Marco Nelissene419d7c2014-05-15 14:17:25 -070070 return translate_error(mData->mImpl->setDataSource(fd, offset, length));
Marco Nelissen0c3be872014-05-01 10:14:44 -070071}
72
Marco Nelissen3425fd52014-05-14 11:12:46 -070073EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -070074media_status_t AMediaExtractor_setDataSource(AMediaExtractor *mData, const char *location) {
Robert Shih6a59e4a2018-08-30 13:31:28 -070075 return AMediaExtractor_setDataSourceWithHeaders(mData, location, 0, NULL, NULL);
76}
77
78EXPORT
79media_status_t AMediaExtractor_setDataSourceWithHeaders(AMediaExtractor *mData,
80 const char *uri,
81 int numheaders,
82 const char * const *keys,
83 const char * const *values) {
84
85 ALOGV("setDataSource(%s)", uri);
Marco Nelissen0c3be872014-05-01 10:14:44 -070086
Robert Shih730af222018-09-14 14:02:57 -070087 sp<MediaHTTPService> httpService = createMediaHttpService(uri, /* version = */ 1);
88 if (httpService == NULL) {
89 ALOGE("can't create http service");
Marco Nelissene419d7c2014-05-15 14:17:25 -070090 return AMEDIA_ERROR_UNSUPPORTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -070091 }
92
Robert Shih6a59e4a2018-08-30 13:31:28 -070093 KeyedVector<String8, String8> headers;
94 for (int i = 0; i < numheaders; ++i) {
95 String8 key8(keys[i]);
96 String8 value8(values[i]);
97 headers.add(key8, value8);
98 }
99
100 status_t err;
101 err = mData->mImpl->setDataSource(httpService, uri, numheaders > 0 ? &headers : NULL);
Marco Nelissene419d7c2014-05-15 14:17:25 -0700102 return translate_error(err);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700103}
104
Marco Nelissen3425fd52014-05-14 11:12:46 -0700105EXPORT
Robert Shih0df451b2017-12-08 14:16:50 -0800106media_status_t AMediaExtractor_setDataSourceCustom(AMediaExtractor* mData, AMediaDataSource *src) {
107 return translate_error(mData->mImpl->setDataSource(new NdkDataSource(src)));
108}
109
110EXPORT
Robert Shih30e3c7d2018-01-21 17:06:12 -0800111AMediaFormat* AMediaExtractor_getFileFormat(AMediaExtractor *mData) {
112 sp<AMessage> format;
113 mData->mImpl->getFileFormat(&format);
114 return AMediaFormat_fromMsg(&format);
115}
116
117EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700118size_t AMediaExtractor_getTrackCount(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700119 return mData->mImpl->countTracks();
120}
121
Marco Nelissen3425fd52014-05-14 11:12:46 -0700122EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700123AMediaFormat* AMediaExtractor_getTrackFormat(AMediaExtractor *mData, size_t idx) {
124 sp<AMessage> format;
125 mData->mImpl->getTrackFormat(idx, &format);
126 return AMediaFormat_fromMsg(&format);
127}
128
Marco Nelissen3425fd52014-05-14 11:12:46 -0700129EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700130media_status_t AMediaExtractor_selectTrack(AMediaExtractor *mData, size_t idx) {
Mark Salyzyn98f28cd2014-06-18 16:32:50 -0700131 ALOGV("selectTrack(%zu)", idx);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700132 return translate_error(mData->mImpl->selectTrack(idx));
133}
134
Marco Nelissen3425fd52014-05-14 11:12:46 -0700135EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700136media_status_t AMediaExtractor_unselectTrack(AMediaExtractor *mData, size_t idx) {
Mark Salyzyn98f28cd2014-06-18 16:32:50 -0700137 ALOGV("unselectTrack(%zu)", idx);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700138 return translate_error(mData->mImpl->unselectTrack(idx));
139}
140
Marco Nelissen3425fd52014-05-14 11:12:46 -0700141EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700142bool AMediaExtractor_advance(AMediaExtractor *mData) {
143 //ALOGV("advance");
Robert Shih70452262016-09-16 16:43:49 -0700144 status_t err = mData->mImpl->advance();
145 if (err == ERROR_END_OF_STREAM) {
146 return false;
147 } else if (err != OK) {
148 ALOGE("sf error code: %d", err);
149 return false;
150 }
151 return true;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700152}
153
Marco Nelissen3425fd52014-05-14 11:12:46 -0700154EXPORT
Marco Nelissen79e2b622014-05-16 08:07:28 -0700155media_status_t AMediaExtractor_seekTo(AMediaExtractor *ex, int64_t seekPosUs, SeekMode mode) {
156 android::MediaSource::ReadOptions::SeekMode sfmode;
157 if (mode == AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC) {
158 sfmode = android::MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC;
159 } else if (mode == AMEDIAEXTRACTOR_SEEK_CLOSEST_SYNC) {
160 sfmode = android::MediaSource::ReadOptions::SEEK_CLOSEST_SYNC;
161 } else {
162 sfmode = android::MediaSource::ReadOptions::SEEK_NEXT_SYNC;
163 }
164
165 return translate_error(ex->mImpl->seekTo(seekPosUs, sfmode));
166}
167
168EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700169ssize_t AMediaExtractor_readSampleData(AMediaExtractor *mData, uint8_t *buffer, size_t capacity) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700170 //ALOGV("readSampleData");
171 sp<ABuffer> tmp = new ABuffer(buffer, capacity);
172 if (mData->mImpl->readSampleData(tmp) == OK) {
173 return tmp->size();
174 }
175 return -1;
176}
177
Marco Nelissen3425fd52014-05-14 11:12:46 -0700178EXPORT
Robert Shih30e3c7d2018-01-21 17:06:12 -0800179ssize_t AMediaExtractor_getSampleSize(AMediaExtractor *mData) {
180 size_t sampleSize;
181 status_t err = mData->mImpl->getSampleSize(&sampleSize);
182 if (err != OK) {
183 return -1;
184 }
185 return sampleSize;
186}
187
188EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700189uint32_t AMediaExtractor_getSampleFlags(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700190 int sampleFlags = 0;
191 sp<MetaData> meta;
192 status_t err = mData->mImpl->getSampleMeta(&meta);
193 if (err != OK) {
194 return -1;
195 }
196 int32_t val;
197 if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700198 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_SYNC;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700199 }
200
201 uint32_t type;
202 const void *data;
203 size_t size;
204 if (meta->findData(kKeyEncryptedSizes, &type, &data, &size)) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700205 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_ENCRYPTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700206 }
207 return sampleFlags;
208}
209
Marco Nelissen3425fd52014-05-14 11:12:46 -0700210EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700211int AMediaExtractor_getSampleTrackIndex(AMediaExtractor *mData) {
212 size_t idx;
213 if (mData->mImpl->getSampleTrackIndex(&idx) != OK) {
214 return -1;
215 }
216 return idx;
217}
218
Marco Nelissen3425fd52014-05-14 11:12:46 -0700219EXPORT
Marco Nelisseneb4860c2014-05-29 08:04:34 -0700220int64_t AMediaExtractor_getSampleTime(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700221 int64_t time;
222 if (mData->mImpl->getSampleTime(&time) != OK) {
223 return -1;
224 }
225 return time;
226}
227
Marco Nelissen3425fd52014-05-14 11:12:46 -0700228EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700229PsshInfo* AMediaExtractor_getPsshInfo(AMediaExtractor *ex) {
230
231 if (ex->mPsshBuf != NULL) {
232 return (PsshInfo*) ex->mPsshBuf->data();
233 }
234
235 sp<AMessage> format;
236 ex->mImpl->getFileFormat(&format);
237 sp<ABuffer> buffer;
238 if(!format->findBuffer("pssh", &buffer)) {
239 return NULL;
240 }
241
242 // the format of the buffer is 1 or more of:
243 // {
244 // 16 byte uuid
245 // 4 byte data length N
246 // N bytes of data
247 // }
248
249 // Determine the number of entries in the source data.
250 // Since we got the data from stagefright, we trust it is valid and properly formatted.
251 const uint8_t* data = buffer->data();
252 size_t len = buffer->size();
253 size_t numentries = 0;
254 while (len > 0) {
255 numentries++;
256
Marco Nelissen346bb512015-04-09 14:32:45 -0700257 if (len < 16) {
258 ALOGE("invalid PSSH data");
259 return NULL;
260 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700261 // skip uuid
262 data += 16;
263 len -= 16;
264
265 // get data length
Marco Nelissen346bb512015-04-09 14:32:45 -0700266 if (len < 4) {
267 ALOGE("invalid PSSH data");
268 return NULL;
269 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700270 uint32_t datalen = *((uint32_t*)data);
271 data += 4;
272 len -= 4;
273
Marco Nelissen346bb512015-04-09 14:32:45 -0700274 if (len < datalen) {
275 ALOGE("invalid PSSH data");
276 return NULL;
277 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700278 // skip the data
279 data += datalen;
280 len -= datalen;
281 }
282
Marco Nelissen58344bc2014-10-23 11:36:38 -0700283 // there are <numentries> in the source buffer, we need
284 // (source buffer size) - (sizeof(uint32_t) * numentries) + sizeof(size_t)
285 // + ((sizeof(void*) + sizeof(size_t)) * numentries) bytes for the PsshInfo structure
286 // Or in other words, the data lengths in the source structure are replaced by size_t
287 // (which may be the same size or larger, for 64 bit), and in addition there is an
288 // extra pointer for each entry, and an extra size_t for the entire PsshInfo.
289 size_t newsize = buffer->size() - (sizeof(uint32_t) * numentries) + sizeof(size_t)
290 + ((sizeof(void*) + sizeof(size_t)) * numentries);
Marco Nelissen346bb512015-04-09 14:32:45 -0700291 if (newsize <= buffer->size()) {
292 ALOGE("invalid PSSH data");
293 return NULL;
294 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700295 ex->mPsshBuf = new ABuffer(newsize);
296 ex->mPsshBuf->setRange(0, newsize);
297
298 // copy data
299 const uint8_t* src = buffer->data();
300 uint8_t* dst = ex->mPsshBuf->data();
Marco Nelissen58344bc2014-10-23 11:36:38 -0700301 uint8_t* dstdata = dst + sizeof(size_t) + numentries * sizeof(PsshEntry);
302 *((size_t*)dst) = numentries;
303 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700304 for (size_t i = 0; i < numentries; i++) {
305 // copy uuid
306 memcpy(dst, src, 16);
307 src += 16;
308 dst += 16;
309
310 // get/copy data length
311 uint32_t datalen = *((uint32_t*)src);
Marco Nelissen58344bc2014-10-23 11:36:38 -0700312 *((size_t*)dst) = datalen;
313 src += sizeof(uint32_t);
314 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700315
316 // the next entry in the destination is a pointer to the actual data, which we store
317 // after the array of PsshEntry
Marco Nelissen58344bc2014-10-23 11:36:38 -0700318 *((void**)dst) = dstdata;
319 dst += sizeof(void*);
Marco Nelissen050eb322014-05-09 15:10:23 -0700320
321 // copy the actual data
322 memcpy(dstdata, src, datalen);
323 dstdata += datalen;
324 src += datalen;
325 }
326
327 return (PsshInfo*) ex->mPsshBuf->data();
328}
329
Marco Nelissen3425fd52014-05-14 11:12:46 -0700330EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700331AMediaCodecCryptoInfo *AMediaExtractor_getSampleCryptoInfo(AMediaExtractor *ex) {
332 sp<MetaData> meta;
333 if(ex->mImpl->getSampleMeta(&meta) != 0) {
334 return NULL;
335 }
336
337 uint32_t type;
338 const void *crypteddata;
339 size_t cryptedsize;
340 if (!meta->findData(kKeyEncryptedSizes, &type, &crypteddata, &cryptedsize)) {
341 return NULL;
342 }
343 size_t numSubSamples = cryptedsize / sizeof(size_t);
344
345 const void *cleardata;
346 size_t clearsize;
347 if (meta->findData(kKeyPlainSizes, &type, &cleardata, &clearsize)) {
348 if (clearsize != cryptedsize) {
349 // The two must be of the same length.
350 return NULL;
351 }
352 }
353
354 const void *key;
355 size_t keysize;
Edwin Wongb2fb3c92016-08-29 14:57:47 -0700356 if (meta->findData(kKeyCryptoKey, &type, &key, &keysize)) {
Marco Nelissen050eb322014-05-09 15:10:23 -0700357 if (keysize != 16) {
Edwin Wongb2fb3c92016-08-29 14:57:47 -0700358 // Keys must be 16 bytes in length.
Marco Nelissen050eb322014-05-09 15:10:23 -0700359 return NULL;
360 }
361 }
362
363 const void *iv;
364 size_t ivsize;
365 if (meta->findData(kKeyCryptoIV, &type, &iv, &ivsize)) {
366 if (ivsize != 16) {
367 // IVs must be 16 bytes in length.
368 return NULL;
369 }
370 }
371
372 int32_t mode;
373 if (!meta->findInt32(kKeyCryptoMode, &mode)) {
374 mode = CryptoPlugin::kMode_AES_CTR;
375 }
376
377 return AMediaCodecCryptoInfo_new(
378 numSubSamples,
379 (uint8_t*) key,
380 (uint8_t*) iv,
Marco Nelissen79e2b622014-05-16 08:07:28 -0700381 (cryptoinfo_mode_t) mode,
Marco Nelissen050eb322014-05-09 15:10:23 -0700382 (size_t*) cleardata,
383 (size_t*) crypteddata);
384}
385
Robert Shih30e3c7d2018-01-21 17:06:12 -0800386EXPORT
387int64_t AMediaExtractor_getCachedDuration(AMediaExtractor *ex) {
388 bool eos;
389 int64_t durationUs;
390 if (ex->mImpl->getCachedDuration(&durationUs, &eos)) {
391 return durationUs;
392 }
393 return -1;
394}
Marco Nelissen0c3be872014-05-01 10:14:44 -0700395
Robert Shihd83d4f42018-02-24 19:02:46 -0800396EXPORT
397media_status_t AMediaExtractor_getSampleFormat(AMediaExtractor *ex, AMediaFormat *fmt) {
398 if (fmt == NULL) {
399 return AMEDIA_ERROR_INVALID_PARAMETER;
400 }
401
402 sp<MetaData> sampleMeta;
403 status_t err = ex->mImpl->getSampleMeta(&sampleMeta);
404 if (err != OK) {
405 return translate_error(err);
406 }
407
408 sp<AMessage> meta;
409 AMediaFormat_getFormat(fmt, &meta);
410 meta->clear();
411
412 int32_t layerId;
413 if (sampleMeta->findInt32(kKeyTemporalLayerId, &layerId)) {
414 meta->setInt32(AMEDIAFORMAT_KEY_TEMPORAL_LAYER_ID, layerId);
415 }
416
417 size_t trackIndex;
418 err = ex->mImpl->getSampleTrackIndex(&trackIndex);
419 if (err == OK) {
420 meta->setInt32(AMEDIAFORMAT_KEY_TRACK_INDEX, trackIndex);
421 sp<AMessage> trackFormat;
422 AString mime;
423 err = ex->mImpl->getTrackFormat(trackIndex, &trackFormat);
424 if (err == OK
425 && trackFormat != NULL
426 && trackFormat->findString(AMEDIAFORMAT_KEY_MIME, &mime)) {
427 meta->setString(AMEDIAFORMAT_KEY_MIME, mime);
428 }
429 }
430
431 int64_t durationUs;
432 if (sampleMeta->findInt64(kKeyDuration, &durationUs)) {
433 meta->setInt64(AMEDIAFORMAT_KEY_DURATION, durationUs);
434 }
435
436 uint32_t dataType; // unused
437 const void *seiData;
438 size_t seiLength;
439 if (sampleMeta->findData(kKeySEI, &dataType, &seiData, &seiLength)) {
440 sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);;
441 meta->setBuffer(AMEDIAFORMAT_KEY_SEI, sei);
442 }
443
444 const void *mpegUserDataPointer;
445 size_t mpegUserDataLength;
446 if (sampleMeta->findData(
447 kKeyMpegUserData, &dataType, &mpegUserDataPointer, &mpegUserDataLength)) {
448 sp<ABuffer> mpegUserData = ABuffer::CreateAsCopy(mpegUserDataPointer, mpegUserDataLength);
449 meta->setBuffer(AMEDIAFORMAT_KEY_MPEG_USER_DATA, mpegUserData);
450 }
451
452 return AMEDIA_OK;
453}
454
Marco Nelissen0c3be872014-05-01 10:14:44 -0700455} // extern "C"
456