Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 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/stagefright/foundation/AHandler.h> |
| 23 | #include <utils/Vector.h> |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | struct ABuffer; |
| 28 | struct ACodec; |
| 29 | struct AMessage; |
| 30 | struct SoftwareRenderer; |
| 31 | struct SurfaceTextureClient; |
| 32 | |
| 33 | struct MediaCodec : public AHandler { |
| 34 | enum ConfigureFlags { |
| 35 | CONFIGURE_FLAG_ENCODE = 1, |
| 36 | }; |
| 37 | |
| 38 | enum BufferFlags { |
| 39 | BUFFER_FLAG_SYNCFRAME = 1, |
| 40 | BUFFER_FLAG_CODECCONFIG = 2, |
| 41 | BUFFER_FLAG_EOS = 4, |
| 42 | }; |
| 43 | |
| 44 | static sp<MediaCodec> CreateByType( |
| 45 | const sp<ALooper> &looper, const char *mime, bool encoder); |
| 46 | |
| 47 | static sp<MediaCodec> CreateByComponentName( |
| 48 | const sp<ALooper> &looper, const char *name); |
| 49 | |
| 50 | status_t configure( |
| 51 | const sp<AMessage> &format, |
| 52 | const sp<SurfaceTextureClient> &nativeWindow, |
| 53 | uint32_t flags); |
| 54 | |
| 55 | status_t start(); |
Andreas Huber | 4484bdd | 2012-02-28 15:54:51 -0800 | [diff] [blame] | 56 | |
| 57 | // Returns to a state in which the component remains allocated but |
| 58 | // unconfigured. |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 59 | status_t stop(); |
| 60 | |
Andreas Huber | 4484bdd | 2012-02-28 15:54:51 -0800 | [diff] [blame] | 61 | // Client MUST call release before releasing final reference to this |
| 62 | // object. |
| 63 | status_t release(); |
| 64 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 65 | status_t flush(); |
| 66 | |
| 67 | status_t queueInputBuffer( |
| 68 | size_t index, |
| 69 | size_t offset, |
| 70 | size_t size, |
| 71 | int64_t presentationTimeUs, |
| 72 | uint32_t flags); |
| 73 | |
| 74 | status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs = 0ll); |
| 75 | |
| 76 | status_t dequeueOutputBuffer( |
| 77 | size_t *index, |
| 78 | size_t *offset, |
| 79 | size_t *size, |
| 80 | int64_t *presentationTimeUs, |
| 81 | uint32_t *flags, |
| 82 | int64_t timeoutUs = 0ll); |
| 83 | |
| 84 | status_t renderOutputBufferAndRelease(size_t index); |
| 85 | status_t releaseOutputBuffer(size_t index); |
| 86 | |
| 87 | status_t getOutputFormat(sp<AMessage> *format) const; |
| 88 | |
| 89 | status_t getInputBuffers(Vector<sp<ABuffer> > *buffers) const; |
| 90 | status_t getOutputBuffers(Vector<sp<ABuffer> > *buffers) const; |
| 91 | |
| 92 | protected: |
| 93 | virtual ~MediaCodec(); |
| 94 | virtual void onMessageReceived(const sp<AMessage> &msg); |
| 95 | |
| 96 | private: |
| 97 | enum State { |
| 98 | UNINITIALIZED, |
| 99 | INITIALIZING, |
| 100 | INITIALIZED, |
| 101 | CONFIGURING, |
| 102 | CONFIGURED, |
| 103 | STARTING, |
| 104 | STARTED, |
| 105 | FLUSHING, |
| 106 | STOPPING, |
Andreas Huber | 4484bdd | 2012-02-28 15:54:51 -0800 | [diff] [blame] | 107 | RELEASING, |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | enum { |
| 111 | kPortIndexInput = 0, |
| 112 | kPortIndexOutput = 1, |
| 113 | }; |
| 114 | |
| 115 | enum { |
| 116 | kWhatInit = 'init', |
| 117 | kWhatConfigure = 'conf', |
| 118 | kWhatStart = 'strt', |
| 119 | kWhatStop = 'stop', |
Andreas Huber | 4484bdd | 2012-02-28 15:54:51 -0800 | [diff] [blame] | 120 | kWhatRelease = 'rele', |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 121 | kWhatDequeueInputBuffer = 'deqI', |
| 122 | kWhatQueueInputBuffer = 'queI', |
| 123 | kWhatDequeueOutputBuffer = 'deqO', |
| 124 | kWhatReleaseOutputBuffer = 'relO', |
| 125 | kWhatGetBuffers = 'getB', |
| 126 | kWhatFlush = 'flus', |
| 127 | kWhatGetOutputFormat = 'getO', |
| 128 | kWhatDequeueInputTimedOut = 'dITO', |
| 129 | kWhatDequeueOutputTimedOut = 'dOTO', |
| 130 | kWhatCodecNotify = 'codc', |
| 131 | }; |
| 132 | |
| 133 | enum { |
| 134 | kFlagIsSoftwareCodec = 1, |
| 135 | kFlagOutputFormatChanged = 2, |
| 136 | kFlagOutputBuffersChanged = 4, |
| 137 | kFlagStickyError = 8, |
| 138 | kFlagDequeueInputPending = 16, |
| 139 | kFlagDequeueOutputPending = 32, |
| 140 | }; |
| 141 | |
| 142 | struct BufferInfo { |
| 143 | void *mBufferID; |
| 144 | sp<ABuffer> mData; |
| 145 | sp<AMessage> mNotify; |
| 146 | bool mOwnedByClient; |
| 147 | }; |
| 148 | |
| 149 | State mState; |
| 150 | sp<ALooper> mLooper; |
| 151 | sp<ALooper> mCodecLooper; |
| 152 | sp<ACodec> mCodec; |
| 153 | uint32_t mReplyID; |
| 154 | uint32_t mFlags; |
| 155 | sp<SurfaceTextureClient> mNativeWindow; |
| 156 | SoftwareRenderer *mSoftRenderer; |
| 157 | sp<AMessage> mOutputFormat; |
| 158 | |
| 159 | List<size_t> mAvailPortBuffers[2]; |
| 160 | Vector<BufferInfo> mPortBuffers[2]; |
| 161 | |
| 162 | int32_t mDequeueInputTimeoutGeneration; |
| 163 | uint32_t mDequeueInputReplyID; |
| 164 | |
| 165 | int32_t mDequeueOutputTimeoutGeneration; |
| 166 | uint32_t mDequeueOutputReplyID; |
| 167 | |
| 168 | MediaCodec(const sp<ALooper> &looper); |
| 169 | |
| 170 | static status_t PostAndAwaitResponse( |
| 171 | const sp<AMessage> &msg, sp<AMessage> *response); |
| 172 | |
| 173 | status_t init(const char *name, bool nameIsType, bool encoder); |
| 174 | |
| 175 | void setState(State newState); |
| 176 | void returnBuffersToCodec(); |
| 177 | void returnBuffersToCodecOnPort(int32_t portIndex); |
| 178 | size_t updateBuffers(int32_t portIndex, const sp<AMessage> &msg); |
| 179 | status_t onQueueInputBuffer(const sp<AMessage> &msg); |
| 180 | status_t onReleaseOutputBuffer(const sp<AMessage> &msg); |
| 181 | ssize_t dequeuePortBuffer(int32_t portIndex); |
| 182 | |
| 183 | bool handleDequeueInputBuffer(uint32_t replyID, bool newRequest = false); |
| 184 | bool handleDequeueOutputBuffer(uint32_t replyID, bool newRequest = false); |
| 185 | void cancelPendingDequeueOperations(); |
| 186 | |
| 187 | DISALLOW_EVIL_CONSTRUCTORS(MediaCodec); |
| 188 | }; |
| 189 | |
| 190 | } // namespace android |
| 191 | |
| 192 | #endif // MEDIA_CODEC_H_ |