1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000 |
- /*
- * Copyright 2012, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- //#define LOG_NDEBUG 0
- #define LOG_TAG "MediaExtractor-JNI"
- #include <utils/Log.h>
- #include "android_media_AudioPresentation.h"
- #include "android_media_MediaDataSource.h"
- #include "android_media_MediaExtractor.h"
- #include "android_media_MediaMetricsJNI.h"
- #include "android_media_Streams.h"
- #include "android_os_HwRemoteBinder.h"
- #include "android_runtime/AndroidRuntime.h"
- #include "android_runtime/Log.h"
- #include "android_util_Binder.h"
- #include "jni.h"
- #include <nativehelper/JNIHelp.h>
- #include <android/hardware/cas/1.0/BpHwCas.h>
- #include <android/hardware/cas/1.0/BnHwCas.h>
- #include <hidl/HybridInterface.h>
- #include <media/IMediaHTTPService.h>
- #include <media/hardware/CryptoAPI.h>
- #include <media/stagefright/foundation/ABuffer.h>
- #include <media/stagefright/foundation/ADebug.h>
- #include <media/stagefright/foundation/AMessage.h>
- #include <media/DataSource.h>
- #include <media/stagefright/InterfaceUtils.h>
- #include <media/stagefright/MediaErrors.h>
- #include <media/stagefright/MetaData.h>
- #include <media/stagefright/NuMediaExtractor.h>
- #include <nativehelper/ScopedLocalRef.h>
- namespace android {
- using namespace hardware::cas::V1_0;
- struct fields_t {
- jfieldID context;
- jmethodID cryptoInfoSetID;
- jmethodID cryptoInfoSetPatternID;
- };
- static fields_t gFields;
- static JAudioPresentationInfo::fields_t gAudioPresentationFields;
- JMediaExtractor::JMediaExtractor(JNIEnv *env, jobject thiz)
- : mClass(NULL),
- mObject(NULL) {
- jclass clazz = env->GetObjectClass(thiz);
- CHECK(clazz != NULL);
- mClass = (jclass)env->NewGlobalRef(clazz);
- mObject = env->NewWeakGlobalRef(thiz);
- mImpl = new NuMediaExtractor;
- }
- JMediaExtractor::~JMediaExtractor() {
- JNIEnv *env = AndroidRuntime::getJNIEnv();
- env->DeleteWeakGlobalRef(mObject);
- mObject = NULL;
- env->DeleteGlobalRef(mClass);
- mClass = NULL;
- }
- status_t JMediaExtractor::setDataSource(
- const sp<IMediaHTTPService> &httpService,
- const char *path,
- const KeyedVector<String8, String8> *headers) {
- return mImpl->setDataSource(httpService, path, headers);
- }
- status_t JMediaExtractor::setDataSource(int fd, off64_t offset, off64_t size) {
- return mImpl->setDataSource(fd, offset, size);
- }
- status_t JMediaExtractor::setDataSource(const sp<DataSource> &datasource) {
- return mImpl->setDataSource(datasource);
- }
- status_t JMediaExtractor::setMediaCas(JNIEnv *env, jobject casBinderObj) {
- if (casBinderObj == NULL) {
- return BAD_VALUE;
- }
- sp<hardware::IBinder> hwBinder =
- JHwRemoteBinder::GetNativeContext(env, casBinderObj)->getBinder();
- if (hwBinder == NULL) {
- return BAD_VALUE;
- }
- sp<ICas> cas = hardware::fromBinder<ICas, BpHwCas, BnHwCas>(hwBinder);
- if (cas == NULL) {
- return BAD_VALUE;
- }
- HalToken halToken;
- if (!createHalToken(cas, &halToken)) {
- return BAD_VALUE;
- }
- return mImpl->setMediaCas(halToken);
- }
- size_t JMediaExtractor::countTracks() const {
- return mImpl->countTracks();
- }
- status_t JMediaExtractor::getTrackFormat(size_t index, jobject *format) const {
- sp<AMessage> msg;
- status_t err;
- if ((err = mImpl->getTrackFormat(index, &msg)) != OK) {
- return err;
- }
- JNIEnv *env = AndroidRuntime::getJNIEnv();
- return ConvertMessageToMap(env, msg, format);
- }
- status_t JMediaExtractor::getFileFormat(jobject *format) const {
- sp<AMessage> msg;
- status_t err;
- if ((err = mImpl->getFileFormat(&msg)) != OK) {
- return err;
- }
- JNIEnv *env = AndroidRuntime::getJNIEnv();
- return ConvertMessageToMap(env, msg, format);
- }
- status_t JMediaExtractor::selectTrack(size_t index) {
- return mImpl->selectTrack(index);
- }
- status_t JMediaExtractor::unselectTrack(size_t index) {
- return mImpl->unselectTrack(index);
- }
- status_t JMediaExtractor::seekTo(
- int64_t timeUs, MediaSource::ReadOptions::SeekMode mode) {
- return mImpl->seekTo(timeUs, mode);
- }
- status_t JMediaExtractor::advance() {
- return mImpl->advance();
- }
- status_t JMediaExtractor::readSampleData(
- jobject byteBuf, size_t offset, size_t *sampleSize) {
- JNIEnv *env = AndroidRuntime::getJNIEnv();
- void *dst = env->GetDirectBufferAddress(byteBuf);
- size_t dstSize;
- jbyteArray byteArray = NULL;
- ScopedLocalRef<jclass> byteBufClass(env, env->FindClass("java/nio/ByteBuffer"));
- CHECK(byteBufClass.get() != NULL);
- if (dst == NULL) {
- jmethodID arrayID =
- env->GetMethodID(byteBufClass.get(), "array", "()[B");
- CHECK(arrayID != NULL);
- byteArray =
- (jbyteArray)env->CallObjectMethod(byteBuf, arrayID);
- if (byteArray == NULL) {
- return INVALID_OPERATION;
- }
- jboolean isCopy;
- dst = env->GetByteArrayElements(byteArray, &isCopy);
- dstSize = (size_t) env->GetArrayLength(byteArray);
- } else {
- dstSize = (size_t) env->GetDirectBufferCapacity(byteBuf);
- }
- if (dstSize < offset) {
- if (byteArray != NULL) {
- env->ReleaseByteArrayElements(byteArray, (jbyte *)dst, 0);
- }
- return -ERANGE;
- }
- sp<ABuffer> buffer = new ABuffer((char *)dst + offset, dstSize - offset);
- status_t err = mImpl->readSampleData(buffer);
- if (byteArray != NULL) {
- env->ReleaseByteArrayElements(byteArray, (jbyte *)dst, 0);
- }
- if (err != OK) {
- return err;
- }
- *sampleSize = buffer->size();
- jmethodID positionID = env->GetMethodID(
- byteBufClass.get(), "position", "(I)Ljava/nio/Buffer;");
- CHECK(positionID != NULL);
- jmethodID limitID = env->GetMethodID(
- byteBufClass.get(), "limit", "(I)Ljava/nio/Buffer;");
- CHECK(limitID != NULL);
- jobject me = env->CallObjectMethod(
- byteBuf, limitID, offset + *sampleSize);
- env->DeleteLocalRef(me);
- me = env->CallObjectMethod(
- byteBuf, positionID, offset);
- env->DeleteLocalRef(me);
- me = NULL;
- return OK;
- }
- status_t JMediaExtractor::getSampleTrackIndex(size_t *trackIndex) {
- return mImpl->getSampleTrackIndex(trackIndex);
- }
- status_t JMediaExtractor::getSampleTime(int64_t *sampleTimeUs) {
- return mImpl->getSampleTime(sampleTimeUs);
- }
- status_t JMediaExtractor::getSampleSize(size_t *sampleSize) {
- return mImpl->getSampleSize(sampleSize);
- }
- status_t JMediaExtractor::getSampleFlags(uint32_t *sampleFlags) {
- *sampleFlags = 0;
- sp<MetaData> meta;
- status_t err = mImpl->getSampleMeta(&meta);
- if (err != OK) {
- return err;
- }
- int32_t val;
- if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) {
- (*sampleFlags) |= NuMediaExtractor::SAMPLE_FLAG_SYNC;
- }
- uint32_t type;
- const void *data;
- size_t size;
- if (meta->findData(kKeyEncryptedSizes, &type, &data, &size)) {
- (*sampleFlags) |= NuMediaExtractor::SAMPLE_FLAG_ENCRYPTED;
- }
- return OK;
- }
- status_t JMediaExtractor::getMetrics(Parcel *reply) const {
- status_t status = mImpl->getMetrics(reply);
- return status;
- }
- status_t JMediaExtractor::getSampleMeta(sp<MetaData> *sampleMeta) {
- return mImpl->getSampleMeta(sampleMeta);
- }
- bool JMediaExtractor::getCachedDuration(int64_t *durationUs, bool *eos) const {
- return mImpl->getCachedDuration(durationUs, eos);
- }
- status_t JMediaExtractor::getAudioPresentations(size_t trackIdx,
- AudioPresentationCollection *presentations) const {
- return mImpl->getAudioPresentations(trackIdx, presentations);
- }
- } // namespace android
- ////////////////////////////////////////////////////////////////////////////////
- using namespace android;
- static sp<JMediaExtractor> setMediaExtractor(
- JNIEnv *env, jobject thiz, const sp<JMediaExtractor> &extractor) {
- sp<JMediaExtractor> old =
- (JMediaExtractor *)env->GetLongField(thiz, gFields.context);
- if (extractor != NULL) {
- extractor->incStrong(thiz);
- }
- if (old != NULL) {
- old->decStrong(thiz);
- }
- env->SetLongField(thiz, gFields.context, (jlong)extractor.get());
- return old;
- }
- static sp<JMediaExtractor> getMediaExtractor(JNIEnv *env, jobject thiz) {
- return (JMediaExtractor *)env->GetLongField(thiz, gFields.context);
- }
- static void android_media_MediaExtractor_release(JNIEnv *env, jobject thiz) {
- setMediaExtractor(env, thiz, NULL);
- }
- static jint android_media_MediaExtractor_getTrackCount(
- JNIEnv *env, jobject thiz) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return -1;
- }
- return (jint) extractor->countTracks();
- }
- static jobject android_media_MediaExtractor_getTrackFormatNative(
- JNIEnv *env, jobject thiz, jint index) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return NULL;
- }
- jobject format;
- status_t err = extractor->getTrackFormat(index, &format);
- if (err != OK) {
- jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
- return NULL;
- }
- return format;
- }
- static jobject android_media_MediaExtractor_getFileFormatNative(
- JNIEnv *env, jobject thiz) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return NULL;
- }
- jobject format;
- status_t err = extractor->getFileFormat(&format);
- if (err != OK) {
- jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
- return NULL;
- }
- return format;
- }
- static void android_media_MediaExtractor_selectTrack(
- JNIEnv *env, jobject thiz, jint index) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return;
- }
- status_t err = extractor->selectTrack(index);
- if (err != OK) {
- jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
- return;
- }
- }
- static void android_media_MediaExtractor_unselectTrack(
- JNIEnv *env, jobject thiz, jint index) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return;
- }
- status_t err = extractor->unselectTrack(index);
- if (err != OK) {
- jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
- return;
- }
- }
- static void android_media_MediaExtractor_seekTo(
- JNIEnv *env, jobject thiz, jlong timeUs, jint mode) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return;
- }
- if (mode < MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC
- || mode >= MediaSource::ReadOptions::SEEK_CLOSEST) {
- jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
- return;
- }
- extractor->seekTo(timeUs, (MediaSource::ReadOptions::SeekMode)mode);
- }
- static jboolean android_media_MediaExtractor_advance(
- JNIEnv *env, jobject thiz) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return JNI_FALSE;
- }
- status_t err = extractor->advance();
- if (err == ERROR_END_OF_STREAM) {
- return JNI_FALSE;
- } else if (err != OK) {
- jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
- return JNI_FALSE;
- }
- return JNI_TRUE;
- }
- static jint android_media_MediaExtractor_readSampleData(
- JNIEnv *env, jobject thiz, jobject byteBuf, jint offset) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return -1;
- }
- size_t sampleSize;
- status_t err = extractor->readSampleData(byteBuf, offset, &sampleSize);
- if (err == ERROR_END_OF_STREAM) {
- return -1;
- } else if (err != OK) {
- jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
- return -1;
- }
- return (jint) sampleSize;
- }
- static jint android_media_MediaExtractor_getSampleTrackIndex(
- JNIEnv *env, jobject thiz) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return -1;
- }
- size_t trackIndex;
- status_t err = extractor->getSampleTrackIndex(&trackIndex);
- if (err == ERROR_END_OF_STREAM) {
- return -1;
- } else if (err != OK) {
- jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
- return -1;
- }
- return (jint) trackIndex;
- }
- static jlong android_media_MediaExtractor_getSampleTime(
- JNIEnv *env, jobject thiz) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return -1LL;
- }
- int64_t sampleTimeUs;
- status_t err = extractor->getSampleTime(&sampleTimeUs);
- if (err == ERROR_END_OF_STREAM) {
- return -1LL;
- } else if (err != OK) {
- jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
- return -1LL;
- }
- return (jlong) sampleTimeUs;
- }
- static jlong android_media_MediaExtractor_getSampleSize(
- JNIEnv *env, jobject thiz) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return -1LL;
- }
- size_t sampleSize;
- status_t err = extractor->getSampleSize(&sampleSize);
- if (err == ERROR_END_OF_STREAM) {
- return -1LL;
- } else if (err != OK) {
- jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
- return -1LL;
- }
- return (jlong) sampleSize;
- }
- static jint android_media_MediaExtractor_getSampleFlags(
- JNIEnv *env, jobject thiz) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return -1;
- }
- uint32_t sampleFlags;
- status_t err = extractor->getSampleFlags(&sampleFlags);
- if (err == ERROR_END_OF_STREAM) {
- return -1;
- } else if (err != OK) {
- jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
- return -1;
- }
- return (jint) sampleFlags;
- }
- static jboolean android_media_MediaExtractor_getSampleCryptoInfo(
- JNIEnv *env, jobject thiz, jobject cryptoInfoObj) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return JNI_FALSE;
- }
- sp<MetaData> meta;
- status_t err = extractor->getSampleMeta(&meta);
- if (err != OK) {
- return JNI_FALSE;
- }
- uint32_t type;
- const void *data;
- size_t size;
- if (!meta->findData(kKeyEncryptedSizes, &type, &data, &size)) {
- return JNI_FALSE;
- }
- size_t numSubSamples = size / sizeof(int32_t);
- if (numSubSamples == 0) {
- return JNI_FALSE;
- }
- jintArray numBytesOfEncryptedDataObj = env->NewIntArray(numSubSamples);
- jboolean isCopy;
- jint *dst = env->GetIntArrayElements(numBytesOfEncryptedDataObj, &isCopy);
- for (size_t i = 0; i < numSubSamples; ++i) {
- dst[i] = ((const int32_t *)data)[i];
- }
- env->ReleaseIntArrayElements(numBytesOfEncryptedDataObj, dst, 0);
- dst = NULL;
- size_t encSize = size;
- jintArray numBytesOfPlainDataObj = NULL;
- if (meta->findData(kKeyPlainSizes, &type, &data, &size)) {
- if (size != encSize) {
- // The two must be of the same length.
- return JNI_FALSE;
- }
- numBytesOfPlainDataObj = env->NewIntArray(numSubSamples);
- jboolean isCopy;
- jint *dst = env->GetIntArrayElements(numBytesOfPlainDataObj, &isCopy);
- for (size_t i = 0; i < numSubSamples; ++i) {
- dst[i] = ((const int32_t *)data)[i];
- }
- env->ReleaseIntArrayElements(numBytesOfPlainDataObj, dst, 0);
- dst = NULL;
- }
- jbyteArray keyObj = NULL;
- if (meta->findData(kKeyCryptoKey, &type, &data, &size)) {
- if (size != 16) {
- // Keys must be 16 bytes in length.
- return JNI_FALSE;
- }
- keyObj = env->NewByteArray(size);
- jboolean isCopy;
- jbyte *dst = env->GetByteArrayElements(keyObj, &isCopy);
- memcpy(dst, data, size);
- env->ReleaseByteArrayElements(keyObj, dst, 0);
- dst = NULL;
- }
- jbyteArray ivObj = NULL;
- if (meta->findData(kKeyCryptoIV, &type, &data, &size)) {
- if (size != 16) {
- // IVs must be 16 bytes in length.
- return JNI_FALSE;
- }
- ivObj = env->NewByteArray(size);
- jboolean isCopy;
- jbyte *dst = env->GetByteArrayElements(ivObj, &isCopy);
- memcpy(dst, data, size);
- env->ReleaseByteArrayElements(ivObj, dst, 0);
- dst = NULL;
- }
- int32_t mode;
- if (!meta->findInt32(kKeyCryptoMode, &mode)) {
- mode = CryptoPlugin::kMode_AES_CTR;
- }
- env->CallVoidMethod(
- cryptoInfoObj,
- gFields.cryptoInfoSetID,
- (jint)numSubSamples,
- numBytesOfPlainDataObj,
- numBytesOfEncryptedDataObj,
- keyObj,
- ivObj,
- mode);
- int32_t encryptedByteBlock = 0, skipByteBlock = 0;
- meta->findInt32(kKeyEncryptedByteBlock, &encryptedByteBlock);
- meta->findInt32(kKeySkipByteBlock, &skipByteBlock);
- env->CallVoidMethod(
- cryptoInfoObj,
- gFields.cryptoInfoSetPatternID,
- encryptedByteBlock,
- skipByteBlock);
- return JNI_TRUE;
- }
- static jobject android_media_MediaExtractor_getAudioPresentations(
- JNIEnv *env, jobject thiz, jint trackIdx) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- jobject presentationsJObj = JAudioPresentationInfo::asJobject(env, gAudioPresentationFields);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return presentationsJObj;
- }
- AudioPresentationCollection presentations;
- status_t err = extractor->getAudioPresentations(trackIdx, &presentations);
- if (err == ERROR_END_OF_STREAM || err == ERROR_UNSUPPORTED) {
- return presentationsJObj;
- } else if (err != OK) {
- jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
- return presentationsJObj;
- }
- JAudioPresentationInfo::addPresentations(
- env, gAudioPresentationFields, presentations, presentationsJObj);
- return presentationsJObj;
- }
- static void android_media_MediaExtractor_native_init(JNIEnv *env) {
- jclass clazz = env->FindClass("android/media/MediaExtractor");
- CHECK(clazz != NULL);
- gFields.context = env->GetFieldID(clazz, "mNativeContext", "J");
- CHECK(gFields.context != NULL);
- clazz = env->FindClass("android/media/MediaCodec$CryptoInfo");
- CHECK(clazz != NULL);
- gFields.cryptoInfoSetID =
- env->GetMethodID(clazz, "set", "(I[I[I[B[BI)V");
- gFields.cryptoInfoSetPatternID =
- env->GetMethodID(clazz, "setPattern", "(II)V");
- gAudioPresentationFields.init(env);
- }
- static void android_media_MediaExtractor_native_setup(
- JNIEnv *env, jobject thiz) {
- sp<JMediaExtractor> extractor = new JMediaExtractor(env, thiz);
- setMediaExtractor(env,thiz, extractor);
- }
- static void android_media_MediaExtractor_setDataSource(
- JNIEnv *env, jobject thiz,
- jobject httpServiceBinderObj,
- jstring pathObj,
- jobjectArray keysArray,
- jobjectArray valuesArray) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return;
- }
- if (pathObj == NULL) {
- jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
- return;
- }
- KeyedVector<String8, String8> headers;
- if (!ConvertKeyValueArraysToKeyedVector(
- env, keysArray, valuesArray, &headers)) {
- return;
- }
- const char *path = env->GetStringUTFChars(pathObj, NULL);
- if (path == NULL) {
- return;
- }
- sp<IMediaHTTPService> httpService;
- if (httpServiceBinderObj != NULL) {
- sp<IBinder> binder = ibinderForJavaObject(env, httpServiceBinderObj);
- httpService = interface_cast<IMediaHTTPService>(binder);
- }
- status_t err = extractor->setDataSource(httpService, path, &headers);
- env->ReleaseStringUTFChars(pathObj, path);
- path = NULL;
- if (err != OK) {
- jniThrowException(
- env,
- "java/io/IOException",
- "Failed to instantiate extractor.");
- return;
- }
- }
- static void android_media_MediaExtractor_setDataSourceFd(
- JNIEnv *env, jobject thiz,
- jobject fileDescObj, jlong offset, jlong length) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return;
- }
- if (fileDescObj == NULL) {
- jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
- return;
- }
- int fd = jniGetFDFromFileDescriptor(env, fileDescObj);
- status_t err = extractor->setDataSource(fd, offset, length);
- if (err != OK) {
- jniThrowException(
- env,
- "java/io/IOException",
- "Failed to instantiate extractor.");
- return;
- }
- }
- static void android_media_MediaExtractor_setDataSourceCallback(
- JNIEnv *env, jobject thiz,
- jobject callbackObj) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return;
- }
- if (callbackObj == NULL) {
- jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
- return;
- }
- sp<DataSource> bridge =
- CreateDataSourceFromIDataSource(new JMediaDataSource(env, callbackObj));
- status_t err = extractor->setDataSource(bridge);
- if (err != OK) {
- // Clear bridge so that JMediaDataSource::close() is called _before_
- // we throw the IOException.
- // Otherwise close() gets called when we go out of scope, it calls
- // Java with a pending exception and crashes the process.
- bridge.clear();
- jniThrowException(
- env,
- "java/io/IOException",
- "Failed to instantiate extractor.");
- return;
- }
- }
- static void android_media_MediaExtractor_setMediaCas(
- JNIEnv *env, jobject thiz, jobject casBinderObj) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return;
- }
- status_t err = extractor->setMediaCas(env, casBinderObj);
- if (err != OK) {
- extractor.clear();
- jniThrowException(
- env,
- "java/lang/IllegalArgumentException",
- "Failed to set MediaCas on extractor.");
- }
- }
- static jlong android_media_MediaExtractor_getCachedDurationUs(
- JNIEnv *env, jobject thiz) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return -1LL;
- }
- int64_t cachedDurationUs;
- bool eos;
- if (!extractor->getCachedDuration(&cachedDurationUs, &eos)) {
- return -1LL;
- }
- return (jlong) cachedDurationUs;
- }
- static jboolean android_media_MediaExtractor_hasCacheReachedEOS(
- JNIEnv *env, jobject thiz) {
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return JNI_TRUE;
- }
- int64_t cachedDurationUs;
- bool eos;
- if (!extractor->getCachedDuration(&cachedDurationUs, &eos)) {
- return JNI_TRUE;
- }
- return eos ? JNI_TRUE : JNI_FALSE;
- }
- static void android_media_MediaExtractor_native_finalize(
- JNIEnv *env, jobject thiz) {
- android_media_MediaExtractor_release(env, thiz);
- }
- static jobject
- android_media_MediaExtractor_native_getMetrics(JNIEnv * env, jobject thiz)
- {
- ALOGV("android_media_MediaExtractor_native_getMetrics");
- sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
- if (extractor == NULL ) {
- jniThrowException(env, "java/lang/IllegalStateException", NULL);
- return NULL;
- }
- // get what we have for the metrics from the codec
- Parcel reply;
- status_t err = extractor->getMetrics(&reply);
- if (err != OK) {
- ALOGE("getMetrics failed");
- return (jobject) NULL;
- }
- // build and return the Bundle
- std::unique_ptr<MediaAnalyticsItem> item(MediaAnalyticsItem::create());
- item->readFromParcel(reply);
- jobject mybundle = MediaMetricsJNI::writeMetricsToBundle(env, item.get(), NULL);
- return mybundle;
- }
- static const JNINativeMethod gMethods[] = {
- { "release", "()V", (void *)android_media_MediaExtractor_release },
- { "getTrackCount", "()I", (void *)android_media_MediaExtractor_getTrackCount },
- { "getFileFormatNative", "()Ljava/util/Map;",
- (void *)android_media_MediaExtractor_getFileFormatNative },
- { "getTrackFormatNative", "(I)Ljava/util/Map;",
- (void *)android_media_MediaExtractor_getTrackFormatNative },
- { "selectTrack", "(I)V", (void *)android_media_MediaExtractor_selectTrack },
- { "unselectTrack", "(I)V",
- (void *)android_media_MediaExtractor_unselectTrack },
- { "seekTo", "(JI)V", (void *)android_media_MediaExtractor_seekTo },
- { "advance", "()Z", (void *)android_media_MediaExtractor_advance },
- { "readSampleData", "(Ljava/nio/ByteBuffer;I)I",
- (void *)android_media_MediaExtractor_readSampleData },
- { "getSampleTrackIndex", "()I",
- (void *)android_media_MediaExtractor_getSampleTrackIndex },
- { "getSampleTime", "()J",
- (void *)android_media_MediaExtractor_getSampleTime },
- { "getSampleSize", "()J",
- (void *)android_media_MediaExtractor_getSampleSize },
- { "getSampleFlags", "()I",
- (void *)android_media_MediaExtractor_getSampleFlags },
- { "getSampleCryptoInfo", "(Landroid/media/MediaCodec$CryptoInfo;)Z",
- (void *)android_media_MediaExtractor_getSampleCryptoInfo },
- { "native_init", "()V", (void *)android_media_MediaExtractor_native_init },
- { "native_setup", "()V",
- (void *)android_media_MediaExtractor_native_setup },
- { "native_finalize", "()V",
- (void *)android_media_MediaExtractor_native_finalize },
- { "nativeSetDataSource",
- "(Landroid/os/IBinder;Ljava/lang/String;[Ljava/lang/String;"
- "[Ljava/lang/String;)V",
- (void *)android_media_MediaExtractor_setDataSource },
- { "setDataSource", "(Ljava/io/FileDescriptor;JJ)V",
- (void *)android_media_MediaExtractor_setDataSourceFd },
- { "setDataSource", "(Landroid/media/MediaDataSource;)V",
- (void *)android_media_MediaExtractor_setDataSourceCallback },
- { "nativeSetMediaCas", "(Landroid/os/IHwBinder;)V",
- (void *)android_media_MediaExtractor_setMediaCas },
- { "getCachedDuration", "()J",
- (void *)android_media_MediaExtractor_getCachedDurationUs },
- { "hasCacheReachedEndOfStream", "()Z",
- (void *)android_media_MediaExtractor_hasCacheReachedEOS },
- {"native_getMetrics", "()Landroid/os/PersistableBundle;",
- (void *)android_media_MediaExtractor_native_getMetrics},
- { "native_getAudioPresentations", "(I)Ljava/util/List;",
- (void *)android_media_MediaExtractor_getAudioPresentations },
- };
- int register_android_media_MediaExtractor(JNIEnv *env) {
- return AndroidRuntime::registerNativeMethods(env,
- "android/media/MediaExtractor", gMethods, NELEM(gMethods));
- }
|