blob: 89f2d9c3e55606bb1b04900e128917f92af88c8b [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
Aurimas Liutikas214c8332016-02-19 14:48:23 -080026#include <inttypes.h>
Marco Nelissen0c3be872014-05-01 10:14:44 -070027#include <utils/Log.h>
28#include <utils/StrongPointer.h>
Marco Nelissen050eb322014-05-09 15:10:23 -070029#include <media/hardware/CryptoAPI.h>
Marco Nelissen0c3be872014-05-01 10:14:44 -070030#include <media/stagefright/foundation/ABuffer.h>
31#include <media/stagefright/foundation/AMessage.h>
32#include <media/stagefright/MetaData.h>
33#include <media/stagefright/NuMediaExtractor.h>
34#include <media/IMediaHTTPService.h>
35#include <android_runtime/AndroidRuntime.h>
36#include <android_util_Binder.h>
37
38#include <jni.h>
39
40using namespace android;
41
Marco Nelissene419d7c2014-05-15 14:17:25 -070042static media_status_t translate_error(status_t err) {
Marco Nelissen0c3be872014-05-01 10:14:44 -070043 if (err == OK) {
Marco Nelissene419d7c2014-05-15 14:17:25 -070044 return AMEDIA_OK;
Marco Nelissen0c3be872014-05-01 10:14:44 -070045 }
46 ALOGE("sf error code: %d", err);
Marco Nelissene419d7c2014-05-15 14:17:25 -070047 return AMEDIA_ERROR_UNKNOWN;
Marco Nelissen0c3be872014-05-01 10:14:44 -070048}
49
50struct AMediaExtractor {
51 sp<NuMediaExtractor> mImpl;
Marco Nelissen050eb322014-05-09 15:10:23 -070052 sp<ABuffer> mPsshBuf;
Marco Nelissen0c3be872014-05-01 10:14:44 -070053
54};
55
56extern "C" {
57
Marco Nelissen3425fd52014-05-14 11:12:46 -070058EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -070059AMediaExtractor* AMediaExtractor_new() {
60 ALOGV("ctor");
61 AMediaExtractor *mData = new AMediaExtractor();
62 mData->mImpl = new NuMediaExtractor();
63 return mData;
64}
65
Marco Nelissen3425fd52014-05-14 11:12:46 -070066EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -070067media_status_t AMediaExtractor_delete(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -070068 ALOGV("dtor");
69 delete mData;
Marco Nelissene419d7c2014-05-15 14:17:25 -070070 return AMEDIA_OK;
Marco Nelissen0c3be872014-05-01 10:14:44 -070071}
72
Marco Nelissen3425fd52014-05-14 11:12:46 -070073EXPORT
Glenn Kastenb187de12014-12-30 08:18:15 -080074media_status_t AMediaExtractor_setDataSourceFd(AMediaExtractor *mData, int fd, off64_t offset,
75 off64_t length) {
Aurimas Liutikas214c8332016-02-19 14:48:23 -080076 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
Marco Nelissene419d7c2014-05-15 14:17:25 -070077 return translate_error(mData->mImpl->setDataSource(fd, offset, length));
Marco Nelissen0c3be872014-05-01 10:14:44 -070078}
79
Marco Nelissen3425fd52014-05-14 11:12:46 -070080EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -070081media_status_t AMediaExtractor_setDataSource(AMediaExtractor *mData, const char *location) {
Marco Nelissen0c3be872014-05-01 10:14:44 -070082 ALOGV("setDataSource(%s)", location);
83 // TODO: add header support
84
85 JNIEnv *env = AndroidRuntime::getJNIEnv();
86 jobject service = NULL;
87 if (env == NULL) {
88 ALOGE("setDataSource(path) must be called from Java thread");
89 env->ExceptionClear();
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
Marco Nelissene419d7c2014-05-15 14:17:25 -0700125size_t AMediaExtractor_getTrackCount(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700126 return mData->mImpl->countTracks();
127}
128
Marco Nelissen3425fd52014-05-14 11:12:46 -0700129EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700130AMediaFormat* AMediaExtractor_getTrackFormat(AMediaExtractor *mData, size_t idx) {
131 sp<AMessage> format;
132 mData->mImpl->getTrackFormat(idx, &format);
133 return AMediaFormat_fromMsg(&format);
134}
135
Marco Nelissen3425fd52014-05-14 11:12:46 -0700136EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700137media_status_t AMediaExtractor_selectTrack(AMediaExtractor *mData, size_t idx) {
Mark Salyzyn98f28cd2014-06-18 16:32:50 -0700138 ALOGV("selectTrack(%zu)", idx);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700139 return translate_error(mData->mImpl->selectTrack(idx));
140}
141
Marco Nelissen3425fd52014-05-14 11:12:46 -0700142EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700143media_status_t AMediaExtractor_unselectTrack(AMediaExtractor *mData, size_t idx) {
Mark Salyzyn98f28cd2014-06-18 16:32:50 -0700144 ALOGV("unselectTrack(%zu)", idx);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700145 return translate_error(mData->mImpl->unselectTrack(idx));
146}
147
Marco Nelissen3425fd52014-05-14 11:12:46 -0700148EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700149bool AMediaExtractor_advance(AMediaExtractor *mData) {
150 //ALOGV("advance");
Robert Shih70452262016-09-16 16:43:49 -0700151 status_t err = mData->mImpl->advance();
152 if (err == ERROR_END_OF_STREAM) {
153 return false;
154 } else if (err != OK) {
155 ALOGE("sf error code: %d", err);
156 return false;
157 }
158 return true;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700159}
160
Marco Nelissen3425fd52014-05-14 11:12:46 -0700161EXPORT
Marco Nelissen79e2b622014-05-16 08:07:28 -0700162media_status_t AMediaExtractor_seekTo(AMediaExtractor *ex, int64_t seekPosUs, SeekMode mode) {
163 android::MediaSource::ReadOptions::SeekMode sfmode;
164 if (mode == AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC) {
165 sfmode = android::MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC;
166 } else if (mode == AMEDIAEXTRACTOR_SEEK_CLOSEST_SYNC) {
167 sfmode = android::MediaSource::ReadOptions::SEEK_CLOSEST_SYNC;
168 } else {
169 sfmode = android::MediaSource::ReadOptions::SEEK_NEXT_SYNC;
170 }
171
172 return translate_error(ex->mImpl->seekTo(seekPosUs, sfmode));
173}
174
175EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700176ssize_t AMediaExtractor_readSampleData(AMediaExtractor *mData, uint8_t *buffer, size_t capacity) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700177 //ALOGV("readSampleData");
178 sp<ABuffer> tmp = new ABuffer(buffer, capacity);
179 if (mData->mImpl->readSampleData(tmp) == OK) {
180 return tmp->size();
181 }
182 return -1;
183}
184
Marco Nelissen3425fd52014-05-14 11:12:46 -0700185EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700186uint32_t AMediaExtractor_getSampleFlags(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700187 int sampleFlags = 0;
188 sp<MetaData> meta;
189 status_t err = mData->mImpl->getSampleMeta(&meta);
190 if (err != OK) {
191 return -1;
192 }
193 int32_t val;
194 if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700195 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_SYNC;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700196 }
197
198 uint32_t type;
199 const void *data;
200 size_t size;
201 if (meta->findData(kKeyEncryptedSizes, &type, &data, &size)) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700202 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_ENCRYPTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700203 }
204 return sampleFlags;
205}
206
Marco Nelissen3425fd52014-05-14 11:12:46 -0700207EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700208int AMediaExtractor_getSampleTrackIndex(AMediaExtractor *mData) {
209 size_t idx;
210 if (mData->mImpl->getSampleTrackIndex(&idx) != OK) {
211 return -1;
212 }
213 return idx;
214}
215
Marco Nelissen3425fd52014-05-14 11:12:46 -0700216EXPORT
Marco Nelisseneb4860c2014-05-29 08:04:34 -0700217int64_t AMediaExtractor_getSampleTime(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700218 int64_t time;
219 if (mData->mImpl->getSampleTime(&time) != OK) {
220 return -1;
221 }
222 return time;
223}
224
Marco Nelissen3425fd52014-05-14 11:12:46 -0700225EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700226PsshInfo* AMediaExtractor_getPsshInfo(AMediaExtractor *ex) {
227
228 if (ex->mPsshBuf != NULL) {
229 return (PsshInfo*) ex->mPsshBuf->data();
230 }
231
232 sp<AMessage> format;
233 ex->mImpl->getFileFormat(&format);
234 sp<ABuffer> buffer;
235 if(!format->findBuffer("pssh", &buffer)) {
236 return NULL;
237 }
238
239 // the format of the buffer is 1 or more of:
240 // {
241 // 16 byte uuid
242 // 4 byte data length N
243 // N bytes of data
244 // }
245
246 // Determine the number of entries in the source data.
247 // Since we got the data from stagefright, we trust it is valid and properly formatted.
248 const uint8_t* data = buffer->data();
249 size_t len = buffer->size();
250 size_t numentries = 0;
251 while (len > 0) {
252 numentries++;
253
Marco Nelissen346bb512015-04-09 14:32:45 -0700254 if (len < 16) {
255 ALOGE("invalid PSSH data");
256 return NULL;
257 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700258 // skip uuid
259 data += 16;
260 len -= 16;
261
262 // get data length
Marco Nelissen346bb512015-04-09 14:32:45 -0700263 if (len < 4) {
264 ALOGE("invalid PSSH data");
265 return NULL;
266 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700267 uint32_t datalen = *((uint32_t*)data);
268 data += 4;
269 len -= 4;
270
Marco Nelissen346bb512015-04-09 14:32:45 -0700271 if (len < datalen) {
272 ALOGE("invalid PSSH data");
273 return NULL;
274 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700275 // skip the data
276 data += datalen;
277 len -= datalen;
278 }
279
Marco Nelissen58344bc2014-10-23 11:36:38 -0700280 // there are <numentries> in the source buffer, we need
281 // (source buffer size) - (sizeof(uint32_t) * numentries) + sizeof(size_t)
282 // + ((sizeof(void*) + sizeof(size_t)) * numentries) bytes for the PsshInfo structure
283 // Or in other words, the data lengths in the source structure are replaced by size_t
284 // (which may be the same size or larger, for 64 bit), and in addition there is an
285 // extra pointer for each entry, and an extra size_t for the entire PsshInfo.
286 size_t newsize = buffer->size() - (sizeof(uint32_t) * numentries) + sizeof(size_t)
287 + ((sizeof(void*) + sizeof(size_t)) * numentries);
Marco Nelissen346bb512015-04-09 14:32:45 -0700288 if (newsize <= buffer->size()) {
289 ALOGE("invalid PSSH data");
290 return NULL;
291 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700292 ex->mPsshBuf = new ABuffer(newsize);
293 ex->mPsshBuf->setRange(0, newsize);
294
295 // copy data
296 const uint8_t* src = buffer->data();
297 uint8_t* dst = ex->mPsshBuf->data();
Marco Nelissen58344bc2014-10-23 11:36:38 -0700298 uint8_t* dstdata = dst + sizeof(size_t) + numentries * sizeof(PsshEntry);
299 *((size_t*)dst) = numentries;
300 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700301 for (size_t i = 0; i < numentries; i++) {
302 // copy uuid
303 memcpy(dst, src, 16);
304 src += 16;
305 dst += 16;
306
307 // get/copy data length
308 uint32_t datalen = *((uint32_t*)src);
Marco Nelissen58344bc2014-10-23 11:36:38 -0700309 *((size_t*)dst) = datalen;
310 src += sizeof(uint32_t);
311 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700312
313 // the next entry in the destination is a pointer to the actual data, which we store
314 // after the array of PsshEntry
Marco Nelissen58344bc2014-10-23 11:36:38 -0700315 *((void**)dst) = dstdata;
316 dst += sizeof(void*);
Marco Nelissen050eb322014-05-09 15:10:23 -0700317
318 // copy the actual data
319 memcpy(dstdata, src, datalen);
320 dstdata += datalen;
321 src += datalen;
322 }
323
324 return (PsshInfo*) ex->mPsshBuf->data();
325}
326
Marco Nelissen3425fd52014-05-14 11:12:46 -0700327EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700328AMediaCodecCryptoInfo *AMediaExtractor_getSampleCryptoInfo(AMediaExtractor *ex) {
329 sp<MetaData> meta;
330 if(ex->mImpl->getSampleMeta(&meta) != 0) {
331 return NULL;
332 }
333
334 uint32_t type;
335 const void *crypteddata;
336 size_t cryptedsize;
337 if (!meta->findData(kKeyEncryptedSizes, &type, &crypteddata, &cryptedsize)) {
338 return NULL;
339 }
340 size_t numSubSamples = cryptedsize / sizeof(size_t);
341
342 const void *cleardata;
343 size_t clearsize;
344 if (meta->findData(kKeyPlainSizes, &type, &cleardata, &clearsize)) {
345 if (clearsize != cryptedsize) {
346 // The two must be of the same length.
347 return NULL;
348 }
349 }
350
351 const void *key;
352 size_t keysize;
Edwin Wongb2fb3c92016-08-29 14:57:47 -0700353 if (meta->findData(kKeyCryptoKey, &type, &key, &keysize)) {
Marco Nelissen050eb322014-05-09 15:10:23 -0700354 if (keysize != 16) {
Edwin Wongb2fb3c92016-08-29 14:57:47 -0700355 // Keys must be 16 bytes in length.
Marco Nelissen050eb322014-05-09 15:10:23 -0700356 return NULL;
357 }
358 }
359
360 const void *iv;
361 size_t ivsize;
362 if (meta->findData(kKeyCryptoIV, &type, &iv, &ivsize)) {
363 if (ivsize != 16) {
364 // IVs must be 16 bytes in length.
365 return NULL;
366 }
367 }
368
369 int32_t mode;
370 if (!meta->findInt32(kKeyCryptoMode, &mode)) {
371 mode = CryptoPlugin::kMode_AES_CTR;
372 }
373
374 return AMediaCodecCryptoInfo_new(
375 numSubSamples,
376 (uint8_t*) key,
377 (uint8_t*) iv,
Marco Nelissen79e2b622014-05-16 08:07:28 -0700378 (cryptoinfo_mode_t) mode,
Marco Nelissen050eb322014-05-09 15:10:23 -0700379 (size_t*) cleardata,
380 (size_t*) crypteddata);
381}
382
Marco Nelissen0c3be872014-05-01 10:14:44 -0700383
384} // extern "C"
385