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