X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fcontrol%2Fmedia_list.c;h=5fa32527b79bd15478387ed3d05b741fb7241a86;hb=658686029ee756fbf4beb8e0cdcc4b0bc5dd18ed;hp=3ae87239100a8c185d263b3e703635224ce4b35e;hpb=df61d33b06e2b3cbbe746b2f5a9bea5b370c24ff;p=vlc diff --git a/src/control/media_list.c b/src/control/media_list.c index 3ae8723910..5fa32527b7 100644 --- a/src/control/media_list.c +++ b/src/control/media_list.c @@ -21,10 +21,19 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ -#include "libvlc_internal.h" -#include #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, @@ -109,6 +118,24 @@ notify_item_deletion( libvlc_media_list_t * p_mlist, libvlc_event_send( p_mlist->p_event_manager, &event ); } +/************************************************************************** + * static mlist_is_writable (private) + * + * Raise exception and return 0 when the media_list instance is read-only, + * or else return 1. + **************************************************************************/ +static inline +int mlist_is_writable( libvlc_media_list_t *p_mlist, libvlc_exception_t *p_e ) +{ + if( !p_mlist||p_mlist->b_read_only ) + { + /* We are read-only from user side */ + libvlc_exception_raise( p_e, "Cannot write to read-only media list." ); + return 0; + } + return 1; +} + /* * Public libvlc functions */ @@ -125,10 +152,9 @@ 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 ) return NULL; - + p_mlist->p_libvlc_instance = p_inst; p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e ); @@ -152,8 +178,9 @@ libvlc_media_list_new( libvlc_instance_t * p_inst, return NULL; } - vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock ); - + vlc_mutex_init( &p_mlist->object_lock ); + vlc_mutex_init( &p_mlist->refcount_lock ); // FIXME: spinlock? + vlc_array_init( &p_mlist->items ); p_mlist->i_refcount = 1; p_mlist->p_md = NULL; @@ -171,21 +198,20 @@ void libvlc_media_list_release( libvlc_media_list_t * p_mlist ) 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_release( p_mlist->p_md ); + libvlc_media_release( p_mlist->p_md ); for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ ) { @@ -206,9 +232,9 @@ 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 ); } @@ -223,8 +249,9 @@ libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist, input_item_t * p_input_item; 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 ) { @@ -259,10 +286,9 @@ void libvlc_media_list_set_media( libvlc_media_list_t * p_mlist, libvlc_exception_t * p_e) { - (void)p_e; + VLC_UNUSED(p_e); vlc_mutex_lock( &p_mlist->object_lock ); - if( p_mlist->p_md ) - libvlc_media_release( p_mlist->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 ); @@ -282,7 +308,7 @@ libvlc_media_list_media( libvlc_media_list_t * p_mlist, libvlc_exception_t * p_e) { libvlc_media_t *p_md; - (void)p_e; + VLC_UNUSED(p_e); vlc_mutex_lock( &p_mlist->object_lock ); p_md = p_mlist->p_md; @@ -301,28 +327,22 @@ libvlc_media_list_media( libvlc_media_list_t * p_mlist, int libvlc_media_list_count( libvlc_media_list_t * p_mlist, libvlc_exception_t * p_e ) { - (void)p_e; + VLC_UNUSED(p_e); return vlc_array_count( &p_mlist->items ); } /************************************************************************** * libvlc_media_list_add_media (Public) * - * Lock should be hold when entering. + * Lock should be held when entering. **************************************************************************/ void libvlc_media_list_add_media( libvlc_media_list_t * p_mlist, libvlc_media_t * p_md, libvlc_exception_t * p_e ) { - 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_add_media( p_mlist, p_md, p_e ); + if( mlist_is_writable(p_mlist,p_e) ) + _libvlc_media_list_add_media( p_mlist, p_md, p_e ); } /* LibVLC internal version */ @@ -331,12 +351,14 @@ void _libvlc_media_list_add_media( libvlc_media_t * p_md, libvlc_exception_t * p_e ) { - (void)p_e; + VLC_UNUSED(p_e); 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 ); } /************************************************************************** @@ -350,13 +372,8 @@ void libvlc_media_list_insert_media( int index, libvlc_exception_t * p_e ) { - 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( p_mlist, p_md, index, p_e ); + if( mlist_is_writable(p_mlist,p_e) ) + _libvlc_media_list_insert_media( p_mlist, p_md, index, p_e ); } /* LibVLC internal version */ @@ -366,7 +383,7 @@ void _libvlc_media_list_insert_media( int index, libvlc_exception_t * p_e ) { - (void)p_e; + VLC_UNUSED(p_e); libvlc_media_retain( p_md ); notify_item_addition( p_mlist, p_md, index, EventWillHappen ); @@ -377,19 +394,14 @@ void _libvlc_media_list_insert_media( /************************************************************************** * 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 ) { - 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,p_e) ) + _libvlc_media_list_remove_index( p_mlist, index, p_e ); } /* LibVLC internal version */ @@ -397,15 +409,14 @@ void _libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist, int index, libvlc_exception_t * p_e ) { - libvlc_media_t * p_md; if( index < 0 || index >= vlc_array_count( &p_mlist->items )) { - libvlc_exception_raise( p_e, "Index out of bounds exception"); + libvlc_exception_raise( p_e, "Index out of bounds"); return; } - + p_md = vlc_array_item_at_index( &p_mlist->items, index ); notify_item_deletion( p_mlist, p_md, index, EventWillHappen ); @@ -418,22 +429,21 @@ void _libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist, /************************************************************************** * libvlc_media_list_item_at_index (Public) * - * Lock should be hold when entering. + * Lock should be held when entering. **************************************************************************/ libvlc_media_t * libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist, int index, libvlc_exception_t * p_e ) { - VLC_UNUSED(p_e); + libvlc_media_t * p_md; if( index < 0 || index >= vlc_array_count( &p_mlist->items )) { - libvlc_exception_raise( p_e, "Index out of bounds exception"); + libvlc_exception_raise( p_e, "Index out of bounds"); return NULL; } - libvlc_media_t * p_md; p_md = vlc_array_item_at_index( &p_mlist->items, index ); libvlc_media_retain( p_md ); return p_md; @@ -442,8 +452,8 @@ libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist, /************************************************************************** * 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_t * p_searched_md, @@ -504,6 +514,6 @@ libvlc_event_manager_t * libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist, libvlc_exception_t * p_e ) { - (void)p_e; + VLC_UNUSED(p_e); return p_mlist->p_event_manager; }