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