services_discovery.c 13 KB
Newer Older
zorglub's avatar
zorglub committed
1 2 3
/*****************************************************************************
 * services_discovery.c : Manage playlist services_discovery modules
 *****************************************************************************
4
 * Copyright (C) 1999-2004 the VideoLAN team
dionoea's avatar
dionoea committed
5
 * $Id$
zorglub's avatar
zorglub committed
6
 *
7
 * Authors: Clément Stenac <zorglub@videolan.org>
zorglub's avatar
zorglub committed
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
zorglub's avatar
zorglub committed
22 23 24
 *****************************************************************************/
#include <vlc/vlc.h>
#include "vlc_playlist.h"
25
#include "vlc_events.h"
26
#include "playlist_internal.h"
zorglub's avatar
zorglub committed
27 28 29

static void RunSD( services_discovery_t *p_sd );

30 31 32 33 34 35
/*
 * Services discovery
 * Basically you just listen to Service discovery event through the
 * sd's event manager.
 * That's how the playlist get's Service Discovery information
 */
36

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 65 66 67 68 69 70 71 72 73 74
/***********************************************************************
 * Create
 ***********************************************************************/
services_discovery_t *
services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name )
{
    services_discovery_t *p_sd = vlc_object_create( p_super, VLC_OBJECT_SD );
    if( !p_sd )
        return NULL;
    
    p_sd->pf_run = NULL;
    p_sd->psz_localized_name = NULL;

    vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
    vlc_event_manager_register_event_type( &p_sd->event_manager, 
            vlc_ServicesDiscoveryItemAdded );
    vlc_event_manager_register_event_type( &p_sd->event_manager, 
            vlc_ServicesDiscoveryItemRemoved );

    p_sd->p_module = module_Need( p_sd, "services_discovery", psz_module_name, 0 );
    
    if( p_sd->p_module == NULL )
    {
        msg_Err( p_super, "no suitable services discovery module" );
        vlc_object_destroy( p_sd );
        return NULL;
    }
    p_sd->psz_module = strdup( psz_module_name );
    p_sd->b_die = VLC_FALSE; /* FIXME */

    return p_sd;
}

/***********************************************************************
 * Destroy
 ***********************************************************************/
void services_discovery_Destroy ( services_discovery_t * p_sd )
{
75
    vlc_event_manager_fini( &p_sd->event_manager );
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

    free( p_sd->psz_module );
    free( p_sd->psz_localized_name );

    vlc_object_destroy( p_sd );
}

/***********************************************************************
 * Start
 ***********************************************************************/
int services_discovery_Start ( services_discovery_t * p_sd )
{
    if ((p_sd->pf_run != NULL)
        && vlc_thread_create( p_sd, "services_discovery", RunSD,
                              VLC_THREAD_PRIORITY_LOW, VLC_FALSE))
    {
        msg_Err( p_sd, "cannot create services discovery thread" );
        vlc_object_destroy( p_sd );
        return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}

99 100 101 102 103 104 105 106 107 108 109
/***********************************************************************
 * Stop
 ***********************************************************************/
void services_discovery_Stop ( services_discovery_t * p_sd )
{
    vlc_object_kill( p_sd );
    if( p_sd->pf_run ) vlc_thread_join( p_sd );

    module_Unneed( p_sd, p_sd->p_module );
}

110 111 112
/***********************************************************************
 * GetLocalizedName
 ***********************************************************************/
113
char *
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
{
    return p_sd->psz_localized_name ? strdup( p_sd->psz_localized_name ) : NULL;
}

/***********************************************************************
 * SetLocalizedName
 ***********************************************************************/
void
services_discovery_SetLocalizedName ( services_discovery_t * p_sd, const char *psz )
{
    if(p_sd->psz_localized_name) free( p_sd->psz_localized_name );
    p_sd->psz_localized_name = strdup(psz);
}

/***********************************************************************
 * EventManager
 ***********************************************************************/
vlc_event_manager_t *
services_discovery_EventManager ( services_discovery_t * p_sd )
{
    return &p_sd->event_manager;
}

/***********************************************************************
 * AddItem
 ***********************************************************************/
void
services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
                             const char * psz_category )
{
    vlc_event_t event;
    event.type = vlc_ServicesDiscoveryItemAdded;
    event.u.services_discovery_item_added.p_new_item = p_item;
    event.u.services_discovery_item_added.psz_category = psz_category;

    vlc_event_send( &p_sd->event_manager, &event );
}

/***********************************************************************
 * RemoveItem
 ***********************************************************************/
void
services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
{
    vlc_event_t event;
    event.type = vlc_ServicesDiscoveryItemRemoved;
    event.u.services_discovery_item_removed.p_item = p_item;

    vlc_event_send( &p_sd->event_manager, &event );
}

/***********************************************************************
 * RunSD (Private)
 ***********************************************************************/
static void RunSD( services_discovery_t *p_sd )
{
    p_sd->pf_run( p_sd );
    return;
}

/*
 * Playlist - Services discovery bridge
 */
 
 /* A new item has been added to a certain sd */
static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
{
    input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
    const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
    playlist_item_t *p_new_item, * p_parent = user_data;

    msg_Dbg( p_parent->p_playlist, "Adding %s in %s", p_input->psz_name, psz_cat );

    /* If p_child is in root category (this is clearly a hack) and we have a cat*/
    if( !EMPTY_STR(psz_cat) &&
        p_parent == playlist_ItemToNode( p_parent->p_playlist, p_parent, VLC_FALSE ) )
    {
        /* */
        playlist_item_t * p_cat;
        p_cat = playlist_ChildSearchName( p_parent, psz_cat );
        if( !p_cat )
        {
            p_cat = playlist_NodeCreate( p_parent->p_playlist, psz_cat,
                                         p_parent, 0 );
            p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
        }
        p_parent = p_cat;
    }
        
    p_new_item = playlist_NodeAddInput( p_parent->p_playlist, p_input, p_parent,
                                        PLAYLIST_APPEND, PLAYLIST_END, VLC_FALSE );
    p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
    p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
}

 /* A new item has been removed from a certain sd */
static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
{
213 214
    input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
    playlist_item_t * p_parent = user_data;
215

216 217
    playlist_DeleteInputInParent( p_parent->p_playlist, p_input->i_id,
                                  p_parent, VLC_FALSE );
218 219
}

220
int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,  const char *psz_modules )
zorglub's avatar
zorglub committed
221
{
222 223 224 225
    const char *psz_parser = psz_modules ?: "";
    int retval = VLC_SUCCESS;

    for (;;)
zorglub's avatar
zorglub committed
226
    {
227 228 229 230
        struct playlist_archived_services_discovery_t * p_asd;
        playlist_item_t * p_cat;
        playlist_item_t * p_one;

231 232
        while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
            psz_parser++;
zorglub's avatar
zorglub committed
233

234 235
        if( *psz_parser == '\0' )
            break;
zorglub's avatar
zorglub committed
236

237 238 239
        const char *psz_next = strchr( psz_parser, ':' );
        if( psz_next == NULL )
            psz_next = psz_parser + strlen( psz_parser );
240

241 242 243 244
        char psz_plugin[psz_next - psz_parser + 1];
        memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
        psz_plugin[sizeof (psz_plugin) - 1] = '\0';
        psz_parser = psz_next;
245

246 247
        /* Perform the addition */
        msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
248
        services_discovery_t *p_sd;
249

250 251 252
        p_sd = services_discovery_Create( (vlc_object_t*)p_playlist, psz_plugin );
        if( !p_sd )
            continue;
253

254 255
        char * psz = services_discovery_GetLocalizedName( p_sd );
        if( psz )
256
        {
257 258 259 260 261 262 263
            playlist_NodesPairCreate( p_playlist, psz,
                    &p_cat, &p_one, VLC_FALSE );
            free( psz );
        }
        else
        {
            /* No name, just add at the top of the playlist */
264
            PL_LOCK;
265 266
            p_cat = p_playlist->p_root_category;
            p_one = p_playlist->p_root_onelevel;
267
            PL_UNLOCK;
268
        }
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

        vlc_event_attach( services_discovery_EventManager( p_sd ),
                          vlc_ServicesDiscoveryItemAdded,
                          playlist_sd_item_added,
                          p_one );

        vlc_event_attach( services_discovery_EventManager( p_sd ),
                          vlc_ServicesDiscoveryItemAdded,
                          playlist_sd_item_added,
                          p_cat );

        vlc_event_attach( services_discovery_EventManager( p_sd ),
                          vlc_ServicesDiscoveryItemRemoved,
                          playlist_sd_item_removed,
                          p_one );

        vlc_event_attach( services_discovery_EventManager( p_sd ),
                          vlc_ServicesDiscoveryItemRemoved,
                          playlist_sd_item_removed,
                          p_cat );

        services_discovery_Start( p_sd );
291

292 293 294 295 296 297
        /* Free in playlist_ServicesDiscoveryRemove */
        p_asd = malloc( sizeof(struct playlist_archived_services_discovery_t) );
        p_asd->p_sd = p_sd;
        p_asd->p_one = p_one;
        p_asd->p_cat = p_cat;
        
298
        PL_LOCK;
299
        TAB_APPEND( p_playlist->i_asds, p_playlist->pp_asds, p_asd );
300
        PL_UNLOCK;
zorglub's avatar
zorglub committed
301
    }
302 303

    return retval;
zorglub's avatar
zorglub committed
304 305
}

zorglub's avatar
zorglub committed
306
int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
zorglub's avatar
zorglub committed
307 308 309
                                       const char *psz_module )
{
    int i;
310
    struct playlist_archived_services_discovery_t *p_asd = NULL;
zorglub's avatar
zorglub committed
311

312
    PL_LOCK;
313
    for( i = 0 ; i< p_playlist->i_asds ; i ++ )
zorglub's avatar
zorglub committed
314
    {
315
        if( !strcmp( psz_module, p_playlist->pp_asds[i]->p_sd->psz_module ) )
zorglub's avatar
zorglub committed
316
        {
317 318
            p_asd = p_playlist->pp_asds[i];
            REMOVE_ELEM( p_playlist->pp_asds, p_playlist->i_asds, i );
zorglub's avatar
zorglub committed
319 320 321
            break;
        }
    }
322
    PL_UNLOCK;
zorglub's avatar
zorglub committed
323

324
    if( p_asd && p_asd->p_sd )
zorglub's avatar
zorglub committed
325
    {
326
        services_discovery_Stop( p_asd->p_sd );
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

        vlc_event_detach( services_discovery_EventManager( p_asd->p_sd ),
                          vlc_ServicesDiscoveryItemAdded,
                          playlist_sd_item_added,
                          p_asd->p_one );
        
        vlc_event_detach( services_discovery_EventManager( p_asd->p_sd ),
                          vlc_ServicesDiscoveryItemAdded,
                          playlist_sd_item_added,
                          p_asd->p_cat );
        
        vlc_event_detach( services_discovery_EventManager( p_asd->p_sd ),
                          vlc_ServicesDiscoveryItemRemoved,
                          playlist_sd_item_removed,
                          p_asd->p_one );
        
        vlc_event_detach( services_discovery_EventManager( p_asd->p_sd ),
                          vlc_ServicesDiscoveryItemRemoved,
                          playlist_sd_item_removed,
                          p_asd->p_cat );
        
        /* Remove the sd playlist node if it exists */
        PL_LOCK;
        if( p_asd->p_cat != p_playlist->p_root_category && 
            p_asd->p_one != p_playlist->p_root_onelevel )
        {
            playlist_NodeDelete( p_playlist, p_asd->p_cat, VLC_TRUE, VLC_FALSE );
            playlist_NodeDelete( p_playlist, p_asd->p_one, VLC_TRUE, VLC_FALSE );
        }
        PL_UNLOCK;

358 359
        services_discovery_Destroy( p_asd->p_sd );

360
        free( p_asd );
zorglub's avatar
zorglub committed
361 362 363 364
    }
    else
    {
        msg_Warn( p_playlist, "module %s is not loaded", psz_module );
zorglub's avatar
zorglub committed
365
        return VLC_EGENERIC;
zorglub's avatar
zorglub committed
366
    }
zorglub's avatar
zorglub committed
367
    return VLC_SUCCESS;
zorglub's avatar
zorglub committed
368 369
}

370 371 372 373
vlc_bool_t playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
                                              const char *psz_module )
{
    int i;
374
    PL_LOCK;
375

376
    for( i = 0 ; i< p_playlist->i_asds ; i ++ )
377
    {
378
        if( !strcmp( psz_module, p_playlist->pp_asds[i]->p_sd->psz_module ) )
379
        {
380
            PL_UNLOCK;
381 382 383
            return VLC_TRUE;
        }
    }
384
    PL_UNLOCK;
385 386 387
    return VLC_FALSE;
}