componentbase.cpp revision 86c08d74951f4c563b1343c5b1735742fc734372
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 = OMX_ErrorNone;
1436
1437    if (current == OMX_StateLoaded) {
1438        OMX_U32 i;
1439        for (i = 0; i < nr_ports; i++) {
1440            if (ports[i]->IsEnabled()) {
1441                if (GetWorkingRole() != NULL &&
1442                        !strncmp (GetWorkingRole(),"video_decoder", 13 )) {
1443                    ret = ports[i]->WaitPortBufferCompletionTimeout(800);
1444                } else {
1445                    ports[i]->WaitPortBufferCompletion();
1446                }
1447            }
1448        }
1449
1450        if (ret == OMX_ErrorNone) {
1451            ret = ProcessorInit();
1452        }
1453        if (ret != OMX_ErrorNone) {
1454            LOGE("%s:%s: ProcessorInit() failed (ret : 0x%08x)\n",
1455                 GetName(), GetWorkingRole(), ret);
1456            goto out;
1457        }
1458    }
1459    else if ((current == OMX_StatePause) || (current == OMX_StateExecuting)) {
1460        pthread_mutex_lock(&ports_block);
1461        FlushPort(OMX_ALL, 0);
1462        pthread_mutex_unlock(&ports_block);
1463        LOGV("%s:%s: flushed all ports\n", GetName(), GetWorkingRole());
1464
1465        bufferwork->CancelScheduledWork(this);
1466        LOGV("%s:%s: discarded all scheduled buffer process work\n",
1467             GetName(), GetWorkingRole());
1468
1469        if (current == OMX_StatePause) {
1470            bufferwork->ResumeWork();
1471            LOGV("%s:%s: buffer process work resumed\n",
1472                 GetName(), GetWorkingRole());
1473        }
1474
1475        bufferwork->StopWork();
1476        LOGV("%s:%s: buffer process work stopped\n",
1477             GetName(), GetWorkingRole());
1478
1479        ret = ProcessorStop();
1480        if (ret != OMX_ErrorNone) {
1481            LOGE("%s:%s: ProcessorStop() failed (ret : 0x%08x)\n",
1482                 GetName(), GetWorkingRole(), ret);
1483            goto out;
1484        }
1485    }
1486    else if (current == OMX_StateWaitForResources) {
1487        LOGV("%s:%s: "
1488             "state transition's requested from WaitForResources to Idle\n",
1489             GetName(), GetWorkingRole());
1490
1491        /* same as Loaded to Idle BUT DO NOTHING for now */
1492
1493        ret = OMX_ErrorNone;
1494    }
1495    else
1496        ret = OMX_ErrorIncorrectStateTransition;
1497
1498out:
1499    return ret;
1500}
1501
1502inline OMX_ERRORTYPE
1503ComponentBase::TransStateToExecuting(OMX_STATETYPE current)
1504{
1505    OMX_ERRORTYPE ret;
1506
1507    if (current == OMX_StateIdle) {
1508        bufferwork->StartWork(true);
1509        LOGV("%s:%s: buffer process work started with executing state\n",
1510             GetName(), GetWorkingRole());
1511
1512        ret = ProcessorStart();
1513        if (ret != OMX_ErrorNone) {
1514            LOGE("%s:%s: ProcessorStart() failed (ret : 0x%08x)\n",
1515                 GetName(), GetWorkingRole(), ret);
1516            goto out;
1517        }
1518    }
1519    else if (current == OMX_StatePause) {
1520        bufferwork->ResumeWork();
1521        LOGV("%s:%s: buffer process work resumed\n",
1522             GetName(), GetWorkingRole());
1523
1524        ret = ProcessorResume();
1525        if (ret != OMX_ErrorNone) {
1526            LOGE("%s:%s: ProcessorResume() failed (ret : 0x%08x)\n",
1527                 GetName(), GetWorkingRole(), ret);
1528            goto out;
1529        }
1530    }
1531    else
1532        ret = OMX_ErrorIncorrectStateTransition;
1533
1534out:
1535    return ret;
1536}
1537
1538inline OMX_ERRORTYPE ComponentBase::TransStateToPause(OMX_STATETYPE current)
1539{
1540    OMX_ERRORTYPE ret;
1541
1542    if (current == OMX_StateIdle) {
1543        bufferwork->StartWork(false);
1544        LOGV("%s:%s: buffer process work started with paused state\n",
1545             GetName(), GetWorkingRole());
1546
1547        ret = ProcessorStart();
1548        if (ret != OMX_ErrorNone) {
1549            LOGE("%s:%s: ProcessorSart() failed (ret : 0x%08x)\n",
1550                 GetName(), GetWorkingRole(), ret);
1551            goto out;
1552        }
1553    }
1554    else if (current == OMX_StateExecuting) {
1555        bufferwork->PauseWork();
1556        LOGV("%s:%s: buffer process work paused\n",
1557             GetName(), GetWorkingRole());
1558
1559        ret = ProcessorPause();
1560        if (ret != OMX_ErrorNone) {
1561            LOGE("%s:%s: ProcessorPause() failed (ret : 0x%08x)\n",
1562                 GetName(), GetWorkingRole(), ret);
1563            goto out;
1564        }
1565    }
1566    else
1567        ret = OMX_ErrorIncorrectStateTransition;
1568
1569out:
1570    return ret;
1571}
1572
1573inline OMX_ERRORTYPE ComponentBase::TransStateToInvalid(OMX_STATETYPE current)
1574{
1575    OMX_ERRORTYPE ret = OMX_ErrorInvalidState;
1576
1577    /*
1578     * Todo
1579     *   graceful escape
1580     */
1581
1582    return ret;
1583}
1584
1585inline OMX_ERRORTYPE
1586ComponentBase::TransStateToWaitForResources(OMX_STATETYPE current)
1587{
1588    OMX_ERRORTYPE ret;
1589
1590    if (current == OMX_StateLoaded) {
1591        LOGV("%s:%s: "
1592             "state transition's requested from Loaded to WaitForResources\n",
1593             GetName(), GetWorkingRole());
1594        ret = OMX_ErrorNone;
1595    }
1596    else
1597        ret = OMX_ErrorIncorrectStateTransition;
1598
1599    return ret;
1600}
1601
1602/* mark buffer */
1603void ComponentBase::PushThisMark(OMX_U32 port_index, OMX_MARKTYPE *mark)
1604{
1605    PortBase *port = NULL;
1606    OMX_EVENTTYPE event;
1607    OMX_U32 data1, data2;
1608    OMX_ERRORTYPE ret;
1609
1610    if (ports)
1611        if (port_index < nr_ports)
1612            port = ports[port_index];
1613
1614    if (!port) {
1615        ret = OMX_ErrorBadPortIndex;
1616        goto notify_event;
1617    }
1618
1619    ret = port->PushMark(mark);
1620    if (ret != OMX_ErrorNone) {
1621        /* don't report OMX_ErrorInsufficientResources */
1622        ret = OMX_ErrorUndefined;
1623        goto notify_event;
1624    }
1625
1626notify_event:
1627    if (ret == OMX_ErrorNone) {
1628        event = OMX_EventCmdComplete;
1629        data1 = OMX_CommandMarkBuffer;
1630        data2 = port_index;
1631    }
1632    else {
1633        event = OMX_EventError;
1634        data1 = ret;
1635        data2 = 0;
1636    }
1637
1638    callbacks->EventHandler(handle, appdata, event, data1, data2, NULL);
1639}
1640
1641void ComponentBase::FlushPort(OMX_U32 port_index, bool notify)
1642{
1643    OMX_U32 i, from_index, to_index;
1644
1645    if ((port_index != OMX_ALL) && (port_index > nr_ports-1))
1646        return;
1647
1648    if (port_index == OMX_ALL) {
1649        from_index = 0;
1650        to_index = nr_ports - 1;
1651    }
1652    else {
1653        from_index = port_index;
1654        to_index = port_index;
1655    }
1656
1657    LOGV("%s:%s: flush ports (from index %lu to %lu)\n",
1658         GetName(), GetWorkingRole(), from_index, to_index);
1659
1660    for (i = from_index; i <= to_index; i++) {
1661        ports[i]->FlushPort();
1662        if (notify)
1663            callbacks->EventHandler(handle, appdata, OMX_EventCmdComplete,
1664                                    OMX_CommandFlush, i, NULL);
1665    }
1666
1667    LOGV("%s:%s: flush ports done\n", GetName(), GetWorkingRole());
1668}
1669
1670extern const char *GetPortStateName(OMX_U8 state); //portbase.cpp
1671
1672void ComponentBase::TransStatePort(OMX_U32 port_index, OMX_U8 state)
1673{
1674    OMX_EVENTTYPE event;
1675    OMX_U32 data1, data2;
1676    OMX_U32 i, from_index, to_index;
1677    OMX_ERRORTYPE ret;
1678
1679    if ((port_index != OMX_ALL) && (port_index > nr_ports-1))
1680        return;
1681
1682    if (port_index == OMX_ALL) {
1683        from_index = 0;
1684        to_index = nr_ports - 1;
1685    }
1686    else {
1687        from_index = port_index;
1688        to_index = port_index;
1689    }
1690
1691    LOGV("%s:%s: transit ports state to %s (from index %lu to %lu)\n",
1692         GetName(), GetWorkingRole(), GetPortStateName(state),
1693         from_index, to_index);
1694
1695    pthread_mutex_lock(&ports_block);
1696    for (i = from_index; i <= to_index; i++) {
1697        ret = ports[i]->TransState(state);
1698        if (ret == OMX_ErrorNone) {
1699            event = OMX_EventCmdComplete;
1700            if (state == PortBase::OMX_PortEnabled) {
1701                data1 = OMX_CommandPortEnable;
1702                ProcessorReset();
1703            } else {
1704                data1 = OMX_CommandPortDisable;
1705            }
1706            data2 = i;
1707        } else {
1708            event = OMX_EventError;
1709            data1 = ret;
1710            data2 = 0;
1711        }
1712        callbacks->EventHandler(handle, appdata, event,
1713                                data1, data2, NULL);
1714    }
1715    pthread_mutex_unlock(&ports_block);
1716
1717    LOGV("%s:%s: transit ports state to %s completed\n",
1718         GetName(), GetWorkingRole(), GetPortStateName(state));
1719}
1720
1721/* set working role */
1722OMX_ERRORTYPE ComponentBase::SetWorkingRole(const OMX_STRING role)
1723{
1724    OMX_U32 i;
1725
1726    if (state != OMX_StateUnloaded && state != OMX_StateLoaded)
1727        return OMX_ErrorIncorrectStateOperation;
1728
1729    if (!role) {
1730        working_role = NULL;
1731        return OMX_ErrorNone;
1732    }
1733
1734    for (i = 0; i < nr_roles; i++) {
1735        if (!strcmp((char *)&roles[i][0], role)) {
1736            working_role = (OMX_STRING)&roles[i][0];
1737            return OMX_ErrorNone;
1738        }
1739    }
1740
1741    LOGE("%s: cannot find %s role\n", GetName(), role);
1742    return OMX_ErrorBadParameter;
1743}
1744
1745/* apply a working role for a component having multiple roles */
1746OMX_ERRORTYPE ComponentBase::ApplyWorkingRole(void)
1747{
1748    OMX_U32 i;
1749    OMX_ERRORTYPE ret;
1750
1751    if (state != OMX_StateUnloaded && state != OMX_StateLoaded)
1752        return OMX_ErrorIncorrectStateOperation;
1753
1754    if (!working_role)
1755        return OMX_ErrorBadParameter;
1756
1757    if (!callbacks || !appdata)
1758        return OMX_ErrorBadParameter;
1759
1760    ret = AllocatePorts();
1761    if (ret != OMX_ErrorNone) {
1762        LOGE("failed to AllocatePorts() (ret = 0x%08x)\n", ret);
1763        return ret;
1764    }
1765
1766    /* now we can access ports */
1767    for (i = 0; i < nr_ports; i++) {
1768        ports[i]->SetOwner(handle);
1769        ports[i]->SetCallbacks(handle, callbacks, appdata);
1770    }
1771
1772    LOGI("%s: set working role %s:", GetName(), GetWorkingRole());
1773    return OMX_ErrorNone;
1774}
1775
1776OMX_ERRORTYPE ComponentBase::AllocatePorts(void)
1777{
1778    OMX_DIRTYPE dir;
1779    bool has_input, has_output;
1780    OMX_U32 i;
1781    OMX_ERRORTYPE ret;
1782
1783    if (ports)
1784        return OMX_ErrorBadParameter;
1785
1786    ret = ComponentAllocatePorts();
1787    if (ret != OMX_ErrorNone) {
1788        LOGE("failed to %s::ComponentAllocatePorts(), ret = 0x%08x\n",
1789             name, ret);
1790        return ret;
1791    }
1792
1793    has_input = false;
1794    has_output = false;
1795    ret = OMX_ErrorNone;
1796    for (i = 0; i < nr_ports; i++) {
1797        dir = ports[i]->GetPortDirection();
1798        if (dir == OMX_DirInput)
1799            has_input = true;
1800        else if (dir == OMX_DirOutput)
1801            has_output = true;
1802        else {
1803            ret = OMX_ErrorUndefined;
1804            break;
1805        }
1806    }
1807    if (ret != OMX_ErrorNone)
1808        goto free_ports;
1809
1810    if ((has_input == false) && (has_output == true))
1811        cvariant = CVARIANT_SOURCE;
1812    else if ((has_input == true) && (has_output == true))
1813        cvariant = CVARIANT_FILTER;
1814    else if ((has_input == true) && (has_output == false))
1815        cvariant = CVARIANT_SINK;
1816    else
1817        goto free_ports;
1818
1819    return OMX_ErrorNone;
1820
1821free_ports:
1822    LOGE("%s(): exit, unknown component variant\n", __func__);
1823    FreePorts();
1824    return OMX_ErrorUndefined;
1825}
1826
1827/* called int FreeHandle() */
1828OMX_ERRORTYPE ComponentBase::FreePorts(void)
1829{
1830    if (ports) {
1831        OMX_U32 i, this_nr_ports = this->nr_ports;
1832
1833        for (i = 0; i < this_nr_ports; i++) {
1834            if (ports[i]) {
1835                OMX_MARKTYPE *mark;
1836                /* it should be empty before this */
1837                while ((mark = ports[i]->PopMark()))
1838                    free(mark);
1839
1840                delete ports[i];
1841                ports[i] = NULL;
1842            }
1843        }
1844        delete []ports;
1845        ports = NULL;
1846    }
1847
1848    return OMX_ErrorNone;
1849}
1850
1851/* buffer processing */
1852/* implement WorkableInterface */
1853void ComponentBase::Work(void)
1854{
1855    OMX_BUFFERHEADERTYPE **buffers[nr_ports];
1856    OMX_BUFFERHEADERTYPE *buffers_hdr[nr_ports];
1857    OMX_BUFFERHEADERTYPE *buffers_org[nr_ports];
1858    buffer_retain_t retain[nr_ports];
1859    OMX_U32 i;
1860    OMX_ERRORTYPE ret;
1861
1862    if (nr_ports == 0) {
1863        return;
1864    }
1865
1866    memset(buffers, 0, sizeof(OMX_BUFFERHEADERTYPE *) * nr_ports);
1867    memset(buffers_hdr, 0, sizeof(OMX_BUFFERHEADERTYPE *) * nr_ports);
1868    memset(buffers_org, 0, sizeof(OMX_BUFFERHEADERTYPE *) * nr_ports);
1869
1870    pthread_mutex_lock(&ports_block);
1871
1872    while(IsAllBufferAvailable())
1873    {
1874        for (i = 0; i < nr_ports; i++) {
1875            buffers_hdr[i] = ports[i]->PopBuffer();
1876            buffers[i] = &buffers_hdr[i];
1877            buffers_org[i] = buffers_hdr[i];
1878            retain[i] = BUFFER_RETAIN_NOT_RETAIN;
1879        }
1880
1881        if (working_role != NULL && !strncmp((char*)working_role, "video_decoder", 13)){
1882            ret = ProcessorProcess(buffers, &retain[0], nr_ports);
1883        }else{
1884            ret = ProcessorProcess(buffers_hdr, &retain[0], nr_ports);
1885        }
1886
1887        if (ret == OMX_ErrorNone) {
1888            if (!working_role || (strncmp((char*)working_role, "video_encoder", 13) != 0))
1889                PostProcessBuffers(buffers, &retain[0]);
1890
1891            for (i = 0; i < nr_ports; i++) {
1892                if (buffers_hdr[i] == NULL)
1893                    continue;
1894
1895                if(retain[i] == BUFFER_RETAIN_GETAGAIN) {
1896                    ports[i]->RetainThisBuffer(*buffers[i], false);
1897                }
1898                else if (retain[i] == BUFFER_RETAIN_ACCUMULATE) {
1899                    ports[i]->RetainThisBuffer(*buffers[i], true);
1900                }
1901                else if (retain[i] == BUFFER_RETAIN_OVERRIDDEN) {
1902                    ports[i]->RetainAndReturnBuffer(buffers_org[i], *buffers[i]);
1903                }
1904                else if (retain[i] == BUFFER_RETAIN_CACHE) {
1905                    //nothing to do
1906                } else {
1907                    ports[i]->ReturnThisBuffer(*buffers[i]);
1908                }
1909            }
1910        }
1911        else {
1912
1913            for (i = 0; i < nr_ports; i++) {
1914                if (buffers_hdr[i] == NULL)
1915                    continue;
1916
1917                /* return buffers by hands, these buffers're not in queue */
1918                ports[i]->ReturnThisBuffer(*buffers[i]);
1919                /* flush ports */
1920                ports[i]->FlushPort();
1921            }
1922
1923            callbacks->EventHandler(handle, appdata, OMX_EventError, ret,
1924                                    0, NULL);
1925        }
1926    }
1927
1928    pthread_mutex_unlock(&ports_block);
1929}
1930
1931bool ComponentBase::IsAllBufferAvailable(void)
1932{
1933    OMX_U32 i;
1934    OMX_U32 nr_avail = 0;
1935
1936    for (i = 0; i < nr_ports; i++) {
1937        OMX_U32 length = 0;
1938
1939        if (ports[i]->IsEnabled()) {
1940            length += ports[i]->BufferQueueLength();
1941            length += ports[i]->RetainedBufferQueueLength();
1942        }
1943
1944        if (length)
1945            nr_avail++;
1946    }
1947
1948    if (nr_avail == nr_ports)
1949        return true;
1950    else
1951        return false;
1952}
1953
1954inline void ComponentBase::SourcePostProcessBuffers(
1955    OMX_BUFFERHEADERTYPE ***buffers,
1956    const buffer_retain_t *retain)
1957{
1958    OMX_U32 i;
1959
1960    for (i = 0; i < nr_ports; i++) {
1961        /*
1962         * in case of source component, buffers're marked when they come
1963         * from the ouput ports
1964         */
1965        if (!(*buffers[i])->hMarkTargetComponent) {
1966            OMX_MARKTYPE *mark;
1967
1968            mark = ports[i]->PopMark();
1969            if (mark) {
1970                (*buffers[i])->hMarkTargetComponent = mark->hMarkTargetComponent;
1971                (*buffers[i])->pMarkData = mark->pMarkData;
1972                free(mark);
1973            }
1974        }
1975    }
1976}
1977
1978inline void ComponentBase::FilterPostProcessBuffers(
1979    OMX_BUFFERHEADERTYPE ***buffers,
1980    const buffer_retain_t *retain)
1981{
1982    OMX_MARKTYPE *mark;
1983    OMX_U32 i, j;
1984
1985    for (i = 0; i < nr_ports; i++) {
1986        if (ports[i]->GetPortDirection() == OMX_DirInput) {
1987            for (j = 0; j < nr_ports; j++) {
1988                if (ports[j]->GetPortDirection() != OMX_DirOutput)
1989                    continue;
1990
1991                /* propagates EOS flag */
1992                /* clear input EOS at the end of this loop */
1993                if (retain[i] != BUFFER_RETAIN_GETAGAIN) {
1994                    if ((*buffers[i])->nFlags & OMX_BUFFERFLAG_EOS)
1995                        (*buffers[j])->nFlags |= OMX_BUFFERFLAG_EOS;
1996                }
1997
1998                /* propagates marks */
1999                /*
2000                 * if hMarkTargetComponent == handle then the mark's not
2001                 * propagated
2002                 */
2003                if ((*buffers[i])->hMarkTargetComponent &&
2004                    ((*buffers[i])->hMarkTargetComponent != handle)) {
2005                    if ((*buffers[j])->hMarkTargetComponent) {
2006                        mark = (OMX_MARKTYPE *)malloc(sizeof(*mark));
2007                        if (mark) {
2008                            mark->hMarkTargetComponent =
2009                                (*buffers[i])->hMarkTargetComponent;
2010                            mark->pMarkData = (*buffers[i])->pMarkData;
2011                            ports[j]->PushMark(mark);
2012                            mark = NULL;
2013                            (*buffers[i])->hMarkTargetComponent = NULL;
2014                            (*buffers[i])->pMarkData = NULL;
2015                        }
2016                    }
2017                    else {
2018                        mark = ports[j]->PopMark();
2019                        if (mark) {
2020                            (*buffers[j])->hMarkTargetComponent =
2021                                mark->hMarkTargetComponent;
2022                            (*buffers[j])->pMarkData = mark->pMarkData;
2023                            free(mark);
2024
2025                            mark = (OMX_MARKTYPE *)malloc(sizeof(*mark));
2026                            if (mark) {
2027                                mark->hMarkTargetComponent =
2028                                    (*buffers[i])->hMarkTargetComponent;
2029                                mark->pMarkData = (*buffers[i])->pMarkData;
2030                                ports[j]->PushMark(mark);
2031                                mark = NULL;
2032                                (*buffers[i])->hMarkTargetComponent = NULL;
2033                                (*buffers[i])->pMarkData = NULL;
2034                            }
2035                        }
2036                        else {
2037                            (*buffers[j])->hMarkTargetComponent =
2038                                (*buffers[i])->hMarkTargetComponent;
2039                            (*buffers[j])->pMarkData = (*buffers[i])->pMarkData;
2040                            (*buffers[i])->hMarkTargetComponent = NULL;
2041                            (*buffers[i])->pMarkData = NULL;
2042                        }
2043                    }
2044                }
2045            }
2046            /* clear input buffer's EOS */
2047            if (retain[i] != BUFFER_RETAIN_GETAGAIN)
2048                (*buffers[i])->nFlags &= ~OMX_BUFFERFLAG_EOS;
2049        }
2050    }
2051}
2052
2053inline void ComponentBase::SinkPostProcessBuffers(
2054    OMX_BUFFERHEADERTYPE ***buffers,
2055    const buffer_retain_t *retain)
2056{
2057    return;
2058}
2059
2060void ComponentBase::PostProcessBuffers(OMX_BUFFERHEADERTYPE ***buffers,
2061                                       const buffer_retain_t *retain)
2062{
2063
2064    if (cvariant == CVARIANT_SOURCE)
2065        SourcePostProcessBuffers(buffers, retain);
2066    else if (cvariant == CVARIANT_FILTER)
2067        FilterPostProcessBuffers(buffers, retain);
2068    else if (cvariant == CVARIANT_SINK) {
2069        SinkPostProcessBuffers(buffers, retain);
2070    }
2071    else {
2072        LOGE("%s(): fatal error unknown component variant (%d)\n",
2073             __func__, cvariant);
2074    }
2075}
2076
2077/* processor default callbacks */
2078OMX_ERRORTYPE ComponentBase::ProcessorInit(void)
2079{
2080    return OMX_ErrorNone;
2081}
2082OMX_ERRORTYPE ComponentBase::ProcessorDeinit(void)
2083{
2084    return OMX_ErrorNone;
2085}
2086
2087OMX_ERRORTYPE ComponentBase::ProcessorStart(void)
2088{
2089    return OMX_ErrorNone;
2090}
2091
2092OMX_ERRORTYPE ComponentBase::ProcessorReset(void)
2093{
2094    return OMX_ErrorNone;
2095}
2096
2097
2098OMX_ERRORTYPE ComponentBase::ProcessorStop(void)
2099{
2100    return OMX_ErrorNone;
2101}
2102
2103OMX_ERRORTYPE ComponentBase::ProcessorPause(void)
2104{
2105    return OMX_ErrorNone;
2106}
2107
2108OMX_ERRORTYPE ComponentBase::ProcessorResume(void)
2109{
2110    return OMX_ErrorNone;
2111}
2112
2113OMX_ERRORTYPE ComponentBase::ProcessorFlush(OMX_U32 port_index)
2114{
2115    return OMX_ErrorNone;
2116}
2117
2118OMX_ERRORTYPE ComponentBase::ProcessorPreFillBuffer(OMX_BUFFERHEADERTYPE* buffer)
2119{
2120    return OMX_ErrorNone;
2121}
2122
2123OMX_ERRORTYPE ComponentBase::ProcessorPreEmptyBuffer(OMX_BUFFERHEADERTYPE* buffer)
2124{
2125    return OMX_ErrorNone;
2126}
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}
2135OMX_ERRORTYPE ComponentBase::ProcessorProcess(OMX_BUFFERHEADERTYPE ***pBuffers,
2136                                           buffer_retain_t *retain,
2137                                           OMX_U32 nr_buffers)
2138{
2139    LOGE("ProcessorProcess not be implemented");
2140    return OMX_ErrorNotImplemented;
2141}
2142
2143OMX_ERRORTYPE ComponentBase::ProcessorPreFreeBuffer(OMX_U32 nPortIndex, OMX_BUFFERHEADERTYPE* pBuffer)
2144{
2145    return OMX_ErrorNone;
2146
2147}
2148/* end of processor callbacks */
2149
2150/* helper for derived class */
2151const OMX_STRING ComponentBase::GetWorkingRole(void)
2152{
2153    return &working_role[0];
2154}
2155
2156const OMX_COMPONENTTYPE *ComponentBase::GetComponentHandle(void)
2157{
2158    return handle;
2159}
2160#if 0
2161void ComponentBase::DumpBuffer(const OMX_BUFFERHEADERTYPE *bufferheader,
2162                               bool dumpdata)
2163{
2164    OMX_U8 *pbuffer = bufferheader->pBuffer, *p;
2165    OMX_U32 offset = bufferheader->nOffset;
2166    OMX_U32 alloc_len = bufferheader->nAllocLen;
2167    OMX_U32 filled_len =  bufferheader->nFilledLen;
2168    OMX_U32 left = filled_len, oneline;
2169    OMX_U32 index = 0, i;
2170    /* 0x%04lx:  %02x %02x .. (n = 16)\n\0 */
2171    char prbuffer[8 + 3 * 0x10 + 2], *pp;
2172    OMX_U32 prbuffer_len;
2173
2174    LOGD("Component %s DumpBuffer\n", name);
2175    LOGD("%s port index = %lu",
2176         (bufferheader->nInputPortIndex != 0x7fffffff) ? "input" : "output",
2177         (bufferheader->nInputPortIndex != 0x7fffffff) ?
2178         bufferheader->nInputPortIndex : bufferheader->nOutputPortIndex);
2179    LOGD("nAllocLen = %lu, nOffset = %lu, nFilledLen = %lu\n",
2180         alloc_len, offset, filled_len);
2181    LOGD("nTimeStamp = %lld, nTickCount = %lu",
2182         bufferheader->nTimeStamp,
2183         bufferheader->nTickCount);
2184    LOGD("nFlags = 0x%08lx\n", bufferheader->nFlags);
2185    LOGD("hMarkTargetComponent = %p, pMarkData = %p\n",
2186         bufferheader->hMarkTargetComponent, bufferheader->pMarkData);
2187
2188    if (!pbuffer || !alloc_len || !filled_len)
2189        return;
2190
2191    if (offset + filled_len > alloc_len)
2192        return;
2193
2194    if (!dumpdata)
2195        return;
2196
2197    p = pbuffer + offset;
2198    while (left) {
2199        oneline = left > 0x10 ? 0x10 : left; /* 16 items per 1 line */
2200        pp += sprintf(pp, "0x%04lx: ", index);
2201        for (i = 0; i < oneline; i++)
2202            pp += sprintf(pp, " %02x", *(p + i));
2203        pp += sprintf(pp, "\n");
2204        *pp = '\0';
2205
2206        index += 0x10;
2207        p += oneline;
2208        left -= oneline;
2209
2210        pp = &prbuffer[0];
2211        LOGD("%s", pp);
2212    }
2213}
2214#endif
2215/* end of component methods & helpers */
2216
2217/*
2218 * omx header manipuation
2219 */
2220void ComponentBase::SetTypeHeader(OMX_PTR type, OMX_U32 size)
2221{
2222    OMX_U32 *nsize;
2223    OMX_VERSIONTYPE *nversion;
2224
2225    if (!type)
2226        return;
2227
2228    nsize = (OMX_U32 *)type;
2229    nversion = (OMX_VERSIONTYPE *)((OMX_U8 *)type + sizeof(OMX_U32));
2230
2231    *nsize = size;
2232    nversion->nVersion = OMX_SPEC_VERSION;
2233}
2234
2235OMX_ERRORTYPE ComponentBase::CheckTypeHeader(const OMX_PTR type, OMX_U32 size)
2236{
2237    OMX_U32 *nsize;
2238    OMX_VERSIONTYPE *nversion;
2239
2240    if (!type)
2241        return OMX_ErrorBadParameter;
2242
2243    nsize = (OMX_U32 *)type;
2244    nversion = (OMX_VERSIONTYPE *)((OMX_U8 *)type + sizeof(OMX_U32));
2245
2246    if (*nsize != size)
2247        return OMX_ErrorBadParameter;
2248
2249    if (nversion->nVersion != OMX_SPEC_VERSION)
2250        return OMX_ErrorVersionMismatch;
2251
2252    return OMX_ErrorNone;
2253}
2254
2255/* end of ComponentBase */
2256