demux: es: fix swab usage
[vlc.git] / modules / demux / playlist / itml.c
blob4993327244467613c2d5e161d7362786e3f5cae4
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 = calloc( 1, sizeof (size_t) );
77 if( unlikely(p_demux->p_sys == NULL) )
78 return 0;
80 /* create new xml parser from stream */
81 p_xml_reader = xml_ReaderCreate( p_demux, p_demux->s );
82 if( !p_xml_reader )
83 goto end;
85 /* locating the root node */
86 int type;
89 type = xml_ReaderNextNode( p_xml_reader, &node );
90 if( type <= 0 )
92 msg_Err( p_demux, "can't read xml stream" );
93 goto end;
96 while( type != XML_READER_STARTELEM );
98 /* checking root node name */
99 if( strcmp( node, "plist" ) )
101 msg_Err( p_demux, "invalid root node <%s>", node );
102 goto end;
105 xml_elem_hnd_t pl_elements[] =
107 {"dict", COMPLEX_CONTENT, {.cmplx = parse_plist_dict} },
108 {NULL, UNKNOWN_CONTENT, {NULL} }
110 parse_plist_node( p_demux, p_subitems, NULL, p_xml_reader, "plist",
111 pl_elements );
113 end:
114 if( p_xml_reader )
115 xml_ReaderDelete( p_xml_reader );
116 free( p_demux->p_sys );
118 /* Needed for correct operation of go back */
119 return 0;
123 * \brief parse the root node of the playlist
125 static bool parse_plist_node( stream_t *p_demux, input_item_node_t *p_input_node,
126 track_elem_t *p_track, xml_reader_t *p_xml_reader,
127 const char *psz_element,
128 xml_elem_hnd_t *p_handlers )
130 VLC_UNUSED(p_track); VLC_UNUSED(psz_element);
131 const char *attr, *value;
132 bool b_version_found = false;
134 /* read all playlist attributes */
135 while( (attr = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
137 /* attribute: version */
138 if( !strcmp( attr, "version" ) )
140 b_version_found = true;
141 if( strcmp( value, "1.0" ) )
142 msg_Warn( p_demux, "unsupported iTunes Media Library version" );
144 /* unknown attribute */
145 else
146 msg_Warn( p_demux, "invalid <plist> attribute:\"%s\"", attr );
149 /* attribute version is mandatory !!! */
150 if( !b_version_found )
151 msg_Warn( p_demux, "<plist> requires \"version\" attribute" );
153 return parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
154 "plist", p_handlers );
158 * \brief parse a <dict>
159 * \param COMPLEX_INTERFACE
161 static bool parse_dict( stream_t *p_demux, input_item_node_t *p_input_node,
162 track_elem_t *p_track, xml_reader_t *p_xml_reader,
163 const char *psz_element, xml_elem_hnd_t *p_handlers )
165 int i_node;
166 const char *node;
167 char *psz_value = NULL;
168 char *psz_key = NULL;
169 xml_elem_hnd_t *p_handler = NULL;
170 bool b_ret = false;
172 while( (i_node = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
174 switch( i_node )
176 /* element start tag */
177 case XML_READER_STARTELEM:
178 /* choose handler */
179 for( p_handler = p_handlers;
180 p_handler->name && strcmp( node, p_handler->name );
181 p_handler++ );
182 if( !p_handler->name )
184 msg_Err( p_demux, "unexpected element <%s>", node );
185 goto end;
187 /* complex content is parsed in a separate function */
188 if( p_handler->type == COMPLEX_CONTENT )
190 if( p_handler->pf_handler.cmplx( p_demux, p_input_node, NULL,
191 p_xml_reader, p_handler->name,
192 NULL ) )
194 p_handler = NULL;
195 FREENULL( psz_key );
196 FREENULL( psz_value );
198 else
199 goto end;
201 break;
203 /* simple element content */
204 case XML_READER_TEXT:
205 free( psz_value );
206 psz_value = strdup( node );
207 if( unlikely(!psz_value) )
208 goto end;
209 break;
211 /* element end tag */
212 case XML_READER_ENDELEM:
213 /* leave if the current parent node <track> is terminated */
214 if( !strcmp( node, psz_element ) )
216 b_ret = true;
217 goto end;
219 /* there MUST have been a start tag for that element name */
220 if( !p_handler || !p_handler->name
221 || strcmp( p_handler->name, node ) )
223 msg_Err( p_demux, "there's no open element left for <%s>",
224 node );
225 goto end;
227 /* special case: key */
228 if( !strcmp( p_handler->name, "key" ) )
230 free( psz_key );
231 psz_key = strdup( psz_value );
233 /* call the simple handler */
234 else if( p_handler->pf_handler.smpl )
236 p_handler->pf_handler.smpl( p_track, psz_key, psz_value );
238 FREENULL(psz_value);
239 p_handler = NULL;
240 break;
243 msg_Err( p_demux, "unexpected end of XML data" );
245 end:
246 free( psz_value );
247 free( psz_key );
248 return b_ret;
251 static bool parse_plist_dict( stream_t *p_demux, input_item_node_t *p_input_node,
252 track_elem_t *p_track, xml_reader_t *p_xml_reader,
253 const char *psz_element,
254 xml_elem_hnd_t *p_handlers )
256 VLC_UNUSED(p_track); VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
257 xml_elem_hnd_t pl_elements[] =
258 { {"dict", COMPLEX_CONTENT, {.cmplx = parse_tracks_dict} },
259 {"array", SIMPLE_CONTENT, {NULL} },
260 {"key", SIMPLE_CONTENT, {NULL} },
261 {"integer", SIMPLE_CONTENT, {NULL} },
262 {"string", SIMPLE_CONTENT, {NULL} },
263 {"date", SIMPLE_CONTENT, {NULL} },
264 {"true", SIMPLE_CONTENT, {NULL} },
265 {"false", SIMPLE_CONTENT, {NULL} },
266 {NULL, UNKNOWN_CONTENT, {NULL} }
269 return parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
270 "dict", pl_elements );
273 static bool parse_tracks_dict( stream_t *p_demux, input_item_node_t *p_input_node,
274 track_elem_t *p_track, xml_reader_t *p_xml_reader,
275 const char *psz_element,
276 xml_elem_hnd_t *p_handlers )
278 VLC_UNUSED(p_track); VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
279 xml_elem_hnd_t tracks_elements[] =
280 { {"dict", COMPLEX_CONTENT, {.cmplx = parse_track_dict} },
281 {"key", SIMPLE_CONTENT, {NULL} },
282 {NULL, UNKNOWN_CONTENT, {NULL} }
285 parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
286 "dict", tracks_elements );
288 msg_Info( p_demux, "added %zu tracks successfully",
289 *(size_t *)p_demux->p_sys );
291 return true;
294 static bool parse_track_dict( stream_t *p_demux, input_item_node_t *p_input_node,
295 track_elem_t *p_track, xml_reader_t *p_xml_reader,
296 const char *psz_element,
297 xml_elem_hnd_t *p_handlers )
299 VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
300 input_item_t *p_new_input = NULL;
301 int i_ret;
302 p_track = new_track();
304 xml_elem_hnd_t track_elements[] =
305 { {"array", COMPLEX_CONTENT, {.cmplx = skip_element} },
306 {"key", SIMPLE_CONTENT, {.smpl = save_data} },
307 {"integer", SIMPLE_CONTENT, {.smpl = save_data} },
308 {"string", SIMPLE_CONTENT, {.smpl = save_data} },
309 {"date", SIMPLE_CONTENT, {.smpl = save_data} },
310 {"true", SIMPLE_CONTENT, {NULL} },
311 {"false", SIMPLE_CONTENT, {NULL} },
312 {NULL, UNKNOWN_CONTENT, {NULL} }
315 i_ret = parse_dict( p_demux, p_input_node, p_track,
316 p_xml_reader, "dict", track_elements );
318 msg_Dbg( p_demux, "name: %s, artist: %s, album: %s, genre: %s, trackNum: %s, location: %s",
319 p_track->name, p_track->artist, p_track->album, p_track->genre, p_track->trackNum, p_track->location );
321 if( !p_track->location )
323 msg_Warn( p_demux, "ignoring track without Location entry" );
324 free_track( p_track );
325 return true;
328 msg_Info( p_demux, "Adding '%s'", p_track->location );
329 p_new_input = input_item_New( p_track->location, NULL );
330 input_item_node_AppendItem( p_input_node, p_new_input );
332 /* add meta info */
333 add_meta( p_new_input, p_track );
334 input_item_Release( p_new_input );
336 (*(size_t *)p_demux->p_sys)++;
338 free_track( p_track );
339 return i_ret;
342 static track_elem_t *new_track()
344 track_elem_t *p_track = malloc( sizeof *p_track );
345 if( likely( p_track ) )
347 p_track->name = NULL;
348 p_track->artist = NULL;
349 p_track->album = NULL;
350 p_track->genre = NULL;
351 p_track->trackNum = NULL;
352 p_track->location = NULL;
353 p_track->duration = 0;
355 return p_track;
358 static void free_track( track_elem_t *p_track )
360 if ( !p_track )
361 return;
363 free( p_track->name );
364 free( p_track->artist );
365 free( p_track->album );
366 free( p_track->genre );
367 free( p_track->trackNum );
368 free( p_track->location );
369 free( p_track );
372 static bool save_data( track_elem_t *p_track, const char *psz_name,
373 char *psz_value )
375 /* exit if setting is impossible */
376 if( !psz_name || !psz_value || !p_track )
377 return false;
379 /* re-convert xml special characters inside psz_value */
380 vlc_xml_decode( psz_value );
382 #define SAVE_INFO( name, value ) \
383 if( !strcmp( psz_name, name ) ) { p_track->value = strdup( psz_value ); }
385 SAVE_INFO( "Name", name )
386 else SAVE_INFO( "Artist", artist )
387 else SAVE_INFO( "Album", album )
388 else SAVE_INFO( "Genre", genre )
389 else SAVE_INFO( "Track Number", trackNum )
390 else SAVE_INFO( "Location", location )
391 else if( !strcmp( psz_name, "Total Time" ) )
393 long i_num = atol( psz_value );
394 p_track->duration = (vlc_tick_t) i_num*1000;
396 #undef SAVE_INFO
397 return true;
401 * \brief handles the supported <track> sub-elements
403 static bool add_meta( input_item_t *p_input_item, track_elem_t *p_track )
405 /* exit if setting is impossible */
406 if( !p_input_item || !p_track )
407 return false;
409 #define SET_INFO( type, prop ) \
410 if( p_track->prop ) {input_item_Set##type( p_input_item, p_track->prop );}
411 SET_INFO( Title, name )
412 SET_INFO( Artist, artist )
413 SET_INFO( Album, album )
414 SET_INFO( Genre, genre )
415 SET_INFO( TrackNum, trackNum )
416 SET_INFO( Duration, duration )
417 #undef SET_INFO
418 return true;
422 * \brief skips complex element content that we can't manage
424 static bool skip_element( stream_t *p_demux, input_item_node_t *p_input_node,
425 track_elem_t *p_track, xml_reader_t *p_xml_reader,
426 const char *psz_element, xml_elem_hnd_t *p_handlers )
428 VLC_UNUSED(p_demux); VLC_UNUSED(p_input_node);
429 VLC_UNUSED(p_track); VLC_UNUSED(p_handlers);
430 const char *node;
431 int type;
433 while( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
434 if( type == XML_READER_ENDELEM && !strcmp( psz_element, node ) )
435 return true;
436 return false;