]> git.sesse.net Git - vlc/blob - bindings/java/core/src/test/java/org/videolan/jvlc/internal/MediaListPlayerTest.java
5baf02b8599550f17e746f18e4d22d9acce41ef7
[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[]{"-vvv","-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_list_release(mediaList);
137         libvlc.libvlc_media_list_player_release(mediaListPlayer);
138     }
139
140     @Test
141     public void mediaListPlayerPlayItemAtIndex() throws Exception
142     {
143         libvlc_exception_t exception = new libvlc_exception_t();
144         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
145         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
146         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
147         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
148         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
149         libvlc.libvlc_media_list_player_play_item_at_index(mediaListPlayer, 0, exception);
150         while (true)
151         {
152             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
153             if (exception.raised == 1)
154             {
155                 throw new RuntimeException("Native exception thrown");
156             }
157             if (playing == 1)
158             {
159                 break;
160             }
161             Thread.sleep(150);
162         }
163         libvlc.libvlc_media_list_player_stop(mediaListPlayer, exception);
164         libvlc.libvlc_media_list_release(mediaList);
165         libvlc.libvlc_media_list_player_release(mediaListPlayer);
166
167     }
168
169     @Test
170     public void mediaListPlayerPlayItem() throws Exception
171     {
172         libvlc_exception_t exception = new libvlc_exception_t();
173         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
174         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
175         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
176         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
177         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
178         libvlc.libvlc_media_list_player_play_item(mediaListPlayer, mediaDescriptor, exception);
179         Assert.assertEquals(0, exception.raised);
180         while (true)
181         {
182             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
183             if (exception.raised == 1)
184             {
185                 throw new RuntimeException("Native exception thrown");
186             }
187             if (playing == 1)
188             {
189                 break;
190             }
191             Thread.sleep(150);
192         }
193         // FIXME give stats the time to run... there's probably a race condition in misc/stats.c:259 that
194         // needs to be fixed
195         Thread.sleep(400);
196         libvlc.libvlc_media_list_player_stop(mediaListPlayer, exception);
197         libvlc.libvlc_media_list_release(mediaList);
198         libvlc.libvlc_media_list_player_release(mediaListPlayer);
199     }
200
201     @Test
202     public void mediaListPlayerGetStateStopped()
203     {
204         libvlc_exception_t exception = new libvlc_exception_t();
205         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
206         int state = libvlc.libvlc_media_list_player_get_state(mediaListPlayer, exception);
207         Assert.assertEquals(LibVlcState.libvlc_Stopped.ordinal(), state);
208         libvlc.libvlc_media_list_player_release(mediaListPlayer);
209     }
210
211     @Test
212     public void mediaListPlayerPause() throws Exception
213     {
214         libvlc_exception_t exception = new libvlc_exception_t();
215         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
216         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
217         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
218         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
219         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
220         libvlc.libvlc_media_list_player_play_item(mediaListPlayer, mediaDescriptor, exception);
221         while (true)
222         {
223             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
224             if (exception.raised == 1)
225             {
226                 throw new RuntimeException("Native exception thrown");
227             }            
228             if (playing == 1)
229             {
230                 break;
231             }
232             Thread.sleep(150);
233         }
234         libvlc.libvlc_media_list_player_pause(mediaListPlayer, exception);
235         Assert.assertEquals(0, exception.raised);
236         while (true)
237         {
238             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
239             if (exception.raised == 1)
240             {
241                 throw new RuntimeException("Native exception thrown");
242             }            
243             if (playing == 0)
244             {
245                 break;
246             }
247             Thread.sleep(150);
248         }
249         int state = libvlc.libvlc_media_list_player_get_state(mediaListPlayer, exception);
250         Assert.assertEquals("Expected state: " + LibVlcState.libvlc_Paused +".\n", LibVlcState.libvlc_Paused.ordinal(), state);
251         libvlc.libvlc_media_list_player_stop(mediaListPlayer, exception);
252         libvlc.libvlc_media_list_release(mediaList);
253         libvlc.libvlc_media_list_player_release(mediaListPlayer);
254     }
255
256     
257     @Test
258     public void mediaListPlayerSetMediaInstance()
259     {
260         libvlc_exception_t exception = new libvlc_exception_t();
261         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
262         LibVlcMediaDescriptor md = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
263         LibVlcMediaInstance mi = libvlc.libvlc_media_player_new_from_media(md, exception);
264         libvlc.libvlc_media_list_player_set_media_player(mediaListPlayer, mi, exception);
265         Assert.assertEquals(0, exception.raised);
266     }
267     
268     @Test
269     public void mediaListPlayerNextNoItems()
270     {
271         libvlc_exception_t exception = new libvlc_exception_t();
272         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
273         libvlc.libvlc_media_list_player_next(mediaListPlayer, exception);
274         Assert.assertEquals(1, exception.raised);
275     }
276     
277     /**
278      * fails, see https://trac.videolan.org/vlc/ticket/1535
279      */
280 //    @Test
281     public void mediaListPlayerNext() throws Exception
282     {
283         libvlc_exception_t exception = new libvlc_exception_t();
284         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
285         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
286         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
287         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
288         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
289         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
290         libvlc.libvlc_media_list_player_play_item_at_index(mediaListPlayer, 0, exception);
291         Thread.sleep(150);
292         libvlc.libvlc_media_list_player_next(mediaListPlayer, exception);
293         Assert.assertEquals(0, exception.raised);
294         libvlc.libvlc_media_list_release(mediaList);
295     }
296
297     @Test
298     public void mediaListPlayerIsPlaying() throws Exception
299     {
300         libvlc_exception_t exception = new libvlc_exception_t();
301         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
302         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
303         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
304         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
305         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
306         libvlc.libvlc_media_list_player_play_item(mediaListPlayer, mediaDescriptor, exception);
307
308         while (true)
309         {
310             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
311             Assert.assertEquals(0, exception.raised);
312             if (playing == 1)
313             {
314                 break;
315             }
316             Thread.sleep(150);
317         }
318         Assert.assertEquals("Expected state: " + LibVlcState.libvlc_Playing +".\n", LibVlcState.libvlc_Playing.ordinal(), libvlc.libvlc_media_list_player_get_state(
319             mediaListPlayer,
320             exception));
321         
322         libvlc.libvlc_media_list_player_stop(mediaListPlayer, exception);
323         while (true)
324         {
325             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
326             Assert.assertEquals(0, exception.raised);
327             if (playing == 0)
328             {
329                 break;
330             }
331             Thread.sleep(150);
332         }
333         Assert.assertEquals(LibVlcState.libvlc_Stopped.ordinal(), libvlc.libvlc_media_list_player_get_state(
334             mediaListPlayer,
335             exception));
336         libvlc.libvlc_media_list_release(mediaList);
337     }
338
339
340
341 }