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 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc_playlist.h>
33 #include <avahi-client/client.h>
35 # include <avahi-client/publish.h>
36 # include <avahi-client/lookup.h>
38 #include <avahi-common/simple-watch.h>
39 #include <avahi-common/malloc.h>
40 #include <avahi-common/error.h>
42 /*****************************************************************************
44 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close( vlc_object_t * );
51 set_shortname( "Bonjour" );
52 set_description( _("Bonjour services") );
53 set_category( CAT_PLAYLIST );
54 set_subcategory( SUBCAT_PLAYLIST_SD );
55 set_capability( "services_discovery", 0 );
56 set_callbacks( Open, Close );
59 /*****************************************************************************
61 *****************************************************************************/
63 struct services_discovery_sys_t
66 playlist_item_t *p_node_cat, *p_node_one;
67 playlist_t *p_playlist;
69 AvahiSimplePoll *simple_poll;
71 AvahiServiceBrowser *sb;
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;
126 if( event == AVAHI_RESOLVER_FAILURE )
128 if( event == AVAHI_RESOLVER_TIMEOUT )
132 "failed to resolve service '%s' of type '%s' in domain '%s'",
133 name, type, domain );
135 else if( event == AVAHI_RESOLVER_FOUND )
138 char *psz_uri = NULL;
139 char *psz_addr = NULL;
140 AvahiStringList *asl = NULL;
141 input_item_t *p_input = NULL;
143 msg_Dbg( p_sd, "service '%s' of type '%s' in domain '%s'",
144 name, type, domain );
146 avahi_address_snprint(a, (sizeof(a)/sizeof(a[0]))-1, address);
147 if( protocol == AVAHI_PROTO_INET6 )
148 asprintf( &psz_addr, "[%s]", a );
151 asl = avahi_string_list_find( txt, "path" );
157 if( avahi_string_list_get_pair( asl, &key, &value, &size ) == 0 &&
160 asprintf( &psz_uri, "http://%s:%d%s",
161 psz_addr != NULL ? psz_addr : a, port, value );
164 avahi_free( (void *)key );
166 avahi_free( (void *)value );
170 asprintf( &psz_uri, "http://%s:%d",
171 psz_addr != NULL ? psz_addr : a, port );
174 if( psz_addr != NULL )
175 free( (void *)psz_addr );
177 if( psz_uri != NULL )
179 p_input = input_ItemNewExt( p_sd, psz_uri, name, 0, NULL, -1 );
180 free( (void *)psz_uri );
182 if( p_input != NULL )
184 playlist_item_t *p_item;
185 p_item = playlist_NodeAddInput( p_sys->p_playlist, p_input,
187 PLAYLIST_APPEND, PLAYLIST_END,
189 p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
190 p_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
191 p_item = playlist_NodeAddInput( p_sys->p_playlist, p_input,
193 PLAYLIST_APPEND, PLAYLIST_END,
195 p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
196 p_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
200 avahi_service_resolver_free( r );
203 /*****************************************************************************
205 *****************************************************************************/
206 static void browse_callback(
207 AvahiServiceBrowser *b,
208 AvahiIfIndex interface,
209 AvahiProtocol protocol,
210 AvahiBrowserEvent event,
215 AvahiLookupResultFlags flags,
219 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
220 services_discovery_sys_t *p_sys = p_sd->p_sys;
222 if( event == AVAHI_BROWSER_NEW )
224 if( avahi_service_resolver_new( p_sys->client, interface, protocol,
225 name, type, domain, AVAHI_PROTO_UNSPEC,
229 resolve_callback, userdata ) == NULL )
231 msg_Err( p_sd, "failed to resolve service '%s': %s", name,
232 avahi_strerror( avahi_client_errno( p_sys->client ) ) );
237 /** \todo Store the input id and search it, rather than searching the items */
238 playlist_item_t *p_item;
239 p_item = playlist_ChildSearchName( p_sys->p_node_cat, name );
241 msg_Err( p_sd, "failed to find service '%s' in playlist", name );
244 playlist_DeleteFromInput( p_sys->p_playlist, p_item->p_input->i_id,
250 /*****************************************************************************
251 * Open: initialize and create stuff
252 *****************************************************************************/
253 static int Open( vlc_object_t *p_this )
255 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
256 services_discovery_sys_t *p_sys;
259 p_sd->p_sys = p_sys = (services_discovery_sys_t *)malloc(
260 sizeof( services_discovery_sys_t ) );
261 if( p_sd->p_sys == NULL )
263 msg_Err( p_sd, "out of memory" );
267 memset( p_sys, 0, sizeof(*p_sys) );
269 p_sys->simple_poll = avahi_simple_poll_new();
270 if( p_sys->simple_poll == NULL )
272 msg_Err( p_sd, "failed to create avahi simple poll" );
276 p_sys->client = avahi_client_new( avahi_simple_poll_get(p_sys->simple_poll),
280 client_callback, p_sd, &err );
281 if( p_sys->client == NULL )
283 msg_Err( p_sd, "failed to create avahi client: %s",
284 avahi_strerror( err ) );
288 p_sys->sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC,
290 "_vlc-http._tcp", NULL,
294 browse_callback, p_sd );
295 if( p_sys->sb == NULL )
297 msg_Err( p_sd, "failed to create avahi service browser" );
301 /* Create our playlist node */
302 p_sys->p_playlist = (playlist_t *)vlc_object_find( p_sd,
305 if( !p_sys->p_playlist )
307 msg_Warn( p_sd, "unable to find playlist, cancelling");
311 p_sys->p_node_cat = playlist_NodeCreate( p_sys->p_playlist, _("Bonjour"),
312 p_sys->p_playlist->p_root_category, 0 );
313 p_sys->p_node_one = playlist_NodeCreate( p_sys->p_playlist, _("Bonjour"),
314 p_sys->p_playlist->p_root_onelevel, 0 );
315 p_sys->p_node_one->p_input->i_id = p_sys->p_node_cat->p_input->i_id;
317 p_sys->p_node_one->i_flags |= PLAYLIST_RO_FLAG;
318 p_sys->p_node_cat->i_flags |= PLAYLIST_RO_FLAG;
319 p_sys->p_node_one->i_flags |= PLAYLIST_SKIP_FLAG;
320 p_sys->p_node_cat->i_flags |= PLAYLIST_SKIP_FLAG;
327 if( p_sys->p_playlist != NULL )
328 vlc_object_release( p_sys->p_playlist );
329 if( p_sys->sb != NULL )
330 avahi_service_browser_free( p_sys->sb );
331 if( p_sys->client != NULL )
332 avahi_client_free( p_sys->client );
333 if( p_sys->simple_poll != NULL )
334 avahi_simple_poll_free( p_sys->simple_poll );
336 free( (void *)p_sys );
341 /*****************************************************************************
343 *****************************************************************************/
344 static void Close( vlc_object_t *p_this )
346 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
347 services_discovery_sys_t *p_sys = p_sd->p_sys;
349 avahi_service_browser_free( p_sys->sb );
350 avahi_client_free( p_sys->client );
351 avahi_simple_poll_free( p_sys->simple_poll );
353 playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node_one, VLC_TRUE, VLC_TRUE );
354 playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node_cat, VLC_TRUE, VLC_TRUE );
355 vlc_object_release( p_sys->p_playlist );
360 /*****************************************************************************
362 *****************************************************************************/
363 static void Run( services_discovery_t *p_sd )
365 services_discovery_sys_t *p_sys = p_sd->p_sys;
367 while( !p_sd->b_die )
369 if( avahi_simple_poll_iterate( p_sys->simple_poll, 100 ) != 0 )
371 msg_Err( p_sd, "poll iterate failed" );