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