[vlc-commits] avahi: Prepare code for support of renderer discovery

Stefan Brüns git at videolan.org
Thu Feb 27 14:10:37 CET 2020


vlc/vlc-3.0 | branch: master | Stefan Brüns <stefan.bruens at rwth-aachen.de> | Sat Jan 19 22:41:13 2019 +0100| [c8beaa13a1cacb484f5ff6e6d8301d5db3a848a8] | committer: Thomas Guillem

avahi: Prepare code for support of renderer discovery

Instead of passing the services_discovery_t as userdata for the callbacks,
use a struct discovery_sys_t which is common for both the input and renderer
private data structures.

The discovery_sys_t is embedded into the service type specific private
member structs, i.e. struct services_discovery_sys_t (done here) and
struct vlc_renderer_discovery_sys (next patch).

Signed-off-by: Thomas Guillem <thomas at gllm.fr>
(cherry picked from commit da019696fbc76b7afea69b33f2c537c85a350a59)
Signed-off-by: Thomas Guillem <thomas at gllm.fr>

> http://git.videolan.org/gitweb.cgi/vlc/vlc-3.0.git/?a=commit;h=c8beaa13a1cacb484f5ff6e6d8301d5db3a848a8
---

 modules/services_discovery/avahi.c | 81 ++++++++++++++++++++++----------------
 1 file changed, 47 insertions(+), 34 deletions(-)

diff --git a/modules/services_discovery/avahi.c b/modules/services_discovery/avahi.c
index e73c44bf91..5a5df7e68a 100644
--- a/modules/services_discovery/avahi.c
+++ b/modules/services_discovery/avahi.c
@@ -46,8 +46,8 @@
  *****************************************************************************/
 
 /* Callbacks */
-static int  Open ( vlc_object_t * );
-static void Close( vlc_object_t * );
+static int  OpenSD ( vlc_object_t * );
+static void CloseSD( vlc_object_t * );
 
 VLC_SD_PROBE_HELPER("avahi", N_("Zeroconf network services"), SD_CAT_LAN)
 
@@ -57,7 +57,7 @@ vlc_module_begin ()
     set_category( CAT_PLAYLIST )
     set_subcategory( SUBCAT_PLAYLIST_SD )
     set_capability( "services_discovery", 0 )
-    set_callbacks( Open, Close )
+    set_callbacks( OpenSD, CloseSD )
     add_shortcut( "mdns", "avahi" )
 
     VLC_SD_PROBE_SUBMODULE
@@ -67,12 +67,14 @@ vlc_module_end ()
  * Local structures
  *****************************************************************************/
 
-struct services_discovery_sys_t
+typedef struct
 {
     AvahiThreadedPoll   *poll;
     AvahiClient         *client;
     vlc_dictionary_t    services_name_to_input_item;
-};
+    vlc_object_t        *parent;
+    bool                renderer;
+} discovery_sys_t;
 
 static const struct
 {
@@ -93,13 +95,12 @@ static const struct
 static void client_callback( AvahiClient *c, AvahiClientState state,
                              void * userdata )
 {
-    services_discovery_t *p_sd = ( services_discovery_t* )userdata;
-    services_discovery_sys_t *p_sys = p_sd->p_sys;
+    discovery_sys_t *p_sys = userdata;
 
     if( state == AVAHI_CLIENT_FAILURE &&
         (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) )
     {
-        msg_Err( p_sd, "avahi client disconnected" );
+        msg_Err( p_sys->parent, "avahi client disconnected" );
         avahi_threaded_poll_quit( p_sys->poll );
     }
 }
@@ -122,15 +123,14 @@ static void resolve_callback(
     AvahiLookupResultFlags flags,
     void* userdata )
 {
-    services_discovery_t *p_sd = ( services_discovery_t* )userdata;
-    services_discovery_sys_t *p_sys = p_sd->p_sys;
+    discovery_sys_t *p_sys = userdata;
 
     VLC_UNUSED(interface); VLC_UNUSED(host_name);
     VLC_UNUSED(flags);
 
     if( event == AVAHI_RESOLVER_FAILURE )
     {
-        msg_Err( p_sd,
+        msg_Err( p_sys->parent,
                  "failed to resolve service '%s' of type '%s' in domain '%s'",
                  name, type, domain );
     }
@@ -142,7 +142,7 @@ static void resolve_callback(
         AvahiStringList *asl = NULL;
         input_item_t *p_input = NULL;
 
-        msg_Info( p_sd, "service '%s' of type '%s' in domain '%s' port %i",
+        msg_Info( p_sys->parent, "service '%s' of type '%s' in domain '%s' port %i",
                   name, type, domain, port );
 
         avahi_address_snprint(a, (sizeof(a)/sizeof(a[0]))-1, address);
@@ -212,6 +212,7 @@ static void resolve_callback(
         }
         if( p_input != NULL )
         {
+            services_discovery_t *p_sd = ( services_discovery_t* )(p_sys->parent);
             vlc_dictionary_insert( &p_sys->services_name_to_input_item,
                 name, p_input );
             services_discovery_AddItem( p_sd, p_input );
@@ -237,8 +238,7 @@ static void browse_callback(
 {
     VLC_UNUSED(b);
     VLC_UNUSED(flags);
-    services_discovery_t *p_sd = ( services_discovery_t* )userdata;
-    services_discovery_sys_t *p_sys = p_sd->p_sys;
+    discovery_sys_t *p_sys = userdata;
     if( event == AVAHI_BROWSER_NEW )
     {
         if( avahi_service_resolver_new( p_sys->client, interface, protocol,
@@ -246,21 +246,22 @@ static void browse_callback(
                                         0,
                                         resolve_callback, userdata ) == NULL )
         {
-            msg_Err( p_sd, "failed to resolve service '%s': %s", name,
+            msg_Err( p_sys->parent, "failed to resolve service '%s': %s", name,
                      avahi_strerror( avahi_client_errno( p_sys->client ) ) );
         }
     }
     else if( event == AVAHI_BROWSER_REMOVE && name )
     {
         /** \todo Store the input id and search it, rather than searching the items */
-        input_item_t *p_item;
+        void *p_item;
         p_item = vlc_dictionary_value_for_key(
                         &p_sys->services_name_to_input_item,
                         name );
         if( !p_item )
-            msg_Err( p_sd, "failed to find service '%s' in playlist", name );
+            msg_Err( p_sys->parent, "failed to find service '%s' in playlist", name );
         else
         {
+            services_discovery_t *p_sd = ( services_discovery_t* )(p_sys->parent);
             services_discovery_RemoveItem( p_sd, p_item );
             vlc_dictionary_remove_value_for_key(
                         &p_sys->services_name_to_input_item,
@@ -279,32 +280,24 @@ static void clear_item( void* p_item, void* p_obj )
 /*****************************************************************************
  * Open: initialize and create stuff
  *****************************************************************************/
-static int Open( vlc_object_t *p_this )
+static int OpenCommon( discovery_sys_t *p_sys )
 {
-    services_discovery_t *p_sd = ( services_discovery_t* )p_this;
-    services_discovery_sys_t *p_sys;
     int err;
 
-    p_sd->p_sys = p_sys = calloc( 1, sizeof( services_discovery_sys_t ) );
-    if( !p_sys )
-        return VLC_ENOMEM;
-
-    p_sd->description = _("Zeroconf network services");
-
     vlc_dictionary_init( &p_sys->services_name_to_input_item, 1 );
 
     p_sys->poll = avahi_threaded_poll_new();
     if( p_sys->poll == NULL )
     {
-        msg_Err( p_sd, "failed to create Avahi threaded poll" );
+        msg_Err( p_sys->parent, "failed to create Avahi threaded poll" );
         goto error;
     }
 
     p_sys->client = avahi_client_new( avahi_threaded_poll_get(p_sys->poll),
-                                      0, client_callback, p_sd, &err );
+                                      0, client_callback, p_sys, &err );
     if( p_sys->client == NULL )
     {
-        msg_Err( p_sd, "failed to create avahi client: %s",
+        msg_Err( p_sys->parent, "failed to create avahi client: %s",
                  avahi_strerror( err ) );
         goto error;
     }
@@ -315,10 +308,10 @@ static int Open( vlc_object_t *p_this )
         sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC,
                 AVAHI_PROTO_UNSPEC,
                 protocols[i].psz_service_name, NULL,
-                0, browse_callback, p_sd );
+                0, browse_callback, p_sys );
         if( sb == NULL )
         {
-            msg_Err( p_sd, "failed to create avahi service browser %s", avahi_strerror( avahi_client_errno(p_sys->client) ) );
+            msg_Err( p_sys->parent, "failed to create avahi service browser %s", avahi_strerror( avahi_client_errno(p_sys->client) ) );
             goto error;
         }
     }
@@ -339,13 +332,26 @@ error:
     return VLC_EGENERIC;
 }
 
+static int OpenSD( vlc_object_t *p_this )
+{
+    services_discovery_t *p_sd = ( services_discovery_t* )p_this;
+    p_sd->description = _("Zeroconf network services");
+
+    p_sd->p_sys = calloc( 1, sizeof( discovery_sys_t ) );
+    discovery_sys_t *p_sys = (void *) p_sd->p_sys;
+    if( !p_sd->p_sys )
+        return VLC_ENOMEM;
+    p_sys->parent = p_this;
+    p_sys->renderer = false;
+
+    return OpenCommon( p_sys );
+}
+
 /*****************************************************************************
  * Close: cleanup
  *****************************************************************************/
-static void Close( vlc_object_t *p_this )
+static void CloseCommon( discovery_sys_t *p_sys )
 {
-    services_discovery_t *p_sd = ( services_discovery_t* )p_this;
-    services_discovery_sys_t *p_sys = p_sd->p_sys;
     avahi_threaded_poll_stop( p_sys->poll );
 
     avahi_client_free( p_sys->client );
@@ -354,3 +360,10 @@ static void Close( vlc_object_t *p_this )
     vlc_dictionary_clear( &p_sys->services_name_to_input_item, clear_item, NULL );
     free( p_sys );
 }
+
+static void CloseSD( vlc_object_t *p_this )
+{
+    services_discovery_t *p_sd = ( services_discovery_t* )p_this;
+    discovery_sys_t *p_sys = (void *) p_sd->p_sys;
+    CloseCommon( p_sys );
+}



More information about the vlc-commits mailing list