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);
54 static void test_core (const char ** argv, int argc);
55 static void test_media_list (const char ** argv, int argc);
56 static void test_events (const char ** argv, int argc);
57 static void test_media_player_play_stop(const char** argv, int argc);
59 /* Tests implementations */
60 static void test_core (const char ** argv, int argc)
62 libvlc_instance_t *vlc;
65 log ("Testing core\n");
67 libvlc_exception_init (&ex);
68 vlc = libvlc_new (argc, argv, &ex);
71 libvlc_playlist_clear (vlc, &ex);
74 id = libvlc_playlist_add_extended (vlc, "/dev/null", "Test", 0, NULL,
78 libvlc_playlist_clear (vlc, &ex);
86 static void test_media_list (const char ** argv, int argc)
88 libvlc_instance_t *vlc;
89 libvlc_media_descriptor_t *md;
90 libvlc_media_list_t *ml;
92 log ("Testing media_list\n");
94 libvlc_exception_init (&ex);
95 vlc = libvlc_new (argc, argv, &ex);
98 ml = libvlc_media_list_new (vlc, &ex);
101 md = libvlc_media_descriptor_new (vlc, "/dev/null", &ex);
104 libvlc_media_list_add_media_descriptor (ml, md, &ex);
106 libvlc_media_list_add_media_descriptor (ml, md, &ex);
109 assert( libvlc_media_list_count (ml, &ex) == 2 );
112 libvlc_media_descriptor_release (md);
114 libvlc_media_list_release (ml);
116 libvlc_release (vlc);
120 /* This one is an internal API. We use it here to run tests that
121 * don't depends on playback, and only test the event framework */
122 extern void libvlc_event_send( libvlc_event_manager_t *, libvlc_event_t *);
124 static void test_events_dummy_callback( const libvlc_event_t * event, void * user_data)
126 vlc_bool_t * callback_was_called = user_data;
127 *callback_was_called = VLC_TRUE;
130 static void test_events_callback_and_detach( const libvlc_event_t * event, void * user_data)
132 vlc_bool_t * callback_was_called = user_data;
133 libvlc_event_manager_t *em;
135 em = libvlc_media_instance_event_manager (event->p_obj, &ex);
138 libvlc_event_detach (em, event->type, test_events_callback_and_detach, user_data, &ex);
139 *callback_was_called = VLC_TRUE;
142 static void test_event_type_reception( libvlc_event_manager_t * em, libvlc_event_type_t event_type, vlc_bool_t * callback_was_called )
144 libvlc_event_t event;
145 event.type = event_type;
146 *callback_was_called = VLC_FALSE;
147 libvlc_event_send (em, &event);
148 assert (*callback_was_called);
151 static void test_events (const char ** argv, int argc)
153 libvlc_instance_t *vlc;
154 libvlc_media_instance_t *mi;
155 libvlc_event_manager_t *em;
156 vlc_bool_t callback_was_called;
157 libvlc_exception_t ex;
158 libvlc_event_type_t mi_events[] = {
159 libvlc_MediaInstancePlayed,
160 libvlc_MediaInstancePaused,
161 libvlc_MediaInstanceReachedEnd,
162 libvlc_MediaInstanceEncounteredError,
163 libvlc_MediaInstanceTimeChanged,
164 libvlc_MediaInstancePositionChanged,
166 int i, mi_events_len = sizeof(mi_events)/sizeof(*mi_events);
168 log ("Testing events\n");
170 libvlc_exception_init (&ex);
171 vlc = libvlc_new (argc, argv, &ex);
174 mi = libvlc_media_instance_new (vlc, &ex);
177 em = libvlc_media_instance_event_manager (mi, &ex);
179 log ("+ Testing attaching to Media Instance\n");
181 for (i = 0; i < mi_events_len; i++) {
182 libvlc_event_attach (em, mi_events[i], test_events_dummy_callback, &callback_was_called, &ex);
186 log ("+ Testing event reception\n");
188 for (i = 0; i < mi_events_len; i++)
189 test_event_type_reception (em, mi_events[i], &callback_was_called);
191 log ("+ Testing event detaching while in the event callback\n");
193 libvlc_event_t event;
194 event.type = mi_events[mi_events_len-1];
195 callback_was_called = VLC_FALSE;
197 libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_dummy_callback, &callback_was_called, &ex);
200 libvlc_event_attach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
203 libvlc_event_send (em, &event);
204 assert( callback_was_called );
206 callback_was_called = VLC_FALSE;
207 libvlc_event_send (em, &event);
208 assert( !callback_was_called );
210 libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
213 log ("+ Testing regular detach()\n");
215 for (i = 0; i < mi_events_len - 1; i++) {
216 libvlc_event_detach (em, mi_events[i], test_events_dummy_callback, &callback_was_called, &ex);
220 libvlc_media_instance_release (mi);
223 libvlc_release (vlc);
227 static void test_media_player_play_stop(const char** argv, int argc)
229 libvlc_instance_t *vlc;
230 libvlc_media_descriptor_t *md;
231 libvlc_media_instance_t *mi;
232 const char * file = "file://../bindings/java/core/src/test/resources/raffa_voice.ogg";
234 log ("Testing play and pause of %s\n", file);
236 libvlc_exception_init (&ex);
237 vlc = libvlc_new (argc, argv, &ex);
240 md = libvlc_media_descriptor_new (vlc, file, &ex);
243 mi = libvlc_media_instance_new_from_media_descriptor (md, &ex);
246 libvlc_media_descriptor_release (md);
248 libvlc_media_instance_play (mi, &ex);
251 /* FIXME: Do something clever */
254 assert( libvlc_media_instance_get_state (mi, &ex) != libvlc_Error );
257 libvlc_media_instance_stop (mi, &ex);
260 libvlc_media_instance_release (mi);
263 libvlc_release (vlc);
267 int main (int argc, char *argv[])
269 const char *args[argc + 5];
270 int nlibvlc_args = sizeof (args) / sizeof (args[0]);
272 alarm (50); /* Make sure "make check" does not get stuck */
277 args[3] = "--plugin-path=../modules:../../modules";
278 args[4] = "--vout=dummy";
279 args[5] = "--aout=dummy";
280 for (int i = 1; i < argc; i++)
281 args[i + 3] = argv[i];
283 test_core (args, nlibvlc_args);
285 test_events (args, nlibvlc_args);
287 test_media_list (args, nlibvlc_args);
289 test_media_player_play_stop(args, nlibvlc_args);