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