]> git.sesse.net Git - vlc/blob - test/libvlc/media_list_player.c
4b2201a9cb1f06eebc53d6e5054cc7de81f43721
[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);
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);
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);
137     queue_expected_item(&check, media_list_add_file_path(vlc, subitems, file));
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     libvlc_media_list_release(subitems);
141
142     libvlc_media_list_player_set_media_list (mlp, ml, &ex);
143
144     libvlc_event_manager_t * em = libvlc_media_list_player_event_manager(mlp);
145     libvlc_event_attach(em, libvlc_MediaListPlayerNextItemSet, check_items_order_callback, &check, &ex);
146     catch ();
147
148     libvlc_media_list_player_play(mlp, &ex);
149     catch ();
150
151     // Wait until all item are read
152     wait_queued_items(&check);
153
154     libvlc_media_list_player_stop (mlp, &ex);
155     catch ();
156
157     while (libvlc_media_list_player_is_playing (mlp, &ex))
158         catch() ;
159
160     libvlc_media_list_player_release (mlp);
161     catch ();
162
163     libvlc_release (vlc);
164     catch ();
165 }
166
167 static void test_media_list_player_previous(const char** argv, int argc)
168 {
169     libvlc_instance_t *vlc;
170     libvlc_media_t *md;
171     libvlc_media_list_t *ml;
172     libvlc_media_list_player_t *mlp;
173
174     const char * file = test_default_sample;
175
176     log ("Testing media player previous()\n");
177
178     libvlc_exception_init (&ex);
179     vlc = libvlc_new (argc, argv, &ex);
180     catch ();
181
182     md = libvlc_media_new (vlc, file, &ex);
183     catch ();
184
185     ml = libvlc_media_list_new (vlc, &ex);
186     catch ();
187
188     mlp = libvlc_media_list_player_new (vlc, &ex);
189     catch ();
190
191     libvlc_media_list_add_media (ml, md, &ex);
192     catch ();
193
194     // Add three media
195     media_list_add_file_path (vlc, ml, file);
196     media_list_add_file_path (vlc, ml, file);
197     media_list_add_file_path (vlc, ml, file);
198
199     libvlc_media_list_player_set_media_list (mlp, ml, &ex);
200
201     libvlc_media_list_player_play_item (mlp, md, &ex);
202     catch ();
203
204     while (!libvlc_media_list_player_is_playing (mlp, &ex))
205         catch() ;
206
207     libvlc_media_release (md);
208
209     libvlc_media_list_player_previous (mlp, &ex);
210     catch ();
211
212     while (!libvlc_media_list_player_is_playing (mlp, &ex))
213         catch() ;
214
215     libvlc_media_list_player_pause (mlp, &ex);
216     catch();
217
218     libvlc_media_list_player_previous (mlp, &ex);
219     catch ();
220
221     while (!libvlc_media_list_player_is_playing (mlp, &ex))
222         catch() ;
223
224     libvlc_media_list_player_stop (mlp, &ex);
225     catch ();
226
227     while (libvlc_media_list_player_is_playing (mlp, &ex))
228         catch() ;
229
230     libvlc_media_list_player_previous (mlp, &ex);
231     catch ();
232
233     while (!libvlc_media_list_player_is_playing (mlp, &ex))
234         catch() ;
235
236     libvlc_media_list_player_stop (mlp, &ex);
237     catch ();
238
239     while (libvlc_media_list_player_is_playing (mlp, &ex))
240         catch() ;
241
242     libvlc_media_list_player_release (mlp);
243     catch ();
244
245     libvlc_release (vlc);
246     catch ();
247 }
248
249 static void test_media_list_player_next(const char** argv, int argc)
250 {
251     libvlc_instance_t *vlc;
252     libvlc_media_t *md;
253     libvlc_media_list_t *ml;
254     libvlc_media_list_player_t *mlp;
255
256     const char * file = test_default_sample;
257
258     log ("Testing media player next()\n");
259
260     libvlc_exception_init (&ex);
261     vlc = libvlc_new (argc, argv, &ex);
262     catch ();
263
264     md = libvlc_media_new (vlc, file, &ex);
265     catch ();
266
267     ml = libvlc_media_list_new (vlc, &ex);
268     catch ();
269
270     mlp = libvlc_media_list_player_new (vlc, &ex);
271     catch ();
272
273     libvlc_media_list_add_media (ml, md, &ex);
274     catch ();
275
276     // Add three media
277     media_list_add_file_path (vlc, ml, file);
278     media_list_add_file_path (vlc, ml, file);
279     media_list_add_file_path (vlc, ml, file);
280
281     libvlc_media_list_player_set_media_list (mlp, ml, &ex);
282
283     libvlc_media_list_player_play_item (mlp, md, &ex);
284     catch ();
285
286     libvlc_media_release (md);
287
288     while (!libvlc_media_list_player_is_playing (mlp, &ex))
289         catch() ;
290
291     libvlc_media_list_player_next (mlp, &ex);
292     catch ();
293
294     while (!libvlc_media_list_player_is_playing (mlp, &ex))
295         catch() ;
296
297     libvlc_media_list_player_pause (mlp, &ex);
298     catch();
299
300     libvlc_media_list_player_next (mlp, &ex);
301     catch ();
302
303     while (!libvlc_media_list_player_is_playing (mlp, &ex))
304         catch() ;
305
306     libvlc_media_list_player_stop (mlp, &ex);
307     catch ();
308
309     while (libvlc_media_list_player_is_playing (mlp, &ex))
310         catch() ;
311
312     libvlc_media_list_player_next (mlp, &ex);
313     catch ();
314
315     while (!libvlc_media_list_player_is_playing (mlp, &ex))
316         catch() ;
317
318     libvlc_media_list_player_stop (mlp, &ex);
319     catch ();
320
321     while (libvlc_media_list_player_is_playing (mlp, &ex))
322         catch() ;
323
324     libvlc_media_list_player_release (mlp);
325     catch ();
326
327     libvlc_release (vlc);
328     catch ();
329 }
330
331 static void test_media_list_player_pause_stop(const char** argv, int argc)
332 {
333     libvlc_instance_t *vlc;
334     libvlc_media_t *md;
335     libvlc_media_list_t *ml;
336     libvlc_media_list_player_t *mlp;
337
338     const char * file = test_default_sample;
339
340     log ("Testing play and pause of %s using the media list.\n", file);
341
342     libvlc_exception_init (&ex);
343     vlc = libvlc_new (argc, argv, &ex);
344     catch ();
345
346     md = libvlc_media_new (vlc, file, &ex);
347     catch ();
348
349     ml = libvlc_media_list_new (vlc, &ex);
350     catch ();
351
352     mlp = libvlc_media_list_player_new (vlc, &ex);
353
354     libvlc_media_list_add_media( ml, md, &ex );
355     catch ();
356
357     libvlc_media_list_player_set_media_list( mlp, ml, &ex );
358
359     libvlc_media_list_player_play_item( mlp, md, &ex );
360     catch ();
361
362     while (!libvlc_media_list_player_is_playing (mlp, &ex))
363         catch() ;
364
365     libvlc_media_list_player_pause (mlp, &ex);
366     catch();
367
368     libvlc_media_list_player_stop (mlp, &ex);
369     catch ();
370
371     while (libvlc_media_list_player_is_playing (mlp, &ex))
372         catch() ;
373
374     libvlc_media_release (md);
375
376     libvlc_media_list_player_release (mlp);
377     catch ();
378
379     libvlc_release (vlc);
380     catch ();
381 }
382
383 static void test_media_list_player_play_item_at_index(const char** argv, int argc)
384 {
385     libvlc_instance_t *vlc;
386     libvlc_media_t *md;
387     libvlc_media_list_t *ml;
388     libvlc_media_list_player_t *mlp;
389
390     const char * file = test_default_sample;
391
392     log ("Testing play_item_at_index of %s using the media list.\n", file);
393
394     libvlc_exception_init (&ex);
395     vlc = libvlc_new (argc, argv, &ex);
396     catch ();
397
398     md = libvlc_media_new (vlc, file, &ex);
399     catch ();
400
401     ml = libvlc_media_list_new (vlc, &ex);
402     catch ();
403
404     mlp = libvlc_media_list_player_new (vlc, &ex);
405
406     for (unsigned i = 0; i < 5; i++)
407     {
408         libvlc_media_list_add_media( ml, md, &ex );
409         catch ();
410     }
411
412     libvlc_media_list_player_set_media_list( mlp, ml, &ex );
413
414     libvlc_media_list_player_play_item_at_index( mlp, 0, &ex );
415     catch ();
416
417     while (!libvlc_media_list_player_is_playing (mlp, &ex))
418         catch() ;
419
420     libvlc_media_list_player_stop (mlp, &ex);
421     catch ();
422
423     while (libvlc_media_list_player_is_playing (mlp, &ex))
424         catch() ;
425
426     libvlc_media_release (md);
427     catch ();
428
429     libvlc_media_list_player_release (mlp);
430     catch ();
431
432     libvlc_release (vlc);
433     catch ();
434 }
435
436 static void test_media_list_player_playback_options (const char** argv, int argc)
437 {
438     libvlc_instance_t *vlc;
439     libvlc_media_t *md;
440     libvlc_media_t *md2;
441     libvlc_media_t *md3;
442     libvlc_media_t *md4;
443     libvlc_media_t *md5;
444     libvlc_media_list_t *ml;
445     libvlc_media_list_t *ml2;
446     libvlc_media_list_t *ml3;
447     libvlc_media_list_t *ml4;
448     libvlc_media_list_t *ml5;
449     libvlc_media_list_t *ml6;
450     libvlc_media_list_player_t *mlp;
451
452     const char * file = test_default_sample;
453
454     log ("Testing media player playback options()\n");
455
456     libvlc_exception_init (&ex);
457     vlc = libvlc_new (argc, argv, &ex);
458     catch ();
459
460     /*
461      *   Create the following media tree:
462      *
463      *  ml1:            0 ---- 1 ---- 2
464      *                 /       |       \
465      *  ml2&4:      0 -- 1     |   0 -- 1 -- 2
466      *                         |
467      *  ml3:    0 -- 1 -- 2 -- 3 -- 4 -- 5 -- 6
468      *                    |                   |
469      *  ml5&6:            0                 0 -- 1
470      */
471
472     md = libvlc_media_new (vlc, file, &ex);
473     catch ();
474
475     md2 = libvlc_media_new (vlc, file, &ex);
476     catch ();
477
478     md3 = libvlc_media_new (vlc, file, &ex);
479     catch ();
480
481     md4 = libvlc_media_new (vlc, file, &ex);
482     catch ();
483
484     md5 = libvlc_media_new (vlc, file, &ex);
485     catch ();
486
487     ml = libvlc_media_list_new (vlc, &ex);
488     catch ();
489
490     ml2 = libvlc_media_list_new (vlc, &ex);
491     catch ();
492
493     ml3 = libvlc_media_list_new (vlc, &ex);
494     catch ();
495
496     ml4 = libvlc_media_list_new (vlc, &ex);
497     catch ();
498
499     ml5 = libvlc_media_list_new (vlc, &ex);
500     catch ();
501
502     ml6 = libvlc_media_list_new (vlc, &ex);
503     catch ();
504
505     media_list_add_file_path (vlc, ml2, file);
506     media_list_add_file_path (vlc, ml2, file);
507
508     media_list_add_file_path (vlc, ml3, file);
509     media_list_add_file_path (vlc, ml3, file);
510     libvlc_media_list_add_media (ml3, md4, &ex);
511     catch ();
512     media_list_add_file_path (vlc, ml3, file);
513     media_list_add_file_path (vlc, ml3, file);
514     media_list_add_file_path (vlc, ml3, file);
515     libvlc_media_list_add_media (ml3, md5, &ex);
516     catch ();
517
518     media_list_add_file_path (vlc, ml4, file);
519     media_list_add_file_path (vlc, ml4, file);
520     media_list_add_file_path (vlc, ml4, file);
521
522     media_list_add_file_path (vlc, ml5, file);
523
524     media_list_add_file_path (vlc, ml6, file);
525     media_list_add_file_path (vlc, ml6, file);
526
527     md->p_subitems = ml2;
528     md2->p_subitems = ml3;
529     md3->p_subitems = ml4;
530     md4->p_subitems = ml5;
531     md5->p_subitems = ml6;
532
533     libvlc_media_list_add_media (ml, md, &ex);
534     catch ();
535
536     libvlc_media_list_add_media (ml, md2, &ex);
537     catch ();
538
539     libvlc_media_list_add_media (ml, md3, &ex);
540     catch ();
541
542     mlp = libvlc_media_list_player_new (vlc, &ex);
543     catch ();
544
545     libvlc_media_list_player_set_media_list (mlp, ml, &ex);
546     catch ();
547
548     // Test default playback mode
549     libvlc_media_list_player_set_playback_mode(mlp, libvlc_playback_mode_default, &ex);
550     catch ();
551
552     libvlc_media_list_player_play_item (mlp, md, &ex);
553     catch ();
554
555     while (!libvlc_media_list_player_is_playing (mlp, &ex))
556         catch() ;
557
558     libvlc_media_release (md);
559     catch ();
560     libvlc_media_release (md2);
561     catch ();
562     libvlc_media_release (md3);
563     catch ();
564     libvlc_media_release (md4);
565     catch ();
566     libvlc_media_release (md5);
567     catch ();
568
569     libvlc_media_list_player_stop (mlp, &ex);
570     catch ();
571
572     while (libvlc_media_list_player_is_playing (mlp, &ex))
573         catch() ;
574
575     // Test looping playback mode
576     log ("Testing media player playback option - Loop\n");
577     libvlc_media_list_player_set_playback_mode(mlp, libvlc_playback_mode_loop, &ex);
578     catch ();
579
580     libvlc_media_list_player_play_item (mlp, md, &ex);
581     catch ();
582
583     while (!libvlc_media_list_player_is_playing (mlp, &ex))
584         catch() ;
585
586     libvlc_media_list_player_stop (mlp, &ex);
587     catch ();
588
589     while (libvlc_media_list_player_is_playing (mlp, &ex))
590         catch() ;
591
592     // Test repeat playback mode
593     log ("Testing media player playback option - Repeat\n");
594     libvlc_media_list_player_set_playback_mode(mlp, libvlc_playback_mode_repeat, &ex);
595     catch ();
596
597     libvlc_media_list_player_play_item (mlp, md, &ex);
598     catch ();
599
600     while (!libvlc_media_list_player_is_playing (mlp, &ex))
601         catch() ;
602
603     libvlc_media_list_player_stop (mlp, &ex);
604     catch ();
605
606     while (libvlc_media_list_player_is_playing (mlp, &ex))
607         catch() ;
608
609     libvlc_media_list_player_release (mlp);
610     catch ();
611
612     libvlc_release (vlc);
613     catch ();
614 }
615
616
617 int main (void)
618 {
619     test_init();
620
621     test_media_list_player_pause_stop (test_defaults_args, test_defaults_nargs);
622     test_media_list_player_play_item_at_index (test_defaults_args, test_defaults_nargs);
623     test_media_list_player_previous (test_defaults_args, test_defaults_nargs);
624     test_media_list_player_next (test_defaults_args, test_defaults_nargs);
625     test_media_list_player_items_queue (test_defaults_args, test_defaults_nargs);
626     test_media_list_player_playback_options (test_defaults_args, test_defaults_nargs);
627     return 0;
628 }