podcast.c 14.1 KB
Newer Older
1 2 3
/*****************************************************************************
 * podcast.c:  Podcast services discovery module
 *****************************************************************************
4
 * Copyright (C) 2005-2009 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * $Id$
 *
 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
 *
 * 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
 *****************************************************************************/

/*****************************************************************************
 * Includes
 *****************************************************************************/

28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
35
#include <vlc_services_discovery.h>
36
#include <vlc_network.h>
37

38 39
#include <assert.h>
#include <unistd.h>
40 41 42 43 44 45 46 47 48 49

/************************************************************************
 * Macros and definitions
 ************************************************************************/

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

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

53
VLC_SD_PROBE_HELPER("podcast", N_("Podcasts"), SD_CAT_INTERNET)
54

55
#define URLS_TEXT N_("Podcast URLs list")
56 57
#define URLS_LONGTEXT N_("Enter the list of podcasts to retrieve, " \
                         "separated by '|' (pipe)." )
58

59 60 61 62 63
vlc_module_begin ()
    set_shortname( "Podcast")
    set_description( N_("Podcasts") )
    set_category( CAT_PLAYLIST )
    set_subcategory( SUBCAT_PLAYLIST_SD )
64

65
    add_string( "podcast-urls", NULL,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
66
                URLS_TEXT, URLS_LONGTEXT, false )
67

68 69
    set_capability( "services_discovery", 0 )
    set_callbacks( Open, Close )
70

71 72
    VLC_SD_PROBE_SUBMODULE

73
vlc_module_end ()
74 75 76 77 78 79


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

80 81 82 83 84
enum {
  UPDATE_URLS,
  UPDATE_REQUEST
}; /* FIXME Temporary. Updating by compound urls string to be removed later. */

85 86 87
struct services_discovery_sys_t
{
    /* playlist node */
88
    input_thread_t **pp_input;
89
    int i_input;
90 91 92

    char **ppsz_urls;
    int i_urls;
93

94 95 96
    input_item_t **pp_items;
    int i_items;

97
    vlc_thread_t thread;
98 99
    vlc_mutex_t lock;
    vlc_cond_t  wait;
100
    bool b_update;
101
    bool b_savedurls_loaded;
102 103
    char *psz_request;
    int update_type;
104 105 106 107 108
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
109
static void *Run( void * );
110 111
static int UrlsChange( vlc_object_t *, char const *, vlc_value_t,
                       vlc_value_t, void * );
112 113 114
static int Request( vlc_object_t *, char const *, vlc_value_t,
                       vlc_value_t, void * );
static void ParseRequest( services_discovery_t *p_sd );
115
static void ParseUrls( services_discovery_t *p_sd, char *psz_urls );
116
static void SaveUrls( services_discovery_t *p_sd );
117 118 119 120 121 122

/*****************************************************************************
 * Open: initialize and create stuff
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
123
    if( strcmp( p_this->obj.parent->obj.object_type, "playlist" ) )
124 125
        return VLC_EGENERIC; /* FIXME: support LibVLC SD too! */

126
    services_discovery_t *p_sd = ( services_discovery_t* )p_this;
127
    services_discovery_sys_t *p_sys = malloc( sizeof( *p_sys ) );
128 129
    if( !p_sys )
        return VLC_ENOMEM;
130

131 132
    p_sys->i_urls = 0;
    p_sys->ppsz_urls = NULL;
133
    p_sys->i_input = 0;
134
    p_sys->pp_input = NULL;
135 136
    p_sys->pp_items = NULL;
    p_sys->i_items = 0;
137 138
    vlc_mutex_init( &p_sys->lock );
    vlc_cond_init( &p_sys->wait );
139
    p_sys->b_update = true;
140
    p_sys->b_savedurls_loaded = false;
141 142
    p_sys->psz_request = NULL;
    p_sys->update_type = UPDATE_URLS;
143

144
    p_sd->p_sys  = p_sys;
145
    p_sd->description = _("Podcasts");
146

147
    /* Launch the callback associated with this variable */
148
    vlc_object_t *pl = p_sd->obj.parent;
149 150
    var_Create( pl, "podcast-urls", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_AddCallback( pl, "podcast-urls", UrlsChange, p_sys );
151

152 153
    var_Create( pl, "podcast-request", VLC_VAR_STRING );
    var_AddCallback( pl, "podcast-request", Request, p_sys );
154

155 156
    if (vlc_clone (&p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
    {
157 158
        var_DelCallback( pl, "podcast-request", Request, p_sys );
        var_DelCallback( pl, "podcast-urls", UrlsChange, p_sys );
159 160
        vlc_cond_destroy( &p_sys->wait );
        vlc_mutex_destroy( &p_sys->lock );
161 162 163
        free (p_sys);
        return VLC_EGENERIC;
    }
164 165 166 167 168 169 170 171 172
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    services_discovery_t *p_sd = ( services_discovery_t* )p_this;
173
    services_discovery_sys_t *p_sys = p_sd->p_sys;
174
    vlc_object_t *pl = p_sd->obj.parent;
175
    int i;
176

177 178 179
    vlc_cancel (p_sys->thread);
    vlc_join (p_sys->thread, NULL);

180 181
    var_DelCallback( pl, "podcast-urls", UrlsChange, p_sys );
    var_DelCallback( pl, "podcast-request", Request, p_sys );
182 183 184
    vlc_cond_destroy( &p_sys->wait );
    vlc_mutex_destroy( &p_sys->lock );

185
    for( i = 0; i < p_sys->i_input; i++ )
186
    {
187 188 189 190
        input_thread_t *p_input = p_sd->p_sys->pp_input[i];
        if( !p_input )
            continue;

191
        input_Stop( p_input );
192
        input_Close( p_input );
193 194

        p_sd->p_sys->pp_input[i] = NULL;
195 196 197 198
    }
    free( p_sd->p_sys->pp_input );
    for( i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
    free( p_sys->ppsz_urls );
199 200
    for( i = 0; i < p_sys->i_items; i++ ) vlc_gc_decref( p_sys->pp_items[i] );
    free( p_sys->pp_items );
201
    free( p_sys->psz_request );
202 203 204 205 206 207
    free( p_sys );
}

/*****************************************************************************
 * Run: main thread
 *****************************************************************************/
208
VLC_NORETURN
209
static void *Run( void *data )
210
{
211
    services_discovery_t *p_sd = data;
212
    services_discovery_sys_t *p_sys  = p_sd->p_sys;
213

214 215
    vlc_mutex_lock( &p_sys->lock );
    mutex_cleanup_push( &p_sys->lock );
216
    for( ;; )
217
    {
218 219
        while( !p_sys->b_update )
            vlc_cond_wait( &p_sys->wait, &p_sys->lock );
220

221 222
        int canc = vlc_savecancel ();
        msg_Dbg( p_sd, "Update required" );
223 224 225

        if( p_sys->update_type == UPDATE_URLS )
        {
226
            char *psz_urls = var_GetNonEmptyString( p_sd->obj.parent,
227
                                                    "podcast-urls" );
228 229
            ParseUrls( p_sd, psz_urls );
            free( psz_urls );
230 231
        }
        else if( p_sys->update_type == UPDATE_REQUEST )
232
            ParseRequest( p_sd );
233

234
        p_sys->b_update = false;
235

236
        for( int i = 0; i < p_sd->p_sys->i_input; i++ )
237
        {
238
            input_thread_t *p_input = p_sd->p_sys->pp_input[i];
239
            int state = var_GetInteger( p_input, "state" );
240

241
            if( state == END_S || state == ERROR_S )
242
            {
243
                input_Stop( p_input );
244
                input_Close( p_input );
245

246
                p_sd->p_sys->pp_input[i] = NULL;
247 248
                REMOVE_ELEM( p_sys->pp_input, p_sys->i_input, i );
                i--;
249
            }
250
        }
251
        vlc_restorecancel (canc);
252
    }
253
    vlc_cleanup_pop();
254
    vlc_assert_unreachable(); /* dead code */
255 256 257 258 259 260
}

static int UrlsChange( vlc_object_t *p_this, char const *psz_var,
                       vlc_value_t oldval, vlc_value_t newval,
                       void *p_data )
{
261 262
    VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
    VLC_UNUSED(newval);
263
    services_discovery_sys_t *p_sys  = (services_discovery_sys_t *)p_data;
264 265

    vlc_mutex_lock( &p_sys->lock );
266
    p_sys->b_update = true;
267
    p_sys->update_type = UPDATE_URLS;
268 269
    vlc_cond_signal( &p_sys->wait );
    vlc_mutex_unlock( &p_sys->lock );
270 271 272
    return VLC_SUCCESS;
}

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
static int Request( vlc_object_t *p_this, char const *psz_var,
                       vlc_value_t oldval, vlc_value_t newval,
                       void *p_data )
{
    VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
    services_discovery_sys_t *p_sys = (services_discovery_sys_t *)p_data;

    vlc_mutex_lock( &p_sys->lock );
    free( p_sys->psz_request );
    p_sys->psz_request = NULL;
    if( newval.psz_string && *newval.psz_string ) {
      p_sys->psz_request = strdup( newval.psz_string );
      p_sys->b_update = true;
      p_sys->update_type = UPDATE_REQUEST;
      vlc_cond_signal( &p_sys->wait );
    }
    vlc_mutex_unlock( &p_sys->lock );
    return VLC_SUCCESS;
}

293 294 295
static void ParseUrls( services_discovery_t *p_sd, char *psz_urls )
{
    services_discovery_sys_t *p_sys = p_sd->p_sys;
296
    int i_new_items = 0;
297
    input_item_t **pp_new_items = NULL;
298

299
    int i_new_urls = 0;
300
    char **ppsz_new_urls = NULL;
301
    p_sys->b_savedurls_loaded = true;
302 303 304

    int i, j;

305 306
    for( ;; )
    {
307 308
        if( !psz_urls )
            break;
309

310 311
        char *psz_tok = strchr( psz_urls, '|' );
        if( psz_tok ) *psz_tok = '\0';
312

313 314 315 316 317
        for( i = 0; i < p_sys->i_urls; i++ )
            if( !strcmp( psz_urls, p_sys->ppsz_urls[i] ) )
                break;
        if( i == p_sys->i_urls )
        {
318
            INSERT_ELEM( ppsz_new_urls, i_new_urls, i_new_urls,
319
                         strdup( psz_urls ) );
320 321

            input_item_t *p_input;
322
            p_input = input_item_New( psz_urls, psz_urls );
323
            input_item_AddOption( p_input, "demux=podcast", VLC_INPUT_OPTION_TRUSTED );
324 325

            INSERT_ELEM( pp_new_items, i_new_items, i_new_items, p_input );
326
            services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
327

328
            INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
329
                         input_CreateAndStart( p_sd, p_input, NULL ) );
330
        }
331 332 333 334 335 336
        else
        {
            INSERT_ELEM( ppsz_new_urls, i_new_urls, i_new_urls,
                         strdup( p_sys->ppsz_urls[i]) );
            INSERT_ELEM( pp_new_items, i_new_items, i_new_items, p_sys->pp_items[i] );
        }
337 338 339 340
        if( psz_tok )
            psz_urls = psz_tok+1;
        else
            break;
341 342 343 344 345 346 347 348 349
    }

    /* delete removed items and signal the removal */
    for( i = 0; i<p_sys->i_items; ++i )
    {
        for( j = 0; j < i_new_items; ++j )
            if( pp_new_items[j] == p_sys->pp_items[i] ) break;
        if( j == i_new_items )
        {
350 351
            services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
            vlc_gc_decref( p_sys->pp_items[i] );
352
        }
353
    }
354
    free( p_sys->pp_items );
355 356
    for( int i = 0; i < p_sys->i_urls; i++ )
        free( p_sys->ppsz_urls[i] );
357 358 359 360 361 362
    free( p_sys->ppsz_urls );

    p_sys->ppsz_urls = ppsz_new_urls;
    p_sys->i_urls = i_new_urls;
    p_sys->pp_items = pp_new_items;
    p_sys->i_items = i_new_items;
363
}
364 365 366 367 368 369 370 371 372 373 374 375

static void ParseRequest( services_discovery_t *p_sd )
{
    services_discovery_sys_t *p_sys = p_sd->p_sys;

    char *psz_request = p_sys->psz_request;

    int i;

    char *psz_tok = strchr( psz_request, ':' );
    if( !psz_tok ) return;
    *psz_tok = '\0';
376 377 378

    if ( ! p_sys->b_savedurls_loaded )
    {
379
        char *psz_urls = var_GetNonEmptyString( p_sd->obj.parent,
380
                                                "podcast-urls" );
381 382 383 384
        ParseUrls( p_sd, psz_urls );
        free( psz_urls );
    }

385 386 387 388 389 390 391 392 393 394 395 396
    if( !strcmp( psz_request, "ADD" ) )
    {
        psz_request = psz_tok + 1;
        for( i = 0; i<p_sys->i_urls; i++ )
            if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
              break;
        if( i == p_sys->i_urls )
        {
            INSERT_ELEM( p_sys->ppsz_urls, p_sys->i_urls, p_sys->i_urls,
              strdup( psz_request ) );

            input_item_t *p_input;
397
            p_input = input_item_New( psz_request, psz_request );
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
            input_item_AddOption( p_input, "demux=podcast", VLC_INPUT_OPTION_TRUSTED );

            INSERT_ELEM( p_sys->pp_items, p_sys->i_items, p_sys->i_items, p_input );
            services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );

            INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
                         input_CreateAndStart( p_sd, p_input, NULL ) );
            SaveUrls( p_sd );
        }
    }
    else if ( !strcmp( psz_request, "RM" ) )
    {
        psz_request = psz_tok + 1;
        for( i = 0; i<p_sys->i_urls; i++ )
          if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
            break;
        if( i != p_sys->i_urls )
        {
            services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
            vlc_gc_decref( p_sys->pp_items[i] );
            REMOVE_ELEM( p_sys->ppsz_urls, p_sys->i_urls, i );
            REMOVE_ELEM( p_sys->pp_items, p_sys->i_items, i );
        }
        SaveUrls( p_sd );
    }

    free( p_sys->psz_request );
    p_sys->psz_request = NULL;
}

static void SaveUrls( services_discovery_t *p_sd )
{
    services_discovery_sys_t *p_sys = p_sd->p_sys;
    int i;
    char *psz_urls;
    int len = 0;

    for( i=0; i < p_sys->i_urls; i++ )
        len += strlen( p_sys->ppsz_urls[i] ) + 1;

    psz_urls = (char*) calloc( len, sizeof(char) );

    for( i=0; i < p_sys->i_urls; i++ )
    {
        strcat( psz_urls, p_sys->ppsz_urls[i] );
        if( i < p_sys->i_urls - 1 ) strcat( psz_urls, "|" );
    }

    config_PutPsz( p_sd, "podcast-urls", psz_urls );

    free( psz_urls );
}