]> git.sesse.net Git - vlc/blob - modules/services_discovery/podcast.c
Merge branch 'master' into lpcm_encoder
[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, NULL,
68                 URLS_TEXT, URLS_LONGTEXT, false )
69         change_autosave ()
70
71     set_capability( "services_discovery", 0 )
72     set_callbacks( Open, Close )
73
74     VLC_SD_PROBE_SUBMODULE
75
76 vlc_module_end ()
77
78
79 /*****************************************************************************
80  * Local structures
81  *****************************************************************************/
82
83 enum {
84   UPDATE_URLS,
85   UPDATE_REQUEST
86 }; /* FIXME Temporary. Updating by compound urls string to be removed later. */
87
88 struct services_discovery_sys_t
89 {
90     /* playlist node */
91     input_thread_t **pp_input;
92     int i_input;
93
94     char **ppsz_urls;
95     int i_urls;
96
97     input_item_t **pp_items;
98     int i_items;
99
100     vlc_thread_t thread;
101     vlc_mutex_t lock;
102     vlc_cond_t  wait;
103     bool b_update;
104     char *psz_request;
105     int update_type;
106 };
107
108 /*****************************************************************************
109  * Local prototypes
110  *****************************************************************************/
111 static void *Run( void * );
112 static int UrlsChange( vlc_object_t *, char const *, vlc_value_t,
113                        vlc_value_t, void * );
114 static int Request( vlc_object_t *, char const *, vlc_value_t,
115                        vlc_value_t, void * );
116 static void ParseRequest( services_discovery_t *p_sd );
117 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls );
118 static void SaveUrls( services_discovery_t *p_sd );
119
120 /*****************************************************************************
121  * Open: initialize and create stuff
122  *****************************************************************************/
123 static int Open( vlc_object_t *p_this )
124 {
125     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
126     services_discovery_sys_t *p_sys  = malloc(
127                                     sizeof( services_discovery_sys_t ) );
128     if( !p_sys )
129         return VLC_ENOMEM;
130
131     p_sys->i_urls = 0;
132     p_sys->ppsz_urls = NULL;
133     p_sys->i_input = 0;
134     p_sys->pp_input = NULL;
135     p_sys->pp_items = NULL;
136     p_sys->i_items = 0;
137     vlc_mutex_init( &p_sys->lock );
138     vlc_cond_init( &p_sys->wait );
139     p_sys->b_update = true;
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     var_Create( p_sd, "podcast-urls", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
147     var_AddCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
148
149     var_Create( p_sd, "podcast-request", VLC_VAR_STRING );
150     var_AddCallback( p_sd, "podcast-request", Request, p_sys );
151
152     if (vlc_clone (&p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
153     {
154         var_DelCallback( p_sd, "podcast-request", Request, p_sys );
155         var_DelCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
156         vlc_cond_destroy( &p_sys->wait );
157         vlc_mutex_destroy( &p_sys->lock );
158         free (p_sys);
159         return VLC_EGENERIC;
160     }
161     return VLC_SUCCESS;
162 }
163
164 /*****************************************************************************
165  * Close:
166  *****************************************************************************/
167 static void Close( vlc_object_t *p_this )
168 {
169     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
170     services_discovery_sys_t *p_sys  = p_sd->p_sys;
171     int i;
172
173     vlc_cancel (p_sys->thread);
174     vlc_join (p_sys->thread, NULL);
175
176     var_DelCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
177     var_DelCallback( p_sd, "podcast-request", Request, p_sys );
178     vlc_cond_destroy( &p_sys->wait );
179     vlc_mutex_destroy( &p_sys->lock );
180
181     for( i = 0; i < p_sys->i_input; i++ )
182     {
183         input_thread_t *p_input = p_sd->p_sys->pp_input[i];
184         if( !p_input )
185             continue;
186
187         input_Stop( p_input, true );
188         vlc_thread_join( p_input );
189         vlc_object_release( p_input );
190
191         p_sd->p_sys->pp_input[i] = NULL;
192     }
193     free( p_sd->p_sys->pp_input );
194     for( i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
195     free( p_sys->ppsz_urls );
196     for( i = 0; i < p_sys->i_items; i++ ) vlc_gc_decref( p_sys->pp_items[i] );
197     free( p_sys->pp_items );
198     free( p_sys->psz_request );
199     free( p_sys );
200 }
201
202 /*****************************************************************************
203  * Run: main thread
204  *****************************************************************************/
205 static void *Run( void *data )
206 {
207     services_discovery_t *p_sd = data;
208     services_discovery_sys_t *p_sys  = p_sd->p_sys;
209
210     vlc_mutex_lock( &p_sys->lock );
211     mutex_cleanup_push( &p_sys->lock );
212     for( ;; )
213     {
214         while( !p_sys->b_update )
215             vlc_cond_wait( &p_sys->wait, &p_sys->lock );
216
217         int canc = vlc_savecancel ();
218         msg_Dbg( p_sd, "Update required" );
219
220         if( p_sys->update_type == UPDATE_URLS )
221         {
222           char* psz_urls = var_GetNonEmptyString( p_sd, "podcast-urls" );
223           ParseUrls( p_sd, psz_urls );
224           free( psz_urls );
225         }
226         else if( p_sys->update_type == UPDATE_REQUEST )
227         {
228           ParseRequest( p_sd );
229         }
230
231         p_sys->b_update = false;
232
233         for( int i = 0; i < p_sd->p_sys->i_input; i++ )
234         {
235             input_thread_t *p_input = p_sd->p_sys->pp_input[i];
236
237             if( p_input->b_eof || p_input->b_error )
238             {
239                 input_Stop( p_input, false );
240                 vlc_thread_join( p_input );
241                 vlc_object_release( 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     assert(0); /* 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
299     int i, j;
300
301     for( ;; )
302     {
303         if( !psz_urls ) break;
304
305         char *psz_tok = strchr( psz_urls, '|' );
306         if( psz_tok ) *psz_tok = '\0';
307
308         for( i = 0; i < p_sys->i_urls; i++ )
309             if( !strcmp( psz_urls, p_sys->ppsz_urls[i] ) )
310                 break;
311         if( i == p_sys->i_urls )
312         {
313             INSERT_ELEM( ppsz_new_urls, i_new_urls, i_new_urls,
314                          strdup( psz_urls ) );
315
316             input_item_t *p_input;
317             p_input = input_item_New( p_sd, psz_urls, psz_urls );
318             input_item_AddOption( p_input, "demux=podcast", VLC_INPUT_OPTION_TRUSTED );
319
320             INSERT_ELEM( pp_new_items, i_new_items, i_new_items, p_input );
321             services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
322
323             INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
324                          input_CreateAndStart( p_sd, p_input, NULL ) );
325         }
326         else
327         {
328             INSERT_ELEM( ppsz_new_urls, i_new_urls, i_new_urls,
329                          strdup( p_sys->ppsz_urls[i]) );
330             INSERT_ELEM( pp_new_items, i_new_items, i_new_items, p_sys->pp_items[i] );
331         }
332         if( psz_tok )  psz_urls = psz_tok+1;
333         else break;
334     }
335
336     /* delete removed items and signal the removal */
337     for( i = 0; i<p_sys->i_items; ++i )
338     {
339         for( j = 0; j < i_new_items; ++j )
340             if( pp_new_items[j] == p_sys->pp_items[i] ) break;
341         if( j == i_new_items )
342         {
343           services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
344           vlc_gc_decref( p_sys->pp_items[i] );
345         }
346     }
347     free( p_sys->pp_items );
348     for( int i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
349     free( p_sys->ppsz_urls );
350
351     p_sys->ppsz_urls = ppsz_new_urls;
352     p_sys->i_urls = i_new_urls;
353     p_sys->pp_items = pp_new_items;
354     p_sys->i_items = i_new_items;
355 }
356
357 static void ParseRequest( services_discovery_t *p_sd )
358 {
359     services_discovery_sys_t *p_sys = p_sd->p_sys;
360
361     char *psz_request = p_sys->psz_request;
362
363     int i;
364
365     char *psz_tok = strchr( psz_request, ':' );
366     if( !psz_tok ) return;
367     *psz_tok = '\0';
368     if( !strcmp( psz_request, "ADD" ) )
369     {
370         psz_request = psz_tok + 1;
371         for( i = 0; i<p_sys->i_urls; i++ )
372             if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
373               break;
374         if( i == p_sys->i_urls )
375         {
376             INSERT_ELEM( p_sys->ppsz_urls, p_sys->i_urls, p_sys->i_urls,
377               strdup( psz_request ) );
378
379             input_item_t *p_input;
380             p_input = input_item_New( p_sd, psz_request, psz_request );
381             input_item_AddOption( p_input, "demux=podcast", VLC_INPUT_OPTION_TRUSTED );
382
383             INSERT_ELEM( p_sys->pp_items, p_sys->i_items, p_sys->i_items, p_input );
384             services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
385
386             INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
387                          input_CreateAndStart( p_sd, p_input, NULL ) );
388             SaveUrls( p_sd );
389         }
390     }
391     else if ( !strcmp( psz_request, "RM" ) )
392     {
393         psz_request = psz_tok + 1;
394         for( i = 0; i<p_sys->i_urls; i++ )
395           if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
396             break;
397         if( i != p_sys->i_urls )
398         {
399             services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
400             vlc_gc_decref( p_sys->pp_items[i] );
401             REMOVE_ELEM( p_sys->ppsz_urls, p_sys->i_urls, i );
402             REMOVE_ELEM( p_sys->pp_items, p_sys->i_items, i );
403         }
404         SaveUrls( p_sd );
405     }
406
407     free( p_sys->psz_request );
408     p_sys->psz_request = NULL;
409 }
410
411 static void SaveUrls( services_discovery_t *p_sd )
412 {
413     services_discovery_sys_t *p_sys = p_sd->p_sys;
414     int i;
415     char *psz_urls;
416     int len = 0;
417
418     for( i=0; i < p_sys->i_urls; i++ )
419         len += strlen( p_sys->ppsz_urls[i] ) + 1;
420
421     psz_urls = (char*) calloc( len, sizeof(char) );
422
423     for( i=0; i < p_sys->i_urls; i++ )
424     {
425         strcat( psz_urls, p_sys->ppsz_urls[i] );
426         if( i < p_sys->i_urls - 1 ) strcat( psz_urls, "|" );
427     }
428
429     config_PutPsz( p_sd, "podcast-urls", psz_urls );
430     config_SaveConfigFile( p_sd, "podcast" );
431
432     free( psz_urls );
433 }