]> git.sesse.net Git - vlc/blob - src/control/media_list.c
control/media_list.c: Remove unused variable due to previous commit.
[vlc] / 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  *       media_descriptor_changed (private) (libvlc Event Callback )
79  *
80  * An item has changed.
81  **************************************************************************/
82 static void
83 media_descriptor_changed( const libvlc_event_t * p_event, void * user_data )
84 {
85     libvlc_media_list_t * p_mlist = user_data;
86     libvlc_media_descriptor_t * p_md = p_event->p_obj;
87     libvlc_event_t event;
88     
89     /* Construct the new media list event */
90     event.type = libvlc_MediaListItemChanged;
91     event.u.media_list_item_changed.item = p_md;
92
93     /* XXX: this is not good, but there is a solution in the pipeline */
94     event.u.media_list_item_changed.index =
95         libvlc_media_list_index_of_item( p_mlist, p_md, NULL );
96
97     /* Send the event */
98     libvlc_event_send( p_mlist->p_event_manager, &event );
99 }
100
101 /**************************************************************************
102  *       media_descriptor_subitem_added (private) (libvlc Event Callback )
103  *
104  * An item (which is a playlist) has gained sub child.
105  **************************************************************************/
106 static void
107 media_descriptor_subitem_added( const libvlc_event_t * p_event, void * user_data )
108 {
109     /* Todo: Just forward that event to our event_manager */
110 }
111
112 /**************************************************************************
113  *       install_media_descriptor_observer (private)
114  *
115  * Do the appropriate action when an item is deleted.
116  **************************************************************************/
117 static void
118 install_media_descriptor_observer( libvlc_media_list_t * p_mlist,
119                                    libvlc_media_descriptor_t * p_md )
120 {
121     libvlc_event_attach( p_md->p_event_manager,
122                          libvlc_MediaDescriptorMetaChanged,
123                          media_descriptor_changed,
124                          p_mlist, NULL );
125     libvlc_event_attach( p_md->p_event_manager,
126                          libvlc_MediaDescriptorSubItemAdded,
127                          media_descriptor_subitem_added,
128                          p_mlist, NULL );
129 }
130
131 /**************************************************************************
132  *       uninstall_media_descriptor_observer (private)
133  *
134  * Do the appropriate action when an item is deleted.
135  **************************************************************************/
136 static void
137 uninstall_media_descriptor_observer( libvlc_media_list_t * p_mlist,
138                                      libvlc_media_descriptor_t * p_md )
139 {
140     libvlc_event_detach( p_md->p_event_manager,
141                          libvlc_MediaDescriptorMetaChanged,
142                          media_descriptor_changed,
143                          p_mlist, NULL );
144     libvlc_event_detach( p_md->p_event_manager,
145                          libvlc_MediaDescriptorSubItemAdded,
146                          media_descriptor_subitem_added,
147                          p_mlist, NULL );
148 }
149
150 /*
151  * Public libvlc functions
152  */
153
154 /**************************************************************************
155  *       libvlc_media_list_new (Public)
156  *
157  * Init an object.
158  **************************************************************************/
159 libvlc_media_list_t *
160 libvlc_media_list_new( libvlc_instance_t * p_inst,
161                        libvlc_exception_t * p_e )
162 {
163     libvlc_media_list_t * p_mlist;
164
165     p_mlist = malloc(sizeof(libvlc_media_list_t));
166
167     if( !p_mlist )
168         return NULL;
169     
170     p_mlist->p_libvlc_instance = p_inst;
171     p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e );
172
173     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
174             libvlc_MediaListItemAdded, p_e );
175     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
176             libvlc_MediaListItemChanged, p_e );
177     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
178             libvlc_MediaListItemDeleted, p_e );
179
180     if( libvlc_exception_raised( p_e ) )
181     {
182         libvlc_event_manager_release( p_mlist->p_event_manager );
183         free( p_mlist );
184         return NULL;
185     }
186
187     vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock );
188     
189     ARRAY_INIT(p_mlist->items);
190     p_mlist->i_refcount = 1;
191     p_mlist->psz_name = NULL;
192
193     return p_mlist;
194 }
195
196 /**************************************************************************
197  *       libvlc_media_list_release (Public)
198  *
199  * Release an object.
200  **************************************************************************/
201 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
202 {
203     libvlc_media_descriptor_t * p_md;
204
205     vlc_mutex_lock( &p_mlist->object_lock );
206     p_mlist->i_refcount--;
207     if( p_mlist->i_refcount > 0 )
208     {
209         vlc_mutex_unlock( &p_mlist->object_lock );        
210         return;
211     }
212     vlc_mutex_unlock( &p_mlist->object_lock );        
213
214     /* Refcount null, time to free */
215
216     libvlc_event_manager_release( p_mlist->p_event_manager );
217
218     FOREACH_ARRAY( p_md, p_mlist->items )
219         uninstall_media_descriptor_observer( p_mlist, p_md );
220         libvlc_media_descriptor_release( p_md );
221     FOREACH_END()
222  
223     free( p_mlist );
224 }
225
226 /**************************************************************************
227  *       libvlc_media_list_retain (Public)
228  *
229  * Increase an object refcount.
230  **************************************************************************/
231 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
232 {
233     vlc_mutex_lock( &p_mlist->object_lock );
234     p_mlist->i_refcount++;
235     vlc_mutex_unlock( &p_mlist->object_lock );
236 }
237
238
239 /**************************************************************************
240  *       add_file_content (Public)
241  **************************************************************************/
242 void
243 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
244                                     const char * psz_uri,
245                                     libvlc_exception_t * p_e )
246 {
247     input_item_t * p_input_item;
248     libvlc_media_descriptor_t * p_md;
249
250     p_input_item = input_ItemNewExt( p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
251                                 _("Media Library"), 0, NULL, -1 );
252
253     if( !p_input_item )
254     {
255         libvlc_exception_raise( p_e, "Can't create an input item" );
256         return;
257     }
258
259     p_md = libvlc_media_descriptor_new_from_input_item(
260             p_mlist->p_libvlc_instance,
261             p_input_item, p_e );
262
263     if( !p_md )
264     {
265         vlc_gc_decref( p_input_item );
266         return;
267     }
268
269     libvlc_media_list_add_media_descriptor( p_mlist, p_md, p_e );
270     if( libvlc_exception_raised( p_e ) )
271         return;
272
273     input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, VLC_TRUE );
274
275     return;
276 }
277
278 /**************************************************************************
279  *       set_name (Public)
280  **************************************************************************/
281 void libvlc_media_list_set_name( libvlc_media_list_t * p_mlist,
282                                  const char * psz_name,
283                                  libvlc_exception_t * p_e)
284
285 {
286     (void)p_e;
287     vlc_mutex_lock( &p_mlist->object_lock );
288     free( p_mlist->psz_name );
289     p_mlist->psz_name = psz_name ? strdup( psz_name ) : NULL;
290     vlc_mutex_unlock( &p_mlist->object_lock );
291 }
292
293 /**************************************************************************
294  *       name (Public)
295  **************************************************************************/
296 char * libvlc_media_list_name( libvlc_media_list_t * p_mlist,
297                                libvlc_exception_t * p_e)
298 {
299     char *ret;
300     (void)p_e;
301
302     vlc_mutex_lock( &p_mlist->object_lock );
303     ret = p_mlist->psz_name ? strdup( p_mlist->psz_name ) : NULL;
304     vlc_mutex_unlock( &p_mlist->object_lock );
305
306     return ret;
307 }
308
309 /**************************************************************************
310  *       libvlc_media_list_count (Public)
311  *
312  * Lock should be hold when entering.
313  **************************************************************************/
314 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
315                              libvlc_exception_t * p_e )
316 {
317     (void)p_e;
318     return p_mlist->items.i_size;
319 }
320
321 /**************************************************************************
322  *       libvlc_media_list_add_media_descriptor (Public)
323  *
324  * Lock should be hold when entering.
325  **************************************************************************/
326 void libvlc_media_list_add_media_descriptor( 
327                                    libvlc_media_list_t * p_mlist,
328                                    libvlc_media_descriptor_t * p_md,
329                                    libvlc_exception_t * p_e )
330 {
331     (void)p_e;
332     libvlc_media_descriptor_retain( p_md );
333     ARRAY_APPEND( p_mlist->items, p_md );
334     notify_item_addition( p_mlist, p_md, p_mlist->items.i_size-1 );
335     install_media_descriptor_observer( p_mlist, p_md );
336 }
337
338 /**************************************************************************
339  *       libvlc_media_list_insert_media_descriptor (Public)
340  *
341  * Lock should be hold when entering.
342  **************************************************************************/
343 void libvlc_media_list_insert_media_descriptor( 
344                                    libvlc_media_list_t * p_mlist,
345                                    libvlc_media_descriptor_t * p_md,
346                                    int index,
347                                    libvlc_exception_t * p_e )
348 {
349     (void)p_e;
350     libvlc_media_descriptor_retain( p_md );
351
352     ARRAY_INSERT( p_mlist->items, p_md, index);
353     notify_item_addition( p_mlist, p_md, index );
354     install_media_descriptor_observer( p_mlist, p_md );
355 }
356
357 /**************************************************************************
358  *       libvlc_media_list_remove_index (Public)
359  *
360  * Lock should be hold when entering.
361  **************************************************************************/
362 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
363                                      int index,
364                                      libvlc_exception_t * p_e )
365 {
366     libvlc_media_descriptor_t * p_md;
367
368     p_md = ARRAY_VAL( p_mlist->items, index );
369
370     uninstall_media_descriptor_observer( p_mlist, p_md );
371
372     ARRAY_REMOVE( p_mlist->items, index )
373     notify_item_deletion( p_mlist, p_md, index );
374
375     libvlc_media_descriptor_release( p_md );
376 }
377
378 /**************************************************************************
379  *       libvlc_media_list_item_at_index (Public)
380  *
381  * Lock should be hold when entering.
382  **************************************************************************/
383 libvlc_media_descriptor_t *
384 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
385                                  int index,
386                                  libvlc_exception_t * p_e )
387 {
388     libvlc_media_descriptor_t * p_md =  ARRAY_VAL( p_mlist->items, index );
389     libvlc_media_descriptor_retain( p_md );
390     return p_md;
391 }
392
393 /**************************************************************************
394  *       libvlc_media_list_index_of_item (Public)
395  *
396  * Lock should be hold when entering.
397  * Warning: this function would return the first matching item
398  **************************************************************************/
399 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
400                                      libvlc_media_descriptor_t * p_searched_md,
401                                      libvlc_exception_t * p_e )
402 {
403     libvlc_media_descriptor_t * p_md;
404     FOREACH_ARRAY( p_md, p_mlist->items )
405         if( p_searched_md == p_md )
406             return fe_idx; /* Once more, we hate macro for that */
407     FOREACH_END()
408     return -1;
409 }
410
411 /**************************************************************************
412  *       libvlc_media_list_lock (Public)
413  *
414  * The lock must be held in access operations. It is never used in the
415  * Public method.
416  **************************************************************************/
417 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
418 {
419     vlc_mutex_lock( &p_mlist->object_lock );
420 }
421
422
423 /**************************************************************************
424  *       libvlc_media_list_unlock (Public)
425  *
426  * The lock must be held in access operations
427  **************************************************************************/
428 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
429 {
430     vlc_mutex_unlock( &p_mlist->object_lock );
431 }
432
433
434
435 /**************************************************************************
436  *       libvlc_media_list_p_event_manager (Public)
437  *
438  * The p_event_manager is immutable, so you don't have to hold the lock
439  **************************************************************************/
440 libvlc_event_manager_t *
441 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
442                                     libvlc_exception_t * p_e )
443 {
444     (void)p_e;
445     return p_mlist->p_event_manager;
446 }