James Dong | 2f1f224 | 2011-03-17 11:02:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 17 | #ifndef A_CODEC_H_ |
| 18 | |
| 19 | #define A_CODEC_H_ |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | #include <android/native_window.h> |
| 23 | #include <media/IOMX.h> |
| 24 | #include <media/stagefright/foundation/AHierarchicalStateMachine.h> |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 25 | #include <OMX_Audio.h> |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | struct ABuffer; |
| 30 | struct MemoryDealer; |
| 31 | |
| 32 | struct ACodec : public AHierarchicalStateMachine { |
| 33 | enum { |
Andreas Huber | 687b32d | 2010-12-15 17:18:20 -0800 | [diff] [blame] | 34 | kWhatFillThisBuffer = 'fill', |
| 35 | kWhatDrainThisBuffer = 'drai', |
| 36 | kWhatEOS = 'eos ', |
| 37 | kWhatShutdownCompleted = 'scom', |
| 38 | kWhatFlushCompleted = 'fcom', |
| 39 | kWhatOutputFormatChanged = 'outC', |
Andreas Huber | d84fd79 | 2011-08-16 13:48:44 -0700 | [diff] [blame] | 40 | kWhatError = 'erro', |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 41 | kWhatComponentAllocated = 'cAll', |
| 42 | kWhatComponentConfigured = 'cCon', |
| 43 | kWhatBuffersAllocated = 'allc', |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | ACodec(); |
| 47 | |
| 48 | void setNotificationMessage(const sp<AMessage> &msg); |
| 49 | void initiateSetup(const sp<AMessage> &msg); |
| 50 | void signalFlush(); |
| 51 | void signalResume(); |
Andreas Huber | 4484bdd | 2012-02-28 15:54:51 -0800 | [diff] [blame] | 52 | void initiateShutdown(bool keepComponentAllocated = false); |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 53 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 54 | void initiateAllocateComponent(const sp<AMessage> &msg); |
| 55 | void initiateConfigureComponent(const sp<AMessage> &msg); |
| 56 | void initiateStart(); |
| 57 | |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 58 | protected: |
| 59 | virtual ~ACodec(); |
| 60 | |
| 61 | private: |
| 62 | struct BaseState; |
| 63 | struct UninitializedState; |
Andreas Huber | 4484bdd | 2012-02-28 15:54:51 -0800 | [diff] [blame] | 64 | struct LoadedState; |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 65 | struct LoadedToIdleState; |
| 66 | struct IdleToExecutingState; |
| 67 | struct ExecutingState; |
| 68 | struct OutputPortSettingsChangedState; |
| 69 | struct ExecutingToIdleState; |
| 70 | struct IdleToLoadedState; |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 71 | struct FlushingState; |
| 72 | |
| 73 | enum { |
| 74 | kWhatSetup = 'setu', |
| 75 | kWhatOMXMessage = 'omx ', |
| 76 | kWhatInputBufferFilled = 'inpF', |
| 77 | kWhatOutputBufferDrained = 'outD', |
| 78 | kWhatShutdown = 'shut', |
| 79 | kWhatFlush = 'flus', |
| 80 | kWhatResume = 'resm', |
| 81 | kWhatDrainDeferredMessages = 'drai', |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 82 | kWhatAllocateComponent = 'allo', |
| 83 | kWhatConfigureComponent = 'conf', |
| 84 | kWhatStart = 'star', |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | enum { |
| 88 | kPortIndexInput = 0, |
| 89 | kPortIndexOutput = 1 |
| 90 | }; |
| 91 | |
| 92 | struct BufferInfo { |
| 93 | enum Status { |
| 94 | OWNED_BY_US, |
| 95 | OWNED_BY_COMPONENT, |
| 96 | OWNED_BY_UPSTREAM, |
| 97 | OWNED_BY_DOWNSTREAM, |
| 98 | OWNED_BY_NATIVE_WINDOW, |
| 99 | }; |
| 100 | |
| 101 | IOMX::buffer_id mBufferID; |
| 102 | Status mStatus; |
| 103 | |
| 104 | sp<ABuffer> mData; |
| 105 | sp<GraphicBuffer> mGraphicBuffer; |
| 106 | }; |
| 107 | |
| 108 | sp<AMessage> mNotify; |
| 109 | |
| 110 | sp<UninitializedState> mUninitializedState; |
Andreas Huber | 4484bdd | 2012-02-28 15:54:51 -0800 | [diff] [blame] | 111 | sp<LoadedState> mLoadedState; |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 112 | sp<LoadedToIdleState> mLoadedToIdleState; |
| 113 | sp<IdleToExecutingState> mIdleToExecutingState; |
| 114 | sp<ExecutingState> mExecutingState; |
| 115 | sp<OutputPortSettingsChangedState> mOutputPortSettingsChangedState; |
| 116 | sp<ExecutingToIdleState> mExecutingToIdleState; |
| 117 | sp<IdleToLoadedState> mIdleToLoadedState; |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 118 | sp<FlushingState> mFlushingState; |
| 119 | |
| 120 | AString mComponentName; |
Andreas Huber | 3d3864f | 2012-02-29 15:47:17 -0800 | [diff] [blame^] | 121 | uint32_t mQuirks; |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 122 | sp<IOMX> mOMX; |
| 123 | IOMX::node_id mNode; |
| 124 | sp<MemoryDealer> mDealer[2]; |
| 125 | |
| 126 | sp<ANativeWindow> mNativeWindow; |
| 127 | |
| 128 | Vector<BufferInfo> mBuffers[2]; |
| 129 | bool mPortEOS[2]; |
Andreas Huber | 928baf1 | 2011-09-26 10:53:29 -0700 | [diff] [blame] | 130 | status_t mInputEOSResult; |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 131 | |
| 132 | List<sp<AMessage> > mDeferredQueue; |
| 133 | |
Andreas Huber | 7caa130 | 2011-01-10 10:38:31 -0800 | [diff] [blame] | 134 | bool mSentFormat; |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 135 | bool mIsEncoder; |
Andreas Huber | 7caa130 | 2011-01-10 10:38:31 -0800 | [diff] [blame] | 136 | |
Andreas Huber | 4484bdd | 2012-02-28 15:54:51 -0800 | [diff] [blame] | 137 | bool mShutdownInProgress; |
| 138 | |
| 139 | // If "mKeepComponentAllocated" we only transition back to Loaded state |
| 140 | // and do not release the component instance. |
| 141 | bool mKeepComponentAllocated; |
| 142 | |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 143 | status_t allocateBuffersOnPort(OMX_U32 portIndex); |
| 144 | status_t freeBuffersOnPort(OMX_U32 portIndex); |
| 145 | status_t freeBuffer(OMX_U32 portIndex, size_t i); |
| 146 | |
| 147 | status_t allocateOutputBuffersFromNativeWindow(); |
| 148 | status_t cancelBufferToNativeWindow(BufferInfo *info); |
Andreas Huber | 6760f98 | 2011-02-04 10:12:26 -0800 | [diff] [blame] | 149 | status_t freeOutputBuffersNotOwnedByComponent(); |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 150 | BufferInfo *dequeueBufferFromNativeWindow(); |
| 151 | |
| 152 | BufferInfo *findBufferByID( |
| 153 | uint32_t portIndex, IOMX::buffer_id bufferID, |
| 154 | ssize_t *index = NULL); |
| 155 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 156 | status_t setComponentRole(bool isEncoder, const char *mime); |
| 157 | status_t configureCodec(const char *mime, const sp<AMessage> &msg); |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 158 | |
| 159 | status_t setVideoPortFormatType( |
| 160 | OMX_U32 portIndex, |
| 161 | OMX_VIDEO_CODINGTYPE compressionFormat, |
| 162 | OMX_COLOR_FORMATTYPE colorFormat); |
| 163 | |
| 164 | status_t setSupportedOutputFormat(); |
| 165 | |
| 166 | status_t setupVideoDecoder( |
| 167 | const char *mime, int32_t width, int32_t height); |
| 168 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 169 | status_t setupVideoEncoder( |
| 170 | const char *mime, const sp<AMessage> &msg); |
| 171 | |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 172 | status_t setVideoFormatOnPort( |
| 173 | OMX_U32 portIndex, |
| 174 | int32_t width, int32_t height, |
| 175 | OMX_VIDEO_CODINGTYPE compressionFormat); |
| 176 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 177 | status_t setupAACCodec( |
| 178 | bool encoder, |
| 179 | int32_t numChannels, int32_t sampleRate, int32_t bitRate); |
| 180 | |
| 181 | status_t selectAudioPortFormat( |
| 182 | OMX_U32 portIndex, OMX_AUDIO_CODINGTYPE desiredFormat); |
| 183 | |
| 184 | status_t setupAMRCodec(bool encoder, bool isWAMR, int32_t bitRate); |
| 185 | status_t setupG711Codec(bool encoder, int32_t numChannels); |
Andreas Huber | 3e408f3 | 2011-09-28 12:37:36 -0700 | [diff] [blame] | 186 | |
| 187 | status_t setupRawAudioFormat( |
| 188 | OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels); |
| 189 | |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 190 | status_t setMinBufferSize(OMX_U32 portIndex, size_t size); |
| 191 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 192 | status_t setupMPEG4EncoderParameters(const sp<AMessage> &msg); |
| 193 | status_t setupH263EncoderParameters(const sp<AMessage> &msg); |
| 194 | status_t setupAVCEncoderParameters(const sp<AMessage> &msg); |
| 195 | |
| 196 | status_t verifySupportForProfileAndLevel(int32_t profile, int32_t level); |
| 197 | status_t configureBitrate(int32_t bitrate); |
| 198 | status_t setupErrorCorrectionParameters(); |
| 199 | |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 200 | status_t initNativeWindow(); |
| 201 | |
| 202 | // Returns true iff all buffers on the given port have status OWNED_BY_US. |
| 203 | bool allYourBuffersAreBelongToUs(OMX_U32 portIndex); |
| 204 | |
| 205 | bool allYourBuffersAreBelongToUs(); |
| 206 | |
Andreas Huber | d03e7d6 | 2011-11-28 10:54:12 -0800 | [diff] [blame] | 207 | size_t countBuffersOwnedByComponent(OMX_U32 portIndex) const; |
| 208 | |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 209 | void deferMessage(const sp<AMessage> &msg); |
| 210 | void processDeferredMessages(); |
| 211 | |
Andreas Huber | 7caa130 | 2011-01-10 10:38:31 -0800 | [diff] [blame] | 212 | void sendFormatChange(); |
| 213 | |
Andreas Huber | 88572f7 | 2012-02-21 11:47:18 -0800 | [diff] [blame] | 214 | void signalError( |
| 215 | OMX_ERRORTYPE error = OMX_ErrorUndefined, |
| 216 | status_t internalError = UNKNOWN_ERROR); |
Andreas Huber | d41108c | 2011-09-12 14:14:08 -0700 | [diff] [blame] | 217 | |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 218 | DISALLOW_EVIL_CONSTRUCTORS(ACodec); |
| 219 | }; |
| 220 | |
| 221 | } // namespace android |
| 222 | |
| 223 | #endif // A_CODEC_H_ |