]> git.sesse.net Git - vlc/blob - modules/services_discovery/podcast.c
Usefull coment instead of "Gruik" (evenif I like the previous one...)
[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
37 #include <errno.h>                                                 /* ENOMEM */
38
39 #ifdef HAVE_UNISTD_H
40 #    include <unistd.h>
41 #endif
42 #ifdef HAVE_SYS_TIME_H
43 #    include <sys/time.h>
44 #endif
45
46 /************************************************************************
47  * Macros and definitions
48  ************************************************************************/
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53
54 /* Callbacks */
55 static int  Open ( vlc_object_t * );
56 static void Close( vlc_object_t * );
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_module_end();
76
77
78 /*****************************************************************************
79  * Local structures
80  *****************************************************************************/
81
82 struct services_discovery_sys_t
83 {
84     /* playlist node */
85     input_thread_t **pp_input;
86     int i_input;
87
88     char **ppsz_urls;
89     int i_urls;
90
91     bool b_update;
92 };
93
94 /*****************************************************************************
95  * Local prototypes
96  *****************************************************************************/
97 static void Run( services_discovery_t *p_intf );
98 static int UrlsChange( vlc_object_t *, char const *, vlc_value_t,
99                        vlc_value_t, void * );
100 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls );
101
102 /*****************************************************************************
103  * Open: initialize and create stuff
104  *****************************************************************************/
105 static int Open( vlc_object_t *p_this )
106 {
107     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
108     services_discovery_sys_t *p_sys  = malloc(
109                                     sizeof( services_discovery_sys_t ) );
110     if( !p_sys )
111         return VLC_ENOMEM;
112
113     p_sys->i_urls = 0;
114     p_sys->ppsz_urls = NULL;
115     p_sys->i_input = 0;
116     p_sys->pp_input = NULL;
117     p_sys->b_update = true;
118
119     p_sd->pf_run = Run;
120     p_sd->p_sys  = p_sys;
121
122     /* Give us a name */
123     services_discovery_SetLocalizedName( p_sd, _("Podcasts") );
124
125     return VLC_SUCCESS;
126 }
127
128 /*****************************************************************************
129  * Close:
130  *****************************************************************************/
131 static void Close( vlc_object_t *p_this )
132 {
133     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
134     services_discovery_sys_t *p_sys  = p_sd->p_sys;
135     int i;
136     for( i = 0; i < p_sys->i_input; i++ )
137     {
138         if( p_sd->p_sys->pp_input[i] )
139         {
140             input_StopThread( p_sd->p_sys->pp_input[i] );
141             vlc_object_release( p_sd->p_sys->pp_input[i] );
142             p_sd->p_sys->pp_input[i] = NULL;
143         }
144     }
145     free( p_sd->p_sys->pp_input );
146     for( i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
147     free( p_sys->ppsz_urls );
148     free( p_sys );
149 }
150
151 /*****************************************************************************
152  * Run: main thread
153  *****************************************************************************/
154 static void Run( services_discovery_t *p_sd )
155 {
156     services_discovery_sys_t *p_sys  = p_sd->p_sys;
157
158     /* Launch the callback associated with this variable */
159     char *psz_urls = var_CreateGetString( p_sd, "podcast-urls" );
160     free( psz_urls );
161     var_AddCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
162
163     while( vlc_object_alive (p_sd) )
164     {
165         int i;
166         if( p_sys->b_update == true )
167         {
168             msg_Dbg( p_sd, "Update required" );
169             psz_urls = var_GetNonEmptyString( p_sd, "podcast-urls" );
170             if( psz_urls != NULL )
171                 ParseUrls( p_sd, psz_urls );
172             free( psz_urls );
173             p_sys->b_update = false;
174         }
175
176         for( i = 0; i < p_sd->p_sys->i_input; i++ )
177         {
178             if( p_sd->p_sys->pp_input[i]->b_eof
179                 || p_sd->p_sys->pp_input[i]->b_error )
180             {
181                 input_StopThread( p_sd->p_sys->pp_input[i] );
182                 vlc_object_release( p_sd->p_sys->pp_input[i] );
183                 p_sd->p_sys->pp_input[i] = NULL;
184                 REMOVE_ELEM( p_sys->pp_input, p_sys->i_input, i );
185                 i--;
186             }
187         }
188         msleep( 500 );
189     }
190 }
191
192 static int UrlsChange( vlc_object_t *p_this, char const *psz_var,
193                        vlc_value_t oldval, vlc_value_t newval,
194                        void *p_data )
195 {
196     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
197     VLC_UNUSED(newval);
198     services_discovery_sys_t *p_sys  = (services_discovery_sys_t *)p_data;
199     p_sys->b_update = true;
200     return VLC_SUCCESS;
201 }
202
203 static void ParseUrls( services_discovery_t *p_sd, char *psz_urls )
204 {
205     services_discovery_sys_t *p_sys = p_sd->p_sys;
206     for( ;; )
207     {
208         int i;
209         char *psz_tok = strchr( psz_urls, '|' );
210         if( psz_tok ) *psz_tok = '\0';
211         for( i = 0; i < p_sys->i_urls; i++ )
212             if( !strcmp( psz_urls, p_sys->ppsz_urls[i] ) )
213                 break;
214         if( i == p_sys->i_urls )
215         {
216             /* Only add new urls.
217              * FIXME: We don't delete urls which have been removed from
218              * the config since we don't have a way to know which inputs
219              * they spawned */
220             input_item_t *p_input;
221             INSERT_ELEM( p_sys->ppsz_urls, p_sys->i_urls, p_sys->i_urls,
222                          strdup( psz_urls ) );
223             p_input = input_ItemNewExt( p_sd, psz_urls,
224                                         psz_urls, 0, NULL, -1 );
225             input_ItemAddOption( p_input, "demux=podcast" );
226             services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
227             INSERT_ELEM( p_sys->pp_input, p_sys->i_input, p_sys->i_input,
228                          input_CreateThread( p_sd, p_input ) );
229         }
230         if( psz_tok )  psz_urls = psz_tok+1;
231         else           return;
232     }
233 }