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 "MediaCodec-JNI" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include "android_media_MediaCodec.h" |
| 22 | |
Andreas Huber | 07ea426 | 2012-04-11 12:21:20 -0700 | [diff] [blame] | 23 | #include "android_media_MediaCrypto.h" |
Chong Zhang | 2659c2f | 2017-04-27 13:18:20 -0700 | [diff] [blame] | 24 | #include "android_media_MediaDescrambler.h" |
Ray Essick | 0e0fee1 | 2017-01-25 18:01:56 -0800 | [diff] [blame] | 25 | #include "android_media_MediaMetricsJNI.h" |
Jooyung Han | cb1e896 | 2019-02-21 14:18:11 +0900 | [diff] [blame] | 26 | #include "android_media_Streams.h" |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 27 | #include "android_runtime/AndroidRuntime.h" |
| 28 | #include "android_runtime/android_view_Surface.h" |
Chong Zhang | d5927ae | 2017-01-03 11:07:18 -0800 | [diff] [blame] | 29 | #include "android_util_Binder.h" |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 30 | #include "jni.h" |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 31 | #include <nativehelper/JNIHelp.h> |
Chong Zhang | a0b72a6 | 2018-02-28 18:46:26 -0800 | [diff] [blame] | 32 | #include <nativehelper/ScopedLocalRef.h> |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 33 | |
Chong Zhang | 2659c2f | 2017-04-27 13:18:20 -0700 | [diff] [blame] | 34 | #include <android/hardware/cas/native/1.0/IDescrambler.h> |
Chong Zhang | d5927ae | 2017-01-03 11:07:18 -0800 | [diff] [blame] | 35 | |
Lajos Molnar | 7ac4f56 | 2014-03-24 15:57:51 -0700 | [diff] [blame] | 36 | #include <cutils/compiler.h> |
| 37 | |
Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 38 | #include <gui/Surface.h> |
Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 39 | |
Wonsik Kim | 4273dd0 | 2016-09-27 15:23:35 +0900 | [diff] [blame] | 40 | #include <media/MediaCodecBuffer.h> |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 41 | #include <media/stagefright/MediaCodec.h> |
| 42 | #include <media/stagefright/foundation/ABuffer.h> |
| 43 | #include <media/stagefright/foundation/ADebug.h> |
| 44 | #include <media/stagefright/foundation/ALooper.h> |
| 45 | #include <media/stagefright/foundation/AMessage.h> |
Andreas Huber | bfc56f4 | 2012-04-19 12:47:07 -0700 | [diff] [blame] | 46 | #include <media/stagefright/foundation/AString.h> |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 47 | #include <media/stagefright/MediaErrors.h> |
Chong Zhang | 8034d60 | 2015-04-28 13:38:48 -0700 | [diff] [blame] | 48 | #include <media/stagefright/PersistentSurface.h> |
Jeff Tinker | cd4d28f | 2018-02-16 16:24:49 -0800 | [diff] [blame] | 49 | #include <mediadrm/ICrypto.h> |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 50 | #include <nativehelper/ScopedLocalRef.h> |
| 51 | |
Andreas Huber | b12a539 | 2012-04-30 14:18:33 -0700 | [diff] [blame] | 52 | #include <system/window.h> |
| 53 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 54 | namespace android { |
| 55 | |
| 56 | // Keep these in sync with their equivalents in MediaCodec.java !!! |
| 57 | enum { |
| 58 | DEQUEUE_INFO_TRY_AGAIN_LATER = -1, |
| 59 | DEQUEUE_INFO_OUTPUT_FORMAT_CHANGED = -2, |
| 60 | DEQUEUE_INFO_OUTPUT_BUFFERS_CHANGED = -3, |
| 61 | }; |
| 62 | |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 63 | enum { |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 64 | EVENT_CALLBACK = 1, |
| 65 | EVENT_SET_CALLBACK = 2, |
Lajos Molnar | d857857 | 2015-06-05 20:17:33 -0700 | [diff] [blame] | 66 | EVENT_FRAME_RENDERED = 3, |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 69 | static struct CryptoErrorCodes { |
Jeff Tinker | 3ed3826 | 2013-08-02 23:24:51 -0700 | [diff] [blame] | 70 | jint cryptoErrorNoKey; |
| 71 | jint cryptoErrorKeyExpired; |
| 72 | jint cryptoErrorResourceBusy; |
Jeff Tinker | 336d3ea | 2014-08-28 17:57:36 -0700 | [diff] [blame] | 73 | jint cryptoErrorInsufficientOutputProtection; |
Jeff Tinker | 96a2a95 | 2015-07-01 17:35:18 -0700 | [diff] [blame] | 74 | jint cryptoErrorSessionNotOpened; |
Jeff Tinker | 20594d8 | 2018-12-12 08:31:22 -0800 | [diff] [blame] | 75 | jint cryptoErrorInsufficientSecurity; |
Jeff Tinker | d393216 | 2016-03-05 11:35:20 -0800 | [diff] [blame] | 76 | jint cryptoErrorUnsupportedOperation; |
Jeff Tinker | 20594d8 | 2018-12-12 08:31:22 -0800 | [diff] [blame] | 77 | jint cryptoErrorFrameTooLarge; |
| 78 | jint cryptoErrorLostState; |
Jeff Tinker | 3ed3826 | 2013-08-02 23:24:51 -0700 | [diff] [blame] | 79 | } gCryptoErrorCodes; |
| 80 | |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 81 | static struct CodecActionCodes { |
| 82 | jint codecActionTransient; |
| 83 | jint codecActionRecoverable; |
| 84 | } gCodecActionCodes; |
| 85 | |
Ronghua Wu | c53ad69 | 2015-05-08 14:40:49 -0700 | [diff] [blame] | 86 | static struct CodecErrorCodes { |
| 87 | jint errorInsufficientResource; |
| 88 | jint errorReclaimed; |
| 89 | } gCodecErrorCodes; |
Ronghua Wu | 9e9ec94 | 2015-04-15 17:10:31 -0700 | [diff] [blame] | 90 | |
Chong Zhang | 8034d60 | 2015-04-28 13:38:48 -0700 | [diff] [blame] | 91 | static struct { |
| 92 | jclass clazz; |
| 93 | jfieldID mLock; |
| 94 | jfieldID mPersistentObject; |
| 95 | jmethodID ctor; |
| 96 | jmethodID setNativeObjectLocked; |
| 97 | } gPersistentSurfaceClassInfo; |
| 98 | |
Jeff Tinker | d4ea5d3 | 2015-12-18 11:56:22 -0800 | [diff] [blame] | 99 | static struct { |
| 100 | jint Unencrypted; |
| 101 | jint AesCtr; |
| 102 | jint AesCbc; |
| 103 | } gCryptoModes; |
| 104 | |
Chong Zhang | a0b72a6 | 2018-02-28 18:46:26 -0800 | [diff] [blame] | 105 | static struct { |
| 106 | jclass capsClazz; |
| 107 | jmethodID capsCtorId; |
| 108 | jclass profileLevelClazz; |
| 109 | jfieldID profileField; |
| 110 | jfieldID levelField; |
| 111 | } gCodecInfo; |
Jeff Tinker | d4ea5d3 | 2015-12-18 11:56:22 -0800 | [diff] [blame] | 112 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 113 | struct fields_t { |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 114 | jmethodID postEventFromNativeID; |
Wonsik Kim | 61796fd | 2018-09-13 13:15:59 -0700 | [diff] [blame] | 115 | jmethodID lockAndGetContextID; |
| 116 | jmethodID setAndUnlockContextID; |
Andreas Huber | 91befdc | 2012-04-18 12:19:51 -0700 | [diff] [blame] | 117 | jfieldID cryptoInfoNumSubSamplesID; |
| 118 | jfieldID cryptoInfoNumBytesOfClearDataID; |
| 119 | jfieldID cryptoInfoNumBytesOfEncryptedDataID; |
| 120 | jfieldID cryptoInfoKeyID; |
| 121 | jfieldID cryptoInfoIVID; |
| 122 | jfieldID cryptoInfoModeID; |
Jeff Tinker | d4ea5d3 | 2015-12-18 11:56:22 -0800 | [diff] [blame] | 123 | jfieldID cryptoInfoPatternID; |
| 124 | jfieldID patternEncryptBlocksID; |
| 125 | jfieldID patternSkipBlocksID; |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | static fields_t gFields; |
Chong Zhang | 8034d60 | 2015-04-28 13:38:48 -0700 | [diff] [blame] | 129 | static const void *sRefBaseOwner; |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 130 | |
| 131 | //////////////////////////////////////////////////////////////////////////////// |
| 132 | |
| 133 | JMediaCodec::JMediaCodec( |
| 134 | JNIEnv *env, jobject thiz, |
| 135 | const char *name, bool nameIsType, bool encoder) |
| 136 | : mClass(NULL), |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 137 | mObject(NULL) { |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 138 | jclass clazz = env->GetObjectClass(thiz); |
| 139 | CHECK(clazz != NULL); |
| 140 | |
| 141 | mClass = (jclass)env->NewGlobalRef(clazz); |
| 142 | mObject = env->NewWeakGlobalRef(thiz); |
| 143 | |
Lajos Molnar | 7de28d3 | 2014-07-25 07:51:02 -0700 | [diff] [blame] | 144 | cacheJavaObjects(env); |
| 145 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 146 | mLooper = new ALooper; |
| 147 | mLooper->setName("MediaCodec_looper"); |
| 148 | |
| 149 | mLooper->start( |
| 150 | false, // runOnCallingThread |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 151 | true, // canCallJava |
Jeff Tinker | e182d20 | 2017-09-07 16:46:50 -0700 | [diff] [blame] | 152 | ANDROID_PRIORITY_VIDEO); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 153 | |
| 154 | if (nameIsType) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 155 | mCodec = MediaCodec::CreateByType(mLooper, name, encoder, &mInitStatus); |
Lajos Molnar | e747387 | 2019-02-05 18:54:27 -0800 | [diff] [blame] | 156 | if (mCodec == nullptr || mCodec->getName(&mNameAtCreation) != OK) { |
| 157 | mNameAtCreation = "(null)"; |
| 158 | } |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 159 | } else { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 160 | mCodec = MediaCodec::CreateByComponentName(mLooper, name, &mInitStatus); |
Lajos Molnar | e747387 | 2019-02-05 18:54:27 -0800 | [diff] [blame] | 161 | mNameAtCreation = name; |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 162 | } |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 163 | CHECK((mCodec != NULL) != (mInitStatus != OK)); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Lajos Molnar | 7de28d3 | 2014-07-25 07:51:02 -0700 | [diff] [blame] | 166 | void JMediaCodec::cacheJavaObjects(JNIEnv *env) { |
| 167 | jclass clazz = (jclass)env->FindClass("java/nio/ByteBuffer"); |
| 168 | mByteBufferClass = (jclass)env->NewGlobalRef(clazz); |
| 169 | CHECK(mByteBufferClass != NULL); |
| 170 | |
| 171 | ScopedLocalRef<jclass> byteOrderClass( |
| 172 | env, env->FindClass("java/nio/ByteOrder")); |
| 173 | CHECK(byteOrderClass.get() != NULL); |
| 174 | |
| 175 | jmethodID nativeOrderID = env->GetStaticMethodID( |
| 176 | byteOrderClass.get(), "nativeOrder", "()Ljava/nio/ByteOrder;"); |
| 177 | CHECK(nativeOrderID != NULL); |
| 178 | |
| 179 | jobject nativeByteOrderObj = |
| 180 | env->CallStaticObjectMethod(byteOrderClass.get(), nativeOrderID); |
| 181 | mNativeByteOrderObj = env->NewGlobalRef(nativeByteOrderObj); |
| 182 | CHECK(mNativeByteOrderObj != NULL); |
| 183 | env->DeleteLocalRef(nativeByteOrderObj); |
| 184 | nativeByteOrderObj = NULL; |
| 185 | |
| 186 | mByteBufferOrderMethodID = env->GetMethodID( |
| 187 | mByteBufferClass, |
| 188 | "order", |
| 189 | "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;"); |
| 190 | CHECK(mByteBufferOrderMethodID != NULL); |
| 191 | |
| 192 | mByteBufferAsReadOnlyBufferMethodID = env->GetMethodID( |
| 193 | mByteBufferClass, "asReadOnlyBuffer", "()Ljava/nio/ByteBuffer;"); |
| 194 | CHECK(mByteBufferAsReadOnlyBufferMethodID != NULL); |
| 195 | |
| 196 | mByteBufferPositionMethodID = env->GetMethodID( |
| 197 | mByteBufferClass, "position", "(I)Ljava/nio/Buffer;"); |
| 198 | CHECK(mByteBufferPositionMethodID != NULL); |
| 199 | |
| 200 | mByteBufferLimitMethodID = env->GetMethodID( |
| 201 | mByteBufferClass, "limit", "(I)Ljava/nio/Buffer;"); |
| 202 | CHECK(mByteBufferLimitMethodID != NULL); |
| 203 | } |
| 204 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 205 | status_t JMediaCodec::initCheck() const { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 206 | return mInitStatus; |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 209 | void JMediaCodec::registerSelf() { |
| 210 | mLooper->registerHandler(this); |
| 211 | } |
| 212 | |
Chong Zhang | 128b012 | 2014-03-01 18:04:13 -0800 | [diff] [blame] | 213 | void JMediaCodec::release() { |
Wonsik Kim | eaed9f5 | 2019-06-18 17:25:02 -0700 | [diff] [blame] | 214 | std::call_once(mReleaseFlag, [this] { |
| 215 | if (mCodec != NULL) { |
| 216 | mCodec->release(); |
| 217 | mInitStatus = NO_INIT; |
| 218 | } |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 219 | |
Wonsik Kim | eaed9f5 | 2019-06-18 17:25:02 -0700 | [diff] [blame] | 220 | if (mLooper != NULL) { |
| 221 | mLooper->unregisterHandler(id()); |
| 222 | mLooper->stop(); |
| 223 | mLooper.clear(); |
| 224 | } |
| 225 | }); |
Chong Zhang | 128b012 | 2014-03-01 18:04:13 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | JMediaCodec::~JMediaCodec() { |
Wonsik Kim | eaed9f5 | 2019-06-18 17:25:02 -0700 | [diff] [blame] | 229 | if (mLooper != NULL) { |
Chong Zhang | 128b012 | 2014-03-01 18:04:13 -0800 | [diff] [blame] | 230 | /* MediaCodec and looper should have been released explicitly already |
| 231 | * in setMediaCodec() (see comments in setMediaCodec()). |
| 232 | * |
| 233 | * Otherwise JMediaCodec::~JMediaCodec() might be called from within the |
| 234 | * message handler, doing release() there risks deadlock as MediaCodec:: |
| 235 | * release() post synchronous message to the same looper. |
| 236 | * |
| 237 | * Print a warning and try to proceed with releasing. |
| 238 | */ |
| 239 | ALOGW("try to release MediaCodec from JMediaCodec::~JMediaCodec()..."); |
| 240 | release(); |
| 241 | ALOGW("done releasing MediaCodec from JMediaCodec::~JMediaCodec()."); |
| 242 | } |
| 243 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 244 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 245 | |
| 246 | env->DeleteWeakGlobalRef(mObject); |
| 247 | mObject = NULL; |
| 248 | env->DeleteGlobalRef(mClass); |
| 249 | mClass = NULL; |
Lajos Molnar | 7de28d3 | 2014-07-25 07:51:02 -0700 | [diff] [blame] | 250 | deleteJavaObjects(env); |
| 251 | } |
| 252 | |
| 253 | void JMediaCodec::deleteJavaObjects(JNIEnv *env) { |
| 254 | env->DeleteGlobalRef(mByteBufferClass); |
| 255 | mByteBufferClass = NULL; |
| 256 | env->DeleteGlobalRef(mNativeByteOrderObj); |
| 257 | mNativeByteOrderObj = NULL; |
| 258 | |
| 259 | mByteBufferOrderMethodID = NULL; |
| 260 | mByteBufferAsReadOnlyBufferMethodID = NULL; |
| 261 | mByteBufferPositionMethodID = NULL; |
| 262 | mByteBufferLimitMethodID = NULL; |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Lajos Molnar | d857857 | 2015-06-05 20:17:33 -0700 | [diff] [blame] | 265 | status_t JMediaCodec::enableOnFrameRenderedListener(jboolean enable) { |
| 266 | if (enable) { |
| 267 | if (mOnFrameRenderedNotification == NULL) { |
| 268 | mOnFrameRenderedNotification = new AMessage(kWhatFrameRendered, this); |
| 269 | } |
| 270 | } else { |
| 271 | mOnFrameRenderedNotification.clear(); |
| 272 | } |
| 273 | |
| 274 | return mCodec->setOnFrameRenderedNotification(mOnFrameRenderedNotification); |
| 275 | } |
| 276 | |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 277 | status_t JMediaCodec::setCallback(jobject cb) { |
| 278 | if (cb != NULL) { |
| 279 | if (mCallbackNotification == NULL) { |
Lajos Molnar | 63834f4 | 2015-03-04 14:39:08 -0800 | [diff] [blame] | 280 | mCallbackNotification = new AMessage(kWhatCallbackNotify, this); |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 281 | } |
| 282 | } else { |
| 283 | mCallbackNotification.clear(); |
| 284 | } |
| 285 | |
| 286 | return mCodec->setCallback(mCallbackNotification); |
| 287 | } |
| 288 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 289 | status_t JMediaCodec::configure( |
| 290 | const sp<AMessage> &format, |
Andy McFadden | d47f7d8 | 2012-12-18 09:48:38 -0800 | [diff] [blame] | 291 | const sp<IGraphicBufferProducer> &bufferProducer, |
Andreas Huber | 8240d92 | 2012-04-04 14:06:32 -0700 | [diff] [blame] | 292 | const sp<ICrypto> &crypto, |
Chong Zhang | d5927ae | 2017-01-03 11:07:18 -0800 | [diff] [blame] | 293 | const sp<IDescrambler> &descrambler, |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 294 | int flags) { |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 295 | sp<Surface> client; |
Andy McFadden | d47f7d8 | 2012-12-18 09:48:38 -0800 | [diff] [blame] | 296 | if (bufferProducer != NULL) { |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 297 | mSurfaceTextureClient = |
| 298 | new Surface(bufferProducer, true /* controlledByApp */); |
Andreas Huber | b12a539 | 2012-04-30 14:18:33 -0700 | [diff] [blame] | 299 | } else { |
| 300 | mSurfaceTextureClient.clear(); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 301 | } |
Andreas Huber | b12a539 | 2012-04-30 14:18:33 -0700 | [diff] [blame] | 302 | |
Chong Zhang | d5927ae | 2017-01-03 11:07:18 -0800 | [diff] [blame] | 303 | return mCodec->configure( |
| 304 | format, mSurfaceTextureClient, crypto, descrambler, flags); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Lajos Molnar | 5e02ba9 | 2015-05-01 15:59:35 -0700 | [diff] [blame] | 307 | status_t JMediaCodec::setSurface( |
| 308 | const sp<IGraphicBufferProducer> &bufferProducer) { |
| 309 | sp<Surface> client; |
| 310 | if (bufferProducer != NULL) { |
| 311 | client = new Surface(bufferProducer, true /* controlledByApp */); |
| 312 | } |
| 313 | status_t err = mCodec->setSurface(client); |
| 314 | if (err == OK) { |
| 315 | mSurfaceTextureClient = client; |
| 316 | } |
| 317 | return err; |
| 318 | } |
| 319 | |
Andy McFadden | 2621e40 | 2013-02-19 07:29:21 -0800 | [diff] [blame] | 320 | status_t JMediaCodec::createInputSurface( |
| 321 | sp<IGraphicBufferProducer>* bufferProducer) { |
| 322 | return mCodec->createInputSurface(bufferProducer); |
| 323 | } |
| 324 | |
Chong Zhang | 9560ddb | 2015-05-13 10:25:29 -0700 | [diff] [blame] | 325 | status_t JMediaCodec::setInputSurface( |
Chong Zhang | 8034d60 | 2015-04-28 13:38:48 -0700 | [diff] [blame] | 326 | const sp<PersistentSurface> &surface) { |
Chong Zhang | 9560ddb | 2015-05-13 10:25:29 -0700 | [diff] [blame] | 327 | return mCodec->setInputSurface(surface); |
Chong Zhang | 8034d60 | 2015-04-28 13:38:48 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 330 | status_t JMediaCodec::start() { |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 331 | return mCodec->start(); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | status_t JMediaCodec::stop() { |
Andreas Huber | b12a539 | 2012-04-30 14:18:33 -0700 | [diff] [blame] | 335 | mSurfaceTextureClient.clear(); |
| 336 | |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 337 | return mCodec->stop(); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | status_t JMediaCodec::flush() { |
| 341 | return mCodec->flush(); |
| 342 | } |
| 343 | |
Lajos Molnar | 1e6e801 | 2014-07-15 16:07:13 -0700 | [diff] [blame] | 344 | status_t JMediaCodec::reset() { |
| 345 | return mCodec->reset(); |
| 346 | } |
| 347 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 348 | status_t JMediaCodec::queueInputBuffer( |
| 349 | size_t index, |
Andreas Huber | bfc56f4 | 2012-04-19 12:47:07 -0700 | [diff] [blame] | 350 | size_t offset, size_t size, int64_t timeUs, uint32_t flags, |
| 351 | AString *errorDetailMsg) { |
| 352 | return mCodec->queueInputBuffer( |
| 353 | index, offset, size, timeUs, flags, errorDetailMsg); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Andreas Huber | 9e6bcce | 2012-04-06 12:14:47 -0700 | [diff] [blame] | 356 | status_t JMediaCodec::queueSecureInputBuffer( |
| 357 | size_t index, |
| 358 | size_t offset, |
| 359 | const CryptoPlugin::SubSample *subSamples, |
| 360 | size_t numSubSamples, |
| 361 | const uint8_t key[16], |
| 362 | const uint8_t iv[16], |
| 363 | CryptoPlugin::Mode mode, |
Jeff Tinker | d4ea5d3 | 2015-12-18 11:56:22 -0800 | [diff] [blame] | 364 | const CryptoPlugin::Pattern &pattern, |
Andreas Huber | 9e6bcce | 2012-04-06 12:14:47 -0700 | [diff] [blame] | 365 | int64_t presentationTimeUs, |
Andreas Huber | bfc56f4 | 2012-04-19 12:47:07 -0700 | [diff] [blame] | 366 | uint32_t flags, |
| 367 | AString *errorDetailMsg) { |
Andreas Huber | 9e6bcce | 2012-04-06 12:14:47 -0700 | [diff] [blame] | 368 | return mCodec->queueSecureInputBuffer( |
Jeff Tinker | d4ea5d3 | 2015-12-18 11:56:22 -0800 | [diff] [blame] | 369 | index, offset, subSamples, numSubSamples, key, iv, mode, pattern, |
Andreas Huber | bfc56f4 | 2012-04-19 12:47:07 -0700 | [diff] [blame] | 370 | presentationTimeUs, flags, errorDetailMsg); |
Andreas Huber | 9e6bcce | 2012-04-06 12:14:47 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 373 | status_t JMediaCodec::dequeueInputBuffer(size_t *index, int64_t timeoutUs) { |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 374 | return mCodec->dequeueInputBuffer(index, timeoutUs); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | status_t JMediaCodec::dequeueOutputBuffer( |
| 378 | JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs) { |
| 379 | size_t size, offset; |
| 380 | int64_t timeUs; |
| 381 | uint32_t flags; |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 382 | status_t err = mCodec->dequeueOutputBuffer( |
| 383 | index, &offset, &size, &timeUs, &flags, timeoutUs); |
| 384 | |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 385 | if (err != OK) { |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 386 | return err; |
| 387 | } |
| 388 | |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 389 | ScopedLocalRef<jclass> clazz( |
| 390 | env, env->FindClass("android/media/MediaCodec$BufferInfo")); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 391 | |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 392 | jmethodID method = env->GetMethodID(clazz.get(), "set", "(IIJI)V"); |
Ashok Bhat | fef85ef | 2014-03-05 15:06:05 +0000 | [diff] [blame] | 393 | env->CallVoidMethod(bufferInfo, method, (jint)offset, (jint)size, timeUs, flags); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 394 | |
| 395 | return OK; |
| 396 | } |
| 397 | |
Lajos Molnar | 7c513b6b | 2014-05-08 17:16:45 -0700 | [diff] [blame] | 398 | status_t JMediaCodec::releaseOutputBuffer( |
| 399 | size_t index, bool render, bool updatePTS, int64_t timestampNs) { |
| 400 | if (updatePTS) { |
| 401 | return mCodec->renderOutputBufferAndRelease(index, timestampNs); |
| 402 | } |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 403 | return render |
| 404 | ? mCodec->renderOutputBufferAndRelease(index) |
| 405 | : mCodec->releaseOutputBuffer(index); |
| 406 | } |
| 407 | |
Andy McFadden | 2621e40 | 2013-02-19 07:29:21 -0800 | [diff] [blame] | 408 | status_t JMediaCodec::signalEndOfInputStream() { |
| 409 | return mCodec->signalEndOfInputStream(); |
| 410 | } |
| 411 | |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 412 | status_t JMediaCodec::getFormat(JNIEnv *env, bool input, jobject *format) const { |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 413 | sp<AMessage> msg; |
| 414 | status_t err; |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 415 | err = input ? mCodec->getInputFormat(&msg) : mCodec->getOutputFormat(&msg); |
| 416 | if (err != OK) { |
| 417 | return err; |
| 418 | } |
| 419 | |
| 420 | return ConvertMessageToMap(env, msg, format); |
| 421 | } |
| 422 | |
| 423 | status_t JMediaCodec::getOutputFormat(JNIEnv *env, size_t index, jobject *format) const { |
| 424 | sp<AMessage> msg; |
| 425 | status_t err; |
| 426 | if ((err = mCodec->getOutputFormat(index, &msg)) != OK) { |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 427 | return err; |
| 428 | } |
| 429 | |
| 430 | return ConvertMessageToMap(env, msg, format); |
| 431 | } |
| 432 | |
| 433 | status_t JMediaCodec::getBuffers( |
| 434 | JNIEnv *env, bool input, jobjectArray *bufArray) const { |
Wonsik Kim | 4273dd0 | 2016-09-27 15:23:35 +0900 | [diff] [blame] | 435 | Vector<sp<MediaCodecBuffer> > buffers; |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 436 | |
| 437 | status_t err = |
| 438 | input |
| 439 | ? mCodec->getInputBuffers(&buffers) |
| 440 | : mCodec->getOutputBuffers(&buffers); |
| 441 | |
| 442 | if (err != OK) { |
| 443 | return err; |
| 444 | } |
| 445 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 446 | *bufArray = (jobjectArray)env->NewObjectArray( |
Lajos Molnar | 7de28d3 | 2014-07-25 07:51:02 -0700 | [diff] [blame] | 447 | buffers.size(), mByteBufferClass, NULL); |
Marco Nelissen | cbbea8e | 2012-12-19 11:42:55 -0800 | [diff] [blame] | 448 | if (*bufArray == NULL) { |
Marco Nelissen | cbbea8e | 2012-12-19 11:42:55 -0800 | [diff] [blame] | 449 | return NO_MEMORY; |
| 450 | } |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 451 | |
| 452 | for (size_t i = 0; i < buffers.size(); ++i) { |
Wonsik Kim | 4273dd0 | 2016-09-27 15:23:35 +0900 | [diff] [blame] | 453 | const sp<MediaCodecBuffer> &buffer = buffers.itemAt(i); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 454 | |
Lajos Molnar | 7de28d3 | 2014-07-25 07:51:02 -0700 | [diff] [blame] | 455 | jobject byteBuffer = NULL; |
| 456 | err = createByteBufferFromABuffer( |
| 457 | env, !input /* readOnly */, true /* clearBuffer */, buffer, &byteBuffer); |
| 458 | if (err != OK) { |
| 459 | return err; |
Marco Nelissen | cbbea8e | 2012-12-19 11:42:55 -0800 | [diff] [blame] | 460 | } |
Lajos Molnar | 7de28d3 | 2014-07-25 07:51:02 -0700 | [diff] [blame] | 461 | if (byteBuffer != NULL) { |
| 462 | env->SetObjectArrayElement( |
| 463 | *bufArray, i, byteBuffer); |
| 464 | |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 465 | env->DeleteLocalRef(byteBuffer); |
Lajos Molnar | 7de28d3 | 2014-07-25 07:51:02 -0700 | [diff] [blame] | 466 | byteBuffer = NULL; |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 467 | } |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 468 | } |
| 469 | |
Lajos Molnar | 7de28d3 | 2014-07-25 07:51:02 -0700 | [diff] [blame] | 470 | return OK; |
| 471 | } |
Andreas Huber | 3dd7fd0 | 2012-05-08 13:50:45 -0700 | [diff] [blame] | 472 | |
Lajos Molnar | 7de28d3 | 2014-07-25 07:51:02 -0700 | [diff] [blame] | 473 | // static |
Wonsik Kim | 4273dd0 | 2016-09-27 15:23:35 +0900 | [diff] [blame] | 474 | template <typename T> |
Lajos Molnar | 7de28d3 | 2014-07-25 07:51:02 -0700 | [diff] [blame] | 475 | status_t JMediaCodec::createByteBufferFromABuffer( |
Wonsik Kim | 4273dd0 | 2016-09-27 15:23:35 +0900 | [diff] [blame] | 476 | JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer, |
Lajos Molnar | 7de28d3 | 2014-07-25 07:51:02 -0700 | [diff] [blame] | 477 | jobject *buf) const { |
| 478 | // if this is an ABuffer that doesn't actually hold any accessible memory, |
| 479 | // use a null ByteBuffer |
| 480 | *buf = NULL; |
Aaron Vaage | e5b641e | 2015-09-03 15:12:57 -0700 | [diff] [blame] | 481 | |
| 482 | if (buffer == NULL) { |
| 483 | ALOGV("createByteBufferFromABuffer - given NULL, returning NULL"); |
| 484 | return OK; |
| 485 | } |
| 486 | |
Lajos Molnar | 7de28d3 | 2014-07-25 07:51:02 -0700 | [diff] [blame] | 487 | if (buffer->base() == NULL) { |
| 488 | return OK; |
| 489 | } |
| 490 | |
| 491 | jobject byteBuffer = |
| 492 | env->NewDirectByteBuffer(buffer->base(), buffer->capacity()); |
| 493 | if (readOnly && byteBuffer != NULL) { |
| 494 | jobject readOnlyBuffer = env->CallObjectMethod( |
| 495 | byteBuffer, mByteBufferAsReadOnlyBufferMethodID); |
| 496 | env->DeleteLocalRef(byteBuffer); |
| 497 | byteBuffer = readOnlyBuffer; |
| 498 | } |
| 499 | if (byteBuffer == NULL) { |
| 500 | return NO_MEMORY; |
| 501 | } |
| 502 | jobject me = env->CallObjectMethod( |
| 503 | byteBuffer, mByteBufferOrderMethodID, mNativeByteOrderObj); |
| 504 | env->DeleteLocalRef(me); |
| 505 | me = env->CallObjectMethod( |
| 506 | byteBuffer, mByteBufferLimitMethodID, |
| 507 | clearBuffer ? buffer->capacity() : (buffer->offset() + buffer->size())); |
| 508 | env->DeleteLocalRef(me); |
| 509 | me = env->CallObjectMethod( |
| 510 | byteBuffer, mByteBufferPositionMethodID, |
| 511 | clearBuffer ? 0 : buffer->offset()); |
| 512 | env->DeleteLocalRef(me); |
| 513 | me = NULL; |
| 514 | |
| 515 | *buf = byteBuffer; |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 516 | return OK; |
| 517 | } |
| 518 | |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 519 | status_t JMediaCodec::getBuffer( |
| 520 | JNIEnv *env, bool input, size_t index, jobject *buf) const { |
Wonsik Kim | 4273dd0 | 2016-09-27 15:23:35 +0900 | [diff] [blame] | 521 | sp<MediaCodecBuffer> buffer; |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 522 | |
| 523 | status_t err = |
| 524 | input |
| 525 | ? mCodec->getInputBuffer(index, &buffer) |
| 526 | : mCodec->getOutputBuffer(index, &buffer); |
| 527 | |
| 528 | if (err != OK) { |
| 529 | return err; |
| 530 | } |
| 531 | |
Lajos Molnar | 7de28d3 | 2014-07-25 07:51:02 -0700 | [diff] [blame] | 532 | return createByteBufferFromABuffer( |
| 533 | env, !input /* readOnly */, input /* clearBuffer */, buffer, buf); |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | status_t JMediaCodec::getImage( |
| 537 | JNIEnv *env, bool input, size_t index, jobject *buf) const { |
Wonsik Kim | 4273dd0 | 2016-09-27 15:23:35 +0900 | [diff] [blame] | 538 | sp<MediaCodecBuffer> buffer; |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 539 | |
| 540 | status_t err = |
| 541 | input |
| 542 | ? mCodec->getInputBuffer(index, &buffer) |
| 543 | : mCodec->getOutputBuffer(index, &buffer); |
| 544 | |
| 545 | if (err != OK) { |
| 546 | return err; |
| 547 | } |
| 548 | |
| 549 | // if this is an ABuffer that doesn't actually hold any accessible memory, |
| 550 | // use a null ByteBuffer |
| 551 | *buf = NULL; |
| 552 | if (buffer->base() == NULL) { |
| 553 | return OK; |
| 554 | } |
| 555 | |
| 556 | // check if buffer is an image |
Lajos Molnar | 7de28d3 | 2014-07-25 07:51:02 -0700 | [diff] [blame] | 557 | sp<ABuffer> imageData; |
| 558 | if (!buffer->meta()->findBuffer("image-data", &imageData)) { |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 559 | return OK; |
| 560 | } |
| 561 | |
Lajos Molnar | 7de28d3 | 2014-07-25 07:51:02 -0700 | [diff] [blame] | 562 | int64_t timestamp = 0; |
| 563 | if (!input && buffer->meta()->findInt64("timeUs", ×tamp)) { |
| 564 | timestamp *= 1000; // adjust to ns |
| 565 | } |
| 566 | |
| 567 | jobject byteBuffer = NULL; |
| 568 | err = createByteBufferFromABuffer( |
| 569 | env, !input /* readOnly */, input /* clearBuffer */, buffer, &byteBuffer); |
| 570 | if (err != OK) { |
| 571 | return OK; |
| 572 | } |
| 573 | |
| 574 | jobject infoBuffer = NULL; |
| 575 | err = createByteBufferFromABuffer( |
| 576 | env, true /* readOnly */, true /* clearBuffer */, imageData, &infoBuffer); |
| 577 | if (err != OK) { |
| 578 | env->DeleteLocalRef(byteBuffer); |
| 579 | byteBuffer = NULL; |
| 580 | return OK; |
| 581 | } |
| 582 | |
| 583 | jobject cropRect = NULL; |
| 584 | int32_t left, top, right, bottom; |
| 585 | if (buffer->meta()->findRect("crop-rect", &left, &top, &right, &bottom)) { |
| 586 | ScopedLocalRef<jclass> rectClazz( |
| 587 | env, env->FindClass("android/graphics/Rect")); |
| 588 | CHECK(rectClazz.get() != NULL); |
| 589 | |
| 590 | jmethodID rectConstructID = env->GetMethodID( |
| 591 | rectClazz.get(), "<init>", "(IIII)V"); |
| 592 | |
| 593 | cropRect = env->NewObject( |
| 594 | rectClazz.get(), rectConstructID, left, top, right + 1, bottom + 1); |
| 595 | } |
| 596 | |
| 597 | ScopedLocalRef<jclass> imageClazz( |
| 598 | env, env->FindClass("android/media/MediaCodec$MediaImage")); |
| 599 | CHECK(imageClazz.get() != NULL); |
| 600 | |
| 601 | jmethodID imageConstructID = env->GetMethodID(imageClazz.get(), "<init>", |
| 602 | "(Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;ZJIILandroid/graphics/Rect;)V"); |
| 603 | |
| 604 | *buf = env->NewObject(imageClazz.get(), imageConstructID, |
| 605 | byteBuffer, infoBuffer, |
| 606 | (jboolean)!input /* readOnly */, |
| 607 | (jlong)timestamp, |
| 608 | (jint)0 /* xOffset */, (jint)0 /* yOffset */, cropRect); |
| 609 | |
| 610 | // if MediaImage creation fails, return null |
| 611 | if (env->ExceptionCheck()) { |
| 612 | env->ExceptionDescribe(); |
| 613 | env->ExceptionClear(); |
| 614 | *buf = NULL; |
| 615 | } |
| 616 | |
| 617 | if (cropRect != NULL) { |
| 618 | env->DeleteLocalRef(cropRect); |
| 619 | cropRect = NULL; |
| 620 | } |
| 621 | |
| 622 | env->DeleteLocalRef(byteBuffer); |
| 623 | byteBuffer = NULL; |
| 624 | |
| 625 | env->DeleteLocalRef(infoBuffer); |
| 626 | infoBuffer = NULL; |
| 627 | |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 628 | return OK; |
| 629 | } |
| 630 | |
Martin Storsjo | 056ef2e | 2012-09-25 11:53:04 +0300 | [diff] [blame] | 631 | status_t JMediaCodec::getName(JNIEnv *env, jstring *nameStr) const { |
| 632 | AString name; |
| 633 | |
| 634 | status_t err = mCodec->getName(&name); |
| 635 | |
| 636 | if (err != OK) { |
| 637 | return err; |
| 638 | } |
| 639 | |
| 640 | *nameStr = env->NewStringUTF(name.c_str()); |
| 641 | |
| 642 | return OK; |
| 643 | } |
| 644 | |
Chong Zhang | a0b72a6 | 2018-02-28 18:46:26 -0800 | [diff] [blame] | 645 | static jobject getCodecCapabilitiesObject( |
| 646 | JNIEnv *env, const char *mime, bool isEncoder, |
| 647 | const sp<MediaCodecInfo::Capabilities> &capabilities) { |
| 648 | Vector<MediaCodecInfo::ProfileLevel> profileLevels; |
| 649 | Vector<uint32_t> colorFormats; |
| 650 | |
| 651 | sp<AMessage> defaultFormat = new AMessage(); |
| 652 | defaultFormat->setString("mime", mime); |
| 653 | |
| 654 | capabilities->getSupportedColorFormats(&colorFormats); |
| 655 | capabilities->getSupportedProfileLevels(&profileLevels); |
Chong Zhang | a0b72a6 | 2018-02-28 18:46:26 -0800 | [diff] [blame] | 656 | sp<AMessage> details = capabilities->getDetails(); |
| 657 | |
| 658 | jobject defaultFormatObj = NULL; |
| 659 | if (ConvertMessageToMap(env, defaultFormat, &defaultFormatObj)) { |
| 660 | return NULL; |
| 661 | } |
| 662 | ScopedLocalRef<jobject> defaultFormatRef(env, defaultFormatObj); |
| 663 | |
| 664 | jobject detailsObj = NULL; |
| 665 | if (ConvertMessageToMap(env, details, &detailsObj)) { |
| 666 | return NULL; |
| 667 | } |
| 668 | ScopedLocalRef<jobject> detailsRef(env, detailsObj); |
| 669 | |
| 670 | ScopedLocalRef<jobjectArray> profileLevelArray(env, env->NewObjectArray( |
| 671 | profileLevels.size(), gCodecInfo.profileLevelClazz, NULL)); |
| 672 | |
| 673 | for (size_t i = 0; i < profileLevels.size(); ++i) { |
| 674 | const MediaCodecInfo::ProfileLevel &src = profileLevels.itemAt(i); |
| 675 | |
| 676 | ScopedLocalRef<jobject> srcRef(env, env->AllocObject( |
| 677 | gCodecInfo.profileLevelClazz)); |
| 678 | |
| 679 | env->SetIntField(srcRef.get(), gCodecInfo.profileField, src.mProfile); |
| 680 | env->SetIntField(srcRef.get(), gCodecInfo.levelField, src.mLevel); |
| 681 | |
| 682 | env->SetObjectArrayElement(profileLevelArray.get(), i, srcRef.get()); |
| 683 | } |
| 684 | |
| 685 | ScopedLocalRef<jintArray> colorFormatsArray( |
| 686 | env, env->NewIntArray(colorFormats.size())); |
| 687 | for (size_t i = 0; i < colorFormats.size(); ++i) { |
| 688 | jint val = colorFormats.itemAt(i); |
| 689 | env->SetIntArrayRegion(colorFormatsArray.get(), i, 1, &val); |
| 690 | } |
| 691 | |
| 692 | return env->NewObject( |
| 693 | gCodecInfo.capsClazz, gCodecInfo.capsCtorId, |
Lajos Molnar | d2a7f47 | 2018-11-15 12:49:20 -0800 | [diff] [blame] | 694 | profileLevelArray.get(), colorFormatsArray.get(), isEncoder, |
Chong Zhang | a0b72a6 | 2018-02-28 18:46:26 -0800 | [diff] [blame] | 695 | defaultFormatRef.get(), detailsRef.get()); |
| 696 | } |
| 697 | |
| 698 | status_t JMediaCodec::getCodecInfo(JNIEnv *env, jobject *codecInfoObject) const { |
| 699 | sp<MediaCodecInfo> codecInfo; |
| 700 | |
| 701 | status_t err = mCodec->getCodecInfo(&codecInfo); |
| 702 | |
| 703 | if (err != OK) { |
| 704 | return err; |
| 705 | } |
| 706 | |
| 707 | ScopedLocalRef<jstring> nameObject(env, |
Lajos Molnar | e747387 | 2019-02-05 18:54:27 -0800 | [diff] [blame] | 708 | env->NewStringUTF(mNameAtCreation.c_str())); |
Chong Zhang | a0b72a6 | 2018-02-28 18:46:26 -0800 | [diff] [blame] | 709 | |
Lajos Molnar | d2a7f47 | 2018-11-15 12:49:20 -0800 | [diff] [blame] | 710 | ScopedLocalRef<jstring> canonicalNameObject(env, |
| 711 | env->NewStringUTF(codecInfo->getCodecName())); |
| 712 | |
| 713 | MediaCodecInfo::Attributes attributes = codecInfo->getAttributes(); |
Chong Zhang | a0b72a6 | 2018-02-28 18:46:26 -0800 | [diff] [blame] | 714 | bool isEncoder = codecInfo->isEncoder(); |
| 715 | |
Lajos Molnar | d2a7f47 | 2018-11-15 12:49:20 -0800 | [diff] [blame] | 716 | Vector<AString> mediaTypes; |
| 717 | codecInfo->getSupportedMediaTypes(&mediaTypes); |
Chong Zhang | a0b72a6 | 2018-02-28 18:46:26 -0800 | [diff] [blame] | 718 | |
| 719 | ScopedLocalRef<jobjectArray> capsArrayObj(env, |
Lajos Molnar | d2a7f47 | 2018-11-15 12:49:20 -0800 | [diff] [blame] | 720 | env->NewObjectArray(mediaTypes.size(), gCodecInfo.capsClazz, NULL)); |
Chong Zhang | a0b72a6 | 2018-02-28 18:46:26 -0800 | [diff] [blame] | 721 | |
Lajos Molnar | d2a7f47 | 2018-11-15 12:49:20 -0800 | [diff] [blame] | 722 | for (size_t i = 0; i < mediaTypes.size(); i++) { |
Chong Zhang | a0b72a6 | 2018-02-28 18:46:26 -0800 | [diff] [blame] | 723 | const sp<MediaCodecInfo::Capabilities> caps = |
Lajos Molnar | d2a7f47 | 2018-11-15 12:49:20 -0800 | [diff] [blame] | 724 | codecInfo->getCapabilitiesFor(mediaTypes[i].c_str()); |
Chong Zhang | a0b72a6 | 2018-02-28 18:46:26 -0800 | [diff] [blame] | 725 | |
| 726 | ScopedLocalRef<jobject> capsObj(env, getCodecCapabilitiesObject( |
Lajos Molnar | d2a7f47 | 2018-11-15 12:49:20 -0800 | [diff] [blame] | 727 | env, mediaTypes[i].c_str(), isEncoder, caps)); |
Chong Zhang | a0b72a6 | 2018-02-28 18:46:26 -0800 | [diff] [blame] | 728 | |
| 729 | env->SetObjectArrayElement(capsArrayObj.get(), i, capsObj.get()); |
| 730 | } |
| 731 | |
| 732 | ScopedLocalRef<jclass> codecInfoClazz(env, |
| 733 | env->FindClass("android/media/MediaCodecInfo")); |
| 734 | CHECK(codecInfoClazz.get() != NULL); |
| 735 | |
| 736 | jmethodID codecInfoCtorID = env->GetMethodID(codecInfoClazz.get(), "<init>", |
Lajos Molnar | b864a79 | 2019-01-17 16:26:22 -0800 | [diff] [blame] | 737 | "(Ljava/lang/String;Ljava/lang/String;I[Landroid/media/MediaCodecInfo$CodecCapabilities;)V"); |
Chong Zhang | a0b72a6 | 2018-02-28 18:46:26 -0800 | [diff] [blame] | 738 | |
| 739 | *codecInfoObject = env->NewObject(codecInfoClazz.get(), codecInfoCtorID, |
Lajos Molnar | d2a7f47 | 2018-11-15 12:49:20 -0800 | [diff] [blame] | 740 | nameObject.get(), canonicalNameObject.get(), attributes, capsArrayObj.get()); |
Chong Zhang | a0b72a6 | 2018-02-28 18:46:26 -0800 | [diff] [blame] | 741 | |
| 742 | return OK; |
| 743 | } |
| 744 | |
Ray Essick | f2d0e40 | 2017-03-09 10:17:51 -0800 | [diff] [blame] | 745 | status_t JMediaCodec::getMetrics(JNIEnv *, MediaAnalyticsItem * &reply) const { |
Ray Essick | e981a55 | 2019-08-26 15:34:10 -0700 | [diff] [blame] | 746 | mediametrics_handle_t reply2 = MediaAnalyticsItem::convert(reply); |
| 747 | status_t status = mCodec->getMetrics(reply2); |
| 748 | // getMetrics() updates reply2, pass the converted update along to our caller. |
| 749 | reply = MediaAnalyticsItem::convert(reply2); |
Ray Essick | 0e0fee1 | 2017-01-25 18:01:56 -0800 | [diff] [blame] | 750 | return status; |
| 751 | } |
| 752 | |
Andreas Huber | 226065b | 2013-08-12 10:14:11 -0700 | [diff] [blame] | 753 | status_t JMediaCodec::setParameters(const sp<AMessage> &msg) { |
| 754 | return mCodec->setParameters(msg); |
| 755 | } |
| 756 | |
Andreas Huber | b12a539 | 2012-04-30 14:18:33 -0700 | [diff] [blame] | 757 | void JMediaCodec::setVideoScalingMode(int mode) { |
| 758 | if (mSurfaceTextureClient != NULL) { |
Lajos Molnar | 832939e | 2018-05-23 14:58:26 -0700 | [diff] [blame] | 759 | // this works for components that queue to surface |
Andreas Huber | b12a539 | 2012-04-30 14:18:33 -0700 | [diff] [blame] | 760 | native_window_set_scaling_mode(mSurfaceTextureClient.get(), mode); |
Lajos Molnar | 832939e | 2018-05-23 14:58:26 -0700 | [diff] [blame] | 761 | // also signal via param for components that queue to IGBP |
| 762 | sp<AMessage> msg = new AMessage; |
| 763 | msg->setInt32("android._video-scaling", mode); |
| 764 | (void)mCodec->setParameters(msg); |
Andreas Huber | b12a539 | 2012-04-30 14:18:33 -0700 | [diff] [blame] | 765 | } |
| 766 | } |
| 767 | |
ybai | 5e05320 | 2018-11-01 13:02:15 +0800 | [diff] [blame] | 768 | void JMediaCodec::selectAudioPresentation(const int32_t presentationId, const int32_t programId) { |
| 769 | sp<AMessage> msg = new AMessage; |
| 770 | msg->setInt32("audio-presentation-presentation-id", presentationId); |
| 771 | msg->setInt32("audio-presentation-program-id", programId); |
| 772 | (void)mCodec->setParameters(msg); |
| 773 | } |
| 774 | |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 775 | static jthrowable createCodecException( |
| 776 | JNIEnv *env, status_t err, int32_t actionCode, const char *msg = NULL) { |
| 777 | ScopedLocalRef<jclass> clazz( |
| 778 | env, env->FindClass("android/media/MediaCodec$CodecException")); |
| 779 | CHECK(clazz.get() != NULL); |
| 780 | |
Ronghua Wu | c53ad69 | 2015-05-08 14:40:49 -0700 | [diff] [blame] | 781 | const jmethodID ctor = env->GetMethodID(clazz.get(), "<init>", "(IILjava/lang/String;)V"); |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 782 | CHECK(ctor != NULL); |
| 783 | |
| 784 | ScopedLocalRef<jstring> msgObj( |
| 785 | env, env->NewStringUTF(msg != NULL ? msg : String8::format("Error %#x", err))); |
| 786 | |
| 787 | // translate action code to Java equivalent |
| 788 | switch (actionCode) { |
| 789 | case ACTION_CODE_TRANSIENT: |
| 790 | actionCode = gCodecActionCodes.codecActionTransient; |
| 791 | break; |
| 792 | case ACTION_CODE_RECOVERABLE: |
| 793 | actionCode = gCodecActionCodes.codecActionRecoverable; |
| 794 | break; |
| 795 | default: |
| 796 | actionCode = 0; // everything else is fatal |
| 797 | break; |
| 798 | } |
| 799 | |
Ronghua Wu | c53ad69 | 2015-05-08 14:40:49 -0700 | [diff] [blame] | 800 | /* translate OS errors to Java API CodecException errorCodes */ |
| 801 | switch (err) { |
| 802 | case NO_MEMORY: |
| 803 | err = gCodecErrorCodes.errorInsufficientResource; |
| 804 | break; |
| 805 | case DEAD_OBJECT: |
| 806 | err = gCodecErrorCodes.errorReclaimed; |
| 807 | break; |
| 808 | default: /* Other error codes go out as is. */ |
| 809 | break; |
| 810 | } |
| 811 | |
| 812 | return (jthrowable)env->NewObject(clazz.get(), ctor, err, actionCode, msgObj.get()); |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 813 | } |
| 814 | |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 815 | void JMediaCodec::handleCallback(const sp<AMessage> &msg) { |
| 816 | int32_t arg1, arg2 = 0; |
| 817 | jobject obj = NULL; |
| 818 | CHECK(msg->findInt32("callbackID", &arg1)); |
| 819 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 820 | |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 821 | switch (arg1) { |
| 822 | case MediaCodec::CB_INPUT_AVAILABLE: |
| 823 | { |
| 824 | CHECK(msg->findInt32("index", &arg2)); |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 825 | break; |
| 826 | } |
| 827 | |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 828 | case MediaCodec::CB_OUTPUT_AVAILABLE: |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 829 | { |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 830 | CHECK(msg->findInt32("index", &arg2)); |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 831 | |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 832 | size_t size, offset; |
| 833 | int64_t timeUs; |
| 834 | uint32_t flags; |
| 835 | CHECK(msg->findSize("size", &size)); |
| 836 | CHECK(msg->findSize("offset", &offset)); |
| 837 | CHECK(msg->findInt64("timeUs", &timeUs)); |
| 838 | CHECK(msg->findInt32("flags", (int32_t *)&flags)); |
| 839 | |
| 840 | ScopedLocalRef<jclass> clazz( |
| 841 | env, env->FindClass("android/media/MediaCodec$BufferInfo")); |
| 842 | jmethodID ctor = env->GetMethodID(clazz.get(), "<init>", "()V"); |
| 843 | jmethodID method = env->GetMethodID(clazz.get(), "set", "(IIJI)V"); |
| 844 | |
| 845 | obj = env->NewObject(clazz.get(), ctor); |
| 846 | |
| 847 | if (obj == NULL) { |
| 848 | if (env->ExceptionCheck()) { |
| 849 | ALOGE("Could not create MediaCodec.BufferInfo."); |
| 850 | env->ExceptionClear(); |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 851 | } |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 852 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 853 | return; |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 854 | } |
| 855 | |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 856 | env->CallVoidMethod(obj, method, (jint)offset, (jint)size, timeUs, flags); |
| 857 | break; |
| 858 | } |
| 859 | |
| 860 | case MediaCodec::CB_ERROR: |
| 861 | { |
Chong Zhang | 94686d1 | 2014-07-11 15:53:58 -0700 | [diff] [blame] | 862 | int32_t err, actionCode; |
| 863 | CHECK(msg->findInt32("err", &err)); |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 864 | CHECK(msg->findInt32("actionCode", &actionCode)); |
| 865 | |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 866 | // note that DRM errors could conceivably alias into a CodecException |
| 867 | obj = (jobject)createCodecException(env, err, actionCode); |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 868 | |
| 869 | if (obj == NULL) { |
| 870 | if (env->ExceptionCheck()) { |
Chong Zhang | 94686d1 | 2014-07-11 15:53:58 -0700 | [diff] [blame] | 871 | ALOGE("Could not create CodecException object."); |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 872 | env->ExceptionClear(); |
| 873 | } |
| 874 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 875 | return; |
| 876 | } |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 877 | |
| 878 | break; |
| 879 | } |
| 880 | |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 881 | case MediaCodec::CB_OUTPUT_FORMAT_CHANGED: |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 882 | { |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 883 | sp<AMessage> format; |
| 884 | CHECK(msg->findMessage("format", &format)); |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 885 | |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 886 | if (OK != ConvertMessageToMap(env, format, &obj)) { |
| 887 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 888 | return; |
| 889 | } |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 890 | |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 891 | break; |
| 892 | } |
| 893 | |
| 894 | default: |
| 895 | TRESPASS(); |
| 896 | } |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 897 | |
| 898 | env->CallVoidMethod( |
| 899 | mObject, |
| 900 | gFields.postEventFromNativeID, |
| 901 | EVENT_CALLBACK, |
| 902 | arg1, |
| 903 | arg2, |
| 904 | obj); |
| 905 | |
| 906 | env->DeleteLocalRef(obj); |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Lajos Molnar | d857857 | 2015-06-05 20:17:33 -0700 | [diff] [blame] | 909 | void JMediaCodec::handleFrameRenderedNotification(const sp<AMessage> &msg) { |
| 910 | int32_t arg1 = 0, arg2 = 0; |
| 911 | jobject obj = NULL; |
| 912 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 913 | |
| 914 | sp<AMessage> data; |
| 915 | CHECK(msg->findMessage("data", &data)); |
| 916 | |
| 917 | status_t err = ConvertMessageToMap(env, data, &obj); |
| 918 | if (err != OK) { |
| 919 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 920 | return; |
| 921 | } |
| 922 | |
| 923 | env->CallVoidMethod( |
| 924 | mObject, gFields.postEventFromNativeID, |
| 925 | EVENT_FRAME_RENDERED, arg1, arg2, obj); |
| 926 | |
| 927 | env->DeleteLocalRef(obj); |
| 928 | } |
| 929 | |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 930 | void JMediaCodec::onMessageReceived(const sp<AMessage> &msg) { |
| 931 | switch (msg->what()) { |
| 932 | case kWhatCallbackNotify: |
| 933 | { |
| 934 | handleCallback(msg); |
| 935 | break; |
| 936 | } |
Lajos Molnar | d857857 | 2015-06-05 20:17:33 -0700 | [diff] [blame] | 937 | case kWhatFrameRendered: |
| 938 | { |
| 939 | handleFrameRenderedNotification(msg); |
| 940 | break; |
| 941 | } |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 942 | default: |
| 943 | TRESPASS(); |
| 944 | } |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 947 | } // namespace android |
| 948 | |
| 949 | //////////////////////////////////////////////////////////////////////////////// |
| 950 | |
| 951 | using namespace android; |
| 952 | |
| 953 | static sp<JMediaCodec> setMediaCodec( |
| 954 | JNIEnv *env, jobject thiz, const sp<JMediaCodec> &codec) { |
Wonsik Kim | 61796fd | 2018-09-13 13:15:59 -0700 | [diff] [blame] | 955 | sp<JMediaCodec> old = (JMediaCodec *)env->CallLongMethod(thiz, gFields.lockAndGetContextID); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 956 | if (codec != NULL) { |
| 957 | codec->incStrong(thiz); |
| 958 | } |
| 959 | if (old != NULL) { |
Chong Zhang | 128b012 | 2014-03-01 18:04:13 -0800 | [diff] [blame] | 960 | /* release MediaCodec and stop the looper now before decStrong. |
| 961 | * otherwise JMediaCodec::~JMediaCodec() could be called from within |
| 962 | * its message handler, doing release() from there will deadlock |
| 963 | * (as MediaCodec::release() post synchronous message to the same looper) |
| 964 | */ |
| 965 | old->release(); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 966 | old->decStrong(thiz); |
| 967 | } |
Wonsik Kim | 61796fd | 2018-09-13 13:15:59 -0700 | [diff] [blame] | 968 | env->CallVoidMethod(thiz, gFields.setAndUnlockContextID, (jlong)codec.get()); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 969 | |
| 970 | return old; |
| 971 | } |
| 972 | |
| 973 | static sp<JMediaCodec> getMediaCodec(JNIEnv *env, jobject thiz) { |
Wonsik Kim | 61796fd | 2018-09-13 13:15:59 -0700 | [diff] [blame] | 974 | sp<JMediaCodec> codec = (JMediaCodec *)env->CallLongMethod(thiz, gFields.lockAndGetContextID); |
| 975 | env->CallVoidMethod(thiz, gFields.setAndUnlockContextID, (jlong)codec.get()); |
| 976 | return codec; |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | static void android_media_MediaCodec_release(JNIEnv *env, jobject thiz) { |
| 980 | setMediaCodec(env, thiz, NULL); |
| 981 | } |
| 982 | |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 983 | static void throwCodecException(JNIEnv *env, status_t err, int32_t actionCode, const char *msg) { |
| 984 | jthrowable exception = createCodecException(env, err, actionCode, msg); |
| 985 | env->Throw(exception); |
| 986 | } |
| 987 | |
Andreas Huber | bfc56f4 | 2012-04-19 12:47:07 -0700 | [diff] [blame] | 988 | static void throwCryptoException(JNIEnv *env, status_t err, const char *msg) { |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 989 | ScopedLocalRef<jclass> clazz( |
| 990 | env, env->FindClass("android/media/MediaCodec$CryptoException")); |
| 991 | CHECK(clazz.get() != NULL); |
Andreas Huber | bfc56f4 | 2012-04-19 12:47:07 -0700 | [diff] [blame] | 992 | |
| 993 | jmethodID constructID = |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 994 | env->GetMethodID(clazz.get(), "<init>", "(ILjava/lang/String;)V"); |
Andreas Huber | bfc56f4 | 2012-04-19 12:47:07 -0700 | [diff] [blame] | 995 | CHECK(constructID != NULL); |
| 996 | |
Jeff Tinker | 3ccb34d | 2015-11-09 17:31:05 -0800 | [diff] [blame] | 997 | const char *defaultMsg = "Unknown Error"; |
Andreas Huber | bfc56f4 | 2012-04-19 12:47:07 -0700 | [diff] [blame] | 998 | |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 999 | /* translate OS errors to Java API CryptoException errorCodes (which are positive) */ |
Jeff Tinker | 3ed3826 | 2013-08-02 23:24:51 -0700 | [diff] [blame] | 1000 | switch (err) { |
| 1001 | case ERROR_DRM_NO_LICENSE: |
| 1002 | err = gCryptoErrorCodes.cryptoErrorNoKey; |
Jeff Tinker | 3ccb34d | 2015-11-09 17:31:05 -0800 | [diff] [blame] | 1003 | defaultMsg = "Crypto key not available"; |
Jeff Tinker | 3ed3826 | 2013-08-02 23:24:51 -0700 | [diff] [blame] | 1004 | break; |
| 1005 | case ERROR_DRM_LICENSE_EXPIRED: |
| 1006 | err = gCryptoErrorCodes.cryptoErrorKeyExpired; |
Jeff Tinker | 3ccb34d | 2015-11-09 17:31:05 -0800 | [diff] [blame] | 1007 | defaultMsg = "License expired"; |
Jeff Tinker | 3ed3826 | 2013-08-02 23:24:51 -0700 | [diff] [blame] | 1008 | break; |
| 1009 | case ERROR_DRM_RESOURCE_BUSY: |
| 1010 | err = gCryptoErrorCodes.cryptoErrorResourceBusy; |
Jeff Tinker | 3ccb34d | 2015-11-09 17:31:05 -0800 | [diff] [blame] | 1011 | defaultMsg = "Resource busy or unavailable"; |
Jeff Tinker | 3ed3826 | 2013-08-02 23:24:51 -0700 | [diff] [blame] | 1012 | break; |
Jeff Tinker | 336d3ea | 2014-08-28 17:57:36 -0700 | [diff] [blame] | 1013 | case ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION: |
| 1014 | err = gCryptoErrorCodes.cryptoErrorInsufficientOutputProtection; |
Jeff Tinker | 3ccb34d | 2015-11-09 17:31:05 -0800 | [diff] [blame] | 1015 | defaultMsg = "Required output protections are not active"; |
Jeff Tinker | 336d3ea | 2014-08-28 17:57:36 -0700 | [diff] [blame] | 1016 | break; |
Jeff Tinker | 96a2a95 | 2015-07-01 17:35:18 -0700 | [diff] [blame] | 1017 | case ERROR_DRM_SESSION_NOT_OPENED: |
| 1018 | err = gCryptoErrorCodes.cryptoErrorSessionNotOpened; |
Jeff Tinker | 3ccb34d | 2015-11-09 17:31:05 -0800 | [diff] [blame] | 1019 | defaultMsg = "Attempted to use a closed session"; |
Jeff Tinker | 96a2a95 | 2015-07-01 17:35:18 -0700 | [diff] [blame] | 1020 | break; |
Jeff Tinker | 20594d8 | 2018-12-12 08:31:22 -0800 | [diff] [blame] | 1021 | case ERROR_DRM_INSUFFICIENT_SECURITY: |
| 1022 | err = gCryptoErrorCodes.cryptoErrorInsufficientSecurity; |
| 1023 | defaultMsg = "Required security level is not met"; |
| 1024 | break; |
Jeff Tinker | d393216 | 2016-03-05 11:35:20 -0800 | [diff] [blame] | 1025 | case ERROR_DRM_CANNOT_HANDLE: |
| 1026 | err = gCryptoErrorCodes.cryptoErrorUnsupportedOperation; |
| 1027 | defaultMsg = "Operation not supported in this configuration"; |
| 1028 | break; |
Jeff Tinker | 20594d8 | 2018-12-12 08:31:22 -0800 | [diff] [blame] | 1029 | case ERROR_DRM_FRAME_TOO_LARGE: |
| 1030 | err = gCryptoErrorCodes.cryptoErrorFrameTooLarge; |
| 1031 | defaultMsg = "Decrytped frame exceeds size of output buffer"; |
| 1032 | break; |
| 1033 | case ERROR_DRM_SESSION_LOST_STATE: |
| 1034 | err = gCryptoErrorCodes.cryptoErrorLostState; |
| 1035 | defaultMsg = "Session state was lost, open a new session and retry"; |
| 1036 | break; |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1037 | default: /* Other negative DRM error codes go out as is. */ |
Jeff Tinker | 3ed3826 | 2013-08-02 23:24:51 -0700 | [diff] [blame] | 1038 | break; |
| 1039 | } |
| 1040 | |
Jeff Tinker | 3ccb34d | 2015-11-09 17:31:05 -0800 | [diff] [blame] | 1041 | jstring msgObj = env->NewStringUTF(msg != NULL ? msg : defaultMsg); |
| 1042 | |
Andreas Huber | bfc56f4 | 2012-04-19 12:47:07 -0700 | [diff] [blame] | 1043 | jthrowable exception = |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 1044 | (jthrowable)env->NewObject(clazz.get(), constructID, err, msgObj); |
Andreas Huber | bfc56f4 | 2012-04-19 12:47:07 -0700 | [diff] [blame] | 1045 | |
| 1046 | env->Throw(exception); |
| 1047 | } |
| 1048 | |
| 1049 | static jint throwExceptionAsNecessary( |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1050 | JNIEnv *env, status_t err, int32_t actionCode = ACTION_CODE_FATAL, |
| 1051 | const char *msg = NULL) { |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1052 | switch (err) { |
| 1053 | case OK: |
| 1054 | return 0; |
| 1055 | |
| 1056 | case -EAGAIN: |
| 1057 | return DEQUEUE_INFO_TRY_AGAIN_LATER; |
| 1058 | |
| 1059 | case INFO_FORMAT_CHANGED: |
| 1060 | return DEQUEUE_INFO_OUTPUT_FORMAT_CHANGED; |
| 1061 | |
| 1062 | case INFO_OUTPUT_BUFFERS_CHANGED: |
| 1063 | return DEQUEUE_INFO_OUTPUT_BUFFERS_CHANGED; |
| 1064 | |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1065 | case INVALID_OPERATION: |
| 1066 | jniThrowException(env, "java/lang/IllegalStateException", msg); |
| 1067 | return 0; |
Jeff Tinker | 3ed3826 | 2013-08-02 23:24:51 -0700 | [diff] [blame] | 1068 | |
Lajos Molnar | 5e02ba9 | 2015-05-01 15:59:35 -0700 | [diff] [blame] | 1069 | case BAD_VALUE: |
| 1070 | jniThrowException(env, "java/lang/IllegalArgumentException", msg); |
| 1071 | return 0; |
| 1072 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1073 | default: |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1074 | if (isCryptoError(err)) { |
| 1075 | throwCryptoException(env, err, msg); |
| 1076 | return 0; |
| 1077 | } |
| 1078 | throwCodecException(env, err, actionCode, msg); |
| 1079 | return 0; |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1080 | } |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1081 | } |
| 1082 | |
Lajos Molnar | d857857 | 2015-06-05 20:17:33 -0700 | [diff] [blame] | 1083 | static void android_media_MediaCodec_native_enableOnFrameRenderedListener( |
| 1084 | JNIEnv *env, |
| 1085 | jobject thiz, |
| 1086 | jboolean enabled) { |
| 1087 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1088 | |
| 1089 | if (codec == NULL) { |
| 1090 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
| 1091 | return; |
| 1092 | } |
| 1093 | |
| 1094 | status_t err = codec->enableOnFrameRenderedListener(enabled); |
| 1095 | |
| 1096 | throwExceptionAsNecessary(env, err); |
| 1097 | } |
| 1098 | |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 1099 | static void android_media_MediaCodec_native_setCallback( |
| 1100 | JNIEnv *env, |
| 1101 | jobject thiz, |
| 1102 | jobject cb) { |
| 1103 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1104 | |
| 1105 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1106 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 1107 | return; |
| 1108 | } |
| 1109 | |
| 1110 | status_t err = codec->setCallback(cb); |
| 1111 | |
| 1112 | throwExceptionAsNecessary(env, err); |
| 1113 | } |
| 1114 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1115 | static void android_media_MediaCodec_native_configure( |
| 1116 | JNIEnv *env, |
| 1117 | jobject thiz, |
| 1118 | jobjectArray keys, jobjectArray values, |
| 1119 | jobject jsurface, |
Andreas Huber | 8240d92 | 2012-04-04 14:06:32 -0700 | [diff] [blame] | 1120 | jobject jcrypto, |
Chong Zhang | d5927ae | 2017-01-03 11:07:18 -0800 | [diff] [blame] | 1121 | jobject descramblerBinderObj, |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1122 | jint flags) { |
| 1123 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1124 | |
| 1125 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1126 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1127 | return; |
| 1128 | } |
| 1129 | |
| 1130 | sp<AMessage> format; |
| 1131 | status_t err = ConvertKeyValueArraysToMessage(env, keys, values, &format); |
| 1132 | |
| 1133 | if (err != OK) { |
| 1134 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 1135 | return; |
| 1136 | } |
| 1137 | |
Andy McFadden | d47f7d8 | 2012-12-18 09:48:38 -0800 | [diff] [blame] | 1138 | sp<IGraphicBufferProducer> bufferProducer; |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1139 | if (jsurface != NULL) { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 1140 | sp<Surface> surface(android_view_Surface_getSurface(env, jsurface)); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1141 | if (surface != NULL) { |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 1142 | bufferProducer = surface->getIGraphicBufferProducer(); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1143 | } else { |
| 1144 | jniThrowException( |
| 1145 | env, |
| 1146 | "java/lang/IllegalArgumentException", |
| 1147 | "The surface has been released"); |
| 1148 | return; |
| 1149 | } |
| 1150 | } |
| 1151 | |
Andreas Huber | 8240d92 | 2012-04-04 14:06:32 -0700 | [diff] [blame] | 1152 | sp<ICrypto> crypto; |
| 1153 | if (jcrypto != NULL) { |
| 1154 | crypto = JCrypto::GetCrypto(env, jcrypto); |
| 1155 | } |
| 1156 | |
Chong Zhang | d5927ae | 2017-01-03 11:07:18 -0800 | [diff] [blame] | 1157 | sp<IDescrambler> descrambler; |
| 1158 | if (descramblerBinderObj != NULL) { |
Chong Zhang | cd53855 | 2018-02-21 17:22:19 -0800 | [diff] [blame] | 1159 | descrambler = GetDescrambler(env, descramblerBinderObj); |
Chong Zhang | d5927ae | 2017-01-03 11:07:18 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | err = codec->configure(format, bufferProducer, crypto, descrambler, flags); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1163 | |
| 1164 | throwExceptionAsNecessary(env, err); |
| 1165 | } |
| 1166 | |
Lajos Molnar | 5e02ba9 | 2015-05-01 15:59:35 -0700 | [diff] [blame] | 1167 | static void android_media_MediaCodec_native_setSurface( |
| 1168 | JNIEnv *env, |
| 1169 | jobject thiz, |
| 1170 | jobject jsurface) { |
| 1171 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1172 | |
| 1173 | if (codec == NULL) { |
| 1174 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
| 1175 | return; |
| 1176 | } |
| 1177 | |
| 1178 | sp<IGraphicBufferProducer> bufferProducer; |
| 1179 | if (jsurface != NULL) { |
| 1180 | sp<Surface> surface(android_view_Surface_getSurface(env, jsurface)); |
| 1181 | if (surface != NULL) { |
| 1182 | bufferProducer = surface->getIGraphicBufferProducer(); |
| 1183 | } else { |
| 1184 | jniThrowException( |
| 1185 | env, |
| 1186 | "java/lang/IllegalArgumentException", |
| 1187 | "The surface has been released"); |
| 1188 | return; |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | status_t err = codec->setSurface(bufferProducer); |
| 1193 | throwExceptionAsNecessary(env, err); |
| 1194 | } |
| 1195 | |
Chong Zhang | 8034d60 | 2015-04-28 13:38:48 -0700 | [diff] [blame] | 1196 | sp<PersistentSurface> android_media_MediaCodec_getPersistentInputSurface( |
| 1197 | JNIEnv* env, jobject object) { |
| 1198 | sp<PersistentSurface> persistentSurface; |
| 1199 | |
| 1200 | jobject lock = env->GetObjectField( |
| 1201 | object, gPersistentSurfaceClassInfo.mLock); |
| 1202 | if (env->MonitorEnter(lock) == JNI_OK) { |
| 1203 | persistentSurface = reinterpret_cast<PersistentSurface *>( |
| 1204 | env->GetLongField(object, |
| 1205 | gPersistentSurfaceClassInfo.mPersistentObject)); |
| 1206 | env->MonitorExit(lock); |
| 1207 | } |
| 1208 | env->DeleteLocalRef(lock); |
| 1209 | |
| 1210 | return persistentSurface; |
| 1211 | } |
| 1212 | |
| 1213 | static jobject android_media_MediaCodec_createPersistentInputSurface( |
| 1214 | JNIEnv* env, jclass /* clazz */) { |
| 1215 | ALOGV("android_media_MediaCodec_createPersistentInputSurface"); |
| 1216 | sp<PersistentSurface> persistentSurface = |
| 1217 | MediaCodec::CreatePersistentInputSurface(); |
| 1218 | |
| 1219 | if (persistentSurface == NULL) { |
| 1220 | return NULL; |
| 1221 | } |
| 1222 | |
| 1223 | sp<Surface> surface = new Surface( |
| 1224 | persistentSurface->getBufferProducer(), true); |
| 1225 | if (surface == NULL) { |
| 1226 | return NULL; |
| 1227 | } |
| 1228 | |
| 1229 | jobject object = env->NewObject( |
| 1230 | gPersistentSurfaceClassInfo.clazz, |
| 1231 | gPersistentSurfaceClassInfo.ctor); |
| 1232 | |
| 1233 | if (object == NULL) { |
| 1234 | if (env->ExceptionCheck()) { |
| 1235 | ALOGE("Could not create PersistentSurface."); |
| 1236 | env->ExceptionClear(); |
| 1237 | } |
| 1238 | return NULL; |
| 1239 | } |
| 1240 | |
| 1241 | jobject lock = env->GetObjectField( |
| 1242 | object, gPersistentSurfaceClassInfo.mLock); |
| 1243 | if (env->MonitorEnter(lock) == JNI_OK) { |
| 1244 | env->CallVoidMethod( |
| 1245 | object, |
| 1246 | gPersistentSurfaceClassInfo.setNativeObjectLocked, |
| 1247 | (jlong)surface.get()); |
| 1248 | env->SetLongField( |
| 1249 | object, |
| 1250 | gPersistentSurfaceClassInfo.mPersistentObject, |
| 1251 | (jlong)persistentSurface.get()); |
| 1252 | env->MonitorExit(lock); |
| 1253 | } else { |
| 1254 | env->DeleteLocalRef(object); |
| 1255 | object = NULL; |
| 1256 | } |
| 1257 | env->DeleteLocalRef(lock); |
| 1258 | |
| 1259 | if (object != NULL) { |
| 1260 | surface->incStrong(&sRefBaseOwner); |
| 1261 | persistentSurface->incStrong(&sRefBaseOwner); |
| 1262 | } |
| 1263 | |
| 1264 | return object; |
| 1265 | } |
| 1266 | |
| 1267 | static void android_media_MediaCodec_releasePersistentInputSurface( |
| 1268 | JNIEnv* env, jclass /* clazz */, jobject object) { |
| 1269 | sp<PersistentSurface> persistentSurface; |
| 1270 | |
| 1271 | jobject lock = env->GetObjectField( |
| 1272 | object, gPersistentSurfaceClassInfo.mLock); |
| 1273 | if (env->MonitorEnter(lock) == JNI_OK) { |
| 1274 | persistentSurface = reinterpret_cast<PersistentSurface *>( |
| 1275 | env->GetLongField( |
| 1276 | object, gPersistentSurfaceClassInfo.mPersistentObject)); |
| 1277 | env->SetLongField( |
| 1278 | object, |
| 1279 | gPersistentSurfaceClassInfo.mPersistentObject, |
| 1280 | (jlong)0); |
| 1281 | env->MonitorExit(lock); |
| 1282 | } |
| 1283 | env->DeleteLocalRef(lock); |
| 1284 | |
| 1285 | if (persistentSurface != NULL) { |
| 1286 | persistentSurface->decStrong(&sRefBaseOwner); |
| 1287 | } |
| 1288 | // no need to release surface as it will be released by Surface's jni |
| 1289 | } |
| 1290 | |
Chong Zhang | 9560ddb | 2015-05-13 10:25:29 -0700 | [diff] [blame] | 1291 | static void android_media_MediaCodec_setInputSurface( |
Chong Zhang | 8034d60 | 2015-04-28 13:38:48 -0700 | [diff] [blame] | 1292 | JNIEnv* env, jobject thiz, jobject object) { |
Chong Zhang | 9560ddb | 2015-05-13 10:25:29 -0700 | [diff] [blame] | 1293 | ALOGV("android_media_MediaCodec_setInputSurface"); |
Chong Zhang | 8034d60 | 2015-04-28 13:38:48 -0700 | [diff] [blame] | 1294 | |
| 1295 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1296 | if (codec == NULL) { |
| 1297 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
| 1298 | return; |
| 1299 | } |
| 1300 | |
| 1301 | sp<PersistentSurface> persistentSurface = |
| 1302 | android_media_MediaCodec_getPersistentInputSurface(env, object); |
| 1303 | |
Marco Nelissen | 59cf9aa | 2018-04-19 11:02:00 -0700 | [diff] [blame] | 1304 | if (persistentSurface == NULL) { |
| 1305 | throwExceptionAsNecessary( |
| 1306 | env, BAD_VALUE, ACTION_CODE_FATAL, "input surface not valid"); |
| 1307 | return; |
| 1308 | } |
Chong Zhang | 9560ddb | 2015-05-13 10:25:29 -0700 | [diff] [blame] | 1309 | status_t err = codec->setInputSurface(persistentSurface); |
Chong Zhang | 8034d60 | 2015-04-28 13:38:48 -0700 | [diff] [blame] | 1310 | if (err != NO_ERROR) { |
| 1311 | throwExceptionAsNecessary(env, err); |
| 1312 | } |
| 1313 | } |
| 1314 | |
Andy McFadden | 2621e40 | 2013-02-19 07:29:21 -0800 | [diff] [blame] | 1315 | static jobject android_media_MediaCodec_createInputSurface(JNIEnv* env, |
| 1316 | jobject thiz) { |
| 1317 | ALOGV("android_media_MediaCodec_createInputSurface"); |
| 1318 | |
| 1319 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1320 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1321 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Andy McFadden | 2621e40 | 2013-02-19 07:29:21 -0800 | [diff] [blame] | 1322 | return NULL; |
| 1323 | } |
| 1324 | |
| 1325 | // Tell the MediaCodec that we want to use a Surface as input. |
| 1326 | sp<IGraphicBufferProducer> bufferProducer; |
| 1327 | status_t err = codec->createInputSurface(&bufferProducer); |
| 1328 | if (err != NO_ERROR) { |
| 1329 | throwExceptionAsNecessary(env, err); |
| 1330 | return NULL; |
| 1331 | } |
| 1332 | |
| 1333 | // Wrap the IGBP in a Java-language Surface. |
| 1334 | return android_view_Surface_createFromIGraphicBufferProducer(env, |
| 1335 | bufferProducer); |
| 1336 | } |
| 1337 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1338 | static void android_media_MediaCodec_start(JNIEnv *env, jobject thiz) { |
| 1339 | ALOGV("android_media_MediaCodec_start"); |
| 1340 | |
| 1341 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1342 | |
| 1343 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1344 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1345 | return; |
| 1346 | } |
| 1347 | |
| 1348 | status_t err = codec->start(); |
| 1349 | |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1350 | throwExceptionAsNecessary(env, err, ACTION_CODE_FATAL, "start failed"); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | static void android_media_MediaCodec_stop(JNIEnv *env, jobject thiz) { |
| 1354 | ALOGV("android_media_MediaCodec_stop"); |
| 1355 | |
| 1356 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1357 | |
| 1358 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1359 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1360 | return; |
| 1361 | } |
| 1362 | |
| 1363 | status_t err = codec->stop(); |
| 1364 | |
| 1365 | throwExceptionAsNecessary(env, err); |
| 1366 | } |
| 1367 | |
Lajos Molnar | 1e6e801 | 2014-07-15 16:07:13 -0700 | [diff] [blame] | 1368 | static void android_media_MediaCodec_reset(JNIEnv *env, jobject thiz) { |
| 1369 | ALOGV("android_media_MediaCodec_reset"); |
| 1370 | |
| 1371 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1372 | |
| 1373 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1374 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Lajos Molnar | 1e6e801 | 2014-07-15 16:07:13 -0700 | [diff] [blame] | 1375 | return; |
| 1376 | } |
| 1377 | |
| 1378 | status_t err = codec->reset(); |
| 1379 | if (err != OK) { |
| 1380 | // treat all errors as fatal for now, though resource not available |
| 1381 | // errors could be treated as transient. |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1382 | // we also should avoid sending INVALID_OPERATION here due to |
| 1383 | // the transitory nature of reset(), it should not inadvertently |
| 1384 | // trigger an IllegalStateException. |
| 1385 | err = UNKNOWN_ERROR; |
Lajos Molnar | 1e6e801 | 2014-07-15 16:07:13 -0700 | [diff] [blame] | 1386 | } |
| 1387 | throwExceptionAsNecessary(env, err); |
| 1388 | } |
| 1389 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1390 | static void android_media_MediaCodec_flush(JNIEnv *env, jobject thiz) { |
| 1391 | ALOGV("android_media_MediaCodec_flush"); |
| 1392 | |
| 1393 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1394 | |
| 1395 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1396 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1397 | return; |
| 1398 | } |
| 1399 | |
| 1400 | status_t err = codec->flush(); |
| 1401 | |
| 1402 | throwExceptionAsNecessary(env, err); |
| 1403 | } |
| 1404 | |
| 1405 | static void android_media_MediaCodec_queueInputBuffer( |
| 1406 | JNIEnv *env, |
| 1407 | jobject thiz, |
| 1408 | jint index, |
| 1409 | jint offset, |
| 1410 | jint size, |
| 1411 | jlong timestampUs, |
| 1412 | jint flags) { |
| 1413 | ALOGV("android_media_MediaCodec_queueInputBuffer"); |
| 1414 | |
| 1415 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1416 | |
| 1417 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1418 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1419 | return; |
| 1420 | } |
| 1421 | |
Andreas Huber | bfc56f4 | 2012-04-19 12:47:07 -0700 | [diff] [blame] | 1422 | AString errorDetailMsg; |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1423 | |
Andreas Huber | bfc56f4 | 2012-04-19 12:47:07 -0700 | [diff] [blame] | 1424 | status_t err = codec->queueInputBuffer( |
| 1425 | index, offset, size, timestampUs, flags, &errorDetailMsg); |
| 1426 | |
| 1427 | throwExceptionAsNecessary( |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1428 | env, err, ACTION_CODE_FATAL, errorDetailMsg.empty() ? NULL : errorDetailMsg.c_str()); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1429 | } |
| 1430 | |
Andreas Huber | 9e6bcce | 2012-04-06 12:14:47 -0700 | [diff] [blame] | 1431 | static void android_media_MediaCodec_queueSecureInputBuffer( |
| 1432 | JNIEnv *env, |
| 1433 | jobject thiz, |
| 1434 | jint index, |
| 1435 | jint offset, |
Andreas Huber | 91befdc | 2012-04-18 12:19:51 -0700 | [diff] [blame] | 1436 | jobject cryptoInfoObj, |
Andreas Huber | 9e6bcce | 2012-04-06 12:14:47 -0700 | [diff] [blame] | 1437 | jlong timestampUs, |
| 1438 | jint flags) { |
| 1439 | ALOGV("android_media_MediaCodec_queueSecureInputBuffer"); |
| 1440 | |
| 1441 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1442 | |
| 1443 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1444 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Andreas Huber | 9e6bcce | 2012-04-06 12:14:47 -0700 | [diff] [blame] | 1445 | return; |
| 1446 | } |
| 1447 | |
Andreas Huber | 91befdc | 2012-04-18 12:19:51 -0700 | [diff] [blame] | 1448 | jint numSubSamples = |
| 1449 | env->GetIntField(cryptoInfoObj, gFields.cryptoInfoNumSubSamplesID); |
| 1450 | |
| 1451 | jintArray numBytesOfClearDataObj = |
| 1452 | (jintArray)env->GetObjectField( |
| 1453 | cryptoInfoObj, gFields.cryptoInfoNumBytesOfClearDataID); |
| 1454 | |
| 1455 | jintArray numBytesOfEncryptedDataObj = |
| 1456 | (jintArray)env->GetObjectField( |
| 1457 | cryptoInfoObj, gFields.cryptoInfoNumBytesOfEncryptedDataID); |
| 1458 | |
| 1459 | jbyteArray keyObj = |
| 1460 | (jbyteArray)env->GetObjectField(cryptoInfoObj, gFields.cryptoInfoKeyID); |
| 1461 | |
| 1462 | jbyteArray ivObj = |
| 1463 | (jbyteArray)env->GetObjectField(cryptoInfoObj, gFields.cryptoInfoIVID); |
| 1464 | |
Jeff Tinker | d4ea5d3 | 2015-12-18 11:56:22 -0800 | [diff] [blame] | 1465 | jint jmode = env->GetIntField(cryptoInfoObj, gFields.cryptoInfoModeID); |
| 1466 | enum CryptoPlugin::Mode mode; |
| 1467 | if (jmode == gCryptoModes.Unencrypted) { |
| 1468 | mode = CryptoPlugin::kMode_Unencrypted; |
| 1469 | } else if (jmode == gCryptoModes.AesCtr) { |
| 1470 | mode = CryptoPlugin::kMode_AES_CTR; |
| 1471 | } else if (jmode == gCryptoModes.AesCbc) { |
| 1472 | mode = CryptoPlugin::kMode_AES_CBC; |
| 1473 | } else { |
| 1474 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
| 1475 | return; |
| 1476 | } |
| 1477 | |
| 1478 | jobject patternObj = env->GetObjectField(cryptoInfoObj, gFields.cryptoInfoPatternID); |
| 1479 | |
| 1480 | CryptoPlugin::Pattern pattern; |
Jeff Tinker | e872ac4 | 2016-03-03 15:47:09 -0800 | [diff] [blame] | 1481 | if (patternObj == NULL) { |
| 1482 | pattern.mEncryptBlocks = 0; |
| 1483 | pattern.mSkipBlocks = 0; |
| 1484 | } else { |
Jeff Tinker | d4ea5d3 | 2015-12-18 11:56:22 -0800 | [diff] [blame] | 1485 | pattern.mEncryptBlocks = env->GetIntField(patternObj, gFields.patternEncryptBlocksID); |
| 1486 | pattern.mSkipBlocks = env->GetIntField(patternObj, gFields.patternSkipBlocksID); |
| 1487 | } |
Andreas Huber | 91befdc | 2012-04-18 12:19:51 -0700 | [diff] [blame] | 1488 | |
Andreas Huber | 9e6bcce | 2012-04-06 12:14:47 -0700 | [diff] [blame] | 1489 | status_t err = OK; |
| 1490 | |
| 1491 | CryptoPlugin::SubSample *subSamples = NULL; |
| 1492 | jbyte *key = NULL; |
| 1493 | jbyte *iv = NULL; |
| 1494 | |
| 1495 | if (numSubSamples <= 0) { |
| 1496 | err = -EINVAL; |
| 1497 | } else if (numBytesOfClearDataObj == NULL |
| 1498 | && numBytesOfEncryptedDataObj == NULL) { |
| 1499 | err = -EINVAL; |
| 1500 | } else if (numBytesOfEncryptedDataObj != NULL |
| 1501 | && env->GetArrayLength(numBytesOfEncryptedDataObj) < numSubSamples) { |
| 1502 | err = -ERANGE; |
| 1503 | } else if (numBytesOfClearDataObj != NULL |
| 1504 | && env->GetArrayLength(numBytesOfClearDataObj) < numSubSamples) { |
| 1505 | err = -ERANGE; |
Lajos Molnar | 7ac4f56 | 2014-03-24 15:57:51 -0700 | [diff] [blame] | 1506 | // subSamples array may silently overflow if number of samples are too large. Use |
| 1507 | // INT32_MAX as maximum allocation size may be less than SIZE_MAX on some platforms |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1508 | } else if ( CC_UNLIKELY(numSubSamples >= (signed)(INT32_MAX / sizeof(*subSamples))) ) { |
Lajos Molnar | 7ac4f56 | 2014-03-24 15:57:51 -0700 | [diff] [blame] | 1509 | err = -EINVAL; |
Andreas Huber | 9e6bcce | 2012-04-06 12:14:47 -0700 | [diff] [blame] | 1510 | } else { |
| 1511 | jboolean isCopy; |
| 1512 | |
| 1513 | jint *numBytesOfClearData = |
| 1514 | (numBytesOfClearDataObj == NULL) |
| 1515 | ? NULL |
| 1516 | : env->GetIntArrayElements(numBytesOfClearDataObj, &isCopy); |
| 1517 | |
| 1518 | jint *numBytesOfEncryptedData = |
| 1519 | (numBytesOfEncryptedDataObj == NULL) |
| 1520 | ? NULL |
| 1521 | : env->GetIntArrayElements(numBytesOfEncryptedDataObj, &isCopy); |
| 1522 | |
| 1523 | subSamples = new CryptoPlugin::SubSample[numSubSamples]; |
| 1524 | |
| 1525 | for (jint i = 0; i < numSubSamples; ++i) { |
| 1526 | subSamples[i].mNumBytesOfClearData = |
| 1527 | (numBytesOfClearData == NULL) ? 0 : numBytesOfClearData[i]; |
| 1528 | |
| 1529 | subSamples[i].mNumBytesOfEncryptedData = |
| 1530 | (numBytesOfEncryptedData == NULL) |
| 1531 | ? 0 : numBytesOfEncryptedData[i]; |
| 1532 | } |
| 1533 | |
| 1534 | if (numBytesOfEncryptedData != NULL) { |
| 1535 | env->ReleaseIntArrayElements( |
| 1536 | numBytesOfEncryptedDataObj, numBytesOfEncryptedData, 0); |
| 1537 | numBytesOfEncryptedData = NULL; |
| 1538 | } |
| 1539 | |
| 1540 | if (numBytesOfClearData != NULL) { |
| 1541 | env->ReleaseIntArrayElements( |
| 1542 | numBytesOfClearDataObj, numBytesOfClearData, 0); |
| 1543 | numBytesOfClearData = NULL; |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | if (err == OK && keyObj != NULL) { |
| 1548 | if (env->GetArrayLength(keyObj) != 16) { |
| 1549 | err = -EINVAL; |
| 1550 | } else { |
| 1551 | jboolean isCopy; |
| 1552 | key = env->GetByteArrayElements(keyObj, &isCopy); |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | if (err == OK && ivObj != NULL) { |
| 1557 | if (env->GetArrayLength(ivObj) != 16) { |
| 1558 | err = -EINVAL; |
| 1559 | } else { |
| 1560 | jboolean isCopy; |
| 1561 | iv = env->GetByteArrayElements(ivObj, &isCopy); |
| 1562 | } |
| 1563 | } |
| 1564 | |
Andreas Huber | bfc56f4 | 2012-04-19 12:47:07 -0700 | [diff] [blame] | 1565 | AString errorDetailMsg; |
| 1566 | |
Andreas Huber | 9e6bcce | 2012-04-06 12:14:47 -0700 | [diff] [blame] | 1567 | if (err == OK) { |
| 1568 | err = codec->queueSecureInputBuffer( |
| 1569 | index, offset, |
| 1570 | subSamples, numSubSamples, |
| 1571 | (const uint8_t *)key, (const uint8_t *)iv, |
Jeff Tinker | d4ea5d3 | 2015-12-18 11:56:22 -0800 | [diff] [blame] | 1572 | mode, |
| 1573 | pattern, |
Andreas Huber | bfc56f4 | 2012-04-19 12:47:07 -0700 | [diff] [blame] | 1574 | timestampUs, |
| 1575 | flags, |
| 1576 | &errorDetailMsg); |
Andreas Huber | 9e6bcce | 2012-04-06 12:14:47 -0700 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | if (iv != NULL) { |
| 1580 | env->ReleaseByteArrayElements(ivObj, iv, 0); |
| 1581 | iv = NULL; |
| 1582 | } |
| 1583 | |
| 1584 | if (key != NULL) { |
| 1585 | env->ReleaseByteArrayElements(keyObj, key, 0); |
| 1586 | key = NULL; |
| 1587 | } |
| 1588 | |
| 1589 | delete[] subSamples; |
| 1590 | subSamples = NULL; |
| 1591 | |
Andreas Huber | bfc56f4 | 2012-04-19 12:47:07 -0700 | [diff] [blame] | 1592 | throwExceptionAsNecessary( |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1593 | env, err, ACTION_CODE_FATAL, errorDetailMsg.empty() ? NULL : errorDetailMsg.c_str()); |
Andreas Huber | 9e6bcce | 2012-04-06 12:14:47 -0700 | [diff] [blame] | 1594 | } |
| 1595 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1596 | static jint android_media_MediaCodec_dequeueInputBuffer( |
| 1597 | JNIEnv *env, jobject thiz, jlong timeoutUs) { |
| 1598 | ALOGV("android_media_MediaCodec_dequeueInputBuffer"); |
| 1599 | |
| 1600 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1601 | |
| 1602 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1603 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1604 | return -1; |
| 1605 | } |
| 1606 | |
| 1607 | size_t index; |
| 1608 | status_t err = codec->dequeueInputBuffer(&index, timeoutUs); |
| 1609 | |
| 1610 | if (err == OK) { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 1611 | return (jint) index; |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | return throwExceptionAsNecessary(env, err); |
| 1615 | } |
| 1616 | |
| 1617 | static jint android_media_MediaCodec_dequeueOutputBuffer( |
| 1618 | JNIEnv *env, jobject thiz, jobject bufferInfo, jlong timeoutUs) { |
| 1619 | ALOGV("android_media_MediaCodec_dequeueOutputBuffer"); |
| 1620 | |
| 1621 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1622 | |
| 1623 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1624 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Andreas Huber | 0e97fc2 | 2012-04-03 13:32:16 -0700 | [diff] [blame] | 1625 | return 0; |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | size_t index; |
| 1629 | status_t err = codec->dequeueOutputBuffer( |
| 1630 | env, bufferInfo, &index, timeoutUs); |
| 1631 | |
| 1632 | if (err == OK) { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 1633 | return (jint) index; |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | return throwExceptionAsNecessary(env, err); |
| 1637 | } |
| 1638 | |
| 1639 | static void android_media_MediaCodec_releaseOutputBuffer( |
Lajos Molnar | 7c513b6b | 2014-05-08 17:16:45 -0700 | [diff] [blame] | 1640 | JNIEnv *env, jobject thiz, |
| 1641 | jint index, jboolean render, jboolean updatePTS, jlong timestampNs) { |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1642 | ALOGV("android_media_MediaCodec_renderOutputBufferAndRelease"); |
| 1643 | |
| 1644 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1645 | |
| 1646 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1647 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1648 | return; |
| 1649 | } |
| 1650 | |
Lajos Molnar | 7c513b6b | 2014-05-08 17:16:45 -0700 | [diff] [blame] | 1651 | status_t err = codec->releaseOutputBuffer(index, render, updatePTS, timestampNs); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1652 | |
| 1653 | throwExceptionAsNecessary(env, err); |
| 1654 | } |
| 1655 | |
Andy McFadden | 2621e40 | 2013-02-19 07:29:21 -0800 | [diff] [blame] | 1656 | static void android_media_MediaCodec_signalEndOfInputStream(JNIEnv* env, |
| 1657 | jobject thiz) { |
| 1658 | ALOGV("android_media_MediaCodec_signalEndOfInputStream"); |
| 1659 | |
| 1660 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1661 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1662 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Andy McFadden | 2621e40 | 2013-02-19 07:29:21 -0800 | [diff] [blame] | 1663 | return; |
| 1664 | } |
| 1665 | |
| 1666 | status_t err = codec->signalEndOfInputStream(); |
| 1667 | |
| 1668 | throwExceptionAsNecessary(env, err); |
| 1669 | } |
| 1670 | |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 1671 | static jobject android_media_MediaCodec_getFormatNative( |
| 1672 | JNIEnv *env, jobject thiz, jboolean input) { |
| 1673 | ALOGV("android_media_MediaCodec_getFormatNative"); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1674 | |
| 1675 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1676 | |
| 1677 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1678 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1679 | return NULL; |
| 1680 | } |
| 1681 | |
| 1682 | jobject format; |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 1683 | status_t err = codec->getFormat(env, input, &format); |
| 1684 | |
| 1685 | if (err == OK) { |
| 1686 | return format; |
| 1687 | } |
| 1688 | |
| 1689 | throwExceptionAsNecessary(env, err); |
| 1690 | |
| 1691 | return NULL; |
| 1692 | } |
| 1693 | |
| 1694 | static jobject android_media_MediaCodec_getOutputFormatForIndexNative( |
| 1695 | JNIEnv *env, jobject thiz, jint index) { |
| 1696 | ALOGV("android_media_MediaCodec_getOutputFormatForIndexNative"); |
| 1697 | |
| 1698 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1699 | |
| 1700 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1701 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 1702 | return NULL; |
| 1703 | } |
| 1704 | |
| 1705 | jobject format; |
| 1706 | status_t err = codec->getOutputFormat(env, index, &format); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1707 | |
| 1708 | if (err == OK) { |
| 1709 | return format; |
| 1710 | } |
| 1711 | |
| 1712 | throwExceptionAsNecessary(env, err); |
| 1713 | |
| 1714 | return NULL; |
| 1715 | } |
| 1716 | |
| 1717 | static jobjectArray android_media_MediaCodec_getBuffers( |
| 1718 | JNIEnv *env, jobject thiz, jboolean input) { |
| 1719 | ALOGV("android_media_MediaCodec_getBuffers"); |
| 1720 | |
| 1721 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1722 | |
| 1723 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1724 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1725 | return NULL; |
| 1726 | } |
| 1727 | |
| 1728 | jobjectArray buffers; |
| 1729 | status_t err = codec->getBuffers(env, input, &buffers); |
| 1730 | |
| 1731 | if (err == OK) { |
| 1732 | return buffers; |
| 1733 | } |
| 1734 | |
Marco Nelissen | cbbea8e | 2012-12-19 11:42:55 -0800 | [diff] [blame] | 1735 | // if we're out of memory, an exception was already thrown |
| 1736 | if (err != NO_MEMORY) { |
| 1737 | throwExceptionAsNecessary(env, err); |
| 1738 | } |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1739 | |
| 1740 | return NULL; |
| 1741 | } |
| 1742 | |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 1743 | static jobject android_media_MediaCodec_getBuffer( |
| 1744 | JNIEnv *env, jobject thiz, jboolean input, jint index) { |
| 1745 | ALOGV("android_media_MediaCodec_getBuffer"); |
| 1746 | |
| 1747 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1748 | |
| 1749 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1750 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 1751 | return NULL; |
| 1752 | } |
| 1753 | |
| 1754 | jobject buffer; |
| 1755 | status_t err = codec->getBuffer(env, input, index, &buffer); |
| 1756 | |
| 1757 | if (err == OK) { |
| 1758 | return buffer; |
| 1759 | } |
| 1760 | |
| 1761 | // if we're out of memory, an exception was already thrown |
| 1762 | if (err != NO_MEMORY) { |
| 1763 | throwExceptionAsNecessary(env, err); |
| 1764 | } |
| 1765 | |
| 1766 | return NULL; |
| 1767 | } |
| 1768 | |
| 1769 | static jobject android_media_MediaCodec_getImage( |
| 1770 | JNIEnv *env, jobject thiz, jboolean input, jint index) { |
| 1771 | ALOGV("android_media_MediaCodec_getImage"); |
| 1772 | |
| 1773 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1774 | |
| 1775 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1776 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 1777 | return NULL; |
| 1778 | } |
| 1779 | |
| 1780 | jobject image; |
| 1781 | status_t err = codec->getImage(env, input, index, &image); |
| 1782 | |
| 1783 | if (err == OK) { |
| 1784 | return image; |
| 1785 | } |
| 1786 | |
| 1787 | // if we're out of memory, an exception was already thrown |
| 1788 | if (err != NO_MEMORY) { |
| 1789 | throwExceptionAsNecessary(env, err); |
| 1790 | } |
| 1791 | |
| 1792 | return NULL; |
| 1793 | } |
| 1794 | |
Martin Storsjo | 056ef2e | 2012-09-25 11:53:04 +0300 | [diff] [blame] | 1795 | static jobject android_media_MediaCodec_getName( |
| 1796 | JNIEnv *env, jobject thiz) { |
| 1797 | ALOGV("android_media_MediaCodec_getName"); |
| 1798 | |
| 1799 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1800 | |
| 1801 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1802 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Martin Storsjo | 056ef2e | 2012-09-25 11:53:04 +0300 | [diff] [blame] | 1803 | return NULL; |
| 1804 | } |
| 1805 | |
| 1806 | jstring name; |
| 1807 | status_t err = codec->getName(env, &name); |
| 1808 | |
| 1809 | if (err == OK) { |
| 1810 | return name; |
| 1811 | } |
| 1812 | |
| 1813 | throwExceptionAsNecessary(env, err); |
| 1814 | |
| 1815 | return NULL; |
| 1816 | } |
| 1817 | |
Chong Zhang | a0b72a6 | 2018-02-28 18:46:26 -0800 | [diff] [blame] | 1818 | static jobject android_media_MediaCodec_getOwnCodecInfo( |
| 1819 | JNIEnv *env, jobject thiz) { |
| 1820 | ALOGV("android_media_MediaCodec_getOwnCodecInfo"); |
| 1821 | |
| 1822 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1823 | |
| 1824 | if (codec == NULL) { |
| 1825 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
| 1826 | return NULL; |
| 1827 | } |
| 1828 | |
| 1829 | jobject codecInfoObj; |
| 1830 | status_t err = codec->getCodecInfo(env, &codecInfoObj); |
| 1831 | |
| 1832 | if (err == OK) { |
| 1833 | return codecInfoObj; |
| 1834 | } |
| 1835 | |
| 1836 | throwExceptionAsNecessary(env, err); |
| 1837 | |
| 1838 | return NULL; |
| 1839 | } |
| 1840 | |
Ray Essick | 0e0fee1 | 2017-01-25 18:01:56 -0800 | [diff] [blame] | 1841 | static jobject |
Ray Essick | f2d0e40 | 2017-03-09 10:17:51 -0800 | [diff] [blame] | 1842 | android_media_MediaCodec_native_getMetrics(JNIEnv *env, jobject thiz) |
Ray Essick | 0e0fee1 | 2017-01-25 18:01:56 -0800 | [diff] [blame] | 1843 | { |
Ray Essick | f2d0e40 | 2017-03-09 10:17:51 -0800 | [diff] [blame] | 1844 | ALOGV("android_media_MediaCodec_native_getMetrics"); |
Ray Essick | 0e0fee1 | 2017-01-25 18:01:56 -0800 | [diff] [blame] | 1845 | |
| 1846 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1847 | if (codec == NULL ) { |
| 1848 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 1849 | return 0; |
| 1850 | } |
| 1851 | |
| 1852 | // get what we have for the metrics from the codec |
Ray Essick | e981a55 | 2019-08-26 15:34:10 -0700 | [diff] [blame] | 1853 | MediaAnalyticsItem *item = 0; |
Ray Essick | f2d0e40 | 2017-03-09 10:17:51 -0800 | [diff] [blame] | 1854 | |
| 1855 | status_t err = codec->getMetrics(env, item); |
Ray Essick | 0e0fee1 | 2017-01-25 18:01:56 -0800 | [diff] [blame] | 1856 | if (err != OK) { |
| 1857 | ALOGE("getMetrics failed"); |
| 1858 | return (jobject) NULL; |
| 1859 | } |
| 1860 | |
Ray Essick | 0e0fee1 | 2017-01-25 18:01:56 -0800 | [diff] [blame] | 1861 | jobject mybundle = MediaMetricsJNI::writeMetricsToBundle(env, item, NULL); |
| 1862 | |
| 1863 | // housekeeping |
| 1864 | delete item; |
Ray Essick | e981a55 | 2019-08-26 15:34:10 -0700 | [diff] [blame] | 1865 | item = 0; |
Ray Essick | 0e0fee1 | 2017-01-25 18:01:56 -0800 | [diff] [blame] | 1866 | |
| 1867 | return mybundle; |
| 1868 | } |
| 1869 | |
Andreas Huber | 226065b | 2013-08-12 10:14:11 -0700 | [diff] [blame] | 1870 | static void android_media_MediaCodec_setParameters( |
| 1871 | JNIEnv *env, jobject thiz, jobjectArray keys, jobjectArray vals) { |
| 1872 | ALOGV("android_media_MediaCodec_setParameters"); |
| 1873 | |
| 1874 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1875 | |
| 1876 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1877 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Andreas Huber | 226065b | 2013-08-12 10:14:11 -0700 | [diff] [blame] | 1878 | return; |
| 1879 | } |
| 1880 | |
| 1881 | sp<AMessage> params; |
| 1882 | status_t err = ConvertKeyValueArraysToMessage(env, keys, vals, ¶ms); |
| 1883 | |
| 1884 | if (err == OK) { |
| 1885 | err = codec->setParameters(params); |
| 1886 | } |
| 1887 | |
| 1888 | throwExceptionAsNecessary(env, err); |
| 1889 | } |
| 1890 | |
Andreas Huber | b12a539 | 2012-04-30 14:18:33 -0700 | [diff] [blame] | 1891 | static void android_media_MediaCodec_setVideoScalingMode( |
| 1892 | JNIEnv *env, jobject thiz, jint mode) { |
| 1893 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1894 | |
| 1895 | if (codec == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 1896 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
Andreas Huber | b12a539 | 2012-04-30 14:18:33 -0700 | [diff] [blame] | 1897 | return; |
| 1898 | } |
| 1899 | |
| 1900 | if (mode != NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW |
| 1901 | && mode != NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) { |
Dongwon Kang | bef01e4 | 2017-06-16 14:02:31 -0700 | [diff] [blame] | 1902 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
Andreas Huber | b12a539 | 2012-04-30 14:18:33 -0700 | [diff] [blame] | 1903 | return; |
| 1904 | } |
| 1905 | |
| 1906 | codec->setVideoScalingMode(mode); |
| 1907 | } |
| 1908 | |
ybai | 5e05320 | 2018-11-01 13:02:15 +0800 | [diff] [blame] | 1909 | static void android_media_MediaCodec_setAudioPresentation( |
| 1910 | JNIEnv *env, jobject thiz, jint presentationId, jint programId) { |
| 1911 | sp<JMediaCodec> codec = getMediaCodec(env, thiz); |
| 1912 | |
| 1913 | if (codec == NULL) { |
| 1914 | throwExceptionAsNecessary(env, INVALID_OPERATION); |
| 1915 | return; |
| 1916 | } |
| 1917 | |
| 1918 | codec->selectAudioPresentation((int32_t)presentationId, (int32_t)programId); |
| 1919 | } |
| 1920 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1921 | static void android_media_MediaCodec_native_init(JNIEnv *env) { |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 1922 | ScopedLocalRef<jclass> clazz( |
| 1923 | env, env->FindClass("android/media/MediaCodec")); |
| 1924 | CHECK(clazz.get() != NULL); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 1925 | |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 1926 | gFields.postEventFromNativeID = |
| 1927 | env->GetMethodID( |
| 1928 | clazz.get(), "postEventFromNative", "(IIILjava/lang/Object;)V"); |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 1929 | CHECK(gFields.postEventFromNativeID != NULL); |
| 1930 | |
Wonsik Kim | 61796fd | 2018-09-13 13:15:59 -0700 | [diff] [blame] | 1931 | gFields.lockAndGetContextID = |
| 1932 | env->GetMethodID( |
| 1933 | clazz.get(), "lockAndGetContext", "()J"); |
| 1934 | CHECK(gFields.lockAndGetContextID != NULL); |
| 1935 | |
| 1936 | gFields.setAndUnlockContextID = |
| 1937 | env->GetMethodID( |
| 1938 | clazz.get(), "setAndUnlockContext", "(J)V"); |
| 1939 | CHECK(gFields.setAndUnlockContextID != NULL); |
| 1940 | |
Jeff Tinker | d4ea5d3 | 2015-12-18 11:56:22 -0800 | [diff] [blame] | 1941 | jfieldID field; |
| 1942 | field = env->GetStaticFieldID(clazz.get(), "CRYPTO_MODE_UNENCRYPTED", "I"); |
| 1943 | CHECK(field != NULL); |
| 1944 | gCryptoModes.Unencrypted = |
| 1945 | env->GetStaticIntField(clazz.get(), field); |
| 1946 | |
| 1947 | field = env->GetStaticFieldID(clazz.get(), "CRYPTO_MODE_AES_CTR", "I"); |
| 1948 | CHECK(field != NULL); |
| 1949 | gCryptoModes.AesCtr = |
| 1950 | env->GetStaticIntField(clazz.get(), field); |
| 1951 | |
| 1952 | field = env->GetStaticFieldID(clazz.get(), "CRYPTO_MODE_AES_CBC", "I"); |
| 1953 | CHECK(field != NULL); |
| 1954 | gCryptoModes.AesCbc = |
| 1955 | env->GetStaticIntField(clazz.get(), field); |
| 1956 | |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 1957 | clazz.reset(env->FindClass("android/media/MediaCodec$CryptoInfo")); |
| 1958 | CHECK(clazz.get() != NULL); |
Andreas Huber | 91befdc | 2012-04-18 12:19:51 -0700 | [diff] [blame] | 1959 | |
| 1960 | gFields.cryptoInfoNumSubSamplesID = |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 1961 | env->GetFieldID(clazz.get(), "numSubSamples", "I"); |
Andreas Huber | 91befdc | 2012-04-18 12:19:51 -0700 | [diff] [blame] | 1962 | CHECK(gFields.cryptoInfoNumSubSamplesID != NULL); |
| 1963 | |
| 1964 | gFields.cryptoInfoNumBytesOfClearDataID = |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 1965 | env->GetFieldID(clazz.get(), "numBytesOfClearData", "[I"); |
Andreas Huber | 91befdc | 2012-04-18 12:19:51 -0700 | [diff] [blame] | 1966 | CHECK(gFields.cryptoInfoNumBytesOfClearDataID != NULL); |
| 1967 | |
| 1968 | gFields.cryptoInfoNumBytesOfEncryptedDataID = |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 1969 | env->GetFieldID(clazz.get(), "numBytesOfEncryptedData", "[I"); |
Andreas Huber | 91befdc | 2012-04-18 12:19:51 -0700 | [diff] [blame] | 1970 | CHECK(gFields.cryptoInfoNumBytesOfEncryptedDataID != NULL); |
| 1971 | |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 1972 | gFields.cryptoInfoKeyID = env->GetFieldID(clazz.get(), "key", "[B"); |
Andreas Huber | 91befdc | 2012-04-18 12:19:51 -0700 | [diff] [blame] | 1973 | CHECK(gFields.cryptoInfoKeyID != NULL); |
| 1974 | |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 1975 | gFields.cryptoInfoIVID = env->GetFieldID(clazz.get(), "iv", "[B"); |
Andreas Huber | 91befdc | 2012-04-18 12:19:51 -0700 | [diff] [blame] | 1976 | CHECK(gFields.cryptoInfoIVID != NULL); |
| 1977 | |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 1978 | gFields.cryptoInfoModeID = env->GetFieldID(clazz.get(), "mode", "I"); |
Andreas Huber | 91befdc | 2012-04-18 12:19:51 -0700 | [diff] [blame] | 1979 | CHECK(gFields.cryptoInfoModeID != NULL); |
Jeff Tinker | 3ed3826 | 2013-08-02 23:24:51 -0700 | [diff] [blame] | 1980 | |
Jeff Tinker | d4ea5d3 | 2015-12-18 11:56:22 -0800 | [diff] [blame] | 1981 | gFields.cryptoInfoPatternID = env->GetFieldID(clazz.get(), "pattern", |
| 1982 | "Landroid/media/MediaCodec$CryptoInfo$Pattern;"); |
| 1983 | CHECK(gFields.cryptoInfoPatternID != NULL); |
| 1984 | |
| 1985 | clazz.reset(env->FindClass("android/media/MediaCodec$CryptoInfo$Pattern")); |
| 1986 | CHECK(clazz.get() != NULL); |
| 1987 | |
| 1988 | gFields.patternEncryptBlocksID = env->GetFieldID(clazz.get(), "mEncryptBlocks", "I"); |
| 1989 | CHECK(gFields.patternEncryptBlocksID != NULL); |
| 1990 | |
| 1991 | gFields.patternSkipBlocksID = env->GetFieldID(clazz.get(), "mSkipBlocks", "I"); |
| 1992 | CHECK(gFields.patternSkipBlocksID != NULL); |
| 1993 | |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 1994 | clazz.reset(env->FindClass("android/media/MediaCodec$CryptoException")); |
| 1995 | CHECK(clazz.get() != NULL); |
Jeff Tinker | 3ed3826 | 2013-08-02 23:24:51 -0700 | [diff] [blame] | 1996 | |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 1997 | field = env->GetStaticFieldID(clazz.get(), "ERROR_NO_KEY", "I"); |
Jeff Tinker | 3ed3826 | 2013-08-02 23:24:51 -0700 | [diff] [blame] | 1998 | CHECK(field != NULL); |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 1999 | gCryptoErrorCodes.cryptoErrorNoKey = |
| 2000 | env->GetStaticIntField(clazz.get(), field); |
Jeff Tinker | 3ed3826 | 2013-08-02 23:24:51 -0700 | [diff] [blame] | 2001 | |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 2002 | field = env->GetStaticFieldID(clazz.get(), "ERROR_KEY_EXPIRED", "I"); |
Jeff Tinker | 3ed3826 | 2013-08-02 23:24:51 -0700 | [diff] [blame] | 2003 | CHECK(field != NULL); |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 2004 | gCryptoErrorCodes.cryptoErrorKeyExpired = |
| 2005 | env->GetStaticIntField(clazz.get(), field); |
Jeff Tinker | 3ed3826 | 2013-08-02 23:24:51 -0700 | [diff] [blame] | 2006 | |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 2007 | field = env->GetStaticFieldID(clazz.get(), "ERROR_RESOURCE_BUSY", "I"); |
Jeff Tinker | 3ed3826 | 2013-08-02 23:24:51 -0700 | [diff] [blame] | 2008 | CHECK(field != NULL); |
Andreas Huber | 8d5f3e3 | 2013-08-12 09:19:45 -0700 | [diff] [blame] | 2009 | gCryptoErrorCodes.cryptoErrorResourceBusy = |
| 2010 | env->GetStaticIntField(clazz.get(), field); |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 2011 | |
Jeff Tinker | 336d3ea | 2014-08-28 17:57:36 -0700 | [diff] [blame] | 2012 | field = env->GetStaticFieldID(clazz.get(), "ERROR_INSUFFICIENT_OUTPUT_PROTECTION", "I"); |
| 2013 | CHECK(field != NULL); |
| 2014 | gCryptoErrorCodes.cryptoErrorInsufficientOutputProtection = |
| 2015 | env->GetStaticIntField(clazz.get(), field); |
| 2016 | |
Jeff Tinker | 96a2a95 | 2015-07-01 17:35:18 -0700 | [diff] [blame] | 2017 | field = env->GetStaticFieldID(clazz.get(), "ERROR_SESSION_NOT_OPENED", "I"); |
| 2018 | CHECK(field != NULL); |
| 2019 | gCryptoErrorCodes.cryptoErrorSessionNotOpened = |
| 2020 | env->GetStaticIntField(clazz.get(), field); |
| 2021 | |
Jeff Tinker | 20594d8 | 2018-12-12 08:31:22 -0800 | [diff] [blame] | 2022 | field = env->GetStaticFieldID(clazz.get(), "ERROR_INSUFFICIENT_SECURITY", "I"); |
| 2023 | CHECK(field != NULL); |
| 2024 | gCryptoErrorCodes.cryptoErrorInsufficientSecurity = |
| 2025 | env->GetStaticIntField(clazz.get(), field); |
| 2026 | |
Jeff Tinker | d393216 | 2016-03-05 11:35:20 -0800 | [diff] [blame] | 2027 | field = env->GetStaticFieldID(clazz.get(), "ERROR_UNSUPPORTED_OPERATION", "I"); |
| 2028 | CHECK(field != NULL); |
| 2029 | gCryptoErrorCodes.cryptoErrorUnsupportedOperation = |
| 2030 | env->GetStaticIntField(clazz.get(), field); |
| 2031 | |
Jeff Tinker | 20594d8 | 2018-12-12 08:31:22 -0800 | [diff] [blame] | 2032 | field = env->GetStaticFieldID(clazz.get(), "ERROR_FRAME_TOO_LARGE", "I"); |
| 2033 | CHECK(field != NULL); |
| 2034 | gCryptoErrorCodes.cryptoErrorFrameTooLarge = |
| 2035 | env->GetStaticIntField(clazz.get(), field); |
| 2036 | |
| 2037 | field = env->GetStaticFieldID(clazz.get(), "ERROR_LOST_STATE", "I"); |
| 2038 | CHECK(field != NULL); |
| 2039 | gCryptoErrorCodes.cryptoErrorLostState = |
| 2040 | env->GetStaticIntField(clazz.get(), field); |
| 2041 | |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 2042 | clazz.reset(env->FindClass("android/media/MediaCodec$CodecException")); |
| 2043 | CHECK(clazz.get() != NULL); |
| 2044 | field = env->GetStaticFieldID(clazz.get(), "ACTION_TRANSIENT", "I"); |
| 2045 | CHECK(field != NULL); |
| 2046 | gCodecActionCodes.codecActionTransient = |
| 2047 | env->GetStaticIntField(clazz.get(), field); |
| 2048 | |
| 2049 | field = env->GetStaticFieldID(clazz.get(), "ACTION_RECOVERABLE", "I"); |
| 2050 | CHECK(field != NULL); |
| 2051 | gCodecActionCodes.codecActionRecoverable = |
| 2052 | env->GetStaticIntField(clazz.get(), field); |
Ronghua Wu | 9e9ec94 | 2015-04-15 17:10:31 -0700 | [diff] [blame] | 2053 | |
Ronghua Wu | c53ad69 | 2015-05-08 14:40:49 -0700 | [diff] [blame] | 2054 | field = env->GetStaticFieldID(clazz.get(), "ERROR_INSUFFICIENT_RESOURCE", "I"); |
Ronghua Wu | 9e9ec94 | 2015-04-15 17:10:31 -0700 | [diff] [blame] | 2055 | CHECK(field != NULL); |
Ronghua Wu | c53ad69 | 2015-05-08 14:40:49 -0700 | [diff] [blame] | 2056 | gCodecErrorCodes.errorInsufficientResource = |
Ronghua Wu | 9e9ec94 | 2015-04-15 17:10:31 -0700 | [diff] [blame] | 2057 | env->GetStaticIntField(clazz.get(), field); |
| 2058 | |
Ronghua Wu | c53ad69 | 2015-05-08 14:40:49 -0700 | [diff] [blame] | 2059 | field = env->GetStaticFieldID(clazz.get(), "ERROR_RECLAIMED", "I"); |
Ronghua Wu | 9e9ec94 | 2015-04-15 17:10:31 -0700 | [diff] [blame] | 2060 | CHECK(field != NULL); |
Ronghua Wu | c53ad69 | 2015-05-08 14:40:49 -0700 | [diff] [blame] | 2061 | gCodecErrorCodes.errorReclaimed = |
Ronghua Wu | 9e9ec94 | 2015-04-15 17:10:31 -0700 | [diff] [blame] | 2062 | env->GetStaticIntField(clazz.get(), field); |
Chong Zhang | 8034d60 | 2015-04-28 13:38:48 -0700 | [diff] [blame] | 2063 | |
| 2064 | clazz.reset(env->FindClass("android/view/Surface")); |
| 2065 | CHECK(clazz.get() != NULL); |
| 2066 | |
| 2067 | field = env->GetFieldID(clazz.get(), "mLock", "Ljava/lang/Object;"); |
| 2068 | CHECK(field != NULL); |
| 2069 | gPersistentSurfaceClassInfo.mLock = field; |
| 2070 | |
| 2071 | jmethodID method = env->GetMethodID(clazz.get(), "setNativeObjectLocked", "(J)V"); |
| 2072 | CHECK(method != NULL); |
| 2073 | gPersistentSurfaceClassInfo.setNativeObjectLocked = method; |
| 2074 | |
| 2075 | clazz.reset(env->FindClass("android/media/MediaCodec$PersistentSurface")); |
| 2076 | CHECK(clazz.get() != NULL); |
| 2077 | gPersistentSurfaceClassInfo.clazz = (jclass)env->NewGlobalRef(clazz.get()); |
| 2078 | |
| 2079 | method = env->GetMethodID(clazz.get(), "<init>", "()V"); |
| 2080 | CHECK(method != NULL); |
| 2081 | gPersistentSurfaceClassInfo.ctor = method; |
| 2082 | |
| 2083 | field = env->GetFieldID(clazz.get(), "mPersistentObject", "J"); |
| 2084 | CHECK(field != NULL); |
| 2085 | gPersistentSurfaceClassInfo.mPersistentObject = field; |
Chong Zhang | a0b72a6 | 2018-02-28 18:46:26 -0800 | [diff] [blame] | 2086 | |
| 2087 | clazz.reset(env->FindClass("android/media/MediaCodecInfo$CodecCapabilities")); |
| 2088 | CHECK(clazz.get() != NULL); |
| 2089 | gCodecInfo.capsClazz = (jclass)env->NewGlobalRef(clazz.get()); |
| 2090 | |
| 2091 | method = env->GetMethodID(clazz.get(), "<init>", |
Lajos Molnar | d2a7f47 | 2018-11-15 12:49:20 -0800 | [diff] [blame] | 2092 | "([Landroid/media/MediaCodecInfo$CodecProfileLevel;[IZ" |
Chong Zhang | a0b72a6 | 2018-02-28 18:46:26 -0800 | [diff] [blame] | 2093 | "Ljava/util/Map;Ljava/util/Map;)V"); |
| 2094 | CHECK(method != NULL); |
| 2095 | gCodecInfo.capsCtorId = method; |
| 2096 | |
| 2097 | clazz.reset(env->FindClass("android/media/MediaCodecInfo$CodecProfileLevel")); |
| 2098 | CHECK(clazz.get() != NULL); |
| 2099 | gCodecInfo.profileLevelClazz = (jclass)env->NewGlobalRef(clazz.get()); |
| 2100 | |
| 2101 | field = env->GetFieldID(clazz.get(), "profile", "I"); |
| 2102 | CHECK(field != NULL); |
| 2103 | gCodecInfo.profileField = field; |
| 2104 | |
| 2105 | field = env->GetFieldID(clazz.get(), "level", "I"); |
| 2106 | CHECK(field != NULL); |
| 2107 | gCodecInfo.levelField = field; |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | static void android_media_MediaCodec_native_setup( |
| 2111 | JNIEnv *env, jobject thiz, |
| 2112 | jstring name, jboolean nameIsType, jboolean encoder) { |
| 2113 | if (name == NULL) { |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 2114 | jniThrowException(env, "java/lang/NullPointerException", NULL); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 2115 | return; |
| 2116 | } |
| 2117 | |
| 2118 | const char *tmp = env->GetStringUTFChars(name, NULL); |
| 2119 | |
| 2120 | if (tmp == NULL) { |
| 2121 | return; |
| 2122 | } |
| 2123 | |
| 2124 | sp<JMediaCodec> codec = new JMediaCodec(env, thiz, tmp, nameIsType, encoder); |
| 2125 | |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 2126 | const status_t err = codec->initCheck(); |
| 2127 | if (err == NAME_NOT_FOUND) { |
| 2128 | // fail and do not try again. |
| 2129 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 2130 | String8::format("Failed to initialize %s, error %#x", tmp, err)); |
| 2131 | env->ReleaseStringUTFChars(name, tmp); |
| 2132 | return; |
Ronghua Wu | c53ad69 | 2015-05-08 14:40:49 -0700 | [diff] [blame] | 2133 | } if (err == NO_MEMORY) { |
| 2134 | throwCodecException(env, err, ACTION_CODE_TRANSIENT, |
| 2135 | String8::format("Failed to initialize %s, error %#x", tmp, err)); |
| 2136 | env->ReleaseStringUTFChars(name, tmp); |
| 2137 | return; |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 2138 | } else if (err != OK) { |
| 2139 | // believed possible to try again |
| 2140 | jniThrowException(env, "java/io/IOException", |
| 2141 | String8::format("Failed to find matching codec %s, error %#x", tmp, err)); |
| 2142 | env->ReleaseStringUTFChars(name, tmp); |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 2143 | return; |
| 2144 | } |
| 2145 | |
Andy Hung | 5f9aa0b | 2014-07-30 15:48:21 -0700 | [diff] [blame] | 2146 | env->ReleaseStringUTFChars(name, tmp); |
| 2147 | |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 2148 | codec->registerSelf(); |
| 2149 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 2150 | setMediaCodec(env,thiz, codec); |
| 2151 | } |
| 2152 | |
| 2153 | static void android_media_MediaCodec_native_finalize( |
| 2154 | JNIEnv *env, jobject thiz) { |
| 2155 | android_media_MediaCodec_release(env, thiz); |
| 2156 | } |
| 2157 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 2158 | static const JNINativeMethod gMethods[] = { |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 2159 | { "native_release", "()V", (void *)android_media_MediaCodec_release }, |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 2160 | |
Lajos Molnar | 1e6e801 | 2014-07-15 16:07:13 -0700 | [diff] [blame] | 2161 | { "native_reset", "()V", (void *)android_media_MediaCodec_reset }, |
| 2162 | |
Chong Zhang | 8034d60 | 2015-04-28 13:38:48 -0700 | [diff] [blame] | 2163 | { "native_releasePersistentInputSurface", |
| 2164 | "(Landroid/view/Surface;)V", |
| 2165 | (void *)android_media_MediaCodec_releasePersistentInputSurface}, |
| 2166 | |
| 2167 | { "native_createPersistentInputSurface", |
| 2168 | "()Landroid/media/MediaCodec$PersistentSurface;", |
| 2169 | (void *)android_media_MediaCodec_createPersistentInputSurface }, |
| 2170 | |
Chong Zhang | 9560ddb | 2015-05-13 10:25:29 -0700 | [diff] [blame] | 2171 | { "native_setInputSurface", "(Landroid/view/Surface;)V", |
| 2172 | (void *)android_media_MediaCodec_setInputSurface }, |
Chong Zhang | 8034d60 | 2015-04-28 13:38:48 -0700 | [diff] [blame] | 2173 | |
Lajos Molnar | d857857 | 2015-06-05 20:17:33 -0700 | [diff] [blame] | 2174 | { "native_enableOnFrameRenderedListener", "(Z)V", |
| 2175 | (void *)android_media_MediaCodec_native_enableOnFrameRenderedListener }, |
| 2176 | |
Chong Zhang | 8d5e556 | 2014-07-08 18:49:21 -0700 | [diff] [blame] | 2177 | { "native_setCallback", |
| 2178 | "(Landroid/media/MediaCodec$Callback;)V", |
| 2179 | (void *)android_media_MediaCodec_native_setCallback }, |
| 2180 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 2181 | { "native_configure", |
Andreas Huber | 8240d92 | 2012-04-04 14:06:32 -0700 | [diff] [blame] | 2182 | "([Ljava/lang/String;[Ljava/lang/Object;Landroid/view/Surface;" |
Chong Zhang | 2659c2f | 2017-04-27 13:18:20 -0700 | [diff] [blame] | 2183 | "Landroid/media/MediaCrypto;Landroid/os/IHwBinder;I)V", |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 2184 | (void *)android_media_MediaCodec_native_configure }, |
| 2185 | |
Lajos Molnar | 5e02ba9 | 2015-05-01 15:59:35 -0700 | [diff] [blame] | 2186 | { "native_setSurface", |
| 2187 | "(Landroid/view/Surface;)V", |
| 2188 | (void *)android_media_MediaCodec_native_setSurface }, |
| 2189 | |
Andy McFadden | 2621e40 | 2013-02-19 07:29:21 -0800 | [diff] [blame] | 2190 | { "createInputSurface", "()Landroid/view/Surface;", |
| 2191 | (void *)android_media_MediaCodec_createInputSurface }, |
| 2192 | |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 2193 | { "native_start", "()V", (void *)android_media_MediaCodec_start }, |
Andreas Huber | aba6713 | 2013-10-22 12:40:01 -0700 | [diff] [blame] | 2194 | { "native_stop", "()V", (void *)android_media_MediaCodec_stop }, |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 2195 | { "native_flush", "()V", (void *)android_media_MediaCodec_flush }, |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 2196 | |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 2197 | { "native_queueInputBuffer", "(IIIJI)V", |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 2198 | (void *)android_media_MediaCodec_queueInputBuffer }, |
| 2199 | |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 2200 | { "native_queueSecureInputBuffer", "(IILandroid/media/MediaCodec$CryptoInfo;JI)V", |
Andreas Huber | 9e6bcce | 2012-04-06 12:14:47 -0700 | [diff] [blame] | 2201 | (void *)android_media_MediaCodec_queueSecureInputBuffer }, |
| 2202 | |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 2203 | { "native_dequeueInputBuffer", "(J)I", |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 2204 | (void *)android_media_MediaCodec_dequeueInputBuffer }, |
| 2205 | |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 2206 | { "native_dequeueOutputBuffer", "(Landroid/media/MediaCodec$BufferInfo;J)I", |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 2207 | (void *)android_media_MediaCodec_dequeueOutputBuffer }, |
| 2208 | |
Lajos Molnar | 7c513b6b | 2014-05-08 17:16:45 -0700 | [diff] [blame] | 2209 | { "releaseOutputBuffer", "(IZZJ)V", |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 2210 | (void *)android_media_MediaCodec_releaseOutputBuffer }, |
| 2211 | |
Andy McFadden | 2621e40 | 2013-02-19 07:29:21 -0800 | [diff] [blame] | 2212 | { "signalEndOfInputStream", "()V", |
| 2213 | (void *)android_media_MediaCodec_signalEndOfInputStream }, |
| 2214 | |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 2215 | { "getFormatNative", "(Z)Ljava/util/Map;", |
| 2216 | (void *)android_media_MediaCodec_getFormatNative }, |
| 2217 | |
| 2218 | { "getOutputFormatNative", "(I)Ljava/util/Map;", |
| 2219 | (void *)android_media_MediaCodec_getOutputFormatForIndexNative }, |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 2220 | |
| 2221 | { "getBuffers", "(Z)[Ljava/nio/ByteBuffer;", |
| 2222 | (void *)android_media_MediaCodec_getBuffers }, |
| 2223 | |
Lajos Molnar | d402311 | 2014-07-11 15:12:59 -0700 | [diff] [blame] | 2224 | { "getBuffer", "(ZI)Ljava/nio/ByteBuffer;", |
| 2225 | (void *)android_media_MediaCodec_getBuffer }, |
| 2226 | |
| 2227 | { "getImage", "(ZI)Landroid/media/Image;", |
| 2228 | (void *)android_media_MediaCodec_getImage }, |
| 2229 | |
Lajos Molnar | d2a7f47 | 2018-11-15 12:49:20 -0800 | [diff] [blame] | 2230 | { "getCanonicalName", "()Ljava/lang/String;", |
Martin Storsjo | 056ef2e | 2012-09-25 11:53:04 +0300 | [diff] [blame] | 2231 | (void *)android_media_MediaCodec_getName }, |
| 2232 | |
Chong Zhang | a0b72a6 | 2018-02-28 18:46:26 -0800 | [diff] [blame] | 2233 | { "getOwnCodecInfo", "()Landroid/media/MediaCodecInfo;", |
| 2234 | (void *)android_media_MediaCodec_getOwnCodecInfo }, |
| 2235 | |
Ray Essick | 10353e3 | 2017-04-14 10:22:55 -0700 | [diff] [blame] | 2236 | { "native_getMetrics", "()Landroid/os/PersistableBundle;", |
Ray Essick | f2d0e40 | 2017-03-09 10:17:51 -0800 | [diff] [blame] | 2237 | (void *)android_media_MediaCodec_native_getMetrics}, |
Ray Essick | 0e0fee1 | 2017-01-25 18:01:56 -0800 | [diff] [blame] | 2238 | |
Andreas Huber | 226065b | 2013-08-12 10:14:11 -0700 | [diff] [blame] | 2239 | { "setParameters", "([Ljava/lang/String;[Ljava/lang/Object;)V", |
| 2240 | (void *)android_media_MediaCodec_setParameters }, |
| 2241 | |
Andreas Huber | b12a539 | 2012-04-30 14:18:33 -0700 | [diff] [blame] | 2242 | { "setVideoScalingMode", "(I)V", |
| 2243 | (void *)android_media_MediaCodec_setVideoScalingMode }, |
| 2244 | |
ybai | 5e05320 | 2018-11-01 13:02:15 +0800 | [diff] [blame] | 2245 | { "native_setAudioPresentation", "(II)V", |
| 2246 | (void *)android_media_MediaCodec_setAudioPresentation }, |
| 2247 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 2248 | { "native_init", "()V", (void *)android_media_MediaCodec_native_init }, |
| 2249 | |
| 2250 | { "native_setup", "(Ljava/lang/String;ZZ)V", |
| 2251 | (void *)android_media_MediaCodec_native_setup }, |
| 2252 | |
| 2253 | { "native_finalize", "()V", |
| 2254 | (void *)android_media_MediaCodec_native_finalize }, |
| 2255 | }; |
| 2256 | |
| 2257 | int register_android_media_MediaCodec(JNIEnv *env) { |
| 2258 | return AndroidRuntime::registerNativeMethods(env, |
| 2259 | "android/media/MediaCodec", gMethods, NELEM(gMethods)); |
| 2260 | } |