blob: fa1a416ebc992ed279fb241bebbcd227cf667d82 [file] [log] [blame]
James Dong2f1f2242011-03-17 11:02:04 -07001/*
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
Andreas Hubera1587462010-12-15 15:17:42 -080017#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>
Andreas Huber88572f72012-02-21 11:47:18 -080025#include <OMX_Audio.h>
Andreas Hubera1587462010-12-15 15:17:42 -080026
27namespace android {
28
29struct ABuffer;
30struct MemoryDealer;
31
32struct ACodec : public AHierarchicalStateMachine {
33 enum {
Andreas Huber687b32d2010-12-15 17:18:20 -080034 kWhatFillThisBuffer = 'fill',
35 kWhatDrainThisBuffer = 'drai',
36 kWhatEOS = 'eos ',
37 kWhatShutdownCompleted = 'scom',
38 kWhatFlushCompleted = 'fcom',
39 kWhatOutputFormatChanged = 'outC',
Andreas Huberd84fd792011-08-16 13:48:44 -070040 kWhatError = 'erro',
Andreas Huber88572f72012-02-21 11:47:18 -080041 kWhatComponentAllocated = 'cAll',
42 kWhatComponentConfigured = 'cCon',
43 kWhatBuffersAllocated = 'allc',
Andreas Hubera1587462010-12-15 15:17:42 -080044 };
45
46 ACodec();
47
48 void setNotificationMessage(const sp<AMessage> &msg);
49 void initiateSetup(const sp<AMessage> &msg);
50 void signalFlush();
51 void signalResume();
Andreas Huber4484bdd2012-02-28 15:54:51 -080052 void initiateShutdown(bool keepComponentAllocated = false);
Andreas Hubera1587462010-12-15 15:17:42 -080053
Andreas Huber88572f72012-02-21 11:47:18 -080054 void initiateAllocateComponent(const sp<AMessage> &msg);
55 void initiateConfigureComponent(const sp<AMessage> &msg);
56 void initiateStart();
57
Andreas Hubera1587462010-12-15 15:17:42 -080058protected:
59 virtual ~ACodec();
60
61private:
62 struct BaseState;
63 struct UninitializedState;
Andreas Huber4484bdd2012-02-28 15:54:51 -080064 struct LoadedState;
Andreas Hubera1587462010-12-15 15:17:42 -080065 struct LoadedToIdleState;
66 struct IdleToExecutingState;
67 struct ExecutingState;
68 struct OutputPortSettingsChangedState;
69 struct ExecutingToIdleState;
70 struct IdleToLoadedState;
Andreas Hubera1587462010-12-15 15:17:42 -080071 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',
Andreas Huber88572f72012-02-21 11:47:18 -080082 kWhatAllocateComponent = 'allo',
83 kWhatConfigureComponent = 'conf',
84 kWhatStart = 'star',
Andreas Hubera1587462010-12-15 15:17:42 -080085 };
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;
Andreas Huber4484bdd2012-02-28 15:54:51 -0800111 sp<LoadedState> mLoadedState;
Andreas Hubera1587462010-12-15 15:17:42 -0800112 sp<LoadedToIdleState> mLoadedToIdleState;
113 sp<IdleToExecutingState> mIdleToExecutingState;
114 sp<ExecutingState> mExecutingState;
115 sp<OutputPortSettingsChangedState> mOutputPortSettingsChangedState;
116 sp<ExecutingToIdleState> mExecutingToIdleState;
117 sp<IdleToLoadedState> mIdleToLoadedState;
Andreas Hubera1587462010-12-15 15:17:42 -0800118 sp<FlushingState> mFlushingState;
119
120 AString mComponentName;
Andreas Huber3d3864f2012-02-29 15:47:17 -0800121 uint32_t mQuirks;
Andreas Hubera1587462010-12-15 15:17:42 -0800122 sp<IOMX> mOMX;
123 IOMX::node_id mNode;
124 sp<MemoryDealer> mDealer[2];
125
126 sp<ANativeWindow> mNativeWindow;
127
128 Vector<BufferInfo> mBuffers[2];
129 bool mPortEOS[2];
Andreas Huber928baf12011-09-26 10:53:29 -0700130 status_t mInputEOSResult;
Andreas Hubera1587462010-12-15 15:17:42 -0800131
132 List<sp<AMessage> > mDeferredQueue;
133
Andreas Huber7caa1302011-01-10 10:38:31 -0800134 bool mSentFormat;
Andreas Huber88572f72012-02-21 11:47:18 -0800135 bool mIsEncoder;
Andreas Huber7caa1302011-01-10 10:38:31 -0800136
Andreas Huber4484bdd2012-02-28 15:54:51 -0800137 bool mShutdownInProgress;
138
139 // If "mKeepComponentAllocated" we only transition back to Loaded state
140 // and do not release the component instance.
141 bool mKeepComponentAllocated;
142
Andreas Hubera1587462010-12-15 15:17:42 -0800143 status_t allocateBuffersOnPort(OMX_U32 portIndex);
144 status_t freeBuffersOnPort(OMX_U32 portIndex);
145 status_t freeBuffer(OMX_U32 portIndex, size_t i);
146
147 status_t allocateOutputBuffersFromNativeWindow();
148 status_t cancelBufferToNativeWindow(BufferInfo *info);
Andreas Huber6760f982011-02-04 10:12:26 -0800149 status_t freeOutputBuffersNotOwnedByComponent();
Andreas Hubera1587462010-12-15 15:17:42 -0800150 BufferInfo *dequeueBufferFromNativeWindow();
151
152 BufferInfo *findBufferByID(
153 uint32_t portIndex, IOMX::buffer_id bufferID,
154 ssize_t *index = NULL);
155
Andreas Huber88572f72012-02-21 11:47:18 -0800156 status_t setComponentRole(bool isEncoder, const char *mime);
157 status_t configureCodec(const char *mime, const sp<AMessage> &msg);
Andreas Hubera1587462010-12-15 15:17:42 -0800158
159 status_t setVideoPortFormatType(
160 OMX_U32 portIndex,
161 OMX_VIDEO_CODINGTYPE compressionFormat,
162 OMX_COLOR_FORMATTYPE colorFormat);
163
164 status_t setSupportedOutputFormat();
165
166 status_t setupVideoDecoder(
167 const char *mime, int32_t width, int32_t height);
168
Andreas Huber88572f72012-02-21 11:47:18 -0800169 status_t setupVideoEncoder(
170 const char *mime, const sp<AMessage> &msg);
171
Andreas Hubera1587462010-12-15 15:17:42 -0800172 status_t setVideoFormatOnPort(
173 OMX_U32 portIndex,
174 int32_t width, int32_t height,
175 OMX_VIDEO_CODINGTYPE compressionFormat);
176
Andreas Huber88572f72012-02-21 11:47:18 -0800177 status_t setupAACCodec(
178 bool encoder,
179 int32_t numChannels, int32_t sampleRate, int32_t bitRate);
180
181 status_t selectAudioPortFormat(
182 OMX_U32 portIndex, OMX_AUDIO_CODINGTYPE desiredFormat);
183
184 status_t setupAMRCodec(bool encoder, bool isWAMR, int32_t bitRate);
185 status_t setupG711Codec(bool encoder, int32_t numChannels);
Andreas Huber3e408f32011-09-28 12:37:36 -0700186
187 status_t setupRawAudioFormat(
188 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels);
189
Andreas Hubera1587462010-12-15 15:17:42 -0800190 status_t setMinBufferSize(OMX_U32 portIndex, size_t size);
191
Andreas Huber88572f72012-02-21 11:47:18 -0800192 status_t setupMPEG4EncoderParameters(const sp<AMessage> &msg);
193 status_t setupH263EncoderParameters(const sp<AMessage> &msg);
194 status_t setupAVCEncoderParameters(const sp<AMessage> &msg);
195
196 status_t verifySupportForProfileAndLevel(int32_t profile, int32_t level);
197 status_t configureBitrate(int32_t bitrate);
198 status_t setupErrorCorrectionParameters();
199
Andreas Hubera1587462010-12-15 15:17:42 -0800200 status_t initNativeWindow();
201
202 // Returns true iff all buffers on the given port have status OWNED_BY_US.
203 bool allYourBuffersAreBelongToUs(OMX_U32 portIndex);
204
205 bool allYourBuffersAreBelongToUs();
206
Andreas Huberd03e7d62011-11-28 10:54:12 -0800207 size_t countBuffersOwnedByComponent(OMX_U32 portIndex) const;
208
Andreas Hubera1587462010-12-15 15:17:42 -0800209 void deferMessage(const sp<AMessage> &msg);
210 void processDeferredMessages();
211
Andreas Huber7caa1302011-01-10 10:38:31 -0800212 void sendFormatChange();
213
Andreas Huber88572f72012-02-21 11:47:18 -0800214 void signalError(
215 OMX_ERRORTYPE error = OMX_ErrorUndefined,
216 status_t internalError = UNKNOWN_ERROR);
Andreas Huberd41108c2011-09-12 14:14:08 -0700217
Andreas Hubera1587462010-12-15 15:17:42 -0800218 DISALLOW_EVIL_CONSTRUCTORS(ACodec);
219};
220
221} // namespace android
222
223#endif // A_CODEC_H_