]> git.sesse.net Git - vlc/blob - modules/demux/playlist/podcast.c
Fixed support of mov/mp4 with fixed video samples size.
[vlc] / modules / demux / playlist / podcast.c
1 /*****************************************************************************
2  * podcast.c : podcast playlist imports
3  *****************************************************************************
4  * Copyright (C) 2005-2009 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  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_demux.h>
33
34 #include "playlist.h"
35 #include <vlc_xml.h>
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int Demux( demux_t *p_demux);
41 static int Control( demux_t *p_demux, int i_query, va_list args );
42 static mtime_t strTimeToMTime( const char *psz );
43
44 /*****************************************************************************
45  * Import_podcast: main import function
46  *****************************************************************************/
47 int Import_podcast( vlc_object_t *p_this )
48 {
49     demux_t *p_demux = (demux_t *)p_this;
50
51     if( !demux_IsForced( p_demux, "podcast" ) )
52         return VLC_EGENERIC;
53
54     p_demux->pf_demux = Demux;
55     p_demux->pf_control = Control;
56     msg_Dbg( p_demux, "using podcast reader" );
57
58     return VLC_SUCCESS;
59 }
60
61 /*****************************************************************************
62  * Deactivate: frees unused data
63  *****************************************************************************/
64 void Close_podcast( vlc_object_t *p_this )
65 {
66     (void)p_this;
67 }
68
69 /* "specs" : http://phobos.apple.com/static/iTunesRSS.html */
70 static int Demux( demux_t *p_demux )
71 {
72     bool b_item = false;
73     bool b_image = false;
74
75     xml_reader_t *p_xml_reader;
76     char *psz_elname = NULL;
77     char *psz_item_mrl = NULL;
78     char *psz_item_size = NULL;
79     char *psz_item_type = NULL;
80     char *psz_item_name = NULL;
81     char *psz_item_date = NULL;
82     char *psz_item_author = NULL;
83     char *psz_item_category = NULL;
84     char *psz_item_duration = NULL;
85     char *psz_item_keywords = NULL;
86     char *psz_item_subtitle = NULL;
87     char *psz_item_summary = NULL;
88     char *psz_art_url = NULL;
89     const char *node;
90     int i_type;
91     input_item_t *p_input;
92     input_item_node_t *p_subitems = NULL;
93
94     input_item_t *p_current_input = GetCurrentItem(p_demux);
95
96     p_xml_reader = xml_ReaderCreate( p_demux, p_demux->s );
97     if( !p_xml_reader )
98         goto error;
99
100     /* xml */
101     /* check root node */
102     if( xml_ReaderNextNode( p_xml_reader, &node ) != XML_READER_STARTELEM )
103     {
104         msg_Err( p_demux, "invalid file (no root node)" );
105         goto error;
106     }
107
108     if( strcmp( node, "rss" ) )
109     {
110         msg_Err( p_demux, "invalid root node <%s>", node );
111         goto error;
112     }
113
114     p_subitems = input_item_node_Create( p_current_input );
115
116     while( (i_type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
117     {
118         switch( i_type )
119         {
120             case XML_READER_STARTELEM:
121             {
122                 free( psz_elname );
123                 psz_elname = strdup( node );
124                 if( unlikely(!node) )
125                     goto error;
126
127                 if( !strcmp( node, "item" ) )
128                     b_item = true;
129                 else if( !strcmp( node, "image" ) )
130                     b_image = true;
131
132                 // Read the attributes
133                 const char *attr, *value;
134                 while( (attr = xml_ReaderNextAttr( p_xml_reader, &value )) )
135                 {
136                     if( !strcmp( node, "enclosure" ) )
137                     {
138                         char **p = NULL;
139                         if( !strcmp( attr, "url" ) )
140                             p = &psz_item_mrl;
141                         else if( !strcmp( attr, "length" ) )
142                             p = &psz_item_size;
143                         else if( !strcmp( attr, "type" ) )
144                             p = &psz_item_type;
145                         if( p != NULL )
146                         {
147                             free( *p );
148                             *p = strdup( value );
149                         }
150                         else
151                             msg_Dbg( p_demux,"unhandled attribute %s in <%s>",
152                                      attr, node );
153                     }
154                     else
155                         msg_Dbg( p_demux,"unhandled attribute %s in <%s>",
156                                  attr, node );
157                 }
158                 break;
159             }
160
161             case XML_READER_TEXT:
162             {
163                 if(!psz_elname) break;
164
165                 /* item specific meta data */
166                 if( b_item )
167                 {
168                     char **p;
169
170                     if( !strcmp( psz_elname, "title" ) )
171                         p = &psz_item_name;
172                     else if( !strcmp( psz_elname, "itunes:author" ) ||
173                              !strcmp( psz_elname, "author" ) )
174                         /* <author> isn't standard iTunes podcast stuff */
175                         p = &psz_item_author;
176                     else if( !strcmp( psz_elname, "itunes:summary" ) ||
177                              !strcmp( psz_elname, "description" ) )
178                         /* <description> isn't standard iTunes podcast stuff */
179                         p = &psz_item_summary;
180                     else if( !strcmp( psz_elname, "pubDate" ) )
181                         p = &psz_item_date;
182                     else if( !strcmp( psz_elname, "itunes:category" ) )
183                         p = &psz_item_category;
184                     else if( !strcmp( psz_elname, "itunes:duration" ) )
185                         p = &psz_item_duration;
186                     else if( !strcmp( psz_elname, "itunes:keywords" ) )
187                         p = &psz_item_keywords;
188                     else if( !strcmp( psz_elname, "itunes:subtitle" ) )
189                         p = &psz_item_subtitle;
190                     else
191                         break;
192
193                     free( *p );
194                     *p = strdup( node );
195                 }
196                 /* toplevel meta data */
197                 else if( !b_image )
198                 {
199                     if( !strcmp( psz_elname, "title" ) )
200                         input_item_SetName( p_current_input, node );
201 #define ADD_GINFO( info, name ) \
202     else if( !strcmp( psz_elname, name ) ) \
203         input_item_AddInfo( p_current_input, _("Podcast Info"), \
204                             info, "%s", node );
205                     ADD_GINFO( _("Podcast Link"), "link" )
206                     ADD_GINFO( _("Podcast Copyright"), "copyright" )
207                     ADD_GINFO( _("Podcast Category"), "itunes:category" )
208                     ADD_GINFO( _("Podcast Keywords"), "itunes:keywords" )
209                     ADD_GINFO( _("Podcast Subtitle"), "itunes:subtitle" )
210 #undef ADD_GINFO
211                     else if( !strcmp( psz_elname, "itunes:summary" ) ||
212                              !strcmp( psz_elname, "description" ) )
213                     { /* <description> isn't standard iTunes podcast stuff */
214                         input_item_AddInfo( p_current_input,
215                             _( "Podcast Info" ), _( "Podcast Summary" ),
216                             "%s", node );
217                     }
218                 }
219                 else
220                 {
221                     if( !strcmp( psz_elname, "url" ) )
222                     {
223                         free( psz_art_url );
224                         psz_art_url = strdup( node );
225                     }
226                     else
227                         msg_Dbg( p_demux, "unhandled text in element <%s>",
228                                  psz_elname );
229                 }
230                 break;
231             }
232
233             // End element
234             case XML_READER_ENDELEM:
235             {
236                 FREENULL( psz_elname );
237
238                 if( !strcmp( node, "item" ) )
239                 {
240                     if( psz_item_mrl == NULL )
241                     {
242                         msg_Err( p_demux, "invalid XML (no enclosure markup)" );
243                         goto error;
244                     }
245
246                     p_input = input_item_New( psz_item_mrl, psz_item_name );
247                     FREENULL( psz_item_mrl );
248                     FREENULL( psz_item_name );
249
250                     if( p_input == NULL )
251                         break; /* FIXME: meta data memory leaks? */
252
253                     /* Set the duration if available */
254                     if( psz_item_duration )
255                         input_item_SetDuration( p_input, strTimeToMTime( psz_item_duration ) );
256
257 #define ADD_INFO( info, field ) \
258     if( field ) { \
259         input_item_AddInfo( p_input, _( "Podcast Info" ), (info), "%s", \
260                             (field) ); \
261         FREENULL( field ); }
262                     ADD_INFO( _("Podcast Publication Date"), psz_item_date  );
263                     ADD_INFO( _("Podcast Author"), psz_item_author );
264                     ADD_INFO( _("Podcast Subcategory"), psz_item_category );
265                     ADD_INFO( _("Podcast Duration"), psz_item_duration );
266                     ADD_INFO( _("Podcast Keywords"), psz_item_keywords );
267                     ADD_INFO( _("Podcast Subtitle"), psz_item_subtitle );
268                     ADD_INFO( _("Podcast Summary"), psz_item_summary );
269                     ADD_INFO( _("Podcast Type"), psz_item_type );
270 #undef ADD_INFO
271
272                     /* Add the global art url to this item, if any */
273                     if( psz_art_url )
274                         input_item_SetArtURL( p_input, psz_art_url );
275
276                     if( psz_item_size )
277                     {
278                         input_item_AddInfo( p_input,
279                                                 _( "Podcast Info" ),
280                                                 _( "Podcast Size" ),
281                                                 _("%s bytes"),
282                                                 psz_item_size );
283                         FREENULL( psz_item_size );
284                     }
285                     input_item_node_AppendItem( p_subitems, p_input );
286                     vlc_gc_decref( p_input );
287                     b_item = false;
288                 }
289                 else if( !strcmp( node, "image" ) )
290                 {
291                     b_image = false;
292                 }
293                 break;
294             }
295         }
296     }
297
298     if( i_type < 0 )
299     {
300         msg_Warn( p_demux, "error while parsing data" );
301     }
302
303     free( psz_art_url );
304     free( psz_elname );
305     xml_ReaderDelete( p_xml_reader );
306
307     input_item_node_PostAndDelete( p_subitems );
308     vlc_gc_decref(p_current_input);
309     return 0; /* Needed for correct operation of go back */
310
311 error:
312     free( psz_item_name );
313     free( psz_item_mrl );
314     free( psz_item_size );
315     free( psz_item_type );
316     free( psz_item_date );
317     free( psz_item_author );
318     free( psz_item_category );
319     free( psz_item_duration );
320     free( psz_item_keywords );
321     free( psz_item_subtitle );
322     free( psz_item_summary );
323     free( psz_art_url );
324     free( psz_elname );
325
326     if( p_xml_reader )
327         xml_ReaderDelete( p_xml_reader );
328     if( p_subitems )
329         input_item_node_Delete( p_subitems );
330
331     vlc_gc_decref(p_current_input);
332     return -1;
333 }
334
335 static int Control( demux_t *p_demux, int i_query, va_list args )
336 {
337     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
338     return VLC_EGENERIC;
339 }
340
341 static mtime_t strTimeToMTime( const char *psz )
342 {
343     int h, m, s;
344     switch( sscanf( psz, "%u:%u:%u", &h, &m, &s ) )
345     {
346     case 3:
347         return (mtime_t)( ( h*60 + m )*60 + s ) * 1000000;
348     case 2:
349         return (mtime_t)( h*60 + m ) * 1000000;
350         break;
351     default:
352         return -1;
353     }
354 }