Avoid crashing when OMX failed to initialize
[gstreamer-omap:gst-openmax.git] / omx / gstomx_core.h
1 /*
2  * Copyright (C) 2006-2009 Texas Instruments, Incorporated
3  * Copyright (C) 2007-2009 Nokia Corporation.
4  *
5  * Author: Felipe Contreras <felipe.contreras@nokia.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation
10  * version 2.1 of the License.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifndef GSTOMX_CORE_H
24 #define GSTOMX_CORE_H
25
26 #include "gstomx_util.h"
27
28 G_BEGIN_DECLS
29
30 /* Typedefs. */
31
32 typedef void (*GOmxCb) (GOmxCore *core);
33 typedef void (*GOmxCbargs2) (GOmxCore *core, gint data1, gint data2);
34
35 /* Structures. */
36
37 struct GOmxCore
38 {
39     gpointer object; /**< GStreamer element. */
40
41     OMX_HANDLETYPE omx_handle;
42     OMX_ERRORTYPE omx_error;
43
44     OMX_STATETYPE omx_state;
45     GCond *omx_state_condition;
46     GMutex *omx_state_mutex;
47
48     GPtrArray *ports;
49
50     GSem *done_sem;
51     GSem *flush_sem;
52     GSem *port_sem;
53
54     GOmxCb settings_changed_cb;
55     GOmxCbargs2 index_settings_changed_cb;
56
57     GOmxImp *imp;
58
59     gboolean done;
60
61     gboolean use_timestamps; /** @todo remove; timestamps should always be used */
62 };
63
64 /* Utility Macros */
65
66 #define _G_OMX_INIT_PARAM(param) G_STMT_START {  /* util for other macros */  \
67         memset ((param), 0, sizeof (*(param)));                               \
68         (param)->nSize = sizeof (*(param));                                   \
69         (param)->nVersion.s.nVersionMajor = 1;                                \
70         (param)->nVersion.s.nVersionMinor = 1;                                \
71     } G_STMT_END
72
73 #define G_OMX_CORE_GET_PARAM(core, idx, param) G_STMT_START {                 \
74         OMX_HANDLETYPE handle = g_omx_core_get_handle (core);                 \
75         if (handle) {                                                         \
76           _G_OMX_INIT_PARAM (param);                                          \
77           OMX_GetParameter (handle, (idx), (param));                          \
78         }                                                                     \
79     } G_STMT_END
80
81 #ifdef __GNUC__
82 #define G_OMX_CORE_SET_PARAM(core, idx, param) ({                             \
83         OMX_ERRORTYPE err;                                                    \
84         OMX_HANDLETYPE handle = g_omx_core_get_handle (core);                 \
85         if (handle) {                                                         \
86           GST_DEBUG ("Setting OMX parameter " #idx " to " #param);            \
87           err = OMX_SetParameter (handle, (idx), (param));                    \
88           if (err != OMX_ErrorNone)                                           \
89               GST_DEBUG ("Error setting " #idx " to " #param ": %x",          \
90                   err, g_omx_error_to_str (err));                             \
91           g_warn_if_fail (err == OMX_ErrorNone);                              \
92         } else {                                                              \
93           GST_DEBUG ("Cannot set OMX parameter " #idx " to " #param);         \
94           err = OMX_ErrorNotReady;                                            \
95         }                                                                     \
96         err;})
97 #else
98 #define G_OMX_CORE_SET_PARAM(core, idx, param) G_STMT_START {                 \
99         OMX_ERRORTYPE err;                                                    \
100         OMX_HANDLETYPE handle = g_omx_core_get_handle (core);                 \
101         if (handle) {                                                         \
102           GST_DEBUG ("Setting OMX parameter " #idx " to " #param);            \
103           err = OMX_SetParameter (handle, (idx), (param));                    \
104           if (err != OMX_ErrorNone)                                           \
105               GST_DEBUG ("Error setting " #idx " to " #param ": %x",          \
106                   err, g_omx_error_to_str (str));                             \
107           g_warn_if_fail (err == OMX_ErrorNone);                              \
108         } else {                                                              \
109           GST_DEBUG ("Cannot set OMX parameter " #idx " to " #param);         \
110         }                                                                     \
111     } G_STMT_END
112 #endif
113
114 #define G_OMX_CORE_GET_CONFIG(core, idx, param) G_STMT_START {                \
115         OMX_HANDLETYPE handle = g_omx_core_get_handle (core);                 \
116         _G_OMX_INIT_PARAM (param);                                            \
117         if (handle) {                                                         \
118           OMX_GetConfig (handle, (idx), (param));                             \
119         } else {                                                              \
120           GST_DEBUG ("Cannot get OMX config " #idx);                          \
121         }                                                                     \
122     } G_STMT_END
123
124 #ifdef __GNUC__
125 #define G_OMX_CORE_SET_CONFIG(core, idx, param) ({                            \
126         OMX_ERRORTYPE err;                                                    \
127         OMX_HANDLETYPE handle = g_omx_core_get_handle (core);                 \
128         if (handle) {                                                         \
129           GST_DEBUG ("Setting OMX config " #idx " to " #param);               \
130           err = OMX_SetConfig (handle, (idx), (param));                       \
131           if (err != OMX_ErrorNone)                                           \
132               GST_DEBUG ("Error setting " #idx " to " #param ": %x",          \
133                   err, g_omx_error_to_str (err));                             \
134           g_warn_if_fail (err == OMX_ErrorNone);                              \
135         } else {                                                              \
136           GST_DEBUG ("Cannot set OMX config " #idx " to " #param);            \
137           err = OMX_ErrorNotReady;                                            \
138         }                                                                     \
139         err;})
140 #else
141 #define G_OMX_CORE_SET_CONFIG(core, idx, param) G_STMT_START {                \
142         OMX_ERRORTYPE err;                                                    \
143         OMX_HANDLETYPE handle = g_omx_core_get_handle (core);                 \
144         if (handle) {                                                         \
145           GST_DEBUG ("Setting OMX config " #idx " to " #param);               \
146           err = OMX_SetConfig (handle, (idx), (param));                       \
147           if (err != OMX_ErrorNone)                                           \
148               GST_DEBUG ("Error setting " #idx " to " #param ": %x",          \
149                   err, g_omx_error_to_str (err));                             \
150           g_warn_if_fail (err == OMX_ErrorNone);                              \
151         } else {                                                              \
152           GST_DEBUG ("Cannot set OMX config " #idx " to " #param);            \
153         }                                                                     \
154     } G_STMT_END
155 #endif
156
157
158 /* Functions. */
159
160 GOmxCore *g_omx_core_new (gpointer object, gpointer klass);
161 void g_omx_core_free (GOmxCore *core);
162 void g_omx_core_init (GOmxCore *core);
163 void g_omx_core_deinit (GOmxCore *core);
164 void g_omx_core_prepare (GOmxCore *core);
165 void g_omx_core_start (GOmxCore *core);
166 void g_omx_core_pause (GOmxCore *core);
167 void g_omx_core_stop (GOmxCore *core);
168 void g_omx_core_unload (GOmxCore *core);
169 void g_omx_core_set_done (GOmxCore *core);
170 void g_omx_core_wait_for_done (GOmxCore *core);
171 void g_omx_core_flush_start (GOmxCore *core);
172 void g_omx_core_flush_stop (GOmxCore *core);
173 OMX_HANDLETYPE g_omx_core_get_handle (GOmxCore *core);
174 GOmxPort *g_omx_core_get_port (GOmxCore *core, const gchar *name, guint index);
175
176 /* Friend:  helpers used by GOmxPort */
177 void g_omx_core_got_buffer (GOmxCore *core,
178         GOmxPort *port,
179         OMX_BUFFERHEADERTYPE *omx_buffer);
180
181 G_END_DECLS
182
183
184 #endif /* GSTOMX_CORE_H */