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"
29 typedef enum EventPlaceInTime {
40 /**************************************************************************
41 * notify_item_addition (private)
43 * Do the appropriate action when an item is deleted.
44 **************************************************************************/
46 notify_item_addition( libvlc_media_list_t * p_mlist,
47 libvlc_media_descriptor_t * p_md,
49 EventPlaceInTime event_status )
53 /* Construct the event */
54 if( event_status == EventDidHappen )
56 event.type = libvlc_MediaListItemAdded;
57 event.u.media_list_item_added.item = p_md;
58 event.u.media_list_item_added.index = index;
60 else /* if( event_status == EventWillHappen ) */
62 event.type = libvlc_MediaListWillAddItem;
63 event.u.media_list_will_add_item.item = p_md;
64 event.u.media_list_will_add_item.index = index;
68 libvlc_event_send( p_mlist->p_event_manager, &event );
71 /**************************************************************************
72 * notify_item_deletion (private)
74 * Do the appropriate action when an item is added.
75 **************************************************************************/
77 notify_item_deletion( libvlc_media_list_t * p_mlist,
78 libvlc_media_descriptor_t * p_md,
80 EventPlaceInTime event_status )
84 /* Construct the event */
85 if( event_status == EventDidHappen )
87 event.type = libvlc_MediaListItemDeleted;
88 event.u.media_list_item_deleted.item = p_md;
89 event.u.media_list_item_deleted.index = index;
91 else /* if( event_status == EventWillHappen ) */
93 event.type = libvlc_MediaListWillDeleteItem;
94 event.u.media_list_will_delete_item.item = p_md;
95 event.u.media_list_will_delete_item.index = index;
99 libvlc_event_send( p_mlist->p_event_manager, &event );
103 * Public libvlc functions
106 /**************************************************************************
107 * libvlc_media_list_new (Public)
110 **************************************************************************/
111 libvlc_media_list_t *
112 libvlc_media_list_new( libvlc_instance_t * p_inst,
113 libvlc_exception_t * p_e )
115 libvlc_media_list_t * p_mlist;
117 p_mlist = malloc(sizeof(libvlc_media_list_t));
122 p_mlist->p_libvlc_instance = p_inst;
123 p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e );
125 /* Code for that one should be handled in flat_media_list.c */
126 p_mlist->p_flat_mlist = NULL;
128 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
129 libvlc_MediaListItemAdded, p_e );
130 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
131 libvlc_MediaListWillAddItem, p_e );
132 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
133 libvlc_MediaListItemDeleted, p_e );
134 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
135 libvlc_MediaListWillDeleteItem, p_e );
137 if( libvlc_exception_raised( p_e ) )
139 libvlc_event_manager_release( p_mlist->p_event_manager );
144 vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock );
146 vlc_array_init( &p_mlist->items );
147 p_mlist->i_refcount = 1;
148 p_mlist->p_md = NULL;
153 /**************************************************************************
154 * libvlc_media_list_release (Public)
157 **************************************************************************/
158 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
160 libvlc_media_descriptor_t * p_md;
163 vlc_mutex_lock( &p_mlist->object_lock );
164 p_mlist->i_refcount--;
165 if( p_mlist->i_refcount > 0 )
167 vlc_mutex_unlock( &p_mlist->object_lock );
170 vlc_mutex_unlock( &p_mlist->object_lock );
172 /* Refcount null, time to free */
174 libvlc_event_manager_release( p_mlist->p_event_manager );
177 libvlc_media_descriptor_release( p_mlist->p_md );
179 for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
181 p_md = vlc_array_item_at_index( &p_mlist->items, i );
182 libvlc_media_descriptor_release( p_md );
185 vlc_mutex_destroy( &p_mlist->object_lock );
186 vlc_array_clear( &p_mlist->items );
191 /**************************************************************************
192 * libvlc_media_list_retain (Public)
194 * Increase an object refcount.
195 **************************************************************************/
196 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
198 vlc_mutex_lock( &p_mlist->object_lock );
199 p_mlist->i_refcount++;
200 vlc_mutex_unlock( &p_mlist->object_lock );
204 /**************************************************************************
205 * add_file_content (Public)
206 **************************************************************************/
208 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
209 const char * psz_uri,
210 libvlc_exception_t * p_e )
212 input_item_t * p_input_item;
213 libvlc_media_descriptor_t * p_md;
215 p_input_item = input_ItemNewExt( p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
216 _("Media Library"), 0, NULL, -1 );
220 libvlc_exception_raise( p_e, "Can't create an input item" );
224 p_md = libvlc_media_descriptor_new_from_input_item(
225 p_mlist->p_libvlc_instance,
230 vlc_gc_decref( p_input_item );
234 libvlc_media_list_add_media_descriptor( p_mlist, p_md, p_e );
235 if( libvlc_exception_raised( p_e ) )
238 input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, VLC_TRUE );
243 /**************************************************************************
244 * set_media_descriptor (Public)
245 **************************************************************************/
246 void libvlc_media_list_set_media_descriptor( libvlc_media_list_t * p_mlist,
247 libvlc_media_descriptor_t * p_md,
248 libvlc_exception_t * p_e)
252 vlc_mutex_lock( &p_mlist->object_lock );
254 libvlc_media_descriptor_release( p_mlist->p_md );
255 libvlc_media_descriptor_retain( p_md );
256 p_mlist->p_md = p_md;
257 vlc_mutex_unlock( &p_mlist->object_lock );
260 /**************************************************************************
261 * media_descriptor (Public)
263 * If this media_list comes is a media_descriptor's subitems,
264 * This holds the corresponding media_descriptor.
265 * This md is also seen as the information holder for the media_list.
266 * Indeed a media_list can have meta information through this
268 **************************************************************************/
269 libvlc_media_descriptor_t *
270 libvlc_media_list_media_descriptor( libvlc_media_list_t * p_mlist,
271 libvlc_exception_t * p_e)
273 libvlc_media_descriptor_t *p_md;
276 vlc_mutex_lock( &p_mlist->object_lock );
277 p_md = p_mlist->p_md;
279 libvlc_media_descriptor_retain( p_md );
280 vlc_mutex_unlock( &p_mlist->object_lock );
285 /**************************************************************************
286 * libvlc_media_list_count (Public)
288 * Lock should be hold when entering.
289 **************************************************************************/
290 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
291 libvlc_exception_t * p_e )
294 return vlc_array_count( &p_mlist->items );
297 /**************************************************************************
298 * libvlc_media_list_add_media_descriptor (Public)
300 * Lock should be hold when entering.
301 **************************************************************************/
302 void libvlc_media_list_add_media_descriptor(
303 libvlc_media_list_t * p_mlist,
304 libvlc_media_descriptor_t * p_md,
305 libvlc_exception_t * p_e )
308 libvlc_media_descriptor_retain( p_md );
310 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ), EventWillHappen );
311 vlc_array_append( &p_mlist->items, p_md );
312 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1, EventDidHappen );
315 /**************************************************************************
316 * libvlc_media_list_insert_media_descriptor (Public)
318 * Lock should be hold when entering.
319 **************************************************************************/
320 void libvlc_media_list_insert_media_descriptor(
321 libvlc_media_list_t * p_mlist,
322 libvlc_media_descriptor_t * p_md,
324 libvlc_exception_t * p_e )
327 libvlc_media_descriptor_retain( p_md );
329 notify_item_addition( p_mlist, p_md, index, EventWillHappen );
330 vlc_array_insert( &p_mlist->items, p_md, index );
331 notify_item_addition( p_mlist, p_md, index, EventDidHappen );
334 /**************************************************************************
335 * libvlc_media_list_remove_index (Public)
337 * Lock should be hold when entering.
338 **************************************************************************/
339 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
341 libvlc_exception_t * p_e )
343 libvlc_media_descriptor_t * p_md;
345 p_md = vlc_array_item_at_index( &p_mlist->items, index );
347 notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
348 vlc_array_remove( &p_mlist->items, index );
349 notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
351 libvlc_media_descriptor_release( p_md );
354 /**************************************************************************
355 * libvlc_media_list_item_at_index (Public)
357 * Lock should be hold when entering.
358 **************************************************************************/
359 libvlc_media_descriptor_t *
360 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
362 libvlc_exception_t * p_e )
364 libvlc_media_descriptor_t * p_md;
365 p_md = vlc_array_item_at_index( &p_mlist->items, index );
366 libvlc_media_descriptor_retain( p_md );
370 /**************************************************************************
371 * libvlc_media_list_index_of_item (Public)
373 * Lock should be hold when entering.
374 * Warning: this function would return the first matching item
375 **************************************************************************/
376 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
377 libvlc_media_descriptor_t * p_searched_md,
378 libvlc_exception_t * p_e )
380 libvlc_media_descriptor_t * p_md;
382 for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
384 p_md = vlc_array_item_at_index( &p_mlist->items, i );
385 if( p_searched_md == p_md )
391 /**************************************************************************
392 * libvlc_media_list_lock (Public)
394 * The lock must be held in access operations. It is never used in the
396 **************************************************************************/
397 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
399 vlc_mutex_lock( &p_mlist->object_lock );
403 /**************************************************************************
404 * libvlc_media_list_unlock (Public)
406 * The lock must be held in access operations
407 **************************************************************************/
408 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
410 vlc_mutex_unlock( &p_mlist->object_lock );
415 /**************************************************************************
416 * libvlc_media_list_p_event_manager (Public)
418 * The p_event_manager is immutable, so you don't have to hold the lock
419 **************************************************************************/
420 libvlc_event_manager_t *
421 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
422 libvlc_exception_t * p_e )
425 return p_mlist->p_event_manager;