2 * testapi.c - libvlc smoke test
7 /**********************************************************************
8 * Copyright (C) 2007 RĂ©mi Denis-Courmont. *
9 * This program is free software; you can redistribute and/or modify *
10 * it under the terms of the GNU General Public License as published *
11 * by the Free Software Foundation; version 2 of the license, or (at *
12 * your option) any later version. *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
17 * See the GNU General Public License for more details. *
19 * You should have received a copy of the GNU General Public License *
20 * along with this program; if not, you can get it from: *
21 * http://www.gnu.org/copyleft/gpl.html *
22 **********************************************************************/
27 #include <vlc/libvlc.h>
36 static libvlc_exception_t ex;
38 #define log( ... ) printf( "testapi: " __VA_ARGS__ );
40 /* test if we have exception */
41 static bool have_exception (void)
43 if (libvlc_exception_raised (&ex))
45 libvlc_exception_clear (&ex);
52 static void catch (void)
54 if (libvlc_exception_raised (&ex))
56 fprintf (stderr, "Exception: %s\n",
57 libvlc_exception_get_message (&ex));
61 assert (libvlc_exception_get_message (&ex) == NULL);
62 libvlc_exception_clear (&ex);
66 static void test_core (const char ** argv, int argc);
67 static void test_media_list (const char ** argv, int argc);
68 static void test_events (const char ** argv, int argc);
69 static void test_media_player_play_stop(const char** argv, int argc);
71 /* Tests implementations */
72 static void test_core (const char ** argv, int argc)
74 libvlc_instance_t *vlc;
77 log ("Testing core\n");
79 libvlc_exception_init (&ex);
80 vlc = libvlc_new (argc, argv, &ex);
83 libvlc_playlist_clear (vlc, &ex);
86 id = libvlc_playlist_add_extended (vlc, "/dev/null", "Test", 0, NULL,
90 libvlc_playlist_clear (vlc, &ex);
98 static void test_media_list (const char ** argv, int argc)
100 libvlc_instance_t *vlc;
101 libvlc_media_t *md1, *md2, *md3, *md4;
102 libvlc_media_list_t *ml;
104 log ("Testing media_list\n");
106 libvlc_exception_init (&ex);
107 vlc = libvlc_new (argc, argv, &ex);
110 ml = libvlc_media_list_new (vlc, &ex);
113 md1 = libvlc_media_new (vlc, "/dev/null", &ex);
115 md2 = libvlc_media_new (vlc, "/dev/null", &ex);
117 md3 = libvlc_media_new (vlc, "/dev/null", &ex);
120 libvlc_media_list_add_media (ml, md1, &ex);
122 libvlc_media_list_add_media (ml, md2, &ex);
125 assert( libvlc_media_list_count (ml, &ex) == 2 );
128 assert( libvlc_media_list_index_of_item (ml, md1, &ex) == 0 );
131 assert( libvlc_media_list_index_of_item (ml, md2, &ex) == 1 );
134 libvlc_media_list_remove_index (ml, 0, &ex); /* removing first item */
137 /* test if second item was moved on first place */
138 assert( libvlc_media_list_index_of_item (ml, md2, &ex) == 0 );
141 libvlc_media_list_add_media (ml, md1, &ex); /* add 2 items */
143 libvlc_media_list_add_media (ml, md1, &ex);
146 /* there should be 3 pieces */
147 assert( libvlc_media_list_count (ml, &ex) == 3 );
150 libvlc_media_list_insert_media (ml, md3, 2, &ex);
153 /* there should be 4 pieces */
154 assert( libvlc_media_list_count (ml, &ex) == 4 );
157 /* test inserting on right place */
158 assert( libvlc_media_list_index_of_item (ml, md3, &ex) == 2 );
161 /* test right returning descriptor*/
162 assert ( libvlc_media_list_item_at_index (ml, 0, &ex) == md2 );
165 assert ( libvlc_media_list_item_at_index (ml, 2, &ex) == md3 );
168 /* test if give exceptions, when it should */
169 /* have 4 items, so index 4 should give exception */
170 libvlc_media_list_remove_index (ml, 4, &ex);
171 assert (have_exception ());
173 libvlc_media_list_remove_index (ml, 100, &ex);
174 assert (have_exception ());
176 libvlc_media_list_remove_index (ml, -1, &ex);
177 assert (have_exception ());
179 /* getting non valid items */
180 libvlc_media_t * p_non_exist =
181 libvlc_media_list_item_at_index (ml, 4, &ex);
182 assert (have_exception ());
184 p_non_exist = libvlc_media_list_item_at_index (ml, 100, &ex);
185 assert (have_exception ());
187 p_non_exist = libvlc_media_list_item_at_index (ml, -1, &ex);
188 assert (have_exception ());
190 md4 = libvlc_media_new (vlc, "/dev/dsp", &ex);
193 /* try to find non inserted item */
195 i_non_exist = libvlc_media_list_index_of_item (ml, md4, &ex);
196 assert ( i_non_exist == -1 );
198 libvlc_media_release (md1);
199 libvlc_media_release (md2);
200 libvlc_media_release (md3);
201 libvlc_media_release (md4);
203 libvlc_media_list_release (ml);
205 libvlc_release (vlc);
209 /* This one is an internal API. We use it here to run tests that
210 * don't depends on playback, and only test the event framework */
211 extern void libvlc_event_send( libvlc_event_manager_t *, libvlc_event_t *);
213 static void test_events_dummy_callback( const libvlc_event_t * event, void * user_data)
215 vlc_bool_t * callback_was_called = user_data;
216 *callback_was_called = VLC_TRUE;
219 static void test_events_callback_and_detach( const libvlc_event_t * event, void * user_data)
221 vlc_bool_t * callback_was_called = user_data;
222 libvlc_event_manager_t *em;
224 em = libvlc_media_player_event_manager (event->p_obj, &ex);
227 libvlc_event_detach (em, event->type, test_events_callback_and_detach, user_data, &ex);
228 *callback_was_called = VLC_TRUE;
231 static void test_event_type_reception( libvlc_event_manager_t * em, libvlc_event_type_t event_type, vlc_bool_t * callback_was_called )
233 libvlc_event_t event;
234 event.type = event_type;
235 *callback_was_called = VLC_FALSE;
236 libvlc_event_send (em, &event);
237 assert (*callback_was_called);
240 static void test_events (const char ** argv, int argc)
242 libvlc_instance_t *vlc;
243 libvlc_media_player_t *mi;
244 libvlc_event_manager_t *em;
245 vlc_bool_t callback_was_called;
246 libvlc_exception_t ex;
247 libvlc_event_type_t mi_events[] = {
248 libvlc_MediaPlayerPlayed,
249 libvlc_MediaPlayerPaused,
250 libvlc_MediaPlayerEndReached,
251 libvlc_MediaPlayerStopped,
252 libvlc_MediaPlayerEncounteredError,
253 libvlc_MediaPlayerTimeChanged,
254 libvlc_MediaPlayerPositionChanged,
256 int i, mi_events_len = sizeof(mi_events)/sizeof(*mi_events);
258 log ("Testing events\n");
260 libvlc_exception_init (&ex);
261 vlc = libvlc_new (argc, argv, &ex);
264 mi = libvlc_media_player_new (vlc, &ex);
267 em = libvlc_media_player_event_manager (mi, &ex);
269 log ("+ Testing attaching to Media Instance\n");
271 for (i = 0; i < mi_events_len; i++) {
272 libvlc_event_attach (em, mi_events[i], test_events_dummy_callback, &callback_was_called, &ex);
276 log ("+ Testing event reception\n");
278 for (i = 0; i < mi_events_len; i++)
279 test_event_type_reception (em, mi_events[i], &callback_was_called);
281 log ("+ Testing event detaching while in the event callback\n");
283 libvlc_event_t event;
284 event.type = mi_events[mi_events_len-1];
285 callback_was_called = VLC_FALSE;
287 libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_dummy_callback, &callback_was_called, &ex);
290 libvlc_event_attach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
293 libvlc_event_send (em, &event);
294 assert( callback_was_called );
296 callback_was_called = VLC_FALSE;
297 libvlc_event_send (em, &event);
298 assert( !callback_was_called );
300 libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
303 log ("+ Testing regular detach()\n");
305 for (i = 0; i < mi_events_len - 1; i++) {
306 libvlc_event_detach (em, mi_events[i], test_events_dummy_callback, &callback_was_called, &ex);
310 libvlc_media_player_release (mi);
313 libvlc_release (vlc);
317 static void test_media_player_play_stop(const char** argv, int argc)
319 libvlc_instance_t *vlc;
321 libvlc_media_player_t *mi;
322 const char * file = "file://../bindings/java/core/src/test/resources/raffa_voice.ogg";
324 log ("Testing play and pause of %s\n", file);
326 libvlc_exception_init (&ex);
327 vlc = libvlc_new (argc, argv, &ex);
330 md = libvlc_media_new (vlc, file, &ex);
333 mi = libvlc_media_player_new_from_media (md, &ex);
336 libvlc_media_release (md);
338 libvlc_media_player_play (mi, &ex);
341 /* FIXME: Do something clever */
344 assert( libvlc_media_player_get_state (mi, &ex) != libvlc_Error );
347 libvlc_media_player_stop (mi, &ex);
350 libvlc_media_player_release (mi);
353 libvlc_release (vlc);
357 int main (int argc, char *argv[])
359 const char *args[argc + 5];
360 int nlibvlc_args = sizeof (args) / sizeof (args[0]);
362 alarm (50); /* Make sure "make check" does not get stuck */
367 args[3] = "--plugin-path=../modules";
368 args[4] = "--vout=dummy";
369 args[5] = "--aout=dummy";
370 for (int i = 1; i < argc; i++)
371 args[i + 3] = argv[i];
373 test_core (args, nlibvlc_args);
375 test_events (args, nlibvlc_args);
377 test_media_list (args, nlibvlc_args);
379 test_media_player_play_stop(args, nlibvlc_args);