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 /* FIXME: This is broken. Scheme-relative (//...) locations
542 * and anchors (#...) are not resolved correctly. Also,
543 * host-relative (/...) and directory-relative locations
544 * ("relative path" in vernacular) should be resolved.
545 * Last, psz_base should default to the XSPF resource
546 * location if missing (not the current working directory).
548 if( p_sys->psz_base && !strstr( psz_value, "://" ) )
551 if( asprintf( &psz_tmp, "%s%s", p_sys->psz_base,
556 input_item_SetURI( p_new_input, psz_tmp );
560 input_item_SetURI( p_new_input, psz_value );
561 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 );
584 msg_Err( p_demux, "unexpected end of xml data" );
593 * \brief handles the supported <track> sub-elements
595 static bool set_item_info SIMPLE_INTERFACE
597 /* exit if setting is impossible */
598 if( !psz_name || !psz_value || !p_input )
601 /* re-convert xml special characters inside psz_value */
602 resolve_xml_special_chars( psz_value );
604 /* handle each info element in a separate "if" clause */
605 if( !strcmp( psz_name, "title" ) )
607 input_item_SetTitle( p_input, psz_value );
609 else if( !strcmp( psz_name, "creator" ) )
611 input_item_SetArtist( p_input, psz_value );
613 else if( !strcmp( psz_name, "album" ) )
615 input_item_SetAlbum( p_input, psz_value );
618 else if( !strcmp( psz_name, "trackNum" ) )
620 input_item_SetTrackNum( p_input, psz_value );
622 else if( !strcmp( psz_name, "duration" ) )
624 long i_num = atol( psz_value );
625 input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
627 else if( !strcmp( psz_name, "annotation" ) )
629 input_item_SetDescription( p_input, psz_value );
631 else if( !strcmp( psz_name, "image" ) )
633 input_item_SetArtURL( p_input, psz_value );
639 * \brief handles the <vlc:option> elements
641 static bool set_option SIMPLE_INTERFACE
643 /* exit if setting is impossible */
644 if( !psz_name || !psz_value || !p_input )
647 /* re-convert xml special characters inside psz_value */
648 resolve_xml_special_chars( psz_value );
650 input_item_AddOption( p_input, psz_value, 0 );
656 * \brief parse the extension node of a XSPF playlist
658 static bool parse_extension_node COMPLEX_INTERFACE
660 input_item_t *p_input_item = p_input_node->p_item;
661 char *psz_name = NULL;
662 char *psz_value = NULL;
663 char *psz_title = NULL;
664 char *psz_application = NULL;
666 bool b_release_input_item = false;
667 xml_elem_hnd_t *p_handler = NULL;
668 input_item_t *p_new_input = NULL;
670 xml_elem_hnd_t pl_elements[] =
671 { {"vlc:node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
672 {"vlc:item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
673 {"vlc:id", SIMPLE_CONTENT, {NULL} },
674 {"vlc:option", SIMPLE_CONTENT, {.smpl = set_option} },
675 {NULL, UNKNOWN_CONTENT, {NULL} }
678 /* read all extension node attributes */
679 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
681 psz_name = xml_ReaderName( p_xml_reader );
682 psz_value = xml_ReaderValue( p_xml_reader );
683 if( !psz_name || !psz_value )
685 msg_Err( p_demux, "invalid xml stream @ <vlc:node>" );
689 /* attribute: title */
690 if( !strcmp( psz_name, "title" ) )
692 resolve_xml_special_chars( psz_value );
693 psz_title = psz_value;
695 /* extension attribute: application */
696 else if( !strcmp( psz_name, "application" ) )
698 psz_application = psz_value;
700 /* unknown attribute */
703 msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element,
711 /* attribute title is mandatory except for <extension> */
712 if( !strcmp( psz_element, "vlc:node" ) )
716 msg_Warn( p_demux, "<vlc:node> requires \"title\" attribute" );
719 p_new_input = input_item_NewWithType( VLC_OBJECT( p_demux ),
720 "vlc://nop", psz_title, 0, NULL, 0, -1,
721 ITEM_TYPE_DIRECTORY );
725 input_item_node_AppendItem( p_input_node, p_new_input );
726 p_input_item = p_new_input;
727 b_release_input_item = true;
731 else if( !strcmp( psz_element, "extension" ) )
733 if( !psz_application )
735 msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
738 else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
740 msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
741 free( psz_application );
745 free( psz_application );
748 /* parse the child elements */
749 while( xml_ReaderRead( p_xml_reader ) == 1 )
751 i_node = xml_ReaderNodeType( p_xml_reader );
754 case XML_READER_NONE:
756 case XML_READER_STARTELEM:
757 /* element start tag */
758 psz_name = xml_ReaderName( p_xml_reader );
759 if( !psz_name || !*psz_name )
761 msg_Err( p_demux, "invalid xml stream" );
763 if( b_release_input_item ) vlc_gc_decref( p_new_input );
767 for( p_handler = pl_elements;
768 p_handler->name && strcmp( psz_name, p_handler->name );
770 if( !p_handler->name )
772 msg_Err( p_demux, "unexpected element <%s>", psz_name );
774 if( b_release_input_item ) vlc_gc_decref( p_new_input );
778 /* complex content is parsed in a separate function */
779 if( p_handler->type == COMPLEX_CONTENT )
781 if( p_handler->pf_handler.cmplx( p_demux,
792 if( b_release_input_item ) vlc_gc_decref( p_new_input );
798 case XML_READER_TEXT:
799 /* simple element content */
801 psz_value = xml_ReaderValue( p_xml_reader );
804 msg_Err( p_demux, "invalid xml stream" );
806 if( b_release_input_item ) vlc_gc_decref( p_new_input );
811 case XML_READER_ENDELEM:
812 /* element end tag */
813 psz_name = xml_ReaderName( p_xml_reader );
816 msg_Err( p_demux, "invalid xml stream" );
818 if( b_release_input_item ) vlc_gc_decref( p_new_input );
821 /* leave if the current parent node is terminated */
822 if( !strcmp( psz_name, psz_element ) )
825 if( b_release_input_item ) vlc_gc_decref( p_new_input );
828 /* there MUST have been a start tag for that element name */
829 if( !p_handler || !p_handler->name
830 || strcmp( p_handler->name, psz_name ))
832 msg_Err( p_demux, "there's no open element left for <%s>",
835 if( b_release_input_item ) vlc_gc_decref( p_new_input );
839 /* special tag <vlc:id> */
840 if( !strcmp( p_handler->name, "vlc:id" ) )
842 p_demux->p_sys->i_track_id = atoi( psz_value );
844 else if( p_handler->pf_handler.smpl )
846 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
854 /* unknown/unexpected xml node */
855 msg_Err( p_demux, "unexpected xml node %i", i_node );
857 if( b_release_input_item ) vlc_gc_decref( p_new_input );
862 if( b_release_input_item ) vlc_gc_decref( p_new_input );
867 * \brief parse the extension item node of a XSPF playlist
869 static bool parse_extitem_node COMPLEX_INTERFACE
871 VLC_UNUSED(psz_element);
872 input_item_t *p_new_input = NULL;
875 /* read all extension item attributes */
876 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
878 char *psz_name = xml_ReaderName( p_xml_reader );
879 char *psz_value = xml_ReaderValue( p_xml_reader );
880 if( !psz_name || !psz_value )
882 msg_Err( p_demux, "invalid xml stream @ <vlc:item>" );
887 /* attribute: href */
888 if( !strcmp( psz_name, "tid" ) )
890 i_tid = atoi( psz_value );
892 /* unknown attribute */
894 msg_Warn( p_demux, "invalid <vlc:item> attribute:\"%s\"", psz_name);
900 /* attribute href is mandatory */
903 msg_Warn( p_demux, "<vlc:item> requires \"tid\" attribute" );
907 if( i_tid >= p_demux->p_sys->i_tracklist_entries )
909 msg_Warn( p_demux, "invalid \"tid\" attribute" );
913 p_new_input = p_demux->p_sys->pp_tracklist[ i_tid ];
916 input_item_node_AppendItem( p_input_node, p_new_input );
917 vlc_gc_decref( p_new_input );
918 p_demux->p_sys->pp_tracklist[i_tid] = NULL;
921 /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
922 /* (libxml sends NONE) */
923 xml_ReaderRead( p_xml_reader );
929 * \brief skips complex element content that we can't manage
931 static bool skip_element COMPLEX_INTERFACE
933 VLC_UNUSED(p_demux); VLC_UNUSED(p_input_node);
935 while( xml_ReaderRead( p_xml_reader ) == 1 )
937 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
939 char *psz_endname = xml_ReaderName( p_xml_reader );
942 if( !strcmp( psz_element, psz_endname ) )