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>
35 #include <vlc_playlist.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;
103 bool b_savedurls_loaded;
108 /*****************************************************************************
110 *****************************************************************************/
111 static void *Run( void * );
112 static int UrlsChange( vlc_object_t *, char const *, vlc_value_t,
113 vlc_value_t, void * );
114 static int Request( vlc_object_t *, char const *, vlc_value_t,
115 vlc_value_t, void * );
116 static void ParseRequest( services_discovery_t *p_sd );
117 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls );
118 static void SaveUrls( services_discovery_t *p_sd );
120 /*****************************************************************************
121 * Open: initialize and create stuff
122 *****************************************************************************/
123 static int Open( vlc_object_t *p_this )
125 playlist_t *pl = pl_Get( p_this );
126 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
127 services_discovery_sys_t *p_sys = malloc(
128 sizeof( services_discovery_sys_t ) );
133 p_sys->ppsz_urls = NULL;
135 p_sys->pp_input = NULL;
136 p_sys->pp_items = NULL;
138 vlc_mutex_init( &p_sys->lock );
139 vlc_cond_init( &p_sys->wait );
140 p_sys->b_update = true;
141 p_sys->b_savedurls_loaded = false;
142 p_sys->psz_request = NULL;
143 p_sys->update_type = UPDATE_URLS;
147 /* Launch the callback associated with this variable */
148 var_Create( pl, "podcast-urls", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
149 var_AddCallback( pl, "podcast-urls", UrlsChange, p_sys );
151 var_Create( pl, "podcast-request", VLC_VAR_STRING );
152 var_AddCallback( pl, "podcast-request", Request, p_sys );
154 if (vlc_clone (&p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
156 var_DelCallback( pl, "podcast-request", Request, p_sys );
157 var_DelCallback( pl, "podcast-urls", UrlsChange, p_sys );
158 vlc_cond_destroy( &p_sys->wait );
159 vlc_mutex_destroy( &p_sys->lock );
166 /*****************************************************************************
168 *****************************************************************************/
169 static void Close( vlc_object_t *p_this )
171 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
172 services_discovery_sys_t *p_sys = p_sd->p_sys;
173 playlist_t *pl = pl_Get( p_this );
176 vlc_cancel (p_sys->thread);
177 vlc_join (p_sys->thread, NULL);
179 var_DelCallback( pl, "podcast-urls", UrlsChange, p_sys );
180 var_DelCallback( pl, "podcast-request", Request, p_sys );
181 vlc_cond_destroy( &p_sys->wait );
182 vlc_mutex_destroy( &p_sys->lock );
184 for( i = 0; i < p_sys->i_input; i++ )
186 input_thread_t *p_input = p_sd->p_sys->pp_input[i];
190 input_Stop( p_input, true );
191 input_Close( p_input );
193 p_sd->p_sys->pp_input[i] = NULL;
195 free( p_sd->p_sys->pp_input );
196 for( i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
197 free( p_sys->ppsz_urls );
198 for( i = 0; i < p_sys->i_items; i++ ) vlc_gc_decref( p_sys->pp_items[i] );
199 free( p_sys->pp_items );
200 free( p_sys->psz_request );
204 /*****************************************************************************
206 *****************************************************************************/
208 static void *Run( void *data )
210 services_discovery_t *p_sd = data;
211 services_discovery_sys_t *p_sys = p_sd->p_sys;
213 vlc_mutex_lock( &p_sys->lock );
214 mutex_cleanup_push( &p_sys->lock );
217 while( !p_sys->b_update )
218 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
220 int canc = vlc_savecancel ();
221 msg_Dbg( p_sd, "Update required" );
223 if( p_sys->update_type == UPDATE_URLS )
225 char* psz_urls = var_GetNonEmptyString( p_sd, "podcast-urls" );
226 ParseUrls( p_sd, psz_urls );
229 else if( p_sys->update_type == UPDATE_REQUEST )
231 ParseRequest( p_sd );
234 p_sys->b_update = false;
236 for( int i = 0; i < p_sd->p_sys->i_input; i++ )
238 input_thread_t *p_input = p_sd->p_sys->pp_input[i];
240 if( p_input->b_eof || p_input->b_error )
242 input_Stop( p_input, false );
243 input_Close( p_input );
245 p_sd->p_sys->pp_input[i] = NULL;
246 REMOVE_ELEM( p_sys->pp_input, p_sys->i_input, i );
250 vlc_restorecancel (canc);
253 assert(0); /* dead code */
256 static int UrlsChange( vlc_object_t *p_this, char const *psz_var,
257 vlc_value_t oldval, vlc_value_t newval,
260 VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
262 services_discovery_sys_t *p_sys = (services_discovery_sys_t *)p_data;
264 vlc_mutex_lock( &p_sys->lock );
265 p_sys->b_update = true;
266 p_sys->update_type = UPDATE_URLS;
267 vlc_cond_signal( &p_sys->wait );
268 vlc_mutex_unlock( &p_sys->lock );
272 static int Request( vlc_object_t *p_this, char const *psz_var,
273 vlc_value_t oldval, vlc_value_t newval,
276 VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
277 services_discovery_sys_t *p_sys = (services_discovery_sys_t *)p_data;
279 vlc_mutex_lock( &p_sys->lock );
280 free( p_sys->psz_request );
281 p_sys->psz_request = NULL;
282 if( newval.psz_string && *newval.psz_string ) {
283 p_sys->psz_request = strdup( newval.psz_string );
284 p_sys->b_update = true;
285 p_sys->update_type = UPDATE_REQUEST;
286 vlc_cond_signal( &p_sys->wait );
288 vlc_mutex_unlock( &p_sys->lock );
292 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls )
294 services_discovery_sys_t *p_sys = p_sd->p_sys;
296 input_item_t **pp_new_items = NULL;
299 char **ppsz_new_urls = NULL;
300 p_sys->b_savedurls_loaded = true;
306 if( !psz_urls ) break;
308 char *psz_tok = strchr( psz_urls, '|' );
309 if( psz_tok ) *psz_tok = '\0';
311 for( i = 0; i < p_sys->i_urls; i++ )
312 if( !strcmp( psz_urls, p_sys->ppsz_urls[i] ) )
314 if( i == p_sys->i_urls )
316 INSERT_ELEM( ppsz_new_urls, i_new_urls, i_new_urls,
317 strdup( psz_urls ) );
319 input_item_t *p_input;
320 p_input = input_item_New( psz_urls, psz_urls );
321 input_item_AddOption( p_input, "demux=podcast", VLC_INPUT_OPTION_TRUSTED );
323 INSERT_ELEM( pp_new_items, i_new_items, i_new_items, p_input );
324 services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
326 INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
327 input_CreateAndStart( p_sd, p_input, NULL ) );
331 INSERT_ELEM( ppsz_new_urls, i_new_urls, i_new_urls,
332 strdup( p_sys->ppsz_urls[i]) );
333 INSERT_ELEM( pp_new_items, i_new_items, i_new_items, p_sys->pp_items[i] );
335 if( psz_tok ) psz_urls = psz_tok+1;
339 /* delete removed items and signal the removal */
340 for( i = 0; i<p_sys->i_items; ++i )
342 for( j = 0; j < i_new_items; ++j )
343 if( pp_new_items[j] == p_sys->pp_items[i] ) break;
344 if( j == i_new_items )
346 services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
347 vlc_gc_decref( p_sys->pp_items[i] );
350 free( p_sys->pp_items );
351 for( int i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
352 free( p_sys->ppsz_urls );
354 p_sys->ppsz_urls = ppsz_new_urls;
355 p_sys->i_urls = i_new_urls;
356 p_sys->pp_items = pp_new_items;
357 p_sys->i_items = i_new_items;
360 static void ParseRequest( services_discovery_t *p_sd )
362 services_discovery_sys_t *p_sys = p_sd->p_sys;
364 char *psz_request = p_sys->psz_request;
368 char *psz_tok = strchr( psz_request, ':' );
369 if( !psz_tok ) return;
372 if ( ! p_sys->b_savedurls_loaded )
374 char* psz_urls = var_GetNonEmptyString( p_sd, "podcast-urls" );
375 ParseUrls( p_sd, psz_urls );
379 if( !strcmp( psz_request, "ADD" ) )
381 psz_request = psz_tok + 1;
382 for( i = 0; i<p_sys->i_urls; i++ )
383 if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
385 if( i == p_sys->i_urls )
387 INSERT_ELEM( p_sys->ppsz_urls, p_sys->i_urls, p_sys->i_urls,
388 strdup( psz_request ) );
390 input_item_t *p_input;
391 p_input = input_item_New( psz_request, psz_request );
392 input_item_AddOption( p_input, "demux=podcast", VLC_INPUT_OPTION_TRUSTED );
394 INSERT_ELEM( p_sys->pp_items, p_sys->i_items, p_sys->i_items, p_input );
395 services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
397 INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
398 input_CreateAndStart( p_sd, p_input, NULL ) );
402 else if ( !strcmp( psz_request, "RM" ) )
404 psz_request = psz_tok + 1;
405 for( i = 0; i<p_sys->i_urls; i++ )
406 if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
408 if( i != p_sys->i_urls )
410 services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
411 vlc_gc_decref( p_sys->pp_items[i] );
412 REMOVE_ELEM( p_sys->ppsz_urls, p_sys->i_urls, i );
413 REMOVE_ELEM( p_sys->pp_items, p_sys->i_items, i );
418 free( p_sys->psz_request );
419 p_sys->psz_request = NULL;
422 static void SaveUrls( services_discovery_t *p_sd )
424 services_discovery_sys_t *p_sys = p_sd->p_sys;
429 for( i=0; i < p_sys->i_urls; i++ )
430 len += strlen( p_sys->ppsz_urls[i] ) + 1;
432 psz_urls = (char*) calloc( len, sizeof(char) );
434 for( i=0; i < p_sys->i_urls; i++ )
436 strcat( psz_urls, p_sys->ppsz_urls[i] );
437 if( i < p_sys->i_urls - 1 ) strcat( psz_urls, "|" );
440 config_PutPsz( p_sd, "podcast-urls", psz_urls );