]> git.sesse.net Git - vlc/blob - modules/services_discovery/podcast.c
19c4a67ee6f66f195cc82d247edc473ee224e6be
[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 #ifdef HAVE_UNISTD_H
41 #    include <unistd.h>
42 #endif
43 #ifdef HAVE_SYS_TIME_H
44 #    include <sys/time.h>
45 #endif
46
47 /************************************************************************
48  * Macros and definitions
49  ************************************************************************/
50
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54
55 /* Callbacks */
56 static int  Open ( vlc_object_t * );
57 static void Close( vlc_object_t * );
58
59 VLC_SD_PROBE_HELPER("podcast", N_("Podcasts"))
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_SD_PROBE_SUBMODULE
79
80 vlc_module_end ()
81
82
83 /*****************************************************************************
84  * Local structures
85  *****************************************************************************/
86
87 enum {
88   UPDATE_URLS,
89   UPDATE_REQUEST
90 }; /* FIXME Temporary. Updating by compound urls string to be removed later. */
91
92 struct services_discovery_sys_t
93 {
94     /* playlist node */
95     input_thread_t **pp_input;
96     int i_input;
97
98     char **ppsz_urls;
99     int i_urls;
100
101     input_item_t **pp_items;
102     int i_items;
103
104     vlc_thread_t thread;
105     vlc_mutex_t lock;
106     vlc_cond_t  wait;
107     bool b_update;
108     char *psz_request;
109     int update_type;
110 };
111
112 /*****************************************************************************
113  * Local prototypes
114  *****************************************************************************/
115 static void *Run( void * );
116 static int UrlsChange( vlc_object_t *, char const *, vlc_value_t,
117                        vlc_value_t, void * );
118 static int Request( vlc_object_t *, char const *, vlc_value_t,
119                        vlc_value_t, void * );
120 static void ParseRequest( services_discovery_t *p_sd );
121 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls );
122 static void SaveUrls( services_discovery_t *p_sd );
123
124 /*****************************************************************************
125  * Open: initialize and create stuff
126  *****************************************************************************/
127 static int Open( vlc_object_t *p_this )
128 {
129     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
130     services_discovery_sys_t *p_sys  = malloc(
131                                     sizeof( services_discovery_sys_t ) );
132     if( !p_sys )
133         return VLC_ENOMEM;
134
135     p_sys->i_urls = 0;
136     p_sys->ppsz_urls = NULL;
137     p_sys->i_input = 0;
138     p_sys->pp_input = NULL;
139     p_sys->pp_items = NULL;
140     p_sys->i_items = 0;
141     vlc_mutex_init( &p_sys->lock );
142     vlc_cond_init( &p_sys->wait );
143     p_sys->b_update = true;
144     p_sys->psz_request = NULL;
145     p_sys->update_type = UPDATE_URLS;
146
147     p_sd->p_sys  = p_sys;
148
149     /* Launch the callback associated with this variable */
150     var_Create( p_sd, "podcast-urls", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
151     var_AddCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
152
153     var_Create( p_sd, "podcast-request", VLC_VAR_STRING );
154     var_AddCallback( p_sd, "podcast-request", Request, p_sys );
155
156     if (vlc_clone (&p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
157     {
158         var_DelCallback( p_sd, "podcast-request", Request, p_sys );
159         var_DelCallback( p_sd, "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;
164     }
165     return VLC_SUCCESS;
166 }
167
168 /*****************************************************************************
169  * Close:
170  *****************************************************************************/
171 static void Close( vlc_object_t *p_this )
172 {
173     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
174     services_discovery_sys_t *p_sys  = p_sd->p_sys;
175     int i;
176
177     vlc_cancel (p_sys->thread);
178     vlc_join (p_sys->thread, NULL);
179
180     var_DelCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
181     var_DelCallback( p_sd, "podcast-request", Request, p_sys );
182     vlc_cond_destroy( &p_sys->wait );
183     vlc_mutex_destroy( &p_sys->lock );
184
185     for( i = 0; i < p_sys->i_input; i++ )
186     {
187         input_thread_t *p_input = p_sd->p_sys->pp_input[i];
188         if( !p_input )
189             continue;
190
191         input_Stop( p_input, true );
192         vlc_thread_join( p_input );
193         vlc_object_release( p_input );
194
195         p_sd->p_sys->pp_input[i] = NULL;
196     }
197     free( p_sd->p_sys->pp_input );
198     for( i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
199     free( p_sys->ppsz_urls );
200     for( i = 0; i < p_sys->i_items; i++ ) vlc_gc_decref( p_sys->pp_items[i] );
201     free( p_sys->pp_items );
202     free( p_sys->psz_request );
203     free( p_sys );
204 }
205
206 /*****************************************************************************
207  * Run: main thread
208  *****************************************************************************/
209 static void *Run( void *data )
210 {
211     services_discovery_t *p_sd = data;
212     services_discovery_sys_t *p_sys  = p_sd->p_sys;
213
214     vlc_mutex_lock( &p_sys->lock );
215     mutex_cleanup_push( &p_sys->lock );
216     for( ;; )
217     {
218         while( !p_sys->b_update )
219             vlc_cond_wait( &p_sys->wait, &p_sys->lock );
220
221         int canc = vlc_savecancel ();
222         msg_Dbg( p_sd, "Update required" );
223
224         if( p_sys->update_type == UPDATE_URLS )
225         {
226           char* psz_urls = var_GetNonEmptyString( p_sd, "podcast-urls" );
227           ParseUrls( p_sd, psz_urls );
228           free( psz_urls );
229         }
230         else if( p_sys->update_type == UPDATE_REQUEST )
231         {
232           ParseRequest( p_sd );
233         }
234
235         p_sys->b_update = false;
236
237         for( int i = 0; i < p_sd->p_sys->i_input; i++ )
238         {
239             input_thread_t *p_input = p_sd->p_sys->pp_input[i];
240
241             if( p_input->b_eof || p_input->b_error )
242             {
243                 input_Stop( p_input, false );
244                 vlc_thread_join( p_input );
245                 vlc_object_release( p_input );
246
247                 p_sd->p_sys->pp_input[i] = NULL;
248                 REMOVE_ELEM( p_sys->pp_input, p_sys->i_input, i );
249                 i--;
250             }
251         }
252         vlc_restorecancel (canc);
253     }
254     vlc_cleanup_pop();
255     assert(0); /* dead code */
256 }
257
258 static int UrlsChange( vlc_object_t *p_this, char const *psz_var,
259                        vlc_value_t oldval, vlc_value_t newval,
260                        void *p_data )
261 {
262     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
263     VLC_UNUSED(newval);
264     services_discovery_sys_t *p_sys  = (services_discovery_sys_t *)p_data;
265
266     vlc_mutex_lock( &p_sys->lock );
267     p_sys->b_update = true;
268     p_sys->update_type = UPDATE_URLS;
269     vlc_cond_signal( &p_sys->wait );
270     vlc_mutex_unlock( &p_sys->lock );
271     return VLC_SUCCESS;
272 }
273
274 static int Request( vlc_object_t *p_this, char const *psz_var,
275                        vlc_value_t oldval, vlc_value_t newval,
276                        void *p_data )
277 {
278     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
279     services_discovery_sys_t *p_sys = (services_discovery_sys_t *)p_data;
280
281     vlc_mutex_lock( &p_sys->lock );
282     free( p_sys->psz_request );
283     p_sys->psz_request = NULL;
284     if( newval.psz_string && *newval.psz_string ) {
285       p_sys->psz_request = strdup( newval.psz_string );
286       p_sys->b_update = true;
287       p_sys->update_type = UPDATE_REQUEST;
288       vlc_cond_signal( &p_sys->wait );
289     }
290     vlc_mutex_unlock( &p_sys->lock );
291     return VLC_SUCCESS;
292 }
293
294 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls )
295 {
296     services_discovery_sys_t *p_sys = p_sd->p_sys;
297     int i_new_items = 0;
298     input_item_t **pp_new_items = NULL;
299
300     int i_new_urls = 0;
301     char **ppsz_new_urls = NULL;
302
303     int i, j;
304
305     for( ;; )
306     {
307         if( !psz_urls ) break;
308
309         char *psz_tok = strchr( psz_urls, '|' );
310         if( psz_tok ) *psz_tok = '\0';
311
312         for( i = 0; i < p_sys->i_urls; i++ )
313             if( !strcmp( psz_urls, p_sys->ppsz_urls[i] ) )
314                 break;
315         if( i == p_sys->i_urls )
316         {
317             INSERT_ELEM( ppsz_new_urls, i_new_urls, i_new_urls,
318                          strdup( psz_urls ) );
319
320             input_item_t *p_input;
321             p_input = input_item_New( p_sd, psz_urls, psz_urls );
322             input_item_AddOption( p_input, "demux=podcast", VLC_INPUT_OPTION_TRUSTED );
323
324             INSERT_ELEM( pp_new_items, i_new_items, i_new_items, p_input );
325             services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
326
327             INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
328                          input_CreateAndStart( p_sd, p_input, NULL ) );
329         }
330         else
331         {
332             INSERT_ELEM( ppsz_new_urls, i_new_urls, i_new_urls,
333                          strdup( p_sys->ppsz_urls[i]) );
334             INSERT_ELEM( pp_new_items, i_new_items, i_new_items, p_sys->pp_items[i] );
335         }
336         if( psz_tok )  psz_urls = psz_tok+1;
337         else 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++ ) free( p_sys->ppsz_urls[i] );
353     free( p_sys->ppsz_urls );
354
355     p_sys->ppsz_urls = ppsz_new_urls;
356     p_sys->i_urls = i_new_urls;
357     p_sys->pp_items = pp_new_items;
358     p_sys->i_items = i_new_items;
359 }
360
361 static void ParseRequest( services_discovery_t *p_sd )
362 {
363     services_discovery_sys_t *p_sys = p_sd->p_sys;
364
365     char *psz_request = p_sys->psz_request;
366
367     int i;
368
369     char *psz_tok = strchr( psz_request, ':' );
370     if( !psz_tok ) return;
371     *psz_tok = '\0';
372     if( !strcmp( psz_request, "ADD" ) )
373     {
374         psz_request = psz_tok + 1;
375         for( i = 0; i<p_sys->i_urls; i++ )
376             if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
377               break;
378         if( i == p_sys->i_urls )
379         {
380             INSERT_ELEM( p_sys->ppsz_urls, p_sys->i_urls, p_sys->i_urls,
381               strdup( psz_request ) );
382
383             input_item_t *p_input;
384             p_input = input_item_New( p_sd, psz_request, psz_request );
385             input_item_AddOption( p_input, "demux=podcast", VLC_INPUT_OPTION_TRUSTED );
386
387             INSERT_ELEM( p_sys->pp_items, p_sys->i_items, p_sys->i_items, p_input );
388             services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
389
390             INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
391                          input_CreateAndStart( p_sd, p_input, NULL ) );
392             SaveUrls( p_sd );
393         }
394     }
395     else if ( !strcmp( psz_request, "RM" ) )
396     {
397         psz_request = psz_tok + 1;
398         for( i = 0; i<p_sys->i_urls; i++ )
399           if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
400             break;
401         if( i != p_sys->i_urls )
402         {
403             services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
404             vlc_gc_decref( p_sys->pp_items[i] );
405             REMOVE_ELEM( p_sys->ppsz_urls, p_sys->i_urls, i );
406             REMOVE_ELEM( p_sys->pp_items, p_sys->i_items, i );
407         }
408         SaveUrls( p_sd );
409     }
410
411     free( p_sys->psz_request );
412     p_sys->psz_request = NULL;
413 }
414
415 static void SaveUrls( services_discovery_t *p_sd )
416 {
417     services_discovery_sys_t *p_sys = p_sd->p_sys;
418     int i;
419     char *psz_urls;
420     int len = 0;
421
422     for( i=0; i < p_sys->i_urls; i++ )
423         len += strlen( p_sys->ppsz_urls[i] ) + 1;
424
425     psz_urls = (char*) calloc( len, sizeof(char) );
426
427     for( i=0; i < p_sys->i_urls; i++ )
428     {
429         strcat( psz_urls, p_sys->ppsz_urls[i] );
430         if( i < p_sys->i_urls - 1 ) strcat( psz_urls, "|" );
431     }
432
433     config_PutPsz( p_sd, "podcast-urls", psz_urls );
434     config_SaveConfigFile( p_sd, "podcast" );
435
436     free( psz_urls );
437 }