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/input.h>
35 #include "vlc_strings.h"
41 playlist_item_t *p_item_in_category;
43 input_item_t **pp_tracklist;
44 int i_tracklist_entries;
48 static int Control( demux_t *, int, va_list );
49 static int Demux( demux_t * );
52 * \brief XSPF submodule initialization function
54 int E_(xspf_import_Activate)( vlc_object_t *p_this )
56 DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xspf", "xspf-open",
57 "using XSPF playlist reader" );
62 * \brief demuxer function for XSPF parsing
64 int Demux( demux_t *p_demux )
66 int i_ret = VLC_SUCCESS;
68 xml_reader_t *p_xml_reader = NULL;
69 char *psz_name = NULL;
71 p_demux->p_sys->p_item_in_category = p_item_in_category;
72 p_demux->p_sys->i_parent_id = i_parent_id;
73 p_demux->p_sys->pp_tracklist = NULL;
74 p_demux->p_sys->i_tracklist_entries = 0;
75 p_demux->p_sys->i_identifier = -1;
77 /* create new xml parser from stream */
78 p_xml = xml_Create( p_demux );
83 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
88 /* locating the root node */
89 if( i_ret == VLC_SUCCESS )
93 if( xml_ReaderRead( p_xml_reader ) != 1 )
95 msg_Err( p_demux, "can't read xml stream" );
98 } while( i_ret == VLC_SUCCESS &&
99 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
101 /* checking root node name */
102 if( i_ret == VLC_SUCCESS )
104 psz_name = xml_ReaderName( p_xml_reader );
105 if( !psz_name || strcmp( psz_name, "playlist" ) )
107 msg_Err( p_demux, "invalid root node name: %s", psz_name );
108 i_ret = VLC_EGENERIC;
113 i_ret = parse_playlist_node( p_demux, p_playlist, p_current, NULL,
114 p_xml_reader, "playlist" );
115 HANDLE_PLAY_AND_RELEASE;
117 xml_ReaderDelete( p_xml, p_xml_reader );
125 /** \brief dummy function for demux callback interface */
126 static int Control( demux_t *p_demux, int i_query, va_list args )
132 * \brief parse the root node of a XSPF playlist
133 * \param p_demux demuxer instance
134 * \param p_playlist playlist instance
135 * \param p_item current playlist item
136 * \param p_input current input item
137 * \param p_xml_reader xml reader instance
138 * \param psz_element name of element to parse
140 static vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
143 char *psz_value=NULL;
144 vlc_bool_t b_version_found = VLC_FALSE;
146 xml_elem_hnd_t *p_handler=NULL;
148 xml_elem_hnd_t pl_elements[] =
149 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
150 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
151 {"annotation", SIMPLE_CONTENT, {NULL} },
152 {"info", SIMPLE_CONTENT, {NULL} },
153 {"location", SIMPLE_CONTENT, {NULL} },
154 {"identifier", SIMPLE_CONTENT, {NULL} },
155 {"image", SIMPLE_CONTENT, {NULL} },
156 {"date", SIMPLE_CONTENT, {NULL} },
157 {"license", SIMPLE_CONTENT, {NULL} },
158 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
159 {"link", SIMPLE_CONTENT, {NULL} },
160 {"meta", SIMPLE_CONTENT, {NULL} },
161 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
162 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
163 {NULL, UNKNOWN_CONTENT, {NULL} }
166 /* read all playlist attributes */
167 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
169 psz_name = xml_ReaderName( p_xml_reader );
170 psz_value = xml_ReaderValue( p_xml_reader );
171 if( !psz_name || !psz_value )
173 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
177 /* attribute: version */
178 if( !strcmp( psz_name, "version" ) )
180 b_version_found = VLC_TRUE;
181 if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
182 msg_Warn( p_demux, "unsupported XSPF version" );
184 /* attribute: xmlns */
185 else if( !strcmp( psz_name, "xmlns" ) )
187 /* unknown attribute */
189 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
193 /* attribute version is mandatory !!! */
194 if( !b_version_found )
195 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
197 /* parse the child elements - we only take care of <trackList> */
198 while( xml_ReaderRead( p_xml_reader ) == 1 )
200 i_node = xml_ReaderNodeType( p_xml_reader );
203 case XML_READER_NONE:
205 case XML_READER_STARTELEM:
206 /* element start tag */
207 psz_name = xml_ReaderName( p_xml_reader );
208 if( !psz_name || !*psz_name )
210 msg_Err( p_demux, "invalid xml stream" );
215 for( p_handler = pl_elements;
216 p_handler->name && strcmp( psz_name, p_handler->name );
218 if( !p_handler->name )
220 msg_Err( p_demux, "unexpected element <%s>", psz_name );
225 /* complex content is parsed in a separate function */
226 if( p_handler->type == COMPLEX_CONTENT )
228 if( p_handler->pf_handler.cmplx( p_demux,
245 case XML_READER_TEXT:
246 /* simple element content */
248 psz_value = xml_ReaderValue( p_xml_reader );
251 msg_Err( p_demux, "invalid xml stream" );
257 case XML_READER_ENDELEM:
258 /* element end tag */
259 psz_name = xml_ReaderName( p_xml_reader );
262 msg_Err( p_demux, "invalid xml stream" );
266 /* leave if the current parent node <playlist> is terminated */
267 if( !strcmp( psz_name, psz_element ) )
272 /* there MUST have been a start tag for that element name */
273 if( !p_handler || !p_handler->name
274 || strcmp( p_handler->name, psz_name ))
276 msg_Err( p_demux, "there's no open element left for <%s>",
282 if( p_handler->pf_handler.smpl )
284 p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
292 /* unknown/unexpected xml node */
293 msg_Err( p_demux, "unexpected xml node %i", i_node );
303 * \brief parses the tracklist node which only may contain <track>s
305 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
311 /* now parse the <track>s */
312 while( xml_ReaderRead( p_xml_reader ) == 1 )
314 i_node = xml_ReaderNodeType( p_xml_reader );
315 if( i_node == XML_READER_STARTELEM )
317 psz_name = xml_ReaderName( p_xml_reader );
320 msg_Err( p_demux, "unexpected end of xml data" );
324 if( strcmp( psz_name, "track") )
326 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
333 /* parse the track data in a separate function */
334 if( parse_track_node( p_demux, p_playlist, p_item, NULL,
335 p_xml_reader,"track" ) == VLC_TRUE )
338 else if( i_node == XML_READER_ENDELEM )
342 /* the <trackList> has to be terminated */
343 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
345 msg_Err( p_demux, "there's a missing </trackList>" );
349 psz_name = xml_ReaderName( p_xml_reader );
350 if( !psz_name || strcmp( psz_name, "trackList" ) )
352 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
358 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
364 * \brief parse one track element
365 * \param COMPLEX_INTERFACE
367 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
369 input_item_t *p_new_input = NULL;
372 char *psz_value=NULL;
373 xml_elem_hnd_t *p_handler=NULL;
375 xml_elem_hnd_t track_elements[] =
376 { {"location", SIMPLE_CONTENT, {NULL} },
377 {"identifier", SIMPLE_CONTENT, {NULL} },
378 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
379 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
380 {"annotation", SIMPLE_CONTENT, {NULL} },
381 {"info", SIMPLE_CONTENT, {NULL} },
382 {"image", SIMPLE_CONTENT, {NULL} },
383 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
384 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
385 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
386 {"link", SIMPLE_CONTENT, {NULL} },
387 {"meta", SIMPLE_CONTENT, {NULL} },
388 {"extension", COMPLEX_CONTENT, {.cmplx = skip_element} },
389 {NULL, UNKNOWN_CONTENT, {NULL} }
392 while( xml_ReaderRead( p_xml_reader ) == 1 )
394 i_node = xml_ReaderNodeType( p_xml_reader );
397 case XML_READER_NONE:
400 case XML_READER_STARTELEM:
401 /* element start tag */
402 psz_name = xml_ReaderName( p_xml_reader );
403 if( !psz_name || !*psz_name )
405 msg_Err( p_demux, "invalid xml stream" );
410 for( p_handler = track_elements;
411 p_handler->name && strcmp( psz_name, p_handler->name );
413 if( !p_handler->name )
415 msg_Err( p_demux, "unexpected element <%s>", psz_name );
420 /* complex content is parsed in a separate function */
421 if( p_handler->type == COMPLEX_CONTENT )
426 "at <%s> level no new item has been allocated",
431 if( p_handler->pf_handler.cmplx( p_demux,
448 case XML_READER_TEXT:
449 /* simple element content */
451 psz_value = xml_ReaderValue( p_xml_reader );
454 msg_Err( p_demux, "invalid xml stream" );
460 case XML_READER_ENDELEM:
461 /* element end tag */
462 psz_name = xml_ReaderName( p_xml_reader );
465 msg_Err( p_demux, "invalid xml stream" );
469 /* leave if the current parent node <track> is terminated */
470 if( !strcmp( psz_name, psz_element ) )
474 playlist_AddWhereverNeeded( p_playlist, p_new_input,
475 p_item, p_demux->p_sys->p_item_in_category,
476 (p_demux->p_sys->i_parent_id >0 ) ? VLC_TRUE:
477 VLC_FALSE, PLAYLIST_APPEND );
478 if( p_demux->p_sys->i_identifier <
479 p_demux->p_sys->i_tracklist_entries )
481 p_demux->p_sys->pp_tracklist[
482 p_demux->p_sys->i_identifier ] = p_new_input;
486 if( p_demux->p_sys->i_identifier >
487 p_demux->p_sys->i_tracklist_entries )
489 p_demux->p_sys->i_tracklist_entries =
490 p_demux->p_sys->i_identifier;
492 INSERT_ELEM( p_demux->p_sys->pp_tracklist,
493 p_demux->p_sys->i_tracklist_entries,
494 p_demux->p_sys->i_tracklist_entries,
499 /* there MUST have been a start tag for that element name */
500 if( !p_handler || !p_handler->name
501 || strcmp( p_handler->name, psz_name ))
503 msg_Err( p_demux, "there's no open element left for <%s>",
509 /* special case: location */
510 if( !strcmp( p_handler->name, "location" ) )
513 /* there MUST NOT be an item */
516 msg_Err( p_demux, "item <%s> already created",
521 psz_uri = unescape_URI_duplicate( psz_value );
525 p_new_input = input_ItemNewExt( p_playlist, psz_uri,
527 p_new_input->p_meta = vlc_meta_New();
529 vlc_input_item_CopyOptions( p_item->p_input, p_new_input );
540 else if( !strcmp( p_handler->name, "identifier" ) )
542 p_demux->p_sys->i_identifier = atoi( psz_value );
546 /* there MUST be an item */
549 msg_Err( p_demux, "item not yet created at <%s>",
554 if( p_handler->pf_handler.smpl )
556 p_handler->pf_handler.smpl( NULL, p_new_input,
567 /* unknown/unexpected xml node */
568 msg_Err( p_demux, "unexpected xml node %i", i_node );
574 msg_Err( p_demux, "unexpected end of xml data" );
580 * \brief handles the supported <track> sub-elements
582 static vlc_bool_t set_item_info SIMPLE_INTERFACE
584 /* exit if setting is impossible */
585 if( !psz_name || !psz_value || !p_input )
589 /* re-convert xml special characters inside psz_value */
590 resolve_xml_special_chars( psz_value );
592 /* handle each info element in a separate "if" clause */
593 if( !strcmp( psz_name, "title" ) )
595 p_input->psz_name = strdup( (char*)psz_value );
597 else if( !strcmp( psz_name, "creator" ) )
599 vlc_meta_SetArtist( p_input->p_meta, psz_value );
601 else if( !strcmp( psz_name, "album" ) )
603 vlc_meta_SetAlbum( p_input->p_meta, psz_value );
606 else if( !strcmp( psz_name, "trackNum" ) )
608 vlc_meta_SetTracknum( p_input->p_meta, psz_value );
610 else if( !strcmp( psz_name, "duration" ) )
612 long i_num = atol( psz_value );
613 p_input->i_duration = i_num*1000;
620 * \brief parse the extension node of a XSPF playlist
622 static vlc_bool_t parse_extension_node COMPLEX_INTERFACE
624 char *psz_name = NULL;
625 char *psz_value = NULL;
626 char *psz_title = NULL;
628 xml_elem_hnd_t *p_handler = NULL;
630 xml_elem_hnd_t pl_elements[] =
631 { {"node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
632 {"item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
633 {NULL, UNKNOWN_CONTENT, {NULL} }
636 /* read all extension node attributes */
637 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
639 psz_name = xml_ReaderName( p_xml_reader );
640 psz_value = xml_ReaderValue( p_xml_reader );
641 if( !psz_name || !psz_value )
643 msg_Err( p_demux, "invalid xml stream @ <node>" );
647 /* attribute: title */
648 if( !strcmp( psz_name, "title" ) )
650 psz_title = unescape_URI_duplicate( psz_value );
652 /* unknown attribute */
654 msg_Warn( p_demux, "invalid <node> attribute:\"%s\"", psz_name);
659 /* attribute title is mandatory except for <extension> */
660 if( !strcmp( psz_element, "node" ) && !psz_title )
662 msg_Warn( p_demux, "<node> requires \"title\" attribute" );
665 if( psz_title ) free( psz_title );
667 /* parse the child elements */
668 while( xml_ReaderRead( p_xml_reader ) == 1 )
670 i_node = xml_ReaderNodeType( p_xml_reader );
673 case XML_READER_NONE:
675 case XML_READER_STARTELEM:
676 /* element start tag */
677 psz_name = xml_ReaderName( p_xml_reader );
678 if( !psz_name || !*psz_name )
680 msg_Err( p_demux, "invalid xml stream" );
685 for( p_handler = pl_elements;
686 p_handler->name && strcmp( psz_name, p_handler->name );
688 if( !p_handler->name )
690 msg_Err( p_demux, "unexpected element <%s>", psz_name );
695 /* complex content is parsed in a separate function */
696 if( p_handler->type == COMPLEX_CONTENT )
698 if( p_handler->pf_handler.cmplx( p_demux,
715 case XML_READER_TEXT:
716 /* simple element content */
718 psz_value = xml_ReaderValue( p_xml_reader );
721 msg_Err( p_demux, "invalid xml stream" );
727 case XML_READER_ENDELEM:
728 /* element end tag */
729 psz_name = xml_ReaderName( p_xml_reader );
732 msg_Err( p_demux, "invalid xml stream" );
736 /* leave if the current parent node is terminated */
737 if( !strcmp( psz_name, psz_element ) )
742 /* there MUST have been a start tag for that element name */
743 if( !p_handler || !p_handler->name
744 || strcmp( p_handler->name, psz_name ))
746 msg_Err( p_demux, "there's no open element left for <%s>",
752 if( p_handler->pf_handler.smpl )
754 p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
762 /* unknown/unexpected xml node */
763 msg_Err( p_demux, "unexpected xml node %i", i_node );
773 * \brief parse the extension item node of a XSPF playlist
775 static vlc_bool_t parse_extitem_node COMPLEX_INTERFACE
777 char *psz_name = NULL;
778 char *psz_value = NULL;
781 /* read all extension item attributes */
782 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
784 psz_name = xml_ReaderName( p_xml_reader );
785 psz_value = xml_ReaderValue( p_xml_reader );
786 if( !psz_name || !psz_value )
788 msg_Err( p_demux, "invalid xml stream @ <item>" );
792 /* attribute: href */
793 if( !strcmp( psz_name, "href" ) )
795 i_href = atoi( psz_value );
797 /* unknown attribute */
799 msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
804 /* attribute href is mandatory */
807 msg_Warn( p_demux, "<item> requires \"href\" attribute" );
811 if( i_href > p_demux->p_sys->i_tracklist_entries )
813 msg_Warn( p_demux, "invalid \"href\" attribute" );
820 * \brief skips complex element content that we can't manage
822 static vlc_bool_t skip_element COMPLEX_INTERFACE
826 while( xml_ReaderRead( p_xml_reader ) == 1 )
828 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
830 psz_endname = xml_ReaderName( p_xml_reader );
833 if( !strcmp( psz_element, psz_endname ) )