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 #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>
39 #include <stdnoreturn.h>
42 /************************************************************************
43 * Macros and definitions
44 ************************************************************************/
46 /*****************************************************************************
48 *****************************************************************************/
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)." )
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
77 /*****************************************************************************
79 *****************************************************************************/
84 }; /* FIXME Temporary. Updating by compound urls string to be removed later. */
86 struct services_discovery_sys_t
89 input_thread_t
**pp_input
;
95 input_item_t
**pp_items
;
102 bool b_savedurls_loaded
;
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 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
) );
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
;
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
);
168 /*****************************************************************************
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_sd
->p_sys
->pp_input
[i
];
191 input_Stop( p_input
);
192 input_Close( p_input
);
194 p_sd
->p_sys
->pp_input
[i
] = NULL
;
196 free( p_sd
->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
);
210 /*****************************************************************************
212 *****************************************************************************/
213 noreturn
static void *Run( void *data
)
215 services_discovery_t
*p_sd
= data
;
216 services_discovery_sys_t
*p_sys
= p_sd
->p_sys
;
218 vlc_mutex_lock( &p_sys
->lock
);
219 mutex_cleanup_push( &p_sys
->lock
);
222 while( !p_sys
->b_update
)
223 vlc_cond_wait( &p_sys
->wait
, &p_sys
->lock
);
225 int canc
= vlc_savecancel ();
226 msg_Dbg( p_sd
, "Update required" );
228 if( p_sys
->update_type
== UPDATE_URLS
)
230 char *psz_urls
= var_GetNonEmptyString( p_sd
->obj
.parent
,
232 ParseUrls( p_sd
, psz_urls
);
235 else if( p_sys
->update_type
== UPDATE_REQUEST
)
236 ParseRequest( p_sd
);
238 p_sys
->b_update
= false;
240 for( int i
= 0; i
< p_sd
->p_sys
->i_input
; i
++ )
242 input_thread_t
*p_input
= p_sd
->p_sys
->pp_input
[i
];
243 int state
= var_GetInteger( p_input
, "state" );
245 if( state
== END_S
|| state
== ERROR_S
)
247 input_Stop( p_input
);
248 input_Close( p_input
);
250 p_sd
->p_sys
->pp_input
[i
] = NULL
;
251 TAB_ERASE(p_sys
->i_input
, p_sys
->pp_input
, i
);
255 vlc_restorecancel (canc
);
258 vlc_assert_unreachable(); /* dead code */
261 static int UrlsChange( vlc_object_t
*p_this
, char const *psz_var
,
262 vlc_value_t oldval
, vlc_value_t newval
,
265 VLC_UNUSED(p_this
); VLC_UNUSED(psz_var
); VLC_UNUSED(oldval
);
267 services_discovery_sys_t
*p_sys
= (services_discovery_sys_t
*)p_data
;
269 vlc_mutex_lock( &p_sys
->lock
);
270 p_sys
->b_update
= true;
271 p_sys
->update_type
= UPDATE_URLS
;
272 vlc_cond_signal( &p_sys
->wait
);
273 vlc_mutex_unlock( &p_sys
->lock
);
277 static int Request( vlc_object_t
*p_this
, char const *psz_var
,
278 vlc_value_t oldval
, vlc_value_t newval
,
281 VLC_UNUSED(p_this
); VLC_UNUSED(psz_var
); VLC_UNUSED(oldval
);
282 services_discovery_sys_t
*p_sys
= (services_discovery_sys_t
*)p_data
;
284 vlc_mutex_lock( &p_sys
->lock
);
285 free( p_sys
->psz_request
);
286 p_sys
->psz_request
= NULL
;
287 if( newval
.psz_string
&& *newval
.psz_string
) {
288 p_sys
->psz_request
= strdup( newval
.psz_string
);
289 p_sys
->b_update
= true;
290 p_sys
->update_type
= UPDATE_REQUEST
;
291 vlc_cond_signal( &p_sys
->wait
);
293 vlc_mutex_unlock( &p_sys
->lock
);
297 static void ParseUrls( services_discovery_t
*p_sd
, char *psz_urls
)
299 services_discovery_sys_t
*p_sys
= p_sd
->p_sys
;
301 input_item_t
**pp_new_items
= NULL
;
304 char **ppsz_new_urls
= NULL
;
305 p_sys
->b_savedurls_loaded
= true;
314 char *psz_tok
= strchr( psz_urls
, '|' );
315 if( psz_tok
) *psz_tok
= '\0';
317 for( i
= 0; i
< p_sys
->i_urls
; i
++ )
318 if( !strcmp( psz_urls
, p_sys
->ppsz_urls
[i
] ) )
320 if( i
== p_sys
->i_urls
)
322 TAB_APPEND( i_new_urls
, ppsz_new_urls
, strdup( psz_urls
) );
324 input_item_t
*p_input
;
325 p_input
= input_item_New( psz_urls
, psz_urls
);
326 input_item_AddOption( p_input
, "demux=podcast", VLC_INPUT_OPTION_TRUSTED
);
328 TAB_APPEND( i_new_items
, pp_new_items
, p_input
);
329 services_discovery_AddItem( p_sd
, p_input
);
331 TAB_APPEND( p_sys
->i_input
, p_sys
->pp_input
,
332 input_CreateAndStart( p_sd
, p_input
, NULL
) );
336 TAB_APPEND( i_new_urls
, ppsz_new_urls
,
337 strdup( p_sys
->ppsz_urls
[i
]) );
338 TAB_APPEND( i_new_items
, pp_new_items
, p_sys
->pp_items
[i
] );
341 psz_urls
= psz_tok
+1;
346 /* delete removed items and signal the removal */
347 for( i
= 0; i
<p_sys
->i_items
; ++i
)
349 for( j
= 0; j
< i_new_items
; ++j
)
350 if( pp_new_items
[j
] == p_sys
->pp_items
[i
] ) break;
351 if( j
== i_new_items
)
353 services_discovery_RemoveItem( p_sd
, p_sys
->pp_items
[i
] );
354 input_item_Release( p_sys
->pp_items
[i
] );
357 free( p_sys
->pp_items
);
358 for( int i
= 0; i
< p_sys
->i_urls
; i
++ )
359 free( p_sys
->ppsz_urls
[i
] );
360 free( p_sys
->ppsz_urls
);
362 p_sys
->ppsz_urls
= ppsz_new_urls
;
363 p_sys
->i_urls
= i_new_urls
;
364 p_sys
->pp_items
= pp_new_items
;
365 p_sys
->i_items
= i_new_items
;
368 static void ParseRequest( services_discovery_t
*p_sd
)
370 services_discovery_sys_t
*p_sys
= p_sd
->p_sys
;
372 char *psz_request
= p_sys
->psz_request
;
376 char *psz_tok
= strchr( psz_request
, ':' );
377 if( !psz_tok
) return;
380 if ( ! p_sys
->b_savedurls_loaded
)
382 char *psz_urls
= var_GetNonEmptyString( p_sd
->obj
.parent
,
384 ParseUrls( p_sd
, psz_urls
);
388 if( !strcmp( psz_request
, "ADD" ) )
390 psz_request
= psz_tok
+ 1;
391 for( i
= 0; i
<p_sys
->i_urls
; i
++ )
392 if( !strcmp(p_sys
->ppsz_urls
[i
],psz_request
) )
394 if( i
== p_sys
->i_urls
)
396 TAB_APPEND( p_sys
->i_urls
, p_sys
->ppsz_urls
,
397 strdup( psz_request
) );
399 input_item_t
*p_input
;
400 p_input
= input_item_New( psz_request
, psz_request
);
401 input_item_AddOption( p_input
, "demux=podcast", VLC_INPUT_OPTION_TRUSTED
);
403 TAB_APPEND( p_sys
->i_items
, p_sys
->pp_items
, p_input
);
404 services_discovery_AddItem( p_sd
, p_input
);
406 TAB_APPEND( p_sys
->i_input
, p_sys
->pp_input
,
407 input_CreateAndStart( p_sd
, p_input
, NULL
) );
411 else if ( !strcmp( psz_request
, "RM" ) )
413 psz_request
= psz_tok
+ 1;
414 for( i
= 0; i
< p_sys
->i_urls
; i
++ )
415 if( !strcmp(p_sys
->ppsz_urls
[i
], psz_request
) )
417 services_discovery_RemoveItem( p_sd
, p_sys
->pp_items
[i
] );
418 input_item_Release( p_sys
->pp_items
[i
] );
419 TAB_ERASE(p_sys
->i_urls
, p_sys
->ppsz_urls
, i
);
420 TAB_ERASE(p_sys
->i_items
, p_sys
->pp_items
, i
);
426 free( p_sys
->psz_request
);
427 p_sys
->psz_request
= NULL
;
430 static void SaveUrls( services_discovery_t
*p_sd
)
432 services_discovery_sys_t
*p_sys
= p_sd
->p_sys
;
437 for( i
=0; i
< p_sys
->i_urls
; i
++ )
438 len
+= strlen( p_sys
->ppsz_urls
[i
] ) + 1;
440 psz_urls
= (char*) calloc( len
, sizeof(char) );
442 for( i
=0; i
< p_sys
->i_urls
; i
++ )
444 strcat( psz_urls
, p_sys
->ppsz_urls
[i
] );
445 if( i
< p_sys
->i_urls
- 1 ) strcat( psz_urls
, "|" );
448 config_PutPsz( p_sd
, "podcast-urls", psz_urls
);