blob: 4be71ac9647408fd7e9fca902253c479832b0f1f [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>
Robert Shih0df451b2017-12-08 14:16:50 -080023#include "NdkMediaDataSourcePriv.h"
Marco Nelissen0c3be872014-05-01 10:14:44 -070024#include "NdkMediaFormatPriv.h"
25
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>
Marco Nelissen0c3be872014-05-01 10:14:44 -070036
37#include <jni.h>
38
Jooyung Hanef280832019-02-21 15:27:38 +090039#include <mutex> // std::call_once,once_flag
40#include <dlfcn.h> // dlopen
41
Marco Nelissen0c3be872014-05-01 10:14:44 -070042using namespace android;
43
Jooyung Hanef280832019-02-21 15:27:38 +090044// load libandroid_runtime.so lazily.
45// A vendor process may use libmediandk but should not depend on libandroid_runtime.
46// TODO(jooyung): remove duplicate (b/125550121)
47// frameworks/native/libs/binder/ndk/ibinder_jni.cpp
48namespace {
49
50typedef JNIEnv* (*getJNIEnv_t)();
51typedef sp<IBinder> (*ibinderForJavaObject_t)(JNIEnv* env, jobject obj);
52
53getJNIEnv_t getJNIEnv_;
54ibinderForJavaObject_t ibinderForJavaObject_;
55
56std::once_flag mLoadFlag;
57
58void load() {
59 std::call_once(mLoadFlag, []() {
60 void* handle = dlopen("libandroid_runtime.so", RTLD_LAZY);
61 if (handle == nullptr) {
62 ALOGE("Could not open libandroid_runtime.");
63 return;
64 }
65
66 getJNIEnv_ = reinterpret_cast<getJNIEnv_t>(
67 dlsym(handle, "_ZN7android14AndroidRuntime9getJNIEnvEv"));
68 if (getJNIEnv_ == nullptr) {
69 ALOGE("Could not find AndroidRuntime::getJNIEnv.");
70 // no return
71 }
72
73 ibinderForJavaObject_ = reinterpret_cast<ibinderForJavaObject_t>(
74 dlsym(handle, "_ZN7android20ibinderForJavaObjectEP7_JNIEnvP8_jobject"));
75 if (ibinderForJavaObject_ == nullptr) {
76 ALOGE("Could not find ibinderForJavaObject.");
77 // no return
78 }
79 });
80}
81
82JNIEnv* getJNIEnv() {
83 load();
84 if (getJNIEnv_ == nullptr) {
85 return nullptr;
86 }
87 return (getJNIEnv_)();
88}
89
90sp<IBinder> ibinderForJavaObject(JNIEnv* env, jobject obj) {
91 load();
92 if (ibinderForJavaObject_ == nullptr) {
93 return nullptr;
94 }
95 return (ibinderForJavaObject_)(env, obj);
96}
97
98} // namespace
99
Marco Nelissene419d7c2014-05-15 14:17:25 -0700100static media_status_t translate_error(status_t err) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700101 if (err == OK) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700102 return AMEDIA_OK;
Robert Shihd83d4f42018-02-24 19:02:46 -0800103 } else if (err == ERROR_END_OF_STREAM) {
104 return AMEDIA_ERROR_END_OF_STREAM;
105 } else if (err == ERROR_IO) {
106 return AMEDIA_ERROR_IO;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700107 }
Robert Shihd83d4f42018-02-24 19:02:46 -0800108
Marco Nelissen0c3be872014-05-01 10:14:44 -0700109 ALOGE("sf error code: %d", err);
Marco Nelissene419d7c2014-05-15 14:17:25 -0700110 return AMEDIA_ERROR_UNKNOWN;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700111}
112
113struct AMediaExtractor {
114 sp<NuMediaExtractor> mImpl;
Marco Nelissen050eb322014-05-09 15:10:23 -0700115 sp<ABuffer> mPsshBuf;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700116};
117
118extern "C" {
119
Marco Nelissen3425fd52014-05-14 11:12:46 -0700120EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700121AMediaExtractor* AMediaExtractor_new() {
122 ALOGV("ctor");
123 AMediaExtractor *mData = new AMediaExtractor();
124 mData->mImpl = new NuMediaExtractor();
125 return mData;
126}
127
Marco Nelissen3425fd52014-05-14 11:12:46 -0700128EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700129media_status_t AMediaExtractor_delete(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700130 ALOGV("dtor");
131 delete mData;
Marco Nelissene419d7c2014-05-15 14:17:25 -0700132 return AMEDIA_OK;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700133}
134
Marco Nelissen3425fd52014-05-14 11:12:46 -0700135EXPORT
Glenn Kastenb187de12014-12-30 08:18:15 -0800136media_status_t AMediaExtractor_setDataSourceFd(AMediaExtractor *mData, int fd, off64_t offset,
137 off64_t length) {
Aurimas Liutikas214c8332016-02-19 14:48:23 -0800138 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
Marco Nelissene419d7c2014-05-15 14:17:25 -0700139 return translate_error(mData->mImpl->setDataSource(fd, offset, length));
Marco Nelissen0c3be872014-05-01 10:14:44 -0700140}
141
Marco Nelissen3425fd52014-05-14 11:12:46 -0700142EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700143media_status_t AMediaExtractor_setDataSource(AMediaExtractor *mData, const char *location) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700144 ALOGV("setDataSource(%s)", location);
145 // TODO: add header support
146
Jooyung Hanef280832019-02-21 15:27:38 +0900147 JNIEnv *env = getJNIEnv();
Marco Nelissen0c3be872014-05-01 10:14:44 -0700148 jobject service = NULL;
149 if (env == NULL) {
150 ALOGE("setDataSource(path) must be called from Java thread");
Marco Nelissene419d7c2014-05-15 14:17:25 -0700151 return AMEDIA_ERROR_UNSUPPORTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700152 }
153
154 jclass mediahttpclass = env->FindClass("android/media/MediaHTTPService");
155 if (mediahttpclass == NULL) {
156 ALOGE("can't find MediaHttpService");
157 env->ExceptionClear();
Marco Nelissene419d7c2014-05-15 14:17:25 -0700158 return AMEDIA_ERROR_UNSUPPORTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700159 }
160
161 jmethodID mediaHttpCreateMethod = env->GetStaticMethodID(mediahttpclass,
162 "createHttpServiceBinderIfNecessary", "(Ljava/lang/String;)Landroid/os/IBinder;");
163 if (mediaHttpCreateMethod == NULL) {
164 ALOGE("can't find method");
165 env->ExceptionClear();
Marco Nelissene419d7c2014-05-15 14:17:25 -0700166 return AMEDIA_ERROR_UNSUPPORTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700167 }
168
169 jstring jloc = env->NewStringUTF(location);
170
171 service = env->CallStaticObjectMethod(mediahttpclass, mediaHttpCreateMethod, jloc);
172 env->DeleteLocalRef(jloc);
173
174 sp<IMediaHTTPService> httpService;
175 if (service != NULL) {
176 sp<IBinder> binder = ibinderForJavaObject(env, service);
177 httpService = interface_cast<IMediaHTTPService>(binder);
178 }
179
Marco Nelissene419d7c2014-05-15 14:17:25 -0700180 status_t err = mData->mImpl->setDataSource(httpService, location, NULL);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700181 env->ExceptionClear();
Marco Nelissene419d7c2014-05-15 14:17:25 -0700182 return translate_error(err);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700183}
184
Marco Nelissen3425fd52014-05-14 11:12:46 -0700185EXPORT
Robert Shih0df451b2017-12-08 14:16:50 -0800186media_status_t AMediaExtractor_setDataSourceCustom(AMediaExtractor* mData, AMediaDataSource *src) {
187 return translate_error(mData->mImpl->setDataSource(new NdkDataSource(src)));
188}
189
190EXPORT
Robert Shih30e3c7d2018-01-21 17:06:12 -0800191AMediaFormat* AMediaExtractor_getFileFormat(AMediaExtractor *mData) {
192 sp<AMessage> format;
193 mData->mImpl->getFileFormat(&format);
194 return AMediaFormat_fromMsg(&format);
195}
196
197EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700198size_t AMediaExtractor_getTrackCount(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700199 return mData->mImpl->countTracks();
200}
201
Marco Nelissen3425fd52014-05-14 11:12:46 -0700202EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700203AMediaFormat* AMediaExtractor_getTrackFormat(AMediaExtractor *mData, size_t idx) {
204 sp<AMessage> format;
205 mData->mImpl->getTrackFormat(idx, &format);
206 return AMediaFormat_fromMsg(&format);
207}
208
Marco Nelissen3425fd52014-05-14 11:12:46 -0700209EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700210media_status_t AMediaExtractor_selectTrack(AMediaExtractor *mData, size_t idx) {
Mark Salyzyn98f28cd2014-06-18 16:32:50 -0700211 ALOGV("selectTrack(%zu)", idx);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700212 return translate_error(mData->mImpl->selectTrack(idx));
213}
214
Marco Nelissen3425fd52014-05-14 11:12:46 -0700215EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700216media_status_t AMediaExtractor_unselectTrack(AMediaExtractor *mData, size_t idx) {
Mark Salyzyn98f28cd2014-06-18 16:32:50 -0700217 ALOGV("unselectTrack(%zu)", idx);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700218 return translate_error(mData->mImpl->unselectTrack(idx));
219}
220
Marco Nelissen3425fd52014-05-14 11:12:46 -0700221EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700222bool AMediaExtractor_advance(AMediaExtractor *mData) {
223 //ALOGV("advance");
Robert Shih70452262016-09-16 16:43:49 -0700224 status_t err = mData->mImpl->advance();
225 if (err == ERROR_END_OF_STREAM) {
226 return false;
227 } else if (err != OK) {
228 ALOGE("sf error code: %d", err);
229 return false;
230 }
231 return true;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700232}
233
Marco Nelissen3425fd52014-05-14 11:12:46 -0700234EXPORT
Marco Nelissen79e2b622014-05-16 08:07:28 -0700235media_status_t AMediaExtractor_seekTo(AMediaExtractor *ex, int64_t seekPosUs, SeekMode mode) {
236 android::MediaSource::ReadOptions::SeekMode sfmode;
237 if (mode == AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC) {
238 sfmode = android::MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC;
239 } else if (mode == AMEDIAEXTRACTOR_SEEK_CLOSEST_SYNC) {
240 sfmode = android::MediaSource::ReadOptions::SEEK_CLOSEST_SYNC;
241 } else {
242 sfmode = android::MediaSource::ReadOptions::SEEK_NEXT_SYNC;
243 }
244
245 return translate_error(ex->mImpl->seekTo(seekPosUs, sfmode));
246}
247
248EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700249ssize_t AMediaExtractor_readSampleData(AMediaExtractor *mData, uint8_t *buffer, size_t capacity) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700250 //ALOGV("readSampleData");
251 sp<ABuffer> tmp = new ABuffer(buffer, capacity);
252 if (mData->mImpl->readSampleData(tmp) == OK) {
253 return tmp->size();
254 }
255 return -1;
256}
257
Marco Nelissen3425fd52014-05-14 11:12:46 -0700258EXPORT
Robert Shih30e3c7d2018-01-21 17:06:12 -0800259ssize_t AMediaExtractor_getSampleSize(AMediaExtractor *mData) {
260 size_t sampleSize;
261 status_t err = mData->mImpl->getSampleSize(&sampleSize);
262 if (err != OK) {
263 return -1;
264 }
265 return sampleSize;
266}
267
268EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700269uint32_t AMediaExtractor_getSampleFlags(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700270 int sampleFlags = 0;
271 sp<MetaData> meta;
272 status_t err = mData->mImpl->getSampleMeta(&meta);
273 if (err != OK) {
274 return -1;
275 }
276 int32_t val;
277 if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700278 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_SYNC;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700279 }
280
281 uint32_t type;
282 const void *data;
283 size_t size;
284 if (meta->findData(kKeyEncryptedSizes, &type, &data, &size)) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700285 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_ENCRYPTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700286 }
287 return sampleFlags;
288}
289
Marco Nelissen3425fd52014-05-14 11:12:46 -0700290EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700291int AMediaExtractor_getSampleTrackIndex(AMediaExtractor *mData) {
292 size_t idx;
293 if (mData->mImpl->getSampleTrackIndex(&idx) != OK) {
294 return -1;
295 }
296 return idx;
297}
298
Marco Nelissen3425fd52014-05-14 11:12:46 -0700299EXPORT
Marco Nelisseneb4860c2014-05-29 08:04:34 -0700300int64_t AMediaExtractor_getSampleTime(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700301 int64_t time;
302 if (mData->mImpl->getSampleTime(&time) != OK) {
303 return -1;
304 }
305 return time;
306}
307
Marco Nelissen3425fd52014-05-14 11:12:46 -0700308EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700309PsshInfo* AMediaExtractor_getPsshInfo(AMediaExtractor *ex) {
310
311 if (ex->mPsshBuf != NULL) {
312 return (PsshInfo*) ex->mPsshBuf->data();
313 }
314
315 sp<AMessage> format;
316 ex->mImpl->getFileFormat(&format);
317 sp<ABuffer> buffer;
318 if(!format->findBuffer("pssh", &buffer)) {
319 return NULL;
320 }
321
322 // the format of the buffer is 1 or more of:
323 // {
324 // 16 byte uuid
325 // 4 byte data length N
326 // N bytes of data
327 // }
328
329 // Determine the number of entries in the source data.
330 // Since we got the data from stagefright, we trust it is valid and properly formatted.
331 const uint8_t* data = buffer->data();
332 size_t len = buffer->size();
333 size_t numentries = 0;
334 while (len > 0) {
335 numentries++;
336
Marco Nelissen346bb512015-04-09 14:32:45 -0700337 if (len < 16) {
338 ALOGE("invalid PSSH data");
339 return NULL;
340 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700341 // skip uuid
342 data += 16;
343 len -= 16;
344
345 // get data length
Marco Nelissen346bb512015-04-09 14:32:45 -0700346 if (len < 4) {
347 ALOGE("invalid PSSH data");
348 return NULL;
349 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700350 uint32_t datalen = *((uint32_t*)data);
351 data += 4;
352 len -= 4;
353
Marco Nelissen346bb512015-04-09 14:32:45 -0700354 if (len < datalen) {
355 ALOGE("invalid PSSH data");
356 return NULL;
357 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700358 // skip the data
359 data += datalen;
360 len -= datalen;
361 }
362
Marco Nelissen58344bc2014-10-23 11:36:38 -0700363 // there are <numentries> in the source buffer, we need
364 // (source buffer size) - (sizeof(uint32_t) * numentries) + sizeof(size_t)
365 // + ((sizeof(void*) + sizeof(size_t)) * numentries) bytes for the PsshInfo structure
366 // Or in other words, the data lengths in the source structure are replaced by size_t
367 // (which may be the same size or larger, for 64 bit), and in addition there is an
368 // extra pointer for each entry, and an extra size_t for the entire PsshInfo.
369 size_t newsize = buffer->size() - (sizeof(uint32_t) * numentries) + sizeof(size_t)
370 + ((sizeof(void*) + sizeof(size_t)) * numentries);
Marco Nelissen346bb512015-04-09 14:32:45 -0700371 if (newsize <= buffer->size()) {
372 ALOGE("invalid PSSH data");
373 return NULL;
374 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700375 ex->mPsshBuf = new ABuffer(newsize);
376 ex->mPsshBuf->setRange(0, newsize);
377
378 // copy data
379 const uint8_t* src = buffer->data();
380 uint8_t* dst = ex->mPsshBuf->data();
Marco Nelissen58344bc2014-10-23 11:36:38 -0700381 uint8_t* dstdata = dst + sizeof(size_t) + numentries * sizeof(PsshEntry);
382 *((size_t*)dst) = numentries;
383 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700384 for (size_t i = 0; i < numentries; i++) {
385 // copy uuid
386 memcpy(dst, src, 16);
387 src += 16;
388 dst += 16;
389
390 // get/copy data length
391 uint32_t datalen = *((uint32_t*)src);
Marco Nelissen58344bc2014-10-23 11:36:38 -0700392 *((size_t*)dst) = datalen;
393 src += sizeof(uint32_t);
394 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700395
396 // the next entry in the destination is a pointer to the actual data, which we store
397 // after the array of PsshEntry
Marco Nelissen58344bc2014-10-23 11:36:38 -0700398 *((void**)dst) = dstdata;
399 dst += sizeof(void*);
Marco Nelissen050eb322014-05-09 15:10:23 -0700400
401 // copy the actual data
402 memcpy(dstdata, src, datalen);
403 dstdata += datalen;
404 src += datalen;
405 }
406
407 return (PsshInfo*) ex->mPsshBuf->data();
408}
409
Marco Nelissen3425fd52014-05-14 11:12:46 -0700410EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700411AMediaCodecCryptoInfo *AMediaExtractor_getSampleCryptoInfo(AMediaExtractor *ex) {
412 sp<MetaData> meta;
413 if(ex->mImpl->getSampleMeta(&meta) != 0) {
414 return NULL;
415 }
416
417 uint32_t type;
418 const void *crypteddata;
419 size_t cryptedsize;
420 if (!meta->findData(kKeyEncryptedSizes, &type, &crypteddata, &cryptedsize)) {
421 return NULL;
422 }
423 size_t numSubSamples = cryptedsize / sizeof(size_t);
424
425 const void *cleardata;
426 size_t clearsize;
427 if (meta->findData(kKeyPlainSizes, &type, &cleardata, &clearsize)) {
428 if (clearsize != cryptedsize) {
429 // The two must be of the same length.
430 return NULL;
431 }
432 }
433
434 const void *key;
435 size_t keysize;
Edwin Wongb2fb3c92016-08-29 14:57:47 -0700436 if (meta->findData(kKeyCryptoKey, &type, &key, &keysize)) {
Marco Nelissen050eb322014-05-09 15:10:23 -0700437 if (keysize != 16) {
Edwin Wongb2fb3c92016-08-29 14:57:47 -0700438 // Keys must be 16 bytes in length.
Marco Nelissen050eb322014-05-09 15:10:23 -0700439 return NULL;
440 }
441 }
442
443 const void *iv;
444 size_t ivsize;
445 if (meta->findData(kKeyCryptoIV, &type, &iv, &ivsize)) {
446 if (ivsize != 16) {
447 // IVs must be 16 bytes in length.
448 return NULL;
449 }
450 }
451
452 int32_t mode;
453 if (!meta->findInt32(kKeyCryptoMode, &mode)) {
454 mode = CryptoPlugin::kMode_AES_CTR;
455 }
456
457 return AMediaCodecCryptoInfo_new(
458 numSubSamples,
459 (uint8_t*) key,
460 (uint8_t*) iv,
Marco Nelissen79e2b622014-05-16 08:07:28 -0700461 (cryptoinfo_mode_t) mode,
Marco Nelissen050eb322014-05-09 15:10:23 -0700462 (size_t*) cleardata,
463 (size_t*) crypteddata);
464}
465
Robert Shih30e3c7d2018-01-21 17:06:12 -0800466EXPORT
467int64_t AMediaExtractor_getCachedDuration(AMediaExtractor *ex) {
468 bool eos;
469 int64_t durationUs;
470 if (ex->mImpl->getCachedDuration(&durationUs, &eos)) {
471 return durationUs;
472 }
473 return -1;
474}
Marco Nelissen0c3be872014-05-01 10:14:44 -0700475
Robert Shihd83d4f42018-02-24 19:02:46 -0800476EXPORT
477media_status_t AMediaExtractor_getSampleFormat(AMediaExtractor *ex, AMediaFormat *fmt) {
478 if (fmt == NULL) {
479 return AMEDIA_ERROR_INVALID_PARAMETER;
480 }
481
482 sp<MetaData> sampleMeta;
483 status_t err = ex->mImpl->getSampleMeta(&sampleMeta);
484 if (err != OK) {
485 return translate_error(err);
486 }
487
488 sp<AMessage> meta;
489 AMediaFormat_getFormat(fmt, &meta);
490 meta->clear();
491
492 int32_t layerId;
493 if (sampleMeta->findInt32(kKeyTemporalLayerId, &layerId)) {
494 meta->setInt32(AMEDIAFORMAT_KEY_TEMPORAL_LAYER_ID, layerId);
495 }
496
497 size_t trackIndex;
498 err = ex->mImpl->getSampleTrackIndex(&trackIndex);
499 if (err == OK) {
500 meta->setInt32(AMEDIAFORMAT_KEY_TRACK_INDEX, trackIndex);
501 sp<AMessage> trackFormat;
502 AString mime;
503 err = ex->mImpl->getTrackFormat(trackIndex, &trackFormat);
504 if (err == OK
505 && trackFormat != NULL
506 && trackFormat->findString(AMEDIAFORMAT_KEY_MIME, &mime)) {
507 meta->setString(AMEDIAFORMAT_KEY_MIME, mime);
508 }
509 }
510
511 int64_t durationUs;
512 if (sampleMeta->findInt64(kKeyDuration, &durationUs)) {
513 meta->setInt64(AMEDIAFORMAT_KEY_DURATION, durationUs);
514 }
515
516 uint32_t dataType; // unused
517 const void *seiData;
518 size_t seiLength;
519 if (sampleMeta->findData(kKeySEI, &dataType, &seiData, &seiLength)) {
520 sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);;
521 meta->setBuffer(AMEDIAFORMAT_KEY_SEI, sei);
522 }
523
524 const void *mpegUserDataPointer;
525 size_t mpegUserDataLength;
526 if (sampleMeta->findData(
527 kKeyMpegUserData, &dataType, &mpegUserDataPointer, &mpegUserDataLength)) {
528 sp<ABuffer> mpegUserData = ABuffer::CreateAsCopy(mpegUserDataPointer, mpegUserDataLength);
529 meta->setBuffer(AMEDIAFORMAT_KEY_MPEG_USER_DATA, mpegUserData);
530 }
531
532 return AMEDIA_OK;
533}
534
Marco Nelissen0c3be872014-05-01 10:14:44 -0700535} // extern "C"
536