]> git.sesse.net Git - vlc/blob - bindings/java/core/src/main/java/org/videolan/jvlc/MediaPlayer.java
Add some locking.
[vlc] / bindings / java / core / src / main / java / org / videolan / jvlc / MediaPlayer.java
1 /*****************************************************************************
2  * MediaInstance.java: VLC Java Bindings Media Instance
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;
27
28 import java.awt.Canvas;
29 import java.util.ArrayList;
30 import java.util.EnumSet;
31 import java.util.List;
32
33 import org.videolan.jvlc.event.MediaPlayerCallback;
34 import org.videolan.jvlc.event.MediaPlayerListener;
35 import org.videolan.jvlc.internal.LibVlc;
36 import org.videolan.jvlc.internal.LibVlcEventType;
37 import org.videolan.jvlc.internal.LibVlc.LibVlcEventManager;
38 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaPlayer;
39 import org.videolan.jvlc.internal.LibVlc.libvlc_exception_t;
40
41 import com.sun.jna.Native;
42 import com.sun.jna.Platform;
43
44
45 public class MediaPlayer
46 {
47
48     private final LibVlcMediaPlayer instance;
49
50     private final LibVlc libvlc;
51
52     private final LibVlcEventManager eventManager;
53
54     private List<MediaPlayerCallback> callbacks = new ArrayList<MediaPlayerCallback>();
55
56     private MediaDescriptor mediaDescriptor;
57
58     private volatile boolean released;
59
60     MediaPlayer(JVLC jvlc, LibVlcMediaPlayer instance)
61     {
62         libvlc_exception_t exception = new libvlc_exception_t();
63         this.instance = instance;
64         libvlc = jvlc.getLibvlc();
65         eventManager = libvlc.libvlc_media_player_event_manager(instance, exception);
66     }
67
68     public MediaPlayer(MediaDescriptor mediaDescriptor)
69     {
70         libvlc_exception_t exception = new libvlc_exception_t();
71         libvlc = mediaDescriptor.getLibvlc();
72         instance = libvlc.libvlc_media_player_new_from_media(mediaDescriptor.getInstance(), exception);
73         eventManager = libvlc.libvlc_media_player_event_manager(instance, exception);
74         this.mediaDescriptor = mediaDescriptor;
75     }
76
77     public MediaDescriptor getMediaDescriptor()
78     {
79         return mediaDescriptor;
80     }
81
82     public void play()
83     {
84         libvlc_exception_t exception = new libvlc_exception_t();
85         libvlc.libvlc_media_player_play(instance, exception);
86     }
87
88     public void stop()
89     {
90         libvlc_exception_t exception = new libvlc_exception_t();
91         libvlc.libvlc_media_player_stop(instance, exception);
92     }
93
94     public void pause()
95     {
96         libvlc_exception_t exception = new libvlc_exception_t();
97         libvlc.libvlc_media_player_pause(instance, exception);
98     }
99
100     public long getLength()
101     {
102         libvlc_exception_t exception = new libvlc_exception_t();
103         return libvlc.libvlc_media_player_get_length(instance, exception);
104     }
105
106     public long getTime()
107     {
108         libvlc_exception_t exception = new libvlc_exception_t();
109         return libvlc.libvlc_media_player_get_time(instance, exception);
110     }
111
112     public void setTime(long time)
113     {
114         libvlc_exception_t exception = new libvlc_exception_t();
115         libvlc.libvlc_media_player_set_time(instance, time, exception);
116     }
117
118     public float getPosition()
119     {
120         libvlc_exception_t exception = new libvlc_exception_t();
121         return libvlc.libvlc_media_player_get_position(instance, exception);
122     }
123
124     public void setPosition(float position)
125     {
126         libvlc_exception_t exception = new libvlc_exception_t();
127         libvlc.libvlc_media_player_set_position(instance, position, exception);
128     }
129
130     public boolean willPlay()
131     {
132         libvlc_exception_t exception = new libvlc_exception_t();
133         return (libvlc.libvlc_media_player_will_play(instance, exception) == 1);
134     }
135
136     public float getRate()
137     {
138         libvlc_exception_t exception = new libvlc_exception_t();
139         return libvlc.libvlc_media_player_get_rate(instance, exception);
140     }
141
142     public void setRate(float rate)
143     {
144         libvlc_exception_t exception = new libvlc_exception_t();
145         libvlc.libvlc_media_player_set_rate(instance, rate, exception);
146     }
147
148     public boolean hasVideoOutput()
149     {
150         libvlc_exception_t exception = new libvlc_exception_t();
151         return (libvlc.libvlc_media_player_has_vout(instance, exception) == 1);
152     }
153
154     public float getFPS()
155     {
156         libvlc_exception_t exception = new libvlc_exception_t();
157         return libvlc.libvlc_media_player_get_fps(instance, exception);
158     }
159
160     public boolean isPlaying()
161     {
162         libvlc_exception_t exception = new libvlc_exception_t();
163         return libvlc.libvlc_media_player_is_playing(instance, exception) == 1 ? true : false;
164     }
165
166     public void addListener(final MediaPlayerListener listener)
167     {
168         MediaPlayerCallback callback = new MediaPlayerCallback(this, listener);
169         libvlc_exception_t exception = new libvlc_exception_t();
170         for (LibVlcEventType event : EnumSet.range(
171             LibVlcEventType.libvlc_MediaPlayerPlaying,
172             LibVlcEventType.libvlc_MediaPlayerTimeChanged))
173         {
174             libvlc.libvlc_event_attach(eventManager, event.ordinal(), callback, null, exception);
175         }
176         callbacks.add(callback);
177     }
178
179     public void setParent(Canvas canvas)
180     {
181         long drawable = Native.getComponentID(canvas);
182         libvlc_exception_t exception = new libvlc_exception_t();
183         if (Platform.isWindows())
184         {
185             libvlc.libvlc_media_player_set_hwnd(instance, drawable, exception);
186         }
187         else
188         {
189             libvlc.libvlc_media_player_set_xwindow(instance, drawable, exception);
190         }
191     }
192
193     /**
194      * {@inheritDoc}
195      */
196     @Override
197     protected void finalize() throws Throwable
198     {
199         release();
200         super.finalize();
201     }
202
203     public void release()
204     {
205         if (released)
206         {
207             return;
208         }
209         released = true;
210
211         libvlc_exception_t exception = new libvlc_exception_t();
212         for (MediaPlayerCallback callback : callbacks)
213         {
214             for (LibVlcEventType event : EnumSet.range(
215                 LibVlcEventType.libvlc_MediaPlayerPlaying,
216                 LibVlcEventType.libvlc_MediaPlayerPositionChanged))
217             {
218                 libvlc.libvlc_event_detach(eventManager, event.ordinal(), callback, null, exception);
219             }
220         }
221         libvlc.libvlc_media_player_release(instance);
222
223     }
224
225     /**
226      * Returns the instance.
227      * @return the instance
228      */
229     LibVlcMediaPlayer getInstance()
230     {
231         return instance;
232     }
233
234 }