Fix compilation.
[vlc/asuraparaju-public.git] / test / libvlc / media_list_player.c
blobaf46b5e4ccb5122ff3ab826f4f6f678615d1fc98
1 /*
2 * media_list_player.c - libvlc smoke test
4 * $Id$
5 */
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 **********************************************************************/
24 #include "test.h"
26 // For msleep
27 #include <vlc_common.h>
28 #include <vlc_mtime.h>
30 #include "libvlc_additions.h"
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.
37 #include "../../src/control/media_internal.h"
39 struct check_items_order_data {
40 bool done_playing;
41 unsigned count;
42 unsigned index;
43 void * items[16];
46 static inline void check_data_init(struct check_items_order_data *check)
48 check->index = 0;
49 check->count = 0;
50 check->done_playing = false;
53 static inline void queue_expected_item(struct check_items_order_data *check, void *item)
55 assert(check->count < 16);
56 check->items[check->count] = item;
57 check->count++;
60 static inline void wait_queued_items(struct check_items_order_data *check)
62 // Wait dummily for check_items_order_callback() to flag 'done_playing':
63 while (!check->done_playing)
64 sched_yield();
67 static inline void wait_playing(libvlc_media_list_player_t *mlp)
69 while (!libvlc_media_list_player_is_playing (mlp))
70 sched_yield();
73 static inline void wait_stopped(libvlc_media_list_player_t *mlp)
75 while (libvlc_media_list_player_is_playing (mlp))
76 sched_yield();
79 static inline void stop_and_wait(libvlc_media_list_player_t *mlp)
81 libvlc_media_list_player_stop (mlp);
82 wait_stopped (mlp);
85 static void check_items_order_callback(const libvlc_event_t * p_event, void * user_data)
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)
92 char *title = libvlc_media_get_meta(md, libvlc_meta_Title);
93 log ("Got items %s\n", title);
94 free(title);
96 assert(checks->items[checks->index] == md);
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);
102 if (checks->index == (checks->count - 1))
104 log ("Done playing with success\n");
105 checks->done_playing = true;
107 checks->index++;
110 static void test_media_list_player_items_queue(const char** argv, int argc)
112 libvlc_instance_t *vlc;
113 libvlc_media_t *md;
114 libvlc_media_list_t *ml;
115 libvlc_media_list_player_t *mlp;
117 const char * file = test_default_sample;
119 log ("Testing media player item queue-ing\n");
121 vlc = libvlc_new (argc, argv);
122 assert (vlc != NULL);
124 md = libvlc_media_new_path (vlc, file);
125 assert(md);
127 ml = libvlc_media_list_new (vlc);
128 assert (ml != NULL);
130 mlp = libvlc_media_list_player_new (vlc);
131 assert(mlp);
133 libvlc_media_list_add_media (ml, md);
135 static struct check_items_order_data check;
136 check_data_init(&check);
137 queue_expected_item(&check, md);
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));
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);
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);
157 libvlc_media_list_player_set_media_list (mlp, ml);
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);
164 libvlc_media_list_player_play(mlp);
166 // Wait until all item are read
167 wait_queued_items(&check);
169 stop_and_wait (mlp);
171 libvlc_media_list_player_release (mlp);
172 libvlc_release (vlc);
175 static void test_media_list_player_previous(const char** argv, int argc)
177 libvlc_instance_t *vlc;
178 libvlc_media_t *md;
179 libvlc_media_list_t *ml;
180 libvlc_media_list_player_t *mlp;
182 const char * file = test_default_sample;
184 log ("Testing media player previous()\n");
186 vlc = libvlc_new (argc, argv);
187 assert (vlc != NULL);
189 md = libvlc_media_new_path (vlc, file);
190 assert(md);
192 ml = libvlc_media_list_new (vlc);
193 assert (ml != NULL);
195 mlp = libvlc_media_list_player_new (vlc);
196 assert(mlp);;
198 libvlc_media_list_add_media (ml, md);
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);
205 libvlc_media_list_player_set_media_list (mlp, ml);
207 libvlc_media_list_player_play_item (mlp, md);
209 wait_playing (mlp);
211 libvlc_media_release (md);
213 libvlc_media_list_player_previous (mlp);
215 wait_playing (mlp);
217 libvlc_media_list_player_pause (mlp);
218 libvlc_media_list_player_previous (mlp);
220 wait_playing (mlp);
222 stop_and_wait (mlp);
224 libvlc_media_list_player_previous (mlp);
226 wait_playing (mlp);
228 stop_and_wait (mlp);
230 libvlc_media_list_player_release (mlp);
231 libvlc_release (vlc);
234 static void test_media_list_player_next(const char** argv, int argc)
236 libvlc_instance_t *vlc;
237 libvlc_media_t *md;
238 libvlc_media_list_t *ml;
239 libvlc_media_list_player_t *mlp;
241 const char * file = test_default_sample;
243 log ("Testing media player next()\n");
245 vlc = libvlc_new (argc, argv);
246 assert (vlc != NULL);
248 md = libvlc_media_new_path (vlc, file);
249 assert(md);
251 ml = libvlc_media_list_new (vlc);
252 assert (ml != NULL);
254 mlp = libvlc_media_list_player_new (vlc);
255 assert(mlp);
257 libvlc_media_list_add_media (ml, md);
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);
264 libvlc_media_list_player_set_media_list (mlp, ml);
266 libvlc_media_list_player_play_item (mlp, md);
268 libvlc_media_release (md);
270 wait_playing (mlp);
272 libvlc_media_list_player_next (mlp);
274 wait_playing (mlp);
276 libvlc_media_list_player_pause (mlp);
277 libvlc_media_list_player_next (mlp);
279 wait_playing (mlp);
281 stop_and_wait (mlp);
283 libvlc_media_list_player_next (mlp);
285 wait_playing (mlp);
287 stop_and_wait (mlp);
289 libvlc_media_list_player_release (mlp);
290 libvlc_release (vlc);
293 static void test_media_list_player_pause_stop(const char** argv, int argc)
295 libvlc_instance_t *vlc;
296 libvlc_media_t *md;
297 libvlc_media_list_t *ml;
298 libvlc_media_list_player_t *mlp;
300 const char * file = test_default_sample;
302 log ("Testing play and pause of %s using the media list.\n", file);
304 vlc = libvlc_new (argc, argv);
305 assert (vlc != NULL);
307 md = libvlc_media_new_path (vlc, file);
308 assert(md);
310 ml = libvlc_media_list_new (vlc);
311 assert (ml != NULL);
313 mlp = libvlc_media_list_player_new (vlc);
314 assert(mlp);
316 libvlc_media_list_add_media( ml, md);
318 libvlc_media_list_player_set_media_list( mlp, ml );
320 libvlc_media_list_player_play_item( mlp, md );
322 wait_playing (mlp);
324 libvlc_media_list_player_pause (mlp);
326 stop_and_wait (mlp);
328 libvlc_media_release (md);
329 libvlc_media_list_player_release (mlp);
330 libvlc_release (vlc);
333 static void test_media_list_player_play_item_at_index(const char** argv, int argc)
335 libvlc_instance_t *vlc;
336 libvlc_media_t *md;
337 libvlc_media_list_t *ml;
338 libvlc_media_list_player_t *mlp;
340 const char * file = test_default_sample;
342 log ("Testing play_item_at_index of %s using the media list.\n", file);
344 vlc = libvlc_new (argc, argv);
345 assert (vlc != NULL);
347 md = libvlc_media_new_path (vlc, file);
348 assert(md);
350 ml = libvlc_media_list_new (vlc);
351 assert (ml != NULL);
353 mlp = libvlc_media_list_player_new (vlc);
354 assert(mlp);
356 for (unsigned i = 0; i < 5; i++)
357 libvlc_media_list_add_media( ml, md );
359 libvlc_media_list_player_set_media_list( mlp, ml );
360 libvlc_media_list_player_play_item_at_index( mlp, 0 );
362 wait_playing (mlp);
364 stop_and_wait (mlp);
366 libvlc_media_release (md);
367 libvlc_media_list_player_release (mlp);
368 libvlc_release (vlc);
371 static void test_media_list_player_playback_options (const char** argv, int argc)
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;
387 const char * file = test_default_sample;
389 log ("Testing media player playback options()\n");
391 vlc = libvlc_new (argc, argv);
392 assert (vlc != NULL);
395 * Create the following media tree:
397 * ml1: 0 ---- 1 ---- 2
398 * / | \
399 * ml2&4: 0 -- 1 | 0 -- 1 -- 2
401 * ml3: 0 -- 1 -- 2 -- 3 -- 4 -- 5 -- 6
402 * | |
403 * ml5&6: 0 0 -- 1
406 md = libvlc_media_new_path (vlc, file);
407 assert(md);
409 md2 = libvlc_media_new_path (vlc, file);
410 assert(md2);
412 md3 = libvlc_media_new_path (vlc, file);
413 assert(md3);
415 md4 = libvlc_media_new_path (vlc, file);
416 assert(md4);
418 md5 = libvlc_media_new_path (vlc, file);
419 assert(md5);
421 ml = libvlc_media_list_new (vlc);
422 assert (ml != NULL);
424 ml2 = libvlc_media_list_new (vlc);
425 assert (ml2 != NULL);
427 ml3 = libvlc_media_list_new (vlc);
428 assert (ml3 != NULL);
430 ml4 = libvlc_media_list_new (vlc);
431 assert (ml4 != NULL);
433 ml5 = libvlc_media_list_new (vlc);
434 assert (ml5 != NULL);
436 ml6 = libvlc_media_list_new (vlc);
437 assert (ml6 != NULL);
439 media_list_add_file_path (vlc, ml2, file);
440 media_list_add_file_path (vlc, ml2, file);
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);
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);
454 media_list_add_file_path (vlc, ml5, file);
456 media_list_add_file_path (vlc, ml6, file);
457 media_list_add_file_path (vlc, ml6, file);
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;
465 libvlc_media_list_add_media (ml, md);
466 libvlc_media_list_add_media (ml, md2);
467 libvlc_media_list_add_media (ml, md3);
469 mlp = libvlc_media_list_player_new (vlc);
470 assert(mlp);
472 libvlc_media_list_player_set_media_list (mlp, ml);
474 // Test default playback mode
475 libvlc_media_list_player_set_playback_mode(mlp, libvlc_playback_mode_default);
477 libvlc_media_list_player_play_item (mlp, md);
479 wait_playing (mlp);
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);
487 libvlc_media_list_player_stop (mlp);
489 while (libvlc_media_list_player_is_playing (mlp))
490 sched_yield();
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);
496 libvlc_media_list_player_play_item (mlp, md);
498 wait_playing (mlp);
500 stop_and_wait (mlp);
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);
506 libvlc_media_list_player_play_item (mlp, md);
508 wait_playing (mlp);
510 stop_and_wait (mlp);
512 libvlc_media_list_player_release (mlp);
513 libvlc_release (vlc);
517 int main (void)
519 test_init();
521 // There are 6 tests. And they take some times.
522 alarm(6 * 5);
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;