ACodec.h revision 35213f1420c669f43314cb75eadea450d21a75cb
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 <media/stagefright/SkipCutBuffer.h> 26#include <OMX_Audio.h> 27 28#define TRACK_BUFFER_TIMING 0 29 30namespace android { 31 32struct ABuffer; 33struct MemoryDealer; 34 35struct ACodec : public AHierarchicalStateMachine { 36 enum { 37 kWhatFillThisBuffer = 'fill', 38 kWhatDrainThisBuffer = 'drai', 39 kWhatEOS = 'eos ', 40 kWhatShutdownCompleted = 'scom', 41 kWhatFlushCompleted = 'fcom', 42 kWhatOutputFormatChanged = 'outC', 43 kWhatError = 'erro', 44 kWhatComponentAllocated = 'cAll', 45 kWhatComponentConfigured = 'cCon', 46 kWhatBuffersAllocated = 'allc', 47 }; 48 49 ACodec(); 50 51 void setNotificationMessage(const sp<AMessage> &msg); 52 void initiateSetup(const sp<AMessage> &msg); 53 void signalFlush(); 54 void signalResume(); 55 void initiateShutdown(bool keepComponentAllocated = false); 56 57 void initiateAllocateComponent(const sp<AMessage> &msg); 58 void initiateConfigureComponent(const sp<AMessage> &msg); 59 void initiateStart(); 60 61 struct PortDescription : public RefBase { 62 size_t countBuffers(); 63 IOMX::buffer_id bufferIDAt(size_t index) const; 64 sp<ABuffer> bufferAt(size_t index) const; 65 66 private: 67 friend struct ACodec; 68 69 Vector<IOMX::buffer_id> mBufferIDs; 70 Vector<sp<ABuffer> > mBuffers; 71 72 PortDescription(); 73 void addBuffer(IOMX::buffer_id id, const sp<ABuffer> &buffer); 74 75 DISALLOW_EVIL_CONSTRUCTORS(PortDescription); 76 }; 77 78protected: 79 virtual ~ACodec(); 80 81private: 82 struct BaseState; 83 struct UninitializedState; 84 struct LoadedState; 85 struct LoadedToIdleState; 86 struct IdleToExecutingState; 87 struct ExecutingState; 88 struct OutputPortSettingsChangedState; 89 struct ExecutingToIdleState; 90 struct IdleToLoadedState; 91 struct FlushingState; 92 93 enum { 94 kWhatSetup = 'setu', 95 kWhatOMXMessage = 'omx ', 96 kWhatInputBufferFilled = 'inpF', 97 kWhatOutputBufferDrained = 'outD', 98 kWhatShutdown = 'shut', 99 kWhatFlush = 'flus', 100 kWhatResume = 'resm', 101 kWhatDrainDeferredMessages = 'drai', 102 kWhatAllocateComponent = 'allo', 103 kWhatConfigureComponent = 'conf', 104 kWhatStart = 'star', 105 }; 106 107 enum { 108 kPortIndexInput = 0, 109 kPortIndexOutput = 1 110 }; 111 112 enum { 113 kFlagIsSecure = 1, 114 }; 115 116 struct BufferInfo { 117 enum Status { 118 OWNED_BY_US, 119 OWNED_BY_COMPONENT, 120 OWNED_BY_UPSTREAM, 121 OWNED_BY_DOWNSTREAM, 122 OWNED_BY_NATIVE_WINDOW, 123 }; 124 125 IOMX::buffer_id mBufferID; 126 Status mStatus; 127 128 sp<ABuffer> mData; 129 sp<GraphicBuffer> mGraphicBuffer; 130 }; 131 132#if TRACK_BUFFER_TIMING 133 struct BufferStats { 134 int64_t mEmptyBufferTimeUs; 135 int64_t mFillBufferDoneTimeUs; 136 }; 137 138 KeyedVector<int64_t, BufferStats> mBufferStats; 139#endif 140 141 sp<AMessage> mNotify; 142 143 sp<UninitializedState> mUninitializedState; 144 sp<LoadedState> mLoadedState; 145 sp<LoadedToIdleState> mLoadedToIdleState; 146 sp<IdleToExecutingState> mIdleToExecutingState; 147 sp<ExecutingState> mExecutingState; 148 sp<OutputPortSettingsChangedState> mOutputPortSettingsChangedState; 149 sp<ExecutingToIdleState> mExecutingToIdleState; 150 sp<IdleToLoadedState> mIdleToLoadedState; 151 sp<FlushingState> mFlushingState; 152 sp<SkipCutBuffer> mSkipCutBuffer; 153 154 AString mComponentName; 155 uint32_t mFlags; 156 uint32_t mQuirks; 157 sp<IOMX> mOMX; 158 IOMX::node_id mNode; 159 sp<MemoryDealer> mDealer[2]; 160 161 sp<ANativeWindow> mNativeWindow; 162 163 Vector<BufferInfo> mBuffers[2]; 164 bool mPortEOS[2]; 165 status_t mInputEOSResult; 166 167 List<sp<AMessage> > mDeferredQueue; 168 169 bool mSentFormat; 170 bool mIsEncoder; 171 172 bool mShutdownInProgress; 173 174 // If "mKeepComponentAllocated" we only transition back to Loaded state 175 // and do not release the component instance. 176 bool mKeepComponentAllocated; 177 178 int32_t mEncoderDelay; 179 int32_t mEncoderPadding; 180 181 bool mChannelMaskPresent; 182 int32_t mChannelMask; 183 184 status_t allocateBuffersOnPort(OMX_U32 portIndex); 185 status_t freeBuffersOnPort(OMX_U32 portIndex); 186 status_t freeBuffer(OMX_U32 portIndex, size_t i); 187 188 status_t allocateOutputBuffersFromNativeWindow(); 189 status_t cancelBufferToNativeWindow(BufferInfo *info); 190 status_t freeOutputBuffersNotOwnedByComponent(); 191 BufferInfo *dequeueBufferFromNativeWindow(); 192 193 BufferInfo *findBufferByID( 194 uint32_t portIndex, IOMX::buffer_id bufferID, 195 ssize_t *index = NULL); 196 197 status_t setComponentRole(bool isEncoder, const char *mime); 198 status_t configureCodec(const char *mime, const sp<AMessage> &msg); 199 200 status_t setVideoPortFormatType( 201 OMX_U32 portIndex, 202 OMX_VIDEO_CODINGTYPE compressionFormat, 203 OMX_COLOR_FORMATTYPE colorFormat); 204 205 status_t setSupportedOutputFormat(); 206 207 status_t setupVideoDecoder( 208 const char *mime, int32_t width, int32_t height); 209 210 status_t setupVideoEncoder( 211 const char *mime, const sp<AMessage> &msg); 212 213 status_t setVideoFormatOnPort( 214 OMX_U32 portIndex, 215 int32_t width, int32_t height, 216 OMX_VIDEO_CODINGTYPE compressionFormat); 217 218 status_t setupAACCodec( 219 bool encoder, 220 int32_t numChannels, int32_t sampleRate, int32_t bitRate, 221 int32_t aacProfile, bool isADTS); 222 223 status_t selectAudioPortFormat( 224 OMX_U32 portIndex, OMX_AUDIO_CODINGTYPE desiredFormat); 225 226 status_t setupAMRCodec(bool encoder, bool isWAMR, int32_t bitRate); 227 status_t setupG711Codec(bool encoder, int32_t numChannels); 228 229 status_t setupFlacCodec( 230 bool encoder, int32_t numChannels, int32_t sampleRate, int32_t compressionLevel); 231 232 status_t setupRawAudioFormat( 233 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels); 234 235 status_t setMinBufferSize(OMX_U32 portIndex, size_t size); 236 237 status_t setupMPEG4EncoderParameters(const sp<AMessage> &msg); 238 status_t setupH263EncoderParameters(const sp<AMessage> &msg); 239 status_t setupAVCEncoderParameters(const sp<AMessage> &msg); 240 241 status_t verifySupportForProfileAndLevel(int32_t profile, int32_t level); 242 status_t configureBitrate(int32_t bitrate); 243 status_t setupErrorCorrectionParameters(); 244 245 status_t initNativeWindow(); 246 247 status_t pushBlankBuffersToNativeWindow(); 248 249 // Returns true iff all buffers on the given port have status OWNED_BY_US. 250 bool allYourBuffersAreBelongToUs(OMX_U32 portIndex); 251 252 bool allYourBuffersAreBelongToUs(); 253 254 size_t countBuffersOwnedByComponent(OMX_U32 portIndex) const; 255 256 void deferMessage(const sp<AMessage> &msg); 257 void processDeferredMessages(); 258 259 void sendFormatChange(); 260 261 void signalError( 262 OMX_ERRORTYPE error = OMX_ErrorUndefined, 263 status_t internalError = UNKNOWN_ERROR); 264 265 DISALLOW_EVIL_CONSTRUCTORS(ACodec); 266}; 267 268} // namespace android 269 270#endif // A_CODEC_H_ 271