]> git.sesse.net Git - vlc/blob - src/control/testapi.c
fix testapi test: libvlc_instance_(play|pause|stop) are asynchronous, so wait for...
[vlc] / src / control / testapi.c
1 /*
2  * testapi.c - libvlc smoke test
3  *
4  * $Id$
5  */
6
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.                                   *
13  *                                                                    *
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.              *
18  *                                                                    *
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  **********************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 #include <vlc/libvlc.h>
28
29 #undef NDEBUG
30 #include <assert.h>
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35
36 static libvlc_exception_t ex;
37
38 #define log( ... ) printf( "testapi: " __VA_ARGS__ );
39
40 static void catch (void)
41 {
42     if (libvlc_exception_raised (&ex))
43     {
44          fprintf (stderr, "Exception: %s\n",
45                   libvlc_exception_get_message (&ex));
46          abort ();
47     }
48
49     assert (libvlc_exception_get_message (&ex) == NULL);
50     libvlc_exception_clear (&ex);
51 }
52
53 /* Test we have */
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);
58
59 /* Tests implementations */
60 static void test_core (const char ** argv, int argc)
61 {
62     libvlc_instance_t *vlc;
63     int id;
64
65     log ("Testing core\n");
66
67     libvlc_exception_init (&ex);
68     vlc = libvlc_new (argc, argv, &ex);
69     catch ();
70
71     libvlc_playlist_clear (vlc, &ex);
72     catch ();
73
74     id = libvlc_playlist_add_extended (vlc, "/dev/null", "Test", 0, NULL,
75                                        &ex);
76     catch ();
77
78     libvlc_playlist_clear (vlc, &ex);
79     catch ();
80
81     libvlc_retain (vlc);
82     libvlc_release (vlc);
83     libvlc_release (vlc);
84 }
85
86 static void test_media_list (const char ** argv, int argc)
87 {
88     libvlc_instance_t *vlc;
89     libvlc_media_descriptor_t *md;
90     libvlc_media_list_t *ml;
91
92     log ("Testing media_list\n");
93
94     libvlc_exception_init (&ex);
95     vlc = libvlc_new (argc, argv, &ex);
96     catch ();
97
98     ml = libvlc_media_list_new (vlc, &ex);
99     catch ();
100
101     md = libvlc_media_descriptor_new (vlc, "/dev/null", &ex);
102     catch ();
103
104     libvlc_media_list_add_media_descriptor (ml, md, &ex);
105     catch ();
106     libvlc_media_list_add_media_descriptor (ml, md, &ex);
107     catch ();
108
109     assert( libvlc_media_list_count (ml, &ex) == 2 );
110     catch ();
111
112     libvlc_media_descriptor_release (md);
113
114     libvlc_media_list_release (ml);
115
116     libvlc_release (vlc);
117     catch ();
118 }
119
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 *);
123
124 static void test_events_dummy_callback( const libvlc_event_t * event, void * user_data)
125 {
126     vlc_bool_t * callback_was_called = user_data;
127     *callback_was_called = VLC_TRUE;
128 }
129
130 static void test_events_callback_and_detach( const libvlc_event_t * event, void * user_data)
131 {
132     vlc_bool_t * callback_was_called = user_data;
133     libvlc_event_manager_t *em;
134
135     em = libvlc_media_instance_event_manager (event->p_obj, &ex);
136     catch();
137
138     libvlc_event_detach (em, event->type, test_events_callback_and_detach, user_data, &ex);
139     *callback_was_called = VLC_TRUE;
140 }
141
142 static void test_event_type_reception( libvlc_event_manager_t * em, libvlc_event_type_t event_type, vlc_bool_t * callback_was_called )
143 {
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);
149 }
150
151 static void test_events (const char ** argv, int argc)
152 {
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,
165     };
166     int i, mi_events_len = sizeof(mi_events)/sizeof(*mi_events);
167     
168     log ("Testing events\n");
169
170     libvlc_exception_init (&ex);
171     vlc = libvlc_new (argc, argv, &ex);
172     catch ();
173
174     mi = libvlc_media_instance_new (vlc, &ex);
175     catch ();
176
177     em = libvlc_media_instance_event_manager (mi, &ex);
178
179     log ("+ Testing attaching to Media Instance\n");
180
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);
183         catch ();
184     }
185
186     log ("+ Testing event reception\n");
187
188     for (i = 0; i < mi_events_len; i++)
189         test_event_type_reception (em, mi_events[i], &callback_was_called);
190
191     log ("+ Testing event detaching while in the event callback\n");
192
193     libvlc_event_t event;
194     event.type = mi_events[mi_events_len-1];
195     callback_was_called = VLC_FALSE;
196
197     libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_dummy_callback, &callback_was_called, &ex);
198     catch ();
199
200     libvlc_event_attach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
201     catch ();
202
203     libvlc_event_send (em, &event);
204     assert( callback_was_called );
205
206     callback_was_called = VLC_FALSE;
207     libvlc_event_send (em, &event);
208     assert( !callback_was_called );
209
210     libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
211     catch ();
212
213     log ("+ Testing regular detach()\n");
214
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);
217         catch ();
218     }
219
220     libvlc_media_instance_release (mi);
221     catch ();
222
223     libvlc_release (vlc);
224     catch ();
225 }
226
227 static void test_media_player_play_stop(const char** argv, int argc)
228 {
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";
233
234     log ("Testing play and pause of %s\n", file);
235
236     libvlc_exception_init (&ex);
237     vlc = libvlc_new (argc, argv, &ex);
238     catch ();
239
240     md = libvlc_media_descriptor_new (vlc, file, &ex);
241     catch ();
242
243     mi = libvlc_media_instance_new_from_media_descriptor (md, &ex);
244     catch ();
245     
246     libvlc_media_descriptor_release (md);
247
248     libvlc_media_instance_play (mi, &ex);
249     catch ();
250
251     /* FIXME: Do something clever */
252     sleep(1);
253
254     assert( libvlc_media_instance_get_state (mi, &ex) != libvlc_Error );
255     catch ();
256
257     libvlc_media_instance_stop (mi, &ex);
258     catch ();
259     sleep(1);
260
261     libvlc_media_instance_release (mi);
262     catch ();
263
264     libvlc_release (vlc);
265     catch ();
266 }
267
268 int main (int argc, char *argv[])
269 {
270     const char *args[argc + 5];
271     int nlibvlc_args = sizeof (args) / sizeof (args[0]);
272
273     alarm (50); /* Make sure "make check" does not get stuck */
274
275     args[0] = "-vvv";
276     args[1] = "-I";
277     args[2] = "dummy";
278     args[3] = "--plugin-path=../modules";
279     args[4] = "--vout=dummy";
280     args[5] = "--aout=dummy";
281     for (int i = 1; i < argc; i++)
282         args[i + 3] = argv[i];
283
284     test_core (args, nlibvlc_args);
285
286     test_events (args, nlibvlc_args);
287
288     test_media_list (args, nlibvlc_args);
289
290     test_media_player_play_stop(args, nlibvlc_args);
291
292     return 0;
293 }