1 /*****************************************************************************
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 *****************************************************************************/
31 #ifdef HAVE_AVAHI_CLIENT
34 #include <avahi-client/client.h>
36 # include <avahi-client/publish.h>
37 # include <avahi-client/lookup.h>
39 #include <avahi-common/alternative.h>
40 #include <avahi-common/simple-watch.h>
41 #include <avahi-common/malloc.h>
42 #include <avahi-common/error.h>
44 /*****************************************************************************
46 *****************************************************************************/
47 typedef struct poll_thread_t
51 AvahiSimplePoll *simple_poll;
54 typedef struct bonjour_t
58 poll_thread_t *poll_thread;
59 AvahiSimplePoll *simple_poll;
60 AvahiEntryGroup *group;
68 /*****************************************************************************
70 *****************************************************************************/
71 static int create_service( bonjour_t * );
73 /*****************************************************************************
74 * entry_group_callback
75 *****************************************************************************/
76 static void entry_group_callback( AvahiEntryGroup *g,
77 AvahiEntryGroupState state,
80 bonjour_t *p_sys = (bonjour_t *)userdata;
82 if( state == AVAHI_ENTRY_GROUP_ESTABLISHED )
84 msg_Dbg( p_sys->p_log, "service '%s' successfully established",
87 else if( state == AVAHI_ENTRY_GROUP_COLLISION )
91 n = avahi_alternative_service_name( p_sys->psz_name );
92 avahi_free( p_sys->psz_name );
95 create_service( p_sys );
99 /*****************************************************************************
101 *****************************************************************************/
102 static int create_service( bonjour_t *p_sys )
106 if( p_sys->group == NULL )
108 p_sys->group = avahi_entry_group_new( p_sys->client,
109 entry_group_callback,
111 if( p_sys->group == NULL )
113 msg_Err( p_sys->p_log, "failed to create avahi entry group: %s",
114 avahi_strerror( avahi_client_errno( p_sys->client ) ) );
119 error = avahi_entry_group_add_service( p_sys->group, AVAHI_IF_UNSPEC,
121 AVAHI_PROTO_UNSPEC, 0, p_sys->psz_name,
123 AVAHI_PROTO_UNSPEC, p_sys->psz_name,
125 p_sys->psz_stype, NULL, NULL,
127 p_sys->psz_txt, NULL );
130 msg_Err( p_sys->p_log, "failed to add %s service: %s",
131 p_sys->psz_stype, avahi_strerror( error ) );
135 error = avahi_entry_group_commit( p_sys->group );
138 msg_Err( p_sys->p_log, "failed to commit entry group: %s",
139 avahi_strerror( error ) );
146 /*****************************************************************************
148 *****************************************************************************/
149 static void client_callback( AvahiClient *c,
150 AvahiClientState state,
153 bonjour_t *p_sys = (bonjour_t *)userdata;
155 if( state == AVAHI_CLIENT_S_RUNNING )
158 create_service( p_sys );
160 else if( state == AVAHI_CLIENT_S_COLLISION )
162 if( p_sys->group != NULL )
163 avahi_entry_group_reset( p_sys->group );
166 else if( state == AVAHI_CLIENT_FAILURE &&
167 (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) )
169 else if( state == AVAHI_CLIENT_DISCONNECTED )
172 msg_Err( p_sys->p_log, "avahi client disconnected" );
173 avahi_simple_poll_quit( p_sys->simple_poll );
177 /*****************************************************************************
178 * poll_iterate_thread
179 *****************************************************************************/
180 static void poll_iterate_thread( poll_thread_t *p_pt )
182 vlc_thread_ready( p_pt );
184 while( !p_pt->b_die )
185 if( avahi_simple_poll_iterate( p_pt->simple_poll, 100 ) != 0 )
189 /*****************************************************************************
190 * bonjour_start_service
191 *****************************************************************************/
192 void *bonjour_start_service( vlc_object_t *p_log, char *psz_stype,
193 char *psz_name, int i_port, char *psz_txt )
198 p_sys = (bonjour_t *)malloc( sizeof(*p_sys) );
201 msg_Err( p_log, "out of memory" );
205 memset( p_sys, 0, sizeof(*p_sys) );
207 p_sys->p_log = p_log;
209 p_sys->i_port = i_port;
210 p_sys->psz_name = avahi_strdup( psz_name );
211 p_sys->psz_stype = avahi_strdup( psz_stype );
212 if( p_sys->psz_name == NULL || p_sys->psz_stype == NULL )
214 msg_Err( p_sys->p_log, "out of memory" );
218 if( psz_txt != NULL )
220 p_sys->psz_txt = avahi_strdup( psz_txt );
221 if( p_sys->psz_txt == NULL )
223 msg_Err( p_sys->p_log, "out of memory" );
228 p_sys->simple_poll = avahi_simple_poll_new();
229 if( p_sys->simple_poll == NULL )
231 msg_Err( p_sys->p_log, "failed to create avahi simple pool" );
235 p_sys->client = avahi_client_new( avahi_simple_poll_get(p_sys->simple_poll),
239 client_callback, p_sys, &err );
240 if( p_sys->client == NULL )
242 msg_Err( p_sys->p_log, "failed to create avahi client: %s",
243 avahi_strerror( err ) );
247 p_sys->poll_thread = vlc_object_create( p_sys->p_log,
248 sizeof(poll_thread_t) );
249 if( p_sys->poll_thread == NULL )
251 msg_Err( p_sys->p_log, "out of memory" );
254 p_sys->poll_thread->simple_poll = p_sys->simple_poll;
256 if( vlc_thread_create( p_sys->poll_thread, "Avahi Poll Iterate Thread",
258 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
260 msg_Err( p_sys->p_log, "failed to create poll iterate thread" );
264 return (void *)p_sys;
267 if( p_sys->poll_thread != NULL )
268 vlc_object_destroy( p_sys->poll_thread );
269 if( p_sys->client != NULL )
270 avahi_client_free( p_sys->client );
271 if( p_sys->simple_poll != NULL )
272 avahi_simple_poll_free( p_sys->simple_poll );
273 if( p_sys->psz_stype != NULL )
274 avahi_free( p_sys->psz_stype );
275 if( p_sys->psz_name != NULL )
276 avahi_free( p_sys->psz_name );
277 if( p_sys->psz_txt != NULL )
278 avahi_free( p_sys->psz_txt );
280 free( (void *)p_sys );
285 /*****************************************************************************
286 * bonjour_stop_service
287 *****************************************************************************/
288 void bonjour_stop_service( void *_p_sys )
290 bonjour_t *p_sys = (bonjour_t *)_p_sys;
292 if( p_sys->poll_thread->b_thread )
294 p_sys->poll_thread->b_die = 1;
295 vlc_thread_join( p_sys->poll_thread );
298 vlc_object_destroy( p_sys->poll_thread );
300 if( p_sys->group != NULL )
301 avahi_entry_group_free( p_sys->group );
303 avahi_client_free( p_sys->client );
304 avahi_simple_poll_free( p_sys->simple_poll );
306 if( p_sys->psz_name != NULL )
307 avahi_free( p_sys->psz_name );
309 if( p_sys->psz_txt != NULL )
310 avahi_free( p_sys->psz_txt );
312 avahi_free( p_sys->psz_stype );
317 #endif /* HAVE_AVAHI_CLIENT */