componentbase.cpp revision c2067bb142cdbc423de50cf97811425a8ab3ff1a
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    switch (nParamIndex) {
480    case OMX_IndexParamAudioInit:
481    case OMX_IndexParamVideoInit:
482    case OMX_IndexParamImageInit:
483    case OMX_IndexParamOtherInit: {
484        OMX_PORT_PARAM_TYPE *p =
485            (OMX_PORT_PARAM_TYPE *)pComponentParameterStructure;
486
487        memcpy(p, &portparam, sizeof(*p));
488        break;
489    }
490    case OMX_IndexParamPortDefinition: {
491        OMX_PARAM_PORTDEFINITIONTYPE *p =
492            (OMX_PARAM_PORTDEFINITIONTYPE *)pComponentParameterStructure;
493        OMX_U32 index = p->nPortIndex;
494        PortBase *port = ports[index];
495
496        memcpy(p, port->GetPortParam(), sizeof(*p));
497        break;
498    }
499    case OMX_IndexParamAudioPortFormat: {
500        OMX_AUDIO_PARAM_PORTFORMATTYPE *p =
501            (OMX_AUDIO_PARAM_PORTFORMATTYPE *)pComponentParameterStructure;
502        OMX_U32 index = p->nPortIndex;
503        PortBase *port = ports[index];
504
505        memcpy(p, port->GetAudioPortParam(), sizeof(*p));
506        break;
507    }
508    case OMX_IndexParamCompBufferSupplier:
509        /*
510         * Todo
511         */
512
513        ret = OMX_ErrorUnsupportedIndex;
514        break;
515    default:
516        ret = ComponentGetParameter(nParamIndex, pComponentParameterStructure);
517    } /* switch */
518
519    return ret;
520}
521
522OMX_ERRORTYPE ComponentBase::SetParameter(
523    OMX_IN  OMX_HANDLETYPE hComponent,
524    OMX_IN  OMX_INDEXTYPE nIndex,
525    OMX_IN  OMX_PTR pComponentParameterStructure)
526{
527    ComponentBase *cbase;
528
529    if (!hComponent)
530        return OMX_ErrorBadParameter;
531
532    cbase = static_cast<ComponentBase *>
533        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
534    if (!cbase)
535        return OMX_ErrorBadParameter;
536
537    return cbase->CBaseSetParameter(hComponent, nIndex,
538                                    pComponentParameterStructure);
539}
540
541OMX_ERRORTYPE ComponentBase::CBaseSetParameter(
542    OMX_IN  OMX_HANDLETYPE hComponent,
543    OMX_IN  OMX_INDEXTYPE nIndex,
544    OMX_IN  OMX_PTR pComponentParameterStructure)
545{
546    OMX_ERRORTYPE ret = OMX_ErrorNone;
547
548    switch (nIndex) {
549    case OMX_IndexParamAudioInit:
550    case OMX_IndexParamVideoInit:
551    case OMX_IndexParamImageInit:
552    case OMX_IndexParamOtherInit: {
553        OMX_PORT_PARAM_TYPE *p = (OMX_PORT_PARAM_TYPE *)
554            pComponentParameterStructure;
555
556        memcpy(&portparam, p, sizeof(*p));
557        break;
558    }
559    case OMX_IndexParamPortDefinition: {
560        OMX_PARAM_PORTDEFINITIONTYPE *p =
561            (OMX_PARAM_PORTDEFINITIONTYPE *)pComponentParameterStructure;
562        OMX_U32 index = p->nPortIndex;
563        PortBase *port = ports[index];
564
565        port->SetPortParam(p);
566        break;
567    }
568    case OMX_IndexParamAudioPortFormat: {
569        OMX_AUDIO_PARAM_PORTFORMATTYPE *p =
570            (OMX_AUDIO_PARAM_PORTFORMATTYPE *)pComponentParameterStructure;
571        OMX_U32 index = p->nPortIndex;
572        PortBase *port = ports[index];
573
574        port->SetAudioPortParam(p);
575        break;
576    }
577    case OMX_IndexParamCompBufferSupplier:
578        /*
579         * Todo
580         */
581
582        ret = OMX_ErrorUnsupportedIndex;
583        break;
584    default:
585        ret = ComponentSetParameter(nIndex, pComponentParameterStructure);
586    } /* switch */
587
588    return ret;
589}
590
591OMX_ERRORTYPE ComponentBase::GetConfig(
592    OMX_IN  OMX_HANDLETYPE hComponent,
593    OMX_IN  OMX_INDEXTYPE nIndex,
594    OMX_INOUT OMX_PTR pComponentConfigStructure)
595{
596    ComponentBase *cbase;
597
598    if (!hComponent)
599        return OMX_ErrorBadParameter;
600
601    cbase = static_cast<ComponentBase *>
602        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
603    if (!cbase)
604        return OMX_ErrorBadParameter;
605
606    return cbase->CBaseGetConfig(hComponent, nIndex,
607                                 pComponentConfigStructure);
608}
609
610OMX_ERRORTYPE ComponentBase::CBaseGetConfig(
611    OMX_IN  OMX_HANDLETYPE hComponent,
612    OMX_IN  OMX_INDEXTYPE nIndex,
613    OMX_INOUT OMX_PTR pComponentConfigStructure)
614{
615    OMX_ERRORTYPE ret;
616
617    if (hComponent != handle)
618        return OMX_ErrorBadParameter;
619
620    switch (nIndex) {
621    default:
622        ret = ComponentGetConfig(nIndex, pComponentConfigStructure);
623    }
624
625    return ret;
626}
627
628OMX_ERRORTYPE ComponentBase::SetConfig(
629    OMX_IN  OMX_HANDLETYPE hComponent,
630    OMX_IN  OMX_INDEXTYPE nIndex,
631    OMX_IN  OMX_PTR pComponentConfigStructure)
632{
633    ComponentBase *cbase;
634
635    if (!hComponent)
636        return OMX_ErrorBadParameter;
637
638    cbase = static_cast<ComponentBase *>
639        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
640    if (!cbase)
641        return OMX_ErrorBadParameter;
642
643    return cbase->CBaseSetConfig(hComponent, nIndex,
644                                 pComponentConfigStructure);
645}
646
647OMX_ERRORTYPE ComponentBase::CBaseSetConfig(
648    OMX_IN  OMX_HANDLETYPE hComponent,
649    OMX_IN  OMX_INDEXTYPE nIndex,
650    OMX_IN  OMX_PTR pComponentConfigStructure)
651{
652    OMX_ERRORTYPE ret;
653
654    if (hComponent != handle)
655        return OMX_ErrorBadParameter;
656
657    switch (nIndex) {
658    default:
659        ret = ComponentSetConfig(nIndex, pComponentConfigStructure);
660    }
661
662    return ret;
663}
664
665OMX_ERRORTYPE ComponentBase::GetExtensionIndex(
666    OMX_IN  OMX_HANDLETYPE hComponent,
667    OMX_IN  OMX_STRING cParameterName,
668    OMX_OUT OMX_INDEXTYPE* pIndexType)
669{
670    ComponentBase *cbase;
671
672    if (!hComponent)
673        return OMX_ErrorBadParameter;
674
675    cbase = static_cast<ComponentBase *>
676        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
677    if (!cbase)
678        return OMX_ErrorBadParameter;
679
680    return cbase->CBaseGetExtensionIndex(hComponent, cParameterName,
681                                         pIndexType);
682}
683
684OMX_ERRORTYPE ComponentBase::CBaseGetExtensionIndex(
685    OMX_IN  OMX_HANDLETYPE hComponent,
686    OMX_IN  OMX_STRING cParameterName,
687    OMX_OUT OMX_INDEXTYPE* pIndexType)
688{
689    /*
690     * Todo
691     */
692
693    return OMX_ErrorNotImplemented;
694}
695
696OMX_ERRORTYPE ComponentBase::GetState(
697    OMX_IN  OMX_HANDLETYPE hComponent,
698    OMX_OUT OMX_STATETYPE* pState)
699{
700    ComponentBase *cbase;
701
702    if (!hComponent)
703        return OMX_ErrorBadParameter;
704
705    cbase = static_cast<ComponentBase *>
706        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
707    if (!cbase)
708        return OMX_ErrorBadParameter;
709
710    return cbase->CBaseGetState(hComponent, pState);
711}
712
713OMX_ERRORTYPE ComponentBase::CBaseGetState(
714    OMX_IN  OMX_HANDLETYPE hComponent,
715    OMX_OUT OMX_STATETYPE* pState)
716{
717    /*
718     * Todo
719     */
720
721    return OMX_ErrorNotImplemented;
722}
723
724OMX_ERRORTYPE ComponentBase::ComponentTunnelRequest(
725    OMX_IN  OMX_HANDLETYPE hComponent,
726    OMX_IN  OMX_U32 nPort,
727    OMX_IN  OMX_HANDLETYPE hTunneledComponent,
728    OMX_IN  OMX_U32 nTunneledPort,
729    OMX_INOUT  OMX_TUNNELSETUPTYPE* pTunnelSetup)
730{
731    ComponentBase *cbase;
732
733    if (!hComponent)
734        return OMX_ErrorBadParameter;
735
736    cbase = static_cast<ComponentBase *>
737        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
738    if (!cbase)
739        return OMX_ErrorBadParameter;
740
741    return cbase->CBaseComponentTunnelRequest(hComponent, nPort,
742                                              hTunneledComponent,
743                                              nTunneledPort, pTunnelSetup);
744}
745
746OMX_ERRORTYPE ComponentBase::CBaseComponentTunnelRequest(
747    OMX_IN  OMX_HANDLETYPE hComp,
748    OMX_IN  OMX_U32 nPort,
749    OMX_IN  OMX_HANDLETYPE hTunneledComp,
750    OMX_IN  OMX_U32 nTunneledPort,
751    OMX_INOUT  OMX_TUNNELSETUPTYPE* pTunnelSetup)
752{
753    /*
754     * Todo
755     */
756
757    return OMX_ErrorNotImplemented;
758}
759
760OMX_ERRORTYPE ComponentBase::UseBuffer(
761    OMX_IN OMX_HANDLETYPE hComponent,
762    OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
763    OMX_IN OMX_U32 nPortIndex,
764    OMX_IN OMX_PTR pAppPrivate,
765    OMX_IN OMX_U32 nSizeBytes,
766    OMX_IN OMX_U8* pBuffer)
767{
768    ComponentBase *cbase;
769
770    if (!hComponent)
771        return OMX_ErrorBadParameter;
772
773    cbase = static_cast<ComponentBase *>
774        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
775    if (!cbase)
776        return OMX_ErrorBadParameter;
777
778    return cbase->CBaseUseBuffer(hComponent, ppBufferHdr, nPortIndex,
779                                 pAppPrivate, nSizeBytes, pBuffer);
780}
781
782OMX_ERRORTYPE ComponentBase::CBaseUseBuffer(
783    OMX_IN OMX_HANDLETYPE hComponent,
784    OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
785    OMX_IN OMX_U32 nPortIndex,
786    OMX_IN OMX_PTR pAppPrivate,
787    OMX_IN OMX_U32 nSizeBytes,
788    OMX_IN OMX_U8* pBuffer)
789{
790    /*
791     * Todo
792     */
793
794    return OMX_ErrorNotImplemented;
795}
796
797OMX_ERRORTYPE ComponentBase::AllocateBuffer(
798    OMX_IN OMX_HANDLETYPE hComponent,
799    OMX_INOUT OMX_BUFFERHEADERTYPE** ppBuffer,
800    OMX_IN OMX_U32 nPortIndex,
801    OMX_IN OMX_PTR pAppPrivate,
802    OMX_IN OMX_U32 nSizeBytes)
803{
804    ComponentBase *cbase;
805
806    if (!hComponent)
807        return OMX_ErrorBadParameter;
808
809    cbase = static_cast<ComponentBase *>
810        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
811    if (!cbase)
812        return OMX_ErrorBadParameter;
813
814    return cbase->CBaseAllocateBuffer(hComponent, ppBuffer, nPortIndex,
815                                      pAppPrivate, nSizeBytes);
816}
817
818OMX_ERRORTYPE ComponentBase::CBaseAllocateBuffer(
819    OMX_IN OMX_HANDLETYPE hComponent,
820    OMX_INOUT OMX_BUFFERHEADERTYPE** ppBuffer,
821    OMX_IN OMX_U32 nPortIndex,
822    OMX_IN OMX_PTR pAppPrivate,
823    OMX_IN OMX_U32 nSizeBytes)
824{
825    /*
826     * Todo
827     */
828
829    return OMX_ErrorNotImplemented;
830}
831
832OMX_ERRORTYPE ComponentBase::FreeBuffer(
833    OMX_IN  OMX_HANDLETYPE hComponent,
834    OMX_IN  OMX_U32 nPortIndex,
835    OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer)
836{
837    ComponentBase *cbase;
838
839    if (!hComponent)
840        return OMX_ErrorBadParameter;
841
842    cbase = static_cast<ComponentBase *>
843        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
844    if (!cbase)
845        return OMX_ErrorBadParameter;
846
847    return cbase->CBaseFreeBuffer(hComponent, nPortIndex, pBuffer);
848}
849
850OMX_ERRORTYPE ComponentBase::CBaseFreeBuffer(
851    OMX_IN  OMX_HANDLETYPE hComponent,
852    OMX_IN  OMX_U32 nPortIndex,
853    OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer)
854{
855    /*
856     * Todo
857     */
858
859    return OMX_ErrorNotImplemented;
860}
861
862OMX_ERRORTYPE ComponentBase::EmptyThisBuffer(
863    OMX_IN  OMX_HANDLETYPE hComponent,
864    OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer)
865{
866    ComponentBase *cbase;
867
868    if (!hComponent)
869        return OMX_ErrorBadParameter;
870
871    cbase = static_cast<ComponentBase *>
872        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
873    if (!cbase)
874        return OMX_ErrorBadParameter;
875
876    return cbase->CBaseEmptyThisBuffer(hComponent, pBuffer);
877}
878
879OMX_ERRORTYPE ComponentBase::CBaseEmptyThisBuffer(
880    OMX_IN  OMX_HANDLETYPE hComponent,
881    OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer)
882{
883    /*
884     * Todo
885     */
886
887    return OMX_ErrorNotImplemented;
888}
889
890OMX_ERRORTYPE ComponentBase::FillThisBuffer(
891    OMX_IN  OMX_HANDLETYPE hComponent,
892    OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer)
893{
894    ComponentBase *cbase;
895
896    if (!hComponent)
897        return OMX_ErrorBadParameter;
898
899    cbase = static_cast<ComponentBase *>
900        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
901    if (!cbase)
902        return OMX_ErrorBadParameter;
903
904    return cbase->CBaseFillThisBuffer(hComponent, pBuffer);
905}
906
907OMX_ERRORTYPE ComponentBase::CBaseFillThisBuffer(
908    OMX_IN  OMX_HANDLETYPE hComponent,
909    OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer)
910{
911    /*
912     * Todo
913     */
914
915    return OMX_ErrorNotImplemented;
916}
917
918OMX_ERRORTYPE ComponentBase::SetCallbacks(
919    OMX_IN  OMX_HANDLETYPE hComponent,
920    OMX_IN  OMX_CALLBACKTYPE* pCallbacks,
921    OMX_IN  OMX_PTR pAppData)
922{
923    ComponentBase *cbase;
924
925    if (!hComponent)
926        return OMX_ErrorBadParameter;
927
928    cbase = static_cast<ComponentBase *>
929        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
930    if (!cbase)
931        return OMX_ErrorBadParameter;
932
933    return cbase->CBaseSetCallbacks(hComponent, pCallbacks, pAppData);
934}
935
936OMX_ERRORTYPE ComponentBase::CBaseSetCallbacks(
937    OMX_IN  OMX_HANDLETYPE hComponent,
938    OMX_IN  OMX_CALLBACKTYPE *pCallbacks,
939    OMX_IN  OMX_PTR pAppData)
940{
941    if (hComponent != handle)
942        return OMX_ErrorBadParameter;
943
944    appdata = pAppData;
945    callbacks = pCallbacks;
946
947    return OMX_ErrorNone;
948}
949
950OMX_ERRORTYPE ComponentBase::ComponentDeInit(
951    OMX_IN  OMX_HANDLETYPE hComponent)
952{
953    ComponentBase *cbase;
954
955    if (!hComponent)
956        return OMX_ErrorBadParameter;
957
958    cbase = static_cast<ComponentBase *>
959        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
960    if (!cbase)
961        return OMX_ErrorBadParameter;
962
963    return cbase->CBaseComponentDeInit(hComponent);
964}
965
966OMX_ERRORTYPE ComponentBase::CBaseComponentDeInit(
967    OMX_IN  OMX_HANDLETYPE hComponent)
968{
969    /*
970     * Todo
971     */
972
973    return OMX_ErrorNotImplemented;
974}
975
976OMX_ERRORTYPE ComponentBase::UseEGLImage(
977    OMX_IN OMX_HANDLETYPE hComponent,
978    OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
979    OMX_IN OMX_U32 nPortIndex,
980    OMX_IN OMX_PTR pAppPrivate,
981    OMX_IN void* eglImage)
982{
983    ComponentBase *cbase;
984
985    if (!hComponent)
986        return OMX_ErrorBadParameter;
987
988    cbase = static_cast<ComponentBase *>
989        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
990    if (!cbase)
991        return OMX_ErrorBadParameter;
992
993    return cbase->CBaseUseEGLImage(hComponent, ppBufferHdr, nPortIndex,
994                                   pAppPrivate, eglImage);
995}
996
997OMX_ERRORTYPE ComponentBase::CBaseUseEGLImage(
998    OMX_IN OMX_HANDLETYPE hComponent,
999    OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
1000    OMX_IN OMX_U32 nPortIndex,
1001    OMX_IN OMX_PTR pAppPrivate,
1002    OMX_IN void* eglImage)
1003{
1004    /*
1005     * Todo
1006     */
1007
1008    return OMX_ErrorNotImplemented;
1009}
1010
1011OMX_ERRORTYPE ComponentBase::ComponentRoleEnum(
1012    OMX_IN OMX_HANDLETYPE hComponent,
1013    OMX_OUT OMX_U8 *cRole,
1014    OMX_IN OMX_U32 nIndex)
1015{
1016    ComponentBase *cbase;
1017
1018    if (!hComponent)
1019        return OMX_ErrorBadParameter;
1020
1021    cbase = static_cast<ComponentBase *>
1022        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
1023    if (!cbase)
1024        return OMX_ErrorBadParameter;
1025
1026    return cbase->CBaseComponentRoleEnum(hComponent, cRole, nIndex);
1027}
1028
1029OMX_ERRORTYPE ComponentBase::CBaseComponentRoleEnum(
1030    OMX_IN OMX_HANDLETYPE hComponent,
1031    OMX_OUT OMX_U8 *cRole,
1032    OMX_IN OMX_U32 nIndex)
1033{
1034    if (hComponent != (OMX_HANDLETYPE *)this->handle)
1035        return OMX_ErrorBadParameter;
1036
1037    if (nIndex > nr_roles)
1038        return OMX_ErrorBadParameter;
1039
1040    strncpy((char *)cRole, (const char *)roles[nIndex],
1041            OMX_MAX_STRINGNAME_SIZE);
1042    return OMX_ErrorNone;
1043}
1044
1045/* implement CmdHandlerInterface */
1046void ComponentBase::CmdHandler(struct cmd_s *cmd)
1047{
1048    OMX_EVENTTYPE event = OMX_EventCmdComplete;
1049    OMX_U32 data1 = cmd->cmd, data2 = cmd->param1;
1050    OMX_PTR eventdata = NULL;
1051
1052    switch (cmd->cmd) {
1053    case OMX_CommandStateSet:
1054        /*
1055         * Todo
1056         */
1057        break;
1058    case OMX_CommandFlush:
1059        /*
1060         * Todo
1061         */
1062        break;
1063    case OMX_CommandPortDisable:
1064        /*
1065         * Todo
1066         */
1067        break;
1068    case OMX_CommandPortEnable:
1069        /*
1070         * Todo
1071         */
1072        break;
1073    case OMX_CommandMarkBuffer:
1074        /*
1075         * Todo
1076         */
1077        break;
1078    }
1079
1080    callbacks->EventHandler(handle, appdata,
1081                            event, data1, data2, eventdata);
1082}
1083
1084/* end of component methods & helpers */
1085
1086/*
1087 * omx header manipuation
1088 */
1089void ComponentBase::SetTypeHeader(OMX_PTR type, OMX_U32 size)
1090{
1091    OMX_U32 *nsize;
1092    OMX_VERSIONTYPE *nversion;
1093
1094    if (!type)
1095        return;
1096
1097    nsize = (OMX_U32 *)type;
1098    nversion = (OMX_VERSIONTYPE *)((OMX_U8 *)type + sizeof(OMX_U32));
1099
1100    *nsize = sizeof(size);
1101    nversion->nVersion = OMX_SPEC_VERSION;
1102}
1103
1104OMX_BOOL ComponentBase::CheckTypeHeader(OMX_PTR type, OMX_U32 size)
1105{
1106    OMX_U32 *nsize;
1107    OMX_VERSIONTYPE *nversion;
1108    OMX_U8 mismatch = 0;
1109
1110    if (!type)
1111        return OMX_FALSE;
1112
1113    nsize = (OMX_U32 *)type;
1114    nversion = (OMX_VERSIONTYPE *)((OMX_U8 *)type + sizeof(OMX_U32));
1115
1116    mismatch = (*nsize != sizeof(size)) ? 1 : 0;
1117    mismatch |= (nversion->nVersion != OMX_SPEC_VERSION) ? 1 : 0;
1118
1119    if (mismatch)
1120        return OMX_TRUE;
1121    else
1122        return OMX_FALSE;
1123}
1124
1125/* end of ComponentBase */
1126