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