aboutsummaryrefslogtreecommitdiff
path: root/base/inc/componentbase.h
blob: 3310f682a3b1a184ecdf7185d68cb14537694a27 (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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
 * componentbase.h, component base class
 *
 * Copyright (c) 2009-2010 Wind River Systems, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __COMPONENTBASE_H
#define __COMPONENTBASE_H

#include <OMX_Core.h>
#include <OMX_Component.h>
#include <OMX_IntelIndexExt.h>
#include <cmodule.h>
#include <portbase.h>

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

/* retain buffers */
typedef enum buffer_retain_e {
    BUFFER_RETAIN_NOT_RETAIN = 0,
    BUFFER_RETAIN_GETAGAIN,
    BUFFER_RETAIN_ACCUMULATE,
    BUFFER_RETAIN_OVERRIDDEN,
    BUFFER_RETAIN_CACHE,
} buffer_retain_t;

/* 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() */

    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);
    OMX_STRING GetName(void);

    /* working role */
    OMX_STRING GetWorkingRole(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);

    /* 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 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 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 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);

    /* end of component methods & helpers */

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

    /* end of omx header manipuation */

    /*
     * helper method for queury_roles()
     */
    static OMX_ERRORTYPE QueryRolesHelper(OMX_U32 nr_comp_roles,
                                          const OMX_U8 **comp_roles,
                                          OMX_U32 *nr_roles, OMX_U8 **roles);

    /* end of helper method for queury_roles() */

protected:
    /* helpers for derived class */
    const OMX_COMPONENTTYPE *GetComponentHandle(void);
#if 0
    void DumpBuffer(const OMX_BUFFERHEADERTYPE *bufferheader, bool dumpdata);
#endif
    /* check if all port has own pending buffer */
    virtual bool IsAllBufferAvailable(void);

    /* end of helpers for derived class */

    /* 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;

    /* adaptive playback param */
    OMX_BOOL mEnableAdaptivePlayback;
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 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);

    /* called in TransStateToIdle(Loaded) */
    OMX_ERRORTYPE ApplyWorkingRole(void);
    /* called in ApplyWorkingRole() */
    OMX_ERRORTYPE AllocatePorts(void);
    /* allocate specific port type derived from portbase */
    virtual OMX_ERRORTYPE ComponentAllocatePorts(void) = 0;

    /* SendCommand:OMX_CommandMarkBuffer */
    /* called in CmdHandler() thread context */
    void PushThisMark(OMX_U32 portindex, OMX_MARKTYPE *mark);
    /* SendCommand:OMX_CommandFlush (notify:1) or other parts (notify:0) */
    void FlushPort(OMX_U32 port_index, bool notify);
    /* SendCommand:OMX_CommandPortDisable/Enable */
    /* state: PortBase::OMX_PortEnabled/Disabled */
    void TransStatePort(OMX_U32 port_index, OMX_U8 state);

    /* 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;

    /* buffer processing */
    /* implement WorkableInterface */
    virtual void Work(void); /* handle this->ports, hold ports_block */

    /* called in Work() after ProcessorProcess() */
    void PostProcessBuffers(OMX_BUFFERHEADERTYPE ***buffers,
                            const buffer_retain_t *retain);
    void SourcePostProcessBuffers(OMX_BUFFERHEADERTYPE ***buffers);
    void FilterPostProcessBuffers(OMX_BUFFERHEADERTYPE ***buffers,
                                  const buffer_retain_t *retain);
    void SinkPostProcessBuffers();

    /* processor callbacks */
    /* TransState */
    virtual OMX_ERRORTYPE ProcessorInit(void);  /* Loaded to Idle */
    virtual OMX_ERRORTYPE ProcessorDeinit(void);/* Idle to Loaded */
    virtual OMX_ERRORTYPE ProcessorStart(void); /* Idle to Executing/Pause */
    virtual OMX_ERRORTYPE ProcessorReset(void); /* Reset */
    virtual OMX_ERRORTYPE ProcessorStop(void);  /* Executing/Pause to Idle */
    virtual OMX_ERRORTYPE ProcessorPause(void); /* Executing to Pause */
    virtual OMX_ERRORTYPE ProcessorResume(void);/* Pause to Executing */
    virtual OMX_ERRORTYPE ProcessorFlush(OMX_U32 port_index); /* Flush */
    virtual OMX_ERRORTYPE ProcessorPreFillBuffer(OMX_BUFFERHEADERTYPE* buffer);
    virtual OMX_ERRORTYPE ProcessorPreEmptyBuffer(OMX_BUFFERHEADERTYPE* buffer);

    /* invoked when buffer is to be freed */
    virtual OMX_ERRORTYPE ProcessorPreFreeBuffer(OMX_U32 nPortIndex, OMX_BUFFERHEADERTYPE* pBuffer);
    /* Work */
    virtual OMX_ERRORTYPE ProcessorProcess(OMX_BUFFERHEADERTYPE ***pBuffers,
                                           buffer_retain_t *retain,
                                           OMX_U32 nr_buffers);
    virtual OMX_ERRORTYPE ProcessorProcess(OMX_BUFFERHEADERTYPE **pBuffers,
                                           buffer_retain_t *retain,
                                           OMX_U32 nr_buffers);

    /* end of component methods & helpers */

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

    /* buffer processing work */
    WorkQueue *bufferwork;

    /* component variant */
    typedef enum component_variant_e {
        CVARIANT_NULL = 0,
        CVARIANT_SOURCE,
        CVARIANT_FILTER,
        CVARIANT_SINK,
    } component_variant_t;

    component_variant_t cvariant;

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

    OMX_STRING working_role;

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

    /* 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];

    /* state lock */
    pthread_mutex_t state_block;

    OMX_U32 mMaxFrameWidth;
    OMX_U32 mMaxFrameHeight;

    /* omx specification version */
#ifndef ANDROID
    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 = 2;
    const static OMX_U8 OMX_SPEC_VERSION_STEP = 0;
#else
    const static OMX_U8 OMX_SPEC_VERSION_MAJOR = 1;
    const static OMX_U8 OMX_SPEC_VERSION_MINOR = 0;
    const static OMX_U8 OMX_SPEC_VERSION_REVISION = 0;
    const static OMX_U8 OMX_SPEC_VERSION_STEP = 0;
#endif

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

#endif /* __COMPONENTBASE_H */