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