]> git.sesse.net Git - vlc/blobdiff - src/playlist/services_discovery.c
probe: helpers for services discovery
[vlc] / src / playlist / services_discovery.c
index 4c96eaeb4a799294ad1992c0a0a3606951ccf457..355dffb7f1483b9d7d60c697bbc0e50147a6e537 100644 (file)
@@ -2,9 +2,9 @@
  * services_discovery.c : Manage playlist services_discovery modules
  *****************************************************************************
  * Copyright (C) 1999-2004 the VideoLAN team
- * $Id: playlist.c 9216 2004-11-07 10:43:52Z zorglub $
+ * $Id$
  *
- * Authors: Clément Stenac <zorglub@videolan.org>
+ * Authors: Clément Stenac <zorglub@videolan.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
-#include <stdlib.h>                                      /* free(), strtol() */
-#include <stdio.h>                                              /* sprintf() */
-#include <string.h>                                            /* strerror() */
-
-#include <vlc/vlc.h>
-#include <vlc/vout.h>
-#include <vlc/sout.h>
-#include <vlc/input.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+#include <assert.h>
 
+#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"
 
-/*****************************************************************************
- * Local prototypes
- *****************************************************************************/
+typedef struct
+{
+    char *name;
+    char *longname;
+} vlc_sd_probe_t;
 
-static void RunSD( services_discovery_t *p_sd );
+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
 
-int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,
-                                   const char *psz_module )
+/**
+ * Gets the list of available services discovery plugins.
+ */
+char **vlc_sd_GetNames (vlc_object_t *obj, char ***pppsz_longnames)
 {
-    services_discovery_t *p_sd;
+    size_t count;
+    vlc_sd_probe_t *tab = vlc_probe (obj, "services probe", &count);
 
-    p_sd = vlc_object_create( p_playlist, VLC_OBJECT_SD );
-    p_sd->pf_run = NULL;
+    if (count == 0)
+    {
+        free (tab);
+        return NULL;
+    }
 
-    p_sd->p_module = module_Need( p_sd, "services_discovery", psz_module, 0 );
+    char **names = malloc (sizeof(char *) * (count + 1));
+    char **longnames = malloc (sizeof(char *) * (count + 1));
 
-    if( p_sd->p_module == NULL )
+    if (unlikely (names == NULL || longnames == NULL))
+        abort();
+    for( size_t i = 0; i < count; i++ )
     {
-        msg_Err( p_playlist, "no suitable services discovery module" );
-        vlc_object_destroy( p_sd );
-        return VLC_EGENERIC;
+        names[i] = tab[i].name;
+        longnames[i] = tab[i].longname;
     }
+    free (tab);
+    names[count] = longnames[count] = NULL;
+    *pppsz_longnames = longnames;
+    return names;
+}
 
-    p_sd->psz_module = strdup( psz_module );
-    p_sd->b_die = VLC_FALSE;
 
-    vlc_mutex_lock( &p_playlist->object_lock );
+struct vlc_sd_internal_t
+{
+    /* the playlist items for category and onelevel */
+    playlist_item_t      *p_cat;
+    playlist_item_t      *p_one;
+    services_discovery_t *p_sd; /**< Loaded service discovery modules */
+    char                 *psz_name;
+};
 
-    INSERT_ELEM( p_playlist->pp_sds, p_playlist->i_sds, p_playlist->i_sds,
-                 p_sd );
+static void services_discovery_Destructor ( vlc_object_t *p_obj );
 
-    vlc_mutex_unlock( &p_playlist->object_lock );
+/*
+ * Services discovery
+ * Basically you just listen to Service discovery event through the
+ * sd's event manager.
+ * That's how the playlist get's Service Discovery information
+ */
+
+/***********************************************************************
+ * Create
+ ***********************************************************************/
+services_discovery_t *vlc_sd_Create( vlc_object_t *p_super )
+{
+    services_discovery_t *p_sd;
 
-    if( vlc_thread_create( p_sd, "services_discovery", RunSD,
-                           VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
+    p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
+                              "services discovery" );
+    if( !p_sd )
+        return 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 );
+    vlc_event_manager_register_event_type( &p_sd->event_manager,
+            vlc_ServicesDiscoveryStarted );
+    vlc_event_manager_register_event_type( &p_sd->event_manager,
+            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, const char *module )
+{
+    assert(!p_sd->p_module);
+
+    p_sd->p_module = module_need( p_sd, "services_discovery", module, true );
+
+    if( p_sd->p_module == NULL )
     {
-        msg_Err( p_sd, "cannot create services discovery thread" );
-        vlc_object_destroy( p_sd );
-        return VLC_EGENERIC;
+        msg_Err( p_sd, "no suitable services discovery module" );
+        return false;
     }
+        
+    vlc_event_t event = {
+        .type = vlc_ServicesDiscoveryStarted
+    };
+    vlc_event_send( &p_sd->event_manager, &event );
+    return true;
+}
+                          
+/***********************************************************************
+ * Stop
+ ***********************************************************************/
+void vlc_sd_Stop ( services_discovery_t * 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 );
+    p_sd->p_module = NULL;
+}
 
-    return VLC_SUCCESS;
+/***********************************************************************
+ * Destructor
+ ***********************************************************************/
+static void services_discovery_Destructor ( vlc_object_t *p_obj )
+{
+    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 );
 }
 
-int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
-                                       const char *psz_module )
+/***********************************************************************
+ * GetLocalizedName
+ ***********************************************************************/
+char *
+services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
+{
+    return strdup( module_get_name( p_sd->p_module, true ) );
+}
+
+/***********************************************************************
+ * EventManager
+ ***********************************************************************/
+vlc_event_manager_t *
+services_discovery_EventManager ( services_discovery_t * p_sd )
+{
+    return &p_sd->event_manager;
+}
+
+/***********************************************************************
+ * AddItem
+ ***********************************************************************/
+void
+services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
+                             const char * psz_category )
+{
+    vlc_event_t event;
+    event.type = vlc_ServicesDiscoveryItemAdded;
+    event.u.services_discovery_item_added.p_new_item = p_item;
+    event.u.services_discovery_item_added.psz_category = psz_category;
+
+    vlc_event_send( &p_sd->event_manager, &event );
+}
+
+/***********************************************************************
+ * RemoveItem
+ ***********************************************************************/
+void
+services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
+{
+    vlc_event_t event;
+    event.type = vlc_ServicesDiscoveryItemRemoved;
+    event.u.services_discovery_item_removed.p_item = p_item;
+
+    vlc_event_send( &p_sd->event_manager, &event );
+}
+
+/*
+ * Playlist - Services discovery bridge
+ */
+
+ /* 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 )
 {
-    int i;
-    services_discovery_t *p_sd = NULL;
-    vlc_mutex_lock( &p_playlist->object_lock );
+    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_t * p_playlist = p_parent->p_playlist;
 
-    for( i = 0 ; i< p_playlist->i_sds ; i ++ )
+    msg_Dbg( p_playlist, "Adding %s in %s",
+                p_input->psz_name ? p_input->psz_name : "(null)",
+                psz_cat ? psz_cat : "(null)" );
+
+    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( !strcmp( psz_module, p_playlist->pp_sds[i]->psz_module ) )
+        /* */
+        playlist_item_t * p_cat;
+        p_cat = playlist_ChildSearchName( p_parent, psz_cat );
+        if( !p_cat )
         {
-            p_sd = p_playlist->pp_sds[i];
-            REMOVE_ELEM( p_playlist->pp_sds, p_playlist->i_sds, i );
-            break;
+            p_cat = playlist_NodeCreate( p_playlist, psz_cat,
+                                         p_parent, 0, NULL );
+            p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
         }
+        p_parent = p_cat;
     }
 
-    if( p_sd )
+    p_new_item = playlist_NodeAddInput( p_playlist, p_input, p_parent,
+                                        PLAYLIST_APPEND, PLAYLIST_END, pl_Locked );
+    if( p_new_item )
     {
-        vlc_mutex_unlock( &p_playlist->object_lock );
-        p_sd->b_die = VLC_TRUE;
-        vlc_thread_join( p_sd );
-        free( p_sd->psz_module );
-        module_Unneed( p_sd, p_sd->p_module );
-        vlc_mutex_lock( &p_playlist->object_lock );
-        vlc_object_destroy( p_sd );
+        p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
+        p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
     }
+    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 )
+{
+    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;
+
+    /* 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 ? */
+    playlist_Lock( p_parent->p_playlist );
+    p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
+            p_input, p_parent, false );
+
+    if( p_pl_item && p_pl_item->i_children > -1 )
+        playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
     else
+        /* Delete the non-node item normally */
+        playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input,
+                                          p_parent, pl_Locked );
+
+    playlist_Unlock( p_parent->p_playlist );
+}
+
+int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_module )
+{
+    /* Perform the addition */
+    msg_Dbg( p_playlist, "adding services_discovery %s...", psz_module );
+
+    services_discovery_t *p_sd = vlc_sd_Create( VLC_OBJECT(p_playlist) );
+    if( !p_sd )
+        return VLC_ENOMEM;
+
+    module_t *m = module_find_by_shortcut( psz_module );
+    if( !m )
     {
-        msg_Warn( p_playlist, "module %s is not loaded", psz_module );
-        vlc_mutex_unlock( &p_playlist->object_lock );
+        msg_Err( p_playlist, "No such module: %s", psz_module );
+        vlc_sd_Destroy( p_sd );
         return VLC_EGENERIC;
     }
 
-    vlc_mutex_unlock( &p_playlist->object_lock );
+    /* 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;
+    }
+
+    playlist_item_t * p_cat;
+    playlist_item_t * p_one;
+
+    PL_LOCK;
+    playlist_NodesPairCreate( p_playlist, module_get_name( m, true ),
+                              &p_cat, &p_one, false );
+    PL_UNLOCK;
+    module_release( m );
+
+    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 );
+
+    if( !vlc_sd_Start( p_sd, psz_module ) )
+    {
+        vlc_sd_Destroy( p_sd );
+        free( p_sds );
+        return VLC_EGENERIC;
+    }
+
+    /* We want tree-view for service directory */
+    p_one->p_input->b_prefers_tree = true;
+    p_sds->p_sd = p_sd;
+    p_sds->p_one = p_one;
+    p_sds->p_cat = p_cat;
+    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;
 }
 
-vlc_bool_t playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
-                                              const char *psz_module )
+int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
+                                      const char *psz_name )
 {
-    int i;
-    vlc_mutex_lock( &p_playlist->object_lock );
+    playlist_private_t *priv = pl_priv( p_playlist );
+    vlc_sd_internal_t * p_sds = NULL;
 
-    for( i = 0 ; i< p_playlist->i_sds ; i ++ )
+    PL_LOCK;
+    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 ) )
         {
-            vlc_mutex_unlock( &p_playlist->object_lock );
-            return VLC_TRUE;
+            p_sds = priv->pp_sds[i];
+            REMOVE_ELEM( priv->pp_sds, priv->i_sds, i );
+            break;
         }
     }
-    vlc_mutex_unlock( &p_playlist->object_lock );
-    return VLC_FALSE;
-}
+    PL_UNLOCK;
 
+    if( !p_sds )
+    {
+        msg_Warn( p_playlist, "discovery %s is not loaded", psz_name );
+        return VLC_EGENERIC;
+    }
 
-/**
- * Load all service discovery modules in a string
- *
- * \param p_playlist the playlist
- * \param psz_modules a list of modules separated by commads
- * return VLC_SUCCESS or an error
- */
-int playlist_AddSDModules( playlist_t *p_playlist, char *psz_modules )
+    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_one );
+
+    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_sd ),
+                        vlc_ServicesDiscoveryItemRemoved,
+                        playlist_sd_item_removed,
+                        p_sds->p_one );
+
+    vlc_event_detach( services_discovery_EventManager( p_sd ),
+                        vlc_ServicesDiscoveryItemRemoved,
+                        playlist_sd_item_removed,
+                        p_sds->p_cat );
+
+    /* 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 );
+    }
+    PL_UNLOCK;
+
+    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_name )
 {
-    if( psz_modules && *psz_modules )
+    playlist_private_t *priv = pl_priv( p_playlist );
+    bool found = false;
+    PL_LOCK;
+
+    for( int i = 0; i < priv->i_sds; i++ )
     {
-        char *psz_parser = psz_modules;
-        char *psz_next;
+        vlc_sd_internal_t *sd = priv->pp_sds[i];
 
-        while( psz_parser && *psz_parser )
+        if( sd->psz_name && !strcmp( psz_name, sd->psz_name ) )
         {
-            while( *psz_parser == ' ' || *psz_parser == ':' )
-            {
-                psz_parser++;
-            }
-
-            if( (psz_next = strchr( psz_parser, ':' ) ) )
-            {
-                *psz_next++ = '\0';
-            }
-            if( *psz_parser == '\0' )
-            {
-                break;
-            }
-
-            playlist_ServicesDiscoveryAdd( p_playlist, psz_parser );
-
-            psz_parser = psz_next;
+            found = true;
+            break;
         }
     }
-    return VLC_SUCCESS;
+    PL_UNLOCK;
+    return found;
 }
 
-static void RunSD( services_discovery_t *p_sd )
+void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
 {
-    p_sd->pf_run( p_sd );
-    return;
+    playlist_private_t *priv = pl_priv( p_playlist );
+
+    while( priv->i_sds > 0 )
+        playlist_ServicesDiscoveryRemove( p_playlist,
+                                          priv->pp_sds[0]->psz_name );
 }