]> git.sesse.net Git - vlc/blob - test/libvlc/media_list_player.c
media_list_player: Make sure we'll correctly play next item, instead of setting the...
[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 static void* media_list_add_file_path(libvlc_instance_t *vlc, libvlc_media_list_t *ml, const char * file_path)
31 {
32     libvlc_media_t *md = libvlc_media_new (vlc, file_path, &ex);
33     catch ();
34
35     libvlc_media_list_add_media (ml, md, &ex);
36     catch ();
37
38     libvlc_media_release (md);
39     return md;
40 }
41
42 static bool done_playing = false;
43 static const unsigned items_count = 4;
44 static void * items[4];
45 static unsigned current_index = 0;
46
47 static void next_item_callback(const libvlc_event_t * p_event, void * user_data)
48 {
49     (void)user_data;
50     libvlc_media_t *md = p_event->u.media_list_player_next_item_set.item;
51     current_index++;
52     assert(current_index < items_count);    
53     assert(items[current_index] == md);
54     log ("Item %d was correctly queued\n", current_index);
55     if (current_index == (items_count - 1))
56         done_playing = true;
57 }
58
59 static void test_media_list_player_items_queue(const char** argv, int argc)
60 {
61     libvlc_instance_t *vlc;
62     libvlc_media_t *md;
63     libvlc_media_list_t *ml;
64     libvlc_media_list_player_t *mlp;
65     
66     const char * file = test_default_sample;
67     
68     log ("Testing media player item queue-ing\n");
69     
70     libvlc_exception_init (&ex);
71     vlc = libvlc_new (argc, argv, &ex);
72     catch ();
73     
74     md = libvlc_media_new (vlc, file, &ex);
75     catch ();
76     
77     ml = libvlc_media_list_new (vlc, &ex);
78     catch ();
79     
80     mlp = libvlc_media_list_player_new (vlc, &ex);
81     catch ();
82     
83     libvlc_media_list_add_media (ml, md, &ex);
84     catch ();
85     
86     items[0] = md;
87
88     // Add three more media
89     items[1] = media_list_add_file_path (vlc, ml, file);
90     items[2] = media_list_add_file_path (vlc, ml, file);
91     items[3] = media_list_add_file_path (vlc, ml, file);
92     
93     libvlc_media_list_player_set_media_list (mlp, ml, &ex);
94
95     libvlc_event_manager_t * em = libvlc_media_list_player_event_manager(mlp);
96     libvlc_event_attach(em, libvlc_MediaListPlayerNextItemSet, next_item_callback, NULL, &ex);
97     catch ();
98
99     libvlc_media_list_player_play_item (mlp, md, &ex);
100     catch ();
101
102     // Wait dummily for next_item_callback() to flag 'done_playing':
103     while (!done_playing)
104         msleep(100000);
105
106     libvlc_media_list_player_stop (mlp, &ex);
107     catch ();
108
109     libvlc_media_list_player_release (mlp);
110     catch ();
111     
112     libvlc_release (vlc);
113     catch ();
114 }
115
116 static void test_media_list_player_next(const char** argv, int argc)
117 {
118     libvlc_instance_t *vlc;
119     libvlc_media_t *md;
120     libvlc_media_list_t *ml;
121     libvlc_media_list_player_t *mlp;
122     
123     const char * file = test_default_sample;
124     
125     log ("Testing media player next()\n");
126     
127     libvlc_exception_init (&ex);
128     vlc = libvlc_new (argc, argv, &ex);
129     catch ();
130     
131     md = libvlc_media_new (vlc, file, &ex);
132     catch ();
133     
134     ml = libvlc_media_list_new (vlc, &ex);
135     catch ();
136     
137     mlp = libvlc_media_list_player_new (vlc, &ex);
138     catch ();
139
140     libvlc_media_list_add_media (ml, md, &ex);
141     catch ();
142
143     // Add three media
144     media_list_add_file_path (vlc, ml, file);
145     media_list_add_file_path (vlc, ml, file);
146     media_list_add_file_path (vlc, ml, file);
147
148     libvlc_media_list_player_set_media_list (mlp, ml, &ex);
149     
150     libvlc_media_list_player_play_item (mlp, md, &ex);
151     catch ();
152
153     libvlc_media_release (md);
154
155     msleep(100000);
156     
157     libvlc_media_list_player_next (mlp, &ex);
158     catch ();
159
160     libvlc_media_list_player_pause (mlp, &ex);
161     catch();
162
163     msleep(100000);
164     
165     libvlc_media_list_player_next (mlp, &ex);
166     catch ();
167     
168     libvlc_media_list_player_stop (mlp, &ex);
169     catch ();
170
171     msleep(100000);
172     
173     libvlc_media_list_player_next (mlp, &ex);
174     catch ();
175         
176     libvlc_media_list_player_release (mlp);
177     catch ();
178     
179     libvlc_release (vlc);
180     catch ();
181 }
182
183 static void test_media_list_player_pause_stop(const char** argv, int argc)
184 {
185     libvlc_instance_t *vlc;
186     libvlc_media_t *md;
187     libvlc_media_list_t *ml;
188     libvlc_media_list_player_t *mlp;
189
190     const char * file = test_default_sample;
191
192     log ("Testing play and pause of %s using the media list.\n", file);
193
194     libvlc_exception_init (&ex);
195     vlc = libvlc_new (argc, argv, &ex);
196     catch ();
197
198     md = libvlc_media_new (vlc, file, &ex);
199     catch ();
200
201     ml = libvlc_media_list_new (vlc, &ex);
202     catch ();
203
204     mlp = libvlc_media_list_player_new (vlc, &ex);
205
206     libvlc_media_list_add_media( ml, md, &ex );
207     catch ();
208
209     libvlc_media_list_player_set_media_list( mlp, ml, &ex );
210
211     libvlc_media_list_player_play_item( mlp, md, &ex );
212     catch ();
213
214     libvlc_media_list_player_pause (mlp, &ex);
215     catch();
216
217     libvlc_media_list_player_stop (mlp, &ex);
218     catch ();
219
220     libvlc_media_release (md);
221
222     libvlc_media_list_player_release (mlp);
223     catch ();
224
225     libvlc_release (vlc);
226     catch ();
227 }
228
229 static void test_media_list_player_play_item_at_index(const char** argv, int argc)
230 {
231     libvlc_instance_t *vlc;
232     libvlc_media_t *md;
233     libvlc_media_list_t *ml;
234     libvlc_media_list_player_t *mlp;
235
236     const char * file = test_default_sample;
237
238     log ("Testing play_item_at_index of %s using the media list.\n", file);
239
240     libvlc_exception_init (&ex);
241     vlc = libvlc_new (argc, argv, &ex);
242     catch ();
243
244     md = libvlc_media_new (vlc, file, &ex);
245     catch ();
246
247     ml = libvlc_media_list_new (vlc, &ex);
248     catch ();
249
250     mlp = libvlc_media_list_player_new (vlc, &ex);
251
252     for (unsigned i = 0; i < 5; i++)
253     {
254         libvlc_media_list_add_media( ml, md, &ex );
255         catch ();
256     }
257
258     libvlc_media_list_player_set_media_list( mlp, ml, &ex );
259
260     libvlc_media_list_player_play_item_at_index( mlp, 0, &ex );
261     catch ();
262
263     libvlc_media_list_player_stop (mlp, &ex);
264     catch ();
265
266     libvlc_media_release (md);
267     catch ();
268
269     libvlc_media_list_player_release (mlp);
270     catch ();
271
272     libvlc_release (vlc);
273     catch ();
274 }
275
276
277 int main (void)
278 {
279     test_init();
280
281     test_media_list_player_pause_stop (test_defaults_args, test_defaults_nargs);
282     test_media_list_player_play_item_at_index (test_defaults_args, test_defaults_nargs);
283     test_media_list_player_next (test_defaults_args, test_defaults_nargs);
284     test_media_list_player_items_queue (test_defaults_args, test_defaults_nargs);
285     return 0;
286 }