]> git.sesse.net Git - vlc/blob - modules/demux/playlist/xspf.c
input_item: compress two functions into one and rename "Add" into "Post" for clarity...
[vlc] / modules / demux / playlist / xspf.c
1 /*******************************************************************************
2  * xspf.c : XSPF playlist import functions
3  *******************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Daniel Stränger <vlc at schmaller dot de>
8  *          Yoann Peronneau <yoann@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  ******************************************************************************/
24 /**
25  * \file modules/demux/playlist/xspf.c
26  * \brief XSPF playlist import functions
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
35
36 #include <vlc_xml.h>
37 #include <vlc_strings.h>
38 #include <vlc_url.h>
39 #include "xspf.h"
40 #include "playlist.h"
41
42 struct demux_sys_t
43 {
44     input_item_t **pp_tracklist;
45     int i_tracklist_entries;
46     int i_track_id;
47     char * psz_base;
48 };
49
50 static int Control( demux_t *, int, va_list );
51 static int Demux( demux_t * );
52
53 /**
54  * \brief XSPF submodule initialization function
55  */
56 int Import_xspf( vlc_object_t *p_this )
57 {
58     DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xspf", "xspf-open",
59                                       "using XSPF playlist reader" );
60     return VLC_SUCCESS;
61 }
62
63 void Close_xspf( vlc_object_t *p_this )
64 {
65     demux_t *p_demux = (demux_t *)p_this;
66     demux_sys_t *p_sys = p_demux->p_sys;
67     for( int i = 0; i < p_sys->i_tracklist_entries; i++ )
68     {
69         if( p_sys->pp_tracklist[i] )
70             vlc_gc_decref( p_sys->pp_tracklist[i] );
71     }
72     free( p_sys->pp_tracklist );
73     free( p_sys->psz_base );
74     free( p_sys );
75 }
76
77 /**
78  * \brief demuxer function for XSPF parsing
79  */
80 int Demux( demux_t *p_demux )
81 {
82     int i_ret = -1;
83     xml_t *p_xml = NULL;
84     xml_reader_t *p_xml_reader = NULL;
85     char *psz_name = NULL;
86     input_item_t *p_current_input = GetCurrentItem(p_demux);
87     p_demux->p_sys->pp_tracklist = NULL;
88     p_demux->p_sys->i_tracklist_entries = 0;
89     p_demux->p_sys->i_track_id = -1;
90     p_demux->p_sys->psz_base = NULL;
91
92     /* create new xml parser from stream */
93     p_xml = xml_Create( p_demux );
94     if( !p_xml )
95         goto end;
96
97     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
98     if( !p_xml_reader )
99         goto end;
100
101     /* locating the root node */
102     do
103     {
104         if( xml_ReaderRead( p_xml_reader ) != 1 )
105         {
106             msg_Err( p_demux, "can't read xml stream" );
107             goto end;
108         }
109     } while( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
110
111     /* checking root node name */
112     psz_name = xml_ReaderName( p_xml_reader );
113     if( !psz_name || strcmp( psz_name, "playlist" ) )
114     {
115         msg_Err( p_demux, "invalid root node name: %s", psz_name );
116         free( psz_name );
117         goto end;
118     }
119     free( psz_name );
120
121     input_item_node_t *p_subitems =
122         input_item_node_Create( p_current_input );
123
124     i_ret = parse_playlist_node( p_demux, p_subitems,
125                                  p_xml_reader, "playlist" ) ? 0 : -1;
126
127     for( int i = 0 ; i < p_demux->p_sys->i_tracklist_entries ; i++ )
128     {
129         input_item_t *p_new_input = p_demux->p_sys->pp_tracklist[i];
130         if( p_new_input )
131         {
132             input_item_node_AppendItem( p_subitems, p_new_input );
133         }
134     }
135
136     input_item_node_PostAndDelete( p_subitems );
137
138 end:
139     vlc_gc_decref(p_current_input);
140     if( p_xml_reader )
141         xml_ReaderDelete( p_xml, p_xml_reader );
142     if( p_xml )
143         xml_Delete( p_xml );
144     return i_ret; /* Needed for correct operation of go back */
145 }
146
147 /** \brief dummy function for demux callback interface */
148 static int Control( demux_t *p_demux, int i_query, va_list args )
149 {
150     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
151     return VLC_EGENERIC;
152 }
153
154 /**
155  * \brief parse the root node of a XSPF playlist
156  * \param p_demux demuxer instance
157  * \param p_input_item current input item
158  * \param p_xml_reader xml reader instance
159  * \param psz_element name of element to parse
160  */
161 static bool parse_playlist_node COMPLEX_INTERFACE
162 {
163     input_item_t *p_input_item = p_input_node->p_item;
164     char *psz_name = NULL;
165     char *psz_value = NULL;
166     bool b_version_found = false;
167     int i_node;
168     bool b_ret = false;
169     xml_elem_hnd_t *p_handler = NULL;
170
171     xml_elem_hnd_t pl_elements[] =
172         { {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
173           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
174           {"annotation",   SIMPLE_CONTENT,  {.smpl = set_item_info} },
175           {"info",         SIMPLE_CONTENT,  {NULL} },
176           {"location",     SIMPLE_CONTENT,  {NULL} },
177           {"identifier",   SIMPLE_CONTENT,  {NULL} },
178           {"image",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
179           {"date",         SIMPLE_CONTENT,  {NULL} },
180           {"license",      SIMPLE_CONTENT,  {NULL} },
181           {"attribution",  COMPLEX_CONTENT, {.cmplx = skip_element} },
182           {"link",         SIMPLE_CONTENT,  {NULL} },
183           {"meta",         SIMPLE_CONTENT,  {NULL} },
184           {"extension",    COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
185           {"trackList",    COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
186           {NULL,           UNKNOWN_CONTENT, {NULL} }
187         };
188
189     /* read all playlist attributes */
190     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
191     {
192         psz_name = xml_ReaderName( p_xml_reader );
193         psz_value = xml_ReaderValue( p_xml_reader );
194         if( !psz_name || !psz_value )
195         {
196             msg_Err( p_demux, "invalid xml stream @ <playlist>" );
197             goto end;
198         }
199         /* attribute: version */
200         if( !strcmp( psz_name, "version" ) )
201         {
202             b_version_found = true;
203             if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
204                 msg_Warn( p_demux, "unsupported XSPF version" );
205         }
206         /* attribute: xmlns */
207         else if( !strcmp( psz_name, "xmlns" ) || !strcmp( psz_name, "xmlns:vlc" ) )
208             ;
209         else if( !strcmp( psz_name, "xml:base" ) )
210         {
211             p_demux->p_sys->psz_base = strdup( psz_value );
212         }
213         /* unknown attribute */
214         else
215             msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
216
217         free( psz_name );
218         free( psz_value );
219     }
220     /* attribute version is mandatory !!! */
221     if( !b_version_found )
222         msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
223
224     /* parse the child elements - we only take care of <trackList> */
225     psz_name = NULL;
226     psz_value = NULL;
227     while( xml_ReaderRead( p_xml_reader ) == 1 )
228     {
229         i_node = xml_ReaderNodeType( p_xml_reader );
230         switch( i_node )
231         {
232             case XML_READER_NONE:
233                 break;
234             case XML_READER_STARTELEM:
235                 /*  element start tag  */
236                 psz_name = xml_ReaderName( p_xml_reader );
237                 if( !psz_name || !*psz_name )
238                 {
239                     msg_Err( p_demux, "invalid xml stream" );
240                     goto end;
241                 }
242                 /* choose handler */
243                 for( p_handler = pl_elements;
244                      p_handler->name && strcmp( psz_name, p_handler->name );
245                      p_handler++ );
246                 if( !p_handler->name )
247                 {
248                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
249                     goto end;
250                 }
251                 FREE_NAME();
252                 /* complex content is parsed in a separate function */
253                 if( p_handler->type == COMPLEX_CONTENT )
254                 {
255                     FREE_VALUE();
256                     if( p_handler->pf_handler.cmplx( p_demux,
257                                                      p_input_node,
258                                                      p_xml_reader,
259                                                      p_handler->name ) )
260                     {
261                         p_handler = NULL;
262                     }
263                     else
264                     {
265                         return false;
266                     }
267                 }
268                 break;
269
270             case XML_READER_TEXT:
271                 /* simple element content */
272                 free( psz_value );
273                 psz_value = xml_ReaderValue( p_xml_reader );
274                 if( !psz_value )
275                 {
276                     msg_Err( p_demux, "invalid xml stream" );
277                     goto end;
278                 }
279                 break;
280
281             case XML_READER_ENDELEM:
282                 /* element end tag */
283                 psz_name = xml_ReaderName( p_xml_reader );
284                 if( !psz_name )
285                 {
286                     msg_Err( p_demux, "invalid xml stream" );
287                     goto end;
288                 }
289                 /* leave if the current parent node <playlist> is terminated */
290                 if( !strcmp( psz_name, psz_element ) )
291                 {
292                     b_ret = true;
293                     goto end;
294                 }
295                 /* there MUST have been a start tag for that element name */
296                 if( !p_handler || !p_handler->name
297                     || strcmp( p_handler->name, psz_name ))
298                 {
299                     msg_Err( p_demux, "there's no open element left for <%s>",
300                              psz_name );
301                     goto end;
302                 }
303
304                 if( p_handler->pf_handler.smpl )
305                 {
306                     p_handler->pf_handler.smpl( p_input_item, p_handler->name,
307                                                 psz_value );
308                 }
309                 FREE_ATT();
310                 p_handler = NULL;
311                 break;
312
313             default:
314                 /* unknown/unexpected xml node */
315                 msg_Err( p_demux, "unexpected xml node %i", i_node );
316                 goto end;
317         }
318     }
319
320 end:
321     free( psz_name );
322     free( psz_value );
323     return b_ret;
324 }
325
326 /**
327  * \brief parses the tracklist node which only may contain <track>s
328  */
329 static bool parse_tracklist_node COMPLEX_INTERFACE
330 {
331     VLC_UNUSED(psz_element);
332     char *psz_name;
333     int i_ntracks = 0;
334
335     /* now parse the <track>s */
336     while( xml_ReaderRead( p_xml_reader ) == 1 )
337     {
338         int i_node = xml_ReaderNodeType( p_xml_reader );
339         if( i_node == XML_READER_STARTELEM )
340         {
341             char *psz_eltname = xml_ReaderName( p_xml_reader );
342             if( !psz_eltname )
343             {
344                 msg_Err( p_demux, "unexpected end of xml data" );
345                 free( psz_eltname );
346                 return false;
347             }
348             if( strcmp( psz_eltname, "track") )
349             {
350                 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
351                          psz_eltname );
352                 free( psz_eltname );
353                 return false;
354             }
355             free( psz_eltname );
356
357             /* parse the track data in a separate function */
358             if( parse_track_node( p_demux, p_input_node,
359                                   p_xml_reader, "track" ) )
360                 i_ntracks++;
361         }
362         else if( i_node == XML_READER_ENDELEM )
363             break;
364     }
365
366     /* the <trackList> has to be terminated */
367     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
368     {
369         msg_Err( p_demux, "there's a missing </trackList>" );
370         return false;
371     }
372     psz_name = xml_ReaderName( p_xml_reader );
373     if( !psz_name || strcmp( psz_name, "trackList" ) )
374     {
375         msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
376         free( psz_name );
377         return false;
378     }
379     free( psz_name );
380
381     msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
382     return true;
383 }
384
385 /**
386  * \brief parse one track element
387  * \param COMPLEX_INTERFACE
388  */
389 static bool parse_track_node COMPLEX_INTERFACE
390 {
391     input_item_t *p_input_item = p_input_node->p_item;
392     char *psz_name = NULL;
393     char *psz_value = NULL;
394     xml_elem_hnd_t *p_handler = NULL;
395     demux_sys_t *p_sys = p_demux->p_sys;
396     bool b_ret = false;
397
398     xml_elem_hnd_t track_elements[] =
399         { {"location",     SIMPLE_CONTENT,  {NULL} },
400           {"identifier",   SIMPLE_CONTENT,  {NULL} },
401           {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
402           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
403           {"annotation",   SIMPLE_CONTENT,  {.smpl = set_item_info} },
404           {"info",         SIMPLE_CONTENT,  {NULL} },
405           {"image",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
406           {"album",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
407           {"trackNum",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
408           {"duration",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
409           {"link",         SIMPLE_CONTENT,  {NULL} },
410           {"meta",         SIMPLE_CONTENT,  {NULL} },
411           {"extension",    COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
412           {NULL,           UNKNOWN_CONTENT, {NULL} }
413         };
414
415     input_item_t *p_new_input = input_item_New( p_demux, NULL, NULL );
416     input_item_node_t *p_new_node = input_item_node_Create( p_new_input );
417
418     if( !p_new_input )
419     {
420         /* malloc has failed for input_item_New, so bailout early */
421         return false;
422     }
423
424     /* reset i_track_id */
425     p_sys->i_track_id = -1;
426
427     while( xml_ReaderRead( p_xml_reader ) == 1 )
428     {
429         int i_node = xml_ReaderNodeType( p_xml_reader );
430         switch( i_node )
431         {
432             case XML_READER_NONE:
433                 break;
434
435             case XML_READER_STARTELEM:
436                 /*  element start tag  */
437                 psz_name = xml_ReaderName( p_xml_reader );
438                 if( !psz_name || !*psz_name )
439                 {
440                     msg_Err( p_demux, "invalid xml stream" );
441                     goto end;
442                 }
443                 /* choose handler */
444                 for( p_handler = track_elements;
445                      p_handler->name && strcmp( psz_name, p_handler->name );
446                      p_handler++ );
447                 if( !p_handler->name )
448                 {
449                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
450                     goto end;
451                 }
452                 FREE_NAME();
453                 /* complex content is parsed in a separate function */
454                 if( p_handler->type == COMPLEX_CONTENT )
455                 {
456                     FREE_VALUE();
457
458                     bool b_res = p_handler->pf_handler.cmplx( p_demux,
459                                                         p_new_node,
460                                                         p_xml_reader,
461                                                         p_handler->name );
462                     if( b_res )
463                     {
464                         p_handler = NULL;
465                     }
466                     else
467                     {
468                         return false;
469                     }
470                 }
471                 break;
472
473             case XML_READER_TEXT:
474                 /* simple element content */
475                 free( psz_value );
476                 psz_value = xml_ReaderValue( p_xml_reader );
477                 if( !psz_value )
478                 {
479                     msg_Err( p_demux, "invalid xml stream" );
480                     goto end;
481                 }
482                 break;
483
484             case XML_READER_ENDELEM:
485                 /* element end tag */
486                 psz_name = xml_ReaderName( p_xml_reader );
487                 if( !psz_name )
488                 {
489                     msg_Err( p_demux, "invalid xml stream" );
490                     goto end;
491                 }
492
493                 /* leave if the current parent node <track> is terminated */
494                 if( !strcmp( psz_name, psz_element ) )
495                 {
496                     free( psz_name );
497                     free( psz_value );
498
499                     /* Make sure we have a URI */
500                     char *psz_uri = input_item_GetURI( p_new_input );
501                     if( !psz_uri )
502                     {
503                         input_item_SetURI( p_new_input, "vlc://nop" );
504                     }
505                     free( psz_uri );
506
507                     if( p_sys->i_track_id < 0 )
508                     {
509                         input_item_node_AppendNode( p_input_node, p_new_node );
510                         vlc_gc_decref( p_new_input );
511                         return true;
512                     }
513
514                     if( p_sys->i_track_id >= p_sys->i_tracklist_entries )
515                     {
516                         input_item_t **pp;
517                         pp = realloc( p_sys->pp_tracklist,
518                             (p_sys->i_track_id + 1) * sizeof(*pp) );
519                         if( !pp )
520                             return false;
521                         p_sys->pp_tracklist = pp;
522                         while( p_sys->i_track_id >= p_sys->i_tracklist_entries )
523                             pp[p_sys->i_tracklist_entries++] = NULL;
524                     }
525
526                     p_sys->pp_tracklist[ p_sys->i_track_id ] = p_new_input;
527                     return true;
528                 }
529                 /* there MUST have been a start tag for that element name */
530                 if( !p_handler || !p_handler->name
531                     || strcmp( p_handler->name, psz_name ))
532                 {
533                     msg_Err( p_demux, "there's no open element left for <%s>",
534                              psz_name );
535                     goto end;
536                 }
537
538                 /* special case: location */
539                 if( !strcmp( p_handler->name, "location" ) )
540                 {
541                     /* FIXME: This is broken. Scheme-relative (//...) locations
542                      * and anchors (#...) are not resolved correctly. Also,
543                      * host-relative (/...) and directory-relative locations
544                      * ("relative path" in vernacular) should be resolved.
545                      * Last, psz_base should default to the XSPF resource
546                      * location if missing (not the current working directory).
547                      * -- Courmisch */
548                     if( p_sys->psz_base && !strstr( psz_value, "://" ) )
549                     {
550                         char* psz_tmp;
551                         if( asprintf( &psz_tmp, "%s%s", p_sys->psz_base,
552                                       psz_value ) == -1 )
553                         {
554                             goto end;
555                         }
556                         input_item_SetURI( p_new_input, psz_tmp );
557                         free( psz_tmp );
558                     }
559                     else
560                         input_item_SetURI( p_new_input, psz_value );
561                     input_item_CopyOptions( p_input_item, p_new_input );
562                 }
563                 else
564                 {
565                     /* there MUST be an item */
566                     if( p_handler->pf_handler.smpl )
567                     {
568                         p_handler->pf_handler.smpl( p_new_input,
569                                                     p_handler->name,
570                                                     psz_value );
571                         FREE_ATT();
572                     }
573                 }
574                 FREE_ATT();
575                 p_handler = NULL;
576                 break;
577
578             default:
579                 /* unknown/unexpected xml node */
580                 msg_Err( p_demux, "unexpected xml node %i", i_node );
581                 goto end;
582         }
583     }
584     msg_Err( p_demux, "unexpected end of xml data" );
585
586 end:
587     free( psz_name );
588     free( psz_value );
589     return b_ret;
590 }
591
592 /**
593  * \brief handles the supported <track> sub-elements
594  */
595 static bool set_item_info SIMPLE_INTERFACE
596 {
597     /* exit if setting is impossible */
598     if( !psz_name || !psz_value || !p_input )
599         return false;
600
601     /* re-convert xml special characters inside psz_value */
602     resolve_xml_special_chars( psz_value );
603
604     /* handle each info element in a separate "if" clause */
605     if( !strcmp( psz_name, "title" ) )
606     {
607         input_item_SetTitle( p_input, psz_value );
608     }
609     else if( !strcmp( psz_name, "creator" ) )
610     {
611         input_item_SetArtist( p_input, psz_value );
612     }
613     else if( !strcmp( psz_name, "album" ) )
614     {
615         input_item_SetAlbum( p_input, psz_value );
616
617     }
618     else if( !strcmp( psz_name, "trackNum" ) )
619     {
620         input_item_SetTrackNum( p_input, psz_value );
621     }
622     else if( !strcmp( psz_name, "duration" ) )
623     {
624         long i_num = atol( psz_value );
625         input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
626     }
627     else if( !strcmp( psz_name, "annotation" ) )
628     {
629         input_item_SetDescription( p_input, psz_value );
630     }
631     else if( !strcmp( psz_name, "image" ) )
632     {
633         input_item_SetArtURL( p_input, psz_value );
634     }
635     return true;
636 }
637
638 /**
639  * \brief handles the <vlc:option> elements
640  */
641 static bool set_option SIMPLE_INTERFACE
642 {
643     /* exit if setting is impossible */
644     if( !psz_name || !psz_value || !p_input )
645         return false;
646
647     /* re-convert xml special characters inside psz_value */
648     resolve_xml_special_chars( psz_value );
649
650     input_item_AddOption( p_input, psz_value, 0 );
651
652     return true;
653 }
654
655 /**
656  * \brief parse the extension node of a XSPF playlist
657  */
658 static bool parse_extension_node COMPLEX_INTERFACE
659 {
660     input_item_t *p_input_item = p_input_node->p_item;
661     char *psz_name = NULL;
662     char *psz_value = NULL;
663     char *psz_title = NULL;
664     char *psz_application = NULL;
665     int i_node;
666     bool b_release_input_item = false;
667     xml_elem_hnd_t *p_handler = NULL;
668     input_item_t *p_new_input = NULL;
669
670     xml_elem_hnd_t pl_elements[] =
671         { {"vlc:node",   COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
672           {"vlc:item",   COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
673           {"vlc:id",     SIMPLE_CONTENT, {NULL} },
674           {"vlc:option", SIMPLE_CONTENT, {.smpl = set_option} },
675           {NULL,    UNKNOWN_CONTENT, {NULL} }
676         };
677
678     /* read all extension node attributes */
679     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
680     {
681         psz_name = xml_ReaderName( p_xml_reader );
682         psz_value = xml_ReaderValue( p_xml_reader );
683         if( !psz_name || !psz_value )
684         {
685             msg_Err( p_demux, "invalid xml stream @ <vlc:node>" );
686             FREE_ATT();
687             return false;
688         }
689         /* attribute: title */
690         if( !strcmp( psz_name, "title" ) )
691         {
692             resolve_xml_special_chars( psz_value );
693             psz_title = psz_value;
694         }
695         /* extension attribute: application */
696         else if( !strcmp( psz_name, "application" ) )
697         {
698             psz_application = psz_value;
699         }
700         /* unknown attribute */
701         else
702         {
703             msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element,
704                       psz_name );
705             FREE_VALUE();
706         }
707         FREE_NAME();
708         psz_value = NULL;
709     }
710
711     /* attribute title is mandatory except for <extension> */
712     if( !strcmp( psz_element, "vlc:node" ) )
713     {
714         if( !psz_title )
715         {
716             msg_Warn( p_demux, "<vlc:node> requires \"title\" attribute" );
717             return false;
718         }
719         p_new_input = input_item_NewWithType( VLC_OBJECT( p_demux ),
720                           "vlc://nop", psz_title, 0, NULL, 0, -1,
721                           ITEM_TYPE_DIRECTORY );
722         if( p_new_input )
723         {
724             p_input_node =
725                 input_item_node_AppendItem( p_input_node, p_new_input );
726             p_input_item = p_new_input;
727             b_release_input_item = true;
728         }
729         free( psz_title );
730     }
731     else if( !strcmp( psz_element, "extension" ) )
732     {
733         if( !psz_application )
734         {
735             msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
736             return false;
737         }
738         else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
739         {
740             msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
741             free( psz_application );
742             return false;
743         }
744     }
745     free( psz_application );
746
747
748     /* parse the child elements */
749     while( xml_ReaderRead( p_xml_reader ) == 1 )
750     {
751         i_node = xml_ReaderNodeType( p_xml_reader );
752         switch( i_node )
753         {
754             case XML_READER_NONE:
755                 break;
756             case XML_READER_STARTELEM:
757                 /*  element start tag  */
758                 psz_name = xml_ReaderName( p_xml_reader );
759                 if( !psz_name || !*psz_name )
760                 {
761                     msg_Err( p_demux, "invalid xml stream" );
762                     FREE_ATT();
763                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
764                     return false;
765                 }
766                 /* choose handler */
767                 for( p_handler = pl_elements;
768                      p_handler->name && strcmp( psz_name, p_handler->name );
769                      p_handler++ );
770                 if( !p_handler->name )
771                 {
772                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
773                     FREE_ATT();
774                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
775                     return false;
776                 }
777                 FREE_NAME();
778                 /* complex content is parsed in a separate function */
779                 if( p_handler->type == COMPLEX_CONTENT )
780                 {
781                     if( p_handler->pf_handler.cmplx( p_demux,
782                                                      p_input_node,
783                                                      p_xml_reader,
784                                                      p_handler->name ) )
785                     {
786                         p_handler = NULL;
787                         FREE_ATT();
788                     }
789                     else
790                     {
791                         FREE_ATT();
792                         if( b_release_input_item ) vlc_gc_decref( p_new_input );
793                         return false;
794                     }
795                 }
796                 break;
797
798             case XML_READER_TEXT:
799                 /* simple element content */
800                 FREE_ATT();
801                 psz_value = xml_ReaderValue( p_xml_reader );
802                 if( !psz_value )
803                 {
804                     msg_Err( p_demux, "invalid xml stream" );
805                     FREE_ATT();
806                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
807                     return false;
808                 }
809                 break;
810
811             case XML_READER_ENDELEM:
812                 /* element end tag */
813                 psz_name = xml_ReaderName( p_xml_reader );
814                 if( !psz_name )
815                 {
816                     msg_Err( p_demux, "invalid xml stream" );
817                     FREE_ATT();
818                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
819                     return false;
820                 }
821                 /* leave if the current parent node is terminated */
822                 if( !strcmp( psz_name, psz_element ) )
823                 {
824                     FREE_ATT();
825                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
826                     return true;
827                 }
828                 /* there MUST have been a start tag for that element name */
829                 if( !p_handler || !p_handler->name
830                     || strcmp( p_handler->name, psz_name ))
831                 {
832                     msg_Err( p_demux, "there's no open element left for <%s>",
833                              psz_name );
834                     FREE_ATT();
835                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
836                     return false;
837                 }
838
839                 /* special tag <vlc:id> */
840                 if( !strcmp( p_handler->name, "vlc:id" ) )
841                 {
842                     p_demux->p_sys->i_track_id = atoi( psz_value );
843                 }
844                 else if( p_handler->pf_handler.smpl )
845                 {
846                     p_handler->pf_handler.smpl( p_input_item, p_handler->name,
847                                                 psz_value );
848                 }
849                 FREE_ATT();
850                 p_handler = NULL;
851                 break;
852
853             default:
854                 /* unknown/unexpected xml node */
855                 msg_Err( p_demux, "unexpected xml node %i", i_node );
856                 FREE_ATT();
857                 if( b_release_input_item ) vlc_gc_decref( p_new_input );
858                 return false;
859         }
860         FREE_NAME();
861     }
862     if( b_release_input_item ) vlc_gc_decref( p_new_input );
863     return false;
864 }
865
866 /**
867  * \brief parse the extension item node of a XSPF playlist
868  */
869 static bool parse_extitem_node COMPLEX_INTERFACE
870 {
871     VLC_UNUSED(psz_element);
872     input_item_t *p_new_input = NULL;
873     int i_tid = -1;
874
875     /* read all extension item attributes */
876     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
877     {
878         char *psz_name = xml_ReaderName( p_xml_reader );
879         char *psz_value = xml_ReaderValue( p_xml_reader );
880         if( !psz_name || !psz_value )
881         {
882             msg_Err( p_demux, "invalid xml stream @ <vlc:item>" );
883             free( psz_name );
884             free( psz_value );
885             return false;
886         }
887         /* attribute: href */
888         if( !strcmp( psz_name, "tid" ) )
889         {
890             i_tid = atoi( psz_value );
891         }
892         /* unknown attribute */
893         else
894             msg_Warn( p_demux, "invalid <vlc:item> attribute:\"%s\"", psz_name);
895
896         free( psz_name );
897         free( psz_value );
898     }
899
900     /* attribute href is mandatory */
901     if( i_tid < 0 )
902     {
903         msg_Warn( p_demux, "<vlc:item> requires \"tid\" attribute" );
904         return false;
905     }
906
907     if( i_tid >= p_demux->p_sys->i_tracklist_entries )
908     {
909         msg_Warn( p_demux, "invalid \"tid\" attribute" );
910         return false;
911     }
912
913     p_new_input = p_demux->p_sys->pp_tracklist[ i_tid ];
914     if( p_new_input )
915     {
916         input_item_node_AppendItem( p_input_node, p_new_input );
917         vlc_gc_decref( p_new_input );
918         p_demux->p_sys->pp_tracklist[i_tid] = NULL;
919     }
920
921     /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
922     /* (libxml sends NONE) */
923     xml_ReaderRead( p_xml_reader );
924
925     return true;
926 }
927
928 /**
929  * \brief skips complex element content that we can't manage
930  */
931 static bool skip_element COMPLEX_INTERFACE
932 {
933     VLC_UNUSED(p_demux); VLC_UNUSED(p_input_node);
934
935     while( xml_ReaderRead( p_xml_reader ) == 1 )
936     {
937         if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
938         {
939             char *psz_endname = xml_ReaderName( p_xml_reader );
940             if( !psz_endname )
941                 return false;
942             if( !strcmp( psz_element, psz_endname ) )
943             {
944                 free( psz_endname );
945                 return true;
946             }
947             else
948                 free( psz_endname );
949         }
950     }
951     return false;
952 }