MediaCodec.h revision 6507d14c6d10f93d390de62b9eed267f9b544985
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 26namespace android { 27 28struct ABuffer; 29struct ACodec; 30struct AMessage; 31struct AString; 32struct ICrypto; 33struct SoftwareRenderer; 34struct Surface; 35 36struct 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 124protected: 125 virtual ~MediaCodec(); 126 virtual void onMessageReceived(const sp<AMessage> &msg); 127 128private: 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 }; 180 181 struct BufferInfo { 182 void *mBufferID; 183 sp<ABuffer> mData; 184 sp<ABuffer> mEncryptedData; 185 sp<AMessage> mNotify; 186 bool mOwnedByClient; 187 }; 188 189 State mState; 190 sp<ALooper> mLooper; 191 sp<ALooper> mCodecLooper; 192 sp<ACodec> mCodec; 193 AString mComponentName; 194 uint32_t mReplyID; 195 uint32_t mFlags; 196 sp<Surface> mNativeWindow; 197 SoftwareRenderer *mSoftRenderer; 198 sp<AMessage> mOutputFormat; 199 200 List<size_t> mAvailPortBuffers[2]; 201 Vector<BufferInfo> mPortBuffers[2]; 202 203 int32_t mDequeueInputTimeoutGeneration; 204 uint32_t mDequeueInputReplyID; 205 206 int32_t mDequeueOutputTimeoutGeneration; 207 uint32_t mDequeueOutputReplyID; 208 209 sp<ICrypto> mCrypto; 210 211 List<sp<ABuffer> > mCSD; 212 213 sp<AMessage> mActivityNotify; 214 215 bool mHaveInputSurface; 216 217 MediaCodec(const sp<ALooper> &looper); 218 219 static status_t PostAndAwaitResponse( 220 const sp<AMessage> &msg, sp<AMessage> *response); 221 222 status_t init(const char *name, bool nameIsType, bool encoder); 223 224 void setState(State newState); 225 void returnBuffersToCodec(); 226 void returnBuffersToCodecOnPort(int32_t portIndex); 227 size_t updateBuffers(int32_t portIndex, const sp<AMessage> &msg); 228 status_t onQueueInputBuffer(const sp<AMessage> &msg); 229 status_t onReleaseOutputBuffer(const sp<AMessage> &msg); 230 ssize_t dequeuePortBuffer(int32_t portIndex); 231 232 bool handleDequeueInputBuffer(uint32_t replyID, bool newRequest = false); 233 bool handleDequeueOutputBuffer(uint32_t replyID, bool newRequest = false); 234 void cancelPendingDequeueOperations(); 235 236 void extractCSD(const sp<AMessage> &format); 237 status_t queueCSDInputBuffer(size_t bufferIndex); 238 239 status_t setNativeWindow( 240 const sp<Surface> &surface); 241 242 void postActivityNotificationIfPossible(); 243 244 status_t onSetParameters(const sp<AMessage> ¶ms); 245 246 DISALLOW_EVIL_CONSTRUCTORS(MediaCodec); 247}; 248 249} // namespace android 250 251#endif // MEDIA_CODEC_H_ 252