]> git.sesse.net Git - vlc/blob - modules/services_discovery/podcast.c
Remove stdlib.h
[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 #define _GNU_SOURCE
28
29 #include <vlc/vlc.h>
30 #include <vlc_playlist.h>
31 #include <vlc_network.h>
32
33 #include <errno.h>                                                 /* ENOMEM */
34
35 #ifdef HAVE_UNISTD_H
36 #    include <unistd.h>
37 #endif
38 #ifdef HAVE_SYS_TIME_H
39 #    include <sys/time.h>
40 #endif
41
42 /************************************************************************
43  * Macros and definitions
44  ************************************************************************/
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49
50 /* Callbacks */
51     static int  Open ( vlc_object_t * );
52     static void Close( vlc_object_t * );
53
54 #define URLS_TEXT N_("Podcast URLs list")
55 #define URLS_LONGTEXT N_("Enter the list of podcasts to retrieve, " \
56                          "separated by '|' (pipe)." )
57
58 vlc_module_begin();
59     set_shortname( "Podcast");
60     set_description( _("Podcasts") );
61     set_category( CAT_PLAYLIST );
62     set_subcategory( SUBCAT_PLAYLIST_SD );
63
64     add_string( "podcast-urls", NULL, NULL,
65                 URLS_TEXT, URLS_LONGTEXT, VLC_FALSE );
66
67     set_capability( "services_discovery", 0 );
68     set_callbacks( Open, Close );
69
70 vlc_module_end();
71
72
73 /*****************************************************************************
74  * Local structures
75  *****************************************************************************/
76
77 struct services_discovery_sys_t
78 {
79     /* playlist node */
80     input_thread_t **pp_input;
81
82     char **ppsz_urls;
83     int i_urls;
84 };
85
86 /*****************************************************************************
87  * Local prototypes
88  *****************************************************************************/
89
90 /* Main functions */
91     static void Run    ( services_discovery_t *p_intf );
92
93 /*****************************************************************************
94  * Open: initialize and create stuff
95  *****************************************************************************/
96 static int Open( vlc_object_t *p_this )
97 {
98     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
99     services_discovery_sys_t *p_sys  = malloc(
100                                     sizeof( services_discovery_sys_t ) );
101     
102     p_sys->i_urls = 0;
103     p_sys->ppsz_urls = NULL;
104     p_sys->pp_input = NULL;
105     
106     p_sd->pf_run = Run;
107     p_sd->p_sys  = p_sys;
108
109     /* Give us a name */
110     services_discovery_SetLocalizedName( p_sd, _("Podcasts") );
111
112     return VLC_SUCCESS;
113 }
114
115 /*****************************************************************************
116  * Close:
117  *****************************************************************************/
118 static void Close( vlc_object_t *p_this )
119 {
120     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
121     services_discovery_sys_t *p_sys  = p_sd->p_sys;
122     int i;
123     for( i = 0; i < p_sys->i_urls; i++ )
124     {
125         if( p_sd->p_sys->pp_input[i] )
126         {
127             input_StopThread( p_sd->p_sys->pp_input[i] );
128             input_DestroyThread( p_sd->p_sys->pp_input[i] );
129             p_sd->p_sys->pp_input[i] = NULL;
130         }
131     }
132     free( p_sd->p_sys->pp_input );
133     for( i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
134     free( p_sys->ppsz_urls );
135     free( p_sys );
136 }
137
138 /*****************************************************************************
139  * Run: main thread
140  *****************************************************************************/
141 static void Run( services_discovery_t *p_sd )
142 {
143     services_discovery_sys_t *p_sys  = p_sd->p_sys;
144
145     int i, j;
146     char *psz_buf;
147     char *psz_tmp = psz_buf = var_CreateGetString( p_sd, "podcast-urls" );
148
149     i = 0;
150     p_sys->i_urls = psz_buf[0] ? 1 : 0;
151     while( psz_buf[i] != 0 )
152         if( psz_buf[i++] == '|' )
153             p_sys->i_urls++;
154
155     p_sys->ppsz_urls = (char **)malloc( p_sys->i_urls * sizeof( char * ) );
156
157     i = 0;
158     j = 0;
159     while( psz_buf[i] != 0 )
160     {
161         if( psz_buf[i] == '|' )
162         {
163             psz_buf[i] = 0;
164             p_sys->ppsz_urls[j] = strdup( psz_tmp );
165             i++;
166             j++;
167             psz_tmp = psz_buf+i;
168         }
169         else
170             i++;
171     }
172     p_sys->ppsz_urls[j] = strdup( psz_tmp );
173     free( psz_buf );
174
175     p_sys->pp_input = malloc( p_sys->i_urls * sizeof( input_thread_t * ) );
176     for( i = 0; i < p_sys->i_urls; i++ )
177     {
178         input_item_t *p_input;
179         asprintf( &psz_buf, "%s", p_sys->ppsz_urls[i] );
180         p_input = input_ItemNewExt( p_sd, psz_buf,
181                                     p_sys->ppsz_urls[i], 0, NULL, -1 );
182         input_ItemAddOption( p_input, "demux=podcast" );
183         services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
184         p_sys->pp_input[i] = input_CreateThread( p_sd, p_input );
185     }
186
187     while( !p_sd->b_die )
188     {
189         int i;
190         for( i = 0; i < p_sd->p_sys->i_urls; i++ )
191         {
192             if( p_sd->p_sys->pp_input[i] &&
193                 ( p_sd->p_sys->pp_input[i]->b_eof
194                   || p_sd->p_sys->pp_input[i]->b_error ) )
195             {
196                 input_StopThread( p_sd->p_sys->pp_input[i] );
197                 input_DestroyThread( p_sd->p_sys->pp_input[i] );
198                 p_sd->p_sys->pp_input[i] = NULL;
199             }
200         }
201         msleep( 100000 );
202     }
203 }