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
30 #include <vlc_demux.h>
34 #include "vlc_strings.h"
40 playlist_item_t *p_item_in_category;
41 input_item_t **pp_tracklist;
42 int i_tracklist_entries;
47 static int Control( demux_t *, int, va_list );
48 static int Demux( demux_t * );
51 * \brief XSPF submodule initialization function
53 int E_(Import_xspf)( vlc_object_t *p_this )
55 DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xspf", "xspf-open",
56 "using XSPF playlist reader" );
60 void E_(Close_xspf)( vlc_object_t *p_this )
62 demux_t *p_demux = (demux_t *)p_this;
63 FREENULL( p_demux->p_sys->pp_tracklist );
64 FREENULL( p_demux->p_sys->psz_base );
65 free( p_demux->p_sys );
69 * \brief demuxer function for XSPF parsing
71 int Demux( demux_t *p_demux )
75 xml_reader_t *p_xml_reader = NULL;
76 char *psz_name = NULL;
78 p_demux->p_sys->pp_tracklist = NULL;
79 p_demux->p_sys->i_tracklist_entries = 0;
80 p_demux->p_sys->i_identifier = 0;
81 p_demux->p_sys->psz_base = NULL;
83 /* create new xml parser from stream */
84 p_xml = xml_Create( p_demux );
89 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
94 /* locating the root node */
99 if( xml_ReaderRead( p_xml_reader ) != 1 )
101 msg_Err( p_demux, "can't read xml stream" );
104 } while( i_ret == VLC_SUCCESS &&
105 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
107 /* checking root node name */
110 psz_name = xml_ReaderName( p_xml_reader );
111 if( !psz_name || strcmp( psz_name, "playlist" ) )
113 msg_Err( p_demux, "invalid root node name: %s", psz_name );
120 i_ret = parse_playlist_node( p_demux, p_playlist, p_current_input,
121 p_xml_reader, "playlist" ) ? 0 : -1;
124 for( 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_ItemAddSubItem( p_current_input, p_new_input, VLC_FALSE );
131 vlc_gc_decref( p_new_input );
134 HANDLE_PLAY_AND_RELEASE;
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 )
149 * \brief parse the root node of a XSPF playlist
150 * \param p_demux demuxer instance
151 * \param p_playlist playlist 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 vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
159 char *psz_value=NULL;
160 vlc_bool_t b_version_found = VLC_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>" );
193 /* attribute: version */
194 if( !strcmp( psz_name, "version" ) )
196 b_version_found = VLC_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" ) )
203 else if( !strcmp( psz_name, "xml:base" ) )
205 p_demux->p_sys->psz_base = decode_URI_duplicate( psz_value );
207 /* unknown attribute */
209 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> */
218 while( xml_ReaderRead( p_xml_reader ) == 1 )
220 i_node = xml_ReaderNodeType( p_xml_reader );
223 case XML_READER_NONE:
225 case XML_READER_STARTELEM:
226 /* element start tag */
227 psz_name = xml_ReaderName( p_xml_reader );
228 if( !psz_name || !*psz_name )
230 msg_Err( p_demux, "invalid xml stream" );
235 for( p_handler = pl_elements;
236 p_handler->name && strcmp( psz_name, p_handler->name );
238 if( !p_handler->name )
240 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 )
248 if( p_handler->pf_handler.cmplx( p_demux,
265 case XML_READER_TEXT:
266 /* simple element content */
268 psz_value = xml_ReaderValue( p_xml_reader );
271 msg_Err( p_demux, "invalid xml stream" );
277 case XML_READER_ENDELEM:
278 /* element end tag */
279 psz_name = xml_ReaderName( p_xml_reader );
282 msg_Err( p_demux, "invalid xml stream" );
286 /* leave if the current parent node <playlist> is terminated */
287 if( !strcmp( psz_name, psz_element ) )
292 /* there MUST have been a start tag for that element name */
293 if( !p_handler || !p_handler->name
294 || strcmp( p_handler->name, psz_name ))
296 msg_Err( p_demux, "there's no open element left for <%s>",
302 if( p_handler->pf_handler.smpl )
304 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
312 /* unknown/unexpected xml node */
313 msg_Err( p_demux, "unexpected xml node %i", i_node );
323 * \brief parses the tracklist node which only may contain <track>s
325 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
331 /* now parse the <track>s */
332 while( xml_ReaderRead( p_xml_reader ) == 1 )
334 i_node = xml_ReaderNodeType( p_xml_reader );
335 if( i_node == XML_READER_STARTELEM )
337 psz_name = xml_ReaderName( p_xml_reader );
340 msg_Err( p_demux, "unexpected end of xml data" );
344 if( strcmp( psz_name, "track") )
346 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
353 /* parse the track data in a separate function */
354 if( parse_track_node( p_demux, p_playlist, p_input_item,
355 p_xml_reader,"track" ) == VLC_TRUE )
358 else if( i_node == XML_READER_ENDELEM )
362 /* the <trackList> has to be terminated */
363 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
365 msg_Err( p_demux, "there's a missing </trackList>" );
369 psz_name = xml_ReaderName( p_xml_reader );
370 if( !psz_name || strcmp( psz_name, "trackList" ) )
372 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
378 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
384 * \brief parse one track element
385 * \param COMPLEX_INTERFACE
387 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
389 input_item_t *p_new_input = NULL;
392 char *psz_value=NULL;
393 xml_elem_hnd_t *p_handler=NULL;
395 xml_elem_hnd_t track_elements[] =
396 { {"location", SIMPLE_CONTENT, {NULL} },
397 {"identifier", SIMPLE_CONTENT, {NULL} },
398 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
399 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
400 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
401 {"info", SIMPLE_CONTENT, {NULL} },
402 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
403 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
404 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
405 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
406 {"link", SIMPLE_CONTENT, {NULL} },
407 {"meta", SIMPLE_CONTENT, {NULL} },
408 {"extension", COMPLEX_CONTENT, {.cmplx = skip_element} },
409 {NULL, UNKNOWN_CONTENT, {NULL} }
412 while( xml_ReaderRead( p_xml_reader ) == 1 )
414 i_node = xml_ReaderNodeType( p_xml_reader );
417 case XML_READER_NONE:
420 case XML_READER_STARTELEM:
421 /* element start tag */
422 psz_name = xml_ReaderName( p_xml_reader );
423 if( !psz_name || !*psz_name )
425 msg_Err( p_demux, "invalid xml stream" );
430 for( p_handler = track_elements;
431 p_handler->name && strcmp( psz_name, p_handler->name );
433 if( !p_handler->name )
435 msg_Err( p_demux, "unexpected element <%s>", psz_name );
440 /* complex content is parsed in a separate function */
441 if( p_handler->type == COMPLEX_CONTENT )
446 "at <%s> level no new item has been allocated",
451 if( p_handler->pf_handler.cmplx( p_demux,
468 case XML_READER_TEXT:
469 /* simple element content */
471 psz_value = xml_ReaderValue( p_xml_reader );
474 msg_Err( p_demux, "invalid xml stream" );
480 case XML_READER_ENDELEM:
481 /* element end tag */
482 psz_name = xml_ReaderName( p_xml_reader );
485 msg_Err( p_demux, "invalid xml stream" );
489 /* leave if the current parent node <track> is terminated */
490 if( !strcmp( psz_name, psz_element ) )
493 if( p_demux->p_sys->i_identifier <
494 p_demux->p_sys->i_tracklist_entries )
496 p_demux->p_sys->pp_tracklist[
497 p_demux->p_sys->i_identifier ] = p_new_input;
501 if( p_demux->p_sys->i_identifier >
502 p_demux->p_sys->i_tracklist_entries )
504 p_demux->p_sys->i_tracklist_entries =
505 p_demux->p_sys->i_identifier;
507 INSERT_ELEM( p_demux->p_sys->pp_tracklist,
508 p_demux->p_sys->i_tracklist_entries,
509 p_demux->p_sys->i_tracklist_entries,
514 /* there MUST have been a start tag for that element name */
515 if( !p_handler || !p_handler->name
516 || strcmp( p_handler->name, psz_name ))
518 msg_Err( p_demux, "there's no open element left for <%s>",
524 /* special case: location */
525 if( !strcmp( p_handler->name, "location" ) )
528 /* there MUST NOT be an item */
531 msg_Err( p_demux, "item <%s> already created",
536 psz_uri = decode_URI_duplicate( psz_value );
540 if( p_demux->p_sys->psz_base &&
541 !strstr( psz_uri, "://" ) )
543 char* psz_tmp = malloc(
544 strlen(p_demux->p_sys->psz_base) +
545 strlen(psz_uri) +1 );
548 msg_Err( p_demux, "out of memory");
551 sprintf( psz_tmp, "%s%s",
552 p_demux->p_sys->psz_base, psz_uri );
556 /* FIXME: We are leaking that one */
557 p_new_input = input_ItemNewExt( p_playlist, psz_uri,
560 input_ItemCopyOptions( p_input_item, p_new_input );
571 else if( !strcmp( p_handler->name, "identifier" ) )
573 p_demux->p_sys->i_identifier = atoi( psz_value );
577 /* there MUST be an item */
580 msg_Err( p_demux, "item not yet created at <%s>",
585 if( p_handler->pf_handler.smpl )
587 p_handler->pf_handler.smpl( p_new_input,
598 /* unknown/unexpected xml node */
599 msg_Err( p_demux, "unexpected xml node %i", i_node );
605 msg_Err( p_demux, "unexpected end of xml data" );
611 * \brief handles the supported <track> sub-elements
613 static vlc_bool_t set_item_info SIMPLE_INTERFACE
615 /* exit if setting is impossible */
616 if( !psz_name || !psz_value || !p_input )
620 /* re-convert xml special characters inside psz_value */
621 resolve_xml_special_chars( psz_value );
623 /* handle each info element in a separate "if" clause */
624 if( !strcmp( psz_name, "title" ) )
626 input_item_SetTitle( p_input, psz_value );
628 else if( !strcmp( psz_name, "creator" ) )
630 input_item_SetArtist( p_input, psz_value );
632 else if( !strcmp( psz_name, "album" ) )
634 input_item_SetAlbum( p_input, psz_value );
637 else if( !strcmp( psz_name, "trackNum" ) )
639 input_item_SetTrackNum( p_input, psz_value );
641 else if( !strcmp( psz_name, "duration" ) )
643 long i_num = atol( psz_value );
644 input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
646 else if( !strcmp( psz_name, "annotation" ) )
648 input_item_SetDescription( p_input, psz_value );
650 else if( !strcmp( psz_name, "image" ) )
652 char *psz_uri = decode_URI_duplicate( psz_value );
653 input_item_SetArtURL( p_input, psz_uri );
661 * \brief parse the extension node of a XSPF playlist
663 static vlc_bool_t parse_extension_node COMPLEX_INTERFACE
665 char *psz_name = NULL;
666 char *psz_value = NULL;
667 char *psz_title = NULL;
668 char *psz_application = NULL;
670 xml_elem_hnd_t *p_handler = NULL;
671 input_item_t *p_new_input = NULL;
673 xml_elem_hnd_t pl_elements[] =
674 { {"node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
675 {"item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
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 @ <node>" );
690 /* attribute: title */
691 if( !strcmp( psz_name, "title" ) )
693 resolve_xml_special_chars( psz_value );
694 psz_title = strdup( psz_value );
696 /* extension attribute: application */
697 else if( !strcmp( psz_name, "application" ) )
699 psz_application = strdup( psz_value );
701 /* unknown attribute */
703 msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element, psz_name );
708 /* attribute title is mandatory except for <extension> */
709 if( !strcmp( psz_element, "node" ) )
713 msg_Warn( p_demux, "<node> requires \"title\" attribute" );
716 p_new_input = input_ItemNewWithType( VLC_OBJECT(p_playlist), "vlc:nop",
717 psz_title, 0, NULL, -1, ITEM_TYPE_DIRECTORY );
720 input_ItemAddSubItem( p_input_item, p_new_input, VLC_TRUE );
721 p_input_item = p_new_input;
725 else if( !strcmp( psz_element, "extension" ) )
727 if( !psz_application )
729 msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
732 else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
734 msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
735 free( psz_application );
739 free( psz_application );
741 /* parse the child elements */
742 while( xml_ReaderRead( p_xml_reader ) == 1 )
744 i_node = xml_ReaderNodeType( p_xml_reader );
747 case XML_READER_NONE:
749 case XML_READER_STARTELEM:
750 /* element start tag */
751 psz_name = xml_ReaderName( p_xml_reader );
752 if( !psz_name || !*psz_name )
754 msg_Err( p_demux, "invalid xml stream" );
759 for( p_handler = pl_elements;
760 p_handler->name && strcmp( psz_name, p_handler->name );
762 if( !p_handler->name )
764 msg_Err( p_demux, "unexpected element <%s>", psz_name );
769 /* complex content is parsed in a separate function */
770 if( p_handler->type == COMPLEX_CONTENT )
772 if( p_handler->pf_handler.cmplx( p_demux,
789 case XML_READER_TEXT:
790 /* simple element content */
792 psz_value = xml_ReaderValue( p_xml_reader );
795 msg_Err( p_demux, "invalid xml stream" );
801 case XML_READER_ENDELEM:
802 /* element end tag */
803 psz_name = xml_ReaderName( p_xml_reader );
806 msg_Err( p_demux, "invalid xml stream" );
810 /* leave if the current parent node is terminated */
811 if( !strcmp( psz_name, psz_element ) )
816 /* there MUST have been a start tag for that element name */
817 if( !p_handler || !p_handler->name
818 || strcmp( p_handler->name, psz_name ))
820 msg_Err( p_demux, "there's no open element left for <%s>",
826 if( p_handler->pf_handler.smpl )
828 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
836 /* unknown/unexpected xml node */
837 msg_Err( p_demux, "unexpected xml node %i", i_node );
847 * \brief parse the extension item node of a XSPF playlist
849 static vlc_bool_t parse_extitem_node COMPLEX_INTERFACE
851 input_item_t *p_new_input = NULL;
852 char *psz_name = NULL;
853 char *psz_value = NULL;
856 /* read all extension item attributes */
857 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
859 psz_name = xml_ReaderName( p_xml_reader );
860 psz_value = xml_ReaderValue( p_xml_reader );
861 if( !psz_name || !psz_value )
863 msg_Err( p_demux, "invalid xml stream @ <item>" );
867 /* attribute: href */
868 if( !strcmp( psz_name, "href" ) )
870 i_href = atoi( psz_value );
872 /* unknown attribute */
874 msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
879 /* attribute href is mandatory */
882 msg_Warn( p_demux, "<item> requires \"href\" attribute" );
886 if( i_href >= p_demux->p_sys->i_tracklist_entries )
888 msg_Warn( p_demux, "invalid \"href\" attribute" );
892 p_new_input = p_demux->p_sys->pp_tracklist[ i_href ];
895 input_ItemAddSubItem( p_input_item, p_new_input, VLC_FALSE );
896 p_demux->p_sys->pp_tracklist[i_href] = NULL;
897 vlc_gc_decref( p_new_input );
900 /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
901 /* (libxml sends NONE) */
902 xml_ReaderRead( p_xml_reader );
908 * \brief skips complex element content that we can't manage
910 static vlc_bool_t skip_element COMPLEX_INTERFACE
914 while( xml_ReaderRead( p_xml_reader ) == 1 )
916 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
918 psz_endname = xml_ReaderName( p_xml_reader );
921 if( !strcmp( psz_element, psz_endname ) )