]> git.sesse.net Git - vlc/blob - modules/codec/omxil/iomx.cpp
iomx: simplify param/config size getter.
[vlc] / modules / codec / omxil / iomx.cpp
1 /*****************************************************************************
2  * iomx.cpp: OpenMAX interface implementation based on IOMX
3  *****************************************************************************
4  * Copyright (C) 2011 VLC authors and VideoLAN
5  *
6  * Authors: Martin Storsjo <martin@martin.st>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 #include <media/stagefright/OMXClient.h>
28 #include <media/IOMX.h>
29 #include <binder/MemoryDealer.h>
30 #include <OMX_Component.h>
31
32 #define PREFIX(x) I ## x
33
34 using namespace android;
35
36 class IOMXContext {
37 public:
38     IOMXContext() {
39     }
40
41     sp<IOMX> iomx;
42     List<IOMX::ComponentInfo> components;
43 };
44
45 static IOMXContext *ctx;
46
47 class OMXNode;
48
49 class OMXCodecObserver : public BnOMXObserver {
50 public:
51     OMXCodecObserver() {
52         node = NULL;
53     }
54     void setNode(OMXNode* n) {
55         node = n;
56     }
57     void onMessage(const omx_message &msg);
58     void registerBuffers(const sp<IMemoryHeap> &) {
59     }
60 private:
61     OMXNode *node;
62 };
63
64 class OMXNode {
65 public:
66     IOMX::node_id node;
67     sp<OMXCodecObserver> observer;
68     OMX_CALLBACKTYPE callbacks;
69     OMX_PTR app_data;
70     OMX_STATETYPE state;
71     List<OMX_BUFFERHEADERTYPE*> buffers;
72     OMX_HANDLETYPE handle;
73     String8 component_name;
74 };
75
76 class OMXBuffer {
77 public:
78     sp<MemoryDealer> dealer;
79     IOMX::buffer_id id;
80 };
81
82 void OMXCodecObserver::onMessage(const omx_message &msg)
83 {
84     if (!node)
85         return;
86     switch (msg.type) {
87     case omx_message::EVENT:
88         // TODO: Needs locking
89         if (msg.u.event_data.event == OMX_EventCmdComplete && msg.u.event_data.data1 == OMX_CommandStateSet)
90             node->state = (OMX_STATETYPE) msg.u.event_data.data2;
91         node->callbacks.EventHandler(node->handle, node->app_data, msg.u.event_data.event, msg.u.event_data.data1, msg.u.event_data.data2, NULL);
92         break;
93     case omx_message::EMPTY_BUFFER_DONE:
94         for( List<OMX_BUFFERHEADERTYPE*>::iterator it = node->buffers.begin(); it != node->buffers.end(); it++ ) {
95             OMXBuffer* info = (OMXBuffer*) (*it)->pPlatformPrivate;
96             if (msg.u.buffer_data.buffer == info->id) {
97                 node->callbacks.EmptyBufferDone(node->handle, node->app_data, *it);
98                 break;
99             }
100         }
101         break;
102     case omx_message::FILL_BUFFER_DONE:
103         for( List<OMX_BUFFERHEADERTYPE*>::iterator it = node->buffers.begin(); it != node->buffers.end(); it++ ) {
104             OMXBuffer* info = (OMXBuffer*) (*it)->pPlatformPrivate;
105             if (msg.u.extended_buffer_data.buffer == info->id) {
106                 OMX_BUFFERHEADERTYPE *buffer = *it;
107                 buffer->nOffset = msg.u.extended_buffer_data.range_offset;
108                 buffer->nFilledLen = msg.u.extended_buffer_data.range_length;
109                 buffer->nFlags = msg.u.extended_buffer_data.flags;
110                 buffer->nTimeStamp = msg.u.extended_buffer_data.timestamp;
111                 node->callbacks.FillBufferDone(node->handle, node->app_data, buffer);
112                 break;
113             }
114         }
115         break;
116     default:
117         break;
118     }
119 }
120
121 static OMX_ERRORTYPE get_error(status_t err)
122 {
123     if (err == OK)
124         return OMX_ErrorNone;
125     return OMX_ErrorUndefined;
126 }
127
128 static OMX_ERRORTYPE iomx_send_command(OMX_HANDLETYPE component, OMX_COMMANDTYPE command, OMX_U32 param1, OMX_PTR)
129 {
130     OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
131     return get_error(ctx->iomx->sendCommand(node->node, command, param1));
132 }
133
134 static OMX_ERRORTYPE iomx_get_parameter(OMX_HANDLETYPE component, OMX_INDEXTYPE param_index, OMX_PTR param)
135 {
136     OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
137     return get_error(ctx->iomx->getParameter(node->node, param_index, param, *(OMX_U32*)param));
138 }
139
140 static OMX_ERRORTYPE iomx_set_parameter(OMX_HANDLETYPE component, OMX_INDEXTYPE param_index, OMX_PTR param)
141 {
142     OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
143     return get_error(ctx->iomx->setParameter(node->node, param_index, param, *(OMX_U32*)param));
144 }
145
146 static OMX_ERRORTYPE iomx_get_state(OMX_HANDLETYPE component, OMX_STATETYPE *ptr) {
147     OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
148     *ptr = node->state;
149     return OMX_ErrorNone;
150 }
151
152 static OMX_ERRORTYPE iomx_allocate_buffer(OMX_HANDLETYPE component, OMX_BUFFERHEADERTYPE **bufferptr, OMX_U32 port_index, OMX_PTR app_private, OMX_U32 size)
153 {
154     OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
155     OMXBuffer* info = new OMXBuffer;
156     info->dealer = new MemoryDealer(size + 4096); // Do we need to keep this around, or is it kept alive via the IMemory that references it?
157     sp<IMemory> mem = info->dealer->allocate(size);
158     int ret = ctx->iomx->allocateBufferWithBackup(node->node, port_index, mem, &info->id);
159     if (ret != OK)
160         return OMX_ErrorUndefined;
161     OMX_BUFFERHEADERTYPE *buffer = (OMX_BUFFERHEADERTYPE*) calloc(1, sizeof(OMX_BUFFERHEADERTYPE));
162     *bufferptr = buffer;
163     buffer->pPlatformPrivate = info;
164     buffer->pAppPrivate = app_private;
165     buffer->nAllocLen = size;
166     buffer->pBuffer = (OMX_U8*) mem->pointer();
167     node->buffers.push_back(buffer);
168     return OMX_ErrorNone;
169 }
170
171 static OMX_ERRORTYPE iomx_free_buffer(OMX_HANDLETYPE component, OMX_U32 port, OMX_BUFFERHEADERTYPE *buffer)
172 {
173     OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
174     OMXBuffer* info = (OMXBuffer*) buffer->pPlatformPrivate;
175     status_t ret = ctx->iomx->freeBuffer(node->node, port, info->id);
176     for( List<OMX_BUFFERHEADERTYPE*>::iterator it = node->buffers.begin(); it != node->buffers.end(); it++ ) {
177         if (buffer == *it) {
178             node->buffers.erase(it);
179             break;
180         }
181     }
182     free(buffer);
183     delete info;
184     return get_error(ret);
185 }
186
187 static OMX_ERRORTYPE iomx_empty_this_buffer(OMX_HANDLETYPE component, OMX_BUFFERHEADERTYPE *buffer)
188 {
189     OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
190     OMXBuffer* info = (OMXBuffer*) buffer->pPlatformPrivate;
191     return get_error(ctx->iomx->emptyBuffer(node->node, info->id, buffer->nOffset, buffer->nFilledLen, buffer->nFlags, buffer->nTimeStamp));
192 }
193
194 static OMX_ERRORTYPE iomx_fill_this_buffer(OMX_HANDLETYPE component, OMX_BUFFERHEADERTYPE *buffer)
195 {
196     OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
197     OMXBuffer* info = (OMXBuffer*) buffer->pPlatformPrivate;
198     return get_error(ctx->iomx->fillBuffer(node->node, info->id));
199 }
200
201 static OMX_ERRORTYPE iomx_component_role_enum(OMX_HANDLETYPE component, OMX_U8 *role, OMX_U32 index)
202 {
203     OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
204     for( List<IOMX::ComponentInfo>::iterator it = ctx->components.begin(); it != ctx->components.end(); it++ ) {
205         if (node->component_name == it->mName) {
206             if (index >= it->mRoles.size())
207                 return OMX_ErrorNoMore;
208             List<String8>::iterator it2 = it->mRoles.begin();
209             for( OMX_U32 i = 0; it2 != it->mRoles.end() && i < index; i++, it2++ ) ;
210             strncpy((char*)role, it2->string(), OMX_MAX_STRINGNAME_SIZE);
211             if (it2->length() >= OMX_MAX_STRINGNAME_SIZE)
212                 role[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
213             return OMX_ErrorNone;
214         }
215     }
216     return OMX_ErrorInvalidComponentName;
217 }
218
219 static OMX_ERRORTYPE iomx_get_extension_index(OMX_HANDLETYPE component, OMX_STRING parameter, OMX_INDEXTYPE *index)
220 {
221     OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
222     return get_error(ctx->iomx->getExtensionIndex(node->node, parameter, index));
223 }
224
225 static OMX_ERRORTYPE iomx_set_config(OMX_HANDLETYPE component, OMX_INDEXTYPE index, OMX_PTR param)
226 {
227     OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
228     return get_error(ctx->iomx->setConfig(node->node, index, param, *(OMX_U32*)param));
229 }
230
231 static OMX_ERRORTYPE iomx_get_config(OMX_HANDLETYPE component, OMX_INDEXTYPE index, OMX_PTR param)
232 {
233     OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
234     return get_error(ctx->iomx->getConfig(node->node, index, param, *(OMX_U32*)param));
235 }
236
237 extern "C" {
238 OMX_ERRORTYPE PREFIX(OMX_GetHandle)(OMX_HANDLETYPE *handle_ptr, OMX_STRING component_name, OMX_PTR app_data, OMX_CALLBACKTYPE *callbacks)
239 {
240     OMXNode* node = new OMXNode();
241     node->app_data = app_data;
242     node->callbacks = *callbacks;
243     node->observer = new OMXCodecObserver();
244     node->observer->setNode(node);
245     node->state = OMX_StateLoaded;
246     node->component_name = component_name;
247
248     OMX_COMPONENTTYPE* component = (OMX_COMPONENTTYPE*) malloc(sizeof(OMX_COMPONENTTYPE));
249     memset(component, 0, sizeof(OMX_COMPONENTTYPE));
250     component->nSize = sizeof(OMX_COMPONENTTYPE);
251     component->nVersion.s.nVersionMajor = 1;
252     component->nVersion.s.nVersionMinor = 1;
253     component->nVersion.s.nRevision = 0;
254     component->nVersion.s.nStep = 0;
255     component->pComponentPrivate = node;
256     component->SendCommand = iomx_send_command;
257     component->GetParameter = iomx_get_parameter;
258     component->SetParameter = iomx_set_parameter;
259     component->FreeBuffer = iomx_free_buffer;
260     component->EmptyThisBuffer = iomx_empty_this_buffer;
261     component->FillThisBuffer = iomx_fill_this_buffer;
262     component->GetState = iomx_get_state;
263     component->AllocateBuffer = iomx_allocate_buffer;
264     component->ComponentRoleEnum = iomx_component_role_enum;
265     component->GetExtensionIndex = iomx_get_extension_index;
266     component->SetConfig = iomx_set_config;
267     component->GetConfig = iomx_get_config;
268
269     *handle_ptr = component;
270     node->handle = component;
271     status_t ret;
272     if ((ret = ctx->iomx->allocateNode( component_name, node->observer, &node->node )) != OK)
273         return OMX_ErrorUndefined;
274     return OMX_ErrorNone;
275 }
276
277 OMX_ERRORTYPE PREFIX(OMX_FreeHandle)(OMX_HANDLETYPE handle)
278 {
279     OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)handle)->pComponentPrivate;
280     ctx->iomx->freeNode( node->node );
281     node->observer->setNode(NULL);
282     delete node;
283     free(handle);
284     return OMX_ErrorNone;
285 }
286
287 OMX_ERRORTYPE PREFIX(OMX_Init)(void)
288 {
289     OMXClient client;
290     if (client.connect() != OK)
291         return OMX_ErrorUndefined;
292
293     if (!ctx)
294         ctx = new IOMXContext();
295     ctx->iomx = client.interface();
296     ctx->iomx->listNodes(&ctx->components);
297     return OMX_ErrorNone;
298 }
299
300 OMX_ERRORTYPE PREFIX(OMX_Deinit)(void)
301 {
302     ctx->iomx = NULL;
303     delete ctx;
304     ctx = NULL;
305     return OMX_ErrorNone;
306 }
307
308 OMX_ERRORTYPE PREFIX(OMX_ComponentNameEnum)(OMX_STRING component_name, OMX_U32 name_length, OMX_U32 index)
309 {
310     if (index >= ctx->components.size())
311         return OMX_ErrorNoMore;
312     List<IOMX::ComponentInfo>::iterator it = ctx->components.begin();
313     for( OMX_U32 i = 0; i < index; i++ )
314         it++;
315     strncpy(component_name, it->mName.string(), name_length);
316     component_name[name_length - 1] = '\0';
317     return OMX_ErrorNone;
318 }
319
320 OMX_ERRORTYPE PREFIX(OMX_GetRolesOfComponent)(OMX_STRING component_name, OMX_U32 *num_roles, OMX_U8 **roles)
321 {
322     for( List<IOMX::ComponentInfo>::iterator it = ctx->components.begin(); it != ctx->components.end(); it++ ) {
323         if (!strcmp(component_name, it->mName.string())) {
324             if (!roles) {
325                 *num_roles = it->mRoles.size();
326                 return OMX_ErrorNone;
327             }
328             if (*num_roles < it->mRoles.size())
329                 return OMX_ErrorInsufficientResources;
330             *num_roles = it->mRoles.size();
331             OMX_U32 i = 0;
332             for( List<String8>::iterator it2 = it->mRoles.begin(); it2 != it->mRoles.end(); i++, it2++ ) {
333                 strncpy((char*)roles[i], it2->string(), OMX_MAX_STRINGNAME_SIZE);
334                 roles[i][OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
335             }
336             return OMX_ErrorNone;
337         }
338     }
339     return OMX_ErrorInvalidComponentName;
340 }
341
342 OMX_ERRORTYPE PREFIX(OMX_GetComponentsOfRole)(OMX_STRING role, OMX_U32 *num_comps, OMX_U8 **comp_names)
343 {
344     OMX_U32 i = 0;
345     for( List<IOMX::ComponentInfo>::iterator it = ctx->components.begin(); it != ctx->components.end(); it++ ) {
346         for( List<String8>::iterator it2 = it->mRoles.begin(); it2 != it->mRoles.end(); it2++ ) {
347             if (!strcmp(it2->string(), role)) {
348                 if (comp_names) {
349                     if (*num_comps < i)
350                         return OMX_ErrorInsufficientResources;
351                     strncpy((char*)comp_names[i], it->mName.string(), OMX_MAX_STRINGNAME_SIZE);
352                     comp_names[i][OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
353                 }
354                 i++;
355                 break;
356             }
357         }
358     }
359     *num_comps = i;
360     return OMX_ErrorNone;
361 }
362 }
363