]> git.sesse.net Git - vlc/blob - test/libvlc/media_list_player.c
test: Expanded and passing media_list_player queue item tests.
[vlc] / test / libvlc / media_list_player.c
1 /*
2  * media_list_player.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 #include "test.h"
25
26  // For msleep
27 #include <vlc_common.h>
28 #include <vlc_mtime.h>
29
30 #include "libvlc_additions.h"
31
32 struct check_items_order_data {
33     bool done_playing;
34     unsigned count;
35     unsigned index;
36     void * items[16];
37 };
38
39 static inline void check_data_init(struct check_items_order_data *check)
40 {
41     check->index = 0;
42     check->count = 0;
43     check->done_playing = false;
44 }
45
46 static inline void queue_expected_item(struct check_items_order_data *check, void *item)
47 {
48     assert(check->count < 16);
49     check->items[check->count] = item;
50     check->count++;
51 }
52
53 static inline void wait_queued_items(struct check_items_order_data *check)
54 {
55     // Wait dummily for check_items_order_callback() to flag 'done_playing':
56     while (!check->done_playing)
57         msleep(100000);
58 }
59
60 static void check_items_order_callback(const libvlc_event_t * p_event, void * user_data)
61 {
62     struct check_items_order_data *checks = user_data;
63     libvlc_media_t *md = p_event->u.media_list_player_next_item_set.item;
64     assert(checks->index < checks->count);
65     if (checks->items[checks->index] != md)
66     {
67         char *title = libvlc_media_get_meta(md, libvlc_meta_Title, NULL);
68         log ("Got items %s\n", title);
69         free(title);
70     }
71     assert(checks->items[checks->index] == md);
72     
73     char *title = libvlc_media_get_meta(md, libvlc_meta_Title, NULL);
74     log ("Item %d '%s' was correctly queued\n", checks->index, title);
75     free(title);
76     
77     if (checks->index == (checks->count - 1))
78     {
79         log ("Done playing with success\n");
80         checks->done_playing = true;
81     }
82     checks->index++;
83 }
84
85 static void test_media_list_player_items_queue(const char** argv, int argc)
86 {
87     libvlc_instance_t *vlc;
88     libvlc_media_t *md;
89     libvlc_media_list_t *ml;
90     libvlc_media_list_player_t *mlp;
91     
92     const char * file = test_default_sample;
93     
94     log ("Testing media player item queue-ing\n");
95     
96     libvlc_exception_init (&ex);
97     vlc = libvlc_new (argc, argv, &ex);
98     catch ();
99     
100     md = libvlc_media_new (vlc, file, &ex);
101     catch ();
102     
103     ml = libvlc_media_list_new (vlc, &ex);
104     catch ();
105     
106     mlp = libvlc_media_list_player_new (vlc, &ex);
107     catch ();
108     
109     libvlc_media_list_add_media (ml, md, &ex);
110     catch ();
111
112     static struct check_items_order_data check;
113     check_data_init(&check);
114     queue_expected_item(&check, md);
115
116     // Add three more media
117     queue_expected_item(&check, media_list_add_file_path (vlc, ml, file));
118     queue_expected_item(&check, media_list_add_file_path (vlc, ml, file));
119     queue_expected_item(&check, media_list_add_file_path (vlc, ml, file));
120
121     // Add a node
122     libvlc_media_t *node = libvlc_media_new_as_node(vlc, "node", &ex);
123     catch ();
124     libvlc_media_list_add_media(ml, node, &ex);
125     catch ();
126     queue_expected_item(&check, node);
127
128     // Add items to that node
129     libvlc_media_list_t *subitems = libvlc_media_subitems(node, &ex);
130     catch ();
131     queue_expected_item(&check, media_list_add_file_path(vlc, subitems, file));
132     queue_expected_item(&check, media_list_add_file_path(vlc, subitems, file));
133     queue_expected_item(&check, media_list_add_file_path(vlc, subitems, file));
134     libvlc_media_list_release(subitems);
135     
136     libvlc_media_list_player_set_media_list (mlp, ml, &ex);
137
138     libvlc_event_manager_t * em = libvlc_media_list_player_event_manager(mlp);
139     libvlc_event_attach(em, libvlc_MediaListPlayerNextItemSet, check_items_order_callback, &check, &ex);
140     catch ();
141
142     libvlc_media_list_player_play(mlp, &ex);
143     catch ();
144
145     // Wait until all item are read
146     wait_queued_items(&check);
147
148     libvlc_media_list_player_stop (mlp, &ex);
149     catch ();
150
151     libvlc_media_list_player_release (mlp);
152     catch ();
153     
154     libvlc_release (vlc);
155     catch ();
156 }
157
158 static void test_media_list_player_next(const char** argv, int argc)
159 {
160     libvlc_instance_t *vlc;
161     libvlc_media_t *md;
162     libvlc_media_list_t *ml;
163     libvlc_media_list_player_t *mlp;
164     
165     const char * file = test_default_sample;
166     
167     log ("Testing media player next()\n");
168     
169     libvlc_exception_init (&ex);
170     vlc = libvlc_new (argc, argv, &ex);
171     catch ();
172     
173     md = libvlc_media_new (vlc, file, &ex);
174     catch ();
175     
176     ml = libvlc_media_list_new (vlc, &ex);
177     catch ();
178     
179     mlp = libvlc_media_list_player_new (vlc, &ex);
180     catch ();
181
182     libvlc_media_list_add_media (ml, md, &ex);
183     catch ();
184
185     // Add three media
186     media_list_add_file_path (vlc, ml, file);
187     media_list_add_file_path (vlc, ml, file);
188     media_list_add_file_path (vlc, ml, file);
189
190     libvlc_media_list_player_set_media_list (mlp, ml, &ex);
191     
192     libvlc_media_list_player_play_item (mlp, md, &ex);
193     catch ();
194
195     libvlc_media_release (md);
196
197     msleep(100000);
198     
199     libvlc_media_list_player_next (mlp, &ex);
200     catch ();
201
202     libvlc_media_list_player_pause (mlp, &ex);
203     catch();
204
205     msleep(100000);
206     
207     libvlc_media_list_player_next (mlp, &ex);
208     catch ();
209     
210     libvlc_media_list_player_stop (mlp, &ex);
211     catch ();
212
213     msleep(100000);
214     
215     libvlc_media_list_player_next (mlp, &ex);
216     catch ();
217         
218     libvlc_media_list_player_release (mlp);
219     catch ();
220     
221     libvlc_release (vlc);
222     catch ();
223 }
224
225 static void test_media_list_player_pause_stop(const char** argv, int argc)
226 {
227     libvlc_instance_t *vlc;
228     libvlc_media_t *md;
229     libvlc_media_list_t *ml;
230     libvlc_media_list_player_t *mlp;
231
232     const char * file = test_default_sample;
233
234     log ("Testing play and pause of %s using the media list.\n", file);
235
236     libvlc_exception_init (&ex);
237     vlc = libvlc_new (argc, argv, &ex);
238     catch ();
239
240     md = libvlc_media_new (vlc, file, &ex);
241     catch ();
242
243     ml = libvlc_media_list_new (vlc, &ex);
244     catch ();
245
246     mlp = libvlc_media_list_player_new (vlc, &ex);
247
248     libvlc_media_list_add_media( ml, md, &ex );
249     catch ();
250
251     libvlc_media_list_player_set_media_list( mlp, ml, &ex );
252
253     libvlc_media_list_player_play_item( mlp, md, &ex );
254     catch ();
255
256     libvlc_media_list_player_pause (mlp, &ex);
257     catch();
258
259     libvlc_media_list_player_stop (mlp, &ex);
260     catch ();
261
262     libvlc_media_release (md);
263
264     libvlc_media_list_player_release (mlp);
265     catch ();
266
267     libvlc_release (vlc);
268     catch ();
269 }
270
271 static void test_media_list_player_play_item_at_index(const char** argv, int argc)
272 {
273     libvlc_instance_t *vlc;
274     libvlc_media_t *md;
275     libvlc_media_list_t *ml;
276     libvlc_media_list_player_t *mlp;
277
278     const char * file = test_default_sample;
279
280     log ("Testing play_item_at_index of %s using the media list.\n", file);
281
282     libvlc_exception_init (&ex);
283     vlc = libvlc_new (argc, argv, &ex);
284     catch ();
285
286     md = libvlc_media_new (vlc, file, &ex);
287     catch ();
288
289     ml = libvlc_media_list_new (vlc, &ex);
290     catch ();
291
292     mlp = libvlc_media_list_player_new (vlc, &ex);
293
294     for (unsigned i = 0; i < 5; i++)
295     {
296         libvlc_media_list_add_media( ml, md, &ex );
297         catch ();
298     }
299
300     libvlc_media_list_player_set_media_list( mlp, ml, &ex );
301
302     libvlc_media_list_player_play_item_at_index( mlp, 0, &ex );
303     catch ();
304
305     libvlc_media_list_player_stop (mlp, &ex);
306     catch ();
307
308     libvlc_media_release (md);
309     catch ();
310
311     libvlc_media_list_player_release (mlp);
312     catch ();
313
314     libvlc_release (vlc);
315     catch ();
316 }
317
318
319 int main (void)
320 {
321     test_init();
322
323     test_media_list_player_pause_stop (test_defaults_args, test_defaults_nargs);
324     test_media_list_player_play_item_at_index (test_defaults_args, test_defaults_nargs);
325     test_media_list_player_next (test_defaults_args, test_defaults_nargs);
326     test_media_list_player_items_queue (test_defaults_args, test_defaults_nargs);
327     return 0;
328 }