componentbase.cpp revision e8ef7cfbad3a42b41edabbdc4ed258ce331a00a0
1/*
2 * componentbase.cpp, component base class
3 *
4 * Copyright (c) 2009-2010 Wind River Systems, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#include <stdlib.h>
20#include <string.h>
21
22#include <pthread.h>
23
24#include <OMX_Core.h>
25#include <OMX_Component.h>
26
27#include <componentbase.h>
28
29#include <queue.h>
30#include <workqueue.h>
31#include <OMX_IndexExt.h>
32
33//#define LOG_NDEBUG 0
34
35#define LOG_TAG "componentbase"
36#include <log.h>
37
38/*
39 * CmdProcessWork
40 */
41CmdProcessWork::CmdProcessWork(CmdHandlerInterface *ci)
42{
43    this->ci = ci;
44
45    workq = new WorkQueue;
46
47    __queue_init(&q);
48    pthread_mutex_init(&lock, NULL);
49
50    workq->StartWork(true);
51
52    LOGV("command process workqueue started\n");
53}
54
55CmdProcessWork::~CmdProcessWork()
56{
57    struct cmd_s *temp;
58
59    workq->StopWork();
60    delete workq;
61
62    while ((temp = PopCmdQueue()))
63        free(temp);
64
65    pthread_mutex_destroy(&lock);
66
67    LOGV("command process workqueue stopped\n");
68}
69
70OMX_ERRORTYPE CmdProcessWork::PushCmdQueue(struct cmd_s *cmd)
71{
72    int ret;
73
74    pthread_mutex_lock(&lock);
75    ret = queue_push_tail(&q, cmd);
76    if (ret) {
77        pthread_mutex_unlock(&lock);
78        return OMX_ErrorInsufficientResources;
79    }
80
81    workq->ScheduleWork(this);
82    pthread_mutex_unlock(&lock);
83
84    return OMX_ErrorNone;
85}
86
87struct cmd_s *CmdProcessWork::PopCmdQueue(void)
88{
89    struct cmd_s *cmd;
90
91    pthread_mutex_lock(&lock);
92    cmd = (struct cmd_s *)queue_pop_head(&q);
93    pthread_mutex_unlock(&lock);
94
95    return cmd;
96}
97
98void CmdProcessWork::Work(void)
99{
100    struct cmd_s *cmd;
101
102    cmd = PopCmdQueue();
103    if (cmd) {
104        ci->CmdHandler(cmd);
105        free(cmd);
106    }
107}
108
109/* end of CmdProcessWork */
110
111/*
112 * ComponentBase
113 */
114/*
115 * constructor & destructor
116 */
117void ComponentBase::__ComponentBase(void)
118{
119    memset(name, 0, OMX_MAX_STRINGNAME_SIZE);
120    cmodule = NULL;
121    handle = NULL;
122
123    roles = NULL;
124    nr_roles = 0;
125
126    working_role = NULL;
127
128    ports = NULL;
129    nr_ports = 0;
130    memset(&portparam, 0, sizeof(portparam));
131
132    state = OMX_StateUnloaded;
133
134    cmdwork = NULL;
135
136    bufferwork = NULL;
137
138    pthread_mutex_init(&ports_block, NULL);
139    pthread_mutex_init(&state_block, NULL);
140}
141
142ComponentBase::ComponentBase()
143{
144    __ComponentBase();
145}
146
147ComponentBase::ComponentBase(const OMX_STRING name)
148{
149    __ComponentBase();
150    SetName(name);
151}
152
153ComponentBase::~ComponentBase()
154{
155    pthread_mutex_destroy(&ports_block);
156    pthread_mutex_destroy(&state_block);
157
158    if (roles) {
159        if (roles[0])
160            free(roles[0]);
161        free(roles);
162    }
163}
164
165/* end of constructor & destructor */
166
167/*
168 * accessor
169 */
170/* name */
171void ComponentBase::SetName(const OMX_STRING name)
172{
173    strncpy(this->name, name, (strlen(name) < OMX_MAX_STRINGNAME_SIZE) ? strlen(name) : (OMX_MAX_STRINGNAME_SIZE-1));
174   // strncpy(this->name, name, OMX_MAX_STRINGNAME_SIZE);
175    this->name[OMX_MAX_STRINGNAME_SIZE-1] = '\0';
176}
177
178const OMX_STRING ComponentBase::GetName(void)
179{
180    return name;
181}
182
183/* component module */
184void ComponentBase::SetCModule(CModule *cmodule)
185{
186    this->cmodule = cmodule;
187}
188
189CModule *ComponentBase::GetCModule(void)
190{
191    return cmodule;
192}
193
194/* end of accessor */
195
196/*
197 * core methods & helpers
198 */
199/* roles */
200OMX_ERRORTYPE ComponentBase::SetRolesOfComponent(OMX_U32 nr_roles,
201                                                 const OMX_U8 **roles)
202{
203    OMX_U32 i;
204
205    if (!roles || !nr_roles)
206        return OMX_ErrorBadParameter;
207
208    if (this->roles) {
209        free(this->roles[0]);
210        free(this->roles);
211        this->roles = NULL;
212    }
213
214    this->roles = (OMX_U8 **)malloc(sizeof(OMX_STRING) * nr_roles);
215    if (!this->roles)
216        return OMX_ErrorInsufficientResources;
217
218    this->roles[0] = (OMX_U8 *)malloc(OMX_MAX_STRINGNAME_SIZE * nr_roles);
219    if (!this->roles[0]) {
220        free(this->roles);
221        this->roles = NULL;
222        return OMX_ErrorInsufficientResources;
223    }
224
225    for (i = 0; i < nr_roles; i++) {
226        if (i < nr_roles-1)
227            this->roles[i+1] = this->roles[i] + OMX_MAX_STRINGNAME_SIZE;
228
229        strncpy((OMX_STRING)&this->roles[i][0],
230                (const OMX_STRING)&roles[i][0], OMX_MAX_STRINGNAME_SIZE);
231    }
232
233    this->nr_roles = nr_roles;
234    return OMX_ErrorNone;
235}
236
237/* GetHandle & FreeHandle */
238OMX_ERRORTYPE ComponentBase::GetHandle(OMX_HANDLETYPE *pHandle,
239                                       OMX_PTR pAppData,
240                                       OMX_CALLBACKTYPE *pCallBacks)
241{
242    OMX_U32 i;
243    OMX_ERRORTYPE ret;
244
245    if (!pHandle)
246        return OMX_ErrorBadParameter;
247
248    if (handle)
249        return OMX_ErrorUndefined;
250
251    cmdwork = new CmdProcessWork(this);
252    if (!cmdwork)
253        return OMX_ErrorInsufficientResources;
254
255    bufferwork = new WorkQueue();
256    if (!bufferwork) {
257        ret = OMX_ErrorInsufficientResources;
258        goto free_cmdwork;
259    }
260
261    handle = (OMX_COMPONENTTYPE *)calloc(1, sizeof(*handle));
262    if (!handle) {
263        ret = OMX_ErrorInsufficientResources;
264        goto free_bufferwork;
265    }
266
267    /* handle initialization */
268    SetTypeHeader(handle, sizeof(*handle));
269    handle->pComponentPrivate = static_cast<OMX_PTR>(this);
270    handle->pApplicationPrivate = pAppData;
271
272    /* connect handle's functions */
273    handle->GetComponentVersion = NULL;
274    handle->SendCommand = SendCommand;
275    handle->GetParameter = GetParameter;
276    handle->SetParameter = SetParameter;
277    handle->GetConfig = GetConfig;
278    handle->SetConfig = SetConfig;
279    handle->GetExtensionIndex = GetExtensionIndex;
280    handle->GetState = GetState;
281    handle->ComponentTunnelRequest = NULL;
282    handle->UseBuffer = UseBuffer;
283    handle->AllocateBuffer = AllocateBuffer;
284    handle->FreeBuffer = FreeBuffer;
285    handle->EmptyThisBuffer = EmptyThisBuffer;
286    handle->FillThisBuffer = FillThisBuffer;
287    handle->SetCallbacks = SetCallbacks;
288    handle->ComponentDeInit = NULL;
289    handle->UseEGLImage = NULL;
290    handle->ComponentRoleEnum = ComponentRoleEnum;
291
292    appdata = pAppData;
293    callbacks = pCallBacks;
294
295    if (nr_roles == 1) {
296        SetWorkingRole((OMX_STRING)&roles[0][0]);
297        ret = ApplyWorkingRole();
298        if (ret != OMX_ErrorNone) {
299            SetWorkingRole(NULL);
300            goto free_handle;
301        }
302    }
303
304    *pHandle = (OMX_HANDLETYPE *)handle;
305    state = OMX_StateLoaded;
306    return OMX_ErrorNone;
307
308free_handle:
309    free(handle);
310
311    appdata = NULL;
312    callbacks = NULL;
313    *pHandle = NULL;
314
315free_bufferwork:
316    delete bufferwork;
317
318free_cmdwork:
319    delete cmdwork;
320
321    return ret;
322}
323
324OMX_ERRORTYPE ComponentBase::FreeHandle(OMX_HANDLETYPE hComponent)
325{
326    OMX_ERRORTYPE ret;
327
328    if (hComponent != handle)
329        return OMX_ErrorBadParameter;
330
331    if (state != OMX_StateLoaded)
332        return OMX_ErrorIncorrectStateOperation;
333
334    FreePorts();
335
336    free(handle);
337
338    appdata = NULL;
339    callbacks = NULL;
340
341    delete cmdwork;
342    delete bufferwork;
343
344    state = OMX_StateUnloaded;
345    return OMX_ErrorNone;
346}
347
348/* end of core methods & helpers */
349
350/*
351 * component methods & helpers
352 */
353OMX_ERRORTYPE ComponentBase::SendCommand(
354    OMX_IN  OMX_HANDLETYPE hComponent,
355    OMX_IN  OMX_COMMANDTYPE Cmd,
356    OMX_IN  OMX_U32 nParam1,
357    OMX_IN  OMX_PTR pCmdData)
358{
359    ComponentBase *cbase;
360
361    if (!hComponent)
362        return OMX_ErrorBadParameter;
363
364    cbase = static_cast<ComponentBase *>
365        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
366    if (!cbase)
367        return OMX_ErrorBadParameter;
368
369    return cbase->CBaseSendCommand(hComponent, Cmd, nParam1, pCmdData);
370}
371
372OMX_ERRORTYPE ComponentBase::CBaseSendCommand(
373    OMX_IN  OMX_HANDLETYPE hComponent,
374    OMX_IN  OMX_COMMANDTYPE Cmd,
375    OMX_IN  OMX_U32 nParam1,
376    OMX_IN  OMX_PTR pCmdData)
377{
378    struct cmd_s *cmd;
379
380    if (hComponent != handle)
381        return OMX_ErrorInvalidComponent;
382
383    /* basic error check */
384    switch (Cmd) {
385    case OMX_CommandStateSet:
386        /*
387         * Todo
388         */
389        break;
390    case OMX_CommandFlush: {
391        OMX_U32 port_index = nParam1;
392
393        if ((port_index != OMX_ALL) && (port_index > nr_ports-1))
394            return OMX_ErrorBadPortIndex;
395        break;
396    }
397    case OMX_CommandPortDisable:
398    case OMX_CommandPortEnable: {
399        OMX_U32 port_index = nParam1;
400
401        if ((port_index != OMX_ALL) && (port_index > nr_ports-1))
402            return OMX_ErrorBadPortIndex;
403        break;
404    }
405    case OMX_CommandMarkBuffer: {
406        OMX_MARKTYPE *mark = (OMX_MARKTYPE *)pCmdData;
407        OMX_MARKTYPE *copiedmark;
408        OMX_U32 port_index = nParam1;
409
410        if (port_index > nr_ports-1)
411            return OMX_ErrorBadPortIndex;
412
413        if (!mark || !mark->hMarkTargetComponent)
414            return OMX_ErrorBadParameter;
415
416        copiedmark = (OMX_MARKTYPE *)malloc(sizeof(*copiedmark));
417        if (!copiedmark)
418            return OMX_ErrorInsufficientResources;
419
420        copiedmark->hMarkTargetComponent = mark->hMarkTargetComponent;
421        copiedmark->pMarkData = mark->pMarkData;
422        pCmdData = (OMX_PTR)copiedmark;
423        break;
424    }
425    default:
426        LOGE("command %d not supported\n", Cmd);
427        return OMX_ErrorUnsupportedIndex;
428    }
429
430    cmd = (struct cmd_s *)malloc(sizeof(*cmd));
431    if (!cmd)
432        return OMX_ErrorInsufficientResources;
433
434    cmd->cmd = Cmd;
435    cmd->param1 = nParam1;
436    cmd->cmddata = pCmdData;
437
438    return cmdwork->PushCmdQueue(cmd);
439}
440
441OMX_ERRORTYPE ComponentBase::GetParameter(
442    OMX_IN  OMX_HANDLETYPE hComponent,
443    OMX_IN  OMX_INDEXTYPE nParamIndex,
444    OMX_INOUT OMX_PTR pComponentParameterStructure)
445{
446    ComponentBase *cbase;
447
448    if (!hComponent)
449        return OMX_ErrorBadParameter;
450
451    cbase = static_cast<ComponentBase *>
452        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
453    if (!cbase)
454        return OMX_ErrorBadParameter;
455
456    return cbase->CBaseGetParameter(hComponent, nParamIndex,
457                                    pComponentParameterStructure);
458}
459
460OMX_ERRORTYPE ComponentBase::CBaseGetParameter(
461    OMX_IN  OMX_HANDLETYPE hComponent,
462    OMX_IN  OMX_INDEXTYPE nParamIndex,
463    OMX_INOUT OMX_PTR pComponentParameterStructure)
464{
465    OMX_ERRORTYPE ret = OMX_ErrorNone;
466
467    if (hComponent != handle)
468        return OMX_ErrorBadParameter;
469
470    switch (nParamIndex) {
471    case OMX_IndexParamAudioInit:
472    case OMX_IndexParamVideoInit:
473    case OMX_IndexParamImageInit:
474    case OMX_IndexParamOtherInit: {
475        OMX_PORT_PARAM_TYPE *p =
476            (OMX_PORT_PARAM_TYPE *)pComponentParameterStructure;
477
478        ret = CheckTypeHeader(p, sizeof(*p));
479        if (ret != OMX_ErrorNone)
480            return ret;
481
482        memcpy(p, &portparam, sizeof(*p));
483        break;
484    }
485    case OMX_IndexParamPortDefinition: {
486        OMX_PARAM_PORTDEFINITIONTYPE *p =
487            (OMX_PARAM_PORTDEFINITIONTYPE *)pComponentParameterStructure;
488        OMX_U32 index = p->nPortIndex;
489        PortBase *port = NULL;
490
491        ret = CheckTypeHeader(p, sizeof(*p));
492        if (ret != OMX_ErrorNone)
493            return ret;
494
495        if (index < nr_ports)
496            port = ports[index];
497
498        if (!port)
499            return OMX_ErrorBadPortIndex;
500
501        memcpy(p, port->GetPortDefinition(), sizeof(*p));
502        break;
503    }
504    case OMX_IndexParamCompBufferSupplier:
505        /*
506         * Todo
507         */
508
509        ret = OMX_ErrorUnsupportedIndex;
510        break;
511
512    default:
513        ret = ComponentGetParameter(nParamIndex, pComponentParameterStructure);
514    } /* switch */
515
516    return ret;
517}
518
519OMX_ERRORTYPE ComponentBase::SetParameter(
520    OMX_IN  OMX_HANDLETYPE hComponent,
521    OMX_IN  OMX_INDEXTYPE nIndex,
522    OMX_IN  OMX_PTR pComponentParameterStructure)
523{
524    ComponentBase *cbase;
525
526    if (!hComponent)
527        return OMX_ErrorBadParameter;
528
529    cbase = static_cast<ComponentBase *>
530        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
531    if (!cbase)
532        return OMX_ErrorBadParameter;
533
534    return cbase->CBaseSetParameter(hComponent, nIndex,
535                                    pComponentParameterStructure);
536}
537
538OMX_ERRORTYPE ComponentBase::CBaseSetParameter(
539    OMX_IN  OMX_HANDLETYPE hComponent,
540    OMX_IN  OMX_INDEXTYPE nIndex,
541    OMX_IN  OMX_PTR pComponentParameterStructure)
542{
543    OMX_ERRORTYPE ret = OMX_ErrorNone;
544
545    if (hComponent != handle)
546        return OMX_ErrorBadParameter;
547
548    switch (nIndex) {
549    case OMX_IndexParamAudioInit:
550    case OMX_IndexParamVideoInit:
551    case OMX_IndexParamImageInit:
552    case OMX_IndexParamOtherInit:
553        /* preventing clients from setting OMX_PORT_PARAM_TYPE */
554        ret = OMX_ErrorUnsupportedIndex;
555        break;
556    case OMX_IndexParamPortDefinition: {
557        OMX_PARAM_PORTDEFINITIONTYPE *p =
558            (OMX_PARAM_PORTDEFINITIONTYPE *)pComponentParameterStructure;
559        OMX_U32 index = p->nPortIndex;
560        PortBase *port = NULL;
561
562        ret = CheckTypeHeader(p, sizeof(*p));
563        if (ret != OMX_ErrorNone)
564            return ret;
565
566        if (index < nr_ports)
567            port = ports[index];
568
569        if (!port)
570            return OMX_ErrorBadPortIndex;
571
572        if (port->IsEnabled()) {
573            if (state != OMX_StateLoaded && state != OMX_StateWaitForResources)
574                return OMX_ErrorIncorrectStateOperation;
575        }
576
577        ret = port->SetPortDefinition(p, false);
578        if (ret != OMX_ErrorNone) {
579            return ret;
580        }
581        break;
582    }
583    case OMX_IndexParamCompBufferSupplier:
584        /*
585         * Todo
586         */
587
588        ret = OMX_ErrorUnsupportedIndex;
589        break;
590    case OMX_IndexParamStandardComponentRole: {
591        OMX_PARAM_COMPONENTROLETYPE *p =
592            (OMX_PARAM_COMPONENTROLETYPE *)pComponentParameterStructure;
593
594        if (state != OMX_StateLoaded && state != OMX_StateWaitForResources)
595            return OMX_ErrorIncorrectStateOperation;
596
597        ret = CheckTypeHeader(p, sizeof(*p));
598        if (ret != OMX_ErrorNone)
599            return ret;
600
601        ret = SetWorkingRole((OMX_STRING)p->cRole);
602        if (ret != OMX_ErrorNone)
603            return ret;
604
605        if (ports)
606            FreePorts();
607
608        ret = ApplyWorkingRole();
609        if (ret != OMX_ErrorNone) {
610            SetWorkingRole(NULL);
611            return ret;
612        }
613        break;
614    }
615
616    default:
617        ret = ComponentSetParameter(nIndex, pComponentParameterStructure);
618    } /* switch */
619
620    return ret;
621}
622
623OMX_ERRORTYPE ComponentBase::GetConfig(
624    OMX_IN  OMX_HANDLETYPE hComponent,
625    OMX_IN  OMX_INDEXTYPE nIndex,
626    OMX_INOUT OMX_PTR pComponentConfigStructure)
627{
628    ComponentBase *cbase;
629
630    if (!hComponent)
631        return OMX_ErrorBadParameter;
632
633    cbase = static_cast<ComponentBase *>
634        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
635    if (!cbase)
636        return OMX_ErrorBadParameter;
637
638    return cbase->CBaseGetConfig(hComponent, nIndex,
639                                 pComponentConfigStructure);
640}
641
642OMX_ERRORTYPE ComponentBase::CBaseGetConfig(
643    OMX_IN  OMX_HANDLETYPE hComponent,
644    OMX_IN  OMX_INDEXTYPE nIndex,
645    OMX_INOUT OMX_PTR pComponentConfigStructure)
646{
647    OMX_ERRORTYPE ret;
648
649    if (hComponent != handle)
650        return OMX_ErrorBadParameter;
651
652    switch (nIndex) {
653    default:
654        ret = ComponentGetConfig(nIndex, pComponentConfigStructure);
655    }
656
657    return ret;
658}
659
660OMX_ERRORTYPE ComponentBase::SetConfig(
661    OMX_IN  OMX_HANDLETYPE hComponent,
662    OMX_IN  OMX_INDEXTYPE nIndex,
663    OMX_IN  OMX_PTR pComponentConfigStructure)
664{
665    ComponentBase *cbase;
666
667    if (!hComponent)
668        return OMX_ErrorBadParameter;
669
670    cbase = static_cast<ComponentBase *>
671        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
672    if (!cbase)
673        return OMX_ErrorBadParameter;
674
675    return cbase->CBaseSetConfig(hComponent, nIndex,
676                                 pComponentConfigStructure);
677}
678
679OMX_ERRORTYPE ComponentBase::CBaseSetConfig(
680    OMX_IN  OMX_HANDLETYPE hComponent,
681    OMX_IN  OMX_INDEXTYPE nIndex,
682    OMX_IN  OMX_PTR pComponentConfigStructure)
683{
684    OMX_ERRORTYPE ret;
685
686    if (hComponent != handle)
687        return OMX_ErrorBadParameter;
688
689    switch (nIndex) {
690    default:
691        ret = ComponentSetConfig(nIndex, pComponentConfigStructure);
692    }
693
694    return ret;
695}
696
697OMX_ERRORTYPE ComponentBase::GetExtensionIndex(
698    OMX_IN  OMX_HANDLETYPE hComponent,
699    OMX_IN  OMX_STRING cParameterName,
700    OMX_OUT OMX_INDEXTYPE* pIndexType)
701{
702    ComponentBase *cbase;
703
704    if (!hComponent)
705        return OMX_ErrorBadParameter;
706
707    cbase = static_cast<ComponentBase *>
708        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
709    if (!cbase)
710        return OMX_ErrorBadParameter;
711
712    return cbase->CBaseGetExtensionIndex(hComponent, cParameterName,
713                                         pIndexType);
714}
715
716OMX_ERRORTYPE ComponentBase::CBaseGetExtensionIndex(
717    OMX_IN  OMX_HANDLETYPE hComponent,
718    OMX_IN  OMX_STRING cParameterName,
719    OMX_OUT OMX_INDEXTYPE* pIndexType)
720{
721    /*
722     * Todo
723     */
724    if (hComponent != handle) {
725
726        return OMX_ErrorBadParameter;
727    }
728
729    if (!strcmp(cParameterName, "OMX.google.android.index.storeMetaDataInBuffers")) {
730        *pIndexType = static_cast<OMX_INDEXTYPE>(OMX_IndexStoreMetaDataInBuffers);
731        return OMX_ErrorNone;
732    }
733
734    if (!strcmp(cParameterName, "OMX.google.android.index.enableAndroidNativeBuffers")) {
735        *pIndexType = static_cast<OMX_INDEXTYPE>(OMX_IndexExtEnableNativeBuffer);
736        return OMX_ErrorNone;
737    }
738
739    if (!strcmp(cParameterName, "OMX.google.android.index.getAndroidNativeBufferUsage")) {
740        *pIndexType = static_cast<OMX_INDEXTYPE>(OMX_IndexExtGetNativeBufferUsage);
741        return OMX_ErrorNone;
742    }
743
744    if (!strcmp(cParameterName, "OMX.google.android.index.useAndroidNativeBuffer")) {
745        *pIndexType = static_cast<OMX_INDEXTYPE>(OMX_IndexExtUseNativeBuffer);
746        return OMX_ErrorNone;
747    }
748
749    if (!strcmp(cParameterName, "OMX.Intel.index.rotation")) {
750        *pIndexType = static_cast<OMX_INDEXTYPE>(OMX_IndexExtRotationDegrees);
751        return OMX_ErrorNone;
752    }
753
754    if (!strcmp(cParameterName, "OMX.Intel.index.enableSyncEncoding")) {
755        *pIndexType = static_cast<OMX_INDEXTYPE>(OMX_IndexExtSyncEncoding);
756        return OMX_ErrorNone;
757    }
758
759    return OMX_ErrorUnsupportedIndex;
760}
761
762OMX_ERRORTYPE ComponentBase::GetState(
763    OMX_IN  OMX_HANDLETYPE hComponent,
764    OMX_OUT OMX_STATETYPE* pState)
765{
766    ComponentBase *cbase;
767
768    if (!hComponent)
769        return OMX_ErrorBadParameter;
770
771    cbase = static_cast<ComponentBase *>
772        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
773    if (!cbase)
774        return OMX_ErrorBadParameter;
775
776    return cbase->CBaseGetState(hComponent, pState);
777}
778
779OMX_ERRORTYPE ComponentBase::CBaseGetState(
780    OMX_IN  OMX_HANDLETYPE hComponent,
781    OMX_OUT OMX_STATETYPE* pState)
782{
783    if (hComponent != handle)
784        return OMX_ErrorBadParameter;
785
786    pthread_mutex_lock(&state_block);
787    *pState = state;
788    pthread_mutex_unlock(&state_block);
789    return OMX_ErrorNone;
790}
791OMX_ERRORTYPE ComponentBase::UseBuffer(
792    OMX_IN OMX_HANDLETYPE hComponent,
793    OMX_INOUT OMX_BUFFERHEADERTYPE **ppBufferHdr,
794    OMX_IN OMX_U32 nPortIndex,
795    OMX_IN OMX_PTR pAppPrivate,
796    OMX_IN OMX_U32 nSizeBytes,
797    OMX_IN OMX_U8 *pBuffer)
798{
799    ComponentBase *cbase;
800
801    if (!hComponent)
802        return OMX_ErrorBadParameter;
803
804    cbase = static_cast<ComponentBase *>
805        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
806    if (!cbase)
807        return OMX_ErrorBadParameter;
808
809    return cbase->CBaseUseBuffer(hComponent, ppBufferHdr, nPortIndex,
810                                 pAppPrivate, nSizeBytes, pBuffer);
811}
812
813OMX_ERRORTYPE ComponentBase::CBaseUseBuffer(
814    OMX_IN OMX_HANDLETYPE hComponent,
815    OMX_INOUT OMX_BUFFERHEADERTYPE **ppBufferHdr,
816    OMX_IN OMX_U32 nPortIndex,
817    OMX_IN OMX_PTR pAppPrivate,
818    OMX_IN OMX_U32 nSizeBytes,
819    OMX_IN OMX_U8 *pBuffer)
820{
821    PortBase *port = NULL;
822    OMX_ERRORTYPE ret;
823
824    if (hComponent != handle)
825        return OMX_ErrorBadParameter;
826
827    if (!ppBufferHdr)
828        return OMX_ErrorBadParameter;
829    *ppBufferHdr = NULL;
830
831    if (!pBuffer)
832        return OMX_ErrorBadParameter;
833
834    if (ports)
835        if (nPortIndex < nr_ports)
836            port = ports[nPortIndex];
837
838    if (!port)
839        return OMX_ErrorBadParameter;
840
841    if (port->IsEnabled()) {
842        if (state != OMX_StateLoaded && state != OMX_StateWaitForResources)
843            return OMX_ErrorIncorrectStateOperation;
844    }
845
846    return port->UseBuffer(ppBufferHdr, nPortIndex, pAppPrivate, nSizeBytes,
847                           pBuffer);
848}
849
850OMX_ERRORTYPE ComponentBase::AllocateBuffer(
851    OMX_IN OMX_HANDLETYPE hComponent,
852    OMX_INOUT OMX_BUFFERHEADERTYPE **ppBuffer,
853    OMX_IN OMX_U32 nPortIndex,
854    OMX_IN OMX_PTR pAppPrivate,
855    OMX_IN OMX_U32 nSizeBytes)
856{
857    ComponentBase *cbase;
858
859    if (!hComponent)
860        return OMX_ErrorBadParameter;
861
862    cbase = static_cast<ComponentBase *>
863        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
864    if (!cbase)
865        return OMX_ErrorBadParameter;
866
867    return cbase->CBaseAllocateBuffer(hComponent, ppBuffer, nPortIndex,
868                                      pAppPrivate, nSizeBytes);
869}
870
871OMX_ERRORTYPE ComponentBase::CBaseAllocateBuffer(
872    OMX_IN OMX_HANDLETYPE hComponent,
873    OMX_INOUT OMX_BUFFERHEADERTYPE **ppBuffer,
874    OMX_IN OMX_U32 nPortIndex,
875    OMX_IN OMX_PTR pAppPrivate,
876    OMX_IN OMX_U32 nSizeBytes)
877{
878    PortBase *port = NULL;
879    OMX_ERRORTYPE ret;
880
881    if (hComponent != handle)
882        return OMX_ErrorBadParameter;
883
884    if (!ppBuffer)
885        return OMX_ErrorBadParameter;
886    *ppBuffer = NULL;
887
888    if (ports)
889        if (nPortIndex < nr_ports)
890            port = ports[nPortIndex];
891
892    if (!port)
893        return OMX_ErrorBadParameter;
894
895    if (port->IsEnabled()) {
896        if (state != OMX_StateLoaded && state != OMX_StateWaitForResources)
897            return OMX_ErrorIncorrectStateOperation;
898    }
899
900    return port->AllocateBuffer(ppBuffer, nPortIndex, pAppPrivate, nSizeBytes);
901}
902
903OMX_ERRORTYPE ComponentBase::FreeBuffer(
904    OMX_IN  OMX_HANDLETYPE hComponent,
905    OMX_IN  OMX_U32 nPortIndex,
906    OMX_IN  OMX_BUFFERHEADERTYPE *pBuffer)
907{
908    ComponentBase *cbase;
909
910    if (!hComponent)
911        return OMX_ErrorBadParameter;
912
913    cbase = static_cast<ComponentBase *>
914        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
915    if (!cbase)
916        return OMX_ErrorBadParameter;
917
918    return cbase->CBaseFreeBuffer(hComponent, nPortIndex, pBuffer);
919}
920
921OMX_ERRORTYPE ComponentBase::CBaseFreeBuffer(
922    OMX_IN  OMX_HANDLETYPE hComponent,
923    OMX_IN  OMX_U32 nPortIndex,
924    OMX_IN  OMX_BUFFERHEADERTYPE *pBuffer)
925{
926    PortBase *port = NULL;
927    OMX_ERRORTYPE ret;
928
929    if (hComponent != handle)
930        return OMX_ErrorBadParameter;
931
932    if (!pBuffer)
933        return OMX_ErrorBadParameter;
934
935    if (ports)
936        if (nPortIndex < nr_ports)
937            port = ports[nPortIndex];
938
939    if (!port)
940        return OMX_ErrorBadParameter;
941
942    ProcessorPreFreeBuffer(nPortIndex, pBuffer);
943
944    return port->FreeBuffer(nPortIndex, pBuffer);
945}
946
947OMX_ERRORTYPE ComponentBase::EmptyThisBuffer(
948    OMX_IN  OMX_HANDLETYPE hComponent,
949    OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer)
950{
951    ComponentBase *cbase;
952
953    if (!hComponent)
954        return OMX_ErrorBadParameter;
955
956    cbase = static_cast<ComponentBase *>
957        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
958    if (!cbase)
959        return OMX_ErrorBadParameter;
960
961    return cbase->CBaseEmptyThisBuffer(hComponent, pBuffer);
962}
963
964OMX_ERRORTYPE ComponentBase::CBaseEmptyThisBuffer(
965    OMX_IN  OMX_HANDLETYPE hComponent,
966    OMX_IN  OMX_BUFFERHEADERTYPE *pBuffer)
967{
968    PortBase *port = NULL;
969    OMX_U32 port_index;
970    OMX_ERRORTYPE ret;
971
972    if ((hComponent != handle) || !pBuffer)
973        return OMX_ErrorBadParameter;
974
975    ret = CheckTypeHeader(pBuffer, sizeof(OMX_BUFFERHEADERTYPE));
976    if (ret != OMX_ErrorNone)
977        return ret;
978
979    port_index = pBuffer->nInputPortIndex;
980    if (port_index == (OMX_U32)-1)
981        return OMX_ErrorBadParameter;
982
983    if (ports)
984        if (port_index < nr_ports)
985            port = ports[port_index];
986
987    if (!port)
988        return OMX_ErrorBadParameter;
989
990    if (pBuffer->pInputPortPrivate != port)
991        return OMX_ErrorBadParameter;
992
993    if (port->IsEnabled()) {
994        if (state != OMX_StateIdle && state != OMX_StateExecuting &&
995            state != OMX_StatePause)
996            return OMX_ErrorIncorrectStateOperation;
997    }
998
999    if (!pBuffer->hMarkTargetComponent) {
1000        OMX_MARKTYPE *mark;
1001
1002        mark = port->PopMark();
1003        if (mark) {
1004            pBuffer->hMarkTargetComponent = mark->hMarkTargetComponent;
1005            pBuffer->pMarkData = mark->pMarkData;
1006            free(mark);
1007        }
1008    }
1009
1010    ProcessorPreEmptyBuffer(pBuffer);
1011
1012    ret = port->PushThisBuffer(pBuffer);
1013    if (ret == OMX_ErrorNone)
1014        bufferwork->ScheduleWork(this);
1015
1016    return ret;
1017}
1018
1019OMX_ERRORTYPE ComponentBase::FillThisBuffer(
1020    OMX_IN  OMX_HANDLETYPE hComponent,
1021    OMX_IN  OMX_BUFFERHEADERTYPE *pBuffer)
1022{
1023    ComponentBase *cbase;
1024
1025    if (!hComponent)
1026        return OMX_ErrorBadParameter;
1027
1028    cbase = static_cast<ComponentBase *>
1029        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
1030    if (!cbase)
1031        return OMX_ErrorBadParameter;
1032
1033    return cbase->CBaseFillThisBuffer(hComponent, pBuffer);
1034}
1035
1036OMX_ERRORTYPE ComponentBase::CBaseFillThisBuffer(
1037    OMX_IN  OMX_HANDLETYPE hComponent,
1038    OMX_IN  OMX_BUFFERHEADERTYPE *pBuffer)
1039{
1040    PortBase *port = NULL;
1041    OMX_U32 port_index;
1042    OMX_ERRORTYPE ret;
1043
1044    if ((hComponent != handle) || !pBuffer)
1045        return OMX_ErrorBadParameter;
1046
1047    ret = CheckTypeHeader(pBuffer, sizeof(OMX_BUFFERHEADERTYPE));
1048    if (ret != OMX_ErrorNone)
1049        return ret;
1050
1051    port_index = pBuffer->nOutputPortIndex;
1052    if (port_index == (OMX_U32)-1)
1053        return OMX_ErrorBadParameter;
1054
1055    if (ports)
1056        if (port_index < nr_ports)
1057            port = ports[port_index];
1058
1059    if (!port)
1060        return OMX_ErrorBadParameter;
1061
1062    if (pBuffer->pOutputPortPrivate != port)
1063        return OMX_ErrorBadParameter;
1064
1065    if (port->IsEnabled()) {
1066        if (state != OMX_StateIdle && state != OMX_StateExecuting &&
1067            state != OMX_StatePause)
1068            return OMX_ErrorIncorrectStateOperation;
1069    }
1070
1071    ProcessorPreFillBuffer(pBuffer);
1072
1073    ret = port->PushThisBuffer(pBuffer);
1074    if (ret == OMX_ErrorNone)
1075        bufferwork->ScheduleWork(this);
1076
1077    return ret;
1078}
1079
1080OMX_ERRORTYPE ComponentBase::SetCallbacks(
1081    OMX_IN  OMX_HANDLETYPE hComponent,
1082    OMX_IN  OMX_CALLBACKTYPE* pCallbacks,
1083    OMX_IN  OMX_PTR pAppData)
1084{
1085    ComponentBase *cbase;
1086
1087    if (!hComponent)
1088        return OMX_ErrorBadParameter;
1089
1090    cbase = static_cast<ComponentBase *>
1091        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
1092    if (!cbase)
1093        return OMX_ErrorBadParameter;
1094
1095    return cbase->CBaseSetCallbacks(hComponent, pCallbacks, pAppData);
1096}
1097
1098OMX_ERRORTYPE ComponentBase::CBaseSetCallbacks(
1099    OMX_IN  OMX_HANDLETYPE hComponent,
1100    OMX_IN  OMX_CALLBACKTYPE *pCallbacks,
1101    OMX_IN  OMX_PTR pAppData)
1102{
1103    if (hComponent != handle)
1104        return OMX_ErrorBadParameter;
1105
1106    appdata = pAppData;
1107    callbacks = pCallbacks;
1108
1109    return OMX_ErrorNone;
1110}
1111
1112OMX_ERRORTYPE ComponentBase::ComponentRoleEnum(
1113    OMX_IN OMX_HANDLETYPE hComponent,
1114    OMX_OUT OMX_U8 *cRole,
1115    OMX_IN OMX_U32 nIndex)
1116{
1117    ComponentBase *cbase;
1118
1119    if (!hComponent)
1120        return OMX_ErrorBadParameter;
1121
1122    cbase = static_cast<ComponentBase *>
1123        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
1124    if (!cbase)
1125        return OMX_ErrorBadParameter;
1126
1127    return cbase->CBaseComponentRoleEnum(hComponent, cRole, nIndex);
1128}
1129
1130OMX_ERRORTYPE ComponentBase::CBaseComponentRoleEnum(
1131    OMX_IN OMX_HANDLETYPE hComponent,
1132    OMX_OUT OMX_U8 *cRole,
1133    OMX_IN OMX_U32 nIndex)
1134{
1135    if (hComponent != (OMX_HANDLETYPE *)this->handle)
1136        return OMX_ErrorBadParameter;
1137
1138    if (nIndex >= nr_roles)
1139        return OMX_ErrorBadParameter;
1140
1141    strncpy((char *)cRole, (const char *)roles[nIndex],
1142            OMX_MAX_STRINGNAME_SIZE);
1143    return OMX_ErrorNone;
1144}
1145
1146/* implement CmdHandlerInterface */
1147static const char *cmd_name[OMX_CommandMarkBuffer+2] = {
1148    "OMX_CommandStateSet",
1149    "OMX_CommandFlush",
1150    "OMX_CommandPortDisable",
1151    "OMX_CommandPortEnable",
1152    "OMX_CommandMarkBuffer",
1153    "Unknown Command",
1154};
1155
1156static inline const char *GetCmdName(OMX_COMMANDTYPE cmd)
1157{
1158    if (cmd > OMX_CommandMarkBuffer)
1159        cmd = (OMX_COMMANDTYPE)(OMX_CommandMarkBuffer+1);
1160
1161    return cmd_name[cmd];
1162}
1163
1164void ComponentBase::CmdHandler(struct cmd_s *cmd)
1165{
1166    LOGV("%s:%s: handling %s command\n",
1167         GetName(), GetWorkingRole(), GetCmdName(cmd->cmd));
1168
1169    switch (cmd->cmd) {
1170    case OMX_CommandStateSet: {
1171        OMX_STATETYPE transition = (OMX_STATETYPE)cmd->param1;
1172
1173        pthread_mutex_lock(&state_block);
1174        TransState(transition);
1175        pthread_mutex_unlock(&state_block);
1176        break;
1177    }
1178    case OMX_CommandFlush: {
1179        OMX_U32 port_index = cmd->param1;
1180        pthread_mutex_lock(&ports_block);
1181        ProcessorFlush(port_index);
1182        FlushPort(port_index, 1);
1183        pthread_mutex_unlock(&ports_block);
1184        break;
1185    }
1186    case OMX_CommandPortDisable: {
1187        OMX_U32 port_index = cmd->param1;
1188
1189        TransStatePort(port_index, PortBase::OMX_PortDisabled);
1190        break;
1191    }
1192    case OMX_CommandPortEnable: {
1193        OMX_U32 port_index = cmd->param1;
1194
1195        TransStatePort(port_index, PortBase::OMX_PortEnabled);
1196        break;
1197    }
1198    case OMX_CommandMarkBuffer: {
1199        OMX_U32 port_index = (OMX_U32)cmd->param1;
1200        OMX_MARKTYPE *mark = (OMX_MARKTYPE *)cmd->cmddata;
1201
1202        PushThisMark(port_index, mark);
1203        break;
1204    }
1205    default:
1206        LOGE("%s:%s:%s: exit failure, command %d cannot be handled\n",
1207             GetName(), GetWorkingRole(), GetCmdName(cmd->cmd), cmd->cmd);
1208        break;
1209    } /* switch */
1210
1211    LOGV("%s:%s: command %s handling done\n",
1212         GetName(), GetWorkingRole(), GetCmdName(cmd->cmd));
1213}
1214
1215/*
1216 * SendCommand:OMX_CommandStateSet
1217 * called in CmdHandler or called in other parts of component for reporting
1218 * internal error (OMX_StateInvalid).
1219 */
1220/*
1221 * Todo
1222 *   Resource Management (OMX_StateWaitForResources)
1223 *   for now, we never notify OMX_ErrorInsufficientResources,
1224 *   so IL client doesn't try to set component' state OMX_StateWaitForResources
1225 */
1226static const char *state_name[OMX_StateWaitForResources+2] = {
1227    "OMX_StateInvalid",
1228    "OMX_StateLoaded",
1229    "OMX_StateIdle",
1230    "OMX_StateExecuting",
1231    "OMX_StatePause",
1232    "OMX_StateWaitForResources",
1233    "Unknown State",
1234};
1235
1236static inline const char *GetStateName(OMX_STATETYPE state)
1237{
1238    if (state > OMX_StateWaitForResources)
1239        state = (OMX_STATETYPE)(OMX_StateWaitForResources+1);
1240
1241    return state_name[state];
1242}
1243
1244void ComponentBase::TransState(OMX_STATETYPE transition)
1245{
1246    OMX_STATETYPE current = this->state;
1247    OMX_EVENTTYPE event;
1248    OMX_U32 data1, data2;
1249    OMX_ERRORTYPE ret;
1250
1251    LOGV("%s:%s: try to transit state from %s to %s\n",
1252         GetName(), GetWorkingRole(), GetStateName(current),
1253         GetStateName(transition));
1254
1255    /* same state */
1256    if (current == transition) {
1257        ret = OMX_ErrorSameState;
1258        LOGE("%s:%s: exit failure, same state (%s)\n",
1259             GetName(), GetWorkingRole(), GetStateName(current));
1260        goto notify_event;
1261    }
1262
1263    /* invalid state */
1264    if (current == OMX_StateInvalid) {
1265        ret = OMX_ErrorInvalidState;
1266        LOGE("%s:%s: exit failure, current state is OMX_StateInvalid\n",
1267             GetName(), GetWorkingRole());
1268        goto notify_event;
1269    }
1270
1271    if (transition == OMX_StateLoaded)
1272        ret = TransStateToLoaded(current);
1273    else if (transition == OMX_StateIdle)
1274        ret = TransStateToIdle(current);
1275    else if (transition == OMX_StateExecuting)
1276        ret = TransStateToExecuting(current);
1277    else if (transition == OMX_StatePause)
1278        ret = TransStateToPause(current);
1279    else if (transition == OMX_StateInvalid)
1280        ret = TransStateToInvalid(current);
1281    else if (transition == OMX_StateWaitForResources)
1282        ret = TransStateToWaitForResources(current);
1283    else
1284        ret = OMX_ErrorIncorrectStateTransition;
1285
1286notify_event:
1287    if (ret == OMX_ErrorNone) {
1288        event = OMX_EventCmdComplete;
1289        data1 = OMX_CommandStateSet;
1290        data2 = transition;
1291
1292        state = transition;
1293        LOGD("%s:%s: transition from %s to %s completed",
1294             GetName(), GetWorkingRole(),
1295             GetStateName(current), GetStateName(transition));
1296    }
1297    else {
1298        event = OMX_EventError;
1299        data1 = ret;
1300        data2 = 0;
1301
1302        if (transition == OMX_StateInvalid || ret == OMX_ErrorInvalidState) {
1303            state = OMX_StateInvalid;
1304            LOGE("%s:%s: exit failure, transition from %s to %s, "
1305                 "current state is %s\n",
1306                 GetName(), GetWorkingRole(), GetStateName(current),
1307                 GetStateName(transition), GetStateName(state));
1308        }
1309    }
1310
1311    callbacks->EventHandler(handle, appdata, event, data1, data2, NULL);
1312
1313    /* WaitForResources workaround */
1314    if (ret == OMX_ErrorNone && transition == OMX_StateWaitForResources)
1315        callbacks->EventHandler(handle, appdata,
1316                                OMX_EventResourcesAcquired, 0, 0, NULL);
1317}
1318
1319inline OMX_ERRORTYPE ComponentBase::TransStateToLoaded(OMX_STATETYPE current)
1320{
1321    OMX_ERRORTYPE ret;
1322
1323    if (current == OMX_StateIdle) {
1324        OMX_U32 i;
1325
1326        for (i = 0; i < nr_ports; i++)
1327	{
1328            if (ports[i]->GetPortBufferCount() > 0) {
1329                ports[i]->WaitPortBufferCompletion();
1330	    };
1331	};
1332
1333        ret = ProcessorDeinit();
1334        if (ret != OMX_ErrorNone) {
1335            LOGE("%s:%s: ProcessorDeinit() failed "
1336                 "(ret : 0x%08x)\n", GetName(), GetWorkingRole(),
1337                 ret);
1338            goto out;
1339        }
1340    }
1341    else if (current == OMX_StateWaitForResources) {
1342        LOGV("%s:%s: "
1343             "state transition's requested from WaitForResources to Loaded\n",
1344             GetName(), GetWorkingRole());
1345
1346        /*
1347         * from WaitForResources to Loaded considered from Loaded to Loaded.
1348         * do nothing
1349         */
1350
1351        ret = OMX_ErrorNone;
1352    }
1353    else
1354        ret = OMX_ErrorIncorrectStateTransition;
1355
1356out:
1357    return ret;
1358}
1359
1360inline OMX_ERRORTYPE ComponentBase::TransStateToIdle(OMX_STATETYPE current)
1361{
1362    OMX_ERRORTYPE ret;
1363
1364    if (current == OMX_StateLoaded) {
1365        OMX_U32 i;
1366        for (i = 0; i < nr_ports; i++) {
1367             if (ports[i]->IsEnabled())
1368                 ports[i]->WaitPortBufferCompletion();
1369        }
1370
1371        ret = ProcessorInit();
1372        if (ret != OMX_ErrorNone) {
1373            LOGE("%s:%s: ProcessorInit() failed (ret : 0x%08x)\n",
1374                 GetName(), GetWorkingRole(), ret);
1375            goto out;
1376        }
1377    }
1378    else if ((current == OMX_StatePause) || (current == OMX_StateExecuting)) {
1379        pthread_mutex_lock(&ports_block);
1380        FlushPort(OMX_ALL, 0);
1381        pthread_mutex_unlock(&ports_block);
1382        LOGV("%s:%s: flushed all ports\n", GetName(), GetWorkingRole());
1383
1384        bufferwork->CancelScheduledWork(this);
1385        LOGV("%s:%s: discarded all scheduled buffer process work\n",
1386             GetName(), GetWorkingRole());
1387
1388        if (current == OMX_StatePause) {
1389            bufferwork->ResumeWork();
1390            LOGV("%s:%s: buffer process work resumed\n",
1391                 GetName(), GetWorkingRole());
1392        }
1393
1394        bufferwork->StopWork();
1395        LOGV("%s:%s: buffer process work stopped\n",
1396             GetName(), GetWorkingRole());
1397
1398        ret = ProcessorStop();
1399        if (ret != OMX_ErrorNone) {
1400            LOGE("%s:%s: ProcessorStop() failed (ret : 0x%08x)\n",
1401                 GetName(), GetWorkingRole(), ret);
1402            goto out;
1403        }
1404    }
1405    else if (current == OMX_StateWaitForResources) {
1406        LOGV("%s:%s: "
1407             "state transition's requested from WaitForResources to Idle\n",
1408             GetName(), GetWorkingRole());
1409
1410        /* same as Loaded to Idle BUT DO NOTHING for now */
1411
1412        ret = OMX_ErrorNone;
1413    }
1414    else
1415        ret = OMX_ErrorIncorrectStateTransition;
1416
1417out:
1418    return ret;
1419}
1420
1421inline OMX_ERRORTYPE
1422ComponentBase::TransStateToExecuting(OMX_STATETYPE current)
1423{
1424    OMX_ERRORTYPE ret;
1425
1426    if (current == OMX_StateIdle) {
1427        bufferwork->StartWork(true);
1428        LOGV("%s:%s: buffer process work started with executing state\n",
1429             GetName(), GetWorkingRole());
1430
1431        ret = ProcessorStart();
1432        if (ret != OMX_ErrorNone) {
1433            LOGE("%s:%s: ProcessorStart() failed (ret : 0x%08x)\n",
1434                 GetName(), GetWorkingRole(), ret);
1435            goto out;
1436        }
1437    }
1438    else if (current == OMX_StatePause) {
1439        bufferwork->ResumeWork();
1440        LOGV("%s:%s: buffer process work resumed\n",
1441             GetName(), GetWorkingRole());
1442
1443        ret = ProcessorResume();
1444        if (ret != OMX_ErrorNone) {
1445            LOGE("%s:%s: ProcessorResume() failed (ret : 0x%08x)\n",
1446                 GetName(), GetWorkingRole(), ret);
1447            goto out;
1448        }
1449    }
1450    else
1451        ret = OMX_ErrorIncorrectStateTransition;
1452
1453out:
1454    return ret;
1455}
1456
1457inline OMX_ERRORTYPE ComponentBase::TransStateToPause(OMX_STATETYPE current)
1458{
1459    OMX_ERRORTYPE ret;
1460
1461    if (current == OMX_StateIdle) {
1462        bufferwork->StartWork(false);
1463        LOGV("%s:%s: buffer process work started with paused state\n",
1464             GetName(), GetWorkingRole());
1465
1466        ret = ProcessorStart();
1467        if (ret != OMX_ErrorNone) {
1468            LOGE("%s:%s: ProcessorSart() failed (ret : 0x%08x)\n",
1469                 GetName(), GetWorkingRole(), ret);
1470            goto out;
1471        }
1472    }
1473    else if (current == OMX_StateExecuting) {
1474        bufferwork->PauseWork();
1475        LOGV("%s:%s: buffer process work paused\n",
1476             GetName(), GetWorkingRole());
1477
1478        ret = ProcessorPause();
1479        if (ret != OMX_ErrorNone) {
1480            LOGE("%s:%s: ProcessorPause() failed (ret : 0x%08x)\n",
1481                 GetName(), GetWorkingRole(), ret);
1482            goto out;
1483        }
1484    }
1485    else
1486        ret = OMX_ErrorIncorrectStateTransition;
1487
1488out:
1489    return ret;
1490}
1491
1492inline OMX_ERRORTYPE ComponentBase::TransStateToInvalid(OMX_STATETYPE current)
1493{
1494    OMX_ERRORTYPE ret = OMX_ErrorInvalidState;
1495
1496    /*
1497     * Todo
1498     *   graceful escape
1499     */
1500
1501    return ret;
1502}
1503
1504inline OMX_ERRORTYPE
1505ComponentBase::TransStateToWaitForResources(OMX_STATETYPE current)
1506{
1507    OMX_ERRORTYPE ret;
1508
1509    if (current == OMX_StateLoaded) {
1510        LOGV("%s:%s: "
1511             "state transition's requested from Loaded to WaitForResources\n",
1512             GetName(), GetWorkingRole());
1513        ret = OMX_ErrorNone;
1514    }
1515    else
1516        ret = OMX_ErrorIncorrectStateTransition;
1517
1518    return ret;
1519}
1520
1521/* mark buffer */
1522void ComponentBase::PushThisMark(OMX_U32 port_index, OMX_MARKTYPE *mark)
1523{
1524    PortBase *port = NULL;
1525    OMX_EVENTTYPE event;
1526    OMX_U32 data1, data2;
1527    OMX_ERRORTYPE ret;
1528
1529    if (ports)
1530        if (port_index < nr_ports)
1531            port = ports[port_index];
1532
1533    if (!port) {
1534        ret = OMX_ErrorBadPortIndex;
1535        goto notify_event;
1536    }
1537
1538    ret = port->PushMark(mark);
1539    if (ret != OMX_ErrorNone) {
1540        /* don't report OMX_ErrorInsufficientResources */
1541        ret = OMX_ErrorUndefined;
1542        goto notify_event;
1543    }
1544
1545notify_event:
1546    if (ret == OMX_ErrorNone) {
1547        event = OMX_EventCmdComplete;
1548        data1 = OMX_CommandMarkBuffer;
1549        data2 = port_index;
1550    }
1551    else {
1552        event = OMX_EventError;
1553        data1 = ret;
1554        data2 = 0;
1555    }
1556
1557    callbacks->EventHandler(handle, appdata, event, data1, data2, NULL);
1558}
1559
1560void ComponentBase::FlushPort(OMX_U32 port_index, bool notify)
1561{
1562    OMX_U32 i, from_index, to_index;
1563
1564    if ((port_index != OMX_ALL) && (port_index > nr_ports-1))
1565        return;
1566
1567    if (port_index == OMX_ALL) {
1568        from_index = 0;
1569        to_index = nr_ports - 1;
1570    }
1571    else {
1572        from_index = port_index;
1573        to_index = port_index;
1574    }
1575
1576    LOGV("%s:%s: flush ports (from index %lu to %lu)\n",
1577         GetName(), GetWorkingRole(), from_index, to_index);
1578
1579    for (i = from_index; i <= to_index; i++) {
1580        ports[i]->FlushPort();
1581        if (notify)
1582            callbacks->EventHandler(handle, appdata, OMX_EventCmdComplete,
1583                                    OMX_CommandFlush, i, NULL);
1584    }
1585
1586    LOGV("%s:%s: flush ports done\n", GetName(), GetWorkingRole());
1587}
1588
1589extern const char *GetPortStateName(OMX_U8 state); //portbase.cpp
1590
1591void ComponentBase::TransStatePort(OMX_U32 port_index, OMX_U8 state)
1592{
1593    OMX_EVENTTYPE event;
1594    OMX_U32 data1, data2;
1595    OMX_U32 i, from_index, to_index;
1596    OMX_ERRORTYPE ret;
1597
1598    if ((port_index != OMX_ALL) && (port_index > nr_ports-1))
1599        return;
1600
1601    if (port_index == OMX_ALL) {
1602        from_index = 0;
1603        to_index = nr_ports - 1;
1604    }
1605    else {
1606        from_index = port_index;
1607        to_index = port_index;
1608    }
1609
1610    LOGV("%s:%s: transit ports state to %s (from index %lu to %lu)\n",
1611         GetName(), GetWorkingRole(), GetPortStateName(state),
1612         from_index, to_index);
1613
1614    pthread_mutex_lock(&ports_block);
1615    for (i = from_index; i <= to_index; i++) {
1616        ret = ports[i]->TransState(state);
1617        if (ret == OMX_ErrorNone) {
1618            event = OMX_EventCmdComplete;
1619            if (state == PortBase::OMX_PortEnabled) {
1620                data1 = OMX_CommandPortEnable;
1621                ProcessorReset();
1622            } else {
1623                data1 = OMX_CommandPortDisable;
1624            }
1625            data2 = i;
1626        }
1627        else {
1628            event = OMX_EventError;
1629            data1 = ret;
1630            data2 = 0;
1631        }
1632        callbacks->EventHandler(handle, appdata, OMX_EventCmdComplete,
1633                                data1, data2, NULL);
1634    }
1635    pthread_mutex_unlock(&ports_block);
1636
1637    LOGV("%s:%s: transit ports state to %s completed\n",
1638         GetName(), GetWorkingRole(), GetPortStateName(state));
1639}
1640
1641/* set working role */
1642OMX_ERRORTYPE ComponentBase::SetWorkingRole(const OMX_STRING role)
1643{
1644    OMX_U32 i;
1645
1646    if (state != OMX_StateUnloaded && state != OMX_StateLoaded)
1647        return OMX_ErrorIncorrectStateOperation;
1648
1649    if (!role) {
1650        working_role = NULL;
1651        return OMX_ErrorNone;
1652    }
1653
1654    for (i = 0; i < nr_roles; i++) {
1655        if (!strcmp((char *)&roles[i][0], role)) {
1656            working_role = (OMX_STRING)&roles[i][0];
1657            return OMX_ErrorNone;
1658        }
1659    }
1660
1661    LOGE("%s: cannot find %s role\n", GetName(), role);
1662    return OMX_ErrorBadParameter;
1663}
1664
1665/* apply a working role for a component having multiple roles */
1666OMX_ERRORTYPE ComponentBase::ApplyWorkingRole(void)
1667{
1668    OMX_U32 i;
1669    OMX_ERRORTYPE ret;
1670
1671    if (state != OMX_StateUnloaded && state != OMX_StateLoaded)
1672        return OMX_ErrorIncorrectStateOperation;
1673
1674    if (!working_role)
1675        return OMX_ErrorBadParameter;
1676
1677    if (!callbacks || !appdata)
1678        return OMX_ErrorBadParameter;
1679
1680    ret = AllocatePorts();
1681    if (ret != OMX_ErrorNone) {
1682        LOGE("failed to AllocatePorts() (ret = 0x%08x)\n", ret);
1683        return ret;
1684    }
1685
1686    /* now we can access ports */
1687    for (i = 0; i < nr_ports; i++) {
1688        ports[i]->SetOwner(handle);
1689        ports[i]->SetCallbacks(handle, callbacks, appdata);
1690    }
1691
1692    LOGI("%s: set working role %s:", GetName(), GetWorkingRole());
1693    return OMX_ErrorNone;
1694}
1695
1696OMX_ERRORTYPE ComponentBase::AllocatePorts(void)
1697{
1698    OMX_DIRTYPE dir;
1699    bool has_input, has_output;
1700    OMX_U32 i;
1701    OMX_ERRORTYPE ret;
1702
1703    if (ports)
1704        return OMX_ErrorBadParameter;
1705
1706    ret = ComponentAllocatePorts();
1707    if (ret != OMX_ErrorNone) {
1708        LOGE("failed to %s::ComponentAllocatePorts(), ret = 0x%08x\n",
1709             name, ret);
1710        return ret;
1711    }
1712
1713    has_input = false;
1714    has_output = false;
1715    ret = OMX_ErrorNone;
1716    for (i = 0; i < nr_ports; i++) {
1717        dir = ports[i]->GetPortDirection();
1718        if (dir == OMX_DirInput)
1719            has_input = true;
1720        else if (dir == OMX_DirOutput)
1721            has_output = true;
1722        else {
1723            ret = OMX_ErrorUndefined;
1724            break;
1725        }
1726    }
1727    if (ret != OMX_ErrorNone)
1728        goto free_ports;
1729
1730    if ((has_input == false) && (has_output == true))
1731        cvariant = CVARIANT_SOURCE;
1732    else if ((has_input == true) && (has_output == true))
1733        cvariant = CVARIANT_FILTER;
1734    else if ((has_input == true) && (has_output == false))
1735        cvariant = CVARIANT_SINK;
1736    else
1737        goto free_ports;
1738
1739    return OMX_ErrorNone;
1740
1741free_ports:
1742    LOGE("%s(): exit, unknown component variant\n", __func__);
1743    FreePorts();
1744    return OMX_ErrorUndefined;
1745}
1746
1747/* called int FreeHandle() */
1748OMX_ERRORTYPE ComponentBase::FreePorts(void)
1749{
1750    if (ports) {
1751        OMX_U32 i, this_nr_ports = this->nr_ports;
1752
1753        for (i = 0; i < this_nr_ports; i++) {
1754            if (ports[i]) {
1755                OMX_MARKTYPE *mark;
1756                /* it should be empty before this */
1757                while ((mark = ports[i]->PopMark()))
1758                    free(mark);
1759
1760                delete ports[i];
1761                ports[i] = NULL;
1762            }
1763        }
1764        delete []ports;
1765        ports = NULL;
1766    }
1767
1768    return OMX_ErrorNone;
1769}
1770
1771/* buffer processing */
1772/* implement WorkableInterface */
1773void ComponentBase::Work(void)
1774{
1775    OMX_BUFFERHEADERTYPE **buffers[nr_ports];
1776    OMX_BUFFERHEADERTYPE *buffers_hdr[nr_ports];
1777    OMX_BUFFERHEADERTYPE *buffers_org[nr_ports];
1778    buffer_retain_t retain[nr_ports];
1779    OMX_U32 i;
1780    OMX_ERRORTYPE ret;
1781
1782    if (nr_ports == 0) {
1783        return;
1784    }
1785
1786    pthread_mutex_lock(&ports_block);
1787
1788    while(IsAllBufferAvailable())
1789    {
1790        for (i = 0; i < nr_ports; i++) {
1791            buffers_hdr[i] = ports[i]->PopBuffer();
1792            buffers[i] = &buffers_hdr[i];
1793            buffers_org[i] = buffers_hdr[i];
1794            retain[i] = BUFFER_RETAIN_NOT_RETAIN;
1795        }
1796
1797        if (working_role != NULL && !strncmp((char*)working_role, "video_decoder", 13)){
1798            ret = ProcessorProcess(buffers, &retain[0], nr_ports);
1799        }else{
1800            ret = ProcessorProcess(buffers_hdr, &retain[0], nr_ports);
1801        }
1802
1803        if (ret == OMX_ErrorNone) {
1804            PostProcessBuffers(buffers, &retain[0]);
1805
1806            for (i = 0; i < nr_ports; i++) {
1807                if(retain[i] == BUFFER_RETAIN_GETAGAIN) {
1808                    ports[i]->RetainThisBuffer(*buffers[i], false);
1809                }
1810                else if (retain[i] == BUFFER_RETAIN_ACCUMULATE) {
1811                    ports[i]->RetainThisBuffer(*buffers[i], true);
1812                }
1813                else if (retain[i] == BUFFER_RETAIN_OVERRIDDEN) {
1814                    ports[i]->RetainAndReturnBuffer(buffers_org[i], *buffers[i]);
1815                }
1816                else if (retain[i] == BUFFER_RETAIN_CACHE) {
1817                    //nothing to do
1818                } else {
1819                    ports[i]->ReturnThisBuffer(*buffers[i]);
1820                }
1821            }
1822        }
1823        else {
1824
1825            for (i = 0; i < nr_ports; i++) {
1826                /* return buffers by hands, these buffers're not in queue */
1827                ports[i]->ReturnThisBuffer(*buffers[i]);
1828                /* flush ports */
1829                ports[i]->FlushPort();
1830            }
1831
1832            callbacks->EventHandler(handle, appdata, OMX_EventError, ret,
1833                                    0, NULL);
1834        }
1835    }
1836
1837    pthread_mutex_unlock(&ports_block);
1838}
1839
1840bool ComponentBase::IsAllBufferAvailable(void)
1841{
1842    OMX_U32 i;
1843    OMX_U32 nr_avail = 0;
1844
1845    for (i = 0; i < nr_ports; i++) {
1846        OMX_U32 length = 0;
1847
1848        if (ports[i]->IsEnabled())
1849            length = ports[i]->BufferQueueLength();
1850
1851        if (length)
1852            nr_avail++;
1853    }
1854
1855    if (nr_avail == nr_ports)
1856        return true;
1857    else
1858        return false;
1859}
1860
1861inline void ComponentBase::SourcePostProcessBuffers(
1862    OMX_BUFFERHEADERTYPE ***buffers,
1863    const buffer_retain_t *retain)
1864{
1865    OMX_U32 i;
1866
1867    for (i = 0; i < nr_ports; i++) {
1868        /*
1869         * in case of source component, buffers're marked when they come
1870         * from the ouput ports
1871         */
1872        if (!(*buffers[i])->hMarkTargetComponent) {
1873            OMX_MARKTYPE *mark;
1874
1875            mark = ports[i]->PopMark();
1876            if (mark) {
1877                (*buffers[i])->hMarkTargetComponent = mark->hMarkTargetComponent;
1878                (*buffers[i])->pMarkData = mark->pMarkData;
1879                free(mark);
1880            }
1881        }
1882    }
1883}
1884
1885inline void ComponentBase::FilterPostProcessBuffers(
1886    OMX_BUFFERHEADERTYPE ***buffers,
1887    const buffer_retain_t *retain)
1888{
1889    OMX_MARKTYPE *mark;
1890    OMX_U32 i, j;
1891
1892    for (i = 0; i < nr_ports; i++) {
1893        if (ports[i]->GetPortDirection() == OMX_DirInput) {
1894            for (j = 0; j < nr_ports; j++) {
1895                if (ports[j]->GetPortDirection() != OMX_DirOutput)
1896                    continue;
1897
1898                /* propagates EOS flag */
1899                /* clear input EOS at the end of this loop */
1900                if (retain[i] != BUFFER_RETAIN_GETAGAIN) {
1901                    if ((*buffers[i])->nFlags & OMX_BUFFERFLAG_EOS)
1902                        (*buffers[j])->nFlags |= OMX_BUFFERFLAG_EOS;
1903                }
1904
1905                /* propagates marks */
1906                /*
1907                 * if hMarkTargetComponent == handle then the mark's not
1908                 * propagated
1909                 */
1910                if ((*buffers[i])->hMarkTargetComponent &&
1911                    ((*buffers[i])->hMarkTargetComponent != handle)) {
1912                    if ((*buffers[j])->hMarkTargetComponent) {
1913                        mark = (OMX_MARKTYPE *)malloc(sizeof(*mark));
1914                        if (mark) {
1915                            mark->hMarkTargetComponent =
1916                                (*buffers[i])->hMarkTargetComponent;
1917                            mark->pMarkData = (*buffers[i])->pMarkData;
1918                            ports[j]->PushMark(mark);
1919                            mark = NULL;
1920                            (*buffers[i])->hMarkTargetComponent = NULL;
1921                            (*buffers[i])->pMarkData = NULL;
1922                        }
1923                    }
1924                    else {
1925                        mark = ports[j]->PopMark();
1926                        if (mark) {
1927                            (*buffers[j])->hMarkTargetComponent =
1928                                mark->hMarkTargetComponent;
1929                            (*buffers[j])->pMarkData = mark->pMarkData;
1930                            free(mark);
1931
1932                            mark = (OMX_MARKTYPE *)malloc(sizeof(*mark));
1933                            if (mark) {
1934                                mark->hMarkTargetComponent =
1935                                    (*buffers[i])->hMarkTargetComponent;
1936                                mark->pMarkData = (*buffers[i])->pMarkData;
1937                                ports[j]->PushMark(mark);
1938                                mark = NULL;
1939                                (*buffers[i])->hMarkTargetComponent = NULL;
1940                                (*buffers[i])->pMarkData = NULL;
1941                            }
1942                        }
1943                        else {
1944                            (*buffers[j])->hMarkTargetComponent =
1945                                (*buffers[i])->hMarkTargetComponent;
1946                            (*buffers[j])->pMarkData = (*buffers[i])->pMarkData;
1947                            (*buffers[i])->hMarkTargetComponent = NULL;
1948                            (*buffers[i])->pMarkData = NULL;
1949                        }
1950                    }
1951                }
1952            }
1953            /* clear input buffer's EOS */
1954            if (retain[i] != BUFFER_RETAIN_GETAGAIN)
1955                (*buffers[i])->nFlags &= ~OMX_BUFFERFLAG_EOS;
1956        }
1957    }
1958}
1959
1960inline void ComponentBase::SinkPostProcessBuffers(
1961    OMX_BUFFERHEADERTYPE ***buffers,
1962    const buffer_retain_t *retain)
1963{
1964    return;
1965}
1966
1967void ComponentBase::PostProcessBuffers(OMX_BUFFERHEADERTYPE ***buffers,
1968                                       const buffer_retain_t *retain)
1969{
1970
1971    if (cvariant == CVARIANT_SOURCE)
1972        SourcePostProcessBuffers(buffers, retain);
1973    else if (cvariant == CVARIANT_FILTER)
1974        FilterPostProcessBuffers(buffers, retain);
1975    else if (cvariant == CVARIANT_SINK) {
1976        SinkPostProcessBuffers(buffers, retain);
1977    }
1978    else {
1979        LOGE("%s(): fatal error unknown component variant (%d)\n",
1980             __func__, cvariant);
1981    }
1982}
1983
1984/* processor default callbacks */
1985OMX_ERRORTYPE ComponentBase::ProcessorInit(void)
1986{
1987    return OMX_ErrorNone;
1988}
1989OMX_ERRORTYPE ComponentBase::ProcessorDeinit(void)
1990{
1991    return OMX_ErrorNone;
1992}
1993
1994OMX_ERRORTYPE ComponentBase::ProcessorStart(void)
1995{
1996    return OMX_ErrorNone;
1997}
1998
1999OMX_ERRORTYPE ComponentBase::ProcessorReset(void)
2000{
2001    return OMX_ErrorNone;
2002}
2003
2004
2005OMX_ERRORTYPE ComponentBase::ProcessorStop(void)
2006{
2007    return OMX_ErrorNone;
2008}
2009
2010OMX_ERRORTYPE ComponentBase::ProcessorPause(void)
2011{
2012    return OMX_ErrorNone;
2013}
2014
2015OMX_ERRORTYPE ComponentBase::ProcessorResume(void)
2016{
2017    return OMX_ErrorNone;
2018}
2019
2020OMX_ERRORTYPE ComponentBase::ProcessorFlush(OMX_U32 port_index)
2021{
2022    return OMX_ErrorNone;
2023}
2024
2025OMX_ERRORTYPE ComponentBase::ProcessorPreFillBuffer(OMX_BUFFERHEADERTYPE* buffer)
2026{
2027    return OMX_ErrorNone;
2028}
2029
2030OMX_ERRORTYPE ComponentBase::ProcessorPreEmptyBuffer(OMX_BUFFERHEADERTYPE* buffer)
2031{
2032    return OMX_ErrorNone;
2033}
2034
2035OMX_ERRORTYPE ComponentBase::ProcessorProcess(OMX_BUFFERHEADERTYPE **pBuffers,
2036                                           buffer_retain_t *retain,
2037                                           OMX_U32 nr_buffers)
2038{
2039    LOGE("ProcessorProcess not be implemented");
2040    return OMX_ErrorNotImplemented;
2041}
2042OMX_ERRORTYPE ComponentBase::ProcessorProcess(OMX_BUFFERHEADERTYPE ***pBuffers,
2043                                           buffer_retain_t *retain,
2044                                           OMX_U32 nr_buffers)
2045{
2046    LOGE("ProcessorProcess not be implemented");
2047    return OMX_ErrorNotImplemented;
2048}
2049
2050OMX_ERRORTYPE ComponentBase::ProcessorPreFreeBuffer(OMX_U32 nPortIndex, OMX_BUFFERHEADERTYPE* pBuffer)
2051{
2052    return OMX_ErrorNone;
2053
2054}
2055/* end of processor callbacks */
2056
2057/* helper for derived class */
2058const OMX_STRING ComponentBase::GetWorkingRole(void)
2059{
2060    return &working_role[0];
2061}
2062
2063const OMX_COMPONENTTYPE *ComponentBase::GetComponentHandle(void)
2064{
2065    return handle;
2066}
2067#if 0
2068void ComponentBase::DumpBuffer(const OMX_BUFFERHEADERTYPE *bufferheader,
2069                               bool dumpdata)
2070{
2071    OMX_U8 *pbuffer = bufferheader->pBuffer, *p;
2072    OMX_U32 offset = bufferheader->nOffset;
2073    OMX_U32 alloc_len = bufferheader->nAllocLen;
2074    OMX_U32 filled_len =  bufferheader->nFilledLen;
2075    OMX_U32 left = filled_len, oneline;
2076    OMX_U32 index = 0, i;
2077    /* 0x%04lx:  %02x %02x .. (n = 16)\n\0 */
2078    char prbuffer[8 + 3 * 0x10 + 2], *pp;
2079    OMX_U32 prbuffer_len;
2080
2081    LOGD("Component %s DumpBuffer\n", name);
2082    LOGD("%s port index = %lu",
2083         (bufferheader->nInputPortIndex != 0x7fffffff) ? "input" : "output",
2084         (bufferheader->nInputPortIndex != 0x7fffffff) ?
2085         bufferheader->nInputPortIndex : bufferheader->nOutputPortIndex);
2086    LOGD("nAllocLen = %lu, nOffset = %lu, nFilledLen = %lu\n",
2087         alloc_len, offset, filled_len);
2088    LOGD("nTimeStamp = %lld, nTickCount = %lu",
2089         bufferheader->nTimeStamp,
2090         bufferheader->nTickCount);
2091    LOGD("nFlags = 0x%08lx\n", bufferheader->nFlags);
2092    LOGD("hMarkTargetComponent = %p, pMarkData = %p\n",
2093         bufferheader->hMarkTargetComponent, bufferheader->pMarkData);
2094
2095    if (!pbuffer || !alloc_len || !filled_len)
2096        return;
2097
2098    if (offset + filled_len > alloc_len)
2099        return;
2100
2101    if (!dumpdata)
2102        return;
2103
2104    p = pbuffer + offset;
2105    while (left) {
2106        oneline = left > 0x10 ? 0x10 : left; /* 16 items per 1 line */
2107        pp += sprintf(pp, "0x%04lx: ", index);
2108        for (i = 0; i < oneline; i++)
2109            pp += sprintf(pp, " %02x", *(p + i));
2110        pp += sprintf(pp, "\n");
2111        *pp = '\0';
2112
2113        index += 0x10;
2114        p += oneline;
2115        left -= oneline;
2116
2117        pp = &prbuffer[0];
2118        LOGD("%s", pp);
2119    }
2120}
2121#endif
2122/* end of component methods & helpers */
2123
2124/*
2125 * omx header manipuation
2126 */
2127void ComponentBase::SetTypeHeader(OMX_PTR type, OMX_U32 size)
2128{
2129    OMX_U32 *nsize;
2130    OMX_VERSIONTYPE *nversion;
2131
2132    if (!type)
2133        return;
2134
2135    nsize = (OMX_U32 *)type;
2136    nversion = (OMX_VERSIONTYPE *)((OMX_U8 *)type + sizeof(OMX_U32));
2137
2138    *nsize = size;
2139    nversion->nVersion = OMX_SPEC_VERSION;
2140}
2141
2142OMX_ERRORTYPE ComponentBase::CheckTypeHeader(const OMX_PTR type, OMX_U32 size)
2143{
2144    OMX_U32 *nsize;
2145    OMX_VERSIONTYPE *nversion;
2146
2147    if (!type)
2148        return OMX_ErrorBadParameter;
2149
2150    nsize = (OMX_U32 *)type;
2151    nversion = (OMX_VERSIONTYPE *)((OMX_U8 *)type + sizeof(OMX_U32));
2152
2153    if (*nsize != size)
2154        return OMX_ErrorBadParameter;
2155
2156    if (nversion->nVersion != OMX_SPEC_VERSION)
2157        return OMX_ErrorVersionMismatch;
2158
2159    return OMX_ErrorNone;
2160}
2161
2162/* end of ComponentBase */
2163