]> git.sesse.net Git - vlc/blobdiff - src/playlist/services_discovery.c
De-uglify root SD item name
[vlc] / src / playlist / services_discovery.c
index 76ce11dc5588f04348ad503b4131bcf23338289a..9d53971d607109714a7395cd43b973acfee08737 100644 (file)
 #include <vlc_common.h>
 #include "vlc_playlist.h"
 #include "vlc_events.h"
+#include <vlc_services_discovery.h>
+#include <vlc_probe.h>
 #include "playlist_internal.h"
 #include "../libvlc.h"
 
-static void RunSD( services_discovery_t *p_sd );
+typedef struct
+{
+    char *name;
+    char *longname;
+} vlc_sd_probe_t;
+
+int vlc_sd_probe_Add (vlc_probe_t *probe, const char *name,
+                      const char *longname)
+{
+    vlc_sd_probe_t names = { strdup(name), strdup(longname) };
+
+    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;
+}
+
+#undef vlc_sd_GetNames
+
+/**
+ * Gets the list of available services discovery plugins.
+ */
+char **vlc_sd_GetNames (vlc_object_t *obj, char ***pppsz_longnames)
+{
+    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));
+
+    if (unlikely (names == NULL || longnames == NULL))
+        abort();
+    for( size_t i = 0; i < count; i++ )
+    {
+        names[i] = tab[i].name;
+        longnames[i] = tab[i].longname;
+    }
+    free (tab);
+    names[count] = longnames[count] = NULL;
+    *pppsz_longnames = longnames;
+    return names;
+}
+
+
+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;
+};
+
+static void services_discovery_Destructor ( vlc_object_t *p_obj );
 
 /*
  * Services discovery
@@ -40,31 +104,18 @@ static void RunSD( services_discovery_t *p_sd );
  * That's how the playlist get's Service Discovery information
  */
 
-/***********************************************************************
- * GetServicesNames
- ***********************************************************************/
-char ** __services_discovery_GetServicesNames( vlc_object_t * p_super,
-                                               char ***pppsz_longnames )
-{
-    return module_GetModulesNamesForCapability( p_super, "services_discovery",
-                                                pppsz_longnames );
-}
-
 /***********************************************************************
  * 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 )
 {
     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 );
@@ -75,91 +126,66 @@ services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name
     vlc_event_manager_register_event_type( &p_sd->event_manager,
             vlc_ServicesDiscoveryEnded );
 
-    p_sd->p_module = module_Need( p_sd, "services_discovery", psz_module_name, true );
-
-    if( p_sd->p_module == NULL )
-    {
-        msg_Err( p_super, "no suitable services discovery module" );
-        vlc_object_release( p_sd );
-        return NULL;
-    }
-    p_sd->psz_module = strdup( psz_module_name );
-    p_sd->b_die = false; /* FIXME */
-
+    vlc_object_set_destructor( p_sd, services_discovery_Destructor );
     vlc_object_attach( p_sd, p_super );
+
     return p_sd;
 }
 
 /***********************************************************************
- * Destroy
+ * Stop
  ***********************************************************************/
-void services_discovery_Destroy ( services_discovery_t * p_sd )
+bool vlc_sd_Start ( services_discovery_t * p_sd, const char *module )
 {
-    vlc_event_manager_fini( &p_sd->event_manager );
-
-    free( p_sd->psz_module );
-    free( p_sd->psz_localized_name );
+    assert(!p_sd->p_module);
 
-    vlc_object_detach( p_sd );
-    vlc_object_release( p_sd );
-}
+    p_sd->p_module = module_need( p_sd, "services_discovery", module, true );
 
-/***********************************************************************
- * Start
- ***********************************************************************/
-int services_discovery_Start ( services_discovery_t * p_sd )
-{
-    if ((p_sd->pf_run != NULL)
-        && vlc_thread_create( p_sd, "services_discovery", RunSD,
-                              VLC_THREAD_PRIORITY_LOW, false))
+    if( p_sd->p_module == NULL )
     {
-        msg_Err( p_sd, "cannot create services discovery thread" );
-        vlc_object_release( p_sd );
-        return VLC_EGENERIC;
+        msg_Err( p_sd, "no suitable services discovery module" );
+        return false;
     }
-    return VLC_SUCCESS;
+        
+    vlc_event_t event = {
+        .type = vlc_ServicesDiscoveryStarted
+    };
+    vlc_event_send( &p_sd->event_manager, &event );
+    return true;
 }
-
+                          
 /***********************************************************************
  * Stop
  ***********************************************************************/
-static void ObjectKillChildrens( vlc_object_t *p_obj )
-{
-    vlc_list_t *p_list;
-    int i;
-    vlc_object_kill( p_obj );
-
-    p_list = vlc_list_children( p_obj );
-    for( i = 0; i < p_list->i_count; i++ )
-        ObjectKillChildrens( p_list->p_values[i].p_object );
-    vlc_list_release( p_list );
-}
-
-void services_discovery_Stop ( services_discovery_t * p_sd )
+void vlc_sd_Stop ( services_discovery_t * p_sd )
 {
-    ObjectKillChildrens( VLC_OBJECT(p_sd) );
-    if( p_sd->pf_run ) vlc_thread_join( p_sd );
+    vlc_event_t event = {
+        .type = vlc_ServicesDiscoveryEnded
+    };
+    
+    vlc_event_send( &p_sd->event_manager, &event );
 
-    module_Unneed( p_sd, p_sd->p_module );
+    module_unneed( p_sd, p_sd->p_module );
+    p_sd->p_module = NULL;
 }
 
 /***********************************************************************
- * GetLocalizedName
+ * Destructor
  ***********************************************************************/
-char *
-services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
+static void services_discovery_Destructor ( vlc_object_t *p_obj )
 {
-    return p_sd->psz_localized_name ? strdup( p_sd->psz_localized_name ) : NULL;
+    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 );
 }
 
 /***********************************************************************
- * SetLocalizedName
+ * GetLocalizedName
  ***********************************************************************/
-void
-services_discovery_SetLocalizedName ( services_discovery_t * p_sd, const char *psz )
+char *
+services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
 {
-    free( p_sd->psz_localized_name );
-    p_sd->psz_localized_name = strdup(psz);
+    return strdup( module_get_name( p_sd->p_module, true ) );
 }
 
 /***********************************************************************
@@ -199,23 +225,6 @@ 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 )
-{
-    vlc_event_t event;
-
-    event.type = vlc_ServicesDiscoveryStarted;
-    vlc_event_send( &p_sd->event_manager, &event );
-
-    p_sd->pf_run( p_sd );
-
-    event.type = vlc_ServicesDiscoveryEnded;
-    vlc_event_send( &p_sd->event_manager, &event );
-    return;
-}
-
 /*
  * Playlist - Services discovery bridge
  */
@@ -225,7 +234,7 @@ static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_dat
 {
     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_playlist, "Adding %s in %s",
@@ -234,8 +243,7 @@ static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_dat
 
     PL_LOCK;
     /* If p_parent is in root category (this is clearly a hack) and we have a cat */
-    if( !EMPTY_STR(psz_cat) &&
-        p_parent->p_parent == p_playlist->p_root_category )
+    if( !EMPTY_STR(psz_cat) )
     {
         /* */
         playlist_item_t * p_cat;
@@ -249,13 +257,9 @@ static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_dat
         p_parent = p_cat;
     }
 
-    p_new_item = playlist_NodeAddInput( p_playlist, p_input, p_parent,
-                                        PLAYLIST_APPEND, PLAYLIST_END, pl_Locked );
-    if( p_new_item )
-    {
-        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;
 }
 
@@ -264,192 +268,151 @@ static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_d
 {
     input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
     playlist_item_t * p_parent = user_data;
-    playlist_item_t * p_pl_item;
+    playlist_DeleteFromInput( p_parent->p_playlist, p_input, false );
+}
+
+int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_module )
+{
+    /* Perform the addition */
+    msg_Dbg( p_playlist, "adding services_discovery %s...", psz_module );
 
-    /* First make sure that if item is a node it will be deleted.
-     * XXX: Why don't we have a function to ensure that in the playlist code ? */
-    vlc_object_lock( p_parent->p_playlist );
-    p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
-            p_input->i_id, p_parent, false );
+    services_discovery_t *p_sd = vlc_sd_Create( VLC_OBJECT(p_playlist) );
+    if( !p_sd )
+        return VLC_ENOMEM;
 
-    if( p_pl_item && p_pl_item->i_children > -1 )
+    char *psz_name = NULL;
+    config_ChainCreate( &psz_name, &p_sd->p_cfg, psz_module );
+
+    module_t *m = module_find_by_shortcut( psz_name );
+    if( !m )
     {
-        playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
-        vlc_object_unlock( p_parent->p_playlist );
-        return;
+        msg_Err( p_playlist, "No such module: %s", psz_name );
+        vlc_sd_Destroy( p_sd );
+        return VLC_EGENERIC;
     }
 
-    /* Delete the non-node item normally */
-    playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input->i_id,
-                                      p_parent, pl_Locked );
-
-    vlc_object_unlock( p_parent->p_playlist );
-}
+    /* Free in playlist_ServicesDiscoveryRemove */
+    vlc_sd_internal_t * p_sds = malloc( sizeof(*p_sds) );
+    if( !p_sds )
+    {
+        vlc_sd_Destroy( p_sd );
+        module_release( m );
+        return VLC_ENOMEM;
+    }
 
-int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,  const char *psz_modules )
-{
-    const char *psz_parser = psz_modules ?: "";
-    int retval = VLC_SUCCESS;
+    playlist_item_t *p_node;
 
-    for (;;)
-    {
-        struct playlist_services_discovery_support_t * p_sds;
-        playlist_item_t * p_cat;
-        playlist_item_t * p_one;
+    PL_LOCK;
+    p_node = playlist_NodeCreate( p_playlist, module_get_name( m, true ),
+                                  p_playlist->p_root, 0, NULL );
+    PL_UNLOCK;
+    module_release( m );
 
-        while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
-            psz_parser++;
+    vlc_event_attach( services_discovery_EventManager( p_sd ),
+                      vlc_ServicesDiscoveryItemAdded,
+                      playlist_sd_item_added, p_node );
 
-        if( *psz_parser == '\0' )
-            break;
+    vlc_event_attach( services_discovery_EventManager( p_sd ),
+                      vlc_ServicesDiscoveryItemRemoved,
+                      playlist_sd_item_removed, p_node );
 
-        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;
-
-        /* Perform the addition */
-        msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
-        services_discovery_t *p_sd;
-
-        p_sd = services_discovery_Create( (vlc_object_t*)p_playlist, psz_plugin );
-        if( !p_sd )
-            continue;
-
-        char * psz = services_discovery_GetLocalizedName( p_sd );
-        assert( psz );
-        PL_LOCK;
-        playlist_NodesPairCreate( p_playlist, psz,
-                &p_cat, &p_one, false );
-        PL_UNLOCK;
-        free( psz );
-
-        vlc_event_attach( services_discovery_EventManager( p_sd ),
-                          vlc_ServicesDiscoveryItemAdded,
-                          playlist_sd_item_added,
-                          p_one );
-
-        vlc_event_attach( services_discovery_EventManager( p_sd ),
-                          vlc_ServicesDiscoveryItemAdded,
-                          playlist_sd_item_added,
-                          p_cat );
-
-        vlc_event_attach( services_discovery_EventManager( p_sd ),
-                          vlc_ServicesDiscoveryItemRemoved,
-                          playlist_sd_item_removed,
-                          p_one );
-
-        vlc_event_attach( services_discovery_EventManager( p_sd ),
-                          vlc_ServicesDiscoveryItemRemoved,
-                          playlist_sd_item_removed,
-                          p_cat );
-
-        services_discovery_Start( p_sd );
-
-        /* Free in playlist_ServicesDiscoveryRemove */
-        p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
-        if( !p_sds )
-            return VLC_ENOMEM;
-        p_sds->p_sd = p_sd;
-        p_sds->p_one = p_one;
-        p_sds->p_cat = p_cat;
-
-        PL_LOCK;
-        TAB_APPEND( p_playlist->i_sds, p_playlist->pp_sds, p_sds );
-        PL_UNLOCK;
+    if( !vlc_sd_Start( p_sd, psz_name ) )
+    {
+        vlc_sd_Destroy( p_sd );
+        free( p_sds );
+        return VLC_EGENERIC;
     }
 
-    return retval;
+    /* We want tree-view for service directory */
+    p_node->p_input->b_prefers_tree = true;
+    p_sds->p_sd = p_sd;
+    p_sds->p_node = p_node;
+    p_sds->psz_name = strdup( psz_module );
+
+    PL_LOCK;
+    TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
+    PL_UNLOCK;
+
+    return VLC_SUCCESS;
 }
 
 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
-                                       const char *psz_module )
+                                      const char *psz_name )
 {
-    struct playlist_services_discovery_support_t * p_sds = NULL;
-    int i;
+    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]->p_sd->psz_module ) )
+        if( !strcmp( psz_name, priv->pp_sds[i]->psz_name ) )
         {
-            p_sds = 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_sds || !p_sds->p_sd )
+    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_Stop( p_sds->p_sd );
+    services_discovery_t *p_sd = p_sds->p_sd;
+    assert( p_sd );
 
-    vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
-                        vlc_ServicesDiscoveryItemAdded,
-                        playlist_sd_item_added,
-                        p_sds->p_one );
+    vlc_sd_Stop( p_sd );
 
-    vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
+    vlc_event_detach( services_discovery_EventManager( p_sd ),
                         vlc_ServicesDiscoveryItemAdded,
                         playlist_sd_item_added,
-                        p_sds->p_cat );
-
-    vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
-                        vlc_ServicesDiscoveryItemRemoved,
-                        playlist_sd_item_removed,
-                        p_sds->p_one );
+                        p_sds->p_node );
 
-    vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
+    vlc_event_detach( services_discovery_EventManager( p_sd ),
                         vlc_ServicesDiscoveryItemRemoved,
                         playlist_sd_item_removed,
-                        p_sds->p_cat );
+                        p_sds->p_node );
 
     /* Remove the sd playlist node if it exists */
     PL_LOCK;
-    if( p_sds->p_cat != p_playlist->p_root_category &&
-        p_sds->p_one != p_playlist->p_root_onelevel )
-    {
-        playlist_NodeDelete( p_playlist, p_sds->p_cat, true, false );
-        playlist_NodeDelete( p_playlist, p_sds->p_one, true, false );
-    }
+    playlist_NodeDelete( p_playlist, p_sds->p_node, true, false );
     PL_UNLOCK;
 
-    services_discovery_Destroy( p_sds->p_sd );
+    vlc_sd_Destroy( p_sd );
+    free( p_sds->psz_name );
     free( p_sds );
 
     return VLC_SUCCESS;
 }
 
 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
-                                              const char *psz_module )
+                                         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]->p_sd->psz_module ) )
+        vlc_sd_internal_t *sd = priv->pp_sds[i];
+
+        if( sd->psz_name && !strcmp( psz_name, sd->psz_name ) )
         {
-            PL_UNLOCK;
-            return true;
+            found = true;
+            break;
         }
     }
     PL_UNLOCK;
-    return false;
+    return found;
 }
 
 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
 {
-    while( p_playlist->i_sds > 0 )
+    playlist_private_t *priv = pl_priv( p_playlist );
+
+    while( priv->i_sds > 0 )
         playlist_ServicesDiscoveryRemove( p_playlist,
-                                     p_playlist->pp_sds[0]->p_sd->psz_module );
+                                          priv->pp_sds[0]->psz_name );
 }
-