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);
70 static void test_media_player_pause_stop(const char** argv, int argc);
71 static void test_media_list_player_pause_stop(const char** argv, int argc);
73 /* Tests implementations */
74 static void test_core (const char ** argv, int argc)
76 libvlc_instance_t *vlc;
79 log ("Testing core\n");
81 libvlc_exception_init (&ex);
82 vlc = libvlc_new (argc, argv, &ex);
85 libvlc_playlist_clear (vlc, &ex);
88 id = libvlc_playlist_add_extended (vlc, "/dev/null", "Test", 0, NULL,
92 libvlc_playlist_clear (vlc, &ex);
100 static void test_media_list (const char ** argv, int argc)
102 libvlc_instance_t *vlc;
103 libvlc_media_t *md1, *md2, *md3, *md4;
104 libvlc_media_list_t *ml;
106 log ("Testing media_list\n");
108 libvlc_exception_init (&ex);
109 vlc = libvlc_new (argc, argv, &ex);
112 ml = libvlc_media_list_new (vlc, &ex);
115 md1 = libvlc_media_new (vlc, "/dev/null", &ex);
117 md2 = libvlc_media_new (vlc, "/dev/null", &ex);
119 md3 = libvlc_media_new (vlc, "/dev/null", &ex);
122 libvlc_media_list_add_media (ml, md1, &ex);
124 libvlc_media_list_add_media (ml, md2, &ex);
127 assert( libvlc_media_list_count (ml, &ex) == 2 );
130 assert( libvlc_media_list_index_of_item (ml, md1, &ex) == 0 );
133 assert( libvlc_media_list_index_of_item (ml, md2, &ex) == 1 );
136 libvlc_media_list_remove_index (ml, 0, &ex); /* removing first item */
139 /* test if second item was moved on first place */
140 assert( libvlc_media_list_index_of_item (ml, md2, &ex) == 0 );
143 libvlc_media_list_add_media (ml, md1, &ex); /* add 2 items */
145 libvlc_media_list_add_media (ml, md1, &ex);
148 /* there should be 3 pieces */
149 assert( libvlc_media_list_count (ml, &ex) == 3 );
152 libvlc_media_list_insert_media (ml, md3, 2, &ex);
155 /* there should be 4 pieces */
156 assert( libvlc_media_list_count (ml, &ex) == 4 );
159 /* test inserting on right place */
160 assert( libvlc_media_list_index_of_item (ml, md3, &ex) == 2 );
163 /* test right returning descriptor*/
164 assert ( libvlc_media_list_item_at_index (ml, 0, &ex) == md2 );
167 assert ( libvlc_media_list_item_at_index (ml, 2, &ex) == md3 );
170 /* test if give exceptions, when it should */
171 /* have 4 items, so index 4 should give exception */
172 libvlc_media_list_remove_index (ml, 4, &ex);
173 assert (have_exception ());
175 libvlc_media_list_remove_index (ml, 100, &ex);
176 assert (have_exception ());
178 libvlc_media_list_remove_index (ml, -1, &ex);
179 assert (have_exception ());
181 /* getting non valid items */
182 libvlc_media_t * p_non_exist =
183 libvlc_media_list_item_at_index (ml, 4, &ex);
184 assert (have_exception ());
186 p_non_exist = libvlc_media_list_item_at_index (ml, 100, &ex);
187 assert (have_exception ());
189 p_non_exist = libvlc_media_list_item_at_index (ml, -1, &ex);
190 assert (have_exception ());
192 md4 = libvlc_media_new (vlc, "/dev/dsp", &ex);
195 /* try to find non inserted item */
197 i_non_exist = libvlc_media_list_index_of_item (ml, md4, &ex);
198 assert ( i_non_exist == -1 );
200 libvlc_media_release (md1);
201 libvlc_media_release (md2);
202 libvlc_media_release (md3);
203 libvlc_media_release (md4);
205 libvlc_media_list_release (ml);
207 libvlc_release (vlc);
211 /* This one is an internal API. We use it here to run tests that
212 * don't depends on playback, and only test the event framework */
213 extern void libvlc_event_send( libvlc_event_manager_t *, libvlc_event_t *);
215 static void test_events_dummy_callback( const libvlc_event_t * event, void * user_data)
217 vlc_bool_t * callback_was_called = user_data;
218 *callback_was_called = VLC_TRUE;
221 static void test_events_callback_and_detach( const libvlc_event_t * event, void * user_data)
223 vlc_bool_t * callback_was_called = user_data;
224 libvlc_event_manager_t *em;
226 em = libvlc_media_player_event_manager (event->p_obj, &ex);
229 libvlc_event_detach (em, event->type, test_events_callback_and_detach, user_data, &ex);
230 *callback_was_called = VLC_TRUE;
233 static void test_event_type_reception( libvlc_event_manager_t * em, libvlc_event_type_t event_type, vlc_bool_t * callback_was_called )
235 libvlc_event_t event;
236 event.type = event_type;
237 *callback_was_called = VLC_FALSE;
238 libvlc_event_send (em, &event);
239 assert (*callback_was_called);
242 static void test_events (const char ** argv, int argc)
244 libvlc_instance_t *vlc;
245 libvlc_media_player_t *mi;
246 libvlc_event_manager_t *em;
247 vlc_bool_t callback_was_called;
248 libvlc_exception_t ex;
249 libvlc_event_type_t mi_events[] = {
250 libvlc_MediaPlayerPlayed,
251 libvlc_MediaPlayerPaused,
252 libvlc_MediaPlayerEndReached,
253 libvlc_MediaPlayerStopped,
254 libvlc_MediaPlayerEncounteredError,
255 libvlc_MediaPlayerTimeChanged,
256 libvlc_MediaPlayerPositionChanged,
258 int i, mi_events_len = sizeof(mi_events)/sizeof(*mi_events);
260 log ("Testing events\n");
262 libvlc_exception_init (&ex);
263 vlc = libvlc_new (argc, argv, &ex);
266 mi = libvlc_media_player_new (vlc, &ex);
269 em = libvlc_media_player_event_manager (mi, &ex);
271 log ("+ Testing attaching to Media Instance\n");
273 for (i = 0; i < mi_events_len; i++) {
274 libvlc_event_attach (em, mi_events[i], test_events_dummy_callback, &callback_was_called, &ex);
278 log ("+ Testing event reception\n");
280 for (i = 0; i < mi_events_len; i++)
281 test_event_type_reception (em, mi_events[i], &callback_was_called);
283 log ("+ Testing event detaching while in the event callback\n");
285 libvlc_event_t event;
286 event.type = mi_events[mi_events_len-1];
287 callback_was_called = VLC_FALSE;
289 libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_dummy_callback, &callback_was_called, &ex);
292 libvlc_event_attach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
295 libvlc_event_send (em, &event);
296 assert( callback_was_called );
298 callback_was_called = VLC_FALSE;
299 libvlc_event_send (em, &event);
300 assert( !callback_was_called );
302 libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
305 log ("+ Testing regular detach()\n");
307 for (i = 0; i < mi_events_len - 1; i++) {
308 libvlc_event_detach (em, mi_events[i], test_events_dummy_callback, &callback_was_called, &ex);
312 libvlc_media_player_release (mi);
315 libvlc_release (vlc);
319 static void test_media_player_play_stop(const char** argv, int argc)
321 libvlc_instance_t *vlc;
323 libvlc_media_player_t *mi;
324 const char * file = "file://../bindings/java/core/src/test/resources/raffa_voice.ogg";
326 log ("Testing play and pause of %s\n", file);
328 libvlc_exception_init (&ex);
329 vlc = libvlc_new (argc, argv, &ex);
332 md = libvlc_media_new (vlc, file, &ex);
335 mi = libvlc_media_player_new_from_media (md, &ex);
338 libvlc_media_release (md);
340 libvlc_media_player_play (mi, &ex);
343 /* FIXME: Do something clever */
346 assert( libvlc_media_player_get_state (mi, &ex) != libvlc_Error );
349 libvlc_media_player_stop (mi, &ex);
352 libvlc_media_player_release (mi);
355 libvlc_release (vlc);
359 static void test_media_player_pause_stop(const char** argv, int argc)
361 libvlc_instance_t *vlc;
363 libvlc_media_player_t *mi;
364 const char * file = "file://../bindings/java/core/src/test/resources/raffa_voice.ogg";
366 log ("Testing play and pause of %s\n", file);
368 libvlc_exception_init (&ex);
369 vlc = libvlc_new (argc, argv, &ex);
372 md = libvlc_media_new (vlc, file, &ex);
375 mi = libvlc_media_player_new_from_media (md, &ex);
378 libvlc_media_release (md);
380 libvlc_media_player_play (mi, &ex);
383 /* FIXME: Do something clever */
386 assert( libvlc_media_player_get_state (mi, &ex) == libvlc_Playing );
389 libvlc_media_player_pause (mi, &ex);
390 assert( libvlc_media_player_get_state (mi, &ex) == libvlc_Paused );
393 libvlc_media_player_stop (mi, &ex);
396 libvlc_media_player_release (mi);
399 libvlc_release (vlc);
403 static void test_media_list_player_pause_stop(const char** argv, int argc)
405 libvlc_instance_t *vlc;
407 libvlc_media_list_t *ml;
408 libvlc_media_list_player_t *mlp;
410 const char * file = "file://../bindings/java/core/src/test/resources/raffa_voice.ogg";
412 log ("Testing play and pause of %s using the media list.\n", file);
414 libvlc_exception_init (&ex);
415 vlc = libvlc_new (argc, argv, &ex);
418 md = libvlc_media_new (vlc, file, &ex);
421 ml = libvlc_media_list_new (vlc, &ex);
424 mlp = libvlc_media_list_player_new (vlc, &ex);
426 libvlc_media_list_add_media( ml, md, &ex );
429 libvlc_media_list_player_set_media_list( mlp, ml, &ex );
431 libvlc_media_list_player_play_item( mlp, md, &ex );
432 sleep(1); // play is asynchronous
435 libvlc_media_list_player_pause (mlp, &ex);
438 libvlc_media_list_player_stop (mlp, &ex);
441 libvlc_media_release (md);
443 libvlc_media_list_player_release (mlp);
446 libvlc_release (vlc);
452 int main (int argc, char *argv[])
454 const char *args[argc + 5];
455 int nlibvlc_args = sizeof (args) / sizeof (args[0]);
457 alarm (50); /* Make sure "make check" does not get stuck */
462 args[3] = "--plugin-path=../modules";
463 args[4] = "--vout=dummy";
464 args[5] = "--aout=dummy";
465 for (int i = 1; i < argc; i++)
466 args[i + 3] = argv[i];
468 test_core (args, nlibvlc_args);
470 test_events (args, nlibvlc_args);
472 test_media_list (args, nlibvlc_args);
474 test_media_player_play_stop(args, nlibvlc_args);
476 test_media_player_pause_stop(args, nlibvlc_args);
478 test_media_list_player_pause_stop(args, nlibvlc_args);