ACodec.h revision 8b71241ce7353731ab75322c46e090ee35014a33
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    int32_t mEncoderDelay;
125    int32_t mEncoderPadding;
126    sp<SkipCutBuffer> mSkipCutBuffer;
127
128    AString mComponentName;
129    uint32_t mFlags;
130    uint32_t mQuirks;
131    sp<IOMX> mOMX;
132    IOMX::node_id mNode;
133    sp<MemoryDealer> mDealer[2];
134
135    sp<ANativeWindow> mNativeWindow;
136
137    Vector<BufferInfo> mBuffers[2];
138    bool mPortEOS[2];
139    status_t mInputEOSResult;
140
141    List<sp<AMessage> > mDeferredQueue;
142
143    bool mSentFormat;
144    bool mIsEncoder;
145
146    bool mShutdownInProgress;
147
148    // If "mKeepComponentAllocated" we only transition back to Loaded state
149    // and do not release the component instance.
150    bool mKeepComponentAllocated;
151
152    status_t allocateBuffersOnPort(OMX_U32 portIndex);
153    status_t freeBuffersOnPort(OMX_U32 portIndex);
154    status_t freeBuffer(OMX_U32 portIndex, size_t i);
155
156    status_t allocateOutputBuffersFromNativeWindow();
157    status_t cancelBufferToNativeWindow(BufferInfo *info);
158    status_t freeOutputBuffersNotOwnedByComponent();
159    BufferInfo *dequeueBufferFromNativeWindow();
160
161    BufferInfo *findBufferByID(
162            uint32_t portIndex, IOMX::buffer_id bufferID,
163            ssize_t *index = NULL);
164
165    status_t setComponentRole(bool isEncoder, const char *mime);
166    status_t configureCodec(const char *mime, const sp<AMessage> &msg);
167
168    status_t setVideoPortFormatType(
169            OMX_U32 portIndex,
170            OMX_VIDEO_CODINGTYPE compressionFormat,
171            OMX_COLOR_FORMATTYPE colorFormat);
172
173    status_t setSupportedOutputFormat();
174
175    status_t setupVideoDecoder(
176            const char *mime, int32_t width, int32_t height);
177
178    status_t setupVideoEncoder(
179            const char *mime, const sp<AMessage> &msg);
180
181    status_t setVideoFormatOnPort(
182            OMX_U32 portIndex,
183            int32_t width, int32_t height,
184            OMX_VIDEO_CODINGTYPE compressionFormat);
185
186    status_t setupAACCodec(
187            bool encoder,
188            int32_t numChannels, int32_t sampleRate, int32_t bitRate,
189            int32_t aacProfile, bool isADTS);
190
191    status_t selectAudioPortFormat(
192            OMX_U32 portIndex, OMX_AUDIO_CODINGTYPE desiredFormat);
193
194    status_t setupAMRCodec(bool encoder, bool isWAMR, int32_t bitRate);
195    status_t setupG711Codec(bool encoder, int32_t numChannels);
196
197    status_t setupRawAudioFormat(
198            OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels);
199
200    status_t setMinBufferSize(OMX_U32 portIndex, size_t size);
201
202    status_t setupMPEG4EncoderParameters(const sp<AMessage> &msg);
203    status_t setupH263EncoderParameters(const sp<AMessage> &msg);
204    status_t setupAVCEncoderParameters(const sp<AMessage> &msg);
205
206    status_t verifySupportForProfileAndLevel(int32_t profile, int32_t level);
207    status_t configureBitrate(int32_t bitrate);
208    status_t setupErrorCorrectionParameters();
209
210    status_t initNativeWindow();
211
212    // Returns true iff all buffers on the given port have status OWNED_BY_US.
213    bool allYourBuffersAreBelongToUs(OMX_U32 portIndex);
214
215    bool allYourBuffersAreBelongToUs();
216
217    size_t countBuffersOwnedByComponent(OMX_U32 portIndex) const;
218
219    void deferMessage(const sp<AMessage> &msg);
220    void processDeferredMessages();
221
222    void sendFormatChange();
223
224    void signalError(
225            OMX_ERRORTYPE error = OMX_ErrorUndefined,
226            status_t internalError = UNKNOWN_ERROR);
227
228    DISALLOW_EVIL_CONSTRUCTORS(ACodec);
229};
230
231}  // namespace android
232
233#endif  // A_CODEC_H_
234