1/*
2 * Copyright (C) 2011 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 SIMPLE_SOFT_OMX_COMPONENT_H_
18
19#define SIMPLE_SOFT_OMX_COMPONENT_H_
20
21#include "SoftOMXComponent.h"
22
23#include <media/stagefright/foundation/AHandlerReflector.h>
24#include <utils/RefBase.h>
25#include <utils/threads.h>
26#include <utils/Vector.h>
27
28namespace android {
29
30struct ALooper;
31
32struct SimpleSoftOMXComponent : public SoftOMXComponent {
33    SimpleSoftOMXComponent(
34            const char *name,
35            const OMX_CALLBACKTYPE *callbacks,
36            OMX_PTR appData,
37            OMX_COMPONENTTYPE **component);
38
39    virtual void prepareForDestruction();
40
41    void onMessageReceived(const sp<AMessage> &msg);
42
43protected:
44    struct BufferInfo {
45        OMX_BUFFERHEADERTYPE *mHeader;
46        bool mOwnedByUs;
47    };
48
49    struct PortInfo {
50        OMX_PARAM_PORTDEFINITIONTYPE mDef;
51        Vector<BufferInfo> mBuffers;
52        List<BufferInfo *> mQueue;
53
54        enum {
55            NONE,
56            DISABLING,
57            ENABLING,
58        } mTransition;
59    };
60
61    void addPort(const OMX_PARAM_PORTDEFINITIONTYPE &def);
62
63    virtual OMX_ERRORTYPE internalGetParameter(
64            OMX_INDEXTYPE index, OMX_PTR params);
65
66    virtual OMX_ERRORTYPE internalSetParameter(
67            OMX_INDEXTYPE index, const OMX_PTR params);
68
69    virtual void onQueueFilled(OMX_U32 portIndex);
70    List<BufferInfo *> &getPortQueue(OMX_U32 portIndex);
71
72    virtual void onPortFlushCompleted(OMX_U32 portIndex);
73    virtual void onPortEnableCompleted(OMX_U32 portIndex, bool enabled);
74
75    PortInfo *editPortInfo(OMX_U32 portIndex);
76
77private:
78    enum {
79        kWhatSendCommand,
80        kWhatEmptyThisBuffer,
81        kWhatFillThisBuffer,
82    };
83
84    Mutex mLock;
85
86    sp<ALooper> mLooper;
87    sp<AHandlerReflector<SimpleSoftOMXComponent> > mHandler;
88
89    OMX_STATETYPE mState;
90    OMX_STATETYPE mTargetState;
91
92    Vector<PortInfo> mPorts;
93
94    bool isSetParameterAllowed(
95            OMX_INDEXTYPE index, const OMX_PTR params) const;
96
97    virtual OMX_ERRORTYPE sendCommand(
98            OMX_COMMANDTYPE cmd, OMX_U32 param, OMX_PTR data);
99
100    virtual OMX_ERRORTYPE getParameter(
101            OMX_INDEXTYPE index, OMX_PTR params);
102
103    virtual OMX_ERRORTYPE setParameter(
104            OMX_INDEXTYPE index, const OMX_PTR params);
105
106    virtual OMX_ERRORTYPE useBuffer(
107            OMX_BUFFERHEADERTYPE **buffer,
108            OMX_U32 portIndex,
109            OMX_PTR appPrivate,
110            OMX_U32 size,
111            OMX_U8 *ptr);
112
113    virtual OMX_ERRORTYPE allocateBuffer(
114            OMX_BUFFERHEADERTYPE **buffer,
115            OMX_U32 portIndex,
116            OMX_PTR appPrivate,
117            OMX_U32 size);
118
119    virtual OMX_ERRORTYPE freeBuffer(
120            OMX_U32 portIndex,
121            OMX_BUFFERHEADERTYPE *buffer);
122
123    virtual OMX_ERRORTYPE emptyThisBuffer(
124            OMX_BUFFERHEADERTYPE *buffer);
125
126    virtual OMX_ERRORTYPE fillThisBuffer(
127            OMX_BUFFERHEADERTYPE *buffer);
128
129    virtual OMX_ERRORTYPE getState(OMX_STATETYPE *state);
130
131    void onSendCommand(OMX_COMMANDTYPE cmd, OMX_U32 param);
132    void onChangeState(OMX_STATETYPE state);
133    void onPortEnable(OMX_U32 portIndex, bool enable);
134    void onPortFlush(OMX_U32 portIndex, bool sendFlushComplete);
135
136    void checkTransitions();
137
138    DISALLOW_EVIL_CONSTRUCTORS(SimpleSoftOMXComponent);
139};
140
141}  // namespace android
142
143#endif  // SIMPLE_SOFT_OMX_COMPONENT_H_
144