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 */
99 if( xml_ReaderRead( p_xml_reader ) != 1 )
101 msg_Err( p_demux, "can't read xml stream" );
104 } while( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
106 /* checking root node name */
107 psz_name = xml_ReaderName( p_xml_reader );
108 if( !psz_name || strcmp( psz_name, "playlist" ) )
110 msg_Err( p_demux, "invalid root node name: %s", psz_name );
116 input_item_node_t *p_subitems =
117 input_item_node_Create( p_current_input );
119 i_ret = parse_playlist_node( p_demux, p_subitems,
120 p_xml_reader, "playlist" ) ? 0 : -1;
122 for( int i = 0 ; i < p_demux->p_sys->i_tracklist_entries ; i++ )
124 input_item_t *p_new_input = p_demux->p_sys->pp_tracklist[i];
127 input_item_node_AppendItem( p_subitems, p_new_input );
131 input_item_node_PostAndDelete( p_subitems );
134 vlc_gc_decref(p_current_input);
136 xml_ReaderDelete( p_xml_reader );
137 return i_ret; /* Needed for correct operation of go back */
140 /** \brief dummy function for demux callback interface */
141 static int Control( demux_t *p_demux, int i_query, va_list args )
143 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
148 * \brief parse the root node of a XSPF playlist
149 * \param p_demux demuxer instance
150 * \param p_input_item current input item
151 * \param p_xml_reader xml reader instance
152 * \param psz_element name of element to parse
154 static bool parse_playlist_node COMPLEX_INTERFACE
156 input_item_t *p_input_item = p_input_node->p_item;
157 char *psz_name = NULL;
158 char *psz_value = NULL;
159 bool b_version_found = false;
162 xml_elem_hnd_t *p_handler = NULL;
164 xml_elem_hnd_t pl_elements[] =
165 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
166 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
167 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
168 {"info", SIMPLE_CONTENT, {NULL} },
169 {"location", SIMPLE_CONTENT, {NULL} },
170 {"identifier", SIMPLE_CONTENT, {NULL} },
171 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
172 {"date", SIMPLE_CONTENT, {NULL} },
173 {"license", SIMPLE_CONTENT, {NULL} },
174 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
175 {"link", SIMPLE_CONTENT, {NULL} },
176 {"meta", SIMPLE_CONTENT, {NULL} },
177 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
178 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
179 {NULL, UNKNOWN_CONTENT, {NULL} }
182 /* read all playlist attributes */
183 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
185 psz_name = xml_ReaderName( p_xml_reader );
186 psz_value = xml_ReaderValue( p_xml_reader );
187 if( !psz_name || !psz_value )
189 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
192 /* attribute: version */
193 if( !strcmp( psz_name, "version" ) )
195 b_version_found = true;
196 if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
197 msg_Warn( p_demux, "unsupported XSPF version" );
199 /* attribute: xmlns */
200 else if( !strcmp( psz_name, "xmlns" ) || !strcmp( psz_name, "xmlns:vlc" ) )
202 else if( !strcmp( psz_name, "xml:base" ) )
204 p_demux->p_sys->psz_base = strdup( psz_value );
206 /* unknown attribute */
208 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
213 /* attribute version is mandatory !!! */
214 if( !b_version_found )
215 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
217 /* parse the child elements - we only take care of <trackList> */
220 while( xml_ReaderRead( p_xml_reader ) == 1 )
222 i_node = xml_ReaderNodeType( p_xml_reader );
225 case XML_READER_NONE:
227 case XML_READER_STARTELEM:
228 /* element start tag */
229 psz_name = xml_ReaderName( p_xml_reader );
230 if( !psz_name || !*psz_name )
232 msg_Err( p_demux, "invalid xml stream" );
236 for( p_handler = pl_elements;
237 p_handler->name && strcmp( psz_name, p_handler->name );
239 if( !p_handler->name )
241 msg_Err( p_demux, "unexpected element <%s>", psz_name );
245 /* complex content is parsed in a separate function */
246 if( p_handler->type == COMPLEX_CONTENT )
249 if( p_handler->pf_handler.cmplx( p_demux,
263 case XML_READER_TEXT:
264 /* simple element content */
266 psz_value = xml_ReaderValue( p_xml_reader );
269 msg_Err( p_demux, "invalid xml stream" );
274 case XML_READER_ENDELEM:
275 /* element end tag */
276 psz_name = xml_ReaderName( p_xml_reader );
279 msg_Err( p_demux, "invalid xml stream" );
282 /* leave if the current parent node <playlist> is terminated */
283 if( !strcmp( psz_name, psz_element ) )
288 /* there MUST have been a start tag for that element name */
289 if( !p_handler || !p_handler->name
290 || strcmp( p_handler->name, psz_name ))
292 msg_Err( p_demux, "there's no open element left for <%s>",
297 if( p_handler->pf_handler.smpl )
299 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
307 /* unknown/unexpected xml node */
308 msg_Err( p_demux, "unexpected xml node %i", i_node );
320 * \brief parses the tracklist node which only may contain <track>s
322 static bool parse_tracklist_node COMPLEX_INTERFACE
324 VLC_UNUSED(psz_element);
328 /* now parse the <track>s */
329 while( xml_ReaderRead( p_xml_reader ) == 1 )
331 int i_node = xml_ReaderNodeType( p_xml_reader );
332 if( i_node == XML_READER_STARTELEM )
334 char *psz_eltname = xml_ReaderName( p_xml_reader );
337 msg_Err( p_demux, "unexpected end of xml data" );
341 if( strcmp( psz_eltname, "track") )
343 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
350 /* parse the track data in a separate function */
351 if( parse_track_node( p_demux, p_input_node,
352 p_xml_reader, "track" ) )
355 else if( i_node == XML_READER_ENDELEM )
359 /* the <trackList> has to be terminated */
360 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
362 msg_Err( p_demux, "there's a missing </trackList>" );
365 psz_name = xml_ReaderName( p_xml_reader );
366 if( !psz_name || strcmp( psz_name, "trackList" ) )
368 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
374 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
379 * \brief parse one track element
380 * \param COMPLEX_INTERFACE
382 static bool parse_track_node COMPLEX_INTERFACE
384 input_item_t *p_input_item = p_input_node->p_item;
385 char *psz_name = NULL;
386 char *psz_value = NULL;
387 xml_elem_hnd_t *p_handler = NULL;
388 demux_sys_t *p_sys = p_demux->p_sys;
391 xml_elem_hnd_t track_elements[] =
392 { {"location", SIMPLE_CONTENT, {NULL} },
393 {"identifier", SIMPLE_CONTENT, {NULL} },
394 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
395 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
396 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
397 {"info", SIMPLE_CONTENT, {NULL} },
398 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
399 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
400 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
401 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
402 {"link", SIMPLE_CONTENT, {NULL} },
403 {"meta", SIMPLE_CONTENT, {NULL} },
404 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
405 {NULL, UNKNOWN_CONTENT, {NULL} }
408 input_item_t *p_new_input = input_item_New( p_demux, NULL, NULL );
409 input_item_node_t *p_new_node = input_item_node_Create( p_new_input );
413 /* malloc has failed for input_item_New, so bailout early */
417 /* reset i_track_id */
418 p_sys->i_track_id = -1;
420 while( xml_ReaderRead( p_xml_reader ) == 1 )
422 int i_node = xml_ReaderNodeType( p_xml_reader );
425 case XML_READER_NONE:
428 case XML_READER_STARTELEM:
429 /* element start tag */
430 psz_name = xml_ReaderName( p_xml_reader );
431 if( !psz_name || !*psz_name )
433 msg_Err( p_demux, "invalid xml stream" );
437 for( p_handler = track_elements;
438 p_handler->name && strcmp( psz_name, p_handler->name );
440 if( !p_handler->name )
442 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 )
451 bool b_res = p_handler->pf_handler.cmplx( p_demux,
466 case XML_READER_TEXT:
467 /* simple element content */
469 psz_value = xml_ReaderValue( p_xml_reader );
472 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" );
486 /* leave if the current parent node <track> is terminated */
487 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_sys->i_track_id < 0 )
502 input_item_node_AppendNode( p_input_node, p_new_node );
503 vlc_gc_decref( p_new_input );
507 if( p_sys->i_track_id >= p_sys->i_tracklist_entries )
510 pp = realloc( p_sys->pp_tracklist,
511 (p_sys->i_track_id + 1) * sizeof(*pp) );
514 p_sys->pp_tracklist = pp;
515 while( p_sys->i_track_id >= p_sys->i_tracklist_entries )
516 pp[p_sys->i_tracklist_entries++] = NULL;
519 p_sys->pp_tracklist[ p_sys->i_track_id ] = p_new_input;
522 /* there MUST have been a start tag for that element name */
523 if( !p_handler || !p_handler->name
524 || strcmp( p_handler->name, psz_name ))
526 msg_Err( p_demux, "there's no open element left for <%s>",
531 /* special case: location */
532 if( !strcmp( p_handler->name, "location" ) )
534 if( psz_value == NULL )
535 input_item_SetURI( p_new_input, "vlc://nop" );
537 /* FIXME: This is broken. Scheme-relative (//...) locations
538 * and anchors (#...) are not resolved correctly. Also,
539 * host-relative (/...) and directory-relative locations
540 * ("relative path" in vernacular) should be resolved.
541 * Last, psz_base should default to the XSPF resource
542 * location if missing (not the current working directory).
544 if( p_sys->psz_base && !strstr( psz_value, "://" ) )
547 if( asprintf( &psz_tmp, "%s%s", p_sys->psz_base,
552 input_item_SetURI( p_new_input, psz_tmp );
556 input_item_SetURI( p_new_input, psz_value );
557 input_item_CopyOptions( p_input_item, p_new_input );
561 /* there MUST be an item */
562 if( p_handler->pf_handler.smpl )
564 p_handler->pf_handler.smpl( p_new_input,
575 /* unknown/unexpected xml node */
576 msg_Err( p_demux, "unexpected xml node %i", i_node );
580 msg_Err( p_demux, "unexpected end of xml data" );
589 * \brief handles the supported <track> sub-elements
591 static bool set_item_info SIMPLE_INTERFACE
593 /* exit if setting is impossible */
594 if( !psz_name || !psz_value || !p_input )
597 /* re-convert xml special characters inside psz_value */
598 resolve_xml_special_chars( psz_value );
600 /* handle each info element in a separate "if" clause */
601 if( !strcmp( psz_name, "title" ) )
603 input_item_SetTitle( p_input, psz_value );
605 else if( !strcmp( psz_name, "creator" ) )
607 input_item_SetArtist( p_input, psz_value );
609 else if( !strcmp( psz_name, "album" ) )
611 input_item_SetAlbum( p_input, psz_value );
614 else if( !strcmp( psz_name, "trackNum" ) )
616 input_item_SetTrackNum( p_input, psz_value );
618 else if( !strcmp( psz_name, "duration" ) )
620 long i_num = atol( psz_value );
621 input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
623 else if( !strcmp( psz_name, "annotation" ) )
625 input_item_SetDescription( p_input, psz_value );
627 else if( !strcmp( psz_name, "image" ) )
629 input_item_SetArtURL( p_input, psz_value );
635 * \brief handles the <vlc:option> elements
637 static bool set_option SIMPLE_INTERFACE
639 /* exit if setting is impossible */
640 if( !psz_name || !psz_value || !p_input )
643 /* re-convert xml special characters inside psz_value */
644 resolve_xml_special_chars( psz_value );
646 input_item_AddOption( p_input, psz_value, 0 );
652 * \brief parse the extension node of a XSPF playlist
654 static bool parse_extension_node COMPLEX_INTERFACE
656 input_item_t *p_input_item = p_input_node->p_item;
657 char *psz_name = NULL;
658 char *psz_value = NULL;
659 char *psz_title = NULL;
660 char *psz_application = NULL;
662 bool b_release_input_item = false;
663 xml_elem_hnd_t *p_handler = NULL;
664 input_item_t *p_new_input = NULL;
666 xml_elem_hnd_t pl_elements[] =
667 { {"vlc:node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
668 {"vlc:item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
669 {"vlc:id", SIMPLE_CONTENT, {NULL} },
670 {"vlc:option", SIMPLE_CONTENT, {.smpl = set_option} },
671 {NULL, UNKNOWN_CONTENT, {NULL} }
674 /* read all extension node attributes */
675 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
677 psz_name = xml_ReaderName( p_xml_reader );
678 psz_value = xml_ReaderValue( p_xml_reader );
679 if( !psz_name || !psz_value )
681 msg_Err( p_demux, "invalid xml stream @ <vlc:node>" );
685 /* attribute: title */
686 if( !strcmp( psz_name, "title" ) )
688 resolve_xml_special_chars( psz_value );
689 psz_title = psz_value;
691 /* extension attribute: application */
692 else if( !strcmp( psz_name, "application" ) )
694 psz_application = psz_value;
696 /* unknown attribute */
699 msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element,
707 /* attribute title is mandatory except for <extension> */
708 if( !strcmp( psz_element, "vlc:node" ) )
712 msg_Warn( p_demux, "<vlc:node> requires \"title\" attribute" );
715 p_new_input = input_item_NewWithType( VLC_OBJECT( p_demux ),
716 "vlc://nop", psz_title, 0, NULL, 0, -1,
717 ITEM_TYPE_DIRECTORY );
721 input_item_node_AppendItem( p_input_node, p_new_input );
722 p_input_item = p_new_input;
723 b_release_input_item = true;
727 else if( !strcmp( psz_element, "extension" ) )
729 if( !psz_application )
731 msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
734 /* Skip the extension if the application is not vlc
735 This will skip all children of the current node */
736 else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
738 msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
739 free( psz_application );
740 /* Skip all children */
741 while( xml_ReaderRead( p_xml_reader ) == 1 )
743 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
745 char *psz_name = xml_ReaderName( p_xml_reader );
746 if( !strcmp( psz_name, "extension" ) )
751 msg_Dbg( p_demux, "\tskipping \"%s\" extension child", psz_name );
758 free( psz_application );
761 /* parse the child elements */
762 while( xml_ReaderRead( p_xml_reader ) == 1 )
764 i_node = xml_ReaderNodeType( p_xml_reader );
767 case XML_READER_NONE:
769 case XML_READER_STARTELEM:
770 /* element start tag */
771 psz_name = xml_ReaderName( p_xml_reader );
772 if( !psz_name || !*psz_name )
774 msg_Err( p_demux, "invalid xml stream" );
776 if( b_release_input_item ) vlc_gc_decref( p_new_input );
780 for( p_handler = pl_elements;
781 p_handler->name && strcmp( psz_name, p_handler->name );
783 if( !p_handler->name )
785 msg_Err( p_demux, "unexpected element <%s>", psz_name );
787 if( b_release_input_item ) vlc_gc_decref( p_new_input );
791 /* complex content is parsed in a separate function */
792 if( p_handler->type == COMPLEX_CONTENT )
794 if( p_handler->pf_handler.cmplx( p_demux,
805 if( b_release_input_item ) vlc_gc_decref( p_new_input );
811 case XML_READER_TEXT:
812 /* simple element content */
814 psz_value = xml_ReaderValue( p_xml_reader );
817 msg_Err( p_demux, "invalid xml stream" );
819 if( b_release_input_item ) vlc_gc_decref( p_new_input );
824 case XML_READER_ENDELEM:
825 /* element end tag */
826 psz_name = xml_ReaderName( p_xml_reader );
829 msg_Err( p_demux, "invalid xml stream" );
831 if( b_release_input_item ) vlc_gc_decref( p_new_input );
834 /* leave if the current parent node is terminated */
835 if( !strcmp( psz_name, psz_element ) )
838 if( b_release_input_item ) vlc_gc_decref( p_new_input );
841 /* there MUST have been a start tag for that element name */
842 if( !p_handler || !p_handler->name
843 || strcmp( p_handler->name, psz_name ))
845 msg_Err( p_demux, "there's no open element left for <%s>",
848 if( b_release_input_item ) vlc_gc_decref( p_new_input );
852 /* special tag <vlc:id> */
853 if( !strcmp( p_handler->name, "vlc:id" ) )
855 p_demux->p_sys->i_track_id = atoi( psz_value );
857 else if( p_handler->pf_handler.smpl )
859 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
867 /* unknown/unexpected xml node */
868 msg_Err( p_demux, "unexpected xml node %i", i_node );
870 if( b_release_input_item ) vlc_gc_decref( p_new_input );
875 if( b_release_input_item ) vlc_gc_decref( p_new_input );
880 * \brief parse the extension item node of a XSPF playlist
882 static bool parse_extitem_node COMPLEX_INTERFACE
884 VLC_UNUSED(psz_element);
885 input_item_t *p_new_input = NULL;
888 /* read all extension item attributes */
889 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
891 char *psz_name = xml_ReaderName( p_xml_reader );
892 char *psz_value = xml_ReaderValue( p_xml_reader );
893 if( !psz_name || !psz_value )
895 msg_Err( p_demux, "invalid xml stream @ <vlc:item>" );
900 /* attribute: href */
901 if( !strcmp( psz_name, "tid" ) )
903 i_tid = atoi( psz_value );
905 /* unknown attribute */
907 msg_Warn( p_demux, "invalid <vlc:item> attribute:\"%s\"", psz_name);
913 /* attribute href is mandatory */
916 msg_Warn( p_demux, "<vlc:item> requires \"tid\" attribute" );
920 if( i_tid >= p_demux->p_sys->i_tracklist_entries )
922 msg_Warn( p_demux, "invalid \"tid\" attribute" );
926 p_new_input = p_demux->p_sys->pp_tracklist[ i_tid ];
929 input_item_node_AppendItem( p_input_node, p_new_input );
930 vlc_gc_decref( p_new_input );
931 p_demux->p_sys->pp_tracklist[i_tid] = NULL;
934 /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
935 /* (libxml sends NONE) */
936 xml_ReaderRead( p_xml_reader );
942 * \brief skips complex element content that we can't manage
944 static bool skip_element COMPLEX_INTERFACE
946 VLC_UNUSED(p_demux); VLC_UNUSED(p_input_node);
948 while( xml_ReaderRead( p_xml_reader ) == 1 )
950 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
952 char *psz_endname = xml_ReaderName( p_xml_reader );
955 if( !strcmp( psz_element, psz_endname ) )