MediaCodecSource.h revision ee0eba046f666303741a5a5f70afad17030cc8b1
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_USE_METADATA_INPUT     = 2,
41        FLAG_PREFER_SOFTWARE_CODEC  = 4,  // used for testing only
42    };
43
44    static sp<MediaCodecSource> Create(
45            const sp<ALooper> &looper,
46            const sp<AMessage> &format,
47            const sp<MediaSource> &source,
48            const sp<IGraphicBufferConsumer> &consumer = NULL,
49            uint32_t flags = 0);
50
51    bool isVideo() const { return mIsVideo; }
52    sp<IGraphicBufferProducer> getGraphicBufferProducer();
53    void setInputBufferTimeOffset(int64_t timeOffsetUs);
54
55    // MediaSource
56    virtual status_t start(MetaData *params = NULL);
57    virtual status_t stop();
58    virtual status_t pause();
59    virtual sp<MetaData> getFormat();
60    virtual status_t read(
61            MediaBuffer **buffer,
62            const ReadOptions *options = NULL);
63
64    // MediaBufferObserver
65    virtual void signalBufferReturned(MediaBuffer *buffer);
66
67    // for AHandlerReflector
68    void onMessageReceived(const sp<AMessage> &msg);
69
70protected:
71    virtual ~MediaCodecSource();
72
73private:
74    struct Puller;
75
76    enum {
77        kWhatPullerNotify,
78        kWhatEncoderActivity,
79        kWhatStart,
80        kWhatStop,
81        kWhatPause,
82        kWhatSetInputBufferTimeOffset,
83        kWhatStopStalled,
84    };
85
86    MediaCodecSource(
87            const sp<ALooper> &looper,
88            const sp<AMessage> &outputFormat,
89            const sp<MediaSource> &source,
90            const sp<IGraphicBufferConsumer> &consumer,
91            uint32_t flags = 0);
92
93    status_t onStart(MetaData *params);
94    status_t init();
95    status_t initEncoder();
96    void releaseEncoder();
97    status_t feedEncoderInputBuffers();
98    void suspend();
99    void resume(int64_t skipFramesBeforeUs = -1ll);
100    void signalEOS(status_t err = ERROR_END_OF_STREAM);
101    bool reachedEOS();
102    status_t postSynchronouslyAndReturnError(const sp<AMessage> &msg);
103
104    sp<ALooper> mLooper;
105    sp<ALooper> mCodecLooper;
106    sp<AHandlerReflector<MediaCodecSource> > mReflector;
107    sp<AMessage> mOutputFormat;
108    Mutexed<sp<MetaData>> mMeta;
109    sp<Puller> mPuller;
110    sp<MediaCodec> mEncoder;
111    uint32_t mFlags;
112    List<sp<AReplyToken>> mStopReplyIDQueue;
113    bool mIsVideo;
114    bool mStarted;
115    bool mStopping;
116    bool mDoMoreWorkPending;
117    bool mSetEncoderFormat;
118    int32_t mEncoderFormat;
119    int32_t mEncoderDataSpace;
120    sp<AMessage> mEncoderActivityNotify;
121    sp<IGraphicBufferProducer> mGraphicBufferProducer;
122    sp<IGraphicBufferConsumer> mGraphicBufferConsumer;
123    List<MediaBuffer *> mInputBufferQueue;
124    List<size_t> mAvailEncoderInputIndices;
125    List<int64_t> mDecodingTimeQueue; // decoding time (us) for video
126    int64_t mInputBufferTimeOffsetUs;
127
128    // audio drift time
129    int64_t mFirstSampleTimeUs;
130    List<int64_t> mDriftTimeQueue;
131
132    struct Output {
133        Output();
134        List<MediaBuffer*> mBufferQueue;
135        bool mEncoderReachedEOS;
136        status_t mErrorCode;
137        Condition mCond;
138    };
139    Mutexed<Output> mOutput;
140
141    int32_t mGeneration;
142
143    DISALLOW_EVIL_CONSTRUCTORS(MediaCodecSource);
144};
145
146} // namespace android
147
148#endif /* MediaCodecSource_H_ */
149