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 )
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;
92 /* create new xml parser from stream */
93 p_xml = xml_Create( p_demux );
97 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
101 /* locating the root node */
104 if( xml_ReaderRead( p_xml_reader ) != 1 )
106 msg_Err( p_demux, "can't read xml stream" );
109 } while( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
111 /* checking root node name */
112 psz_name = xml_ReaderName( p_xml_reader );
113 if( !psz_name || strcmp( psz_name, "playlist" ) )
115 msg_Err( p_demux, "invalid root node name: %s", psz_name );
121 input_item_node_t *p_subitems =
122 input_item_node_Create( p_current_input );
124 i_ret = parse_playlist_node( p_demux, p_subitems,
125 p_xml_reader, "playlist" ) ? 0 : -1;
127 for( int i = 0 ; i < p_demux->p_sys->i_tracklist_entries ; i++ )
129 input_item_t *p_new_input = p_demux->p_sys->pp_tracklist[i];
132 input_item_node_AppendItem( p_subitems, p_new_input );
136 input_item_node_PostAndDelete( p_subitems );
139 vlc_gc_decref(p_current_input);
141 xml_ReaderDelete( p_xml, p_xml_reader );
144 return i_ret; /* Needed for correct operation of go back */
147 /** \brief dummy function for demux callback interface */
148 static int Control( demux_t *p_demux, int i_query, va_list args )
150 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
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
161 static bool parse_playlist_node COMPLEX_INTERFACE
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;
169 xml_elem_hnd_t *p_handler = NULL;
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} }
189 /* read all playlist attributes */
190 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
192 psz_name = xml_ReaderName( p_xml_reader );
193 psz_value = xml_ReaderValue( p_xml_reader );
194 if( !psz_name || !psz_value )
196 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
199 /* attribute: version */
200 if( !strcmp( psz_name, "version" ) )
202 b_version_found = true;
203 if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
204 msg_Warn( p_demux, "unsupported XSPF version" );
206 /* attribute: xmlns */
207 else if( !strcmp( psz_name, "xmlns" ) || !strcmp( psz_name, "xmlns:vlc" ) )
209 else if( !strcmp( psz_name, "xml:base" ) )
211 p_demux->p_sys->psz_base = strdup( psz_value );
213 /* unknown attribute */
215 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
220 /* attribute version is mandatory !!! */
221 if( !b_version_found )
222 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
224 /* parse the child elements - we only take care of <trackList> */
227 while( xml_ReaderRead( p_xml_reader ) == 1 )
229 i_node = xml_ReaderNodeType( p_xml_reader );
232 case XML_READER_NONE:
234 case XML_READER_STARTELEM:
235 /* element start tag */
236 psz_name = xml_ReaderName( p_xml_reader );
237 if( !psz_name || !*psz_name )
239 msg_Err( p_demux, "invalid xml stream" );
243 for( p_handler = pl_elements;
244 p_handler->name && strcmp( psz_name, p_handler->name );
246 if( !p_handler->name )
248 msg_Err( p_demux, "unexpected element <%s>", psz_name );
252 /* complex content is parsed in a separate function */
253 if( p_handler->type == COMPLEX_CONTENT )
256 if( p_handler->pf_handler.cmplx( p_demux,
270 case XML_READER_TEXT:
271 /* simple element content */
273 psz_value = xml_ReaderValue( p_xml_reader );
276 msg_Err( p_demux, "invalid xml stream" );
281 case XML_READER_ENDELEM:
282 /* element end tag */
283 psz_name = xml_ReaderName( p_xml_reader );
286 msg_Err( p_demux, "invalid xml stream" );
289 /* leave if the current parent node <playlist> is terminated */
290 if( !strcmp( psz_name, psz_element ) )
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 ))
299 msg_Err( p_demux, "there's no open element left for <%s>",
304 if( p_handler->pf_handler.smpl )
306 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
314 /* unknown/unexpected xml node */
315 msg_Err( p_demux, "unexpected xml node %i", i_node );
327 * \brief parses the tracklist node which only may contain <track>s
329 static bool parse_tracklist_node COMPLEX_INTERFACE
331 VLC_UNUSED(psz_element);
335 /* now parse the <track>s */
336 while( xml_ReaderRead( p_xml_reader ) == 1 )
338 int i_node = xml_ReaderNodeType( p_xml_reader );
339 if( i_node == XML_READER_STARTELEM )
341 char *psz_eltname = xml_ReaderName( p_xml_reader );
344 msg_Err( p_demux, "unexpected end of xml data" );
348 if( strcmp( psz_eltname, "track") )
350 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
357 /* parse the track data in a separate function */
358 if( parse_track_node( p_demux, p_input_node,
359 p_xml_reader, "track" ) )
362 else if( i_node == XML_READER_ENDELEM )
366 /* the <trackList> has to be terminated */
367 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
369 msg_Err( p_demux, "there's a missing </trackList>" );
372 psz_name = xml_ReaderName( p_xml_reader );
373 if( !psz_name || strcmp( psz_name, "trackList" ) )
375 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
381 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
386 * \brief parse one track element
387 * \param COMPLEX_INTERFACE
389 static bool parse_track_node COMPLEX_INTERFACE
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;
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} }
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 );
420 /* malloc has failed for input_item_New, so bailout early */
424 /* reset i_track_id */
425 p_sys->i_track_id = -1;
427 while( xml_ReaderRead( p_xml_reader ) == 1 )
429 int i_node = xml_ReaderNodeType( p_xml_reader );
432 case XML_READER_NONE:
435 case XML_READER_STARTELEM:
436 /* element start tag */
437 psz_name = xml_ReaderName( p_xml_reader );
438 if( !psz_name || !*psz_name )
440 msg_Err( p_demux, "invalid xml stream" );
444 for( p_handler = track_elements;
445 p_handler->name && strcmp( psz_name, p_handler->name );
447 if( !p_handler->name )
449 msg_Err( p_demux, "unexpected element <%s>", psz_name );
453 /* complex content is parsed in a separate function */
454 if( p_handler->type == COMPLEX_CONTENT )
458 bool b_res = p_handler->pf_handler.cmplx( p_demux,
473 case XML_READER_TEXT:
474 /* simple element content */
476 psz_value = xml_ReaderValue( p_xml_reader );
479 msg_Err( p_demux, "invalid xml stream" );
484 case XML_READER_ENDELEM:
485 /* element end tag */
486 psz_name = xml_ReaderName( p_xml_reader );
489 msg_Err( p_demux, "invalid xml stream" );
493 /* leave if the current parent node <track> is terminated */
494 if( !strcmp( psz_name, psz_element ) )
499 /* Make sure we have a URI */
500 char *psz_uri = input_item_GetURI( p_new_input );
503 input_item_SetURI( p_new_input, "vlc://nop" );
507 if( p_sys->i_track_id < 0 )
509 input_item_node_AppendNode( p_input_node, p_new_node );
510 vlc_gc_decref( p_new_input );
514 if( p_sys->i_track_id >= p_sys->i_tracklist_entries )
517 pp = realloc( p_sys->pp_tracklist,
518 (p_sys->i_track_id + 1) * sizeof(*pp) );
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;
526 p_sys->pp_tracklist[ p_sys->i_track_id ] = p_new_input;
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 ))
533 msg_Err( p_demux, "there's no open element left for <%s>",
538 /* special case: location */
539 if( !strcmp( p_handler->name, "location" ) )
541 if( psz_value == NULL )
542 input_item_SetURI( p_new_input, "vlc://nop" );
544 /* FIXME: This is broken. Scheme-relative (//...) locations
545 * and anchors (#...) are not resolved correctly. Also,
546 * host-relative (/...) and directory-relative locations
547 * ("relative path" in vernacular) should be resolved.
548 * Last, psz_base should default to the XSPF resource
549 * location if missing (not the current working directory).
551 if( p_sys->psz_base && !strstr( psz_value, "://" ) )
554 if( asprintf( &psz_tmp, "%s%s", p_sys->psz_base,
559 input_item_SetURI( p_new_input, psz_tmp );
563 input_item_SetURI( p_new_input, psz_value );
564 input_item_CopyOptions( p_input_item, p_new_input );
568 /* there MUST be an item */
569 if( p_handler->pf_handler.smpl )
571 p_handler->pf_handler.smpl( p_new_input,
582 /* unknown/unexpected xml node */
583 msg_Err( p_demux, "unexpected xml node %i", i_node );
587 msg_Err( p_demux, "unexpected end of xml data" );
596 * \brief handles the supported <track> sub-elements
598 static bool set_item_info SIMPLE_INTERFACE
600 /* exit if setting is impossible */
601 if( !psz_name || !psz_value || !p_input )
604 /* re-convert xml special characters inside psz_value */
605 resolve_xml_special_chars( psz_value );
607 /* handle each info element in a separate "if" clause */
608 if( !strcmp( psz_name, "title" ) )
610 input_item_SetTitle( p_input, psz_value );
612 else if( !strcmp( psz_name, "creator" ) )
614 input_item_SetArtist( p_input, psz_value );
616 else if( !strcmp( psz_name, "album" ) )
618 input_item_SetAlbum( p_input, psz_value );
621 else if( !strcmp( psz_name, "trackNum" ) )
623 input_item_SetTrackNum( p_input, psz_value );
625 else if( !strcmp( psz_name, "duration" ) )
627 long i_num = atol( psz_value );
628 input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
630 else if( !strcmp( psz_name, "annotation" ) )
632 input_item_SetDescription( p_input, psz_value );
634 else if( !strcmp( psz_name, "image" ) )
636 input_item_SetArtURL( p_input, psz_value );
642 * \brief handles the <vlc:option> elements
644 static bool set_option SIMPLE_INTERFACE
646 /* exit if setting is impossible */
647 if( !psz_name || !psz_value || !p_input )
650 /* re-convert xml special characters inside psz_value */
651 resolve_xml_special_chars( psz_value );
653 input_item_AddOption( p_input, psz_value, 0 );
659 * \brief parse the extension node of a XSPF playlist
661 static bool parse_extension_node COMPLEX_INTERFACE
663 input_item_t *p_input_item = p_input_node->p_item;
664 char *psz_name = NULL;
665 char *psz_value = NULL;
666 char *psz_title = NULL;
667 char *psz_application = NULL;
669 bool b_release_input_item = false;
670 xml_elem_hnd_t *p_handler = NULL;
671 input_item_t *p_new_input = NULL;
673 xml_elem_hnd_t pl_elements[] =
674 { {"vlc:node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
675 {"vlc:item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
676 {"vlc:id", SIMPLE_CONTENT, {NULL} },
677 {"vlc:option", SIMPLE_CONTENT, {.smpl = set_option} },
678 {NULL, UNKNOWN_CONTENT, {NULL} }
681 /* read all extension node attributes */
682 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
684 psz_name = xml_ReaderName( p_xml_reader );
685 psz_value = xml_ReaderValue( p_xml_reader );
686 if( !psz_name || !psz_value )
688 msg_Err( p_demux, "invalid xml stream @ <vlc:node>" );
692 /* attribute: title */
693 if( !strcmp( psz_name, "title" ) )
695 resolve_xml_special_chars( psz_value );
696 psz_title = psz_value;
698 /* extension attribute: application */
699 else if( !strcmp( psz_name, "application" ) )
701 psz_application = psz_value;
703 /* unknown attribute */
706 msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element,
714 /* attribute title is mandatory except for <extension> */
715 if( !strcmp( psz_element, "vlc:node" ) )
719 msg_Warn( p_demux, "<vlc:node> requires \"title\" attribute" );
722 p_new_input = input_item_NewWithType( VLC_OBJECT( p_demux ),
723 "vlc://nop", psz_title, 0, NULL, 0, -1,
724 ITEM_TYPE_DIRECTORY );
728 input_item_node_AppendItem( p_input_node, p_new_input );
729 p_input_item = p_new_input;
730 b_release_input_item = true;
734 else if( !strcmp( psz_element, "extension" ) )
736 if( !psz_application )
738 msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
741 /* Skip the extension if the application is not vlc
742 This will skip all children of the current node */
743 else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
745 msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
746 free( psz_application );
747 /* Skip all children */
748 while( xml_ReaderRead( p_xml_reader ) == 1 )
750 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
752 char *psz_name = xml_ReaderName( p_xml_reader );
753 if( !strcmp( psz_name, "extension" ) )
758 msg_Dbg( p_demux, "\tskipping \"%s\" extension child", psz_name );
765 free( psz_application );
768 /* parse the child elements */
769 while( xml_ReaderRead( p_xml_reader ) == 1 )
771 i_node = xml_ReaderNodeType( p_xml_reader );
774 case XML_READER_NONE:
776 case XML_READER_STARTELEM:
777 /* element start tag */
778 psz_name = xml_ReaderName( p_xml_reader );
779 if( !psz_name || !*psz_name )
781 msg_Err( p_demux, "invalid xml stream" );
783 if( b_release_input_item ) vlc_gc_decref( p_new_input );
787 for( p_handler = pl_elements;
788 p_handler->name && strcmp( psz_name, p_handler->name );
790 if( !p_handler->name )
792 msg_Err( p_demux, "unexpected element <%s>", psz_name );
794 if( b_release_input_item ) vlc_gc_decref( p_new_input );
798 /* complex content is parsed in a separate function */
799 if( p_handler->type == COMPLEX_CONTENT )
801 if( p_handler->pf_handler.cmplx( p_demux,
812 if( b_release_input_item ) vlc_gc_decref( p_new_input );
818 case XML_READER_TEXT:
819 /* simple element content */
821 psz_value = xml_ReaderValue( p_xml_reader );
824 msg_Err( p_demux, "invalid xml stream" );
826 if( b_release_input_item ) vlc_gc_decref( p_new_input );
831 case XML_READER_ENDELEM:
832 /* element end tag */
833 psz_name = xml_ReaderName( p_xml_reader );
836 msg_Err( p_demux, "invalid xml stream" );
838 if( b_release_input_item ) vlc_gc_decref( p_new_input );
841 /* leave if the current parent node is terminated */
842 if( !strcmp( psz_name, psz_element ) )
845 if( b_release_input_item ) vlc_gc_decref( p_new_input );
848 /* there MUST have been a start tag for that element name */
849 if( !p_handler || !p_handler->name
850 || strcmp( p_handler->name, psz_name ))
852 msg_Err( p_demux, "there's no open element left for <%s>",
855 if( b_release_input_item ) vlc_gc_decref( p_new_input );
859 /* special tag <vlc:id> */
860 if( !strcmp( p_handler->name, "vlc:id" ) )
862 p_demux->p_sys->i_track_id = atoi( psz_value );
864 else if( p_handler->pf_handler.smpl )
866 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
874 /* unknown/unexpected xml node */
875 msg_Err( p_demux, "unexpected xml node %i", i_node );
877 if( b_release_input_item ) vlc_gc_decref( p_new_input );
882 if( b_release_input_item ) vlc_gc_decref( p_new_input );
887 * \brief parse the extension item node of a XSPF playlist
889 static bool parse_extitem_node COMPLEX_INTERFACE
891 VLC_UNUSED(psz_element);
892 input_item_t *p_new_input = NULL;
895 /* read all extension item attributes */
896 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
898 char *psz_name = xml_ReaderName( p_xml_reader );
899 char *psz_value = xml_ReaderValue( p_xml_reader );
900 if( !psz_name || !psz_value )
902 msg_Err( p_demux, "invalid xml stream @ <vlc:item>" );
907 /* attribute: href */
908 if( !strcmp( psz_name, "tid" ) )
910 i_tid = atoi( psz_value );
912 /* unknown attribute */
914 msg_Warn( p_demux, "invalid <vlc:item> attribute:\"%s\"", psz_name);
920 /* attribute href is mandatory */
923 msg_Warn( p_demux, "<vlc:item> requires \"tid\" attribute" );
927 if( i_tid >= p_demux->p_sys->i_tracklist_entries )
929 msg_Warn( p_demux, "invalid \"tid\" attribute" );
933 p_new_input = p_demux->p_sys->pp_tracklist[ i_tid ];
936 input_item_node_AppendItem( p_input_node, p_new_input );
937 vlc_gc_decref( p_new_input );
938 p_demux->p_sys->pp_tracklist[i_tid] = NULL;
941 /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
942 /* (libxml sends NONE) */
943 xml_ReaderRead( p_xml_reader );
949 * \brief skips complex element content that we can't manage
951 static bool skip_element COMPLEX_INTERFACE
953 VLC_UNUSED(p_demux); VLC_UNUSED(p_input_node);
955 while( xml_ReaderRead( p_xml_reader ) == 1 )
957 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
959 char *psz_endname = xml_ReaderName( p_xml_reader );
962 if( !strcmp( psz_element, psz_endname ) )