]> git.sesse.net Git - vlc/blob - modules/services_discovery/bonjour.c
A bit of headers cleanup
[vlc] / modules / services_discovery / bonjour.c
1 /*****************************************************************************
2  * bonjour.c: Bonjour services discovery module
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  * Includes
26  *****************************************************************************/
27 #define _GNU_SOURCE
28 #include <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc_playlist.h>
32
33 #include <avahi-client/client.h>
34 #ifdef HAVE_AVAHI_06
35 # include <avahi-client/publish.h>
36 # include <avahi-client/lookup.h>
37 #endif
38 #include <avahi-common/simple-watch.h>
39 #include <avahi-common/malloc.h>
40 #include <avahi-common/error.h>
41
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45
46 /* Callbacks */
47     static int  Open ( vlc_object_t * );
48     static void Close( vlc_object_t * );
49
50 vlc_module_begin();
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 );
57 vlc_module_end();
58
59 /*****************************************************************************
60  * Local structures
61  *****************************************************************************/
62
63 struct services_discovery_sys_t
64 {
65     /* playlist node */
66     playlist_item_t     *p_node_cat, *p_node_one;
67     playlist_t          *p_playlist;
68
69     AvahiSimplePoll     *simple_poll;
70     AvahiClient         *client;
71     AvahiServiceBrowser *sb;
72 };
73
74 /*****************************************************************************
75  * Local prototypes
76  *****************************************************************************/
77
78 /* Main functions */
79     static void Run    ( services_discovery_t *p_intf );
80
81 /*****************************************************************************
82  * client_callback
83  *****************************************************************************/
84 static void client_callback( AvahiClient *c, AvahiClientState state,
85                              void * userdata )
86 {
87     services_discovery_t *p_sd = ( services_discovery_t* )userdata;
88     services_discovery_sys_t *p_sys = p_sd->p_sys;
89
90 #ifdef HAVE_AVAHI_06
91     if( state == AVAHI_CLIENT_FAILURE &&
92         (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) )
93 #else
94     if( state == AVAHI_CLIENT_DISCONNECTED )
95 #endif
96     {
97         msg_Err( p_sd, "avahi client disconnected" );
98         avahi_simple_poll_quit( p_sys->simple_poll );
99     }
100 }
101
102 /*****************************************************************************
103  * resolve_callback
104  *****************************************************************************/
105 static void resolve_callback(
106     AvahiServiceResolver *r,
107     AvahiIfIndex interface,
108     AvahiProtocol protocol,
109     AvahiResolverEvent event,
110     const char *name,
111     const char *type,
112     const char *domain,
113     const char *host_name,
114     const AvahiAddress *address,
115     uint16_t port,
116     AvahiStringList *txt,
117 #ifdef HAVE_AVAHI_06
118     AvahiLookupResultFlags flags,
119 #endif
120     void* userdata )
121 {
122     services_discovery_t *p_sd = ( services_discovery_t* )userdata;
123     services_discovery_sys_t *p_sys = p_sd->p_sys;
124
125 #ifdef HAVE_AVAHI_06
126     if( event == AVAHI_RESOLVER_FAILURE )
127 #else
128     if( event == AVAHI_RESOLVER_TIMEOUT )
129 #endif
130     {
131         msg_Err( p_sd,
132                  "failed to resolve service '%s' of type '%s' in domain '%s'",
133                  name, type, domain );
134     }
135     else if( event == AVAHI_RESOLVER_FOUND )
136     {
137         char a[128];
138         char *psz_uri = NULL;
139         char *psz_addr = NULL;
140         AvahiStringList *asl = NULL;
141         input_item_t *p_input = NULL;
142
143         msg_Dbg( p_sd, "service '%s' of type '%s' in domain '%s'",
144                  name, type, domain );
145
146         avahi_address_snprint(a, (sizeof(a)/sizeof(a[0]))-1, address);
147         if( protocol == AVAHI_PROTO_INET6 )
148             asprintf( &psz_addr, "[%s]", a );
149
150         if( txt != NULL )
151             asl = avahi_string_list_find( txt, "path" );
152         if( asl != NULL )
153         {
154             size_t size;
155             char *key = NULL;
156             char *value = NULL;
157             if( avahi_string_list_get_pair( asl, &key, &value, &size ) == 0 &&
158                 value != NULL )
159             {
160                 asprintf( &psz_uri, "http://%s:%d%s",
161                           psz_addr != NULL ? psz_addr : a, port, value );
162             }
163             if( key != NULL )
164                 avahi_free( (void *)key );
165             if( value != NULL )
166                 avahi_free( (void *)value );
167         }
168         else
169         {
170             asprintf( &psz_uri, "http://%s:%d",
171                       psz_addr != NULL ? psz_addr : a, port );
172         }
173
174         if( psz_addr != NULL )
175             free( (void *)psz_addr );
176
177         if( psz_uri != NULL )
178         {
179             p_input = input_ItemNewExt( p_sd, psz_uri, name, 0, NULL, -1 );
180             free( (void *)psz_uri );
181         }
182         if( p_input != NULL )
183         {
184             playlist_item_t *p_item;
185             p_item = playlist_NodeAddInput( p_sys->p_playlist, p_input,
186                                             p_sys->p_node_cat,
187                                             PLAYLIST_APPEND, PLAYLIST_END );
188             p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
189             p_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
190             p_item = playlist_NodeAddInput( p_sys->p_playlist, p_input,
191                                             p_sys->p_node_one,
192                                             PLAYLIST_APPEND, PLAYLIST_END );
193             p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
194             p_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
195        }
196     }
197
198     avahi_service_resolver_free( r );
199 }
200
201 /*****************************************************************************
202  * browser_callback
203  *****************************************************************************/
204 static void browse_callback(
205     AvahiServiceBrowser *b,
206     AvahiIfIndex interface,
207     AvahiProtocol protocol,
208     AvahiBrowserEvent event,
209     const char *name,
210     const char *type,
211     const char *domain,
212 #ifdef HAVE_AVAHI_06
213     AvahiLookupResultFlags flags,
214 #endif
215     void* userdata )
216 {
217     services_discovery_t *p_sd = ( services_discovery_t* )userdata;
218     services_discovery_sys_t *p_sys = p_sd->p_sys;
219
220     if( event == AVAHI_BROWSER_NEW )
221     {
222         if( avahi_service_resolver_new( p_sys->client, interface, protocol,
223                                         name, type, domain, AVAHI_PROTO_UNSPEC,
224 #ifdef HAVE_AVAHI_06
225                                         0,
226 #endif
227                                         resolve_callback, userdata ) == NULL )
228         {
229             msg_Err( p_sd, "failed to resolve service '%s': %s", name,
230                      avahi_strerror( avahi_client_errno( p_sys->client ) ) );
231         }
232     }
233     else
234     {
235         /** \todo Store the input id and search it, rather than searching the items */
236         playlist_item_t *p_item;
237         p_item = playlist_ChildSearchName( p_sys->p_node_cat, name );
238         if( p_item == NULL )
239             msg_Err( p_sd, "failed to find service '%s' in playlist", name );
240         else
241         {
242             playlist_DeleteFromInput( p_sys->p_playlist, p_item->p_input->i_id,
243                                       VLC_FALSE );
244         }
245     }
246 }
247
248 /*****************************************************************************
249  * Open: initialize and create stuff
250  *****************************************************************************/
251 static int Open( vlc_object_t *p_this )
252 {
253     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
254     services_discovery_sys_t *p_sys;
255     int err;
256
257     p_sd->p_sys = p_sys = (services_discovery_sys_t *)malloc(
258         sizeof( services_discovery_sys_t ) );
259     if( p_sd->p_sys == NULL )
260     {
261         msg_Err( p_sd, "out of memory" );
262         return VLC_EGENERIC;
263     }
264
265     memset( p_sys, 0, sizeof(*p_sys) );
266
267     p_sys->simple_poll = avahi_simple_poll_new();
268     if( p_sys->simple_poll == NULL )
269     {
270         msg_Err( p_sd, "failed to create avahi simple poll" );
271         goto error;
272     }
273
274     p_sys->client = avahi_client_new( avahi_simple_poll_get(p_sys->simple_poll),
275 #ifdef HAVE_AVAHI_06
276                                       0,
277 #endif
278                                       client_callback, p_sd, &err );
279     if( p_sys->client == NULL )
280     {
281         msg_Err( p_sd, "failed to create avahi client: %s",
282                  avahi_strerror( err ) );
283         goto error;
284     }
285
286     p_sys->sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC,
287                                            AVAHI_PROTO_UNSPEC,
288                                            "_vlc-http._tcp", NULL,
289 #ifdef HAVE_AVAHI_06
290                                            0,
291 #endif
292                                            browse_callback, p_sd );
293     if( p_sys->sb == NULL )
294     {
295         msg_Err( p_sd, "failed to create avahi service browser" );
296         goto error;
297     }
298
299     /* Create our playlist node */
300     p_sys->p_playlist = (playlist_t *)vlc_object_find( p_sd,
301                                                        VLC_OBJECT_PLAYLIST,
302                                                        FIND_ANYWHERE );
303     if( !p_sys->p_playlist )
304     {
305         msg_Warn( p_sd, "unable to find playlist, cancelling");
306         goto error;
307     }
308
309     p_sys->p_node_cat = playlist_NodeCreate( p_sys->p_playlist, _("Bonjour"),
310                                              p_sys->p_playlist->p_root_category );
311     p_sys->p_node_one = playlist_NodeCreate( p_sys->p_playlist, _("Bonjour"),
312                                              p_sys->p_playlist->p_root_onelevel );
313     p_sys->p_node_one->p_input->i_id = p_sys->p_node_cat->p_input->i_id;
314
315     p_sys->p_node_one->i_flags |= PLAYLIST_RO_FLAG;
316     p_sys->p_node_cat->i_flags |= PLAYLIST_RO_FLAG;
317     p_sys->p_node_one->i_flags |= PLAYLIST_SKIP_FLAG;
318     p_sys->p_node_cat->i_flags |= PLAYLIST_SKIP_FLAG;
319
320     p_sd->pf_run = Run;
321
322     return VLC_SUCCESS;
323
324 error:
325     if( p_sys->p_playlist != NULL )
326         vlc_object_release( p_sys->p_playlist );
327     if( p_sys->sb != NULL )
328         avahi_service_browser_free( p_sys->sb );
329     if( p_sys->client != NULL )
330         avahi_client_free( p_sys->client );
331     if( p_sys->simple_poll != NULL )
332         avahi_simple_poll_free( p_sys->simple_poll );
333
334     free( (void *)p_sys );
335
336     return VLC_EGENERIC;
337 }
338
339 /*****************************************************************************
340  * Close: cleanup
341  *****************************************************************************/
342 static void Close( vlc_object_t *p_this )
343 {
344     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
345     services_discovery_sys_t *p_sys = p_sd->p_sys;
346
347     avahi_service_browser_free( p_sys->sb );
348     avahi_client_free( p_sys->client );
349     avahi_simple_poll_free( p_sys->simple_poll );
350
351     playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node_one, VLC_TRUE, VLC_TRUE );
352     playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node_cat, VLC_TRUE, VLC_TRUE );
353     vlc_object_release( p_sys->p_playlist );
354
355     free( p_sys );
356 }
357
358 /*****************************************************************************
359  * Run: main thread
360  *****************************************************************************/
361 static void Run( services_discovery_t *p_sd )
362 {
363     services_discovery_sys_t *p_sys = p_sd->p_sys;
364
365     while( !p_sd->b_die )
366     {
367         if( avahi_simple_poll_iterate( p_sys->simple_poll, 100 ) != 0 )
368         {
369             msg_Err( p_sd, "poll iterate failed" );
370             break;
371         }
372     }
373 }