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