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