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