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