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