media_discoverer.c 13.8 KB
Newer Older
1 2 3
/*****************************************************************************
 * media_discoverer.c: libvlc new API media discoverer functions
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2007 VLC authors and VideoLAN
5 6 7 8
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12 13 14 15
 * (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
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23
 *****************************************************************************/

24 25 26 27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

28
#include <assert.h>
29 30 31 32 33 34 35 36 37 38 39

#include <vlc/libvlc.h>
#include <vlc/libvlc_media.h>
#include <vlc/libvlc_media_list.h>
#include <vlc/libvlc_media_discoverer.h>
#include <vlc/libvlc_events.h>

#include <vlc_services_discovery.h>

#include "libvlc_internal.h"
#include "media_internal.h" // libvlc_media_new_from_input_item()
40
#include "media_list_internal.h" // libvlc_media_list_internal_add_media()
41 42 43

struct libvlc_media_discoverer_t
{
44
    libvlc_event_manager_t   event_manager;
45 46 47 48
    libvlc_instance_t *      p_libvlc_instance;
    services_discovery_t *   p_sd;
    libvlc_media_list_t *    p_mlist;
    vlc_dictionary_t         catname_to_submedialist;
49
    char                     name[];
50
};
51 52 53 54

/*
 * Private functions
 */
55

56 57 58 59
/**************************************************************************
 *       services_discovery_item_added (Private) (VLC event callback)
 **************************************************************************/

60
static void services_discovery_item_added( services_discovery_t *sd,
61
                                           input_item_t *parent,
62 63
                                           input_item_t *p_item,
                                           const char *psz_cat )
64
{
65
    libvlc_media_t * p_md;
66
    libvlc_media_discoverer_t *p_mdis = sd->owner.sys;
67
    libvlc_media_list_t * p_mlist = p_mdis->p_mlist;
68

69 70
    p_md = libvlc_media_new_from_input_item( p_mdis->p_libvlc_instance,
                                             p_item );
71

72 73 74 75 76
    if( parent != NULL )
    {
        /* Flatten items list for now. TODO: tree support. */
    }
    else
77 78 79 80 81
    /* If we have a category, that mean we have to group the items having
     * that category in a media_list. */
    if( psz_cat )
    {
        p_mlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, psz_cat );
82

83 84
        if( p_mlist == kVLCDictionaryNotFound )
        {
85
            libvlc_media_t * p_catmd;
86
            p_catmd = libvlc_media_new_as_node( p_mdis->p_libvlc_instance, psz_cat );
87
            p_mlist = libvlc_media_subitems( p_catmd );
88
            p_mlist->b_read_only = true;
89

90
            /* Insert the newly created mlist in our dictionary */
91
            vlc_dictionary_insert( &p_mdis->catname_to_submedialist, psz_cat, p_mlist );
92

93
            /* Insert the md into the root list */
94
            libvlc_media_list_lock( p_mdis->p_mlist );
95
            libvlc_media_list_internal_add_media( p_mdis->p_mlist, p_catmd );
96
            libvlc_media_list_unlock( p_mdis->p_mlist );
97 98 99

            /* We don't release the mlist cause the dictionary
             * doesn't retain the object. But we release the md. */
100
            libvlc_media_release( p_catmd );
101 102
        }
    }
103 104

    libvlc_media_list_lock( p_mlist );
105
    libvlc_media_list_internal_add_media( p_mlist, p_md );
106
    libvlc_media_list_unlock( p_mlist );
107 108

    libvlc_media_release( p_md );
109 110 111 112 113 114
}

/**************************************************************************
 *       services_discovery_item_removed (Private) (VLC event callback)
 **************************************************************************/

115 116
static void services_discovery_item_removed( services_discovery_t *sd,
                                             input_item_t *p_item )
117
{
118
    libvlc_media_t * p_md;
119
    libvlc_media_discoverer_t *p_mdis = sd->owner.sys;
120

121
    int i, count = libvlc_media_list_count( p_mdis->p_mlist );
122 123 124
    libvlc_media_list_lock( p_mdis->p_mlist );
    for( i = 0; i < count; i++ )
    {
125
        p_md = libvlc_media_list_item_at_index( p_mdis->p_mlist, i );
126
        assert(p_md != NULL);
127 128
        if( p_md->p_input_item == p_item )
        {
129
            libvlc_media_list_internal_remove_index( p_mdis->p_mlist, i );
130
            libvlc_media_release( p_md );
131 132
            break;
        }
133
        libvlc_media_release( p_md );
134 135 136 137
    }
    libvlc_media_list_unlock( p_mdis->p_mlist );
}

138 139 140 141 142 143 144 145
/*
 * Public libvlc functions
 */

/**************************************************************************
 *       new (Public)
 **************************************************************************/
libvlc_media_discoverer_t *
146
libvlc_media_discoverer_new( libvlc_instance_t * p_inst, const char * psz_name )
147
{
148 149 150
    /* podcast SD is a hack and only works with custom playlist callbacks. */
    if( !strncasecmp( psz_name, "podcast", 7 ) )
        return NULL;
151

152 153 154 155
    libvlc_media_discoverer_t *p_mdis;

    p_mdis = malloc(sizeof(*p_mdis) + strlen(psz_name) + 1);
    if( unlikely(p_mdis == NULL) )
156
    {
157
        libvlc_printerr( "Not enough memory" );
158 159 160 161
        return NULL;
    }

    p_mdis->p_libvlc_instance = p_inst;
162
    p_mdis->p_mlist = libvlc_media_list_new( p_inst );
163
    p_mdis->p_mlist->b_read_only = true;
164
    p_mdis->p_sd = NULL;
165

166
    vlc_dictionary_init( &p_mdis->catname_to_submedialist, 0 );
167
    libvlc_event_manager_init( &p_mdis->event_manager, p_mdis );
168

169
    libvlc_retain( p_inst );
170
    strcpy( p_mdis->name, psz_name );
171 172 173 174 175 176 177 178 179
    return p_mdis;
}

/**************************************************************************
 *       start (Public)
 **************************************************************************/
LIBVLC_API int
libvlc_media_discoverer_start( libvlc_media_discoverer_t * p_mdis )
{
180 181 182 183 184 185
    struct services_discovery_owner_t owner = {
        p_mdis,
        services_discovery_item_added,
        services_discovery_item_removed,
    };

186
    /* Here we go */
187 188 189 190 191
    p_mdis->p_sd = vlc_sd_Create( (vlc_object_t *)p_mdis->p_libvlc_instance->p_libvlc_int,
                                  p_mdis->name, &owner );
    if( p_mdis->p_sd == NULL )
    {
        libvlc_printerr( "%s: no such discovery module found", p_mdis->name );
192
        return -1;
193
    }
194 195 196

    libvlc_event_t event;
    event.type = libvlc_MediaDiscovererStarted;
197
    libvlc_event_send( &p_mdis->event_manager, &event );
198
    return 0;
199 200 201 202 203 204 205 206
}

/**************************************************************************
 *       stop (Public)
 **************************************************************************/
LIBVLC_API void
libvlc_media_discoverer_stop( libvlc_media_discoverer_t * p_mdis )
{
207 208 209 210 211 212 213
    libvlc_media_list_t * p_mlist = p_mdis->p_mlist;
    libvlc_media_list_lock( p_mlist );
    libvlc_media_list_internal_end_reached( p_mlist );
    libvlc_media_list_unlock( p_mlist );

    libvlc_event_t event;
    event.type = libvlc_MediaDiscovererEnded;
214
    libvlc_event_send( &p_mdis->event_manager, &event );
215

216 217
    vlc_sd_Destroy( p_mdis->p_sd );
    p_mdis->p_sd = NULL;
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
}

/**************************************************************************
 *       new_from_name (Public)
 *
 * \deprecated Use libvlc_media_discoverer_new and libvlc_media_discoverer_start
 **************************************************************************/
libvlc_media_discoverer_t *
libvlc_media_discoverer_new_from_name( libvlc_instance_t * p_inst,
                                       const char * psz_name )
{
    libvlc_media_discoverer_t *p_mdis = libvlc_media_discoverer_new( p_inst, psz_name );

    if( !p_mdis )
        return NULL;

    if( libvlc_media_discoverer_start( p_mdis ) != 0)
235
    {
236
        libvlc_media_discoverer_release( p_mdis );
237 238
        return NULL;
    }
239 240 241 242 243 244 245

    return p_mdis;
}

/**************************************************************************
 * release (Public)
 **************************************************************************/
246 247 248 249 250 251 252
static void
MediaListDictValueRelease( void* mlist, void* obj )
{
    libvlc_media_list_release( mlist );
    (void)obj;
}

253 254 255
void
libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis )
{
256
    if( p_mdis->p_sd != NULL )
257
        libvlc_media_discoverer_stop( p_mdis );
258

259 260
    libvlc_media_list_release( p_mdis->p_mlist );

261 262
    vlc_dictionary_clear( &p_mdis->catname_to_submedialist,
        MediaListDictValueRelease, NULL );
263

264
    libvlc_event_manager_destroy( &p_mdis->event_manager );
265
    libvlc_release( p_mdis->p_libvlc_instance );
266

267 268 269 270 271 272 273 274 275
    free( p_mdis );
}

/**************************************************************************
 * localized_name (Public)
 **************************************************************************/
char *
libvlc_media_discoverer_localized_name( libvlc_media_discoverer_t * p_mdis )
{
276 277 278
    if( p_mdis->p_sd == NULL || p_mdis->p_sd->description == NULL )
        return NULL;
    return strdup( p_mdis->p_sd->description );
279 280 281 282 283 284 285 286
}

/**************************************************************************
 * media_list (Public)
 **************************************************************************/
libvlc_media_list_t *
libvlc_media_discoverer_media_list( libvlc_media_discoverer_t * p_mdis )
{
287 288
    libvlc_media_list_retain( p_mdis->p_mlist );
    return p_mdis->p_mlist;
289 290
}

291 292 293 294 295 296
/**************************************************************************
 * event_manager (Public)
 **************************************************************************/
libvlc_event_manager_t *
libvlc_media_discoverer_event_manager( libvlc_media_discoverer_t * p_mdis )
{
297
    return &p_mdis->event_manager;
298 299
}

300 301 302 303

/**************************************************************************
 * running (Public)
 **************************************************************************/
304
int
305 306
libvlc_media_discoverer_is_running( libvlc_media_discoverer_t * p_mdis )
{
307
    return p_mdis->p_sd != NULL;
308
}
309 310

void
311
libvlc_media_discoverer_list_release( libvlc_media_discoverer_description_t **pp_services,
312
                                      size_t i_count )
313 314 315
{
    if( i_count > 0 )
    {
316
        for( size_t i = 0; i < i_count; ++i )
317 318 319 320 321 322 323 324 325
        {
            free( pp_services[i]->psz_name );
            free( pp_services[i]->psz_longname );
        }
        free( *pp_services );
        free( pp_services );
    }
}

326
size_t
327
libvlc_media_discoverer_list_get( libvlc_instance_t *p_inst,
328 329
                                  libvlc_media_discoverer_category_t i_cat,
                                  libvlc_media_discoverer_description_t ***ppp_services )
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
{
    assert( p_inst != NULL && ppp_services != NULL );

    int i_core_cat;
    switch( i_cat )
    {
    case libvlc_media_discoverer_devices:
        i_core_cat = SD_CAT_DEVICES;
        break;
    case libvlc_media_discoverer_lan:
        i_core_cat = SD_CAT_LAN;
        break;
    case libvlc_media_discoverer_podcasts:
        i_core_cat = SD_CAT_INTERNET;
        break;
    case libvlc_media_discoverer_localdirs:
        i_core_cat = SD_CAT_MYCOMPUTER;
        break;
    default:
        vlc_assert_unreachable();
        *ppp_services = NULL;
351
        return 0;
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    }

    /* Fetch all sd names, longnames and categories */
    char **ppsz_names, **ppsz_longnames;
    int *p_categories;
    ppsz_names = vlc_sd_GetNames( p_inst->p_libvlc_int, &ppsz_longnames,
                                  &p_categories );

    if( ppsz_names == NULL )
    {
        *ppp_services = NULL;
        return 0;
    }

    /* Count the number of sd matching our category (i_cat/i_core_cat) */
367
    size_t i_nb_services = 0;
368 369 370 371 372 373 374 375
    char **ppsz_name = ppsz_names;
    int *p_category = p_categories;
    for( ; *ppsz_name != NULL; ppsz_name++, p_category++ )
    {
        if( *p_category == i_core_cat )
            i_nb_services++;
    }

376
    libvlc_media_discoverer_description_t **pp_services = NULL, *p_services = NULL;
377 378 379 380 381 382 383
    if( i_nb_services > 0 )
    {
        /* Double alloc here, so that the caller iterates through pointers of
         * struct instead of structs. This allows us to modify the struct
         * without breaking the API. */

        pp_services = malloc( i_nb_services
384
                              * sizeof(libvlc_media_discoverer_description_t *) );
385
        p_services = malloc( i_nb_services
386
                             * sizeof(libvlc_media_discoverer_description_t) );
387 388 389 390 391 392
        if( pp_services == NULL || p_services == NULL )
        {
            free( pp_services );
            free( p_services );
            pp_services = NULL;
            p_services = NULL;
393
            i_nb_services = 0;
394 395 396 397 398 399 400 401 402 403
            /* Even if alloc fails, the next loop must be run in order to free
             * names returned by vlc_sd_GetNames */
        }
    }

    /* Fill output pp_services or free unused name, longname */
    char **ppsz_longname = ppsz_longnames;
    ppsz_name = ppsz_names;
    p_category = p_categories;
    unsigned int i_service_idx = 0;
404
    libvlc_media_discoverer_description_t *p_service = p_services;
405
    for( ; *ppsz_name != NULL; ppsz_name++, ppsz_longname++, p_category++ )
406 407 408 409 410 411
    {
        if( pp_services != NULL && *p_category == i_core_cat )
        {
            p_service->psz_name = *ppsz_name;
            p_service->psz_longname = *ppsz_longname;
            p_service->i_cat = i_cat;
412
            pp_services[i_service_idx++] = p_service++;
413 414 415 416 417 418 419 420 421 422 423 424 425 426
        }
        else
        {
            free( *ppsz_name );
            free( *ppsz_longname );
        }
    }
    free( ppsz_names );
    free( ppsz_longnames );
    free( p_categories );

    *ppp_services = pp_services;
    return i_nb_services;
}