aboutsummaryrefslogtreecommitdiff
path: root/base/inc/componentbase.h
blob: fe70fe10001c2a909e86ef66b8f1c85e22a7870f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
 * Copyright (C) 2009 Wind River Systems.
 */

#ifndef __COMPONENTBASE_H
#define __COMPONENTBASE_H

#include <OMX_Core.h>
#include <OMX_Component.h>

#include <cmodule.h>
#include <portbase.h>

#include <queue.h>
#include <workqueue.h>

/* ProcessCmdWork */
struct cmd_s {
    OMX_COMMANDTYPE cmd;
    OMX_U32 param1;
    OMX_PTR cmddata;
};

class CmdHandlerInterface
{
public:
    virtual ~CmdHandlerInterface() {};
    virtual void CmdHandler(struct cmd_s *cmd) = 0;
};

class CmdProcessWork : public WorkableInterface
{
public:
    CmdProcessWork(CmdHandlerInterface *ci);
    ~CmdProcessWork();

    OMX_ERRORTYPE PushCmdQueue(struct cmd_s *cmd);

private:
    struct cmd_s *PopCmdQueue(void);

    virtual void Work(void); /* call ci->CmdHandler() */

    void ScheduleIfAvailable(void);

    WorkQueue *workq;

    struct queue q;
    pthread_mutex_t lock;

    CmdHandlerInterface *ci; /* to run ComponentBase::CmdHandler() */
};

class ComponentBase : public CmdHandlerInterface, public WorkableInterface
{
public:
    /*
     * constructor & destructor
     */
    ComponentBase();
    ComponentBase(const OMX_STRING name);
    virtual ~ComponentBase();

    /*
     * accessor
     */
    /* name */
    void SetName(const OMX_STRING name);
    const OMX_STRING GetName(void);

    /* cmodule */
    void SetCModule(CModule *cmodule);
    CModule *GetCModule(void);

    /* end of accessor */

    /*
     * core methods & helpers
     */
    /* roles */
    OMX_ERRORTYPE SetRolesOfComponent(OMX_U32 nr_roles, const OMX_U8 **roles);
    OMX_ERRORTYPE GetRolesOfComponent(OMX_U32 *nr_roles, OMX_U8 **roles);
    bool QueryHavingThisRole(const OMX_STRING role);

    /* GetHandle & FreeHandle */
    OMX_ERRORTYPE GetHandle(OMX_HANDLETYPE* pHandle,
                            OMX_PTR pAppData,
                            OMX_CALLBACKTYPE *pCallBacks);
    OMX_ERRORTYPE FreeHandle(OMX_HANDLETYPE hComponent);

    /* end of core methods & helpers */

    /*
     * component methods & helpers
     */
    static OMX_ERRORTYPE GetComponentVersion(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_OUT OMX_STRING pComponentName,
        OMX_OUT OMX_VERSIONTYPE* pComponentVersion,
        OMX_OUT OMX_VERSIONTYPE* pSpecVersion,
        OMX_OUT OMX_UUIDTYPE* pComponentUUID);
    OMX_ERRORTYPE CBaseGetComponentVersion(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_OUT OMX_STRING pComponentName,
        OMX_OUT OMX_VERSIONTYPE* pComponentVersion,
        OMX_OUT OMX_VERSIONTYPE* pSpecVersion,
        OMX_OUT OMX_UUIDTYPE* pComponentUUID);

    static OMX_ERRORTYPE SendCommand(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_COMMANDTYPE Cmd,
        OMX_IN  OMX_U32 nParam1,
        OMX_IN  OMX_PTR pCmdData);
    OMX_ERRORTYPE CBaseSendCommand(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_COMMANDTYPE Cmd,
        OMX_IN  OMX_U32 nParam1,
        OMX_IN  OMX_PTR pCmdData);

    static OMX_ERRORTYPE GetParameter(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_INDEXTYPE nParamIndex,
        OMX_INOUT OMX_PTR pComponentParameterStructure);
    OMX_ERRORTYPE CBaseGetParameter(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_INDEXTYPE nParamIndex,
        OMX_INOUT OMX_PTR pComponentParameterStructure);

    static OMX_ERRORTYPE SetParameter(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_INDEXTYPE nIndex,
        OMX_IN  OMX_PTR pComponentParameterStructure);
    OMX_ERRORTYPE CBaseSetParameter(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_INDEXTYPE nIndex,
        OMX_IN  OMX_PTR pComponentParameterStructure);

    static OMX_ERRORTYPE GetConfig(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_INDEXTYPE nIndex,
        OMX_INOUT OMX_PTR pComponentConfigStructure);
    OMX_ERRORTYPE CBaseGetConfig(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_INDEXTYPE nIndex,
        OMX_INOUT OMX_PTR pComponentConfigStructure);

    static OMX_ERRORTYPE SetConfig(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_INDEXTYPE nIndex,
        OMX_IN  OMX_PTR pComponentConfigStructure);
    OMX_ERRORTYPE CBaseSetConfig(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_INDEXTYPE nIndex,
        OMX_IN  OMX_PTR pComponentConfigStructure);

    static OMX_ERRORTYPE GetExtensionIndex(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_STRING cParameterName,
        OMX_OUT OMX_INDEXTYPE* pIndexType);
    OMX_ERRORTYPE CBaseGetExtensionIndex(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_STRING cParameterName,
        OMX_OUT OMX_INDEXTYPE* pIndexType);

    static OMX_ERRORTYPE GetState(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_OUT OMX_STATETYPE* pState);
    OMX_ERRORTYPE CBaseGetState(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_OUT OMX_STATETYPE* pState);

    static OMX_ERRORTYPE ComponentTunnelRequest(
        OMX_IN  OMX_HANDLETYPE hComp,
        OMX_IN  OMX_U32 nPort,
        OMX_IN  OMX_HANDLETYPE hTunneledComp,
        OMX_IN  OMX_U32 nTunneledPort,
        OMX_INOUT  OMX_TUNNELSETUPTYPE* pTunnelSetup);
    OMX_ERRORTYPE CBaseComponentTunnelRequest(
        OMX_IN  OMX_HANDLETYPE hComp,
        OMX_IN  OMX_U32 nPort,
        OMX_IN  OMX_HANDLETYPE hTunneledComp,
        OMX_IN  OMX_U32 nTunneledPort,
        OMX_INOUT  OMX_TUNNELSETUPTYPE* pTunnelSetup);

    static OMX_ERRORTYPE UseBuffer(
        OMX_IN OMX_HANDLETYPE hComponent,
        OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
        OMX_IN OMX_U32 nPortIndex,
        OMX_IN OMX_PTR pAppPrivate,
        OMX_IN OMX_U32 nSizeBytes,
        OMX_IN OMX_U8* pBuffer);
    OMX_ERRORTYPE CBaseUseBuffer(
        OMX_IN OMX_HANDLETYPE hComponent,
        OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
        OMX_IN OMX_U32 nPortIndex,
        OMX_IN OMX_PTR pAppPrivate,
        OMX_IN OMX_U32 nSizeBytes,
        OMX_IN OMX_U8* pBuffer);

    static OMX_ERRORTYPE AllocateBuffer(
        OMX_IN OMX_HANDLETYPE hComponent,
        OMX_INOUT OMX_BUFFERHEADERTYPE** ppBuffer,
        OMX_IN OMX_U32 nPortIndex,
        OMX_IN OMX_PTR pAppPrivate,
        OMX_IN OMX_U32 nSizeBytes);
    OMX_ERRORTYPE CBaseAllocateBuffer(
        OMX_IN OMX_HANDLETYPE hComponent,
        OMX_INOUT OMX_BUFFERHEADERTYPE** ppBuffer,
        OMX_IN OMX_U32 nPortIndex,
        OMX_IN OMX_PTR pAppPrivate,
        OMX_IN OMX_U32 nSizeBytes);

    static OMX_ERRORTYPE FreeBuffer(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_U32 nPortIndex,
        OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);
    OMX_ERRORTYPE CBaseFreeBuffer(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_U32 nPortIndex,
        OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);

    static OMX_ERRORTYPE EmptyThisBuffer(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);
    OMX_ERRORTYPE CBaseEmptyThisBuffer(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);

    static OMX_ERRORTYPE FillThisBuffer(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);
    OMX_ERRORTYPE CBaseFillThisBuffer(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);

    static OMX_ERRORTYPE SetCallbacks(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_CALLBACKTYPE* pCallbacks,
        OMX_IN  OMX_PTR pAppData);
    OMX_ERRORTYPE CBaseSetCallbacks(
        OMX_IN  OMX_HANDLETYPE hComponent,
        OMX_IN  OMX_CALLBACKTYPE* pCallbacks,
        OMX_IN  OMX_PTR pAppData);

    static OMX_ERRORTYPE ComponentDeInit(
        OMX_IN  OMX_HANDLETYPE hComponent);
    OMX_ERRORTYPE CBaseComponentDeInit(
        OMX_IN  OMX_HANDLETYPE hComponent);

    static OMX_ERRORTYPE UseEGLImage(
        OMX_IN OMX_HANDLETYPE hComponent,
        OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
        OMX_IN OMX_U32 nPortIndex,
        OMX_IN OMX_PTR pAppPrivate,
        OMX_IN void* eglImage);
    OMX_ERRORTYPE CBaseUseEGLImage(
        OMX_IN OMX_HANDLETYPE hComponent,
        OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
        OMX_IN OMX_U32 nPortIndex,
        OMX_IN OMX_PTR pAppPrivate,
        OMX_IN void* eglImage);

    static OMX_ERRORTYPE ComponentRoleEnum(
        OMX_IN OMX_HANDLETYPE hComponent,
        OMX_OUT OMX_U8 *cRole,
        OMX_IN OMX_U32 nIndex);
    OMX_ERRORTYPE CBaseComponentRoleEnum(
        OMX_IN OMX_HANDLETYPE hComponent,
        OMX_OUT OMX_U8 *cRole,
        OMX_IN OMX_U32 nIndex);

    /* buffer processing */
    /* implement WorkableInterface */
    virtual void Work(void); /* handle this->ports, hold ports_block */
    /* check if all port has own pending buffer */
    bool IsAllBufferAvailable(void);
    /* bufferwork->ScheduleWork() if IsAllBufferAvailable is true */
    void ScheduleIfAllBufferAvailable(void);

    /* component's processor */
    virtual void ComponentProcessBuffers(OMX_BUFFERHEADERTYPE **buffers,
                                         OMX_U32 nr_buffers) = 0;

    /* end of component methods & helpers */

    /*
     * omx header manipuation
     */
    static void SetTypeHeader(OMX_PTR type, OMX_U32 size);
    static OMX_ERRORTYPE CheckTypeHeader(OMX_PTR type, OMX_U32 size);

    /* end of omx header manipuation */

protected:
    /* omx standard handle */
    /* allocated at GetHandle, freed at FreeHandle */
    OMX_COMPONENTTYPE *handle;

    /* ports */
    /*
     * allocated with derived port classes by derived component classes
     */
    PortBase **ports;
    OMX_U32 nr_ports;
    OMX_PORT_PARAM_TYPE portparam;

    /* ports big lock, must be held when accessing all ports at one time */
    pthread_mutex_t ports_block;

private:
    /* common routines for constructor */
    void __ComponentBase(void);

    /*
     * core methods & helpers
     */
    /* called in GetHandle (nr_roles == 1) or SetParameter(ComponentRole) */
    OMX_ERRORTYPE SetWorkingRole(const OMX_STRING role);
    /* called in GetHandle (nr_roles == 1) or TransStateToIdle(Loaded) */
    OMX_ERRORTYPE ApplyWorkingRole(void);

    /* called in ApplyWorkingRole() */
    OMX_ERRORTYPE AllocatePorts(void);
    virtual OMX_ERRORTYPE ComponentAllocatePorts(void) = 0;
    /* called int FreeHandle() */
    OMX_ERRORTYPE FreePorts(void);

    /* end of core methods & helpers */

    /*
     * component methods & helpers
     */
    /* SendCommand */
    /* implement CmdHandlerInterface */
    virtual void CmdHandler(struct cmd_s *cmd);

    /* SendCommand:OMX_CommandStateSet */
    /* called in CmdHandler() thread context or by component itself */
    void TransState(OMX_STATETYPE transition);
    inline OMX_ERRORTYPE TransStateToLoaded(OMX_STATETYPE current);
    inline OMX_ERRORTYPE TransStateToIdle(OMX_STATETYPE current);
    inline OMX_ERRORTYPE TransStateToExecuting(OMX_STATETYPE current);
    inline OMX_ERRORTYPE TransStateToPause(OMX_STATETYPE current);
    inline OMX_ERRORTYPE TransStateToWaitForResources(OMX_STATETYPE current);
    inline OMX_ERRORTYPE TransStateToInvalid(OMX_STATETYPE current);

    /* Get/SetParameter */
    virtual OMX_ERRORTYPE
        ComponentGetParameter(OMX_INDEXTYPE nParamIndex,
                              OMX_PTR pComponentParameterStructure) = 0;
    virtual OMX_ERRORTYPE
        ComponentSetParameter(OMX_INDEXTYPE nIndex,
                              OMX_PTR pComponentParameterStructure) = 0;

    /* Get/SetConfig */
    virtual OMX_ERRORTYPE
        ComponentGetConfig(OMX_INDEXTYPE nIndex,
                           OMX_PTR pComponentConfigStructure) = 0;
    virtual OMX_ERRORTYPE
        ComponentSetConfig(OMX_INDEXTYPE nIndex,
                           OMX_PTR pComponentConfigStructure) = 0;

    /* end of component methods & helpers */

    /* process component's commands work */
    CmdProcessWork *cmdwork;

    /* buffer processing work */
    WorkQueue *bufferwork;

    /* StatePause <-> StateExecuting */
    bool executing;
    pthread_mutex_t executing_lock;
    pthread_cond_t executing_wait;

    /* roles */
    OMX_U8 **roles;
    OMX_U32 nr_roles;

    OMX_STRING working_role;

    /* component module */
    CModule *cmodule;

    OMX_STATETYPE state;

    const static OMX_STATETYPE OMX_StateUnloaded = OMX_StateVendorStartUnused;

    /* omx standard callbacks */
    OMX_PTR appdata;
    OMX_CALLBACKTYPE *callbacks;

    /* component name */
    char name[OMX_MAX_STRINGNAME_SIZE];

    /* omx specification version */
    const static OMX_U8 OMX_SPEC_VERSION_MAJOR = 1;
    const static OMX_U8 OMX_SPEC_VERSION_MINOR = 1;
    const static OMX_U8 OMX_SPEC_VERSION_REVISION = 0;
    const static OMX_U8 OMX_SPEC_VERSION_STEP = 0;

    const static OMX_U32 OMX_SPEC_VERSION = 0
        | (OMX_SPEC_VERSION_MAJOR << 24)
        | (OMX_SPEC_VERSION_MINOR << 16)
        | (OMX_SPEC_VERSION_REVISION << 8)
        | (OMX_SPEC_VERSION_STEP << 0);
};

#endif /* __COMPONENTBASE_H */