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/foundation/Mutexed.h>
23#include <media/stagefright/MediaSource.h>
24
25#include <gui/IGraphicBufferConsumer.h>
26
27namespace android {
28
29struct ALooper;
30struct AMessage;
31struct AReplyToken;
32class IGraphicBufferProducer;
33struct MediaCodec;
34class MetaData;
35
36struct MediaCodecSource : public MediaSource,
37                          public MediaBufferObserver {
38    enum FlagBits {
39        FLAG_USE_SURFACE_INPUT      = 1,
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();
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        kWhatStopStalled,
83    };
84
85    MediaCodecSource(
86            const sp<ALooper> &looper,
87            const sp<AMessage> &outputFormat,
88            const sp<MediaSource> &source,
89            const sp<IGraphicBufferConsumer> &consumer,
90            uint32_t flags = 0);
91
92    status_t onStart(MetaData *params);
93    status_t init();
94    status_t initEncoder();
95    void releaseEncoder();
96    status_t feedEncoderInputBuffers();
97    void suspend();
98    void resume(int64_t skipFramesBeforeUs = -1ll);
99    void signalEOS(status_t err = ERROR_END_OF_STREAM);
100    bool reachedEOS();
101    status_t postSynchronouslyAndReturnError(const sp<AMessage> &msg);
102
103    sp<ALooper> mLooper;
104    sp<ALooper> mCodecLooper;
105    sp<AHandlerReflector<MediaCodecSource> > mReflector;
106    sp<AMessage> mOutputFormat;
107    Mutexed<sp<MetaData>> mMeta;
108    sp<Puller> mPuller;
109    sp<MediaCodec> mEncoder;
110    uint32_t mFlags;
111    List<sp<AReplyToken>> mStopReplyIDQueue;
112    bool mIsVideo;
113    bool mStarted;
114    bool mStopping;
115    bool mDoMoreWorkPending;
116    bool mSetEncoderFormat;
117    int32_t mEncoderFormat;
118    int32_t mEncoderDataSpace;
119    sp<AMessage> mEncoderActivityNotify;
120    sp<IGraphicBufferProducer> mGraphicBufferProducer;
121    sp<IGraphicBufferConsumer> mGraphicBufferConsumer;
122    List<MediaBuffer *> mInputBufferQueue;
123    List<size_t> mAvailEncoderInputIndices;
124    List<int64_t> mDecodingTimeQueue; // decoding time (us) for video
125    int64_t mInputBufferTimeOffsetUs;
126
127    // audio drift time
128    int64_t mFirstSampleTimeUs;
129    List<int64_t> mDriftTimeQueue;
130
131    struct Output {
132        Output();
133        List<MediaBuffer*> mBufferQueue;
134        bool mEncoderReachedEOS;
135        status_t mErrorCode;
136        Condition mCond;
137    };
138    Mutexed<Output> mOutput;
139
140    int32_t mGeneration;
141
142    DISALLOW_EVIL_CONSTRUCTORS(MediaCodecSource);
143};
144
145} // namespace android
146
147#endif /* MediaCodecSource_H_ */
148