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 <mutex> 21 22 #include "jni.h" 23 24 #include <C2Buffer.h> 25 #include <binder/MemoryHeapBase.h> 26 #include <media/MediaCodecBuffer.h> 27 #include <media/MediaMetricsItem.h> 28 #include <media/hardware/CryptoAPI.h> 29 #include <media/stagefright/foundation/ABase.h> 30 #include <media/stagefright/foundation/AHandler.h> 31 #include <utils/Errors.h> 32 33 class C2Buffer; 34 35 namespace android { 36 37 struct ABuffer; 38 struct ALooper; 39 struct AMessage; 40 struct AString; 41 struct ICrypto; 42 class IGraphicBufferProducer; 43 struct MediaCodec; 44 struct PersistentSurface; 45 class Surface; 46 namespace hardware { 47 class HidlMemory; 48 namespace cas { 49 namespace native { 50 namespace V1_0 { 51 struct IDescrambler; 52 }}}} 53 using hardware::cas::native::V1_0::IDescrambler; 54 55 struct JMediaCodec : public AHandler { 56 JMediaCodec( 57 JNIEnv *env, jobject thiz, 58 const char *name, bool nameIsType, bool encoder, int pid, int uid); 59 60 status_t initCheck() const; 61 62 void registerSelf(); 63 void release(); 64 void releaseAsync(); 65 66 status_t enableOnFirstTunnelFrameReadyListener(jboolean enable); 67 68 status_t enableOnFrameRenderedListener(jboolean enable); 69 70 status_t setCallback(jobject cb); 71 72 status_t configure( 73 const sp<AMessage> &format, 74 const sp<IGraphicBufferProducer> &bufferProducer, 75 const sp<ICrypto> &crypto, 76 const sp<IDescrambler> &descrambler, 77 int flags); 78 79 status_t setSurface( 80 const sp<IGraphicBufferProducer> &surface); 81 82 status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer); 83 status_t setInputSurface(const sp<PersistentSurface> &surface); 84 85 status_t start(); 86 status_t stop(); 87 status_t reset(); 88 89 status_t flush(); 90 91 status_t queueInputBuffer( 92 size_t index, 93 size_t offset, size_t size, int64_t timeUs, uint32_t flags, 94 AString *errorDetailMsg); 95 96 status_t queueSecureInputBuffer( 97 size_t index, 98 size_t offset, 99 const CryptoPlugin::SubSample *subSamples, 100 size_t numSubSamples, 101 const uint8_t key[16], 102 const uint8_t iv[16], 103 CryptoPlugin::Mode mode, 104 const CryptoPlugin::Pattern &pattern, 105 int64_t presentationTimeUs, 106 uint32_t flags, 107 AString *errorDetailMsg); 108 109 status_t queueBuffer( 110 size_t index, const std::shared_ptr<C2Buffer> &buffer, 111 int64_t timeUs, uint32_t flags, const sp<AMessage> &tunings, 112 AString *errorDetailMsg); 113 114 status_t queueEncryptedLinearBlock( 115 size_t index, 116 const sp<hardware::HidlMemory> &buffer, 117 size_t offset, 118 const CryptoPlugin::SubSample *subSamples, 119 size_t numSubSamples, 120 const uint8_t key[16], 121 const uint8_t iv[16], 122 CryptoPlugin::Mode mode, 123 const CryptoPlugin::Pattern &pattern, 124 int64_t presentationTimeUs, 125 uint32_t flags, 126 const sp<AMessage> &tunings, 127 AString *errorDetailMsg); 128 129 status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs); 130 131 status_t dequeueOutputBuffer( 132 JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs); 133 134 status_t releaseOutputBuffer( 135 size_t index, bool render, bool updatePTS, int64_t timestampNs); 136 137 status_t signalEndOfInputStream(); 138 139 status_t getFormat(JNIEnv *env, bool input, jobject *format) const; 140 141 status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const; 142 143 status_t getBuffers( 144 JNIEnv *env, bool input, jobjectArray *bufArray) const; 145 146 status_t getBuffer( 147 JNIEnv *env, bool input, size_t index, jobject *buf) const; 148 149 status_t getImage( 150 JNIEnv *env, bool input, size_t index, jobject *image) const; 151 152 status_t getOutputFrame( 153 JNIEnv *env, jobject frame, size_t index) const; 154 155 status_t getName(JNIEnv *env, jstring *name) const; 156 157 status_t getCodecInfo(JNIEnv *env, jobject *codecInfo) const; 158 159 status_t getMetrics(JNIEnv *env, mediametrics::Item * &reply) const; 160 161 status_t setParameters(const sp<AMessage> ¶ms); 162 163 void setVideoScalingMode(int mode); 164 165 void selectAudioPresentation(const int32_t presentationId, const int32_t programId); 166 167 status_t querySupportedVendorParameters(JNIEnv *env, jobject *names); 168 169 status_t describeParameter(JNIEnv *env, jstring name, jobject *desc); 170 171 status_t subscribeToVendorParameters(JNIEnv *env, jobject names); 172 173 status_t unsubscribeFromVendorParameters(JNIEnv *env, jobject names); 174 hasCryptoOrDescramblerJMediaCodec175 bool hasCryptoOrDescrambler() { return mHasCryptoOrDescrambler; } 176 getCryptoJMediaCodec177 const sp<ICrypto> &getCrypto() { return mCrypto; } 178 179 std::string getExceptionMessage(const char *msg) const; 180 181 protected: 182 virtual ~JMediaCodec(); 183 184 virtual void onMessageReceived(const sp<AMessage> &msg); 185 186 private: 187 enum { 188 kWhatCallbackNotify, 189 kWhatFrameRendered, 190 kWhatAsyncReleaseComplete, 191 kWhatFirstTunnelFrameReady, 192 }; 193 194 jclass mClass; 195 jweak mObject; 196 sp<Surface> mSurfaceTextureClient; 197 198 sp<ALooper> mLooper; 199 sp<MediaCodec> mCodec; 200 AString mNameAtCreation; 201 bool mGraphicOutput{false}; 202 bool mHasCryptoOrDescrambler{false}; 203 std::once_flag mReleaseFlag; 204 std::once_flag mAsyncReleaseFlag; 205 206 sp<AMessage> mCallbackNotification; 207 sp<AMessage> mOnFirstTunnelFrameReadyNotification; 208 sp<AMessage> mOnFrameRenderedNotification; 209 210 status_t mInitStatus; 211 212 sp<ICrypto> mCrypto; 213 214 template <typename T> 215 status_t createByteBufferFromABuffer( 216 JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer, 217 jobject *buf) const; 218 219 void handleCallback(const sp<AMessage> &msg); 220 void handleFirstTunnelFrameReadyNotification(const sp<AMessage> &msg); 221 void handleFrameRenderedNotification(const sp<AMessage> &msg); 222 223 DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec); 224 }; 225 226 } // namespace android 227 228 #endif // _ANDROID_MEDIA_MEDIACODEC_H_ 229