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 SOFT_OMX_COMPONENT_H_
18
19#define SOFT_OMX_COMPONENT_H_
20
21#include <media/stagefright/foundation/ABase.h>
22#include <media/stagefright/foundation/AString.h>
23#include <utils/RefBase.h>
24
25#include <OMX_Component.h>
26
27namespace android {
28
29struct SoftOMXComponent : public RefBase {
30    SoftOMXComponent(
31            const char *name,
32            const OMX_CALLBACKTYPE *callbacks,
33            OMX_PTR appData,
34            OMX_COMPONENTTYPE **component);
35
36    virtual OMX_ERRORTYPE initCheck() const;
37
38    void setLibHandle(void *libHandle);
39    void *libHandle() const;
40
41    virtual void prepareForDestruction() {}
42
43protected:
44    virtual ~SoftOMXComponent();
45
46    const char *name() const;
47
48    void notify(
49            OMX_EVENTTYPE event,
50            OMX_U32 data1, OMX_U32 data2, OMX_PTR data);
51
52    void notifyEmptyBufferDone(OMX_BUFFERHEADERTYPE *header);
53    void notifyFillBufferDone(OMX_BUFFERHEADERTYPE *header);
54
55    virtual OMX_ERRORTYPE sendCommand(
56            OMX_COMMANDTYPE cmd, OMX_U32 param, OMX_PTR data);
57
58    virtual OMX_ERRORTYPE getParameter(
59            OMX_INDEXTYPE index, OMX_PTR params);
60
61    virtual OMX_ERRORTYPE setParameter(
62            OMX_INDEXTYPE index, const OMX_PTR params);
63
64    virtual OMX_ERRORTYPE getConfig(
65            OMX_INDEXTYPE index, OMX_PTR params);
66
67    virtual OMX_ERRORTYPE setConfig(
68            OMX_INDEXTYPE index, const OMX_PTR params);
69
70    virtual OMX_ERRORTYPE getExtensionIndex(
71            const char *name, OMX_INDEXTYPE *index);
72
73    virtual OMX_ERRORTYPE useBuffer(
74            OMX_BUFFERHEADERTYPE **buffer,
75            OMX_U32 portIndex,
76            OMX_PTR appPrivate,
77            OMX_U32 size,
78            OMX_U8 *ptr);
79
80    virtual OMX_ERRORTYPE allocateBuffer(
81            OMX_BUFFERHEADERTYPE **buffer,
82            OMX_U32 portIndex,
83            OMX_PTR appPrivate,
84            OMX_U32 size);
85
86    virtual OMX_ERRORTYPE freeBuffer(
87            OMX_U32 portIndex,
88            OMX_BUFFERHEADERTYPE *buffer);
89
90    virtual OMX_ERRORTYPE emptyThisBuffer(
91            OMX_BUFFERHEADERTYPE *buffer);
92
93    virtual OMX_ERRORTYPE fillThisBuffer(
94            OMX_BUFFERHEADERTYPE *buffer);
95
96    virtual OMX_ERRORTYPE getState(OMX_STATETYPE *state);
97
98private:
99    AString mName;
100    const OMX_CALLBACKTYPE *mCallbacks;
101    OMX_COMPONENTTYPE *mComponent;
102
103    void *mLibHandle;
104
105    static OMX_ERRORTYPE SendCommandWrapper(
106            OMX_HANDLETYPE component,
107            OMX_COMMANDTYPE cmd,
108            OMX_U32 param,
109            OMX_PTR data);
110
111    static OMX_ERRORTYPE GetParameterWrapper(
112            OMX_HANDLETYPE component,
113            OMX_INDEXTYPE index,
114            OMX_PTR params);
115
116    static OMX_ERRORTYPE SetParameterWrapper(
117            OMX_HANDLETYPE component,
118            OMX_INDEXTYPE index,
119            OMX_PTR params);
120
121    static OMX_ERRORTYPE GetConfigWrapper(
122            OMX_HANDLETYPE component,
123            OMX_INDEXTYPE index,
124            OMX_PTR params);
125
126    static OMX_ERRORTYPE SetConfigWrapper(
127            OMX_HANDLETYPE component,
128            OMX_INDEXTYPE index,
129            OMX_PTR params);
130
131    static OMX_ERRORTYPE GetExtensionIndexWrapper(
132            OMX_HANDLETYPE component,
133            OMX_STRING name,
134            OMX_INDEXTYPE *index);
135
136    static OMX_ERRORTYPE UseBufferWrapper(
137            OMX_HANDLETYPE component,
138            OMX_BUFFERHEADERTYPE **buffer,
139            OMX_U32 portIndex,
140            OMX_PTR appPrivate,
141            OMX_U32 size,
142            OMX_U8 *ptr);
143
144    static OMX_ERRORTYPE AllocateBufferWrapper(
145            OMX_HANDLETYPE component,
146            OMX_BUFFERHEADERTYPE **buffer,
147            OMX_U32 portIndex,
148            OMX_PTR appPrivate,
149            OMX_U32 size);
150
151    static OMX_ERRORTYPE FreeBufferWrapper(
152            OMX_HANDLETYPE component,
153            OMX_U32 portIndex,
154            OMX_BUFFERHEADERTYPE *buffer);
155
156    static OMX_ERRORTYPE EmptyThisBufferWrapper(
157            OMX_HANDLETYPE component,
158            OMX_BUFFERHEADERTYPE *buffer);
159
160    static OMX_ERRORTYPE FillThisBufferWrapper(
161            OMX_HANDLETYPE component,
162            OMX_BUFFERHEADERTYPE *buffer);
163
164    static OMX_ERRORTYPE GetStateWrapper(
165            OMX_HANDLETYPE component,
166            OMX_STATETYPE *state);
167
168    DISALLOW_EVIL_CONSTRUCTORS(SoftOMXComponent);
169};
170
171template<typename T>
172bool isValidOMXParam(T *a) {
173  static_assert(offsetof(typeof(*a), nSize) == 0, "nSize not at offset 0");
174  static_assert(std::is_same< decltype(a->nSize), OMX_U32>::value, "nSize has wrong type");
175  static_assert(offsetof(typeof(*a), nVersion) == 4, "nVersion not at offset 4");
176  static_assert(std::is_same< decltype(a->nVersion), OMX_VERSIONTYPE>::value,
177          "nVersion has wrong type");
178
179  if (a->nSize < sizeof(*a)) {
180      ALOGE("b/27207275: need %zu, got %u", sizeof(*a), a->nSize);
181      android_errorWriteLog(0x534e4554, "27207275");
182      return false;
183  }
184  return true;
185}
186
187}  // namespace android
188
189#endif  // SOFT_OMX_COMPONENT_H_
190