ACodec.h revision afc16d667afa23f5aa00154ccad62f8c45cf5419
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 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> 25#include <OMX_Audio.h> 26 27namespace android { 28 29struct ABuffer; 30struct MemoryDealer; 31 32struct ACodec : public AHierarchicalStateMachine { 33 enum { 34 kWhatFillThisBuffer = 'fill', 35 kWhatDrainThisBuffer = 'drai', 36 kWhatEOS = 'eos ', 37 kWhatShutdownCompleted = 'scom', 38 kWhatFlushCompleted = 'fcom', 39 kWhatOutputFormatChanged = 'outC', 40 kWhatError = 'erro', 41 kWhatComponentAllocated = 'cAll', 42 kWhatComponentConfigured = 'cCon', 43 kWhatBuffersAllocated = 'allc', 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(); 52 void initiateShutdown(bool keepComponentAllocated = false); 53 54 void initiateAllocateComponent(const sp<AMessage> &msg); 55 void initiateConfigureComponent(const sp<AMessage> &msg); 56 void initiateStart(); 57 58protected: 59 virtual ~ACodec(); 60 61private: 62 struct BaseState; 63 struct UninitializedState; 64 struct LoadedState; 65 struct LoadedToIdleState; 66 struct IdleToExecutingState; 67 struct ExecutingState; 68 struct OutputPortSettingsChangedState; 69 struct ExecutingToIdleState; 70 struct IdleToLoadedState; 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', 82 kWhatAllocateComponent = 'allo', 83 kWhatConfigureComponent = 'conf', 84 kWhatStart = 'star', 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; 111 sp<LoadedState> mLoadedState; 112 sp<LoadedToIdleState> mLoadedToIdleState; 113 sp<IdleToExecutingState> mIdleToExecutingState; 114 sp<ExecutingState> mExecutingState; 115 sp<OutputPortSettingsChangedState> mOutputPortSettingsChangedState; 116 sp<ExecutingToIdleState> mExecutingToIdleState; 117 sp<IdleToLoadedState> mIdleToLoadedState; 118 sp<FlushingState> mFlushingState; 119 120 AString mComponentName; 121 uint32_t mQuirks; 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]; 130 status_t mInputEOSResult; 131 132 List<sp<AMessage> > mDeferredQueue; 133 134 bool mSentFormat; 135 bool mIsEncoder; 136 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 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); 149 status_t freeOutputBuffersNotOwnedByComponent(); 150 BufferInfo *dequeueBufferFromNativeWindow(); 151 152 BufferInfo *findBufferByID( 153 uint32_t portIndex, IOMX::buffer_id bufferID, 154 ssize_t *index = NULL); 155 156 status_t setComponentRole(bool isEncoder, const char *mime); 157 status_t configureCodec(const char *mime, const sp<AMessage> &msg); 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 169 status_t setupVideoEncoder( 170 const char *mime, const sp<AMessage> &msg); 171 172 status_t setVideoFormatOnPort( 173 OMX_U32 portIndex, 174 int32_t width, int32_t height, 175 OMX_VIDEO_CODINGTYPE compressionFormat); 176 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); 186 187 status_t setupRawAudioFormat( 188 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels); 189 190 status_t setMinBufferSize(OMX_U32 portIndex, size_t size); 191 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 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 207 size_t countBuffersOwnedByComponent(OMX_U32 portIndex) const; 208 209 void deferMessage(const sp<AMessage> &msg); 210 void processDeferredMessages(); 211 212 void sendFormatChange(); 213 214 void signalError( 215 OMX_ERRORTYPE error = OMX_ErrorUndefined, 216 status_t internalError = UNKNOWN_ERROR); 217 218 DISALLOW_EVIL_CONSTRUCTORS(ACodec); 219}; 220 221} // namespace android 222 223#endif // A_CODEC_H_ 224