1 /*****************************************************************************
2 * iomx.cpp: OpenMAX interface implementation based on IOMX
3 *****************************************************************************
4 * Copyright (C) 2011 VLC authors and VideoLAN
6 * Authors: Martin Storsjo <martin@martin.st>
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.
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.
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 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
27 #include <media/stagefright/OMXClient.h>
28 #include <media/IOMX.h>
29 #include <binder/MemoryDealer.h>
30 #include <OMX_Component.h>
32 #define PREFIX(x) I ## x
34 using namespace android;
42 List<IOMX::ComponentInfo> components;
45 static IOMXContext *ctx;
49 class OMXCodecObserver : public BnOMXObserver {
54 void setNode(OMXNode* n) {
57 void onMessage(const omx_message &msg);
58 void registerBuffers(const sp<IMemoryHeap> &) {
67 sp<OMXCodecObserver> observer;
68 OMX_CALLBACKTYPE callbacks;
71 List<OMX_BUFFERHEADERTYPE*> buffers;
72 OMX_HANDLETYPE handle;
73 String8 component_name;
78 sp<MemoryDealer> dealer;
82 void OMXCodecObserver::onMessage(const omx_message &msg)
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);
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);
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);
121 static OMX_ERRORTYPE get_error(status_t err)
124 return OMX_ErrorNone;
125 return OMX_ErrorUndefined;
128 static OMX_ERRORTYPE iomx_send_command(OMX_HANDLETYPE component, OMX_COMMANDTYPE command, OMX_U32 param1, OMX_PTR)
130 OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
131 return get_error(ctx->iomx->sendCommand(node->node, command, param1));
134 static OMX_ERRORTYPE iomx_get_parameter(OMX_HANDLETYPE component, OMX_INDEXTYPE param_index, OMX_PTR param)
136 OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
137 return get_error(ctx->iomx->getParameter(node->node, param_index, param, *(OMX_U32*)param));
140 static OMX_ERRORTYPE iomx_set_parameter(OMX_HANDLETYPE component, OMX_INDEXTYPE param_index, OMX_PTR param)
142 OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
143 return get_error(ctx->iomx->setParameter(node->node, param_index, param, *(OMX_U32*)param));
146 static OMX_ERRORTYPE iomx_get_state(OMX_HANDLETYPE component, OMX_STATETYPE *ptr) {
147 OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
149 return OMX_ErrorNone;
152 static OMX_ERRORTYPE iomx_allocate_buffer(OMX_HANDLETYPE component, OMX_BUFFERHEADERTYPE **bufferptr, OMX_U32 port_index, OMX_PTR app_private, OMX_U32 size)
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);
160 return OMX_ErrorUndefined;
161 OMX_BUFFERHEADERTYPE *buffer = (OMX_BUFFERHEADERTYPE*) calloc(1, sizeof(OMX_BUFFERHEADERTYPE));
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;
171 static OMX_ERRORTYPE iomx_free_buffer(OMX_HANDLETYPE component, OMX_U32 port, OMX_BUFFERHEADERTYPE *buffer)
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++ ) {
178 node->buffers.erase(it);
184 return get_error(ret);
187 static OMX_ERRORTYPE iomx_empty_this_buffer(OMX_HANDLETYPE component, OMX_BUFFERHEADERTYPE *buffer)
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));
194 static OMX_ERRORTYPE iomx_fill_this_buffer(OMX_HANDLETYPE component, OMX_BUFFERHEADERTYPE *buffer)
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));
201 static OMX_ERRORTYPE iomx_component_role_enum(OMX_HANDLETYPE component, OMX_U8 *role, OMX_U32 index)
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;
216 return OMX_ErrorInvalidComponentName;
219 static OMX_ERRORTYPE iomx_get_extension_index(OMX_HANDLETYPE component, OMX_STRING parameter, OMX_INDEXTYPE *index)
221 OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
222 return get_error(ctx->iomx->getExtensionIndex(node->node, parameter, index));
225 static OMX_ERRORTYPE iomx_set_config(OMX_HANDLETYPE component, OMX_INDEXTYPE index, OMX_PTR param)
227 OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
228 return get_error(ctx->iomx->setConfig(node->node, index, param, *(OMX_U32*)param));
231 static OMX_ERRORTYPE iomx_get_config(OMX_HANDLETYPE component, OMX_INDEXTYPE index, OMX_PTR param)
233 OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)component)->pComponentPrivate;
234 return get_error(ctx->iomx->getConfig(node->node, index, param, *(OMX_U32*)param));
238 OMX_ERRORTYPE PREFIX(OMX_GetHandle)(OMX_HANDLETYPE *handle_ptr, OMX_STRING component_name, OMX_PTR app_data, OMX_CALLBACKTYPE *callbacks)
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;
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;
269 *handle_ptr = component;
270 node->handle = component;
272 if ((ret = ctx->iomx->allocateNode( component_name, node->observer, &node->node )) != OK)
273 return OMX_ErrorUndefined;
274 return OMX_ErrorNone;
277 OMX_ERRORTYPE PREFIX(OMX_FreeHandle)(OMX_HANDLETYPE handle)
279 OMXNode* node = (OMXNode*) ((OMX_COMPONENTTYPE*)handle)->pComponentPrivate;
280 ctx->iomx->freeNode( node->node );
281 node->observer->setNode(NULL);
284 return OMX_ErrorNone;
287 OMX_ERRORTYPE PREFIX(OMX_Init)(void)
290 if (client.connect() != OK)
291 return OMX_ErrorUndefined;
294 ctx = new IOMXContext();
295 ctx->iomx = client.interface();
296 ctx->iomx->listNodes(&ctx->components);
297 return OMX_ErrorNone;
300 OMX_ERRORTYPE PREFIX(OMX_Deinit)(void)
305 return OMX_ErrorNone;
308 OMX_ERRORTYPE PREFIX(OMX_ComponentNameEnum)(OMX_STRING component_name, OMX_U32 name_length, OMX_U32 index)
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++ )
315 strncpy(component_name, it->mName.string(), name_length);
316 component_name[name_length - 1] = '\0';
317 return OMX_ErrorNone;
320 OMX_ERRORTYPE PREFIX(OMX_GetRolesOfComponent)(OMX_STRING component_name, OMX_U32 *num_roles, OMX_U8 **roles)
322 for( List<IOMX::ComponentInfo>::iterator it = ctx->components.begin(); it != ctx->components.end(); it++ ) {
323 if (!strcmp(component_name, it->mName.string())) {
325 *num_roles = it->mRoles.size();
326 return OMX_ErrorNone;
328 if (*num_roles < it->mRoles.size())
329 return OMX_ErrorInsufficientResources;
330 *num_roles = it->mRoles.size();
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';
336 return OMX_ErrorNone;
339 return OMX_ErrorInvalidComponentName;
342 OMX_ERRORTYPE PREFIX(OMX_GetComponentsOfRole)(OMX_STRING role, OMX_U32 *num_comps, OMX_U8 **comp_names)
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)) {
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';
360 return OMX_ErrorNone;