]> git.sesse.net Git - vlc/blob - modules/services_discovery/podcast.c
Restore SD human-readable names
[vlc] / modules / services_discovery / podcast.c
1 /*****************************************************************************
2  * podcast.c:  Podcast services discovery module
3  *****************************************************************************
4  * Copyright (C) 2005 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_network.h>
36 #include <assert.h>
37
38 #include <errno.h>                                                 /* ENOMEM */
39
40 #ifdef HAVE_UNISTD_H
41 #    include <unistd.h>
42 #endif
43 #ifdef HAVE_SYS_TIME_H
44 #    include <sys/time.h>
45 #endif
46
47 /************************************************************************
48  * Macros and definitions
49  ************************************************************************/
50
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54
55 /* Callbacks */
56 static int  Open ( vlc_object_t * );
57 static void Close( vlc_object_t * );
58
59 #define URLS_TEXT N_("Podcast URLs list")
60 #define URLS_LONGTEXT N_("Enter the list of podcasts to retrieve, " \
61                          "separated by '|' (pipe)." )
62
63 vlc_module_begin ()
64     set_shortname( "Podcast")
65     set_description( N_("Podcasts") )
66     set_category( CAT_PLAYLIST )
67     set_subcategory( SUBCAT_PLAYLIST_SD )
68
69     add_string( "podcast-urls", NULL, NULL,
70                 URLS_TEXT, URLS_LONGTEXT, false );
71         change_autosave ()
72
73     set_capability( "services_discovery", 0 )
74     set_callbacks( Open, Close )
75
76 vlc_module_end ()
77
78
79 /*****************************************************************************
80  * Local structures
81  *****************************************************************************/
82
83 struct services_discovery_sys_t
84 {
85     /* playlist node */
86     input_thread_t **pp_input;
87     int i_input;
88
89     char **ppsz_urls;
90     int i_urls;
91
92     vlc_thread_t thread;
93     vlc_mutex_t lock;
94     vlc_cond_t  wait;
95     bool b_update;
96 };
97
98 /*****************************************************************************
99  * Local prototypes
100  *****************************************************************************/
101 static void *Run( void * );
102 static int UrlsChange( vlc_object_t *, char const *, vlc_value_t,
103                        vlc_value_t, void * );
104 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls );
105
106 /*****************************************************************************
107  * Open: initialize and create stuff
108  *****************************************************************************/
109 static int Open( vlc_object_t *p_this )
110 {
111     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
112     services_discovery_sys_t *p_sys  = malloc(
113                                     sizeof( services_discovery_sys_t ) );
114     if( !p_sys )
115         return VLC_ENOMEM;
116
117     p_sys->i_urls = 0;
118     p_sys->ppsz_urls = NULL;
119     p_sys->i_input = 0;
120     p_sys->pp_input = NULL;
121     vlc_mutex_init( &p_sys->lock );
122     vlc_cond_init( &p_sys->wait );
123     p_sys->b_update = true;
124
125     p_sd->p_sys  = p_sys;
126
127     /* Launch the callback associated with this variable */
128     var_Create( p_sd, "podcast-urls", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
129     var_AddCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
130
131     if (vlc_clone (&p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
132     {
133         free (p_sys);
134         return VLC_EGENERIC;
135     }
136     return VLC_SUCCESS;
137 }
138
139 /*****************************************************************************
140  * Close:
141  *****************************************************************************/
142 static void Close( vlc_object_t *p_this )
143 {
144     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
145     services_discovery_sys_t *p_sys  = p_sd->p_sys;
146     int i;
147
148     vlc_cancel (p_sys->thread);
149     vlc_join (p_sys->thread, NULL);
150
151     var_DelCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
152     vlc_cond_destroy( &p_sys->wait );
153     vlc_mutex_destroy( &p_sys->lock );
154
155     for( i = 0; i < p_sys->i_input; i++ )
156     {
157         if( p_sd->p_sys->pp_input[i] )
158         {
159             input_StopThread( p_sd->p_sys->pp_input[i] );
160             vlc_object_release( p_sd->p_sys->pp_input[i] );
161             p_sd->p_sys->pp_input[i] = NULL;
162         }
163     }
164     free( p_sd->p_sys->pp_input );
165     for( i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
166     free( p_sys->ppsz_urls );
167     free( p_sys );
168 }
169
170 /*****************************************************************************
171  * Run: main thread
172  *****************************************************************************/
173 static void *Run( void *data )
174 {
175     services_discovery_t *p_sd = data;
176     services_discovery_sys_t *p_sys  = p_sd->p_sys;
177
178     vlc_mutex_lock( &p_sys->lock );
179     mutex_cleanup_push( &p_sys->lock );
180     for( ;; )
181     {
182         while( !p_sys->b_update )
183             vlc_cond_wait( &p_sys->wait, &p_sys->lock );
184
185         int canc = vlc_savecancel ();
186         msg_Dbg( p_sd, "Update required" );
187         char* psz_urls = var_GetNonEmptyString( p_sd, "podcast-urls" );
188         if( psz_urls != NULL )
189             ParseUrls( p_sd, psz_urls );
190         free( psz_urls );
191         p_sys->b_update = false;
192
193         for( int i = 0; i < p_sd->p_sys->i_input; i++ )
194         {
195             if( p_sd->p_sys->pp_input[i]->b_eof
196                 || p_sd->p_sys->pp_input[i]->b_error )
197             {
198                 input_StopThread( p_sd->p_sys->pp_input[i] );
199                 vlc_object_release( p_sd->p_sys->pp_input[i] );
200                 p_sd->p_sys->pp_input[i] = NULL;
201                 REMOVE_ELEM( p_sys->pp_input, p_sys->i_input, i );
202                 i--;
203             }
204         }
205         vlc_restorecancel (canc);
206     }
207     vlc_cleanup_pop();
208     assert(0); /* dead code */
209 }
210
211 static int UrlsChange( vlc_object_t *p_this, char const *psz_var,
212                        vlc_value_t oldval, vlc_value_t newval,
213                        void *p_data )
214 {
215     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
216     VLC_UNUSED(newval);
217     services_discovery_sys_t *p_sys  = (services_discovery_sys_t *)p_data;
218
219     vlc_mutex_lock( &p_sys->lock );
220     p_sys->b_update = true;
221     vlc_cond_signal( &p_sys->wait );
222     vlc_mutex_unlock( &p_sys->lock );
223     return VLC_SUCCESS;
224 }
225
226 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls )
227 {
228     services_discovery_sys_t *p_sys = p_sd->p_sys;
229     for( ;; )
230     {
231         int i;
232         char *psz_tok = strchr( psz_urls, '|' );
233         if( psz_tok ) *psz_tok = '\0';
234         for( i = 0; i < p_sys->i_urls; i++ )
235             if( !strcmp( psz_urls, p_sys->ppsz_urls[i] ) )
236                 break;
237         if( i == p_sys->i_urls )
238         {
239             /* Only add new urls.
240              * FIXME: We don't delete urls which have been removed from
241              * the config since we don't have a way to know which inputs
242              * they spawned */
243             input_item_t *p_input;
244             INSERT_ELEM( p_sys->ppsz_urls, p_sys->i_urls, p_sys->i_urls,
245                          strdup( psz_urls ) );
246             p_input = input_item_NewExt( p_sd, psz_urls,
247                                         psz_urls, 0, NULL, -1 );
248             input_item_AddOption( p_input, "demux=podcast" );
249             services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
250             vlc_gc_decref( p_input );
251             INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
252                          input_CreateThread( p_sd, p_input ) );
253         }
254         if( psz_tok )  psz_urls = psz_tok+1;
255         else           return;
256     }
257 }