1 /*****************************************************************************
2 * bonjour.c: Bonjour services discovery module
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Jon Lech Johansen <jon@nanocrew.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_playlist.h>
34 #include <vlc_arrays.h>
36 #include <avahi-client/client.h>
38 # include <avahi-client/publish.h>
39 # include <avahi-client/lookup.h>
41 #include <avahi-common/simple-watch.h>
42 #include <avahi-common/malloc.h>
43 #include <avahi-common/error.h>
45 /*****************************************************************************
47 *****************************************************************************/
50 static int Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
54 set_shortname( "Bonjour" );
55 set_description( _("Bonjour services") );
56 set_category( CAT_PLAYLIST );
57 set_subcategory( SUBCAT_PLAYLIST_SD );
58 set_capability( "services_discovery", 0 );
59 set_callbacks( Open, Close );
62 /*****************************************************************************
64 *****************************************************************************/
66 struct services_discovery_sys_t
68 AvahiSimplePoll *simple_poll;
70 AvahiServiceBrowser *sb;
71 vlc_dictionary_t services_name_to_input_item;
74 /*****************************************************************************
76 *****************************************************************************/
79 static void Run ( services_discovery_t *p_intf );
81 /*****************************************************************************
83 *****************************************************************************/
84 static void client_callback( AvahiClient *c, AvahiClientState state,
87 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
88 services_discovery_sys_t *p_sys = p_sd->p_sys;
91 if( state == AVAHI_CLIENT_FAILURE &&
92 (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) )
94 if( state == AVAHI_CLIENT_DISCONNECTED )
97 msg_Err( p_sd, "avahi client disconnected" );
98 avahi_simple_poll_quit( p_sys->simple_poll );
102 /*****************************************************************************
104 *****************************************************************************/
105 static void resolve_callback(
106 AvahiServiceResolver *r,
107 AvahiIfIndex interface,
108 AvahiProtocol protocol,
109 AvahiResolverEvent event,
113 const char *host_name,
114 const AvahiAddress *address,
116 AvahiStringList *txt,
118 AvahiLookupResultFlags flags,
122 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
123 services_discovery_sys_t *p_sys = p_sd->p_sys;
125 VLC_UNUSED(interface); VLC_UNUSED(host_name);
131 if( event == AVAHI_RESOLVER_FAILURE )
133 if( event == AVAHI_RESOLVER_TIMEOUT )
137 "failed to resolve service '%s' of type '%s' in domain '%s'",
138 name, type, domain );
140 else if( event == AVAHI_RESOLVER_FOUND )
143 char *psz_uri = NULL;
144 char *psz_addr = NULL;
145 AvahiStringList *asl = NULL;
146 input_item_t *p_input = NULL;
148 msg_Dbg( p_sd, "service '%s' of type '%s' in domain '%s'",
149 name, type, domain );
151 avahi_address_snprint(a, (sizeof(a)/sizeof(a[0]))-1, address);
152 if( protocol == AVAHI_PROTO_INET6 )
153 if( asprintf( &psz_addr, "[%s]", a ) == -1 )
157 asl = avahi_string_list_find( txt, "path" );
163 if( avahi_string_list_get_pair( asl, &key, &value, &size ) == 0 &&
166 if( asprintf( &psz_uri, "http://%s:%d%s",
167 psz_addr != NULL ? psz_addr : a, port, value ) == -1 )
174 avahi_free( (void *)key );
176 avahi_free( (void *)value );
180 if( asprintf( &psz_uri, "http://%s:%d",
181 psz_addr != NULL ? psz_addr : a, port ) == -1 )
188 if( psz_addr != NULL )
189 free( (void *)psz_addr );
191 if( psz_uri != NULL )
193 p_input = input_ItemNewExt( p_sd, psz_uri, name, 0, NULL, -1 );
194 free( (void *)psz_uri );
196 if( p_input != NULL )
198 vlc_dictionary_insert( &p_sys->services_name_to_input_item,
200 services_discovery_AddItem( p_sd, p_input, NULL /* no category */ );
201 vlc_gc_decref( p_input );
205 avahi_service_resolver_free( r );
208 /*****************************************************************************
210 *****************************************************************************/
211 static void browse_callback(
212 AvahiServiceBrowser *b,
213 AvahiIfIndex interface,
214 AvahiProtocol protocol,
215 AvahiBrowserEvent event,
220 AvahiLookupResultFlags flags,
228 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
229 services_discovery_sys_t *p_sys = p_sd->p_sys;
230 if( event == AVAHI_BROWSER_NEW )
232 if( avahi_service_resolver_new( p_sys->client, interface, protocol,
233 name, type, domain, AVAHI_PROTO_UNSPEC,
237 resolve_callback, userdata ) == NULL )
239 msg_Err( p_sd, "failed to resolve service '%s': %s", name,
240 avahi_strerror( avahi_client_errno( p_sys->client ) ) );
245 /** \todo Store the input id and search it, rather than searching the items */
246 input_item_t *p_item;
247 p_item = vlc_dictionary_value_for_key(
248 &p_sys->services_name_to_input_item,
251 msg_Err( p_sd, "failed to find service '%s' in playlist", name );
254 services_discovery_RemoveItem( p_sd, p_item );
255 vlc_dictionary_remove_value_for_key(
256 &p_sys->services_name_to_input_item,
262 /*****************************************************************************
263 * Open: initialize and create stuff
264 *****************************************************************************/
265 static int Open( vlc_object_t *p_this )
267 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
268 services_discovery_sys_t *p_sys;
271 p_sd->p_sys = p_sys = (services_discovery_sys_t *)malloc(
272 sizeof( services_discovery_sys_t ) );
273 if( p_sd->p_sys == NULL )
275 msg_Err( p_sd, "out of memory" );
279 memset( p_sys, 0, sizeof(*p_sys) );
281 vlc_dictionary_init( &p_sys->services_name_to_input_item, 1 );
283 p_sys->simple_poll = avahi_simple_poll_new();
284 if( p_sys->simple_poll == NULL )
286 msg_Err( p_sd, "failed to create avahi simple poll" );
290 p_sys->client = avahi_client_new( avahi_simple_poll_get(p_sys->simple_poll),
294 client_callback, p_sd, &err );
295 if( p_sys->client == NULL )
297 msg_Err( p_sd, "failed to create avahi client: %s",
298 avahi_strerror( err ) );
302 p_sys->sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC,
304 "_vlc-http._tcp", NULL,
308 browse_callback, p_sd );
309 if( p_sys->sb == NULL )
311 msg_Err( p_sd, "failed to create avahi service browser" );
315 services_discovery_SetLocalizedName( p_sd, _("Bonjour") );
322 if( p_sys->sb != NULL )
323 avahi_service_browser_free( p_sys->sb );
324 if( p_sys->client != NULL )
325 avahi_client_free( p_sys->client );
326 if( p_sys->simple_poll != NULL )
327 avahi_simple_poll_free( p_sys->simple_poll );
329 vlc_dictionary_clear( &p_sys->services_name_to_input_item );
335 /*****************************************************************************
337 *****************************************************************************/
338 static void Close( vlc_object_t *p_this )
340 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
341 services_discovery_sys_t *p_sys = p_sd->p_sys;
343 avahi_service_browser_free( p_sys->sb );
344 avahi_client_free( p_sys->client );
345 avahi_simple_poll_free( p_sys->simple_poll );
347 vlc_dictionary_clear( &p_sys->services_name_to_input_item );
351 /*****************************************************************************
353 *****************************************************************************/
354 static void Run( services_discovery_t *p_sd )
356 services_discovery_sys_t *p_sys = p_sd->p_sys;
358 while( !p_sd->b_die )
360 if( avahi_simple_poll_iterate( p_sys->simple_poll, 100 ) != 0 )
362 msg_Err( p_sd, "poll iterate failed" );