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 *****************************************************************************/
30 #include <vlc/libvlc.h>
31 #include <vlc/libvlc_media.h>
32 #include <vlc/libvlc_media_list.h>
33 #include <vlc/libvlc_events.h>
35 #include <vlc_common.h>
36 #include <vlc_input.h>
38 #include "libvlc_internal.h"
39 #include "media_internal.h" // libvlc_media_new_from_input_item()
40 #include "media_list_internal.h"
42 typedef enum EventPlaceInTime {
47 //#define DEBUG_MEDIA_LIST
49 #ifdef DEBUG_MEDIA_LIST
50 # define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
61 /**************************************************************************
62 * notify_item_addition (private)
64 * Do the appropriate action when an item is deleted.
65 **************************************************************************/
67 notify_item_addition( libvlc_media_list_t * p_mlist,
68 libvlc_media_t * p_md,
70 EventPlaceInTime event_status )
74 /* Construct the event */
75 if( event_status == EventDidHappen )
77 trace("item was added at index %d\n", index);
78 event.type = libvlc_MediaListItemAdded;
79 event.u.media_list_item_added.item = p_md;
80 event.u.media_list_item_added.index = index;
82 else /* if( event_status == EventWillHappen ) */
84 event.type = libvlc_MediaListWillAddItem;
85 event.u.media_list_will_add_item.item = p_md;
86 event.u.media_list_will_add_item.index = index;
90 libvlc_event_send( p_mlist->p_event_manager, &event );
93 /**************************************************************************
94 * notify_item_deletion (private)
96 * Do the appropriate action when an item is added.
97 **************************************************************************/
99 notify_item_deletion( libvlc_media_list_t * p_mlist,
100 libvlc_media_t * p_md,
102 EventPlaceInTime event_status )
104 libvlc_event_t event;
106 /* Construct the event */
107 if( event_status == EventDidHappen )
109 trace("item at index %d was deleted\n", index);
110 event.type = libvlc_MediaListItemDeleted;
111 event.u.media_list_item_deleted.item = p_md;
112 event.u.media_list_item_deleted.index = index;
114 else /* if( event_status == EventWillHappen ) */
116 event.type = libvlc_MediaListWillDeleteItem;
117 event.u.media_list_will_delete_item.item = p_md;
118 event.u.media_list_will_delete_item.index = index;
122 libvlc_event_send( p_mlist->p_event_manager, &event );
125 /**************************************************************************
126 * static mlist_is_writable (private)
128 * Raise exception and return 0 when the media_list instance is read-only,
130 **************************************************************************/
132 int mlist_is_writable( libvlc_media_list_t *p_mlist, libvlc_exception_t *p_e )
134 if( !p_mlist||p_mlist->b_read_only )
136 /* We are read-only from user side */
137 libvlc_exception_raise( p_e );
138 libvlc_printerr( "Attempt to write a read-only media list" );
145 * Public libvlc functions
148 /**************************************************************************
149 * libvlc_media_list_new (Public)
152 **************************************************************************/
153 libvlc_media_list_t *
154 libvlc_media_list_new( libvlc_instance_t * p_inst,
155 libvlc_exception_t * p_e )
157 libvlc_media_list_t * p_mlist;
159 p_mlist = malloc(sizeof(libvlc_media_list_t));
160 if( unlikely(p_mlist == NULL) )
162 libvlc_printerr( "Not enough memory" );
166 p_mlist->p_libvlc_instance = p_inst;
167 p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst );
168 if( unlikely(p_mlist->p_event_manager == NULL) )
174 /* Code for that one should be handled in flat_media_list.c */
175 p_mlist->p_flat_mlist = NULL;
176 p_mlist->b_read_only = false;
178 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
179 libvlc_MediaListItemAdded );
180 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
181 libvlc_MediaListWillAddItem );
182 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
183 libvlc_MediaListItemDeleted );
184 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
185 libvlc_MediaListWillDeleteItem );
187 vlc_mutex_init( &p_mlist->object_lock );
188 vlc_mutex_init( &p_mlist->refcount_lock ); // FIXME: spinlock?
190 vlc_array_init( &p_mlist->items );
191 p_mlist->i_refcount = 1;
192 p_mlist->p_md = NULL;
197 /**************************************************************************
198 * libvlc_media_list_release (Public)
201 **************************************************************************/
202 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
204 libvlc_media_t * p_md;
207 vlc_mutex_lock( &p_mlist->refcount_lock );
208 p_mlist->i_refcount--;
209 if( p_mlist->i_refcount > 0 )
211 vlc_mutex_unlock( &p_mlist->refcount_lock );
214 vlc_mutex_unlock( &p_mlist->refcount_lock );
216 /* Refcount null, time to free */
218 libvlc_event_manager_release( p_mlist->p_event_manager );
220 libvlc_media_release( p_mlist->p_md );
222 for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
224 p_md = vlc_array_item_at_index( &p_mlist->items, i );
225 libvlc_media_release( p_md );
228 vlc_mutex_destroy( &p_mlist->object_lock );
229 vlc_array_clear( &p_mlist->items );
234 /**************************************************************************
235 * libvlc_media_list_retain (Public)
237 * Increase an object refcount.
238 **************************************************************************/
239 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
241 vlc_mutex_lock( &p_mlist->refcount_lock );
242 p_mlist->i_refcount++;
243 vlc_mutex_unlock( &p_mlist->refcount_lock );
247 /**************************************************************************
248 * add_file_content (Public)
249 **************************************************************************/
251 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
252 const char * psz_uri,
253 libvlc_exception_t * p_e )
255 input_item_t * p_input_item;
256 libvlc_media_t * p_md;
258 p_input_item = input_item_NewExt(
259 p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
260 _("Media Library"), 0, NULL, 0, -1 );
264 libvlc_exception_raise( p_e );
265 libvlc_printerr( "Not enough memory" );
269 p_md = libvlc_media_new_from_input_item(
270 p_mlist->p_libvlc_instance,
275 vlc_gc_decref( p_input_item );
279 libvlc_media_list_add_media( p_mlist, p_md, p_e );
280 if( libvlc_exception_raised( p_e ) )
283 input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item );
288 /**************************************************************************
290 **************************************************************************/
291 void libvlc_media_list_set_media( libvlc_media_list_t * p_mlist,
292 libvlc_media_t * p_md )
295 vlc_mutex_lock( &p_mlist->object_lock );
296 libvlc_media_release( p_mlist->p_md );
297 libvlc_media_retain( p_md );
298 p_mlist->p_md = p_md;
299 vlc_mutex_unlock( &p_mlist->object_lock );
302 /**************************************************************************
305 * If this media_list comes is a media's subitems,
306 * This holds the corresponding media.
307 * This md is also seen as the information holder for the media_list.
308 * Indeed a media_list can have meta information through this
310 **************************************************************************/
312 libvlc_media_list_media( libvlc_media_list_t * p_mlist )
314 libvlc_media_t *p_md;
316 vlc_mutex_lock( &p_mlist->object_lock );
317 p_md = p_mlist->p_md;
319 libvlc_media_retain( p_md );
320 vlc_mutex_unlock( &p_mlist->object_lock );
325 /**************************************************************************
326 * libvlc_media_list_count (Public)
328 * Lock should be held when entering.
329 **************************************************************************/
330 int libvlc_media_list_count( libvlc_media_list_t * p_mlist )
332 return vlc_array_count( &p_mlist->items );
335 /**************************************************************************
336 * libvlc_media_list_add_media (Public)
338 * Lock should be held when entering.
339 **************************************************************************/
340 void libvlc_media_list_add_media(
341 libvlc_media_list_t * p_mlist,
342 libvlc_media_t * p_md,
343 libvlc_exception_t * p_e )
345 if( mlist_is_writable(p_mlist,p_e) )
346 _libvlc_media_list_add_media( p_mlist, p_md );
349 /* LibVLC internal version */
350 void _libvlc_media_list_add_media( libvlc_media_list_t * p_mlist,
351 libvlc_media_t * p_md )
353 libvlc_media_retain( p_md );
355 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ),
357 vlc_array_append( &p_mlist->items, p_md );
358 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1,
362 /**************************************************************************
363 * libvlc_media_list_insert_media (Public)
365 * Lock should be hold when entering.
366 **************************************************************************/
367 void libvlc_media_list_insert_media(
368 libvlc_media_list_t * p_mlist,
369 libvlc_media_t * p_md,
371 libvlc_exception_t * p_e )
373 if( mlist_is_writable(p_mlist,p_e) )
374 _libvlc_media_list_insert_media( p_mlist, p_md, index );
377 /* LibVLC internal version */
378 void _libvlc_media_list_insert_media(
379 libvlc_media_list_t * p_mlist,
380 libvlc_media_t * p_md,
383 libvlc_media_retain( p_md );
385 notify_item_addition( p_mlist, p_md, index, EventWillHappen );
386 vlc_array_insert( &p_mlist->items, p_md, index );
387 notify_item_addition( p_mlist, p_md, index, EventDidHappen );
390 /**************************************************************************
391 * libvlc_media_list_remove_index (Public)
393 * Lock should be held when entering.
394 **************************************************************************/
395 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
397 libvlc_exception_t * p_e )
399 if( mlist_is_writable(p_mlist,p_e) )
400 _libvlc_media_list_remove_index( p_mlist, index, p_e );
403 /* LibVLC internal version */
404 void _libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
406 libvlc_exception_t * p_e )
408 libvlc_media_t * p_md;
410 if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
412 libvlc_exception_raise( p_e );
413 libvlc_printerr( "Index out of bounds" );
417 p_md = vlc_array_item_at_index( &p_mlist->items, index );
419 notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
420 vlc_array_remove( &p_mlist->items, index );
421 notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
423 libvlc_media_release( p_md );
426 /**************************************************************************
427 * libvlc_media_list_item_at_index (Public)
429 * Lock should be held when entering.
430 **************************************************************************/
432 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
434 libvlc_exception_t * p_e )
436 libvlc_media_t * p_md;
438 if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
440 libvlc_exception_raise( p_e );
441 libvlc_printerr( "Index out of bounds" );
445 p_md = vlc_array_item_at_index( &p_mlist->items, index );
446 libvlc_media_retain( p_md );
450 /**************************************************************************
451 * libvlc_media_list_index_of_item (Public)
453 * Lock should be held when entering.
454 * Warning: this function returns the first matching item.
455 **************************************************************************/
456 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
457 libvlc_media_t * p_searched_md )
459 libvlc_media_t * p_md;
461 for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
463 p_md = vlc_array_item_at_index( &p_mlist->items, i );
464 if( p_searched_md == p_md )
470 /**************************************************************************
471 * libvlc_media_list_is_readonly (Public)
473 * This indicates if this media list is read-only from a user point of view
474 **************************************************************************/
475 int libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist )
477 return p_mlist->b_read_only;
480 /**************************************************************************
481 * libvlc_media_list_lock (Public)
483 * The lock must be held in access operations. It is never used in the
485 **************************************************************************/
486 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
488 vlc_mutex_lock( &p_mlist->object_lock );
492 /**************************************************************************
493 * libvlc_media_list_unlock (Public)
495 * The lock must be held in access operations
496 **************************************************************************/
497 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
499 vlc_mutex_unlock( &p_mlist->object_lock );
503 /**************************************************************************
504 * libvlc_media_list_p_event_manager (Public)
506 * The p_event_manager is immutable, so you don't have to hold the lock
507 **************************************************************************/
508 libvlc_event_manager_t *
509 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist )
511 return p_mlist->p_event_manager;