1 /*******************************************************************************
2 * xspf.c : XSPF playlist import functions
3 *******************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Daniel Stränger <vlc at schmaller dot de>
8 * Yoann Peronneau <yoann@videolan.org>
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.
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.
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 ******************************************************************************/
25 * \file modules/demux/playlist/xspf.c
26 * \brief XSPF playlist import functions
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
37 #include <vlc_strings.h>
44 input_item_t **pp_tracklist;
45 int i_tracklist_entries;
50 static int Control( demux_t *, int, va_list );
51 static int Demux( demux_t * );
54 * \brief XSPF submodule initialization function
56 int Import_xspf( vlc_object_t *p_this )
58 DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xspf", "xspf-open",
59 "using XSPF playlist reader" );
63 void Close_xspf( vlc_object_t *p_this )
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++ )
69 if( p_sys->pp_tracklist[i] )
70 vlc_gc_decref( p_sys->pp_tracklist[i] );
72 free( p_sys->pp_tracklist );
73 free( p_sys->psz_base );
78 * \brief demuxer function for XSPF parsing
80 int Demux( demux_t *p_demux )
83 xml_reader_t *p_xml_reader = NULL;
84 char *psz_name = NULL;
85 input_item_t *p_current_input = GetCurrentItem(p_demux);
86 p_demux->p_sys->pp_tracklist = NULL;
87 p_demux->p_sys->i_tracklist_entries = 0;
88 p_demux->p_sys->i_track_id = -1;
89 p_demux->p_sys->psz_base = NULL;
91 /* create new xml parser from stream */
92 p_xml_reader = xml_ReaderCreate( p_demux, p_demux->s );
96 /* locating the root node */
97 if( xml_ReaderNextNode( p_xml_reader ) != XML_READER_STARTELEM )
99 msg_Err( p_demux, "can't read xml stream" );
103 /* checking root node name */
104 psz_name = xml_ReaderName( p_xml_reader );
105 if( !psz_name || strcmp( psz_name, "playlist" ) )
107 msg_Err( p_demux, "invalid root node name: %s", psz_name );
113 input_item_node_t *p_subitems =
114 input_item_node_Create( p_current_input );
116 i_ret = parse_playlist_node( p_demux, p_subitems,
117 p_xml_reader, "playlist" ) ? 0 : -1;
119 for( int i = 0 ; i < p_demux->p_sys->i_tracklist_entries ; i++ )
121 input_item_t *p_new_input = p_demux->p_sys->pp_tracklist[i];
124 input_item_node_AppendItem( p_subitems, p_new_input );
128 input_item_node_PostAndDelete( p_subitems );
131 vlc_gc_decref(p_current_input);
133 xml_ReaderDelete( p_xml_reader );
134 return i_ret; /* Needed for correct operation of go back */
137 /** \brief dummy function for demux callback interface */
138 static int Control( demux_t *p_demux, int i_query, va_list args )
140 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
145 * \brief parse the root node of a XSPF playlist
146 * \param p_demux demuxer instance
147 * \param p_input_item current input item
148 * \param p_xml_reader xml reader instance
149 * \param psz_element name of element to parse
151 static bool parse_playlist_node COMPLEX_INTERFACE
153 input_item_t *p_input_item = p_input_node->p_item;
154 char *psz_name = NULL;
155 char *psz_value = NULL;
156 bool b_version_found = false;
159 xml_elem_hnd_t *p_handler = NULL;
161 xml_elem_hnd_t pl_elements[] =
162 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
163 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
164 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
165 {"info", SIMPLE_CONTENT, {NULL} },
166 {"location", SIMPLE_CONTENT, {NULL} },
167 {"identifier", SIMPLE_CONTENT, {NULL} },
168 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
169 {"date", SIMPLE_CONTENT, {NULL} },
170 {"license", SIMPLE_CONTENT, {NULL} },
171 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
172 {"link", SIMPLE_CONTENT, {NULL} },
173 {"meta", SIMPLE_CONTENT, {NULL} },
174 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
175 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
176 {NULL, UNKNOWN_CONTENT, {NULL} }
179 /* read all playlist attributes */
180 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
182 psz_name = xml_ReaderName( p_xml_reader );
183 psz_value = xml_ReaderValue( p_xml_reader );
184 if( !psz_name || !psz_value )
186 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
189 /* attribute: version */
190 if( !strcmp( psz_name, "version" ) )
192 b_version_found = true;
193 if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
194 msg_Warn( p_demux, "unsupported XSPF version" );
196 /* attribute: xmlns */
197 else if( !strcmp( psz_name, "xmlns" ) || !strcmp( psz_name, "xmlns:vlc" ) )
199 else if( !strcmp( psz_name, "xml:base" ) )
201 p_demux->p_sys->psz_base = strdup( psz_value );
203 /* unknown attribute */
205 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
210 /* attribute version is mandatory !!! */
211 if( !b_version_found )
212 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
214 /* parse the child elements - we only take care of <trackList> */
217 while( (i_node = xml_ReaderNextNode( p_xml_reader )) > 0 )
221 case XML_READER_STARTELEM:
222 /* element start tag */
223 psz_name = xml_ReaderName( p_xml_reader );
224 if( !psz_name || !*psz_name )
226 msg_Err( p_demux, "invalid xml stream" );
230 for( p_handler = pl_elements;
231 p_handler->name && strcmp( psz_name, p_handler->name );
233 if( !p_handler->name )
235 msg_Err( p_demux, "unexpected element <%s>", psz_name );
239 /* complex content is parsed in a separate function */
240 if( p_handler->type == COMPLEX_CONTENT )
243 if( p_handler->pf_handler.cmplx( p_demux,
257 case XML_READER_TEXT:
258 /* simple element content */
260 psz_value = xml_ReaderValue( p_xml_reader );
263 msg_Err( p_demux, "invalid xml stream" );
268 case XML_READER_ENDELEM:
269 /* element end tag */
270 psz_name = xml_ReaderName( p_xml_reader );
273 msg_Err( p_demux, "invalid xml stream" );
276 /* leave if the current parent node <playlist> is terminated */
277 if( !strcmp( psz_name, psz_element ) )
282 /* there MUST have been a start tag for that element name */
283 if( !p_handler || !p_handler->name
284 || strcmp( p_handler->name, psz_name ))
286 msg_Err( p_demux, "there's no open element left for <%s>",
291 if( p_handler->pf_handler.smpl )
293 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
309 * \brief parses the tracklist node which only may contain <track>s
311 static bool parse_tracklist_node COMPLEX_INTERFACE
313 VLC_UNUSED(psz_element);
318 /* now parse the <track>s */
319 while( (i_node = xml_ReaderNextNode( p_xml_reader )) > 0 )
321 if( i_node == XML_READER_STARTELEM )
323 char *psz_eltname = xml_ReaderName( p_xml_reader );
326 msg_Err( p_demux, "unexpected end of xml data" );
330 if( strcmp( psz_eltname, "track") )
332 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
339 /* parse the track data in a separate function */
340 if( parse_track_node( p_demux, p_input_node,
341 p_xml_reader, "track" ) )
344 else if( i_node == XML_READER_ENDELEM )
348 /* the <trackList> has to be terminated */
349 if( i_node != XML_READER_ENDELEM )
351 msg_Err( p_demux, "there's a missing </trackList>" );
354 psz_name = xml_ReaderName( p_xml_reader );
355 if( !psz_name || strcmp( psz_name, "trackList" ) )
357 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
363 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
368 * \brief parse one track element
369 * \param COMPLEX_INTERFACE
371 static bool parse_track_node COMPLEX_INTERFACE
373 input_item_t *p_input_item = p_input_node->p_item;
374 char *psz_name = NULL;
375 char *psz_value = NULL;
376 xml_elem_hnd_t *p_handler = NULL;
377 demux_sys_t *p_sys = p_demux->p_sys;
381 xml_elem_hnd_t track_elements[] =
382 { {"location", SIMPLE_CONTENT, {NULL} },
383 {"identifier", SIMPLE_CONTENT, {NULL} },
384 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
385 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
386 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
387 {"info", SIMPLE_CONTENT, {NULL} },
388 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
389 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
390 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
391 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
392 {"link", SIMPLE_CONTENT, {NULL} },
393 {"meta", SIMPLE_CONTENT, {NULL} },
394 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
395 {NULL, UNKNOWN_CONTENT, {NULL} }
398 input_item_t *p_new_input = input_item_New( p_demux, NULL, NULL );
399 input_item_node_t *p_new_node = input_item_node_Create( p_new_input );
403 /* malloc has failed for input_item_New, so bailout early */
407 /* reset i_track_id */
408 p_sys->i_track_id = -1;
410 while( (i_node = xml_ReaderNextNode( p_xml_reader )) > 0 )
414 case XML_READER_STARTELEM:
415 /* element start tag */
416 psz_name = xml_ReaderName( p_xml_reader );
417 if( !psz_name || !*psz_name )
419 msg_Err( p_demux, "invalid xml stream" );
423 for( p_handler = track_elements;
424 p_handler->name && strcmp( psz_name, p_handler->name );
426 if( !p_handler->name )
428 msg_Err( p_demux, "unexpected element <%s>", psz_name );
432 /* complex content is parsed in a separate function */
433 if( p_handler->type == COMPLEX_CONTENT )
437 bool b_res = p_handler->pf_handler.cmplx( p_demux,
452 case XML_READER_TEXT:
453 /* simple element content */
455 psz_value = xml_ReaderValue( p_xml_reader );
458 msg_Err( p_demux, "invalid xml stream" );
463 case XML_READER_ENDELEM:
464 /* element end tag */
465 psz_name = xml_ReaderName( p_xml_reader );
468 msg_Err( p_demux, "invalid xml stream" );
472 /* leave if the current parent node <track> is terminated */
473 if( !strcmp( psz_name, psz_element ) )
478 /* Make sure we have a URI */
479 char *psz_uri = input_item_GetURI( p_new_input );
482 input_item_SetURI( p_new_input, "vlc://nop" );
486 if( p_sys->i_track_id < 0 )
488 input_item_node_AppendNode( p_input_node, p_new_node );
489 vlc_gc_decref( p_new_input );
493 if( p_sys->i_track_id >= p_sys->i_tracklist_entries )
496 pp = realloc( p_sys->pp_tracklist,
497 (p_sys->i_track_id + 1) * sizeof(*pp) );
500 p_sys->pp_tracklist = pp;
501 while( p_sys->i_track_id >= p_sys->i_tracklist_entries )
502 pp[p_sys->i_tracklist_entries++] = NULL;
505 p_sys->pp_tracklist[ p_sys->i_track_id ] = p_new_input;
508 /* there MUST have been a start tag for that element name */
509 if( !p_handler || !p_handler->name
510 || strcmp( p_handler->name, psz_name ))
512 msg_Err( p_demux, "there's no open element left for <%s>",
517 /* special case: location */
518 if( !strcmp( p_handler->name, "location" ) )
520 if( psz_value == NULL )
521 input_item_SetURI( p_new_input, "vlc://nop" );
523 /* FIXME: This is broken. Scheme-relative (//...) locations
524 * and anchors (#...) are not resolved correctly. Also,
525 * host-relative (/...) and directory-relative locations
526 * ("relative path" in vernacular) should be resolved.
527 * Last, psz_base should default to the XSPF resource
528 * location if missing (not the current working directory).
530 if( p_sys->psz_base && !strstr( psz_value, "://" ) )
533 if( asprintf( &psz_tmp, "%s%s", p_sys->psz_base,
538 input_item_SetURI( p_new_input, psz_tmp );
542 input_item_SetURI( p_new_input, psz_value );
543 input_item_CopyOptions( p_input_item, p_new_input );
547 /* there MUST be an item */
548 if( p_handler->pf_handler.smpl )
550 p_handler->pf_handler.smpl( p_new_input,
561 msg_Err( p_demux, "unexpected end of xml data" );
570 * \brief handles the supported <track> sub-elements
572 static bool set_item_info SIMPLE_INTERFACE
574 /* exit if setting is impossible */
575 if( !psz_name || !psz_value || !p_input )
578 /* re-convert xml special characters inside psz_value */
579 resolve_xml_special_chars( psz_value );
581 /* handle each info element in a separate "if" clause */
582 if( !strcmp( psz_name, "title" ) )
584 input_item_SetTitle( p_input, psz_value );
586 else if( !strcmp( psz_name, "creator" ) )
588 input_item_SetArtist( p_input, psz_value );
590 else if( !strcmp( psz_name, "album" ) )
592 input_item_SetAlbum( p_input, psz_value );
595 else if( !strcmp( psz_name, "trackNum" ) )
597 input_item_SetTrackNum( p_input, psz_value );
599 else if( !strcmp( psz_name, "duration" ) )
601 long i_num = atol( psz_value );
602 input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
604 else if( !strcmp( psz_name, "annotation" ) )
606 input_item_SetDescription( p_input, psz_value );
608 else if( !strcmp( psz_name, "image" ) )
610 input_item_SetArtURL( p_input, psz_value );
616 * \brief handles the <vlc:option> elements
618 static bool set_option SIMPLE_INTERFACE
620 /* exit if setting is impossible */
621 if( !psz_name || !psz_value || !p_input )
624 /* re-convert xml special characters inside psz_value */
625 resolve_xml_special_chars( psz_value );
627 input_item_AddOption( p_input, psz_value, 0 );
633 * \brief parse the extension node of a XSPF playlist
635 static bool parse_extension_node COMPLEX_INTERFACE
637 input_item_t *p_input_item = p_input_node->p_item;
638 char *psz_name = NULL;
639 char *psz_value = NULL;
640 char *psz_title = NULL;
641 char *psz_application = NULL;
643 bool b_release_input_item = false;
644 xml_elem_hnd_t *p_handler = NULL;
645 input_item_t *p_new_input = NULL;
647 xml_elem_hnd_t pl_elements[] =
648 { {"vlc:node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
649 {"vlc:item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
650 {"vlc:id", SIMPLE_CONTENT, {NULL} },
651 {"vlc:option", SIMPLE_CONTENT, {.smpl = set_option} },
652 {NULL, UNKNOWN_CONTENT, {NULL} }
655 /* read all extension node attributes */
656 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
658 psz_name = xml_ReaderName( p_xml_reader );
659 psz_value = xml_ReaderValue( p_xml_reader );
660 if( !psz_name || !psz_value )
662 msg_Err( p_demux, "invalid xml stream @ <vlc:node>" );
666 /* attribute: title */
667 if( !strcmp( psz_name, "title" ) )
669 resolve_xml_special_chars( psz_value );
670 psz_title = psz_value;
672 /* extension attribute: application */
673 else if( !strcmp( psz_name, "application" ) )
675 psz_application = psz_value;
677 /* unknown attribute */
680 msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element,
688 /* attribute title is mandatory except for <extension> */
689 if( !strcmp( psz_element, "vlc:node" ) )
693 msg_Warn( p_demux, "<vlc:node> requires \"title\" attribute" );
696 p_new_input = input_item_NewWithType( VLC_OBJECT( p_demux ),
697 "vlc://nop", psz_title, 0, NULL, 0, -1,
698 ITEM_TYPE_DIRECTORY );
702 input_item_node_AppendItem( p_input_node, p_new_input );
703 p_input_item = p_new_input;
704 b_release_input_item = true;
708 else if( !strcmp( psz_element, "extension" ) )
710 if( !psz_application )
712 msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
715 /* Skip the extension if the application is not vlc
716 This will skip all children of the current node */
717 else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
719 msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
720 free( psz_application );
721 /* Skip all children */
722 for( unsigned lvl = 1; lvl; )
723 switch( xml_ReaderNextNode( p_xml_reader ) )
725 case XML_READER_STARTELEM: lvl++; break;
726 case XML_READER_ENDELEM: lvl--; break;
727 case 0: case -1: return -1;
732 free( psz_application );
735 /* parse the child elements */
736 while( (i_node = xml_ReaderNextNode( p_xml_reader )) > 0 )
740 case XML_READER_STARTELEM:
741 /* element start tag */
742 psz_name = xml_ReaderName( p_xml_reader );
743 if( !psz_name || !*psz_name )
745 msg_Err( p_demux, "invalid xml stream" );
747 if( b_release_input_item ) vlc_gc_decref( p_new_input );
751 for( p_handler = pl_elements;
752 p_handler->name && strcmp( psz_name, p_handler->name );
754 if( !p_handler->name )
756 msg_Err( p_demux, "unexpected element <%s>", psz_name );
758 if( b_release_input_item ) vlc_gc_decref( p_new_input );
762 /* complex content is parsed in a separate function */
763 if( p_handler->type == COMPLEX_CONTENT )
765 if( p_handler->pf_handler.cmplx( p_demux,
776 if( b_release_input_item ) vlc_gc_decref( p_new_input );
782 case XML_READER_TEXT:
783 /* simple element content */
785 psz_value = xml_ReaderValue( p_xml_reader );
788 msg_Err( p_demux, "invalid xml stream" );
790 if( b_release_input_item ) vlc_gc_decref( p_new_input );
795 case XML_READER_ENDELEM:
796 /* element end tag */
797 psz_name = xml_ReaderName( p_xml_reader );
800 msg_Err( p_demux, "invalid xml stream" );
802 if( b_release_input_item ) vlc_gc_decref( p_new_input );
805 /* leave if the current parent node is terminated */
806 if( !strcmp( psz_name, psz_element ) )
809 if( b_release_input_item ) vlc_gc_decref( p_new_input );
812 /* there MUST have been a start tag for that element name */
813 if( !p_handler || !p_handler->name
814 || strcmp( p_handler->name, psz_name ))
816 msg_Err( p_demux, "there's no open element left for <%s>",
819 if( b_release_input_item ) vlc_gc_decref( p_new_input );
823 /* special tag <vlc:id> */
824 if( !strcmp( p_handler->name, "vlc:id" ) )
826 p_demux->p_sys->i_track_id = atoi( psz_value );
828 else if( p_handler->pf_handler.smpl )
830 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
839 if( b_release_input_item ) vlc_gc_decref( p_new_input );
844 * \brief parse the extension item node of a XSPF playlist
846 static bool parse_extitem_node COMPLEX_INTERFACE
848 VLC_UNUSED(psz_element);
849 input_item_t *p_new_input = NULL;
852 /* read all extension item attributes */
853 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
855 char *psz_name = xml_ReaderName( p_xml_reader );
856 char *psz_value = xml_ReaderValue( p_xml_reader );
857 if( !psz_name || !psz_value )
859 msg_Err( p_demux, "invalid xml stream @ <vlc:item>" );
864 /* attribute: href */
865 if( !strcmp( psz_name, "tid" ) )
867 i_tid = atoi( psz_value );
869 /* unknown attribute */
871 msg_Warn( p_demux, "invalid <vlc:item> attribute:\"%s\"", psz_name);
877 /* attribute href is mandatory */
880 msg_Warn( p_demux, "<vlc:item> requires \"tid\" attribute" );
884 if( i_tid >= p_demux->p_sys->i_tracklist_entries )
886 msg_Warn( p_demux, "invalid \"tid\" attribute" );
890 p_new_input = p_demux->p_sys->pp_tracklist[ i_tid ];
893 input_item_node_AppendItem( p_input_node, p_new_input );
894 vlc_gc_decref( p_new_input );
895 p_demux->p_sys->pp_tracklist[i_tid] = NULL;
902 * \brief skips complex element content that we can't manage
904 static bool skip_element COMPLEX_INTERFACE
906 VLC_UNUSED(p_demux); VLC_UNUSED(p_input_node);
907 VLC_UNUSED(psz_element);
909 for( unsigned lvl = 1; lvl; )
910 switch( xml_ReaderNextNode( p_xml_reader ) )
912 case XML_READER_STARTELEM: lvl++; break;
913 case XML_READER_ENDELEM: lvl--; break;
914 case 0: case -1: return false;