blob: b0a9590da852a5545e2f4ce145516865bc111a50 [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
17#define LOG_NDEBUG 0
18#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
Marco Nelissene419d7c2014-05-15 14:17:25 -070073media_status_t AMediaExtractor_setDataSourceFd(AMediaExtractor *mData, int fd, off64_t offset, off64_t length) {
Marco Nelissen0c3be872014-05-01 10:14:44 -070074 ALOGV("setDataSource(%d, %lld, %lld)", fd, offset, length);
Marco Nelissene419d7c2014-05-15 14:17:25 -070075 return translate_error(mData->mImpl->setDataSource(fd, offset, length));
Marco Nelissen0c3be872014-05-01 10:14:44 -070076}
77
Marco Nelissen3425fd52014-05-14 11:12:46 -070078EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -070079media_status_t AMediaExtractor_setDataSource(AMediaExtractor *mData, const char *location) {
Marco Nelissen0c3be872014-05-01 10:14:44 -070080 ALOGV("setDataSource(%s)", location);
81 // TODO: add header support
82
83 JNIEnv *env = AndroidRuntime::getJNIEnv();
84 jobject service = NULL;
85 if (env == NULL) {
86 ALOGE("setDataSource(path) must be called from Java thread");
87 env->ExceptionClear();
Marco Nelissene419d7c2014-05-15 14:17:25 -070088 return AMEDIA_ERROR_UNSUPPORTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -070089 }
90
91 jclass mediahttpclass = env->FindClass("android/media/MediaHTTPService");
92 if (mediahttpclass == NULL) {
93 ALOGE("can't find MediaHttpService");
94 env->ExceptionClear();
Marco Nelissene419d7c2014-05-15 14:17:25 -070095 return AMEDIA_ERROR_UNSUPPORTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -070096 }
97
98 jmethodID mediaHttpCreateMethod = env->GetStaticMethodID(mediahttpclass,
99 "createHttpServiceBinderIfNecessary", "(Ljava/lang/String;)Landroid/os/IBinder;");
100 if (mediaHttpCreateMethod == NULL) {
101 ALOGE("can't find method");
102 env->ExceptionClear();
Marco Nelissene419d7c2014-05-15 14:17:25 -0700103 return AMEDIA_ERROR_UNSUPPORTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700104 }
105
106 jstring jloc = env->NewStringUTF(location);
107
108 service = env->CallStaticObjectMethod(mediahttpclass, mediaHttpCreateMethod, jloc);
109 env->DeleteLocalRef(jloc);
110
111 sp<IMediaHTTPService> httpService;
112 if (service != NULL) {
113 sp<IBinder> binder = ibinderForJavaObject(env, service);
114 httpService = interface_cast<IMediaHTTPService>(binder);
115 }
116
Marco Nelissene419d7c2014-05-15 14:17:25 -0700117 status_t err = mData->mImpl->setDataSource(httpService, location, NULL);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700118 env->ExceptionClear();
Marco Nelissene419d7c2014-05-15 14:17:25 -0700119 return translate_error(err);
Marco Nelissen0c3be872014-05-01 10:14:44 -0700120}
121
Marco Nelissen3425fd52014-05-14 11:12:46 -0700122EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700123size_t AMediaExtractor_getTrackCount(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700124 return mData->mImpl->countTracks();
125}
126
Marco Nelissen3425fd52014-05-14 11:12:46 -0700127EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700128AMediaFormat* AMediaExtractor_getTrackFormat(AMediaExtractor *mData, size_t idx) {
129 sp<AMessage> format;
130 mData->mImpl->getTrackFormat(idx, &format);
131 return AMediaFormat_fromMsg(&format);
132}
133
Marco Nelissen3425fd52014-05-14 11:12:46 -0700134EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700135media_status_t AMediaExtractor_selectTrack(AMediaExtractor *mData, size_t idx) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700136 ALOGV("selectTrack(%z)", idx);
137 return translate_error(mData->mImpl->selectTrack(idx));
138}
139
Marco Nelissen3425fd52014-05-14 11:12:46 -0700140EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700141media_status_t AMediaExtractor_unselectTrack(AMediaExtractor *mData, size_t idx) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700142 ALOGV("unselectTrack(%z)", idx);
143 return translate_error(mData->mImpl->unselectTrack(idx));
144}
145
Marco Nelissen3425fd52014-05-14 11:12:46 -0700146EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700147bool AMediaExtractor_advance(AMediaExtractor *mData) {
148 //ALOGV("advance");
149 return mData->mImpl->advance();
150}
151
Marco Nelissen3425fd52014-05-14 11:12:46 -0700152EXPORT
Marco Nelissen79e2b622014-05-16 08:07:28 -0700153media_status_t AMediaExtractor_seekTo(AMediaExtractor *ex, int64_t seekPosUs, SeekMode mode) {
154 android::MediaSource::ReadOptions::SeekMode sfmode;
155 if (mode == AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC) {
156 sfmode = android::MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC;
157 } else if (mode == AMEDIAEXTRACTOR_SEEK_CLOSEST_SYNC) {
158 sfmode = android::MediaSource::ReadOptions::SEEK_CLOSEST_SYNC;
159 } else {
160 sfmode = android::MediaSource::ReadOptions::SEEK_NEXT_SYNC;
161 }
162
163 return translate_error(ex->mImpl->seekTo(seekPosUs, sfmode));
164}
165
166EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700167ssize_t AMediaExtractor_readSampleData(AMediaExtractor *mData, uint8_t *buffer, size_t capacity) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700168 //ALOGV("readSampleData");
169 sp<ABuffer> tmp = new ABuffer(buffer, capacity);
170 if (mData->mImpl->readSampleData(tmp) == OK) {
171 return tmp->size();
172 }
173 return -1;
174}
175
Marco Nelissen3425fd52014-05-14 11:12:46 -0700176EXPORT
Marco Nelissene419d7c2014-05-15 14:17:25 -0700177uint32_t AMediaExtractor_getSampleFlags(AMediaExtractor *mData) {
Marco Nelissen0c3be872014-05-01 10:14:44 -0700178 int sampleFlags = 0;
179 sp<MetaData> meta;
180 status_t err = mData->mImpl->getSampleMeta(&meta);
181 if (err != OK) {
182 return -1;
183 }
184 int32_t val;
185 if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700186 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_SYNC;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700187 }
188
189 uint32_t type;
190 const void *data;
191 size_t size;
192 if (meta->findData(kKeyEncryptedSizes, &type, &data, &size)) {
Marco Nelissene419d7c2014-05-15 14:17:25 -0700193 sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_ENCRYPTED;
Marco Nelissen0c3be872014-05-01 10:14:44 -0700194 }
195 return sampleFlags;
196}
197
Marco Nelissen3425fd52014-05-14 11:12:46 -0700198EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700199int AMediaExtractor_getSampleTrackIndex(AMediaExtractor *mData) {
200 size_t idx;
201 if (mData->mImpl->getSampleTrackIndex(&idx) != OK) {
202 return -1;
203 }
204 return idx;
205}
206
Marco Nelissen3425fd52014-05-14 11:12:46 -0700207EXPORT
Marco Nelissen0c3be872014-05-01 10:14:44 -0700208int64_t AMediaExtractor_getSampletime(AMediaExtractor *mData) {
209 int64_t time;
210 if (mData->mImpl->getSampleTime(&time) != OK) {
211 return -1;
212 }
213 return time;
214}
215
Marco Nelissen3425fd52014-05-14 11:12:46 -0700216EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700217PsshInfo* AMediaExtractor_getPsshInfo(AMediaExtractor *ex) {
218
219 if (ex->mPsshBuf != NULL) {
220 return (PsshInfo*) ex->mPsshBuf->data();
221 }
222
223 sp<AMessage> format;
224 ex->mImpl->getFileFormat(&format);
225 sp<ABuffer> buffer;
226 if(!format->findBuffer("pssh", &buffer)) {
227 return NULL;
228 }
229
230 // the format of the buffer is 1 or more of:
231 // {
232 // 16 byte uuid
233 // 4 byte data length N
234 // N bytes of data
235 // }
236
237 // Determine the number of entries in the source data.
238 // Since we got the data from stagefright, we trust it is valid and properly formatted.
239 const uint8_t* data = buffer->data();
240 size_t len = buffer->size();
241 size_t numentries = 0;
242 while (len > 0) {
243 numentries++;
244
245 // skip uuid
246 data += 16;
247 len -= 16;
248
249 // get data length
250 uint32_t datalen = *((uint32_t*)data);
251 data += 4;
252 len -= 4;
253
254 // skip the data
255 data += datalen;
256 len -= datalen;
257 }
258
259 // there are <numentries> in the buffer, we need
260 // (source buffer size) + 4 + (4 * numentries) bytes for the PsshInfo structure
261 size_t newsize = buffer->size() + 4 + (4 * numentries);
262 ex->mPsshBuf = new ABuffer(newsize);
263 ex->mPsshBuf->setRange(0, newsize);
264
265 // copy data
266 const uint8_t* src = buffer->data();
267 uint8_t* dst = ex->mPsshBuf->data();
268 uint8_t* dstdata = dst + 4 + numentries * sizeof(PsshEntry);
269 *((uint32_t*)dst) = numentries;
270 dst += 4;
271 for (size_t i = 0; i < numentries; i++) {
272 // copy uuid
273 memcpy(dst, src, 16);
274 src += 16;
275 dst += 16;
276
277 // get/copy data length
278 uint32_t datalen = *((uint32_t*)src);
279 memcpy(dst, src, 4);
280 src += 4;
281 dst += 4;
282
283 // the next entry in the destination is a pointer to the actual data, which we store
284 // after the array of PsshEntry
285 memcpy(dst, &dstdata, sizeof(dstdata));
286 dst += 4;
287
288 // copy the actual data
289 memcpy(dstdata, src, datalen);
290 dstdata += datalen;
291 src += datalen;
292 }
293
294 return (PsshInfo*) ex->mPsshBuf->data();
295}
296
Marco Nelissen3425fd52014-05-14 11:12:46 -0700297EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700298AMediaCodecCryptoInfo *AMediaExtractor_getSampleCryptoInfo(AMediaExtractor *ex) {
299 sp<MetaData> meta;
300 if(ex->mImpl->getSampleMeta(&meta) != 0) {
301 return NULL;
302 }
303
304 uint32_t type;
305 const void *crypteddata;
306 size_t cryptedsize;
307 if (!meta->findData(kKeyEncryptedSizes, &type, &crypteddata, &cryptedsize)) {
308 return NULL;
309 }
310 size_t numSubSamples = cryptedsize / sizeof(size_t);
311
312 const void *cleardata;
313 size_t clearsize;
314 if (meta->findData(kKeyPlainSizes, &type, &cleardata, &clearsize)) {
315 if (clearsize != cryptedsize) {
316 // The two must be of the same length.
317 return NULL;
318 }
319 }
320
321 const void *key;
322 size_t keysize;
323 if (meta->findData(kKeyCryptoIV, &type, &key, &keysize)) {
324 if (keysize != 16) {
325 // IVs must be 16 bytes in length.
326 return NULL;
327 }
328 }
329
330 const void *iv;
331 size_t ivsize;
332 if (meta->findData(kKeyCryptoIV, &type, &iv, &ivsize)) {
333 if (ivsize != 16) {
334 // IVs must be 16 bytes in length.
335 return NULL;
336 }
337 }
338
339 int32_t mode;
340 if (!meta->findInt32(kKeyCryptoMode, &mode)) {
341 mode = CryptoPlugin::kMode_AES_CTR;
342 }
343
344 return AMediaCodecCryptoInfo_new(
345 numSubSamples,
346 (uint8_t*) key,
347 (uint8_t*) iv,
Marco Nelissen79e2b622014-05-16 08:07:28 -0700348 (cryptoinfo_mode_t) mode,
Marco Nelissen050eb322014-05-09 15:10:23 -0700349 (size_t*) cleardata,
350 (size_t*) crypteddata);
351}
352
Marco Nelissen0c3be872014-05-01 10:14:44 -0700353
354} // extern "C"
355