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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #ifdef HAVE_AVAHI_CLIENT
35 #include <avahi-client/client.h>
37 # include <avahi-client/publish.h>
38 # include <avahi-client/lookup.h>
40 #include <avahi-common/alternative.h>
41 #include <avahi-common/simple-watch.h>
42 #include <avahi-common/malloc.h>
43 #include <avahi-common/error.h>
45 /*****************************************************************************
47 *****************************************************************************/
48 typedef struct poll_thread_t
52 AvahiSimplePoll *simple_poll;
55 typedef struct bonjour_t
59 poll_thread_t *poll_thread;
60 AvahiSimplePoll *simple_poll;
61 AvahiEntryGroup *group;
69 /*****************************************************************************
71 *****************************************************************************/
72 static int create_service( bonjour_t * );
74 /*****************************************************************************
75 * entry_group_callback
76 *****************************************************************************/
77 static void entry_group_callback( AvahiEntryGroup *g,
78 AvahiEntryGroupState state,
81 bonjour_t *p_sys = (bonjour_t *)userdata;
83 if( state == AVAHI_ENTRY_GROUP_ESTABLISHED )
85 msg_Dbg( p_sys->p_log, "service '%s' successfully established",
88 else if( state == AVAHI_ENTRY_GROUP_COLLISION )
92 n = avahi_alternative_service_name( p_sys->psz_name );
93 avahi_free( p_sys->psz_name );
96 create_service( p_sys );
100 /*****************************************************************************
102 *****************************************************************************/
103 static int create_service( bonjour_t *p_sys )
107 if( p_sys->group == NULL )
109 p_sys->group = avahi_entry_group_new( p_sys->client,
110 entry_group_callback,
112 if( p_sys->group == NULL )
114 msg_Err( p_sys->p_log, "failed to create avahi entry group: %s",
115 avahi_strerror( avahi_client_errno( p_sys->client ) ) );
120 error = avahi_entry_group_add_service( p_sys->group, AVAHI_IF_UNSPEC,
122 AVAHI_PROTO_UNSPEC, 0, p_sys->psz_name,
124 AVAHI_PROTO_UNSPEC, p_sys->psz_name,
126 p_sys->psz_stype, NULL, NULL,
128 p_sys->psz_txt, NULL );
131 msg_Err( p_sys->p_log, "failed to add %s service: %s",
132 p_sys->psz_stype, avahi_strerror( error ) );
136 error = avahi_entry_group_commit( p_sys->group );
139 msg_Err( p_sys->p_log, "failed to commit entry group: %s",
140 avahi_strerror( error ) );
147 /*****************************************************************************
149 *****************************************************************************/
150 static void client_callback( AvahiClient *c,
151 AvahiClientState state,
154 bonjour_t *p_sys = (bonjour_t *)userdata;
156 if( state == AVAHI_CLIENT_S_RUNNING )
159 create_service( p_sys );
161 else if( state == AVAHI_CLIENT_S_COLLISION )
163 if( p_sys->group != NULL )
164 avahi_entry_group_reset( p_sys->group );
167 else if( state == AVAHI_CLIENT_FAILURE &&
168 (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) )
170 else if( state == AVAHI_CLIENT_DISCONNECTED )
173 msg_Err( p_sys->p_log, "avahi client disconnected" );
174 avahi_simple_poll_quit( p_sys->simple_poll );
178 /*****************************************************************************
179 * poll_iterate_thread
180 *****************************************************************************/
181 static void poll_iterate_thread( poll_thread_t *p_pt )
183 vlc_thread_ready( p_pt );
185 while( !p_pt->b_die )
186 if( avahi_simple_poll_iterate( p_pt->simple_poll, 100 ) != 0 )
190 /*****************************************************************************
191 * bonjour_start_service
192 *****************************************************************************/
193 void *bonjour_start_service( vlc_object_t *p_log, char *psz_stype,
194 char *psz_name, int i_port, char *psz_txt )
199 p_sys = (bonjour_t *)malloc( sizeof(*p_sys) );
202 msg_Err( p_log, "out of memory" );
206 memset( p_sys, 0, sizeof(*p_sys) );
208 p_sys->p_log = p_log;
210 p_sys->i_port = i_port;
211 p_sys->psz_name = avahi_strdup( psz_name );
212 p_sys->psz_stype = avahi_strdup( psz_stype );
213 if( p_sys->psz_name == NULL || p_sys->psz_stype == NULL )
215 msg_Err( p_sys->p_log, "out of memory" );
219 if( psz_txt != NULL )
221 p_sys->psz_txt = avahi_strdup( psz_txt );
222 if( p_sys->psz_txt == NULL )
224 msg_Err( p_sys->p_log, "out of memory" );
229 p_sys->simple_poll = avahi_simple_poll_new();
230 if( p_sys->simple_poll == NULL )
232 msg_Err( p_sys->p_log, "failed to create avahi simple pool" );
236 p_sys->client = avahi_client_new( avahi_simple_poll_get(p_sys->simple_poll),
240 client_callback, p_sys, &err );
241 if( p_sys->client == NULL )
243 msg_Err( p_sys->p_log, "failed to create avahi client: %s",
244 avahi_strerror( err ) );
248 p_sys->poll_thread = vlc_object_create( p_sys->p_log,
249 sizeof(poll_thread_t) );
250 if( p_sys->poll_thread == NULL )
252 msg_Err( p_sys->p_log, "out of memory" );
255 p_sys->poll_thread->simple_poll = p_sys->simple_poll;
257 if( vlc_thread_create( p_sys->poll_thread, "Avahi Poll Iterate Thread",
259 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
261 msg_Err( p_sys->p_log, "failed to create poll iterate thread" );
265 return (void *)p_sys;
268 if( p_sys->poll_thread != NULL )
269 vlc_object_destroy( p_sys->poll_thread );
270 if( p_sys->client != NULL )
271 avahi_client_free( p_sys->client );
272 if( p_sys->simple_poll != NULL )
273 avahi_simple_poll_free( p_sys->simple_poll );
274 if( p_sys->psz_stype != NULL )
275 avahi_free( p_sys->psz_stype );
276 if( p_sys->psz_name != NULL )
277 avahi_free( p_sys->psz_name );
278 if( p_sys->psz_txt != NULL )
279 avahi_free( p_sys->psz_txt );
281 free( (void *)p_sys );
286 /*****************************************************************************
287 * bonjour_stop_service
288 *****************************************************************************/
289 void bonjour_stop_service( void *_p_sys )
291 bonjour_t *p_sys = (bonjour_t *)_p_sys;
293 if( p_sys->poll_thread->b_thread )
295 p_sys->poll_thread->b_die = 1;
296 vlc_thread_join( p_sys->poll_thread );
299 vlc_object_destroy( p_sys->poll_thread );
301 if( p_sys->group != NULL )
302 avahi_entry_group_free( p_sys->group );
304 avahi_client_free( p_sys->client );
305 avahi_simple_poll_free( p_sys->simple_poll );
307 if( p_sys->psz_name != NULL )
308 avahi_free( p_sys->psz_name );
310 if( p_sys->psz_txt != NULL )
311 avahi_free( p_sys->psz_txt );
313 avahi_free( p_sys->psz_stype );
318 #endif /* HAVE_AVAHI_CLIENT */