MediaCodec.h revision c5619c7a6dcc1137fde7520351ad5284e3e958ab
1/*
2 * Copyright 2012, 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 MEDIA_CODEC_H_
18
19#define MEDIA_CODEC_H_
20
21#include <gui/IGraphicBufferProducer.h>
22#include <media/hardware/CryptoAPI.h>
23#include <media/stagefright/foundation/AHandler.h>
24#include <utils/Vector.h>
25
26namespace android {
27
28struct ABuffer;
29struct AMessage;
30struct AString;
31struct CodecBase;
32struct ICrypto;
33struct SoftwareRenderer;
34struct Surface;
35
36struct MediaCodec : public AHandler {
37    enum ConfigureFlags {
38        CONFIGURE_FLAG_ENCODE   = 1,
39    };
40
41    enum BufferFlags {
42        BUFFER_FLAG_SYNCFRAME   = 1,
43        BUFFER_FLAG_CODECCONFIG = 2,
44        BUFFER_FLAG_EOS         = 4,
45    };
46
47    enum {
48        CB_INPUT_AVAILABLE = 1,
49        CB_OUTPUT_AVAILABLE = 2,
50        CB_ERROR = 3,
51        CB_OUTPUT_FORMAT_CHANGED = 4,
52    };
53
54    static sp<MediaCodec> CreateByType(
55            const sp<ALooper> &looper, const char *mime, bool encoder);
56
57    static sp<MediaCodec> CreateByComponentName(
58            const sp<ALooper> &looper, const char *name);
59
60    status_t configure(
61            const sp<AMessage> &format,
62            const sp<Surface> &nativeWindow,
63            const sp<ICrypto> &crypto,
64            uint32_t flags);
65
66    status_t setCallback(const sp<AMessage> &callback);
67
68    status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
69
70    status_t start();
71
72    // Returns to a state in which the component remains allocated but
73    // unconfigured.
74    status_t stop();
75
76    // Client MUST call release before releasing final reference to this
77    // object.
78    status_t release();
79
80    status_t flush();
81
82    status_t queueInputBuffer(
83            size_t index,
84            size_t offset,
85            size_t size,
86            int64_t presentationTimeUs,
87            uint32_t flags,
88            AString *errorDetailMsg = NULL);
89
90    status_t queueSecureInputBuffer(
91            size_t index,
92            size_t offset,
93            const CryptoPlugin::SubSample *subSamples,
94            size_t numSubSamples,
95            const uint8_t key[16],
96            const uint8_t iv[16],
97            CryptoPlugin::Mode mode,
98            int64_t presentationTimeUs,
99            uint32_t flags,
100            AString *errorDetailMsg = NULL);
101
102    status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs = 0ll);
103
104    status_t dequeueOutputBuffer(
105            size_t *index,
106            size_t *offset,
107            size_t *size,
108            int64_t *presentationTimeUs,
109            uint32_t *flags,
110            int64_t timeoutUs = 0ll);
111
112    status_t renderOutputBufferAndRelease(size_t index, int64_t timestampNs);
113    status_t renderOutputBufferAndRelease(size_t index);
114    status_t releaseOutputBuffer(size_t index);
115
116    status_t signalEndOfInputStream();
117
118    status_t getOutputFormat(sp<AMessage> *format) const;
119    status_t getInputFormat(sp<AMessage> *format) const;
120
121    status_t getInputBuffers(Vector<sp<ABuffer> > *buffers) const;
122    status_t getOutputBuffers(Vector<sp<ABuffer> > *buffers) const;
123
124    status_t getOutputBuffer(size_t index, sp<ABuffer> *buffer);
125    status_t getOutputFormat(size_t index, sp<AMessage> *format);
126    status_t getInputBuffer(size_t index, sp<ABuffer> *buffer);
127
128    status_t requestIDRFrame();
129
130    // Notification will be posted once there "is something to do", i.e.
131    // an input/output buffer has become available, a format change is
132    // pending, an error is pending.
133    void requestActivityNotification(const sp<AMessage> &notify);
134
135    status_t getName(AString *componentName) const;
136
137    status_t setParameters(const sp<AMessage> &params);
138
139protected:
140    virtual ~MediaCodec();
141    virtual void onMessageReceived(const sp<AMessage> &msg);
142
143private:
144    enum State {
145        UNINITIALIZED,
146        INITIALIZING,
147        INITIALIZED,
148        CONFIGURING,
149        CONFIGURED,
150        STARTING,
151        STARTED,
152        FLUSHING,
153        STOPPING,
154        RELEASING,
155    };
156
157    enum {
158        kPortIndexInput         = 0,
159        kPortIndexOutput        = 1,
160    };
161
162    enum {
163        kWhatInit                           = 'init',
164        kWhatConfigure                      = 'conf',
165        kWhatCreateInputSurface             = 'cisf',
166        kWhatStart                          = 'strt',
167        kWhatStop                           = 'stop',
168        kWhatRelease                        = 'rele',
169        kWhatDequeueInputBuffer             = 'deqI',
170        kWhatQueueInputBuffer               = 'queI',
171        kWhatDequeueOutputBuffer            = 'deqO',
172        kWhatReleaseOutputBuffer            = 'relO',
173        kWhatSignalEndOfInputStream         = 'eois',
174        kWhatGetBuffers                     = 'getB',
175        kWhatFlush                          = 'flus',
176        kWhatGetOutputFormat                = 'getO',
177        kWhatGetInputFormat                 = 'getI',
178        kWhatDequeueInputTimedOut           = 'dITO',
179        kWhatDequeueOutputTimedOut          = 'dOTO',
180        kWhatCodecNotify                    = 'codc',
181        kWhatRequestIDRFrame                = 'ridr',
182        kWhatRequestActivityNotification    = 'racN',
183        kWhatGetName                        = 'getN',
184        kWhatSetParameters                  = 'setP',
185        kWhatSetCallback                    = 'setC',
186    };
187
188    enum {
189        kFlagIsSoftwareCodec            = 1,
190        kFlagOutputFormatChanged        = 2,
191        kFlagOutputBuffersChanged       = 4,
192        kFlagStickyError                = 8,
193        kFlagDequeueInputPending        = 16,
194        kFlagDequeueOutputPending       = 32,
195        kFlagIsSecure                   = 64,
196        kFlagSawMediaServerDie          = 128,
197        kFlagIsEncoder                  = 256,
198        kFlagGatherCodecSpecificData    = 512,
199        kFlagIsAsync                    = 1024,
200    };
201
202    struct BufferInfo {
203        uint32_t mBufferID;
204        sp<ABuffer> mData;
205        sp<ABuffer> mEncryptedData;
206        sp<AMessage> mNotify;
207        sp<AMessage> mFormat;
208        bool mOwnedByClient;
209    };
210
211    State mState;
212    sp<ALooper> mLooper;
213    sp<ALooper> mCodecLooper;
214    sp<CodecBase> mCodec;
215    AString mComponentName;
216    uint32_t mReplyID;
217    uint32_t mFlags;
218    sp<Surface> mNativeWindow;
219    SoftwareRenderer *mSoftRenderer;
220    sp<AMessage> mOutputFormat;
221    sp<AMessage> mInputFormat;
222    sp<AMessage> mCallback;
223
224    // Used only to synchronize asynchronous getBufferAndFormat
225    // across all the other (synchronous) buffer state change
226    // operations, such as de/queueIn/OutputBuffer, start and
227    // stop/flush/reset/release.
228    Mutex mBufferLock;
229
230    List<size_t> mAvailPortBuffers[2];
231    Vector<BufferInfo> mPortBuffers[2];
232
233    int32_t mDequeueInputTimeoutGeneration;
234    uint32_t mDequeueInputReplyID;
235
236    int32_t mDequeueOutputTimeoutGeneration;
237    uint32_t mDequeueOutputReplyID;
238
239    sp<ICrypto> mCrypto;
240
241    List<sp<ABuffer> > mCSD;
242
243    sp<AMessage> mActivityNotify;
244
245    bool mHaveInputSurface;
246
247    MediaCodec(const sp<ALooper> &looper);
248
249    static status_t PostAndAwaitResponse(
250            const sp<AMessage> &msg, sp<AMessage> *response);
251
252    static void PostReplyWithError(int32_t replyID, int32_t err);
253
254    status_t init(const char *name, bool nameIsType, bool encoder);
255
256    void setState(State newState);
257    void returnBuffersToCodec();
258    void returnBuffersToCodecOnPort(int32_t portIndex);
259    size_t updateBuffers(int32_t portIndex, const sp<AMessage> &msg);
260    status_t onQueueInputBuffer(const sp<AMessage> &msg);
261    status_t onReleaseOutputBuffer(const sp<AMessage> &msg);
262    ssize_t dequeuePortBuffer(int32_t portIndex);
263
264    status_t getBufferAndFormat(
265            size_t portIndex, size_t index,
266            sp<ABuffer> *buffer, sp<AMessage> *format);
267
268    bool handleDequeueInputBuffer(uint32_t replyID, bool newRequest = false);
269    bool handleDequeueOutputBuffer(uint32_t replyID, bool newRequest = false);
270    void cancelPendingDequeueOperations();
271
272    void extractCSD(const sp<AMessage> &format);
273    status_t queueCSDInputBuffer(size_t bufferIndex);
274
275    status_t setNativeWindow(
276            const sp<Surface> &surface);
277
278    void postActivityNotificationIfPossible();
279
280    void onInputBufferAvailable();
281    void onOutputBufferAvailable();
282    void onError(int32_t actionCode, status_t err);
283    void onOutputFormatChanged();
284
285    status_t onSetParameters(const sp<AMessage> &params);
286
287    status_t amendOutputFormatWithCodecSpecificData(const sp<ABuffer> &buffer);
288
289    DISALLOW_EVIL_CONSTRUCTORS(MediaCodec);
290};
291
292}  // namespace android
293
294#endif  // MEDIA_CODEC_H_
295