]> git.sesse.net Git - vlc/blob - modules/services_discovery/podcast.c
podcast: per item add/remove request
[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_playlist.h>
35 #include <vlc_services_discovery.h>
36
37 #include <vlc_network.h>
38 #include <assert.h>
39
40 #include <errno.h>                                                 /* ENOMEM */
41
42 #ifdef HAVE_UNISTD_H
43 #    include <unistd.h>
44 #endif
45 #ifdef HAVE_SYS_TIME_H
46 #    include <sys/time.h>
47 #endif
48
49 /************************************************************************
50  * Macros and definitions
51  ************************************************************************/
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56
57 /* Callbacks */
58 static int  Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
60
61 #define URLS_TEXT N_("Podcast URLs list")
62 #define URLS_LONGTEXT N_("Enter the list of podcasts to retrieve, " \
63                          "separated by '|' (pipe)." )
64
65 vlc_module_begin ()
66     set_shortname( "Podcast")
67     set_description( N_("Podcasts") )
68     set_category( CAT_PLAYLIST )
69     set_subcategory( SUBCAT_PLAYLIST_SD )
70
71     add_string( "podcast-urls", NULL, NULL,
72                 URLS_TEXT, URLS_LONGTEXT, false )
73         change_autosave ()
74
75     set_capability( "services_discovery", 0 )
76     set_callbacks( Open, Close )
77
78 vlc_module_end ()
79
80
81 /*****************************************************************************
82  * Local structures
83  *****************************************************************************/
84
85 enum {
86   UPDATE_URLS,
87   UPDATE_REQUEST
88 }; /* FIXME Temporary. Updating by compound urls string to be removed later. */
89
90 struct services_discovery_sys_t
91 {
92     /* playlist node */
93     input_thread_t **pp_input;
94     int i_input;
95
96     char **ppsz_urls;
97     int i_urls;
98
99     input_item_t **pp_items;
100     int i_items;
101
102     vlc_thread_t thread;
103     vlc_mutex_t lock;
104     vlc_cond_t  wait;
105     bool b_update;
106     char *psz_request;
107     int update_type;
108 };
109
110 /*****************************************************************************
111  * Local prototypes
112  *****************************************************************************/
113 static void *Run( void * );
114 static int UrlsChange( vlc_object_t *, char const *, vlc_value_t,
115                        vlc_value_t, void * );
116 static int Request( vlc_object_t *, char const *, vlc_value_t,
117                        vlc_value_t, void * );
118 static void ParseRequest( services_discovery_t *p_sd );
119 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls );
120 static void SaveUrls( services_discovery_t *p_sd );
121
122 /*****************************************************************************
123  * Open: initialize and create stuff
124  *****************************************************************************/
125 static int Open( vlc_object_t *p_this )
126 {
127     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
128     services_discovery_sys_t *p_sys  = malloc(
129                                     sizeof( services_discovery_sys_t ) );
130     if( !p_sys )
131         return VLC_ENOMEM;
132
133     p_sys->i_urls = 0;
134     p_sys->ppsz_urls = NULL;
135     p_sys->i_input = 0;
136     p_sys->pp_input = NULL;
137     p_sys->pp_items = NULL;
138     p_sys->i_items = 0;
139     vlc_mutex_init( &p_sys->lock );
140     vlc_cond_init( &p_sys->wait );
141     p_sys->b_update = true;
142     p_sys->psz_request = NULL;
143     p_sys->update_type = UPDATE_URLS;
144
145     p_sd->p_sys  = p_sys;
146
147     /* Launch the callback associated with this variable */
148     var_Create( p_sd, "podcast-urls", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
149     var_AddCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
150
151     var_Create( p_sd, "podcast-request", VLC_VAR_STRING );
152     var_AddCallback( p_sd, "podcast-request", Request, p_sys );
153
154     if (vlc_clone (&p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
155     {
156         var_DelCallback( p_sd, "podcast-request", Request, p_sys );
157         var_DelCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
158         vlc_cond_destroy( &p_sys->wait );
159         vlc_mutex_destroy( &p_sys->lock );
160         free (p_sys);
161         return VLC_EGENERIC;
162     }
163     return VLC_SUCCESS;
164 }
165
166 /*****************************************************************************
167  * Close:
168  *****************************************************************************/
169 static void Close( vlc_object_t *p_this )
170 {
171     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
172     services_discovery_sys_t *p_sys  = p_sd->p_sys;
173     int i;
174
175     vlc_cancel (p_sys->thread);
176     vlc_join (p_sys->thread, NULL);
177
178     var_DelCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
179     var_DelCallback( p_sd, "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, true );
190         vlc_thread_join( p_input );
191         vlc_object_release( p_input );
192
193         p_sd->p_sys->pp_input[i] = NULL;
194     }
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 );
201     free( p_sys );
202 }
203
204 /*****************************************************************************
205  * Run: main thread
206  *****************************************************************************/
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, "podcast-urls" );
225           ParseUrls( p_sd, psz_urls );
226           free( psz_urls );
227         }
228         else if( p_sys->update_type == UPDATE_REQUEST )
229         {
230           ParseRequest( p_sd );
231         }
232
233         p_sys->b_update = false;
234
235         for( int i = 0; i < p_sd->p_sys->i_input; i++ )
236         {
237             input_thread_t *p_input = p_sd->p_sys->pp_input[i];
238
239             if( p_input->b_eof || p_input->b_error )
240             {
241                 input_Stop( p_input, false );
242                 vlc_thread_join( p_input );
243                 vlc_object_release( p_input );
244
245                 p_sd->p_sys->pp_input[i] = NULL;
246                 REMOVE_ELEM( p_sys->pp_input, p_sys->i_input, i );
247                 i--;
248             }
249         }
250         vlc_restorecancel (canc);
251     }
252     vlc_cleanup_pop();
253     assert(0); /* dead code */
254 }
255
256 static int UrlsChange( vlc_object_t *p_this, char const *psz_var,
257                        vlc_value_t oldval, vlc_value_t newval,
258                        void *p_data )
259 {
260     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
261     VLC_UNUSED(newval);
262     services_discovery_sys_t *p_sys  = (services_discovery_sys_t *)p_data;
263
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 );
269     return VLC_SUCCESS;
270 }
271
272 static int Request( vlc_object_t *p_this, char const *psz_var,
273                        vlc_value_t oldval, vlc_value_t newval,
274                        void *p_data )
275 {
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;
278
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 );
287     }
288     vlc_mutex_unlock( &p_sys->lock );
289     return VLC_SUCCESS;
290 }
291
292 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls )
293 {
294     services_discovery_sys_t *p_sys = p_sd->p_sys;
295     int i_new_items = 0;
296     input_item_t **pp_new_items = NULL;
297
298     int i_new_urls = 0;
299     char **ppsz_new_urls = NULL;
300
301     int i, j;
302
303     for( ;; )
304     {
305         if( !psz_urls ) 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( p_sd, 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 )  psz_urls = psz_tok+1;
335         else break;
336     }
337
338     /* delete removed items and signal the removal */
339     for( i = 0; i<p_sys->i_items; ++i )
340     {
341         for( j = 0; j < i_new_items; ++j )
342             if( pp_new_items[j] == p_sys->pp_items[i] ) break;
343         if( j == i_new_items )
344         {
345           services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
346           vlc_gc_decref( p_sys->pp_items[i] );
347         }
348     }
349     free( p_sys->pp_items );
350     for( int i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
351     free( p_sys->ppsz_urls );
352
353     p_sys->ppsz_urls = ppsz_new_urls;
354     p_sys->i_urls = i_new_urls;
355     p_sys->pp_items = pp_new_items;
356     p_sys->i_items = i_new_items;
357 }
358
359 static void ParseRequest( services_discovery_t *p_sd )
360 {
361     services_discovery_sys_t *p_sys = p_sd->p_sys;
362
363     char *psz_request = p_sys->psz_request;
364
365     int i;
366
367     char *psz_tok = strchr( psz_request, ':' );
368     if( !psz_tok ) return;
369     *psz_tok = '\0';
370     if( !strcmp( psz_request, "ADD" ) )
371     {
372         psz_request = psz_tok + 1;
373         for( i = 0; i<p_sys->i_urls; i++ )
374             if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
375               break;
376         if( i == p_sys->i_urls )
377         {
378             INSERT_ELEM( p_sys->ppsz_urls, p_sys->i_urls, p_sys->i_urls,
379               strdup( psz_request ) );
380
381             input_item_t *p_input;
382             p_input = input_item_New( p_sd, psz_request, psz_request );
383             input_item_AddOption( p_input, "demux=podcast", VLC_INPUT_OPTION_TRUSTED );
384
385             INSERT_ELEM( p_sys->pp_items, p_sys->i_items, p_sys->i_items, p_input );
386             services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
387
388             INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
389                          input_CreateAndStart( p_sd, p_input, NULL ) );
390             SaveUrls( p_sd );
391         }
392     }
393     else if ( !strcmp( psz_request, "RM" ) )
394     {
395         psz_request = psz_tok + 1;
396         for( i = 0; i<p_sys->i_urls; i++ )
397           if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
398             break;
399         if( i != p_sys->i_urls )
400         {
401             services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
402             vlc_gc_decref( p_sys->pp_items[i] );
403             REMOVE_ELEM( p_sys->ppsz_urls, p_sys->i_urls, i );
404             REMOVE_ELEM( p_sys->pp_items, p_sys->i_items, i );
405         }
406         SaveUrls( p_sd );
407     }
408
409     free( p_sys->psz_request );
410     p_sys->psz_request = NULL;
411 }
412
413 static void SaveUrls( services_discovery_t *p_sd )
414 {
415     services_discovery_sys_t *p_sys = p_sd->p_sys;
416     int i;
417     char *psz_urls;
418     int len = 0;
419
420     for( i=0; i < p_sys->i_urls; i++ )
421         len += strlen( p_sys->ppsz_urls[i] ) + 1;
422
423     psz_urls = (char*) calloc( len, sizeof(char) );
424
425     for( i=0; i < p_sys->i_urls; i++ )
426     {
427         strcat( psz_urls, p_sys->ppsz_urls[i] );
428         if( i < p_sys->i_urls - 1 ) strcat( psz_urls, "|" );
429     }
430
431     config_PutPsz( p_sd, "podcast-urls", psz_urls );
432     config_SaveConfigFile( p_sd, "podcast" );
433
434     free( psz_urls );
435 }