]> git.sesse.net Git - vlc/blob - bindings/java/core/src/test/java/org/videolan/jvlc/internal/MediaListPlayerTest.java
14000484b16bbda6da19778c720c98a912b835f6
[vlc] / bindings / java / core / src / test / java / org / videolan / jvlc / internal / MediaListPlayerTest.java
1 /*****************************************************************************
2  * MediaListPlayerTest.java: VLC Java Bindings
3  *****************************************************************************
4  * Copyright (C) 1998-2008 the VideoLAN team
5  *
6  * Authors: Filippo Carone <filippo@carone.org>
7  *
8  *
9  * $Id $
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 package org.videolan.jvlc.internal;
27
28 import junit.framework.Assert;
29
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.videolan.jvlc.internal.LibVlc.LibVlcInstance;
34 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaDescriptor;
35 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaInstance;
36 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaList;
37 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaListPlayer;
38 import org.videolan.jvlc.internal.LibVlc.libvlc_exception_t;
39
40
41 public class MediaListPlayerTest
42 {
43
44     private LibVlc libvlc = LibVlc.SYNC_INSTANCE;
45
46     private LibVlcInstance libvlcInstance;
47
48     private String mrl = this.getClass().getResource("/raffa_voice.ogg").getPath();
49
50     @Before
51     public void testSetup() throws Exception
52     {
53         libvlc_exception_t exception = new libvlc_exception_t();
54         libvlcInstance = libvlc.libvlc_new(0, new String[]{"-I", "dummy", "--aout=dummy", "--vout=dummy" }, exception);
55         // use the following line to use your audio card.
56         // libvlcInstance = libvlc.libvlc_new(0, new String[]{}, exception);
57     }
58
59     @After
60     public void tearDown()
61     {
62         libvlc.libvlc_release(libvlcInstance);
63     }
64
65     @Test
66     public void mediaListPlayerNewTest()
67     {
68         libvlc_exception_t exception = new libvlc_exception_t();
69         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
70         Assert.assertNotNull(mediaListPlayer);
71         Assert.assertEquals(0, exception.raised);
72         libvlc.libvlc_media_list_player_release(mediaListPlayer);
73     }
74
75     @Test
76     public void mediaListPlayerSetMediaListTest()
77     {
78         libvlc_exception_t exception = new libvlc_exception_t();
79         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
80         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
81         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
82         Assert.assertEquals(0, exception.raised);
83         libvlc.libvlc_media_list_release(mediaList);
84         libvlc.libvlc_media_list_player_release(mediaListPlayer);
85     }
86
87     @Test
88     public void mediaListPlayerSetMediaListTest2()
89     {
90         libvlc_exception_t exception = new libvlc_exception_t();
91         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
92         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
93         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
94         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
95         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
96         Assert.assertEquals(0, exception.raised);
97         libvlc.libvlc_media_list_release(mediaList);
98         libvlc.libvlc_media_list_player_release(mediaListPlayer);
99     }
100
101     @Test
102     public void mediaListPlayerIsNotPlayingTest()
103     {
104         libvlc_exception_t exception = new libvlc_exception_t();
105         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
106         int result = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
107         Assert.assertEquals(0, result);
108         Assert.assertEquals(0, exception.raised);
109         libvlc.libvlc_media_list_player_release(mediaListPlayer);
110     }
111
112     @Test
113     public void mediaListPlayerPlayNoItemTest()
114     {
115         libvlc_exception_t exception = new libvlc_exception_t();
116         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
117         libvlc.libvlc_media_list_player_play(mediaListPlayer, exception);
118         Assert.assertEquals(1, exception.raised);
119         libvlc.libvlc_media_list_player_release(mediaListPlayer);
120     }
121
122     /**
123      * this fails: see https://trac.videolan.org/vlc/ticket/1527
124      */
125     // @Test
126     public void mediaListPlayerPlay()
127     {
128         libvlc_exception_t exception = new libvlc_exception_t();
129         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
130         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
131         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
132         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
133         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
134         libvlc.libvlc_media_list_player_play(mediaListPlayer, exception);
135         Assert.assertEquals("Exception message: " + exception.message + ".\n", 0, exception.raised);
136         libvlc.libvlc_media_release(mediaDescriptor);
137         libvlc.libvlc_media_list_release(mediaList);
138         libvlc.libvlc_media_list_player_release(mediaListPlayer);
139     }
140
141     @Test
142     public void mediaListPlayerPlayItemAtIndex() throws Exception
143     {
144         libvlc_exception_t exception = new libvlc_exception_t();
145         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
146         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
147         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
148         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
149         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
150         libvlc.libvlc_media_list_player_play_item_at_index(mediaListPlayer, 0, exception);
151         while (true)
152         {
153             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
154             if (exception.raised == 1)
155             {
156                 throw new RuntimeException("Native exception thrown");
157             }
158             if (playing == 1)
159             {
160                 break;
161             }
162             Thread.sleep(150);
163         }
164         libvlc.libvlc_media_list_player_stop(mediaListPlayer, exception);
165         libvlc.libvlc_media_release(mediaDescriptor);
166         libvlc.libvlc_media_list_release(mediaList);
167         libvlc.libvlc_media_list_player_release(mediaListPlayer);
168     }
169
170     @Test
171     public void mediaListPlayerPlayItem() throws Exception
172     {
173         libvlc_exception_t exception = new libvlc_exception_t();
174         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
175         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
176         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
177         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
178         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
179         libvlc.libvlc_media_list_player_play_item(mediaListPlayer, mediaDescriptor, exception);
180         Assert.assertEquals(0, exception.raised);
181         while (true)
182         {
183             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
184             if (exception.raised == 1)
185             {
186                 throw new RuntimeException("Native exception thrown");
187             }
188             if (playing == 1)
189             {
190                 break;
191             }
192             Thread.sleep(150);
193         }
194         // FIXME give stats the time to run... there's probably a race condition in misc/stats.c:259 that
195         // needs to be fixed
196         // Thread.sleep(400);
197         libvlc.libvlc_media_list_player_stop(mediaListPlayer, exception);
198         libvlc.libvlc_media_list_release(mediaList);
199         libvlc.libvlc_media_list_player_release(mediaListPlayer);
200     }
201
202     @Test
203     public void mediaListPlayerGetStateEnded()
204     {
205         libvlc_exception_t exception = new libvlc_exception_t();
206         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
207         int state = libvlc.libvlc_media_list_player_get_state(mediaListPlayer, exception);
208         Assert.assertEquals(LibVlcState.libvlc_Ended.ordinal(), state);
209         libvlc.libvlc_media_list_player_release(mediaListPlayer);
210     }
211
212     @Test
213     public void mediaLtistPlayerPause() throws Exception
214     {
215         libvlc_exception_t exception = new libvlc_exception_t();
216         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
217         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
218         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
219         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
220         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
221         libvlc.libvlc_media_list_player_play_item(mediaListPlayer, mediaDescriptor, exception);
222         while (true)
223         {
224             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
225             if (exception.raised == 1)
226             {
227                 throw new RuntimeException("Native exception thrown");
228             }
229             if (playing == 1)
230             {
231                 break;
232             }
233             Thread.sleep(150);
234         }
235         libvlc.libvlc_media_list_player_pause(mediaListPlayer, exception);
236         Assert.assertEquals(exception.message, 0, exception.raised);
237         while (true)
238         {
239             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
240             if (exception.raised == 1)
241             {
242                 throw new RuntimeException("Native exception thrown");
243             }
244             if (playing == 0)
245             {
246                 break;
247             }
248             Thread.sleep(150);
249         }
250         int state = libvlc.libvlc_media_list_player_get_state(mediaListPlayer, exception);
251         Assert.assertEquals(exception.message, 0, exception.raised);
252         Assert.assertEquals(
253             "Expected state: " + LibVlcState.libvlc_Paused + ".\n",
254             LibVlcState.libvlc_Paused.ordinal(),
255             state);
256         libvlc.libvlc_media_list_player_stop(mediaListPlayer, exception);
257         libvlc.libvlc_media_list_release(mediaList);
258         libvlc.libvlc_media_list_player_release(mediaListPlayer);
259     }
260
261     @Test
262     public void mediaListPlayerSetMediaInstance()
263     {
264         libvlc_exception_t exception = new libvlc_exception_t();
265         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
266         LibVlcMediaDescriptor md = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
267         LibVlcMediaInstance mi = libvlc.libvlc_media_player_new_from_media(md, exception);
268         libvlc.libvlc_media_list_player_set_media_player(mediaListPlayer, mi, exception);
269         Assert.assertEquals(0, exception.raised);
270     }
271
272     @Test
273     public void mediaListPlayerNextNoItems()
274     {
275         libvlc_exception_t exception = new libvlc_exception_t();
276         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
277         libvlc.libvlc_media_list_player_next(mediaListPlayer, exception);
278         Assert.assertEquals(1, exception.raised);
279     }
280
281     /**
282      * fails, see https://trac.videolan.org/vlc/ticket/1535
283      */
284     // @Test
285     public void mediaListPlayerNext() throws Exception
286     {
287         libvlc_exception_t exception = new libvlc_exception_t();
288         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
289         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
290         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
291         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
292         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
293         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
294         libvlc.libvlc_media_list_player_play_item_at_index(mediaListPlayer, 0, exception);
295         Thread.sleep(150);
296         libvlc.libvlc_media_list_player_next(mediaListPlayer, exception);
297         Assert.assertEquals(0, exception.raised);
298         libvlc.libvlc_media_list_release(mediaList);
299     }
300
301     @Test
302     public void mediaListPlayerIsPlaying() throws Exception
303     {
304         libvlc_exception_t exception = new libvlc_exception_t();
305         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
306         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
307         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
308         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
309         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
310         libvlc.libvlc_media_list_player_play_item(mediaListPlayer, mediaDescriptor, exception);
311
312         while (true)
313         {
314             int playing = libvlc.libvlc_media_list_player_get_state(mediaListPlayer, exception);
315             Assert.assertEquals(0, exception.raised);
316             if (playing == LibVlcState.libvlc_Playing.ordinal())
317             {
318                 break;
319             }
320             Thread.sleep(150);
321         }
322
323         libvlc.libvlc_media_list_player_stop(mediaListPlayer, exception);
324         while (true)
325         {
326             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
327             Assert.assertEquals(0, exception.raised);
328             if (playing == 0)
329             {
330                 break;
331             }
332             Thread.sleep(150);
333         }
334         Assert.assertEquals(LibVlcState.libvlc_Ended.ordinal(), libvlc.libvlc_media_list_player_get_state(
335             mediaListPlayer,
336             exception));
337         libvlc.libvlc_media_list_release(mediaList);
338     }
339
340 }