componentbase.cpp revision fbd63aabced2ef7b356f2dea4903b5ca7b12362c
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    if (!strcmp(cParameterName, "OMX.google.android.index.prependSPSPPSToIDRFrames")) {
760        *pIndexType = static_cast<OMX_INDEXTYPE>(OMX_IndexExtPrependSPSPPS);
761        return OMX_ErrorNone;
762    }
763
764    return OMX_ErrorUnsupportedIndex;
765}
766
767OMX_ERRORTYPE ComponentBase::GetState(
768    OMX_IN  OMX_HANDLETYPE hComponent,
769    OMX_OUT OMX_STATETYPE* pState)
770{
771    ComponentBase *cbase;
772
773    if (!hComponent)
774        return OMX_ErrorBadParameter;
775
776    cbase = static_cast<ComponentBase *>
777        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
778    if (!cbase)
779        return OMX_ErrorBadParameter;
780
781    return cbase->CBaseGetState(hComponent, pState);
782}
783
784OMX_ERRORTYPE ComponentBase::CBaseGetState(
785    OMX_IN  OMX_HANDLETYPE hComponent,
786    OMX_OUT OMX_STATETYPE* pState)
787{
788    if (hComponent != handle)
789        return OMX_ErrorBadParameter;
790
791    pthread_mutex_lock(&state_block);
792    *pState = state;
793    pthread_mutex_unlock(&state_block);
794    return OMX_ErrorNone;
795}
796OMX_ERRORTYPE ComponentBase::UseBuffer(
797    OMX_IN OMX_HANDLETYPE hComponent,
798    OMX_INOUT OMX_BUFFERHEADERTYPE **ppBufferHdr,
799    OMX_IN OMX_U32 nPortIndex,
800    OMX_IN OMX_PTR pAppPrivate,
801    OMX_IN OMX_U32 nSizeBytes,
802    OMX_IN OMX_U8 *pBuffer)
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->CBaseUseBuffer(hComponent, ppBufferHdr, nPortIndex,
815                                 pAppPrivate, nSizeBytes, pBuffer);
816}
817
818OMX_ERRORTYPE ComponentBase::CBaseUseBuffer(
819    OMX_IN OMX_HANDLETYPE hComponent,
820    OMX_INOUT OMX_BUFFERHEADERTYPE **ppBufferHdr,
821    OMX_IN OMX_U32 nPortIndex,
822    OMX_IN OMX_PTR pAppPrivate,
823    OMX_IN OMX_U32 nSizeBytes,
824    OMX_IN OMX_U8 *pBuffer)
825{
826    PortBase *port = NULL;
827    OMX_ERRORTYPE ret;
828
829    if (hComponent != handle)
830        return OMX_ErrorBadParameter;
831
832    if (!ppBufferHdr)
833        return OMX_ErrorBadParameter;
834    *ppBufferHdr = NULL;
835
836    if (!pBuffer)
837        return OMX_ErrorBadParameter;
838
839    if (ports)
840        if (nPortIndex < nr_ports)
841            port = ports[nPortIndex];
842
843    if (!port)
844        return OMX_ErrorBadParameter;
845
846    if (port->IsEnabled()) {
847        if (state != OMX_StateLoaded && state != OMX_StateWaitForResources)
848            return OMX_ErrorIncorrectStateOperation;
849    }
850
851    return port->UseBuffer(ppBufferHdr, nPortIndex, pAppPrivate, nSizeBytes,
852                           pBuffer);
853}
854
855OMX_ERRORTYPE ComponentBase::AllocateBuffer(
856    OMX_IN OMX_HANDLETYPE hComponent,
857    OMX_INOUT OMX_BUFFERHEADERTYPE **ppBuffer,
858    OMX_IN OMX_U32 nPortIndex,
859    OMX_IN OMX_PTR pAppPrivate,
860    OMX_IN OMX_U32 nSizeBytes)
861{
862    ComponentBase *cbase;
863
864    if (!hComponent)
865        return OMX_ErrorBadParameter;
866
867    cbase = static_cast<ComponentBase *>
868        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
869    if (!cbase)
870        return OMX_ErrorBadParameter;
871
872    return cbase->CBaseAllocateBuffer(hComponent, ppBuffer, nPortIndex,
873                                      pAppPrivate, nSizeBytes);
874}
875
876OMX_ERRORTYPE ComponentBase::CBaseAllocateBuffer(
877    OMX_IN OMX_HANDLETYPE hComponent,
878    OMX_INOUT OMX_BUFFERHEADERTYPE **ppBuffer,
879    OMX_IN OMX_U32 nPortIndex,
880    OMX_IN OMX_PTR pAppPrivate,
881    OMX_IN OMX_U32 nSizeBytes)
882{
883    PortBase *port = NULL;
884    OMX_ERRORTYPE ret;
885
886    if (hComponent != handle)
887        return OMX_ErrorBadParameter;
888
889    if (!ppBuffer)
890        return OMX_ErrorBadParameter;
891    *ppBuffer = NULL;
892
893    if (ports)
894        if (nPortIndex < nr_ports)
895            port = ports[nPortIndex];
896
897    if (!port)
898        return OMX_ErrorBadParameter;
899
900    if (port->IsEnabled()) {
901        if (state != OMX_StateLoaded && state != OMX_StateWaitForResources)
902            return OMX_ErrorIncorrectStateOperation;
903    }
904
905    return port->AllocateBuffer(ppBuffer, nPortIndex, pAppPrivate, nSizeBytes);
906}
907
908OMX_ERRORTYPE ComponentBase::FreeBuffer(
909    OMX_IN  OMX_HANDLETYPE hComponent,
910    OMX_IN  OMX_U32 nPortIndex,
911    OMX_IN  OMX_BUFFERHEADERTYPE *pBuffer)
912{
913    ComponentBase *cbase;
914
915    if (!hComponent)
916        return OMX_ErrorBadParameter;
917
918    cbase = static_cast<ComponentBase *>
919        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
920    if (!cbase)
921        return OMX_ErrorBadParameter;
922
923    return cbase->CBaseFreeBuffer(hComponent, nPortIndex, pBuffer);
924}
925
926OMX_ERRORTYPE ComponentBase::CBaseFreeBuffer(
927    OMX_IN  OMX_HANDLETYPE hComponent,
928    OMX_IN  OMX_U32 nPortIndex,
929    OMX_IN  OMX_BUFFERHEADERTYPE *pBuffer)
930{
931    PortBase *port = NULL;
932    OMX_ERRORTYPE ret;
933
934    if (hComponent != handle)
935        return OMX_ErrorBadParameter;
936
937    if (!pBuffer)
938        return OMX_ErrorBadParameter;
939
940    if (ports)
941        if (nPortIndex < nr_ports)
942            port = ports[nPortIndex];
943
944    if (!port)
945        return OMX_ErrorBadParameter;
946
947    ProcessorPreFreeBuffer(nPortIndex, pBuffer);
948
949    return port->FreeBuffer(nPortIndex, pBuffer);
950}
951
952OMX_ERRORTYPE ComponentBase::EmptyThisBuffer(
953    OMX_IN  OMX_HANDLETYPE hComponent,
954    OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer)
955{
956    ComponentBase *cbase;
957
958    if (!hComponent)
959        return OMX_ErrorBadParameter;
960
961    cbase = static_cast<ComponentBase *>
962        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
963    if (!cbase)
964        return OMX_ErrorBadParameter;
965
966    return cbase->CBaseEmptyThisBuffer(hComponent, pBuffer);
967}
968
969OMX_ERRORTYPE ComponentBase::CBaseEmptyThisBuffer(
970    OMX_IN  OMX_HANDLETYPE hComponent,
971    OMX_IN  OMX_BUFFERHEADERTYPE *pBuffer)
972{
973    PortBase *port = NULL;
974    OMX_U32 port_index;
975    OMX_ERRORTYPE ret;
976
977    if ((hComponent != handle) || !pBuffer)
978        return OMX_ErrorBadParameter;
979
980    ret = CheckTypeHeader(pBuffer, sizeof(OMX_BUFFERHEADERTYPE));
981    if (ret != OMX_ErrorNone)
982        return ret;
983
984    port_index = pBuffer->nInputPortIndex;
985    if (port_index == (OMX_U32)-1)
986        return OMX_ErrorBadParameter;
987
988    if (ports)
989        if (port_index < nr_ports)
990            port = ports[port_index];
991
992    if (!port)
993        return OMX_ErrorBadParameter;
994
995    if (pBuffer->pInputPortPrivate != port)
996        return OMX_ErrorBadParameter;
997
998    if (port->IsEnabled()) {
999        if (state != OMX_StateIdle && state != OMX_StateExecuting &&
1000            state != OMX_StatePause)
1001            return OMX_ErrorIncorrectStateOperation;
1002    }
1003
1004    if (!pBuffer->hMarkTargetComponent) {
1005        OMX_MARKTYPE *mark;
1006
1007        mark = port->PopMark();
1008        if (mark) {
1009            pBuffer->hMarkTargetComponent = mark->hMarkTargetComponent;
1010            pBuffer->pMarkData = mark->pMarkData;
1011            free(mark);
1012        }
1013    }
1014
1015    ProcessorPreEmptyBuffer(pBuffer);
1016
1017    ret = port->PushThisBuffer(pBuffer);
1018    if (ret == OMX_ErrorNone)
1019        bufferwork->ScheduleWork(this);
1020
1021    return ret;
1022}
1023
1024OMX_ERRORTYPE ComponentBase::FillThisBuffer(
1025    OMX_IN  OMX_HANDLETYPE hComponent,
1026    OMX_IN  OMX_BUFFERHEADERTYPE *pBuffer)
1027{
1028    ComponentBase *cbase;
1029
1030    if (!hComponent)
1031        return OMX_ErrorBadParameter;
1032
1033    cbase = static_cast<ComponentBase *>
1034        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
1035    if (!cbase)
1036        return OMX_ErrorBadParameter;
1037
1038    return cbase->CBaseFillThisBuffer(hComponent, pBuffer);
1039}
1040
1041OMX_ERRORTYPE ComponentBase::CBaseFillThisBuffer(
1042    OMX_IN  OMX_HANDLETYPE hComponent,
1043    OMX_IN  OMX_BUFFERHEADERTYPE *pBuffer)
1044{
1045    PortBase *port = NULL;
1046    OMX_U32 port_index;
1047    OMX_ERRORTYPE ret;
1048
1049    if ((hComponent != handle) || !pBuffer)
1050        return OMX_ErrorBadParameter;
1051
1052    ret = CheckTypeHeader(pBuffer, sizeof(OMX_BUFFERHEADERTYPE));
1053    if (ret != OMX_ErrorNone)
1054        return ret;
1055
1056    port_index = pBuffer->nOutputPortIndex;
1057    if (port_index == (OMX_U32)-1)
1058        return OMX_ErrorBadParameter;
1059
1060    if (ports)
1061        if (port_index < nr_ports)
1062            port = ports[port_index];
1063
1064    if (!port)
1065        return OMX_ErrorBadParameter;
1066
1067    if (pBuffer->pOutputPortPrivate != port)
1068        return OMX_ErrorBadParameter;
1069
1070    if (port->IsEnabled()) {
1071        if (state != OMX_StateIdle && state != OMX_StateExecuting &&
1072            state != OMX_StatePause)
1073            return OMX_ErrorIncorrectStateOperation;
1074    }
1075
1076    ProcessorPreFillBuffer(pBuffer);
1077
1078    ret = port->PushThisBuffer(pBuffer);
1079    if (ret == OMX_ErrorNone)
1080        bufferwork->ScheduleWork(this);
1081
1082    return ret;
1083}
1084
1085OMX_ERRORTYPE ComponentBase::SetCallbacks(
1086    OMX_IN  OMX_HANDLETYPE hComponent,
1087    OMX_IN  OMX_CALLBACKTYPE* pCallbacks,
1088    OMX_IN  OMX_PTR pAppData)
1089{
1090    ComponentBase *cbase;
1091
1092    if (!hComponent)
1093        return OMX_ErrorBadParameter;
1094
1095    cbase = static_cast<ComponentBase *>
1096        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
1097    if (!cbase)
1098        return OMX_ErrorBadParameter;
1099
1100    return cbase->CBaseSetCallbacks(hComponent, pCallbacks, pAppData);
1101}
1102
1103OMX_ERRORTYPE ComponentBase::CBaseSetCallbacks(
1104    OMX_IN  OMX_HANDLETYPE hComponent,
1105    OMX_IN  OMX_CALLBACKTYPE *pCallbacks,
1106    OMX_IN  OMX_PTR pAppData)
1107{
1108    if (hComponent != handle)
1109        return OMX_ErrorBadParameter;
1110
1111    appdata = pAppData;
1112    callbacks = pCallbacks;
1113
1114    return OMX_ErrorNone;
1115}
1116
1117OMX_ERRORTYPE ComponentBase::ComponentRoleEnum(
1118    OMX_IN OMX_HANDLETYPE hComponent,
1119    OMX_OUT OMX_U8 *cRole,
1120    OMX_IN OMX_U32 nIndex)
1121{
1122    ComponentBase *cbase;
1123
1124    if (!hComponent)
1125        return OMX_ErrorBadParameter;
1126
1127    cbase = static_cast<ComponentBase *>
1128        (((OMX_COMPONENTTYPE *)hComponent)->pComponentPrivate);
1129    if (!cbase)
1130        return OMX_ErrorBadParameter;
1131
1132    return cbase->CBaseComponentRoleEnum(hComponent, cRole, nIndex);
1133}
1134
1135OMX_ERRORTYPE ComponentBase::CBaseComponentRoleEnum(
1136    OMX_IN OMX_HANDLETYPE hComponent,
1137    OMX_OUT OMX_U8 *cRole,
1138    OMX_IN OMX_U32 nIndex)
1139{
1140    if (hComponent != (OMX_HANDLETYPE *)this->handle)
1141        return OMX_ErrorBadParameter;
1142
1143    if (nIndex >= nr_roles)
1144        return OMX_ErrorBadParameter;
1145
1146    strncpy((char *)cRole, (const char *)roles[nIndex],
1147            OMX_MAX_STRINGNAME_SIZE);
1148    return OMX_ErrorNone;
1149}
1150
1151/* implement CmdHandlerInterface */
1152static const char *cmd_name[OMX_CommandMarkBuffer+2] = {
1153    "OMX_CommandStateSet",
1154    "OMX_CommandFlush",
1155    "OMX_CommandPortDisable",
1156    "OMX_CommandPortEnable",
1157    "OMX_CommandMarkBuffer",
1158    "Unknown Command",
1159};
1160
1161static inline const char *GetCmdName(OMX_COMMANDTYPE cmd)
1162{
1163    if (cmd > OMX_CommandMarkBuffer)
1164        cmd = (OMX_COMMANDTYPE)(OMX_CommandMarkBuffer+1);
1165
1166    return cmd_name[cmd];
1167}
1168
1169void ComponentBase::CmdHandler(struct cmd_s *cmd)
1170{
1171    LOGV("%s:%s: handling %s command\n",
1172         GetName(), GetWorkingRole(), GetCmdName(cmd->cmd));
1173
1174    switch (cmd->cmd) {
1175    case OMX_CommandStateSet: {
1176        OMX_STATETYPE transition = (OMX_STATETYPE)cmd->param1;
1177
1178        pthread_mutex_lock(&state_block);
1179        TransState(transition);
1180        pthread_mutex_unlock(&state_block);
1181        break;
1182    }
1183    case OMX_CommandFlush: {
1184        OMX_U32 port_index = cmd->param1;
1185        pthread_mutex_lock(&ports_block);
1186        ProcessorFlush(port_index);
1187        FlushPort(port_index, 1);
1188        pthread_mutex_unlock(&ports_block);
1189        break;
1190    }
1191    case OMX_CommandPortDisable: {
1192        OMX_U32 port_index = cmd->param1;
1193
1194        TransStatePort(port_index, PortBase::OMX_PortDisabled);
1195        break;
1196    }
1197    case OMX_CommandPortEnable: {
1198        OMX_U32 port_index = cmd->param1;
1199
1200        TransStatePort(port_index, PortBase::OMX_PortEnabled);
1201        break;
1202    }
1203    case OMX_CommandMarkBuffer: {
1204        OMX_U32 port_index = (OMX_U32)cmd->param1;
1205        OMX_MARKTYPE *mark = (OMX_MARKTYPE *)cmd->cmddata;
1206
1207        PushThisMark(port_index, mark);
1208        break;
1209    }
1210    default:
1211        LOGE("%s:%s:%s: exit failure, command %d cannot be handled\n",
1212             GetName(), GetWorkingRole(), GetCmdName(cmd->cmd), cmd->cmd);
1213        break;
1214    } /* switch */
1215
1216    LOGV("%s:%s: command %s handling done\n",
1217         GetName(), GetWorkingRole(), GetCmdName(cmd->cmd));
1218}
1219
1220/*
1221 * SendCommand:OMX_CommandStateSet
1222 * called in CmdHandler or called in other parts of component for reporting
1223 * internal error (OMX_StateInvalid).
1224 */
1225/*
1226 * Todo
1227 *   Resource Management (OMX_StateWaitForResources)
1228 *   for now, we never notify OMX_ErrorInsufficientResources,
1229 *   so IL client doesn't try to set component' state OMX_StateWaitForResources
1230 */
1231static const char *state_name[OMX_StateWaitForResources+2] = {
1232    "OMX_StateInvalid",
1233    "OMX_StateLoaded",
1234    "OMX_StateIdle",
1235    "OMX_StateExecuting",
1236    "OMX_StatePause",
1237    "OMX_StateWaitForResources",
1238    "Unknown State",
1239};
1240
1241static inline const char *GetStateName(OMX_STATETYPE state)
1242{
1243    if (state > OMX_StateWaitForResources)
1244        state = (OMX_STATETYPE)(OMX_StateWaitForResources+1);
1245
1246    return state_name[state];
1247}
1248
1249void ComponentBase::TransState(OMX_STATETYPE transition)
1250{
1251    OMX_STATETYPE current = this->state;
1252    OMX_EVENTTYPE event;
1253    OMX_U32 data1, data2;
1254    OMX_ERRORTYPE ret;
1255
1256    LOGV("%s:%s: try to transit state from %s to %s\n",
1257         GetName(), GetWorkingRole(), GetStateName(current),
1258         GetStateName(transition));
1259
1260    /* same state */
1261    if (current == transition) {
1262        ret = OMX_ErrorSameState;
1263        LOGE("%s:%s: exit failure, same state (%s)\n",
1264             GetName(), GetWorkingRole(), GetStateName(current));
1265        goto notify_event;
1266    }
1267
1268    /* invalid state */
1269    if (current == OMX_StateInvalid) {
1270        ret = OMX_ErrorInvalidState;
1271        LOGE("%s:%s: exit failure, current state is OMX_StateInvalid\n",
1272             GetName(), GetWorkingRole());
1273        goto notify_event;
1274    }
1275
1276    if (transition == OMX_StateLoaded)
1277        ret = TransStateToLoaded(current);
1278    else if (transition == OMX_StateIdle)
1279        ret = TransStateToIdle(current);
1280    else if (transition == OMX_StateExecuting)
1281        ret = TransStateToExecuting(current);
1282    else if (transition == OMX_StatePause)
1283        ret = TransStateToPause(current);
1284    else if (transition == OMX_StateInvalid)
1285        ret = TransStateToInvalid(current);
1286    else if (transition == OMX_StateWaitForResources)
1287        ret = TransStateToWaitForResources(current);
1288    else
1289        ret = OMX_ErrorIncorrectStateTransition;
1290
1291notify_event:
1292    if (ret == OMX_ErrorNone) {
1293        event = OMX_EventCmdComplete;
1294        data1 = OMX_CommandStateSet;
1295        data2 = transition;
1296
1297        state = transition;
1298        LOGD("%s:%s: transition from %s to %s completed",
1299             GetName(), GetWorkingRole(),
1300             GetStateName(current), GetStateName(transition));
1301    }
1302    else {
1303        event = OMX_EventError;
1304        data1 = ret;
1305        data2 = 0;
1306
1307        if (transition == OMX_StateInvalid || ret == OMX_ErrorInvalidState) {
1308            state = OMX_StateInvalid;
1309            LOGE("%s:%s: exit failure, transition from %s to %s, "
1310                 "current state is %s\n",
1311                 GetName(), GetWorkingRole(), GetStateName(current),
1312                 GetStateName(transition), GetStateName(state));
1313        }
1314    }
1315
1316    callbacks->EventHandler(handle, appdata, event, data1, data2, NULL);
1317
1318    /* WaitForResources workaround */
1319    if (ret == OMX_ErrorNone && transition == OMX_StateWaitForResources)
1320        callbacks->EventHandler(handle, appdata,
1321                                OMX_EventResourcesAcquired, 0, 0, NULL);
1322}
1323
1324inline OMX_ERRORTYPE ComponentBase::TransStateToLoaded(OMX_STATETYPE current)
1325{
1326    OMX_ERRORTYPE ret;
1327
1328    if (current == OMX_StateIdle) {
1329        OMX_U32 i;
1330
1331        for (i = 0; i < nr_ports; i++)
1332	{
1333            if (ports[i]->GetPortBufferCount() > 0) {
1334                ports[i]->WaitPortBufferCompletion();
1335	    };
1336	};
1337
1338        ret = ProcessorDeinit();
1339        if (ret != OMX_ErrorNone) {
1340            LOGE("%s:%s: ProcessorDeinit() failed "
1341                 "(ret : 0x%08x)\n", GetName(), GetWorkingRole(),
1342                 ret);
1343            goto out;
1344        }
1345    }
1346    else if (current == OMX_StateWaitForResources) {
1347        LOGV("%s:%s: "
1348             "state transition's requested from WaitForResources to Loaded\n",
1349             GetName(), GetWorkingRole());
1350
1351        /*
1352         * from WaitForResources to Loaded considered from Loaded to Loaded.
1353         * do nothing
1354         */
1355
1356        ret = OMX_ErrorNone;
1357    }
1358    else
1359        ret = OMX_ErrorIncorrectStateTransition;
1360
1361out:
1362    return ret;
1363}
1364
1365inline OMX_ERRORTYPE ComponentBase::TransStateToIdle(OMX_STATETYPE current)
1366{
1367    OMX_ERRORTYPE ret;
1368
1369    if (current == OMX_StateLoaded) {
1370        OMX_U32 i;
1371        for (i = 0; i < nr_ports; i++) {
1372             if (ports[i]->IsEnabled())
1373                 ports[i]->WaitPortBufferCompletion();
1374        }
1375
1376        ret = ProcessorInit();
1377        if (ret != OMX_ErrorNone) {
1378            LOGE("%s:%s: ProcessorInit() failed (ret : 0x%08x)\n",
1379                 GetName(), GetWorkingRole(), ret);
1380            goto out;
1381        }
1382    }
1383    else if ((current == OMX_StatePause) || (current == OMX_StateExecuting)) {
1384        pthread_mutex_lock(&ports_block);
1385        FlushPort(OMX_ALL, 0);
1386        pthread_mutex_unlock(&ports_block);
1387        LOGV("%s:%s: flushed all ports\n", GetName(), GetWorkingRole());
1388
1389        bufferwork->CancelScheduledWork(this);
1390        LOGV("%s:%s: discarded all scheduled buffer process work\n",
1391             GetName(), GetWorkingRole());
1392
1393        if (current == OMX_StatePause) {
1394            bufferwork->ResumeWork();
1395            LOGV("%s:%s: buffer process work resumed\n",
1396                 GetName(), GetWorkingRole());
1397        }
1398
1399        bufferwork->StopWork();
1400        LOGV("%s:%s: buffer process work stopped\n",
1401             GetName(), GetWorkingRole());
1402
1403        ret = ProcessorStop();
1404        if (ret != OMX_ErrorNone) {
1405            LOGE("%s:%s: ProcessorStop() failed (ret : 0x%08x)\n",
1406                 GetName(), GetWorkingRole(), ret);
1407            goto out;
1408        }
1409    }
1410    else if (current == OMX_StateWaitForResources) {
1411        LOGV("%s:%s: "
1412             "state transition's requested from WaitForResources to Idle\n",
1413             GetName(), GetWorkingRole());
1414
1415        /* same as Loaded to Idle BUT DO NOTHING for now */
1416
1417        ret = OMX_ErrorNone;
1418    }
1419    else
1420        ret = OMX_ErrorIncorrectStateTransition;
1421
1422out:
1423    return ret;
1424}
1425
1426inline OMX_ERRORTYPE
1427ComponentBase::TransStateToExecuting(OMX_STATETYPE current)
1428{
1429    OMX_ERRORTYPE ret;
1430
1431    if (current == OMX_StateIdle) {
1432        bufferwork->StartWork(true);
1433        LOGV("%s:%s: buffer process work started with executing state\n",
1434             GetName(), GetWorkingRole());
1435
1436        ret = ProcessorStart();
1437        if (ret != OMX_ErrorNone) {
1438            LOGE("%s:%s: ProcessorStart() failed (ret : 0x%08x)\n",
1439                 GetName(), GetWorkingRole(), ret);
1440            goto out;
1441        }
1442    }
1443    else if (current == OMX_StatePause) {
1444        bufferwork->ResumeWork();
1445        LOGV("%s:%s: buffer process work resumed\n",
1446             GetName(), GetWorkingRole());
1447
1448        ret = ProcessorResume();
1449        if (ret != OMX_ErrorNone) {
1450            LOGE("%s:%s: ProcessorResume() failed (ret : 0x%08x)\n",
1451                 GetName(), GetWorkingRole(), ret);
1452            goto out;
1453        }
1454    }
1455    else
1456        ret = OMX_ErrorIncorrectStateTransition;
1457
1458out:
1459    return ret;
1460}
1461
1462inline OMX_ERRORTYPE ComponentBase::TransStateToPause(OMX_STATETYPE current)
1463{
1464    OMX_ERRORTYPE ret;
1465
1466    if (current == OMX_StateIdle) {
1467        bufferwork->StartWork(false);
1468        LOGV("%s:%s: buffer process work started with paused state\n",
1469             GetName(), GetWorkingRole());
1470
1471        ret = ProcessorStart();
1472        if (ret != OMX_ErrorNone) {
1473            LOGE("%s:%s: ProcessorSart() failed (ret : 0x%08x)\n",
1474                 GetName(), GetWorkingRole(), ret);
1475            goto out;
1476        }
1477    }
1478    else if (current == OMX_StateExecuting) {
1479        bufferwork->PauseWork();
1480        LOGV("%s:%s: buffer process work paused\n",
1481             GetName(), GetWorkingRole());
1482
1483        ret = ProcessorPause();
1484        if (ret != OMX_ErrorNone) {
1485            LOGE("%s:%s: ProcessorPause() failed (ret : 0x%08x)\n",
1486                 GetName(), GetWorkingRole(), ret);
1487            goto out;
1488        }
1489    }
1490    else
1491        ret = OMX_ErrorIncorrectStateTransition;
1492
1493out:
1494    return ret;
1495}
1496
1497inline OMX_ERRORTYPE ComponentBase::TransStateToInvalid(OMX_STATETYPE current)
1498{
1499    OMX_ERRORTYPE ret = OMX_ErrorInvalidState;
1500
1501    /*
1502     * Todo
1503     *   graceful escape
1504     */
1505
1506    return ret;
1507}
1508
1509inline OMX_ERRORTYPE
1510ComponentBase::TransStateToWaitForResources(OMX_STATETYPE current)
1511{
1512    OMX_ERRORTYPE ret;
1513
1514    if (current == OMX_StateLoaded) {
1515        LOGV("%s:%s: "
1516             "state transition's requested from Loaded to WaitForResources\n",
1517             GetName(), GetWorkingRole());
1518        ret = OMX_ErrorNone;
1519    }
1520    else
1521        ret = OMX_ErrorIncorrectStateTransition;
1522
1523    return ret;
1524}
1525
1526/* mark buffer */
1527void ComponentBase::PushThisMark(OMX_U32 port_index, OMX_MARKTYPE *mark)
1528{
1529    PortBase *port = NULL;
1530    OMX_EVENTTYPE event;
1531    OMX_U32 data1, data2;
1532    OMX_ERRORTYPE ret;
1533
1534    if (ports)
1535        if (port_index < nr_ports)
1536            port = ports[port_index];
1537
1538    if (!port) {
1539        ret = OMX_ErrorBadPortIndex;
1540        goto notify_event;
1541    }
1542
1543    ret = port->PushMark(mark);
1544    if (ret != OMX_ErrorNone) {
1545        /* don't report OMX_ErrorInsufficientResources */
1546        ret = OMX_ErrorUndefined;
1547        goto notify_event;
1548    }
1549
1550notify_event:
1551    if (ret == OMX_ErrorNone) {
1552        event = OMX_EventCmdComplete;
1553        data1 = OMX_CommandMarkBuffer;
1554        data2 = port_index;
1555    }
1556    else {
1557        event = OMX_EventError;
1558        data1 = ret;
1559        data2 = 0;
1560    }
1561
1562    callbacks->EventHandler(handle, appdata, event, data1, data2, NULL);
1563}
1564
1565void ComponentBase::FlushPort(OMX_U32 port_index, bool notify)
1566{
1567    OMX_U32 i, from_index, to_index;
1568
1569    if ((port_index != OMX_ALL) && (port_index > nr_ports-1))
1570        return;
1571
1572    if (port_index == OMX_ALL) {
1573        from_index = 0;
1574        to_index = nr_ports - 1;
1575    }
1576    else {
1577        from_index = port_index;
1578        to_index = port_index;
1579    }
1580
1581    LOGV("%s:%s: flush ports (from index %lu to %lu)\n",
1582         GetName(), GetWorkingRole(), from_index, to_index);
1583
1584    for (i = from_index; i <= to_index; i++) {
1585        ports[i]->FlushPort();
1586        if (notify)
1587            callbacks->EventHandler(handle, appdata, OMX_EventCmdComplete,
1588                                    OMX_CommandFlush, i, NULL);
1589    }
1590
1591    LOGV("%s:%s: flush ports done\n", GetName(), GetWorkingRole());
1592}
1593
1594extern const char *GetPortStateName(OMX_U8 state); //portbase.cpp
1595
1596void ComponentBase::TransStatePort(OMX_U32 port_index, OMX_U8 state)
1597{
1598    OMX_EVENTTYPE event;
1599    OMX_U32 data1, data2;
1600    OMX_U32 i, from_index, to_index;
1601    OMX_ERRORTYPE ret;
1602
1603    if ((port_index != OMX_ALL) && (port_index > nr_ports-1))
1604        return;
1605
1606    if (port_index == OMX_ALL) {
1607        from_index = 0;
1608        to_index = nr_ports - 1;
1609    }
1610    else {
1611        from_index = port_index;
1612        to_index = port_index;
1613    }
1614
1615    LOGV("%s:%s: transit ports state to %s (from index %lu to %lu)\n",
1616         GetName(), GetWorkingRole(), GetPortStateName(state),
1617         from_index, to_index);
1618
1619    pthread_mutex_lock(&ports_block);
1620    for (i = from_index; i <= to_index; i++) {
1621        ret = ports[i]->TransState(state);
1622        if (ret == OMX_ErrorNone) {
1623            event = OMX_EventCmdComplete;
1624            if (state == PortBase::OMX_PortEnabled) {
1625                data1 = OMX_CommandPortEnable;
1626                ProcessorReset();
1627            } else {
1628                data1 = OMX_CommandPortDisable;
1629            }
1630            data2 = i;
1631        }
1632        else {
1633            event = OMX_EventError;
1634            data1 = ret;
1635            data2 = 0;
1636        }
1637        callbacks->EventHandler(handle, appdata, OMX_EventCmdComplete,
1638                                data1, data2, NULL);
1639    }
1640    pthread_mutex_unlock(&ports_block);
1641
1642    LOGV("%s:%s: transit ports state to %s completed\n",
1643         GetName(), GetWorkingRole(), GetPortStateName(state));
1644}
1645
1646/* set working role */
1647OMX_ERRORTYPE ComponentBase::SetWorkingRole(const OMX_STRING role)
1648{
1649    OMX_U32 i;
1650
1651    if (state != OMX_StateUnloaded && state != OMX_StateLoaded)
1652        return OMX_ErrorIncorrectStateOperation;
1653
1654    if (!role) {
1655        working_role = NULL;
1656        return OMX_ErrorNone;
1657    }
1658
1659    for (i = 0; i < nr_roles; i++) {
1660        if (!strcmp((char *)&roles[i][0], role)) {
1661            working_role = (OMX_STRING)&roles[i][0];
1662            return OMX_ErrorNone;
1663        }
1664    }
1665
1666    LOGE("%s: cannot find %s role\n", GetName(), role);
1667    return OMX_ErrorBadParameter;
1668}
1669
1670/* apply a working role for a component having multiple roles */
1671OMX_ERRORTYPE ComponentBase::ApplyWorkingRole(void)
1672{
1673    OMX_U32 i;
1674    OMX_ERRORTYPE ret;
1675
1676    if (state != OMX_StateUnloaded && state != OMX_StateLoaded)
1677        return OMX_ErrorIncorrectStateOperation;
1678
1679    if (!working_role)
1680        return OMX_ErrorBadParameter;
1681
1682    if (!callbacks || !appdata)
1683        return OMX_ErrorBadParameter;
1684
1685    ret = AllocatePorts();
1686    if (ret != OMX_ErrorNone) {
1687        LOGE("failed to AllocatePorts() (ret = 0x%08x)\n", ret);
1688        return ret;
1689    }
1690
1691    /* now we can access ports */
1692    for (i = 0; i < nr_ports; i++) {
1693        ports[i]->SetOwner(handle);
1694        ports[i]->SetCallbacks(handle, callbacks, appdata);
1695    }
1696
1697    LOGI("%s: set working role %s:", GetName(), GetWorkingRole());
1698    return OMX_ErrorNone;
1699}
1700
1701OMX_ERRORTYPE ComponentBase::AllocatePorts(void)
1702{
1703    OMX_DIRTYPE dir;
1704    bool has_input, has_output;
1705    OMX_U32 i;
1706    OMX_ERRORTYPE ret;
1707
1708    if (ports)
1709        return OMX_ErrorBadParameter;
1710
1711    ret = ComponentAllocatePorts();
1712    if (ret != OMX_ErrorNone) {
1713        LOGE("failed to %s::ComponentAllocatePorts(), ret = 0x%08x\n",
1714             name, ret);
1715        return ret;
1716    }
1717
1718    has_input = false;
1719    has_output = false;
1720    ret = OMX_ErrorNone;
1721    for (i = 0; i < nr_ports; i++) {
1722        dir = ports[i]->GetPortDirection();
1723        if (dir == OMX_DirInput)
1724            has_input = true;
1725        else if (dir == OMX_DirOutput)
1726            has_output = true;
1727        else {
1728            ret = OMX_ErrorUndefined;
1729            break;
1730        }
1731    }
1732    if (ret != OMX_ErrorNone)
1733        goto free_ports;
1734
1735    if ((has_input == false) && (has_output == true))
1736        cvariant = CVARIANT_SOURCE;
1737    else if ((has_input == true) && (has_output == true))
1738        cvariant = CVARIANT_FILTER;
1739    else if ((has_input == true) && (has_output == false))
1740        cvariant = CVARIANT_SINK;
1741    else
1742        goto free_ports;
1743
1744    return OMX_ErrorNone;
1745
1746free_ports:
1747    LOGE("%s(): exit, unknown component variant\n", __func__);
1748    FreePorts();
1749    return OMX_ErrorUndefined;
1750}
1751
1752/* called int FreeHandle() */
1753OMX_ERRORTYPE ComponentBase::FreePorts(void)
1754{
1755    if (ports) {
1756        OMX_U32 i, this_nr_ports = this->nr_ports;
1757
1758        for (i = 0; i < this_nr_ports; i++) {
1759            if (ports[i]) {
1760                OMX_MARKTYPE *mark;
1761                /* it should be empty before this */
1762                while ((mark = ports[i]->PopMark()))
1763                    free(mark);
1764
1765                delete ports[i];
1766                ports[i] = NULL;
1767            }
1768        }
1769        delete []ports;
1770        ports = NULL;
1771    }
1772
1773    return OMX_ErrorNone;
1774}
1775
1776/* buffer processing */
1777/* implement WorkableInterface */
1778void ComponentBase::Work(void)
1779{
1780    OMX_BUFFERHEADERTYPE **buffers[nr_ports];
1781    OMX_BUFFERHEADERTYPE *buffers_hdr[nr_ports];
1782    OMX_BUFFERHEADERTYPE *buffers_org[nr_ports];
1783    buffer_retain_t retain[nr_ports];
1784    OMX_U32 i;
1785    OMX_ERRORTYPE ret;
1786
1787    if (nr_ports == 0) {
1788        return;
1789    }
1790
1791    pthread_mutex_lock(&ports_block);
1792
1793    while(IsAllBufferAvailable())
1794    {
1795        for (i = 0; i < nr_ports; i++) {
1796            buffers_hdr[i] = ports[i]->PopBuffer();
1797            buffers[i] = &buffers_hdr[i];
1798            buffers_org[i] = buffers_hdr[i];
1799            retain[i] = BUFFER_RETAIN_NOT_RETAIN;
1800        }
1801
1802        if (working_role != NULL && !strncmp((char*)working_role, "video_decoder", 13)){
1803            ret = ProcessorProcess(buffers, &retain[0], nr_ports);
1804        }else{
1805            ret = ProcessorProcess(buffers_hdr, &retain[0], nr_ports);
1806        }
1807
1808        if (ret == OMX_ErrorNone) {
1809            PostProcessBuffers(buffers, &retain[0]);
1810
1811            for (i = 0; i < nr_ports; i++) {
1812                if(retain[i] == BUFFER_RETAIN_GETAGAIN) {
1813                    ports[i]->RetainThisBuffer(*buffers[i], false);
1814                }
1815                else if (retain[i] == BUFFER_RETAIN_ACCUMULATE) {
1816                    ports[i]->RetainThisBuffer(*buffers[i], true);
1817                }
1818                else if (retain[i] == BUFFER_RETAIN_OVERRIDDEN) {
1819                    ports[i]->RetainAndReturnBuffer(buffers_org[i], *buffers[i]);
1820                }
1821                else if (retain[i] == BUFFER_RETAIN_CACHE) {
1822                    //nothing to do
1823                } else {
1824                    ports[i]->ReturnThisBuffer(*buffers[i]);
1825                }
1826            }
1827        }
1828        else {
1829
1830            for (i = 0; i < nr_ports; i++) {
1831                /* return buffers by hands, these buffers're not in queue */
1832                ports[i]->ReturnThisBuffer(*buffers[i]);
1833                /* flush ports */
1834                ports[i]->FlushPort();
1835            }
1836
1837            callbacks->EventHandler(handle, appdata, OMX_EventError, ret,
1838                                    0, NULL);
1839        }
1840    }
1841
1842    pthread_mutex_unlock(&ports_block);
1843}
1844
1845bool ComponentBase::IsAllBufferAvailable(void)
1846{
1847    OMX_U32 i;
1848    OMX_U32 nr_avail = 0;
1849
1850    for (i = 0; i < nr_ports; i++) {
1851        OMX_U32 length = 0;
1852
1853        if (ports[i]->IsEnabled())
1854            length = ports[i]->BufferQueueLength();
1855
1856        if (length)
1857            nr_avail++;
1858    }
1859
1860    if (nr_avail == nr_ports)
1861        return true;
1862    else
1863        return false;
1864}
1865
1866inline void ComponentBase::SourcePostProcessBuffers(
1867    OMX_BUFFERHEADERTYPE ***buffers,
1868    const buffer_retain_t *retain)
1869{
1870    OMX_U32 i;
1871
1872    for (i = 0; i < nr_ports; i++) {
1873        /*
1874         * in case of source component, buffers're marked when they come
1875         * from the ouput ports
1876         */
1877        if (!(*buffers[i])->hMarkTargetComponent) {
1878            OMX_MARKTYPE *mark;
1879
1880            mark = ports[i]->PopMark();
1881            if (mark) {
1882                (*buffers[i])->hMarkTargetComponent = mark->hMarkTargetComponent;
1883                (*buffers[i])->pMarkData = mark->pMarkData;
1884                free(mark);
1885            }
1886        }
1887    }
1888}
1889
1890inline void ComponentBase::FilterPostProcessBuffers(
1891    OMX_BUFFERHEADERTYPE ***buffers,
1892    const buffer_retain_t *retain)
1893{
1894    OMX_MARKTYPE *mark;
1895    OMX_U32 i, j;
1896
1897    for (i = 0; i < nr_ports; i++) {
1898        if (ports[i]->GetPortDirection() == OMX_DirInput) {
1899            for (j = 0; j < nr_ports; j++) {
1900                if (ports[j]->GetPortDirection() != OMX_DirOutput)
1901                    continue;
1902
1903                /* propagates EOS flag */
1904                /* clear input EOS at the end of this loop */
1905                if (retain[i] != BUFFER_RETAIN_GETAGAIN) {
1906                    if ((*buffers[i])->nFlags & OMX_BUFFERFLAG_EOS)
1907                        (*buffers[j])->nFlags |= OMX_BUFFERFLAG_EOS;
1908                }
1909
1910                /* propagates marks */
1911                /*
1912                 * if hMarkTargetComponent == handle then the mark's not
1913                 * propagated
1914                 */
1915                if ((*buffers[i])->hMarkTargetComponent &&
1916                    ((*buffers[i])->hMarkTargetComponent != handle)) {
1917                    if ((*buffers[j])->hMarkTargetComponent) {
1918                        mark = (OMX_MARKTYPE *)malloc(sizeof(*mark));
1919                        if (mark) {
1920                            mark->hMarkTargetComponent =
1921                                (*buffers[i])->hMarkTargetComponent;
1922                            mark->pMarkData = (*buffers[i])->pMarkData;
1923                            ports[j]->PushMark(mark);
1924                            mark = NULL;
1925                            (*buffers[i])->hMarkTargetComponent = NULL;
1926                            (*buffers[i])->pMarkData = NULL;
1927                        }
1928                    }
1929                    else {
1930                        mark = ports[j]->PopMark();
1931                        if (mark) {
1932                            (*buffers[j])->hMarkTargetComponent =
1933                                mark->hMarkTargetComponent;
1934                            (*buffers[j])->pMarkData = mark->pMarkData;
1935                            free(mark);
1936
1937                            mark = (OMX_MARKTYPE *)malloc(sizeof(*mark));
1938                            if (mark) {
1939                                mark->hMarkTargetComponent =
1940                                    (*buffers[i])->hMarkTargetComponent;
1941                                mark->pMarkData = (*buffers[i])->pMarkData;
1942                                ports[j]->PushMark(mark);
1943                                mark = NULL;
1944                                (*buffers[i])->hMarkTargetComponent = NULL;
1945                                (*buffers[i])->pMarkData = NULL;
1946                            }
1947                        }
1948                        else {
1949                            (*buffers[j])->hMarkTargetComponent =
1950                                (*buffers[i])->hMarkTargetComponent;
1951                            (*buffers[j])->pMarkData = (*buffers[i])->pMarkData;
1952                            (*buffers[i])->hMarkTargetComponent = NULL;
1953                            (*buffers[i])->pMarkData = NULL;
1954                        }
1955                    }
1956                }
1957            }
1958            /* clear input buffer's EOS */
1959            if (retain[i] != BUFFER_RETAIN_GETAGAIN)
1960                (*buffers[i])->nFlags &= ~OMX_BUFFERFLAG_EOS;
1961        }
1962    }
1963}
1964
1965inline void ComponentBase::SinkPostProcessBuffers(
1966    OMX_BUFFERHEADERTYPE ***buffers,
1967    const buffer_retain_t *retain)
1968{
1969    return;
1970}
1971
1972void ComponentBase::PostProcessBuffers(OMX_BUFFERHEADERTYPE ***buffers,
1973                                       const buffer_retain_t *retain)
1974{
1975
1976    if (cvariant == CVARIANT_SOURCE)
1977        SourcePostProcessBuffers(buffers, retain);
1978    else if (cvariant == CVARIANT_FILTER)
1979        FilterPostProcessBuffers(buffers, retain);
1980    else if (cvariant == CVARIANT_SINK) {
1981        SinkPostProcessBuffers(buffers, retain);
1982    }
1983    else {
1984        LOGE("%s(): fatal error unknown component variant (%d)\n",
1985             __func__, cvariant);
1986    }
1987}
1988
1989/* processor default callbacks */
1990OMX_ERRORTYPE ComponentBase::ProcessorInit(void)
1991{
1992    return OMX_ErrorNone;
1993}
1994OMX_ERRORTYPE ComponentBase::ProcessorDeinit(void)
1995{
1996    return OMX_ErrorNone;
1997}
1998
1999OMX_ERRORTYPE ComponentBase::ProcessorStart(void)
2000{
2001    return OMX_ErrorNone;
2002}
2003
2004OMX_ERRORTYPE ComponentBase::ProcessorReset(void)
2005{
2006    return OMX_ErrorNone;
2007}
2008
2009
2010OMX_ERRORTYPE ComponentBase::ProcessorStop(void)
2011{
2012    return OMX_ErrorNone;
2013}
2014
2015OMX_ERRORTYPE ComponentBase::ProcessorPause(void)
2016{
2017    return OMX_ErrorNone;
2018}
2019
2020OMX_ERRORTYPE ComponentBase::ProcessorResume(void)
2021{
2022    return OMX_ErrorNone;
2023}
2024
2025OMX_ERRORTYPE ComponentBase::ProcessorFlush(OMX_U32 port_index)
2026{
2027    return OMX_ErrorNone;
2028}
2029
2030OMX_ERRORTYPE ComponentBase::ProcessorPreFillBuffer(OMX_BUFFERHEADERTYPE* buffer)
2031{
2032    return OMX_ErrorNone;
2033}
2034
2035OMX_ERRORTYPE ComponentBase::ProcessorPreEmptyBuffer(OMX_BUFFERHEADERTYPE* buffer)
2036{
2037    return OMX_ErrorNone;
2038}
2039
2040OMX_ERRORTYPE ComponentBase::ProcessorProcess(OMX_BUFFERHEADERTYPE **pBuffers,
2041                                           buffer_retain_t *retain,
2042                                           OMX_U32 nr_buffers)
2043{
2044    LOGE("ProcessorProcess not be implemented");
2045    return OMX_ErrorNotImplemented;
2046}
2047OMX_ERRORTYPE ComponentBase::ProcessorProcess(OMX_BUFFERHEADERTYPE ***pBuffers,
2048                                           buffer_retain_t *retain,
2049                                           OMX_U32 nr_buffers)
2050{
2051    LOGE("ProcessorProcess not be implemented");
2052    return OMX_ErrorNotImplemented;
2053}
2054
2055OMX_ERRORTYPE ComponentBase::ProcessorPreFreeBuffer(OMX_U32 nPortIndex, OMX_BUFFERHEADERTYPE* pBuffer)
2056{
2057    return OMX_ErrorNone;
2058
2059}
2060/* end of processor callbacks */
2061
2062/* helper for derived class */
2063const OMX_STRING ComponentBase::GetWorkingRole(void)
2064{
2065    return &working_role[0];
2066}
2067
2068const OMX_COMPONENTTYPE *ComponentBase::GetComponentHandle(void)
2069{
2070    return handle;
2071}
2072#if 0
2073void ComponentBase::DumpBuffer(const OMX_BUFFERHEADERTYPE *bufferheader,
2074                               bool dumpdata)
2075{
2076    OMX_U8 *pbuffer = bufferheader->pBuffer, *p;
2077    OMX_U32 offset = bufferheader->nOffset;
2078    OMX_U32 alloc_len = bufferheader->nAllocLen;
2079    OMX_U32 filled_len =  bufferheader->nFilledLen;
2080    OMX_U32 left = filled_len, oneline;
2081    OMX_U32 index = 0, i;
2082    /* 0x%04lx:  %02x %02x .. (n = 16)\n\0 */
2083    char prbuffer[8 + 3 * 0x10 + 2], *pp;
2084    OMX_U32 prbuffer_len;
2085
2086    LOGD("Component %s DumpBuffer\n", name);
2087    LOGD("%s port index = %lu",
2088         (bufferheader->nInputPortIndex != 0x7fffffff) ? "input" : "output",
2089         (bufferheader->nInputPortIndex != 0x7fffffff) ?
2090         bufferheader->nInputPortIndex : bufferheader->nOutputPortIndex);
2091    LOGD("nAllocLen = %lu, nOffset = %lu, nFilledLen = %lu\n",
2092         alloc_len, offset, filled_len);
2093    LOGD("nTimeStamp = %lld, nTickCount = %lu",
2094         bufferheader->nTimeStamp,
2095         bufferheader->nTickCount);
2096    LOGD("nFlags = 0x%08lx\n", bufferheader->nFlags);
2097    LOGD("hMarkTargetComponent = %p, pMarkData = %p\n",
2098         bufferheader->hMarkTargetComponent, bufferheader->pMarkData);
2099
2100    if (!pbuffer || !alloc_len || !filled_len)
2101        return;
2102
2103    if (offset + filled_len > alloc_len)
2104        return;
2105
2106    if (!dumpdata)
2107        return;
2108
2109    p = pbuffer + offset;
2110    while (left) {
2111        oneline = left > 0x10 ? 0x10 : left; /* 16 items per 1 line */
2112        pp += sprintf(pp, "0x%04lx: ", index);
2113        for (i = 0; i < oneline; i++)
2114            pp += sprintf(pp, " %02x", *(p + i));
2115        pp += sprintf(pp, "\n");
2116        *pp = '\0';
2117
2118        index += 0x10;
2119        p += oneline;
2120        left -= oneline;
2121
2122        pp = &prbuffer[0];
2123        LOGD("%s", pp);
2124    }
2125}
2126#endif
2127/* end of component methods & helpers */
2128
2129/*
2130 * omx header manipuation
2131 */
2132void ComponentBase::SetTypeHeader(OMX_PTR type, OMX_U32 size)
2133{
2134    OMX_U32 *nsize;
2135    OMX_VERSIONTYPE *nversion;
2136
2137    if (!type)
2138        return;
2139
2140    nsize = (OMX_U32 *)type;
2141    nversion = (OMX_VERSIONTYPE *)((OMX_U8 *)type + sizeof(OMX_U32));
2142
2143    *nsize = size;
2144    nversion->nVersion = OMX_SPEC_VERSION;
2145}
2146
2147OMX_ERRORTYPE ComponentBase::CheckTypeHeader(const OMX_PTR type, OMX_U32 size)
2148{
2149    OMX_U32 *nsize;
2150    OMX_VERSIONTYPE *nversion;
2151
2152    if (!type)
2153        return OMX_ErrorBadParameter;
2154
2155    nsize = (OMX_U32 *)type;
2156    nversion = (OMX_VERSIONTYPE *)((OMX_U8 *)type + sizeof(OMX_U32));
2157
2158    if (*nsize != size)
2159        return OMX_ErrorBadParameter;
2160
2161    if (nversion->nVersion != OMX_SPEC_VERSION)
2162        return OMX_ErrorVersionMismatch;
2163
2164    return OMX_ErrorNone;
2165}
2166
2167/* end of ComponentBase */
2168