1 /*
2 * Copyright 2012, 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 "MediaExtractor-JNI"
19 #include <utils/Log.h>
20
21 #include "android_media_AudioPresentation.h"
22 #include "android_media_MediaDataSource.h"
23 #include "android_media_MediaExtractor.h"
24 #include "android_media_MediaMetricsJNI.h"
25 #include "android_media_Streams.h"
26 #include "android_os_HwRemoteBinder.h"
27 #include "android_runtime/AndroidRuntime.h"
28 #include "android_runtime/Log.h"
29 #include "android_util_Binder.h"
30 #include "jni.h"
31 #include <nativehelper/JNIPlatformHelp.h>
32
33 #include <android/hardware/cas/1.0/BpHwCas.h>
34 #include <android/hardware/cas/1.0/BnHwCas.h>
35 #include <hidl/HybridInterface.h>
36 #include <media/IMediaHTTPService.h>
37 #include <media/hardware/CryptoAPI.h>
38 #include <media/stagefright/foundation/ABuffer.h>
39 #include <media/stagefright/foundation/ADebug.h>
40 #include <media/stagefright/foundation/AMessage.h>
41 #include <media/DataSource.h>
42 #include <media/stagefright/InterfaceUtils.h>
43 #include <media/stagefright/MediaErrors.h>
44 #include <media/stagefright/MetaData.h>
45 #include <media/stagefright/NuMediaExtractor.h>
46 #include <nativehelper/ScopedLocalRef.h>
47
48 namespace android {
49
50 using namespace hardware::cas::V1_0;
51
52 struct fields_t {
53 jfieldID context;
54
55 jmethodID cryptoInfoSetID;
56 jmethodID cryptoInfoSetPatternID;
57 };
58
59 static fields_t gFields;
60 static JAudioPresentationInfo::fields_t gAudioPresentationFields;
61
JMediaExtractor(JNIEnv * env,jobject thiz)62 JMediaExtractor::JMediaExtractor(JNIEnv *env, jobject thiz)
63 : mClass(NULL),
64 mObject(NULL) {
65 jclass clazz = env->GetObjectClass(thiz);
66 CHECK(clazz != NULL);
67
68 mClass = (jclass)env->NewGlobalRef(clazz);
69 mObject = env->NewWeakGlobalRef(thiz);
70
71 mImpl = new NuMediaExtractor(NuMediaExtractor::EntryPoint::SDK);
72 }
73
~JMediaExtractor()74 JMediaExtractor::~JMediaExtractor() {
75 JNIEnv *env = AndroidRuntime::getJNIEnv();
76
77 env->DeleteWeakGlobalRef(mObject);
78 mObject = NULL;
79 env->DeleteGlobalRef(mClass);
80 mClass = NULL;
81 }
82
setDataSource(const sp<IMediaHTTPService> & httpService,const char * path,const KeyedVector<String8,String8> * headers)83 status_t JMediaExtractor::setDataSource(
84 const sp<IMediaHTTPService> &httpService,
85 const char *path,
86 const KeyedVector<String8, String8> *headers) {
87 return mImpl->setDataSource(httpService, path, headers);
88 }
89
setDataSource(int fd,off64_t offset,off64_t size)90 status_t JMediaExtractor::setDataSource(int fd, off64_t offset, off64_t size) {
91 return mImpl->setDataSource(fd, offset, size);
92 }
93
setDataSource(const sp<DataSource> & datasource)94 status_t JMediaExtractor::setDataSource(const sp<DataSource> &datasource) {
95 return mImpl->setDataSource(datasource);
96 }
97
setMediaCas(JNIEnv * env,jobject casBinderObj)98 status_t JMediaExtractor::setMediaCas(JNIEnv *env, jobject casBinderObj) {
99 if (casBinderObj == NULL) {
100 return BAD_VALUE;
101 }
102
103 sp<hardware::IBinder> hwBinder =
104 JHwRemoteBinder::GetNativeContext(env, casBinderObj)->getBinder();
105 if (hwBinder == NULL) {
106 return BAD_VALUE;
107 }
108
109 sp<ICas> cas = hardware::fromBinder<ICas, BpHwCas, BnHwCas>(hwBinder);
110 if (cas == NULL) {
111 return BAD_VALUE;
112 }
113
114 HalToken halToken;
115 if (!createHalToken(cas, &halToken)) {
116 return BAD_VALUE;
117 }
118
119 return mImpl->setMediaCas(halToken);
120 }
121
countTracks() const122 size_t JMediaExtractor::countTracks() const {
123 return mImpl->countTracks();
124 }
125
getTrackFormat(size_t index,jobject * format) const126 status_t JMediaExtractor::getTrackFormat(size_t index, jobject *format) const {
127 sp<AMessage> msg;
128 status_t err;
129 if ((err = mImpl->getTrackFormat(index, &msg)) != OK) {
130 return err;
131 }
132
133 JNIEnv *env = AndroidRuntime::getJNIEnv();
134
135 return ConvertMessageToMap(env, msg, format);
136 }
137
getFileFormat(jobject * format) const138 status_t JMediaExtractor::getFileFormat(jobject *format) const {
139 sp<AMessage> msg;
140 status_t err;
141 if ((err = mImpl->getFileFormat(&msg)) != OK) {
142 return err;
143 }
144
145 JNIEnv *env = AndroidRuntime::getJNIEnv();
146
147 return ConvertMessageToMap(env, msg, format);
148 }
149
selectTrack(size_t index)150 status_t JMediaExtractor::selectTrack(size_t index) {
151 return mImpl->selectTrack(index);
152 }
153
unselectTrack(size_t index)154 status_t JMediaExtractor::unselectTrack(size_t index) {
155 return mImpl->unselectTrack(index);
156 }
157
seekTo(int64_t timeUs,MediaSource::ReadOptions::SeekMode mode)158 status_t JMediaExtractor::seekTo(
159 int64_t timeUs, MediaSource::ReadOptions::SeekMode mode) {
160 return mImpl->seekTo(timeUs, mode);
161 }
162
advance()163 status_t JMediaExtractor::advance() {
164 return mImpl->advance();
165 }
166
readSampleData(jobject byteBuf,size_t offset,size_t * sampleSize)167 status_t JMediaExtractor::readSampleData(
168 jobject byteBuf, size_t offset, size_t *sampleSize) {
169 JNIEnv *env = AndroidRuntime::getJNIEnv();
170
171 void *dst = env->GetDirectBufferAddress(byteBuf);
172
173 size_t dstSize;
174 jbyteArray byteArray = NULL;
175
176 ScopedLocalRef<jclass> byteBufClass(env, env->FindClass("java/nio/ByteBuffer"));
177 CHECK(byteBufClass.get() != NULL);
178
179 if (dst == NULL) {
180 jmethodID arrayID =
181 env->GetMethodID(byteBufClass.get(), "array", "()[B");
182 CHECK(arrayID != NULL);
183
184 byteArray =
185 (jbyteArray)env->CallObjectMethod(byteBuf, arrayID);
186
187 if (byteArray == NULL) {
188 return INVALID_OPERATION;
189 }
190
191 jboolean isCopy;
192 dst = env->GetByteArrayElements(byteArray, &isCopy);
193
194 dstSize = (size_t) env->GetArrayLength(byteArray);
195 } else {
196 dstSize = (size_t) env->GetDirectBufferCapacity(byteBuf);
197 }
198
199 if (dstSize < offset) {
200 if (byteArray != NULL) {
201 env->ReleaseByteArrayElements(byteArray, (jbyte *)dst, 0);
202 }
203
204 return -ERANGE;
205 }
206
207 sp<ABuffer> buffer = new ABuffer((char *)dst + offset, dstSize - offset);
208
209 status_t err = mImpl->readSampleData(buffer);
210
211 if (byteArray != NULL) {
212 env->ReleaseByteArrayElements(byteArray, (jbyte *)dst, 0);
213 }
214
215 if (err != OK) {
216 return err;
217 }
218
219 *sampleSize = buffer->size();
220
221 jmethodID positionID = env->GetMethodID(
222 byteBufClass.get(), "position", "(I)Ljava/nio/Buffer;");
223
224 CHECK(positionID != NULL);
225
226 jmethodID limitID = env->GetMethodID(
227 byteBufClass.get(), "limit", "(I)Ljava/nio/Buffer;");
228
229 CHECK(limitID != NULL);
230
231 jobject me = env->CallObjectMethod(
232 byteBuf, limitID, offset + *sampleSize);
233 env->DeleteLocalRef(me);
234 me = env->CallObjectMethod(
235 byteBuf, positionID, offset);
236 env->DeleteLocalRef(me);
237 me = NULL;
238
239 return OK;
240 }
241
getSampleTrackIndex(size_t * trackIndex)242 status_t JMediaExtractor::getSampleTrackIndex(size_t *trackIndex) {
243 return mImpl->getSampleTrackIndex(trackIndex);
244 }
245
getSampleTime(int64_t * sampleTimeUs)246 status_t JMediaExtractor::getSampleTime(int64_t *sampleTimeUs) {
247 return mImpl->getSampleTime(sampleTimeUs);
248 }
249
getSampleSize(size_t * sampleSize)250 status_t JMediaExtractor::getSampleSize(size_t *sampleSize) {
251 return mImpl->getSampleSize(sampleSize);
252 }
253
getSampleFlags(uint32_t * sampleFlags)254 status_t JMediaExtractor::getSampleFlags(uint32_t *sampleFlags) {
255 *sampleFlags = 0;
256
257 sp<MetaData> meta;
258 status_t err = mImpl->getSampleMeta(&meta);
259
260 if (err != OK) {
261 return err;
262 }
263
264 int32_t val;
265 if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) {
266 (*sampleFlags) |= NuMediaExtractor::SAMPLE_FLAG_SYNC;
267 }
268
269 uint32_t type;
270 const void *data;
271 size_t size;
272 if (meta->findData(kKeyEncryptedSizes, &type, &data, &size)) {
273 (*sampleFlags) |= NuMediaExtractor::SAMPLE_FLAG_ENCRYPTED;
274 }
275
276 return OK;
277 }
278
getMetrics(Parcel * reply) const279 status_t JMediaExtractor::getMetrics(Parcel *reply) const {
280
281 status_t status = mImpl->getMetrics(reply);
282 return status;
283 }
284
getSampleMeta(sp<MetaData> * sampleMeta)285 status_t JMediaExtractor::getSampleMeta(sp<MetaData> *sampleMeta) {
286 return mImpl->getSampleMeta(sampleMeta);
287 }
288
getCachedDuration(int64_t * durationUs,bool * eos) const289 bool JMediaExtractor::getCachedDuration(int64_t *durationUs, bool *eos) const {
290 return mImpl->getCachedDuration(durationUs, eos);
291 }
292
getAudioPresentations(size_t trackIdx,AudioPresentationCollection * presentations) const293 status_t JMediaExtractor::getAudioPresentations(size_t trackIdx,
294 AudioPresentationCollection *presentations) const {
295 return mImpl->getAudioPresentations(trackIdx, presentations);
296 }
297
setLogSessionId(const String8 & LogSessionId)298 status_t JMediaExtractor::setLogSessionId(const String8 &LogSessionId) {
299 return mImpl->setLogSessionId(LogSessionId);
300 }
301 } // namespace android
302
303 ////////////////////////////////////////////////////////////////////////////////
304
305 using namespace android;
306
setMediaExtractor(JNIEnv * env,jobject thiz,const sp<JMediaExtractor> & extractor)307 static sp<JMediaExtractor> setMediaExtractor(
308 JNIEnv *env, jobject thiz, const sp<JMediaExtractor> &extractor) {
309 sp<JMediaExtractor> old =
310 (JMediaExtractor *)env->GetLongField(thiz, gFields.context);
311
312 if (extractor != NULL) {
313 extractor->incStrong(thiz);
314 }
315 if (old != NULL) {
316 old->decStrong(thiz);
317 }
318 env->SetLongField(thiz, gFields.context, (jlong)extractor.get());
319
320 return old;
321 }
322
getMediaExtractor(JNIEnv * env,jobject thiz)323 static sp<JMediaExtractor> getMediaExtractor(JNIEnv *env, jobject thiz) {
324 return (JMediaExtractor *)env->GetLongField(thiz, gFields.context);
325 }
326
android_media_MediaExtractor_release(JNIEnv * env,jobject thiz)327 static void android_media_MediaExtractor_release(JNIEnv *env, jobject thiz) {
328 setMediaExtractor(env, thiz, NULL);
329 }
330
android_media_MediaExtractor_getTrackCount(JNIEnv * env,jobject thiz)331 static jint android_media_MediaExtractor_getTrackCount(
332 JNIEnv *env, jobject thiz) {
333 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
334
335 if (extractor == NULL) {
336 jniThrowException(env, "java/lang/IllegalStateException", NULL);
337 return -1;
338 }
339
340 return (jint) extractor->countTracks();
341 }
342
android_media_MediaExtractor_getTrackFormatNative(JNIEnv * env,jobject thiz,jint index)343 static jobject android_media_MediaExtractor_getTrackFormatNative(
344 JNIEnv *env, jobject thiz, jint index) {
345 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
346
347 if (extractor == NULL) {
348 jniThrowException(env, "java/lang/IllegalStateException", NULL);
349 return NULL;
350 }
351
352 jobject format;
353 status_t err = extractor->getTrackFormat(index, &format);
354
355 if (err != OK) {
356 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
357 return NULL;
358 }
359
360 return format;
361 }
362
android_media_MediaExtractor_getFileFormatNative(JNIEnv * env,jobject thiz)363 static jobject android_media_MediaExtractor_getFileFormatNative(
364 JNIEnv *env, jobject thiz) {
365 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
366
367 if (extractor == NULL) {
368 jniThrowException(env, "java/lang/IllegalStateException", NULL);
369 return NULL;
370 }
371
372 jobject format;
373 status_t err = extractor->getFileFormat(&format);
374
375 if (err != OK) {
376 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
377 return NULL;
378 }
379
380 return format;
381 }
382
android_media_MediaExtractor_selectTrack(JNIEnv * env,jobject thiz,jint index)383 static void android_media_MediaExtractor_selectTrack(
384 JNIEnv *env, jobject thiz, jint index) {
385 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
386
387 if (extractor == NULL) {
388 jniThrowException(env, "java/lang/IllegalStateException", NULL);
389 return;
390 }
391
392 status_t err = extractor->selectTrack(index);
393
394 if (err != OK) {
395 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
396 return;
397 }
398 }
399
android_media_MediaExtractor_unselectTrack(JNIEnv * env,jobject thiz,jint index)400 static void android_media_MediaExtractor_unselectTrack(
401 JNIEnv *env, jobject thiz, jint index) {
402 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
403
404 if (extractor == NULL) {
405 jniThrowException(env, "java/lang/IllegalStateException", NULL);
406 return;
407 }
408
409 status_t err = extractor->unselectTrack(index);
410
411 if (err != OK) {
412 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
413 return;
414 }
415 }
416
android_media_MediaExtractor_seekTo(JNIEnv * env,jobject thiz,jlong timeUs,jint mode)417 static void android_media_MediaExtractor_seekTo(
418 JNIEnv *env, jobject thiz, jlong timeUs, jint mode) {
419 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
420
421 if (extractor == NULL) {
422 jniThrowException(env, "java/lang/IllegalStateException", NULL);
423 return;
424 }
425
426 if (mode < MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC
427 || mode >= MediaSource::ReadOptions::SEEK_CLOSEST) {
428 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
429 return;
430 }
431
432 extractor->seekTo(timeUs, (MediaSource::ReadOptions::SeekMode)mode);
433 }
434
android_media_MediaExtractor_advance(JNIEnv * env,jobject thiz)435 static jboolean android_media_MediaExtractor_advance(
436 JNIEnv *env, jobject thiz) {
437 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
438
439 if (extractor == NULL) {
440 jniThrowException(env, "java/lang/IllegalStateException", NULL);
441 return JNI_FALSE;
442 }
443
444 status_t err = extractor->advance();
445
446 if (err == ERROR_END_OF_STREAM) {
447 return JNI_FALSE;
448 } else if (err != OK) {
449 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
450 return JNI_FALSE;
451 }
452
453 return JNI_TRUE;
454 }
455
android_media_MediaExtractor_readSampleData(JNIEnv * env,jobject thiz,jobject byteBuf,jint offset)456 static jint android_media_MediaExtractor_readSampleData(
457 JNIEnv *env, jobject thiz, jobject byteBuf, jint offset) {
458 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
459
460 if (extractor == NULL) {
461 jniThrowException(env, "java/lang/IllegalStateException", NULL);
462 return -1;
463 }
464
465 size_t sampleSize;
466 status_t err = extractor->readSampleData(byteBuf, offset, &sampleSize);
467
468 if (err == ERROR_END_OF_STREAM) {
469 return -1;
470 } else if (err != OK) {
471 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
472 return -1;
473 }
474
475 return (jint) sampleSize;
476 }
477
android_media_MediaExtractor_getSampleTrackIndex(JNIEnv * env,jobject thiz)478 static jint android_media_MediaExtractor_getSampleTrackIndex(
479 JNIEnv *env, jobject thiz) {
480 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
481
482 if (extractor == NULL) {
483 jniThrowException(env, "java/lang/IllegalStateException", NULL);
484 return -1;
485 }
486
487 size_t trackIndex;
488 status_t err = extractor->getSampleTrackIndex(&trackIndex);
489
490 if (err == ERROR_END_OF_STREAM) {
491 return -1;
492 } else if (err != OK) {
493 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
494 return -1;
495 }
496
497 return (jint) trackIndex;
498 }
499
android_media_MediaExtractor_getSampleTime(JNIEnv * env,jobject thiz)500 static jlong android_media_MediaExtractor_getSampleTime(
501 JNIEnv *env, jobject thiz) {
502 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
503
504 if (extractor == NULL) {
505 jniThrowException(env, "java/lang/IllegalStateException", NULL);
506 return -1LL;
507 }
508
509 int64_t sampleTimeUs;
510 status_t err = extractor->getSampleTime(&sampleTimeUs);
511
512 if (err == ERROR_END_OF_STREAM) {
513 return -1LL;
514 } else if (err != OK) {
515 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
516 return -1LL;
517 }
518
519 return (jlong) sampleTimeUs;
520 }
521
android_media_MediaExtractor_getSampleSize(JNIEnv * env,jobject thiz)522 static jlong android_media_MediaExtractor_getSampleSize(
523 JNIEnv *env, jobject thiz) {
524 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
525
526 if (extractor == NULL) {
527 jniThrowException(env, "java/lang/IllegalStateException", NULL);
528 return -1LL;
529 }
530
531 size_t sampleSize;
532 status_t err = extractor->getSampleSize(&sampleSize);
533
534 if (err == ERROR_END_OF_STREAM) {
535 return -1LL;
536 } else if (err != OK) {
537 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
538 return -1LL;
539 }
540
541 return (jlong) sampleSize;
542 }
543
android_media_MediaExtractor_getSampleFlags(JNIEnv * env,jobject thiz)544 static jint android_media_MediaExtractor_getSampleFlags(
545 JNIEnv *env, jobject thiz) {
546 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
547
548 if (extractor == NULL) {
549 jniThrowException(env, "java/lang/IllegalStateException", NULL);
550 return -1;
551 }
552
553 uint32_t sampleFlags;
554 status_t err = extractor->getSampleFlags(&sampleFlags);
555
556 if (err == ERROR_END_OF_STREAM) {
557 return -1;
558 } else if (err != OK) {
559 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
560 return -1;
561 }
562
563 return (jint) sampleFlags;
564 }
565
android_media_MediaExtractor_getSampleCryptoInfo(JNIEnv * env,jobject thiz,jobject cryptoInfoObj)566 static jboolean android_media_MediaExtractor_getSampleCryptoInfo(
567 JNIEnv *env, jobject thiz, jobject cryptoInfoObj) {
568 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
569
570 if (extractor == NULL) {
571 jniThrowException(env, "java/lang/IllegalStateException", NULL);
572 return JNI_FALSE;
573 }
574
575 sp<MetaData> meta;
576 status_t err = extractor->getSampleMeta(&meta);
577
578 if (err != OK) {
579 return JNI_FALSE;
580 }
581
582 uint32_t type;
583 const void *data;
584 size_t size;
585 if (!meta->findData(kKeyEncryptedSizes, &type, &data, &size)) {
586 return JNI_FALSE;
587 }
588
589 size_t numSubSamples = size / sizeof(int32_t);
590
591 if (numSubSamples == 0) {
592 return JNI_FALSE;
593 }
594
595 jintArray numBytesOfEncryptedDataObj = env->NewIntArray(numSubSamples);
596 jboolean isCopy;
597 jint *dst = env->GetIntArrayElements(numBytesOfEncryptedDataObj, &isCopy);
598 for (size_t i = 0; i < numSubSamples; ++i) {
599 dst[i] = ((const int32_t *)data)[i];
600 }
601 env->ReleaseIntArrayElements(numBytesOfEncryptedDataObj, dst, 0);
602 dst = NULL;
603
604 size_t encSize = size;
605 jintArray numBytesOfPlainDataObj = NULL;
606 if (meta->findData(kKeyPlainSizes, &type, &data, &size)) {
607 if (size != encSize) {
608 // The two must be of the same length.
609 return JNI_FALSE;
610 }
611
612 numBytesOfPlainDataObj = env->NewIntArray(numSubSamples);
613 jboolean isCopy;
614 jint *dst = env->GetIntArrayElements(numBytesOfPlainDataObj, &isCopy);
615 for (size_t i = 0; i < numSubSamples; ++i) {
616 dst[i] = ((const int32_t *)data)[i];
617 }
618 env->ReleaseIntArrayElements(numBytesOfPlainDataObj, dst, 0);
619 dst = NULL;
620 }
621
622 jbyteArray keyObj = NULL;
623 if (meta->findData(kKeyCryptoKey, &type, &data, &size)) {
624 if (size != 16) {
625 // Keys must be 16 bytes in length.
626 return JNI_FALSE;
627 }
628
629 keyObj = env->NewByteArray(size);
630 jboolean isCopy;
631 jbyte *dst = env->GetByteArrayElements(keyObj, &isCopy);
632 memcpy(dst, data, size);
633 env->ReleaseByteArrayElements(keyObj, dst, 0);
634 dst = NULL;
635 }
636
637 jbyteArray ivObj = NULL;
638 if (meta->findData(kKeyCryptoIV, &type, &data, &size)) {
639 if (size != 16) {
640 // IVs must be 16 bytes in length.
641 return JNI_FALSE;
642 }
643
644 ivObj = env->NewByteArray(size);
645 jboolean isCopy;
646 jbyte *dst = env->GetByteArrayElements(ivObj, &isCopy);
647 memcpy(dst, data, size);
648 env->ReleaseByteArrayElements(ivObj, dst, 0);
649 dst = NULL;
650 }
651
652 int32_t mode;
653 if (!meta->findInt32(kKeyCryptoMode, &mode)) {
654 mode = CryptoPlugin::kMode_AES_CTR;
655 }
656
657 env->CallVoidMethod(
658 cryptoInfoObj,
659 gFields.cryptoInfoSetID,
660 (jint)numSubSamples,
661 numBytesOfPlainDataObj,
662 numBytesOfEncryptedDataObj,
663 keyObj,
664 ivObj,
665 mode);
666
667 int32_t encryptedByteBlock = 0, skipByteBlock = 0;
668 meta->findInt32(kKeyEncryptedByteBlock, &encryptedByteBlock);
669 meta->findInt32(kKeySkipByteBlock, &skipByteBlock);
670
671 env->CallVoidMethod(
672 cryptoInfoObj,
673 gFields.cryptoInfoSetPatternID,
674 encryptedByteBlock,
675 skipByteBlock);
676
677 return JNI_TRUE;
678 }
679
android_media_MediaExtractor_getAudioPresentations(JNIEnv * env,jobject thiz,jint trackIdx)680 static jobject android_media_MediaExtractor_getAudioPresentations(
681 JNIEnv *env, jobject thiz, jint trackIdx) {
682 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
683 jobject presentationsJObj = JAudioPresentationInfo::asJobject(env, gAudioPresentationFields);
684 if (extractor == NULL) {
685 jniThrowException(env, "java/lang/IllegalStateException", NULL);
686 return presentationsJObj;
687 }
688 AudioPresentationCollection presentations;
689 status_t err = extractor->getAudioPresentations(trackIdx, &presentations);
690 if (err == ERROR_END_OF_STREAM || err == ERROR_UNSUPPORTED) {
691 return presentationsJObj;
692 } else if (err != OK) {
693 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
694 return presentationsJObj;
695 }
696
697 JAudioPresentationInfo::addPresentations(
698 env, gAudioPresentationFields, presentations, presentationsJObj);
699 return presentationsJObj;
700 }
701
android_media_MediaExtractor_native_init(JNIEnv * env)702 static void android_media_MediaExtractor_native_init(JNIEnv *env) {
703 jclass clazz = env->FindClass("android/media/MediaExtractor");
704 CHECK(clazz != NULL);
705
706 gFields.context = env->GetFieldID(clazz, "mNativeContext", "J");
707 CHECK(gFields.context != NULL);
708
709 clazz = env->FindClass("android/media/MediaCodec$CryptoInfo");
710 CHECK(clazz != NULL);
711
712 gFields.cryptoInfoSetID =
713 env->GetMethodID(clazz, "set", "(I[I[I[B[BI)V");
714
715 gFields.cryptoInfoSetPatternID =
716 env->GetMethodID(clazz, "setPattern", "(II)V");
717
718 gAudioPresentationFields.init(env);
719 }
720
android_media_MediaExtractor_native_setup(JNIEnv * env,jobject thiz)721 static void android_media_MediaExtractor_native_setup(
722 JNIEnv *env, jobject thiz) {
723 sp<JMediaExtractor> extractor = new JMediaExtractor(env, thiz);
724 setMediaExtractor(env,thiz, extractor);
725 }
726
android_media_MediaExtractor_setDataSource(JNIEnv * env,jobject thiz,jobject httpServiceBinderObj,jstring pathObj,jobjectArray keysArray,jobjectArray valuesArray)727 static void android_media_MediaExtractor_setDataSource(
728 JNIEnv *env, jobject thiz,
729 jobject httpServiceBinderObj,
730 jstring pathObj,
731 jobjectArray keysArray,
732 jobjectArray valuesArray) {
733 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
734
735 if (extractor == NULL) {
736 jniThrowException(env, "java/lang/IllegalStateException", NULL);
737 return;
738 }
739
740 if (pathObj == NULL) {
741 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
742 return;
743 }
744
745 KeyedVector<String8, String8> headers;
746 if (!ConvertKeyValueArraysToKeyedVector(
747 env, keysArray, valuesArray, &headers)) {
748 return;
749 }
750
751 const char *path = env->GetStringUTFChars(pathObj, NULL);
752
753 if (path == NULL) {
754 return;
755 }
756
757 sp<IMediaHTTPService> httpService;
758 if (httpServiceBinderObj != NULL) {
759 sp<IBinder> binder = ibinderForJavaObject(env, httpServiceBinderObj);
760 httpService = interface_cast<IMediaHTTPService>(binder);
761 }
762
763 status_t err = extractor->setDataSource(httpService, path, &headers);
764
765 env->ReleaseStringUTFChars(pathObj, path);
766 path = NULL;
767
768 if (err != OK) {
769 jniThrowException(
770 env,
771 "java/io/IOException",
772 "Failed to instantiate extractor.");
773 return;
774 }
775 }
776
android_media_MediaExtractor_setDataSourceFd(JNIEnv * env,jobject thiz,jobject fileDescObj,jlong offset,jlong length)777 static void android_media_MediaExtractor_setDataSourceFd(
778 JNIEnv *env, jobject thiz,
779 jobject fileDescObj, jlong offset, jlong length) {
780 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
781
782 if (extractor == NULL) {
783 jniThrowException(env, "java/lang/IllegalStateException", NULL);
784 return;
785 }
786
787 if (fileDescObj == NULL) {
788 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
789 return;
790 }
791
792 int fd = jniGetFDFromFileDescriptor(env, fileDescObj);
793
794 status_t err = extractor->setDataSource(fd, offset, length);
795
796 if (err != OK) {
797 jniThrowException(
798 env,
799 "java/io/IOException",
800 "Failed to instantiate extractor.");
801 return;
802 }
803 }
804
android_media_MediaExtractor_setDataSourceCallback(JNIEnv * env,jobject thiz,jobject callbackObj)805 static void android_media_MediaExtractor_setDataSourceCallback(
806 JNIEnv *env, jobject thiz,
807 jobject callbackObj) {
808 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
809
810 if (extractor == NULL) {
811 jniThrowException(env, "java/lang/IllegalStateException", NULL);
812 return;
813 }
814
815 if (callbackObj == NULL) {
816 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
817 return;
818 }
819
820 sp<DataSource> bridge =
821 CreateDataSourceFromIDataSource(new JMediaDataSource(env, callbackObj));
822 status_t err = extractor->setDataSource(bridge);
823
824 if (err != OK) {
825 // Clear bridge so that JMediaDataSource::close() is called _before_
826 // we throw the IOException.
827 // Otherwise close() gets called when we go out of scope, it calls
828 // Java with a pending exception and crashes the process.
829 bridge.clear();
830 jniThrowException(
831 env,
832 "java/io/IOException",
833 "Failed to instantiate extractor.");
834 return;
835 }
836 }
837
android_media_MediaExtractor_setMediaCas(JNIEnv * env,jobject thiz,jobject casBinderObj)838 static void android_media_MediaExtractor_setMediaCas(
839 JNIEnv *env, jobject thiz, jobject casBinderObj) {
840 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
841
842 if (extractor == NULL) {
843 jniThrowException(env, "java/lang/IllegalStateException", NULL);
844 return;
845 }
846
847 status_t err = extractor->setMediaCas(env, casBinderObj);
848
849 if (err != OK) {
850 extractor.clear();
851 jniThrowException(
852 env,
853 "java/lang/IllegalArgumentException",
854 "Failed to set MediaCas on extractor.");
855 }
856 }
857
android_media_MediaExtractor_getCachedDurationUs(JNIEnv * env,jobject thiz)858 static jlong android_media_MediaExtractor_getCachedDurationUs(
859 JNIEnv *env, jobject thiz) {
860 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
861
862 if (extractor == NULL) {
863 jniThrowException(env, "java/lang/IllegalStateException", NULL);
864 return -1LL;
865 }
866
867 int64_t cachedDurationUs;
868 bool eos;
869 if (!extractor->getCachedDuration(&cachedDurationUs, &eos)) {
870 return -1LL;
871 }
872
873 return (jlong) cachedDurationUs;
874 }
875
android_media_MediaExtractor_hasCacheReachedEOS(JNIEnv * env,jobject thiz)876 static jboolean android_media_MediaExtractor_hasCacheReachedEOS(
877 JNIEnv *env, jobject thiz) {
878 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
879
880 if (extractor == NULL) {
881 jniThrowException(env, "java/lang/IllegalStateException", NULL);
882 return JNI_TRUE;
883 }
884
885 int64_t cachedDurationUs;
886 bool eos;
887 if (!extractor->getCachedDuration(&cachedDurationUs, &eos)) {
888 return JNI_TRUE;
889 }
890
891 return eos ? JNI_TRUE : JNI_FALSE;
892 }
893
android_media_MediaExtractor_native_finalize(JNIEnv * env,jobject thiz)894 static void android_media_MediaExtractor_native_finalize(
895 JNIEnv *env, jobject thiz) {
896 android_media_MediaExtractor_release(env, thiz);
897 }
898
899 static jobject
android_media_MediaExtractor_native_getMetrics(JNIEnv * env,jobject thiz)900 android_media_MediaExtractor_native_getMetrics(JNIEnv * env, jobject thiz)
901 {
902 ALOGV("android_media_MediaExtractor_native_getMetrics");
903
904 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
905 if (extractor == NULL ) {
906 jniThrowException(env, "java/lang/IllegalStateException", NULL);
907 return NULL;
908 }
909
910 // get what we have for the metrics from the codec
911 Parcel reply;
912 status_t err = extractor->getMetrics(&reply);
913 if (err != OK) {
914 ALOGE("getMetrics failed");
915 return (jobject) NULL;
916 }
917
918 // build and return the Bundle
919 std::unique_ptr<mediametrics::Item> item(mediametrics::Item::create());
920 item->readFromParcel(reply);
921 jobject mybundle = MediaMetricsJNI::writeMetricsToBundle(env, item.get(), NULL);
922
923 return mybundle;
924 }
925
926 static void
android_media_MediaExtractor_native_setLogSessionId(JNIEnv * env,jobject thiz,jstring logSessionIdJString)927 android_media_MediaExtractor_native_setLogSessionId(
928 JNIEnv * env, jobject thiz, jstring logSessionIdJString)
929 {
930 ALOGV("android_media_MediaExtractor_native_setLogSessionId");
931
932 sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
933 if (extractor == nullptr) {
934 jniThrowException(env, "java/lang/IllegalStateException", nullptr);
935 }
936
937 const char* logSessionId = env->GetStringUTFChars(logSessionIdJString, nullptr);
938 if (extractor->setLogSessionId(String8(logSessionId)) != OK) {
939 ALOGE("setLogSessionId failed");
940 }
941 env->ReleaseStringUTFChars(logSessionIdJString, logSessionId);
942 }
943
944 static const JNINativeMethod gMethods[] = {
945 { "release", "()V", (void *)android_media_MediaExtractor_release },
946
947 { "getTrackCount", "()I", (void *)android_media_MediaExtractor_getTrackCount },
948
949 { "getFileFormatNative", "()Ljava/util/Map;",
950 (void *)android_media_MediaExtractor_getFileFormatNative },
951
952 { "getTrackFormatNative", "(I)Ljava/util/Map;",
953 (void *)android_media_MediaExtractor_getTrackFormatNative },
954
955 { "selectTrack", "(I)V", (void *)android_media_MediaExtractor_selectTrack },
956
957 { "unselectTrack", "(I)V",
958 (void *)android_media_MediaExtractor_unselectTrack },
959
960 { "seekTo", "(JI)V", (void *)android_media_MediaExtractor_seekTo },
961
962 { "advance", "()Z", (void *)android_media_MediaExtractor_advance },
963
964 { "readSampleData", "(Ljava/nio/ByteBuffer;I)I",
965 (void *)android_media_MediaExtractor_readSampleData },
966
967 { "getSampleTrackIndex", "()I",
968 (void *)android_media_MediaExtractor_getSampleTrackIndex },
969
970 { "getSampleTime", "()J",
971 (void *)android_media_MediaExtractor_getSampleTime },
972
973 { "getSampleSize", "()J",
974 (void *)android_media_MediaExtractor_getSampleSize },
975
976 { "getSampleFlags", "()I",
977 (void *)android_media_MediaExtractor_getSampleFlags },
978
979 { "getSampleCryptoInfo", "(Landroid/media/MediaCodec$CryptoInfo;)Z",
980 (void *)android_media_MediaExtractor_getSampleCryptoInfo },
981
982 { "native_init", "()V", (void *)android_media_MediaExtractor_native_init },
983
984 { "native_setup", "()V",
985 (void *)android_media_MediaExtractor_native_setup },
986
987 { "native_finalize", "()V",
988 (void *)android_media_MediaExtractor_native_finalize },
989
990 { "nativeSetDataSource",
991 "(Landroid/os/IBinder;Ljava/lang/String;[Ljava/lang/String;"
992 "[Ljava/lang/String;)V",
993 (void *)android_media_MediaExtractor_setDataSource },
994
995 { "setDataSource", "(Ljava/io/FileDescriptor;JJ)V",
996 (void *)android_media_MediaExtractor_setDataSourceFd },
997
998 { "setDataSource", "(Landroid/media/MediaDataSource;)V",
999 (void *)android_media_MediaExtractor_setDataSourceCallback },
1000
1001 { "nativeSetMediaCas", "(Landroid/os/IHwBinder;)V",
1002 (void *)android_media_MediaExtractor_setMediaCas },
1003
1004 { "getCachedDuration", "()J",
1005 (void *)android_media_MediaExtractor_getCachedDurationUs },
1006
1007 { "hasCacheReachedEndOfStream", "()Z",
1008 (void *)android_media_MediaExtractor_hasCacheReachedEOS },
1009
1010 {"native_getMetrics", "()Landroid/os/PersistableBundle;",
1011 (void *)android_media_MediaExtractor_native_getMetrics},
1012
1013 { "native_setLogSessionId", "(Ljava/lang/String;)V",
1014 (void *)android_media_MediaExtractor_native_setLogSessionId},
1015
1016 { "native_getAudioPresentations", "(I)Ljava/util/List;",
1017 (void *)android_media_MediaExtractor_getAudioPresentations },
1018 };
1019
register_android_media_MediaExtractor(JNIEnv * env)1020 int register_android_media_MediaExtractor(JNIEnv *env) {
1021 return AndroidRuntime::registerNativeMethods(env,
1022 "android/media/MediaExtractor", gMethods, NELEM(gMethods));
1023 }
1024