portbase.h revision 37787374d75727678e1f9d19191fdad363ee6c54
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 __PORTBASE_H
10#define __PORTBASE_H
11
12#include <pthread.h>
13
14#include <OMX_Core.h>
15#include <OMX_Component.h>
16
17#include <list.h>
18#include <queue.h>
19
20class PortBase
21{
22public:
23    /*
24     * constructor & destructor
25     */
26    PortBase();
27    PortBase(const OMX_PARAM_PORTDEFINITIONTYPE *portdefinition);
28    virtual ~PortBase();
29
30    /* end of constructor & destructor */
31
32    /*
33     * accessor
34     */
35    /* owner */
36    void SetOwner(OMX_COMPONENTTYPE *handle);
37    OMX_COMPONENTTYPE *GetOwner(void);
38
39    /* for ReturnThisBuffer() */
40    OMX_ERRORTYPE SetCallbacks(OMX_HANDLETYPE hComponent,
41                               OMX_CALLBACKTYPE *pCallbacks,
42                               OMX_PTR pAppData);
43    /* end of accessor */
44
45    /*
46     * component methods & helpers
47     */
48    /* Get/SetParameter */
49    OMX_ERRORTYPE SetPortDefinition(const OMX_PARAM_PORTDEFINITIONTYPE *p,
50                                    bool overwrite_readonly);
51    const OMX_PARAM_PORTDEFINITIONTYPE *GetPortDefinition(void);
52
53    /* Use/Allocate/FreeBuffer */
54    OMX_ERRORTYPE UseBuffer(OMX_BUFFERHEADERTYPE **ppBufferHdr,
55                            OMX_U32 nPortIndex,
56                            OMX_PTR pAppPrivate,
57                            OMX_U32 nSizeBytes,
58                            OMX_U8 *pBuffer);
59    OMX_ERRORTYPE AllocateBuffer(OMX_BUFFERHEADERTYPE **ppBuffer,
60                                 OMX_U32 nPortIndex,
61                                 OMX_PTR pAppPrivate,
62                                 OMX_U32 nSizeBytes);
63    OMX_ERRORTYPE FreeBuffer(OMX_U32 nPortIndex,
64                             OMX_BUFFERHEADERTYPE *pBuffer);
65
66    /*
67     * called in ComponentBase::TransStateToLoaded(OMX_StateIdle) or
68     * in ComponentBase::TransStateToIdle(OMX_StateLoaded)
69     * wokeup by Use/Allocate/FreeBuffer
70     */
71    void WaitPortBufferCompletion(void);
72
73    /* Empty/FillThisBuffer */
74    OMX_ERRORTYPE PushThisBuffer(OMX_BUFFERHEADERTYPE *pBuffer);
75    OMX_BUFFERHEADERTYPE *PopBuffer(void);
76    OMX_U32 BufferQueueLength(void);
77
78    /* Empty/FillBufferDone */
79    OMX_ERRORTYPE ReturnThisBuffer(OMX_BUFFERHEADERTYPE *pBuffer);
80
81    /* retain buffer */
82    OMX_ERRORTYPE RetainThisBuffer(OMX_BUFFERHEADERTYPE *pBuffer,
83                                   bool accumulate);
84    /*
85     * components have responsibilty of calling this function to return all
86     * accumulated buffers to omx-il clients.
87     */
88    void ReturnAllRetainedBuffers(void);
89
90    /* flush all buffers not under processing */
91    OMX_ERRORTYPE FlushPort(void);
92
93    bool IsEnabled(void);
94
95    OMX_DIRTYPE GetPortDirection(void);
96    OMX_U32 GetPortBufferCount(void);
97
98    OMX_ERRORTYPE PushMark(OMX_MARKTYPE *mark);
99    OMX_MARKTYPE *PopMark(void);
100
101    /* SendCommand(OMX_CommandPortDisable/Enable) */
102    OMX_ERRORTYPE TransState(OMX_U8 state);
103
104    /* end of component methods & helpers */
105
106    /* TransState, state */
107    static const OMX_U8 OMX_PortDisabled = 0;
108    static const OMX_U8 OMX_PortEnabled = 1;
109
110private:
111    /* common routines for constructor */
112    void __PortBase(void);
113
114    /*
115     * component methods & helpers
116     */
117    OMX_STATETYPE GetOwnerState(void);
118
119    /* end of component methods & helpers */
120
121    /* buffer headers */
122    struct list *buffer_hdrs;
123    OMX_U32 nr_buffer_hdrs;
124    bool buffer_hdrs_completion; /* Use/Allocate/FreeBuffer completion flag */
125    pthread_mutex_t hdrs_lock;
126    pthread_cond_t hdrs_wait;
127
128    struct queue bufferq;
129    pthread_mutex_t bufferq_lock;
130
131    /* retained buffers (only accumulated buffer) */
132    struct queue retainedbufferq;
133    pthread_mutex_t retainedbufferq_lock;
134
135    struct queue markq;
136    pthread_mutex_t markq_lock;
137
138    /* state */
139    OMX_U8 state;
140    pthread_mutex_t state_lock;
141
142    /* parameter */
143    OMX_PARAM_PORTDEFINITIONTYPE portdefinition;
144    /* room for portdefinition.format.*.cMIMEType */
145    char definition_format_mimetype[OMX_MAX_STRINGNAME_SIZE];
146
147    OMX_AUDIO_PARAM_PORTFORMATTYPE audioparam;
148
149    /* owner handle */
150    OMX_COMPONENTTYPE *owner;
151
152    /* omx standard callbacks */
153    OMX_PTR appdata;
154    OMX_CALLBACKTYPE *callbacks;
155};
156
157/* end of PortBase */
158
159#endif /* __PORTBASE_H */
160