]> git.sesse.net Git - vlc/blob - modules/services_discovery/podcast.c
modules/services_discovery/podcast.c: Use the new API.
[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     input_thread_t **pp_input;
82
83     char **ppsz_urls;
84     int i_urls;
85 };
86
87 /*****************************************************************************
88  * Local prototypes
89  *****************************************************************************/
90
91 /* Main functions */
92     static void Run    ( services_discovery_t *p_intf );
93
94 /*****************************************************************************
95  * Open: initialize and create stuff
96  *****************************************************************************/
97 static int Open( vlc_object_t *p_this )
98 {
99     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
100     services_discovery_sys_t *p_sys  = malloc(
101                                     sizeof( services_discovery_sys_t ) );
102     
103     p_sys->i_urls = 0;
104     p_sys->ppsz_urls = NULL;
105     p_sys->pp_input = NULL;
106     
107     p_sd->pf_run = Run;
108     p_sd->p_sys  = p_sys;
109
110     /* Give us a name */
111     services_discovery_SetLocalizedName( p_sd, _("Podcasts") );
112
113     return VLC_SUCCESS;
114 }
115
116 /*****************************************************************************
117  * Close:
118  *****************************************************************************/
119 static void Close( vlc_object_t *p_this )
120 {
121     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
122     services_discovery_sys_t *p_sys  = p_sd->p_sys;
123     int i;
124     for( i = 0; i < p_sys->i_urls; i++ )
125     {
126         if( p_sd->p_sys->pp_input[i] )
127         {
128             input_StopThread( p_sd->p_sys->pp_input[i] );
129             input_DestroyThread( p_sd->p_sys->pp_input[i] );
130             p_sd->p_sys->pp_input[i] = NULL;
131         }
132     }
133     free( p_sd->p_sys->pp_input );
134     for( i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
135     free( p_sys->ppsz_urls );
136     free( p_sys );
137 }
138
139 /*****************************************************************************
140  * Run: main thread
141  *****************************************************************************/
142 static void Run( services_discovery_t *p_sd )
143 {
144     services_discovery_sys_t *p_sys  = p_sd->p_sys;
145
146     int i, j;
147     char *psz_buf;
148     char *psz_tmp = psz_buf = var_CreateGetString( p_sd, "podcast-urls" );
149
150     i = 0;
151     p_sys->i_urls = 1;
152     while( psz_buf[i] != 0 )
153         if( psz_buf[i++] == '|' )
154             p_sys->i_urls++;
155
156     p_sys->ppsz_urls = (char **)malloc( p_sys->i_urls * sizeof( char * ) );
157
158     i = 0;
159     j = 0;
160     while( psz_buf[i] != 0 )
161     {
162         if( psz_buf[i] == '|' )
163         {
164             psz_buf[i] = 0;
165             p_sys->ppsz_urls[j] = strdup( psz_tmp );
166             i++;
167             j++;
168             psz_tmp = psz_buf+i;
169         }
170         else
171             i++;
172     }
173     p_sys->ppsz_urls[j] = strdup( psz_tmp );
174     free( psz_buf );
175
176     p_sys->pp_input = malloc( p_sys->i_urls * sizeof( input_thread_t * ) );
177     for( i = 0; i < p_sys->i_urls; i++ )
178     {
179         input_item_t *p_input;
180         asprintf( &psz_buf, "%s", p_sys->ppsz_urls[i] );
181         p_input = input_ItemNewExt( p_sd, psz_buf,
182                                     p_sys->ppsz_urls[i], 0, NULL, -1 );
183         input_ItemAddOption( p_input, "demux=podcast" );
184         services_discovery_AddItem( p_sd, p_input, NULL /* no cat */ );
185         p_sys->pp_input[i] = input_CreateThread( p_sd, p_input );
186     }
187
188     while( !p_sd->b_die )
189     {
190         int i;
191         for( i = 0; i < p_sd->p_sys->i_urls; i++ )
192         {
193             if( p_sd->p_sys->pp_input[i] &&
194                 ( p_sd->p_sys->pp_input[i]->b_eof
195                   || p_sd->p_sys->pp_input[i]->b_error ) )
196             {
197                 input_StopThread( p_sd->p_sys->pp_input[i] );
198                 input_DestroyThread( p_sd->p_sys->pp_input[i] );
199                 p_sd->p_sys->pp_input[i] = NULL;
200             }
201         }
202         msleep( 100000 );
203     }
204 }