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 vlc_array_init( &p_mlist->items );
118 p_mlist->i_refcount = 1;
119 p_mlist->p_md = 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;
134 vlc_mutex_lock( &p_mlist->object_lock );
135 p_mlist->i_refcount--;
136 if( p_mlist->i_refcount > 0 )
138 vlc_mutex_unlock( &p_mlist->object_lock );
141 vlc_mutex_unlock( &p_mlist->object_lock );
143 /* Refcount null, time to free */
145 /* Handled in flat_media_list.c */
146 libvlc_media_list_flat_media_list_release( p_mlist );
148 libvlc_event_manager_release( p_mlist->p_event_manager );
151 libvlc_media_descriptor_release( p_mlist->p_md );
153 for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
155 p_md = vlc_array_object_at_index( &p_mlist->items, i );
156 libvlc_media_descriptor_release( p_md );
159 vlc_mutex_destroy( &p_mlist->object_lock );
160 vlc_array_clear( &p_mlist->items );
165 /**************************************************************************
166 * libvlc_media_list_retain (Public)
168 * Increase an object refcount.
169 **************************************************************************/
170 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
172 vlc_mutex_lock( &p_mlist->object_lock );
173 p_mlist->i_refcount++;
174 vlc_mutex_unlock( &p_mlist->object_lock );
178 /**************************************************************************
179 * add_file_content (Public)
180 **************************************************************************/
182 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
183 const char * psz_uri,
184 libvlc_exception_t * p_e )
186 input_item_t * p_input_item;
187 libvlc_media_descriptor_t * p_md;
189 p_input_item = input_ItemNewExt( p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
190 _("Media Library"), 0, NULL, -1 );
194 libvlc_exception_raise( p_e, "Can't create an input item" );
198 p_md = libvlc_media_descriptor_new_from_input_item(
199 p_mlist->p_libvlc_instance,
204 vlc_gc_decref( p_input_item );
208 libvlc_media_list_add_media_descriptor( p_mlist, p_md, p_e );
209 if( libvlc_exception_raised( p_e ) )
212 input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, VLC_TRUE );
217 /**************************************************************************
218 * set_media_descriptor (Public)
219 **************************************************************************/
220 void libvlc_media_list_set_media_descriptor( libvlc_media_list_t * p_mlist,
221 libvlc_media_descriptor_t * p_md,
222 libvlc_exception_t * p_e)
226 vlc_mutex_lock( &p_mlist->object_lock );
228 libvlc_media_descriptor_release( p_mlist->p_md );
229 libvlc_media_descriptor_retain( p_md );
230 p_mlist->p_md = p_md;
231 vlc_mutex_unlock( &p_mlist->object_lock );
234 /**************************************************************************
235 * media_descriptor (Public)
237 * If this media_list comes is a media_descriptor's subitems,
238 * This holds the corresponding media_descriptor.
239 * This md is also seen as the information holder for the media_list.
240 * Indeed a media_list can have meta information through this
242 **************************************************************************/
243 libvlc_media_descriptor_t *
244 libvlc_media_list_media_descriptor( libvlc_media_list_t * p_mlist,
245 libvlc_exception_t * p_e)
247 libvlc_media_descriptor_t *p_md;
250 vlc_mutex_lock( &p_mlist->object_lock );
251 p_md = p_mlist->p_md;
253 libvlc_media_descriptor_retain( p_md );
254 vlc_mutex_unlock( &p_mlist->object_lock );
259 /**************************************************************************
260 * libvlc_media_list_count (Public)
262 * Lock should be hold when entering.
263 **************************************************************************/
264 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
265 libvlc_exception_t * p_e )
268 return vlc_array_count( &p_mlist->items );
271 /**************************************************************************
272 * libvlc_media_list_add_media_descriptor (Public)
274 * Lock should be hold when entering.
275 **************************************************************************/
276 void libvlc_media_list_add_media_descriptor(
277 libvlc_media_list_t * p_mlist,
278 libvlc_media_descriptor_t * p_md,
279 libvlc_exception_t * p_e )
282 libvlc_media_descriptor_retain( p_md );
283 vlc_array_append( &p_mlist->items, p_md );
284 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1 );
287 /**************************************************************************
288 * libvlc_media_list_insert_media_descriptor (Public)
290 * Lock should be hold when entering.
291 **************************************************************************/
292 void libvlc_media_list_insert_media_descriptor(
293 libvlc_media_list_t * p_mlist,
294 libvlc_media_descriptor_t * p_md,
296 libvlc_exception_t * p_e )
299 libvlc_media_descriptor_retain( p_md );
301 vlc_array_insert( &p_mlist->items, p_md, index );
302 notify_item_addition( p_mlist, p_md, index );
305 /**************************************************************************
306 * libvlc_media_list_remove_index (Public)
308 * Lock should be hold when entering.
309 **************************************************************************/
310 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
312 libvlc_exception_t * p_e )
314 libvlc_media_descriptor_t * p_md;
316 p_md = vlc_array_object_at_index( &p_mlist->items, index );
318 vlc_array_remove( &p_mlist->items, index );
319 notify_item_deletion( p_mlist, p_md, index );
321 libvlc_media_descriptor_release( p_md );
324 /**************************************************************************
325 * libvlc_media_list_item_at_index (Public)
327 * Lock should be hold when entering.
328 **************************************************************************/
329 libvlc_media_descriptor_t *
330 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
332 libvlc_exception_t * p_e )
334 libvlc_media_descriptor_t * p_md;
335 p_md = vlc_array_object_at_index( &p_mlist->items, index );
336 libvlc_media_descriptor_retain( p_md );
340 /**************************************************************************
341 * libvlc_media_list_index_of_item (Public)
343 * Lock should be hold when entering.
344 * Warning: this function would return the first matching item
345 **************************************************************************/
346 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
347 libvlc_media_descriptor_t * p_searched_md,
348 libvlc_exception_t * p_e )
350 libvlc_media_descriptor_t * p_md;
352 for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
354 p_md = vlc_array_object_at_index( &p_mlist->items, i );
355 if( p_searched_md == p_md )
361 /**************************************************************************
362 * libvlc_media_list_lock (Public)
364 * The lock must be held in access operations. It is never used in the
366 **************************************************************************/
367 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
369 vlc_mutex_lock( &p_mlist->object_lock );
373 /**************************************************************************
374 * libvlc_media_list_unlock (Public)
376 * The lock must be held in access operations
377 **************************************************************************/
378 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
380 vlc_mutex_unlock( &p_mlist->object_lock );
385 /**************************************************************************
386 * libvlc_media_list_p_event_manager (Public)
388 * The p_event_manager is immutable, so you don't have to hold the lock
389 **************************************************************************/
390 libvlc_event_manager_t *
391 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
392 libvlc_exception_t * p_e )
395 return p_mlist->p_event_manager;