]> git.sesse.net Git - vlc/blob - modules/services_discovery/podcast.c
podcast: missing initialization
[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 #define URLS_TEXT N_("Podcast URLs list")
62 #define URLS_LONGTEXT N_("Enter the list of podcasts to retrieve, " \
63                          "separated by '|' (pipe)." )
64
65 vlc_module_begin ()
66     set_shortname( "Podcast")
67     set_description( N_("Podcasts") )
68     set_category( CAT_PLAYLIST )
69     set_subcategory( SUBCAT_PLAYLIST_SD )
70
71     add_string( "podcast-urls", NULL, NULL,
72                 URLS_TEXT, URLS_LONGTEXT, false )
73         change_autosave ()
74
75     set_capability( "services_discovery", 0 )
76     set_callbacks( Open, Close )
77
78 vlc_module_end ()
79
80
81 /*****************************************************************************
82  * Local structures
83  *****************************************************************************/
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 };
102
103 /*****************************************************************************
104  * Local prototypes
105  *****************************************************************************/
106 static void *Run( void * );
107 static int UrlsChange( vlc_object_t *, char const *, vlc_value_t,
108                        vlc_value_t, void * );
109 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls );
110
111 /*****************************************************************************
112  * Open: initialize and create stuff
113  *****************************************************************************/
114 static int Open( vlc_object_t *p_this )
115 {
116     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
117     services_discovery_sys_t *p_sys  = malloc(
118                                     sizeof( services_discovery_sys_t ) );
119     if( !p_sys )
120         return VLC_ENOMEM;
121
122     p_sys->i_urls = 0;
123     p_sys->ppsz_urls = NULL;
124     p_sys->i_input = 0;
125     p_sys->pp_input = NULL;
126     p_sys->pp_items = NULL;
127     p_sys->i_items = 0;
128     vlc_mutex_init( &p_sys->lock );
129     vlc_cond_init( &p_sys->wait );
130     p_sys->b_update = true;
131
132     p_sd->p_sys  = p_sys;
133
134     /* Launch the callback associated with this variable */
135     var_Create( p_sd, "podcast-urls", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
136     var_AddCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
137
138     if (vlc_clone (&p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
139     {
140         var_DelCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
141         vlc_cond_destroy( &p_sys->wait );
142         vlc_mutex_destroy( &p_sys->lock );
143         free (p_sys);
144         return VLC_EGENERIC;
145     }
146     return VLC_SUCCESS;
147 }
148
149 /*****************************************************************************
150  * Close:
151  *****************************************************************************/
152 static void Close( vlc_object_t *p_this )
153 {
154     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
155     services_discovery_sys_t *p_sys  = p_sd->p_sys;
156     int i;
157
158     vlc_cancel (p_sys->thread);
159     vlc_join (p_sys->thread, NULL);
160
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
165     for( i = 0; i < p_sys->i_input; i++ )
166     {
167         input_thread_t *p_input = p_sd->p_sys->pp_input[i];
168         if( !p_input )
169             continue;
170
171         input_Stop( p_input, true );
172         vlc_thread_join( p_input );
173         vlc_object_release( p_input );
174
175         p_sd->p_sys->pp_input[i] = NULL;
176     }
177     free( p_sd->p_sys->pp_input );
178     for( i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
179     free( p_sys->ppsz_urls );
180     for( i = 0; i < p_sys->i_items; i++ ) vlc_gc_decref( p_sys->pp_items[i] );
181     free( p_sys->pp_items );
182     free( p_sys );
183 }
184
185 /*****************************************************************************
186  * Run: main thread
187  *****************************************************************************/
188 static void *Run( void *data )
189 {
190     services_discovery_t *p_sd = data;
191     services_discovery_sys_t *p_sys  = p_sd->p_sys;
192
193     vlc_mutex_lock( &p_sys->lock );
194     mutex_cleanup_push( &p_sys->lock );
195     for( ;; )
196     {
197         while( !p_sys->b_update )
198             vlc_cond_wait( &p_sys->wait, &p_sys->lock );
199
200         int canc = vlc_savecancel ();
201         msg_Dbg( p_sd, "Update required" );
202         char* psz_urls = var_GetNonEmptyString( p_sd, "podcast-urls" );
203         ParseUrls( p_sd, psz_urls );
204         free( psz_urls );
205         p_sys->b_update = false;
206
207         for( int i = 0; i < p_sd->p_sys->i_input; i++ )
208         {
209             input_thread_t *p_input = p_sd->p_sys->pp_input[i];
210
211             if( p_input->b_eof || p_input->b_error )
212             {
213                 input_Stop( p_input, false );
214                 vlc_thread_join( p_input );
215                 vlc_object_release( p_input );
216
217                 p_sd->p_sys->pp_input[i] = NULL;
218                 REMOVE_ELEM( p_sys->pp_input, p_sys->i_input, i );
219                 i--;
220             }
221         }
222         vlc_restorecancel (canc);
223     }
224     vlc_cleanup_pop();
225     assert(0); /* dead code */
226 }
227
228 static int UrlsChange( vlc_object_t *p_this, char const *psz_var,
229                        vlc_value_t oldval, vlc_value_t newval,
230                        void *p_data )
231 {
232     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
233     VLC_UNUSED(newval);
234     services_discovery_sys_t *p_sys  = (services_discovery_sys_t *)p_data;
235
236     vlc_mutex_lock( &p_sys->lock );
237     p_sys->b_update = true;
238     vlc_cond_signal( &p_sys->wait );
239     vlc_mutex_unlock( &p_sys->lock );
240     return VLC_SUCCESS;
241 }
242
243 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls )
244 {
245     services_discovery_sys_t *p_sys = p_sd->p_sys;
246     int i_new_items = 0;
247     input_item_t **pp_new_items = NULL;
248
249     int i_new_urls = 0;
250     char **ppsz_new_urls = NULL;
251
252     int i, j;
253
254     for( ;; )
255     {
256         if( !psz_urls ) break;
257
258         char *psz_tok = strchr( psz_urls, '|' );
259         if( psz_tok ) *psz_tok = '\0';
260
261         for( i = 0; i < p_sys->i_urls; i++ )
262             if( !strcmp( psz_urls, p_sys->ppsz_urls[i] ) )
263                 break;
264         if( i == p_sys->i_urls )
265         {
266             INSERT_ELEM( ppsz_new_urls, i_new_urls, i_new_urls,
267                          strdup( psz_urls ) );
268
269             input_item_t *p_input;
270             p_input = input_item_New( p_sd, psz_urls, psz_urls );
271             input_item_AddOption( p_input, "demux=podcast", VLC_INPUT_OPTION_TRUSTED );
272
273             INSERT_ELEM( pp_new_items, i_new_items, i_new_items, p_input );
274             services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
275
276             INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
277                          input_CreateAndStart( p_sd, p_input, NULL ) );
278         }
279         else
280         {
281             INSERT_ELEM( ppsz_new_urls, i_new_urls, i_new_urls,
282                          strdup( p_sys->ppsz_urls[i]) );
283             INSERT_ELEM( pp_new_items, i_new_items, i_new_items, p_sys->pp_items[i] );
284         }
285         if( psz_tok )  psz_urls = psz_tok+1;
286         else break;
287     }
288
289     /* delete removed items and signal the removal */
290     for( i = 0; i<p_sys->i_items; ++i )
291     {
292         for( j = 0; j < i_new_items; ++j )
293             if( pp_new_items[j] == p_sys->pp_items[i] ) break;
294         if( j == i_new_items )
295         {
296           services_discovery_RemoveItem( p_sd, p_sys->pp_items[i] );
297           vlc_gc_decref( p_sys->pp_items[i] );
298         }
299     }
300     free( p_sys->pp_items );
301     for( int i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
302     free( p_sys->ppsz_urls );
303
304     p_sys->ppsz_urls = ppsz_new_urls;
305     p_sys->i_urls = i_new_urls;
306     p_sys->pp_items = pp_new_items;
307     p_sys->i_items = i_new_items;
308 }