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