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