]> git.sesse.net Git - vlc/blob - modules/services_discovery/podcast.c
SD: Fix podcast announcement and Qt special hack
[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
44 /************************************************************************
45  * Macros and definitions
46  ************************************************************************/
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51
52 /* Callbacks */
53 static int  Open ( vlc_object_t * );
54 static void Close( vlc_object_t * );
55
56 VLC_SD_PROBE_HELPER("podcast{longname=Podcasts}", N_("Podcasts"))
57
58 #define URLS_TEXT N_("Podcast URLs list")
59 #define URLS_LONGTEXT N_("Enter the list of podcasts to retrieve, " \
60                          "separated by '|' (pipe)." )
61
62 vlc_module_begin ()
63     set_shortname( "Podcast")
64     set_description( N_("Podcasts") )
65     set_category( CAT_PLAYLIST )
66     set_subcategory( SUBCAT_PLAYLIST_SD )
67
68     add_string( "podcast-urls", NULL, NULL,
69                 URLS_TEXT, URLS_LONGTEXT, false )
70         change_autosave ()
71
72     set_capability( "services_discovery", 0 )
73     set_callbacks( Open, Close )
74
75     VLC_SD_PROBE_SUBMODULE
76
77 vlc_module_end ()
78
79
80 /*****************************************************************************
81  * Local structures
82  *****************************************************************************/
83
84 enum {
85   UPDATE_URLS,
86   UPDATE_REQUEST
87 }; /* FIXME Temporary. Updating by compound urls string to be removed later. */
88
89 struct services_discovery_sys_t
90 {
91     /* playlist node */
92     input_thread_t **pp_input;
93     int i_input;
94
95     char **ppsz_urls;
96     int i_urls;
97
98     input_item_t **pp_items;
99     int i_items;
100
101     vlc_thread_t thread;
102     vlc_mutex_t lock;
103     vlc_cond_t  wait;
104     bool b_update;
105     char *psz_request;
106     int update_type;
107 };
108
109 /*****************************************************************************
110  * Local prototypes
111  *****************************************************************************/
112 static void *Run( void * );
113 static int UrlsChange( vlc_object_t *, char const *, vlc_value_t,
114                        vlc_value_t, void * );
115 static int Request( vlc_object_t *, char const *, vlc_value_t,
116                        vlc_value_t, void * );
117 static void ParseRequest( services_discovery_t *p_sd );
118 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls );
119 static void SaveUrls( services_discovery_t *p_sd );
120
121 /*****************************************************************************
122  * Open: initialize and create stuff
123  *****************************************************************************/
124 static int Open( vlc_object_t *p_this )
125 {
126     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
127     services_discovery_sys_t *p_sys  = malloc(
128                                     sizeof( services_discovery_sys_t ) );
129     if( !p_sys )
130         return VLC_ENOMEM;
131
132     p_sys->i_urls = 0;
133     p_sys->ppsz_urls = NULL;
134     p_sys->i_input = 0;
135     p_sys->pp_input = NULL;
136     p_sys->pp_items = NULL;
137     p_sys->i_items = 0;
138     vlc_mutex_init( &p_sys->lock );
139     vlc_cond_init( &p_sys->wait );
140     p_sys->b_update = true;
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     var_Create( p_sd, "podcast-urls", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
148     var_AddCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
149
150     var_Create( p_sd, "podcast-request", VLC_VAR_STRING );
151     var_AddCallback( p_sd, "podcast-request", Request, p_sys );
152
153     if (vlc_clone (&p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
154     {
155         var_DelCallback( p_sd, "podcast-request", Request, p_sys );
156         var_DelCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
157         vlc_cond_destroy( &p_sys->wait );
158         vlc_mutex_destroy( &p_sys->lock );
159         free (p_sys);
160         return VLC_EGENERIC;
161     }
162     return VLC_SUCCESS;
163 }
164
165 /*****************************************************************************
166  * Close:
167  *****************************************************************************/
168 static void Close( vlc_object_t *p_this )
169 {
170     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
171     services_discovery_sys_t *p_sys  = p_sd->p_sys;
172     int i;
173
174     vlc_cancel (p_sys->thread);
175     vlc_join (p_sys->thread, NULL);
176
177     var_DelCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
178     var_DelCallback( p_sd, "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         vlc_thread_join( p_input );
190         vlc_object_release( p_input );
191
192         p_sd->p_sys->pp_input[i] = NULL;
193     }
194     free( p_sd->p_sys->pp_input );
195     for( i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
196     free( p_sys->ppsz_urls );
197     for( i = 0; i < p_sys->i_items; i++ ) vlc_gc_decref( p_sys->pp_items[i] );
198     free( p_sys->pp_items );
199     free( p_sys->psz_request );
200     free( p_sys );
201 }
202
203 /*****************************************************************************
204  * Run: main thread
205  *****************************************************************************/
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           char* psz_urls = var_GetNonEmptyString( p_sd, "podcast-urls" );
224           ParseUrls( p_sd, psz_urls );
225           free( psz_urls );
226         }
227         else if( p_sys->update_type == UPDATE_REQUEST )
228         {
229           ParseRequest( p_sd );
230         }
231
232         p_sys->b_update = false;
233
234         for( int i = 0; i < p_sd->p_sys->i_input; i++ )
235         {
236             input_thread_t *p_input = p_sd->p_sys->pp_input[i];
237
238             if( p_input->b_eof || p_input->b_error )
239             {
240                 input_Stop( p_input, false );
241                 vlc_thread_join( p_input );
242                 vlc_object_release( 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     assert(0); /* 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
300     int i, j;
301
302     for( ;; )
303     {
304         if( !psz_urls ) 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( p_sd, 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 )  psz_urls = psz_tok+1;
334         else break;
335     }
336
337     /* delete removed items and signal the removal */
338     for( i = 0; i<p_sys->i_items; ++i )
339     {
340         for( j = 0; j < i_new_items; ++j )
341             if( pp_new_items[j] == p_sys->pp_items[i] ) break;
342         if( j == i_new_items )
343         {
344           services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
345           vlc_gc_decref( p_sys->pp_items[i] );
346         }
347     }
348     free( p_sys->pp_items );
349     for( int i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
350     free( p_sys->ppsz_urls );
351
352     p_sys->ppsz_urls = ppsz_new_urls;
353     p_sys->i_urls = i_new_urls;
354     p_sys->pp_items = pp_new_items;
355     p_sys->i_items = i_new_items;
356 }
357
358 static void ParseRequest( services_discovery_t *p_sd )
359 {
360     services_discovery_sys_t *p_sys = p_sd->p_sys;
361
362     char *psz_request = p_sys->psz_request;
363
364     int i;
365
366     char *psz_tok = strchr( psz_request, ':' );
367     if( !psz_tok ) return;
368     *psz_tok = '\0';
369     if( !strcmp( psz_request, "ADD" ) )
370     {
371         psz_request = psz_tok + 1;
372         for( i = 0; i<p_sys->i_urls; i++ )
373             if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
374               break;
375         if( i == p_sys->i_urls )
376         {
377             INSERT_ELEM( p_sys->ppsz_urls, p_sys->i_urls, p_sys->i_urls,
378               strdup( psz_request ) );
379
380             input_item_t *p_input;
381             p_input = input_item_New( p_sd, psz_request, psz_request );
382             input_item_AddOption( p_input, "demux=podcast", VLC_INPUT_OPTION_TRUSTED );
383
384             INSERT_ELEM( p_sys->pp_items, p_sys->i_items, p_sys->i_items, p_input );
385             services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
386
387             INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
388                          input_CreateAndStart( p_sd, p_input, NULL ) );
389             SaveUrls( p_sd );
390         }
391     }
392     else if ( !strcmp( psz_request, "RM" ) )
393     {
394         psz_request = psz_tok + 1;
395         for( i = 0; i<p_sys->i_urls; i++ )
396           if( !strcmp(p_sys->ppsz_urls[i],psz_request) )
397             break;
398         if( i != p_sys->i_urls )
399         {
400             services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
401             vlc_gc_decref( p_sys->pp_items[i] );
402             REMOVE_ELEM( p_sys->ppsz_urls, p_sys->i_urls, i );
403             REMOVE_ELEM( p_sys->pp_items, p_sys->i_items, i );
404         }
405         SaveUrls( p_sd );
406     }
407
408     free( p_sys->psz_request );
409     p_sys->psz_request = NULL;
410 }
411
412 static void SaveUrls( services_discovery_t *p_sd )
413 {
414     services_discovery_sys_t *p_sys = p_sd->p_sys;
415     int i;
416     char *psz_urls;
417     int len = 0;
418
419     for( i=0; i < p_sys->i_urls; i++ )
420         len += strlen( p_sys->ppsz_urls[i] ) + 1;
421
422     psz_urls = (char*) calloc( len, sizeof(char) );
423
424     for( i=0; i < p_sys->i_urls; i++ )
425     {
426         strcat( psz_urls, p_sys->ppsz_urls[i] );
427         if( i < p_sys->i_urls - 1 ) strcat( psz_urls, "|" );
428     }
429
430     config_PutPsz( p_sd, "podcast-urls", psz_urls );
431     config_SaveConfigFile( p_sd, "podcast" );
432
433     free( psz_urls );
434 }