]> git.sesse.net Git - vlc/blob - src/control/testapi.c
449f7b528ae9e9e784f2fbde01d51a05cda83d34
[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 static void test_core (const char ** argv, int argc);
54 static void test_media_list (const char ** argv, int argc);
55
56 static void test_core (const char ** argv, int argc)
57 {
58     libvlc_instance_t *vlc;
59     int id;
60
61     log ("Testing core\n");
62
63     libvlc_exception_init (&ex);
64     vlc = libvlc_new (argc, argv, &ex);
65     catch ();
66
67     libvlc_playlist_clear (vlc, &ex);
68     catch ();
69
70     id = libvlc_playlist_add_extended (vlc, "/dev/null", "Test", 0, NULL,
71                                        &ex);
72     catch ();
73
74     libvlc_playlist_clear (vlc, &ex);
75     catch ();
76
77     libvlc_retain (vlc);
78     libvlc_release (vlc);
79     libvlc_release (vlc);
80 }
81
82 static void test_media_list (const char ** argv, int argc)
83 {
84     libvlc_instance_t *vlc;
85     libvlc_media_descriptor_t *md;
86     libvlc_media_list_t *ml;
87
88     log ("Testing media_list\n");
89
90     libvlc_exception_init (&ex);
91     vlc = libvlc_new (argc, argv, &ex);
92     catch ();
93
94     ml = libvlc_media_list_new (vlc, &ex);
95     catch ();
96
97     md = libvlc_media_descriptor_new (vlc, "/dev/null", &ex);
98     catch ();
99
100     libvlc_media_list_add_media_descriptor (ml, md, &ex);
101     catch ();
102     libvlc_media_list_add_media_descriptor (ml, md, &ex);
103     catch ();
104
105     assert( libvlc_media_list_count (ml, &ex) == 2 );
106     catch ();
107
108     libvlc_media_descriptor_release (md);
109
110     libvlc_media_list_release (ml);
111
112     libvlc_release (vlc);
113     catch ();
114 }
115
116 static void test_file_playback (const char ** argv, int argc, const char * file)
117 {
118     libvlc_instance_t *vlc;
119     libvlc_media_descriptor_t *md;
120     libvlc_media_instance_t *mi;
121
122     log ("Testing playback of %s\n", file);
123
124     libvlc_exception_init (&ex);
125     vlc = libvlc_new (argc, argv, &ex);
126     catch ();
127
128     md = libvlc_media_descriptor_new (vlc, file, &ex);
129     catch ();
130
131     mi = libvlc_media_instance_new_from_media_descriptor (md, &ex);
132     catch ();
133     
134     libvlc_media_descriptor_release (md);
135
136     libvlc_media_instance_play (mi, &ex);
137     catch ();
138
139     /* FIXME: Do something clever */
140     sleep(1);
141
142     assert( libvlc_media_instance_get_state (mi, &ex) != libvlc_Error );
143     catch ();
144
145     libvlc_media_instance_stop (mi, &ex);
146     catch ();
147
148     libvlc_media_instance_release (mi);
149     catch ();
150
151     libvlc_release (vlc);
152     catch ();
153 }
154
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 *);
158
159 static void test_events_dummy_callback( const libvlc_event_t * event, void * user_data)
160 {
161     vlc_bool_t * callback_was_called = user_data;
162     *callback_was_called = VLC_TRUE;
163 }
164
165 static void test_events_callback_and_detach( const libvlc_event_t * event, void * user_data)
166 {
167     vlc_bool_t * callback_was_called = user_data;
168     libvlc_event_manager_t *em;
169
170     em = libvlc_media_instance_event_manager (event->p_obj, &ex);
171     catch();
172
173     libvlc_event_detach (em, event->type, test_events_callback_and_detach, user_data, &ex);
174     *callback_was_called = VLC_TRUE;
175 }
176
177 static void test_event_type_reception( libvlc_event_manager_t * em, libvlc_event_type_t event_type, vlc_bool_t * callback_was_called )
178 {
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);
184 }
185
186 static void test_events (const char ** argv, int argc)
187 {
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,
200     };
201     int i, mi_events_len = sizeof(mi_events)/sizeof(*mi_events);
202     
203     log ("Testing events\n");
204
205     libvlc_exception_init (&ex);
206     vlc = libvlc_new (argc, argv, &ex);
207     catch ();
208
209     mi = libvlc_media_instance_new (vlc, &ex);
210     catch ();
211
212     em = libvlc_media_instance_event_manager (mi, &ex);
213
214     log ("+ Testing attaching to Media Instance\n");
215
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);
218         catch ();
219     }
220
221     log ("+ Testing event reception\n");
222
223     for (i = 0; i < mi_events_len; i++)
224         test_event_type_reception (em, mi_events[i], &callback_was_called);
225
226     log ("+ Testing event detaching while in the event callback\n");
227
228     libvlc_event_t event;
229     event.type = mi_events[mi_events_len-1];
230     callback_was_called = VLC_FALSE;
231
232     libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_dummy_callback, &callback_was_called, &ex);
233     catch ();
234
235     libvlc_event_attach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
236     catch ();
237
238     libvlc_event_send (em, &event);
239     assert( callback_was_called );
240
241     callback_was_called = VLC_FALSE;
242     libvlc_event_send (em, &event);
243     assert( !callback_was_called );
244
245     libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
246     catch ();
247
248     log ("+ Testing regular detach()\n");
249
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);
252         catch ();
253     }
254
255     libvlc_media_instance_release (mi);
256     catch ();
257
258     libvlc_release (vlc);
259     catch ();
260 }
261
262 int main (int argc, char *argv[])
263 {
264     const char *args[argc + 5];
265     int nlibvlc_args = sizeof (args) / sizeof (args[0]);
266
267     alarm (30); /* Make sure "make check" does not get stuck */
268
269     args[0] = "-vvv";
270     args[1] = "-I";
271     args[2] = "-dummy";
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];
277
278     test_core (args, nlibvlc_args);
279
280     test_events (args, nlibvlc_args);
281
282     test_media_list (args, nlibvlc_args);
283
284     return 0;
285 }