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/MediaSource.h>
21#include <media/stagefright/foundation/ABase.h>
22#include <media/stagefright/foundation/AHandlerReflector.h>
23#include <media/stagefright/foundation/Mutexed.h>
24#include <media/stagefright/PersistentSurface.h>
25
26namespace android {
27
28struct ALooper;
29struct AMessage;
30struct AReplyToken;
31class IGraphicBufferProducer;
32struct MediaCodec;
33
34struct MediaCodecSource : public MediaSource,
35                          public MediaBufferObserver {
36    enum FlagBits {
37        FLAG_USE_SURFACE_INPUT      = 1,
38        FLAG_PREFER_SOFTWARE_CODEC  = 4,  // used for testing only
39    };
40
41    static sp<MediaCodecSource> Create(
42            const sp<ALooper> &looper,
43            const sp<AMessage> &format,
44            const sp<MediaSource> &source,
45            const sp<PersistentSurface> &persistentSurface = NULL,
46            uint32_t flags = 0);
47
48    bool isVideo() const { return mIsVideo; }
49    sp<IGraphicBufferProducer> getGraphicBufferProducer();
50    status_t setInputBufferTimeOffset(int64_t timeOffsetUs);
51    int64_t getFirstSampleSystemTimeUs();
52
53    // MediaSource
54    virtual status_t start(MetaData *params = NULL);
55    virtual status_t stop();
56    virtual status_t pause() { return pause(NULL); }
57    virtual status_t pause(MetaData *params);
58    virtual sp<MetaData> getFormat();
59    virtual status_t read(
60            MediaBufferBase **buffer,
61            const ReadOptions *options = NULL);
62    virtual status_t setStopTimeUs(int64_t stopTimeUs);
63
64
65    // MediaBufferObserver
66    virtual void signalBufferReturned(MediaBufferBase *buffer);
67
68    // for AHandlerReflector
69    void onMessageReceived(const sp<AMessage> &msg);
70
71
72
73protected:
74    virtual ~MediaCodecSource();
75
76private:
77    struct Puller;
78
79    enum {
80        kWhatPullerNotify,
81        kWhatEncoderActivity,
82        kWhatStart,
83        kWhatStop,
84        kWhatPause,
85        kWhatSetInputBufferTimeOffset,
86        kWhatSetStopTimeUs,
87        kWhatGetFirstSampleSystemTimeUs,
88        kWhatStopStalled,
89    };
90
91    MediaCodecSource(
92            const sp<ALooper> &looper,
93            const sp<AMessage> &outputFormat,
94            const sp<MediaSource> &source,
95            const sp<PersistentSurface> &persistentSurface,
96            uint32_t flags = 0);
97
98    status_t onStart(MetaData *params);
99
100    // Pause the source at pauseStartTimeUs. For non-surface input,
101    // buffers will be dropped immediately. For surface input, buffers
102    // with timestamp smaller than pauseStartTimeUs will still be encoded.
103    // Buffers with timestamp larger or queal to pauseStartTimeUs will be
104    // dropped. pauseStartTimeUs uses SYSTEM_TIME_MONOTONIC time base.
105    void onPause(int64_t pauseStartTimeUs);
106
107    status_t init();
108    status_t initEncoder();
109    void releaseEncoder();
110    status_t feedEncoderInputBuffers();
111    // Resume GraphicBufferSource at resumeStartTimeUs. Buffers
112    // from GraphicBufferSource with timestamp larger or equal to
113    // resumeStartTimeUs will be encoded. resumeStartTimeUs uses
114    // SYSTEM_TIME_MONOTONIC time base.
115    void resume(int64_t resumeStartTimeUs = -1ll);
116    void signalEOS(status_t err = ERROR_END_OF_STREAM);
117    bool reachedEOS();
118    status_t postSynchronouslyAndReturnError(const sp<AMessage> &msg);
119
120    sp<ALooper> mLooper;
121    sp<ALooper> mCodecLooper;
122    sp<AHandlerReflector<MediaCodecSource> > mReflector;
123    sp<AMessage> mOutputFormat;
124    Mutexed<sp<MetaData>> mMeta;
125    sp<Puller> mPuller;
126    sp<MediaCodec> mEncoder;
127    uint32_t mFlags;
128    List<sp<AReplyToken>> mStopReplyIDQueue;
129    bool mIsVideo;
130    bool mStarted;
131    bool mStopping;
132    bool mDoMoreWorkPending;
133    bool mSetEncoderFormat;
134    int32_t mEncoderFormat;
135    int32_t mEncoderDataSpace;
136    sp<AMessage> mEncoderActivityNotify;
137    sp<IGraphicBufferProducer> mGraphicBufferProducer;
138    sp<PersistentSurface> mPersistentSurface;
139    List<MediaBufferBase *> mInputBufferQueue;
140    List<size_t> mAvailEncoderInputIndices;
141    List<int64_t> mDecodingTimeQueue; // decoding time (us) for video
142    int64_t mInputBufferTimeOffsetUs;
143    int64_t mFirstSampleSystemTimeUs;
144    bool mPausePending;
145
146    // audio drift time
147    int64_t mFirstSampleTimeUs;
148    List<int64_t> mDriftTimeQueue;
149
150    struct Output {
151        Output();
152        List<MediaBufferBase*> mBufferQueue;
153        bool mEncoderReachedEOS;
154        status_t mErrorCode;
155        Condition mCond;
156    };
157    Mutexed<Output> mOutput;
158
159    int32_t mGeneration;
160
161    DISALLOW_EVIL_CONSTRUCTORS(MediaCodecSource);
162};
163
164} // namespace android
165
166#endif /* MediaCodecSource_H_ */
167