]> git.sesse.net Git - vlc/blob - modules/services_discovery/bonjour.c
Fix a bunch of gcc warnings
[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/intf.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_LockDeleteAllFromInput( p_sys->p_playlist,
243                                               p_item->p_input->i_id );
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     playlist_view_t *p_view;
256     vlc_value_t val;
257     int err;
258
259     p_sd->p_sys = p_sys = (services_discovery_sys_t *)malloc(
260         sizeof( services_discovery_sys_t ) );
261     if( p_sd->p_sys == NULL )
262     {
263         msg_Err( p_sd, "out of memory" );
264         return VLC_EGENERIC;
265     }
266
267     memset( p_sys, 0, sizeof(*p_sys) );
268
269     p_sys->simple_poll = avahi_simple_poll_new();
270     if( p_sys->simple_poll == NULL )
271     {
272         msg_Err( p_sd, "failed to create avahi simple poll" );
273         goto error;
274     }
275
276     p_sys->client = avahi_client_new( avahi_simple_poll_get(p_sys->simple_poll),
277 #ifdef HAVE_AVAHI_06
278                                       0,
279 #endif
280                                       client_callback, p_sd, &err );
281     if( p_sys->client == NULL )
282     {
283         msg_Err( p_sd, "failed to create avahi client: %s",
284                  avahi_strerror( err ) );
285         goto error;
286     }
287
288     p_sys->sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC,
289                                            AVAHI_PROTO_UNSPEC,
290                                            "_vlc-http._tcp", NULL,
291 #ifdef HAVE_AVAHI_06
292                                            0,
293 #endif
294                                            browse_callback, p_sd );
295     if( p_sys->sb == NULL )
296     {
297         msg_Err( p_sd, "failed to create avahi service browser" );
298         goto error;
299     }
300
301     /* Create our playlist node */
302     p_sys->p_playlist = (playlist_t *)vlc_object_find( p_sd,
303                                                        VLC_OBJECT_PLAYLIST,
304                                                        FIND_ANYWHERE );
305     if( !p_sys->p_playlist )
306     {
307         msg_Warn( p_sd, "unable to find playlist, cancelling");
308         goto error;
309     }
310
311     playlist_NodesCreate( p_sys->p_playlist, _("Bonjour"),
312                               &p_sys->p_node_cat,&p_sys->p_node_one,
313                               VLC_TRUE );
314     p_sd->pf_run = Run;
315
316     return VLC_SUCCESS;
317
318 error:
319     if( p_sys->p_playlist != NULL )
320         vlc_object_release( p_sys->p_playlist );
321     if( p_sys->sb != NULL )
322         avahi_service_browser_free( p_sys->sb );
323     if( p_sys->client != NULL )
324         avahi_client_free( p_sys->client );
325     if( p_sys->simple_poll != NULL )
326         avahi_simple_poll_free( p_sys->simple_poll );
327
328     free( (void *)p_sys );
329
330     return VLC_EGENERIC;
331 }
332
333 /*****************************************************************************
334  * Close: cleanup
335  *****************************************************************************/
336 static void Close( vlc_object_t *p_this )
337 {
338     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
339     services_discovery_sys_t *p_sys = p_sd->p_sys;
340
341     avahi_service_browser_free( p_sys->sb );
342     avahi_client_free( p_sys->client );
343     avahi_simple_poll_free( p_sys->simple_poll );
344
345     playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node_one, VLC_TRUE, VLC_TRUE );
346     playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node_cat, VLC_TRUE, VLC_TRUE );
347     vlc_object_release( p_sys->p_playlist );
348
349     free( p_sys );
350 }
351
352 /*****************************************************************************
353  * Run: main thread
354  *****************************************************************************/
355 static void Run( services_discovery_t *p_sd )
356 {
357     services_discovery_sys_t *p_sys = p_sd->p_sys;
358
359     while( !p_sd->b_die )
360     {
361         if( avahi_simple_poll_iterate( p_sys->simple_poll, 100 ) != 0 )
362         {
363             msg_Err( p_sd, "poll iterate failed" );
364             break;
365         }
366     }
367 }