ACodec.h revision 5778822d86b0337407514b9372562b86edfa91cd
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();
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 LoadedToIdleState;
65    struct IdleToExecutingState;
66    struct ExecutingState;
67    struct OutputPortSettingsChangedState;
68    struct ExecutingToIdleState;
69    struct IdleToLoadedState;
70    struct FlushingState;
71
72    enum {
73        kWhatSetup                   = 'setu',
74        kWhatOMXMessage              = 'omx ',
75        kWhatInputBufferFilled       = 'inpF',
76        kWhatOutputBufferDrained     = 'outD',
77        kWhatShutdown                = 'shut',
78        kWhatFlush                   = 'flus',
79        kWhatResume                  = 'resm',
80        kWhatDrainDeferredMessages   = 'drai',
81        kWhatAllocateComponent       = 'allo',
82        kWhatConfigureComponent      = 'conf',
83        kWhatStart                   = 'star',
84    };
85
86    enum {
87        kPortIndexInput  = 0,
88        kPortIndexOutput = 1
89    };
90
91    struct BufferInfo {
92        enum Status {
93            OWNED_BY_US,
94            OWNED_BY_COMPONENT,
95            OWNED_BY_UPSTREAM,
96            OWNED_BY_DOWNSTREAM,
97            OWNED_BY_NATIVE_WINDOW,
98        };
99
100        IOMX::buffer_id mBufferID;
101        Status mStatus;
102
103        sp<ABuffer> mData;
104        sp<GraphicBuffer> mGraphicBuffer;
105    };
106
107    sp<AMessage> mNotify;
108
109    sp<UninitializedState> mUninitializedState;
110    sp<LoadedToIdleState> mLoadedToIdleState;
111    sp<IdleToExecutingState> mIdleToExecutingState;
112    sp<ExecutingState> mExecutingState;
113    sp<OutputPortSettingsChangedState> mOutputPortSettingsChangedState;
114    sp<ExecutingToIdleState> mExecutingToIdleState;
115    sp<IdleToLoadedState> mIdleToLoadedState;
116    sp<FlushingState> mFlushingState;
117
118    AString mComponentName;
119    sp<IOMX> mOMX;
120    IOMX::node_id mNode;
121    sp<MemoryDealer> mDealer[2];
122
123    sp<ANativeWindow> mNativeWindow;
124
125    Vector<BufferInfo> mBuffers[2];
126    bool mPortEOS[2];
127    status_t mInputEOSResult;
128
129    List<sp<AMessage> > mDeferredQueue;
130
131    bool mSentFormat;
132    bool mIsEncoder;
133
134    status_t allocateBuffersOnPort(OMX_U32 portIndex);
135    status_t freeBuffersOnPort(OMX_U32 portIndex);
136    status_t freeBuffer(OMX_U32 portIndex, size_t i);
137
138    status_t allocateOutputBuffersFromNativeWindow();
139    status_t cancelBufferToNativeWindow(BufferInfo *info);
140    status_t freeOutputBuffersNotOwnedByComponent();
141    BufferInfo *dequeueBufferFromNativeWindow();
142
143    BufferInfo *findBufferByID(
144            uint32_t portIndex, IOMX::buffer_id bufferID,
145            ssize_t *index = NULL);
146
147    status_t setComponentRole(bool isEncoder, const char *mime);
148    status_t configureCodec(const char *mime, const sp<AMessage> &msg);
149
150    status_t setVideoPortFormatType(
151            OMX_U32 portIndex,
152            OMX_VIDEO_CODINGTYPE compressionFormat,
153            OMX_COLOR_FORMATTYPE colorFormat);
154
155    status_t setSupportedOutputFormat();
156
157    status_t setupVideoDecoder(
158            const char *mime, int32_t width, int32_t height);
159
160    status_t setupVideoEncoder(
161            const char *mime, const sp<AMessage> &msg);
162
163    status_t setVideoFormatOnPort(
164            OMX_U32 portIndex,
165            int32_t width, int32_t height,
166            OMX_VIDEO_CODINGTYPE compressionFormat);
167
168    status_t setupAACCodec(
169            bool encoder,
170            int32_t numChannels, int32_t sampleRate, int32_t bitRate);
171
172    status_t selectAudioPortFormat(
173            OMX_U32 portIndex, OMX_AUDIO_CODINGTYPE desiredFormat);
174
175    status_t setupAMRCodec(bool encoder, bool isWAMR, int32_t bitRate);
176    status_t setupG711Codec(bool encoder, int32_t numChannels);
177
178    status_t setupRawAudioFormat(
179            OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels);
180
181    status_t setMinBufferSize(OMX_U32 portIndex, size_t size);
182
183    status_t setupMPEG4EncoderParameters(const sp<AMessage> &msg);
184    status_t setupH263EncoderParameters(const sp<AMessage> &msg);
185    status_t setupAVCEncoderParameters(const sp<AMessage> &msg);
186
187    status_t verifySupportForProfileAndLevel(int32_t profile, int32_t level);
188    status_t configureBitrate(int32_t bitrate);
189    status_t setupErrorCorrectionParameters();
190
191    status_t initNativeWindow();
192
193    // Returns true iff all buffers on the given port have status OWNED_BY_US.
194    bool allYourBuffersAreBelongToUs(OMX_U32 portIndex);
195
196    bool allYourBuffersAreBelongToUs();
197
198    size_t countBuffersOwnedByComponent(OMX_U32 portIndex) const;
199
200    void deferMessage(const sp<AMessage> &msg);
201    void processDeferredMessages();
202
203    void sendFormatChange();
204
205    void signalError(
206            OMX_ERRORTYPE error = OMX_ErrorUndefined,
207            status_t internalError = UNKNOWN_ERROR);
208
209    DISALLOW_EVIL_CONSTRUCTORS(ACodec);
210};
211
212}  // namespace android
213
214#endif  // A_CODEC_H_
215