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