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/ISurfaceTexture.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 SurfaceTextureClient; 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<SurfaceTextureClient> &nativeWindow, 56 const sp<ICrypto> &crypto, 57 uint32_t flags); 58 59 status_t start(); 60 61 // Returns to a state in which the component remains allocated but 62 // unconfigured. 63 status_t stop(); 64 65 // Client MUST call release before releasing final reference to this 66 // object. 67 status_t release(); 68 69 status_t flush(); 70 71 status_t queueInputBuffer( 72 size_t index, 73 size_t offset, 74 size_t size, 75 int64_t presentationTimeUs, 76 uint32_t flags, 77 AString *errorDetailMsg = NULL); 78 79 status_t queueSecureInputBuffer( 80 size_t index, 81 size_t offset, 82 const CryptoPlugin::SubSample *subSamples, 83 size_t numSubSamples, 84 const uint8_t key[16], 85 const uint8_t iv[16], 86 CryptoPlugin::Mode mode, 87 int64_t presentationTimeUs, 88 uint32_t flags, 89 AString *errorDetailMsg = NULL); 90 91 status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs = 0ll); 92 93 status_t dequeueOutputBuffer( 94 size_t *index, 95 size_t *offset, 96 size_t *size, 97 int64_t *presentationTimeUs, 98 uint32_t *flags, 99 int64_t timeoutUs = 0ll); 100 101 status_t renderOutputBufferAndRelease(size_t index); 102 status_t releaseOutputBuffer(size_t index); 103 104 status_t getOutputFormat(sp<AMessage> *format) const; 105 106 status_t getInputBuffers(Vector<sp<ABuffer> > *buffers) const; 107 status_t getOutputBuffers(Vector<sp<ABuffer> > *buffers) const; 108 109 protected: 110 virtual ~MediaCodec(); 111 virtual void onMessageReceived(const sp<AMessage> &msg); 112 113 private: 114 enum State { 115 UNINITIALIZED, 116 INITIALIZING, 117 INITIALIZED, 118 CONFIGURING, 119 CONFIGURED, 120 STARTING, 121 STARTED, 122 FLUSHING, 123 STOPPING, 124 RELEASING, 125 }; 126 127 enum { 128 kPortIndexInput = 0, 129 kPortIndexOutput = 1, 130 }; 131 132 enum { 133 kWhatInit = 'init', 134 kWhatConfigure = 'conf', 135 kWhatStart = 'strt', 136 kWhatStop = 'stop', 137 kWhatRelease = 'rele', 138 kWhatDequeueInputBuffer = 'deqI', 139 kWhatQueueInputBuffer = 'queI', 140 kWhatDequeueOutputBuffer = 'deqO', 141 kWhatReleaseOutputBuffer = 'relO', 142 kWhatGetBuffers = 'getB', 143 kWhatFlush = 'flus', 144 kWhatGetOutputFormat = 'getO', 145 kWhatDequeueInputTimedOut = 'dITO', 146 kWhatDequeueOutputTimedOut = 'dOTO', 147 kWhatCodecNotify = 'codc', 148 }; 149 150 enum { 151 kFlagIsSoftwareCodec = 1, 152 kFlagOutputFormatChanged = 2, 153 kFlagOutputBuffersChanged = 4, 154 kFlagStickyError = 8, 155 kFlagDequeueInputPending = 16, 156 kFlagDequeueOutputPending = 32, 157 kFlagIsSecure = 64, 158 }; 159 160 struct BufferInfo { 161 void *mBufferID; 162 sp<ABuffer> mData; 163 sp<ABuffer> mEncryptedData; 164 sp<AMessage> mNotify; 165 bool mOwnedByClient; 166 }; 167 168 State mState; 169 sp<ALooper> mLooper; 170 sp<ALooper> mCodecLooper; 171 sp<ACodec> mCodec; 172 uint32_t mReplyID; 173 uint32_t mFlags; 174 sp<SurfaceTextureClient> mNativeWindow; 175 SoftwareRenderer *mSoftRenderer; 176 sp<AMessage> mOutputFormat; 177 178 List<size_t> mAvailPortBuffers[2]; 179 Vector<BufferInfo> mPortBuffers[2]; 180 181 int32_t mDequeueInputTimeoutGeneration; 182 uint32_t mDequeueInputReplyID; 183 184 int32_t mDequeueOutputTimeoutGeneration; 185 uint32_t mDequeueOutputReplyID; 186 187 sp<ICrypto> mCrypto; 188 189 List<sp<ABuffer> > mCSD; 190 191 MediaCodec(const sp<ALooper> &looper); 192 193 static status_t PostAndAwaitResponse( 194 const sp<AMessage> &msg, sp<AMessage> *response); 195 196 status_t init(const char *name, bool nameIsType, bool encoder); 197 198 void setState(State newState); 199 void returnBuffersToCodec(); 200 void returnBuffersToCodecOnPort(int32_t portIndex); 201 size_t updateBuffers(int32_t portIndex, const sp<AMessage> &msg); 202 status_t onQueueInputBuffer(const sp<AMessage> &msg); 203 status_t onReleaseOutputBuffer(const sp<AMessage> &msg); 204 ssize_t dequeuePortBuffer(int32_t portIndex); 205 206 bool handleDequeueInputBuffer(uint32_t replyID, bool newRequest = false); 207 bool handleDequeueOutputBuffer(uint32_t replyID, bool newRequest = false); 208 void cancelPendingDequeueOperations(); 209 210 void extractCSD(const sp<AMessage> &format); 211 status_t queueCSDInputBuffer(size_t bufferIndex); 212 213 status_t setNativeWindow( 214 const sp<SurfaceTextureClient> &surfaceTextureClient); 215 216 DISALLOW_EVIL_CONSTRUCTORS(MediaCodec); 217 }; 218 219 } // namespace android 220 221 #endif // MEDIA_CODEC_H_ 222