]> git.sesse.net Git - vlc/blob - modules/services_discovery/podcast.c
Fix a bunch of gcc warnings
[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 #include <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/intf.h>
32
33 #include <vlc/input.h>
34
35 #include "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( _("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, VLC_FALSE );
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     playlist_item_t *p_node_cat;
85     playlist_item_t *p_node_one;
86     input_thread_t **pp_input;
87
88     char **ppsz_urls;
89     int i_urls;
90 };
91
92 /*****************************************************************************
93  * Local prototypes
94  *****************************************************************************/
95
96 /* Main functions */
97     static void Run    ( services_discovery_t *p_intf );
98
99 /*****************************************************************************
100  * Open: initialize and create stuff
101  *****************************************************************************/
102 static int Open( vlc_object_t *p_this )
103 {
104     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
105     services_discovery_sys_t *p_sys  = malloc(
106                                     sizeof( services_discovery_sys_t ) );
107
108     vlc_value_t         val;
109     playlist_t          *p_playlist;
110     playlist_item_t     *p_item;
111
112     int i, j;
113     char *psz_buf;
114     char *psz_tmp = psz_buf = var_CreateGetString( p_sd, "podcast-urls" );
115
116     i = 0;
117     p_sys->i_urls = 1;
118     while( psz_buf[i] != 0 )
119         if( psz_buf[i++] == '|' )
120             p_sys->i_urls++;
121
122     p_sys->ppsz_urls = (char **)malloc( p_sys->i_urls * sizeof( char * ) );
123
124     i = 0;
125     j = 0;
126     while( psz_buf[i] != 0 )
127     {
128         if( psz_buf[i] == '|' )
129         {
130             psz_buf[i] = 0;
131             p_sys->ppsz_urls[j] = strdup( psz_tmp );
132             i++;
133             j++;
134             psz_tmp = psz_buf+i;
135         }
136         else
137             i++;
138     }
139     p_sys->ppsz_urls[j] = strdup( psz_tmp );
140     free( psz_buf );
141
142     p_sd->pf_run = Run;
143     p_sd->p_sys  = p_sys;
144
145     /* Create our playlist node */
146     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
147                                                 FIND_ANYWHERE );
148     if( !p_playlist )
149     {
150         msg_Warn( p_sd, "unable to find playlist, cancelling");
151         return VLC_EGENERIC;
152     }
153
154     p_sys->p_node_cat = playlist_NodeCreate( p_playlist, _("Podcast"),
155                                          p_playlist->p_root_category );
156     p_sys->p_node_one = playlist_NodeCreate( p_playlist, _("Podcast"),
157                                          p_playlist->p_root_onelevel );
158     p_sys->p_node_one->p_input->i_id = p_sys->p_node_cat->p_input->i_id;
159
160     p_sys->p_node_one->i_flags |= PLAYLIST_RO_FLAG;
161     p_sys->p_node_cat->i_flags |= PLAYLIST_RO_FLAG;
162     p_sys->p_node_one->i_flags |= PLAYLIST_SKIP_FLAG;
163     p_sys->p_node_cat->i_flags |= PLAYLIST_SKIP_FLAG;
164     p_sys->pp_input = malloc( p_sys->i_urls * sizeof( input_thread_t * ) );
165     for( i = 0; i < p_sys->i_urls; i++ )
166     {
167         input_item_t *p_input;
168         asprintf( &psz_buf, "%s", p_sys->ppsz_urls[i] );
169         p_input = input_ItemNewExt( p_playlist, psz_buf,
170                                     p_sys->ppsz_urls[i], 0, NULL, -1 );
171         input_ItemAddOption( p_input, "demux=podcast" );
172         p_item = playlist_NodeAddInput( p_playlist, p_input, p_sys->p_node_cat,
173                                         PLAYLIST_APPEND, PLAYLIST_END );
174         p_item = playlist_NodeAddInput( p_playlist, p_input, p_sys->p_node_one,
175                                         PLAYLIST_APPEND, PLAYLIST_END );
176         free( psz_buf );
177         p_sys->pp_input[i] = input_CreateThread( p_playlist, p_input );
178     }
179
180     val.b_bool = VLC_TRUE;
181     var_Set( p_playlist, "intf-change", val );
182
183     vlc_object_release( p_playlist );
184
185     return VLC_SUCCESS;
186 }
187
188 /*****************************************************************************
189  * Close:
190  *****************************************************************************/
191 static void Close( vlc_object_t *p_this )
192 {
193     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
194     services_discovery_sys_t *p_sys  = p_sd->p_sys;
195     playlist_t *p_playlist =  (playlist_t *) vlc_object_find( p_sd,
196                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
197     int i;
198     for( i = 0; i < p_sys->i_urls; i++ )
199     {
200         if( p_sd->p_sys->pp_input[i] )
201         {
202             input_StopThread( p_sd->p_sys->pp_input[i] );
203             input_DestroyThread( p_sd->p_sys->pp_input[i] );
204             vlc_object_detach( p_sd->p_sys->pp_input[i] );
205             vlc_object_destroy( p_sd->p_sys->pp_input[i] );
206             p_sd->p_sys->pp_input[i] = NULL;
207         }
208     }
209     free( p_sd->p_sys->pp_input );
210     if( p_playlist )
211     {
212         playlist_NodeDelete( p_playlist, p_sys->p_node_cat, VLC_TRUE, VLC_TRUE );
213         playlist_NodeDelete( p_playlist, p_sys->p_node_one, VLC_TRUE, VLC_TRUE );
214         vlc_object_release( p_playlist );
215     }
216     for( i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
217     free( p_sys->ppsz_urls );
218     free( p_sys );
219 }
220
221 /*****************************************************************************
222  * Run: main thread
223  *****************************************************************************/
224 static void Run( services_discovery_t *p_sd )
225 {
226     while( !p_sd->b_die )
227     {
228         int i;
229         for( i = 0; i < p_sd->p_sys->i_urls; i++ )
230         {
231             if( p_sd->p_sys->pp_input[i] &&
232                 ( p_sd->p_sys->pp_input[i]->b_eof
233                   || p_sd->p_sys->pp_input[i]->b_error ) )
234             {
235                 input_StopThread( p_sd->p_sys->pp_input[i] );
236                 input_DestroyThread( p_sd->p_sys->pp_input[i] );
237                 vlc_object_detach( p_sd->p_sys->pp_input[i] );
238                 vlc_object_destroy( p_sd->p_sys->pp_input[i] );
239                 p_sd->p_sys->pp_input[i] = NULL;
240             }
241         }
242         msleep( 100000 );
243     }
244 }