componentbase.cpp revision 6790df551efca72bca4fb25bd53a3b7259ecb3f7
1/*
2 * Copyright (C) 2009 Wind River Systems.
3 */
4
5#include <stdlib.h>
6#include <string.h>
7
8#include <pthread.h>
9
10#include <OMX_Core.h>
11#include <OMX_Component.h>
12
13#include <componentbase.h>
14
15#include <queue.h>
16#include <workqueue.h>
17
18#define LOG_TAG "componentbase"
19#include <log.h>
20
21/*
22 * CmdProcessWork
23 */
24CmdProcessWork::CmdProcessWork(CmdHandlerInterface *ci)
25{
26    this->ci = ci;
27
28    workq = new WorkQueue;
29
30    __queue_init(&q);
31    pthread_mutex_init(&lock, NULL);
32}
33
34CmdProcessWork::~CmdProcessWork()
35{
36    workq->FlushWork();
37    delete workq;
38
39    pthread_mutex_lock(&lock);
40    queue_free_all(&q);
41    pthread_mutex_unlock(&lock);
42
43    pthread_mutex_destroy(&lock);
44}
45
46OMX_ERRORTYPE CmdProcessWork::PushCmdQueue(struct cmd_s *cmd)
47{
48    int ret;
49
50    pthread_mutex_lock(&lock);
51    ret = queue_push_tail(&q, cmd);
52    if (ret) {
53        pthread_mutex_unlock(&lock);
54        return OMX_ErrorInsufficientResources;
55    }
56
57    workq->ScheduleWork(this);
58    pthread_mutex_unlock(&lock);
59
60    return OMX_ErrorNone;
61}
62
63struct cmd_s *CmdProcessWork::PopCmdQueue(void)
64{
65    struct cmd_s *cmd;
66
67    pthread_mutex_lock(&lock);
68    cmd = (struct cmd_s *)queue_pop_head(&q);
69    pthread_mutex_unlock(&lock);
70
71    return cmd;
72}
73
74void CmdProcessWork::ScheduleIfAvailable(void)
75{
76    bool avail;
77
78    pthread_mutex_lock(&lock);
79    avail = queue_length(&q) ? true : false;
80    pthread_mutex_unlock(&lock);
81
82    if (avail)
83        workq->ScheduleWork(this);
84}
85
86void CmdProcessWork::Work(void)
87{
88    struct cmd_s *cmd;
89
90    cmd = PopCmdQueue();
91    if (cmd) {
92        ci->CmdHandler(cmd);
93        free(cmd);
94    }
95    ScheduleIfAvailable();
96}
97
98/* end of CmdProcessWork */
99
100/*
101 * ComponentBase
102 */
103/*
104 * constructor & destructor
105 */
106void ComponentBase::__ComponentBase(void)
107{
108    memset(name, 0, OMX_MAX_STRINGNAME_SIZE);
109    cmodule = NULL;
110    handle = NULL;
111
112    roles = NULL;
113    nr_roles = 0;
114
115    ports = NULL;
116    nr_ports = 0;
117    memset(&portparam, 0, sizeof(portparam));
118
119    cmdwork = new CmdProcessWork(this);
120}
121
122ComponentBase::ComponentBase()
123{
124    __ComponentBase();
125}
126
127ComponentBase::ComponentBase(const OMX_STRING name)
128{
129    __ComponentBase();
130    SetName(name);
131}
132
133ComponentBase::~ComponentBase()
134{
135    delete cmdwork;
136
137    if (roles) {
138        OMX_U32 i;
139
140        for (i = 0; i < nr_roles; i++)
141            free(roles[i]);
142
143        free(roles);
144    }
145}
146
147/* end of constructor & destructor */
148
149/*
150 * accessor
151 */
152/* name */
153void ComponentBase::SetName(const OMX_STRING name)
154{
155    strncpy(this->name, name, OMX_MAX_STRINGNAME_SIZE);
156    this->name[OMX_MAX_STRINGNAME_SIZE-1] = '\0';
157}
158
159const OMX_STRING ComponentBase::GetName(void)
160{
161    return name;
162}
163
164/* component module */
165void ComponentBase::SetCModule(CModule *cmodule)
166{
167    this->cmodule = cmodule;
168}
169
170CModule *ComponentBase::GetCModule(void)
171{
172    return cmodule;
173}
174
175/* end of accessor */
176
177/*
178 * core methods & helpers
179 */
180/* roles */
181OMX_ERRORTYPE ComponentBase::SetRolesOfComponent(OMX_U32 nr_roles,
182                                                 const OMX_U8 **roles)
183{
184    OMX_U32 i;
185
186    this->roles = (OMX_U8 **)malloc(sizeof(OMX_STRING) * nr_roles);
187    if (!this->roles)
188        return OMX_ErrorInsufficientResources;
189
190    for (i = 0; i < nr_roles; i++) {
191        this->roles[i] = (OMX_U8 *)malloc(OMX_MAX_STRINGNAME_SIZE);
192        if (!this->roles[i]) {
193            int j;
194
195            for (j = (int )i-1; j >= 0; j--)
196                free(this->roles[j]);
197            free(this->roles);
198
199            return OMX_ErrorInsufficientResources;
200        }
201
202        strncpy((OMX_STRING)&this->roles[i][0],
203                (const OMX_STRING)&roles[i][0],
204                OMX_MAX_STRINGNAME_SIZE);
205    }
206
207    this->nr_roles = nr_roles;
208    return OMX_ErrorNone;
209}
210
211OMX_ERRORTYPE ComponentBase::GetRolesOfComponent(OMX_U32 *nr_roles,
212                                                 OMX_U8 **roles)
213{
214    OMX_U32 i;
215    OMX_U32 this_nr_roles = this->nr_roles;
216
217    if (!roles) {
218        *nr_roles = this_nr_roles;
219        return OMX_ErrorNone;
220    }
221
222    if (!nr_roles || (*nr_roles != this_nr_roles))
223        return OMX_ErrorBadParameter;
224
225    for (i = 0; i < this_nr_roles; i++) {
226        if (!roles[i])
227            break;
228
229        if (roles && roles[i])
230            strncpy((OMX_STRING)&roles[i][0],
231                    (const OMX_STRING)&this->roles[i][0],
232                    OMX_MAX_STRINGNAME_SIZE);
233    }
234
235    if (i != this_nr_roles)
236        return OMX_ErrorBadParameter;
237
238    *nr_roles = this_nr_roles;
239    return OMX_ErrorNone;
240}
241
242bool ComponentBase::QueryHavingThisRole(const OMX_STRING role)
243{
244    OMX_U32 i;
245
246    if (!roles || !role)
247        return false;
248
249    for (i = 0; i < nr_roles; i++) {
250        if (!strcmp((OMX_STRING)&roles[i][0], role))
251            return true;
252    }
253
254    return false;
255}
256
257/* GetHandle & FreeHandle */
258OMX_ERRORTYPE ComponentBase::GetHandle(OMX_HANDLETYPE *pHandle,
259                                       OMX_PTR pAppData,
260                                       OMX_CALLBACKTYPE *pCallBacks)
261{
262    OMX_ERRORTYPE ret;
263
264    if (handle)
265        return OMX_ErrorUndefined;
266
267    handle = (OMX_COMPONENTTYPE *)calloc(1, sizeof(*handle));
268    if (!handle)
269        return OMX_ErrorInsufficientResources;
270
271    /* handle initialization */
272    SetTypeHeader(handle, sizeof(*handle));
273    handle->pComponentPrivate = static_cast<OMX_PTR>(this);
274    handle->pApplicationPrivate = pAppData;
275
276    /* virtual - see derived class */
277    ret = InitComponent();
278    if (ret != OMX_ErrorNone) {
279        LOGE("failed to %s::InitComponent(), ret = 0x%08x\n",
280             name, ret);
281        goto free_handle;
282    }
283
284    /* connect handle's functions */
285    handle->GetComponentVersion = GetComponentVersion;
286    handle->SendCommand = SendCommand;
287    handle->GetParameter = GetParameter;
288    handle->SetParameter = SetParameter;
289    handle->GetConfig = GetConfig;
290    handle->SetConfig = SetConfig;
291    handle->GetExtensionIndex = GetExtensionIndex;
292    handle->GetState = GetState;
293    handle->ComponentTunnelRequest = ComponentTunnelRequest;
294    handle->UseBuffer = UseBuffer;
295    handle->AllocateBuffer = AllocateBuffer;
296    handle->FreeBuffer = FreeBuffer;
297    handle->EmptyThisBuffer = EmptyThisBuffer;
298    handle->FillThisBuffer = FillThisBuffer;
299    handle->SetCallbacks = SetCallbacks;
300    handle->ComponentDeInit = ComponentDeInit;
301    handle->UseEGLImage = UseEGLImage;
302    handle->ComponentRoleEnum = ComponentRoleEnum;
303
304    appdata = pAppData;
305    callbacks = pCallBacks;
306    *pHandle = (OMX_HANDLETYPE *)handle;
307
308    return OMX_ErrorNone;
309
310free_handle:
311    free(this->handle);
312    this->handle = NULL;
313
314    return ret;
315}
316
317OMX_ERRORTYPE ComponentBase::FreeHandle(OMX_HANDLETYPE hComponent)
318{
319    OMX_ERRORTYPE ret;
320
321    if (hComponent != handle)
322        return OMX_ErrorBadParameter;
323
324    /* virtual - see derived class */
325    ret = ExitComponent();
326    if (ret != OMX_ErrorNone)
327        return ret;
328
329    free(handle);
330
331    appdata = NULL;
332    callbacks = NULL;
333
334    return OMX_ErrorNone;
335}
336
337/* end of core methods & helpers */
338
339/*
340 * component methods & helpers
341 */
342OMX_ERRORTYPE ComponentBase::GetComponentVersion(
343    OMX_IN  OMX_HANDLETYPE hComponent,
344    OMX_OUT OMX_STRING pComponentName,
345    OMX_OUT OMX_VERSIONTYPE* pComponentVersion,
346    OMX_OUT OMX_VERSIONTYPE* pSpecVersion,
347    OMX_OUT OMX_UUIDTYPE* pComponentUUID)
348{
349    ComponentBase *cbase;
350
351    if (!hComponent)
352        return OMX_ErrorBadParameter;
353
354    cbase = static_cast<ComponentBase *>
355        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
356    if (!cbase)
357        return OMX_ErrorBadParameter;
358
359    return cbase->CBaseGetComponentVersion(hComponent,
360                                           pComponentName,
361                                           pComponentVersion,
362                                           pSpecVersion,
363                                           pComponentUUID);
364}
365
366OMX_ERRORTYPE ComponentBase::CBaseGetComponentVersion(
367    OMX_IN  OMX_HANDLETYPE hComponent,
368    OMX_OUT OMX_STRING pComponentName,
369    OMX_OUT OMX_VERSIONTYPE* pComponentVersion,
370    OMX_OUT OMX_VERSIONTYPE* pSpecVersion,
371    OMX_OUT OMX_UUIDTYPE* pComponentUUID)
372{
373    /*
374     * Todo
375     */
376
377    return OMX_ErrorNotImplemented;
378}
379
380OMX_ERRORTYPE ComponentBase::SendCommand(
381    OMX_IN  OMX_HANDLETYPE hComponent,
382    OMX_IN  OMX_COMMANDTYPE Cmd,
383    OMX_IN  OMX_U32 nParam1,
384    OMX_IN  OMX_PTR pCmdData)
385{
386    ComponentBase *cbase;
387
388    if (!hComponent)
389        return OMX_ErrorBadParameter;
390
391    cbase = static_cast<ComponentBase *>
392        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
393    if (!cbase)
394        return OMX_ErrorBadParameter;
395
396    return cbase->CBaseSendCommand(hComponent, Cmd, nParam1, pCmdData);
397}
398
399OMX_ERRORTYPE ComponentBase::CBaseSendCommand(
400    OMX_IN  OMX_HANDLETYPE hComponent,
401    OMX_IN  OMX_COMMANDTYPE Cmd,
402    OMX_IN  OMX_U32 nParam1,
403    OMX_IN  OMX_PTR pCmdData)
404{
405    struct cmd_s *cmd;
406
407    if (hComponent != handle)
408        return OMX_ErrorInvalidComponent;
409
410    /* basic error check */
411    switch (Cmd) {
412    case OMX_CommandStateSet:
413        /*
414         * Todo
415         */
416        break;
417    case OMX_CommandFlush:
418        /*
419         * Todo
420         */
421        break;
422    case OMX_CommandPortDisable:
423        /*
424         * Todo
425         */
426        break;
427    case OMX_CommandPortEnable:
428        /*
429         * Todo
430         */
431        break;
432    case OMX_CommandMarkBuffer:
433        /*
434         * Todo
435         */
436        break;
437    default:
438        LOGE("command %d not supported\n", Cmd);
439        return OMX_ErrorUnsupportedIndex;
440    }
441
442    cmd = (struct cmd_s *)malloc(sizeof(*cmd));
443    if (!cmd)
444        return OMX_ErrorInsufficientResources;
445
446    cmd->cmd = Cmd;
447    cmd->param1 = nParam1;
448    cmd->cmddata = pCmdData;
449
450    return cmdwork->PushCmdQueue(cmd);
451}
452
453OMX_ERRORTYPE ComponentBase::GetParameter(
454    OMX_IN  OMX_HANDLETYPE hComponent,
455    OMX_IN  OMX_INDEXTYPE nParamIndex,
456    OMX_INOUT OMX_PTR pComponentParameterStructure)
457{
458    ComponentBase *cbase;
459
460    if (!hComponent)
461        return OMX_ErrorBadParameter;
462
463    cbase = static_cast<ComponentBase *>
464        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
465    if (!cbase)
466        return OMX_ErrorBadParameter;
467
468    return cbase->CBaseGetParameter(hComponent, nParamIndex,
469                                    pComponentParameterStructure);
470}
471
472OMX_ERRORTYPE ComponentBase::CBaseGetParameter(
473    OMX_IN  OMX_HANDLETYPE hComponent,
474    OMX_IN  OMX_INDEXTYPE nParamIndex,
475    OMX_INOUT OMX_PTR pComponentParameterStructure)
476{
477    OMX_ERRORTYPE ret = OMX_ErrorNone;
478
479    if (hComponent != handle)
480        return OMX_ErrorBadParameter;
481
482    switch (nParamIndex) {
483    case OMX_IndexParamAudioInit:
484    case OMX_IndexParamVideoInit:
485    case OMX_IndexParamImageInit:
486    case OMX_IndexParamOtherInit: {
487        OMX_PORT_PARAM_TYPE *p =
488            (OMX_PORT_PARAM_TYPE *)pComponentParameterStructure;
489
490        memcpy(p, &portparam, sizeof(*p));
491        break;
492    }
493    case OMX_IndexParamPortDefinition: {
494        OMX_PARAM_PORTDEFINITIONTYPE *p =
495            (OMX_PARAM_PORTDEFINITIONTYPE *)pComponentParameterStructure;
496        OMX_U32 index = p->nPortIndex;
497        PortBase *port = ports[index];
498
499        memcpy(p, port->GetPortParam(), sizeof(*p));
500        break;
501    }
502    case OMX_IndexParamAudioPortFormat: {
503        OMX_AUDIO_PARAM_PORTFORMATTYPE *p =
504            (OMX_AUDIO_PARAM_PORTFORMATTYPE *)pComponentParameterStructure;
505        OMX_U32 index = p->nPortIndex;
506        PortBase *port = ports[index];
507
508        memcpy(p, port->GetAudioPortParam(), sizeof(*p));
509        break;
510    }
511    case OMX_IndexParamCompBufferSupplier:
512        /*
513         * Todo
514         */
515
516        ret = OMX_ErrorUnsupportedIndex;
517        break;
518    default:
519        ret = ComponentGetParameter(nParamIndex, pComponentParameterStructure);
520    } /* switch */
521
522    return ret;
523}
524
525OMX_ERRORTYPE ComponentBase::SetParameter(
526    OMX_IN  OMX_HANDLETYPE hComponent,
527    OMX_IN  OMX_INDEXTYPE nIndex,
528    OMX_IN  OMX_PTR pComponentParameterStructure)
529{
530    ComponentBase *cbase;
531
532    if (!hComponent)
533        return OMX_ErrorBadParameter;
534
535    cbase = static_cast<ComponentBase *>
536        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
537    if (!cbase)
538        return OMX_ErrorBadParameter;
539
540    return cbase->CBaseSetParameter(hComponent, nIndex,
541                                    pComponentParameterStructure);
542}
543
544OMX_ERRORTYPE ComponentBase::CBaseSetParameter(
545    OMX_IN  OMX_HANDLETYPE hComponent,
546    OMX_IN  OMX_INDEXTYPE nIndex,
547    OMX_IN  OMX_PTR pComponentParameterStructure)
548{
549    OMX_ERRORTYPE ret = OMX_ErrorNone;
550
551    if (hComponent != handle)
552        return OMX_ErrorBadParameter;
553
554    switch (nIndex) {
555    case OMX_IndexParamAudioInit:
556    case OMX_IndexParamVideoInit:
557    case OMX_IndexParamImageInit:
558    case OMX_IndexParamOtherInit: {
559        OMX_PORT_PARAM_TYPE *p = (OMX_PORT_PARAM_TYPE *)
560            pComponentParameterStructure;
561
562        memcpy(&portparam, p, sizeof(*p));
563        break;
564    }
565    case OMX_IndexParamPortDefinition: {
566        OMX_PARAM_PORTDEFINITIONTYPE *p =
567            (OMX_PARAM_PORTDEFINITIONTYPE *)pComponentParameterStructure;
568        OMX_U32 index = p->nPortIndex;
569        PortBase *port = ports[index];
570
571        port->SetPortParam(p);
572        break;
573    }
574    case OMX_IndexParamAudioPortFormat: {
575        OMX_AUDIO_PARAM_PORTFORMATTYPE *p =
576            (OMX_AUDIO_PARAM_PORTFORMATTYPE *)pComponentParameterStructure;
577        OMX_U32 index = p->nPortIndex;
578        PortBase *port = ports[index];
579
580        port->SetAudioPortParam(p);
581        break;
582    }
583    case OMX_IndexParamCompBufferSupplier:
584        /*
585         * Todo
586         */
587
588        ret = OMX_ErrorUnsupportedIndex;
589        break;
590    default:
591        ret = ComponentSetParameter(nIndex, pComponentParameterStructure);
592    } /* switch */
593
594    return ret;
595}
596
597OMX_ERRORTYPE ComponentBase::GetConfig(
598    OMX_IN  OMX_HANDLETYPE hComponent,
599    OMX_IN  OMX_INDEXTYPE nIndex,
600    OMX_INOUT OMX_PTR pComponentConfigStructure)
601{
602    ComponentBase *cbase;
603
604    if (!hComponent)
605        return OMX_ErrorBadParameter;
606
607    cbase = static_cast<ComponentBase *>
608        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
609    if (!cbase)
610        return OMX_ErrorBadParameter;
611
612    return cbase->CBaseGetConfig(hComponent, nIndex,
613                                 pComponentConfigStructure);
614}
615
616OMX_ERRORTYPE ComponentBase::CBaseGetConfig(
617    OMX_IN  OMX_HANDLETYPE hComponent,
618    OMX_IN  OMX_INDEXTYPE nIndex,
619    OMX_INOUT OMX_PTR pComponentConfigStructure)
620{
621    OMX_ERRORTYPE ret;
622
623    if (hComponent != handle)
624        return OMX_ErrorBadParameter;
625
626    switch (nIndex) {
627    default:
628        ret = ComponentGetConfig(nIndex, pComponentConfigStructure);
629    }
630
631    return ret;
632}
633
634OMX_ERRORTYPE ComponentBase::SetConfig(
635    OMX_IN  OMX_HANDLETYPE hComponent,
636    OMX_IN  OMX_INDEXTYPE nIndex,
637    OMX_IN  OMX_PTR pComponentConfigStructure)
638{
639    ComponentBase *cbase;
640
641    if (!hComponent)
642        return OMX_ErrorBadParameter;
643
644    cbase = static_cast<ComponentBase *>
645        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
646    if (!cbase)
647        return OMX_ErrorBadParameter;
648
649    return cbase->CBaseSetConfig(hComponent, nIndex,
650                                 pComponentConfigStructure);
651}
652
653OMX_ERRORTYPE ComponentBase::CBaseSetConfig(
654    OMX_IN  OMX_HANDLETYPE hComponent,
655    OMX_IN  OMX_INDEXTYPE nIndex,
656    OMX_IN  OMX_PTR pComponentConfigStructure)
657{
658    OMX_ERRORTYPE ret;
659
660    if (hComponent != handle)
661        return OMX_ErrorBadParameter;
662
663    switch (nIndex) {
664    default:
665        ret = ComponentSetConfig(nIndex, pComponentConfigStructure);
666    }
667
668    return ret;
669}
670
671OMX_ERRORTYPE ComponentBase::GetExtensionIndex(
672    OMX_IN  OMX_HANDLETYPE hComponent,
673    OMX_IN  OMX_STRING cParameterName,
674    OMX_OUT OMX_INDEXTYPE* pIndexType)
675{
676    ComponentBase *cbase;
677
678    if (!hComponent)
679        return OMX_ErrorBadParameter;
680
681    cbase = static_cast<ComponentBase *>
682        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
683    if (!cbase)
684        return OMX_ErrorBadParameter;
685
686    return cbase->CBaseGetExtensionIndex(hComponent, cParameterName,
687                                         pIndexType);
688}
689
690OMX_ERRORTYPE ComponentBase::CBaseGetExtensionIndex(
691    OMX_IN  OMX_HANDLETYPE hComponent,
692    OMX_IN  OMX_STRING cParameterName,
693    OMX_OUT OMX_INDEXTYPE* pIndexType)
694{
695    /*
696     * Todo
697     */
698
699    return OMX_ErrorNotImplemented;
700}
701
702OMX_ERRORTYPE ComponentBase::GetState(
703    OMX_IN  OMX_HANDLETYPE hComponent,
704    OMX_OUT OMX_STATETYPE* pState)
705{
706    ComponentBase *cbase;
707
708    if (!hComponent)
709        return OMX_ErrorBadParameter;
710
711    cbase = static_cast<ComponentBase *>
712        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
713    if (!cbase)
714        return OMX_ErrorBadParameter;
715
716    return cbase->CBaseGetState(hComponent, pState);
717}
718
719OMX_ERRORTYPE ComponentBase::CBaseGetState(
720    OMX_IN  OMX_HANDLETYPE hComponent,
721    OMX_OUT OMX_STATETYPE* pState)
722{
723    /*
724     * Todo
725     */
726
727    return OMX_ErrorNotImplemented;
728}
729
730OMX_ERRORTYPE ComponentBase::ComponentTunnelRequest(
731    OMX_IN  OMX_HANDLETYPE hComponent,
732    OMX_IN  OMX_U32 nPort,
733    OMX_IN  OMX_HANDLETYPE hTunneledComponent,
734    OMX_IN  OMX_U32 nTunneledPort,
735    OMX_INOUT  OMX_TUNNELSETUPTYPE* pTunnelSetup)
736{
737    ComponentBase *cbase;
738
739    if (!hComponent)
740        return OMX_ErrorBadParameter;
741
742    cbase = static_cast<ComponentBase *>
743        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
744    if (!cbase)
745        return OMX_ErrorBadParameter;
746
747    return cbase->CBaseComponentTunnelRequest(hComponent, nPort,
748                                              hTunneledComponent,
749                                              nTunneledPort, pTunnelSetup);
750}
751
752OMX_ERRORTYPE ComponentBase::CBaseComponentTunnelRequest(
753    OMX_IN  OMX_HANDLETYPE hComp,
754    OMX_IN  OMX_U32 nPort,
755    OMX_IN  OMX_HANDLETYPE hTunneledComp,
756    OMX_IN  OMX_U32 nTunneledPort,
757    OMX_INOUT  OMX_TUNNELSETUPTYPE* pTunnelSetup)
758{
759    /*
760     * Todo
761     */
762
763    return OMX_ErrorNotImplemented;
764}
765
766OMX_ERRORTYPE ComponentBase::UseBuffer(
767    OMX_IN OMX_HANDLETYPE hComponent,
768    OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
769    OMX_IN OMX_U32 nPortIndex,
770    OMX_IN OMX_PTR pAppPrivate,
771    OMX_IN OMX_U32 nSizeBytes,
772    OMX_IN OMX_U8* pBuffer)
773{
774    ComponentBase *cbase;
775
776    if (!hComponent)
777        return OMX_ErrorBadParameter;
778
779    cbase = static_cast<ComponentBase *>
780        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
781    if (!cbase)
782        return OMX_ErrorBadParameter;
783
784    return cbase->CBaseUseBuffer(hComponent, ppBufferHdr, nPortIndex,
785                                 pAppPrivate, nSizeBytes, pBuffer);
786}
787
788OMX_ERRORTYPE ComponentBase::CBaseUseBuffer(
789    OMX_IN OMX_HANDLETYPE hComponent,
790    OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
791    OMX_IN OMX_U32 nPortIndex,
792    OMX_IN OMX_PTR pAppPrivate,
793    OMX_IN OMX_U32 nSizeBytes,
794    OMX_IN OMX_U8* pBuffer)
795{
796    /*
797     * Todo
798     */
799
800    return OMX_ErrorNotImplemented;
801}
802
803OMX_ERRORTYPE ComponentBase::AllocateBuffer(
804    OMX_IN OMX_HANDLETYPE hComponent,
805    OMX_INOUT OMX_BUFFERHEADERTYPE** ppBuffer,
806    OMX_IN OMX_U32 nPortIndex,
807    OMX_IN OMX_PTR pAppPrivate,
808    OMX_IN OMX_U32 nSizeBytes)
809{
810    ComponentBase *cbase;
811
812    if (!hComponent)
813        return OMX_ErrorBadParameter;
814
815    cbase = static_cast<ComponentBase *>
816        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
817    if (!cbase)
818        return OMX_ErrorBadParameter;
819
820    return cbase->CBaseAllocateBuffer(hComponent, ppBuffer, nPortIndex,
821                                      pAppPrivate, nSizeBytes);
822}
823
824OMX_ERRORTYPE ComponentBase::CBaseAllocateBuffer(
825    OMX_IN OMX_HANDLETYPE hComponent,
826    OMX_INOUT OMX_BUFFERHEADERTYPE** ppBuffer,
827    OMX_IN OMX_U32 nPortIndex,
828    OMX_IN OMX_PTR pAppPrivate,
829    OMX_IN OMX_U32 nSizeBytes)
830{
831    /*
832     * Todo
833     */
834
835    return OMX_ErrorNotImplemented;
836}
837
838OMX_ERRORTYPE ComponentBase::FreeBuffer(
839    OMX_IN  OMX_HANDLETYPE hComponent,
840    OMX_IN  OMX_U32 nPortIndex,
841    OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer)
842{
843    ComponentBase *cbase;
844
845    if (!hComponent)
846        return OMX_ErrorBadParameter;
847
848    cbase = static_cast<ComponentBase *>
849        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
850    if (!cbase)
851        return OMX_ErrorBadParameter;
852
853    return cbase->CBaseFreeBuffer(hComponent, nPortIndex, pBuffer);
854}
855
856OMX_ERRORTYPE ComponentBase::CBaseFreeBuffer(
857    OMX_IN  OMX_HANDLETYPE hComponent,
858    OMX_IN  OMX_U32 nPortIndex,
859    OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer)
860{
861    /*
862     * Todo
863     */
864
865    return OMX_ErrorNotImplemented;
866}
867
868OMX_ERRORTYPE ComponentBase::EmptyThisBuffer(
869    OMX_IN  OMX_HANDLETYPE hComponent,
870    OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer)
871{
872    ComponentBase *cbase;
873
874    if (!hComponent)
875        return OMX_ErrorBadParameter;
876
877    cbase = static_cast<ComponentBase *>
878        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
879    if (!cbase)
880        return OMX_ErrorBadParameter;
881
882    return cbase->CBaseEmptyThisBuffer(hComponent, pBuffer);
883}
884
885OMX_ERRORTYPE ComponentBase::CBaseEmptyThisBuffer(
886    OMX_IN  OMX_HANDLETYPE hComponent,
887    OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer)
888{
889    /*
890     * Todo
891     */
892
893    return OMX_ErrorNotImplemented;
894}
895
896OMX_ERRORTYPE ComponentBase::FillThisBuffer(
897    OMX_IN  OMX_HANDLETYPE hComponent,
898    OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer)
899{
900    ComponentBase *cbase;
901
902    if (!hComponent)
903        return OMX_ErrorBadParameter;
904
905    cbase = static_cast<ComponentBase *>
906        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
907    if (!cbase)
908        return OMX_ErrorBadParameter;
909
910    return cbase->CBaseFillThisBuffer(hComponent, pBuffer);
911}
912
913OMX_ERRORTYPE ComponentBase::CBaseFillThisBuffer(
914    OMX_IN  OMX_HANDLETYPE hComponent,
915    OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer)
916{
917    /*
918     * Todo
919     */
920
921    return OMX_ErrorNotImplemented;
922}
923
924OMX_ERRORTYPE ComponentBase::SetCallbacks(
925    OMX_IN  OMX_HANDLETYPE hComponent,
926    OMX_IN  OMX_CALLBACKTYPE* pCallbacks,
927    OMX_IN  OMX_PTR pAppData)
928{
929    ComponentBase *cbase;
930
931    if (!hComponent)
932        return OMX_ErrorBadParameter;
933
934    cbase = static_cast<ComponentBase *>
935        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
936    if (!cbase)
937        return OMX_ErrorBadParameter;
938
939    return cbase->CBaseSetCallbacks(hComponent, pCallbacks, pAppData);
940}
941
942OMX_ERRORTYPE ComponentBase::CBaseSetCallbacks(
943    OMX_IN  OMX_HANDLETYPE hComponent,
944    OMX_IN  OMX_CALLBACKTYPE *pCallbacks,
945    OMX_IN  OMX_PTR pAppData)
946{
947    if (hComponent != handle)
948        return OMX_ErrorBadParameter;
949
950    appdata = pAppData;
951    callbacks = pCallbacks;
952
953    return OMX_ErrorNone;
954}
955
956OMX_ERRORTYPE ComponentBase::ComponentDeInit(
957    OMX_IN  OMX_HANDLETYPE hComponent)
958{
959    ComponentBase *cbase;
960
961    if (!hComponent)
962        return OMX_ErrorBadParameter;
963
964    cbase = static_cast<ComponentBase *>
965        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
966    if (!cbase)
967        return OMX_ErrorBadParameter;
968
969    return cbase->CBaseComponentDeInit(hComponent);
970}
971
972OMX_ERRORTYPE ComponentBase::CBaseComponentDeInit(
973    OMX_IN  OMX_HANDLETYPE hComponent)
974{
975    /*
976     * Todo
977     */
978
979    return OMX_ErrorNotImplemented;
980}
981
982OMX_ERRORTYPE ComponentBase::UseEGLImage(
983    OMX_IN OMX_HANDLETYPE hComponent,
984    OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
985    OMX_IN OMX_U32 nPortIndex,
986    OMX_IN OMX_PTR pAppPrivate,
987    OMX_IN void* eglImage)
988{
989    ComponentBase *cbase;
990
991    if (!hComponent)
992        return OMX_ErrorBadParameter;
993
994    cbase = static_cast<ComponentBase *>
995        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
996    if (!cbase)
997        return OMX_ErrorBadParameter;
998
999    return cbase->CBaseUseEGLImage(hComponent, ppBufferHdr, nPortIndex,
1000                                   pAppPrivate, eglImage);
1001}
1002
1003OMX_ERRORTYPE ComponentBase::CBaseUseEGLImage(
1004    OMX_IN OMX_HANDLETYPE hComponent,
1005    OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
1006    OMX_IN OMX_U32 nPortIndex,
1007    OMX_IN OMX_PTR pAppPrivate,
1008    OMX_IN void* eglImage)
1009{
1010    /*
1011     * Todo
1012     */
1013
1014    return OMX_ErrorNotImplemented;
1015}
1016
1017OMX_ERRORTYPE ComponentBase::ComponentRoleEnum(
1018    OMX_IN OMX_HANDLETYPE hComponent,
1019    OMX_OUT OMX_U8 *cRole,
1020    OMX_IN OMX_U32 nIndex)
1021{
1022    ComponentBase *cbase;
1023
1024    if (!hComponent)
1025        return OMX_ErrorBadParameter;
1026
1027    cbase = static_cast<ComponentBase *>
1028        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
1029    if (!cbase)
1030        return OMX_ErrorBadParameter;
1031
1032    return cbase->CBaseComponentRoleEnum(hComponent, cRole, nIndex);
1033}
1034
1035OMX_ERRORTYPE ComponentBase::CBaseComponentRoleEnum(
1036    OMX_IN OMX_HANDLETYPE hComponent,
1037    OMX_OUT OMX_U8 *cRole,
1038    OMX_IN OMX_U32 nIndex)
1039{
1040    if (hComponent != (OMX_HANDLETYPE *)this->handle)
1041        return OMX_ErrorBadParameter;
1042
1043    if (nIndex > nr_roles)
1044        return OMX_ErrorBadParameter;
1045
1046    strncpy((char *)cRole, (const char *)roles[nIndex],
1047            OMX_MAX_STRINGNAME_SIZE);
1048    return OMX_ErrorNone;
1049}
1050
1051/* implement CmdHandlerInterface */
1052void ComponentBase::CmdHandler(struct cmd_s *cmd)
1053{
1054    OMX_EVENTTYPE event = OMX_EventCmdComplete;
1055    OMX_U32 data1 = cmd->cmd, data2 = cmd->param1;
1056    OMX_PTR eventdata = NULL;
1057
1058    switch (cmd->cmd) {
1059    case OMX_CommandStateSet:
1060        /*
1061         * Todo
1062         */
1063        break;
1064    case OMX_CommandFlush:
1065        /*
1066         * Todo
1067         */
1068        break;
1069    case OMX_CommandPortDisable:
1070        /*
1071         * Todo
1072         */
1073        break;
1074    case OMX_CommandPortEnable:
1075        /*
1076         * Todo
1077         */
1078        break;
1079    case OMX_CommandMarkBuffer:
1080        /*
1081         * Todo
1082         */
1083        break;
1084    }
1085
1086    callbacks->EventHandler(handle, appdata,
1087                            event, data1, data2, eventdata);
1088}
1089
1090/* end of component methods & helpers */
1091
1092/*
1093 * omx header manipuation
1094 */
1095void ComponentBase::SetTypeHeader(OMX_PTR type, OMX_U32 size)
1096{
1097    OMX_U32 *nsize;
1098    OMX_VERSIONTYPE *nversion;
1099
1100    if (!type)
1101        return;
1102
1103    nsize = (OMX_U32 *)type;
1104    nversion = (OMX_VERSIONTYPE *)((OMX_U8 *)type + sizeof(OMX_U32));
1105
1106    *nsize = sizeof(size);
1107    nversion->nVersion = OMX_SPEC_VERSION;
1108}
1109
1110OMX_BOOL ComponentBase::CheckTypeHeader(OMX_PTR type, OMX_U32 size)
1111{
1112    OMX_U32 *nsize;
1113    OMX_VERSIONTYPE *nversion;
1114    OMX_U8 mismatch = 0;
1115
1116    if (!type)
1117        return OMX_FALSE;
1118
1119    nsize = (OMX_U32 *)type;
1120    nversion = (OMX_VERSIONTYPE *)((OMX_U8 *)type + sizeof(OMX_U32));
1121
1122    mismatch = (*nsize != sizeof(size)) ? 1 : 0;
1123    mismatch |= (nversion->nVersion != OMX_SPEC_VERSION) ? 1 : 0;
1124
1125    if (mismatch)
1126        return OMX_TRUE;
1127    else
1128        return OMX_FALSE;
1129}
1130
1131/* end of ComponentBase */
1132