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