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