blob: 1118959266c72c1adb1c911b111f501492cd6ccd [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");
151 return mData->mImpl->advance();
152}
153
Marco Nelissen3425fd52014-05-14 11:12:46 -0700154EXPORT
Marco Nelissen79e2b622014-05-16 08:07:28 -0700155media_status_t AMediaExtractor_seekTo(AMediaExtractor *ex, int64_t seekPosUs, SeekMode mode) {
156 android::MediaSource::ReadOptions::SeekMode sfmode;
157 if (mode == AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC) {
158 sfmode = android::MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC;
159 } else if (mode == AMEDIAEXTRACTOR_SEEK_CLOSEST_SYNC) {
160 sfmode = android::MediaSource::ReadOptions::SEEK_CLOSEST_SYNC;
161 } else {
162 sfmode = android::MediaSource::ReadOptions::SEEK_NEXT_SYNC;
163 }
164
165 return translate_error(ex->mImpl->seekTo(seekPosUs, sfmode));
166}
167
168EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700169ssize_t AMediaExtractor_readSampleData(AMediaExtractor *mData, uint8_t *buffer, size_t capacity) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700170 //ALOGV("readSampleData");
171 sp<ABuffer> tmp = new ABuffer(buffer, capacity);
172 if (mData->mImpl->readSampleData(tmp) == OK) {
173 return tmp->size();
174 }
175 return -1;
176}
177
Marco Nelissen3425fd52014-05-14 11:12:46 -0700178EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700179uint32_t AMediaExtractor_getSampleFlags(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700180 int sampleFlags = 0;
181 sp<MetaData> meta;
182 status_t err = mData->mImpl->getSampleMeta(&meta);
183 if (err != OK) {
184 return -1;
185 }
186 int32_t val;
187 if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700188 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_SYNC;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700189 }
190
191 uint32_t type;
192 const void *data;
193 size_t size;
194 if (meta->findData(kKeyEncryptedSizes, &type, &data, &size)) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700195 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_ENCRYPTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700196 }
197 return sampleFlags;
198}
199
Marco Nelissen3425fd52014-05-14 11:12:46 -0700200EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700201int AMediaExtractor_getSampleTrackIndex(AMediaExtractor *mData) {
202 size_t idx;
203 if (mData->mImpl->getSampleTrackIndex(&idx) != OK) {
204 return -1;
205 }
206 return idx;
207}
208
Marco Nelissen3425fd52014-05-14 11:12:46 -0700209EXPORT
Marco Nelisseneb4860c2014-05-29 08:04:34 -0700210int64_t AMediaExtractor_getSampleTime(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700211 int64_t time;
212 if (mData->mImpl->getSampleTime(&time) != OK) {
213 return -1;
214 }
215 return time;
216}
217
Marco Nelissen3425fd52014-05-14 11:12:46 -0700218EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700219PsshInfo* AMediaExtractor_getPsshInfo(AMediaExtractor *ex) {
220
221 if (ex->mPsshBuf != NULL) {
222 return (PsshInfo*) ex->mPsshBuf->data();
223 }
224
225 sp<AMessage> format;
226 ex->mImpl->getFileFormat(&format);
227 sp<ABuffer> buffer;
228 if(!format->findBuffer("pssh", &buffer)) {
229 return NULL;
230 }
231
232 // the format of the buffer is 1 or more of:
233 // {
234 // 16 byte uuid
235 // 4 byte data length N
236 // N bytes of data
237 // }
238
239 // Determine the number of entries in the source data.
240 // Since we got the data from stagefright, we trust it is valid and properly formatted.
241 const uint8_t* data = buffer->data();
242 size_t len = buffer->size();
243 size_t numentries = 0;
244 while (len > 0) {
245 numentries++;
246
Marco Nelissen346bb512015-04-09 14:32:45 -0700247 if (len < 16) {
248 ALOGE("invalid PSSH data");
249 return NULL;
250 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700251 // skip uuid
252 data += 16;
253 len -= 16;
254
255 // get data length
Marco Nelissen346bb512015-04-09 14:32:45 -0700256 if (len < 4) {
257 ALOGE("invalid PSSH data");
258 return NULL;
259 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700260 uint32_t datalen = *((uint32_t*)data);
261 data += 4;
262 len -= 4;
263
Marco Nelissen346bb512015-04-09 14:32:45 -0700264 if (len < datalen) {
265 ALOGE("invalid PSSH data");
266 return NULL;
267 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700268 // skip the data
269 data += datalen;
270 len -= datalen;
271 }
272
Marco Nelissen58344bc2014-10-23 11:36:38 -0700273 // there are <numentries> in the source buffer, we need
274 // (source buffer size) - (sizeof(uint32_t) * numentries) + sizeof(size_t)
275 // + ((sizeof(void*) + sizeof(size_t)) * numentries) bytes for the PsshInfo structure
276 // Or in other words, the data lengths in the source structure are replaced by size_t
277 // (which may be the same size or larger, for 64 bit), and in addition there is an
278 // extra pointer for each entry, and an extra size_t for the entire PsshInfo.
279 size_t newsize = buffer->size() - (sizeof(uint32_t) * numentries) + sizeof(size_t)
280 + ((sizeof(void*) + sizeof(size_t)) * numentries);
Marco Nelissen346bb512015-04-09 14:32:45 -0700281 if (newsize <= buffer->size()) {
282 ALOGE("invalid PSSH data");
283 return NULL;
284 }
Marco Nelissen050eb322014-05-09 15:10:23 -0700285 ex->mPsshBuf = new ABuffer(newsize);
286 ex->mPsshBuf->setRange(0, newsize);
287
288 // copy data
289 const uint8_t* src = buffer->data();
290 uint8_t* dst = ex->mPsshBuf->data();
Marco Nelissen58344bc2014-10-23 11:36:38 -0700291 uint8_t* dstdata = dst + sizeof(size_t) + numentries * sizeof(PsshEntry);
292 *((size_t*)dst) = numentries;
293 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700294 for (size_t i = 0; i < numentries; i++) {
295 // copy uuid
296 memcpy(dst, src, 16);
297 src += 16;
298 dst += 16;
299
300 // get/copy data length
301 uint32_t datalen = *((uint32_t*)src);
Marco Nelissen58344bc2014-10-23 11:36:38 -0700302 *((size_t*)dst) = datalen;
303 src += sizeof(uint32_t);
304 dst += sizeof(size_t);
Marco Nelissen050eb322014-05-09 15:10:23 -0700305
306 // the next entry in the destination is a pointer to the actual data, which we store
307 // after the array of PsshEntry
Marco Nelissen58344bc2014-10-23 11:36:38 -0700308 *((void**)dst) = dstdata;
309 dst += sizeof(void*);
Marco Nelissen050eb322014-05-09 15:10:23 -0700310
311 // copy the actual data
312 memcpy(dstdata, src, datalen);
313 dstdata += datalen;
314 src += datalen;
315 }
316
317 return (PsshInfo*) ex->mPsshBuf->data();
318}
319
Marco Nelissen3425fd52014-05-14 11:12:46 -0700320EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700321AMediaCodecCryptoInfo *AMediaExtractor_getSampleCryptoInfo(AMediaExtractor *ex) {
322 sp<MetaData> meta;
323 if(ex->mImpl->getSampleMeta(&meta) != 0) {
324 return NULL;
325 }
326
327 uint32_t type;
328 const void *crypteddata;
329 size_t cryptedsize;
330 if (!meta->findData(kKeyEncryptedSizes, &type, &crypteddata, &cryptedsize)) {
331 return NULL;
332 }
333 size_t numSubSamples = cryptedsize / sizeof(size_t);
334
335 const void *cleardata;
336 size_t clearsize;
337 if (meta->findData(kKeyPlainSizes, &type, &cleardata, &clearsize)) {
338 if (clearsize != cryptedsize) {
339 // The two must be of the same length.
340 return NULL;
341 }
342 }
343
344 const void *key;
345 size_t keysize;
346 if (meta->findData(kKeyCryptoIV, &type, &key, &keysize)) {
347 if (keysize != 16) {
348 // IVs must be 16 bytes in length.
349 return NULL;
350 }
351 }
352
353 const void *iv;
354 size_t ivsize;
355 if (meta->findData(kKeyCryptoIV, &type, &iv, &ivsize)) {
356 if (ivsize != 16) {
357 // IVs must be 16 bytes in length.
358 return NULL;
359 }
360 }
361
362 int32_t mode;
363 if (!meta->findInt32(kKeyCryptoMode, &mode)) {
364 mode = CryptoPlugin::kMode_AES_CTR;
365 }
366
367 return AMediaCodecCryptoInfo_new(
368 numSubSamples,
369 (uint8_t*) key,
370 (uint8_t*) iv,
Marco Nelissen79e2b622014-05-16 08:07:28 -0700371 (cryptoinfo_mode_t) mode,
Marco Nelissen050eb322014-05-09 15:10:23 -0700372 (size_t*) cleardata,
373 (size_t*) crypteddata);
374}
375
Marco Nelissen0c3be872014-05-01 10:14:44 -0700376
377} // extern "C"
378