bonjour.c 12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*****************************************************************************
 * bonjour.c: Bonjour services discovery module
 *****************************************************************************
 * Copyright (C) 2005 the VideoLAN team
 * $Id$
 *
 * Authors: Jon Lech Johansen <jon@nanocrew.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27 28 29 30 31 32
 *****************************************************************************/

/*****************************************************************************
 * Includes
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */

#include <vlc/vlc.h>
#include <vlc/intf.h>

#include <avahi-client/client.h>
33 34 35 36
#ifdef HAVE_AVAHI_06
# include <avahi-client/publish.h>
# include <avahi-client/lookup.h>
#endif
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <avahi-common/simple-watch.h>
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/

/* Callbacks */
    static int  Open ( vlc_object_t * );
    static void Close( vlc_object_t * );

vlc_module_begin();
    set_shortname( "Bonjour" );
    set_description( _("Bonjour services") );
    set_category( CAT_PLAYLIST );
    set_subcategory( SUBCAT_PLAYLIST_SD );
    set_capability( "services_discovery", 0 );
    set_callbacks( Open, Close );
vlc_module_end();

/*****************************************************************************
 * Local structures
 *****************************************************************************/

struct services_discovery_sys_t
{
    /* playlist node */
65
    playlist_item_t     *p_node_cat, *p_node_one;
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    playlist_t          *p_playlist;

    AvahiSimplePoll     *simple_poll;
    AvahiClient         *client;
    AvahiServiceBrowser *sb;
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/

/* Main functions */
    static void Run    ( services_discovery_t *p_intf );

/*****************************************************************************
 * client_callback
 *****************************************************************************/
static void client_callback( AvahiClient *c, AvahiClientState state,
                             void * userdata )
{
    services_discovery_t *p_sd = ( services_discovery_t* )userdata;
    services_discovery_sys_t *p_sys = p_sd->p_sys;

89 90 91 92
#ifdef HAVE_AVAHI_06
    if( state == AVAHI_CLIENT_FAILURE &&
        (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) )
#else
93
    if( state == AVAHI_CLIENT_DISCONNECTED )
94
#endif
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    {
        msg_Err( p_sd, "avahi client disconnected" );
        avahi_simple_poll_quit( p_sys->simple_poll );
    }
}

/*****************************************************************************
 * resolve_callback
 *****************************************************************************/
static void resolve_callback(
    AvahiServiceResolver *r,
    AvahiIfIndex interface,
    AvahiProtocol protocol,
    AvahiResolverEvent event,
    const char *name,
    const char *type,
    const char *domain,
    const char *host_name,
    const AvahiAddress *address,
    uint16_t port,
    AvahiStringList *txt,
116 117 118
#ifdef HAVE_AVAHI_06
    AvahiLookupResultFlags flags,
#endif
119 120 121 122 123
    void* userdata )
{
    services_discovery_t *p_sd = ( services_discovery_t* )userdata;
    services_discovery_sys_t *p_sys = p_sd->p_sys;

124 125 126
#ifdef HAVE_AVAHI_06
    if( event == AVAHI_RESOLVER_FAILURE )
#else
127
    if( event == AVAHI_RESOLVER_TIMEOUT )
128
#endif
129 130 131 132 133 134 135 136 137
    {
        msg_Err( p_sd,
                 "failed to resolve service '%s' of type '%s' in domain '%s'",
                 name, type, domain );
    }
    else if( event == AVAHI_RESOLVER_FOUND )
    {
        char a[128];
        char *psz_uri = NULL;
138 139
        char *psz_addr = NULL;
        AvahiStringList *asl = NULL;
140
        input_item_t *p_input = NULL;
141 142 143 144 145

        msg_Dbg( p_sd, "service '%s' of type '%s' in domain '%s'",
                 name, type, domain );

        avahi_address_snprint(a, (sizeof(a)/sizeof(a[0]))-1, address);
146 147
        if( protocol == AVAHI_PROTO_INET6 )
            asprintf( &psz_addr, "[%s]", a );
148

149 150
        if( txt != NULL )
            asl = avahi_string_list_find( txt, "path" );
151 152 153 154 155
        if( asl != NULL )
        {
            size_t size;
            char *key = NULL;
            char *value = NULL;
156 157 158 159 160 161 162 163 164 165
            if( avahi_string_list_get_pair( asl, &key, &value, &size ) == 0 &&
                value != NULL )
            {
                asprintf( &psz_uri, "http://%s:%d%s",
                          psz_addr != NULL ? psz_addr : a, port, value );
            }
            if( key != NULL )
                avahi_free( (void *)key );
            if( value != NULL )
                avahi_free( (void *)value );
166 167 168
        }
        else
        {
169 170
            asprintf( &psz_uri, "http://%s:%d",
                      psz_addr != NULL ? psz_addr : a, port );
171 172
        }

173 174 175
        if( psz_addr != NULL )
            free( (void *)psz_addr );

176
        if( psz_uri != NULL )
177
        {
178
            p_input = input_ItemNewExt( p_sd, psz_uri, name, 0, NULL, -1 );
179 180
            free( (void *)psz_uri );
        }
181
        if( p_input != NULL )
182
        {
183 184 185 186
            playlist_item_t *p_item;
            p_item = playlist_NodeAddInput( p_sys->p_playlist, p_input,
                                            p_sys->p_node_cat,
                                            PLAYLIST_APPEND, PLAYLIST_END );
187
            p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
188 189 190 191 192 193 194
            p_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
            p_item = playlist_NodeAddInput( p_sys->p_playlist, p_input,
                                            p_sys->p_node_one,
                                            PLAYLIST_APPEND, PLAYLIST_END );
            p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
            p_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
       }
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    }

    avahi_service_resolver_free( r );
}

/*****************************************************************************
 * browser_callback
 *****************************************************************************/
static void browse_callback(
    AvahiServiceBrowser *b,
    AvahiIfIndex interface,
    AvahiProtocol protocol,
    AvahiBrowserEvent event,
    const char *name,
    const char *type,
    const char *domain,
211 212 213
#ifdef HAVE_AVAHI_06
    AvahiLookupResultFlags flags,
#endif
214 215 216 217 218 219 220 221 222
    void* userdata )
{
    services_discovery_t *p_sd = ( services_discovery_t* )userdata;
    services_discovery_sys_t *p_sys = p_sd->p_sys;

    if( event == AVAHI_BROWSER_NEW )
    {
        if( avahi_service_resolver_new( p_sys->client, interface, protocol,
                                        name, type, domain, AVAHI_PROTO_UNSPEC,
223 224 225
#ifdef HAVE_AVAHI_06
                                        0,
#endif
226 227 228 229 230 231 232 233
                                        resolve_callback, userdata ) == NULL )
        {
            msg_Err( p_sd, "failed to resolve service '%s': %s", name,
                     avahi_strerror( avahi_client_errno( p_sys->client ) ) );
        }
    }
    else
    {
234
        /** \todo Store the input id and search it, rather than searching the items */
235
        playlist_item_t *p_item;
236
        p_item = playlist_ChildSearchName( p_sys->p_node_cat, name );
237 238 239 240
        if( p_item == NULL )
            msg_Err( p_sd, "failed to find service '%s' in playlist", name );
        else
        {
241 242
            playlist_LockDeleteAllFromInput( p_sys->p_playlist,
                                              p_item->p_input->i_id );
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
        }
    }
}

/*****************************************************************************
 * Open: initialize and create stuff
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    services_discovery_t *p_sd = ( services_discovery_t* )p_this;
    services_discovery_sys_t *p_sys;
    playlist_view_t *p_view;
    vlc_value_t val;
    int err;

    p_sd->p_sys = p_sys = (services_discovery_sys_t *)malloc(
        sizeof( services_discovery_sys_t ) );
    if( p_sd->p_sys == NULL )
    {
        msg_Err( p_sd, "out of memory" );
        return VLC_EGENERIC;
    }

    memset( p_sys, 0, sizeof(*p_sys) );

    p_sys->simple_poll = avahi_simple_poll_new();
    if( p_sys->simple_poll == NULL )
    {
        msg_Err( p_sd, "failed to create avahi simple poll" );
        goto error;
    }

    p_sys->client = avahi_client_new( avahi_simple_poll_get(p_sys->simple_poll),
276 277 278
#ifdef HAVE_AVAHI_06
                                      0,
#endif
279 280 281 282 283 284 285 286 287 288 289
                                      client_callback, p_sd, &err );
    if( p_sys->client == NULL )
    {
        msg_Err( p_sd, "failed to create avahi client: %s",
                 avahi_strerror( err ) );
        goto error;
    }

    p_sys->sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC,
                                           AVAHI_PROTO_UNSPEC,
                                           "_vlc-http._tcp", NULL,
290 291 292
#ifdef HAVE_AVAHI_06
                                           0,
#endif
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
                                           browse_callback, p_sd );
    if( p_sys->sb == NULL )
    {
        msg_Err( p_sd, "failed to create avahi service browser" );
        goto error;
    }

    /* Create our playlist node */
    p_sys->p_playlist = (playlist_t *)vlc_object_find( p_sd,
                                                       VLC_OBJECT_PLAYLIST,
                                                       FIND_ANYWHERE );
    if( !p_sys->p_playlist )
    {
        msg_Warn( p_sd, "unable to find playlist, cancelling");
        goto error;
    }

310 311 312
    playlist_NodesCreate( p_sys->p_playlist, _("Bonjour"),
                              &p_sys->p_node_cat,&p_sys->p_node_one,
                              VLC_TRUE );
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    p_sd->pf_run = Run;

    return VLC_SUCCESS;

error:
    if( p_sys->p_playlist != NULL )
        vlc_object_release( p_sys->p_playlist );
    if( p_sys->sb != NULL )
        avahi_service_browser_free( p_sys->sb );
    if( p_sys->client != NULL )
        avahi_client_free( p_sys->client );
    if( p_sys->simple_poll != NULL )
        avahi_simple_poll_free( p_sys->simple_poll );

    free( (void *)p_sys );

    return VLC_EGENERIC;
}

/*****************************************************************************
 * Close: cleanup
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    services_discovery_t *p_sd = ( services_discovery_t* )p_this;
    services_discovery_sys_t *p_sys = p_sd->p_sys;

    avahi_service_browser_free( p_sys->sb );
    avahi_client_free( p_sys->client );
    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 );
    playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node_cat, VLC_TRUE, VLC_TRUE );
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    vlc_object_release( p_sys->p_playlist );

    free( p_sys );
}

/*****************************************************************************
 * Run: main thread
 *****************************************************************************/
static void Run( services_discovery_t *p_sd )
{
    services_discovery_sys_t *p_sys = p_sd->p_sys;

    while( !p_sd->b_die )
    {
        if( avahi_simple_poll_iterate( p_sys->simple_poll, 100 ) != 0 )
        {
            msg_Err( p_sd, "poll iterate failed" );
            break;
        }
    }
}