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 static void catch (void)
42 if (libvlc_exception_raised (&ex))
44 fprintf (stderr, "Exception: %s\n",
45 libvlc_exception_get_message (&ex));
49 assert (libvlc_exception_get_message (&ex) == NULL);
50 libvlc_exception_clear (&ex);
53 static void test_core (const char ** argv, int argc);
54 static void test_media_list (const char ** argv, int argc);
56 static void test_core (const char ** argv, int argc)
58 libvlc_instance_t *vlc;
61 log ("Testing core\n");
63 libvlc_exception_init (&ex);
64 vlc = libvlc_new (argc, argv, &ex);
67 libvlc_playlist_clear (vlc, &ex);
70 id = libvlc_playlist_add_extended (vlc, "/dev/null", "Test", 0, NULL,
74 libvlc_playlist_clear (vlc, &ex);
82 static void test_media_list (const char ** argv, int argc)
84 libvlc_instance_t *vlc;
85 libvlc_media_descriptor_t *md;
86 libvlc_media_list_t *ml;
88 log ("Testing media_list\n");
90 libvlc_exception_init (&ex);
91 vlc = libvlc_new (argc, argv, &ex);
94 ml = libvlc_media_list_new (vlc, &ex);
97 md = libvlc_media_descriptor_new (vlc, "/dev/null", &ex);
100 libvlc_media_list_add_media_descriptor (ml, md, &ex);
102 libvlc_media_list_add_media_descriptor (ml, md, &ex);
105 assert( libvlc_media_list_count (ml, &ex) == 2 );
108 libvlc_media_descriptor_release (md);
110 libvlc_media_list_release (ml);
112 libvlc_release (vlc);
116 static void test_file_playback (const char ** argv, int argc, const char * file)
118 libvlc_instance_t *vlc;
119 libvlc_media_descriptor_t *md;
120 libvlc_media_instance_t *mi;
122 log ("Testing playback of %s\n", file);
124 libvlc_exception_init (&ex);
125 vlc = libvlc_new (argc, argv, &ex);
128 md = libvlc_media_descriptor_new (vlc, file, &ex);
131 mi = libvlc_media_instance_new_from_media_descriptor (md, &ex);
134 libvlc_media_descriptor_release (md);
136 libvlc_media_instance_play (mi, &ex);
139 /* FIXME: Do something clever */
142 assert( libvlc_media_instance_get_state (mi, &ex) != libvlc_Error );
145 libvlc_media_instance_stop (mi, &ex);
148 libvlc_media_instance_release (mi);
151 libvlc_release (vlc);
155 /* This one is an internal API. We use it here to run tests that
156 * don't depends on playback, and only test the event framework */
157 extern void libvlc_event_send( libvlc_event_manager_t *, libvlc_event_t *);
159 static void test_events_dummy_callback( const libvlc_event_t * event, void * user_data)
161 vlc_bool_t * callback_was_called = user_data;
162 *callback_was_called = VLC_TRUE;
165 static void test_events_callback_and_detach( const libvlc_event_t * event, void * user_data)
167 vlc_bool_t * callback_was_called = user_data;
168 libvlc_event_manager_t *em;
170 em = libvlc_media_instance_event_manager (event->p_obj, &ex);
173 libvlc_event_detach (em, event->type, test_events_callback_and_detach, user_data, &ex);
174 *callback_was_called = VLC_TRUE;
177 static void test_event_type_reception( libvlc_event_manager_t * em, libvlc_event_type_t event_type, vlc_bool_t * callback_was_called )
179 libvlc_event_t event;
180 event.type = event_type;
181 *callback_was_called = VLC_FALSE;
182 libvlc_event_send (em, &event);
183 assert (*callback_was_called);
186 static void test_events (const char ** argv, int argc)
188 libvlc_instance_t *vlc;
189 libvlc_media_instance_t *mi;
190 libvlc_event_manager_t *em;
191 vlc_bool_t callback_was_called;
192 libvlc_exception_t ex;
193 libvlc_event_type_t mi_events[] = {
194 libvlc_MediaInstancePlayed,
195 libvlc_MediaInstancePaused,
196 libvlc_MediaInstanceReachedEnd,
197 libvlc_MediaInstanceEncounteredError,
198 libvlc_MediaInstanceTimeChanged,
199 libvlc_MediaInstancePositionChanged,
201 int i, mi_events_len = sizeof(mi_events)/sizeof(*mi_events);
203 log ("Testing events\n");
205 libvlc_exception_init (&ex);
206 vlc = libvlc_new (argc, argv, &ex);
209 mi = libvlc_media_instance_new (vlc, &ex);
212 em = libvlc_media_instance_event_manager (mi, &ex);
214 log ("+ Testing attaching to Media Instance\n");
216 for (i = 0; i < mi_events_len; i++) {
217 libvlc_event_attach (em, mi_events[i], test_events_dummy_callback, &callback_was_called, &ex);
221 log ("+ Testing event reception\n");
223 for (i = 0; i < mi_events_len; i++)
224 test_event_type_reception (em, mi_events[i], &callback_was_called);
226 log ("+ Testing event detaching while in the event callback\n");
228 libvlc_event_t event;
229 event.type = mi_events[mi_events_len-1];
230 callback_was_called = VLC_FALSE;
232 libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_dummy_callback, &callback_was_called, &ex);
235 libvlc_event_attach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
238 libvlc_event_send (em, &event);
239 assert( callback_was_called );
241 callback_was_called = VLC_FALSE;
242 libvlc_event_send (em, &event);
243 assert( !callback_was_called );
245 libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
248 log ("+ Testing regular detach()\n");
250 for (i = 0; i < mi_events_len - 1; i++) {
251 libvlc_event_detach (em, mi_events[i], test_events_dummy_callback, &callback_was_called, &ex);
255 libvlc_media_instance_release (mi);
258 libvlc_release (vlc);
262 int main (int argc, char *argv[])
264 const char *args[argc + 5];
265 int nlibvlc_args = sizeof (args) / sizeof (args[0]);
267 alarm (30); /* Make sure "make check" does not get stuck */
272 args[3] = "--plugin-path=../modules";
273 args[4] = "--vout=dummy";
274 args[5] = "--aout=dummy";
275 for (int i = 1; i < argc; i++)
276 args[i + 3] = argv[i];
278 test_core (args, nlibvlc_args);
280 test_events (args, nlibvlc_args);
282 test_media_list (args, nlibvlc_args);