]> git.sesse.net Git - vlc/blob - modules/services_discovery/podcast.c
FSF address change.
[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 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/intf.h>
31
32 #include <vlc/input.h>
33
34 #include "network.h"
35
36 #include <errno.h>                                                 /* ENOMEM */
37
38 #ifdef HAVE_UNISTD_H
39 #    include <unistd.h>
40 #endif
41 #ifdef HAVE_SYS_TIME_H
42 #    include <sys/time.h>
43 #endif
44
45 /************************************************************************
46  * Macros and definitions
47  ************************************************************************/
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52
53 /* Callbacks */
54     static int  Open ( vlc_object_t * );
55     static void Close( vlc_object_t * );
56
57 #define URLS_TEXT N_("Podcast URLs list")
58 #define URLS_LONGTEXT N_("Podcast '|' (pipe) seperated URLs list")
59
60 vlc_module_begin();
61     set_shortname( "Podcast");
62     set_description( _("Podcast Service Discovery") );
63     set_category( CAT_PLAYLIST );
64     set_subcategory( SUBCAT_PLAYLIST_SD );
65
66     add_string( "podcast-urls", NULL, NULL,
67                 URLS_TEXT, URLS_LONGTEXT, VLC_FALSE );
68
69     set_capability( "services_discovery", 0 );
70     set_callbacks( Open, Close );
71
72 vlc_module_end();
73
74
75 /*****************************************************************************
76  * Local structures
77  *****************************************************************************/
78
79 struct services_discovery_sys_t
80 {
81     /* playlist node */
82     playlist_item_t *p_node;
83     input_thread_t *p_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_view_t     *p_view;
108     playlist_item_t     *p_item;
109
110     int i, j;
111     char *psz_buf;
112     char *psz_tmp = psz_buf = var_CreateGetString( p_sd, "podcast-urls" );
113
114     i = 0;
115     p_sys->i_urls = 1;
116     while( psz_buf[i] != 0 )
117         if( psz_buf[i++] == '|' )
118             p_sys->i_urls++;
119
120     p_sys->ppsz_urls = (char **)malloc( p_sys->i_urls * sizeof( char * ) );
121
122     i = 0;
123     j = 0;
124     while( psz_buf[i] != 0 )
125     {
126         if( psz_buf[i] == '|' )
127         {
128             psz_buf[i] = 0;
129             p_sys->ppsz_urls[j] = strdup( psz_tmp );
130             i++;
131             j++;
132             psz_tmp = psz_buf+i;
133         }
134         else
135             i++;
136     }
137     p_sys->ppsz_urls[j] = strdup( psz_tmp );
138     free( psz_buf );
139
140     p_sd->pf_run = Run;
141     p_sd->p_sys  = p_sys;
142
143     /* Create our playlist node */
144     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
145                                                 FIND_ANYWHERE );
146     if( !p_playlist )
147     {
148         msg_Warn( p_sd, "unable to find playlist, cancelling");
149         return VLC_EGENERIC;
150     }
151
152     p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
153     p_sys->p_node = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
154                                          _("Podcast"), p_view->p_root );
155     for( i = 0; i < p_sys->i_urls; i++ )
156     {
157         asprintf( &psz_buf, "%s", p_sys->ppsz_urls[i] );
158         p_item = playlist_ItemNew( p_playlist, psz_buf,
159                                          p_sys->ppsz_urls[i] );
160         free( psz_buf );
161         playlist_ItemAddOption( p_item, "demux=podcast" );
162         playlist_NodeAddItem( p_playlist, p_item,
163                               p_sys->p_node->pp_parents[0]->i_view,
164                               p_sys->p_node, PLAYLIST_APPEND,
165                               PLAYLIST_END );
166
167         /* We need to declare the parents of the node as the same of the
168          * parent's ones */
169         playlist_CopyParents( p_sys->p_node, p_item );
170         p_sys->p_input = input_CreateThread( p_playlist, &p_item->input );
171     }
172
173
174     p_sys->p_node->i_flags |= PLAYLIST_RO_FLAG;
175     val.b_bool = VLC_TRUE;
176     var_Set( p_playlist, "intf-change", val );
177
178     vlc_object_release( p_playlist );
179
180     return VLC_SUCCESS;
181 }
182
183 /*****************************************************************************
184  * Close:
185  *****************************************************************************/
186 static void Close( vlc_object_t *p_this )
187 {
188     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
189     services_discovery_sys_t *p_sys  = p_sd->p_sys;
190     playlist_t *p_playlist =  (playlist_t *) vlc_object_find( p_sd,
191                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
192     int i;
193     if( p_sd->p_sys->p_input )
194     {
195         input_StopThread( p_sd->p_sys->p_input );
196         input_DestroyThread( p_sd->p_sys->p_input );
197         vlc_object_detach( p_sd->p_sys->p_input );
198         vlc_object_destroy( p_sd->p_sys->p_input );
199         p_sd->p_sys->p_input = NULL;
200     }
201     if( p_playlist )
202     {
203         playlist_NodeDelete( p_playlist, p_sys->p_node, VLC_TRUE, VLC_TRUE );
204         vlc_object_release( p_playlist );
205     }
206     for( i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
207     free( p_sys->ppsz_urls );
208     free( p_sys );
209 }
210
211 /*****************************************************************************
212  * Run: main thread
213  *****************************************************************************/
214 static void Run( services_discovery_t *p_sd )
215 {
216     while( !p_sd->b_die )
217     {
218         if( p_sd->p_sys->p_input &&
219             ( p_sd->p_sys->p_input->b_eof || p_sd->p_sys->p_input->b_error ) )
220         {
221             input_StopThread( p_sd->p_sys->p_input );
222             input_DestroyThread( p_sd->p_sys->p_input );
223             vlc_object_detach( p_sd->p_sys->p_input );
224             vlc_object_destroy( p_sd->p_sys->p_input );
225             p_sd->p_sys->p_input = NULL;
226         }
227         msleep( 100000 );
228     }
229 }