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