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