]> git.sesse.net Git - vlc/blob - modules/services_discovery/podcast.c
Remove inconsistently used HAVE_UNISTD_H
[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 #include <vlc_playlist.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     playlist_t *pl = pl_Get( p_this );
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->b_savedurls_loaded = false;
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( pl, "podcast-urls", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
147     var_AddCallback( pl, "podcast-urls", UrlsChange, p_sys );
148
149     var_Create( pl, "podcast-request", VLC_VAR_STRING );
150     var_AddCallback( pl, "podcast-request", Request, p_sys );
151
152     if (vlc_clone (&p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
153     {
154         var_DelCallback( pl, "podcast-request", Request, p_sys );
155         var_DelCallback( pl, "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     playlist_t *pl = pl_Get( p_this );
172     int i;
173
174     vlc_cancel (p_sys->thread);
175     vlc_join (p_sys->thread, NULL);
176
177     var_DelCallback( pl, "podcast-urls", UrlsChange, p_sys );
178     var_DelCallback( pl, "podcast-request", Request, p_sys );
179     vlc_cond_destroy( &p_sys->wait );
180     vlc_mutex_destroy( &p_sys->lock );
181
182     for( i = 0; i < p_sys->i_input; i++ )
183     {
184         input_thread_t *p_input = p_sd->p_sys->pp_input[i];
185         if( !p_input )
186             continue;
187
188         input_Stop( p_input, true );
189         input_Close( 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 VLC_NORETURN
206 static void *Run( void *data )
207 {
208     services_discovery_t *p_sd = data;
209     services_discovery_sys_t *p_sys  = p_sd->p_sys;
210
211     vlc_mutex_lock( &p_sys->lock );
212     mutex_cleanup_push( &p_sys->lock );
213     for( ;; )
214     {
215         while( !p_sys->b_update )
216             vlc_cond_wait( &p_sys->wait, &p_sys->lock );
217
218         int canc = vlc_savecancel ();
219         msg_Dbg( p_sd, "Update required" );
220
221         if( p_sys->update_type == UPDATE_URLS )
222         {
223             playlist_t *pl = pl_Get( p_sd );
224             char* psz_urls = var_GetNonEmptyString( pl, "podcast-urls" );
225             ParseUrls( p_sd, psz_urls );
226             free( psz_urls );
227         }
228         else if( p_sys->update_type == UPDATE_REQUEST )
229             ParseRequest( p_sd );
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                 input_Close( 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     p_sys->b_savedurls_loaded = true;
298
299     int i, j;
300
301     for( ;; )
302     {
303         if( !psz_urls )
304             break;
305
306         char *psz_tok = strchr( psz_urls, '|' );
307         if( psz_tok ) *psz_tok = '\0';
308
309         for( i = 0; i < p_sys->i_urls; i++ )
310             if( !strcmp( psz_urls, p_sys->ppsz_urls[i] ) )
311                 break;
312         if( i == p_sys->i_urls )
313         {
314             INSERT_ELEM( ppsz_new_urls, i_new_urls, i_new_urls,
315                          strdup( psz_urls ) );
316
317             input_item_t *p_input;
318             p_input = input_item_New( psz_urls, psz_urls );
319             input_item_AddOption( p_input, "demux=podcast", VLC_INPUT_OPTION_TRUSTED );
320
321             INSERT_ELEM( pp_new_items, i_new_items, i_new_items, p_input );
322             services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
323
324             INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
325                          input_CreateAndStart( p_sd, p_input, NULL ) );
326         }
327         else
328         {
329             INSERT_ELEM( ppsz_new_urls, i_new_urls, i_new_urls,
330                          strdup( p_sys->ppsz_urls[i]) );
331             INSERT_ELEM( pp_new_items, i_new_items, i_new_items, p_sys->pp_items[i] );
332         }
333         if( psz_tok )
334             psz_urls = psz_tok+1;
335         else
336             break;
337     }
338
339     /* delete removed items and signal the removal */
340     for( i = 0; i<p_sys->i_items; ++i )
341     {
342         for( j = 0; j < i_new_items; ++j )
343             if( pp_new_items[j] == p_sys->pp_items[i] ) break;
344         if( j == i_new_items )
345         {
346             services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
347             vlc_gc_decref( p_sys->pp_items[i] );
348         }
349     }
350     free( p_sys->pp_items );
351     for( int i = 0; i < p_sys->i_urls; i++ )
352         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
373     if ( ! p_sys->b_savedurls_loaded )
374     {
375         playlist_t *pl = pl_Get( p_sd );
376         char* psz_urls = var_GetNonEmptyString( pl, "podcast-urls" );
377         ParseUrls( p_sd, psz_urls );
378         free( psz_urls );
379     }
380
381     if( !strcmp( psz_request, "ADD" ) )
382     {
383         psz_request = psz_tok + 1;
384         for( i = 0; i<p_sys->i_urls; i++ )
385             if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
386               break;
387         if( i == p_sys->i_urls )
388         {
389             INSERT_ELEM( p_sys->ppsz_urls, p_sys->i_urls, p_sys->i_urls,
390               strdup( psz_request ) );
391
392             input_item_t *p_input;
393             p_input = input_item_New( psz_request, psz_request );
394             input_item_AddOption( p_input, "demux=podcast", VLC_INPUT_OPTION_TRUSTED );
395
396             INSERT_ELEM( p_sys->pp_items, p_sys->i_items, p_sys->i_items, p_input );
397             services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
398
399             INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
400                          input_CreateAndStart( p_sd, p_input, NULL ) );
401             SaveUrls( p_sd );
402         }
403     }
404     else if ( !strcmp( psz_request, "RM" ) )
405     {
406         psz_request = psz_tok + 1;
407         for( i = 0; i<p_sys->i_urls; i++ )
408           if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
409             break;
410         if( i != p_sys->i_urls )
411         {
412             services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
413             vlc_gc_decref( p_sys->pp_items[i] );
414             REMOVE_ELEM( p_sys->ppsz_urls, p_sys->i_urls, i );
415             REMOVE_ELEM( p_sys->pp_items, p_sys->i_items, i );
416         }
417         SaveUrls( p_sd );
418     }
419
420     free( p_sys->psz_request );
421     p_sys->psz_request = NULL;
422 }
423
424 static void SaveUrls( services_discovery_t *p_sd )
425 {
426     services_discovery_sys_t *p_sys = p_sd->p_sys;
427     int i;
428     char *psz_urls;
429     int len = 0;
430
431     for( i=0; i < p_sys->i_urls; i++ )
432         len += strlen( p_sys->ppsz_urls[i] ) + 1;
433
434     psz_urls = (char*) calloc( len, sizeof(char) );
435
436     for( i=0; i < p_sys->i_urls; i++ )
437     {
438         strcat( psz_urls, p_sys->ppsz_urls[i] );
439         if( i < p_sys->i_urls - 1 ) strcat( psz_urls, "|" );
440     }
441
442     config_PutPsz( p_sd, "podcast-urls", psz_urls );
443
444     free( psz_urls );
445 }