[vlc-devel] [PATCH 12/14] dsm/sd: discover in a separate thread and handle commands

Thomas Guillem thomas at gllm.fr
Fri Dec 5 14:43:03 CET 2014


---
 modules/access/dsm/sd.c | 200 ++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 168 insertions(+), 32 deletions(-)

diff --git a/modules/access/dsm/sd.c b/modules/access/dsm/sd.c
index 9b581f6..1e846c1 100644
--- a/modules/access/dsm/sd.c
+++ b/modules/access/dsm/sd.c
@@ -34,7 +34,14 @@
 
 struct services_discovery_sys_t
 {
-    netbios_ns      *ns;
+    bdsm_sys_t      bdsm;
+    vlc_thread_t    thread;
+    vlc_mutex_t     mutex;
+    vlc_cond_t      cond;
+
+    bool b_stop;
+    enum services_discovery_command_e cmd;
+    char *psz_cmd_arg;
 };
 
 int bdsm_sd_probe_Open (vlc_object_t *p_this)
@@ -47,53 +54,167 @@ int bdsm_sd_probe_Open (vlc_object_t *p_this)
     return VLC_PROBE_CONTINUE;
 }
 
+static bool ShouldAbort (services_discovery_t *p_sd)
+{
+    services_discovery_sys_t *p_sys = p_sd->p_sys;
+    bool b_abort;
+
+    vlc_mutex_lock( &p_sys->mutex );
+    b_abort = p_sys->b_stop || p_sys->cmd != 0;
+    vlc_mutex_unlock( &p_sys->mutex );
+    return b_abort;
+}
+
+static int AddToSD (void *p_opaque, input_item_t *p_item)
+{
+    services_discovery_t *p_sd = p_opaque;
+    services_discovery_AddItem( p_sd, p_item, NULL );
+    return 0;
+}
+
+static int SdBrowse (services_discovery_t *p_sd, const char *psz_path)
+{
+    services_discovery_sys_t *p_sys = p_sd->p_sys;
+    netbios_ns *ns = p_sys->bdsm.p_ns;
+
+    if( psz_path == NULL ) {
+        /* search for netbios */
+        if( !netbios_ns_discover( ns ) )
+            return VLC_EGENERIC;
+
+        if( ShouldAbort( p_sd ) )
+            return VLC_SUCCESS;
+
+        for( ssize_t i = 0; i < netbios_ns_entry_count( ns ); i++ )
+        {
+            netbios_ns_entry *p_entry = netbios_ns_entry_at( ns, i );
+
+            if( p_entry->type == 0x20 )
+            {
+                input_item_t *p_item;
+                char *psz_mrl;
+
+                if( asprintf(&psz_mrl, "smb://%s", p_entry->name) < 0 )
+                    return VLC_EGENERIC;
+
+                p_item = input_item_NewWithType( psz_mrl, p_entry->name, 0, NULL,
+                                                 0, -1, ITEM_TYPE_NODE );
+                msg_Dbg( p_sd, "Adding item %s", psz_mrl );
+
+                services_discovery_AddItem( p_sd, p_item, NULL );
+
+                free( psz_mrl );
+            }
+        }
+    } else {
+        bdsm_item_cb_t cb;
+
+        if( bdsm_Connect( &p_sys->bdsm, psz_path ) != VLC_SUCCESS )
+            return VLC_SUCCESS;
+
+        cb.pf_add_item = AddToSD;
+        cb.p_opaque = p_sd;
+        bdsm_Browse( &p_sys->bdsm, &cb, psz_path );
+    }
+
+    return VLC_SUCCESS;
+}
+
+static void *Run( void *data )
+{
+    services_discovery_t *p_sd = data;
+    services_discovery_sys_t *p_sys = p_sd->p_sys;
+
+    while( true ) {
+        enum services_discovery_command_e cmd;
+        char *psz_cmd_arg;
+        int i_ret = 0;
+
+        vlc_mutex_lock( &p_sys->mutex );
+        while( !p_sys->b_stop && p_sys->cmd == 0 )
+            vlc_cond_wait( &p_sys->cond, &p_sys->mutex );
+        if( p_sys->b_stop ) {
+            vlc_mutex_unlock( &p_sys->mutex );
+            break;
+        }
+        cmd = p_sys->cmd;
+        psz_cmd_arg = p_sys->psz_cmd_arg;
+
+        p_sys->cmd = 0;
+        p_sys->psz_cmd_arg = NULL;
+
+        vlc_mutex_unlock( &p_sys->mutex );
+        switch( cmd )
+        {
+            case SD_CMD_BROWSE:
+                i_ret = SdBrowse( p_sd, psz_cmd_arg );
+                break;
+            default:
+                msg_Warn( p_sd, "unknown command ");
+        }
+
+        free(psz_cmd_arg);
+        if( i_ret != 0 )
+            break;
+    }
+    return NULL;
+}
+
+static int Control (services_discovery_t *p_sd, int i_control, va_list args)
+{
+    services_discovery_sys_t *p_sys = p_sd->p_sys;
+
+    switch( i_control)
+    {
+        case SD_CMD_BROWSE:
+        {
+            const char *psz_path = va_arg( args, const char * );
+
+            vlc_mutex_lock( &p_sys->mutex );
+            p_sys->cmd = i_control;
+            p_sys->psz_cmd_arg = psz_path ? strdup( psz_path ) : NULL;
+            vlc_cond_signal( &p_sys->cond );
+            vlc_mutex_unlock( &p_sys->mutex );
+
+            netbios_ns_abort( p_sys->bdsm.p_ns );
+            return VLC_SUCCESS;
+        }
+    }
+    return VLC_EGENERIC;
+}
+
 int bdsm_SdOpen (vlc_object_t *p_this)
 {
     services_discovery_t *p_sd = (services_discovery_t *)p_this;
-    services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
+    services_discovery_sys_t *p_sys = calloc (1, sizeof (*p_sys));
 
     if( p_sys == NULL )
         return VLC_ENOMEM;
-    p_sd->p_sys = p_sys;
 
     /* Let's create a NETBIOS name service object */
-    p_sys->ns = netbios_ns_new();
-    if( p_sys->ns == NULL )
+    if( bdsm_Init( &p_sys->bdsm, VLC_OBJECT(p_sd) ) != VLC_SUCCESS )
         goto error;
 
-    if( !netbios_ns_discover( p_sys->ns ) )
-        goto error;
+    vlc_mutex_init( &p_sys->mutex );
+    vlc_cond_init( &p_sys->cond );
 
-    for( ssize_t i = 0; i < netbios_ns_entry_count( p_sys->ns ); i++ )
-    {
-        netbios_ns_entry *p_entry = netbios_ns_entry_at( p_sys->ns, i );
+    p_sys->cmd = SD_CMD_BROWSE;
+    p_sys->psz_cmd_arg = NULL;
 
-        if( p_entry->type == 0x20 )
-        {
-            input_item_t *p_item;
-            char *psz_mrl;
-
-            if( asprintf(&psz_mrl, "smb://%s", p_entry->name) < 0 )
-                goto error;
-
-            p_item = input_item_NewWithType( psz_mrl, p_entry->name, 0, NULL,
-                                             0, -1, ITEM_TYPE_NODE );
-            msg_Dbg( p_sd, "Adding item %s", psz_mrl );
-
-            services_discovery_AddItem( p_sd, p_item, NULL );
+    p_sd->p_sys = p_sys;
 
-            free( psz_mrl );
-        }
+    if( vlc_clone( &p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW ) )
+    {
+        p_sys->thread = 0;
+        goto error;
     }
 
+    p_sd->pf_control = Control;
+
     return VLC_SUCCESS;
 
     error:
-        if( p_sys->ns != NULL )
-            netbios_ns_destroy( p_sys->ns );
-        free( p_sys );
-        p_sd->p_sys = NULL;
-
+        bdsm_SdClose( p_this );
         return VLC_EGENERIC;
 }
 
@@ -105,7 +226,22 @@ void bdsm_SdClose (vlc_object_t *p_this)
     if( p_sys == NULL )
         return;
 
-    if( p_sys->ns != NULL )
-        netbios_ns_destroy( p_sys->ns );
+    if( p_sys->thread ) {
+        vlc_mutex_lock( &p_sys->mutex );
+        p_sys->b_stop = true;
+        vlc_cond_signal( &p_sys->cond );
+        vlc_mutex_unlock( &p_sys->mutex );
+
+        if( p_sys->bdsm.p_ns )
+            netbios_ns_abort( p_sys->bdsm.p_ns );
+        vlc_join( p_sys->thread, NULL );
+    }
+
+    vlc_mutex_destroy( &p_sys->mutex );
+    vlc_cond_destroy( &p_sys->cond );
+
+    bdsm_Destroy( &p_sys->bdsm );
+
+    free( p_sys );
 }
 
-- 
2.1.3




More information about the vlc-devel mailing list