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_MediaListItemDeleted, p_e );
133 if( libvlc_exception_raised( p_e ) )
135 libvlc_event_manager_release( p_mlist->p_event_manager );
140 vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock );
142 vlc_array_init( &p_mlist->items );
143 p_mlist->i_refcount = 1;
144 p_mlist->p_md = NULL;
149 /**************************************************************************
150 * libvlc_media_list_release (Public)
153 **************************************************************************/
154 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
156 libvlc_media_descriptor_t * p_md;
159 vlc_mutex_lock( &p_mlist->object_lock );
160 p_mlist->i_refcount--;
161 if( p_mlist->i_refcount > 0 )
163 vlc_mutex_unlock( &p_mlist->object_lock );
166 vlc_mutex_unlock( &p_mlist->object_lock );
168 /* Refcount null, time to free */
170 libvlc_event_manager_release( p_mlist->p_event_manager );
173 libvlc_media_descriptor_release( p_mlist->p_md );
175 for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
177 p_md = vlc_array_item_at_index( &p_mlist->items, i );
178 libvlc_media_descriptor_release( p_md );
181 vlc_mutex_destroy( &p_mlist->object_lock );
182 vlc_array_clear( &p_mlist->items );
187 /**************************************************************************
188 * libvlc_media_list_retain (Public)
190 * Increase an object refcount.
191 **************************************************************************/
192 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
194 vlc_mutex_lock( &p_mlist->object_lock );
195 p_mlist->i_refcount++;
196 vlc_mutex_unlock( &p_mlist->object_lock );
200 /**************************************************************************
201 * add_file_content (Public)
202 **************************************************************************/
204 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
205 const char * psz_uri,
206 libvlc_exception_t * p_e )
208 input_item_t * p_input_item;
209 libvlc_media_descriptor_t * p_md;
211 p_input_item = input_ItemNewExt( p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
212 _("Media Library"), 0, NULL, -1 );
216 libvlc_exception_raise( p_e, "Can't create an input item" );
220 p_md = libvlc_media_descriptor_new_from_input_item(
221 p_mlist->p_libvlc_instance,
226 vlc_gc_decref( p_input_item );
230 libvlc_media_list_add_media_descriptor( p_mlist, p_md, p_e );
231 if( libvlc_exception_raised( p_e ) )
234 input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, VLC_TRUE );
239 /**************************************************************************
240 * set_media_descriptor (Public)
241 **************************************************************************/
242 void libvlc_media_list_set_media_descriptor( libvlc_media_list_t * p_mlist,
243 libvlc_media_descriptor_t * p_md,
244 libvlc_exception_t * p_e)
248 vlc_mutex_lock( &p_mlist->object_lock );
250 libvlc_media_descriptor_release( p_mlist->p_md );
251 libvlc_media_descriptor_retain( p_md );
252 p_mlist->p_md = p_md;
253 vlc_mutex_unlock( &p_mlist->object_lock );
256 /**************************************************************************
257 * media_descriptor (Public)
259 * If this media_list comes is a media_descriptor's subitems,
260 * This holds the corresponding media_descriptor.
261 * This md is also seen as the information holder for the media_list.
262 * Indeed a media_list can have meta information through this
264 **************************************************************************/
265 libvlc_media_descriptor_t *
266 libvlc_media_list_media_descriptor( libvlc_media_list_t * p_mlist,
267 libvlc_exception_t * p_e)
269 libvlc_media_descriptor_t *p_md;
272 vlc_mutex_lock( &p_mlist->object_lock );
273 p_md = p_mlist->p_md;
275 libvlc_media_descriptor_retain( p_md );
276 vlc_mutex_unlock( &p_mlist->object_lock );
281 /**************************************************************************
282 * libvlc_media_list_count (Public)
284 * Lock should be hold when entering.
285 **************************************************************************/
286 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
287 libvlc_exception_t * p_e )
290 return vlc_array_count( &p_mlist->items );
293 /**************************************************************************
294 * libvlc_media_list_add_media_descriptor (Public)
296 * Lock should be hold when entering.
297 **************************************************************************/
298 void libvlc_media_list_add_media_descriptor(
299 libvlc_media_list_t * p_mlist,
300 libvlc_media_descriptor_t * p_md,
301 libvlc_exception_t * p_e )
304 libvlc_media_descriptor_retain( p_md );
306 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ), EventWillHappen );
307 vlc_array_append( &p_mlist->items, p_md );
308 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1, EventDidHappen );
311 /**************************************************************************
312 * libvlc_media_list_insert_media_descriptor (Public)
314 * Lock should be hold when entering.
315 **************************************************************************/
316 void libvlc_media_list_insert_media_descriptor(
317 libvlc_media_list_t * p_mlist,
318 libvlc_media_descriptor_t * p_md,
320 libvlc_exception_t * p_e )
323 libvlc_media_descriptor_retain( p_md );
325 notify_item_addition( p_mlist, p_md, index, EventWillHappen );
326 vlc_array_insert( &p_mlist->items, p_md, index );
327 notify_item_addition( p_mlist, p_md, index, EventDidHappen );
330 /**************************************************************************
331 * libvlc_media_list_remove_index (Public)
333 * Lock should be hold when entering.
334 **************************************************************************/
335 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
337 libvlc_exception_t * p_e )
339 libvlc_media_descriptor_t * p_md;
341 p_md = vlc_array_item_at_index( &p_mlist->items, index );
343 notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
344 vlc_array_remove( &p_mlist->items, index );
345 notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
347 libvlc_media_descriptor_release( p_md );
350 /**************************************************************************
351 * libvlc_media_list_item_at_index (Public)
353 * Lock should be hold when entering.
354 **************************************************************************/
355 libvlc_media_descriptor_t *
356 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
358 libvlc_exception_t * p_e )
360 libvlc_media_descriptor_t * p_md;
361 p_md = vlc_array_item_at_index( &p_mlist->items, index );
362 libvlc_media_descriptor_retain( p_md );
366 /**************************************************************************
367 * libvlc_media_list_index_of_item (Public)
369 * Lock should be hold when entering.
370 * Warning: this function would return the first matching item
371 **************************************************************************/
372 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
373 libvlc_media_descriptor_t * p_searched_md,
374 libvlc_exception_t * p_e )
376 libvlc_media_descriptor_t * p_md;
378 for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
380 p_md = vlc_array_item_at_index( &p_mlist->items, i );
381 if( p_searched_md == p_md )
387 /**************************************************************************
388 * libvlc_media_list_lock (Public)
390 * The lock must be held in access operations. It is never used in the
392 **************************************************************************/
393 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
395 vlc_mutex_lock( &p_mlist->object_lock );
399 /**************************************************************************
400 * libvlc_media_list_unlock (Public)
402 * The lock must be held in access operations
403 **************************************************************************/
404 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
406 vlc_mutex_unlock( &p_mlist->object_lock );
411 /**************************************************************************
412 * libvlc_media_list_p_event_manager (Public)
414 * The p_event_manager is immutable, so you don't have to hold the lock
415 **************************************************************************/
416 libvlc_event_manager_t *
417 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
418 libvlc_exception_t * p_e )
421 return p_mlist->p_event_manager;