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