asx: fix mimetype and stream Peek
[vlc.git] / modules / demux / playlist / itml.c
blob2f4b24cb42708f496ce98d2a13078244029d11ce
1 /*******************************************************************************
2 * itml.c : iTunes Music Library import functions
3 *******************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Yoann Peronneau <yoann@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *******************************************************************************/
23 /**
24 * \file modules/demux/playlist/itml.c
25 * \brief iTunes Music Library import functions
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_access.h>
34 #include <vlc_xml.h>
35 #include <vlc_strings.h>
36 #include <vlc_url.h>
38 #include "itml.h"
39 #include "playlist.h"
41 static int ReadDir( stream_t *, input_item_node_t * );
43 /**
44 * \brief iTML submodule initialization function
46 int Import_iTML( vlc_object_t *p_this )
48 stream_t *p_demux = (stream_t *)p_this;
49 CHECK_FILE(p_demux);
50 if( !stream_HasExtension( p_demux, ".xml" )
51 && !p_demux->obj.force )
52 return VLC_EGENERIC;
54 const uint8_t *p_peek;
55 const ssize_t i_peek = vlc_stream_Peek( p_demux->s, &p_peek, 128 );
56 if ( i_peek < 32 ||
57 !strnstr( (const char *) p_peek, "<!DOCTYPE plist ", i_peek ) )
58 return VLC_EGENERIC;
60 msg_Dbg( p_demux, "using iTunes Media Library reader" );
62 p_demux->pf_readdir = ReadDir;
63 p_demux->pf_control = access_vaDirectoryControlHelper;
65 return VLC_SUCCESS;
68 /**
69 * \brief demuxer function for iTML parsing
71 static int ReadDir( stream_t *p_demux, input_item_node_t *p_subitems )
73 xml_reader_t *p_xml_reader;
74 const char *node;
76 p_demux->p_sys = (void *)(uintptr_t)0;
78 /* create new xml parser from stream */
79 p_xml_reader = xml_ReaderCreate( p_demux, p_demux->s );
80 if( !p_xml_reader )
81 goto end;
83 /* locating the root node */
84 int type;
87 type = xml_ReaderNextNode( p_xml_reader, &node );
88 if( type <= 0 )
90 msg_Err( p_demux, "can't read xml stream" );
91 goto end;
94 while( type != XML_READER_STARTELEM );
96 /* checking root node name */
97 if( strcmp( node, "plist" ) )
99 msg_Err( p_demux, "invalid root node <%s>", node );
100 goto end;
103 xml_elem_hnd_t pl_elements[] =
104 { {"dict", COMPLEX_CONTENT, {.cmplx = parse_plist_dict} } };
105 parse_plist_node( p_demux, p_subitems, NULL, p_xml_reader, "plist",
106 pl_elements );
108 end:
109 if( p_xml_reader )
110 xml_ReaderDelete( p_xml_reader );
112 /* Needed for correct operation of go back */
113 return 0;
117 * \brief parse the root node of the playlist
119 static bool parse_plist_node( stream_t *p_demux, input_item_node_t *p_input_node,
120 track_elem_t *p_track, xml_reader_t *p_xml_reader,
121 const char *psz_element,
122 xml_elem_hnd_t *p_handlers )
124 VLC_UNUSED(p_track); VLC_UNUSED(psz_element);
125 const char *attr, *value;
126 bool b_version_found = false;
128 /* read all playlist attributes */
129 while( (attr = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
131 /* attribute: version */
132 if( !strcmp( attr, "version" ) )
134 b_version_found = true;
135 if( strcmp( value, "1.0" ) )
136 msg_Warn( p_demux, "unsupported iTunes Media Library version" );
138 /* unknown attribute */
139 else
140 msg_Warn( p_demux, "invalid <plist> attribute:\"%s\"", attr );
143 /* attribute version is mandatory !!! */
144 if( !b_version_found )
145 msg_Warn( p_demux, "<plist> requires \"version\" attribute" );
147 return parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
148 "plist", p_handlers );
152 * \brief parse a <dict>
153 * \param COMPLEX_INTERFACE
155 static bool parse_dict( stream_t *p_demux, input_item_node_t *p_input_node,
156 track_elem_t *p_track, xml_reader_t *p_xml_reader,
157 const char *psz_element, xml_elem_hnd_t *p_handlers )
159 int i_node;
160 const char *node;
161 char *psz_value = NULL;
162 char *psz_key = NULL;
163 xml_elem_hnd_t *p_handler = NULL;
164 bool b_ret = false;
166 while( (i_node = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
168 switch( i_node )
170 /* element start tag */
171 case XML_READER_STARTELEM:
172 /* choose handler */
173 for( p_handler = p_handlers;
174 p_handler->name && strcmp( node, p_handler->name );
175 p_handler++ );
176 if( !p_handler->name )
178 msg_Err( p_demux, "unexpected element <%s>", node );
179 goto end;
181 /* complex content is parsed in a separate function */
182 if( p_handler->type == COMPLEX_CONTENT )
184 if( p_handler->pf_handler.cmplx( p_demux, p_input_node, NULL,
185 p_xml_reader, p_handler->name,
186 NULL ) )
188 p_handler = NULL;
189 FREENULL( psz_key );
190 FREENULL( psz_value );
192 else
193 goto end;
195 break;
197 /* simple element content */
198 case XML_READER_TEXT:
199 free( psz_value );
200 psz_value = strdup( node );
201 if( unlikely(!psz_value) )
202 goto end;
203 break;
205 /* element end tag */
206 case XML_READER_ENDELEM:
207 /* leave if the current parent node <track> is terminated */
208 if( !strcmp( node, psz_element ) )
210 b_ret = true;
211 goto end;
213 /* there MUST have been a start tag for that element name */
214 if( !p_handler || !p_handler->name
215 || strcmp( p_handler->name, node ) )
217 msg_Err( p_demux, "there's no open element left for <%s>",
218 node );
219 goto end;
221 /* special case: key */
222 if( !strcmp( p_handler->name, "key" ) )
224 free( psz_key );
225 psz_key = strdup( psz_value );
227 /* call the simple handler */
228 else if( p_handler->pf_handler.smpl )
230 p_handler->pf_handler.smpl( p_track, psz_key, psz_value, p_demux->p_sys );
232 FREENULL(psz_value);
233 p_handler = NULL;
234 break;
237 msg_Err( p_demux, "unexpected end of XML data" );
239 end:
240 free( psz_value );
241 free( psz_key );
242 return b_ret;
245 static bool parse_plist_dict( stream_t *p_demux, input_item_node_t *p_input_node,
246 track_elem_t *p_track, xml_reader_t *p_xml_reader,
247 const char *psz_element,
248 xml_elem_hnd_t *p_handlers )
250 VLC_UNUSED(p_track); VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
251 xml_elem_hnd_t pl_elements[] =
252 { {"dict", COMPLEX_CONTENT, {.cmplx = parse_tracks_dict} },
253 {"array", SIMPLE_CONTENT, {NULL} },
254 {"key", SIMPLE_CONTENT, {NULL} },
255 {"integer", SIMPLE_CONTENT, {NULL} },
256 {"string", SIMPLE_CONTENT, {NULL} },
257 {"date", SIMPLE_CONTENT, {NULL} },
258 {"true", SIMPLE_CONTENT, {NULL} },
259 {"false", SIMPLE_CONTENT, {NULL} },
260 {NULL, UNKNOWN_CONTENT, {NULL} }
263 return parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
264 "dict", pl_elements );
267 static bool parse_tracks_dict( stream_t *p_demux, input_item_node_t *p_input_node,
268 track_elem_t *p_track, xml_reader_t *p_xml_reader,
269 const char *psz_element,
270 xml_elem_hnd_t *p_handlers )
272 VLC_UNUSED(p_track); VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
273 xml_elem_hnd_t tracks_elements[] =
274 { {"dict", COMPLEX_CONTENT, {.cmplx = parse_track_dict} },
275 {"key", SIMPLE_CONTENT, {NULL} },
276 {NULL, UNKNOWN_CONTENT, {NULL} }
279 parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
280 "dict", tracks_elements );
282 msg_Info( p_demux, "added %zi tracks successfully",
283 (size_t)p_demux->p_sys );
285 return true;
288 static bool parse_track_dict( stream_t *p_demux, input_item_node_t *p_input_node,
289 track_elem_t *p_track, xml_reader_t *p_xml_reader,
290 const char *psz_element,
291 xml_elem_hnd_t *p_handlers )
293 VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
294 input_item_t *p_new_input = NULL;
295 int i_ret;
296 p_track = new_track();
298 xml_elem_hnd_t track_elements[] =
299 { {"array", COMPLEX_CONTENT, {.cmplx = skip_element} },
300 {"key", SIMPLE_CONTENT, {.smpl = save_data} },
301 {"integer", SIMPLE_CONTENT, {.smpl = save_data} },
302 {"string", SIMPLE_CONTENT, {.smpl = save_data} },
303 {"date", SIMPLE_CONTENT, {.smpl = save_data} },
304 {"true", SIMPLE_CONTENT, {NULL} },
305 {"false", SIMPLE_CONTENT, {NULL} },
306 {NULL, UNKNOWN_CONTENT, {NULL} }
309 i_ret = parse_dict( p_demux, p_input_node, p_track,
310 p_xml_reader, "dict", track_elements );
312 msg_Dbg( p_demux, "name: %s, artist: %s, album: %s, genre: %s, trackNum: %s, location: %s",
313 p_track->name, p_track->artist, p_track->album, p_track->genre, p_track->trackNum, p_track->location );
315 if( !p_track->location )
317 msg_Warn( p_demux, "ignoring track without Location entry" );
318 free_track( p_track );
319 return true;
322 msg_Info( p_demux, "Adding '%s'", p_track->location );
323 p_new_input = input_item_New( p_track->location, NULL );
324 input_item_node_AppendItem( p_input_node, p_new_input );
326 /* add meta info */
327 add_meta( p_new_input, p_track );
328 input_item_Release( p_new_input );
330 p_demux->p_sys = (void *)((uintptr_t)p_demux->p_sys + 1);
332 free_track( p_track );
333 return i_ret;
336 static track_elem_t *new_track()
338 track_elem_t *p_track = malloc( sizeof *p_track );
339 if( likely( p_track ) )
341 p_track->name = NULL;
342 p_track->artist = NULL;
343 p_track->album = NULL;
344 p_track->genre = NULL;
345 p_track->trackNum = NULL;
346 p_track->location = NULL;
347 p_track->duration = 0;
349 return p_track;
352 static void free_track( track_elem_t *p_track )
354 if ( !p_track )
355 return;
357 free( p_track->name );
358 free( p_track->artist );
359 free( p_track->album );
360 free( p_track->genre );
361 free( p_track->trackNum );
362 free( p_track->location );
363 free( p_track );
366 static bool save_data( track_elem_t *p_track, const char *psz_name,
367 char *psz_value, void *opaque )
369 VLC_UNUSED(opaque);
370 /* exit if setting is impossible */
371 if( !psz_name || !psz_value || !p_track )
372 return false;
374 /* re-convert xml special characters inside psz_value */
375 vlc_xml_decode( psz_value );
377 #define SAVE_INFO( name, value ) \
378 if( !strcmp( psz_name, name ) ) { p_track->value = strdup( psz_value ); }
380 SAVE_INFO( "Name", name )
381 else SAVE_INFO( "Artist", artist )
382 else SAVE_INFO( "Album", album )
383 else SAVE_INFO( "Genre", genre )
384 else SAVE_INFO( "Track Number", trackNum )
385 else SAVE_INFO( "Location", location )
386 else if( !strcmp( psz_name, "Total Time" ) )
388 long i_num = atol( psz_value );
389 p_track->duration = (mtime_t) i_num*1000;
391 #undef SAVE_INFO
392 return true;
396 * \brief handles the supported <track> sub-elements
398 static bool add_meta( input_item_t *p_input_item, track_elem_t *p_track )
400 /* exit if setting is impossible */
401 if( !p_input_item || !p_track )
402 return false;
404 #define SET_INFO( type, prop ) \
405 if( p_track->prop ) {input_item_Set##type( p_input_item, p_track->prop );}
406 SET_INFO( Title, name )
407 SET_INFO( Artist, artist )
408 SET_INFO( Album, album )
409 SET_INFO( Genre, genre )
410 SET_INFO( TrackNum, trackNum )
411 SET_INFO( Duration, duration )
412 #undef SET_INFO
413 return true;
417 * \brief skips complex element content that we can't manage
419 static bool skip_element( stream_t *p_demux, input_item_node_t *p_input_node,
420 track_elem_t *p_track, xml_reader_t *p_xml_reader,
421 const char *psz_element, xml_elem_hnd_t *p_handlers )
423 VLC_UNUSED(p_demux); VLC_UNUSED(p_input_node);
424 VLC_UNUSED(p_track); VLC_UNUSED(p_handlers);
425 const char *node;
426 int type;
428 while( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
429 if( type == XML_READER_ENDELEM && !strcmp( psz_element, node ) )
430 return true;
431 return false;