Initialize Tizen 2.3
[framework/multimedia/gst-openmax.git] / mobile / tests / check_libomxil.c
1 /*
2  * Copyright (C) 2008-2009 Nokia Corporation.
3  *
4  * Author: Felipe Contreras <felipe.contreras@nokia.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation
9  * version 2.1 of the License.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #include <check.h>
23 #include <OMX_Core.h>
24 #include <OMX_Component.h>
25
26 #include <glib.h>
27 #include <dlfcn.h>
28
29 static const char *lib_name;
30 static void *dl_handle;
31 static OMX_ERRORTYPE (*init) (void);
32 static OMX_ERRORTYPE (*deinit) (void);
33 static OMX_ERRORTYPE (*get_handle) (OMX_HANDLETYPE * handle,
34     OMX_STRING name, OMX_PTR data, OMX_CALLBACKTYPE * callbacks);
35 static OMX_ERRORTYPE (*free_handle) (OMX_HANDLETYPE handle);
36
37 typedef struct CustomData CustomData;
38
39 struct CustomData
40 {
41   OMX_HANDLETYPE omx_handle;
42   OMX_STATETYPE omx_state;
43   GCond *omx_state_condition;
44   GMutex *omx_state_mutex;
45 };
46
47 static CustomData *
48 custom_data_new (void)
49 {
50   CustomData *custom_data;
51   custom_data = g_new0 (CustomData, 1);
52   custom_data->omx_state_condition = g_cond_new ();
53   custom_data->omx_state_mutex = g_mutex_new ();
54   return custom_data;
55 }
56
57 static void
58 custom_data_free (CustomData * custom_data)
59 {
60   g_mutex_free (custom_data->omx_state_mutex);
61   g_cond_free (custom_data->omx_state_condition);
62   g_free (custom_data);
63 }
64
65 static inline void
66 change_state (CustomData * core, OMX_STATETYPE state)
67 {
68   fail_if (OMX_SendCommand (core->omx_handle, OMX_CommandStateSet, state,
69           NULL) != OMX_ErrorNone);
70 }
71
72 static inline void
73 complete_change_state (CustomData * core, OMX_STATETYPE state)
74 {
75   g_mutex_lock (core->omx_state_mutex);
76
77   core->omx_state = state;
78   g_cond_signal (core->omx_state_condition);
79
80   g_mutex_unlock (core->omx_state_mutex);
81 }
82
83 static inline void
84 wait_for_state (CustomData * core, OMX_STATETYPE state)
85 {
86   g_mutex_lock (core->omx_state_mutex);
87
88   while (core->omx_state != state)
89     g_cond_wait (core->omx_state_condition, core->omx_state_mutex);
90
91   g_mutex_unlock (core->omx_state_mutex);
92 }
93
94 static OMX_ERRORTYPE
95 EventHandler (OMX_HANDLETYPE omx_handle,
96     OMX_PTR app_data,
97     OMX_EVENTTYPE event, OMX_U32 data_1, OMX_U32 data_2, OMX_PTR event_data)
98 {
99   CustomData *core;
100
101   core = app_data;
102
103   switch (event) {
104     case OMX_EventCmdComplete:
105     {
106       OMX_COMMANDTYPE cmd;
107
108       cmd = (OMX_COMMANDTYPE) data_1;
109
110       switch (cmd) {
111         case OMX_CommandStateSet:
112           complete_change_state (core, data_2);
113           break;
114         default:
115           break;
116       }
117       break;
118     }
119     default:
120       break;
121   }
122
123   return OMX_ErrorNone;
124 }
125
126 static OMX_CALLBACKTYPE callbacks = { EventHandler, NULL, NULL };
127
128 START_TEST (test_basic)
129 {
130   OMX_ERRORTYPE omx_error;
131   omx_error = init ();
132   fail_if (omx_error != OMX_ErrorNone);
133   omx_error = deinit ();
134   fail_if (omx_error != OMX_ErrorNone);
135 }
136
137 END_TEST
138 START_TEST (test_handle)
139 {
140   OMX_ERRORTYPE omx_error;
141   OMX_HANDLETYPE omx_handle;
142
143   omx_error = init ();
144   fail_if (omx_error != OMX_ErrorNone);
145
146   omx_error = get_handle (&omx_handle, "OMX.check.dummy", NULL, NULL);
147   fail_if (omx_error != OMX_ErrorNone);
148
149   omx_error = free_handle (omx_handle);
150   fail_if (omx_error != OMX_ErrorNone);
151
152   omx_error = deinit ();
153   fail_if (omx_error != OMX_ErrorNone);
154 }
155
156 END_TEST
157 START_TEST (test_idle)
158 {
159   CustomData *custom_data;
160   OMX_ERRORTYPE omx_error;
161   OMX_HANDLETYPE omx_handle;
162
163   custom_data = custom_data_new ();
164
165   omx_error = init ();
166   fail_if (omx_error != OMX_ErrorNone);
167
168   omx_error =
169       get_handle (&omx_handle, "OMX.check.dummy", custom_data, &callbacks);
170   fail_if (omx_error != OMX_ErrorNone);
171
172   custom_data->omx_handle = omx_handle;
173
174   change_state (custom_data, OMX_StateIdle);
175
176   /* allocate_buffers */
177
178   wait_for_state (custom_data, OMX_StateIdle);
179
180   change_state (custom_data, OMX_StateLoaded);
181
182   /* free_buffers */
183
184   wait_for_state (custom_data, OMX_StateLoaded);
185
186   omx_error = free_handle (omx_handle);
187   fail_if (omx_error != OMX_ErrorNone);
188
189   omx_error = deinit ();
190   fail_if (omx_error != OMX_ErrorNone);
191
192   custom_data_free (custom_data);
193 }
194
195 END_TEST static Suite *
196 util_suite (void)
197 {
198   Suite *s = suite_create ("libomxil");
199   TCase *tc_chain = tcase_create ("general");
200
201   lib_name = "libomxil-foo.so";
202
203   if (!g_thread_supported ())
204     g_thread_init (NULL);
205
206   {
207     dl_handle = dlopen (lib_name, RTLD_LAZY);
208     if (!dl_handle) {
209             /** @todo report error. */
210     }
211
212     init = dlsym (dl_handle, "OMX_Init");
213     deinit = dlsym (dl_handle, "OMX_Deinit");
214     get_handle = dlsym (dl_handle, "OMX_GetHandle");
215     free_handle = dlsym (dl_handle, "OMX_FreeHandle");
216   }
217
218   tcase_add_test (tc_chain, test_basic);
219   tcase_add_test (tc_chain, test_handle);
220   tcase_add_test (tc_chain, test_idle);
221   suite_add_tcase (s, tc_chain);
222
223   return s;
224 }
225
226 int
227 main (void)
228 {
229   int number_failed;
230   Suite *s;
231   SRunner *sr;
232
233   s = util_suite ();
234   sr = srunner_create (s);
235   srunner_run_all (sr, CK_NORMAL);
236   number_failed = srunner_ntests_failed (sr);
237   srunner_free (sr);
238
239   return (number_failed == 0) ? 0 : 1;
240 }