MediaCodec.h revision 609b815a3131d22da38b2f452faa9f89daad4039
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 ACodec;
30struct AMessage;
31struct AString;
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    static sp<MediaCodec> CreateByType(
48            const sp<ALooper> &looper, const char *mime, bool encoder);
49
50    static sp<MediaCodec> CreateByComponentName(
51            const sp<ALooper> &looper, const char *name);
52
53    status_t configure(
54            const sp<AMessage> &format,
55            const sp<Surface> &nativeWindow,
56            const sp<ICrypto> &crypto,
57            uint32_t flags);
58
59    status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
60
61    status_t start();
62
63    // Returns to a state in which the component remains allocated but
64    // unconfigured.
65    status_t stop();
66
67    // Client MUST call release before releasing final reference to this
68    // object.
69    status_t release();
70
71    status_t flush();
72
73    status_t queueInputBuffer(
74            size_t index,
75            size_t offset,
76            size_t size,
77            int64_t presentationTimeUs,
78            uint32_t flags,
79            AString *errorDetailMsg = NULL);
80
81    status_t queueSecureInputBuffer(
82            size_t index,
83            size_t offset,
84            const CryptoPlugin::SubSample *subSamples,
85            size_t numSubSamples,
86            const uint8_t key[16],
87            const uint8_t iv[16],
88            CryptoPlugin::Mode mode,
89            int64_t presentationTimeUs,
90            uint32_t flags,
91            AString *errorDetailMsg = NULL);
92
93    status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs = 0ll);
94
95    status_t dequeueOutputBuffer(
96            size_t *index,
97            size_t *offset,
98            size_t *size,
99            int64_t *presentationTimeUs,
100            uint32_t *flags,
101            int64_t timeoutUs = 0ll);
102
103    status_t renderOutputBufferAndRelease(size_t index, int64_t timestampNs);
104    status_t renderOutputBufferAndRelease(size_t index);
105    status_t releaseOutputBuffer(size_t index);
106
107    status_t signalEndOfInputStream();
108
109    status_t getOutputFormat(sp<AMessage> *format) const;
110    status_t getInputFormat(sp<AMessage> *format) const;
111
112    status_t getInputBuffers(Vector<sp<ABuffer> > *buffers) const;
113    status_t getOutputBuffers(Vector<sp<ABuffer> > *buffers) const;
114
115    status_t requestIDRFrame();
116
117    // Notification will be posted once there "is something to do", i.e.
118    // an input/output buffer has become available, a format change is
119    // pending, an error is pending.
120    void requestActivityNotification(const sp<AMessage> &notify);
121
122    status_t getName(AString *componentName) const;
123
124    status_t setParameters(const sp<AMessage> &params);
125
126protected:
127    virtual ~MediaCodec();
128    virtual void onMessageReceived(const sp<AMessage> &msg);
129
130private:
131    enum State {
132        UNINITIALIZED,
133        INITIALIZING,
134        INITIALIZED,
135        CONFIGURING,
136        CONFIGURED,
137        STARTING,
138        STARTED,
139        FLUSHING,
140        STOPPING,
141        RELEASING,
142    };
143
144    enum {
145        kPortIndexInput         = 0,
146        kPortIndexOutput        = 1,
147    };
148
149    enum {
150        kWhatInit                           = 'init',
151        kWhatConfigure                      = 'conf',
152        kWhatCreateInputSurface             = 'cisf',
153        kWhatStart                          = 'strt',
154        kWhatStop                           = 'stop',
155        kWhatRelease                        = 'rele',
156        kWhatDequeueInputBuffer             = 'deqI',
157        kWhatQueueInputBuffer               = 'queI',
158        kWhatDequeueOutputBuffer            = 'deqO',
159        kWhatReleaseOutputBuffer            = 'relO',
160        kWhatSignalEndOfInputStream         = 'eois',
161        kWhatGetBuffers                     = 'getB',
162        kWhatFlush                          = 'flus',
163        kWhatGetOutputFormat                = 'getO',
164        kWhatGetInputFormat                 = 'getI',
165        kWhatDequeueInputTimedOut           = 'dITO',
166        kWhatDequeueOutputTimedOut          = 'dOTO',
167        kWhatCodecNotify                    = 'codc',
168        kWhatRequestIDRFrame                = 'ridr',
169        kWhatRequestActivityNotification    = 'racN',
170        kWhatGetName                        = 'getN',
171        kWhatSetParameters                  = 'setP',
172    };
173
174    enum {
175        kFlagIsSoftwareCodec            = 1,
176        kFlagOutputFormatChanged        = 2,
177        kFlagOutputBuffersChanged       = 4,
178        kFlagStickyError                = 8,
179        kFlagDequeueInputPending        = 16,
180        kFlagDequeueOutputPending       = 32,
181        kFlagIsSecure                   = 64,
182        kFlagSawMediaServerDie          = 128,
183        kFlagIsEncoder                  = 256,
184        kFlagGatherCodecSpecificData    = 512,
185    };
186
187    struct BufferInfo {
188        uint32_t mBufferID;
189        sp<ABuffer> mData;
190        sp<ABuffer> mEncryptedData;
191        sp<AMessage> mNotify;
192        bool mOwnedByClient;
193    };
194
195    State mState;
196    sp<ALooper> mLooper;
197    sp<ALooper> mCodecLooper;
198    sp<ACodec> mCodec;
199    AString mComponentName;
200    uint32_t mReplyID;
201    uint32_t mFlags;
202    sp<Surface> mNativeWindow;
203    SoftwareRenderer *mSoftRenderer;
204    sp<AMessage> mOutputFormat;
205    sp<AMessage> mInputFormat;
206
207    List<size_t> mAvailPortBuffers[2];
208    Vector<BufferInfo> mPortBuffers[2];
209
210    int32_t mDequeueInputTimeoutGeneration;
211    uint32_t mDequeueInputReplyID;
212
213    int32_t mDequeueOutputTimeoutGeneration;
214    uint32_t mDequeueOutputReplyID;
215
216    sp<ICrypto> mCrypto;
217
218    List<sp<ABuffer> > mCSD;
219
220    sp<AMessage> mActivityNotify;
221
222    bool mHaveInputSurface;
223
224    MediaCodec(const sp<ALooper> &looper);
225
226    static status_t PostAndAwaitResponse(
227            const sp<AMessage> &msg, sp<AMessage> *response);
228
229    status_t init(const char *name, bool nameIsType, bool encoder);
230
231    void setState(State newState);
232    void returnBuffersToCodec();
233    void returnBuffersToCodecOnPort(int32_t portIndex);
234    size_t updateBuffers(int32_t portIndex, const sp<AMessage> &msg);
235    status_t onQueueInputBuffer(const sp<AMessage> &msg);
236    status_t onReleaseOutputBuffer(const sp<AMessage> &msg);
237    ssize_t dequeuePortBuffer(int32_t portIndex);
238
239    bool handleDequeueInputBuffer(uint32_t replyID, bool newRequest = false);
240    bool handleDequeueOutputBuffer(uint32_t replyID, bool newRequest = false);
241    void cancelPendingDequeueOperations();
242
243    void extractCSD(const sp<AMessage> &format);
244    status_t queueCSDInputBuffer(size_t bufferIndex);
245
246    status_t setNativeWindow(
247            const sp<Surface> &surface);
248
249    void postActivityNotificationIfPossible();
250
251    status_t onSetParameters(const sp<AMessage> &params);
252
253    status_t amendOutputFormatWithCodecSpecificData(const sp<ABuffer> &buffer);
254
255    DISALLOW_EVIL_CONSTRUCTORS(MediaCodec);
256};
257
258}  // namespace android
259
260#endif  // MEDIA_CODEC_H_
261