]> git.sesse.net Git - vlc/blob - test/libvlc/media_list_player.c
0a59b23f309d62058a4327f89fd30048aa842530
[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);
111     assert (ml != NULL);
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);
143
144     libvlc_event_manager_t * em = libvlc_media_list_player_event_manager(mlp);
145     int val = libvlc_event_attach(em, libvlc_MediaListPlayerNextItemSet,
146                                   check_items_order_callback, &check);
147     assert(val == 0);
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);
156     while (libvlc_media_list_player_is_playing (mlp))
157         sched_yield();
158
159     libvlc_media_list_player_release (mlp);
160     libvlc_release (vlc);
161 }
162
163 static void test_media_list_player_previous(const char** argv, int argc)
164 {
165     libvlc_instance_t *vlc;
166     libvlc_media_t *md;
167     libvlc_media_list_t *ml;
168     libvlc_media_list_player_t *mlp;
169
170     const char * file = test_default_sample;
171
172     log ("Testing media player previous()\n");
173
174     libvlc_exception_init (&ex);
175     vlc = libvlc_new (argc, argv, &ex);
176     catch ();
177
178     md = libvlc_media_new (vlc, file, &ex);
179     catch ();
180
181     ml = libvlc_media_list_new (vlc);
182     assert (ml != NULL);
183
184     mlp = libvlc_media_list_player_new (vlc, &ex);
185     catch ();
186
187     libvlc_media_list_add_media (ml, md, &ex);
188     catch ();
189
190     // Add three media
191     media_list_add_file_path (vlc, ml, file);
192     media_list_add_file_path (vlc, ml, file);
193     media_list_add_file_path (vlc, ml, file);
194
195     libvlc_media_list_player_set_media_list (mlp, ml);
196
197     libvlc_media_list_player_play_item (mlp, md, &ex);
198     catch ();
199
200     while (!libvlc_media_list_player_is_playing (mlp))
201         sched_yield();
202
203     libvlc_media_release (md);
204
205     libvlc_media_list_player_previous (mlp, &ex);
206     catch ();
207
208     while (!libvlc_media_list_player_is_playing (mlp))
209         sched_yield();
210
211     libvlc_media_list_player_pause (mlp, &ex);
212     catch();
213
214     libvlc_media_list_player_previous (mlp, &ex);
215     catch ();
216
217     while (!libvlc_media_list_player_is_playing (mlp))
218         sched_yield();
219
220     libvlc_media_list_player_stop (mlp);
221
222     while (libvlc_media_list_player_is_playing (mlp))
223         sched_yield();
224
225     libvlc_media_list_player_previous (mlp, &ex);
226     catch ();
227
228     while (!libvlc_media_list_player_is_playing (mlp))
229         sched_yield();
230
231     libvlc_media_list_player_stop (mlp);
232
233     while (libvlc_media_list_player_is_playing (mlp))
234         sched_yield();
235
236     libvlc_media_list_player_release (mlp);
237     libvlc_release (vlc);
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);
259     assert (ml != NULL);
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);
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))
280         sched_yield();
281
282     libvlc_media_list_player_next (mlp, &ex);
283     catch ();
284
285     while (!libvlc_media_list_player_is_playing (mlp))
286         sched_yield();
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))
295         sched_yield();
296
297     libvlc_media_list_player_stop (mlp);
298
299     while (libvlc_media_list_player_is_playing (mlp))
300         sched_yield();
301
302     libvlc_media_list_player_next (mlp, &ex);
303     catch ();
304
305     while (!libvlc_media_list_player_is_playing (mlp))
306         sched_yield();
307
308     libvlc_media_list_player_stop (mlp);
309     while (libvlc_media_list_player_is_playing (mlp))
310         sched_yield();
311
312     libvlc_media_list_player_release (mlp);
313     libvlc_release (vlc);
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);
335     assert (ml != NULL);
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 );
343
344     libvlc_media_list_player_play_item( mlp, md, &ex );
345     catch ();
346
347     while (!libvlc_media_list_player_is_playing (mlp))
348         sched_yield();
349
350     libvlc_media_list_player_pause (mlp, &ex);
351     catch();
352
353     libvlc_media_list_player_stop (mlp);
354
355     while (libvlc_media_list_player_is_playing (mlp))
356         sched_yield();
357
358     libvlc_media_release (md);
359     libvlc_media_list_player_release (mlp);
360     libvlc_release (vlc);
361 }
362
363 static void test_media_list_player_play_item_at_index(const char** argv, int argc)
364 {
365     libvlc_instance_t *vlc;
366     libvlc_media_t *md;
367     libvlc_media_list_t *ml;
368     libvlc_media_list_player_t *mlp;
369
370     const char * file = test_default_sample;
371
372     log ("Testing play_item_at_index of %s using the media list.\n", file);
373
374     libvlc_exception_init (&ex);
375     vlc = libvlc_new (argc, argv, &ex);
376     catch ();
377
378     md = libvlc_media_new (vlc, file, &ex);
379     catch ();
380
381     ml = libvlc_media_list_new (vlc);
382     assert (ml != NULL);
383
384     mlp = libvlc_media_list_player_new (vlc, &ex);
385
386     for (unsigned i = 0; i < 5; i++)
387     {
388         libvlc_media_list_add_media( ml, md, &ex );
389         catch ();
390     }
391
392     libvlc_media_list_player_set_media_list( mlp, ml );
393
394     libvlc_media_list_player_play_item_at_index( mlp, 0, &ex );
395     catch ();
396
397     while (!libvlc_media_list_player_is_playing (mlp))
398         sched_yield();
399
400     libvlc_media_list_player_stop (mlp);
401
402     while (libvlc_media_list_player_is_playing (mlp))
403         sched_yield();
404
405     libvlc_media_release (md);
406     libvlc_media_list_player_release (mlp);
407     libvlc_release (vlc);
408 }
409
410 static void test_media_list_player_playback_options (const char** argv, int argc)
411 {
412     libvlc_instance_t *vlc;
413     libvlc_media_t *md;
414     libvlc_media_t *md2;
415     libvlc_media_t *md3;
416     libvlc_media_t *md4;
417     libvlc_media_t *md5;
418     libvlc_media_list_t *ml;
419     libvlc_media_list_t *ml2;
420     libvlc_media_list_t *ml3;
421     libvlc_media_list_t *ml4;
422     libvlc_media_list_t *ml5;
423     libvlc_media_list_t *ml6;
424     libvlc_media_list_player_t *mlp;
425
426     const char * file = test_default_sample;
427
428     log ("Testing media player playback options()\n");
429
430     libvlc_exception_init (&ex);
431     vlc = libvlc_new (argc, argv, &ex);
432     catch ();
433
434     /*
435      *   Create the following media tree:
436      *
437      *  ml1:            0 ---- 1 ---- 2
438      *                 /       |       \
439      *  ml2&4:      0 -- 1     |   0 -- 1 -- 2
440      *                         |
441      *  ml3:    0 -- 1 -- 2 -- 3 -- 4 -- 5 -- 6
442      *                    |                   |
443      *  ml5&6:            0                 0 -- 1
444      */
445
446     md = libvlc_media_new (vlc, file, &ex);
447     catch ();
448
449     md2 = libvlc_media_new (vlc, file, &ex);
450     catch ();
451
452     md3 = libvlc_media_new (vlc, file, &ex);
453     catch ();
454
455     md4 = libvlc_media_new (vlc, file, &ex);
456     catch ();
457
458     md5 = libvlc_media_new (vlc, file, &ex);
459     catch ();
460
461     ml = libvlc_media_list_new (vlc);
462     assert (ml != NULL);
463
464     ml2 = libvlc_media_list_new (vlc);
465     assert (ml2 != NULL);
466
467     ml3 = libvlc_media_list_new (vlc);
468     assert (ml3 != NULL);
469
470     ml4 = libvlc_media_list_new (vlc);
471     assert (ml4 != NULL);
472
473     ml5 = libvlc_media_list_new (vlc);
474     assert (ml5 != NULL);
475
476     ml6 = libvlc_media_list_new (vlc);
477     assert (ml6 != NULL);
478
479     media_list_add_file_path (vlc, ml2, file);
480     media_list_add_file_path (vlc, ml2, file);
481
482     media_list_add_file_path (vlc, ml3, file);
483     media_list_add_file_path (vlc, ml3, file);
484     libvlc_media_list_add_media (ml3, md4, &ex);
485     catch ();
486     media_list_add_file_path (vlc, ml3, file);
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, md5, &ex);
490     catch ();
491
492     media_list_add_file_path (vlc, ml4, file);
493     media_list_add_file_path (vlc, ml4, file);
494     media_list_add_file_path (vlc, ml4, file);
495
496     media_list_add_file_path (vlc, ml5, file);
497
498     media_list_add_file_path (vlc, ml6, file);
499     media_list_add_file_path (vlc, ml6, file);
500
501     md->p_subitems = ml2;
502     md2->p_subitems = ml3;
503     md3->p_subitems = ml4;
504     md4->p_subitems = ml5;
505     md5->p_subitems = ml6;
506
507     libvlc_media_list_add_media (ml, md, &ex);
508     catch ();
509
510     libvlc_media_list_add_media (ml, md2, &ex);
511     catch ();
512
513     libvlc_media_list_add_media (ml, md3, &ex);
514     catch ();
515
516     mlp = libvlc_media_list_player_new (vlc, &ex);
517     catch ();
518
519     libvlc_media_list_player_set_media_list (mlp, ml);
520
521     // Test default playback mode
522     libvlc_media_list_player_set_playback_mode(mlp, libvlc_playback_mode_default);
523
524     libvlc_media_list_player_play_item (mlp, md, &ex);
525     catch ();
526
527     while (!libvlc_media_list_player_is_playing (mlp))
528         sched_yield();
529
530     libvlc_media_release (md);
531     libvlc_media_release (md2);
532     libvlc_media_release (md3);
533     libvlc_media_release (md4);
534     libvlc_media_release (md5);
535
536     libvlc_media_list_player_stop (mlp);
537
538     while (libvlc_media_list_player_is_playing (mlp))
539         sched_yield();
540
541     // Test looping playback mode
542     log ("Testing media player playback option - Loop\n");
543     libvlc_media_list_player_set_playback_mode(mlp, libvlc_playback_mode_loop);
544
545     libvlc_media_list_player_play_item (mlp, md, &ex);
546     catch ();
547
548     while (!libvlc_media_list_player_is_playing (mlp))
549         sched_yield();
550
551     libvlc_media_list_player_stop (mlp);
552
553     while (libvlc_media_list_player_is_playing (mlp))
554         sched_yield();
555
556     // Test repeat playback mode
557     log ("Testing media player playback option - Repeat\n");
558     libvlc_media_list_player_set_playback_mode(mlp, libvlc_playback_mode_repeat);
559
560     libvlc_media_list_player_play_item (mlp, md, &ex);
561     catch ();
562
563     while (!libvlc_media_list_player_is_playing (mlp))
564         sched_yield();
565
566     libvlc_media_list_player_stop (mlp);
567
568     while (libvlc_media_list_player_is_playing (mlp))
569         sched_yield();
570
571     libvlc_media_list_player_release (mlp);
572     libvlc_release (vlc);
573 }
574
575
576 int main (void)
577 {
578     test_init();
579
580     test_media_list_player_pause_stop (test_defaults_args, test_defaults_nargs);
581     test_media_list_player_play_item_at_index (test_defaults_args, test_defaults_nargs);
582     test_media_list_player_previous (test_defaults_args, test_defaults_nargs);
583     test_media_list_player_next (test_defaults_args, test_defaults_nargs);
584     test_media_list_player_items_queue (test_defaults_args, test_defaults_nargs);
585     test_media_list_player_playback_options (test_defaults_args, test_defaults_nargs);
586     return 0;
587 }