ACodec.h revision 729de186450f78c099637e1fce743fe531862c52
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 26namespace android { 27 28struct ABuffer; 29struct MemoryDealer; 30 31struct ACodec : public AHierarchicalStateMachine { 32 enum { 33 kWhatFillThisBuffer = 'fill', 34 kWhatDrainThisBuffer = 'drai', 35 kWhatEOS = 'eos ', 36 kWhatShutdownCompleted = 'scom', 37 kWhatFlushCompleted = 'fcom', 38 kWhatOutputFormatChanged = 'outC', 39 kWhatError = 'erro', 40 }; 41 42 ACodec(); 43 44 void setNotificationMessage(const sp<AMessage> &msg); 45 void initiateSetup(const sp<AMessage> &msg); 46 void signalFlush(); 47 void signalResume(); 48 void initiateShutdown(); 49 50protected: 51 virtual ~ACodec(); 52 53private: 54 struct BaseState; 55 struct UninitializedState; 56 struct LoadedToIdleState; 57 struct IdleToExecutingState; 58 struct ExecutingState; 59 struct OutputPortSettingsChangedState; 60 struct ExecutingToIdleState; 61 struct IdleToLoadedState; 62 struct FlushingState; 63 64 enum { 65 kWhatSetup = 'setu', 66 kWhatOMXMessage = 'omx ', 67 kWhatInputBufferFilled = 'inpF', 68 kWhatOutputBufferDrained = 'outD', 69 kWhatShutdown = 'shut', 70 kWhatFlush = 'flus', 71 kWhatResume = 'resm', 72 kWhatDrainDeferredMessages = 'drai', 73 }; 74 75 enum { 76 kPortIndexInput = 0, 77 kPortIndexOutput = 1 78 }; 79 80 struct BufferInfo { 81 enum Status { 82 OWNED_BY_US, 83 OWNED_BY_COMPONENT, 84 OWNED_BY_UPSTREAM, 85 OWNED_BY_DOWNSTREAM, 86 OWNED_BY_NATIVE_WINDOW, 87 }; 88 89 IOMX::buffer_id mBufferID; 90 Status mStatus; 91 92 sp<ABuffer> mData; 93 sp<GraphicBuffer> mGraphicBuffer; 94 }; 95 96 sp<AMessage> mNotify; 97 98 sp<UninitializedState> mUninitializedState; 99 sp<LoadedToIdleState> mLoadedToIdleState; 100 sp<IdleToExecutingState> mIdleToExecutingState; 101 sp<ExecutingState> mExecutingState; 102 sp<OutputPortSettingsChangedState> mOutputPortSettingsChangedState; 103 sp<ExecutingToIdleState> mExecutingToIdleState; 104 sp<IdleToLoadedState> mIdleToLoadedState; 105 sp<FlushingState> mFlushingState; 106 107 AString mComponentName; 108 sp<IOMX> mOMX; 109 IOMX::node_id mNode; 110 sp<MemoryDealer> mDealer[2]; 111 112 sp<ANativeWindow> mNativeWindow; 113 114 Vector<BufferInfo> mBuffers[2]; 115 bool mPortEOS[2]; 116 status_t mInputEOSResult; 117 118 List<sp<AMessage> > mDeferredQueue; 119 120 bool mSentFormat; 121 122 status_t allocateBuffersOnPort(OMX_U32 portIndex); 123 status_t freeBuffersOnPort(OMX_U32 portIndex); 124 status_t freeBuffer(OMX_U32 portIndex, size_t i); 125 126 status_t allocateOutputBuffersFromNativeWindow(); 127 status_t cancelBufferToNativeWindow(BufferInfo *info); 128 status_t freeOutputBuffersNotOwnedByComponent(); 129 BufferInfo *dequeueBufferFromNativeWindow(); 130 131 BufferInfo *findBufferByID( 132 uint32_t portIndex, IOMX::buffer_id bufferID, 133 ssize_t *index = NULL); 134 135 void setComponentRole(bool isEncoder, const char *mime); 136 void configureCodec(const char *mime, const sp<AMessage> &msg); 137 138 status_t setVideoPortFormatType( 139 OMX_U32 portIndex, 140 OMX_VIDEO_CODINGTYPE compressionFormat, 141 OMX_COLOR_FORMATTYPE colorFormat); 142 143 status_t setSupportedOutputFormat(); 144 145 status_t setupVideoDecoder( 146 const char *mime, int32_t width, int32_t height); 147 148 status_t setVideoFormatOnPort( 149 OMX_U32 portIndex, 150 int32_t width, int32_t height, 151 OMX_VIDEO_CODINGTYPE compressionFormat); 152 153 status_t setupAACDecoder(int32_t numChannels, int32_t sampleRate); 154 status_t setupAMRDecoder(bool isWAMR); 155 status_t setupG711Decoder(int32_t numChannels); 156 157 status_t setupRawAudioFormat( 158 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels); 159 160 status_t setMinBufferSize(OMX_U32 portIndex, size_t size); 161 162 status_t initNativeWindow(); 163 164 // Returns true iff all buffers on the given port have status OWNED_BY_US. 165 bool allYourBuffersAreBelongToUs(OMX_U32 portIndex); 166 167 bool allYourBuffersAreBelongToUs(); 168 169 void deferMessage(const sp<AMessage> &msg); 170 void processDeferredMessages(); 171 172 void sendFormatChange(); 173 174 void signalError(OMX_ERRORTYPE error = OMX_ErrorUndefined); 175 176 DISALLOW_EVIL_CONSTRUCTORS(ACodec); 177}; 178 179} // namespace android 180 181#endif // A_CODEC_H_ 182