1 /*****************************************************************************
2 * podcast.c: Podcast services discovery module
3 *****************************************************************************
4 * Copyright (C) 2005-2009 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_services_discovery.h>
36 #include <vlc_network.h>
43 /************************************************************************
44 * Macros and definitions
45 ************************************************************************/
47 /*****************************************************************************
49 *****************************************************************************/
52 static int Open ( vlc_object_t * );
53 static void Close( vlc_object_t * );
55 VLC_SD_PROBE_HELPER("podcast", "Podcasts", SD_CAT_INTERNET)
57 #define URLS_TEXT N_("Podcast URLs list")
58 #define URLS_LONGTEXT N_("Enter the list of podcasts to retrieve, " \
59 "separated by '|' (pipe)." )
62 set_shortname( "Podcast")
63 set_description( N_("Podcasts") )
64 set_category( CAT_PLAYLIST )
65 set_subcategory( SUBCAT_PLAYLIST_SD )
67 add_string( "podcast-urls", NULL,
68 URLS_TEXT, URLS_LONGTEXT, false )
70 set_capability( "services_discovery", 0 )
71 set_callbacks( Open, Close )
73 VLC_SD_PROBE_SUBMODULE
78 /*****************************************************************************
80 *****************************************************************************/
85 }; /* FIXME Temporary. Updating by compound urls string to be removed later. */
87 struct services_discovery_sys_t
90 input_thread_t **pp_input;
96 input_item_t **pp_items;
107 /*****************************************************************************
109 *****************************************************************************/
110 static void *Run( void * );
111 static int UrlsChange( vlc_object_t *, char const *, vlc_value_t,
112 vlc_value_t, void * );
113 static int Request( vlc_object_t *, char const *, vlc_value_t,
114 vlc_value_t, void * );
115 static void ParseRequest( services_discovery_t *p_sd );
116 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls );
117 static void SaveUrls( services_discovery_t *p_sd );
119 /*****************************************************************************
120 * Open: initialize and create stuff
121 *****************************************************************************/
122 static int Open( vlc_object_t *p_this )
124 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
125 services_discovery_sys_t *p_sys = malloc(
126 sizeof( services_discovery_sys_t ) );
131 p_sys->ppsz_urls = NULL;
133 p_sys->pp_input = NULL;
134 p_sys->pp_items = NULL;
136 vlc_mutex_init( &p_sys->lock );
137 vlc_cond_init( &p_sys->wait );
138 p_sys->b_update = true;
139 p_sys->psz_request = NULL;
140 p_sys->update_type = UPDATE_URLS;
144 /* Launch the callback associated with this variable */
145 var_Create( p_sd, "podcast-urls", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
146 var_AddCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
148 var_Create( p_sd, "podcast-request", VLC_VAR_STRING );
149 var_AddCallback( p_sd, "podcast-request", Request, p_sys );
151 if (vlc_clone (&p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
153 var_DelCallback( p_sd, "podcast-request", Request, p_sys );
154 var_DelCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
155 vlc_cond_destroy( &p_sys->wait );
156 vlc_mutex_destroy( &p_sys->lock );
163 /*****************************************************************************
165 *****************************************************************************/
166 static void Close( vlc_object_t *p_this )
168 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
169 services_discovery_sys_t *p_sys = p_sd->p_sys;
172 vlc_cancel (p_sys->thread);
173 vlc_join (p_sys->thread, NULL);
175 var_DelCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
176 var_DelCallback( p_sd, "podcast-request", Request, p_sys );
177 vlc_cond_destroy( &p_sys->wait );
178 vlc_mutex_destroy( &p_sys->lock );
180 for( i = 0; i < p_sys->i_input; i++ )
182 input_thread_t *p_input = p_sd->p_sys->pp_input[i];
186 input_Stop( p_input, true );
187 input_Close( p_input );
189 p_sd->p_sys->pp_input[i] = NULL;
191 free( p_sd->p_sys->pp_input );
192 for( i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
193 free( p_sys->ppsz_urls );
194 for( i = 0; i < p_sys->i_items; i++ ) vlc_gc_decref( p_sys->pp_items[i] );
195 free( p_sys->pp_items );
196 free( p_sys->psz_request );
200 /*****************************************************************************
202 *****************************************************************************/
204 static void *Run( void *data )
206 services_discovery_t *p_sd = data;
207 services_discovery_sys_t *p_sys = p_sd->p_sys;
209 vlc_mutex_lock( &p_sys->lock );
210 mutex_cleanup_push( &p_sys->lock );
213 while( !p_sys->b_update )
214 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
216 int canc = vlc_savecancel ();
217 msg_Dbg( p_sd, "Update required" );
219 if( p_sys->update_type == UPDATE_URLS )
221 char* psz_urls = var_GetNonEmptyString( p_sd, "podcast-urls" );
222 ParseUrls( p_sd, psz_urls );
225 else if( p_sys->update_type == UPDATE_REQUEST )
227 ParseRequest( p_sd );
230 p_sys->b_update = false;
232 for( int i = 0; i < p_sd->p_sys->i_input; i++ )
234 input_thread_t *p_input = p_sd->p_sys->pp_input[i];
236 if( p_input->b_eof || p_input->b_error )
238 input_Stop( p_input, false );
239 input_Close( p_input );
241 p_sd->p_sys->pp_input[i] = NULL;
242 REMOVE_ELEM( p_sys->pp_input, p_sys->i_input, i );
246 vlc_restorecancel (canc);
249 assert(0); /* dead code */
252 static int UrlsChange( vlc_object_t *p_this, char const *psz_var,
253 vlc_value_t oldval, vlc_value_t newval,
256 VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
258 services_discovery_sys_t *p_sys = (services_discovery_sys_t *)p_data;
260 vlc_mutex_lock( &p_sys->lock );
261 p_sys->b_update = true;
262 p_sys->update_type = UPDATE_URLS;
263 vlc_cond_signal( &p_sys->wait );
264 vlc_mutex_unlock( &p_sys->lock );
268 static int Request( vlc_object_t *p_this, char const *psz_var,
269 vlc_value_t oldval, vlc_value_t newval,
272 VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
273 services_discovery_sys_t *p_sys = (services_discovery_sys_t *)p_data;
275 vlc_mutex_lock( &p_sys->lock );
276 free( p_sys->psz_request );
277 p_sys->psz_request = NULL;
278 if( newval.psz_string && *newval.psz_string ) {
279 p_sys->psz_request = strdup( newval.psz_string );
280 p_sys->b_update = true;
281 p_sys->update_type = UPDATE_REQUEST;
282 vlc_cond_signal( &p_sys->wait );
284 vlc_mutex_unlock( &p_sys->lock );
288 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls )
290 services_discovery_sys_t *p_sys = p_sd->p_sys;
292 input_item_t **pp_new_items = NULL;
295 char **ppsz_new_urls = NULL;
301 if( !psz_urls ) break;
303 char *psz_tok = strchr( psz_urls, '|' );
304 if( psz_tok ) *psz_tok = '\0';
306 for( i = 0; i < p_sys->i_urls; i++ )
307 if( !strcmp( psz_urls, p_sys->ppsz_urls[i] ) )
309 if( i == p_sys->i_urls )
311 INSERT_ELEM( ppsz_new_urls, i_new_urls, i_new_urls,
312 strdup( psz_urls ) );
314 input_item_t *p_input;
315 p_input = input_item_New( psz_urls, psz_urls );
316 input_item_AddOption( p_input, "demux=podcast", VLC_INPUT_OPTION_TRUSTED );
318 INSERT_ELEM( pp_new_items, i_new_items, i_new_items, p_input );
319 services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
321 INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
322 input_CreateAndStart( p_sd, p_input, NULL ) );
326 INSERT_ELEM( ppsz_new_urls, i_new_urls, i_new_urls,
327 strdup( p_sys->ppsz_urls[i]) );
328 INSERT_ELEM( pp_new_items, i_new_items, i_new_items, p_sys->pp_items[i] );
330 if( psz_tok ) psz_urls = psz_tok+1;
334 /* delete removed items and signal the removal */
335 for( i = 0; i<p_sys->i_items; ++i )
337 for( j = 0; j < i_new_items; ++j )
338 if( pp_new_items[j] == p_sys->pp_items[i] ) break;
339 if( j == i_new_items )
341 services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
342 vlc_gc_decref( p_sys->pp_items[i] );
345 free( p_sys->pp_items );
346 for( int i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
347 free( p_sys->ppsz_urls );
349 p_sys->ppsz_urls = ppsz_new_urls;
350 p_sys->i_urls = i_new_urls;
351 p_sys->pp_items = pp_new_items;
352 p_sys->i_items = i_new_items;
355 static void ParseRequest( services_discovery_t *p_sd )
357 services_discovery_sys_t *p_sys = p_sd->p_sys;
359 char *psz_request = p_sys->psz_request;
363 char *psz_tok = strchr( psz_request, ':' );
364 if( !psz_tok ) return;
366 if( !strcmp( psz_request, "ADD" ) )
368 psz_request = psz_tok + 1;
369 for( i = 0; i<p_sys->i_urls; i++ )
370 if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
372 if( i == p_sys->i_urls )
374 INSERT_ELEM( p_sys->ppsz_urls, p_sys->i_urls, p_sys->i_urls,
375 strdup( psz_request ) );
377 input_item_t *p_input;
378 p_input = input_item_New( psz_request, psz_request );
379 input_item_AddOption( p_input, "demux=podcast", VLC_INPUT_OPTION_TRUSTED );
381 INSERT_ELEM( p_sys->pp_items, p_sys->i_items, p_sys->i_items, p_input );
382 services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
384 INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
385 input_CreateAndStart( p_sd, p_input, NULL ) );
389 else if ( !strcmp( psz_request, "RM" ) )
391 psz_request = psz_tok + 1;
392 for( i = 0; i<p_sys->i_urls; i++ )
393 if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
395 if( i != p_sys->i_urls )
397 services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
398 vlc_gc_decref( p_sys->pp_items[i] );
399 REMOVE_ELEM( p_sys->ppsz_urls, p_sys->i_urls, i );
400 REMOVE_ELEM( p_sys->pp_items, p_sys->i_items, i );
405 free( p_sys->psz_request );
406 p_sys->psz_request = NULL;
409 static void SaveUrls( services_discovery_t *p_sd )
411 services_discovery_sys_t *p_sys = p_sd->p_sys;
416 for( i=0; i < p_sys->i_urls; i++ )
417 len += strlen( p_sys->ppsz_urls[i] ) + 1;
419 psz_urls = (char*) calloc( len, sizeof(char) );
421 for( i=0; i < p_sys->i_urls; i++ )
423 strcat( psz_urls, p_sys->ppsz_urls[i] );
424 if( i < p_sys->i_urls - 1 ) strcat( psz_urls, "|" );
427 config_PutPsz( p_sd, "podcast-urls", psz_urls );