podcast.c 8.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*****************************************************************************
 * podcast.c:  Podcast services discovery module
 *****************************************************************************
 * Copyright (C) 2005 the VideoLAN team
 * $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
#include <vlc_common.h>
33
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
34
#include <vlc_playlist.h>
35 36
#include <vlc_services_discovery.h>

Clément Stenac's avatar
Clément Stenac committed
37
#include <vlc_network.h>
38
#include <assert.h>
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

#include <errno.h>                                                 /* ENOMEM */

#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#endif
#ifdef HAVE_SYS_TIME_H
#    include <sys/time.h>
#endif

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

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

/* Callbacks */
58 59
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
60 61

#define URLS_TEXT N_("Podcast URLs list")
62 63
#define URLS_LONGTEXT N_("Enter the list of podcasts to retrieve, " \
                         "separated by '|' (pipe)." )
64

65 66 67 68 69
vlc_module_begin ()
    set_shortname( "Podcast")
    set_description( N_("Podcasts") )
    set_category( CAT_PLAYLIST )
    set_subcategory( SUBCAT_PLAYLIST_SD )
70 71

    add_string( "podcast-urls", NULL, NULL,
72
                URLS_TEXT, URLS_LONGTEXT, false );
73
        change_autosave ()
74

75 76
    set_capability( "services_discovery", 0 )
    set_callbacks( Open, Close )
77

78
vlc_module_end ()
79 80 81 82 83 84 85 86 87


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

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
    vlc_thread_t thread;
95 96
    vlc_mutex_t lock;
    vlc_cond_t  wait;
97
    bool b_update;
98 99 100 101 102
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
103
static void *Run( void * );
104 105 106
static int UrlsChange( vlc_object_t *, char const *, vlc_value_t,
                       vlc_value_t, void * );
static void ParseUrls( services_discovery_t *p_sd, char *psz_urls );
107 108 109 110 111 112 113 114 115

/*****************************************************************************
 * 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  = malloc(
                                    sizeof( services_discovery_sys_t ) );
116 117
    if( !p_sys )
        return VLC_ENOMEM;
118

119 120
    p_sys->i_urls = 0;
    p_sys->ppsz_urls = NULL;
121
    p_sys->i_input = 0;
122
    p_sys->pp_input = NULL;
123 124
    vlc_mutex_init( &p_sys->lock );
    vlc_cond_init( &p_sys->wait );
125
    p_sys->b_update = true;
126

127
    p_sd->p_sys  = p_sys;
128

129 130 131 132
    /* Launch the callback associated with this variable */
    var_Create( p_sd, "podcast-urls", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_AddCallback( p_sd, "podcast-urls", UrlsChange, p_sys );

133 134 135 136 137
    if (vlc_clone (&p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
    {
        free (p_sys);
        return VLC_EGENERIC;
    }
138 139 140 141 142 143 144 145 146 147 148
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close:
 *****************************************************************************/
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;
    int i;
149

150 151 152
    vlc_cancel (p_sys->thread);
    vlc_join (p_sys->thread, NULL);

153 154 155 156
    var_DelCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
    vlc_cond_destroy( &p_sys->wait );
    vlc_mutex_destroy( &p_sys->lock );

157
    for( i = 0; i < p_sys->i_input; i++ )
158 159 160 161
    {
        if( p_sd->p_sys->pp_input[i] )
        {
            input_StopThread( p_sd->p_sys->pp_input[i] );
162
            vlc_object_release( p_sd->p_sys->pp_input[i] );
163 164 165 166 167 168 169 170 171 172 173 174
            p_sd->p_sys->pp_input[i] = NULL;
        }
    }
    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 );
    free( p_sys );
}

/*****************************************************************************
 * Run: main thread
 *****************************************************************************/
175
static void *Run( void *data )
176
{
177
    services_discovery_t *p_sd = data;
178
    services_discovery_sys_t *p_sys  = p_sd->p_sys;
179

180 181
    vlc_mutex_lock( &p_sys->lock );
    mutex_cleanup_push( &p_sys->lock );
182
    for( ;; )
183
    {
184 185
        while( !p_sys->b_update )
            vlc_cond_wait( &p_sys->wait, &p_sys->lock );
186

187 188 189 190 191 192 193
        int canc = vlc_savecancel ();
        msg_Dbg( p_sd, "Update required" );
        char* psz_urls = var_GetNonEmptyString( p_sd, "podcast-urls" );
        if( psz_urls != NULL )
            ParseUrls( p_sd, psz_urls );
        free( psz_urls );
        p_sys->b_update = false;
194

195
        for( int i = 0; i < p_sd->p_sys->i_input; i++ )
196
        {
197 198
            if( p_sd->p_sys->pp_input[i]->b_eof
                || p_sd->p_sys->pp_input[i]->b_error )
199 200
            {
                input_StopThread( p_sd->p_sys->pp_input[i] );
201
                vlc_object_release( p_sd->p_sys->pp_input[i] );
202
                p_sd->p_sys->pp_input[i] = NULL;
203 204
                REMOVE_ELEM( p_sys->pp_input, p_sys->i_input, i );
                i--;
205
            }
206
        }
207
        vlc_restorecancel (canc);
208
    }
209 210
    vlc_cleanup_pop();
    assert(0); /* dead code */
211 212 213 214 215 216
}

static int UrlsChange( vlc_object_t *p_this, char const *psz_var,
                       vlc_value_t oldval, vlc_value_t newval,
                       void *p_data )
{
217 218
    VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
    VLC_UNUSED(newval);
219
    services_discovery_sys_t *p_sys  = (services_discovery_sys_t *)p_data;
220 221

    vlc_mutex_lock( &p_sys->lock );
222
    p_sys->b_update = true;
223 224
    vlc_cond_signal( &p_sys->wait );
    vlc_mutex_unlock( &p_sys->lock );
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    return VLC_SUCCESS;
}

static void ParseUrls( services_discovery_t *p_sd, char *psz_urls )
{
    services_discovery_sys_t *p_sys = p_sd->p_sys;
    for( ;; )
    {
        int i;
        char *psz_tok = strchr( psz_urls, '|' );
        if( psz_tok ) *psz_tok = '\0';
        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 )
        {
            /* Only add new urls.
             * FIXME: We don't delete urls which have been removed from
             * the config since we don't have a way to know which inputs
             * they spawned */
            input_item_t *p_input;
            INSERT_ELEM( p_sys->ppsz_urls, p_sys->i_urls, p_sys->i_urls,
                         strdup( psz_urls ) );
248
            p_input = input_item_NewExt( p_sd, psz_urls,
249
                                        psz_urls, 0, NULL, -1 );
250
            input_item_AddOption( p_input, "demux=podcast", VLC_INPUT_OPTION_TRUSTED );
251
            services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
252
            vlc_gc_decref( p_input );
253 254 255 256 257
            INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
                         input_CreateThread( p_sd, p_input ) );
        }
        if( psz_tok )  psz_urls = psz_tok+1;
        else           return;
258 259
    }
}