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