ACodec.h revision 27c174483a8ae9688d5d4897c19074f62c7f1701
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 }; 40 41 ACodec(); 42 43 void setNotificationMessage(const sp<AMessage> &msg); 44 void initiateSetup(const sp<AMessage> &msg); 45 void signalFlush(); 46 void signalResume(); 47 void initiateShutdown(); 48 49protected: 50 virtual ~ACodec(); 51 52private: 53 struct BaseState; 54 struct UninitializedState; 55 struct LoadedToIdleState; 56 struct IdleToExecutingState; 57 struct ExecutingState; 58 struct OutputPortSettingsChangedState; 59 struct ExecutingToIdleState; 60 struct IdleToLoadedState; 61 struct ErrorState; 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<ErrorState> mErrorState; 106 sp<FlushingState> mFlushingState; 107 108 AString mComponentName; 109 sp<IOMX> mOMX; 110 IOMX::node_id mNode; 111 sp<MemoryDealer> mDealer[2]; 112 113 sp<ANativeWindow> mNativeWindow; 114 115 Vector<BufferInfo> mBuffers[2]; 116 bool mPortEOS[2]; 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 setMinBufferSize(OMX_U32 portIndex, size_t size); 155 156 status_t initNativeWindow(); 157 158 // Returns true iff all buffers on the given port have status OWNED_BY_US. 159 bool allYourBuffersAreBelongToUs(OMX_U32 portIndex); 160 161 bool allYourBuffersAreBelongToUs(); 162 163 void deferMessage(const sp<AMessage> &msg); 164 void processDeferredMessages(); 165 166 void sendFormatChange(); 167 168 DISALLOW_EVIL_CONSTRUCTORS(ACodec); 169}; 170 171} // namespace android 172 173#endif // A_CODEC_H_ 174