X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fcontrol%2Fmedia_list.c;h=08271247a3ceca66c8f9a946777741ae7bcbf5a6;hb=12ade3e3bc975d5426ba4af155b7372c31093b31;hp=6f69b68597843e01b14a29f98ce4b68b4cb5e7b5;hpb=c7e71cb6d0206530a46ecd4ff5f6fa7ad4953658;p=vlc diff --git a/src/control/media_list.c b/src/control/media_list.c index 6f69b68597..08271247a3 100644 --- a/src/control/media_list.c +++ b/src/control/media_list.c @@ -21,16 +21,37 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ -#include "libvlc_internal.h" -#include +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + #include -#include "vlc_arrays.h" + +#include +#include +#include +#include + +#include +#include + +#include "libvlc_internal.h" +#include "media_internal.h" // libvlc_media_new_from_input_item() +#include "media_list_internal.h" typedef enum EventPlaceInTime { EventWillHappen, EventDidHappen } EventPlaceInTime; +//#define DEBUG_MEDIA_LIST + +#ifdef DEBUG_MEDIA_LIST +# define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ ) +#else +# define trace( ... ) +#endif + /* * Private functions */ @@ -44,7 +65,7 @@ typedef enum EventPlaceInTime { **************************************************************************/ static void notify_item_addition( libvlc_media_list_t * p_mlist, - libvlc_media_descriptor_t * p_md, + libvlc_media_t * p_md, int index, EventPlaceInTime event_status ) { @@ -53,6 +74,7 @@ notify_item_addition( libvlc_media_list_t * p_mlist, /* Construct the event */ if( event_status == EventDidHappen ) { + trace("item was added at index %d\n", index); event.type = libvlc_MediaListItemAdded; event.u.media_list_item_added.item = p_md; event.u.media_list_item_added.index = index; @@ -75,7 +97,7 @@ notify_item_addition( libvlc_media_list_t * p_mlist, **************************************************************************/ static void notify_item_deletion( libvlc_media_list_t * p_mlist, - libvlc_media_descriptor_t * p_md, + libvlc_media_t * p_md, int index, EventPlaceInTime event_status ) { @@ -84,6 +106,7 @@ notify_item_deletion( libvlc_media_list_t * p_mlist, /* Construct the event */ if( event_status == EventDidHappen ) { + trace("item at index %d was deleted\n", index); event.type = libvlc_MediaListItemDeleted; event.u.media_list_item_deleted.item = p_md; event.u.media_list_item_deleted.index = index; @@ -99,6 +122,21 @@ notify_item_deletion( libvlc_media_list_t * p_mlist, libvlc_event_send( p_mlist->p_event_manager, &event ); } +/************************************************************************** + * static mlist_is_writable (private) + **************************************************************************/ +static inline +bool mlist_is_writable( libvlc_media_list_t *p_mlist ) +{ + if( !p_mlist||p_mlist->b_read_only ) + { + /* We are read-only from user side */ + libvlc_printerr( "Attempt to write a read-only media list" ); + return false; + } + return true; +} + /* * Public libvlc functions */ @@ -109,42 +147,43 @@ notify_item_deletion( libvlc_media_list_t * p_mlist, * Init an object. **************************************************************************/ libvlc_media_list_t * -libvlc_media_list_new( libvlc_instance_t * p_inst, - libvlc_exception_t * p_e ) +libvlc_media_list_new( libvlc_instance_t * p_inst ) { libvlc_media_list_t * p_mlist; p_mlist = malloc(sizeof(libvlc_media_list_t)); - - if( !p_mlist ) + if( unlikely(p_mlist == NULL) ) + { + libvlc_printerr( "Not enough memory" ); return NULL; - + } + p_mlist->p_libvlc_instance = p_inst; - p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e ); + p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst ); + if( unlikely(p_mlist->p_event_manager == NULL) ) + { + free(p_mlist); + return NULL; + } /* Code for that one should be handled in flat_media_list.c */ p_mlist->p_flat_mlist = NULL; - p_mlist->b_read_only = VLC_FALSE; + p_mlist->b_read_only = false; libvlc_event_manager_register_event_type( p_mlist->p_event_manager, - libvlc_MediaListItemAdded, p_e ); + libvlc_MediaListItemAdded ); libvlc_event_manager_register_event_type( p_mlist->p_event_manager, - libvlc_MediaListWillAddItem, p_e ); + libvlc_MediaListWillAddItem ); libvlc_event_manager_register_event_type( p_mlist->p_event_manager, - libvlc_MediaListItemDeleted, p_e ); + libvlc_MediaListItemDeleted ); libvlc_event_manager_register_event_type( p_mlist->p_event_manager, - libvlc_MediaListWillDeleteItem, p_e ); + libvlc_MediaListWillDeleteItem ); - if( libvlc_exception_raised( p_e ) ) - { - libvlc_event_manager_release( p_mlist->p_event_manager ); - free( p_mlist ); - return NULL; - } + vlc_mutex_init( &p_mlist->object_lock ); + vlc_mutex_init( &p_mlist->refcount_lock ); // FIXME: spinlock? - vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock ); - vlc_array_init( &p_mlist->items ); + assert( p_mlist->items.i_count == 0 ); p_mlist->i_refcount = 1; p_mlist->p_md = NULL; @@ -158,29 +197,28 @@ libvlc_media_list_new( libvlc_instance_t * p_inst, **************************************************************************/ void libvlc_media_list_release( libvlc_media_list_t * p_mlist ) { - libvlc_media_descriptor_t * p_md; + libvlc_media_t * p_md; int i; - vlc_mutex_lock( &p_mlist->object_lock ); + vlc_mutex_lock( &p_mlist->refcount_lock ); p_mlist->i_refcount--; if( p_mlist->i_refcount > 0 ) { - vlc_mutex_unlock( &p_mlist->object_lock ); + vlc_mutex_unlock( &p_mlist->refcount_lock ); return; } - vlc_mutex_unlock( &p_mlist->object_lock ); + vlc_mutex_unlock( &p_mlist->refcount_lock ); /* Refcount null, time to free */ libvlc_event_manager_release( p_mlist->p_event_manager ); - if( p_mlist->p_md ) - libvlc_media_descriptor_release( p_mlist->p_md ); + libvlc_media_release( p_mlist->p_md ); for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ ) { p_md = vlc_array_item_at_index( &p_mlist->items, i ); - libvlc_media_descriptor_release( p_md ); + libvlc_media_release( p_md ); } vlc_mutex_destroy( &p_mlist->object_lock ); @@ -196,88 +234,83 @@ void libvlc_media_list_release( libvlc_media_list_t * p_mlist ) **************************************************************************/ void libvlc_media_list_retain( libvlc_media_list_t * p_mlist ) { - vlc_mutex_lock( &p_mlist->object_lock ); + vlc_mutex_lock( &p_mlist->refcount_lock ); p_mlist->i_refcount++; - vlc_mutex_unlock( &p_mlist->object_lock ); + vlc_mutex_unlock( &p_mlist->refcount_lock ); } /************************************************************************** * add_file_content (Public) **************************************************************************/ -void +int libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist, - const char * psz_uri, - libvlc_exception_t * p_e ) + const char * psz_uri ) { input_item_t * p_input_item; - libvlc_media_descriptor_t * p_md; + libvlc_media_t * p_md; - p_input_item = input_ItemNewExt( p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri, - _("Media Library"), 0, NULL, -1 ); + p_input_item = input_item_NewExt( + p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri, + _("Media Library"), 0, NULL, 0, -1 ); if( !p_input_item ) { - libvlc_exception_raise( p_e, "Can't create an input item" ); - return; + libvlc_printerr( "Not enough memory" ); + return -1; } - p_md = libvlc_media_descriptor_new_from_input_item( - p_mlist->p_libvlc_instance, - p_input_item, p_e ); - + p_md = libvlc_media_new_from_input_item( p_mlist->p_libvlc_instance, + p_input_item ); if( !p_md ) { vlc_gc_decref( p_input_item ); - return; + return -1; } - libvlc_media_list_add_media_descriptor( p_mlist, p_md, p_e ); - if( libvlc_exception_raised( p_e ) ) - return; - - input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, VLC_TRUE ); + if( libvlc_media_list_add_media( p_mlist, p_md ) ) + { +#warning Missing error handling! + /* printerr and leaks */ + return -1; + } - return; + input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item ); + return 0; } /************************************************************************** - * set_media_descriptor (Public) + * set_media (Public) **************************************************************************/ -void libvlc_media_list_set_media_descriptor( libvlc_media_list_t * p_mlist, - libvlc_media_descriptor_t * p_md, - libvlc_exception_t * p_e) +void libvlc_media_list_set_media( libvlc_media_list_t * p_mlist, + libvlc_media_t * p_md ) { - (void)p_e; vlc_mutex_lock( &p_mlist->object_lock ); - if( p_mlist->p_md ) - libvlc_media_descriptor_release( p_mlist->p_md ); - libvlc_media_descriptor_retain( p_md ); + libvlc_media_release( p_mlist->p_md ); + libvlc_media_retain( p_md ); p_mlist->p_md = p_md; vlc_mutex_unlock( &p_mlist->object_lock ); } /************************************************************************** - * media_descriptor (Public) + * media (Public) * - * If this media_list comes is a media_descriptor's subitems, - * This holds the corresponding media_descriptor. + * If this media_list comes is a media's subitems, + * This holds the corresponding media. * This md is also seen as the information holder for the media_list. * Indeed a media_list can have meta information through this - * media_descriptor. + * media. **************************************************************************/ -libvlc_media_descriptor_t * -libvlc_media_list_media_descriptor( libvlc_media_list_t * p_mlist, - libvlc_exception_t * p_e) +libvlc_media_t * +libvlc_media_list_media( libvlc_media_list_t * p_mlist ) { - libvlc_media_descriptor_t *p_md; - (void)p_e; + libvlc_media_t *p_md; vlc_mutex_lock( &p_mlist->object_lock ); p_md = p_mlist->p_md; if( p_md ) - libvlc_media_descriptor_retain( p_md ); + libvlc_media_retain( p_md ); vlc_mutex_unlock( &p_mlist->object_lock ); return p_md; @@ -286,62 +319,62 @@ libvlc_media_list_media_descriptor( libvlc_media_list_t * p_mlist, /************************************************************************** * libvlc_media_list_count (Public) * - * Lock should be hold when entering. + * Lock should be held when entering. **************************************************************************/ -int libvlc_media_list_count( libvlc_media_list_t * p_mlist, - libvlc_exception_t * p_e ) +int libvlc_media_list_count( libvlc_media_list_t * p_mlist ) { - (void)p_e; return vlc_array_count( &p_mlist->items ); } /************************************************************************** - * libvlc_media_list_add_media_descriptor (Public) + * libvlc_media_list_add_media (Public) * - * Lock should be hold when entering. + * Lock should be held when entering. **************************************************************************/ -void libvlc_media_list_add_media_descriptor( - libvlc_media_list_t * p_mlist, - libvlc_media_descriptor_t * p_md, - libvlc_exception_t * p_e ) +int libvlc_media_list_add_media( libvlc_media_list_t * p_mlist, + libvlc_media_t * p_md ) +{ + if( !mlist_is_writable(p_mlist) ) + return -1; + _libvlc_media_list_add_media( p_mlist, p_md ); + return 0; +} + +/* LibVLC internal version */ +void _libvlc_media_list_add_media( libvlc_media_list_t * p_mlist, + libvlc_media_t * p_md ) { - (void)p_e; - libvlc_media_descriptor_retain( p_md ); + libvlc_media_retain( p_md ); - notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ), EventWillHappen ); + notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ), + EventWillHappen ); vlc_array_append( &p_mlist->items, p_md ); - notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1, EventDidHappen ); + notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1, + EventDidHappen ); } /************************************************************************** - * libvlc_media_list_insert_media_descriptor (Public) + * libvlc_media_list_insert_media (Public) * * Lock should be hold when entering. **************************************************************************/ -void libvlc_media_list_insert_media_descriptor( - libvlc_media_list_t * p_mlist, - libvlc_media_descriptor_t * p_md, - int index, - libvlc_exception_t * p_e ) +int libvlc_media_list_insert_media( libvlc_media_list_t * p_mlist, + libvlc_media_t * p_md, + int index ) { - if( p_mlist->b_read_only ) - { - /* We are read only from user side */ - libvlc_exception_raise( p_e, "Trying to write into a read-only media list." ); - return; - } - _libvlc_media_list_insert_media_descriptor( p_mlist, p_md, index, p_e ); + if( !mlist_is_writable(p_mlist) ) + return -1; + _libvlc_media_list_insert_media( p_mlist, p_md, index ); + return 0; } /* LibVLC internal version */ -void _libvlc_media_list_insert_media_descriptor( +void _libvlc_media_list_insert_media( libvlc_media_list_t * p_mlist, - libvlc_media_descriptor_t * p_md, - int index, - libvlc_exception_t * p_e ) + libvlc_media_t * p_md, + int index ) { - (void)p_e; - libvlc_media_descriptor_retain( p_md ); + libvlc_media_retain( p_md ); notify_item_addition( p_mlist, p_md, index, EventWillHappen ); vlc_array_insert( &p_mlist->items, p_md, index ); @@ -351,27 +384,27 @@ void _libvlc_media_list_insert_media_descriptor( /************************************************************************** * libvlc_media_list_remove_index (Public) * - * Lock should be hold when entering. + * Lock should be held when entering. **************************************************************************/ -void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist, - int index, - libvlc_exception_t * p_e ) +int libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist, + int index ) { - if( p_mlist->b_read_only ) - { - /* We are read only from user side */ - libvlc_exception_raise( p_e, "Trying to write into a read-only media list." ); - return; - } - _libvlc_media_list_remove_index( p_mlist, index, p_e ); + if( !mlist_is_writable(p_mlist) ) + return -1; + return _libvlc_media_list_remove_index( p_mlist, index ); } /* LibVLC internal version */ -void _libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist, - int index, - libvlc_exception_t * p_e ) +int _libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist, + int index ) { - libvlc_media_descriptor_t * p_md; + libvlc_media_t * p_md; + + if( index < 0 || index >= vlc_array_count( &p_mlist->items )) + { + libvlc_printerr( "Index out of bounds" ); + return -1; + } p_md = vlc_array_item_at_index( &p_mlist->items, index ); @@ -379,36 +412,42 @@ void _libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist, vlc_array_remove( &p_mlist->items, index ); notify_item_deletion( p_mlist, p_md, index, EventDidHappen ); - libvlc_media_descriptor_release( p_md ); + libvlc_media_release( p_md ); + return 0; } /************************************************************************** * libvlc_media_list_item_at_index (Public) * - * Lock should be hold when entering. + * Lock should be held when entering. **************************************************************************/ -libvlc_media_descriptor_t * +libvlc_media_t * libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist, - int index, - libvlc_exception_t * p_e ) + int index ) { - libvlc_media_descriptor_t * p_md; + libvlc_media_t * p_md; + + if( index < 0 || index >= vlc_array_count( &p_mlist->items )) + { + libvlc_printerr( "Index out of bounds" ); + return NULL; + } + p_md = vlc_array_item_at_index( &p_mlist->items, index ); - libvlc_media_descriptor_retain( p_md ); + libvlc_media_retain( p_md ); return p_md; } /************************************************************************** * libvlc_media_list_index_of_item (Public) * - * Lock should be hold when entering. - * Warning: this function would return the first matching item + * Lock should be held when entering. + * Warning: this function returns the first matching item. **************************************************************************/ int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist, - libvlc_media_descriptor_t * p_searched_md, - libvlc_exception_t * p_e ) + libvlc_media_t * p_searched_md ) { - libvlc_media_descriptor_t * p_md; + libvlc_media_t * p_md; int i; for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ ) { @@ -424,7 +463,7 @@ int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist, * * This indicates if this media list is read-only from a user point of view **************************************************************************/ -vlc_bool_t libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist ) +int libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist ) { return p_mlist->b_read_only; } @@ -458,9 +497,7 @@ void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist ) * The p_event_manager is immutable, so you don't have to hold the lock **************************************************************************/ libvlc_event_manager_t * -libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist, - libvlc_exception_t * p_e ) +libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist ) { - (void)p_e; return p_mlist->p_event_manager; }