]> git.sesse.net Git - vlc/blob - modules/services_discovery/podcast.c
58089ac0e13ba468d8707d06bee8ca17c73b7d6f
[vlc] / modules / services_discovery / podcast.c
1 /*****************************************************************************
2  * podcast.c:  Podcast services discovery module
3  *****************************************************************************
4  * Copyright (C) 2005-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Includes
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_services_discovery.h>
35 #include <vlc_network.h>
36
37 #include <assert.h>
38 #include <unistd.h>
39
40 /************************************************************************
41  * Macros and definitions
42  ************************************************************************/
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47
48 /* Callbacks */
49 static int  Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
51
52 VLC_SD_PROBE_HELPER("podcast", "Podcasts", SD_CAT_INTERNET)
53
54 #define URLS_TEXT N_("Podcast URLs list")
55 #define URLS_LONGTEXT N_("Enter the list of podcasts to retrieve, " \
56                          "separated by '|' (pipe)." )
57
58 vlc_module_begin ()
59     set_shortname( "Podcast")
60     set_description( N_("Podcasts") )
61     set_category( CAT_PLAYLIST )
62     set_subcategory( SUBCAT_PLAYLIST_SD )
63
64     add_string( "podcast-urls", NULL,
65                 URLS_TEXT, URLS_LONGTEXT, false )
66
67     set_capability( "services_discovery", 0 )
68     set_callbacks( Open, Close )
69
70     VLC_SD_PROBE_SUBMODULE
71
72 vlc_module_end ()
73
74
75 /*****************************************************************************
76  * Local structures
77  *****************************************************************************/
78
79 enum {
80   UPDATE_URLS,
81   UPDATE_REQUEST
82 }; /* FIXME Temporary. Updating by compound urls string to be removed later. */
83
84 struct services_discovery_sys_t
85 {
86     /* playlist node */
87     input_thread_t **pp_input;
88     int i_input;
89
90     char **ppsz_urls;
91     int i_urls;
92
93     input_item_t **pp_items;
94     int i_items;
95
96     vlc_thread_t thread;
97     vlc_mutex_t lock;
98     vlc_cond_t  wait;
99     bool b_update;
100     bool b_savedurls_loaded;
101     char *psz_request;
102     int update_type;
103 };
104
105 /*****************************************************************************
106  * Local prototypes
107  *****************************************************************************/
108 static void *Run( void * );
109 static int UrlsChange( vlc_object_t *, char const *, vlc_value_t,
110                        vlc_value_t, void * );
111 static int Request( vlc_object_t *, char const *, vlc_value_t,
112                        vlc_value_t, void * );
113 static void ParseRequest( services_discovery_t *p_sd );
114 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls );
115 static void SaveUrls( services_discovery_t *p_sd );
116
117 /*****************************************************************************
118  * Open: initialize and create stuff
119  *****************************************************************************/
120 static int Open( vlc_object_t *p_this )
121 {
122     if( strcmp( p_this->p_parent->psz_object_type, "playlist" ) )
123         return VLC_EGENERIC; /* FIXME: support LibVLC SD too! */
124
125     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
126     services_discovery_sys_t *p_sys = malloc( sizeof( *p_sys ) );
127     if( !p_sys )
128         return VLC_ENOMEM;
129
130     p_sys->i_urls = 0;
131     p_sys->ppsz_urls = NULL;
132     p_sys->i_input = 0;
133     p_sys->pp_input = NULL;
134     p_sys->pp_items = NULL;
135     p_sys->i_items = 0;
136     vlc_mutex_init( &p_sys->lock );
137     vlc_cond_init( &p_sys->wait );
138     p_sys->b_update = true;
139     p_sys->b_savedurls_loaded = false;
140     p_sys->psz_request = NULL;
141     p_sys->update_type = UPDATE_URLS;
142
143     p_sd->p_sys  = p_sys;
144
145     /* Launch the callback associated with this variable */
146     vlc_object_t *pl = p_sd->p_parent;
147     var_Create( pl, "podcast-urls", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
148     var_AddCallback( pl, "podcast-urls", UrlsChange, p_sys );
149
150     var_Create( pl, "podcast-request", VLC_VAR_STRING );
151     var_AddCallback( pl, "podcast-request", Request, p_sys );
152
153     if (vlc_clone (&p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
154     {
155         var_DelCallback( pl, "podcast-request", Request, p_sys );
156         var_DelCallback( pl, "podcast-urls", UrlsChange, p_sys );
157         vlc_cond_destroy( &p_sys->wait );
158         vlc_mutex_destroy( &p_sys->lock );
159         free (p_sys);
160         return VLC_EGENERIC;
161     }
162     return VLC_SUCCESS;
163 }
164
165 /*****************************************************************************
166  * Close:
167  *****************************************************************************/
168 static void Close( vlc_object_t *p_this )
169 {
170     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
171     services_discovery_sys_t *p_sys = p_sd->p_sys;
172     vlc_object_t *pl = p_sd->p_parent;
173     int i;
174
175     vlc_cancel (p_sys->thread);
176     vlc_join (p_sys->thread, NULL);
177
178     var_DelCallback( pl, "podcast-urls", UrlsChange, p_sys );
179     var_DelCallback( pl, "podcast-request", Request, p_sys );
180     vlc_cond_destroy( &p_sys->wait );
181     vlc_mutex_destroy( &p_sys->lock );
182
183     for( i = 0; i < p_sys->i_input; i++ )
184     {
185         input_thread_t *p_input = p_sd->p_sys->pp_input[i];
186         if( !p_input )
187             continue;
188
189         input_Stop( p_input );
190         input_Close( p_input );
191
192         p_sd->p_sys->pp_input[i] = NULL;
193     }
194     free( p_sd->p_sys->pp_input );
195     for( i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
196     free( p_sys->ppsz_urls );
197     for( i = 0; i < p_sys->i_items; i++ ) vlc_gc_decref( p_sys->pp_items[i] );
198     free( p_sys->pp_items );
199     free( p_sys->psz_request );
200     free( p_sys );
201 }
202
203 /*****************************************************************************
204  * Run: main thread
205  *****************************************************************************/
206 VLC_NORETURN
207 static void *Run( void *data )
208 {
209     services_discovery_t *p_sd = data;
210     services_discovery_sys_t *p_sys  = p_sd->p_sys;
211
212     vlc_mutex_lock( &p_sys->lock );
213     mutex_cleanup_push( &p_sys->lock );
214     for( ;; )
215     {
216         while( !p_sys->b_update )
217             vlc_cond_wait( &p_sys->wait, &p_sys->lock );
218
219         int canc = vlc_savecancel ();
220         msg_Dbg( p_sd, "Update required" );
221
222         if( p_sys->update_type == UPDATE_URLS )
223         {
224             char *psz_urls = var_GetNonEmptyString( p_sd->p_parent,
225                                                     "podcast-urls" );
226             ParseUrls( p_sd, psz_urls );
227             free( psz_urls );
228         }
229         else if( p_sys->update_type == UPDATE_REQUEST )
230             ParseRequest( p_sd );
231
232         p_sys->b_update = false;
233
234         for( int i = 0; i < p_sd->p_sys->i_input; i++ )
235         {
236             input_thread_t *p_input = p_sd->p_sys->pp_input[i];
237
238             if( p_input->b_eof || p_input->b_error )
239             {
240                 input_Stop( p_input );
241                 input_Close( p_input );
242
243                 p_sd->p_sys->pp_input[i] = NULL;
244                 REMOVE_ELEM( p_sys->pp_input, p_sys->i_input, i );
245                 i--;
246             }
247         }
248         vlc_restorecancel (canc);
249     }
250     vlc_cleanup_pop();
251     vlc_assert_unreachable(); /* dead code */
252 }
253
254 static int UrlsChange( vlc_object_t *p_this, char const *psz_var,
255                        vlc_value_t oldval, vlc_value_t newval,
256                        void *p_data )
257 {
258     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
259     VLC_UNUSED(newval);
260     services_discovery_sys_t *p_sys  = (services_discovery_sys_t *)p_data;
261
262     vlc_mutex_lock( &p_sys->lock );
263     p_sys->b_update = true;
264     p_sys->update_type = UPDATE_URLS;
265     vlc_cond_signal( &p_sys->wait );
266     vlc_mutex_unlock( &p_sys->lock );
267     return VLC_SUCCESS;
268 }
269
270 static int Request( vlc_object_t *p_this, char const *psz_var,
271                        vlc_value_t oldval, vlc_value_t newval,
272                        void *p_data )
273 {
274     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
275     services_discovery_sys_t *p_sys = (services_discovery_sys_t *)p_data;
276
277     vlc_mutex_lock( &p_sys->lock );
278     free( p_sys->psz_request );
279     p_sys->psz_request = NULL;
280     if( newval.psz_string && *newval.psz_string ) {
281       p_sys->psz_request = strdup( newval.psz_string );
282       p_sys->b_update = true;
283       p_sys->update_type = UPDATE_REQUEST;
284       vlc_cond_signal( &p_sys->wait );
285     }
286     vlc_mutex_unlock( &p_sys->lock );
287     return VLC_SUCCESS;
288 }
289
290 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls )
291 {
292     services_discovery_sys_t *p_sys = p_sd->p_sys;
293     int i_new_items = 0;
294     input_item_t **pp_new_items = NULL;
295
296     int i_new_urls = 0;
297     char **ppsz_new_urls = NULL;
298     p_sys->b_savedurls_loaded = true;
299
300     int i, j;
301
302     for( ;; )
303     {
304         if( !psz_urls )
305             break;
306
307         char *psz_tok = strchr( psz_urls, '|' );
308         if( psz_tok ) *psz_tok = '\0';
309
310         for( i = 0; i < p_sys->i_urls; i++ )
311             if( !strcmp( psz_urls, p_sys->ppsz_urls[i] ) )
312                 break;
313         if( i == p_sys->i_urls )
314         {
315             INSERT_ELEM( ppsz_new_urls, i_new_urls, i_new_urls,
316                          strdup( psz_urls ) );
317
318             input_item_t *p_input;
319             p_input = input_item_New( psz_urls, psz_urls );
320             input_item_AddOption( p_input, "demux=podcast", VLC_INPUT_OPTION_TRUSTED );
321
322             INSERT_ELEM( pp_new_items, i_new_items, i_new_items, p_input );
323             services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
324
325             INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
326                          input_CreateAndStart( p_sd, p_input, NULL ) );
327         }
328         else
329         {
330             INSERT_ELEM( ppsz_new_urls, i_new_urls, i_new_urls,
331                          strdup( p_sys->ppsz_urls[i]) );
332             INSERT_ELEM( pp_new_items, i_new_items, i_new_items, p_sys->pp_items[i] );
333         }
334         if( psz_tok )
335             psz_urls = psz_tok+1;
336         else
337             break;
338     }
339
340     /* delete removed items and signal the removal */
341     for( i = 0; i<p_sys->i_items; ++i )
342     {
343         for( j = 0; j < i_new_items; ++j )
344             if( pp_new_items[j] == p_sys->pp_items[i] ) break;
345         if( j == i_new_items )
346         {
347             services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
348             vlc_gc_decref( p_sys->pp_items[i] );
349         }
350     }
351     free( p_sys->pp_items );
352     for( int i = 0; i < p_sys->i_urls; i++ )
353         free( p_sys->ppsz_urls[i] );
354     free( p_sys->ppsz_urls );
355
356     p_sys->ppsz_urls = ppsz_new_urls;
357     p_sys->i_urls = i_new_urls;
358     p_sys->pp_items = pp_new_items;
359     p_sys->i_items = i_new_items;
360 }
361
362 static void ParseRequest( services_discovery_t *p_sd )
363 {
364     services_discovery_sys_t *p_sys = p_sd->p_sys;
365
366     char *psz_request = p_sys->psz_request;
367
368     int i;
369
370     char *psz_tok = strchr( psz_request, ':' );
371     if( !psz_tok ) return;
372     *psz_tok = '\0';
373
374     if ( ! p_sys->b_savedurls_loaded )
375     {
376         char *psz_urls = var_GetNonEmptyString( p_sd->p_parent,
377                                                 "podcast-urls" );
378         ParseUrls( p_sd, psz_urls );
379         free( psz_urls );
380     }
381
382     if( !strcmp( psz_request, "ADD" ) )
383     {
384         psz_request = psz_tok + 1;
385         for( i = 0; i<p_sys->i_urls; i++ )
386             if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
387               break;
388         if( i == p_sys->i_urls )
389         {
390             INSERT_ELEM( p_sys->ppsz_urls, p_sys->i_urls, p_sys->i_urls,
391               strdup( psz_request ) );
392
393             input_item_t *p_input;
394             p_input = input_item_New( psz_request, psz_request );
395             input_item_AddOption( p_input, "demux=podcast", VLC_INPUT_OPTION_TRUSTED );
396
397             INSERT_ELEM( p_sys->pp_items, p_sys->i_items, p_sys->i_items, p_input );
398             services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
399
400             INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
401                          input_CreateAndStart( p_sd, p_input, NULL ) );
402             SaveUrls( p_sd );
403         }
404     }
405     else if ( !strcmp( psz_request, "RM" ) )
406     {
407         psz_request = psz_tok + 1;
408         for( i = 0; i<p_sys->i_urls; i++ )
409           if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
410             break;
411         if( i != p_sys->i_urls )
412         {
413             services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
414             vlc_gc_decref( p_sys->pp_items[i] );
415             REMOVE_ELEM( p_sys->ppsz_urls, p_sys->i_urls, i );
416             REMOVE_ELEM( p_sys->pp_items, p_sys->i_items, i );
417         }
418         SaveUrls( p_sd );
419     }
420
421     free( p_sys->psz_request );
422     p_sys->psz_request = NULL;
423 }
424
425 static void SaveUrls( services_discovery_t *p_sd )
426 {
427     services_discovery_sys_t *p_sys = p_sd->p_sys;
428     int i;
429     char *psz_urls;
430     int len = 0;
431
432     for( i=0; i < p_sys->i_urls; i++ )
433         len += strlen( p_sys->ppsz_urls[i] ) + 1;
434
435     psz_urls = (char*) calloc( len, sizeof(char) );
436
437     for( i=0; i < p_sys->i_urls; i++ )
438     {
439         strcat( psz_urls, p_sys->ppsz_urls[i] );
440         if( i < p_sys->i_urls - 1 ) strcat( psz_urls, "|" );
441     }
442
443     config_PutPsz( p_sd, "podcast-urls", psz_urls );
444
445     free( psz_urls );
446 }