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    enum {
62        kStoreMetaDataExtensionIndex = OMX_IndexVendorStartUnused + 1,
63        kPrepareForAdaptivePlaybackIndex,
64    };
65
66    void addPort(const OMX_PARAM_PORTDEFINITIONTYPE &def);
67
68    virtual OMX_ERRORTYPE internalGetParameter(
69            OMX_INDEXTYPE index, OMX_PTR params);
70
71    virtual OMX_ERRORTYPE internalSetParameter(
72            OMX_INDEXTYPE index, const OMX_PTR params);
73
74    virtual void onQueueFilled(OMX_U32 portIndex);
75    List<BufferInfo *> &getPortQueue(OMX_U32 portIndex);
76
77    virtual void onPortFlushCompleted(OMX_U32 portIndex);
78    virtual void onPortEnableCompleted(OMX_U32 portIndex, bool enabled);
79    virtual void onReset();
80
81    PortInfo *editPortInfo(OMX_U32 portIndex);
82
83private:
84    enum {
85        kWhatSendCommand,
86        kWhatEmptyThisBuffer,
87        kWhatFillThisBuffer,
88    };
89
90    Mutex mLock;
91
92    sp<ALooper> mLooper;
93    sp<AHandlerReflector<SimpleSoftOMXComponent> > mHandler;
94
95    OMX_STATETYPE mState;
96    OMX_STATETYPE mTargetState;
97
98    Vector<PortInfo> mPorts;
99
100    bool isSetParameterAllowed(
101            OMX_INDEXTYPE index, const OMX_PTR params) const;
102
103    virtual OMX_ERRORTYPE sendCommand(
104            OMX_COMMANDTYPE cmd, OMX_U32 param, OMX_PTR data);
105
106    virtual OMX_ERRORTYPE getParameter(
107            OMX_INDEXTYPE index, OMX_PTR params);
108
109    virtual OMX_ERRORTYPE setParameter(
110            OMX_INDEXTYPE index, const OMX_PTR params);
111
112    virtual OMX_ERRORTYPE useBuffer(
113            OMX_BUFFERHEADERTYPE **buffer,
114            OMX_U32 portIndex,
115            OMX_PTR appPrivate,
116            OMX_U32 size,
117            OMX_U8 *ptr);
118
119    virtual OMX_ERRORTYPE allocateBuffer(
120            OMX_BUFFERHEADERTYPE **buffer,
121            OMX_U32 portIndex,
122            OMX_PTR appPrivate,
123            OMX_U32 size);
124
125    virtual OMX_ERRORTYPE freeBuffer(
126            OMX_U32 portIndex,
127            OMX_BUFFERHEADERTYPE *buffer);
128
129    virtual OMX_ERRORTYPE emptyThisBuffer(
130            OMX_BUFFERHEADERTYPE *buffer);
131
132    virtual OMX_ERRORTYPE fillThisBuffer(
133            OMX_BUFFERHEADERTYPE *buffer);
134
135    virtual OMX_ERRORTYPE getState(OMX_STATETYPE *state);
136
137    void onSendCommand(OMX_COMMANDTYPE cmd, OMX_U32 param);
138    void onChangeState(OMX_STATETYPE state);
139    void onPortEnable(OMX_U32 portIndex, bool enable);
140    void onPortFlush(OMX_U32 portIndex, bool sendFlushComplete);
141
142    void checkTransitions();
143
144    DISALLOW_EVIL_CONSTRUCTORS(SimpleSoftOMXComponent);
145};
146
147}  // namespace android
148
149#endif  // SIMPLE_SOFT_OMX_COMPONENT_H_
150