]> git.sesse.net Git - vlc/blob - modules/services_discovery/podcast.c
Forward port [15485] and [15486].
[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_("Enter the list of podcasts to retrieve, " \
59                          "separated by '|' (pipe)." )
60
61 vlc_module_begin();
62     set_shortname( "Podcast");
63     set_description( _("Podcasts") );
64     set_category( CAT_PLAYLIST );
65     set_subcategory( SUBCAT_PLAYLIST_SD );
66
67     add_string( "podcast-urls", NULL, NULL,
68                 URLS_TEXT, URLS_LONGTEXT, VLC_FALSE );
69
70     set_capability( "services_discovery", 0 );
71     set_callbacks( Open, Close );
72
73 vlc_module_end();
74
75
76 /*****************************************************************************
77  * Local structures
78  *****************************************************************************/
79
80 struct services_discovery_sys_t
81 {
82     /* playlist node */
83     playlist_item_t *p_node;
84     input_thread_t **pp_input;
85
86     char **ppsz_urls;
87     int i_urls;
88 };
89
90 /*****************************************************************************
91  * Local prototypes
92  *****************************************************************************/
93
94 /* Main functions */
95     static void Run    ( services_discovery_t *p_intf );
96
97 /*****************************************************************************
98  * Open: initialize and create stuff
99  *****************************************************************************/
100 static int Open( vlc_object_t *p_this )
101 {
102     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
103     services_discovery_sys_t *p_sys  = malloc(
104                                     sizeof( services_discovery_sys_t ) );
105
106     vlc_value_t         val;
107     playlist_t          *p_playlist;
108     playlist_view_t     *p_view;
109     playlist_item_t     *p_item;
110
111     int i, j;
112     char *psz_buf;
113     char *psz_tmp = psz_buf = var_CreateGetString( p_sd, "podcast-urls" );
114
115     i = 0;
116     p_sys->i_urls = 1;
117     while( psz_buf[i] != 0 )
118         if( psz_buf[i++] == '|' )
119             p_sys->i_urls++;
120
121     p_sys->ppsz_urls = (char **)malloc( p_sys->i_urls * sizeof( char * ) );
122
123     i = 0;
124     j = 0;
125     while( psz_buf[i] != 0 )
126     {
127         if( psz_buf[i] == '|' )
128         {
129             psz_buf[i] = 0;
130             p_sys->ppsz_urls[j] = strdup( psz_tmp );
131             i++;
132             j++;
133             psz_tmp = psz_buf+i;
134         }
135         else
136             i++;
137     }
138     p_sys->ppsz_urls[j] = strdup( psz_tmp );
139     free( psz_buf );
140
141     p_sd->pf_run = Run;
142     p_sd->p_sys  = p_sys;
143
144     /* Create our playlist node */
145     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
146                                                 FIND_ANYWHERE );
147     if( !p_playlist )
148     {
149         msg_Warn( p_sd, "unable to find playlist, cancelling");
150         return VLC_EGENERIC;
151     }
152
153     p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
154     p_sys->p_node = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
155                                          _("Podcast"), p_view->p_root );
156     p_sys->pp_input = malloc( p_sys->i_urls * sizeof( input_thread_t * ) );
157     for( i = 0; i < p_sys->i_urls; i++ )
158     {
159         asprintf( &psz_buf, "%s", p_sys->ppsz_urls[i] );
160         p_item = playlist_ItemNew( p_playlist, psz_buf,
161                                          p_sys->ppsz_urls[i] );
162         free( psz_buf );
163         playlist_ItemAddOption( p_item, "demux=podcast" );
164         playlist_NodeAddItem( p_playlist, p_item,
165                               p_sys->p_node->pp_parents[0]->i_view,
166                               p_sys->p_node, PLAYLIST_APPEND,
167                               PLAYLIST_END );
168
169         /* We need to declare the parents of the node as the same of the
170          * parent's ones */
171         playlist_CopyParents( p_sys->p_node, p_item );
172         p_sys->pp_input[i] = input_CreateThread( p_playlist, &p_item->input );
173     }
174
175     p_sys->p_node->i_flags |= PLAYLIST_RO_FLAG;
176     val.b_bool = VLC_TRUE;
177     var_Set( p_playlist, "intf-change", val );
178
179     vlc_object_release( p_playlist );
180
181     return VLC_SUCCESS;
182 }
183
184 /*****************************************************************************
185  * Close:
186  *****************************************************************************/
187 static void Close( vlc_object_t *p_this )
188 {
189     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
190     services_discovery_sys_t *p_sys  = p_sd->p_sys;
191     playlist_t *p_playlist =  (playlist_t *) vlc_object_find( p_sd,
192                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
193     int i;
194     for( i = 0; i < p_sys->i_urls; i++ )
195     {
196         if( p_sd->p_sys->pp_input[i] )
197         {
198             input_StopThread( p_sd->p_sys->pp_input[i] );
199             input_DestroyThread( p_sd->p_sys->pp_input[i] );
200             vlc_object_detach( p_sd->p_sys->pp_input[i] );
201             vlc_object_destroy( p_sd->p_sys->pp_input[i] );
202             p_sd->p_sys->pp_input[i] = NULL;
203         }
204     }
205     free( p_sd->p_sys->pp_input );
206     if( p_playlist )
207     {
208         playlist_NodeDelete( p_playlist, p_sys->p_node, VLC_TRUE, VLC_TRUE );
209         vlc_object_release( p_playlist );
210     }
211     for( i = 0; i < p_sys->i_urls; i++ ) free( p_sys->ppsz_urls[i] );
212     free( p_sys->ppsz_urls );
213     free( p_sys );
214 }
215
216 /*****************************************************************************
217  * Run: main thread
218  *****************************************************************************/
219 static void Run( services_discovery_t *p_sd )
220 {
221     while( !p_sd->b_die )
222     {
223         int i;
224         for( i = 0; i < p_sd->p_sys->i_urls; i++ )
225         {
226             if( p_sd->p_sys->pp_input[i] &&
227                 ( p_sd->p_sys->pp_input[i]->b_eof
228                   || p_sd->p_sys->pp_input[i]->b_error ) )
229             {
230                 input_StopThread( p_sd->p_sys->pp_input[i] );
231                 input_DestroyThread( p_sd->p_sys->pp_input[i] );
232                 vlc_object_detach( p_sd->p_sys->pp_input[i] );
233                 vlc_object_destroy( p_sd->p_sys->pp_input[i] );
234                 p_sd->p_sys->pp_input[i] = NULL;
235             }
236         }
237         msleep( 100000 );
238     }
239 }