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 );
78 * Public libvlc functions
81 /**************************************************************************
82 * libvlc_media_list_new (Public)
85 **************************************************************************/
87 libvlc_media_list_new( libvlc_instance_t * p_inst,
88 libvlc_exception_t * p_e )
90 libvlc_media_list_t * p_mlist;
92 p_mlist = malloc(sizeof(libvlc_media_list_t));
97 p_mlist->p_libvlc_instance = p_inst;
98 p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e );
100 /* Code for that one should be handled in flat_media_list.c */
101 p_mlist->p_flat_mlist = NULL;
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 );
108 if( libvlc_exception_raised( p_e ) )
110 libvlc_event_manager_release( p_mlist->p_event_manager );
115 vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock );
117 ARRAY_INIT(p_mlist->items);
118 p_mlist->i_refcount = 1;
119 p_mlist->psz_name = NULL;
124 /**************************************************************************
125 * libvlc_media_list_release (Public)
128 **************************************************************************/
129 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
131 libvlc_media_descriptor_t * p_md;
133 vlc_mutex_lock( &p_mlist->object_lock );
134 p_mlist->i_refcount--;
135 if( p_mlist->i_refcount > 0 )
137 vlc_mutex_unlock( &p_mlist->object_lock );
140 vlc_mutex_unlock( &p_mlist->object_lock );
142 /* Refcount null, time to free */
144 /* Handled in flat_media_list.c */
145 libvlc_media_list_flat_media_list_release( p_mlist );
147 libvlc_event_manager_release( p_mlist->p_event_manager );
149 FOREACH_ARRAY( p_md, p_mlist->items )
150 libvlc_media_descriptor_release( p_md );
156 /**************************************************************************
157 * libvlc_media_list_retain (Public)
159 * Increase an object refcount.
160 **************************************************************************/
161 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
163 vlc_mutex_lock( &p_mlist->object_lock );
164 p_mlist->i_refcount++;
165 vlc_mutex_unlock( &p_mlist->object_lock );
169 /**************************************************************************
170 * add_file_content (Public)
171 **************************************************************************/
173 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
174 const char * psz_uri,
175 libvlc_exception_t * p_e )
177 input_item_t * p_input_item;
178 libvlc_media_descriptor_t * p_md;
180 p_input_item = input_ItemNewExt( p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
181 _("Media Library"), 0, NULL, -1 );
185 libvlc_exception_raise( p_e, "Can't create an input item" );
189 p_md = libvlc_media_descriptor_new_from_input_item(
190 p_mlist->p_libvlc_instance,
195 vlc_gc_decref( p_input_item );
199 libvlc_media_list_add_media_descriptor( p_mlist, p_md, p_e );
200 if( libvlc_exception_raised( p_e ) )
203 input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, VLC_TRUE );
208 /**************************************************************************
210 **************************************************************************/
211 void libvlc_media_list_set_name( libvlc_media_list_t * p_mlist,
212 const char * psz_name,
213 libvlc_exception_t * p_e)
217 vlc_mutex_lock( &p_mlist->object_lock );
218 free( p_mlist->psz_name );
219 p_mlist->psz_name = psz_name ? strdup( psz_name ) : NULL;
220 vlc_mutex_unlock( &p_mlist->object_lock );
223 /**************************************************************************
225 **************************************************************************/
226 char * libvlc_media_list_name( libvlc_media_list_t * p_mlist,
227 libvlc_exception_t * p_e)
232 vlc_mutex_lock( &p_mlist->object_lock );
233 ret = p_mlist->psz_name ? strdup( p_mlist->psz_name ) : NULL;
234 vlc_mutex_unlock( &p_mlist->object_lock );
239 /**************************************************************************
240 * libvlc_media_list_count (Public)
242 * Lock should be hold when entering.
243 **************************************************************************/
244 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
245 libvlc_exception_t * p_e )
248 return p_mlist->items.i_size;
251 /**************************************************************************
252 * libvlc_media_list_add_media_descriptor (Public)
254 * Lock should be hold when entering.
255 **************************************************************************/
256 void libvlc_media_list_add_media_descriptor(
257 libvlc_media_list_t * p_mlist,
258 libvlc_media_descriptor_t * p_md,
259 libvlc_exception_t * p_e )
262 libvlc_media_descriptor_retain( p_md );
263 ARRAY_INSERT( p_mlist->items, p_md, p_mlist->items.i_size );
264 notify_item_addition( p_mlist, p_md, p_mlist->items.i_size-1 );
267 /**************************************************************************
268 * libvlc_media_list_insert_media_descriptor (Public)
270 * Lock should be hold when entering.
271 **************************************************************************/
272 void libvlc_media_list_insert_media_descriptor(
273 libvlc_media_list_t * p_mlist,
274 libvlc_media_descriptor_t * p_md,
276 libvlc_exception_t * p_e )
279 libvlc_media_descriptor_retain( p_md );
281 ARRAY_INSERT( p_mlist->items, p_md, index);
282 notify_item_addition( p_mlist, p_md, index );
285 /**************************************************************************
286 * libvlc_media_list_remove_index (Public)
288 * Lock should be hold when entering.
289 **************************************************************************/
290 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
292 libvlc_exception_t * p_e )
294 libvlc_media_descriptor_t * p_md;
296 p_md = ARRAY_VAL( p_mlist->items, index );
298 ARRAY_REMOVE( p_mlist->items, index )
299 notify_item_deletion( p_mlist, p_md, index );
301 libvlc_media_descriptor_release( p_md );
304 /**************************************************************************
305 * libvlc_media_list_item_at_index (Public)
307 * Lock should be hold when entering.
308 **************************************************************************/
309 libvlc_media_descriptor_t *
310 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
312 libvlc_exception_t * p_e )
314 libvlc_media_descriptor_t * p_md = ARRAY_VAL( p_mlist->items, index );
315 libvlc_media_descriptor_retain( p_md );
319 /**************************************************************************
320 * libvlc_media_list_index_of_item (Public)
322 * Lock should be hold when entering.
323 * Warning: this function would return the first matching item
324 **************************************************************************/
325 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
326 libvlc_media_descriptor_t * p_searched_md,
327 libvlc_exception_t * p_e )
329 libvlc_media_descriptor_t * p_md;
330 FOREACH_ARRAY( p_md, p_mlist->items )
331 if( p_searched_md == p_md )
332 return fe_idx; /* Once more, we hate macro for that */
337 /**************************************************************************
338 * libvlc_media_list_lock (Public)
340 * The lock must be held in access operations. It is never used in the
342 **************************************************************************/
343 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
345 vlc_mutex_lock( &p_mlist->object_lock );
349 /**************************************************************************
350 * libvlc_media_list_unlock (Public)
352 * The lock must be held in access operations
353 **************************************************************************/
354 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
356 vlc_mutex_unlock( &p_mlist->object_lock );
361 /**************************************************************************
362 * libvlc_media_list_p_event_manager (Public)
364 * The p_event_manager is immutable, so you don't have to hold the lock
365 **************************************************************************/
366 libvlc_event_manager_t *
367 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
368 libvlc_exception_t * p_e )
371 return p_mlist->p_event_manager;