MediaCodecSource.h revision 16fcc47c113e63efa69f5af5decf1ad46ec653a9
1/*
2 * Copyright (C) 2014 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 MediaCodecSource_H_
18#define MediaCodecSource_H_
19
20#include <media/stagefright/foundation/ABase.h>
21#include <media/stagefright/foundation/AHandlerReflector.h>
22#include <media/stagefright/MediaSource.h>
23
24#include <gui/IGraphicBufferConsumer.h>
25
26namespace android {
27
28struct ALooper;
29struct AMessage;
30struct AReplyToken;
31class IGraphicBufferProducer;
32struct MediaCodec;
33class MetaData;
34
35struct MediaCodecSource : public MediaSource,
36                          public MediaBufferObserver {
37    enum FlagBits {
38        FLAG_USE_SURFACE_INPUT      = 1,
39        FLAG_USE_METADATA_INPUT     = 2,
40        FLAG_PREFER_SOFTWARE_CODEC  = 4,  // used for testing only
41    };
42
43    static sp<MediaCodecSource> Create(
44            const sp<ALooper> &looper,
45            const sp<AMessage> &format,
46            const sp<MediaSource> &source,
47            const sp<IGraphicBufferConsumer> &consumer = NULL,
48            uint32_t flags = 0);
49
50    bool isVideo() const { return mIsVideo; }
51    sp<IGraphicBufferProducer> getGraphicBufferProducer();
52    void setInputBufferTimeOffset(int64_t timeOffsetUs);
53
54    // MediaSource
55    virtual status_t start(MetaData *params = NULL);
56    virtual status_t stop();
57    virtual status_t pause();
58    virtual sp<MetaData> getFormat() { return mMeta; }
59    virtual status_t read(
60            MediaBuffer **buffer,
61            const ReadOptions *options = NULL);
62
63    // MediaBufferObserver
64    virtual void signalBufferReturned(MediaBuffer *buffer);
65
66    // for AHandlerReflector
67    void onMessageReceived(const sp<AMessage> &msg);
68
69protected:
70    virtual ~MediaCodecSource();
71
72private:
73    struct Puller;
74
75    enum {
76        kWhatPullerNotify,
77        kWhatEncoderActivity,
78        kWhatStart,
79        kWhatStop,
80        kWhatPause,
81        kWhatSetInputBufferTimeOffset,
82    };
83
84    MediaCodecSource(
85            const sp<ALooper> &looper,
86            const sp<AMessage> &outputFormat,
87            const sp<MediaSource> &source,
88            const sp<IGraphicBufferConsumer> &consumer,
89            uint32_t flags = 0);
90
91    status_t onStart(MetaData *params);
92    status_t init();
93    status_t initEncoder();
94    void releaseEncoder();
95    status_t feedEncoderInputBuffers();
96    void suspend();
97    void resume(int64_t skipFramesBeforeUs = -1ll);
98    void signalEOS(status_t err = ERROR_END_OF_STREAM);
99    bool reachedEOS();
100    status_t postSynchronouslyAndReturnError(const sp<AMessage> &msg);
101
102    sp<ALooper> mLooper;
103    sp<ALooper> mCodecLooper;
104    sp<AHandlerReflector<MediaCodecSource> > mReflector;
105    sp<AMessage> mOutputFormat;
106    sp<MetaData> mMeta;
107    sp<Puller> mPuller;
108    sp<MediaCodec> mEncoder;
109    uint32_t mFlags;
110    List<sp<AReplyToken>> mStopReplyIDQueue;
111    bool mIsVideo;
112    bool mStarted;
113    bool mStopping;
114    bool mDoMoreWorkPending;
115    bool mSetEncoderFormat;
116    int mEncoderFormat;
117    int mEncoderDataSpace;
118    sp<AMessage> mEncoderActivityNotify;
119    sp<IGraphicBufferProducer> mGraphicBufferProducer;
120    sp<IGraphicBufferConsumer> mGraphicBufferConsumer;
121    List<MediaBuffer *> mInputBufferQueue;
122    List<size_t> mAvailEncoderInputIndices;
123    List<int64_t> mDecodingTimeQueue; // decoding time (us) for video
124    int64_t mInputBufferTimeOffsetUs;
125
126    // audio drift time
127    int64_t mFirstSampleTimeUs;
128    List<int64_t> mDriftTimeQueue;
129
130    // following variables are protected by mOutputBufferLock
131    Mutex mOutputBufferLock;
132    Condition mOutputBufferCond;
133    List<MediaBuffer*> mOutputBufferQueue;
134    bool mEncoderReachedEOS;
135    status_t mErrorCode;
136
137    DISALLOW_EVIL_CONSTRUCTORS(MediaCodecSource);
138};
139
140} // namespace android
141
142#endif /* MediaCodecSource_H_ */
143