Debian Codegraph @

media/jni/android_media_MediaCodec.cpp

raw symbols SCIP
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 <type_traits>
22
23 #include "android_media_MediaCodec.h"
24
25 #include "android_media_MediaCodecLinearBlock.h"
26 #include "android_media_MediaCrypto.h"
27 #include "android_media_MediaDescrambler.h"
28 #include "android_media_MediaMetricsJNI.h"
29 #include "android_media_Streams.h"
30 #include "android_runtime/AndroidRuntime.h"
31 #include "android_runtime/android_view_Surface.h"
32 #include "android_util_Binder.h"
33 #include "jni.h"
34 #include <nativehelper/JNIHelp.h>
35 #include <nativehelper/ScopedLocalRef.h>
36
37 #include <C2AllocatorGralloc.h>
38 #include <C2BlockInternal.h>
39 #include <C2Buffer.h>
40 #include <C2PlatformSupport.h>
41
42 #include <android/hardware/cas/native/1.0/IDescrambler.h>
43
44 #include <android_runtime/android_hardware_HardwareBuffer.h>
45
46 #include <android-base/stringprintf.h>
47
48 #include <binder/MemoryDealer.h>
49
50 #include <cutils/compiler.h>
51
52 #include <gui/Surface.h>
53
54 #include <hidlmemory/FrameworkUtils.h>
55
56 #include <media/MediaCodecBuffer.h>
57 #include <media/hardware/VideoAPI.h>
58 #include <media/stagefright/CodecBase.h>
59 #include <media/stagefright/MediaCodec.h>
60 #include <media/stagefright/foundation/ABuffer.h>
61 #include <media/stagefright/foundation/ADebug.h>
62 #include <media/stagefright/foundation/ALooper.h>
63 #include <media/stagefright/foundation/AMessage.h>
64 #include <media/stagefright/foundation/AString.h>
65 #include <media/stagefright/MediaErrors.h>
66 #include <media/stagefright/PersistentSurface.h>
67 #include <mediadrm/DrmUtils.h>
68 #include <mediadrm/ICrypto.h>
69
70 #include <private/android/AHardwareBufferHelpers.h>
71
72 #include <system/window.h>
73
74 namespace android {
75
76 // Keep these in sync with their equivalents in MediaCodec.java !!!
77 enum {
78 DEQUEUE_INFO_TRY_AGAIN_LATER = -1,
79 DEQUEUE_INFO_OUTPUT_FORMAT_CHANGED = -2,
80 DEQUEUE_INFO_OUTPUT_BUFFERS_CHANGED = -3,
81 };
82
83 enum {
84 EVENT_CALLBACK = 1,
85 EVENT_SET_CALLBACK = 2,
86 EVENT_FRAME_RENDERED = 3,
87 EVENT_FIRST_TUNNEL_FRAME_READY = 4,
88 };
89
90 // From MediaFormat.java
91 enum {
92 TYPE_NULL = 0,
93 TYPE_INTEGER = 1,
94 TYPE_LONG = 2,
95 TYPE_FLOAT = 3,
96 TYPE_STRING = 4,
97 TYPE_BYTE_BUFFER = 5,
98 };
99
100 static struct CryptoErrorCodes {
101 jint cryptoErrorNoKey;
102 jint cryptoErrorKeyExpired;
103 jint cryptoErrorResourceBusy;
104 jint cryptoErrorInsufficientOutputProtection;
105 jint cryptoErrorSessionNotOpened;
106 jint cryptoErrorInsufficientSecurity;
107 jint cryptoErrorUnsupportedOperation;
108 jint cryptoErrorFrameTooLarge;
109 jint cryptoErrorLostState;
110 } gCryptoErrorCodes;
111
112 static struct CodecActionCodes {
113 jint codecActionTransient;
114 jint codecActionRecoverable;
115 } gCodecActionCodes;
116
117 static struct CodecErrorCodes {
118 jint errorInsufficientResource;
119 jint errorReclaimed;
120 } gCodecErrorCodes;
121
122 static struct {
123 jclass clazz;
124 jfieldID mLock;
125 jfieldID mPersistentObject;
126 jmethodID ctor;
127 jmethodID setNativeObjectLocked;
128 } gPersistentSurfaceClassInfo;
129
130 static struct {
131 jint Unencrypted;
132 jint AesCtr;
133 jint AesCbc;
134 } gCryptoModes;
135
136 static struct {
137 jclass capsClazz;
138 jmethodID capsCtorId;
139 jclass profileLevelClazz;
140 jfieldID profileField;
141 jfieldID levelField;
142 } gCodecInfo;
143
144 static struct {
145 jclass clazz;
146 jobject nativeByteOrder;
147 jmethodID orderId;
148 jmethodID asReadOnlyBufferId;
149 jmethodID positionId;
150 jmethodID limitId;
151 jmethodID getPositionId;
152 jmethodID getLimitId;
153 } gByteBufferInfo;
154
155 static struct {
156 jclass clazz;
157 jmethodID ctorId;
158 jmethodID sizeId;
159 jmethodID getId;
160 jmethodID addId;
161 } gArrayListInfo;
162
163 static struct {
164 jclass clazz;
165 jmethodID ctorId;
166 jmethodID setInternalStateId;
167 jfieldID contextId;
168 jfieldID validId;
169 jfieldID lockId;
170 } gLinearBlockInfo;
171
172 static struct {
173 jclass clazz;
174 jmethodID ctorId;
175 jfieldID nameId;
176 jfieldID typeId;
177 } gDescriptorInfo;
178
179 static struct {
180 jclass clazz;
181 jmethodID ctorId;
182 jmethodID setId;
183 } gBufferInfo;
184
185 struct fields_t {
186 jmethodID postEventFromNativeID;
187 jmethodID lockAndGetContextID;
188 jmethodID setAndUnlockContextID;
189 jfieldID cryptoInfoNumSubSamplesID;
190 jfieldID cryptoInfoNumBytesOfClearDataID;
191 jfieldID cryptoInfoNumBytesOfEncryptedDataID;
192 jfieldID cryptoInfoKeyID;
193 jfieldID cryptoInfoIVID;
194 jfieldID cryptoInfoModeID;
195 jfieldID cryptoInfoPatternID;
196 jfieldID patternEncryptBlocksID;
197 jfieldID patternSkipBlocksID;
198 jfieldID queueRequestIndexID;
199 jfieldID outputFrameLinearBlockID;
200 jfieldID outputFrameHardwareBufferID;
201 jfieldID outputFrameChangedKeysID;
202 jfieldID outputFrameFormatID;
203 };
204
205 static fields_t gFields;
206 static const void *sRefBaseOwner;
207
208
209 ////////////////////////////////////////////////////////////////////////////////
210
211 JMediaCodec::JMediaCodec(
212 JNIEnv *env, jobject thiz,
213 const char *name, bool nameIsType, bool encoder, int pid, int uid)
214 : mClass(NULL),
215 mObject(NULL) {
216 jclass clazz = env->GetObjectClass(thiz);
217 CHECK(clazz != NULL);
218
219 mClass = (jclass)env->NewGlobalRef(clazz);
220 mObject = env->NewWeakGlobalRef(thiz);
221
222 mLooper = new ALooper;
223 mLooper->setName("MediaCodec_looper");
224
225 mLooper->start(
226 false, // runOnCallingThread
227 true, // canCallJava
228 ANDROID_PRIORITY_VIDEO);
229
230 if (nameIsType) {
231 mCodec = MediaCodec::CreateByType(mLooper, name, encoder, &mInitStatus, pid, uid);
232 if (mCodec == nullptr || mCodec->getName(&mNameAtCreation) != OK) {
233 mNameAtCreation = "(null)";
234 }
235 } else {
236 mCodec = MediaCodec::CreateByComponentName(mLooper, name, &mInitStatus, pid, uid);
237 mNameAtCreation = name;
238 }
239 CHECK((mCodec != NULL) != (mInitStatus != OK));
240 }
241
242 status_t JMediaCodec::initCheck() const {
243 return mInitStatus;
244 }
245
246 void JMediaCodec::registerSelf() {
247 mLooper->registerHandler(this);
248 }
249
250 void JMediaCodec::release() {
251 std::call_once(mReleaseFlag, [this] {
252 if (mCodec != NULL) {
253 mCodec->release();
254 mInitStatus = NO_INIT;
255 }
256
257 if (mLooper != NULL) {
258 mLooper->unregisterHandler(id());
259 mLooper->stop();
260 mLooper.clear();
261 }
262 });
263 }
264
265 void JMediaCodec::releaseAsync() {
266 std::call_once(mAsyncReleaseFlag, [this] {
267 if (mCodec != NULL) {
268 sp<AMessage> notify = new AMessage(kWhatAsyncReleaseComplete, this);
269 // Hold strong reference to this until async release is complete
270 notify->setObject("this", this);
271 mCodec->releaseAsync(notify);
272 }
273 mInitStatus = NO_INIT;
274 });
275 }
276
277 JMediaCodec::~JMediaCodec() {
278 if (mLooper != NULL) {
279 /* MediaCodec and looper should have been released explicitly already
280 * in setMediaCodec() (see comments in setMediaCodec()).
281 *
282 * Otherwise JMediaCodec::~JMediaCodec() might be called from within the
283 * message handler, doing release() there risks deadlock as MediaCodec::
284 * release() post synchronous message to the same looper.
285 *
286 * Print a warning and try to proceed with releasing.
287 */
288 ALOGW("try to release MediaCodec from JMediaCodec::~JMediaCodec()...");
289 release();
290 ALOGW("done releasing MediaCodec from JMediaCodec::~JMediaCodec().");
291 }
292
293 JNIEnv *env = AndroidRuntime::getJNIEnv();
294
295 env->DeleteWeakGlobalRef(mObject);
296 mObject = NULL;
297 env->DeleteGlobalRef(mClass);
298 mClass = NULL;
299 }
300
301 status_t JMediaCodec::enableOnFirstTunnelFrameReadyListener(jboolean enable) {
302 if (enable) {
303 if (mOnFirstTunnelFrameReadyNotification == NULL) {
304 mOnFirstTunnelFrameReadyNotification = new AMessage(kWhatFirstTunnelFrameReady, this);
305 }
306 } else {
307 mOnFirstTunnelFrameReadyNotification.clear();
308 }
309
310 return mCodec->setOnFirstTunnelFrameReadyNotification(mOnFirstTunnelFrameReadyNotification);
311 }
312
313 status_t JMediaCodec::enableOnFrameRenderedListener(jboolean enable) {
314 if (enable) {
315 if (mOnFrameRenderedNotification == NULL) {
316 mOnFrameRenderedNotification = new AMessage(kWhatFrameRendered, this);
317 }
318 } else {
319 mOnFrameRenderedNotification.clear();
320 }
321
322 return mCodec->setOnFrameRenderedNotification(mOnFrameRenderedNotification);
323 }
324
325 status_t JMediaCodec::setCallback(jobject cb) {
326 if (cb != NULL) {
327 if (mCallbackNotification == NULL) {
328 mCallbackNotification = new AMessage(kWhatCallbackNotify, this);
329 }
330 } else {
331 mCallbackNotification.clear();
332 }
333
334 return mCodec->setCallback(mCallbackNotification);
335 }
336
337 status_t JMediaCodec::configure(
338 const sp<AMessage> &format,
339 const sp<IGraphicBufferProducer> &bufferProducer,
340 const sp<ICrypto> &crypto,
341 const sp<IDescrambler> &descrambler,
342 int flags) {
343 sp<Surface> client;
344 if (bufferProducer != NULL) {
345 mSurfaceTextureClient =
346 new Surface(bufferProducer, true /* controlledByApp */);
347 } else {
348 mSurfaceTextureClient.clear();
349 }
350
351 constexpr int32_t CONFIGURE_FLAG_ENCODE = 1;
352 AString mime;
353 CHECK(format->findString("mime", &mime));
354 mGraphicOutput = (mime.startsWithIgnoreCase("video/") || mime.startsWithIgnoreCase("image/"))
355 && !(flags & CONFIGURE_FLAG_ENCODE);
356 mHasCryptoOrDescrambler = (crypto != nullptr) || (descrambler != nullptr);
357 mCrypto = crypto;
358
359 return mCodec->configure(
360 format, mSurfaceTextureClient, crypto, descrambler, flags);
361 }
362
363 status_t JMediaCodec::setSurface(
364 const sp<IGraphicBufferProducer> &bufferProducer) {
365 sp<Surface> client;
366 if (bufferProducer != NULL) {
367 client = new Surface(bufferProducer, true /* controlledByApp */);
368 }
369 status_t err = mCodec->setSurface(client);
370 if (err == OK) {
371 mSurfaceTextureClient = client;
372 }
373 return err;
374 }
375
376 status_t JMediaCodec::createInputSurface(
377 sp<IGraphicBufferProducer>* bufferProducer) {
378 return mCodec->createInputSurface(bufferProducer);
379 }
380
381 status_t JMediaCodec::setInputSurface(
382 const sp<PersistentSurface> &surface) {
383 return mCodec->setInputSurface(surface);
384 }
385
386 status_t JMediaCodec::start() {
387 return mCodec->start();
388 }
389
390 status_t JMediaCodec::stop() {
391 mSurfaceTextureClient.clear();
392
393 return mCodec->stop();
394 }
395
396 status_t JMediaCodec::flush() {
397 return mCodec->flush();
398 }
399
400 status_t JMediaCodec::reset() {
401 return mCodec->reset();
402 }
403
404 status_t JMediaCodec::queueInputBuffer(
405 size_t index,
406 size_t offset, size_t size, int64_t timeUs, uint32_t flags,
407 AString *errorDetailMsg) {
408 return mCodec->queueInputBuffer(
409 index, offset, size, timeUs, flags, errorDetailMsg);
410 }
411
412 status_t JMediaCodec::queueSecureInputBuffer(
413 size_t index,
414 size_t offset,
415 const CryptoPlugin::SubSample *subSamples,
416 size_t numSubSamples,
417 const uint8_t key[16],
418 const uint8_t iv[16],
419 CryptoPlugin::Mode mode,
420 const CryptoPlugin::Pattern &pattern,
421 int64_t presentationTimeUs,
422 uint32_t flags,
423 AString *errorDetailMsg) {
424 return mCodec->queueSecureInputBuffer(
425 index, offset, subSamples, numSubSamples, key, iv, mode, pattern,
426 presentationTimeUs, flags, errorDetailMsg);
427 }
428
429 status_t JMediaCodec::queueBuffer(
430 size_t index, const std::shared_ptr<C2Buffer> &buffer, int64_t timeUs,
431 uint32_t flags, const sp<AMessage> &tunings, AString *errorDetailMsg) {
432 return mCodec->queueBuffer(
433 index, buffer, timeUs, flags, tunings, errorDetailMsg);
434 }
435
436 status_t JMediaCodec::queueEncryptedLinearBlock(
437 size_t index,
438 const sp<hardware::HidlMemory> &buffer,
439 size_t offset,
440 const CryptoPlugin::SubSample *subSamples,
441 size_t numSubSamples,
442 const uint8_t key[16],
443 const uint8_t iv[16],
444 CryptoPlugin::Mode mode,
445 const CryptoPlugin::Pattern &pattern,
446 int64_t presentationTimeUs,
447 uint32_t flags,
448 const sp<AMessage> &tunings,
449 AString *errorDetailMsg) {
450 return mCodec->queueEncryptedBuffer(
451 index, buffer, offset, subSamples, numSubSamples, key, iv, mode, pattern,
452 presentationTimeUs, flags, tunings, errorDetailMsg);
453 }
454
455 status_t JMediaCodec::dequeueInputBuffer(size_t *index, int64_t timeoutUs) {
456 return mCodec->dequeueInputBuffer(index, timeoutUs);
457 }
458
459 status_t JMediaCodec::dequeueOutputBuffer(
460 JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs) {
461 size_t size, offset;
462 int64_t timeUs;
463 uint32_t flags;
464 status_t err = mCodec->dequeueOutputBuffer(
465 index, &offset, &size, &timeUs, &flags, timeoutUs);
466
467 if (err != OK) {
468 return err;
469 }
470
471 env->CallVoidMethod(bufferInfo, gBufferInfo.setId, (jint)offset, (jint)size, timeUs, flags);
472
473 return OK;
474 }
475
476 status_t JMediaCodec::releaseOutputBuffer(
477 size_t index, bool render, bool updatePTS, int64_t timestampNs) {
478 if (updatePTS) {
479 return mCodec->renderOutputBufferAndRelease(index, timestampNs);
480 }
481 return render
482 ? mCodec->renderOutputBufferAndRelease(index)
483 : mCodec->releaseOutputBuffer(index);
484 }
485
486 status_t JMediaCodec::signalEndOfInputStream() {
487 return mCodec->signalEndOfInputStream();
488 }
489
490 status_t JMediaCodec::getFormat(JNIEnv *env, bool input, jobject *format) const {
491 sp<AMessage> msg;
492 status_t err;
493 err = input ? mCodec->getInputFormat(&msg) : mCodec->getOutputFormat(&msg);
494 if (err != OK) {
495 return err;
496 }
497
498 return ConvertMessageToMap(env, msg, format);
499 }
500
501 status_t JMediaCodec::getOutputFormat(JNIEnv *env, size_t index, jobject *format) const {
502 sp<AMessage> msg;
503 status_t err;
504 if ((err = mCodec->getOutputFormat(index, &msg)) != OK) {
505 return err;
506 }
507
508 return ConvertMessageToMap(env, msg, format);
509 }
510
511 status_t JMediaCodec::getBuffers(
512 JNIEnv *env, bool input, jobjectArray *bufArray) const {
513 Vector<sp<MediaCodecBuffer> > buffers;
514
515 status_t err =
516 input
517 ? mCodec->getInputBuffers(&buffers)
518 : mCodec->getOutputBuffers(&buffers);
519
520 if (err != OK) {
521 return err;
522 }
523
524 *bufArray = (jobjectArray)env->NewObjectArray(
525 buffers.size(), gByteBufferInfo.clazz, NULL);
526 if (*bufArray == NULL) {
527 return NO_MEMORY;
528 }
529
530 for (size_t i = 0; i < buffers.size(); ++i) {
531 const sp<MediaCodecBuffer> &buffer = buffers.itemAt(i);
532
533 jobject byteBuffer = NULL;
534 err = createByteBufferFromABuffer(
535 env, !input /* readOnly */, true /* clearBuffer */, buffer, &byteBuffer);
536 if (err != OK) {
537 return err;
538 }
539 if (byteBuffer != NULL) {
540 env->SetObjectArrayElement(
541 *bufArray, i, byteBuffer);
542
543 env->DeleteLocalRef(byteBuffer);
544 byteBuffer = NULL;
545 }
546 }
547
548 return OK;
549 }
550
551 template <typename T>
552 static jobject CreateByteBuffer(
553 JNIEnv *env, T *base, size_t capacity, size_t offset, size_t size,
554 bool readOnly, bool clearBuffer) {
555 jobject byteBuffer =
556 env->NewDirectByteBuffer(
557 const_cast<typename std::remove_const<T>::type *>(base),
558 capacity);
559 if (readOnly && byteBuffer != NULL) {
560 jobject readOnlyBuffer = env->CallObjectMethod(
561 byteBuffer, gByteBufferInfo.asReadOnlyBufferId);
562 env->DeleteLocalRef(byteBuffer);
563 byteBuffer = readOnlyBuffer;
564 }
565 if (byteBuffer == NULL) {
566 return nullptr;
567 }
568 jobject me = env->CallObjectMethod(
569 byteBuffer, gByteBufferInfo.orderId, gByteBufferInfo.nativeByteOrder);
570 env->DeleteLocalRef(me);
571 me = env->CallObjectMethod(
572 byteBuffer, gByteBufferInfo.limitId,
573 clearBuffer ? capacity : offset + size);
574 env->DeleteLocalRef(me);
575 me = env->CallObjectMethod(
576 byteBuffer, gByteBufferInfo.positionId,
577 clearBuffer ? 0 : offset);
578 env->DeleteLocalRef(me);
579 me = NULL;
580 return byteBuffer;
581 }
582
583
584 // static
585 template <typename T>
586 status_t JMediaCodec::createByteBufferFromABuffer(
587 JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer,
588 jobject *buf) const {
589 // if this is an ABuffer that doesn't actually hold any accessible memory,
590 // use a null ByteBuffer
591 *buf = NULL;
592
593 if (buffer == NULL) {
594 ALOGV("createByteBufferFromABuffer - given NULL, returning NULL");
595 return OK;
596 }
597
598 if (buffer->base() == NULL) {
599 return OK;
600 }
601
602 jobject byteBuffer = CreateByteBuffer(
603 env, buffer->base(), buffer->capacity(), buffer->offset(), buffer->size(),
604 readOnly, clearBuffer);
605
606 *buf = byteBuffer;
607 return OK;
608 }
609
610 status_t JMediaCodec::getBuffer(
611 JNIEnv *env, bool input, size_t index, jobject *buf) const {
612 sp<MediaCodecBuffer> buffer;
613
614 status_t err =
615 input
616 ? mCodec->getInputBuffer(index, &buffer)
617 : mCodec->getOutputBuffer(index, &buffer);
618
619 if (err != OK) {
620 return err;
621 }
622
623 return createByteBufferFromABuffer(
624 env, !input /* readOnly */, input /* clearBuffer */, buffer, buf);
625 }
626
627 status_t JMediaCodec::getImage(
628 JNIEnv *env, bool input, size_t index, jobject *buf) const {
629 sp<MediaCodecBuffer> buffer;
630
631 status_t err =
632 input
633 ? mCodec->getInputBuffer(index, &buffer)
634 : mCodec->getOutputBuffer(index, &buffer);
635
636 if (err != OK) {
637 return err;
638 }
639
640 // if this is an ABuffer that doesn't actually hold any accessible memory,
641 // use a null ByteBuffer
642 *buf = NULL;
643 if (buffer->base() == NULL) {
644 return OK;
645 }
646
647 // check if buffer is an image
648 sp<ABuffer> imageData;
649 if (!buffer->meta()->findBuffer("image-data", &imageData)) {
650 return OK;
651 }
652
653 int64_t timestamp = 0;
654 if (!input && buffer->meta()->findInt64("timeUs", &timestamp)) {
655 timestamp *= 1000; // adjust to ns
656 }
657
658 jobject byteBuffer = NULL;
659 err = createByteBufferFromABuffer(
660 env, !input /* readOnly */, input /* clearBuffer */, buffer, &byteBuffer);
661 if (err != OK) {
662 return OK;
663 }
664
665 jobject infoBuffer = NULL;
666 err = createByteBufferFromABuffer(
667 env, true /* readOnly */, true /* clearBuffer */, imageData, &infoBuffer);
668 if (err != OK) {
669 env->DeleteLocalRef(byteBuffer);
670 byteBuffer = NULL;
671 return OK;
672 }
673
674 jobject cropRect = NULL;
675 int32_t left, top, right, bottom;
676 if (buffer->meta()->findRect("crop-rect", &left, &top, &right, &bottom)) {
677 ScopedLocalRef<jclass> rectClazz(
678 env, env->FindClass("android/graphics/Rect"));
679 CHECK(rectClazz.get() != NULL);
680
681 jmethodID rectConstructID = env->GetMethodID(
682 rectClazz.get(), "<init>", "(IIII)V");
683
684 cropRect = env->NewObject(
685 rectClazz.get(), rectConstructID, left, top, right + 1, bottom + 1);
686 }
687
688 ScopedLocalRef<jclass> imageClazz(
689 env, env->FindClass("android/media/MediaCodec$MediaImage"));
690 CHECK(imageClazz.get() != NULL);
691
692 jmethodID imageConstructID = env->GetMethodID(imageClazz.get(), "<init>",
693 "(Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;ZJIILandroid/graphics/Rect;)V");
694
695 *buf = env->NewObject(imageClazz.get(), imageConstructID,
696 byteBuffer, infoBuffer,
697 (jboolean)!input /* readOnly */,
698 (jlong)timestamp,
699 (jint)0 /* xOffset */, (jint)0 /* yOffset */, cropRect);
700
701 // if MediaImage creation fails, return null
702 if (env->ExceptionCheck()) {
703 env->ExceptionDescribe();
704 env->ExceptionClear();
705 *buf = NULL;
706 }
707
708 if (cropRect != NULL) {
709 env->DeleteLocalRef(cropRect);
710 cropRect = NULL;
711 }
712
713 env->DeleteLocalRef(byteBuffer);
714 byteBuffer = NULL;
715
716 env->DeleteLocalRef(infoBuffer);
717 infoBuffer = NULL;
718
719 return OK;
720 }
721
722 status_t JMediaCodec::getOutputFrame(
723 JNIEnv *env, jobject frame, size_t index) const {
724 sp<MediaCodecBuffer> buffer;
725
726 status_t err = mCodec->getOutputBuffer(index, &buffer);
727 if (err != OK) {
728 return err;
729 }
730
731 if (buffer->size() > 0) {
732 std::shared_ptr<C2Buffer> c2Buffer = buffer->asC2Buffer();
733 if (c2Buffer) {
734 switch (c2Buffer->data().type()) {
735 case C2BufferData::LINEAR: {
736 std::unique_ptr<JMediaCodecLinearBlock> context{new JMediaCodecLinearBlock};
737 context->mCodecNames.push_back(mNameAtCreation.c_str());
738 context->mBuffer = c2Buffer;
739 ScopedLocalRef<jobject> linearBlock{env, env->NewObject(
740 gLinearBlockInfo.clazz, gLinearBlockInfo.ctorId)};
741 env->CallVoidMethod(
742 linearBlock.get(),
743 gLinearBlockInfo.setInternalStateId,
744 (jlong)context.release(),
745 true);
746 env->SetObjectField(frame, gFields.outputFrameLinearBlockID, linearBlock.get());
747 break;
748 }
749 case C2BufferData::GRAPHIC: {
750 const C2Handle *c2Handle = c2Buffer->data().graphicBlocks().front().handle();
751 uint32_t width, height, format, stride, igbp_slot, generation;
752 uint64_t usage, igbp_id;
753 _UnwrapNativeCodec2GrallocMetadata(
754 c2Handle, &width, &height, &format, &usage, &stride, &generation,
755 &igbp_id, &igbp_slot);
756 native_handle_t *grallocHandle = UnwrapNativeCodec2GrallocHandle(c2Handle);
757 GraphicBuffer* graphicBuffer = new GraphicBuffer(
758 grallocHandle, GraphicBuffer::CLONE_HANDLE,
759 width, height, format, 1, usage, stride);
760 ScopedLocalRef<jobject> hardwareBuffer{
761 env,
762 android_hardware_HardwareBuffer_createFromAHardwareBuffer(
763 env, AHardwareBuffer_from_GraphicBuffer(graphicBuffer))};
764 env->SetObjectField(
765 frame, gFields.outputFrameHardwareBufferID, hardwareBuffer.get());
766 break;
767 }
768 case C2BufferData::LINEAR_CHUNKS: [[fallthrough]];
769 case C2BufferData::GRAPHIC_CHUNKS: [[fallthrough]];
770 case C2BufferData::INVALID: [[fallthrough]];
771 default:
772 return INVALID_OPERATION;
773 }
774 } else {
775 if (!mGraphicOutput) {
776 std::unique_ptr<JMediaCodecLinearBlock> context{new JMediaCodecLinearBlock};
777 context->mCodecNames.push_back(mNameAtCreation.c_str());
778 context->mLegacyBuffer = buffer;
779 ScopedLocalRef<jobject> linearBlock{env, env->NewObject(
780 gLinearBlockInfo.clazz, gLinearBlockInfo.ctorId)};
781 env->CallVoidMethod(
782 linearBlock.get(),
783 gLinearBlockInfo.setInternalStateId,
784 (jlong)context.release(),
785 true);
786 env->SetObjectField(frame, gFields.outputFrameLinearBlockID, linearBlock.get());
787 } else {
788 // No-op.
789 }
790 }
791 }
792
793 jobject formatMap;
794 err = getOutputFormat(env, index, &formatMap);
795 if (err != OK) {
796 return err;
797 }
798 ScopedLocalRef<jclass> mediaFormatClass{env, env->FindClass("android/media/MediaFormat")};
799 ScopedLocalRef<jobject> format{env, env->NewObject(
800 mediaFormatClass.get(),
801 env->GetMethodID(mediaFormatClass.get(), "<init>", "(Ljava/util/Map;)V"),
802 formatMap)};
803 env->SetObjectField(frame, gFields.outputFrameFormatID, format.get());
804 env->DeleteLocalRef(formatMap);
805 formatMap = nullptr;
806
807 sp<RefBase> obj;
808 if (buffer->meta()->findObject("changedKeys", &obj) && obj) {
809 sp<MediaCodec::WrapperObject<std::set<std::string>>> changedKeys{
810 (decltype(changedKeys.get()))obj.get()};
811 ScopedLocalRef<jobject> changedKeysObj{env, env->GetObjectField(
812 frame, gFields.outputFrameChangedKeysID)};
813 for (const std::string &key : changedKeys->value) {
814 ScopedLocalRef<jstring> keyStr{env, env->NewStringUTF(key.c_str())};
815 (void)env->CallBooleanMethod(changedKeysObj.get(), gArrayListInfo.addId, keyStr.get());
816 }
817 }
818 return OK;
819 }
820
821 status_t JMediaCodec::getName(JNIEnv *env, jstring *nameStr) const {
822 AString name;
823
824 status_t err = mCodec->getName(&name);
825
826 if (err != OK) {
827 return err;
828 }
829
830 *nameStr = env->NewStringUTF(name.c_str());
831
832 return OK;
833 }
834
835 static jobject getCodecCapabilitiesObject(
836 JNIEnv *env, const char *mime, bool isEncoder,
837 const sp<MediaCodecInfo::Capabilities> &capabilities) {
838 Vector<MediaCodecInfo::ProfileLevel> profileLevels;
839 Vector<uint32_t> colorFormats;
840
841 sp<AMessage> defaultFormat = new AMessage();
842 defaultFormat->setString("mime", mime);
843
844 capabilities->getSupportedColorFormats(&colorFormats);
845 capabilities->getSupportedProfileLevels(&profileLevels);
846 sp<AMessage> details = capabilities->getDetails();
847
848 jobject defaultFormatObj = NULL;
849 if (ConvertMessageToMap(env, defaultFormat, &defaultFormatObj)) {
850 return NULL;
851 }
852 ScopedLocalRef<jobject> defaultFormatRef(env, defaultFormatObj);
853
854 jobject detailsObj = NULL;
855 if (ConvertMessageToMap(env, details, &detailsObj)) {
856 return NULL;
857 }
858 ScopedLocalRef<jobject> detailsRef(env, detailsObj);
859
860 ScopedLocalRef<jobjectArray> profileLevelArray(env, env->NewObjectArray(
861 profileLevels.size(), gCodecInfo.profileLevelClazz, NULL));
862
863 for (size_t i = 0; i < profileLevels.size(); ++i) {
864 const MediaCodecInfo::ProfileLevel &src = profileLevels.itemAt(i);
865
866 ScopedLocalRef<jobject> srcRef(env, env->AllocObject(
867 gCodecInfo.profileLevelClazz));
868
869 env->SetIntField(srcRef.get(), gCodecInfo.profileField, src.mProfile);
870 env->SetIntField(srcRef.get(), gCodecInfo.levelField, src.mLevel);
871
872 env->SetObjectArrayElement(profileLevelArray.get(), i, srcRef.get());
873 }
874
875 ScopedLocalRef<jintArray> colorFormatsArray(
876 env, env->NewIntArray(colorFormats.size()));
877 for (size_t i = 0; i < colorFormats.size(); ++i) {
878 jint val = colorFormats.itemAt(i);
879 env->SetIntArrayRegion(colorFormatsArray.get(), i, 1, &val);
880 }
881
882 return env->NewObject(
883 gCodecInfo.capsClazz, gCodecInfo.capsCtorId,
884 profileLevelArray.get(), colorFormatsArray.get(), isEncoder,
885 defaultFormatRef.get(), detailsRef.get());
886 }
887
888 status_t JMediaCodec::getCodecInfo(JNIEnv *env, jobject *codecInfoObject) const {
889 sp<MediaCodecInfo> codecInfo;
890
891 status_t err = mCodec->getCodecInfo(&codecInfo);
892
893 if (err != OK) {
894 return err;
895 }
896
897 ScopedLocalRef<jstring> nameObject(env,
898 env->NewStringUTF(mNameAtCreation.c_str()));
899
900 ScopedLocalRef<jstring> canonicalNameObject(env,
901 env->NewStringUTF(codecInfo->getCodecName()));
902
903 MediaCodecInfo::Attributes attributes = codecInfo->getAttributes();
904 bool isEncoder = codecInfo->isEncoder();
905
906 Vector<AString> mediaTypes;
907 codecInfo->getSupportedMediaTypes(&mediaTypes);
908
909 ScopedLocalRef<jobjectArray> capsArrayObj(env,
910 env->NewObjectArray(mediaTypes.size(), gCodecInfo.capsClazz, NULL));
911
912 for (size_t i = 0; i < mediaTypes.size(); i++) {
913 const sp<MediaCodecInfo::Capabilities> caps =
914 codecInfo->getCapabilitiesFor(mediaTypes[i].c_str());
915
916 ScopedLocalRef<jobject> capsObj(env, getCodecCapabilitiesObject(
917 env, mediaTypes[i].c_str(), isEncoder, caps));
918
919 env->SetObjectArrayElement(capsArrayObj.get(), i, capsObj.get());
920 }
921
922 ScopedLocalRef<jclass> codecInfoClazz(env,
923 env->FindClass("android/media/MediaCodecInfo"));
924 CHECK(codecInfoClazz.get() != NULL);
925
926 jmethodID codecInfoCtorID = env->GetMethodID(codecInfoClazz.get(), "<init>",
927 "(Ljava/lang/String;Ljava/lang/String;I[Landroid/media/MediaCodecInfo$CodecCapabilities;)V");
928
929 *codecInfoObject = env->NewObject(codecInfoClazz.get(), codecInfoCtorID,
930 nameObject.get(), canonicalNameObject.get(), attributes, capsArrayObj.get());
931
932 return OK;
933 }
934
935 status_t JMediaCodec::getMetrics(JNIEnv *, mediametrics::Item * &reply) const {
936 mediametrics_handle_t reply2 = mediametrics::Item::convert(reply);
937 status_t status = mCodec->getMetrics(reply2);
938 // getMetrics() updates reply2, pass the converted update along to our caller.
939 reply = mediametrics::Item::convert(reply2);
940 return status;
941 }
942
943 status_t JMediaCodec::setParameters(const sp<AMessage> &msg) {
944 return mCodec->setParameters(msg);
945 }
946
947 void JMediaCodec::setVideoScalingMode(int mode) {
948 if (mSurfaceTextureClient != NULL) {
949 // this works for components that queue to surface
950 native_window_set_scaling_mode(mSurfaceTextureClient.get(), mode);
951 // also signal via param for components that queue to IGBP
952 sp<AMessage> msg = new AMessage;
953 msg->setInt32("android._video-scaling", mode);
954 (void)mCodec->setParameters(msg);
955 }
956 }
957
958 void JMediaCodec::selectAudioPresentation(const int32_t presentationId, const int32_t programId) {
959 sp<AMessage> msg = new AMessage;
960 msg->setInt32("audio-presentation-presentation-id", presentationId);
961 msg->setInt32("audio-presentation-program-id", programId);
962 (void)mCodec->setParameters(msg);
963 }
964
965 status_t JMediaCodec::querySupportedVendorParameters(JNIEnv *env, jobject *namesObj) {
966 std::vector<std::string> names;
967 status_t status = mCodec->querySupportedVendorParameters(&names);
968 if (status != OK) {
969 return status;
970 }
971 *namesObj = env->NewObject(gArrayListInfo.clazz, gArrayListInfo.ctorId);
972 for (const std::string &name : names) {
973 ScopedLocalRef<jstring> nameStr{env, env->NewStringUTF(name.c_str())};
974 (void)env->CallBooleanMethod(*namesObj, gArrayListInfo.addId, nameStr.get());
975 }
976 return OK;
977 }
978
979 status_t JMediaCodec::describeParameter(JNIEnv *env, jstring name, jobject *descObj) {
980 const char *tmp = env->GetStringUTFChars(name, nullptr);
981 CodecParameterDescriptor desc;
982 status_t status = mCodec->describeParameter(tmp, &desc);
983 env->ReleaseStringUTFChars(name, tmp);
984 if (status != OK) {
985 return status;
986 }
987 jint type = TYPE_NULL;
988 switch (desc.type) {
989 case AMessage::kTypeInt32: type = TYPE_INTEGER; break;
990 case AMessage::kTypeSize:
991 case AMessage::kTypeInt64: type = TYPE_LONG; break;
992 case AMessage::kTypeFloat: type = TYPE_FLOAT; break;
993 case AMessage::kTypeString: type = TYPE_STRING; break;
994 case AMessage::kTypeBuffer: type = TYPE_BYTE_BUFFER; break;
995 default: type = TYPE_NULL; break;
996 }
997 if (type == TYPE_NULL) {
998 return BAD_VALUE;
999 }
1000 *descObj = env->NewObject(gDescriptorInfo.clazz, gDescriptorInfo.ctorId);
1001 env->SetObjectField(*descObj, gDescriptorInfo.nameId, name);
1002 env->SetIntField(*descObj, gDescriptorInfo.typeId, type);
1003 return OK;
1004 }
1005
1006 static void BuildVectorFromList(JNIEnv *env, jobject list, std::vector<std::string> *vec) {
1007 ScopedLocalRef<jclass> listClazz{env, env->FindClass("java/util/List")};
1008 ScopedLocalRef<jclass> iterClazz{env, env->FindClass("java/util/Iterator")};
1009 jmethodID hasNextID = env->GetMethodID(iterClazz.get(), "hasNext", "()Z");
1010 jmethodID nextID = env->GetMethodID(iterClazz.get(), "next", "()Ljava/lang/Object;");
1011 jobject it = env->CallObjectMethod(
1012 list, env->GetMethodID(listClazz.get(), "iterator", "()Ljava/util/Iterator;"));
1013 while (env->CallBooleanMethod(it, hasNextID)) {
1014 jstring name = (jstring)env->CallObjectMethod(it, nextID);
1015 const char *tmp = env->GetStringUTFChars(name, nullptr);
1016 vec->push_back(tmp);
1017 env->ReleaseStringUTFChars(name, tmp);
1018 }
1019 }
1020
1021 status_t JMediaCodec::subscribeToVendorParameters(JNIEnv *env, jobject namesObj) {
1022 std::vector<std::string> names;
1023 BuildVectorFromList(env, namesObj, &names);
1024 return mCodec->subscribeToVendorParameters(names);
1025 }
1026
1027 status_t JMediaCodec::unsubscribeFromVendorParameters(JNIEnv *env, jobject namesObj) {
1028 std::vector<std::string> names;
1029 BuildVectorFromList(env, namesObj, &names);
1030 return mCodec->unsubscribeFromVendorParameters(names);
1031 }
1032
1033 static jthrowable createCodecException(
1034 JNIEnv *env, status_t err, int32_t actionCode, const char *msg = NULL) {
1035 ScopedLocalRef<jclass> clazz(
1036 env, env->FindClass("android/media/MediaCodec$CodecException"));
1037 CHECK(clazz.get() != NULL);
1038
1039 const jmethodID ctor = env->GetMethodID(clazz.get(), "<init>", "(IILjava/lang/String;)V");
1040 CHECK(ctor != NULL);
1041
1042 ScopedLocalRef<jstring> msgObj(
1043 env, env->NewStringUTF(msg != NULL ? msg : String8::format("Error %#x", err)));
1044
1045 // translate action code to Java equivalent
1046 switch (actionCode) {
1047 case ACTION_CODE_TRANSIENT:
1048 actionCode = gCodecActionCodes.codecActionTransient;
1049 break;
1050 case ACTION_CODE_RECOVERABLE:
1051 actionCode = gCodecActionCodes.codecActionRecoverable;
1052 break;
1053 default:
1054 actionCode = 0; // everything else is fatal
1055 break;
1056 }
1057
1058 /* translate OS errors to Java API CodecException errorCodes */
1059 switch (err) {
1060 case NO_MEMORY:
1061 err = gCodecErrorCodes.errorInsufficientResource;
1062 break;
1063 case DEAD_OBJECT:
1064 err = gCodecErrorCodes.errorReclaimed;
1065 break;
1066 default: /* Other error codes go out as is. */
1067 break;
1068 }
1069
1070 return (jthrowable)env->NewObject(clazz.get(), ctor, err, actionCode, msgObj.get());
1071 }
1072
1073 void JMediaCodec::handleCallback(const sp<AMessage> &msg) {
1074 int32_t arg1, arg2 = 0;
1075 jobject obj = NULL;
1076 CHECK(msg->findInt32("callbackID", &arg1));
1077 JNIEnv *env = AndroidRuntime::getJNIEnv();
1078
1079 switch (arg1) {
1080 case MediaCodec::CB_INPUT_AVAILABLE:
1081 {
1082 CHECK(msg->findInt32("index", &arg2));
1083 break;
1084 }
1085
1086 case MediaCodec::CB_OUTPUT_AVAILABLE:
1087 {
1088 CHECK(msg->findInt32("index", &arg2));
1089
1090 size_t size, offset;
1091 int64_t timeUs;
1092 uint32_t flags;
1093 CHECK(msg->findSize("size", &size));
1094 CHECK(msg->findSize("offset", &offset));
1095 CHECK(msg->findInt64("timeUs", &timeUs));
1096 CHECK(msg->findInt32("flags", (int32_t *)&flags));
1097
1098 obj = env->NewObject(gBufferInfo.clazz, gBufferInfo.ctorId);
1099 if (obj == NULL) {
1100 if (env->ExceptionCheck()) {
1101 ALOGE("Could not create MediaCodec.BufferInfo.");
1102 env->ExceptionClear();
1103 }
1104 jniThrowException(env, "java/lang/IllegalStateException",
1105 "Fatal error: could not create MediaCodec.BufferInfo object");
1106 return;
1107 }
1108
1109 env->CallVoidMethod(obj, gBufferInfo.setId, (jint)offset, (jint)size, timeUs, flags);
1110 break;
1111 }
1112
1113 case MediaCodec::CB_ERROR:
1114 {
1115 int32_t err, actionCode;
1116 CHECK(msg->findInt32("err", &err));
1117 CHECK(msg->findInt32("actionCode", &actionCode));
1118
1119 // note that DRM errors could conceivably alias into a CodecException
1120 obj = (jobject)createCodecException(env, err, actionCode);
1121
1122 if (obj == NULL) {
1123 if (env->ExceptionCheck()) {
1124 ALOGE("Could not create CodecException object.");
1125 env->ExceptionClear();
1126 }
1127 jniThrowException(env, "java/lang/IllegalStateException",
1128 "Fatal error: could not create CodecException object");
1129 return;
1130 }
1131
1132 break;
1133 }
1134
1135 case MediaCodec::CB_OUTPUT_FORMAT_CHANGED:
1136 {
1137 sp<AMessage> format;
1138 CHECK(msg->findMessage("format", &format));
1139
1140 if (OK != ConvertMessageToMap(env, format, &obj)) {
1141 jniThrowException(env, "java/lang/IllegalStateException",
1142 "Fatal error: failed to convert format "
1143 "from native to Java object");
1144 return;
1145 }
1146
1147 break;
1148 }
1149
1150 default:
1151 TRESPASS();
1152 }
1153
1154 env->CallVoidMethod(
1155 mObject,
1156 gFields.postEventFromNativeID,
1157 EVENT_CALLBACK,
1158 arg1,
1159 arg2,
1160 obj);
1161
1162 env->DeleteLocalRef(obj);
1163 }
1164
1165 void JMediaCodec::handleFirstTunnelFrameReadyNotification(const sp<AMessage> &msg) {
1166 int32_t arg1 = 0, arg2 = 0;
1167 jobject obj = NULL;
1168 JNIEnv *env = AndroidRuntime::getJNIEnv();
1169
1170 sp<AMessage> data;
1171 CHECK(msg->findMessage("data", &data));
1172
1173 status_t err = ConvertMessageToMap(env, data, &obj);
1174 if (err != OK) {
1175 jniThrowException(env, "java/lang/IllegalStateException",
1176 "Fatal error: failed to convert format from native to Java object");
1177 return;
1178 }
1179
1180 env->CallVoidMethod(
1181 mObject, gFields.postEventFromNativeID,
1182 EVENT_FIRST_TUNNEL_FRAME_READY, arg1, arg2, obj);
1183
1184 env->DeleteLocalRef(obj);
1185 }
1186
1187 void JMediaCodec::handleFrameRenderedNotification(const sp<AMessage> &msg) {
1188 int32_t arg1 = 0, arg2 = 0;
1189 jobject obj = NULL;
1190 JNIEnv *env = AndroidRuntime::getJNIEnv();
1191
1192 sp<AMessage> data;
1193 CHECK(msg->findMessage("data", &data));
1194
1195 status_t err = ConvertMessageToMap(env, data, &obj);
1196 if (err != OK) {
1197 jniThrowException(env, "java/lang/IllegalStateException",
1198 "Fatal error: failed to convert format from native to Java object");
1199 return;
1200 }
1201
1202 env->CallVoidMethod(
1203 mObject, gFields.postEventFromNativeID,
1204 EVENT_FRAME_RENDERED, arg1, arg2, obj);
1205
1206 env->DeleteLocalRef(obj);
1207 }
1208
1209 std::string JMediaCodec::getExceptionMessage(const char *msg = nullptr) const {
1210 if (mCodec == nullptr) {
1211 return msg ?: "";
1212 }
1213 std::string prefix = "";
1214 if (msg && msg[0] != '\0') {
1215 prefix.append(msg);
1216 prefix.append("\n");
1217 }
1218 return prefix + mCodec->getErrorLog().extract();
1219 }
1220
1221 void JMediaCodec::onMessageReceived(const sp<AMessage> &msg) {
1222 switch (msg->what()) {
1223 case kWhatCallbackNotify:
1224 {
1225 handleCallback(msg);
1226 break;
1227 }
1228 case kWhatFrameRendered:
1229 {
1230 handleFrameRenderedNotification(msg);
1231 break;
1232 }
1233 case kWhatAsyncReleaseComplete:
1234 {
1235 if (mLooper != NULL) {
1236 mLooper->unregisterHandler(id());
1237 mLooper->stop();
1238 mLooper.clear();
1239 }
1240 break;
1241 }
1242 case kWhatFirstTunnelFrameReady:
1243 {
1244 handleFirstTunnelFrameReadyNotification(msg);
1245 break;
1246 }
1247 default:
1248 TRESPASS();
1249 }
1250 }
1251
1252 jint MediaErrorToJavaError(status_t err);
1253
1254 } // namespace android
1255
1256 ////////////////////////////////////////////////////////////////////////////////
1257
1258 using namespace android;
1259
1260 static sp<JMediaCodec> setMediaCodec(
1261 JNIEnv *env, jobject thiz, const sp<JMediaCodec> &codec, bool release = true) {
1262 sp<JMediaCodec> old = (JMediaCodec *)env->CallLongMethod(thiz, gFields.lockAndGetContextID);
1263 if (codec != NULL) {
1264 codec->incStrong(thiz);
1265 }
1266 if (old != NULL) {
1267 /* release MediaCodec and stop the looper now before decStrong.
1268 * otherwise JMediaCodec::~JMediaCodec() could be called from within
1269 * its message handler, doing release() from there will deadlock
1270 * (as MediaCodec::release() post synchronous message to the same looper)
1271 */
1272 if (release) {
1273 old->release();
1274 }
1275 old->decStrong(thiz);
1276 }
1277 env->CallVoidMethod(thiz, gFields.setAndUnlockContextID, (jlong)codec.get());
1278
1279 return old;
1280 }
1281
1282 static sp<JMediaCodec> getMediaCodec(JNIEnv *env, jobject thiz) {
1283 sp<JMediaCodec> codec = (JMediaCodec *)env->CallLongMethod(thiz, gFields.lockAndGetContextID);
1284 env->CallVoidMethod(thiz, gFields.setAndUnlockContextID, (jlong)codec.get());
1285 return codec;
1286 }
1287
1288 static void android_media_MediaCodec_release(JNIEnv *env, jobject thiz) {
1289 // Clear Java native reference.
1290 sp<JMediaCodec> codec = setMediaCodec(env, thiz, nullptr, false /* release */);
1291 if (codec != NULL) {
1292 codec->releaseAsync();
1293 }
1294 }
1295
1296 static void throwCodecException(JNIEnv *env, status_t err, int32_t actionCode, const char *msg) {
1297 jthrowable exception = createCodecException(env, err, actionCode, msg);
1298 env->Throw(exception);
1299 }
1300
1301 static void throwCryptoException(JNIEnv *env, status_t err, const char *msg,
1302 const sp<ICrypto> &crypto) {
1303 ScopedLocalRef<jclass> clazz(
1304 env, env->FindClass("android/media/MediaCodec$CryptoException"));
1305 CHECK(clazz.get() != NULL);
1306
1307 jmethodID constructID =
1308 env->GetMethodID(clazz.get(), "<init>", "(Ljava/lang/String;IIII)V");
1309 CHECK(constructID != NULL);
1310
1311 std::string defaultMsg = "Unknown Error";
1312
1313 /* translate OS errors to Java API CryptoException errorCodes (which are positive) */
1314 jint jerr = 0;
1315 switch (err) {
1316 case ERROR_DRM_NO_LICENSE:
1317 jerr = gCryptoErrorCodes.cryptoErrorNoKey;
1318 defaultMsg = "Crypto key not available";
1319 break;
1320 case ERROR_DRM_LICENSE_EXPIRED:
1321 jerr = gCryptoErrorCodes.cryptoErrorKeyExpired;
1322 defaultMsg = "License expired";
1323 break;
1324 case ERROR_DRM_RESOURCE_BUSY:
1325 jerr = gCryptoErrorCodes.cryptoErrorResourceBusy;
1326 defaultMsg = "Resource busy or unavailable";
1327 break;
1328 case ERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION:
1329 jerr = gCryptoErrorCodes.cryptoErrorInsufficientOutputProtection;
1330 defaultMsg = "Required output protections are not active";
1331 break;
1332 case ERROR_DRM_SESSION_NOT_OPENED:
1333 jerr = gCryptoErrorCodes.cryptoErrorSessionNotOpened;
1334 defaultMsg = "Attempted to use a closed session";
1335 break;
1336 case ERROR_DRM_INSUFFICIENT_SECURITY:
1337 jerr = gCryptoErrorCodes.cryptoErrorInsufficientSecurity;
1338 defaultMsg = "Required security level is not met";
1339 break;
1340 case ERROR_DRM_CANNOT_HANDLE:
1341 jerr = gCryptoErrorCodes.cryptoErrorUnsupportedOperation;
1342 defaultMsg = "Operation not supported in this configuration";
1343 break;
1344 case ERROR_DRM_FRAME_TOO_LARGE:
1345 jerr = gCryptoErrorCodes.cryptoErrorFrameTooLarge;
1346 defaultMsg = "Decrytped frame exceeds size of output buffer";
1347 break;
1348 case ERROR_DRM_SESSION_LOST_STATE:
1349 jerr = gCryptoErrorCodes.cryptoErrorLostState;
1350 defaultMsg = "Session state was lost, open a new session and retry";
1351 break;
1352 default: /* Other negative DRM error codes go out best-effort. */
1353 jerr = MediaErrorToJavaError(err);
1354 defaultMsg = StrCryptoError(err);
1355 break;
1356 }
1357
1358 std::string originalMsg(msg != NULL ? msg : defaultMsg.c_str());
1359 DrmStatus dStatus(err, originalMsg.c_str());
1360 std::string detailedMsg(DrmUtils::GetExceptionMessage(dStatus, defaultMsg.c_str(), crypto));
1361 jstring msgObj = env->NewStringUTF(detailedMsg.c_str());
1362
1363 jthrowable exception =
1364 (jthrowable)env->NewObject(clazz.get(), constructID, msgObj, jerr,
1365 dStatus.getCdmErr(), dStatus.getOemErr(), dStatus.getContext());
1366
1367 env->Throw(exception);
1368 }
1369
1370 static std::string GetExceptionMessage(const sp<JMediaCodec> &codec, const char *msg) {
1371 if (codec == NULL) {
1372 return msg ?: "codec is released already";
1373 }
1374 return codec->getExceptionMessage(msg);
1375 }
1376
1377 static jint throwExceptionAsNecessary(
1378 JNIEnv *env, status_t err, int32_t actionCode = ACTION_CODE_FATAL,
1379 const char *msg = NULL, const sp<ICrypto>& crypto = NULL,
1380 const sp<JMediaCodec> &codec = NULL) {
1381 switch (err) {
1382 case OK:
1383 return 0;
1384
1385 case -EAGAIN:
1386 return DEQUEUE_INFO_TRY_AGAIN_LATER;
1387
1388 case INFO_FORMAT_CHANGED:
1389 return DEQUEUE_INFO_OUTPUT_FORMAT_CHANGED;
1390
1391 case INFO_OUTPUT_BUFFERS_CHANGED:
1392 return DEQUEUE_INFO_OUTPUT_BUFFERS_CHANGED;
1393
1394 case INVALID_OPERATION:
1395 jniThrowException(
1396 env, "java/lang/IllegalStateException",
1397 GetExceptionMessage(codec, msg).c_str());
1398 return 0;
1399
1400 case BAD_VALUE:
1401 jniThrowException(
1402 env, "java/lang/IllegalArgumentException",
1403 GetExceptionMessage(codec, msg).c_str());
1404 return 0;
1405
1406 default:
1407 if (isCryptoError(err)) {
1408 throwCryptoException(
1409 env, err,
1410 GetExceptionMessage(codec, msg).c_str(),
1411 crypto);
1412 return 0;
1413 }
1414 throwCodecException(
1415 env, err, actionCode,
1416 GetExceptionMessage(codec, msg).c_str());
1417 return 0;
1418 }
1419 }
1420
1421 static jint throwExceptionAsNecessary(
1422 JNIEnv *env, status_t err, const sp<JMediaCodec> &codec,
1423 int32_t actionCode = ACTION_CODE_FATAL) {
1424 return throwExceptionAsNecessary(env, err, actionCode, NULL, NULL, codec);
1425 }
1426
1427 static void android_media_MediaCodec_native_enableOnFirstTunnelFrameReadyListener(
1428 JNIEnv *env,
1429 jobject thiz,
1430 jboolean enabled) {
1431 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
1432
1433 if (codec == NULL || codec->initCheck() != OK) {
1434 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
1435 return;
1436 }
1437
1438 status_t err = codec->enableOnFirstTunnelFrameReadyListener(enabled);
1439
1440 throwExceptionAsNecessary(env, err, codec);
1441 }
1442
1443 static void android_media_MediaCodec_native_enableOnFrameRenderedListener(
1444 JNIEnv *env,
1445 jobject thiz,
1446 jboolean enabled) {
1447 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
1448
1449 if (codec == NULL || codec->initCheck() != OK) {
1450 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
1451 return;
1452 }
1453
1454 status_t err = codec->enableOnFrameRenderedListener(enabled);
1455
1456 throwExceptionAsNecessary(env, err, codec);
1457 }
1458
1459 static void android_media_MediaCodec_native_setCallback(
1460 JNIEnv *env,
1461 jobject thiz,
1462 jobject cb) {
1463 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
1464
1465 if (codec == NULL || codec->initCheck() != OK) {
1466 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
1467 return;
1468 }
1469
1470 status_t err = codec->setCallback(cb);
1471
1472 throwExceptionAsNecessary(env, err, codec);
1473 }
1474
1475 static void android_media_MediaCodec_native_configure(
1476 JNIEnv *env,
1477 jobject thiz,
1478 jobjectArray keys, jobjectArray values,
1479 jobject jsurface,
1480 jobject jcrypto,
1481 jobject descramblerBinderObj,
1482 jint flags) {
1483 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
1484
1485 if (codec == NULL || codec->initCheck() != OK) {
1486 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
1487 return;
1488 }
1489
1490 sp<AMessage> format;
1491 status_t err = ConvertKeyValueArraysToMessage(env, keys, values, &format);
1492
1493 if (err != OK) {
1494 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
1495 return;
1496 }
1497
1498 sp<IGraphicBufferProducer> bufferProducer;
1499 if (jsurface != NULL) {
1500 sp<Surface> surface(android_view_Surface_getSurface(env, jsurface));
1501 if (surface != NULL) {
1502 bufferProducer = surface->getIGraphicBufferProducer();
1503 } else {
1504 jniThrowException(
1505 env,
1506 "java/lang/IllegalArgumentException",
1507 "The surface has been released");
1508 return;
1509 }
1510 }
1511
1512 sp<ICrypto> crypto;
1513 if (jcrypto != NULL) {
1514 crypto = JCrypto::GetCrypto(env, jcrypto);
1515 }
1516
1517 sp<IDescrambler> descrambler;
1518 if (descramblerBinderObj != NULL) {
1519 descrambler = GetDescrambler(env, descramblerBinderObj);
1520 }
1521
1522 err = codec->configure(format, bufferProducer, crypto, descrambler, flags);
1523
1524 throwExceptionAsNecessary(env, err, codec);
1525 }
1526
1527 static void android_media_MediaCodec_native_setSurface(
1528 JNIEnv *env,
1529 jobject thiz,
1530 jobject jsurface) {
1531 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
1532
1533 if (codec == NULL || codec->initCheck() != OK) {
1534 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
1535 return;
1536 }
1537
1538 sp<IGraphicBufferProducer> bufferProducer;
1539 if (jsurface != NULL) {
1540 sp<Surface> surface(android_view_Surface_getSurface(env, jsurface));
1541 if (surface != NULL) {
1542 bufferProducer = surface->getIGraphicBufferProducer();
1543 } else {
1544 jniThrowException(
1545 env,
1546 "java/lang/IllegalArgumentException",
1547 "The surface has been released");
1548 return;
1549 }
1550 }
1551
1552 status_t err = codec->setSurface(bufferProducer);
1553 throwExceptionAsNecessary(env, err, codec);
1554 }
1555
1556 sp<PersistentSurface> android_media_MediaCodec_getPersistentInputSurface(
1557 JNIEnv* env, jobject object) {
1558 sp<PersistentSurface> persistentSurface;
1559
1560 jobject lock = env->GetObjectField(
1561 object, gPersistentSurfaceClassInfo.mLock);
1562 if (env->MonitorEnter(lock) == JNI_OK) {
1563 persistentSurface = reinterpret_cast<PersistentSurface *>(
1564 env->GetLongField(object,
1565 gPersistentSurfaceClassInfo.mPersistentObject));
1566 env->MonitorExit(lock);
1567 }
1568 env->DeleteLocalRef(lock);
1569
1570 return persistentSurface;
1571 }
1572
1573 static jobject android_media_MediaCodec_createPersistentInputSurface(
1574 JNIEnv* env, jclass /* clazz */) {
1575 ALOGV("android_media_MediaCodec_createPersistentInputSurface");
1576 sp<PersistentSurface> persistentSurface =
1577 MediaCodec::CreatePersistentInputSurface();
1578
1579 if (persistentSurface == NULL) {
1580 return NULL;
1581 }
1582
1583 sp<Surface> surface = new Surface(
1584 persistentSurface->getBufferProducer(), true);
1585 if (surface == NULL) {
1586 return NULL;
1587 }
1588
1589 jobject object = env->NewObject(
1590 gPersistentSurfaceClassInfo.clazz,
1591 gPersistentSurfaceClassInfo.ctor);
1592
1593 if (object == NULL) {
1594 if (env->ExceptionCheck()) {
1595 ALOGE("Could not create PersistentSurface.");
1596 env->ExceptionClear();
1597 }
1598 return NULL;
1599 }
1600
1601 jobject lock = env->GetObjectField(
1602 object, gPersistentSurfaceClassInfo.mLock);
1603 if (env->MonitorEnter(lock) == JNI_OK) {
1604 env->CallVoidMethod(
1605 object,
1606 gPersistentSurfaceClassInfo.setNativeObjectLocked,
1607 (jlong)surface.get());
1608 env->SetLongField(
1609 object,
1610 gPersistentSurfaceClassInfo.mPersistentObject,
1611 (jlong)persistentSurface.get());
1612 env->MonitorExit(lock);
1613 } else {
1614 env->DeleteLocalRef(object);
1615 object = NULL;
1616 }
1617 env->DeleteLocalRef(lock);
1618
1619 if (object != NULL) {
1620 surface->incStrong(&sRefBaseOwner);
1621 persistentSurface->incStrong(&sRefBaseOwner);
1622 }
1623
1624 return object;
1625 }
1626
1627 static void android_media_MediaCodec_releasePersistentInputSurface(
1628 JNIEnv* env, jclass /* clazz */, jobject object) {
1629 sp<PersistentSurface> persistentSurface;
1630
1631 jobject lock = env->GetObjectField(
1632 object, gPersistentSurfaceClassInfo.mLock);
1633 if (env->MonitorEnter(lock) == JNI_OK) {
1634 persistentSurface = reinterpret_cast<PersistentSurface *>(
1635 env->GetLongField(
1636 object, gPersistentSurfaceClassInfo.mPersistentObject));
1637 env->SetLongField(
1638 object,
1639 gPersistentSurfaceClassInfo.mPersistentObject,
1640 (jlong)0);
1641 env->MonitorExit(lock);
1642 }
1643 env->DeleteLocalRef(lock);
1644
1645 if (persistentSurface != NULL) {
1646 persistentSurface->decStrong(&sRefBaseOwner);
1647 }
1648 // no need to release surface as it will be released by Surface's jni
1649 }
1650
1651 static void android_media_MediaCodec_setInputSurface(
1652 JNIEnv* env, jobject thiz, jobject object) {
1653 ALOGV("android_media_MediaCodec_setInputSurface");
1654
1655 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
1656 if (codec == NULL || codec->initCheck() != OK) {
1657 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
1658 return;
1659 }
1660
1661 sp<PersistentSurface> persistentSurface =
1662 android_media_MediaCodec_getPersistentInputSurface(env, object);
1663
1664 if (persistentSurface == NULL) {
1665 throwExceptionAsNecessary(
1666 env, BAD_VALUE, ACTION_CODE_FATAL, "input surface not valid");
1667 return;
1668 }
1669 status_t err = codec->setInputSurface(persistentSurface);
1670 if (err != NO_ERROR) {
1671 throwExceptionAsNecessary(env, err, codec);
1672 }
1673 }
1674
1675 static jobject android_media_MediaCodec_createInputSurface(JNIEnv* env,
1676 jobject thiz) {
1677 ALOGV("android_media_MediaCodec_createInputSurface");
1678
1679 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
1680 if (codec == NULL || codec->initCheck() != OK) {
1681 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
1682 return NULL;
1683 }
1684
1685 // Tell the MediaCodec that we want to use a Surface as input.
1686 sp<IGraphicBufferProducer> bufferProducer;
1687 status_t err = codec->createInputSurface(&bufferProducer);
1688 if (err != NO_ERROR) {
1689 throwExceptionAsNecessary(env, err, codec);
1690 return NULL;
1691 }
1692
1693 // Wrap the IGBP in a Java-language Surface.
1694 return android_view_Surface_createFromIGraphicBufferProducer(env,
1695 bufferProducer);
1696 }
1697
1698 static void android_media_MediaCodec_start(JNIEnv *env, jobject thiz) {
1699 ALOGV("android_media_MediaCodec_start");
1700
1701 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
1702
1703 if (codec == NULL || codec->initCheck() != OK) {
1704 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
1705 return;
1706 }
1707
1708 status_t err = codec->start();
1709
1710 throwExceptionAsNecessary(env, err, codec);
1711 }
1712
1713 static void android_media_MediaCodec_stop(JNIEnv *env, jobject thiz) {
1714 ALOGV("android_media_MediaCodec_stop");
1715
1716 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
1717
1718 if (codec == NULL || codec->initCheck() != OK) {
1719 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
1720 return;
1721 }
1722
1723 status_t err = codec->stop();
1724
1725 throwExceptionAsNecessary(env, err, codec);
1726 }
1727
1728 static void android_media_MediaCodec_reset(JNIEnv *env, jobject thiz) {
1729 ALOGV("android_media_MediaCodec_reset");
1730
1731 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
1732
1733 if (codec == NULL || codec->initCheck() != OK) {
1734 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
1735 return;
1736 }
1737
1738 status_t err = codec->reset();
1739 if (err != OK) {
1740 // treat all errors as fatal for now, though resource not available
1741 // errors could be treated as transient.
1742 // we also should avoid sending INVALID_OPERATION here due to
1743 // the transitory nature of reset(), it should not inadvertently
1744 // trigger an IllegalStateException.
1745 err = UNKNOWN_ERROR;
1746 }
1747 throwExceptionAsNecessary(env, err, codec);
1748 }
1749
1750 static void android_media_MediaCodec_flush(JNIEnv *env, jobject thiz) {
1751 ALOGV("android_media_MediaCodec_flush");
1752
1753 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
1754
1755 if (codec == NULL || codec->initCheck() != OK) {
1756 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
1757 return;
1758 }
1759
1760 status_t err = codec->flush();
1761
1762 throwExceptionAsNecessary(env, err, codec);
1763 }
1764
1765 static void android_media_MediaCodec_queueInputBuffer(
1766 JNIEnv *env,
1767 jobject thiz,
1768 jint index,
1769 jint offset,
1770 jint size,
1771 jlong timestampUs,
1772 jint flags) {
1773 ALOGV("android_media_MediaCodec_queueInputBuffer");
1774
1775 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
1776
1777 if (codec == NULL || codec->initCheck() != OK) {
1778 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
1779 return;
1780 }
1781
1782 AString errorDetailMsg;
1783
1784 status_t err = codec->queueInputBuffer(
1785 index, offset, size, timestampUs, flags, &errorDetailMsg);
1786
1787 throwExceptionAsNecessary(
1788 env, err, ACTION_CODE_FATAL,
1789 codec->getExceptionMessage(errorDetailMsg.c_str()).c_str());
1790 }
1791
1792 struct NativeCryptoInfo {
1793 NativeCryptoInfo(JNIEnv *env, jobject cryptoInfoObj)
1794 : mEnv{env},
1795 mIvObj{env, (jbyteArray)env->GetObjectField(cryptoInfoObj, gFields.cryptoInfoIVID)},
1796 mKeyObj{env, (jbyteArray)env->GetObjectField(cryptoInfoObj, gFields.cryptoInfoKeyID)} {
1797 mNumSubSamples = env->GetIntField(cryptoInfoObj, gFields.cryptoInfoNumSubSamplesID);
1798
1799 ScopedLocalRef<jintArray> numBytesOfClearDataObj{env, (jintArray)env->GetObjectField(
1800 cryptoInfoObj, gFields.cryptoInfoNumBytesOfClearDataID)};
1801
1802 ScopedLocalRef<jintArray> numBytesOfEncryptedDataObj{env, (jintArray)env->GetObjectField(
1803 cryptoInfoObj, gFields.cryptoInfoNumBytesOfEncryptedDataID)};
1804
1805 jint jmode = env->GetIntField(cryptoInfoObj, gFields.cryptoInfoModeID);
1806 if (jmode == gCryptoModes.Unencrypted) {
1807 mMode = CryptoPlugin::kMode_Unencrypted;
1808 } else if (jmode == gCryptoModes.AesCtr) {
1809 mMode = CryptoPlugin::kMode_AES_CTR;
1810 } else if (jmode == gCryptoModes.AesCbc) {
1811 mMode = CryptoPlugin::kMode_AES_CBC;
1812 } else {
1813 throwExceptionAsNecessary(
1814 env, INVALID_OPERATION, ACTION_CODE_FATAL,
1815 base::StringPrintf("unrecognized crypto mode: %d", jmode).c_str());
1816 return;
1817 }
1818
1819 ScopedLocalRef<jobject> patternObj{
1820 env, env->GetObjectField(cryptoInfoObj, gFields.cryptoInfoPatternID)};
1821
1822 if (patternObj.get() == nullptr) {
1823 mPattern.mEncryptBlocks = 0;
1824 mPattern.mSkipBlocks = 0;
1825 } else {
1826 mPattern.mEncryptBlocks = env->GetIntField(
1827 patternObj.get(), gFields.patternEncryptBlocksID);
1828 mPattern.mSkipBlocks = env->GetIntField(
1829 patternObj.get(), gFields.patternSkipBlocksID);
1830 }
1831
1832 mErr = OK;
1833 if (mNumSubSamples <= 0) {
1834 mErr = -EINVAL;
1835 } else if (numBytesOfClearDataObj == nullptr
1836 && numBytesOfEncryptedDataObj == nullptr) {
1837 mErr = -EINVAL;
1838 } else if (numBytesOfEncryptedDataObj != nullptr
1839 && env->GetArrayLength(numBytesOfEncryptedDataObj.get()) < mNumSubSamples) {
1840 mErr = -ERANGE;
1841 } else if (numBytesOfClearDataObj != nullptr
1842 && env->GetArrayLength(numBytesOfClearDataObj.get()) < mNumSubSamples) {
1843 mErr = -ERANGE;
1844 // subSamples array may silently overflow if number of samples are too large. Use
1845 // INT32_MAX as maximum allocation size may be less than SIZE_MAX on some platforms
1846 } else if (CC_UNLIKELY(mNumSubSamples >= (signed)(INT32_MAX / sizeof(*mSubSamples))) ) {
1847 mErr = -EINVAL;
1848 } else {
1849 jint *numBytesOfClearData =
1850 (numBytesOfClearDataObj == nullptr)
1851 ? nullptr
1852 : env->GetIntArrayElements(numBytesOfClearDataObj.get(), nullptr);
1853
1854 jint *numBytesOfEncryptedData =
1855 (numBytesOfEncryptedDataObj == nullptr)
1856 ? nullptr
1857 : env->GetIntArrayElements(numBytesOfEncryptedDataObj.get(), nullptr);
1858
1859 mSubSamples = new CryptoPlugin::SubSample[mNumSubSamples];
1860
1861 for (jint i = 0; i < mNumSubSamples; ++i) {
1862 mSubSamples[i].mNumBytesOfClearData =
1863 (numBytesOfClearData == nullptr) ? 0 : numBytesOfClearData[i];
1864
1865 mSubSamples[i].mNumBytesOfEncryptedData =
1866 (numBytesOfEncryptedData == nullptr) ? 0 : numBytesOfEncryptedData[i];
1867 }
1868
1869 if (numBytesOfEncryptedData != nullptr) {
1870 env->ReleaseIntArrayElements(
1871 numBytesOfEncryptedDataObj.get(), numBytesOfEncryptedData, 0);
1872 numBytesOfEncryptedData = nullptr;
1873 }
1874
1875 if (numBytesOfClearData != nullptr) {
1876 env->ReleaseIntArrayElements(
1877 numBytesOfClearDataObj.get(), numBytesOfClearData, 0);
1878 numBytesOfClearData = nullptr;
1879 }
1880 }
1881
1882 if (mErr == OK && mKeyObj.get() != nullptr) {
1883 if (env->GetArrayLength(mKeyObj.get()) != 16) {
1884 mErr = -EINVAL;
1885 } else {
1886 mKey = env->GetByteArrayElements(mKeyObj.get(), nullptr);
1887 }
1888 }
1889
1890 if (mErr == OK && mIvObj.get() != nullptr) {
1891 if (env->GetArrayLength(mIvObj.get()) != 16) {
1892 mErr = -EINVAL;
1893 } else {
1894 mIv = env->GetByteArrayElements(mIvObj.get(), nullptr);
1895 }
1896 }
1897
1898 }
1899
1900 explicit NativeCryptoInfo(jint size)
1901 : mIvObj{nullptr, nullptr},
1902 mKeyObj{nullptr, nullptr},
1903 mMode{CryptoPlugin::kMode_Unencrypted},
1904 mPattern{0, 0} {
1905 mSubSamples = new CryptoPlugin::SubSample[1];
1906 mNumSubSamples = 1;
1907 mSubSamples[0].mNumBytesOfClearData = size;
1908 mSubSamples[0].mNumBytesOfEncryptedData = 0;
1909 }
1910
1911 ~NativeCryptoInfo() {
1912 if (mIv != nullptr) {
1913 mEnv->ReleaseByteArrayElements(mIvObj.get(), mIv, 0);
1914 }
1915
1916 if (mKey != nullptr) {
1917 mEnv->ReleaseByteArrayElements(mKeyObj.get(), mKey, 0);
1918 }
1919
1920 if (mSubSamples != nullptr) {
1921 delete[] mSubSamples;
1922 }
1923 }
1924
1925 JNIEnv *mEnv{nullptr};
1926 ScopedLocalRef<jbyteArray> mIvObj;
1927 ScopedLocalRef<jbyteArray> mKeyObj;
1928 status_t mErr{OK};
1929
1930 CryptoPlugin::SubSample *mSubSamples{nullptr};
1931 int32_t mNumSubSamples{0};
1932 jbyte *mIv{nullptr};
1933 jbyte *mKey{nullptr};
1934 enum CryptoPlugin::Mode mMode;
1935 CryptoPlugin::Pattern mPattern;
1936 };
1937
1938 static void android_media_MediaCodec_queueSecureInputBuffer(
1939 JNIEnv *env,
1940 jobject thiz,
1941 jint index,
1942 jint offset,
1943 jobject cryptoInfoObj,
1944 jlong timestampUs,
1945 jint flags) {
1946 ALOGV("android_media_MediaCodec_queueSecureInputBuffer");
1947
1948 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
1949
1950 if (codec == NULL || codec->initCheck() != OK) {
1951 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
1952 return;
1953 }
1954
1955 jint numSubSamples =
1956 env->GetIntField(cryptoInfoObj, gFields.cryptoInfoNumSubSamplesID);
1957
1958 jintArray numBytesOfClearDataObj =
1959 (jintArray)env->GetObjectField(
1960 cryptoInfoObj, gFields.cryptoInfoNumBytesOfClearDataID);
1961
1962 jintArray numBytesOfEncryptedDataObj =
1963 (jintArray)env->GetObjectField(
1964 cryptoInfoObj, gFields.cryptoInfoNumBytesOfEncryptedDataID);
1965
1966 jbyteArray keyObj =
1967 (jbyteArray)env->GetObjectField(cryptoInfoObj, gFields.cryptoInfoKeyID);
1968
1969 jbyteArray ivObj =
1970 (jbyteArray)env->GetObjectField(cryptoInfoObj, gFields.cryptoInfoIVID);
1971
1972 jint jmode = env->GetIntField(cryptoInfoObj, gFields.cryptoInfoModeID);
1973 enum CryptoPlugin::Mode mode;
1974 if (jmode == gCryptoModes.Unencrypted) {
1975 mode = CryptoPlugin::kMode_Unencrypted;
1976 } else if (jmode == gCryptoModes.AesCtr) {
1977 mode = CryptoPlugin::kMode_AES_CTR;
1978 } else if (jmode == gCryptoModes.AesCbc) {
1979 mode = CryptoPlugin::kMode_AES_CBC;
1980 } else {
1981 throwExceptionAsNecessary(
1982 env, INVALID_OPERATION, ACTION_CODE_FATAL,
1983 base::StringPrintf("Unrecognized crypto mode: %d", jmode).c_str());
1984 return;
1985 }
1986
1987 jobject patternObj = env->GetObjectField(cryptoInfoObj, gFields.cryptoInfoPatternID);
1988
1989 CryptoPlugin::Pattern pattern;
1990 if (patternObj == NULL) {
1991 pattern.mEncryptBlocks = 0;
1992 pattern.mSkipBlocks = 0;
1993 } else {
1994 pattern.mEncryptBlocks = env->GetIntField(patternObj, gFields.patternEncryptBlocksID);
1995 pattern.mSkipBlocks = env->GetIntField(patternObj, gFields.patternSkipBlocksID);
1996 }
1997
1998 status_t err = OK;
1999
2000 CryptoPlugin::SubSample *subSamples = NULL;
2001 jbyte *key = NULL;
2002 jbyte *iv = NULL;
2003
2004 if (numSubSamples <= 0) {
2005 err = -EINVAL;
2006 } else if (numBytesOfClearDataObj == NULL
2007 && numBytesOfEncryptedDataObj == NULL) {
2008 err = -EINVAL;
2009 } else if (numBytesOfEncryptedDataObj != NULL
2010 && env->GetArrayLength(numBytesOfEncryptedDataObj) < numSubSamples) {
2011 err = -ERANGE;
2012 } else if (numBytesOfClearDataObj != NULL
2013 && env->GetArrayLength(numBytesOfClearDataObj) < numSubSamples) {
2014 err = -ERANGE;
2015 // subSamples array may silently overflow if number of samples are too large. Use
2016 // INT32_MAX as maximum allocation size may be less than SIZE_MAX on some platforms
2017 } else if ( CC_UNLIKELY(numSubSamples >= (signed)(INT32_MAX / sizeof(*subSamples))) ) {
2018 err = -EINVAL;
2019 } else {
2020 jboolean isCopy;
2021
2022 jint *numBytesOfClearData =
2023 (numBytesOfClearDataObj == NULL)
2024 ? NULL
2025 : env->GetIntArrayElements(numBytesOfClearDataObj, &isCopy);
2026
2027 jint *numBytesOfEncryptedData =
2028 (numBytesOfEncryptedDataObj == NULL)
2029 ? NULL
2030 : env->GetIntArrayElements(numBytesOfEncryptedDataObj, &isCopy);
2031
2032 subSamples = new CryptoPlugin::SubSample[numSubSamples];
2033
2034 for (jint i = 0; i < numSubSamples; ++i) {
2035 subSamples[i].mNumBytesOfClearData =
2036 (numBytesOfClearData == NULL) ? 0 : numBytesOfClearData[i];
2037
2038 subSamples[i].mNumBytesOfEncryptedData =
2039 (numBytesOfEncryptedData == NULL)
2040 ? 0 : numBytesOfEncryptedData[i];
2041 }
2042
2043 if (numBytesOfEncryptedData != NULL) {
2044 env->ReleaseIntArrayElements(
2045 numBytesOfEncryptedDataObj, numBytesOfEncryptedData, 0);
2046 numBytesOfEncryptedData = NULL;
2047 }
2048
2049 if (numBytesOfClearData != NULL) {
2050 env->ReleaseIntArrayElements(
2051 numBytesOfClearDataObj, numBytesOfClearData, 0);
2052 numBytesOfClearData = NULL;
2053 }
2054 }
2055
2056 if (err == OK && keyObj != NULL) {
2057 if (env->GetArrayLength(keyObj) != 16) {
2058 err = -EINVAL;
2059 } else {
2060 jboolean isCopy;
2061 key = env->GetByteArrayElements(keyObj, &isCopy);
2062 }
2063 }
2064
2065 if (err == OK && ivObj != NULL) {
2066 if (env->GetArrayLength(ivObj) != 16) {
2067 err = -EINVAL;
2068 } else {
2069 jboolean isCopy;
2070 iv = env->GetByteArrayElements(ivObj, &isCopy);
2071 }
2072 }
2073
2074 AString errorDetailMsg;
2075
2076 if (err == OK) {
2077 err = codec->queueSecureInputBuffer(
2078 index, offset,
2079 subSamples, numSubSamples,
2080 (const uint8_t *)key, (const uint8_t *)iv,
2081 mode,
2082 pattern,
2083 timestampUs,
2084 flags,
2085 &errorDetailMsg);
2086 }
2087
2088 if (iv != NULL) {
2089 env->ReleaseByteArrayElements(ivObj, iv, 0);
2090 iv = NULL;
2091 }
2092
2093 if (key != NULL) {
2094 env->ReleaseByteArrayElements(keyObj, key, 0);
2095 key = NULL;
2096 }
2097
2098 delete[] subSamples;
2099 subSamples = NULL;
2100
2101 throwExceptionAsNecessary(
2102 env, err, ACTION_CODE_FATAL,
2103 codec->getExceptionMessage(errorDetailMsg.c_str()).c_str(), codec->getCrypto());
2104 }
2105
2106 static jobject android_media_MediaCodec_mapHardwareBuffer(JNIEnv *env, jclass, jobject bufferObj) {
2107 ALOGV("android_media_MediaCodec_mapHardwareBuffer");
2108 AHardwareBuffer *hardwareBuffer = android_hardware_HardwareBuffer_getNativeHardwareBuffer(
2109 env, bufferObj);
2110 AHardwareBuffer_Desc desc;
2111 AHardwareBuffer_describe(hardwareBuffer, &desc);
2112 if (desc.format != AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420) {
2113 ALOGI("mapHardwareBuffer: unmappable format: %d", desc.format);
2114 return nullptr;
2115 }
2116 if ((desc.usage & AHARDWAREBUFFER_USAGE_CPU_READ_MASK) == 0) {
2117 ALOGI("mapHardwareBuffer: buffer not CPU readable");
2118 return nullptr;
2119 }
2120 bool readOnly = ((desc.usage & AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK) == 0);
2121
2122 uint64_t cpuUsage = 0;
2123 cpuUsage |= (desc.usage & AHARDWAREBUFFER_USAGE_CPU_READ_MASK);
2124 cpuUsage |= (desc.usage & AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK);
2125
2126 AHardwareBuffer_Planes planes;
2127 int err = AHardwareBuffer_lockPlanes(
2128 hardwareBuffer, cpuUsage, -1 /* fence */, nullptr /* rect */, &planes);
2129 if (err != 0) {
2130 ALOGI("mapHardwareBuffer: Failed to lock planes (err=%d)", err);
2131 return nullptr;
2132 }
2133
2134 if (planes.planeCount != 3) {
2135 ALOGI("mapHardwareBuffer: planeCount expected 3, actual %u", planes.planeCount);
2136 return nullptr;
2137 }
2138
2139 ScopedLocalRef<jobjectArray> buffersArray{
2140 env, env->NewObjectArray(3, gByteBufferInfo.clazz, NULL)};
2141 ScopedLocalRef<jintArray> rowStridesArray{env, env->NewIntArray(3)};
2142 ScopedLocalRef<jintArray> pixelStridesArray{env, env->NewIntArray(3)};
2143
2144 jboolean isCopy = JNI_FALSE;
2145 jint *rowStrides = env->GetIntArrayElements(rowStridesArray.get(), &isCopy);
2146 jint *pixelStrides = env->GetIntArrayElements(rowStridesArray.get(), &isCopy);
2147
2148 // For Y plane
2149 int rowSampling = 1;
2150 int colSampling = 1;
2151 // plane indices are Y-U-V.
2152 for (uint32_t i = 0; i < 3; ++i) {
2153 const AHardwareBuffer_Plane &plane = planes.planes[i];
2154 int maxRowOffset = plane.rowStride * (desc.height / rowSampling - 1);
2155 int maxColOffset = plane.pixelStride * (desc.width / colSampling - 1);
2156 int maxOffset = maxRowOffset + maxColOffset;
2157 ScopedLocalRef<jobject> byteBuffer{env, CreateByteBuffer(
2158 env,
2159 plane.data,
2160 maxOffset + 1,
2161 0,
2162 maxOffset + 1,
2163 readOnly,
2164 true)};
2165
2166 env->SetObjectArrayElement(buffersArray.get(), i, byteBuffer.get());
2167 rowStrides[i] = plane.rowStride;
2168 pixelStrides[i] = plane.pixelStride;
2169 // For U-V planes
2170 rowSampling = 2;
2171 colSampling = 2;
2172 }
2173
2174 env->ReleaseIntArrayElements(rowStridesArray.get(), rowStrides, 0);
2175 env->ReleaseIntArrayElements(pixelStridesArray.get(), pixelStrides, 0);
2176 rowStrides = pixelStrides = nullptr;
2177
2178 ScopedLocalRef<jclass> imageClazz(
2179 env, env->FindClass("android/media/MediaCodec$MediaImage"));
2180 CHECK(imageClazz.get() != NULL);
2181
2182 jmethodID imageConstructID = env->GetMethodID(imageClazz.get(), "<init>",
2183 "([Ljava/nio/ByteBuffer;[I[IIIIZJIILandroid/graphics/Rect;J)V");
2184
2185 jobject img = env->NewObject(imageClazz.get(), imageConstructID,
2186 buffersArray.get(),
2187 rowStridesArray.get(),
2188 pixelStridesArray.get(),
2189 desc.width,
2190 desc.height,
2191 desc.format, // ???
2192 (jboolean)readOnly /* readOnly */,
2193 (jlong)0 /* timestamp */,
2194 (jint)0 /* xOffset */, (jint)0 /* yOffset */, nullptr /* cropRect */,
2195 (jlong)hardwareBuffer);
2196
2197 // if MediaImage creation fails, return null
2198 if (env->ExceptionCheck()) {
2199 env->ExceptionDescribe();
2200 env->ExceptionClear();
2201 return nullptr;
2202 }
2203
2204 AHardwareBuffer_acquire(hardwareBuffer);
2205
2206 return img;
2207 }
2208
2209 static void android_media_MediaCodec_closeMediaImage(JNIEnv *, jclass, jlong context) {
2210 ALOGV("android_media_MediaCodec_closeMediaImage");
2211 if (context == 0) {
2212 return;
2213 }
2214 AHardwareBuffer *hardwareBuffer = (AHardwareBuffer *)context;
2215
2216 int err = AHardwareBuffer_unlock(hardwareBuffer, nullptr);
2217 if (err != 0) {
2218 ALOGI("closeMediaImage: failed to unlock (err=%d)", err);
2219 // Continue to release the hardwareBuffer
2220 }
2221
2222 AHardwareBuffer_release(hardwareBuffer);
2223 }
2224
2225 static status_t ConvertKeyValueListsToAMessage(
2226 JNIEnv *env, jobject keys, jobject values, sp<AMessage> *msg) {
2227 static struct Fields {
2228 explicit Fields(JNIEnv *env) {
2229 ScopedLocalRef<jclass> clazz{env, env->FindClass("java/lang/String")};
2230 CHECK(clazz.get() != NULL);
2231 mStringClass = (jclass)env->NewGlobalRef(clazz.get());
2232
2233 clazz.reset(env->FindClass("java/lang/Integer"));
2234 CHECK(clazz.get() != NULL);
2235 mIntegerClass = (jclass)env->NewGlobalRef(clazz.get());
2236
2237 mIntegerValueId = env->GetMethodID(clazz.get(), "intValue", "()I");
2238 CHECK(mIntegerValueId != NULL);
2239
2240 clazz.reset(env->FindClass("java/lang/Long"));
2241 CHECK(clazz.get() != NULL);
2242 mLongClass = (jclass)env->NewGlobalRef(clazz.get());
2243
2244 mLongValueId = env->GetMethodID(clazz.get(), "longValue", "()J");
2245 CHECK(mLongValueId != NULL);
2246
2247 clazz.reset(env->FindClass("java/lang/Float"));
2248 CHECK(clazz.get() != NULL);
2249 mFloatClass = (jclass)env->NewGlobalRef(clazz.get());
2250
2251 mFloatValueId = env->GetMethodID(clazz.get(), "floatValue", "()F");
2252 CHECK(mFloatValueId != NULL);
2253
2254 clazz.reset(env->FindClass("java/util/ArrayList"));
2255 CHECK(clazz.get() != NULL);
2256
2257 mByteBufferArrayId = env->GetMethodID(gByteBufferInfo.clazz, "array", "()[B");
2258 CHECK(mByteBufferArrayId != NULL);
2259 }
2260
2261 jclass mStringClass;
2262 jclass mIntegerClass;
2263 jmethodID mIntegerValueId;
2264 jclass mLongClass;
2265 jmethodID mLongValueId;
2266 jclass mFloatClass;
2267 jmethodID mFloatValueId;
2268 jmethodID mByteBufferArrayId;
2269 } sFields{env};
2270
2271 jint size = env->CallIntMethod(keys, gArrayListInfo.sizeId);
2272 if (size != env->CallIntMethod(values, gArrayListInfo.sizeId)) {
2273 return BAD_VALUE;
2274 }
2275
2276 sp<AMessage> result{new AMessage};
2277 for (jint i = 0; i < size; ++i) {
2278 ScopedLocalRef<jstring> jkey{
2279 env, (jstring)env->CallObjectMethod(keys, gArrayListInfo.getId, i)};
2280 const char *tmp = env->GetStringUTFChars(jkey.get(), nullptr);
2281 AString key;
2282 if (tmp) {
2283 key.setTo(tmp);
2284 }
2285 env->ReleaseStringUTFChars(jkey.get(), tmp);
2286 if (key.empty()) {
2287 return NO_MEMORY;
2288 }
2289
2290 ScopedLocalRef<jobject> jvalue{
2291 env, env->CallObjectMethod(values, gArrayListInfo.getId, i)};
2292
2293 if (env->IsInstanceOf(jvalue.get(), sFields.mStringClass)) {
2294 const char *tmp = env->GetStringUTFChars((jstring)jvalue.get(), nullptr);
2295 AString value;
2296 if (!tmp) {
2297 return NO_MEMORY;
2298 }
2299 value.setTo(tmp);
2300 env->ReleaseStringUTFChars((jstring)jvalue.get(), tmp);
2301 result->setString(key.c_str(), value);
2302 } else if (env->IsInstanceOf(jvalue.get(), sFields.mIntegerClass)) {
2303 jint value = env->CallIntMethod(jvalue.get(), sFields.mIntegerValueId);
2304 result->setInt32(key.c_str(), value);
2305 } else if (env->IsInstanceOf(jvalue.get(), sFields.mLongClass)) {
2306 jlong value = env->CallLongMethod(jvalue.get(), sFields.mLongValueId);
2307 result->setInt64(key.c_str(), value);
2308 } else if (env->IsInstanceOf(jvalue.get(), sFields.mFloatClass)) {
2309 jfloat value = env->CallFloatMethod(jvalue.get(), sFields.mFloatValueId);
2310 result->setFloat(key.c_str(), value);
2311 } else if (env->IsInstanceOf(jvalue.get(), gByteBufferInfo.clazz)) {
2312 jint position = env->CallIntMethod(jvalue.get(), gByteBufferInfo.getPositionId);
2313 jint limit = env->CallIntMethod(jvalue.get(), gByteBufferInfo.getLimitId);
2314 sp<ABuffer> buffer{new ABuffer(limit - position)};
2315 void *data = env->GetDirectBufferAddress(jvalue.get());
2316 if (data != nullptr) {
2317 memcpy(buffer->data(),
2318 static_cast<const uint8_t *>(data) + position,
2319 buffer->size());
2320 } else {
2321 ScopedLocalRef<jbyteArray> byteArray{env, (jbyteArray)env->CallObjectMethod(
2322 jvalue.get(), sFields.mByteBufferArrayId)};
2323 env->GetByteArrayRegion(byteArray.get(), position, buffer->size(),
2324 reinterpret_cast<jbyte *>(buffer->data()));
2325 }
2326 result->setBuffer(key.c_str(), buffer);
2327 }
2328 }
2329
2330 *msg = result;
2331 return OK;
2332 }
2333
2334 static bool obtain(
2335 JMediaCodecLinearBlock *context,
2336 int capacity,
2337 const std::vector<std::string> &names,
2338 bool secure) {
2339 if (secure) {
2340 // Start at 1MB, which is an arbitrary starting point that can
2341 // increase when needed.
2342 constexpr size_t kInitialDealerCapacity = 1048576;
2343 thread_local sp<MemoryDealer> sDealer = new MemoryDealer(
2344 kInitialDealerCapacity, "JNI(1MB)");
2345 context->mMemory = sDealer->allocate(capacity);
2346 if (context->mMemory == nullptr) {
2347 size_t newDealerCapacity = sDealer->getMemoryHeap()->getSize() * 2;
2348 while (capacity * 2 > newDealerCapacity) {
2349 newDealerCapacity *= 2;
2350 }
2351 ALOGI("LinearBlock.native_obtain: "
2352 "Dealer capacity increasing from %zuMB to %zuMB",
2353 sDealer->getMemoryHeap()->getSize() / 1048576,
2354 newDealerCapacity / 1048576);
2355 sDealer = new MemoryDealer(
2356 newDealerCapacity,
2357 AStringPrintf("JNI(%zuMB)", newDealerCapacity).c_str());
2358 context->mMemory = sDealer->allocate(capacity);
2359 }
2360 context->mHidlMemory = hardware::fromHeap(context->mMemory->getMemory(
2361 &context->mHidlMemoryOffset, &context->mHidlMemorySize));
2362 } else {
2363 context->mBlock = MediaCodec::FetchLinearBlock(capacity, names);
2364 if (!context->mBlock) {
2365 return false;
2366 }
2367 }
2368 context->mCodecNames = names;
2369 return true;
2370 }
2371
2372 static void extractMemoryFromContext(
2373 JMediaCodecLinearBlock *context,
2374 jint offset,
2375 jint size,
2376 sp<hardware::HidlMemory> *memory) {
2377 *memory = context->toHidlMemory();
2378 if (*memory == nullptr) {
2379 if (!context->mBlock) {
2380 ALOGW("extractMemoryFromContext: the buffer is missing both IMemory and C2Block");
2381 return;
2382 }
2383 ALOGD("extractMemoryFromContext: realloc & copying from C2Block to IMemory (cap=%zu)",
2384 context->capacity());
2385 if (!obtain(context, context->capacity(),
2386 context->mCodecNames, true /* secure */)) {
2387 ALOGW("extractMemoryFromContext: failed to obtain secure block");
2388 return;
2389 }
2390 C2WriteView view = context->mBlock->map().get();
2391 if (view.error() != C2_OK) {
2392 ALOGW("extractMemoryFromContext: failed to map C2Block (%d)", view.error());
2393 return;
2394 }
2395 uint8_t *memoryPtr = static_cast<uint8_t *>(context->mMemory->unsecurePointer());
2396 memcpy(memoryPtr + offset, view.base() + offset, size);
2397 context->mBlock.reset();
2398 context->mReadWriteMapping.reset();
2399 *memory = context->toHidlMemory();
2400 }
2401 }
2402
2403 static void extractBufferFromContext(
2404 JMediaCodecLinearBlock *context,
2405 jint offset,
2406 jint size,
2407 std::shared_ptr<C2Buffer> *buffer) {
2408 *buffer = context->toC2Buffer(offset, size);
2409 if (*buffer == nullptr) {
2410 if (!context->mMemory) {
2411 ALOGW("extractBufferFromContext: the buffer is missing both IMemory and C2Block");
2412 return;
2413 }
2414 ALOGD("extractBufferFromContext: realloc & copying from IMemory to C2Block (cap=%zu)",
2415 context->capacity());
2416 if (obtain(context, context->capacity(),
2417 context->mCodecNames, false /* secure */)) {
2418 ALOGW("extractBufferFromContext: failed to obtain non-secure block");
2419 return;
2420 }
2421 C2WriteView view = context->mBlock->map().get();
2422 if (view.error() != C2_OK) {
2423 ALOGW("extractBufferFromContext: failed to map C2Block (%d)", view.error());
2424 return;
2425 }
2426 uint8_t *memoryPtr = static_cast<uint8_t *>(context->mMemory->unsecurePointer());
2427 memcpy(view.base() + offset, memoryPtr + offset, size);
2428 context->mMemory.clear();
2429 context->mHidlMemory.clear();
2430 context->mHidlMemorySize = 0;
2431 context->mHidlMemoryOffset = 0;
2432 *buffer = context->toC2Buffer(offset, size);
2433 }
2434 }
2435
2436 static void android_media_MediaCodec_native_queueLinearBlock(
2437 JNIEnv *env, jobject thiz, jint index, jobject bufferObj,
2438 jint offset, jint size, jobject cryptoInfoObj,
2439 jlong presentationTimeUs, jint flags, jobject keys, jobject values) {
2440 ALOGV("android_media_MediaCodec_native_queueLinearBlock");
2441
2442 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2443
2444 if (codec == nullptr || codec->initCheck() != OK) {
2445 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2446 return;
2447 }
2448
2449 sp<AMessage> tunings;
2450 status_t err = ConvertKeyValueListsToAMessage(env, keys, values, &tunings);
2451 if (err != OK) {
2452 throwExceptionAsNecessary(
2453 env, err, ACTION_CODE_FATAL,
2454 "error occurred while converting tunings from Java to native");
2455 return;
2456 }
2457
2458 std::shared_ptr<C2Buffer> buffer;
2459 sp<hardware::HidlMemory> memory;
2460 ScopedLocalRef<jobject> lock{env, env->GetObjectField(bufferObj, gLinearBlockInfo.lockId)};
2461 if (env->MonitorEnter(lock.get()) == JNI_OK) {
2462 if (env->GetBooleanField(bufferObj, gLinearBlockInfo.validId)) {
2463 JMediaCodecLinearBlock *context =
2464 (JMediaCodecLinearBlock *)env->GetLongField(bufferObj, gLinearBlockInfo.contextId);
2465 if (codec->hasCryptoOrDescrambler()) {
2466 extractMemoryFromContext(context, offset, size, &memory);
2467 offset += context->mHidlMemoryOffset;
2468 } else {
2469 extractBufferFromContext(context, offset, size, &buffer);
2470 }
2471 }
2472 env->MonitorExit(lock.get());
2473 } else {
2474 throwExceptionAsNecessary(
2475 env, INVALID_OPERATION, ACTION_CODE_FATAL,
2476 "Failed to grab lock for a LinearBlock object");
2477 return;
2478 }
2479
2480 AString errorDetailMsg;
2481 if (codec->hasCryptoOrDescrambler()) {
2482 if (!memory) {
2483 // It means there was an unexpected failure in extractMemoryFromContext above
2484 ALOGI("queueLinearBlock: no ashmem memory for encrypted content");
2485 throwExceptionAsNecessary(
2486 env, BAD_VALUE, ACTION_CODE_FATAL,
2487 "Unexpected error: the input buffer is not compatible with "
2488 "the secure codec, and a fallback logic failed.\n"
2489 "Suggestion: please try including the secure codec when calling "
2490 "MediaCodec.LinearBlock#obtain method to obtain a compatible buffer.");
2491 return;
2492 }
2493 auto cryptoInfo =
2494 cryptoInfoObj ? NativeCryptoInfo{size} : NativeCryptoInfo{env, cryptoInfoObj};
2495 if (env->ExceptionCheck()) {
2496 // Creation of cryptoInfo failed. Let the exception bubble up.
2497 return;
2498 }
2499 err = codec->queueEncryptedLinearBlock(
2500 index,
2501 memory,
2502 offset,
2503 cryptoInfo.mSubSamples, cryptoInfo.mNumSubSamples,
2504 (const uint8_t *)cryptoInfo.mKey, (const uint8_t *)cryptoInfo.mIv,
2505 cryptoInfo.mMode,
2506 cryptoInfo.mPattern,
2507 presentationTimeUs,
2508 flags,
2509 tunings,
2510 &errorDetailMsg);
2511 ALOGI_IF(err != OK, "queueEncryptedLinearBlock returned err = %d", err);
2512 } else {
2513 if (!buffer) {
2514 // It means there was an unexpected failure in extractBufferFromContext above
2515 ALOGI("queueLinearBlock: no C2Buffer found");
2516 throwExceptionAsNecessary(
2517 env, BAD_VALUE, ACTION_CODE_FATAL,
2518 "Unexpected error: the input buffer is not compatible with "
2519 "the non-secure codec, and a fallback logic failed.\n"
2520 "Suggestion: please do not include the secure codec when calling "
2521 "MediaCodec.LinearBlock#obtain method to obtain a compatible buffer.");
2522 return;
2523 }
2524 err = codec->queueBuffer(
2525 index, buffer, presentationTimeUs, flags, tunings, &errorDetailMsg);
2526 }
2527 throwExceptionAsNecessary(
2528 env, err, ACTION_CODE_FATAL,
2529 codec->getExceptionMessage(errorDetailMsg.c_str()).c_str());
2530 }
2531
2532 static void android_media_MediaCodec_native_queueHardwareBuffer(
2533 JNIEnv *env, jobject thiz, jint index, jobject bufferObj,
2534 jlong presentationTimeUs, jint flags, jobject keys, jobject values) {
2535 ALOGV("android_media_MediaCodec_native_queueHardwareBuffer");
2536
2537 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2538
2539 if (codec == NULL || codec->initCheck() != OK) {
2540 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2541 return;
2542 }
2543
2544 sp<AMessage> tunings;
2545 status_t err = ConvertKeyValueListsToAMessage(env, keys, values, &tunings);
2546 if (err != OK) {
2547 throwExceptionAsNecessary(
2548 env, err, ACTION_CODE_FATAL,
2549 "error occurred while converting tunings from Java to native");
2550 return;
2551 }
2552
2553 AHardwareBuffer *hardwareBuffer = android_hardware_HardwareBuffer_getNativeHardwareBuffer(
2554 env, bufferObj);
2555 sp<GraphicBuffer> graphicBuffer{AHardwareBuffer_to_GraphicBuffer(hardwareBuffer)};
2556 C2Handle *handle = WrapNativeCodec2GrallocHandle(
2557 graphicBuffer->handle, graphicBuffer->width, graphicBuffer->height,
2558 graphicBuffer->format, graphicBuffer->usage, graphicBuffer->stride);
2559 static std::shared_ptr<C2Allocator> sGrallocAlloc = []() -> std::shared_ptr<C2Allocator> {
2560 std::shared_ptr<C2Allocator> alloc;
2561 c2_status_t err = GetCodec2PlatformAllocatorStore()->fetchAllocator(
2562 C2PlatformAllocatorStore::GRALLOC, &alloc);
2563 if (err == C2_OK) {
2564 return alloc;
2565 }
2566 return nullptr;
2567 }();
2568 std::shared_ptr<C2GraphicAllocation> alloc;
2569 c2_status_t c2err = sGrallocAlloc->priorGraphicAllocation(handle, &alloc);
2570 if (c2err != C2_OK) {
2571 ALOGW("Failed to wrap AHardwareBuffer into C2GraphicAllocation");
2572 native_handle_close(handle);
2573 native_handle_delete(handle);
2574 throwExceptionAsNecessary(
2575 env, BAD_VALUE, ACTION_CODE_FATAL,
2576 "HardwareBuffer not recognized");
2577 return;
2578 }
2579 std::shared_ptr<C2GraphicBlock> block = _C2BlockFactory::CreateGraphicBlock(alloc);
2580 std::shared_ptr<C2Buffer> buffer = C2Buffer::CreateGraphicBuffer(block->share(
2581 block->crop(), C2Fence{}));
2582 AString errorDetailMsg;
2583 err = codec->queueBuffer(
2584 index, buffer, presentationTimeUs, flags, tunings, &errorDetailMsg);
2585 throwExceptionAsNecessary(
2586 env, err, ACTION_CODE_FATAL,
2587 codec->getExceptionMessage(errorDetailMsg.c_str()).c_str());
2588 }
2589
2590 static void android_media_MediaCodec_native_getOutputFrame(
2591 JNIEnv *env, jobject thiz, jobject frame, jint index) {
2592 ALOGV("android_media_MediaCodec_native_getOutputFrame");
2593
2594 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2595
2596 if (codec == NULL || codec->initCheck() != OK) {
2597 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2598 return;
2599 }
2600
2601 status_t err = codec->getOutputFrame(env, frame, index);
2602 if (err != OK) {
2603 throwExceptionAsNecessary(env, err, codec);
2604 }
2605 }
2606
2607 static jint android_media_MediaCodec_dequeueInputBuffer(
2608 JNIEnv *env, jobject thiz, jlong timeoutUs) {
2609 ALOGV("android_media_MediaCodec_dequeueInputBuffer");
2610
2611 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2612
2613 if (codec == NULL || codec->initCheck() != OK) {
2614 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2615 return -1;
2616 }
2617
2618 size_t index;
2619 status_t err = codec->dequeueInputBuffer(&index, timeoutUs);
2620
2621 if (err == OK) {
2622 return (jint) index;
2623 }
2624
2625 return throwExceptionAsNecessary(env, err, codec);
2626 }
2627
2628 static jint android_media_MediaCodec_dequeueOutputBuffer(
2629 JNIEnv *env, jobject thiz, jobject bufferInfo, jlong timeoutUs) {
2630 ALOGV("android_media_MediaCodec_dequeueOutputBuffer");
2631
2632 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2633
2634 if (codec == NULL || codec->initCheck() != OK) {
2635 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2636 return 0;
2637 }
2638
2639 size_t index;
2640 status_t err = codec->dequeueOutputBuffer(
2641 env, bufferInfo, &index, timeoutUs);
2642
2643 if (err == OK) {
2644 return (jint) index;
2645 }
2646
2647 return throwExceptionAsNecessary(env, err, codec);
2648 }
2649
2650 static void android_media_MediaCodec_releaseOutputBuffer(
2651 JNIEnv *env, jobject thiz,
2652 jint index, jboolean render, jboolean updatePTS, jlong timestampNs) {
2653 ALOGV("android_media_MediaCodec_renderOutputBufferAndRelease");
2654
2655 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2656
2657 if (codec == NULL || codec->initCheck() != OK) {
2658 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2659 return;
2660 }
2661
2662 status_t err = codec->releaseOutputBuffer(index, render, updatePTS, timestampNs);
2663
2664 throwExceptionAsNecessary(env, err, codec);
2665 }
2666
2667 static void android_media_MediaCodec_signalEndOfInputStream(JNIEnv* env,
2668 jobject thiz) {
2669 ALOGV("android_media_MediaCodec_signalEndOfInputStream");
2670
2671 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2672 if (codec == NULL || codec->initCheck() != OK) {
2673 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2674 return;
2675 }
2676
2677 status_t err = codec->signalEndOfInputStream();
2678
2679 throwExceptionAsNecessary(env, err, codec);
2680 }
2681
2682 static jobject android_media_MediaCodec_getFormatNative(
2683 JNIEnv *env, jobject thiz, jboolean input) {
2684 ALOGV("android_media_MediaCodec_getFormatNative");
2685
2686 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2687
2688 if (codec == NULL || codec->initCheck() != OK) {
2689 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2690 return NULL;
2691 }
2692
2693 jobject format;
2694 status_t err = codec->getFormat(env, input, &format);
2695
2696 if (err == OK) {
2697 return format;
2698 }
2699
2700 throwExceptionAsNecessary(env, err, codec);
2701
2702 return NULL;
2703 }
2704
2705 static jobject android_media_MediaCodec_getOutputFormatForIndexNative(
2706 JNIEnv *env, jobject thiz, jint index) {
2707 ALOGV("android_media_MediaCodec_getOutputFormatForIndexNative");
2708
2709 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2710
2711 if (codec == NULL || codec->initCheck() != OK) {
2712 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2713 return NULL;
2714 }
2715
2716 jobject format;
2717 status_t err = codec->getOutputFormat(env, index, &format);
2718
2719 if (err == OK) {
2720 return format;
2721 }
2722
2723 throwExceptionAsNecessary(env, err, codec);
2724
2725 return NULL;
2726 }
2727
2728 static jobjectArray android_media_MediaCodec_getBuffers(
2729 JNIEnv *env, jobject thiz, jboolean input) {
2730 ALOGV("android_media_MediaCodec_getBuffers");
2731
2732 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2733
2734 if (codec == NULL || codec->initCheck() != OK) {
2735 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2736 return NULL;
2737 }
2738
2739 jobjectArray buffers;
2740 status_t err = codec->getBuffers(env, input, &buffers);
2741
2742 if (err == OK) {
2743 return buffers;
2744 }
2745
2746 // if we're out of memory, an exception was already thrown
2747 if (err != NO_MEMORY) {
2748 throwExceptionAsNecessary(env, err, codec);
2749 }
2750
2751 return NULL;
2752 }
2753
2754 static jobject android_media_MediaCodec_getBuffer(
2755 JNIEnv *env, jobject thiz, jboolean input, jint index) {
2756 ALOGV("android_media_MediaCodec_getBuffer");
2757
2758 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2759
2760 if (codec == NULL || codec->initCheck() != OK) {
2761 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2762 return NULL;
2763 }
2764
2765 jobject buffer;
2766 status_t err = codec->getBuffer(env, input, index, &buffer);
2767
2768 if (err == OK) {
2769 return buffer;
2770 }
2771
2772 // if we're out of memory, an exception was already thrown
2773 if (err != NO_MEMORY) {
2774 throwExceptionAsNecessary(env, err, codec);
2775 }
2776
2777 return NULL;
2778 }
2779
2780 static jobject android_media_MediaCodec_getImage(
2781 JNIEnv *env, jobject thiz, jboolean input, jint index) {
2782 ALOGV("android_media_MediaCodec_getImage");
2783
2784 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2785
2786 if (codec == NULL || codec->initCheck() != OK) {
2787 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2788 return NULL;
2789 }
2790
2791 jobject image;
2792 status_t err = codec->getImage(env, input, index, &image);
2793
2794 if (err == OK) {
2795 return image;
2796 }
2797
2798 // if we're out of memory, an exception was already thrown
2799 if (err != NO_MEMORY) {
2800 throwExceptionAsNecessary(env, err, codec);
2801 }
2802
2803 return NULL;
2804 }
2805
2806 static jobject android_media_MediaCodec_getName(
2807 JNIEnv *env, jobject thiz) {
2808 ALOGV("android_media_MediaCodec_getName");
2809
2810 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2811
2812 if (codec == NULL || codec->initCheck() != OK) {
2813 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2814 return NULL;
2815 }
2816
2817 jstring name;
2818 status_t err = codec->getName(env, &name);
2819
2820 if (err == OK) {
2821 return name;
2822 }
2823
2824 throwExceptionAsNecessary(env, err, codec);
2825
2826 return NULL;
2827 }
2828
2829 static jobject android_media_MediaCodec_getOwnCodecInfo(
2830 JNIEnv *env, jobject thiz) {
2831 ALOGV("android_media_MediaCodec_getOwnCodecInfo");
2832
2833 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2834
2835 if (codec == NULL || codec->initCheck() != OK) {
2836 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2837 return NULL;
2838 }
2839
2840 jobject codecInfoObj;
2841 status_t err = codec->getCodecInfo(env, &codecInfoObj);
2842
2843 if (err == OK) {
2844 return codecInfoObj;
2845 }
2846
2847 throwExceptionAsNecessary(env, err, codec);
2848
2849 return NULL;
2850 }
2851
2852 static jobject
2853 android_media_MediaCodec_native_getMetrics(JNIEnv *env, jobject thiz)
2854 {
2855 ALOGV("android_media_MediaCodec_native_getMetrics");
2856
2857 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2858 if (codec == NULL || codec->initCheck() != OK) {
2859 jniThrowException(env, "java/lang/IllegalStateException",
2860 GetExceptionMessage(codec, NULL).c_str());
2861 return 0;
2862 }
2863
2864 // get what we have for the metrics from the codec
2865 mediametrics::Item *item = 0;
2866
2867 status_t err = codec->getMetrics(env, item);
2868 if (err != OK) {
2869 ALOGE("getMetrics failed");
2870 return (jobject) NULL;
2871 }
2872
2873 jobject mybundle = MediaMetricsJNI::writeMetricsToBundle(env, item, NULL);
2874
2875 // housekeeping
2876 delete item;
2877 item = 0;
2878
2879 return mybundle;
2880 }
2881
2882 static void android_media_MediaCodec_setParameters(
2883 JNIEnv *env, jobject thiz, jobjectArray keys, jobjectArray vals) {
2884 ALOGV("android_media_MediaCodec_setParameters");
2885
2886 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2887
2888 if (codec == NULL || codec->initCheck() != OK) {
2889 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2890 return;
2891 }
2892
2893 sp<AMessage> params;
2894 status_t err = ConvertKeyValueArraysToMessage(env, keys, vals, &params);
2895
2896 if (err == OK) {
2897 err = codec->setParameters(params);
2898 }
2899
2900 throwExceptionAsNecessary(env, err, codec);
2901 }
2902
2903 static void android_media_MediaCodec_setVideoScalingMode(
2904 JNIEnv *env, jobject thiz, jint mode) {
2905 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2906
2907 if (codec == NULL || codec->initCheck() != OK) {
2908 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2909 return;
2910 }
2911
2912 if (mode != NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW
2913 && mode != NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
2914 jniThrowException(env, "java/lang/IllegalArgumentException",
2915 String8::format("Unrecognized mode: %d", mode));
2916 return;
2917 }
2918
2919 codec->setVideoScalingMode(mode);
2920 }
2921
2922 static void android_media_MediaCodec_setAudioPresentation(
2923 JNIEnv *env, jobject thiz, jint presentationId, jint programId) {
2924 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2925
2926 if (codec == NULL || codec->initCheck() != OK) {
2927 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2928 return;
2929 }
2930
2931 codec->selectAudioPresentation((int32_t)presentationId, (int32_t)programId);
2932 }
2933
2934 static jobject android_media_MediaCodec_getSupportedVendorParameters(
2935 JNIEnv *env, jobject thiz) {
2936 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2937
2938 if (codec == NULL || codec->initCheck() != OK) {
2939 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2940 return NULL;
2941 }
2942
2943 jobject ret = NULL;
2944 status_t status = codec->querySupportedVendorParameters(env, &ret);
2945 if (status != OK) {
2946 throwExceptionAsNecessary(env, status, codec);
2947 }
2948
2949 return ret;
2950 }
2951
2952 static jobject android_media_MediaCodec_getParameterDescriptor(
2953 JNIEnv *env, jobject thiz, jstring name) {
2954 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2955
2956 if (codec == NULL || codec->initCheck() != OK) {
2957 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2958 return NULL;
2959 }
2960
2961 jobject ret = NULL;
2962 status_t status = codec->describeParameter(env, name, &ret);
2963 if (status != OK) {
2964 ret = NULL;
2965 }
2966 return ret;
2967 }
2968
2969 static void android_media_MediaCodec_subscribeToVendorParameters(
2970 JNIEnv *env, jobject thiz, jobject names) {
2971 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2972
2973 if (codec == NULL || codec->initCheck() != OK) {
2974 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2975 return;
2976 }
2977
2978 status_t status = codec->subscribeToVendorParameters(env, names);
2979 if (status != OK) {
2980 throwExceptionAsNecessary(env, status, codec);
2981 }
2982 return;
2983 }
2984
2985 static void android_media_MediaCodec_unsubscribeFromVendorParameters(
2986 JNIEnv *env, jobject thiz, jobject names) {
2987 sp<JMediaCodec> codec = getMediaCodec(env, thiz);
2988
2989 if (codec == NULL || codec->initCheck() != OK) {
2990 throwExceptionAsNecessary(env, INVALID_OPERATION, codec);
2991 return;
2992 }
2993
2994 status_t status = codec->unsubscribeFromVendorParameters(env, names);
2995 if (status != OK) {
2996 throwExceptionAsNecessary(env, status, codec);
2997 }
2998 return;
2999 }
3000
3001 static void android_media_MediaCodec_native_init(JNIEnv *env, jclass) {
3002 ScopedLocalRef<jclass> clazz(
3003 env, env->FindClass("android/media/MediaCodec"));
3004 CHECK(clazz.get() != NULL);
3005
3006 gFields.postEventFromNativeID =
3007 env->GetMethodID(
3008 clazz.get(), "postEventFromNative", "(IIILjava/lang/Object;)V");
3009 CHECK(gFields.postEventFromNativeID != NULL);
3010
3011 gFields.lockAndGetContextID =
3012 env->GetMethodID(
3013 clazz.get(), "lockAndGetContext", "()J");
3014 CHECK(gFields.lockAndGetContextID != NULL);
3015
3016 gFields.setAndUnlockContextID =
3017 env->GetMethodID(
3018 clazz.get(), "setAndUnlockContext", "(J)V");
3019 CHECK(gFields.setAndUnlockContextID != NULL);
3020
3021 jfieldID field;
3022 field = env->GetStaticFieldID(clazz.get(), "CRYPTO_MODE_UNENCRYPTED", "I");
3023 CHECK(field != NULL);
3024 gCryptoModes.Unencrypted =
3025 env->GetStaticIntField(clazz.get(), field);
3026
3027 field = env->GetStaticFieldID(clazz.get(), "CRYPTO_MODE_AES_CTR", "I");
3028 CHECK(field != NULL);
3029 gCryptoModes.AesCtr =
3030 env->GetStaticIntField(clazz.get(), field);
3031
3032 field = env->GetStaticFieldID(clazz.get(), "CRYPTO_MODE_AES_CBC", "I");
3033 CHECK(field != NULL);
3034 gCryptoModes.AesCbc =
3035 env->GetStaticIntField(clazz.get(), field);
3036
3037 clazz.reset(env->FindClass("android/media/MediaCodec$CryptoInfo"));
3038 CHECK(clazz.get() != NULL);
3039
3040 gFields.cryptoInfoNumSubSamplesID =
3041 env->GetFieldID(clazz.get(), "numSubSamples", "I");
3042 CHECK(gFields.cryptoInfoNumSubSamplesID != NULL);
3043
3044 gFields.cryptoInfoNumBytesOfClearDataID =
3045 env->GetFieldID(clazz.get(), "numBytesOfClearData", "[I");
3046 CHECK(gFields.cryptoInfoNumBytesOfClearDataID != NULL);
3047
3048 gFields.cryptoInfoNumBytesOfEncryptedDataID =
3049 env->GetFieldID(clazz.get(), "numBytesOfEncryptedData", "[I");
3050 CHECK(gFields.cryptoInfoNumBytesOfEncryptedDataID != NULL);
3051
3052 gFields.cryptoInfoKeyID = env->GetFieldID(clazz.get(), "key", "[B");
3053 CHECK(gFields.cryptoInfoKeyID != NULL);
3054
3055 gFields.cryptoInfoIVID = env->GetFieldID(clazz.get(), "iv", "[B");
3056 CHECK(gFields.cryptoInfoIVID != NULL);
3057
3058 gFields.cryptoInfoModeID = env->GetFieldID(clazz.get(), "mode", "I");
3059 CHECK(gFields.cryptoInfoModeID != NULL);
3060
3061 gFields.cryptoInfoPatternID = env->GetFieldID(clazz.get(), "mPattern",
3062 "Landroid/media/MediaCodec$CryptoInfo$Pattern;");
3063 CHECK(gFields.cryptoInfoPatternID != NULL);
3064
3065 clazz.reset(env->FindClass("android/media/MediaCodec$CryptoInfo$Pattern"));
3066 CHECK(clazz.get() != NULL);
3067
3068 gFields.patternEncryptBlocksID = env->GetFieldID(clazz.get(), "mEncryptBlocks", "I");
3069 CHECK(gFields.patternEncryptBlocksID != NULL);
3070
3071 gFields.patternSkipBlocksID = env->GetFieldID(clazz.get(), "mSkipBlocks", "I");
3072 CHECK(gFields.patternSkipBlocksID != NULL);
3073
3074 clazz.reset(env->FindClass("android/media/MediaCodec$QueueRequest"));
3075 CHECK(clazz.get() != NULL);
3076
3077 gFields.queueRequestIndexID = env->GetFieldID(clazz.get(), "mIndex", "I");
3078 CHECK(gFields.queueRequestIndexID != NULL);
3079
3080 clazz.reset(env->FindClass("android/media/MediaCodec$OutputFrame"));
3081 CHECK(clazz.get() != NULL);
3082
3083 gFields.outputFrameLinearBlockID =
3084 env->GetFieldID(clazz.get(), "mLinearBlock", "Landroid/media/MediaCodec$LinearBlock;");
3085 CHECK(gFields.outputFrameLinearBlockID != NULL);
3086
3087 gFields.outputFrameHardwareBufferID =
3088 env->GetFieldID(clazz.get(), "mHardwareBuffer", "Landroid/hardware/HardwareBuffer;");
3089 CHECK(gFields.outputFrameHardwareBufferID != NULL);
3090
3091 gFields.outputFrameChangedKeysID =
3092 env->GetFieldID(clazz.get(), "mChangedKeys", "Ljava/util/ArrayList;");
3093 CHECK(gFields.outputFrameChangedKeysID != NULL);
3094
3095 gFields.outputFrameFormatID =
3096 env->GetFieldID(clazz.get(), "mFormat", "Landroid/media/MediaFormat;");
3097 CHECK(gFields.outputFrameFormatID != NULL);
3098
3099 clazz.reset(env->FindClass("android/media/MediaCodec$CryptoException"));
3100 CHECK(clazz.get() != NULL);
3101
3102 field = env->GetStaticFieldID(clazz.get(), "ERROR_NO_KEY", "I");
3103 CHECK(field != NULL);
3104 gCryptoErrorCodes.cryptoErrorNoKey =
3105 env->GetStaticIntField(clazz.get(), field);
3106
3107 field = env->GetStaticFieldID(clazz.get(), "ERROR_KEY_EXPIRED", "I");
3108 CHECK(field != NULL);
3109 gCryptoErrorCodes.cryptoErrorKeyExpired =
3110 env->GetStaticIntField(clazz.get(), field);
3111
3112 field = env->GetStaticFieldID(clazz.get(), "ERROR_RESOURCE_BUSY", "I");
3113 CHECK(field != NULL);
3114 gCryptoErrorCodes.cryptoErrorResourceBusy =
3115 env->GetStaticIntField(clazz.get(), field);
3116
3117 field = env->GetStaticFieldID(clazz.get(), "ERROR_INSUFFICIENT_OUTPUT_PROTECTION", "I");
3118 CHECK(field != NULL);
3119 gCryptoErrorCodes.cryptoErrorInsufficientOutputProtection =
3120 env->GetStaticIntField(clazz.get(), field);
3121
3122 field = env->GetStaticFieldID(clazz.get(), "ERROR_SESSION_NOT_OPENED", "I");
3123 CHECK(field != NULL);
3124 gCryptoErrorCodes.cryptoErrorSessionNotOpened =
3125 env->GetStaticIntField(clazz.get(), field);
3126
3127 field = env->GetStaticFieldID(clazz.get(), "ERROR_INSUFFICIENT_SECURITY", "I");
3128 CHECK(field != NULL);
3129 gCryptoErrorCodes.cryptoErrorInsufficientSecurity =
3130 env->GetStaticIntField(clazz.get(), field);
3131
3132 field = env->GetStaticFieldID(clazz.get(), "ERROR_UNSUPPORTED_OPERATION", "I");
3133 CHECK(field != NULL);
3134 gCryptoErrorCodes.cryptoErrorUnsupportedOperation =
3135 env->GetStaticIntField(clazz.get(), field);
3136
3137 field = env->GetStaticFieldID(clazz.get(), "ERROR_FRAME_TOO_LARGE", "I");
3138 CHECK(field != NULL);
3139 gCryptoErrorCodes.cryptoErrorFrameTooLarge =
3140 env->GetStaticIntField(clazz.get(), field);
3141
3142 field = env->GetStaticFieldID(clazz.get(), "ERROR_LOST_STATE", "I");
3143 CHECK(field != NULL);
3144 gCryptoErrorCodes.cryptoErrorLostState =
3145 env->GetStaticIntField(clazz.get(), field);
3146
3147 clazz.reset(env->FindClass("android/media/MediaCodec$CodecException"));
3148 CHECK(clazz.get() != NULL);
3149 field = env->GetStaticFieldID(clazz.get(), "ACTION_TRANSIENT", "I");
3150 CHECK(field != NULL);
3151 gCodecActionCodes.codecActionTransient =
3152 env->GetStaticIntField(clazz.get(), field);
3153
3154 field = env->GetStaticFieldID(clazz.get(), "ACTION_RECOVERABLE", "I");
3155 CHECK(field != NULL);
3156 gCodecActionCodes.codecActionRecoverable =
3157 env->GetStaticIntField(clazz.get(), field);
3158
3159 field = env->GetStaticFieldID(clazz.get(), "ERROR_INSUFFICIENT_RESOURCE", "I");
3160 CHECK(field != NULL);
3161 gCodecErrorCodes.errorInsufficientResource =
3162 env->GetStaticIntField(clazz.get(), field);
3163
3164 field = env->GetStaticFieldID(clazz.get(), "ERROR_RECLAIMED", "I");
3165 CHECK(field != NULL);
3166 gCodecErrorCodes.errorReclaimed =
3167 env->GetStaticIntField(clazz.get(), field);
3168
3169 clazz.reset(env->FindClass("android/view/Surface"));
3170 CHECK(clazz.get() != NULL);
3171
3172 field = env->GetFieldID(clazz.get(), "mLock", "Ljava/lang/Object;");
3173 CHECK(field != NULL);
3174 gPersistentSurfaceClassInfo.mLock = field;
3175
3176 jmethodID method = env->GetMethodID(clazz.get(), "setNativeObjectLocked", "(J)V");
3177 CHECK(method != NULL);
3178 gPersistentSurfaceClassInfo.setNativeObjectLocked = method;
3179
3180 clazz.reset(env->FindClass("android/media/MediaCodec$PersistentSurface"));
3181 CHECK(clazz.get() != NULL);
3182 gPersistentSurfaceClassInfo.clazz = (jclass)env->NewGlobalRef(clazz.get());
3183
3184 method = env->GetMethodID(clazz.get(), "<init>", "()V");
3185 CHECK(method != NULL);
3186 gPersistentSurfaceClassInfo.ctor = method;
3187
3188 field = env->GetFieldID(clazz.get(), "mPersistentObject", "J");
3189 CHECK(field != NULL);
3190 gPersistentSurfaceClassInfo.mPersistentObject = field;
3191
3192 clazz.reset(env->FindClass("android/media/MediaCodecInfo$CodecCapabilities"));
3193 CHECK(clazz.get() != NULL);
3194 gCodecInfo.capsClazz = (jclass)env->NewGlobalRef(clazz.get());
3195
3196 method = env->GetMethodID(clazz.get(), "<init>",
3197 "([Landroid/media/MediaCodecInfo$CodecProfileLevel;[IZ"
3198 "Ljava/util/Map;Ljava/util/Map;)V");
3199 CHECK(method != NULL);
3200 gCodecInfo.capsCtorId = method;
3201
3202 clazz.reset(env->FindClass("android/media/MediaCodecInfo$CodecProfileLevel"));
3203 CHECK(clazz.get() != NULL);
3204 gCodecInfo.profileLevelClazz = (jclass)env->NewGlobalRef(clazz.get());
3205
3206 field = env->GetFieldID(clazz.get(), "profile", "I");
3207 CHECK(field != NULL);
3208 gCodecInfo.profileField = field;
3209
3210 field = env->GetFieldID(clazz.get(), "level", "I");
3211 CHECK(field != NULL);
3212 gCodecInfo.levelField = field;
3213
3214 clazz.reset(env->FindClass("java/nio/ByteBuffer"));
3215 CHECK(clazz.get() != NULL);
3216 gByteBufferInfo.clazz = (jclass)env->NewGlobalRef(clazz.get());
3217
3218 ScopedLocalRef<jclass> byteOrderClass(
3219 env, env->FindClass("java/nio/ByteOrder"));
3220 CHECK(byteOrderClass.get() != NULL);
3221
3222 jmethodID nativeOrderID = env->GetStaticMethodID(
3223 byteOrderClass.get(), "nativeOrder", "()Ljava/nio/ByteOrder;");
3224 CHECK(nativeOrderID != NULL);
3225
3226 ScopedLocalRef<jobject> nativeByteOrderObj{
3227 env, env->CallStaticObjectMethod(byteOrderClass.get(), nativeOrderID)};
3228 gByteBufferInfo.nativeByteOrder = env->NewGlobalRef(nativeByteOrderObj.get());
3229 CHECK(gByteBufferInfo.nativeByteOrder != NULL);
3230 nativeByteOrderObj.reset();
3231
3232 gByteBufferInfo.orderId = env->GetMethodID(
3233 clazz.get(),
3234 "order",
3235 "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;");
3236 CHECK(gByteBufferInfo.orderId != NULL);
3237
3238 gByteBufferInfo.asReadOnlyBufferId = env->GetMethodID(
3239 clazz.get(), "asReadOnlyBuffer", "()Ljava/nio/ByteBuffer;");
3240 CHECK(gByteBufferInfo.asReadOnlyBufferId != NULL);
3241
3242 gByteBufferInfo.positionId = env->GetMethodID(
3243 clazz.get(), "position", "(I)Ljava/nio/Buffer;");
3244 CHECK(gByteBufferInfo.positionId != NULL);
3245
3246 gByteBufferInfo.limitId = env->GetMethodID(
3247 clazz.get(), "limit", "(I)Ljava/nio/Buffer;");
3248 CHECK(gByteBufferInfo.limitId != NULL);
3249
3250 gByteBufferInfo.getPositionId = env->GetMethodID(
3251 clazz.get(), "position", "()I");
3252 CHECK(gByteBufferInfo.getPositionId != NULL);
3253
3254 gByteBufferInfo.getLimitId = env->GetMethodID(
3255 clazz.get(), "limit", "()I");
3256 CHECK(gByteBufferInfo.getLimitId != NULL);
3257
3258 clazz.reset(env->FindClass("java/util/ArrayList"));
3259 CHECK(clazz.get() != NULL);
3260 gArrayListInfo.clazz = (jclass)env->NewGlobalRef(clazz.get());
3261
3262 gArrayListInfo.ctorId = env->GetMethodID(clazz.get(), "<init>", "()V");
3263 CHECK(gArrayListInfo.ctorId != NULL);
3264
3265 gArrayListInfo.sizeId = env->GetMethodID(clazz.get(), "size", "()I");
3266 CHECK(gArrayListInfo.sizeId != NULL);
3267
3268 gArrayListInfo.getId = env->GetMethodID(clazz.get(), "get", "(I)Ljava/lang/Object;");
3269 CHECK(gArrayListInfo.getId != NULL);
3270
3271 gArrayListInfo.addId = env->GetMethodID(clazz.get(), "add", "(Ljava/lang/Object;)Z");
3272 CHECK(gArrayListInfo.addId != NULL);
3273
3274 clazz.reset(env->FindClass("android/media/MediaCodec$LinearBlock"));
3275 CHECK(clazz.get() != NULL);
3276
3277 gLinearBlockInfo.clazz = (jclass)env->NewGlobalRef(clazz.get());
3278
3279 gLinearBlockInfo.ctorId = env->GetMethodID(clazz.get(), "<init>", "()V");
3280 CHECK(gLinearBlockInfo.ctorId != NULL);
3281
3282 gLinearBlockInfo.setInternalStateId = env->GetMethodID(
3283 clazz.get(), "setInternalStateLocked", "(JZ)V");
3284 CHECK(gLinearBlockInfo.setInternalStateId != NULL);
3285
3286 gLinearBlockInfo.contextId = env->GetFieldID(clazz.get(), "mNativeContext", "J");
3287 CHECK(gLinearBlockInfo.contextId != NULL);
3288
3289 gLinearBlockInfo.validId = env->GetFieldID(clazz.get(), "mValid", "Z");
3290 CHECK(gLinearBlockInfo.validId != NULL);
3291
3292 gLinearBlockInfo.lockId = env->GetFieldID(clazz.get(), "mLock", "Ljava/lang/Object;");
3293 CHECK(gLinearBlockInfo.lockId != NULL);
3294
3295 clazz.reset(env->FindClass("android/media/MediaCodec$ParameterDescriptor"));
3296 CHECK(clazz.get() != NULL);
3297 gDescriptorInfo.clazz = (jclass)env->NewGlobalRef(clazz.get());
3298
3299 gDescriptorInfo.ctorId = env->GetMethodID(clazz.get(), "<init>", "()V");
3300 CHECK(gDescriptorInfo.ctorId != NULL);
3301
3302 gDescriptorInfo.nameId = env->GetFieldID(clazz.get(), "mName", "Ljava/lang/String;");
3303 CHECK(gDescriptorInfo.nameId != NULL);
3304
3305 gDescriptorInfo.typeId = env->GetFieldID(clazz.get(), "mType", "I");
3306 CHECK(gDescriptorInfo.typeId != NULL);
3307
3308 clazz.reset(env->FindClass("android/media/MediaCodec$BufferInfo"));
3309 CHECK(clazz.get() != NULL);
3310 gBufferInfo.clazz = (jclass)env->NewGlobalRef(clazz.get());
3311
3312 gBufferInfo.ctorId = env->GetMethodID(clazz.get(), "<init>", "()V");
3313 CHECK(gBufferInfo.ctorId != NULL);
3314
3315 gBufferInfo.setId = env->GetMethodID(clazz.get(), "set", "(IIJI)V");
3316 CHECK(gBufferInfo.setId != NULL);
3317 }
3318
3319 static void android_media_MediaCodec_native_setup(
3320 JNIEnv *env, jobject thiz,
3321 jstring name, jboolean nameIsType, jboolean encoder, int pid, int uid) {
3322 if (name == NULL) {
3323 jniThrowException(env, "java/lang/NullPointerException", NULL);
3324 return;
3325 }
3326
3327 const char *tmp = env->GetStringUTFChars(name, NULL);
3328
3329 if (tmp == NULL) {
3330 return;
3331 }
3332
3333 sp<JMediaCodec> codec = new JMediaCodec(env, thiz, tmp, nameIsType, encoder, pid, uid);
3334
3335 const status_t err = codec->initCheck();
3336 if (err == NAME_NOT_FOUND) {
3337 // fail and do not try again.
3338 jniThrowException(env, "java/lang/IllegalArgumentException",
3339 String8::format("Failed to initialize %s, error %#x (NAME_NOT_FOUND)", tmp, err));
3340 env->ReleaseStringUTFChars(name, tmp);
3341 return;
3342 }
3343 if (err == NO_MEMORY) {
3344 throwCodecException(env, err, ACTION_CODE_TRANSIENT,
3345 String8::format("Failed to initialize %s, error %#x (NO_MEMORY)", tmp, err));
3346 env->ReleaseStringUTFChars(name, tmp);
3347 return;
3348 }
3349 if (err == PERMISSION_DENIED) {
3350 jniThrowException(env, "java/lang/SecurityException",
3351 String8::format("Failed to initialize %s, error %#x (PERMISSION_DENIED)", tmp,
3352 err));
3353 env->ReleaseStringUTFChars(name, tmp);
3354 return;
3355 }
3356 if (err != OK) {
3357 // believed possible to try again
3358 jniThrowException(env, "java/io/IOException",
3359 String8::format("Failed to find matching codec %s, error %#x (?)", tmp, err));
3360 env->ReleaseStringUTFChars(name, tmp);
3361 return;
3362 }
3363
3364 env->ReleaseStringUTFChars(name, tmp);
3365
3366 codec->registerSelf();
3367
3368 setMediaCodec(env, thiz, codec);
3369 }
3370
3371 static void android_media_MediaCodec_native_finalize(
3372 JNIEnv *env, jobject thiz) {
3373 setMediaCodec(env, thiz, NULL);
3374 }
3375
3376 // MediaCodec.LinearBlock
3377
3378 static jobject android_media_MediaCodec_LinearBlock_native_map(
3379 JNIEnv *env, jobject thiz) {
3380 JMediaCodecLinearBlock *context =
3381 (JMediaCodecLinearBlock *)env->GetLongField(thiz, gLinearBlockInfo.contextId);
3382 if (context->mBuffer) {
3383 std::shared_ptr<C2Buffer> buffer = context->mBuffer;
3384 if (!context->mReadonlyMapping) {
3385 const C2BufferData data = buffer->data();
3386 if (data.type() != C2BufferData::LINEAR) {
3387 throwExceptionAsNecessary(
3388 env, INVALID_OPERATION, ACTION_CODE_FATAL,
3389 "Underlying buffer is not a linear buffer");
3390 return nullptr;
3391 }
3392 if (data.linearBlocks().size() != 1u) {
3393 throwExceptionAsNecessary(
3394 env, INVALID_OPERATION, ACTION_CODE_FATAL,
3395 "Underlying buffer contains more than one block");
3396 return nullptr;
3397 }
3398 C2ConstLinearBlock block = data.linearBlocks().front();
3399 context->mReadonlyMapping =
3400 std::make_shared<C2ReadView>(block.map().get());
3401 }
3402 return CreateByteBuffer(
3403 env,
3404 context->mReadonlyMapping->data(), // base
3405 context->mReadonlyMapping->capacity(), // capacity
3406 0u, // offset
3407 context->mReadonlyMapping->capacity(), // size
3408 true, // readOnly
3409 true /* clearBuffer */);
3410 } else if (context->mBlock) {
3411 std::shared_ptr<C2LinearBlock> block = context->mBlock;
3412 if (!context->mReadWriteMapping) {
3413 context->mReadWriteMapping =
3414 std::make_shared<C2WriteView>(block->map().get());
3415 }
3416 return CreateByteBuffer(
3417 env,
3418 context->mReadWriteMapping->base(),
3419 context->mReadWriteMapping->capacity(),
3420 context->mReadWriteMapping->offset(),
3421 context->mReadWriteMapping->size(),
3422 false, // readOnly
3423 true /* clearBuffer */);
3424 } else if (context->mLegacyBuffer) {
3425 return CreateByteBuffer(
3426 env,
3427 context->mLegacyBuffer->base(),
3428 context->mLegacyBuffer->capacity(),
3429 context->mLegacyBuffer->offset(),
3430 context->mLegacyBuffer->size(),
3431 true, // readOnly
3432 true /* clearBuffer */);
3433 } else if (context->mMemory) {
3434 return CreateByteBuffer(
3435 env,
3436 context->mMemory->unsecurePointer(),
3437 context->mMemory->size(),
3438 0,
3439 context->mMemory->size(),
3440 false, // readOnly
3441 true /* clearBuffer */);
3442 }
3443 throwExceptionAsNecessary(
3444 env, INVALID_OPERATION, ACTION_CODE_FATAL,
3445 "Underlying buffer is empty");
3446 return nullptr;
3447 }
3448
3449 static void android_media_MediaCodec_LinearBlock_native_recycle(
3450 JNIEnv *env, jobject thiz) {
3451 JMediaCodecLinearBlock *context =
3452 (JMediaCodecLinearBlock *)env->GetLongField(thiz, gLinearBlockInfo.contextId);
3453 env->CallVoidMethod(thiz, gLinearBlockInfo.setInternalStateId, jlong(0), false);
3454 delete context;
3455 }
3456
3457 static void PopulateNamesVector(
3458 JNIEnv *env, jobjectArray codecNames, std::vector<std::string> *names) {
3459 jsize length = env->GetArrayLength(codecNames);
3460 for (jsize i = 0; i < length; ++i) {
3461 jstring jstr = static_cast<jstring>(env->GetObjectArrayElement(codecNames, i));
3462 if (jstr == nullptr) {
3463 // null entries are ignored
3464 continue;
3465 }
3466 const char *cstr = env->GetStringUTFChars(jstr, nullptr);
3467 if (cstr == nullptr) {
3468 throwExceptionAsNecessary(
3469 env, BAD_VALUE, ACTION_CODE_FATAL,
3470 "Error converting Java string to native");
3471 return;
3472 }
3473 names->emplace_back(cstr);
3474 env->ReleaseStringUTFChars(jstr, cstr);
3475 }
3476 }
3477
3478 static void android_media_MediaCodec_LinearBlock_native_obtain(
3479 JNIEnv *env, jobject thiz, jint capacity, jobjectArray codecNames) {
3480 std::unique_ptr<JMediaCodecLinearBlock> context{new JMediaCodecLinearBlock};
3481 std::vector<std::string> names;
3482 PopulateNamesVector(env, codecNames, &names);
3483 bool hasSecure = false;
3484 bool hasNonSecure = false;
3485 for (const std::string &name : names) {
3486 if (name.length() >= 7 && name.substr(name.length() - 7) == ".secure") {
3487 hasSecure = true;
3488 } else {
3489 hasNonSecure = true;
3490 }
3491 }
3492 if (!obtain(context.get(), capacity, names, (hasSecure && !hasNonSecure) /* secure */)) {
3493 jniThrowException(env, "java/io/IOException", nullptr);
3494 return;
3495 }
3496 env->CallVoidMethod(
3497 thiz,
3498 gLinearBlockInfo.setInternalStateId,
3499 (jlong)context.release(),
3500 true /* isMappable */);
3501 }
3502
3503 static jboolean android_media_MediaCodec_LinearBlock_checkCompatible(
3504 JNIEnv *env, jclass, jobjectArray codecNames) {
3505 std::vector<std::string> names;
3506 PopulateNamesVector(env, codecNames, &names);
3507 bool isCompatible = false;
3508 bool hasSecure = false;
3509 bool hasNonSecure = false;
3510 for (const std::string &name : names) {
3511 if (name.length() >= 7 && name.substr(name.length() - 7) == ".secure") {
3512 hasSecure = true;
3513 } else {
3514 hasNonSecure = true;
3515 }
3516 }
3517 if (hasSecure && hasNonSecure) {
3518 return false;
3519 }
3520 status_t err = MediaCodec::CanFetchLinearBlock(names, &isCompatible);
3521 if (err != OK) {
3522 // TODO: CodecErrorLog
3523 throwExceptionAsNecessary(env, err);
3524 }
3525 return isCompatible;
3526 }
3527
3528 static const JNINativeMethod gMethods[] = {
3529 { "native_release", "()V", (void *)android_media_MediaCodec_release },
3530
3531 { "native_reset", "()V", (void *)android_media_MediaCodec_reset },
3532
3533 { "native_releasePersistentInputSurface",
3534 "(Landroid/view/Surface;)V",
3535 (void *)android_media_MediaCodec_releasePersistentInputSurface},
3536
3537 { "native_createPersistentInputSurface",
3538 "()Landroid/media/MediaCodec$PersistentSurface;",
3539 (void *)android_media_MediaCodec_createPersistentInputSurface },
3540
3541 { "native_setInputSurface", "(Landroid/view/Surface;)V",
3542 (void *)android_media_MediaCodec_setInputSurface },
3543
3544 { "native_enableOnFirstTunnelFrameReadyListener", "(Z)V",
3545 (void *)android_media_MediaCodec_native_enableOnFirstTunnelFrameReadyListener },
3546
3547 { "native_enableOnFrameRenderedListener", "(Z)V",
3548 (void *)android_media_MediaCodec_native_enableOnFrameRenderedListener },
3549
3550 { "native_setCallback",
3551 "(Landroid/media/MediaCodec$Callback;)V",
3552 (void *)android_media_MediaCodec_native_setCallback },
3553
3554 { "native_configure",
3555 "([Ljava/lang/String;[Ljava/lang/Object;Landroid/view/Surface;"
3556 "Landroid/media/MediaCrypto;Landroid/os/IHwBinder;I)V",
3557 (void *)android_media_MediaCodec_native_configure },
3558
3559 { "native_setSurface",
3560 "(Landroid/view/Surface;)V",
3561 (void *)android_media_MediaCodec_native_setSurface },
3562
3563 { "createInputSurface", "()Landroid/view/Surface;",
3564 (void *)android_media_MediaCodec_createInputSurface },
3565
3566 { "native_start", "()V", (void *)android_media_MediaCodec_start },
3567 { "native_stop", "()V", (void *)android_media_MediaCodec_stop },
3568 { "native_flush", "()V", (void *)android_media_MediaCodec_flush },
3569
3570 { "native_queueInputBuffer", "(IIIJI)V",
3571 (void *)android_media_MediaCodec_queueInputBuffer },
3572
3573 { "native_queueSecureInputBuffer", "(IILandroid/media/MediaCodec$CryptoInfo;JI)V",
3574 (void *)android_media_MediaCodec_queueSecureInputBuffer },
3575
3576 { "native_mapHardwareBuffer",
3577 "(Landroid/hardware/HardwareBuffer;)Landroid/media/Image;",
3578 (void *)android_media_MediaCodec_mapHardwareBuffer },
3579
3580 { "native_closeMediaImage", "(J)V", (void *)android_media_MediaCodec_closeMediaImage },
3581
3582 { "native_queueLinearBlock",
3583 "(ILandroid/media/MediaCodec$LinearBlock;IILandroid/media/MediaCodec$CryptoInfo;JI"
3584 "Ljava/util/ArrayList;Ljava/util/ArrayList;)V",
3585 (void *)android_media_MediaCodec_native_queueLinearBlock },
3586
3587 { "native_queueHardwareBuffer",
3588 "(ILandroid/hardware/HardwareBuffer;JILjava/util/ArrayList;Ljava/util/ArrayList;)V",
3589 (void *)android_media_MediaCodec_native_queueHardwareBuffer },
3590
3591 { "native_getOutputFrame",
3592 "(Landroid/media/MediaCodec$OutputFrame;I)V",
3593 (void *)android_media_MediaCodec_native_getOutputFrame },
3594
3595 { "native_dequeueInputBuffer", "(J)I",
3596 (void *)android_media_MediaCodec_dequeueInputBuffer },
3597
3598 { "native_dequeueOutputBuffer", "(Landroid/media/MediaCodec$BufferInfo;J)I",
3599 (void *)android_media_MediaCodec_dequeueOutputBuffer },
3600
3601 { "releaseOutputBuffer", "(IZZJ)V",
3602 (void *)android_media_MediaCodec_releaseOutputBuffer },
3603
3604 { "signalEndOfInputStream", "()V",
3605 (void *)android_media_MediaCodec_signalEndOfInputStream },
3606
3607 { "getFormatNative", "(Z)Ljava/util/Map;",
3608 (void *)android_media_MediaCodec_getFormatNative },
3609
3610 { "getOutputFormatNative", "(I)Ljava/util/Map;",
3611 (void *)android_media_MediaCodec_getOutputFormatForIndexNative },
3612
3613 { "getBuffers", "(Z)[Ljava/nio/ByteBuffer;",
3614 (void *)android_media_MediaCodec_getBuffers },
3615
3616 { "getBuffer", "(ZI)Ljava/nio/ByteBuffer;",
3617 (void *)android_media_MediaCodec_getBuffer },
3618
3619 { "getImage", "(ZI)Landroid/media/Image;",
3620 (void *)android_media_MediaCodec_getImage },
3621
3622 { "getCanonicalName", "()Ljava/lang/String;",
3623 (void *)android_media_MediaCodec_getName },
3624
3625 { "getOwnCodecInfo", "()Landroid/media/MediaCodecInfo;",
3626 (void *)android_media_MediaCodec_getOwnCodecInfo },
3627
3628 { "native_getMetrics", "()Landroid/os/PersistableBundle;",
3629 (void *)android_media_MediaCodec_native_getMetrics},
3630
3631 { "setParameters", "([Ljava/lang/String;[Ljava/lang/Object;)V",
3632 (void *)android_media_MediaCodec_setParameters },
3633
3634 { "setVideoScalingMode", "(I)V",
3635 (void *)android_media_MediaCodec_setVideoScalingMode },
3636
3637 { "native_setAudioPresentation", "(II)V",
3638 (void *)android_media_MediaCodec_setAudioPresentation },
3639
3640 { "native_getSupportedVendorParameters", "()Ljava/util/List;",
3641 (void *)android_media_MediaCodec_getSupportedVendorParameters },
3642
3643 { "native_getParameterDescriptor",
3644 "(Ljava/lang/String;)Landroid/media/MediaCodec$ParameterDescriptor;",
3645 (void *)android_media_MediaCodec_getParameterDescriptor },
3646
3647 { "native_subscribeToVendorParameters",
3648 "(Ljava/util/List;)V",
3649 (void *)android_media_MediaCodec_subscribeToVendorParameters},
3650
3651 { "native_unsubscribeFromVendorParameters",
3652 "(Ljava/util/List;)V",
3653 (void *)android_media_MediaCodec_unsubscribeFromVendorParameters},
3654
3655 { "native_init", "()V", (void *)android_media_MediaCodec_native_init },
3656
3657 { "native_setup", "(Ljava/lang/String;ZZII)V",
3658 (void *)android_media_MediaCodec_native_setup },
3659
3660 { "native_finalize", "()V",
3661 (void *)android_media_MediaCodec_native_finalize },
3662 };
3663
3664 static const JNINativeMethod gLinearBlockMethods[] = {
3665 { "native_map", "()Ljava/nio/ByteBuffer;",
3666 (void *)android_media_MediaCodec_LinearBlock_native_map },
3667
3668 { "native_recycle", "()V",
3669 (void *)android_media_MediaCodec_LinearBlock_native_recycle },
3670
3671 { "native_obtain", "(I[Ljava/lang/String;)V",
3672 (void *)android_media_MediaCodec_LinearBlock_native_obtain },
3673
3674 { "native_checkCompatible", "([Ljava/lang/String;)Z",
3675 (void *)android_media_MediaCodec_LinearBlock_checkCompatible },
3676 };
3677
3678 int register_android_media_MediaCodec(JNIEnv *env) {
3679 int result = AndroidRuntime::registerNativeMethods(env,
3680 "android/media/MediaCodec", gMethods, NELEM(gMethods));
3681 if (result != JNI_OK) {
3682 return result;
3683 }
3684 result = AndroidRuntime::registerNativeMethods(env,
3685 "android/media/MediaCodec$LinearBlock",
3686 gLinearBlockMethods,
3687 NELEM(gLinearBlockMethods));
3688 return result;
3689 }