blob: 5dee8b04672ce2dcc5dbf35a743810305dbe3093 [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>
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;
Marco Nelissen0c3be872014-05-01 10:14:44 -070046 }
47 ALOGE("sf error code: %d", err);
Marco Nelissene419d7c2014-05-15 14:17:25 -070048 return AMEDIA_ERROR_UNKNOWN;
Marco Nelissen0c3be872014-05-01 10:14:44 -070049}
50
51struct AMediaExtractor {
52 sp<NuMediaExtractor> mImpl;
Marco Nelissen050eb322014-05-09 15:10:23 -070053 sp<ABuffer> mPsshBuf;
Marco Nelissen0c3be872014-05-01 10:14:44 -070054
55};
56
57extern "C" {
58
Marco Nelissen3425fd52014-05-14 11:12:46 -070059EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -070060AMediaExtractor* AMediaExtractor_new() {
61 ALOGV("ctor");
62 AMediaExtractor *mData = new AMediaExtractor();
63 mData->mImpl = new NuMediaExtractor();
64 return mData;
65}
66
Marco Nelissen3425fd52014-05-14 11:12:46 -070067EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -070068media_status_t AMediaExtractor_delete(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -070069 ALOGV("dtor");
70 delete mData;
Marco Nelissene419d7c2014-05-15 14:17:25 -070071 return AMEDIA_OK;
Marco Nelissen0c3be872014-05-01 10:14:44 -070072}
73
Marco Nelissen3425fd52014-05-14 11:12:46 -070074EXPORT
Glenn Kastenb187de12014-12-30 08:18:15 -080075media_status_t AMediaExtractor_setDataSourceFd(AMediaExtractor *mData, int fd, off64_t offset,
76 off64_t length) {
Aurimas Liutikas214c8332016-02-19 14:48:23 -080077 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
Marco Nelissene419d7c2014-05-15 14:17:25 -070078 return translate_error(mData->mImpl->setDataSource(fd, offset, length));
Marco Nelissen0c3be872014-05-01 10:14:44 -070079}
80
Marco Nelissen3425fd52014-05-14 11:12:46 -070081EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -070082media_status_t AMediaExtractor_setDataSource(AMediaExtractor *mData, const char *location) {
Marco Nelissen0c3be872014-05-01 10:14:44 -070083 ALOGV("setDataSource(%s)", location);
84 // TODO: add header support
85
86 JNIEnv *env = AndroidRuntime::getJNIEnv();
87 jobject service = NULL;
88 if (env == NULL) {
89 ALOGE("setDataSource(path) must be called from Java thread");
Marco Nelissene419d7c2014-05-15 14:17:25 -070090 return AMEDIA_ERROR_UNSUPPORTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -070091 }
92
93 jclass mediahttpclass = env->FindClass("android/media/MediaHTTPService");
94 if (mediahttpclass == NULL) {
95 ALOGE("can't find MediaHttpService");
96 env->ExceptionClear();
Marco Nelissene419d7c2014-05-15 14:17:25 -070097 return AMEDIA_ERROR_UNSUPPORTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -070098 }
99
100 jmethodID mediaHttpCreateMethod = env->GetStaticMethodID(mediahttpclass,
101 "createHttpServiceBinderIfNecessary", "(Ljava/lang/String;)Landroid/os/IBinder;");
102 if (mediaHttpCreateMethod == NULL) {
103 ALOGE("can't find method");
104 env->ExceptionClear();
Marco Nelissene419d7c2014-05-15 14:17:25 -0700105 return AMEDIA_ERROR_UNSUPPORTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700106 }
107
108 jstring jloc = env->NewStringUTF(location);
109
110 service = env->CallStaticObjectMethod(mediahttpclass, mediaHttpCreateMethod, jloc);
111 env->DeleteLocalRef(jloc);
112
113 sp<IMediaHTTPService> httpService;
114 if (service != NULL) {
115 sp<IBinder> binder = ibinderForJavaObject(env, service);
116 httpService = interface_cast<IMediaHTTPService>(binder);
117 }
118
Marco Nelissene419d7c2014-05-15 14:17:25 -0700119 status_t err = mData->mImpl->setDataSource(httpService, location, NULL);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700120 env->ExceptionClear();
Marco Nelissene419d7c2014-05-15 14:17:25 -0700121 return translate_error(err);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700122}
123
Marco Nelissen3425fd52014-05-14 11:12:46 -0700124EXPORT
Robert Shih0df451b2017-12-08 14:16:50 -0800125media_status_t AMediaExtractor_setDataSourceCustom(AMediaExtractor* mData, AMediaDataSource *src) {
126 return translate_error(mData->mImpl->setDataSource(new NdkDataSource(src)));
127}
128
129EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700130size_t AMediaExtractor_getTrackCount(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700131 return mData->mImpl->countTracks();
132}
133
Marco Nelissen3425fd52014-05-14 11:12:46 -0700134EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700135AMediaFormat* AMediaExtractor_getTrackFormat(AMediaExtractor *mData, size_t idx) {
136 sp<AMessage> format;
137 mData->mImpl->getTrackFormat(idx, &format);
138 return AMediaFormat_fromMsg(&format);
139}
140
Marco Nelissen3425fd52014-05-14 11:12:46 -0700141EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700142media_status_t AMediaExtractor_selectTrack(AMediaExtractor *mData, size_t idx) {
Mark Salyzyn98f28cd2014-06-18 16:32:50 -0700143 ALOGV("selectTrack(%zu)", idx);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700144 return translate_error(mData->mImpl->selectTrack(idx));
145}
146
Marco Nelissen3425fd52014-05-14 11:12:46 -0700147EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700148media_status_t AMediaExtractor_unselectTrack(AMediaExtractor *mData, size_t idx) {
Mark Salyzyn98f28cd2014-06-18 16:32:50 -0700149 ALOGV("unselectTrack(%zu)", idx);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700150 return translate_error(mData->mImpl->unselectTrack(idx));
151}
152
Marco Nelissen3425fd52014-05-14 11:12:46 -0700153EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700154bool AMediaExtractor_advance(AMediaExtractor *mData) {
155 //ALOGV("advance");
Robert Shih70452262016-09-16 16:43:49 -0700156 status_t err = mData->mImpl->advance();
157 if (err == ERROR_END_OF_STREAM) {
158 return false;
159 } else if (err != OK) {
160 ALOGE("sf error code: %d", err);
161 return false;
162 }
163 return true;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700164}
165
Marco Nelissen3425fd52014-05-14 11:12:46 -0700166EXPORT
Marco Nelissen79e2b622014-05-16 08:07:28 -0700167media_status_t AMediaExtractor_seekTo(AMediaExtractor *ex, int64_t seekPosUs, SeekMode mode) {
168 android::MediaSource::ReadOptions::SeekMode sfmode;
169 if (mode == AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC) {
170 sfmode = android::MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC;
171 } else if (mode == AMEDIAEXTRACTOR_SEEK_CLOSEST_SYNC) {
172 sfmode = android::MediaSource::ReadOptions::SEEK_CLOSEST_SYNC;
173 } else {
174 sfmode = android::MediaSource::ReadOptions::SEEK_NEXT_SYNC;
175 }
176
177 return translate_error(ex->mImpl->seekTo(seekPosUs, sfmode));
178}
179
180EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700181ssize_t AMediaExtractor_readSampleData(AMediaExtractor *mData, uint8_t *buffer, size_t capacity) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700182 //ALOGV("readSampleData");
183 sp<ABuffer> tmp = new ABuffer(buffer, capacity);
184 if (mData->mImpl->readSampleData(tmp) == OK) {
185 return tmp->size();
186 }
187 return -1;
188}
189
Marco Nelissen3425fd52014-05-14 11:12:46 -0700190EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700191uint32_t AMediaExtractor_getSampleFlags(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700192 int sampleFlags = 0;
193 sp<MetaData> meta;
194 status_t err = mData->mImpl->getSampleMeta(&meta);
195 if (err != OK) {
196 return -1;
197 }
198 int32_t val;
199 if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700200 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_SYNC;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700201 }
202
203 uint32_t type;
204 const void *data;
205 size_t size;
206 if (meta->findData(kKeyEncryptedSizes, &type, &data, &size)) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700207 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_ENCRYPTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700208 }
209 return sampleFlags;
210}
211
Marco Nelissen3425fd52014-05-14 11:12:46 -0700212EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700213int AMediaExtractor_getSampleTrackIndex(AMediaExtractor *mData) {
214 size_t idx;
215 if (mData->mImpl->getSampleTrackIndex(&idx) != OK) {
216 return -1;
217 }
218 return idx;
219}
220
Marco Nelissen3425fd52014-05-14 11:12:46 -0700221EXPORT
Marco Nelisseneb4860c2014-05-29 08:04:34 -0700222int64_t AMediaExtractor_getSampleTime(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700223 int64_t time;
224 if (mData->mImpl->getSampleTime(&time) != OK) {
225 return -1;
226 }
227 return time;
228}
229
Marco Nelissen3425fd52014-05-14 11:12:46 -0700230EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700231PsshInfo* AMediaExtractor_getPsshInfo(AMediaExtractor *ex) {
232
233 if (ex->mPsshBuf != NULL) {
234 return (PsshInfo*) ex->mPsshBuf->data();
235 }
236
237 sp<AMessage> format;
238 ex->mImpl->getFileFormat(&format);
239 sp<ABuffer> buffer;
240 if(!format->findBuffer("pssh", &buffer)) {
241 return NULL;
242 }
243
244 // the format of the buffer is 1 or more of:
245 // {
246 // 16 byte uuid
247 // 4 byte data length N
248 // N bytes of data
249 // }
250
251 // Determine the number of entries in the source data.
252 // Since we got the data from stagefright, we trust it is valid and properly formatted.
253 const uint8_t* data = buffer->data();
254 size_t len = buffer->size();
255 size_t numentries = 0;
256 while (len > 0) {
257 numentries++;
258
Marco Nelissen346bb512015-04-09 14:32:45 -0700259 if (len < 16) {
260 ALOGE("invalid PSSH data");
261 return NULL;
262 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700263 // skip uuid
264 data += 16;
265 len -= 16;
266
267 // get data length
Marco Nelissen346bb512015-04-09 14:32:45 -0700268 if (len < 4) {
269 ALOGE("invalid PSSH data");
270 return NULL;
271 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700272 uint32_t datalen = *((uint32_t*)data);
273 data += 4;
274 len -= 4;
275
Marco Nelissen346bb512015-04-09 14:32:45 -0700276 if (len < datalen) {
277 ALOGE("invalid PSSH data");
278 return NULL;
279 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700280 // skip the data
281 data += datalen;
282 len -= datalen;
283 }
284
Marco Nelissen58344bc2014-10-23 11:36:38 -0700285 // there are <numentries> in the source buffer, we need
286 // (source buffer size) - (sizeof(uint32_t) * numentries) + sizeof(size_t)
287 // + ((sizeof(void*) + sizeof(size_t)) * numentries) bytes for the PsshInfo structure
288 // Or in other words, the data lengths in the source structure are replaced by size_t
289 // (which may be the same size or larger, for 64 bit), and in addition there is an
290 // extra pointer for each entry, and an extra size_t for the entire PsshInfo.
291 size_t newsize = buffer->size() - (sizeof(uint32_t) * numentries) + sizeof(size_t)
292 + ((sizeof(void*) + sizeof(size_t)) * numentries);
Marco Nelissen346bb512015-04-09 14:32:45 -0700293 if (newsize <= buffer->size()) {
294 ALOGE("invalid PSSH data");
295 return NULL;
296 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700297 ex->mPsshBuf = new ABuffer(newsize);
298 ex->mPsshBuf->setRange(0, newsize);
299
300 // copy data
301 const uint8_t* src = buffer->data();
302 uint8_t* dst = ex->mPsshBuf->data();
Marco Nelissen58344bc2014-10-23 11:36:38 -0700303 uint8_t* dstdata = dst + sizeof(size_t) + numentries * sizeof(PsshEntry);
304 *((size_t*)dst) = numentries;
305 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700306 for (size_t i = 0; i < numentries; i++) {
307 // copy uuid
308 memcpy(dst, src, 16);
309 src += 16;
310 dst += 16;
311
312 // get/copy data length
313 uint32_t datalen = *((uint32_t*)src);
Marco Nelissen58344bc2014-10-23 11:36:38 -0700314 *((size_t*)dst) = datalen;
315 src += sizeof(uint32_t);
316 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700317
318 // the next entry in the destination is a pointer to the actual data, which we store
319 // after the array of PsshEntry
Marco Nelissen58344bc2014-10-23 11:36:38 -0700320 *((void**)dst) = dstdata;
321 dst += sizeof(void*);
Marco Nelissen050eb322014-05-09 15:10:23 -0700322
323 // copy the actual data
324 memcpy(dstdata, src, datalen);
325 dstdata += datalen;
326 src += datalen;
327 }
328
329 return (PsshInfo*) ex->mPsshBuf->data();
330}
331
Marco Nelissen3425fd52014-05-14 11:12:46 -0700332EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700333AMediaCodecCryptoInfo *AMediaExtractor_getSampleCryptoInfo(AMediaExtractor *ex) {
334 sp<MetaData> meta;
335 if(ex->mImpl->getSampleMeta(&meta) != 0) {
336 return NULL;
337 }
338
339 uint32_t type;
340 const void *crypteddata;
341 size_t cryptedsize;
342 if (!meta->findData(kKeyEncryptedSizes, &type, &crypteddata, &cryptedsize)) {
343 return NULL;
344 }
345 size_t numSubSamples = cryptedsize / sizeof(size_t);
346
347 const void *cleardata;
348 size_t clearsize;
349 if (meta->findData(kKeyPlainSizes, &type, &cleardata, &clearsize)) {
350 if (clearsize != cryptedsize) {
351 // The two must be of the same length.
352 return NULL;
353 }
354 }
355
356 const void *key;
357 size_t keysize;
Edwin Wongb2fb3c92016-08-29 14:57:47 -0700358 if (meta->findData(kKeyCryptoKey, &type, &key, &keysize)) {
Marco Nelissen050eb322014-05-09 15:10:23 -0700359 if (keysize != 16) {
Edwin Wongb2fb3c92016-08-29 14:57:47 -0700360 // Keys must be 16 bytes in length.
Marco Nelissen050eb322014-05-09 15:10:23 -0700361 return NULL;
362 }
363 }
364
365 const void *iv;
366 size_t ivsize;
367 if (meta->findData(kKeyCryptoIV, &type, &iv, &ivsize)) {
368 if (ivsize != 16) {
369 // IVs must be 16 bytes in length.
370 return NULL;
371 }
372 }
373
374 int32_t mode;
375 if (!meta->findInt32(kKeyCryptoMode, &mode)) {
376 mode = CryptoPlugin::kMode_AES_CTR;
377 }
378
379 return AMediaCodecCryptoInfo_new(
380 numSubSamples,
381 (uint8_t*) key,
382 (uint8_t*) iv,
Marco Nelissen79e2b622014-05-16 08:07:28 -0700383 (cryptoinfo_mode_t) mode,
Marco Nelissen050eb322014-05-09 15:10:23 -0700384 (size_t*) cleardata,
385 (size_t*) crypteddata);
386}
387
Marco Nelissen0c3be872014-05-01 10:14:44 -0700388
389} // extern "C"
390