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 *****************************************************************************/
32 #include <vlc_common.h>
35 #ifdef HAVE_AVAHI_CLIENT
38 #include <avahi-client/client.h>
40 # include <avahi-client/publish.h>
41 # include <avahi-client/lookup.h>
43 #include <avahi-common/alternative.h>
44 #include <avahi-common/simple-watch.h>
45 #include <avahi-common/malloc.h>
46 #include <avahi-common/error.h>
48 /*****************************************************************************
50 *****************************************************************************/
51 typedef struct poll_thread_t
55 AvahiSimplePoll *simple_poll;
58 typedef struct bonjour_t
62 poll_thread_t *poll_thread;
63 AvahiSimplePoll *simple_poll;
64 AvahiEntryGroup *group;
72 /*****************************************************************************
74 *****************************************************************************/
75 static int create_service( bonjour_t * );
77 /*****************************************************************************
78 * entry_group_callback
79 *****************************************************************************/
80 static void entry_group_callback( AvahiEntryGroup *g,
81 AvahiEntryGroupState state,
84 bonjour_t *p_sys = (bonjour_t *)userdata;
86 if( state == AVAHI_ENTRY_GROUP_ESTABLISHED )
88 msg_Dbg( p_sys->p_log, "service '%s' successfully established",
91 else if( state == AVAHI_ENTRY_GROUP_COLLISION )
95 n = avahi_alternative_service_name( p_sys->psz_name );
96 avahi_free( p_sys->psz_name );
99 create_service( p_sys );
103 /*****************************************************************************
105 *****************************************************************************/
106 static int create_service( bonjour_t *p_sys )
110 if( p_sys->group == NULL )
112 p_sys->group = avahi_entry_group_new( p_sys->client,
113 entry_group_callback,
115 if( p_sys->group == NULL )
117 msg_Err( p_sys->p_log, "failed to create avahi entry group: %s",
118 avahi_strerror( avahi_client_errno( p_sys->client ) ) );
123 error = avahi_entry_group_add_service( p_sys->group, AVAHI_IF_UNSPEC,
125 AVAHI_PROTO_UNSPEC, 0, p_sys->psz_name,
127 AVAHI_PROTO_UNSPEC, p_sys->psz_name,
129 p_sys->psz_stype, NULL, NULL,
131 p_sys->psz_txt, NULL );
134 msg_Err( p_sys->p_log, "failed to add %s service: %s",
135 p_sys->psz_stype, avahi_strerror( error ) );
139 error = avahi_entry_group_commit( p_sys->group );
142 msg_Err( p_sys->p_log, "failed to commit entry group: %s",
143 avahi_strerror( error ) );
150 /*****************************************************************************
152 *****************************************************************************/
153 static void client_callback( AvahiClient *c,
154 AvahiClientState state,
157 bonjour_t *p_sys = (bonjour_t *)userdata;
159 if( state == AVAHI_CLIENT_S_RUNNING )
162 create_service( p_sys );
164 else if( state == AVAHI_CLIENT_S_COLLISION )
166 if( p_sys->group != NULL )
167 avahi_entry_group_reset( p_sys->group );
170 else if( state == AVAHI_CLIENT_FAILURE &&
171 (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) )
173 else if( state == AVAHI_CLIENT_DISCONNECTED )
176 msg_Err( p_sys->p_log, "avahi client disconnected" );
177 avahi_simple_poll_quit( p_sys->simple_poll );
181 /*****************************************************************************
182 * poll_iterate_thread
183 *****************************************************************************/
184 static void poll_iterate_thread( poll_thread_t *p_pt )
186 vlc_thread_ready( p_pt );
188 while( vlc_object_alive (p_pt) )
189 if( avahi_simple_poll_iterate( p_pt->simple_poll, 100 ) != 0 )
193 /*****************************************************************************
194 * bonjour_start_service
195 *****************************************************************************/
196 void *bonjour_start_service( vlc_object_t *p_log, const char *psz_stype,
197 const char *psz_name, int i_port, char *psz_txt )
202 p_sys = (bonjour_t *)malloc( sizeof(*p_sys) );
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 )
216 if( psz_txt != NULL )
218 p_sys->psz_txt = avahi_strdup( psz_txt );
219 if( p_sys->psz_txt == NULL )
223 p_sys->simple_poll = avahi_simple_poll_new();
224 if( p_sys->simple_poll == NULL )
226 msg_Err( p_sys->p_log, "failed to create avahi simple pool" );
230 p_sys->client = avahi_client_new( avahi_simple_poll_get(p_sys->simple_poll),
234 client_callback, p_sys, &err );
235 if( p_sys->client == NULL )
237 msg_Err( p_sys->p_log, "failed to create avahi client: %s",
238 avahi_strerror( err ) );
242 p_sys->poll_thread = vlc_object_create( p_sys->p_log,
243 sizeof(poll_thread_t) );
244 if( p_sys->poll_thread == NULL )
246 p_sys->poll_thread->simple_poll = p_sys->simple_poll;
248 if( vlc_thread_create( p_sys->poll_thread, "Avahi Poll Iterate Thread",
250 VLC_THREAD_PRIORITY_HIGHEST, false ) )
252 msg_Err( p_sys->p_log, "failed to create poll iterate thread" );
256 return (void *)p_sys;
259 if( p_sys->poll_thread != NULL )
260 vlc_object_release( p_sys->poll_thread );
261 if( p_sys->client != NULL )
262 avahi_client_free( p_sys->client );
263 if( p_sys->simple_poll != NULL )
264 avahi_simple_poll_free( p_sys->simple_poll );
265 if( p_sys->psz_stype != NULL )
266 avahi_free( p_sys->psz_stype );
267 if( p_sys->psz_name != NULL )
268 avahi_free( p_sys->psz_name );
269 if( p_sys->psz_txt != NULL )
270 avahi_free( p_sys->psz_txt );
272 free( (void *)p_sys );
277 /*****************************************************************************
278 * bonjour_stop_service
279 *****************************************************************************/
280 void bonjour_stop_service( void *_p_sys )
282 bonjour_t *p_sys = (bonjour_t *)_p_sys;
284 vlc_object_kill( p_sys->poll_thread );
285 vlc_thread_join( p_sys->poll_thread );
286 vlc_object_release( p_sys->poll_thread );
288 if( p_sys->group != NULL )
289 avahi_entry_group_free( p_sys->group );
291 avahi_client_free( p_sys->client );
292 avahi_simple_poll_free( p_sys->simple_poll );
294 if( p_sys->psz_name != NULL )
295 avahi_free( p_sys->psz_name );
297 if( p_sys->psz_txt != NULL )
298 avahi_free( p_sys->psz_txt );
300 avahi_free( p_sys->psz_stype );
305 #endif /* HAVE_AVAHI_CLIENT */