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