blob: 386e42c28d7b715d16b242655ff812a47c5a1fae [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>
Santiago Seifert79c34f22020-11-11 23:49:28 +000025#include "NdkJavaVMHelperPriv.h"
Robert Shih0df451b2017-12-08 14:16:50 -080026#include "NdkMediaDataSourcePriv.h"
Marco Nelissen0c3be872014-05-01 10:14:44 -070027
28
Aurimas Liutikas214c8332016-02-19 14:48:23 -080029#include <inttypes.h>
Marco Nelissen0c3be872014-05-01 10:14:44 -070030#include <utils/Log.h>
31#include <utils/StrongPointer.h>
Marco Nelissen050eb322014-05-09 15:10:23 -070032#include <media/hardware/CryptoAPI.h>
Marco Nelissen0c3be872014-05-01 10:14:44 -070033#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 Nelissen0c3be872014-05-01 10:14:44 -070038#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
Robert Shih0b4530d2019-01-18 12:12:03 -080049sp<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 Nelissen0c3be872014-05-01 10:14:44 -070061extern "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();
Santiago Seifert79c34f22020-11-11 23:49:28 +000067 mData->mImpl = new NuMediaExtractor(
68 NdkJavaVMHelper::getJNIEnv() != nullptr
69 ? NuMediaExtractor::EntryPoint::NDK_WITH_JVM
70 : NuMediaExtractor::EntryPoint::NDK_NO_JVM );
Marco Nelissen0c3be872014-05-01 10:14:44 -070071 return mData;
72}
73
Marco Nelissen3425fd52014-05-14 11:12:46 -070074EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -070075media_status_t AMediaExtractor_delete(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -070076 ALOGV("dtor");
77 delete mData;
Marco Nelissene419d7c2014-05-15 14:17:25 -070078 return AMEDIA_OK;
Marco Nelissen0c3be872014-05-01 10:14:44 -070079}
80
Marco Nelissen3425fd52014-05-14 11:12:46 -070081EXPORT
Glenn Kastenb187de12014-12-30 08:18:15 -080082media_status_t AMediaExtractor_setDataSourceFd(AMediaExtractor *mData, int fd, off64_t offset,
83 off64_t length) {
Aurimas Liutikas214c8332016-02-19 14:48:23 -080084 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
Marco Nelissene419d7c2014-05-15 14:17:25 -070085 return translate_error(mData->mImpl->setDataSource(fd, offset, length));
Marco Nelissen0c3be872014-05-01 10:14:44 -070086}
87
Robert Shih6a59e4a2018-08-30 13:31:28 -070088media_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 Nelissen0c3be872014-05-01 10:14:44 -070095
Marco Nelissena5232042019-09-24 09:27:40 -070096 sp<MediaHTTPService> httpService = createMediaHttpService(uri);
Robert Shih730af222018-09-14 14:02:57 -070097 if (httpService == NULL) {
98 ALOGE("can't create http service");
Marco Nelissene419d7c2014-05-15 14:17:25 -070099 return AMEDIA_ERROR_UNSUPPORTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700100 }
101
Robert Shih6a59e4a2018-08-30 13:31:28 -0700102 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 Nelissene419d7c2014-05-15 14:17:25 -0700111 return translate_error(err);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700112}
113
Marco Nelissen3425fd52014-05-14 11:12:46 -0700114EXPORT
Robert Shih82248d12018-10-03 15:57:47 -0700115media_status_t AMediaExtractor_setDataSource(AMediaExtractor *mData, const char *location) {
116 return AMediaExtractor_setDataSourceWithHeaders(mData, location, 0, NULL, NULL);
117}
118
119EXPORT
Robert Shih0df451b2017-12-08 14:16:50 -0800120media_status_t AMediaExtractor_setDataSourceCustom(AMediaExtractor* mData, AMediaDataSource *src) {
121 return translate_error(mData->mImpl->setDataSource(new NdkDataSource(src)));
122}
123
124EXPORT
Robert Shih30e3c7d2018-01-21 17:06:12 -0800125AMediaFormat* AMediaExtractor_getFileFormat(AMediaExtractor *mData) {
126 sp<AMessage> format;
127 mData->mImpl->getFileFormat(&format);
Ray Essickc4022792022-11-17 20:43:44 -0600128 // ignore any error, we want to return the empty format
Robert Shih30e3c7d2018-01-21 17:06:12 -0800129 return AMediaFormat_fromMsg(&format);
130}
131
132EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700133size_t AMediaExtractor_getTrackCount(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700134 return mData->mImpl->countTracks();
135}
136
Marco Nelissen3425fd52014-05-14 11:12:46 -0700137EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700138AMediaFormat* AMediaExtractor_getTrackFormat(AMediaExtractor *mData, size_t idx) {
139 sp<AMessage> format;
140 mData->mImpl->getTrackFormat(idx, &format);
141 return AMediaFormat_fromMsg(&format);
142}
143
Marco Nelissen3425fd52014-05-14 11:12:46 -0700144EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700145media_status_t AMediaExtractor_selectTrack(AMediaExtractor *mData, size_t idx) {
Mark Salyzyn98f28cd2014-06-18 16:32:50 -0700146 ALOGV("selectTrack(%zu)", idx);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700147 return translate_error(mData->mImpl->selectTrack(idx));
148}
149
Marco Nelissen3425fd52014-05-14 11:12:46 -0700150EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700151media_status_t AMediaExtractor_unselectTrack(AMediaExtractor *mData, size_t idx) {
Mark Salyzyn98f28cd2014-06-18 16:32:50 -0700152 ALOGV("unselectTrack(%zu)", idx);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700153 return translate_error(mData->mImpl->unselectTrack(idx));
154}
155
Marco Nelissen3425fd52014-05-14 11:12:46 -0700156EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700157bool AMediaExtractor_advance(AMediaExtractor *mData) {
158 //ALOGV("advance");
Robert Shih70452262016-09-16 16:43:49 -0700159 status_t err = mData->mImpl->advance();
160 if (err == ERROR_END_OF_STREAM) {
161 return false;
162 } else if (err != OK) {
163 ALOGE("sf error code: %d", err);
164 return false;
165 }
166 return true;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700167}
168
Marco Nelissen3425fd52014-05-14 11:12:46 -0700169EXPORT
Marco Nelissen79e2b622014-05-16 08:07:28 -0700170media_status_t AMediaExtractor_seekTo(AMediaExtractor *ex, int64_t seekPosUs, SeekMode mode) {
171 android::MediaSource::ReadOptions::SeekMode sfmode;
172 if (mode == AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC) {
173 sfmode = android::MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC;
174 } else if (mode == AMEDIAEXTRACTOR_SEEK_CLOSEST_SYNC) {
175 sfmode = android::MediaSource::ReadOptions::SEEK_CLOSEST_SYNC;
176 } else {
177 sfmode = android::MediaSource::ReadOptions::SEEK_NEXT_SYNC;
178 }
179
180 return translate_error(ex->mImpl->seekTo(seekPosUs, sfmode));
181}
182
183EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700184ssize_t AMediaExtractor_readSampleData(AMediaExtractor *mData, uint8_t *buffer, size_t capacity) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700185 //ALOGV("readSampleData");
186 sp<ABuffer> tmp = new ABuffer(buffer, capacity);
187 if (mData->mImpl->readSampleData(tmp) == OK) {
188 return tmp->size();
189 }
190 return -1;
191}
192
Marco Nelissen3425fd52014-05-14 11:12:46 -0700193EXPORT
Robert Shih30e3c7d2018-01-21 17:06:12 -0800194ssize_t AMediaExtractor_getSampleSize(AMediaExtractor *mData) {
195 size_t sampleSize;
196 status_t err = mData->mImpl->getSampleSize(&sampleSize);
197 if (err != OK) {
198 return -1;
199 }
200 return sampleSize;
201}
202
203EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700204uint32_t AMediaExtractor_getSampleFlags(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700205 int sampleFlags = 0;
206 sp<MetaData> meta;
207 status_t err = mData->mImpl->getSampleMeta(&meta);
208 if (err != OK) {
209 return -1;
210 }
211 int32_t val;
212 if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700213 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_SYNC;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700214 }
215
216 uint32_t type;
217 const void *data;
218 size_t size;
219 if (meta->findData(kKeyEncryptedSizes, &type, &data, &size)) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700220 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_ENCRYPTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700221 }
222 return sampleFlags;
223}
224
Marco Nelissen3425fd52014-05-14 11:12:46 -0700225EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700226int AMediaExtractor_getSampleTrackIndex(AMediaExtractor *mData) {
227 size_t idx;
228 if (mData->mImpl->getSampleTrackIndex(&idx) != OK) {
229 return -1;
230 }
231 return idx;
232}
233
Marco Nelissen3425fd52014-05-14 11:12:46 -0700234EXPORT
Marco Nelisseneb4860c2014-05-29 08:04:34 -0700235int64_t AMediaExtractor_getSampleTime(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700236 int64_t time;
237 if (mData->mImpl->getSampleTime(&time) != OK) {
238 return -1;
239 }
240 return time;
241}
242
Marco Nelissen3425fd52014-05-14 11:12:46 -0700243EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700244PsshInfo* AMediaExtractor_getPsshInfo(AMediaExtractor *ex) {
245
246 if (ex->mPsshBuf != NULL) {
247 return (PsshInfo*) ex->mPsshBuf->data();
248 }
249
250 sp<AMessage> format;
Ray Essickc4022792022-11-17 20:43:44 -0600251 if (ex->mImpl->getFileFormat(&format) != OK) {
252 android_errorWriteWithInfoLog(0x534e4554, "243222985", -1, nullptr, 0);
253 return NULL;
254 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700255 sp<ABuffer> buffer;
256 if(!format->findBuffer("pssh", &buffer)) {
257 return NULL;
258 }
259
260 // the format of the buffer is 1 or more of:
261 // {
262 // 16 byte uuid
263 // 4 byte data length N
264 // N bytes of data
265 // }
266
267 // Determine the number of entries in the source data.
268 // Since we got the data from stagefright, we trust it is valid and properly formatted.
269 const uint8_t* data = buffer->data();
270 size_t len = buffer->size();
271 size_t numentries = 0;
272 while (len > 0) {
273 numentries++;
274
Marco Nelissen346bb512015-04-09 14:32:45 -0700275 if (len < 16) {
276 ALOGE("invalid PSSH data");
277 return NULL;
278 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700279 // skip uuid
280 data += 16;
281 len -= 16;
282
283 // get data length
Marco Nelissen346bb512015-04-09 14:32:45 -0700284 if (len < 4) {
285 ALOGE("invalid PSSH data");
286 return NULL;
287 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700288 uint32_t datalen = *((uint32_t*)data);
289 data += 4;
290 len -= 4;
291
Marco Nelissen346bb512015-04-09 14:32:45 -0700292 if (len < datalen) {
293 ALOGE("invalid PSSH data");
294 return NULL;
295 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700296 // skip the data
297 data += datalen;
298 len -= datalen;
299 }
300
Marco Nelissen58344bc2014-10-23 11:36:38 -0700301 // there are <numentries> in the source buffer, we need
302 // (source buffer size) - (sizeof(uint32_t) * numentries) + sizeof(size_t)
303 // + ((sizeof(void*) + sizeof(size_t)) * numentries) bytes for the PsshInfo structure
304 // Or in other words, the data lengths in the source structure are replaced by size_t
305 // (which may be the same size or larger, for 64 bit), and in addition there is an
306 // extra pointer for each entry, and an extra size_t for the entire PsshInfo.
307 size_t newsize = buffer->size() - (sizeof(uint32_t) * numentries) + sizeof(size_t)
308 + ((sizeof(void*) + sizeof(size_t)) * numentries);
Marco Nelissen346bb512015-04-09 14:32:45 -0700309 if (newsize <= buffer->size()) {
310 ALOGE("invalid PSSH data");
311 return NULL;
312 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700313 ex->mPsshBuf = new ABuffer(newsize);
314 ex->mPsshBuf->setRange(0, newsize);
315
316 // copy data
317 const uint8_t* src = buffer->data();
318 uint8_t* dst = ex->mPsshBuf->data();
Marco Nelissen58344bc2014-10-23 11:36:38 -0700319 uint8_t* dstdata = dst + sizeof(size_t) + numentries * sizeof(PsshEntry);
320 *((size_t*)dst) = numentries;
321 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700322 for (size_t i = 0; i < numentries; i++) {
323 // copy uuid
324 memcpy(dst, src, 16);
325 src += 16;
326 dst += 16;
327
328 // get/copy data length
329 uint32_t datalen = *((uint32_t*)src);
Marco Nelissen58344bc2014-10-23 11:36:38 -0700330 *((size_t*)dst) = datalen;
331 src += sizeof(uint32_t);
332 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700333
334 // the next entry in the destination is a pointer to the actual data, which we store
335 // after the array of PsshEntry
Marco Nelissen58344bc2014-10-23 11:36:38 -0700336 *((void**)dst) = dstdata;
337 dst += sizeof(void*);
Marco Nelissen050eb322014-05-09 15:10:23 -0700338
339 // copy the actual data
340 memcpy(dstdata, src, datalen);
341 dstdata += datalen;
342 src += datalen;
343 }
344
345 return (PsshInfo*) ex->mPsshBuf->data();
346}
347
Marco Nelissen3425fd52014-05-14 11:12:46 -0700348EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700349AMediaCodecCryptoInfo *AMediaExtractor_getSampleCryptoInfo(AMediaExtractor *ex) {
350 sp<MetaData> meta;
351 if(ex->mImpl->getSampleMeta(&meta) != 0) {
352 return NULL;
353 }
354
355 uint32_t type;
356 const void *crypteddata;
357 size_t cryptedsize;
358 if (!meta->findData(kKeyEncryptedSizes, &type, &crypteddata, &cryptedsize)) {
359 return NULL;
360 }
Robert Shih0b4530d2019-01-18 12:12:03 -0800361 size_t numSubSamples = cryptedsize / sizeof(uint32_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700362
363 const void *cleardata;
364 size_t clearsize;
365 if (meta->findData(kKeyPlainSizes, &type, &cleardata, &clearsize)) {
366 if (clearsize != cryptedsize) {
367 // The two must be of the same length.
368 return NULL;
369 }
370 }
371
372 const void *key;
373 size_t keysize;
Edwin Wongb2fb3c92016-08-29 14:57:47 -0700374 if (meta->findData(kKeyCryptoKey, &type, &key, &keysize)) {
Marco Nelissen050eb322014-05-09 15:10:23 -0700375 if (keysize != 16) {
Edwin Wongb2fb3c92016-08-29 14:57:47 -0700376 // Keys must be 16 bytes in length.
Marco Nelissen050eb322014-05-09 15:10:23 -0700377 return NULL;
378 }
379 }
380
381 const void *iv;
382 size_t ivsize;
383 if (meta->findData(kKeyCryptoIV, &type, &iv, &ivsize)) {
384 if (ivsize != 16) {
385 // IVs must be 16 bytes in length.
386 return NULL;
387 }
388 }
389
390 int32_t mode;
391 if (!meta->findInt32(kKeyCryptoMode, &mode)) {
392 mode = CryptoPlugin::kMode_AES_CTR;
393 }
394
Robert Shih593a4bc2022-04-20 00:58:59 +0000395 sp<ABuffer> clearbuf;
396 sp<ABuffer> cryptedbuf;
Robert Shih0b4530d2019-01-18 12:12:03 -0800397 if (sizeof(uint32_t) != sizeof(size_t)) {
Robert Shih593a4bc2022-04-20 00:58:59 +0000398 clearbuf = U32ArrayToSizeBuf(numSubSamples, (uint32_t *)cleardata);
399 cryptedbuf = U32ArrayToSizeBuf(numSubSamples, (uint32_t *)crypteddata);
Robert Shih0b4530d2019-01-18 12:12:03 -0800400 cleardata = clearbuf == NULL ? NULL : clearbuf->data();
401 crypteddata = crypteddata == NULL ? NULL : cryptedbuf->data();
402 if(crypteddata == NULL || cleardata == NULL) {
403 return NULL;
404 }
405 }
406
Marco Nelissen050eb322014-05-09 15:10:23 -0700407 return AMediaCodecCryptoInfo_new(
408 numSubSamples,
409 (uint8_t*) key,
410 (uint8_t*) iv,
Marco Nelissen79e2b622014-05-16 08:07:28 -0700411 (cryptoinfo_mode_t) mode,
Marco Nelissen050eb322014-05-09 15:10:23 -0700412 (size_t*) cleardata,
413 (size_t*) crypteddata);
414}
415
Robert Shih30e3c7d2018-01-21 17:06:12 -0800416EXPORT
417int64_t AMediaExtractor_getCachedDuration(AMediaExtractor *ex) {
418 bool eos;
419 int64_t durationUs;
420 if (ex->mImpl->getCachedDuration(&durationUs, &eos)) {
421 return durationUs;
422 }
423 return -1;
424}
Marco Nelissen0c3be872014-05-01 10:14:44 -0700425
Robert Shihd83d4f42018-02-24 19:02:46 -0800426EXPORT
427media_status_t AMediaExtractor_getSampleFormat(AMediaExtractor *ex, AMediaFormat *fmt) {
Gopalakrishnan Nallasamy13957712021-03-29 10:36:15 -0700428 ALOGV("AMediaExtractor_getSampleFormat");
Robert Shihd83d4f42018-02-24 19:02:46 -0800429 if (fmt == NULL) {
430 return AMEDIA_ERROR_INVALID_PARAMETER;
431 }
432
433 sp<MetaData> sampleMeta;
434 status_t err = ex->mImpl->getSampleMeta(&sampleMeta);
435 if (err != OK) {
436 return translate_error(err);
437 }
Gopalakrishnan Nallasamy13957712021-03-29 10:36:15 -0700438#ifdef LOG_NDEBUG
439 sampleMeta->dumpToLog();
440#endif
Robert Shihd83d4f42018-02-24 19:02:46 -0800441
442 sp<AMessage> meta;
443 AMediaFormat_getFormat(fmt, &meta);
444 meta->clear();
445
446 int32_t layerId;
447 if (sampleMeta->findInt32(kKeyTemporalLayerId, &layerId)) {
448 meta->setInt32(AMEDIAFORMAT_KEY_TEMPORAL_LAYER_ID, layerId);
449 }
450
451 size_t trackIndex;
452 err = ex->mImpl->getSampleTrackIndex(&trackIndex);
453 if (err == OK) {
454 meta->setInt32(AMEDIAFORMAT_KEY_TRACK_INDEX, trackIndex);
455 sp<AMessage> trackFormat;
456 AString mime;
457 err = ex->mImpl->getTrackFormat(trackIndex, &trackFormat);
458 if (err == OK
459 && trackFormat != NULL
460 && trackFormat->findString(AMEDIAFORMAT_KEY_MIME, &mime)) {
461 meta->setString(AMEDIAFORMAT_KEY_MIME, mime);
462 }
463 }
464
465 int64_t durationUs;
466 if (sampleMeta->findInt64(kKeyDuration, &durationUs)) {
467 meta->setInt64(AMEDIAFORMAT_KEY_DURATION, durationUs);
468 }
469
470 uint32_t dataType; // unused
471 const void *seiData;
472 size_t seiLength;
473 if (sampleMeta->findData(kKeySEI, &dataType, &seiData, &seiLength)) {
474 sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);;
475 meta->setBuffer(AMEDIAFORMAT_KEY_SEI, sei);
476 }
477
478 const void *mpegUserDataPointer;
479 size_t mpegUserDataLength;
480 if (sampleMeta->findData(
481 kKeyMpegUserData, &dataType, &mpegUserDataPointer, &mpegUserDataLength)) {
482 sp<ABuffer> mpegUserData = ABuffer::CreateAsCopy(mpegUserDataPointer, mpegUserDataLength);
483 meta->setBuffer(AMEDIAFORMAT_KEY_MPEG_USER_DATA, mpegUserData);
484 }
485
Sampath Shettyc6148a62018-12-18 15:50:43 +1100486 const void *audioPresentationsPointer;
487 size_t audioPresentationsLength;
488 if (sampleMeta->findData(
489 kKeyAudioPresentationInfo, &dataType,
490 &audioPresentationsPointer, &audioPresentationsLength)) {
491 sp<ABuffer> audioPresentationsData = ABuffer::CreateAsCopy(
492 audioPresentationsPointer, audioPresentationsLength);
493 meta->setBuffer(AMEDIAFORMAT_KEY_AUDIO_PRESENTATION_INFO, audioPresentationsData);
494 }
495
Gopalakrishnan Nallasamy13957712021-03-29 10:36:15 -0700496 int64_t val64;
497 if (sampleMeta->findInt64(kKeySampleFileOffset, &val64)) {
498 meta->setInt64("sample-file-offset", val64);
499 ALOGV("SampleFileOffset Found");
500 }
501 if (sampleMeta->findInt64(kKeyLastSampleIndexInChunk, &val64)) {
502 meta->setInt64("last-sample-index-in-chunk" /*AMEDIAFORMAT_KEY_LAST_SAMPLE_INDEX_IN_CHUNK*/,
503 val64);
504 ALOGV("kKeyLastSampleIndexInChunk Found");
505 }
506
507 ALOGV("AMediaFormat_toString:%s", AMediaFormat_toString(fmt));
508
Robert Shihd83d4f42018-02-24 19:02:46 -0800509 return AMEDIA_OK;
510}
511
Marco Nelissen0c3be872014-05-01 10:14:44 -0700512} // extern "C"
513