]> git.sesse.net Git - vlc/blob - bindings/java/core/src/main/java/org/videolan/jvlc/MediaList.java
jvlc: update libvlc_exception_t structure.
[vlc] / bindings / java / core / src / main / java / org / videolan / jvlc / MediaList.java
1 /*****************************************************************************
2  * MediaList.java: VLC Java Bindings, MediaList
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.util.ArrayList;
29 import java.util.List;
30
31 import org.videolan.jvlc.internal.LibVlc.LibVlcEventManager;
32 import org.videolan.jvlc.internal.LibVlc.LibVlcMedia;
33 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaList;
34 import org.videolan.jvlc.internal.LibVlc.libvlc_exception_t;
35
36
37 public class MediaList
38 {
39
40     private final JVLC jvlc;
41
42     private final LibVlcMediaList instance;
43
44     private List<String> items = new ArrayList<String>();
45
46     private LibVlcEventManager eventManager;
47
48     private volatile boolean released;
49     
50     public MediaList(JVLC jvlc)
51     {
52         this.jvlc = jvlc;
53         libvlc_exception_t exception = new libvlc_exception_t();
54         instance = jvlc.getLibvlc().libvlc_media_list_new(jvlc.getInstance(), exception);
55         eventManager = jvlc.getLibvlc().libvlc_media_list_event_manager(instance, exception);
56     }
57
58     /**
59      * @param mrl The media resource locator to add to the media list.
60      */
61     public void addMedia(String mrl)
62     {
63         MediaDescriptor descriptor = new MediaDescriptor(jvlc, mrl);
64         addMedia(descriptor);
65     }
66
67     /**
68      * @param descriptor The media descriptor to add to the media list.
69      */
70     public void addMedia(MediaDescriptor descriptor)
71     {
72         if (items.contains(descriptor.getMrl()))
73         {
74             return;
75         }
76         items.add(descriptor.getMrl());
77         libvlc_exception_t exception = new libvlc_exception_t();
78         jvlc.getLibvlc().libvlc_media_list_add_media(instance, descriptor.getInstance(), exception);
79     }
80
81     /**
82      * @return The current number of items in the media list.
83      */
84     public int size()
85     {
86         libvlc_exception_t exception = new libvlc_exception_t();
87         return jvlc.getLibvlc().libvlc_media_list_count(instance, exception);
88     }
89
90     /**
91      * @param descriptor The media descriptor to get the index of.
92      * @return The index of the media descriptor, or -1 if not found.
93      */
94     public int indexOf(MediaDescriptor descriptor)
95     {
96         libvlc_exception_t exception = new libvlc_exception_t();
97         return jvlc.getLibvlc().libvlc_media_list_index_of_item(instance, descriptor.getInstance(), exception);
98     }
99
100     /**
101      * @param index The index of the media descriptor to get.
102      * @return The media descriptor at the given index.
103      * @throws IndexOutOfBoundsException if index is bigger than size() or < 0, or there are no items in the media_list.
104      */
105     public MediaDescriptor getMediaDescriptorAtIndex(int index)
106     {
107         libvlc_exception_t exception = new libvlc_exception_t();
108         if (size() == 0)
109         {
110             throw new IndexOutOfBoundsException();
111         }
112         if (index < 0 || index > size())
113         {
114             throw new IndexOutOfBoundsException();
115         }
116         LibVlcMedia descriptor = jvlc.getLibvlc().libvlc_media_list_item_at_index(instance, index, exception);
117         return new MediaDescriptor(jvlc, descriptor);
118     }
119
120     /**
121      * @param index The index of the media to remove.
122      * @return True if the media was successfully removed, false otherwise.
123      */
124     public boolean removeMedia(int index)
125     {
126         libvlc_exception_t exception = new libvlc_exception_t();
127         jvlc.getLibvlc().libvlc_media_list_remove_index(instance, index, exception);
128         if (exception.b_raised == 0)
129         {
130             items.remove(index);
131             return true;
132         }
133         return false;
134     }
135
136     /**
137      * @param mrl The media descriptor mrl.
138      */
139     public boolean removeMedia(String mrl)
140     {
141         int index = items.indexOf(mrl);
142         if (index == -1)
143         {
144             return false;
145         }
146         return removeMedia(index);
147     }
148     
149     /**
150      * @param mediaDescriptor The media descriptor to remove.
151      */
152     public boolean removeMedia(MediaDescriptor mediaDescriptor)
153     {
154         String mrl = mediaDescriptor.getMrl();
155         int index = items.indexOf(mrl);
156         if (index == -1)
157         {
158             return false;
159         }
160         return removeMedia(index);
161     }
162     
163     /**
164      * Removes all items from the media list.
165      */
166     public void clear()
167     {
168         for (int i = 0; i < size(); i++)
169         {
170             removeMedia(i);
171         }
172     }
173
174     /**
175      * @param descriptor The media descriptor to insert.
176      * @param index The index of the inserted media descriptor.
177      */
178     public void insertMediaDescriptor(MediaDescriptor descriptor, int index)
179     {
180         libvlc_exception_t exception = new libvlc_exception_t();
181         jvlc
182             .getLibvlc()
183             .libvlc_media_list_insert_media(instance, descriptor.getInstance(), index, exception);
184     }
185
186     /**
187      * {@inheritDoc}
188      */
189     @Override
190     protected void finalize() throws Throwable
191     {
192         release();
193         super.finalize();
194     }
195
196     /**
197      * Returns the instance.
198      * @return the instance
199      */
200     LibVlcMediaList getInstance()
201     {
202         return instance;
203     }
204
205     /**
206      * 
207      */
208     public void release()
209     {
210         if (released)
211         {
212             return;
213         }
214         released = true;
215         
216         jvlc.getLibvlc().libvlc_media_list_release(instance);
217     }
218
219
220
221 }