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