ACodec.h revision c95c2ddcdfc974f42408a377fbe2de51b94a8c94
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    struct BufferInfo {
93        enum Status {
94            OWNED_BY_US,
95            OWNED_BY_COMPONENT,
96            OWNED_BY_UPSTREAM,
97            OWNED_BY_DOWNSTREAM,
98            OWNED_BY_NATIVE_WINDOW,
99        };
100
101        IOMX::buffer_id mBufferID;
102        Status mStatus;
103
104        sp<ABuffer> mData;
105        sp<GraphicBuffer> mGraphicBuffer;
106    };
107
108    sp<AMessage> mNotify;
109
110    sp<UninitializedState> mUninitializedState;
111    sp<LoadedState> mLoadedState;
112    sp<LoadedToIdleState> mLoadedToIdleState;
113    sp<IdleToExecutingState> mIdleToExecutingState;
114    sp<ExecutingState> mExecutingState;
115    sp<OutputPortSettingsChangedState> mOutputPortSettingsChangedState;
116    sp<ExecutingToIdleState> mExecutingToIdleState;
117    sp<IdleToLoadedState> mIdleToLoadedState;
118    sp<FlushingState> mFlushingState;
119
120    AString mComponentName;
121    sp<IOMX> mOMX;
122    IOMX::node_id mNode;
123    sp<MemoryDealer> mDealer[2];
124
125    sp<ANativeWindow> mNativeWindow;
126
127    Vector<BufferInfo> mBuffers[2];
128    bool mPortEOS[2];
129    status_t mInputEOSResult;
130
131    List<sp<AMessage> > mDeferredQueue;
132
133    bool mSentFormat;
134    bool mIsEncoder;
135
136    bool mShutdownInProgress;
137
138    // If "mKeepComponentAllocated" we only transition back to Loaded state
139    // and do not release the component instance.
140    bool mKeepComponentAllocated;
141
142    status_t allocateBuffersOnPort(OMX_U32 portIndex);
143    status_t freeBuffersOnPort(OMX_U32 portIndex);
144    status_t freeBuffer(OMX_U32 portIndex, size_t i);
145
146    status_t allocateOutputBuffersFromNativeWindow();
147    status_t cancelBufferToNativeWindow(BufferInfo *info);
148    status_t freeOutputBuffersNotOwnedByComponent();
149    BufferInfo *dequeueBufferFromNativeWindow();
150
151    BufferInfo *findBufferByID(
152            uint32_t portIndex, IOMX::buffer_id bufferID,
153            ssize_t *index = NULL);
154
155    status_t setComponentRole(bool isEncoder, const char *mime);
156    status_t configureCodec(const char *mime, const sp<AMessage> &msg);
157
158    status_t setVideoPortFormatType(
159            OMX_U32 portIndex,
160            OMX_VIDEO_CODINGTYPE compressionFormat,
161            OMX_COLOR_FORMATTYPE colorFormat);
162
163    status_t setSupportedOutputFormat();
164
165    status_t setupVideoDecoder(
166            const char *mime, int32_t width, int32_t height);
167
168    status_t setupVideoEncoder(
169            const char *mime, const sp<AMessage> &msg);
170
171    status_t setVideoFormatOnPort(
172            OMX_U32 portIndex,
173            int32_t width, int32_t height,
174            OMX_VIDEO_CODINGTYPE compressionFormat);
175
176    status_t setupAACCodec(
177            bool encoder,
178            int32_t numChannels, int32_t sampleRate, int32_t bitRate);
179
180    status_t selectAudioPortFormat(
181            OMX_U32 portIndex, OMX_AUDIO_CODINGTYPE desiredFormat);
182
183    status_t setupAMRCodec(bool encoder, bool isWAMR, int32_t bitRate);
184    status_t setupG711Codec(bool encoder, int32_t numChannels);
185
186    status_t setupRawAudioFormat(
187            OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels);
188
189    status_t setMinBufferSize(OMX_U32 portIndex, size_t size);
190
191    status_t setupMPEG4EncoderParameters(const sp<AMessage> &msg);
192    status_t setupH263EncoderParameters(const sp<AMessage> &msg);
193    status_t setupAVCEncoderParameters(const sp<AMessage> &msg);
194
195    status_t verifySupportForProfileAndLevel(int32_t profile, int32_t level);
196    status_t configureBitrate(int32_t bitrate);
197    status_t setupErrorCorrectionParameters();
198
199    status_t initNativeWindow();
200
201    // Returns true iff all buffers on the given port have status OWNED_BY_US.
202    bool allYourBuffersAreBelongToUs(OMX_U32 portIndex);
203
204    bool allYourBuffersAreBelongToUs();
205
206    size_t countBuffersOwnedByComponent(OMX_U32 portIndex) const;
207
208    void deferMessage(const sp<AMessage> &msg);
209    void processDeferredMessages();
210
211    void sendFormatChange();
212
213    void signalError(
214            OMX_ERRORTYPE error = OMX_ErrorUndefined,
215            status_t internalError = UNKNOWN_ERROR);
216
217    DISALLOW_EVIL_CONSTRUCTORS(ACodec);
218};
219
220}  // namespace android
221
222#endif  // A_CODEC_H_
223