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