componentbase.h revision dd0f8243514bd9380e5172ba883e488bcf65dcc9
1/*
2 * Copyright (c) 2009 Wind River Systems, Inc.
3 *
4 * The right to copy, distribute, modify, or otherwise make use
5 * of this software may be licensed only pursuant to the terms
6 * of an applicable Wind River license agreement.
7 */
8
9#ifndef __COMPONENTBASE_H
10#define __COMPONENTBASE_H
11
12#include <OMX_Core.h>
13#include <OMX_Component.h>
14
15#include <cmodule.h>
16#include <portbase.h>
17
18#include <queue.h>
19#include <workqueue.h>
20
21/* ProcessCmdWork */
22struct cmd_s {
23    OMX_COMMANDTYPE cmd;
24    OMX_U32 param1;
25    OMX_PTR cmddata;
26};
27
28class CmdHandlerInterface
29{
30public:
31    virtual ~CmdHandlerInterface() {};
32    virtual void CmdHandler(struct cmd_s *cmd) = 0;
33};
34
35class CmdProcessWork : public WorkableInterface
36{
37public:
38    CmdProcessWork(CmdHandlerInterface *ci);
39    ~CmdProcessWork();
40
41    OMX_ERRORTYPE PushCmdQueue(struct cmd_s *cmd);
42
43private:
44    struct cmd_s *PopCmdQueue(void);
45
46    virtual void Work(void); /* call ci->CmdHandler() */
47
48    void ScheduleIfAvailable(void);
49
50    WorkQueue *workq;
51
52    struct queue q;
53    pthread_mutex_t lock;
54
55    CmdHandlerInterface *ci; /* to run ComponentBase::CmdHandler() */
56};
57
58class ComponentBase : public CmdHandlerInterface, public WorkableInterface
59{
60public:
61    /*
62     * constructor & destructor
63     */
64    ComponentBase();
65    ComponentBase(const OMX_STRING name);
66    virtual ~ComponentBase();
67
68    /*
69     * accessor
70     */
71    /* name */
72    void SetName(const OMX_STRING name);
73    const OMX_STRING GetName(void);
74
75    /* cmodule */
76    void SetCModule(CModule *cmodule);
77    CModule *GetCModule(void);
78
79    /* end of accessor */
80
81    /*
82     * core methods & helpers
83     */
84    /* roles */
85    OMX_ERRORTYPE SetRolesOfComponent(OMX_U32 nr_roles, const OMX_U8 **roles);
86
87    /* GetHandle & FreeHandle */
88    OMX_ERRORTYPE GetHandle(OMX_HANDLETYPE* pHandle,
89                            OMX_PTR pAppData,
90                            OMX_CALLBACKTYPE *pCallBacks);
91    OMX_ERRORTYPE FreeHandle(OMX_HANDLETYPE hComponent);
92
93    /* end of core methods & helpers */
94
95    /*
96     * component methods & helpers
97     */
98    static OMX_ERRORTYPE GetComponentVersion(
99        OMX_IN  OMX_HANDLETYPE hComponent,
100        OMX_OUT OMX_STRING pComponentName,
101        OMX_OUT OMX_VERSIONTYPE* pComponentVersion,
102        OMX_OUT OMX_VERSIONTYPE* pSpecVersion,
103        OMX_OUT OMX_UUIDTYPE* pComponentUUID);
104    OMX_ERRORTYPE CBaseGetComponentVersion(
105        OMX_IN  OMX_HANDLETYPE hComponent,
106        OMX_OUT OMX_STRING pComponentName,
107        OMX_OUT OMX_VERSIONTYPE* pComponentVersion,
108        OMX_OUT OMX_VERSIONTYPE* pSpecVersion,
109        OMX_OUT OMX_UUIDTYPE* pComponentUUID);
110
111    static OMX_ERRORTYPE SendCommand(
112        OMX_IN  OMX_HANDLETYPE hComponent,
113        OMX_IN  OMX_COMMANDTYPE Cmd,
114        OMX_IN  OMX_U32 nParam1,
115        OMX_IN  OMX_PTR pCmdData);
116    OMX_ERRORTYPE CBaseSendCommand(
117        OMX_IN  OMX_HANDLETYPE hComponent,
118        OMX_IN  OMX_COMMANDTYPE Cmd,
119        OMX_IN  OMX_U32 nParam1,
120        OMX_IN  OMX_PTR pCmdData);
121
122    static OMX_ERRORTYPE GetParameter(
123        OMX_IN  OMX_HANDLETYPE hComponent,
124        OMX_IN  OMX_INDEXTYPE nParamIndex,
125        OMX_INOUT OMX_PTR pComponentParameterStructure);
126    OMX_ERRORTYPE CBaseGetParameter(
127        OMX_IN  OMX_HANDLETYPE hComponent,
128        OMX_IN  OMX_INDEXTYPE nParamIndex,
129        OMX_INOUT OMX_PTR pComponentParameterStructure);
130
131    static OMX_ERRORTYPE SetParameter(
132        OMX_IN  OMX_HANDLETYPE hComponent,
133        OMX_IN  OMX_INDEXTYPE nIndex,
134        OMX_IN  OMX_PTR pComponentParameterStructure);
135    OMX_ERRORTYPE CBaseSetParameter(
136        OMX_IN  OMX_HANDLETYPE hComponent,
137        OMX_IN  OMX_INDEXTYPE nIndex,
138        OMX_IN  OMX_PTR pComponentParameterStructure);
139
140    static OMX_ERRORTYPE GetConfig(
141        OMX_IN  OMX_HANDLETYPE hComponent,
142        OMX_IN  OMX_INDEXTYPE nIndex,
143        OMX_INOUT OMX_PTR pComponentConfigStructure);
144    OMX_ERRORTYPE CBaseGetConfig(
145        OMX_IN  OMX_HANDLETYPE hComponent,
146        OMX_IN  OMX_INDEXTYPE nIndex,
147        OMX_INOUT OMX_PTR pComponentConfigStructure);
148
149    static OMX_ERRORTYPE SetConfig(
150        OMX_IN  OMX_HANDLETYPE hComponent,
151        OMX_IN  OMX_INDEXTYPE nIndex,
152        OMX_IN  OMX_PTR pComponentConfigStructure);
153    OMX_ERRORTYPE CBaseSetConfig(
154        OMX_IN  OMX_HANDLETYPE hComponent,
155        OMX_IN  OMX_INDEXTYPE nIndex,
156        OMX_IN  OMX_PTR pComponentConfigStructure);
157
158    static OMX_ERRORTYPE GetExtensionIndex(
159        OMX_IN  OMX_HANDLETYPE hComponent,
160        OMX_IN  OMX_STRING cParameterName,
161        OMX_OUT OMX_INDEXTYPE* pIndexType);
162    OMX_ERRORTYPE CBaseGetExtensionIndex(
163        OMX_IN  OMX_HANDLETYPE hComponent,
164        OMX_IN  OMX_STRING cParameterName,
165        OMX_OUT OMX_INDEXTYPE* pIndexType);
166
167    static OMX_ERRORTYPE GetState(
168        OMX_IN  OMX_HANDLETYPE hComponent,
169        OMX_OUT OMX_STATETYPE* pState);
170    OMX_ERRORTYPE CBaseGetState(
171        OMX_IN  OMX_HANDLETYPE hComponent,
172        OMX_OUT OMX_STATETYPE* pState);
173
174    static OMX_ERRORTYPE ComponentTunnelRequest(
175        OMX_IN  OMX_HANDLETYPE hComp,
176        OMX_IN  OMX_U32 nPort,
177        OMX_IN  OMX_HANDLETYPE hTunneledComp,
178        OMX_IN  OMX_U32 nTunneledPort,
179        OMX_INOUT  OMX_TUNNELSETUPTYPE* pTunnelSetup);
180    OMX_ERRORTYPE CBaseComponentTunnelRequest(
181        OMX_IN  OMX_HANDLETYPE hComp,
182        OMX_IN  OMX_U32 nPort,
183        OMX_IN  OMX_HANDLETYPE hTunneledComp,
184        OMX_IN  OMX_U32 nTunneledPort,
185        OMX_INOUT  OMX_TUNNELSETUPTYPE* pTunnelSetup);
186
187    static OMX_ERRORTYPE UseBuffer(
188        OMX_IN OMX_HANDLETYPE hComponent,
189        OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
190        OMX_IN OMX_U32 nPortIndex,
191        OMX_IN OMX_PTR pAppPrivate,
192        OMX_IN OMX_U32 nSizeBytes,
193        OMX_IN OMX_U8* pBuffer);
194    OMX_ERRORTYPE CBaseUseBuffer(
195        OMX_IN OMX_HANDLETYPE hComponent,
196        OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
197        OMX_IN OMX_U32 nPortIndex,
198        OMX_IN OMX_PTR pAppPrivate,
199        OMX_IN OMX_U32 nSizeBytes,
200        OMX_IN OMX_U8* pBuffer);
201
202    static OMX_ERRORTYPE AllocateBuffer(
203        OMX_IN OMX_HANDLETYPE hComponent,
204        OMX_INOUT OMX_BUFFERHEADERTYPE** ppBuffer,
205        OMX_IN OMX_U32 nPortIndex,
206        OMX_IN OMX_PTR pAppPrivate,
207        OMX_IN OMX_U32 nSizeBytes);
208    OMX_ERRORTYPE CBaseAllocateBuffer(
209        OMX_IN OMX_HANDLETYPE hComponent,
210        OMX_INOUT OMX_BUFFERHEADERTYPE** ppBuffer,
211        OMX_IN OMX_U32 nPortIndex,
212        OMX_IN OMX_PTR pAppPrivate,
213        OMX_IN OMX_U32 nSizeBytes);
214
215    static OMX_ERRORTYPE FreeBuffer(
216        OMX_IN  OMX_HANDLETYPE hComponent,
217        OMX_IN  OMX_U32 nPortIndex,
218        OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);
219    OMX_ERRORTYPE CBaseFreeBuffer(
220        OMX_IN  OMX_HANDLETYPE hComponent,
221        OMX_IN  OMX_U32 nPortIndex,
222        OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);
223
224    static OMX_ERRORTYPE EmptyThisBuffer(
225        OMX_IN  OMX_HANDLETYPE hComponent,
226        OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);
227    OMX_ERRORTYPE CBaseEmptyThisBuffer(
228        OMX_IN  OMX_HANDLETYPE hComponent,
229        OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);
230
231    static OMX_ERRORTYPE FillThisBuffer(
232        OMX_IN  OMX_HANDLETYPE hComponent,
233        OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);
234    OMX_ERRORTYPE CBaseFillThisBuffer(
235        OMX_IN  OMX_HANDLETYPE hComponent,
236        OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);
237
238    static OMX_ERRORTYPE SetCallbacks(
239        OMX_IN  OMX_HANDLETYPE hComponent,
240        OMX_IN  OMX_CALLBACKTYPE* pCallbacks,
241        OMX_IN  OMX_PTR pAppData);
242    OMX_ERRORTYPE CBaseSetCallbacks(
243        OMX_IN  OMX_HANDLETYPE hComponent,
244        OMX_IN  OMX_CALLBACKTYPE* pCallbacks,
245        OMX_IN  OMX_PTR pAppData);
246
247    static OMX_ERRORTYPE ComponentDeInit(
248        OMX_IN  OMX_HANDLETYPE hComponent);
249    OMX_ERRORTYPE CBaseComponentDeInit(
250        OMX_IN  OMX_HANDLETYPE hComponent);
251
252    static OMX_ERRORTYPE UseEGLImage(
253        OMX_IN OMX_HANDLETYPE hComponent,
254        OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
255        OMX_IN OMX_U32 nPortIndex,
256        OMX_IN OMX_PTR pAppPrivate,
257        OMX_IN void* eglImage);
258    OMX_ERRORTYPE CBaseUseEGLImage(
259        OMX_IN OMX_HANDLETYPE hComponent,
260        OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
261        OMX_IN OMX_U32 nPortIndex,
262        OMX_IN OMX_PTR pAppPrivate,
263        OMX_IN void* eglImage);
264
265    static OMX_ERRORTYPE ComponentRoleEnum(
266        OMX_IN OMX_HANDLETYPE hComponent,
267        OMX_OUT OMX_U8 *cRole,
268        OMX_IN OMX_U32 nIndex);
269    OMX_ERRORTYPE CBaseComponentRoleEnum(
270        OMX_IN OMX_HANDLETYPE hComponent,
271        OMX_OUT OMX_U8 *cRole,
272        OMX_IN OMX_U32 nIndex);
273
274    /* end of component methods & helpers */
275
276    /*
277     * omx header manipuation
278     */
279    static void SetTypeHeader(OMX_PTR type, OMX_U32 size);
280    static OMX_ERRORTYPE CheckTypeHeader(const OMX_PTR type, OMX_U32 size);
281
282    /* end of omx header manipuation */
283
284    /*
285     * helper method for queury_roles()
286     */
287    static OMX_ERRORTYPE QueryRolesHelper(OMX_U32 nr_comp_roles,
288                                          const OMX_U8 **comp_roles,
289                                          OMX_U32 *nr_roles, OMX_U8 **roles);
290
291    /* end of helper method for queury_roles() */
292
293protected:
294    /* helpers for derived class */
295    const OMX_STRING GetWorkingRole(void);
296    const OMX_COMPONENTTYPE *GetComponentHandle(void);
297
298    void DumpBuffer(const OMX_BUFFERHEADERTYPE *bufferheader, bool dumpdata);
299
300    /* end of helpers for derived class */
301
302    /* ports */
303    /*
304     * allocated with derived port classes by derived component classes
305     */
306    PortBase **ports;
307    OMX_U32 nr_ports;
308    OMX_PORT_PARAM_TYPE portparam;
309
310    /* ports big lock, must be held when accessing all ports at one time */
311    pthread_mutex_t ports_block;
312
313private:
314    /* common routines for constructor */
315    void __ComponentBase(void);
316
317    /*
318     * core methods & helpers
319     */
320    /* called in GetHandle (nr_roles == 1) or SetParameter(ComponentRole) */
321    OMX_ERRORTYPE SetWorkingRole(const OMX_STRING role);
322    /* called int FreeHandle() */
323    OMX_ERRORTYPE FreePorts(void);
324
325    /* end of core methods & helpers */
326
327    /*
328     * component methods & helpers
329     */
330    /* SendCommand */
331    /* implement CmdHandlerInterface */
332    virtual void CmdHandler(struct cmd_s *cmd);
333
334    /* SendCommand:OMX_CommandStateSet */
335    /* called in CmdHandler() thread context or by component itself */
336    void TransState(OMX_STATETYPE transition);
337    inline OMX_ERRORTYPE TransStateToLoaded(OMX_STATETYPE current);
338    inline OMX_ERRORTYPE TransStateToIdle(OMX_STATETYPE current);
339    inline OMX_ERRORTYPE TransStateToExecuting(OMX_STATETYPE current);
340    inline OMX_ERRORTYPE TransStateToPause(OMX_STATETYPE current);
341    inline OMX_ERRORTYPE TransStateToWaitForResources(OMX_STATETYPE current);
342    inline OMX_ERRORTYPE TransStateToInvalid(OMX_STATETYPE current);
343
344    /* called in TransStateToIdle(Loaded) */
345    OMX_ERRORTYPE ApplyWorkingRole(void);
346    /* called in ApplyWorkingRole() */
347    OMX_ERRORTYPE AllocatePorts(void);
348    /* allocate specific port type derived from portbase */
349    virtual OMX_ERRORTYPE ComponentAllocatePorts(void) = 0;
350
351    /* SendCommand:OMX_CommandMarkBuffer */
352    /* called in CmdHandler() thread context */
353    void PushThisMark(OMX_U32 portindex, OMX_MARKTYPE *mark);
354
355    /* SendCommand:OMX_CommandFlush (notify:1) or other parts (notify:0) */
356    void FlushPort(OMX_U32 port_index, bool notify);
357
358    /* SendCommand:OMX_CommandPortDisable/Enable */
359    /* state: PortBase::OMX_PortEnabled/Disabled */
360    void TransStatePort(OMX_U32 port_index, OMX_U8 state);
361
362    /* Get/SetParameter */
363    virtual OMX_ERRORTYPE
364        ComponentGetParameter(OMX_INDEXTYPE nParamIndex,
365                              OMX_PTR pComponentParameterStructure) = 0;
366    virtual OMX_ERRORTYPE
367        ComponentSetParameter(OMX_INDEXTYPE nIndex,
368                              OMX_PTR pComponentParameterStructure) = 0;
369
370    /* Get/SetConfig */
371    virtual OMX_ERRORTYPE
372        ComponentGetConfig(OMX_INDEXTYPE nIndex,
373                           OMX_PTR pComponentConfigStructure) = 0;
374    virtual OMX_ERRORTYPE
375        ComponentSetConfig(OMX_INDEXTYPE nIndex,
376                           OMX_PTR pComponentConfigStructure) = 0;
377
378    /* buffer processing */
379    /* implement WorkableInterface */
380    virtual void Work(void); /* handle this->ports, hold ports_block */
381    /* check if all port has own pending buffer */
382    bool IsAllBufferAvailable(void);
383    /* bufferwork->ScheduleWork() if IsAllBufferAvailable is true */
384    void ScheduleIfAllBufferAvailable(void);
385    /* called in Work() after ProcessorProcess() */
386    void PostProcessBuffer(OMX_BUFFERHEADERTYPE **buffers,
387                           bool *retain,
388                           OMX_U32 nr_buffers);
389
390    /* processor callbacks */
391    /* TransState */
392    virtual OMX_ERRORTYPE ProcessorInit(void);  /* Loaded to Idle */
393    virtual OMX_ERRORTYPE ProcessorDeinit(void);/* Idle to Loaded */
394    virtual OMX_ERRORTYPE ProcessorStart(void); /* Idle to Executing/Pause */
395    virtual OMX_ERRORTYPE ProcessorStop(void);  /* Executing/Pause to Idle */
396    virtual OMX_ERRORTYPE ProcessorPause(void); /* Executing to Pause */
397    virtual OMX_ERRORTYPE ProcessorResume(void);/* Pause to Executing */
398    /* Work */
399    virtual void ProcessorProcess(OMX_BUFFERHEADERTYPE **buffers,
400                                  bool *retain,
401                                  OMX_U32 nr_buffers) = 0;
402
403    /* end of component methods & helpers */
404
405    /* process component's commands work */
406    CmdProcessWork *cmdwork;
407
408    /* buffer processing work */
409    WorkQueue *bufferwork;
410
411    /* roles */
412    OMX_U8 **roles;
413    OMX_U32 nr_roles;
414
415    OMX_STRING working_role;
416
417    /* omx standard handle */
418    /* allocated at GetHandle, freed at FreeHandle */
419    OMX_COMPONENTTYPE *handle;
420
421    /* component module */
422    CModule *cmodule;
423
424    OMX_STATETYPE state;
425
426    const static OMX_STATETYPE OMX_StateUnloaded = OMX_StateVendorStartUnused;
427
428    /* omx standard callbacks */
429    OMX_PTR appdata;
430    OMX_CALLBACKTYPE *callbacks;
431
432    /* component name */
433    char name[OMX_MAX_STRINGNAME_SIZE];
434
435    /* omx specification version */
436#ifndef ANDROID
437    const static OMX_U8 OMX_SPEC_VERSION_MAJOR = 1;
438    const static OMX_U8 OMX_SPEC_VERSION_MINOR = 1;
439    const static OMX_U8 OMX_SPEC_VERSION_REVISION = 2;
440    const static OMX_U8 OMX_SPEC_VERSION_STEP = 0;
441#else
442    const static OMX_U8 OMX_SPEC_VERSION_MAJOR = 1;
443    const static OMX_U8 OMX_SPEC_VERSION_MINOR = 0;
444    const static OMX_U8 OMX_SPEC_VERSION_REVISION = 0;
445    const static OMX_U8 OMX_SPEC_VERSION_STEP = 0;
446#endif
447
448    const static OMX_U32 OMX_SPEC_VERSION = 0
449        | (OMX_SPEC_VERSION_MAJOR << 0)
450        | (OMX_SPEC_VERSION_MINOR << 8)
451        | (OMX_SPEC_VERSION_REVISION << 16)
452        | (OMX_SPEC_VERSION_STEP << 24);
453};
454
455#endif /* __COMPONENTBASE_H */
456