]> git.sesse.net Git - vlc/blob - test/libvlc/media_list_player.c
a5665aae33ccb15a338548e6099a846d2071da96
[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 /* 
33     HACK - FIX ME
34     This allows for the direct addition of subitems in the playback options test.
35     This would not be necessary if there were an add subitems function.
36 */
37 #include "../../src/control/media_internal.h"
38
39 struct check_items_order_data {
40     bool done_playing;
41     unsigned count;
42     unsigned index;
43     void * items[16];
44 };
45
46 static inline void check_data_init(struct check_items_order_data *check)
47 {
48     check->index = 0;
49     check->count = 0;
50     check->done_playing = false;
51 }
52
53 static inline void queue_expected_item(struct check_items_order_data *check, void *item)
54 {
55     assert(check->count < 16);
56     check->items[check->count] = item;
57     check->count++;
58 }
59
60 static inline void wait_queued_items(struct check_items_order_data *check)
61 {
62     // Wait dummily for check_items_order_callback() to flag 'done_playing':
63     while (!check->done_playing)
64         msleep(100000);
65 }
66
67 static void check_items_order_callback(const libvlc_event_t * p_event, void * user_data)
68 {
69     struct check_items_order_data *checks = user_data;
70     libvlc_media_t *md = p_event->u.media_list_player_next_item_set.item;
71     assert(checks->index < checks->count);
72     if (checks->items[checks->index] != md)
73     {
74         char *title = libvlc_media_get_meta(md, libvlc_meta_Title, NULL);
75         log ("Got items %s\n", title);
76         free(title);
77     }
78     assert(checks->items[checks->index] == md);
79     
80     char *title = libvlc_media_get_meta(md, libvlc_meta_Title, NULL);
81     log ("Item %d '%s' was correctly queued\n", checks->index, title);
82     free(title);
83     
84     if (checks->index == (checks->count - 1))
85     {
86         log ("Done playing with success\n");
87         checks->done_playing = true;
88     }
89     checks->index++;
90 }
91
92 static void test_media_list_player_items_queue(const char** argv, int argc)
93 {
94     libvlc_instance_t *vlc;
95     libvlc_media_t *md;
96     libvlc_media_list_t *ml;
97     libvlc_media_list_player_t *mlp;
98     
99     const char * file = test_default_sample;
100     
101     log ("Testing media player item queue-ing\n");
102     
103     libvlc_exception_init (&ex);
104     vlc = libvlc_new (argc, argv, &ex);
105     catch ();
106     
107     md = libvlc_media_new (vlc, file, &ex);
108     catch ();
109     
110     ml = libvlc_media_list_new (vlc, &ex);
111     catch ();
112     
113     mlp = libvlc_media_list_player_new (vlc, &ex);
114     catch ();
115     
116     libvlc_media_list_add_media (ml, md, &ex);
117     catch ();
118
119     static struct check_items_order_data check;
120     check_data_init(&check);
121     queue_expected_item(&check, md);
122
123     // Add three more media
124     queue_expected_item(&check, media_list_add_file_path (vlc, ml, file));
125     queue_expected_item(&check, media_list_add_file_path (vlc, ml, file));
126     queue_expected_item(&check, media_list_add_file_path (vlc, ml, file));
127
128     // Add a node
129     libvlc_media_t *node = libvlc_media_new_as_node(vlc, "node", &ex);
130     catch ();
131     libvlc_media_list_add_media(ml, node, &ex);
132     catch ();
133     queue_expected_item(&check, node);
134
135     // Add items to that node
136     libvlc_media_list_t *subitems = libvlc_media_subitems(node, &ex);
137     catch ();
138     queue_expected_item(&check, media_list_add_file_path(vlc, subitems, file));
139     queue_expected_item(&check, media_list_add_file_path(vlc, subitems, file));
140     queue_expected_item(&check, media_list_add_file_path(vlc, subitems, file));
141     libvlc_media_list_release(subitems);
142     
143     libvlc_media_list_player_set_media_list (mlp, ml, &ex);
144
145     libvlc_event_manager_t * em = libvlc_media_list_player_event_manager(mlp);
146     libvlc_event_attach(em, libvlc_MediaListPlayerNextItemSet, check_items_order_callback, &check, &ex);
147     catch ();
148
149     libvlc_media_list_player_play(mlp, &ex);
150     catch ();
151
152     // Wait until all item are read
153     wait_queued_items(&check);
154
155     libvlc_media_list_player_stop (mlp, &ex);
156     catch ();
157
158     libvlc_media_list_player_release (mlp);
159     catch ();
160     
161     libvlc_release (vlc);
162     catch ();
163 }
164
165 static void test_media_list_player_previous(const char** argv, int argc)
166 {
167     libvlc_instance_t *vlc;
168     libvlc_media_t *md;
169     libvlc_media_list_t *ml;
170     libvlc_media_list_player_t *mlp;
171     
172     const char * file = test_default_sample;
173     
174     log ("Testing media player previous()\n");
175     
176     libvlc_exception_init (&ex);
177     vlc = libvlc_new (argc, argv, &ex);
178     catch ();
179     
180     md = libvlc_media_new (vlc, file, &ex);
181     catch ();
182     
183     ml = libvlc_media_list_new (vlc, &ex);
184     catch ();
185     
186     mlp = libvlc_media_list_player_new (vlc, &ex);
187     catch ();
188
189     libvlc_media_list_add_media (ml, md, &ex);
190     catch ();
191
192     // Add three media
193     media_list_add_file_path (vlc, ml, file);
194     media_list_add_file_path (vlc, ml, file);
195     media_list_add_file_path (vlc, ml, file);
196
197     libvlc_media_list_player_set_media_list (mlp, ml, &ex);
198
199     libvlc_media_list_player_play_item (mlp, md, &ex);
200     catch ();
201
202     libvlc_media_release (md);
203
204     msleep(100000);
205     
206     libvlc_media_list_player_previous (mlp, &ex);
207     catch ();
208
209     libvlc_media_list_player_pause (mlp, &ex);
210     catch();
211
212     msleep(100000);
213     
214     libvlc_media_list_player_previous (mlp, &ex);
215     catch ();
216     
217     libvlc_media_list_player_stop (mlp, &ex);
218     catch ();
219
220     msleep(100000);
221     
222     libvlc_media_list_player_previous (mlp, &ex);
223     catch ();
224     
225     libvlc_media_list_player_release (mlp);
226     catch ();
227     
228     libvlc_release (vlc);
229     catch ();
230 }
231
232 static void test_media_list_player_next(const char** argv, int argc)
233 {
234     libvlc_instance_t *vlc;
235     libvlc_media_t *md;
236     libvlc_media_list_t *ml;
237     libvlc_media_list_player_t *mlp;
238     
239     const char * file = test_default_sample;
240     
241     log ("Testing media player next()\n");
242     
243     libvlc_exception_init (&ex);
244     vlc = libvlc_new (argc, argv, &ex);
245     catch ();
246     
247     md = libvlc_media_new (vlc, file, &ex);
248     catch ();
249     
250     ml = libvlc_media_list_new (vlc, &ex);
251     catch ();
252     
253     mlp = libvlc_media_list_player_new (vlc, &ex);
254     catch ();
255
256     libvlc_media_list_add_media (ml, md, &ex);
257     catch ();
258
259     // Add three media
260     media_list_add_file_path (vlc, ml, file);
261     media_list_add_file_path (vlc, ml, file);
262     media_list_add_file_path (vlc, ml, file);
263
264     libvlc_media_list_player_set_media_list (mlp, ml, &ex);
265     
266     libvlc_media_list_player_play_item (mlp, md, &ex);
267     catch ();
268
269     libvlc_media_release (md);
270
271     msleep(100000);
272     
273     libvlc_media_list_player_next (mlp, &ex);
274     catch ();
275
276     libvlc_media_list_player_pause (mlp, &ex);
277     catch();
278
279     msleep(100000);
280     
281     libvlc_media_list_player_next (mlp, &ex);
282     catch ();
283     
284     libvlc_media_list_player_stop (mlp, &ex);
285     catch ();
286
287     msleep(100000);
288     
289     libvlc_media_list_player_next (mlp, &ex);
290     catch ();
291         
292     libvlc_media_list_player_release (mlp);
293     catch ();
294     
295     libvlc_release (vlc);
296     catch ();
297 }
298
299 static void test_media_list_player_pause_stop(const char** argv, int argc)
300 {
301     libvlc_instance_t *vlc;
302     libvlc_media_t *md;
303     libvlc_media_list_t *ml;
304     libvlc_media_list_player_t *mlp;
305
306     const char * file = test_default_sample;
307
308     log ("Testing play and pause of %s using the media list.\n", file);
309
310     libvlc_exception_init (&ex);
311     vlc = libvlc_new (argc, argv, &ex);
312     catch ();
313
314     md = libvlc_media_new (vlc, file, &ex);
315     catch ();
316
317     ml = libvlc_media_list_new (vlc, &ex);
318     catch ();
319
320     mlp = libvlc_media_list_player_new (vlc, &ex);
321
322     libvlc_media_list_add_media( ml, md, &ex );
323     catch ();
324
325     libvlc_media_list_player_set_media_list( mlp, ml, &ex );
326
327     libvlc_media_list_player_play_item( mlp, md, &ex );
328     catch ();
329
330     libvlc_media_list_player_pause (mlp, &ex);
331     catch();
332
333     libvlc_media_list_player_stop (mlp, &ex);
334     catch ();
335
336     libvlc_media_release (md);
337
338     libvlc_media_list_player_release (mlp);
339     catch ();
340
341     libvlc_release (vlc);
342     catch ();
343 }
344
345 static void test_media_list_player_play_item_at_index(const char** argv, int argc)
346 {
347     libvlc_instance_t *vlc;
348     libvlc_media_t *md;
349     libvlc_media_list_t *ml;
350     libvlc_media_list_player_t *mlp;
351
352     const char * file = test_default_sample;
353
354     log ("Testing play_item_at_index of %s using the media list.\n", file);
355
356     libvlc_exception_init (&ex);
357     vlc = libvlc_new (argc, argv, &ex);
358     catch ();
359
360     md = libvlc_media_new (vlc, file, &ex);
361     catch ();
362
363     ml = libvlc_media_list_new (vlc, &ex);
364     catch ();
365
366     mlp = libvlc_media_list_player_new (vlc, &ex);
367
368     for (unsigned i = 0; i < 5; i++)
369     {
370         libvlc_media_list_add_media( ml, md, &ex );
371         catch ();
372     }
373
374     libvlc_media_list_player_set_media_list( mlp, ml, &ex );
375
376     libvlc_media_list_player_play_item_at_index( mlp, 0, &ex );
377     catch ();
378
379     libvlc_media_list_player_stop (mlp, &ex);
380     catch ();
381
382     libvlc_media_release (md);
383     catch ();
384
385     libvlc_media_list_player_release (mlp);
386     catch ();
387
388     libvlc_release (vlc);
389     catch ();
390 }
391
392 static void test_media_list_player_playback_options (const char** argv, int argc)
393 {
394     libvlc_instance_t *vlc;
395     libvlc_media_t *md;
396     libvlc_media_t *md2;
397     libvlc_media_t *md3;
398     libvlc_media_t *md4;
399     libvlc_media_t *md5;
400     libvlc_media_list_t *ml;
401     libvlc_media_list_t *ml2;
402     libvlc_media_list_t *ml3;
403     libvlc_media_list_t *ml4;
404     libvlc_media_list_t *ml5;
405     libvlc_media_list_t *ml6;
406     libvlc_media_list_player_t *mlp;
407     
408     const char * file = test_default_sample;
409     
410     log ("Testing media player playback options()\n");
411     
412     libvlc_exception_init (&ex);
413     vlc = libvlc_new (argc, argv, &ex);
414     catch ();
415     
416     /*
417      *   Create the following media tree:
418      *
419      *  ml1:            0 ---- 1 ---- 2
420      *                 /       |       \
421      *  ml2&4:      0 -- 1     |   0 -- 1 -- 2
422      *                         |
423      *  ml3:    0 -- 1 -- 2 -- 3 -- 4 -- 5 -- 6
424      *                    |                   |
425      *  ml5&6:            0                 0 -- 1
426      */
427
428     md = libvlc_media_new (vlc, file, &ex);
429     catch ();
430
431     md2 = libvlc_media_new (vlc, file, &ex);
432     catch ();
433
434     md3 = libvlc_media_new (vlc, file, &ex);
435     catch ();
436
437     md4 = libvlc_media_new (vlc, file, &ex);
438     catch ();
439
440     md5 = libvlc_media_new (vlc, file, &ex);
441     catch ();
442     
443     ml = libvlc_media_list_new (vlc, &ex);
444     catch ();
445     
446     ml2 = libvlc_media_list_new (vlc, &ex);
447     catch ();
448
449     ml3 = libvlc_media_list_new (vlc, &ex);
450     catch ();
451
452     ml4 = libvlc_media_list_new (vlc, &ex);
453     catch ();
454
455     ml5 = libvlc_media_list_new (vlc, &ex);
456     catch ();
457
458     ml6 = libvlc_media_list_new (vlc, &ex);
459     catch ();
460
461     media_list_add_file_path (vlc, ml2, file);
462     media_list_add_file_path (vlc, ml2, file);
463
464     media_list_add_file_path (vlc, ml3, file);
465     media_list_add_file_path (vlc, ml3, file);
466     libvlc_media_list_add_media (ml3, md4, &ex);
467     catch ();
468     media_list_add_file_path (vlc, ml3, file);
469     media_list_add_file_path (vlc, ml3, file);
470     media_list_add_file_path (vlc, ml3, file);
471     libvlc_media_list_add_media (ml3, md5, &ex);
472     catch ();
473
474     media_list_add_file_path (vlc, ml4, file);
475     media_list_add_file_path (vlc, ml4, file);
476     media_list_add_file_path (vlc, ml4, file);
477
478     media_list_add_file_path (vlc, ml5, file);
479
480     media_list_add_file_path (vlc, ml6, file);
481     media_list_add_file_path (vlc, ml6, file);
482
483     md->p_subitems = ml2;
484     md2->p_subitems = ml3;
485     md3->p_subitems = ml4;
486     md4->p_subitems = ml5;
487     md5->p_subitems = ml6;
488
489     libvlc_media_list_add_media (ml, md, &ex);
490     catch ();
491
492     libvlc_media_list_add_media (ml, md2, &ex);
493     catch ();
494
495     libvlc_media_list_add_media (ml, md3, &ex);
496     catch ();
497     
498     mlp = libvlc_media_list_player_new (vlc, &ex);
499     catch ();
500
501     libvlc_media_list_player_set_media_list (mlp, ml, &ex);
502     catch ();
503     
504     // Test default playback mode 
505     libvlc_media_list_player_set_playback_mode(mlp, libvlc_playback_mode_default, &ex);
506     catch ();
507
508     libvlc_media_list_player_play_item (mlp, md, &ex);
509     catch ();
510
511     libvlc_media_release (md);
512     catch ();
513     libvlc_media_release (md2);
514     catch ();
515     libvlc_media_release (md3);
516     catch ();
517     libvlc_media_release (md4);
518     catch ();
519     libvlc_media_release (md5);
520     catch ();
521
522     msleep(500000);
523
524     libvlc_media_list_player_stop (mlp, &ex);
525     catch ();
526
527     // Test looping playback mode
528     log ("Testing media player playback option - Loop\n");
529     libvlc_media_list_player_set_playback_mode(mlp, libvlc_playback_mode_loop, &ex);
530     catch ();
531
532     libvlc_media_list_player_play_item (mlp, md, &ex);
533     catch ();
534
535     msleep(500000);
536
537     libvlc_media_list_player_stop (mlp, &ex);
538     catch ();
539
540     // Test repeat playback mode
541     log ("Testing media player playback option - Repeat\n");
542     libvlc_media_list_player_set_playback_mode(mlp, libvlc_playback_mode_repeat, &ex);
543     catch ();
544
545     libvlc_media_list_player_play_item (mlp, md, &ex);
546     catch ();
547
548     msleep(500000);
549         
550     libvlc_media_list_player_release (mlp);
551     catch ();
552     
553     libvlc_release (vlc);
554     catch ();
555 }
556
557
558 int main (void)
559 {
560     test_init();
561
562     test_media_list_player_pause_stop (test_defaults_args, test_defaults_nargs);
563     test_media_list_player_play_item_at_index (test_defaults_args, test_defaults_nargs);
564     test_media_list_player_previous (test_defaults_args, test_defaults_nargs);
565     test_media_list_player_next (test_defaults_args, test_defaults_nargs);
566     test_media_list_player_items_queue (test_defaults_args, test_defaults_nargs);
567     test_media_list_player_playback_options (test_defaults_args, test_defaults_nargs);
568     return 0;
569 }