blob: b869c54d3e568c5f351a80b2c475bdeea6352dd5 [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
Marco Nelissen050eb322014-05-09 15:10:23 -070021#include "NdkMediaError.h"
Marco Nelissen0c3be872014-05-01 10:14:44 -070022#include "NdkMediaExtractor.h"
23#include "NdkMediaFormatPriv.h"
24
25
26#include <utils/Log.h>
27#include <utils/StrongPointer.h>
Marco Nelissen050eb322014-05-09 15:10:23 -070028#include <media/hardware/CryptoAPI.h>
Marco Nelissen0c3be872014-05-01 10:14:44 -070029#include <media/stagefright/foundation/ABuffer.h>
30#include <media/stagefright/foundation/AMessage.h>
31#include <media/stagefright/MetaData.h>
32#include <media/stagefright/NuMediaExtractor.h>
33#include <media/IMediaHTTPService.h>
34#include <android_runtime/AndroidRuntime.h>
35#include <android_util_Binder.h>
36
37#include <jni.h>
38
39using namespace android;
40
Marco Nelissene419d7c2014-05-15 14:17:25 -070041static media_status_t translate_error(status_t err) {
Marco Nelissen0c3be872014-05-01 10:14:44 -070042 if (err == OK) {
Marco Nelissene419d7c2014-05-15 14:17:25 -070043 return AMEDIA_OK;
Marco Nelissen0c3be872014-05-01 10:14:44 -070044 }
45 ALOGE("sf error code: %d", err);
Marco Nelissene419d7c2014-05-15 14:17:25 -070046 return AMEDIA_ERROR_UNKNOWN;
Marco Nelissen0c3be872014-05-01 10:14:44 -070047}
48
49struct AMediaExtractor {
50 sp<NuMediaExtractor> mImpl;
Marco Nelissen050eb322014-05-09 15:10:23 -070051 sp<ABuffer> mPsshBuf;
Marco Nelissen0c3be872014-05-01 10:14:44 -070052
53};
54
55extern "C" {
56
Marco Nelissen3425fd52014-05-14 11:12:46 -070057EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -070058AMediaExtractor* AMediaExtractor_new() {
59 ALOGV("ctor");
60 AMediaExtractor *mData = new AMediaExtractor();
61 mData->mImpl = new NuMediaExtractor();
62 return mData;
63}
64
Marco Nelissen3425fd52014-05-14 11:12:46 -070065EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -070066media_status_t AMediaExtractor_delete(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -070067 ALOGV("dtor");
68 delete mData;
Marco Nelissene419d7c2014-05-15 14:17:25 -070069 return AMEDIA_OK;
Marco Nelissen0c3be872014-05-01 10:14:44 -070070}
71
Marco Nelissen3425fd52014-05-14 11:12:46 -070072EXPORT
Glenn Kastenb187de12014-12-30 08:18:15 -080073media_status_t AMediaExtractor_setDataSourceFd(AMediaExtractor *mData, int fd, off64_t offset,
74 off64_t length) {
Marco Nelissen0c3be872014-05-01 10:14:44 -070075 ALOGV("setDataSource(%d, %lld, %lld)", fd, offset, length);
Marco Nelissene419d7c2014-05-15 14:17:25 -070076 return translate_error(mData->mImpl->setDataSource(fd, offset, length));
Marco Nelissen0c3be872014-05-01 10:14:44 -070077}
78
Marco Nelissen3425fd52014-05-14 11:12:46 -070079EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -070080media_status_t AMediaExtractor_setDataSource(AMediaExtractor *mData, const char *location) {
Marco Nelissen0c3be872014-05-01 10:14:44 -070081 ALOGV("setDataSource(%s)", location);
82 // TODO: add header support
83
84 JNIEnv *env = AndroidRuntime::getJNIEnv();
85 jobject service = NULL;
86 if (env == NULL) {
87 ALOGE("setDataSource(path) must be called from Java thread");
88 env->ExceptionClear();
Marco Nelissene419d7c2014-05-15 14:17:25 -070089 return AMEDIA_ERROR_UNSUPPORTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -070090 }
91
92 jclass mediahttpclass = env->FindClass("android/media/MediaHTTPService");
93 if (mediahttpclass == NULL) {
94 ALOGE("can't find MediaHttpService");
95 env->ExceptionClear();
Marco Nelissene419d7c2014-05-15 14:17:25 -070096 return AMEDIA_ERROR_UNSUPPORTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -070097 }
98
99 jmethodID mediaHttpCreateMethod = env->GetStaticMethodID(mediahttpclass,
100 "createHttpServiceBinderIfNecessary", "(Ljava/lang/String;)Landroid/os/IBinder;");
101 if (mediaHttpCreateMethod == NULL) {
102 ALOGE("can't find method");
103 env->ExceptionClear();
Marco Nelissene419d7c2014-05-15 14:17:25 -0700104 return AMEDIA_ERROR_UNSUPPORTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700105 }
106
107 jstring jloc = env->NewStringUTF(location);
108
109 service = env->CallStaticObjectMethod(mediahttpclass, mediaHttpCreateMethod, jloc);
110 env->DeleteLocalRef(jloc);
111
112 sp<IMediaHTTPService> httpService;
113 if (service != NULL) {
114 sp<IBinder> binder = ibinderForJavaObject(env, service);
115 httpService = interface_cast<IMediaHTTPService>(binder);
116 }
117
Marco Nelissene419d7c2014-05-15 14:17:25 -0700118 status_t err = mData->mImpl->setDataSource(httpService, location, NULL);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700119 env->ExceptionClear();
Marco Nelissene419d7c2014-05-15 14:17:25 -0700120 return translate_error(err);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700121}
122
Marco Nelissen3425fd52014-05-14 11:12:46 -0700123EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700124size_t AMediaExtractor_getTrackCount(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700125 return mData->mImpl->countTracks();
126}
127
Marco Nelissen3425fd52014-05-14 11:12:46 -0700128EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700129AMediaFormat* AMediaExtractor_getTrackFormat(AMediaExtractor *mData, size_t idx) {
130 sp<AMessage> format;
131 mData->mImpl->getTrackFormat(idx, &format);
132 return AMediaFormat_fromMsg(&format);
133}
134
Marco Nelissen3425fd52014-05-14 11:12:46 -0700135EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700136media_status_t AMediaExtractor_selectTrack(AMediaExtractor *mData, size_t idx) {
Mark Salyzyn98f28cd2014-06-18 16:32:50 -0700137 ALOGV("selectTrack(%zu)", idx);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700138 return translate_error(mData->mImpl->selectTrack(idx));
139}
140
Marco Nelissen3425fd52014-05-14 11:12:46 -0700141EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700142media_status_t AMediaExtractor_unselectTrack(AMediaExtractor *mData, size_t idx) {
Mark Salyzyn98f28cd2014-06-18 16:32:50 -0700143 ALOGV("unselectTrack(%zu)", idx);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700144 return translate_error(mData->mImpl->unselectTrack(idx));
145}
146
Marco Nelissen3425fd52014-05-14 11:12:46 -0700147EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700148bool AMediaExtractor_advance(AMediaExtractor *mData) {
149 //ALOGV("advance");
150 return mData->mImpl->advance();
151}
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
Marco Nelissene419d7c2014-05-15 14:17:25 -0700178uint32_t AMediaExtractor_getSampleFlags(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700179 int sampleFlags = 0;
180 sp<MetaData> meta;
181 status_t err = mData->mImpl->getSampleMeta(&meta);
182 if (err != OK) {
183 return -1;
184 }
185 int32_t val;
186 if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700187 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_SYNC;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700188 }
189
190 uint32_t type;
191 const void *data;
192 size_t size;
193 if (meta->findData(kKeyEncryptedSizes, &type, &data, &size)) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700194 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_ENCRYPTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700195 }
196 return sampleFlags;
197}
198
Marco Nelissen3425fd52014-05-14 11:12:46 -0700199EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700200int AMediaExtractor_getSampleTrackIndex(AMediaExtractor *mData) {
201 size_t idx;
202 if (mData->mImpl->getSampleTrackIndex(&idx) != OK) {
203 return -1;
204 }
205 return idx;
206}
207
Marco Nelissen3425fd52014-05-14 11:12:46 -0700208EXPORT
Marco Nelisseneb4860c2014-05-29 08:04:34 -0700209int64_t AMediaExtractor_getSampleTime(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700210 int64_t time;
211 if (mData->mImpl->getSampleTime(&time) != OK) {
212 return -1;
213 }
214 return time;
215}
216
Marco Nelissen3425fd52014-05-14 11:12:46 -0700217EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700218PsshInfo* AMediaExtractor_getPsshInfo(AMediaExtractor *ex) {
219
220 if (ex->mPsshBuf != NULL) {
221 return (PsshInfo*) ex->mPsshBuf->data();
222 }
223
224 sp<AMessage> format;
225 ex->mImpl->getFileFormat(&format);
226 sp<ABuffer> buffer;
227 if(!format->findBuffer("pssh", &buffer)) {
228 return NULL;
229 }
230
231 // the format of the buffer is 1 or more of:
232 // {
233 // 16 byte uuid
234 // 4 byte data length N
235 // N bytes of data
236 // }
237
238 // Determine the number of entries in the source data.
239 // Since we got the data from stagefright, we trust it is valid and properly formatted.
240 const uint8_t* data = buffer->data();
241 size_t len = buffer->size();
242 size_t numentries = 0;
243 while (len > 0) {
244 numentries++;
245
Marco Nelissen346bb512015-04-09 14:32:45 -0700246 if (len < 16) {
247 ALOGE("invalid PSSH data");
248 return NULL;
249 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700250 // skip uuid
251 data += 16;
252 len -= 16;
253
254 // get data length
Marco Nelissen346bb512015-04-09 14:32:45 -0700255 if (len < 4) {
256 ALOGE("invalid PSSH data");
257 return NULL;
258 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700259 uint32_t datalen = *((uint32_t*)data);
260 data += 4;
261 len -= 4;
262
Marco Nelissen346bb512015-04-09 14:32:45 -0700263 if (len < datalen) {
264 ALOGE("invalid PSSH data");
265 return NULL;
266 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700267 // skip the data
268 data += datalen;
269 len -= datalen;
270 }
271
Marco Nelissen58344bc2014-10-23 11:36:38 -0700272 // there are <numentries> in the source buffer, we need
273 // (source buffer size) - (sizeof(uint32_t) * numentries) + sizeof(size_t)
274 // + ((sizeof(void*) + sizeof(size_t)) * numentries) bytes for the PsshInfo structure
275 // Or in other words, the data lengths in the source structure are replaced by size_t
276 // (which may be the same size or larger, for 64 bit), and in addition there is an
277 // extra pointer for each entry, and an extra size_t for the entire PsshInfo.
278 size_t newsize = buffer->size() - (sizeof(uint32_t) * numentries) + sizeof(size_t)
279 + ((sizeof(void*) + sizeof(size_t)) * numentries);
Marco Nelissen346bb512015-04-09 14:32:45 -0700280 if (newsize <= buffer->size()) {
281 ALOGE("invalid PSSH data");
282 return NULL;
283 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700284 ex->mPsshBuf = new ABuffer(newsize);
285 ex->mPsshBuf->setRange(0, newsize);
286
287 // copy data
288 const uint8_t* src = buffer->data();
289 uint8_t* dst = ex->mPsshBuf->data();
Marco Nelissen58344bc2014-10-23 11:36:38 -0700290 uint8_t* dstdata = dst + sizeof(size_t) + numentries * sizeof(PsshEntry);
291 *((size_t*)dst) = numentries;
292 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700293 for (size_t i = 0; i < numentries; i++) {
294 // copy uuid
295 memcpy(dst, src, 16);
296 src += 16;
297 dst += 16;
298
299 // get/copy data length
300 uint32_t datalen = *((uint32_t*)src);
Marco Nelissen58344bc2014-10-23 11:36:38 -0700301 *((size_t*)dst) = datalen;
302 src += sizeof(uint32_t);
303 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700304
305 // the next entry in the destination is a pointer to the actual data, which we store
306 // after the array of PsshEntry
Marco Nelissen58344bc2014-10-23 11:36:38 -0700307 *((void**)dst) = dstdata;
308 dst += sizeof(void*);
Marco Nelissen050eb322014-05-09 15:10:23 -0700309
310 // copy the actual data
311 memcpy(dstdata, src, datalen);
312 dstdata += datalen;
313 src += datalen;
314 }
315
316 return (PsshInfo*) ex->mPsshBuf->data();
317}
318
Marco Nelissen3425fd52014-05-14 11:12:46 -0700319EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700320AMediaCodecCryptoInfo *AMediaExtractor_getSampleCryptoInfo(AMediaExtractor *ex) {
321 sp<MetaData> meta;
322 if(ex->mImpl->getSampleMeta(&meta) != 0) {
323 return NULL;
324 }
325
326 uint32_t type;
327 const void *crypteddata;
328 size_t cryptedsize;
329 if (!meta->findData(kKeyEncryptedSizes, &type, &crypteddata, &cryptedsize)) {
330 return NULL;
331 }
332 size_t numSubSamples = cryptedsize / sizeof(size_t);
333
334 const void *cleardata;
335 size_t clearsize;
336 if (meta->findData(kKeyPlainSizes, &type, &cleardata, &clearsize)) {
337 if (clearsize != cryptedsize) {
338 // The two must be of the same length.
339 return NULL;
340 }
341 }
342
343 const void *key;
344 size_t keysize;
345 if (meta->findData(kKeyCryptoIV, &type, &key, &keysize)) {
346 if (keysize != 16) {
347 // IVs must be 16 bytes in length.
348 return NULL;
349 }
350 }
351
352 const void *iv;
353 size_t ivsize;
354 if (meta->findData(kKeyCryptoIV, &type, &iv, &ivsize)) {
355 if (ivsize != 16) {
356 // IVs must be 16 bytes in length.
357 return NULL;
358 }
359 }
360
361 int32_t mode;
362 if (!meta->findInt32(kKeyCryptoMode, &mode)) {
363 mode = CryptoPlugin::kMode_AES_CTR;
364 }
365
366 return AMediaCodecCryptoInfo_new(
367 numSubSamples,
368 (uint8_t*) key,
369 (uint8_t*) iv,
Marco Nelissen79e2b622014-05-16 08:07:28 -0700370 (cryptoinfo_mode_t) mode,
Marco Nelissen050eb322014-05-09 15:10:23 -0700371 (size_t*) cleardata,
372 (size_t*) crypteddata);
373}
374
Marco Nelissen0c3be872014-05-01 10:14:44 -0700375
376} // extern "C"
377