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