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 MEDIA_CODEC_H_ 18 19 #define MEDIA_CODEC_H_ 20 21 #include <gui/IGraphicBufferProducer.h> 22 #include <media/hardware/CryptoAPI.h> 23 #include <media/stagefright/foundation/AHandler.h> 24 #include <utils/Vector.h> 25 26 namespace android { 27 28 struct ABuffer; 29 struct ACodec; 30 struct AMessage; 31 struct AString; 32 struct ICrypto; 33 struct SoftwareRenderer; 34 struct Surface; 35 36 struct MediaCodec : public AHandler { 37 enum ConfigureFlags { 38 CONFIGURE_FLAG_ENCODE = 1, 39 }; 40 41 enum BufferFlags { 42 BUFFER_FLAG_SYNCFRAME = 1, 43 BUFFER_FLAG_CODECCONFIG = 2, 44 BUFFER_FLAG_EOS = 4, 45 }; 46 47 static sp<MediaCodec> CreateByType( 48 const sp<ALooper> &looper, const char *mime, bool encoder); 49 50 static sp<MediaCodec> CreateByComponentName( 51 const sp<ALooper> &looper, const char *name); 52 53 status_t configure( 54 const sp<AMessage> &format, 55 const sp<Surface> &nativeWindow, 56 const sp<ICrypto> &crypto, 57 uint32_t flags); 58 59 status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer); 60 61 status_t start(); 62 63 // Returns to a state in which the component remains allocated but 64 // unconfigured. 65 status_t stop(); 66 67 // Client MUST call release before releasing final reference to this 68 // object. 69 status_t release(); 70 71 status_t flush(); 72 73 status_t queueInputBuffer( 74 size_t index, 75 size_t offset, 76 size_t size, 77 int64_t presentationTimeUs, 78 uint32_t flags, 79 AString *errorDetailMsg = NULL); 80 81 status_t queueSecureInputBuffer( 82 size_t index, 83 size_t offset, 84 const CryptoPlugin::SubSample *subSamples, 85 size_t numSubSamples, 86 const uint8_t key[16], 87 const uint8_t iv[16], 88 CryptoPlugin::Mode mode, 89 int64_t presentationTimeUs, 90 uint32_t flags, 91 AString *errorDetailMsg = NULL); 92 93 status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs = 0ll); 94 95 status_t dequeueOutputBuffer( 96 size_t *index, 97 size_t *offset, 98 size_t *size, 99 int64_t *presentationTimeUs, 100 uint32_t *flags, 101 int64_t timeoutUs = 0ll); 102 103 status_t renderOutputBufferAndRelease(size_t index); 104 status_t releaseOutputBuffer(size_t index); 105 106 status_t signalEndOfInputStream(); 107 108 status_t getOutputFormat(sp<AMessage> *format) const; 109 110 status_t getInputBuffers(Vector<sp<ABuffer> > *buffers) const; 111 status_t getOutputBuffers(Vector<sp<ABuffer> > *buffers) const; 112 113 status_t requestIDRFrame(); 114 115 // Notification will be posted once there "is something to do", i.e. 116 // an input/output buffer has become available, a format change is 117 // pending, an error is pending. 118 void requestActivityNotification(const sp<AMessage> ¬ify); 119 120 status_t getName(AString *componentName) const; 121 122 status_t setParameters(const sp<AMessage> ¶ms); 123 124 protected: 125 virtual ~MediaCodec(); 126 virtual void onMessageReceived(const sp<AMessage> &msg); 127 128 private: 129 enum State { 130 UNINITIALIZED, 131 INITIALIZING, 132 INITIALIZED, 133 CONFIGURING, 134 CONFIGURED, 135 STARTING, 136 STARTED, 137 FLUSHING, 138 STOPPING, 139 RELEASING, 140 }; 141 142 enum { 143 kPortIndexInput = 0, 144 kPortIndexOutput = 1, 145 }; 146 147 enum { 148 kWhatInit = 'init', 149 kWhatConfigure = 'conf', 150 kWhatCreateInputSurface = 'cisf', 151 kWhatStart = 'strt', 152 kWhatStop = 'stop', 153 kWhatRelease = 'rele', 154 kWhatDequeueInputBuffer = 'deqI', 155 kWhatQueueInputBuffer = 'queI', 156 kWhatDequeueOutputBuffer = 'deqO', 157 kWhatReleaseOutputBuffer = 'relO', 158 kWhatSignalEndOfInputStream = 'eois', 159 kWhatGetBuffers = 'getB', 160 kWhatFlush = 'flus', 161 kWhatGetOutputFormat = 'getO', 162 kWhatDequeueInputTimedOut = 'dITO', 163 kWhatDequeueOutputTimedOut = 'dOTO', 164 kWhatCodecNotify = 'codc', 165 kWhatRequestIDRFrame = 'ridr', 166 kWhatRequestActivityNotification = 'racN', 167 kWhatGetName = 'getN', 168 kWhatSetParameters = 'setP', 169 }; 170 171 enum { 172 kFlagIsSoftwareCodec = 1, 173 kFlagOutputFormatChanged = 2, 174 kFlagOutputBuffersChanged = 4, 175 kFlagStickyError = 8, 176 kFlagDequeueInputPending = 16, 177 kFlagDequeueOutputPending = 32, 178 kFlagIsSecure = 64, 179 kFlagSawMediaServerDie = 128, 180 kFlagIsEncoder = 256, 181 kFlagGatherCodecSpecificData = 512, 182 }; 183 184 struct BufferInfo { 185 void *mBufferID; 186 sp<ABuffer> mData; 187 sp<ABuffer> mEncryptedData; 188 sp<AMessage> mNotify; 189 bool mOwnedByClient; 190 }; 191 192 State mState; 193 sp<ALooper> mLooper; 194 sp<ALooper> mCodecLooper; 195 sp<ACodec> mCodec; 196 AString mComponentName; 197 uint32_t mReplyID; 198 uint32_t mFlags; 199 sp<Surface> mNativeWindow; 200 SoftwareRenderer *mSoftRenderer; 201 sp<AMessage> mOutputFormat; 202 203 List<size_t> mAvailPortBuffers[2]; 204 Vector<BufferInfo> mPortBuffers[2]; 205 206 int32_t mDequeueInputTimeoutGeneration; 207 uint32_t mDequeueInputReplyID; 208 209 int32_t mDequeueOutputTimeoutGeneration; 210 uint32_t mDequeueOutputReplyID; 211 212 sp<ICrypto> mCrypto; 213 214 List<sp<ABuffer> > mCSD; 215 216 sp<AMessage> mActivityNotify; 217 218 bool mHaveInputSurface; 219 220 MediaCodec(const sp<ALooper> &looper); 221 222 static status_t PostAndAwaitResponse( 223 const sp<AMessage> &msg, sp<AMessage> *response); 224 225 status_t init(const char *name, bool nameIsType, bool encoder); 226 227 void setState(State newState); 228 void returnBuffersToCodec(); 229 void returnBuffersToCodecOnPort(int32_t portIndex); 230 size_t updateBuffers(int32_t portIndex, const sp<AMessage> &msg); 231 status_t onQueueInputBuffer(const sp<AMessage> &msg); 232 status_t onReleaseOutputBuffer(const sp<AMessage> &msg); 233 ssize_t dequeuePortBuffer(int32_t portIndex); 234 235 bool handleDequeueInputBuffer(uint32_t replyID, bool newRequest = false); 236 bool handleDequeueOutputBuffer(uint32_t replyID, bool newRequest = false); 237 void cancelPendingDequeueOperations(); 238 239 void extractCSD(const sp<AMessage> &format); 240 status_t queueCSDInputBuffer(size_t bufferIndex); 241 242 status_t setNativeWindow( 243 const sp<Surface> &surface); 244 245 void postActivityNotificationIfPossible(); 246 247 status_t onSetParameters(const sp<AMessage> ¶ms); 248 249 status_t amendOutputFormatWithCodecSpecificData(const sp<ABuffer> &buffer); 250 251 DISALLOW_EVIL_CONSTRUCTORS(MediaCodec); 252 }; 253 254 } // namespace android 255 256 #endif // MEDIA_CODEC_H_ 257