]> git.sesse.net Git - vlc/blob - bindings/java/core/src/test/java/org/videolan/jvlc/internal/MediaListPlayerTest.java
jvlc: wait for correct player status before releasing libvlc
[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.Test;
32 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaDescriptor;
33 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaInstance;
34 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaList;
35 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaListPlayer;
36 import org.videolan.jvlc.internal.LibVlc.libvlc_exception_t;
37
38
39 public class MediaListPlayerTest extends AbstractVLCInternalTest
40 {
41
42     private LibVlcMediaListPlayer current;
43
44     @Test
45     public void mediaListPlayerNewTest()
46     {
47         libvlc_exception_t exception = new libvlc_exception_t();
48         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
49         Assert.assertNotNull(mediaListPlayer);
50         Assert.assertEquals(0, exception.raised);
51         libvlc.libvlc_media_list_player_release(mediaListPlayer);
52     }
53
54     @Test
55     public void mediaListPlayerSetMediaListTest()
56     {
57         libvlc_exception_t exception = new libvlc_exception_t();
58         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
59         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
60         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
61         Assert.assertEquals(0, exception.raised);
62         libvlc.libvlc_media_list_release(mediaList);
63         libvlc.libvlc_media_list_player_release(mediaListPlayer);
64     }
65
66     @Test
67     public void mediaListPlayerSetMediaListTest2()
68     {
69         libvlc_exception_t exception = new libvlc_exception_t();
70         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
71         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
72         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
73         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
74         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
75         Assert.assertEquals(0, exception.raised);
76         libvlc.libvlc_media_list_release(mediaList);
77         libvlc.libvlc_media_list_player_release(mediaListPlayer);
78     }
79
80     @Test
81     public void mediaListPlayerIsNotPlayingTest()
82     {
83         libvlc_exception_t exception = new libvlc_exception_t();
84         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
85         int result = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
86         Assert.assertEquals(0, result);
87         Assert.assertEquals(0, exception.raised);
88         libvlc.libvlc_media_list_player_release(mediaListPlayer);
89     }
90
91     @Test
92     public void mediaListPlayerPlayNoItemTest()
93     {
94         libvlc_exception_t exception = new libvlc_exception_t();
95         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
96         libvlc.libvlc_media_list_player_play(mediaListPlayer, exception);
97         Assert.assertEquals(1, exception.raised);
98         libvlc.libvlc_media_list_player_release(mediaListPlayer);
99     }
100
101     /**
102      * this fails: see https://trac.videolan.org/vlc/ticket/1527
103      */
104     @Test
105     public void mediaListPlayerPlay()
106     {
107         libvlc_exception_t exception = new libvlc_exception_t();
108         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
109         current = mediaListPlayer;
110         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
111         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
112         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
113         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
114         libvlc.libvlc_media_list_player_play(mediaListPlayer, exception);
115         Assert.assertEquals("Exception message: " + exception.message + ".\n", 0, exception.raised);
116         libvlc.libvlc_media_release(mediaDescriptor);
117         libvlc.libvlc_media_list_release(mediaList);
118         libvlc.libvlc_media_list_player_release(mediaListPlayer);
119     }
120
121     @Test
122     public void mediaListPlayerPlayItemAtIndex() throws Exception
123     {
124         libvlc_exception_t exception = new libvlc_exception_t();
125         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
126         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
127         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
128         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
129         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
130         libvlc.libvlc_media_list_player_play_item_at_index(mediaListPlayer, 0, exception);
131         while (true)
132         {
133             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
134             if (exception.raised == 1)
135             {
136                 throw new RuntimeException("Native exception thrown");
137             }
138             if (playing == 1)
139             {
140                 break;
141             }
142             Thread.sleep(150);
143         }
144         libvlc.libvlc_media_list_player_stop(mediaListPlayer, exception);
145         while (libvlc.libvlc_media_list_player_get_state(mediaListPlayer, exception) != LibVlcState.libvlc_Ended
146             .ordinal())
147         {
148             Thread.sleep(100);
149         }
150         libvlc.libvlc_media_release(mediaDescriptor);
151         libvlc.libvlc_media_list_release(mediaList);
152         libvlc.libvlc_media_list_player_release(mediaListPlayer);
153     }
154
155     @Test
156     public void mediaListPlayerPlayItem() throws Exception
157     {
158         libvlc_exception_t exception = new libvlc_exception_t();
159         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
160         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
161         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
162         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
163         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
164         libvlc.libvlc_media_list_player_play_item(mediaListPlayer, mediaDescriptor, exception);
165         Assert.assertEquals(0, exception.raised);
166         while (true)
167         {
168             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
169             if (exception.raised == 1)
170             {
171                 throw new RuntimeException("Native exception thrown");
172             }
173             if (playing == 1)
174             {
175                 break;
176             }
177             Thread.sleep(150);
178         }
179         Thread.sleep(400);
180         libvlc.libvlc_media_list_player_stop(mediaListPlayer, exception);
181         libvlc.libvlc_media_list_release(mediaList);
182         libvlc.libvlc_media_list_player_release(mediaListPlayer);
183     }
184
185     @Test
186     public void mediaListPlayerGetStateEnded()
187     {
188         libvlc_exception_t exception = new libvlc_exception_t();
189         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
190         int state = libvlc.libvlc_media_list_player_get_state(mediaListPlayer, exception);
191         Assert.assertEquals(LibVlcState.libvlc_Ended.ordinal(), state);
192         libvlc.libvlc_media_list_player_release(mediaListPlayer);
193     }
194
195     @Test
196     public void mediaLtistPlayerPause() throws Exception
197     {
198         libvlc_exception_t exception = new libvlc_exception_t();
199         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
200         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
201         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
202         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
203         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
204         libvlc.libvlc_media_list_player_play_item(mediaListPlayer, mediaDescriptor, exception);
205         while (true)
206         {
207             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
208             if (exception.raised == 1)
209             {
210                 throw new RuntimeException("Native exception thrown");
211             }
212             if (playing == 1)
213             {
214                 break;
215             }
216             Thread.sleep(150);
217         }
218         libvlc.libvlc_media_list_player_pause(mediaListPlayer, exception);
219
220         int state = libvlc.libvlc_media_list_player_get_state(mediaListPlayer, exception);
221         Assert.assertEquals(exception.message, 0, exception.raised);
222         Assert.assertEquals(
223             "Expected state: " + LibVlcState.libvlc_Paused + ".\n",
224             LibVlcState.libvlc_Paused.ordinal(),
225             state);
226         libvlc.libvlc_media_list_player_stop(mediaListPlayer, exception);
227         libvlc.libvlc_media_list_release(mediaList);
228         libvlc.libvlc_media_list_player_release(mediaListPlayer);
229     }
230
231     @Test
232     public void mediaListPlayerSetMediaInstance()
233     {
234         libvlc_exception_t exception = new libvlc_exception_t();
235         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
236         LibVlcMediaDescriptor md = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
237         LibVlcMediaInstance mi = libvlc.libvlc_media_player_new_from_media(md, exception);
238         libvlc.libvlc_media_list_player_set_media_player(mediaListPlayer, mi, exception);
239         Assert.assertEquals(0, exception.raised);
240     }
241
242     @Test
243     public void mediaListPlayerNextNoItems()
244     {
245         libvlc_exception_t exception = new libvlc_exception_t();
246         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
247         libvlc.libvlc_media_list_player_next(mediaListPlayer, exception);
248         Assert.assertEquals(1, exception.raised);
249     }
250
251     /**
252      * fails, see https://trac.videolan.org/vlc/ticket/1535
253      */
254     // @Test
255     public void mediaListPlayerNext() throws Exception
256     {
257         libvlc_exception_t exception = new libvlc_exception_t();
258         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
259         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
260         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
261         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
262         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
263         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
264         libvlc.libvlc_media_list_player_play_item_at_index(mediaListPlayer, 0, exception);
265         Thread.sleep(150);
266         libvlc.libvlc_media_list_player_next(mediaListPlayer, exception);
267         Assert.assertEquals(0, exception.raised);
268         libvlc.libvlc_media_list_release(mediaList);
269     }
270
271     @Test
272     public void mediaListPlayerIsPlaying() throws Exception
273     {
274         libvlc_exception_t exception = new libvlc_exception_t();
275         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
276         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
277         LibVlcMediaDescriptor mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
278         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
279         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
280         libvlc.libvlc_media_list_player_play_item(mediaListPlayer, mediaDescriptor, exception);
281
282         while (true)
283         {
284             int playing = libvlc.libvlc_media_list_player_get_state(mediaListPlayer, exception);
285             Assert.assertEquals(0, exception.raised);
286             if (playing == LibVlcState.libvlc_Playing.ordinal())
287             {
288                 break;
289             }
290             Thread.sleep(150);
291         }
292
293         libvlc.libvlc_media_list_player_stop(mediaListPlayer, exception);
294         while (true)
295         {
296             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
297             Assert.assertEquals(0, exception.raised);
298             if (playing == 0)
299             {
300                 break;
301             }
302             Thread.sleep(150);
303         }
304         Assert.assertEquals(LibVlcState.libvlc_Ended.ordinal(), libvlc.libvlc_media_list_player_get_state(
305             mediaListPlayer,
306             exception));
307         libvlc.libvlc_media_list_release(mediaList);
308     }
309
310     @Override
311     @After
312     public void tearDown()
313     {
314         if (current != null)
315         {
316             libvlc.libvlc_media_list_player_stop(current, exception);
317             while (libvlc.libvlc_media_list_player_get_state(current, exception) != LibVlcState.libvlc_Ended.ordinal())
318             {
319                 try
320                 {
321                     Thread.sleep(100);
322                 }
323                 catch (InterruptedException e)
324                 {
325                     //
326                 }
327             }
328         }
329         current = null;
330         super.tearDown();
331     }
332
333 }