input: remove input_LegacyVarStop
[vlc.git] / modules / services_discovery / podcast.c
blob4ae66669b96c14430e54a292d4f482624577a8ce
1 /*****************************************************************************
2 * podcast.c: Podcast services discovery module
3 *****************************************************************************
4 * Copyright (C) 2005-2009 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
25 * Includes
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #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>
38 #include <assert.h>
39 #include <stdnoreturn.h>
40 #include <unistd.h>
42 /************************************************************************
43 * Macros and definitions
44 ************************************************************************/
46 /*****************************************************************************
47 * Module descriptor
48 *****************************************************************************/
50 /* Callbacks */
51 static int Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
54 VLC_SD_PROBE_HELPER("podcast", N_("Podcasts"), SD_CAT_INTERNET)
56 #define URLS_TEXT N_("Podcast URLs list")
57 #define URLS_LONGTEXT N_("Enter the list of podcasts to retrieve, " \
58 "separated by '|' (pipe)." )
60 vlc_module_begin ()
61 set_shortname( "Podcast")
62 set_description( N_("Podcasts") )
63 set_category( CAT_PLAYLIST )
64 set_subcategory( SUBCAT_PLAYLIST_SD )
66 add_string( "podcast-urls", NULL,
67 URLS_TEXT, URLS_LONGTEXT, false )
69 set_capability( "services_discovery", 0 )
70 set_callbacks( Open, Close )
72 VLC_SD_PROBE_SUBMODULE
74 vlc_module_end ()
77 /*****************************************************************************
78 * Local structures
79 *****************************************************************************/
81 enum {
82 UPDATE_URLS,
83 UPDATE_REQUEST
84 }; /* FIXME Temporary. Updating by compound urls string to be removed later. */
86 typedef struct
88 /* playlist node */
89 input_thread_t **pp_input;
90 int i_input;
92 char **ppsz_urls;
93 int i_urls;
95 input_item_t **pp_items;
96 int i_items;
98 vlc_thread_t thread;
99 vlc_mutex_t lock;
100 vlc_cond_t wait;
101 bool b_update;
102 bool b_savedurls_loaded;
103 char *psz_request;
104 int update_type;
105 } services_discovery_sys_t;
107 /*****************************************************************************
108 * Local prototypes
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 if( strcmp( p_this->obj.parent->obj.object_type, "playlist" ) )
125 return VLC_EGENERIC; /* FIXME: support LibVLC SD too! */
127 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
128 services_discovery_sys_t *p_sys = malloc( sizeof( *p_sys ) );
129 if( !p_sys )
130 return VLC_ENOMEM;
132 p_sys->i_urls = 0;
133 p_sys->ppsz_urls = NULL;
134 p_sys->i_input = 0;
135 p_sys->pp_input = NULL;
136 p_sys->pp_items = NULL;
137 p_sys->i_items = 0;
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;
145 p_sd->p_sys = p_sys;
146 p_sd->description = _("Podcasts");
148 /* Launch the callback associated with this variable */
149 vlc_object_t *pl = p_sd->obj.parent;
150 var_Create( pl, "podcast-urls", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
151 var_AddCallback( pl, "podcast-urls", UrlsChange, p_sys );
153 var_Create( pl, "podcast-request", VLC_VAR_STRING );
154 var_AddCallback( pl, "podcast-request", Request, p_sys );
156 if (vlc_clone (&p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
158 var_DelCallback( pl, "podcast-request", Request, p_sys );
159 var_DelCallback( pl, "podcast-urls", UrlsChange, p_sys );
160 vlc_cond_destroy( &p_sys->wait );
161 vlc_mutex_destroy( &p_sys->lock );
162 free (p_sys);
163 return VLC_EGENERIC;
165 return VLC_SUCCESS;
168 /*****************************************************************************
169 * Close:
170 *****************************************************************************/
171 static void Close( vlc_object_t *p_this )
173 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
174 services_discovery_sys_t *p_sys = p_sd->p_sys;
175 vlc_object_t *pl = p_sd->obj.parent;
177 vlc_cancel (p_sys->thread);
178 vlc_join (p_sys->thread, NULL);
180 var_DelCallback( pl, "podcast-urls", UrlsChange, p_sys );
181 var_DelCallback( pl, "podcast-request", Request, p_sys );
182 vlc_cond_destroy( &p_sys->wait );
183 vlc_mutex_destroy( &p_sys->lock );
185 for( int i = 0; i < p_sys->i_input; i++ )
187 input_thread_t *p_input = p_sys->pp_input[i];
188 if( !p_input )
189 continue;
191 input_Stop( p_input );
192 input_Close( p_input );
194 p_sys->pp_input[i] = NULL;
196 free( p_sys->pp_input );
198 for( int i = 0; i < p_sys->i_urls; i++ )
199 free( p_sys->ppsz_urls[i] );
200 free( p_sys->ppsz_urls );
202 for( int i = 0; i < p_sys->i_items; i++ )
203 input_item_Release( p_sys->pp_items[i] );
204 free( p_sys->pp_items );
206 free( p_sys->psz_request );
207 free( p_sys );
210 /*****************************************************************************
211 * Run: main thread
212 *****************************************************************************/
213 static input_thread_t *InputCreateAndStart( services_discovery_t *sd,
214 input_item_t *item )
216 input_thread_t *input = input_Create( sd, input_LegacyEvents, NULL, item, NULL, NULL, NULL );
217 if( input != NULL && input_Start( input ) )
219 input_LegacyVarInit( input );
220 vlc_object_release( input );
221 input = NULL;
223 return input;
226 noreturn static void *Run( void *data )
228 services_discovery_t *p_sd = data;
229 services_discovery_sys_t *p_sys = p_sd->p_sys;
231 vlc_mutex_lock( &p_sys->lock );
232 mutex_cleanup_push( &p_sys->lock );
233 for( ;; )
235 while( !p_sys->b_update )
236 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
238 int canc = vlc_savecancel ();
239 msg_Dbg( p_sd, "Update required" );
241 if( p_sys->update_type == UPDATE_URLS )
243 char *psz_urls = var_GetNonEmptyString( p_sd->obj.parent,
244 "podcast-urls" );
245 ParseUrls( p_sd, psz_urls );
246 free( psz_urls );
248 else if( p_sys->update_type == UPDATE_REQUEST )
249 ParseRequest( p_sd );
251 p_sys->b_update = false;
253 for( int i = 0; i < p_sys->i_input; i++ )
255 input_thread_t *p_input = p_sys->pp_input[i];
256 int state = var_GetInteger( p_input, "state" );
258 if( state == END_S || state == ERROR_S )
260 input_Stop( p_input );
261 input_Close( p_input );
263 p_sys->pp_input[i] = NULL;
264 TAB_ERASE(p_sys->i_input, p_sys->pp_input, i);
265 i--;
268 vlc_restorecancel (canc);
270 vlc_cleanup_pop();
271 vlc_assert_unreachable(); /* dead code */
274 static int UrlsChange( vlc_object_t *p_this, char const *psz_var,
275 vlc_value_t oldval, vlc_value_t newval,
276 void *p_data )
278 VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
279 VLC_UNUSED(newval);
280 services_discovery_sys_t *p_sys = (services_discovery_sys_t *)p_data;
282 vlc_mutex_lock( &p_sys->lock );
283 p_sys->b_update = true;
284 p_sys->update_type = UPDATE_URLS;
285 vlc_cond_signal( &p_sys->wait );
286 vlc_mutex_unlock( &p_sys->lock );
287 return VLC_SUCCESS;
290 static int Request( vlc_object_t *p_this, char const *psz_var,
291 vlc_value_t oldval, vlc_value_t newval,
292 void *p_data )
294 VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
295 services_discovery_sys_t *p_sys = (services_discovery_sys_t *)p_data;
297 vlc_mutex_lock( &p_sys->lock );
298 free( p_sys->psz_request );
299 p_sys->psz_request = NULL;
300 if( newval.psz_string && *newval.psz_string ) {
301 p_sys->psz_request = strdup( newval.psz_string );
302 p_sys->b_update = true;
303 p_sys->update_type = UPDATE_REQUEST;
304 vlc_cond_signal( &p_sys->wait );
306 vlc_mutex_unlock( &p_sys->lock );
307 return VLC_SUCCESS;
310 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls )
312 services_discovery_sys_t *p_sys = p_sd->p_sys;
313 int i_new_items = 0;
314 input_item_t **pp_new_items = NULL;
316 int i_new_urls = 0;
317 char **ppsz_new_urls = NULL;
318 p_sys->b_savedurls_loaded = true;
320 int i, j;
322 for( ;; )
324 if( !psz_urls )
325 break;
327 char *psz_tok = strchr( psz_urls, '|' );
328 if( psz_tok ) *psz_tok = '\0';
330 for( i = 0; i < p_sys->i_urls; i++ )
331 if( !strcmp( psz_urls, p_sys->ppsz_urls[i] ) )
332 break;
333 if( i == p_sys->i_urls )
335 TAB_APPEND( i_new_urls, ppsz_new_urls, strdup( psz_urls ) );
337 input_item_t *p_input;
338 p_input = input_item_New( psz_urls, psz_urls );
339 input_item_AddOption( p_input, "demux=directory", VLC_INPUT_OPTION_TRUSTED );
341 TAB_APPEND( i_new_items, pp_new_items, p_input );
342 services_discovery_AddItem( p_sd, p_input );
344 TAB_APPEND( p_sys->i_input, p_sys->pp_input,
345 InputCreateAndStart( p_sd, p_input ) );
347 else
349 TAB_APPEND( i_new_urls, ppsz_new_urls,
350 strdup( p_sys->ppsz_urls[i]) );
351 TAB_APPEND( i_new_items, pp_new_items, p_sys->pp_items[i] );
353 if( psz_tok )
354 psz_urls = psz_tok+1;
355 else
356 break;
359 /* delete removed items and signal the removal */
360 for( i = 0; i<p_sys->i_items; ++i )
362 for( j = 0; j < i_new_items; ++j )
363 if( pp_new_items[j] == p_sys->pp_items[i] ) break;
364 if( j == i_new_items )
366 services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
367 input_item_Release( p_sys->pp_items[i] );
370 free( p_sys->pp_items );
371 for( int i = 0; i < p_sys->i_urls; i++ )
372 free( p_sys->ppsz_urls[i] );
373 free( p_sys->ppsz_urls );
375 p_sys->ppsz_urls = ppsz_new_urls;
376 p_sys->i_urls = i_new_urls;
377 p_sys->pp_items = pp_new_items;
378 p_sys->i_items = i_new_items;
381 static void ParseRequest( services_discovery_t *p_sd )
383 services_discovery_sys_t *p_sys = p_sd->p_sys;
385 char *psz_request = p_sys->psz_request;
387 int i;
389 char *psz_tok = strchr( psz_request, ':' );
390 if( !psz_tok ) return;
391 *psz_tok = '\0';
393 if ( ! p_sys->b_savedurls_loaded )
395 char *psz_urls = var_GetNonEmptyString( p_sd->obj.parent,
396 "podcast-urls" );
397 ParseUrls( p_sd, psz_urls );
398 free( psz_urls );
401 if( !strcmp( psz_request, "ADD" ) )
403 psz_request = psz_tok + 1;
404 for( i = 0; i<p_sys->i_urls; i++ )
405 if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
406 break;
407 if( i == p_sys->i_urls )
409 TAB_APPEND( p_sys->i_urls, p_sys->ppsz_urls,
410 strdup( psz_request ) );
412 input_item_t *p_input;
413 p_input = input_item_New( psz_request, psz_request );
414 input_item_AddOption( p_input, "demux=directory", VLC_INPUT_OPTION_TRUSTED );
416 TAB_APPEND( p_sys->i_items, p_sys->pp_items, p_input );
417 services_discovery_AddItem( p_sd, p_input );
419 TAB_APPEND( p_sys->i_input, p_sys->pp_input,
420 InputCreateAndStart( p_sd, p_input ) );
421 SaveUrls( p_sd );
424 else if ( !strcmp( psz_request, "RM" ) )
426 psz_request = psz_tok + 1;
427 for( i = 0; i < p_sys->i_urls; i++ )
428 if( !strcmp(p_sys->ppsz_urls[i], psz_request) )
430 services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
431 input_item_Release( p_sys->pp_items[i] );
432 TAB_ERASE(p_sys->i_urls, p_sys->ppsz_urls, i );
433 TAB_ERASE(p_sys->i_items, p_sys->pp_items, i );
434 break;
436 SaveUrls( p_sd );
439 free( p_sys->psz_request );
440 p_sys->psz_request = NULL;
443 static void SaveUrls( services_discovery_t *p_sd )
445 services_discovery_sys_t *p_sys = p_sd->p_sys;
446 int i;
447 char *psz_urls;
448 int len = 0;
450 for( i=0; i < p_sys->i_urls; i++ )
451 len += strlen( p_sys->ppsz_urls[i] ) + 1;
453 psz_urls = (char*) calloc( len, sizeof(char) );
455 for( i=0; i < p_sys->i_urls; i++ )
457 strcat( psz_urls, p_sys->ppsz_urls[i] );
458 if( i < p_sys->i_urls - 1 ) strcat( psz_urls, "|" );
461 config_PutPsz( "podcast-urls", psz_urls );
463 free( psz_urls );