]> git.sesse.net Git - vlc/blob - modules/access_output/bonjour.c
6092c09000903edd679eb599b3570c62e46c0413
[vlc] / modules / access_output / bonjour.c
1 /*****************************************************************************
2  * bonjour.c
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon@nanocrew.net>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include "bonjour.h"
34
35 #ifdef HAVE_AVAHI_CLIENT
36 #include <vlc_sout.h>
37
38 #include <avahi-client/client.h>
39 #include <avahi-client/publish.h>
40 #include <avahi-client/lookup.h>
41 #include <avahi-common/alternative.h>
42 #include <avahi-common/simple-watch.h>
43 #include <avahi-common/malloc.h>
44 #include <avahi-common/error.h>
45
46 /*****************************************************************************
47  * Structures
48  *****************************************************************************/
49 typedef struct poll_thread_t
50 {
51     VLC_COMMON_MEMBERS
52
53     AvahiSimplePoll     *simple_poll;
54 } poll_thread_t;
55
56 typedef struct bonjour_t
57 {
58     vlc_object_t        *p_log;
59
60     poll_thread_t       *poll_thread;
61     AvahiSimplePoll     *simple_poll;
62     AvahiEntryGroup     *group;
63     AvahiClient         *client;
64     char                *psz_stype;
65     char                *psz_name;
66     int                 i_port;
67     char                *psz_txt;
68 } bonjour_t;
69
70 /*****************************************************************************
71  * Prototypes
72  *****************************************************************************/
73 static int create_service( bonjour_t * );
74
75 /*****************************************************************************
76  * entry_group_callback
77  *****************************************************************************/
78 static void entry_group_callback( AvahiEntryGroup *g,
79                                   AvahiEntryGroupState state,
80                                   void *userdata )
81 {
82     (void)g;
83     bonjour_t *p_sys = (bonjour_t *)userdata;
84
85     if( state == AVAHI_ENTRY_GROUP_ESTABLISHED )
86     {
87         msg_Dbg( p_sys->p_log, "service '%s' successfully established",
88                  p_sys->psz_name );
89     }
90     else if( state == AVAHI_ENTRY_GROUP_COLLISION )
91     {
92         char *n;
93
94         n = avahi_alternative_service_name( p_sys->psz_name );
95         avahi_free( p_sys->psz_name );
96         p_sys->psz_name = n;
97
98         create_service( p_sys );
99     }
100 }
101
102 /*****************************************************************************
103  * create_service
104  *****************************************************************************/
105 static int create_service( bonjour_t *p_sys )
106 {
107     int error;
108
109     if( p_sys->group == NULL )
110     {
111         p_sys->group = avahi_entry_group_new( p_sys->client,
112                                               entry_group_callback,
113                                               p_sys );
114         if( p_sys->group == NULL )
115         {
116             msg_Err( p_sys->p_log, "failed to create avahi entry group: %s",
117                      avahi_strerror( avahi_client_errno( p_sys->client ) ) );
118             return VLC_EGENERIC;
119         }
120     }
121
122     error = avahi_entry_group_add_service( p_sys->group, AVAHI_IF_UNSPEC,
123                                            AVAHI_PROTO_UNSPEC, 0, p_sys->psz_name,
124                                            p_sys->psz_stype, NULL, NULL,
125                                            p_sys->i_port,
126                                            p_sys->psz_txt, NULL );
127     if( error < 0 )
128     {
129         msg_Err( p_sys->p_log, "failed to add %s service: %s",
130                  p_sys->psz_stype, avahi_strerror( error ) );
131         return VLC_EGENERIC;
132     }
133
134     error = avahi_entry_group_commit( p_sys->group );
135     if( error < 0 )
136     {
137         msg_Err( p_sys->p_log, "failed to commit entry group: %s",
138                  avahi_strerror( error ) );
139         return VLC_EGENERIC;
140     }
141
142     return VLC_SUCCESS;
143 }
144
145 /*****************************************************************************
146  * client_callback
147  *****************************************************************************/
148 static void client_callback( AvahiClient *c,
149                              AvahiClientState state,
150                              void * userdata )
151 {
152     bonjour_t *p_sys = (bonjour_t *)userdata;
153
154     if( state == AVAHI_CLIENT_S_RUNNING )
155     {
156         p_sys->client = c;
157         create_service( p_sys );
158     }
159     else if( state == AVAHI_CLIENT_S_COLLISION )
160     {
161         if( p_sys->group != NULL )
162             avahi_entry_group_reset( p_sys->group );
163     }
164     else if( state == AVAHI_CLIENT_FAILURE &&
165               (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) )
166     {
167         msg_Err( p_sys->p_log, "avahi client disconnected" );
168         avahi_simple_poll_quit( p_sys->simple_poll );
169     }
170 }
171
172 /*****************************************************************************
173  * poll_iterate_thread
174  *****************************************************************************/
175 static void* poll_iterate_thread( vlc_object_t *p_this )
176 {
177     poll_thread_t *p_pt = (poll_thread_t*)p_this;
178     int canc = vlc_savecancel ();
179
180     while( vlc_object_alive (p_pt) )
181         if( avahi_simple_poll_iterate( p_pt->simple_poll, 100 ) != 0 )
182             break;
183
184     vlc_restorecancel (canc);
185     return NULL;
186 }
187
188 /*****************************************************************************
189  * bonjour_start_service
190  *****************************************************************************/
191 void *bonjour_start_service( vlc_object_t *p_log, const char *psz_stype,
192                              const char *psz_name, int i_port, char *psz_txt )
193 {
194     int err;
195     bonjour_t *p_sys;
196
197     p_sys = (bonjour_t *)malloc( sizeof(*p_sys) );
198     if( p_sys == NULL )
199         return NULL;
200
201     memset( p_sys, 0, sizeof(*p_sys) );
202
203     p_sys->p_log = p_log;
204
205     p_sys->i_port = i_port;
206     p_sys->psz_name = avahi_strdup( psz_name );
207     p_sys->psz_stype = avahi_strdup( psz_stype );
208     if( p_sys->psz_name == NULL || p_sys->psz_stype == NULL )
209         goto error;
210
211     if( psz_txt != NULL )
212     {
213         p_sys->psz_txt = avahi_strdup( psz_txt );
214         if( p_sys->psz_txt == NULL )
215             goto error;
216     }
217
218     p_sys->simple_poll = avahi_simple_poll_new();
219     if( p_sys->simple_poll == NULL )
220     {
221         msg_Err( p_sys->p_log, "failed to create avahi simple pool" );
222         goto error;
223     }
224
225     p_sys->client = avahi_client_new( avahi_simple_poll_get(p_sys->simple_poll),
226                                       0,
227                                       client_callback, p_sys, &err );
228     if( p_sys->client == NULL )
229     {
230         msg_Err( p_sys->p_log, "failed to create avahi client: %s",
231                  avahi_strerror( err ) );
232         goto error;
233     }
234
235     p_sys->poll_thread = vlc_object_create( p_sys->p_log,
236                                             sizeof(poll_thread_t) );
237     if( p_sys->poll_thread == NULL )
238         goto error;
239     p_sys->poll_thread->simple_poll = p_sys->simple_poll;
240
241     if( vlc_thread_create( p_sys->poll_thread, "Avahi Poll Iterate Thread",
242                            poll_iterate_thread,
243                            VLC_THREAD_PRIORITY_HIGHEST, false ) )
244     {
245         msg_Err( p_sys->p_log, "failed to create poll iterate thread" );
246         goto error;
247     }
248
249     return (void *)p_sys;
250
251 error:
252     if( p_sys->poll_thread != NULL )
253         vlc_object_release( p_sys->poll_thread );
254     if( p_sys->client != NULL )
255         avahi_client_free( p_sys->client );
256     if( p_sys->simple_poll != NULL )
257         avahi_simple_poll_free( p_sys->simple_poll );
258     if( p_sys->psz_stype != NULL )
259         avahi_free( p_sys->psz_stype );
260     if( p_sys->psz_name != NULL )
261         avahi_free( p_sys->psz_name );
262     if( p_sys->psz_txt != NULL )
263         avahi_free( p_sys->psz_txt );
264
265     free( p_sys );
266
267     return NULL;
268 }
269
270 /*****************************************************************************
271  * bonjour_stop_service
272  *****************************************************************************/
273 void bonjour_stop_service( void *_p_sys )
274 {
275     bonjour_t *p_sys = (bonjour_t *)_p_sys;
276
277     vlc_object_kill( p_sys->poll_thread );
278     vlc_thread_join( p_sys->poll_thread );
279     vlc_object_release( p_sys->poll_thread );
280
281     if( p_sys->group != NULL )
282         avahi_entry_group_free( p_sys->group );
283
284     avahi_client_free( p_sys->client );
285     avahi_simple_poll_free( p_sys->simple_poll );
286
287     if( p_sys->psz_name != NULL )
288         avahi_free( p_sys->psz_name );
289
290     if( p_sys->psz_txt != NULL )
291         avahi_free( p_sys->psz_txt );
292
293     avahi_free( p_sys->psz_stype );
294
295     free( _p_sys );
296 }
297
298 #endif /* HAVE_AVAHI_CLIENT */