Qt4 - Remove the enqueue button and regroup it under the play button, since it is...
[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 #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                                             VLC_FALSE);
189             p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
190             p_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
191             p_item = playlist_NodeAddInput( p_sys->p_playlist, p_input,
192                                             p_sys->p_node_one,
193                                             PLAYLIST_APPEND, PLAYLIST_END,
194                                             VLC_FALSE );
195             p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
196             p_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
197        }
198     }
199
200     avahi_service_resolver_free( r );
201 }
202
203 /*****************************************************************************
204  * browser_callback
205  *****************************************************************************/
206 static void browse_callback(
207     AvahiServiceBrowser *b,
208     AvahiIfIndex interface,
209     AvahiProtocol protocol,
210     AvahiBrowserEvent event,
211     const char *name,
212     const char *type,
213     const char *domain,
214 #ifdef HAVE_AVAHI_06
215     AvahiLookupResultFlags flags,
216 #endif
217     void* userdata )
218 {
219     services_discovery_t *p_sd = ( services_discovery_t* )userdata;
220     services_discovery_sys_t *p_sys = p_sd->p_sys;
221
222     if( event == AVAHI_BROWSER_NEW )
223     {
224         if( avahi_service_resolver_new( p_sys->client, interface, protocol,
225                                         name, type, domain, AVAHI_PROTO_UNSPEC,
226 #ifdef HAVE_AVAHI_06
227                                         0,
228 #endif
229                                         resolve_callback, userdata ) == NULL )
230         {
231             msg_Err( p_sd, "failed to resolve service '%s': %s", name,
232                      avahi_strerror( avahi_client_errno( p_sys->client ) ) );
233         }
234     }
235     else
236     {
237         /** \todo Store the input id and search it, rather than searching the items */
238         playlist_item_t *p_item;
239         p_item = playlist_ChildSearchName( p_sys->p_node_cat, name );
240         if( p_item == NULL )
241             msg_Err( p_sd, "failed to find service '%s' in playlist", name );
242         else
243         {
244             playlist_DeleteFromInput( p_sys->p_playlist, p_item->p_input->i_id,
245                                       VLC_FALSE );
246         }
247     }
248 }
249
250 /*****************************************************************************
251  * Open: initialize and create stuff
252  *****************************************************************************/
253 static int Open( vlc_object_t *p_this )
254 {
255     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
256     services_discovery_sys_t *p_sys;
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     p_sys->p_node_cat = playlist_NodeCreate( p_sys->p_playlist, _("Bonjour"),
312                                        p_sys->p_playlist->p_root_category, 0 );
313     p_sys->p_node_one = playlist_NodeCreate( p_sys->p_playlist, _("Bonjour"),
314                                        p_sys->p_playlist->p_root_onelevel, 0 );
315     p_sys->p_node_one->p_input->i_id = p_sys->p_node_cat->p_input->i_id;
316
317     p_sys->p_node_one->i_flags |= PLAYLIST_RO_FLAG;
318     p_sys->p_node_cat->i_flags |= PLAYLIST_RO_FLAG;
319     p_sys->p_node_one->i_flags |= PLAYLIST_SKIP_FLAG;
320     p_sys->p_node_cat->i_flags |= PLAYLIST_SKIP_FLAG;
321
322     p_sd->pf_run = Run;
323
324     return VLC_SUCCESS;
325
326 error:
327     if( p_sys->p_playlist != NULL )
328         vlc_object_release( p_sys->p_playlist );
329     if( p_sys->sb != NULL )
330         avahi_service_browser_free( p_sys->sb );
331     if( p_sys->client != NULL )
332         avahi_client_free( p_sys->client );
333     if( p_sys->simple_poll != NULL )
334         avahi_simple_poll_free( p_sys->simple_poll );
335
336     free( (void *)p_sys );
337
338     return VLC_EGENERIC;
339 }
340
341 /*****************************************************************************
342  * Close: cleanup
343  *****************************************************************************/
344 static void Close( vlc_object_t *p_this )
345 {
346     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
347     services_discovery_sys_t *p_sys = p_sd->p_sys;
348
349     avahi_service_browser_free( p_sys->sb );
350     avahi_client_free( p_sys->client );
351     avahi_simple_poll_free( p_sys->simple_poll );
352
353     playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node_one, VLC_TRUE, VLC_TRUE );
354     playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node_cat, VLC_TRUE, VLC_TRUE );
355     vlc_object_release( p_sys->p_playlist );
356
357     free( p_sys );
358 }
359
360 /*****************************************************************************
361  * Run: main thread
362  *****************************************************************************/
363 static void Run( services_discovery_t *p_sd )
364 {
365     services_discovery_sys_t *p_sys = p_sd->p_sys;
366
367     while( !p_sd->b_die )
368     {
369         if( avahi_simple_poll_iterate( p_sys->simple_poll, 100 ) != 0 )
370         {
371             msg_Err( p_sd, "poll iterate failed" );
372             break;
373         }
374     }
375 }