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 #ifndef _ANDROID_MEDIA_MEDIACODEC_H_ 18 #define _ANDROID_MEDIA_MEDIACODEC_H_ 19 20 #include "jni.h" 21 22 #include <media/MediaAnalyticsItem.h> 23 #include <media/hardware/CryptoAPI.h> 24 #include <media/stagefright/foundation/ABase.h> 25 #include <media/stagefright/foundation/AHandler.h> 26 #include <utils/Errors.h> 27 28 namespace android { 29 30 struct ABuffer; 31 struct ALooper; 32 struct AMessage; 33 struct AString; 34 struct ICrypto; 35 class IGraphicBufferProducer; 36 struct MediaCodec; 37 struct PersistentSurface; 38 class Surface; 39 namespace hardware { 40 namespace cas { 41 namespace native { 42 namespace V1_0 { 43 struct IDescrambler; 44 }}}} 45 using hardware::cas::native::V1_0::IDescrambler; 46 47 struct JMediaCodec : public AHandler { 48 JMediaCodec( 49 JNIEnv *env, jobject thiz, 50 const char *name, bool nameIsType, bool encoder); 51 52 status_t initCheck() const; 53 54 void registerSelf(); 55 void release(); 56 57 status_t enableOnFrameRenderedListener(jboolean enable); 58 59 status_t setCallback(jobject cb); 60 61 status_t configure( 62 const sp<AMessage> &format, 63 const sp<IGraphicBufferProducer> &bufferProducer, 64 const sp<ICrypto> &crypto, 65 const sp<IDescrambler> &descrambler, 66 int flags); 67 68 status_t setSurface( 69 const sp<IGraphicBufferProducer> &surface); 70 71 status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer); 72 status_t setInputSurface(const sp<PersistentSurface> &surface); 73 74 status_t start(); 75 status_t stop(); 76 status_t reset(); 77 78 status_t flush(); 79 80 status_t queueInputBuffer( 81 size_t index, 82 size_t offset, size_t size, int64_t timeUs, uint32_t flags, 83 AString *errorDetailMsg); 84 85 status_t queueSecureInputBuffer( 86 size_t index, 87 size_t offset, 88 const CryptoPlugin::SubSample *subSamples, 89 size_t numSubSamples, 90 const uint8_t key[16], 91 const uint8_t iv[16], 92 CryptoPlugin::Mode mode, 93 const CryptoPlugin::Pattern &pattern, 94 int64_t presentationTimeUs, 95 uint32_t flags, 96 AString *errorDetailMsg); 97 98 status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs); 99 100 status_t dequeueOutputBuffer( 101 JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs); 102 103 status_t releaseOutputBuffer( 104 size_t index, bool render, bool updatePTS, int64_t timestampNs); 105 106 status_t signalEndOfInputStream(); 107 108 status_t getFormat(JNIEnv *env, bool input, jobject *format) const; 109 110 status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const; 111 112 status_t getBuffers( 113 JNIEnv *env, bool input, jobjectArray *bufArray) const; 114 115 status_t getBuffer( 116 JNIEnv *env, bool input, size_t index, jobject *buf) const; 117 118 status_t getImage( 119 JNIEnv *env, bool input, size_t index, jobject *image) const; 120 121 status_t getName(JNIEnv *env, jstring *name) const; 122 123 status_t getMetrics(JNIEnv *env, MediaAnalyticsItem * &reply) const; 124 125 status_t setParameters(const sp<AMessage> ¶ms); 126 127 void setVideoScalingMode(int mode); 128 129 protected: 130 virtual ~JMediaCodec(); 131 132 virtual void onMessageReceived(const sp<AMessage> &msg); 133 134 private: 135 enum { 136 kWhatCallbackNotify, 137 kWhatFrameRendered, 138 }; 139 140 jclass mClass; 141 jweak mObject; 142 sp<Surface> mSurfaceTextureClient; 143 144 // java objects cached 145 jclass mByteBufferClass; 146 jobject mNativeByteOrderObj; 147 jmethodID mByteBufferOrderMethodID; 148 jmethodID mByteBufferPositionMethodID; 149 jmethodID mByteBufferLimitMethodID; 150 jmethodID mByteBufferAsReadOnlyBufferMethodID; 151 152 sp<ALooper> mLooper; 153 sp<MediaCodec> mCodec; 154 155 sp<AMessage> mCallbackNotification; 156 sp<AMessage> mOnFrameRenderedNotification; 157 158 status_t mInitStatus; 159 160 template <typename T> 161 status_t createByteBufferFromABuffer( 162 JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer, 163 jobject *buf) const; 164 165 void cacheJavaObjects(JNIEnv *env); 166 void deleteJavaObjects(JNIEnv *env); 167 void handleCallback(const sp<AMessage> &msg); 168 void handleFrameRenderedNotification(const sp<AMessage> &msg); 169 170 DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec); 171 }; 172 173 } // namespace android 174 175 #endif // _ANDROID_MEDIA_MEDIACODEC_H_ 176