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 MEDIA_FILTER_H_
18#define MEDIA_FILTER_H_
19
20#include <media/stagefright/CodecBase.h>
21
22namespace android {
23
24class ACodecBufferChannel;
25struct GraphicBufferListener;
26class MemoryDealer;
27struct SimpleFilter;
28
29struct MediaFilter : public CodecBase {
30    MediaFilter();
31
32    virtual std::shared_ptr<BufferChannelBase> getBufferChannel() override;
33    virtual void initiateAllocateComponent(const sp<AMessage> &msg);
34    virtual void initiateConfigureComponent(const sp<AMessage> &msg);
35    virtual void initiateCreateInputSurface();
36    virtual void initiateSetInputSurface(const sp<PersistentSurface> &surface);
37
38    virtual void initiateStart();
39    virtual void initiateShutdown(bool keepComponentAllocated = false);
40
41    virtual void signalFlush();
42    virtual void signalResume();
43
44    virtual void signalRequestIDRFrame();
45    virtual void signalSetParameters(const sp<AMessage> &msg);
46    virtual void signalEndOfInputStream();
47
48    virtual void onMessageReceived(const sp<AMessage> &msg);
49
50protected:
51    virtual ~MediaFilter();
52
53private:
54    struct BufferInfo {
55        enum Status {
56            OWNED_BY_US,
57            OWNED_BY_UPSTREAM,
58        };
59
60        IOMX::buffer_id mBufferID;
61        int32_t mGeneration;
62        int32_t mOutputFlags;
63        Status mStatus;
64
65        sp<MediaCodecBuffer> mData;
66    };
67
68    enum State {
69      UNINITIALIZED,
70      INITIALIZED,
71      CONFIGURED,
72      STARTED,
73    };
74
75    enum {
76        kWhatInputBufferFilled       = 'inpF',
77        kWhatOutputBufferDrained     = 'outD',
78        kWhatShutdown                = 'shut',
79        kWhatFlush                   = 'flus',
80        kWhatResume                  = 'resm',
81        kWhatAllocateComponent       = 'allo',
82        kWhatConfigureComponent      = 'conf',
83        kWhatCreateInputSurface      = 'cisf',
84        kWhatSignalEndOfInputStream  = 'eois',
85        kWhatStart                   = 'star',
86        kWhatSetParameters           = 'setP',
87        kWhatProcessBuffers          = 'proc',
88    };
89
90    enum {
91        kPortIndexInput  = 0,
92        kPortIndexOutput = 1
93    };
94
95    // member variables
96    AString mComponentName;
97    State mState;
98    status_t mInputEOSResult;
99    int32_t mWidth, mHeight;
100    int32_t mStride, mSliceHeight;
101    int32_t mColorFormatIn, mColorFormatOut;
102    size_t mMaxInputSize, mMaxOutputSize;
103    int32_t mGeneration;
104    sp<AMessage> mInputFormat;
105    sp<AMessage> mOutputFormat;
106
107    sp<MemoryDealer> mDealer[2];
108    Vector<BufferInfo> mBuffers[2];
109    Vector<BufferInfo*> mAvailableInputBuffers;
110    Vector<BufferInfo*> mAvailableOutputBuffers;
111    bool mPortEOS[2];
112
113    sp<SimpleFilter> mFilter;
114    sp<GraphicBufferListener> mGraphicBufferListener;
115
116    std::shared_ptr<ACodecBufferChannel> mBufferChannel;
117
118    // helper functions
119    void signalProcessBuffers();
120    void signalError(status_t error);
121
122    status_t allocateBuffersOnPort(OMX_U32 portIndex);
123    BufferInfo *findBufferByID(
124            uint32_t portIndex, IOMX::buffer_id bufferID,
125            ssize_t *index = NULL);
126    void postFillThisBuffer(BufferInfo *info);
127    void postDrainThisBuffer(BufferInfo *info);
128    void postEOS();
129    void requestFillEmptyInput();
130    void processBuffers();
131
132    void onAllocateComponent(const sp<AMessage> &msg);
133    void onConfigureComponent(const sp<AMessage> &msg);
134    void onStart();
135    void onInputBufferFilled(const sp<AMessage> &msg);
136    void onOutputBufferDrained(const sp<AMessage> &msg);
137    void onShutdown(const sp<AMessage> &msg);
138    void onFlush();
139    void onSetParameters(const sp<AMessage> &msg);
140    void onCreateInputSurface();
141    void onInputFrameAvailable();
142    void onSignalEndOfInputStream();
143
144    DISALLOW_EVIL_CONSTRUCTORS(MediaFilter);
145};
146
147}  // namespace android
148
149#endif  // MEDIA_FILTER_H_
150