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 i_ret = parse_playlist_node( p_demux, p_current_input,
122 p_xml_reader, "playlist" ) ? 0 : -1;
124 for( int i = 0 ; i < p_demux->p_sys->i_tracklist_entries ; i++ )
126 input_item_t *p_new_input = p_demux->p_sys->pp_tracklist[i];
129 input_item_AddSubItem( p_current_input, p_new_input );
134 vlc_gc_decref(p_current_input);
136 xml_ReaderDelete( p_xml, p_xml_reader );
139 return i_ret; /* Needed for correct operation of go back */
142 /** \brief dummy function for demux callback interface */
143 static int Control( demux_t *p_demux, int i_query, va_list args )
145 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
150 * \brief parse the root node of a XSPF playlist
151 * \param p_demux demuxer instance
152 * \param p_input_item current input item
153 * \param p_xml_reader xml reader instance
154 * \param psz_element name of element to parse
156 static bool parse_playlist_node COMPLEX_INTERFACE
158 char *psz_name = NULL;
159 char *psz_value = NULL;
160 bool b_version_found = false;
163 xml_elem_hnd_t *p_handler = NULL;
165 xml_elem_hnd_t pl_elements[] =
166 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
167 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
168 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
169 {"info", SIMPLE_CONTENT, {NULL} },
170 {"location", SIMPLE_CONTENT, {NULL} },
171 {"identifier", SIMPLE_CONTENT, {NULL} },
172 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
173 {"date", SIMPLE_CONTENT, {NULL} },
174 {"license", SIMPLE_CONTENT, {NULL} },
175 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
176 {"link", SIMPLE_CONTENT, {NULL} },
177 {"meta", SIMPLE_CONTENT, {NULL} },
178 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
179 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
180 {NULL, UNKNOWN_CONTENT, {NULL} }
183 /* read all playlist attributes */
184 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
186 psz_name = xml_ReaderName( p_xml_reader );
187 psz_value = xml_ReaderValue( p_xml_reader );
188 if( !psz_name || !psz_value )
190 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
193 /* attribute: version */
194 if( !strcmp( psz_name, "version" ) )
196 b_version_found = true;
197 if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
198 msg_Warn( p_demux, "unsupported XSPF version" );
200 /* attribute: xmlns */
201 else if( !strcmp( psz_name, "xmlns" ) || !strcmp( psz_name, "xmlns:vlc" ) )
203 else if( !strcmp( psz_name, "xml:base" ) )
205 p_demux->p_sys->psz_base = strdup( psz_value );
207 /* unknown attribute */
209 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
214 /* attribute version is mandatory !!! */
215 if( !b_version_found )
216 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
218 /* parse the child elements - we only take care of <trackList> */
221 while( xml_ReaderRead( p_xml_reader ) == 1 )
223 i_node = xml_ReaderNodeType( p_xml_reader );
226 case XML_READER_NONE:
228 case XML_READER_STARTELEM:
229 /* element start tag */
230 psz_name = xml_ReaderName( p_xml_reader );
231 if( !psz_name || !*psz_name )
233 msg_Err( p_demux, "invalid xml stream" );
237 for( p_handler = pl_elements;
238 p_handler->name && strcmp( psz_name, p_handler->name );
240 if( !p_handler->name )
242 msg_Err( p_demux, "unexpected element <%s>", psz_name );
246 /* complex content is parsed in a separate function */
247 if( p_handler->type == COMPLEX_CONTENT )
250 if( p_handler->pf_handler.cmplx( p_demux,
264 case XML_READER_TEXT:
265 /* simple element content */
267 psz_value = xml_ReaderValue( p_xml_reader );
270 msg_Err( p_demux, "invalid xml stream" );
275 case XML_READER_ENDELEM:
276 /* element end tag */
277 psz_name = xml_ReaderName( p_xml_reader );
280 msg_Err( p_demux, "invalid xml stream" );
283 /* leave if the current parent node <playlist> is terminated */
284 if( !strcmp( psz_name, psz_element ) )
289 /* there MUST have been a start tag for that element name */
290 if( !p_handler || !p_handler->name
291 || strcmp( p_handler->name, psz_name ))
293 msg_Err( p_demux, "there's no open element left for <%s>",
298 if( p_handler->pf_handler.smpl )
300 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
308 /* unknown/unexpected xml node */
309 msg_Err( p_demux, "unexpected xml node %i", i_node );
321 * \brief parses the tracklist node which only may contain <track>s
323 static bool parse_tracklist_node COMPLEX_INTERFACE
325 VLC_UNUSED(psz_element);
329 /* now parse the <track>s */
330 while( xml_ReaderRead( p_xml_reader ) == 1 )
332 int i_node = xml_ReaderNodeType( p_xml_reader );
333 if( i_node == XML_READER_STARTELEM )
335 char *psz_eltname = xml_ReaderName( p_xml_reader );
338 msg_Err( p_demux, "unexpected end of xml data" );
342 if( strcmp( psz_eltname, "track") )
344 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
351 /* parse the track data in a separate function */
352 if( parse_track_node( p_demux, p_input_item, p_xml_reader,
356 else if( i_node == XML_READER_ENDELEM )
360 /* the <trackList> has to be terminated */
361 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
363 msg_Err( p_demux, "there's a missing </trackList>" );
366 psz_name = xml_ReaderName( p_xml_reader );
367 if( !psz_name || strcmp( psz_name, "trackList" ) )
369 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
375 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
380 * \brief parse one track element
381 * \param COMPLEX_INTERFACE
383 static bool parse_track_node COMPLEX_INTERFACE
386 char *psz_name = NULL;
387 char *psz_value = NULL;
388 xml_elem_hnd_t *p_handler = NULL;
390 xml_elem_hnd_t track_elements[] =
391 { {"location", SIMPLE_CONTENT, {NULL} },
392 {"identifier", SIMPLE_CONTENT, {NULL} },
393 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
394 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
395 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
396 {"info", SIMPLE_CONTENT, {NULL} },
397 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
398 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
399 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
400 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
401 {"link", SIMPLE_CONTENT, {NULL} },
402 {"meta", SIMPLE_CONTENT, {NULL} },
403 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
404 {NULL, UNKNOWN_CONTENT, {NULL} }
407 input_item_t *p_new_input = input_item_New( p_demux, NULL, NULL );
411 /* malloc has failed for input_item_New, so bailout early */
415 /* reset i_track_id */
416 p_demux->p_sys->i_track_id = -1;
418 while( xml_ReaderRead( p_xml_reader ) == 1 )
420 i_node = xml_ReaderNodeType( p_xml_reader );
423 case XML_READER_NONE:
426 case XML_READER_STARTELEM:
427 /* element start tag */
428 psz_name = xml_ReaderName( p_xml_reader );
429 if( !psz_name || !*psz_name )
431 msg_Err( p_demux, "invalid xml stream" );
436 for( p_handler = track_elements;
437 p_handler->name && strcmp( psz_name, p_handler->name );
439 if( !p_handler->name )
441 msg_Err( p_demux, "unexpected element <%s>", psz_name );
446 /* complex content is parsed in a separate function */
447 if( p_handler->type == COMPLEX_CONTENT )
449 if( p_handler->pf_handler.cmplx( p_demux,
465 case XML_READER_TEXT:
466 /* simple element content */
468 psz_value = xml_ReaderValue( p_xml_reader );
471 msg_Err( p_demux, "invalid xml stream" );
477 case XML_READER_ENDELEM:
478 /* element end tag */
479 psz_name = xml_ReaderName( p_xml_reader );
482 msg_Err( p_demux, "invalid xml stream" );
487 /* leave if the current parent node <track> is terminated */
488 if( !strcmp( psz_name, psz_element ) )
492 /* Make sure we have a URI */
493 char *psz_uri = input_item_GetURI( p_new_input );
496 input_item_SetURI( p_new_input, "vlc://nop" );
500 if( p_demux->p_sys->i_track_id < 0 )
502 input_item_AddSubItem( p_input_item, p_new_input );
503 vlc_gc_decref( p_new_input );
507 if( p_demux->p_sys->i_track_id >=
508 p_demux->p_sys->i_tracklist_entries )
511 pp = realloc( p_demux->p_sys->pp_tracklist,
512 (p_demux->p_sys->i_track_id + 1) * sizeof(*pp) );
515 p_demux->p_sys->pp_tracklist = pp;
516 while( p_demux->p_sys->i_track_id >=
517 p_demux->p_sys->i_tracklist_entries )
518 pp[p_demux->p_sys->i_tracklist_entries++] = NULL;
521 p_demux->p_sys->pp_tracklist[
522 p_demux->p_sys->i_track_id ] = p_new_input;
525 /* there MUST have been a start tag for that element name */
526 if( !p_handler || !p_handler->name
527 || strcmp( p_handler->name, psz_name ))
529 msg_Err( p_demux, "there's no open element left for <%s>",
535 /* special case: location */
536 if( !strcmp( p_handler->name, "location" ) )
538 /* FIXME: This is broken. Scheme-relative (//...) locations
539 * and anchors (#...) are not resolved correctly. Also,
540 * host-relative (/...) and directory-relative locations
541 * ("relative path" in vernacular) should be resolved.
542 * Last, psz_base should default to the XSPF resource
543 * location if missing (not the current working directory).
545 if( p_demux->p_sys->psz_base && !strstr( psz_value, "://" ) )
548 if( asprintf( &psz_tmp, "%s%s",
549 p_demux->p_sys->psz_base, psz_value ) == -1 )
554 input_item_SetURI( p_new_input, psz_tmp );
558 input_item_SetURI( p_new_input, psz_value );
559 input_item_CopyOptions( p_input_item, p_new_input );
565 /* there MUST be an item */
566 if( p_handler->pf_handler.smpl )
568 p_handler->pf_handler.smpl( p_new_input,
579 /* unknown/unexpected xml node */
580 msg_Err( p_demux, "unexpected xml node %i", i_node );
586 msg_Err( p_demux, "unexpected end of xml data" );
592 * \brief handles the supported <track> sub-elements
594 static bool set_item_info SIMPLE_INTERFACE
596 /* exit if setting is impossible */
597 if( !psz_name || !psz_value || !p_input )
600 /* re-convert xml special characters inside psz_value */
601 resolve_xml_special_chars( psz_value );
603 /* handle each info element in a separate "if" clause */
604 if( !strcmp( psz_name, "title" ) )
606 input_item_SetTitle( p_input, psz_value );
608 else if( !strcmp( psz_name, "creator" ) )
610 input_item_SetArtist( p_input, psz_value );
612 else if( !strcmp( psz_name, "album" ) )
614 input_item_SetAlbum( p_input, psz_value );
617 else if( !strcmp( psz_name, "trackNum" ) )
619 input_item_SetTrackNum( p_input, psz_value );
621 else if( !strcmp( psz_name, "duration" ) )
623 long i_num = atol( psz_value );
624 input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
626 else if( !strcmp( psz_name, "annotation" ) )
628 input_item_SetDescription( p_input, psz_value );
630 else if( !strcmp( psz_name, "image" ) )
632 input_item_SetArtURL( p_input, psz_value );
638 * \brief handles the <vlc:option> elements
640 static bool set_option SIMPLE_INTERFACE
642 /* exit if setting is impossible */
643 if( !psz_name || !psz_value || !p_input )
646 /* re-convert xml special characters inside psz_value */
647 resolve_xml_special_chars( psz_value );
649 input_item_AddOption( p_input, psz_value, 0 );
655 * \brief parse the extension node of a XSPF playlist
657 static bool parse_extension_node COMPLEX_INTERFACE
659 char *psz_name = NULL;
660 char *psz_value = NULL;
661 char *psz_title = NULL;
662 char *psz_application = NULL;
664 bool b_release_input_item = false;
665 xml_elem_hnd_t *p_handler = NULL;
666 input_item_t *p_new_input = NULL;
668 xml_elem_hnd_t pl_elements[] =
669 { {"vlc:node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
670 {"vlc:item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
671 {"vlc:id", SIMPLE_CONTENT, {NULL} },
672 {"vlc:option", SIMPLE_CONTENT, {.smpl = set_option} },
673 {NULL, UNKNOWN_CONTENT, {NULL} }
676 /* read all extension node attributes */
677 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
679 psz_name = xml_ReaderName( p_xml_reader );
680 psz_value = xml_ReaderValue( p_xml_reader );
681 if( !psz_name || !psz_value )
683 msg_Err( p_demux, "invalid xml stream @ <vlc:node>" );
687 /* attribute: title */
688 if( !strcmp( psz_name, "title" ) )
690 resolve_xml_special_chars( psz_value );
691 psz_title = psz_value;
693 /* extension attribute: application */
694 else if( !strcmp( psz_name, "application" ) )
696 psz_application = psz_value;
698 /* unknown attribute */
701 msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element,
709 /* attribute title is mandatory except for <extension> */
710 if( !strcmp( psz_element, "vlc:node" ) )
714 msg_Warn( p_demux, "<vlc:node> requires \"title\" attribute" );
717 p_new_input = input_item_NewWithType( VLC_OBJECT( p_demux ),
718 "vlc://nop", psz_title, 0, NULL, 0, -1,
719 ITEM_TYPE_DIRECTORY );
722 input_item_AddSubItem( p_input_item, p_new_input );
723 p_input_item = p_new_input;
724 b_release_input_item = true;
728 else if( !strcmp( psz_element, "extension" ) )
730 if( !psz_application )
732 msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
735 else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
737 msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
738 free( psz_application );
742 free( psz_application );
744 /* parse the child elements */
745 while( xml_ReaderRead( p_xml_reader ) == 1 )
747 i_node = xml_ReaderNodeType( p_xml_reader );
750 case XML_READER_NONE:
752 case XML_READER_STARTELEM:
753 /* element start tag */
754 psz_name = xml_ReaderName( p_xml_reader );
755 if( !psz_name || !*psz_name )
757 msg_Err( p_demux, "invalid xml stream" );
759 if( b_release_input_item ) vlc_gc_decref( p_new_input );
763 for( p_handler = pl_elements;
764 p_handler->name && strcmp( psz_name, p_handler->name );
766 if( !p_handler->name )
768 msg_Err( p_demux, "unexpected element <%s>", psz_name );
770 if( b_release_input_item ) vlc_gc_decref( p_new_input );
774 /* complex content is parsed in a separate function */
775 if( p_handler->type == COMPLEX_CONTENT )
777 if( p_handler->pf_handler.cmplx( p_demux,
788 if( b_release_input_item ) vlc_gc_decref( p_new_input );
794 case XML_READER_TEXT:
795 /* simple element content */
797 psz_value = xml_ReaderValue( p_xml_reader );
800 msg_Err( p_demux, "invalid xml stream" );
802 if( b_release_input_item ) vlc_gc_decref( p_new_input );
807 case XML_READER_ENDELEM:
808 /* element end tag */
809 psz_name = xml_ReaderName( p_xml_reader );
812 msg_Err( p_demux, "invalid xml stream" );
814 if( b_release_input_item ) vlc_gc_decref( p_new_input );
817 /* leave if the current parent node is terminated */
818 if( !strcmp( psz_name, psz_element ) )
821 if( b_release_input_item ) vlc_gc_decref( p_new_input );
824 /* there MUST have been a start tag for that element name */
825 if( !p_handler || !p_handler->name
826 || strcmp( p_handler->name, psz_name ))
828 msg_Err( p_demux, "there's no open element left for <%s>",
831 if( b_release_input_item ) vlc_gc_decref( p_new_input );
835 /* special tag <vlc:id> */
836 if( !strcmp( p_handler->name, "vlc:id" ) )
838 p_demux->p_sys->i_track_id = atoi( psz_value );
840 else if( p_handler->pf_handler.smpl )
842 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
850 /* unknown/unexpected xml node */
851 msg_Err( p_demux, "unexpected xml node %i", i_node );
853 if( b_release_input_item ) vlc_gc_decref( p_new_input );
858 if( b_release_input_item ) vlc_gc_decref( p_new_input );
863 * \brief parse the extension item node of a XSPF playlist
865 static bool parse_extitem_node COMPLEX_INTERFACE
867 VLC_UNUSED(psz_element);
868 input_item_t *p_new_input = NULL;
871 /* read all extension item attributes */
872 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
874 char *psz_name = xml_ReaderName( p_xml_reader );
875 char *psz_value = xml_ReaderValue( p_xml_reader );
876 if( !psz_name || !psz_value )
878 msg_Err( p_demux, "invalid xml stream @ <vlc:item>" );
883 /* attribute: href */
884 if( !strcmp( psz_name, "tid" ) )
886 i_tid = atoi( psz_value );
888 /* unknown attribute */
890 msg_Warn( p_demux, "invalid <vlc:item> attribute:\"%s\"", psz_name);
896 /* attribute href is mandatory */
899 msg_Warn( p_demux, "<vlc:item> requires \"tid\" attribute" );
903 if( i_tid >= p_demux->p_sys->i_tracklist_entries )
905 msg_Warn( p_demux, "invalid \"tid\" attribute" );
909 p_new_input = p_demux->p_sys->pp_tracklist[ i_tid ];
912 input_item_AddSubItem( p_input_item, p_new_input );
913 vlc_gc_decref( p_new_input );
914 p_demux->p_sys->pp_tracklist[i_tid] = NULL;
917 /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
918 /* (libxml sends NONE) */
919 xml_ReaderRead( p_xml_reader );
925 * \brief skips complex element content that we can't manage
927 static bool skip_element COMPLEX_INTERFACE
929 VLC_UNUSED(p_demux); VLC_UNUSED(p_input_item);
931 while( xml_ReaderRead( p_xml_reader ) == 1 )
933 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
935 char *psz_endname = xml_ReaderName( p_xml_reader );
938 if( !strcmp( psz_element, psz_endname ) )