control/media_list.c: Use the new array API.
[vlc.git] / src / control / media_list.c
1 /*****************************************************************************
2  * media_list.c: libvlc new API media list functions
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "libvlc_internal.h"
25 #include <vlc/libvlc.h>
26 #include <assert.h>
27 #include "vlc_arrays.h"
28
29 /*
30  * Private functions
31  */
32
33
34
35 /**************************************************************************
36  *       notify_item_addition (private)
37  *
38  * Do the appropriate action when an item is deleted.
39  **************************************************************************/
40 static void
41 notify_item_addition( libvlc_media_list_t * p_mlist,
42                       libvlc_media_descriptor_t * p_md,
43                       int index )
44 {
45     libvlc_event_t event;
46
47     /* Construct the event */
48     event.type = libvlc_MediaListItemAdded;
49     event.u.media_list_item_added.item = p_md;
50     event.u.media_list_item_added.index = index;
51
52     /* Send the event */
53     libvlc_event_send( p_mlist->p_event_manager, &event );
54 }
55
56 /**************************************************************************
57  *       notify_item_deletion (private)
58  *
59  * Do the appropriate action when an item is added.
60  **************************************************************************/
61 static void
62 notify_item_deletion( libvlc_media_list_t * p_mlist,
63                       libvlc_media_descriptor_t * p_md,
64                       int index )
65 {
66     libvlc_event_t event;
67
68     /* Construct the event */
69     event.type = libvlc_MediaListItemDeleted;
70     event.u.media_list_item_deleted.item = p_md;
71     event.u.media_list_item_deleted.index = index;
72
73     /* Send the event */
74     libvlc_event_send( p_mlist->p_event_manager, &event );
75 }
76
77 /*
78  * Public libvlc functions
79  */
80
81 /**************************************************************************
82  *       libvlc_media_list_new (Public)
83  *
84  * Init an object.
85  **************************************************************************/
86 libvlc_media_list_t *
87 libvlc_media_list_new( libvlc_instance_t * p_inst,
88                        libvlc_exception_t * p_e )
89 {
90     libvlc_media_list_t * p_mlist;
91
92     p_mlist = malloc(sizeof(libvlc_media_list_t));
93
94     if( !p_mlist )
95         return NULL;
96  
97     p_mlist->p_libvlc_instance = p_inst;
98     p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e );
99
100     /* Code for that one should be handled in flat_media_list.c */
101     p_mlist->p_flat_mlist = NULL;
102
103     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
104             libvlc_MediaListItemAdded, p_e );
105     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
106             libvlc_MediaListItemDeleted, p_e );
107
108     if( libvlc_exception_raised( p_e ) )
109     {
110         libvlc_event_manager_release( p_mlist->p_event_manager );
111         free( p_mlist );
112         return NULL;
113     }
114
115     vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock );
116  
117     vlc_array_init( &p_mlist->items );
118     p_mlist->i_refcount = 1;
119     p_mlist->p_md = NULL;
120
121     return p_mlist;
122 }
123
124 /**************************************************************************
125  *       libvlc_media_list_release (Public)
126  *
127  * Release an object.
128  **************************************************************************/
129 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
130 {
131     libvlc_media_descriptor_t * p_md;
132     int i;
133
134     vlc_mutex_lock( &p_mlist->object_lock );
135     p_mlist->i_refcount--;
136     if( p_mlist->i_refcount > 0 )
137     {
138         vlc_mutex_unlock( &p_mlist->object_lock );
139         return;
140     }
141     vlc_mutex_unlock( &p_mlist->object_lock );
142
143     /* Refcount null, time to free */
144
145     /* Handled in flat_media_list.c */
146     libvlc_media_list_flat_media_list_release( p_mlist );
147
148     libvlc_event_manager_release( p_mlist->p_event_manager );
149
150     if( p_mlist->p_md )
151         libvlc_media_descriptor_release( p_mlist->p_md );
152
153     for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
154     {
155         p_md = vlc_array_object_at_index( &p_mlist->items, i );
156         libvlc_media_descriptor_release( p_md );
157     }
158
159     vlc_array_clear( &p_mlist->items );
160
161     free( p_mlist );
162 }
163
164 /**************************************************************************
165  *       libvlc_media_list_retain (Public)
166  *
167  * Increase an object refcount.
168  **************************************************************************/
169 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
170 {
171     vlc_mutex_lock( &p_mlist->object_lock );
172     p_mlist->i_refcount++;
173     vlc_mutex_unlock( &p_mlist->object_lock );
174 }
175
176
177 /**************************************************************************
178  *       add_file_content (Public)
179  **************************************************************************/
180 void
181 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
182                                     const char * psz_uri,
183                                     libvlc_exception_t * p_e )
184 {
185     input_item_t * p_input_item;
186     libvlc_media_descriptor_t * p_md;
187
188     p_input_item = input_ItemNewExt( p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
189                                 _("Media Library"), 0, NULL, -1 );
190
191     if( !p_input_item )
192     {
193         libvlc_exception_raise( p_e, "Can't create an input item" );
194         return;
195     }
196
197     p_md = libvlc_media_descriptor_new_from_input_item(
198             p_mlist->p_libvlc_instance,
199             p_input_item, p_e );
200
201     if( !p_md )
202     {
203         vlc_gc_decref( p_input_item );
204         return;
205     }
206
207     libvlc_media_list_add_media_descriptor( p_mlist, p_md, p_e );
208     if( libvlc_exception_raised( p_e ) )
209         return;
210
211     input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, VLC_TRUE );
212
213     return;
214 }
215
216 /**************************************************************************
217  *       set_media_descriptor (Public)
218  **************************************************************************/
219 void libvlc_media_list_set_media_descriptor( libvlc_media_list_t * p_mlist,
220                                              libvlc_media_descriptor_t * p_md,
221                                              libvlc_exception_t * p_e)
222
223 {
224     (void)p_e;
225     vlc_mutex_lock( &p_mlist->object_lock );
226     if( p_mlist->p_md )
227         libvlc_media_descriptor_release( p_mlist->p_md );
228     libvlc_media_descriptor_retain( p_md );
229     p_mlist->p_md = p_md;
230     vlc_mutex_unlock( &p_mlist->object_lock );
231 }
232
233 /**************************************************************************
234  *       media_descriptor (Public)
235  *
236  * If this media_list comes is a media_descriptor's subitems,
237  * This holds the corresponding media_descriptor.
238  * This md is also seen as the information holder for the media_list.
239  * Indeed a media_list can have meta information through this
240  * media_descriptor.
241  **************************************************************************/
242 libvlc_media_descriptor_t *
243 libvlc_media_list_media_descriptor( libvlc_media_list_t * p_mlist,
244                                     libvlc_exception_t * p_e)
245 {
246     libvlc_media_descriptor_t *p_md;
247     (void)p_e;
248
249     vlc_mutex_lock( &p_mlist->object_lock );
250     p_md = p_mlist->p_md;
251     if( p_md )
252         libvlc_media_descriptor_retain( p_md );
253     vlc_mutex_unlock( &p_mlist->object_lock );
254
255     return p_md;
256 }
257
258 /**************************************************************************
259  *       libvlc_media_list_count (Public)
260  *
261  * Lock should be hold when entering.
262  **************************************************************************/
263 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
264                              libvlc_exception_t * p_e )
265 {
266     (void)p_e;
267     return vlc_array_count( &p_mlist->items );
268 }
269
270 /**************************************************************************
271  *       libvlc_media_list_add_media_descriptor (Public)
272  *
273  * Lock should be hold when entering.
274  **************************************************************************/
275 void libvlc_media_list_add_media_descriptor(
276                                    libvlc_media_list_t * p_mlist,
277                                    libvlc_media_descriptor_t * p_md,
278                                    libvlc_exception_t * p_e )
279 {
280     (void)p_e;
281     libvlc_media_descriptor_retain( p_md );
282     vlc_array_append( &p_mlist->items, p_md );
283     notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1 );
284 }
285
286 /**************************************************************************
287  *       libvlc_media_list_insert_media_descriptor (Public)
288  *
289  * Lock should be hold when entering.
290  **************************************************************************/
291 void libvlc_media_list_insert_media_descriptor(
292                                    libvlc_media_list_t * p_mlist,
293                                    libvlc_media_descriptor_t * p_md,
294                                    int index,
295                                    libvlc_exception_t * p_e )
296 {
297     (void)p_e;
298     libvlc_media_descriptor_retain( p_md );
299
300     vlc_array_insert( &p_mlist->items, p_md, index );
301     notify_item_addition( p_mlist, p_md, index );
302 }
303
304 /**************************************************************************
305  *       libvlc_media_list_remove_index (Public)
306  *
307  * Lock should be hold when entering.
308  **************************************************************************/
309 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
310                                      int index,
311                                      libvlc_exception_t * p_e )
312 {
313     libvlc_media_descriptor_t * p_md;
314
315     p_md = vlc_array_object_at_index( &p_mlist->items, index );
316
317     vlc_array_remove( &p_mlist->items, index );
318     notify_item_deletion( p_mlist, p_md, index );
319
320     libvlc_media_descriptor_release( p_md );
321 }
322
323 /**************************************************************************
324  *       libvlc_media_list_item_at_index (Public)
325  *
326  * Lock should be hold when entering.
327  **************************************************************************/
328 libvlc_media_descriptor_t *
329 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
330                                  int index,
331                                  libvlc_exception_t * p_e )
332 {
333     libvlc_media_descriptor_t * p_md;
334     p_md = vlc_array_object_at_index( &p_mlist->items, index );
335     libvlc_media_descriptor_retain( p_md );
336     return p_md;
337 }
338
339 /**************************************************************************
340  *       libvlc_media_list_index_of_item (Public)
341  *
342  * Lock should be hold when entering.
343  * Warning: this function would return the first matching item
344  **************************************************************************/
345 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
346                                      libvlc_media_descriptor_t * p_searched_md,
347                                      libvlc_exception_t * p_e )
348 {
349     libvlc_media_descriptor_t * p_md;
350     int i;
351     for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
352     {
353         p_md = vlc_array_object_at_index( &p_mlist->items, i );
354         if( p_searched_md == p_md )
355             return i;
356     }
357     return -1;
358 }
359
360 /**************************************************************************
361  *       libvlc_media_list_lock (Public)
362  *
363  * The lock must be held in access operations. It is never used in the
364  * Public method.
365  **************************************************************************/
366 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
367 {
368     vlc_mutex_lock( &p_mlist->object_lock );
369 }
370
371
372 /**************************************************************************
373  *       libvlc_media_list_unlock (Public)
374  *
375  * The lock must be held in access operations
376  **************************************************************************/
377 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
378 {
379     vlc_mutex_unlock( &p_mlist->object_lock );
380 }
381
382
383
384 /**************************************************************************
385  *       libvlc_media_list_p_event_manager (Public)
386  *
387  * The p_event_manager is immutable, so you don't have to hold the lock
388  **************************************************************************/
389 libvlc_event_manager_t *
390 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
391                                     libvlc_exception_t * p_e )
392 {
393     (void)p_e;
394     return p_mlist->p_event_manager;
395 }