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