MediaCodec.h revision 1bd139a2a68690e80398b70b27ca59550fea0e65
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/stagefright/foundation/AHandler.h>
23#include <utils/Vector.h>
24
25namespace android {
26
27struct ABuffer;
28struct ACodec;
29struct AMessage;
30struct ICrypto;
31struct SoftwareRenderer;
32struct SurfaceTextureClient;
33
34struct MediaCodec : public AHandler {
35    enum ConfigureFlags {
36        CONFIGURE_FLAG_ENCODE   = 1,
37    };
38
39    enum BufferFlags {
40        BUFFER_FLAG_SYNCFRAME   = 1,
41        BUFFER_FLAG_CODECCONFIG = 2,
42        BUFFER_FLAG_EOS         = 4,
43        BUFFER_FLAG_ENCRYPTED   = 8,
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 dequeueInputBuffer(size_t *index, int64_t timeoutUs = 0ll);
78
79    status_t dequeueOutputBuffer(
80            size_t *index,
81            size_t *offset,
82            size_t *size,
83            int64_t *presentationTimeUs,
84            uint32_t *flags,
85            int64_t timeoutUs = 0ll);
86
87    status_t renderOutputBufferAndRelease(size_t index);
88    status_t releaseOutputBuffer(size_t index);
89
90    status_t getOutputFormat(sp<AMessage> *format) const;
91
92    status_t getInputBuffers(Vector<sp<ABuffer> > *buffers) const;
93    status_t getOutputBuffers(Vector<sp<ABuffer> > *buffers) const;
94
95protected:
96    virtual ~MediaCodec();
97    virtual void onMessageReceived(const sp<AMessage> &msg);
98
99private:
100    enum State {
101        UNINITIALIZED,
102        INITIALIZING,
103        INITIALIZED,
104        CONFIGURING,
105        CONFIGURED,
106        STARTING,
107        STARTED,
108        FLUSHING,
109        STOPPING,
110        RELEASING,
111    };
112
113    enum {
114        kPortIndexInput         = 0,
115        kPortIndexOutput        = 1,
116    };
117
118    enum {
119        kWhatInit                       = 'init',
120        kWhatConfigure                  = 'conf',
121        kWhatStart                      = 'strt',
122        kWhatStop                       = 'stop',
123        kWhatRelease                    = 'rele',
124        kWhatDequeueInputBuffer         = 'deqI',
125        kWhatQueueInputBuffer           = 'queI',
126        kWhatDequeueOutputBuffer        = 'deqO',
127        kWhatReleaseOutputBuffer        = 'relO',
128        kWhatGetBuffers                 = 'getB',
129        kWhatFlush                      = 'flus',
130        kWhatGetOutputFormat            = 'getO',
131        kWhatDequeueInputTimedOut       = 'dITO',
132        kWhatDequeueOutputTimedOut      = 'dOTO',
133        kWhatCodecNotify                = 'codc',
134    };
135
136    enum {
137        kFlagIsSoftwareCodec            = 1,
138        kFlagOutputFormatChanged        = 2,
139        kFlagOutputBuffersChanged       = 4,
140        kFlagStickyError                = 8,
141        kFlagDequeueInputPending        = 16,
142        kFlagDequeueOutputPending       = 32,
143        kFlagIsSecure                   = 64,
144    };
145
146    struct BufferInfo {
147        void *mBufferID;
148        sp<ABuffer> mData;
149        sp<ABuffer> mEncryptedData;
150        sp<AMessage> mNotify;
151        bool mOwnedByClient;
152    };
153
154    State mState;
155    sp<ALooper> mLooper;
156    sp<ALooper> mCodecLooper;
157    sp<ACodec> mCodec;
158    uint32_t mReplyID;
159    uint32_t mFlags;
160    sp<SurfaceTextureClient> mNativeWindow;
161    SoftwareRenderer *mSoftRenderer;
162    sp<AMessage> mOutputFormat;
163
164    List<size_t> mAvailPortBuffers[2];
165    Vector<BufferInfo> mPortBuffers[2];
166
167    int32_t mDequeueInputTimeoutGeneration;
168    uint32_t mDequeueInputReplyID;
169
170    int32_t mDequeueOutputTimeoutGeneration;
171    uint32_t mDequeueOutputReplyID;
172
173    sp<ICrypto> mCrypto;
174
175    MediaCodec(const sp<ALooper> &looper);
176
177    static status_t PostAndAwaitResponse(
178            const sp<AMessage> &msg, sp<AMessage> *response);
179
180    status_t init(const char *name, bool nameIsType, bool encoder);
181
182    void setState(State newState);
183    void returnBuffersToCodec();
184    void returnBuffersToCodecOnPort(int32_t portIndex);
185    size_t updateBuffers(int32_t portIndex, const sp<AMessage> &msg);
186    status_t onQueueInputBuffer(const sp<AMessage> &msg);
187    status_t onReleaseOutputBuffer(const sp<AMessage> &msg);
188    ssize_t dequeuePortBuffer(int32_t portIndex);
189
190    bool handleDequeueInputBuffer(uint32_t replyID, bool newRequest = false);
191    bool handleDequeueOutputBuffer(uint32_t replyID, bool newRequest = false);
192    void cancelPendingDequeueOperations();
193
194    DISALLOW_EVIL_CONSTRUCTORS(MediaCodec);
195};
196
197}  // namespace android
198
199#endif  // MEDIA_CODEC_H_
200