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 *****************************************************************************/
26 #include <vlc/libvlc.h>
27 #include <vlc/libvlc_media.h>
28 #include <vlc/libvlc_media_list.h>
29 #include <vlc/libvlc_events.h>
31 #include <vlc_common.h>
32 #include <vlc_input.h>
34 #include "libvlc_internal.h"
35 #include "media_internal.h" // libvlc_media_new_from_input_item()
36 #include "media_list_internal.h"
38 typedef enum EventPlaceInTime {
43 //#define DEBUG_MEDIA_LIST
45 #ifdef DEBUG_MEDIA_LIST
46 # define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
57 /**************************************************************************
58 * notify_item_addition (private)
60 * Do the appropriate action when an item is deleted.
61 **************************************************************************/
63 notify_item_addition( libvlc_media_list_t * p_mlist,
64 libvlc_media_t * p_md,
66 EventPlaceInTime event_status )
70 /* Construct the event */
71 if( event_status == EventDidHappen )
73 trace("item was added at index %d\n", index);
74 event.type = libvlc_MediaListItemAdded;
75 event.u.media_list_item_added.item = p_md;
76 event.u.media_list_item_added.index = index;
78 else /* if( event_status == EventWillHappen ) */
80 event.type = libvlc_MediaListWillAddItem;
81 event.u.media_list_will_add_item.item = p_md;
82 event.u.media_list_will_add_item.index = index;
86 libvlc_event_send( p_mlist->p_event_manager, &event );
89 /**************************************************************************
90 * notify_item_deletion (private)
92 * Do the appropriate action when an item is added.
93 **************************************************************************/
95 notify_item_deletion( libvlc_media_list_t * p_mlist,
96 libvlc_media_t * p_md,
98 EventPlaceInTime event_status )
100 libvlc_event_t event;
102 /* Construct the event */
103 if( event_status == EventDidHappen )
105 trace("item at index %d was deleted\n", index);
106 event.type = libvlc_MediaListItemDeleted;
107 event.u.media_list_item_deleted.item = p_md;
108 event.u.media_list_item_deleted.index = index;
110 else /* if( event_status == EventWillHappen ) */
112 event.type = libvlc_MediaListWillDeleteItem;
113 event.u.media_list_will_delete_item.item = p_md;
114 event.u.media_list_will_delete_item.index = index;
118 libvlc_event_send( p_mlist->p_event_manager, &event );
121 /**************************************************************************
122 * static mlist_is_writable (private)
124 * Raise exception and return 0 when the media_list instance is read-only,
126 **************************************************************************/
128 int mlist_is_writable( libvlc_media_list_t *p_mlist, libvlc_exception_t *p_e )
130 if( !p_mlist||p_mlist->b_read_only )
132 /* We are read-only from user side */
133 libvlc_exception_raise( p_e, "Cannot write to read-only media list." );
140 * Public libvlc functions
143 /**************************************************************************
144 * libvlc_media_list_new (Public)
147 **************************************************************************/
148 libvlc_media_list_t *
149 libvlc_media_list_new( libvlc_instance_t * p_inst,
150 libvlc_exception_t * p_e )
152 libvlc_media_list_t * p_mlist;
154 p_mlist = malloc(sizeof(libvlc_media_list_t));
158 p_mlist->p_libvlc_instance = p_inst;
159 p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e );
161 /* Code for that one should be handled in flat_media_list.c */
162 p_mlist->p_flat_mlist = NULL;
163 p_mlist->b_read_only = false;
165 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
166 libvlc_MediaListItemAdded, p_e );
167 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
168 libvlc_MediaListWillAddItem, p_e );
169 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
170 libvlc_MediaListItemDeleted, p_e );
171 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
172 libvlc_MediaListWillDeleteItem, p_e );
174 if( libvlc_exception_raised( p_e ) )
176 libvlc_event_manager_release( p_mlist->p_event_manager );
181 vlc_mutex_init( &p_mlist->object_lock );
182 vlc_mutex_init( &p_mlist->refcount_lock ); // FIXME: spinlock?
184 vlc_array_init( &p_mlist->items );
185 p_mlist->i_refcount = 1;
186 p_mlist->p_md = NULL;
191 /**************************************************************************
192 * libvlc_media_list_release (Public)
195 **************************************************************************/
196 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
198 libvlc_media_t * p_md;
201 vlc_mutex_lock( &p_mlist->refcount_lock );
202 p_mlist->i_refcount--;
203 if( p_mlist->i_refcount > 0 )
205 vlc_mutex_unlock( &p_mlist->refcount_lock );
208 vlc_mutex_unlock( &p_mlist->refcount_lock );
210 /* Refcount null, time to free */
212 libvlc_event_manager_release( p_mlist->p_event_manager );
214 libvlc_media_release( p_mlist->p_md );
216 for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
218 p_md = vlc_array_item_at_index( &p_mlist->items, i );
219 libvlc_media_release( p_md );
222 vlc_mutex_destroy( &p_mlist->object_lock );
223 vlc_array_clear( &p_mlist->items );
228 /**************************************************************************
229 * libvlc_media_list_retain (Public)
231 * Increase an object refcount.
232 **************************************************************************/
233 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
235 vlc_mutex_lock( &p_mlist->refcount_lock );
236 p_mlist->i_refcount++;
237 vlc_mutex_unlock( &p_mlist->refcount_lock );
241 /**************************************************************************
242 * add_file_content (Public)
243 **************************************************************************/
245 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
246 const char * psz_uri,
247 libvlc_exception_t * p_e )
249 input_item_t * p_input_item;
250 libvlc_media_t * p_md;
252 p_input_item = input_item_NewExt(
253 p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
254 _("Media Library"), 0, NULL, 0, -1 );
258 libvlc_exception_raise( p_e, "Can't create an input item" );
262 p_md = libvlc_media_new_from_input_item(
263 p_mlist->p_libvlc_instance,
268 vlc_gc_decref( p_input_item );
272 libvlc_media_list_add_media( p_mlist, p_md, p_e );
273 if( libvlc_exception_raised( p_e ) )
276 input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, true );
281 /**************************************************************************
283 **************************************************************************/
284 void libvlc_media_list_set_media( libvlc_media_list_t * p_mlist,
285 libvlc_media_t * p_md,
286 libvlc_exception_t * p_e)
290 vlc_mutex_lock( &p_mlist->object_lock );
291 libvlc_media_release( p_mlist->p_md );
292 libvlc_media_retain( p_md );
293 p_mlist->p_md = p_md;
294 vlc_mutex_unlock( &p_mlist->object_lock );
297 /**************************************************************************
300 * If this media_list comes is a media's subitems,
301 * This holds the corresponding media.
302 * This md is also seen as the information holder for the media_list.
303 * Indeed a media_list can have meta information through this
305 **************************************************************************/
307 libvlc_media_list_media( libvlc_media_list_t * p_mlist,
308 libvlc_exception_t * p_e)
310 libvlc_media_t *p_md;
313 vlc_mutex_lock( &p_mlist->object_lock );
314 p_md = p_mlist->p_md;
316 libvlc_media_retain( p_md );
317 vlc_mutex_unlock( &p_mlist->object_lock );
322 /**************************************************************************
323 * libvlc_media_list_count (Public)
325 * Lock should be hold when entering.
326 **************************************************************************/
327 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
328 libvlc_exception_t * p_e )
331 return vlc_array_count( &p_mlist->items );
334 /**************************************************************************
335 * libvlc_media_list_add_media (Public)
337 * Lock should be held when entering.
338 **************************************************************************/
339 void libvlc_media_list_add_media(
340 libvlc_media_list_t * p_mlist,
341 libvlc_media_t * p_md,
342 libvlc_exception_t * p_e )
344 if( mlist_is_writable(p_mlist,p_e) )
345 _libvlc_media_list_add_media( p_mlist, p_md, p_e );
348 /* LibVLC internal version */
349 void _libvlc_media_list_add_media(
350 libvlc_media_list_t * p_mlist,
351 libvlc_media_t * p_md,
352 libvlc_exception_t * p_e )
355 libvlc_media_retain( p_md );
357 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ),
359 vlc_array_append( &p_mlist->items, p_md );
360 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1,
364 /**************************************************************************
365 * libvlc_media_list_insert_media (Public)
367 * Lock should be hold when entering.
368 **************************************************************************/
369 void libvlc_media_list_insert_media(
370 libvlc_media_list_t * p_mlist,
371 libvlc_media_t * p_md,
373 libvlc_exception_t * p_e )
375 if( mlist_is_writable(p_mlist,p_e) )
376 _libvlc_media_list_insert_media( p_mlist, p_md, index, p_e );
379 /* LibVLC internal version */
380 void _libvlc_media_list_insert_media(
381 libvlc_media_list_t * p_mlist,
382 libvlc_media_t * p_md,
384 libvlc_exception_t * p_e )
387 libvlc_media_retain( p_md );
389 notify_item_addition( p_mlist, p_md, index, EventWillHappen );
390 vlc_array_insert( &p_mlist->items, p_md, index );
391 notify_item_addition( p_mlist, p_md, index, EventDidHappen );
394 /**************************************************************************
395 * libvlc_media_list_remove_index (Public)
397 * Lock should be held when entering.
398 **************************************************************************/
399 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
401 libvlc_exception_t * p_e )
403 if( mlist_is_writable(p_mlist,p_e) )
404 _libvlc_media_list_remove_index( p_mlist, index, p_e );
407 /* LibVLC internal version */
408 void _libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
410 libvlc_exception_t * p_e )
412 libvlc_media_t * p_md;
414 if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
416 libvlc_exception_raise( p_e, "Index out of bounds");
420 p_md = vlc_array_item_at_index( &p_mlist->items, index );
422 notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
423 vlc_array_remove( &p_mlist->items, index );
424 notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
426 libvlc_media_release( p_md );
429 /**************************************************************************
430 * libvlc_media_list_item_at_index (Public)
432 * Lock should be held when entering.
433 **************************************************************************/
435 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
437 libvlc_exception_t * p_e )
439 libvlc_media_t * p_md;
441 if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
443 libvlc_exception_raise( p_e, "Index out of bounds");
447 p_md = vlc_array_item_at_index( &p_mlist->items, index );
448 libvlc_media_retain( p_md );
452 /**************************************************************************
453 * libvlc_media_list_index_of_item (Public)
455 * Lock should be held when entering.
456 * Warning: this function returns the first matching item.
457 **************************************************************************/
458 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
459 libvlc_media_t * p_searched_md,
460 libvlc_exception_t * p_e )
464 libvlc_media_t * p_md;
466 for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
468 p_md = vlc_array_item_at_index( &p_mlist->items, i );
469 if( p_searched_md == p_md )
475 /**************************************************************************
476 * libvlc_media_list_is_readonly (Public)
478 * This indicates if this media list is read-only from a user point of view
479 **************************************************************************/
480 int libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist )
482 return p_mlist->b_read_only;
485 /**************************************************************************
486 * libvlc_media_list_lock (Public)
488 * The lock must be held in access operations. It is never used in the
490 **************************************************************************/
491 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
493 vlc_mutex_lock( &p_mlist->object_lock );
497 /**************************************************************************
498 * libvlc_media_list_unlock (Public)
500 * The lock must be held in access operations
501 **************************************************************************/
502 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
504 vlc_mutex_unlock( &p_mlist->object_lock );
508 /**************************************************************************
509 * libvlc_media_list_p_event_manager (Public)
511 * The p_event_manager is immutable, so you don't have to hold the lock
512 **************************************************************************/
513 libvlc_event_manager_t *
514 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
515 libvlc_exception_t * p_e )
518 return p_mlist->p_event_manager;