blob: f697bd1af46449f3ba3782aa92b6c093d57fb279 [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 Nelissen5dcf85a2018-10-11 09:49:02 -070023#include <media/NdkMediaErrorPriv.h>
Marco Nelissen98603d82018-07-17 11:06:55 -070024#include <media/NdkMediaFormatPriv.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
Robert Shih6a59e4a2018-08-30 13:31:28 -070073media_status_t AMediaExtractor_setDataSourceWithHeaders(AMediaExtractor *mData,
74 const char *uri,
75 int numheaders,
76 const char * const *keys,
77 const char * const *values) {
78
79 ALOGV("setDataSource(%s)", uri);
Marco Nelissen0c3be872014-05-01 10:14:44 -070080
Robert Shih730af222018-09-14 14:02:57 -070081 sp<MediaHTTPService> httpService = createMediaHttpService(uri, /* version = */ 1);
82 if (httpService == NULL) {
83 ALOGE("can't create http service");
Marco Nelissene419d7c2014-05-15 14:17:25 -070084 return AMEDIA_ERROR_UNSUPPORTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -070085 }
86
Robert Shih6a59e4a2018-08-30 13:31:28 -070087 KeyedVector<String8, String8> headers;
88 for (int i = 0; i < numheaders; ++i) {
89 String8 key8(keys[i]);
90 String8 value8(values[i]);
91 headers.add(key8, value8);
92 }
93
94 status_t err;
95 err = mData->mImpl->setDataSource(httpService, uri, numheaders > 0 ? &headers : NULL);
Marco Nelissene419d7c2014-05-15 14:17:25 -070096 return translate_error(err);
Marco Nelissen0c3be872014-05-01 10:14:44 -070097}
98
Marco Nelissen3425fd52014-05-14 11:12:46 -070099EXPORT
Robert Shih82248d12018-10-03 15:57:47 -0700100media_status_t AMediaExtractor_setDataSource(AMediaExtractor *mData, const char *location) {
101 return AMediaExtractor_setDataSourceWithHeaders(mData, location, 0, NULL, NULL);
102}
103
104EXPORT
Robert Shih0df451b2017-12-08 14:16:50 -0800105media_status_t AMediaExtractor_setDataSourceCustom(AMediaExtractor* mData, AMediaDataSource *src) {
106 return translate_error(mData->mImpl->setDataSource(new NdkDataSource(src)));
107}
108
109EXPORT
Robert Shih30e3c7d2018-01-21 17:06:12 -0800110AMediaFormat* AMediaExtractor_getFileFormat(AMediaExtractor *mData) {
111 sp<AMessage> format;
112 mData->mImpl->getFileFormat(&format);
113 return AMediaFormat_fromMsg(&format);
114}
115
116EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700117size_t AMediaExtractor_getTrackCount(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700118 return mData->mImpl->countTracks();
119}
120
Marco Nelissen3425fd52014-05-14 11:12:46 -0700121EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700122AMediaFormat* AMediaExtractor_getTrackFormat(AMediaExtractor *mData, size_t idx) {
123 sp<AMessage> format;
124 mData->mImpl->getTrackFormat(idx, &format);
125 return AMediaFormat_fromMsg(&format);
126}
127
Marco Nelissen3425fd52014-05-14 11:12:46 -0700128EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700129media_status_t AMediaExtractor_selectTrack(AMediaExtractor *mData, size_t idx) {
Mark Salyzyn98f28cd2014-06-18 16:32:50 -0700130 ALOGV("selectTrack(%zu)", idx);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700131 return translate_error(mData->mImpl->selectTrack(idx));
132}
133
Marco Nelissen3425fd52014-05-14 11:12:46 -0700134EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700135media_status_t AMediaExtractor_unselectTrack(AMediaExtractor *mData, size_t idx) {
Mark Salyzyn98f28cd2014-06-18 16:32:50 -0700136 ALOGV("unselectTrack(%zu)", idx);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700137 return translate_error(mData->mImpl->unselectTrack(idx));
138}
139
Marco Nelissen3425fd52014-05-14 11:12:46 -0700140EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700141bool AMediaExtractor_advance(AMediaExtractor *mData) {
142 //ALOGV("advance");
Robert Shih70452262016-09-16 16:43:49 -0700143 status_t err = mData->mImpl->advance();
144 if (err == ERROR_END_OF_STREAM) {
145 return false;
146 } else if (err != OK) {
147 ALOGE("sf error code: %d", err);
148 return false;
149 }
150 return true;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700151}
152
Marco Nelissen3425fd52014-05-14 11:12:46 -0700153EXPORT
Marco Nelissen79e2b622014-05-16 08:07:28 -0700154media_status_t AMediaExtractor_seekTo(AMediaExtractor *ex, int64_t seekPosUs, SeekMode mode) {
155 android::MediaSource::ReadOptions::SeekMode sfmode;
156 if (mode == AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC) {
157 sfmode = android::MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC;
158 } else if (mode == AMEDIAEXTRACTOR_SEEK_CLOSEST_SYNC) {
159 sfmode = android::MediaSource::ReadOptions::SEEK_CLOSEST_SYNC;
160 } else {
161 sfmode = android::MediaSource::ReadOptions::SEEK_NEXT_SYNC;
162 }
163
164 return translate_error(ex->mImpl->seekTo(seekPosUs, sfmode));
165}
166
167EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700168ssize_t AMediaExtractor_readSampleData(AMediaExtractor *mData, uint8_t *buffer, size_t capacity) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700169 //ALOGV("readSampleData");
170 sp<ABuffer> tmp = new ABuffer(buffer, capacity);
171 if (mData->mImpl->readSampleData(tmp) == OK) {
172 return tmp->size();
173 }
174 return -1;
175}
176
Marco Nelissen3425fd52014-05-14 11:12:46 -0700177EXPORT
Robert Shih30e3c7d2018-01-21 17:06:12 -0800178ssize_t AMediaExtractor_getSampleSize(AMediaExtractor *mData) {
179 size_t sampleSize;
180 status_t err = mData->mImpl->getSampleSize(&sampleSize);
181 if (err != OK) {
182 return -1;
183 }
184 return sampleSize;
185}
186
187EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700188uint32_t AMediaExtractor_getSampleFlags(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700189 int sampleFlags = 0;
190 sp<MetaData> meta;
191 status_t err = mData->mImpl->getSampleMeta(&meta);
192 if (err != OK) {
193 return -1;
194 }
195 int32_t val;
196 if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700197 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_SYNC;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700198 }
199
200 uint32_t type;
201 const void *data;
202 size_t size;
203 if (meta->findData(kKeyEncryptedSizes, &type, &data, &size)) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700204 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_ENCRYPTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700205 }
206 return sampleFlags;
207}
208
Marco Nelissen3425fd52014-05-14 11:12:46 -0700209EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700210int AMediaExtractor_getSampleTrackIndex(AMediaExtractor *mData) {
211 size_t idx;
212 if (mData->mImpl->getSampleTrackIndex(&idx) != OK) {
213 return -1;
214 }
215 return idx;
216}
217
Marco Nelissen3425fd52014-05-14 11:12:46 -0700218EXPORT
Marco Nelisseneb4860c2014-05-29 08:04:34 -0700219int64_t AMediaExtractor_getSampleTime(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700220 int64_t time;
221 if (mData->mImpl->getSampleTime(&time) != OK) {
222 return -1;
223 }
224 return time;
225}
226
Marco Nelissen3425fd52014-05-14 11:12:46 -0700227EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700228PsshInfo* AMediaExtractor_getPsshInfo(AMediaExtractor *ex) {
229
230 if (ex->mPsshBuf != NULL) {
231 return (PsshInfo*) ex->mPsshBuf->data();
232 }
233
234 sp<AMessage> format;
235 ex->mImpl->getFileFormat(&format);
236 sp<ABuffer> buffer;
237 if(!format->findBuffer("pssh", &buffer)) {
238 return NULL;
239 }
240
241 // the format of the buffer is 1 or more of:
242 // {
243 // 16 byte uuid
244 // 4 byte data length N
245 // N bytes of data
246 // }
247
248 // Determine the number of entries in the source data.
249 // Since we got the data from stagefright, we trust it is valid and properly formatted.
250 const uint8_t* data = buffer->data();
251 size_t len = buffer->size();
252 size_t numentries = 0;
253 while (len > 0) {
254 numentries++;
255
Marco Nelissen346bb512015-04-09 14:32:45 -0700256 if (len < 16) {
257 ALOGE("invalid PSSH data");
258 return NULL;
259 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700260 // skip uuid
261 data += 16;
262 len -= 16;
263
264 // get data length
Marco Nelissen346bb512015-04-09 14:32:45 -0700265 if (len < 4) {
266 ALOGE("invalid PSSH data");
267 return NULL;
268 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700269 uint32_t datalen = *((uint32_t*)data);
270 data += 4;
271 len -= 4;
272
Marco Nelissen346bb512015-04-09 14:32:45 -0700273 if (len < datalen) {
274 ALOGE("invalid PSSH data");
275 return NULL;
276 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700277 // skip the data
278 data += datalen;
279 len -= datalen;
280 }
281
Marco Nelissen58344bc2014-10-23 11:36:38 -0700282 // there are <numentries> in the source buffer, we need
283 // (source buffer size) - (sizeof(uint32_t) * numentries) + sizeof(size_t)
284 // + ((sizeof(void*) + sizeof(size_t)) * numentries) bytes for the PsshInfo structure
285 // Or in other words, the data lengths in the source structure are replaced by size_t
286 // (which may be the same size or larger, for 64 bit), and in addition there is an
287 // extra pointer for each entry, and an extra size_t for the entire PsshInfo.
288 size_t newsize = buffer->size() - (sizeof(uint32_t) * numentries) + sizeof(size_t)
289 + ((sizeof(void*) + sizeof(size_t)) * numentries);
Marco Nelissen346bb512015-04-09 14:32:45 -0700290 if (newsize <= buffer->size()) {
291 ALOGE("invalid PSSH data");
292 return NULL;
293 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700294 ex->mPsshBuf = new ABuffer(newsize);
295 ex->mPsshBuf->setRange(0, newsize);
296
297 // copy data
298 const uint8_t* src = buffer->data();
299 uint8_t* dst = ex->mPsshBuf->data();
Marco Nelissen58344bc2014-10-23 11:36:38 -0700300 uint8_t* dstdata = dst + sizeof(size_t) + numentries * sizeof(PsshEntry);
301 *((size_t*)dst) = numentries;
302 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700303 for (size_t i = 0; i < numentries; i++) {
304 // copy uuid
305 memcpy(dst, src, 16);
306 src += 16;
307 dst += 16;
308
309 // get/copy data length
310 uint32_t datalen = *((uint32_t*)src);
Marco Nelissen58344bc2014-10-23 11:36:38 -0700311 *((size_t*)dst) = datalen;
312 src += sizeof(uint32_t);
313 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700314
315 // the next entry in the destination is a pointer to the actual data, which we store
316 // after the array of PsshEntry
Marco Nelissen58344bc2014-10-23 11:36:38 -0700317 *((void**)dst) = dstdata;
318 dst += sizeof(void*);
Marco Nelissen050eb322014-05-09 15:10:23 -0700319
320 // copy the actual data
321 memcpy(dstdata, src, datalen);
322 dstdata += datalen;
323 src += datalen;
324 }
325
326 return (PsshInfo*) ex->mPsshBuf->data();
327}
328
Marco Nelissen3425fd52014-05-14 11:12:46 -0700329EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700330AMediaCodecCryptoInfo *AMediaExtractor_getSampleCryptoInfo(AMediaExtractor *ex) {
331 sp<MetaData> meta;
332 if(ex->mImpl->getSampleMeta(&meta) != 0) {
333 return NULL;
334 }
335
336 uint32_t type;
337 const void *crypteddata;
338 size_t cryptedsize;
339 if (!meta->findData(kKeyEncryptedSizes, &type, &crypteddata, &cryptedsize)) {
340 return NULL;
341 }
342 size_t numSubSamples = cryptedsize / sizeof(size_t);
343
344 const void *cleardata;
345 size_t clearsize;
346 if (meta->findData(kKeyPlainSizes, &type, &cleardata, &clearsize)) {
347 if (clearsize != cryptedsize) {
348 // The two must be of the same length.
349 return NULL;
350 }
351 }
352
353 const void *key;
354 size_t keysize;
Edwin Wongb2fb3c92016-08-29 14:57:47 -0700355 if (meta->findData(kKeyCryptoKey, &type, &key, &keysize)) {
Marco Nelissen050eb322014-05-09 15:10:23 -0700356 if (keysize != 16) {
Edwin Wongb2fb3c92016-08-29 14:57:47 -0700357 // Keys must be 16 bytes in length.
Marco Nelissen050eb322014-05-09 15:10:23 -0700358 return NULL;
359 }
360 }
361
362 const void *iv;
363 size_t ivsize;
364 if (meta->findData(kKeyCryptoIV, &type, &iv, &ivsize)) {
365 if (ivsize != 16) {
366 // IVs must be 16 bytes in length.
367 return NULL;
368 }
369 }
370
371 int32_t mode;
372 if (!meta->findInt32(kKeyCryptoMode, &mode)) {
373 mode = CryptoPlugin::kMode_AES_CTR;
374 }
375
376 return AMediaCodecCryptoInfo_new(
377 numSubSamples,
378 (uint8_t*) key,
379 (uint8_t*) iv,
Marco Nelissen79e2b622014-05-16 08:07:28 -0700380 (cryptoinfo_mode_t) mode,
Marco Nelissen050eb322014-05-09 15:10:23 -0700381 (size_t*) cleardata,
382 (size_t*) crypteddata);
383}
384
Robert Shih30e3c7d2018-01-21 17:06:12 -0800385EXPORT
386int64_t AMediaExtractor_getCachedDuration(AMediaExtractor *ex) {
387 bool eos;
388 int64_t durationUs;
389 if (ex->mImpl->getCachedDuration(&durationUs, &eos)) {
390 return durationUs;
391 }
392 return -1;
393}
Marco Nelissen0c3be872014-05-01 10:14:44 -0700394
Robert Shihd83d4f42018-02-24 19:02:46 -0800395EXPORT
396media_status_t AMediaExtractor_getSampleFormat(AMediaExtractor *ex, AMediaFormat *fmt) {
397 if (fmt == NULL) {
398 return AMEDIA_ERROR_INVALID_PARAMETER;
399 }
400
401 sp<MetaData> sampleMeta;
402 status_t err = ex->mImpl->getSampleMeta(&sampleMeta);
403 if (err != OK) {
404 return translate_error(err);
405 }
406
407 sp<AMessage> meta;
408 AMediaFormat_getFormat(fmt, &meta);
409 meta->clear();
410
411 int32_t layerId;
412 if (sampleMeta->findInt32(kKeyTemporalLayerId, &layerId)) {
413 meta->setInt32(AMEDIAFORMAT_KEY_TEMPORAL_LAYER_ID, layerId);
414 }
415
416 size_t trackIndex;
417 err = ex->mImpl->getSampleTrackIndex(&trackIndex);
418 if (err == OK) {
419 meta->setInt32(AMEDIAFORMAT_KEY_TRACK_INDEX, trackIndex);
420 sp<AMessage> trackFormat;
421 AString mime;
422 err = ex->mImpl->getTrackFormat(trackIndex, &trackFormat);
423 if (err == OK
424 && trackFormat != NULL
425 && trackFormat->findString(AMEDIAFORMAT_KEY_MIME, &mime)) {
426 meta->setString(AMEDIAFORMAT_KEY_MIME, mime);
427 }
428 }
429
430 int64_t durationUs;
431 if (sampleMeta->findInt64(kKeyDuration, &durationUs)) {
432 meta->setInt64(AMEDIAFORMAT_KEY_DURATION, durationUs);
433 }
434
435 uint32_t dataType; // unused
436 const void *seiData;
437 size_t seiLength;
438 if (sampleMeta->findData(kKeySEI, &dataType, &seiData, &seiLength)) {
439 sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);;
440 meta->setBuffer(AMEDIAFORMAT_KEY_SEI, sei);
441 }
442
443 const void *mpegUserDataPointer;
444 size_t mpegUserDataLength;
445 if (sampleMeta->findData(
446 kKeyMpegUserData, &dataType, &mpegUserDataPointer, &mpegUserDataLength)) {
447 sp<ABuffer> mpegUserData = ABuffer::CreateAsCopy(mpegUserDataPointer, mpegUserDataLength);
448 meta->setBuffer(AMEDIAFORMAT_KEY_MPEG_USER_DATA, mpegUserData);
449 }
450
451 return AMEDIA_OK;
452}
453
Marco Nelissen0c3be872014-05-01 10:14:44 -0700454} // extern "C"
455