1 /*****************************************************************************
2 * media_list.c: libvlc new API media list functions
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
7 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
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.
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.
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 *****************************************************************************/
24 #include "libvlc_internal.h"
25 #include <vlc/libvlc.h>
27 #include "vlc_arrays.h"
35 /**************************************************************************
36 * notify_item_addition (private)
38 * Do the appropriate action when an item is deleted.
39 **************************************************************************/
41 notify_item_addition( libvlc_media_list_t * p_mlist,
42 libvlc_media_descriptor_t * p_md,
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;
53 libvlc_event_send( p_mlist->p_event_manager, &event );
56 /**************************************************************************
57 * notify_item_deletion (private)
59 * Do the appropriate action when an item is added.
60 **************************************************************************/
62 notify_item_deletion( libvlc_media_list_t * p_mlist,
63 libvlc_media_descriptor_t * p_md,
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;
74 libvlc_event_send( p_mlist->p_event_manager, &event );
77 /**************************************************************************
78 * media_descriptor_changed (private) (libvlc Event Callback )
80 * An item has changed.
81 **************************************************************************/
83 media_descriptor_changed( const libvlc_event_t * p_event, void * user_data )
85 libvlc_media_list_t * p_mlist = user_data;
86 libvlc_media_descriptor_t * p_md = p_event->p_obj;
89 /* Construct the new media list event */
90 event.type = libvlc_MediaListItemChanged;
91 event.u.media_list_item_changed.item = p_md;
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 );
98 libvlc_event_send( p_mlist->p_event_manager, &event );
101 /**************************************************************************
102 * media_descriptor_subitem_added (private) (libvlc Event Callback )
104 * An item (which is a playlist) has gained sub child.
105 **************************************************************************/
107 media_descriptor_subitem_added( const libvlc_event_t * p_event, void * user_data )
109 /* Todo: Just forward that event to our event_manager */
112 /**************************************************************************
113 * install_media_descriptor_observer (private)
115 * Do the appropriate action when an item is deleted.
116 **************************************************************************/
118 install_media_descriptor_observer( libvlc_media_list_t * p_mlist,
119 libvlc_media_descriptor_t * p_md )
121 libvlc_event_attach( p_md->p_event_manager,
122 libvlc_MediaDescriptorMetaChanged,
123 media_descriptor_changed,
125 libvlc_event_attach( p_md->p_event_manager,
126 libvlc_MediaDescriptorSubItemAdded,
127 media_descriptor_subitem_added,
131 /**************************************************************************
132 * uninstall_media_descriptor_observer (private)
134 * Do the appropriate action when an item is deleted.
135 **************************************************************************/
137 uninstall_media_descriptor_observer( libvlc_media_list_t * p_mlist,
138 libvlc_media_descriptor_t * p_md )
140 libvlc_event_detach( p_md->p_event_manager,
141 libvlc_MediaDescriptorMetaChanged,
142 media_descriptor_changed,
144 libvlc_event_detach( p_md->p_event_manager,
145 libvlc_MediaDescriptorSubItemAdded,
146 media_descriptor_subitem_added,
151 * Public libvlc functions
154 /**************************************************************************
155 * libvlc_media_list_new (Public)
158 **************************************************************************/
159 libvlc_media_list_t *
160 libvlc_media_list_new( libvlc_instance_t * p_inst,
161 libvlc_exception_t * p_e )
163 libvlc_media_list_t * p_mlist;
165 p_mlist = malloc(sizeof(libvlc_media_list_t));
170 p_mlist->p_libvlc_instance = p_inst;
171 p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e );
173 /* Code for that one should be handled in flat_media_list.c */
174 p_mlist->p_flat_mlist = NULL;
176 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
177 libvlc_MediaListItemAdded, p_e );
178 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
179 libvlc_MediaListItemChanged, p_e );
180 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
181 libvlc_MediaListItemDeleted, p_e );
183 if( libvlc_exception_raised( p_e ) )
185 libvlc_event_manager_release( p_mlist->p_event_manager );
190 vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock );
192 ARRAY_INIT(p_mlist->items);
193 p_mlist->i_refcount = 1;
194 p_mlist->psz_name = NULL;
199 /**************************************************************************
200 * libvlc_media_list_release (Public)
203 **************************************************************************/
204 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
206 libvlc_media_descriptor_t * p_md;
208 vlc_mutex_lock( &p_mlist->object_lock );
209 p_mlist->i_refcount--;
210 if( p_mlist->i_refcount > 0 )
212 vlc_mutex_unlock( &p_mlist->object_lock );
215 vlc_mutex_unlock( &p_mlist->object_lock );
217 /* Refcount null, time to free */
219 /* Handled in flat_media_list.c */
220 if( p_mlist->p_flat_mlist )
221 libvlc_media_list_release( p_mlist->p_flat_mlist );
223 libvlc_event_manager_release( p_mlist->p_event_manager );
225 FOREACH_ARRAY( p_md, p_mlist->items )
226 uninstall_media_descriptor_observer( p_mlist, p_md );
227 libvlc_media_descriptor_release( p_md );
233 /**************************************************************************
234 * libvlc_media_list_retain (Public)
236 * Increase an object refcount.
237 **************************************************************************/
238 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
240 vlc_mutex_lock( &p_mlist->object_lock );
241 p_mlist->i_refcount++;
242 vlc_mutex_unlock( &p_mlist->object_lock );
246 /**************************************************************************
247 * add_file_content (Public)
248 **************************************************************************/
250 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
251 const char * psz_uri,
252 libvlc_exception_t * p_e )
254 input_item_t * p_input_item;
255 libvlc_media_descriptor_t * p_md;
257 p_input_item = input_ItemNewExt( p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
258 _("Media Library"), 0, NULL, -1 );
262 libvlc_exception_raise( p_e, "Can't create an input item" );
266 p_md = libvlc_media_descriptor_new_from_input_item(
267 p_mlist->p_libvlc_instance,
272 vlc_gc_decref( p_input_item );
276 libvlc_media_list_add_media_descriptor( p_mlist, p_md, p_e );
277 if( libvlc_exception_raised( p_e ) )
280 input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, VLC_TRUE );
285 /**************************************************************************
287 **************************************************************************/
288 void libvlc_media_list_set_name( libvlc_media_list_t * p_mlist,
289 const char * psz_name,
290 libvlc_exception_t * p_e)
294 vlc_mutex_lock( &p_mlist->object_lock );
295 free( p_mlist->psz_name );
296 p_mlist->psz_name = psz_name ? strdup( psz_name ) : NULL;
297 vlc_mutex_unlock( &p_mlist->object_lock );
300 /**************************************************************************
302 **************************************************************************/
303 char * libvlc_media_list_name( libvlc_media_list_t * p_mlist,
304 libvlc_exception_t * p_e)
309 vlc_mutex_lock( &p_mlist->object_lock );
310 ret = p_mlist->psz_name ? strdup( p_mlist->psz_name ) : NULL;
311 vlc_mutex_unlock( &p_mlist->object_lock );
316 /**************************************************************************
317 * libvlc_media_list_count (Public)
319 * Lock should be hold when entering.
320 **************************************************************************/
321 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
322 libvlc_exception_t * p_e )
325 return p_mlist->items.i_size;
328 /**************************************************************************
329 * libvlc_media_list_add_media_descriptor (Public)
331 * Lock should be hold when entering.
332 **************************************************************************/
333 void libvlc_media_list_add_media_descriptor(
334 libvlc_media_list_t * p_mlist,
335 libvlc_media_descriptor_t * p_md,
336 libvlc_exception_t * p_e )
339 libvlc_media_descriptor_retain( p_md );
340 ARRAY_APPEND( p_mlist->items, p_md );
341 notify_item_addition( p_mlist, p_md, p_mlist->items.i_size-1 );
342 install_media_descriptor_observer( p_mlist, p_md );
345 /**************************************************************************
346 * libvlc_media_list_insert_media_descriptor (Public)
348 * Lock should be hold when entering.
349 **************************************************************************/
350 void libvlc_media_list_insert_media_descriptor(
351 libvlc_media_list_t * p_mlist,
352 libvlc_media_descriptor_t * p_md,
354 libvlc_exception_t * p_e )
357 libvlc_media_descriptor_retain( p_md );
359 ARRAY_INSERT( p_mlist->items, p_md, index);
360 notify_item_addition( p_mlist, p_md, index );
361 install_media_descriptor_observer( p_mlist, p_md );
364 /**************************************************************************
365 * libvlc_media_list_remove_index (Public)
367 * Lock should be hold when entering.
368 **************************************************************************/
369 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
371 libvlc_exception_t * p_e )
373 libvlc_media_descriptor_t * p_md;
375 p_md = ARRAY_VAL( p_mlist->items, index );
377 uninstall_media_descriptor_observer( p_mlist, p_md );
379 ARRAY_REMOVE( p_mlist->items, index )
380 notify_item_deletion( p_mlist, p_md, index );
382 libvlc_media_descriptor_release( p_md );
385 /**************************************************************************
386 * libvlc_media_list_item_at_index (Public)
388 * Lock should be hold when entering.
389 **************************************************************************/
390 libvlc_media_descriptor_t *
391 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
393 libvlc_exception_t * p_e )
395 libvlc_media_descriptor_t * p_md = ARRAY_VAL( p_mlist->items, index );
396 libvlc_media_descriptor_retain( p_md );
400 /**************************************************************************
401 * libvlc_media_list_index_of_item (Public)
403 * Lock should be hold when entering.
404 * Warning: this function would return the first matching item
405 **************************************************************************/
406 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
407 libvlc_media_descriptor_t * p_searched_md,
408 libvlc_exception_t * p_e )
410 libvlc_media_descriptor_t * p_md;
411 FOREACH_ARRAY( p_md, p_mlist->items )
412 if( p_searched_md == p_md )
413 return fe_idx; /* Once more, we hate macro for that */
418 /**************************************************************************
419 * libvlc_media_list_lock (Public)
421 * The lock must be held in access operations. It is never used in the
423 **************************************************************************/
424 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
426 vlc_mutex_lock( &p_mlist->object_lock );
430 /**************************************************************************
431 * libvlc_media_list_unlock (Public)
433 * The lock must be held in access operations
434 **************************************************************************/
435 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
437 vlc_mutex_unlock( &p_mlist->object_lock );
442 /**************************************************************************
443 * libvlc_media_list_p_event_manager (Public)
445 * The p_event_manager is immutable, so you don't have to hold the lock
446 **************************************************************************/
447 libvlc_event_manager_t *
448 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
449 libvlc_exception_t * p_e )
452 return p_mlist->p_event_manager;