MediaCodec.h revision 4b75a9c8b93a90749bc5d22912ad0d96c12f4ecf
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 ICrypto;
32struct SoftwareRenderer;
33struct SurfaceTextureClient;
34
35struct MediaCodec : public AHandler {
36    enum ConfigureFlags {
37        CONFIGURE_FLAG_ENCODE   = 1,
38    };
39
40    enum BufferFlags {
41        BUFFER_FLAG_SYNCFRAME   = 1,
42        BUFFER_FLAG_CODECCONFIG = 2,
43        BUFFER_FLAG_EOS         = 4,
44    };
45
46    static sp<MediaCodec> CreateByType(
47            const sp<ALooper> &looper, const char *mime, bool encoder);
48
49    static sp<MediaCodec> CreateByComponentName(
50            const sp<ALooper> &looper, const char *name);
51
52    status_t configure(
53            const sp<AMessage> &format,
54            const sp<SurfaceTextureClient> &nativeWindow,
55            const sp<ICrypto> &crypto,
56            uint32_t flags);
57
58    status_t start();
59
60    // Returns to a state in which the component remains allocated but
61    // unconfigured.
62    status_t stop();
63
64    // Client MUST call release before releasing final reference to this
65    // object.
66    status_t release();
67
68    status_t flush();
69
70    status_t queueInputBuffer(
71            size_t index,
72            size_t offset,
73            size_t size,
74            int64_t presentationTimeUs,
75            uint32_t flags);
76
77    status_t queueSecureInputBuffer(
78            size_t index,
79            size_t offset,
80            const CryptoPlugin::SubSample *subSamples,
81            size_t numSubSamples,
82            const uint8_t key[16],
83            const uint8_t iv[16],
84            CryptoPlugin::Mode mode,
85            int64_t presentationTimeUs,
86            uint32_t flags);
87
88    status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs = 0ll);
89
90    status_t dequeueOutputBuffer(
91            size_t *index,
92            size_t *offset,
93            size_t *size,
94            int64_t *presentationTimeUs,
95            uint32_t *flags,
96            int64_t timeoutUs = 0ll);
97
98    status_t renderOutputBufferAndRelease(size_t index);
99    status_t releaseOutputBuffer(size_t index);
100
101    status_t getOutputFormat(sp<AMessage> *format) const;
102
103    status_t getInputBuffers(Vector<sp<ABuffer> > *buffers) const;
104    status_t getOutputBuffers(Vector<sp<ABuffer> > *buffers) const;
105
106protected:
107    virtual ~MediaCodec();
108    virtual void onMessageReceived(const sp<AMessage> &msg);
109
110private:
111    enum State {
112        UNINITIALIZED,
113        INITIALIZING,
114        INITIALIZED,
115        CONFIGURING,
116        CONFIGURED,
117        STARTING,
118        STARTED,
119        FLUSHING,
120        STOPPING,
121        RELEASING,
122    };
123
124    enum {
125        kPortIndexInput         = 0,
126        kPortIndexOutput        = 1,
127    };
128
129    enum {
130        kWhatInit                       = 'init',
131        kWhatConfigure                  = 'conf',
132        kWhatStart                      = 'strt',
133        kWhatStop                       = 'stop',
134        kWhatRelease                    = 'rele',
135        kWhatDequeueInputBuffer         = 'deqI',
136        kWhatQueueInputBuffer           = 'queI',
137        kWhatDequeueOutputBuffer        = 'deqO',
138        kWhatReleaseOutputBuffer        = 'relO',
139        kWhatGetBuffers                 = 'getB',
140        kWhatFlush                      = 'flus',
141        kWhatGetOutputFormat            = 'getO',
142        kWhatDequeueInputTimedOut       = 'dITO',
143        kWhatDequeueOutputTimedOut      = 'dOTO',
144        kWhatCodecNotify                = 'codc',
145    };
146
147    enum {
148        kFlagIsSoftwareCodec            = 1,
149        kFlagOutputFormatChanged        = 2,
150        kFlagOutputBuffersChanged       = 4,
151        kFlagStickyError                = 8,
152        kFlagDequeueInputPending        = 16,
153        kFlagDequeueOutputPending       = 32,
154        kFlagIsSecure                   = 64,
155    };
156
157    struct BufferInfo {
158        void *mBufferID;
159        sp<ABuffer> mData;
160        sp<ABuffer> mEncryptedData;
161        sp<AMessage> mNotify;
162        bool mOwnedByClient;
163    };
164
165    State mState;
166    sp<ALooper> mLooper;
167    sp<ALooper> mCodecLooper;
168    sp<ACodec> mCodec;
169    uint32_t mReplyID;
170    uint32_t mFlags;
171    sp<SurfaceTextureClient> mNativeWindow;
172    SoftwareRenderer *mSoftRenderer;
173    sp<AMessage> mOutputFormat;
174
175    List<size_t> mAvailPortBuffers[2];
176    Vector<BufferInfo> mPortBuffers[2];
177
178    int32_t mDequeueInputTimeoutGeneration;
179    uint32_t mDequeueInputReplyID;
180
181    int32_t mDequeueOutputTimeoutGeneration;
182    uint32_t mDequeueOutputReplyID;
183
184    sp<ICrypto> mCrypto;
185
186    MediaCodec(const sp<ALooper> &looper);
187
188    static status_t PostAndAwaitResponse(
189            const sp<AMessage> &msg, sp<AMessage> *response);
190
191    status_t init(const char *name, bool nameIsType, bool encoder);
192
193    void setState(State newState);
194    void returnBuffersToCodec();
195    void returnBuffersToCodecOnPort(int32_t portIndex);
196    size_t updateBuffers(int32_t portIndex, const sp<AMessage> &msg);
197    status_t onQueueInputBuffer(const sp<AMessage> &msg);
198    status_t onReleaseOutputBuffer(const sp<AMessage> &msg);
199    ssize_t dequeuePortBuffer(int32_t portIndex);
200
201    bool handleDequeueInputBuffer(uint32_t replyID, bool newRequest = false);
202    bool handleDequeueOutputBuffer(uint32_t replyID, bool newRequest = false);
203    void cancelPendingDequeueOperations();
204
205    DISALLOW_EVIL_CONSTRUCTORS(MediaCodec);
206};
207
208}  // namespace android
209
210#endif  // MEDIA_CODEC_H_
211