componentbase.cpp revision a4315bcc3603ce74c2faeb4b81563dd3396cbd64
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    pthread_mutex_lock(&ports_block);
1860
1861    while(IsAllBufferAvailable())
1862    {
1863        for (i = 0; i < nr_ports; i++) {
1864            buffers_hdr[i] = ports[i]->PopBuffer();
1865            buffers[i] = &buffers_hdr[i];
1866            buffers_org[i] = buffers_hdr[i];
1867            retain[i] = BUFFER_RETAIN_NOT_RETAIN;
1868        }
1869
1870        if (working_role != NULL && !strncmp((char*)working_role, "video_decoder", 13)){
1871            ret = ProcessorProcess(buffers, &retain[0], nr_ports);
1872        }else{
1873            ret = ProcessorProcess(buffers_hdr, &retain[0], nr_ports);
1874        }
1875
1876        if (ret == OMX_ErrorNone) {
1877            if (!working_role || (strncmp((char*)working_role, "video_encoder", 13) != 0))
1878                PostProcessBuffers(buffers, &retain[0]);
1879
1880            for (i = 0; i < nr_ports; i++) {
1881                if (*buffers[i] == NULL)
1882                    continue;
1883
1884                if(retain[i] == BUFFER_RETAIN_GETAGAIN) {
1885                    ports[i]->RetainThisBuffer(*buffers[i], false);
1886                }
1887                else if (retain[i] == BUFFER_RETAIN_ACCUMULATE) {
1888                    ports[i]->RetainThisBuffer(*buffers[i], true);
1889                }
1890                else if (retain[i] == BUFFER_RETAIN_OVERRIDDEN) {
1891                    ports[i]->RetainAndReturnBuffer(buffers_org[i], *buffers[i]);
1892                }
1893                else if (retain[i] == BUFFER_RETAIN_CACHE) {
1894                    //nothing to do
1895                } else {
1896                    ports[i]->ReturnThisBuffer(*buffers[i]);
1897                }
1898            }
1899        }
1900        else {
1901
1902            for (i = 0; i < nr_ports; i++) {
1903                /* return buffers by hands, these buffers're not in queue */
1904                ports[i]->ReturnThisBuffer(*buffers[i]);
1905                /* flush ports */
1906                ports[i]->FlushPort();
1907            }
1908
1909            callbacks->EventHandler(handle, appdata, OMX_EventError, ret,
1910                                    0, NULL);
1911        }
1912    }
1913
1914    pthread_mutex_unlock(&ports_block);
1915}
1916
1917bool ComponentBase::IsAllBufferAvailable(void)
1918{
1919    OMX_U32 i;
1920    OMX_U32 nr_avail = 0;
1921
1922    for (i = 0; i < nr_ports; i++) {
1923        OMX_U32 length = 0;
1924
1925        if (ports[i]->IsEnabled()) {
1926            length += ports[i]->BufferQueueLength();
1927            length += ports[i]->RetainedBufferQueueLength();
1928        }
1929
1930        if (length)
1931            nr_avail++;
1932    }
1933
1934    if (nr_avail == nr_ports)
1935        return true;
1936    else
1937        return false;
1938}
1939
1940inline void ComponentBase::SourcePostProcessBuffers(
1941    OMX_BUFFERHEADERTYPE ***buffers,
1942    const buffer_retain_t *retain)
1943{
1944    OMX_U32 i;
1945
1946    for (i = 0; i < nr_ports; i++) {
1947        /*
1948         * in case of source component, buffers're marked when they come
1949         * from the ouput ports
1950         */
1951        if (!(*buffers[i])->hMarkTargetComponent) {
1952            OMX_MARKTYPE *mark;
1953
1954            mark = ports[i]->PopMark();
1955            if (mark) {
1956                (*buffers[i])->hMarkTargetComponent = mark->hMarkTargetComponent;
1957                (*buffers[i])->pMarkData = mark->pMarkData;
1958                free(mark);
1959            }
1960        }
1961    }
1962}
1963
1964inline void ComponentBase::FilterPostProcessBuffers(
1965    OMX_BUFFERHEADERTYPE ***buffers,
1966    const buffer_retain_t *retain)
1967{
1968    OMX_MARKTYPE *mark;
1969    OMX_U32 i, j;
1970
1971    for (i = 0; i < nr_ports; i++) {
1972        if (ports[i]->GetPortDirection() == OMX_DirInput) {
1973            for (j = 0; j < nr_ports; j++) {
1974                if (ports[j]->GetPortDirection() != OMX_DirOutput)
1975                    continue;
1976
1977                /* propagates EOS flag */
1978                /* clear input EOS at the end of this loop */
1979                if (retain[i] != BUFFER_RETAIN_GETAGAIN) {
1980                    if ((*buffers[i])->nFlags & OMX_BUFFERFLAG_EOS)
1981                        (*buffers[j])->nFlags |= OMX_BUFFERFLAG_EOS;
1982                }
1983
1984                /* propagates marks */
1985                /*
1986                 * if hMarkTargetComponent == handle then the mark's not
1987                 * propagated
1988                 */
1989                if ((*buffers[i])->hMarkTargetComponent &&
1990                    ((*buffers[i])->hMarkTargetComponent != handle)) {
1991                    if ((*buffers[j])->hMarkTargetComponent) {
1992                        mark = (OMX_MARKTYPE *)malloc(sizeof(*mark));
1993                        if (mark) {
1994                            mark->hMarkTargetComponent =
1995                                (*buffers[i])->hMarkTargetComponent;
1996                            mark->pMarkData = (*buffers[i])->pMarkData;
1997                            ports[j]->PushMark(mark);
1998                            mark = NULL;
1999                            (*buffers[i])->hMarkTargetComponent = NULL;
2000                            (*buffers[i])->pMarkData = NULL;
2001                        }
2002                    }
2003                    else {
2004                        mark = ports[j]->PopMark();
2005                        if (mark) {
2006                            (*buffers[j])->hMarkTargetComponent =
2007                                mark->hMarkTargetComponent;
2008                            (*buffers[j])->pMarkData = mark->pMarkData;
2009                            free(mark);
2010
2011                            mark = (OMX_MARKTYPE *)malloc(sizeof(*mark));
2012                            if (mark) {
2013                                mark->hMarkTargetComponent =
2014                                    (*buffers[i])->hMarkTargetComponent;
2015                                mark->pMarkData = (*buffers[i])->pMarkData;
2016                                ports[j]->PushMark(mark);
2017                                mark = NULL;
2018                                (*buffers[i])->hMarkTargetComponent = NULL;
2019                                (*buffers[i])->pMarkData = NULL;
2020                            }
2021                        }
2022                        else {
2023                            (*buffers[j])->hMarkTargetComponent =
2024                                (*buffers[i])->hMarkTargetComponent;
2025                            (*buffers[j])->pMarkData = (*buffers[i])->pMarkData;
2026                            (*buffers[i])->hMarkTargetComponent = NULL;
2027                            (*buffers[i])->pMarkData = NULL;
2028                        }
2029                    }
2030                }
2031            }
2032            /* clear input buffer's EOS */
2033            if (retain[i] != BUFFER_RETAIN_GETAGAIN)
2034                (*buffers[i])->nFlags &= ~OMX_BUFFERFLAG_EOS;
2035        }
2036    }
2037}
2038
2039inline void ComponentBase::SinkPostProcessBuffers(
2040    OMX_BUFFERHEADERTYPE ***buffers,
2041    const buffer_retain_t *retain)
2042{
2043    return;
2044}
2045
2046void ComponentBase::PostProcessBuffers(OMX_BUFFERHEADERTYPE ***buffers,
2047                                       const buffer_retain_t *retain)
2048{
2049
2050    if (cvariant == CVARIANT_SOURCE)
2051        SourcePostProcessBuffers(buffers, retain);
2052    else if (cvariant == CVARIANT_FILTER)
2053        FilterPostProcessBuffers(buffers, retain);
2054    else if (cvariant == CVARIANT_SINK) {
2055        SinkPostProcessBuffers(buffers, retain);
2056    }
2057    else {
2058        LOGE("%s(): fatal error unknown component variant (%d)\n",
2059             __func__, cvariant);
2060    }
2061}
2062
2063/* processor default callbacks */
2064OMX_ERRORTYPE ComponentBase::ProcessorInit(void)
2065{
2066    return OMX_ErrorNone;
2067}
2068OMX_ERRORTYPE ComponentBase::ProcessorDeinit(void)
2069{
2070    return OMX_ErrorNone;
2071}
2072
2073OMX_ERRORTYPE ComponentBase::ProcessorStart(void)
2074{
2075    return OMX_ErrorNone;
2076}
2077
2078OMX_ERRORTYPE ComponentBase::ProcessorReset(void)
2079{
2080    return OMX_ErrorNone;
2081}
2082
2083
2084OMX_ERRORTYPE ComponentBase::ProcessorStop(void)
2085{
2086    return OMX_ErrorNone;
2087}
2088
2089OMX_ERRORTYPE ComponentBase::ProcessorPause(void)
2090{
2091    return OMX_ErrorNone;
2092}
2093
2094OMX_ERRORTYPE ComponentBase::ProcessorResume(void)
2095{
2096    return OMX_ErrorNone;
2097}
2098
2099OMX_ERRORTYPE ComponentBase::ProcessorFlush(OMX_U32 port_index)
2100{
2101    return OMX_ErrorNone;
2102}
2103
2104OMX_ERRORTYPE ComponentBase::ProcessorPreFillBuffer(OMX_BUFFERHEADERTYPE* buffer)
2105{
2106    return OMX_ErrorNone;
2107}
2108
2109OMX_ERRORTYPE ComponentBase::ProcessorPreEmptyBuffer(OMX_BUFFERHEADERTYPE* buffer)
2110{
2111    return OMX_ErrorNone;
2112}
2113
2114OMX_ERRORTYPE ComponentBase::ProcessorProcess(OMX_BUFFERHEADERTYPE **pBuffers,
2115                                           buffer_retain_t *retain,
2116                                           OMX_U32 nr_buffers)
2117{
2118    LOGE("ProcessorProcess not be implemented");
2119    return OMX_ErrorNotImplemented;
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}
2128
2129OMX_ERRORTYPE ComponentBase::ProcessorPreFreeBuffer(OMX_U32 nPortIndex, OMX_BUFFERHEADERTYPE* pBuffer)
2130{
2131    return OMX_ErrorNone;
2132
2133}
2134/* end of processor callbacks */
2135
2136/* helper for derived class */
2137const OMX_STRING ComponentBase::GetWorkingRole(void)
2138{
2139    return &working_role[0];
2140}
2141
2142const OMX_COMPONENTTYPE *ComponentBase::GetComponentHandle(void)
2143{
2144    return handle;
2145}
2146#if 0
2147void ComponentBase::DumpBuffer(const OMX_BUFFERHEADERTYPE *bufferheader,
2148                               bool dumpdata)
2149{
2150    OMX_U8 *pbuffer = bufferheader->pBuffer, *p;
2151    OMX_U32 offset = bufferheader->nOffset;
2152    OMX_U32 alloc_len = bufferheader->nAllocLen;
2153    OMX_U32 filled_len =  bufferheader->nFilledLen;
2154    OMX_U32 left = filled_len, oneline;
2155    OMX_U32 index = 0, i;
2156    /* 0x%04lx:  %02x %02x .. (n = 16)\n\0 */
2157    char prbuffer[8 + 3 * 0x10 + 2], *pp;
2158    OMX_U32 prbuffer_len;
2159
2160    LOGD("Component %s DumpBuffer\n", name);
2161    LOGD("%s port index = %lu",
2162         (bufferheader->nInputPortIndex != 0x7fffffff) ? "input" : "output",
2163         (bufferheader->nInputPortIndex != 0x7fffffff) ?
2164         bufferheader->nInputPortIndex : bufferheader->nOutputPortIndex);
2165    LOGD("nAllocLen = %lu, nOffset = %lu, nFilledLen = %lu\n",
2166         alloc_len, offset, filled_len);
2167    LOGD("nTimeStamp = %lld, nTickCount = %lu",
2168         bufferheader->nTimeStamp,
2169         bufferheader->nTickCount);
2170    LOGD("nFlags = 0x%08lx\n", bufferheader->nFlags);
2171    LOGD("hMarkTargetComponent = %p, pMarkData = %p\n",
2172         bufferheader->hMarkTargetComponent, bufferheader->pMarkData);
2173
2174    if (!pbuffer || !alloc_len || !filled_len)
2175        return;
2176
2177    if (offset + filled_len > alloc_len)
2178        return;
2179
2180    if (!dumpdata)
2181        return;
2182
2183    p = pbuffer + offset;
2184    while (left) {
2185        oneline = left > 0x10 ? 0x10 : left; /* 16 items per 1 line */
2186        pp += sprintf(pp, "0x%04lx: ", index);
2187        for (i = 0; i < oneline; i++)
2188            pp += sprintf(pp, " %02x", *(p + i));
2189        pp += sprintf(pp, "\n");
2190        *pp = '\0';
2191
2192        index += 0x10;
2193        p += oneline;
2194        left -= oneline;
2195
2196        pp = &prbuffer[0];
2197        LOGD("%s", pp);
2198    }
2199}
2200#endif
2201/* end of component methods & helpers */
2202
2203/*
2204 * omx header manipuation
2205 */
2206void ComponentBase::SetTypeHeader(OMX_PTR type, OMX_U32 size)
2207{
2208    OMX_U32 *nsize;
2209    OMX_VERSIONTYPE *nversion;
2210
2211    if (!type)
2212        return;
2213
2214    nsize = (OMX_U32 *)type;
2215    nversion = (OMX_VERSIONTYPE *)((OMX_U8 *)type + sizeof(OMX_U32));
2216
2217    *nsize = size;
2218    nversion->nVersion = OMX_SPEC_VERSION;
2219}
2220
2221OMX_ERRORTYPE ComponentBase::CheckTypeHeader(const OMX_PTR type, OMX_U32 size)
2222{
2223    OMX_U32 *nsize;
2224    OMX_VERSIONTYPE *nversion;
2225
2226    if (!type)
2227        return OMX_ErrorBadParameter;
2228
2229    nsize = (OMX_U32 *)type;
2230    nversion = (OMX_VERSIONTYPE *)((OMX_U8 *)type + sizeof(OMX_U32));
2231
2232    if (*nsize != size)
2233        return OMX_ErrorBadParameter;
2234
2235    if (nversion->nVersion != OMX_SPEC_VERSION)
2236        return OMX_ErrorVersionMismatch;
2237
2238    return OMX_ErrorNone;
2239}
2240
2241/* end of ComponentBase */
2242