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