X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fplaylist%2Fservices_discovery.c;h=2f384e679add18ac2eeae47aa8a795025e636b62;hb=3df4c74ca5f142255454a6cbdee037ef91c8a357;hp=51c796523192687eaa4a1c82a716f13075d9f012;hpb=66f38edb847aa8868a766475b3c1967356c09773;p=vlc diff --git a/src/playlist/services_discovery.c b/src/playlist/services_discovery.c index 51c7965231..2f384e679a 100644 --- a/src/playlist/services_discovery.c +++ b/src/playlist/services_discovery.c @@ -20,12 +20,81 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ -#include +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif +#include + +#include #include "vlc_playlist.h" #include "vlc_events.h" +#include +#include +#include #include "playlist_internal.h" +#include "../libvlc.h" + +typedef struct +{ + char *name; + char *longname; + int category; +} vlc_sd_probe_t; + +int vlc_sd_probe_Add (vlc_probe_t *probe, const char *name, + const char *longname, int category) +{ + vlc_sd_probe_t names = { strdup(name), strdup(longname), category }; + + if (unlikely (names.name == NULL || names.longname == NULL + || vlc_probe_add (probe, &names, sizeof (names)))) + { + free (names.name); + free (names.longname); + return VLC_ENOMEM; + } + return VLC_PROBE_CONTINUE; +} -static void RunSD( services_discovery_t *p_sd ); +#undef vlc_sd_GetNames + +/** + * Gets the list of available services discovery plugins. + */ +char **vlc_sd_GetNames (vlc_object_t *obj, char ***pppsz_longnames, int **pp_categories) +{ + size_t count; + vlc_sd_probe_t *tab = vlc_probe (obj, "services probe", &count); + + if (count == 0) + { + free (tab); + return NULL; + } + + char **names = malloc (sizeof(char *) * (count + 1)); + char **longnames = malloc (sizeof(char *) * (count + 1)); + int *categories = malloc(sizeof(int) * (count + 1)); + + if (unlikely (names == NULL || longnames == NULL || categories == NULL)) + abort(); + for( size_t i = 0; i < count; i++ ) + { + names[i] = tab[i].name; + longnames[i] = tab[i].longname; + categories[i] = tab[i].category; + } + free (tab); + names[count] = longnames[count] = NULL; + categories[count] = 0; + *pppsz_longnames = longnames; + if( pp_categories ) *pp_categories = categories; + else free( categories ); + return names; +} + + +static void services_discovery_Destructor ( vlc_object_t *p_obj ); /* * Services discovery @@ -33,71 +102,86 @@ static void RunSD( services_discovery_t *p_sd ); * sd's event manager. * That's how the playlist get's Service Discovery information */ - + /*********************************************************************** * Create ***********************************************************************/ -services_discovery_t * -services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name ) +services_discovery_t *vlc_sd_Create( vlc_object_t *p_super, + const char *cfg ) { - services_discovery_t *p_sd = vlc_object_create( p_super, VLC_OBJECT_SD ); + services_discovery_t *p_sd; + + p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC, + "services discovery" ); if( !p_sd ) return NULL; - - p_sd->pf_run = NULL; - p_sd->psz_localized_name = NULL; - - vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd ); - vlc_event_manager_register_event_type( &p_sd->event_manager, - vlc_ServicesDiscoveryItemAdded ); - vlc_event_manager_register_event_type( &p_sd->event_manager, - vlc_ServicesDiscoveryItemRemoved ); - - p_sd->p_module = module_Need( p_sd, "services_discovery", psz_module_name, 0 ); - + free(config_ChainCreate( &p_sd->psz_name, &p_sd->p_cfg, cfg )); + + vlc_event_manager_t *em = &p_sd->event_manager; + vlc_event_manager_init( em, p_sd, (vlc_object_t *)p_sd ); + vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryItemAdded); + vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryItemRemoved); + vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryStarted); + vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryEnded); + + vlc_object_set_destructor( p_sd, services_discovery_Destructor ); + vlc_object_attach( p_sd, p_super ); + + return p_sd; +} + +/*********************************************************************** + * Stop + ***********************************************************************/ +bool vlc_sd_Start ( services_discovery_t * p_sd ) +{ + assert(!p_sd->p_module); + + p_sd->p_module = module_need( p_sd, "services_discovery", + p_sd->psz_name, true ); if( p_sd->p_module == NULL ) { - msg_Err( p_super, "no suitable services discovery module" ); - vlc_object_destroy( p_sd ); - return NULL; + msg_Err( p_sd, "no suitable services discovery module" ); + return false; } - p_sd->psz_module = strdup( psz_module_name ); - p_sd->b_die = VLC_FALSE; /* FIXME */ - return p_sd; + vlc_event_t event = { + .type = vlc_ServicesDiscoveryStarted + }; + vlc_event_send( &p_sd->event_manager, &event ); + return true; } /*********************************************************************** - * Destroy + * Stop ***********************************************************************/ -void services_discovery_Destroy ( services_discovery_t * p_sd ) +void vlc_sd_Stop ( services_discovery_t * p_sd ) { - vlc_object_kill( p_sd ); - if( p_sd->pf_run ) vlc_thread_join( p_sd ); + vlc_event_t event = { + .type = vlc_ServicesDiscoveryEnded + }; - free( p_sd->psz_module ); - module_Unneed( p_sd, p_sd->p_module ); + vlc_event_send( &p_sd->event_manager, &event ); - vlc_event_manager_fini( &p_sd->event_manager ); - free( p_sd->psz_localized_name ); + module_unneed( p_sd, p_sd->p_module ); + p_sd->p_module = NULL; +} - vlc_object_destroy( p_sd ); +void vlc_sd_Destroy( services_discovery_t *p_sd ) +{ + config_ChainDestroy( p_sd->p_cfg ); + free( p_sd->psz_name ); + vlc_object_release( p_sd ); } /*********************************************************************** - * Start + * Destructor ***********************************************************************/ -int services_discovery_Start ( services_discovery_t * p_sd ) +static void services_discovery_Destructor ( vlc_object_t *p_obj ) { - if ((p_sd->pf_run != NULL) - && vlc_thread_create( p_sd, "services_discovery", RunSD, - VLC_THREAD_PRIORITY_LOW, VLC_FALSE)) - { - msg_Err( p_sd, "cannot create services discovery thread" ); - vlc_object_destroy( p_sd ); - return VLC_EGENERIC; - } - return VLC_SUCCESS; + services_discovery_t * p_sd = (services_discovery_t *)p_obj; + assert(!p_sd->p_module); /* Forgot to call Stop */ + vlc_event_manager_fini( &p_sd->event_manager ); } /*********************************************************************** @@ -106,17 +190,7 @@ int services_discovery_Start ( services_discovery_t * p_sd ) char * services_discovery_GetLocalizedName ( services_discovery_t * p_sd ) { - return p_sd->psz_localized_name ? strdup( p_sd->psz_localized_name ) : NULL; -} - -/*********************************************************************** - * SetLocalizedName - ***********************************************************************/ -void -services_discovery_SetLocalizedName ( services_discovery_t * p_sd, const char *psz ) -{ - if(p_sd->psz_localized_name) free( p_sd->psz_localized_name ); - p_sd->psz_localized_name = strdup(psz); + return strdup( module_get_name( p_sd->p_module, true ) ); } /*********************************************************************** @@ -156,178 +230,221 @@ services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_it vlc_event_send( &p_sd->event_manager, &event ); } -/*********************************************************************** - * RunSD (Private) - ***********************************************************************/ -static void RunSD( services_discovery_t *p_sd ) -{ - p_sd->pf_run( p_sd ); - return; -} - /* * Playlist - Services discovery bridge */ - + +struct vlc_sd_internal_t +{ + /* the playlist items for category and onelevel */ + playlist_item_t *p_node; + services_discovery_t *p_sd; /**< Loaded service discovery modules */ + char *psz_name; +}; + /* A new item has been added to a certain sd */ static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data ) { input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item; const char * psz_cat = p_event->u.services_discovery_item_added.psz_category; - playlist_item_t *p_new_item, * p_parent = user_data; + playlist_item_t * p_parent = user_data; + playlist_t * p_playlist = p_parent->p_playlist; - msg_Dbg( p_parent->p_playlist, "Adding %s in %s", p_input->psz_name, psz_cat ); + msg_Dbg( p_playlist, "Adding %s in %s", + p_input->psz_name ? p_input->psz_name : "(null)", + psz_cat ? psz_cat : "(null)" ); - /* If p_child is in root category (this is clearly a hack) and we have a cat*/ - if( !EMPTY_STR(psz_cat) && - p_parent == playlist_ItemToNode( p_parent->p_playlist, p_parent, VLC_FALSE ) ) + PL_LOCK; + /* If p_parent is in root category (this is clearly a hack) and we have a cat */ + if( !EMPTY_STR(psz_cat) ) { /* */ playlist_item_t * p_cat; p_cat = playlist_ChildSearchName( p_parent, psz_cat ); if( !p_cat ) { - p_cat = playlist_NodeCreate( p_parent->p_playlist, psz_cat, - p_parent, 0 ); + p_cat = playlist_NodeCreate( p_playlist, psz_cat, + p_parent, PLAYLIST_END, 0, NULL ); p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG; } p_parent = p_cat; } - - p_new_item = playlist_NodeAddInput( p_parent->p_playlist, p_input, p_parent, - PLAYLIST_APPEND, PLAYLIST_END, VLC_FALSE ); - p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG; - p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG; + + playlist_NodeAddInput( p_playlist, p_input, p_parent, + PLAYLIST_APPEND, PLAYLIST_END, + pl_Locked ); + PL_UNLOCK; } /* A new item has been removed from a certain sd */ static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data ) { - playlist_item_t * p_child = user_data; + input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item; + playlist_item_t * p_sd_node = user_data; + playlist_t *p_playlist = p_sd_node->p_playlist; - msg_Err( p_child->p_playlist, "Service Discovery item deletion not handled" ); + PL_LOCK; + playlist_item_t *p_item = + playlist_ItemFindFromInputAndRoot( p_playlist, p_input, + p_sd_node, false ); + if( !p_item ) + { + PL_UNLOCK; return; + } + playlist_item_t *p_parent = p_item->p_parent; + /* if the item was added under a category and the category node + becomes empty, delete that node as well */ + if( p_parent->i_children > 1 || p_parent == p_sd_node ) + playlist_DeleteItem( p_playlist, p_item, true ); + else + playlist_NodeDelete( p_playlist, p_parent, true, true ); + PL_UNLOCK; } -int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_modules ) +int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, + const char *psz_name ) { - const char *psz_parser = psz_modules ?: ""; - int retval = VLC_SUCCESS; - - for (;;) - { - playlist_item_t * p_cat, * p_one; - while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' ) - psz_parser++; + /* Perform the addition */ + services_discovery_t *p_sd; - if( *psz_parser == '\0' ) - break; - - const char *psz_next = strchr( psz_parser, ':' ); - if( psz_next == NULL ) - psz_next = psz_parser + strlen( psz_parser ); - - char psz_plugin[psz_next - psz_parser + 1]; - memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1); - psz_plugin[sizeof (psz_plugin) - 1] = '\0'; - psz_parser = psz_next; + msg_Dbg( p_playlist, "adding services_discovery %s...", psz_name ); + p_sd = vlc_sd_Create( VLC_OBJECT(p_playlist), psz_name ); + if( !p_sd ) + return VLC_ENOMEM; - /* Perform the addition */ - msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin ); - services_discovery_t *p_sd; + /* Free in playlist_ServicesDiscoveryRemove */ + vlc_sd_internal_t * p_sds = malloc( sizeof(*p_sds) ); + if( !p_sds ) + { + vlc_sd_Destroy( p_sd ); + return VLC_ENOMEM; + } - p_sd = services_discovery_Create( (vlc_object_t*)p_playlist, psz_plugin ); - if( !p_sd ) - continue; + playlist_item_t *p_node; - char * psz = services_discovery_GetLocalizedName( p_sd ); - if( psz ) - { - playlist_NodesPairCreate( p_playlist, psz, - &p_cat, &p_one, VLC_FALSE ); - free( psz ); - } - else + /* Look for configuration chain "longname" */ + const char *psz_longname = "?"; + if( p_sd->p_cfg ) + { + config_chain_t *cfg = p_sd->p_cfg; + while( cfg ) { - /* No name, just add at the top of the playlist */ - p_cat = p_playlist->p_root_category; - p_one = p_playlist->p_root_onelevel; + if( cfg->psz_name && !strcmp( cfg->psz_name, "longname" ) ) + { + psz_longname = cfg->psz_value; + break; + } + cfg = cfg->p_next; } + } - vlc_event_attach( services_discovery_EventManager( p_sd ), - vlc_ServicesDiscoveryItemAdded, - playlist_sd_item_added, - p_one ); + PL_LOCK; + p_node = playlist_NodeCreate( p_playlist, psz_longname, + p_playlist->p_root, PLAYLIST_END, 0, NULL ); + PL_UNLOCK; - vlc_event_attach( services_discovery_EventManager( p_sd ), - vlc_ServicesDiscoveryItemAdded, - playlist_sd_item_added, - p_cat ); + vlc_event_manager_t *em = services_discovery_EventManager( p_sd ); + vlc_event_attach( em, vlc_ServicesDiscoveryItemAdded, + playlist_sd_item_added, p_node ); - vlc_event_attach( services_discovery_EventManager( p_sd ), - vlc_ServicesDiscoveryItemRemoved, - playlist_sd_item_removed, - p_one ); + vlc_event_attach( em, vlc_ServicesDiscoveryItemRemoved, + playlist_sd_item_removed, p_node ); - vlc_event_attach( services_discovery_EventManager( p_sd ), - vlc_ServicesDiscoveryItemRemoved, - playlist_sd_item_removed, - p_cat ); + if( !vlc_sd_Start( p_sd ) ) + { + vlc_sd_Destroy( p_sd ); + free( p_sds ); + return VLC_EGENERIC; + } - services_discovery_Start( p_sd ); + p_sds->p_sd = p_sd; + p_sds->p_node = p_node; + p_sds->psz_name = strdup( psz_name ); - PL_LOCK; - TAB_APPEND( p_playlist->i_sds, p_playlist->pp_sds, p_sd ); - PL_UNLOCK; - } + PL_LOCK; + TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds ); + PL_UNLOCK; - return retval; + return VLC_SUCCESS; } int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist, - const char *psz_module ) + const char *psz_name ) { - int i; - services_discovery_t *p_sd = NULL; + playlist_private_t *priv = pl_priv( p_playlist ); + vlc_sd_internal_t * p_sds = NULL; PL_LOCK; - for( i = 0 ; i< p_playlist->i_sds ; i ++ ) + for( int i = 0; i < priv->i_sds; i++ ) { - if( !strcmp( psz_module, p_playlist->pp_sds[i]->psz_module ) ) + if( !strcmp( psz_name, priv->pp_sds[i]->psz_name ) ) { - p_sd = p_playlist->pp_sds[i]; - REMOVE_ELEM( p_playlist->pp_sds, p_playlist->i_sds, i ); + p_sds = priv->pp_sds[i]; + REMOVE_ELEM( priv->pp_sds, priv->i_sds, i ); break; } } PL_UNLOCK; - if( p_sd ) - { - services_discovery_Destroy( p_sd ); - } - else + if( !p_sds ) { - msg_Warn( p_playlist, "module %s is not loaded", psz_module ); + msg_Warn( p_playlist, "discovery %s is not loaded", psz_name ); return VLC_EGENERIC; } + + services_discovery_t *p_sd = p_sds->p_sd; + assert( p_sd ); + + vlc_sd_Stop( p_sd ); + + vlc_event_detach( services_discovery_EventManager( p_sd ), + vlc_ServicesDiscoveryItemAdded, + playlist_sd_item_added, + p_sds->p_node ); + + vlc_event_detach( services_discovery_EventManager( p_sd ), + vlc_ServicesDiscoveryItemRemoved, + playlist_sd_item_removed, + p_sds->p_node ); + + /* Remove the sd playlist node if it exists */ + PL_LOCK; + playlist_NodeDelete( p_playlist, p_sds->p_node, true, false ); + PL_UNLOCK; + + vlc_sd_Destroy( p_sd ); + free( p_sds->psz_name ); + free( p_sds ); + return VLC_SUCCESS; } -vlc_bool_t playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist, - const char *psz_module ) +bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist, + const char *psz_name ) { - int i; + playlist_private_t *priv = pl_priv( p_playlist ); + bool found = false; PL_LOCK; - for( i = 0 ; i< p_playlist->i_sds ; i ++ ) + for( int i = 0; i < priv->i_sds; i++ ) { - if( !strcmp( psz_module, p_playlist->pp_sds[i]->psz_module ) ) + vlc_sd_internal_t *sd = priv->pp_sds[i]; + + if( sd->psz_name && !strcmp( psz_name, sd->psz_name ) ) { - PL_UNLOCK; - return VLC_TRUE; + found = true; + break; } } PL_UNLOCK; - return VLC_FALSE; + return found; } +void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist ) +{ + playlist_private_t *priv = pl_priv( p_playlist ); + + while( priv->i_sds > 0 ) + playlist_ServicesDiscoveryRemove( p_playlist, + priv->pp_sds[0]->psz_name ); +}