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