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