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
34 #include <vlc_demux.h>
38 #include "vlc_strings.h"
44 playlist_item_t *p_item_in_category;
45 input_item_t **pp_tracklist;
46 int i_tracklist_entries;
51 static int Control( demux_t *, int, va_list );
52 static int Demux( demux_t * );
55 * \brief XSPF submodule initialization function
57 int E_(Import_xspf)( vlc_object_t *p_this )
59 DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xspf", "xspf-open",
60 "using XSPF playlist reader" );
64 void E_(Close_xspf)( vlc_object_t *p_this )
66 demux_t *p_demux = (demux_t *)p_this;
67 FREENULL( p_demux->p_sys->pp_tracklist );
68 FREENULL( p_demux->p_sys->psz_base );
69 free( p_demux->p_sys );
73 * \brief demuxer function for XSPF parsing
75 int Demux( demux_t *p_demux )
79 xml_reader_t *p_xml_reader = NULL;
80 char *psz_name = NULL;
82 p_demux->p_sys->pp_tracklist = NULL;
83 p_demux->p_sys->i_tracklist_entries = 0;
84 p_demux->p_sys->i_identifier = 0;
85 p_demux->p_sys->psz_base = NULL;
87 /* create new xml parser from stream */
88 p_xml = xml_Create( p_demux );
93 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
98 /* locating the root node */
103 if( xml_ReaderRead( p_xml_reader ) != 1 )
105 msg_Err( p_demux, "can't read xml stream" );
108 } while( i_ret == VLC_SUCCESS &&
109 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
111 /* checking root node name */
114 psz_name = xml_ReaderName( p_xml_reader );
115 if( !psz_name || strcmp( psz_name, "playlist" ) )
117 msg_Err( p_demux, "invalid root node name: %s", psz_name );
124 i_ret = parse_playlist_node( p_demux, p_playlist, p_current_input,
125 p_xml_reader, "playlist" ) ? 0 : -1;
128 for( i = 0 ; i < p_demux->p_sys->i_tracklist_entries ; i++ )
130 input_item_t *p_new_input = p_demux->p_sys->pp_tracklist[i];
133 input_ItemAddSubItem( p_current_input, p_new_input );
137 HANDLE_PLAY_AND_RELEASE;
139 xml_ReaderDelete( p_xml, p_xml_reader );
142 return i_ret; /* Needed for correct operation of go back */
145 /** \brief dummy function for demux callback interface */
146 static int Control( demux_t *p_demux, int i_query, va_list args )
148 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
153 * \brief parse the root node of a XSPF playlist
154 * \param p_demux demuxer instance
155 * \param p_playlist playlist instance
156 * \param p_input_item current input item
157 * \param p_xml_reader xml reader instance
158 * \param psz_element name of element to parse
160 static vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
163 char *psz_value=NULL;
164 vlc_bool_t b_version_found = VLC_FALSE;
166 xml_elem_hnd_t *p_handler=NULL;
168 xml_elem_hnd_t pl_elements[] =
169 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
170 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
171 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
172 {"info", SIMPLE_CONTENT, {NULL} },
173 {"location", SIMPLE_CONTENT, {NULL} },
174 {"identifier", SIMPLE_CONTENT, {NULL} },
175 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
176 {"date", SIMPLE_CONTENT, {NULL} },
177 {"license", SIMPLE_CONTENT, {NULL} },
178 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
179 {"link", SIMPLE_CONTENT, {NULL} },
180 {"meta", SIMPLE_CONTENT, {NULL} },
181 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
182 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
183 {NULL, UNKNOWN_CONTENT, {NULL} }
186 /* read all playlist attributes */
187 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
189 psz_name = xml_ReaderName( p_xml_reader );
190 psz_value = xml_ReaderValue( p_xml_reader );
191 if( !psz_name || !psz_value )
193 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
197 /* attribute: version */
198 if( !strcmp( psz_name, "version" ) )
200 b_version_found = VLC_TRUE;
201 if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
202 msg_Warn( p_demux, "unsupported XSPF version" );
204 /* attribute: xmlns */
205 else if( !strcmp( psz_name, "xmlns" ) )
207 else if( !strcmp( psz_name, "xml:base" ) )
209 p_demux->p_sys->psz_base = decode_URI_duplicate( psz_value );
211 /* unknown attribute */
213 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
217 /* attribute version is mandatory !!! */
218 if( !b_version_found )
219 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
221 /* parse the child elements - we only take care of <trackList> */
222 while( xml_ReaderRead( p_xml_reader ) == 1 )
224 i_node = xml_ReaderNodeType( p_xml_reader );
227 case XML_READER_NONE:
229 case XML_READER_STARTELEM:
230 /* element start tag */
231 psz_name = xml_ReaderName( p_xml_reader );
232 if( !psz_name || !*psz_name )
234 msg_Err( p_demux, "invalid xml stream" );
239 for( p_handler = pl_elements;
240 p_handler->name && strcmp( psz_name, p_handler->name );
242 if( !p_handler->name )
244 msg_Err( p_demux, "unexpected element <%s>", psz_name );
249 /* complex content is parsed in a separate function */
250 if( p_handler->type == COMPLEX_CONTENT )
252 if( p_handler->pf_handler.cmplx( p_demux,
269 case XML_READER_TEXT:
270 /* simple element content */
272 psz_value = xml_ReaderValue( p_xml_reader );
275 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" );
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>",
306 if( p_handler->pf_handler.smpl )
308 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
316 /* unknown/unexpected xml node */
317 msg_Err( p_demux, "unexpected xml node %i", i_node );
327 * \brief parses the tracklist node which only may contain <track>s
329 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
331 VLC_UNUSED(psz_element);
336 /* now parse the <track>s */
337 while( xml_ReaderRead( p_xml_reader ) == 1 )
339 i_node = xml_ReaderNodeType( p_xml_reader );
340 if( i_node == XML_READER_STARTELEM )
342 psz_name = xml_ReaderName( p_xml_reader );
345 msg_Err( p_demux, "unexpected end of xml data" );
349 if( strcmp( psz_name, "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_playlist, p_input_item,
360 p_xml_reader,"track" ) == VLC_TRUE )
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>" );
374 psz_name = xml_ReaderName( p_xml_reader );
375 if( !psz_name || strcmp( psz_name, "trackList" ) )
377 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
383 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
389 * \brief parse one track element
390 * \param COMPLEX_INTERFACE
392 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
394 input_item_t *p_new_input = NULL;
397 char *psz_value=NULL;
398 xml_elem_hnd_t *p_handler=NULL;
400 xml_elem_hnd_t track_elements[] =
401 { {"location", SIMPLE_CONTENT, {NULL} },
402 {"identifier", SIMPLE_CONTENT, {NULL} },
403 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
404 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
405 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
406 {"info", SIMPLE_CONTENT, {NULL} },
407 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
408 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
409 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
410 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
411 {"link", SIMPLE_CONTENT, {NULL} },
412 {"meta", SIMPLE_CONTENT, {NULL} },
413 {"extension", COMPLEX_CONTENT, {.cmplx = skip_element} },
414 {NULL, UNKNOWN_CONTENT, {NULL} }
417 while( xml_ReaderRead( p_xml_reader ) == 1 )
419 i_node = xml_ReaderNodeType( p_xml_reader );
422 case XML_READER_NONE:
425 case XML_READER_STARTELEM:
426 /* element start tag */
427 psz_name = xml_ReaderName( p_xml_reader );
428 if( !psz_name || !*psz_name )
430 msg_Err( p_demux, "invalid xml stream" );
435 for( p_handler = track_elements;
436 p_handler->name && strcmp( psz_name, p_handler->name );
438 if( !p_handler->name )
440 msg_Err( p_demux, "unexpected element <%s>", psz_name );
445 /* complex content is parsed in a separate function */
446 if( p_handler->type == COMPLEX_CONTENT )
451 "at <%s> level no new item has been allocated",
456 if( 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" );
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 ) )
498 if( p_demux->p_sys->i_identifier <
499 p_demux->p_sys->i_tracklist_entries )
501 p_demux->p_sys->pp_tracklist[
502 p_demux->p_sys->i_identifier ] = p_new_input;
506 if( p_demux->p_sys->i_identifier >
507 p_demux->p_sys->i_tracklist_entries )
509 p_demux->p_sys->i_tracklist_entries =
510 p_demux->p_sys->i_identifier;
512 INSERT_ELEM( p_demux->p_sys->pp_tracklist,
513 p_demux->p_sys->i_tracklist_entries,
514 p_demux->p_sys->i_tracklist_entries,
519 /* there MUST have been a start tag for that element name */
520 if( !p_handler || !p_handler->name
521 || strcmp( p_handler->name, psz_name ))
523 msg_Err( p_demux, "there's no open element left for <%s>",
529 /* special case: location */
530 if( !strcmp( p_handler->name, "location" ) )
533 /* there MUST NOT be an item */
536 msg_Err( p_demux, "item <%s> already created",
541 psz_uri = decode_URI_duplicate( psz_value );
545 if( p_demux->p_sys->psz_base &&
546 !strstr( psz_uri, "://" ) )
548 char* psz_tmp = malloc(
549 strlen(p_demux->p_sys->psz_base) +
550 strlen(psz_uri) +1 );
553 msg_Err( p_demux, "out of memory");
556 sprintf( psz_tmp, "%s%s",
557 p_demux->p_sys->psz_base, psz_uri );
561 /* FIXME: We are leaking that one */
562 p_new_input = input_ItemNewExt( p_playlist, psz_uri,
565 input_ItemCopyOptions( p_input_item, p_new_input );
576 else if( !strcmp( p_handler->name, "identifier" ) )
578 p_demux->p_sys->i_identifier = atoi( psz_value );
582 /* there MUST be an item */
585 msg_Err( p_demux, "item not yet created at <%s>",
590 if( p_handler->pf_handler.smpl )
592 p_handler->pf_handler.smpl( p_new_input,
603 /* unknown/unexpected xml node */
604 msg_Err( p_demux, "unexpected xml node %i", i_node );
610 msg_Err( p_demux, "unexpected end of xml data" );
616 * \brief handles the supported <track> sub-elements
618 static vlc_bool_t set_item_info SIMPLE_INTERFACE
620 /* exit if setting is impossible */
621 if( !psz_name || !psz_value || !p_input )
625 /* re-convert xml special characters inside psz_value */
626 resolve_xml_special_chars( psz_value );
628 /* handle each info element in a separate "if" clause */
629 if( !strcmp( psz_name, "title" ) )
631 input_item_SetTitle( p_input, psz_value );
633 else if( !strcmp( psz_name, "creator" ) )
635 input_item_SetArtist( p_input, psz_value );
637 else if( !strcmp( psz_name, "album" ) )
639 input_item_SetAlbum( p_input, psz_value );
642 else if( !strcmp( psz_name, "trackNum" ) )
644 input_item_SetTrackNum( p_input, psz_value );
646 else if( !strcmp( psz_name, "duration" ) )
648 long i_num = atol( psz_value );
649 input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
651 else if( !strcmp( psz_name, "annotation" ) )
653 input_item_SetDescription( p_input, psz_value );
655 else if( !strcmp( psz_name, "image" ) )
657 char *psz_uri = decode_URI_duplicate( psz_value );
658 input_item_SetArtURL( p_input, psz_uri );
666 * \brief parse the extension node of a XSPF playlist
668 static vlc_bool_t parse_extension_node COMPLEX_INTERFACE
670 char *psz_name = NULL;
671 char *psz_value = NULL;
672 char *psz_title = NULL;
673 char *psz_application = NULL;
675 xml_elem_hnd_t *p_handler = NULL;
676 input_item_t *p_new_input = NULL;
678 xml_elem_hnd_t pl_elements[] =
679 { {"node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
680 {"item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
681 {NULL, UNKNOWN_CONTENT, {NULL} }
684 /* read all extension node attributes */
685 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
687 psz_name = xml_ReaderName( p_xml_reader );
688 psz_value = xml_ReaderValue( p_xml_reader );
689 if( !psz_name || !psz_value )
691 msg_Err( p_demux, "invalid xml stream @ <node>" );
695 /* attribute: title */
696 if( !strcmp( psz_name, "title" ) )
698 resolve_xml_special_chars( psz_value );
699 psz_title = strdup( psz_value );
701 /* extension attribute: application */
702 else if( !strcmp( psz_name, "application" ) )
704 psz_application = strdup( psz_value );
706 /* unknown attribute */
708 msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element, psz_name );
713 /* attribute title is mandatory except for <extension> */
714 if( !strcmp( psz_element, "node" ) )
718 msg_Warn( p_demux, "<node> requires \"title\" attribute" );
721 p_new_input = input_ItemNewWithType( VLC_OBJECT( p_playlist ), "vlc://nop",
722 psz_title, 0, NULL, -1, ITEM_TYPE_DIRECTORY );
725 input_ItemAddSubItem( p_input_item, p_new_input );
726 p_input_item = p_new_input;
730 else if( !strcmp( psz_element, "extension" ) )
732 if( !psz_application )
734 msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
737 else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
739 msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
740 free( psz_application );
744 free( psz_application );
746 /* parse the child elements */
747 while( xml_ReaderRead( p_xml_reader ) == 1 )
749 i_node = xml_ReaderNodeType( p_xml_reader );
752 case XML_READER_NONE:
754 case XML_READER_STARTELEM:
755 /* element start tag */
756 psz_name = xml_ReaderName( p_xml_reader );
757 if( !psz_name || !*psz_name )
759 msg_Err( p_demux, "invalid xml stream" );
764 for( p_handler = pl_elements;
765 p_handler->name && strcmp( psz_name, p_handler->name );
767 if( !p_handler->name )
769 msg_Err( p_demux, "unexpected element <%s>", psz_name );
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,
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" );
806 case XML_READER_ENDELEM:
807 /* element end tag */
808 psz_name = xml_ReaderName( p_xml_reader );
811 msg_Err( p_demux, "invalid xml stream" );
815 /* leave if the current parent node is terminated */
816 if( !strcmp( psz_name, psz_element ) )
821 /* there MUST have been a start tag for that element name */
822 if( !p_handler || !p_handler->name
823 || strcmp( p_handler->name, psz_name ))
825 msg_Err( p_demux, "there's no open element left for <%s>",
831 if( p_handler->pf_handler.smpl )
833 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
841 /* unknown/unexpected xml node */
842 msg_Err( p_demux, "unexpected xml node %i", i_node );
852 * \brief parse the extension item node of a XSPF playlist
854 static vlc_bool_t parse_extitem_node COMPLEX_INTERFACE
856 VLC_UNUSED(p_playlist); VLC_UNUSED(psz_element);
857 input_item_t *p_new_input = NULL;
858 char *psz_name = NULL;
859 char *psz_value = NULL;
862 /* read all extension item attributes */
863 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
865 psz_name = xml_ReaderName( p_xml_reader );
866 psz_value = xml_ReaderValue( p_xml_reader );
867 if( !psz_name || !psz_value )
869 msg_Err( p_demux, "invalid xml stream @ <item>" );
873 /* attribute: href */
874 if( !strcmp( psz_name, "href" ) )
876 i_href = atoi( psz_value );
878 /* unknown attribute */
880 msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
885 /* attribute href is mandatory */
888 msg_Warn( p_demux, "<item> requires \"href\" attribute" );
892 if( i_href >= p_demux->p_sys->i_tracklist_entries )
894 msg_Warn( p_demux, "invalid \"href\" attribute" );
898 p_new_input = p_demux->p_sys->pp_tracklist[ i_href ];
901 input_ItemAddSubItem( p_input_item, p_new_input );
902 p_demux->p_sys->pp_tracklist[i_href] = NULL;
905 /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
906 /* (libxml sends NONE) */
907 xml_ReaderRead( p_xml_reader );
913 * \brief skips complex element content that we can't manage
915 static vlc_bool_t skip_element COMPLEX_INTERFACE
917 VLC_UNUSED(p_demux); VLC_UNUSED(p_playlist); VLC_UNUSED(p_input_item);
920 while( xml_ReaderRead( p_xml_reader ) == 1 )
922 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
924 psz_endname = xml_ReaderName( p_xml_reader );
927 if( !strcmp( psz_element, psz_endname ) )