podcast.c 14 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", "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

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

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

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

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

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

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

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

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

        p_sd->p_sys->pp_input[i] = NULL;
194 195 196 197
    }
    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 );
198 199
    for( i = 0; i < p_sys->i_items; i++ ) vlc_gc_decref( p_sys->pp_items[i] );
    free( p_sys->pp_items );
200
    free( p_sys->psz_request );
201 202 203 204 205 206
    free( p_sys );
}

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

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

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

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

233
        p_sys->b_update = false;
234

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

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

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

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

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

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
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;
}

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

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

    int i, j;

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

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

312 313 314 315 316
        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 )
        {
317
            INSERT_ELEM( ppsz_new_urls, i_new_urls, i_new_urls,
318
                         strdup( psz_urls ) );
319 320

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

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

327
            INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
328
                         input_CreateAndStart( p_sd, p_input, NULL ) );
329
        }
330 331 332 333 334 335
        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] );
        }
336 337 338 339
        if( psz_tok )
            psz_urls = psz_tok+1;
        else
            break;
340 341 342 343 344 345 346 347 348
    }

    /* 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 )
        {
349 350
            services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
            vlc_gc_decref( p_sys->pp_items[i] );
351
        }
352
    }
353
    free( p_sys->pp_items );
354 355
    for( int i = 0; i < p_sys->i_urls; i++ )
        free( p_sys->ppsz_urls[i] );
356 357 358 359 360 361
    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;
362
}
363 364 365 366 367 368 369 370 371 372 373 374

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';
375 376 377

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

384 385 386 387 388 389 390 391 392 393 394 395
    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;
396
            p_input = input_item_New( psz_request, psz_request );
397 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
            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 );
}