OSDN Git Service

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