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