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