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