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_AddSubItemTree( p_subitems );
137 input_item_node_Delete( p_subitems );
140 vlc_gc_decref(p_current_input);
142 xml_ReaderDelete( p_xml, p_xml_reader );
145 return i_ret; /* Needed for correct operation of go back */
148 /** \brief dummy function for demux callback interface */
149 static int Control( demux_t *p_demux, int i_query, va_list args )
151 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
156 * \brief parse the root node of a XSPF playlist
157 * \param p_demux demuxer instance
158 * \param p_input_item current input item
159 * \param p_xml_reader xml reader instance
160 * \param psz_element name of element to parse
162 static bool parse_playlist_node COMPLEX_INTERFACE
164 input_item_t *p_input_item = p_input_node->p_item;
165 char *psz_name = NULL;
166 char *psz_value = NULL;
167 bool b_version_found = false;
170 xml_elem_hnd_t *p_handler = NULL;
172 xml_elem_hnd_t pl_elements[] =
173 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
174 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
175 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
176 {"info", SIMPLE_CONTENT, {NULL} },
177 {"location", SIMPLE_CONTENT, {NULL} },
178 {"identifier", SIMPLE_CONTENT, {NULL} },
179 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
180 {"date", SIMPLE_CONTENT, {NULL} },
181 {"license", SIMPLE_CONTENT, {NULL} },
182 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
183 {"link", SIMPLE_CONTENT, {NULL} },
184 {"meta", SIMPLE_CONTENT, {NULL} },
185 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
186 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
187 {NULL, UNKNOWN_CONTENT, {NULL} }
190 /* read all playlist attributes */
191 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
193 psz_name = xml_ReaderName( p_xml_reader );
194 psz_value = xml_ReaderValue( p_xml_reader );
195 if( !psz_name || !psz_value )
197 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
200 /* attribute: version */
201 if( !strcmp( psz_name, "version" ) )
203 b_version_found = true;
204 if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
205 msg_Warn( p_demux, "unsupported XSPF version" );
207 /* attribute: xmlns */
208 else if( !strcmp( psz_name, "xmlns" ) || !strcmp( psz_name, "xmlns:vlc" ) )
210 else if( !strcmp( psz_name, "xml:base" ) )
212 p_demux->p_sys->psz_base = strdup( psz_value );
214 /* unknown attribute */
216 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
221 /* attribute version is mandatory !!! */
222 if( !b_version_found )
223 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
225 /* parse the child elements - we only take care of <trackList> */
228 while( xml_ReaderRead( p_xml_reader ) == 1 )
230 i_node = xml_ReaderNodeType( p_xml_reader );
233 case XML_READER_NONE:
235 case XML_READER_STARTELEM:
236 /* element start tag */
237 psz_name = xml_ReaderName( p_xml_reader );
238 if( !psz_name || !*psz_name )
240 msg_Err( p_demux, "invalid xml stream" );
244 for( p_handler = pl_elements;
245 p_handler->name && strcmp( psz_name, p_handler->name );
247 if( !p_handler->name )
249 msg_Err( p_demux, "unexpected element <%s>", psz_name );
253 /* complex content is parsed in a separate function */
254 if( p_handler->type == COMPLEX_CONTENT )
257 if( p_handler->pf_handler.cmplx( p_demux,
271 case XML_READER_TEXT:
272 /* simple element content */
274 psz_value = xml_ReaderValue( p_xml_reader );
277 msg_Err( p_demux, "invalid xml stream" );
282 case XML_READER_ENDELEM:
283 /* element end tag */
284 psz_name = xml_ReaderName( p_xml_reader );
287 msg_Err( p_demux, "invalid xml stream" );
290 /* leave if the current parent node <playlist> is terminated */
291 if( !strcmp( psz_name, psz_element ) )
296 /* there MUST have been a start tag for that element name */
297 if( !p_handler || !p_handler->name
298 || strcmp( p_handler->name, psz_name ))
300 msg_Err( p_demux, "there's no open element left for <%s>",
305 if( p_handler->pf_handler.smpl )
307 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
315 /* unknown/unexpected xml node */
316 msg_Err( p_demux, "unexpected xml node %i", i_node );
328 * \brief parses the tracklist node which only may contain <track>s
330 static bool parse_tracklist_node COMPLEX_INTERFACE
332 VLC_UNUSED(psz_element);
336 /* now parse the <track>s */
337 while( xml_ReaderRead( p_xml_reader ) == 1 )
339 int i_node = xml_ReaderNodeType( p_xml_reader );
340 if( i_node == XML_READER_STARTELEM )
342 char *psz_eltname = xml_ReaderName( p_xml_reader );
345 msg_Err( p_demux, "unexpected end of xml data" );
349 if( strcmp( psz_eltname, "track") )
351 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
358 /* parse the track data in a separate function */
359 if( parse_track_node( p_demux, p_input_node,
360 p_xml_reader, "track" ) )
363 else if( i_node == XML_READER_ENDELEM )
367 /* the <trackList> has to be terminated */
368 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
370 msg_Err( p_demux, "there's a missing </trackList>" );
373 psz_name = xml_ReaderName( p_xml_reader );
374 if( !psz_name || strcmp( psz_name, "trackList" ) )
376 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
382 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
387 * \brief parse one track element
388 * \param COMPLEX_INTERFACE
390 static bool parse_track_node COMPLEX_INTERFACE
392 input_item_t *p_input_item = p_input_node->p_item;
393 char *psz_name = NULL;
394 char *psz_value = NULL;
395 xml_elem_hnd_t *p_handler = NULL;
396 demux_sys_t *p_sys = p_demux->p_sys;
399 xml_elem_hnd_t track_elements[] =
400 { {"location", SIMPLE_CONTENT, {NULL} },
401 {"identifier", SIMPLE_CONTENT, {NULL} },
402 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
403 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
404 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
405 {"info", SIMPLE_CONTENT, {NULL} },
406 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
407 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
408 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
409 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
410 {"link", SIMPLE_CONTENT, {NULL} },
411 {"meta", SIMPLE_CONTENT, {NULL} },
412 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
413 {NULL, UNKNOWN_CONTENT, {NULL} }
416 input_item_t *p_new_input = input_item_New( p_demux, NULL, NULL );
417 input_item_node_t *p_new_node = input_item_node_Create( p_new_input );
421 /* malloc has failed for input_item_New, so bailout early */
425 /* reset i_track_id */
426 p_sys->i_track_id = -1;
428 while( xml_ReaderRead( p_xml_reader ) == 1 )
430 int i_node = xml_ReaderNodeType( p_xml_reader );
433 case XML_READER_NONE:
436 case XML_READER_STARTELEM:
437 /* element start tag */
438 psz_name = xml_ReaderName( p_xml_reader );
439 if( !psz_name || !*psz_name )
441 msg_Err( p_demux, "invalid xml stream" );
445 for( p_handler = track_elements;
446 p_handler->name && strcmp( psz_name, p_handler->name );
448 if( !p_handler->name )
450 msg_Err( p_demux, "unexpected element <%s>", psz_name );
454 /* complex content is parsed in a separate function */
455 if( p_handler->type == COMPLEX_CONTENT )
459 bool b_res = p_handler->pf_handler.cmplx( p_demux,
474 case XML_READER_TEXT:
475 /* simple element content */
477 psz_value = xml_ReaderValue( p_xml_reader );
480 msg_Err( p_demux, "invalid xml stream" );
485 case XML_READER_ENDELEM:
486 /* element end tag */
487 psz_name = xml_ReaderName( p_xml_reader );
490 msg_Err( p_demux, "invalid xml stream" );
494 /* leave if the current parent node <track> is terminated */
495 if( !strcmp( psz_name, psz_element ) )
500 /* Make sure we have a URI */
501 char *psz_uri = input_item_GetURI( p_new_input );
504 input_item_SetURI( p_new_input, "vlc://nop" );
508 if( p_sys->i_track_id < 0 )
510 input_item_node_AppendNode( p_input_node, p_new_node );
511 vlc_gc_decref( p_new_input );
515 if( p_sys->i_track_id >= p_sys->i_tracklist_entries )
518 pp = realloc( p_sys->pp_tracklist,
519 (p_sys->i_track_id + 1) * sizeof(*pp) );
522 p_sys->pp_tracklist = pp;
523 while( p_sys->i_track_id >= p_sys->i_tracklist_entries )
524 pp[p_sys->i_tracklist_entries++] = NULL;
527 p_sys->pp_tracklist[ p_sys->i_track_id ] = p_new_input;
530 /* there MUST have been a start tag for that element name */
531 if( !p_handler || !p_handler->name
532 || strcmp( p_handler->name, psz_name ))
534 msg_Err( p_demux, "there's no open element left for <%s>",
539 /* special case: location */
540 if( !strcmp( p_handler->name, "location" ) )
542 /* FIXME: This is broken. Scheme-relative (//...) locations
543 * and anchors (#...) are not resolved correctly. Also,
544 * host-relative (/...) and directory-relative locations
545 * ("relative path" in vernacular) should be resolved.
546 * Last, psz_base should default to the XSPF resource
547 * location if missing (not the current working directory).
549 if( p_sys->psz_base && !strstr( psz_value, "://" ) )
552 if( asprintf( &psz_tmp, "%s%s", p_sys->psz_base,
557 input_item_SetURI( p_new_input, psz_tmp );
561 input_item_SetURI( p_new_input, psz_value );
562 input_item_CopyOptions( p_input_item, p_new_input );
566 /* there MUST be an item */
567 if( p_handler->pf_handler.smpl )
569 p_handler->pf_handler.smpl( p_new_input,
580 /* unknown/unexpected xml node */
581 msg_Err( p_demux, "unexpected xml node %i", i_node );
585 msg_Err( p_demux, "unexpected end of xml data" );
594 * \brief handles the supported <track> sub-elements
596 static bool set_item_info SIMPLE_INTERFACE
598 /* exit if setting is impossible */
599 if( !psz_name || !psz_value || !p_input )
602 /* re-convert xml special characters inside psz_value */
603 resolve_xml_special_chars( psz_value );
605 /* handle each info element in a separate "if" clause */
606 if( !strcmp( psz_name, "title" ) )
608 input_item_SetTitle( p_input, psz_value );
610 else if( !strcmp( psz_name, "creator" ) )
612 input_item_SetArtist( p_input, psz_value );
614 else if( !strcmp( psz_name, "album" ) )
616 input_item_SetAlbum( p_input, psz_value );
619 else if( !strcmp( psz_name, "trackNum" ) )
621 input_item_SetTrackNum( p_input, psz_value );
623 else if( !strcmp( psz_name, "duration" ) )
625 long i_num = atol( psz_value );
626 input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
628 else if( !strcmp( psz_name, "annotation" ) )
630 input_item_SetDescription( p_input, psz_value );
632 else if( !strcmp( psz_name, "image" ) )
634 input_item_SetArtURL( p_input, psz_value );
640 * \brief handles the <vlc:option> elements
642 static bool set_option SIMPLE_INTERFACE
644 /* exit if setting is impossible */
645 if( !psz_name || !psz_value || !p_input )
648 /* re-convert xml special characters inside psz_value */
649 resolve_xml_special_chars( psz_value );
651 input_item_AddOption( p_input, psz_value, 0 );
657 * \brief parse the extension node of a XSPF playlist
659 static bool parse_extension_node COMPLEX_INTERFACE
661 input_item_t *p_input_item = p_input_node->p_item;
662 char *psz_name = NULL;
663 char *psz_value = NULL;
664 char *psz_title = NULL;
665 char *psz_application = NULL;
667 bool b_release_input_item = false;
668 xml_elem_hnd_t *p_handler = NULL;
669 input_item_t *p_new_input = NULL;
671 xml_elem_hnd_t pl_elements[] =
672 { {"vlc:node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
673 {"vlc:item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
674 {"vlc:id", SIMPLE_CONTENT, {NULL} },
675 {"vlc:option", SIMPLE_CONTENT, {.smpl = set_option} },
676 {NULL, UNKNOWN_CONTENT, {NULL} }
679 /* read all extension node attributes */
680 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
682 psz_name = xml_ReaderName( p_xml_reader );
683 psz_value = xml_ReaderValue( p_xml_reader );
684 if( !psz_name || !psz_value )
686 msg_Err( p_demux, "invalid xml stream @ <vlc:node>" );
690 /* attribute: title */
691 if( !strcmp( psz_name, "title" ) )
693 resolve_xml_special_chars( psz_value );
694 psz_title = psz_value;
696 /* extension attribute: application */
697 else if( !strcmp( psz_name, "application" ) )
699 psz_application = psz_value;
701 /* unknown attribute */
704 msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element,
712 /* attribute title is mandatory except for <extension> */
713 if( !strcmp( psz_element, "vlc:node" ) )
717 msg_Warn( p_demux, "<vlc:node> requires \"title\" attribute" );
720 p_new_input = input_item_NewWithType( VLC_OBJECT( p_demux ),
721 "vlc://nop", psz_title, 0, NULL, 0, -1,
722 ITEM_TYPE_DIRECTORY );
726 input_item_node_AppendItem( p_input_node, p_new_input );
727 p_input_item = p_new_input;
728 b_release_input_item = true;
732 else if( !strcmp( psz_element, "extension" ) )
734 if( !psz_application )
736 msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
739 else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
741 msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
742 free( psz_application );
746 free( psz_application );
749 /* parse the child elements */
750 while( xml_ReaderRead( p_xml_reader ) == 1 )
752 i_node = xml_ReaderNodeType( p_xml_reader );
755 case XML_READER_NONE:
757 case XML_READER_STARTELEM:
758 /* element start tag */
759 psz_name = xml_ReaderName( p_xml_reader );
760 if( !psz_name || !*psz_name )
762 msg_Err( p_demux, "invalid xml stream" );
764 if( b_release_input_item ) vlc_gc_decref( p_new_input );
768 for( p_handler = pl_elements;
769 p_handler->name && strcmp( psz_name, p_handler->name );
771 if( !p_handler->name )
773 msg_Err( p_demux, "unexpected element <%s>", psz_name );
775 if( b_release_input_item ) vlc_gc_decref( p_new_input );
779 /* complex content is parsed in a separate function */
780 if( p_handler->type == COMPLEX_CONTENT )
782 if( p_handler->pf_handler.cmplx( p_demux,
793 if( b_release_input_item ) vlc_gc_decref( p_new_input );
799 case XML_READER_TEXT:
800 /* simple element content */
802 psz_value = xml_ReaderValue( p_xml_reader );
805 msg_Err( p_demux, "invalid xml stream" );
807 if( b_release_input_item ) vlc_gc_decref( p_new_input );
812 case XML_READER_ENDELEM:
813 /* element end tag */
814 psz_name = xml_ReaderName( p_xml_reader );
817 msg_Err( p_demux, "invalid xml stream" );
819 if( b_release_input_item ) vlc_gc_decref( p_new_input );
822 /* leave if the current parent node is terminated */
823 if( !strcmp( psz_name, psz_element ) )
826 if( b_release_input_item ) vlc_gc_decref( p_new_input );
829 /* there MUST have been a start tag for that element name */
830 if( !p_handler || !p_handler->name
831 || strcmp( p_handler->name, psz_name ))
833 msg_Err( p_demux, "there's no open element left for <%s>",
836 if( b_release_input_item ) vlc_gc_decref( p_new_input );
840 /* special tag <vlc:id> */
841 if( !strcmp( p_handler->name, "vlc:id" ) )
843 p_demux->p_sys->i_track_id = atoi( psz_value );
845 else if( p_handler->pf_handler.smpl )
847 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
855 /* unknown/unexpected xml node */
856 msg_Err( p_demux, "unexpected xml node %i", i_node );
858 if( b_release_input_item ) vlc_gc_decref( p_new_input );
863 if( b_release_input_item ) vlc_gc_decref( p_new_input );
868 * \brief parse the extension item node of a XSPF playlist
870 static bool parse_extitem_node COMPLEX_INTERFACE
872 VLC_UNUSED(psz_element);
873 input_item_t *p_new_input = NULL;
876 /* read all extension item attributes */
877 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
879 char *psz_name = xml_ReaderName( p_xml_reader );
880 char *psz_value = xml_ReaderValue( p_xml_reader );
881 if( !psz_name || !psz_value )
883 msg_Err( p_demux, "invalid xml stream @ <vlc:item>" );
888 /* attribute: href */
889 if( !strcmp( psz_name, "tid" ) )
891 i_tid = atoi( psz_value );
893 /* unknown attribute */
895 msg_Warn( p_demux, "invalid <vlc:item> attribute:\"%s\"", psz_name);
901 /* attribute href is mandatory */
904 msg_Warn( p_demux, "<vlc:item> requires \"tid\" attribute" );
908 if( i_tid >= p_demux->p_sys->i_tracklist_entries )
910 msg_Warn( p_demux, "invalid \"tid\" attribute" );
914 p_new_input = p_demux->p_sys->pp_tracklist[ i_tid ];
917 input_item_node_AppendItem( p_input_node, p_new_input );
918 vlc_gc_decref( p_new_input );
919 p_demux->p_sys->pp_tracklist[i_tid] = NULL;
922 /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
923 /* (libxml sends NONE) */
924 xml_ReaderRead( p_xml_reader );
930 * \brief skips complex element content that we can't manage
932 static bool skip_element COMPLEX_INTERFACE
934 VLC_UNUSED(p_demux); VLC_UNUSED(p_input_node);
936 while( xml_ReaderRead( p_xml_reader ) == 1 )
938 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
940 char *psz_endname = xml_ReaderName( p_xml_reader );
943 if( !strcmp( psz_element, psz_endname ) )