1 /*****************************************************************************
2 * podcast.c: Podcast services discovery module
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_playlist.h>
35 #include <vlc_network.h>
38 #include <errno.h> /* ENOMEM */
43 #ifdef HAVE_SYS_TIME_H
44 # include <sys/time.h>
47 /************************************************************************
48 * Macros and definitions
49 ************************************************************************/
51 /*****************************************************************************
53 *****************************************************************************/
56 static int Open ( vlc_object_t * );
57 static void Close( vlc_object_t * );
59 #define URLS_TEXT N_("Podcast URLs list")
60 #define URLS_LONGTEXT N_("Enter the list of podcasts to retrieve, " \
61 "separated by '|' (pipe)." )
64 set_shortname( "Podcast");
65 set_description( N_("Podcasts") );
66 set_category( CAT_PLAYLIST );
67 set_subcategory( SUBCAT_PLAYLIST_SD );
69 add_string( "podcast-urls", NULL, NULL,
70 URLS_TEXT, URLS_LONGTEXT, false );
73 set_capability( "services_discovery", 0 );
74 set_callbacks( Open, Close );
79 /*****************************************************************************
81 *****************************************************************************/
83 struct services_discovery_sys_t
86 input_thread_t **pp_input;
97 /*****************************************************************************
99 *****************************************************************************/
100 static void Run( services_discovery_t *p_intf );
101 static int UrlsChange( vlc_object_t *, char const *, vlc_value_t,
102 vlc_value_t, void * );
103 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls );
105 /*****************************************************************************
106 * Open: initialize and create stuff
107 *****************************************************************************/
108 static int Open( vlc_object_t *p_this )
110 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
111 services_discovery_sys_t *p_sys = malloc(
112 sizeof( services_discovery_sys_t ) );
117 p_sys->ppsz_urls = NULL;
119 p_sys->pp_input = NULL;
120 vlc_mutex_init( &p_sys->lock );
121 vlc_cond_init( &p_sys->wait );
122 p_sys->b_update = true;
128 services_discovery_SetLocalizedName( p_sd, _("Podcasts") );
130 /* Launch the callback associated with this variable */
131 var_Create( p_sd, "podcast-urls", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
132 var_AddCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
137 /*****************************************************************************
139 *****************************************************************************/
140 static void Close( vlc_object_t *p_this )
142 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
143 services_discovery_sys_t *p_sys = p_sd->p_sys;
146 var_DelCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
147 vlc_cond_destroy( &p_sys->wait );
148 vlc_mutex_destroy( &p_sys->lock );
150 for( i = 0; i < p_sys->i_input; i++ )
152 if( p_sd->p_sys->pp_input[i] )
154 input_StopThread( p_sd->p_sys->pp_input[i] );
155 vlc_object_release( p_sd->p_sys->pp_input[i] );
156 p_sd->p_sys->pp_input[i] = NULL;
159 free( p_sd->p_sys->pp_input );
160 for( i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
161 free( p_sys->ppsz_urls );
165 /*****************************************************************************
167 *****************************************************************************/
168 static void Run( services_discovery_t *p_sd )
170 services_discovery_sys_t *p_sys = p_sd->p_sys;
172 vlc_mutex_lock( &p_sys->lock );
173 mutex_cleanup_push( &p_sys->lock );
176 while( !p_sys->b_update )
177 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
179 int canc = vlc_savecancel ();
180 msg_Dbg( p_sd, "Update required" );
181 char* psz_urls = var_GetNonEmptyString( p_sd, "podcast-urls" );
182 if( psz_urls != NULL )
183 ParseUrls( p_sd, psz_urls );
185 p_sys->b_update = false;
187 for( int i = 0; i < p_sd->p_sys->i_input; i++ )
189 if( p_sd->p_sys->pp_input[i]->b_eof
190 || p_sd->p_sys->pp_input[i]->b_error )
192 input_StopThread( p_sd->p_sys->pp_input[i] );
193 vlc_object_release( p_sd->p_sys->pp_input[i] );
194 p_sd->p_sys->pp_input[i] = NULL;
195 REMOVE_ELEM( p_sys->pp_input, p_sys->i_input, i );
199 vlc_restorecancel (canc);
202 assert(0); /* dead code */
205 static int UrlsChange( vlc_object_t *p_this, char const *psz_var,
206 vlc_value_t oldval, vlc_value_t newval,
209 VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
211 services_discovery_sys_t *p_sys = (services_discovery_sys_t *)p_data;
213 vlc_mutex_lock( &p_sys->lock );
214 p_sys->b_update = true;
215 vlc_cond_signal( &p_sys->wait );
216 vlc_mutex_unlock( &p_sys->lock );
220 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls )
222 services_discovery_sys_t *p_sys = p_sd->p_sys;
226 char *psz_tok = strchr( psz_urls, '|' );
227 if( psz_tok ) *psz_tok = '\0';
228 for( i = 0; i < p_sys->i_urls; i++ )
229 if( !strcmp( psz_urls, p_sys->ppsz_urls[i] ) )
231 if( i == p_sys->i_urls )
233 /* Only add new urls.
234 * FIXME: We don't delete urls which have been removed from
235 * the config since we don't have a way to know which inputs
237 input_item_t *p_input;
238 INSERT_ELEM( p_sys->ppsz_urls, p_sys->i_urls, p_sys->i_urls,
239 strdup( psz_urls ) );
240 p_input = input_item_NewExt( p_sd, psz_urls,
241 psz_urls, 0, NULL, -1 );
242 input_item_AddOption( p_input, "demux=podcast" );
243 services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
244 vlc_gc_decref( p_input );
245 INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
246 input_CreateThread( p_sd, p_input ) );
248 if( psz_tok ) psz_urls = psz_tok+1;