]> git.sesse.net Git - vlc/blob - modules/services_discovery/podcast.c
* Start cleaning up libvlc playlist API (Refs:#457)
[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_playlist.h>
32 #include <vlc_network.h>
33
34 #include <errno.h>                                                 /* ENOMEM */
35
36 #ifdef HAVE_UNISTD_H
37 #    include <unistd.h>
38 #endif
39 #ifdef HAVE_SYS_TIME_H
40 #    include <sys/time.h>
41 #endif
42
43 /************************************************************************
44  * Macros and definitions
45  ************************************************************************/
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50
51 /* Callbacks */
52     static int  Open ( vlc_object_t * );
53     static void Close( vlc_object_t * );
54
55 #define URLS_TEXT N_("Podcast URLs list")
56 #define URLS_LONGTEXT N_("Enter the list of podcasts to retrieve, " \
57                          "separated by '|' (pipe)." )
58
59 vlc_module_begin();
60     set_shortname( "Podcast");
61     set_description( _("Podcasts") );
62     set_category( CAT_PLAYLIST );
63     set_subcategory( SUBCAT_PLAYLIST_SD );
64
65     add_string( "podcast-urls", NULL, NULL,
66                 URLS_TEXT, URLS_LONGTEXT, VLC_FALSE );
67
68     set_capability( "services_discovery", 0 );
69     set_callbacks( Open, Close );
70
71 vlc_module_end();
72
73
74 /*****************************************************************************
75  * Local structures
76  *****************************************************************************/
77
78 struct services_discovery_sys_t
79 {
80     /* playlist node */
81     playlist_item_t *p_node_cat;
82     playlist_item_t *p_node_one;
83     input_thread_t **pp_input;
84
85     char **ppsz_urls;
86     int i_urls;
87 };
88
89 /*****************************************************************************
90  * Local prototypes
91  *****************************************************************************/
92
93 /* Main functions */
94     static void Run    ( services_discovery_t *p_intf );
95
96 /*****************************************************************************
97  * Open: initialize and create stuff
98  *****************************************************************************/
99 static int Open( vlc_object_t *p_this )
100 {
101     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
102     services_discovery_sys_t *p_sys  = malloc(
103                                     sizeof( services_discovery_sys_t ) );
104
105     vlc_value_t         val;
106     playlist_t          *p_playlist;
107     playlist_item_t     *p_item;
108
109     int i, j;
110     char *psz_buf;
111     char *psz_tmp = psz_buf = var_CreateGetString( p_sd, "podcast-urls" );
112
113     i = 0;
114     p_sys->i_urls = 1;
115     while( psz_buf[i] != 0 )
116         if( psz_buf[i++] == '|' )
117             p_sys->i_urls++;
118
119     p_sys->ppsz_urls = (char **)malloc( p_sys->i_urls * sizeof( char * ) );
120
121     i = 0;
122     j = 0;
123     while( psz_buf[i] != 0 )
124     {
125         if( psz_buf[i] == '|' )
126         {
127             psz_buf[i] = 0;
128             p_sys->ppsz_urls[j] = strdup( psz_tmp );
129             i++;
130             j++;
131             psz_tmp = psz_buf+i;
132         }
133         else
134             i++;
135     }
136     p_sys->ppsz_urls[j] = strdup( psz_tmp );
137     free( psz_buf );
138
139     p_sd->pf_run = Run;
140     p_sd->p_sys  = p_sys;
141
142     /* Create our playlist node */
143     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
144                                                 FIND_ANYWHERE );
145     if( !p_playlist )
146     {
147         msg_Warn( p_sd, "unable to find playlist, cancelling");
148         return VLC_EGENERIC;
149     }
150
151     p_sys->p_node_cat = playlist_NodeCreate( p_playlist, _("Podcast"),
152                                          p_playlist->p_root_category, 0 );
153     p_sys->p_node_one = playlist_NodeCreate( p_playlist, _("Podcast"),
154                                          p_playlist->p_root_onelevel, 0 );
155     p_sys->p_node_one->p_input->i_id = p_sys->p_node_cat->p_input->i_id;
156
157     p_sys->p_node_one->i_flags |= PLAYLIST_RO_FLAG;
158     p_sys->p_node_cat->i_flags |= PLAYLIST_RO_FLAG;
159     p_sys->p_node_one->i_flags |= PLAYLIST_SKIP_FLAG;
160     p_sys->p_node_cat->i_flags |= PLAYLIST_SKIP_FLAG;
161     p_sys->pp_input = malloc( p_sys->i_urls * sizeof( input_thread_t * ) );
162     for( i = 0; i < p_sys->i_urls; i++ )
163     {
164         input_item_t *p_input;
165         asprintf( &psz_buf, "%s", p_sys->ppsz_urls[i] );
166         p_input = input_ItemNewExt( p_playlist, psz_buf,
167                                     p_sys->ppsz_urls[i], 0, NULL, -1 );
168         input_ItemAddOption( p_input, "demux=podcast" );
169         p_item = playlist_NodeAddInput( p_playlist, p_input, p_sys->p_node_cat,
170                                         PLAYLIST_APPEND, PLAYLIST_END,
171                                         VLC_FALSE );
172         p_item = playlist_NodeAddInput( p_playlist, p_input, p_sys->p_node_one,
173                                         PLAYLIST_APPEND, PLAYLIST_END,
174                                         VLC_FALSE );
175         free( psz_buf );
176         p_sys->pp_input[i] = input_CreateThread( p_playlist, p_input );
177     }
178
179     val.b_bool = VLC_TRUE;
180     var_Set( p_playlist, "intf-change", val );
181
182     vlc_object_release( p_playlist );
183
184     return VLC_SUCCESS;
185 }
186
187 /*****************************************************************************
188  * Close:
189  *****************************************************************************/
190 static void Close( vlc_object_t *p_this )
191 {
192     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
193     services_discovery_sys_t *p_sys  = p_sd->p_sys;
194     playlist_t *p_playlist =  (playlist_t *) vlc_object_find( p_sd,
195                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
196     int i;
197     for( i = 0; i < p_sys->i_urls; i++ )
198     {
199         if( p_sd->p_sys->pp_input[i] )
200         {
201             input_StopThread( p_sd->p_sys->pp_input[i] );
202             input_DestroyThread( p_sd->p_sys->pp_input[i] );
203             vlc_object_detach( p_sd->p_sys->pp_input[i] );
204             vlc_object_destroy( p_sd->p_sys->pp_input[i] );
205             p_sd->p_sys->pp_input[i] = NULL;
206         }
207     }
208     free( p_sd->p_sys->pp_input );
209     if( p_playlist )
210     {
211         playlist_NodeDelete( p_playlist, p_sys->p_node_cat, VLC_TRUE, VLC_TRUE );
212         playlist_NodeDelete( p_playlist, p_sys->p_node_one, VLC_TRUE, VLC_TRUE );
213         vlc_object_release( p_playlist );
214     }
215     for( i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
216     free( p_sys->ppsz_urls );
217     free( p_sys );
218 }
219
220 /*****************************************************************************
221  * Run: main thread
222  *****************************************************************************/
223 static void Run( services_discovery_t *p_sd )
224 {
225     while( !p_sd->b_die )
226     {
227         int i;
228         for( i = 0; i < p_sd->p_sys->i_urls; i++ )
229         {
230             if( p_sd->p_sys->pp_input[i] &&
231                 ( p_sd->p_sys->pp_input[i]->b_eof
232                   || p_sd->p_sys->pp_input[i]->b_error ) )
233             {
234                 input_StopThread( p_sd->p_sys->pp_input[i] );
235                 input_DestroyThread( p_sd->p_sys->pp_input[i] );
236                 vlc_object_detach( p_sd->p_sys->pp_input[i] );
237                 vlc_object_destroy( p_sd->p_sys->pp_input[i] );
238                 p_sd->p_sys->pp_input[i] = NULL;
239             }
240         }
241         msleep( 100000 );
242     }
243 }