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;
49 static int Control( demux_t *, int, va_list );
50 static int Demux( demux_t * );
53 * \brief XSPF submodule initialization function
55 int E_(Import_xspf)( vlc_object_t *p_this )
57 DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xspf", "xspf-open",
58 "using XSPF playlist reader" );
62 void E_(Close_xspf)( vlc_object_t *p_this )
64 demux_t *p_demux = (demux_t *)p_this;
65 FREENULL( p_demux->p_sys->psz_base);
66 free( p_demux->p_sys );
70 * \brief demuxer function for XSPF parsing
72 int Demux( demux_t *p_demux )
74 int i_ret = VLC_SUCCESS;
76 xml_reader_t *p_xml_reader = NULL;
77 char *psz_name = NULL;
79 p_demux->p_sys->p_item_in_category = p_item_in_category;
80 p_demux->p_sys->i_parent_id = i_parent_id;
81 p_demux->p_sys->pp_tracklist = NULL;
82 p_demux->p_sys->i_tracklist_entries = 0;
83 p_demux->p_sys->i_identifier = -1;
84 p_demux->p_sys->psz_base = NULL;
86 /* create new xml parser from stream */
87 p_xml = xml_Create( p_demux );
92 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
97 /* locating the root node */
98 if( i_ret == VLC_SUCCESS )
102 if( xml_ReaderRead( p_xml_reader ) != 1 )
104 msg_Err( p_demux, "can't read xml stream" );
105 i_ret = VLC_EGENERIC;
107 } while( i_ret == VLC_SUCCESS &&
108 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
110 /* checking root node name */
111 if( i_ret == VLC_SUCCESS )
113 psz_name = xml_ReaderName( p_xml_reader );
114 if( !psz_name || strcmp( psz_name, "playlist" ) )
116 msg_Err( p_demux, "invalid root node name: %s", psz_name );
117 i_ret = VLC_EGENERIC;
122 i_ret = parse_playlist_node( p_demux, p_playlist, p_current, NULL,
123 p_xml_reader, "playlist" );
124 HANDLE_PLAY_AND_RELEASE;
126 xml_ReaderDelete( p_xml, p_xml_reader );
132 /** \brief dummy function for demux callback interface */
133 static int Control( demux_t *p_demux, int i_query, va_list args )
139 * \brief parse the root node of a XSPF playlist
140 * \param p_demux demuxer instance
141 * \param p_playlist playlist instance
142 * \param p_item current playlist item
143 * \param p_input current input item
144 * \param p_xml_reader xml reader instance
145 * \param psz_element name of element to parse
147 static vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
150 char *psz_value=NULL;
151 vlc_bool_t b_version_found = VLC_FALSE;
153 xml_elem_hnd_t *p_handler=NULL;
155 xml_elem_hnd_t pl_elements[] =
156 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
157 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
158 {"annotation", SIMPLE_CONTENT, {NULL} },
159 {"info", SIMPLE_CONTENT, {NULL} },
160 {"location", SIMPLE_CONTENT, {NULL} },
161 {"identifier", SIMPLE_CONTENT, {NULL} },
162 {"image", SIMPLE_CONTENT, {NULL} },
163 {"date", SIMPLE_CONTENT, {NULL} },
164 {"license", SIMPLE_CONTENT, {NULL} },
165 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
166 {"link", SIMPLE_CONTENT, {NULL} },
167 {"meta", SIMPLE_CONTENT, {NULL} },
168 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
169 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
170 {NULL, UNKNOWN_CONTENT, {NULL} }
173 /* read all playlist attributes */
174 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
176 psz_name = xml_ReaderName( p_xml_reader );
177 psz_value = xml_ReaderValue( p_xml_reader );
178 if( !psz_name || !psz_value )
180 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
184 /* attribute: version */
185 if( !strcmp( psz_name, "version" ) )
187 b_version_found = VLC_TRUE;
188 if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
189 msg_Warn( p_demux, "unsupported XSPF version" );
191 /* attribute: xmlns */
192 else if( !strcmp( psz_name, "xmlns" ) )
194 else if( !strcmp( psz_name, "xml:base" ) )
196 p_demux->p_sys->psz_base = decode_URI_duplicate( psz_value );
198 /* unknown attribute */
200 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
204 /* attribute version is mandatory !!! */
205 if( !b_version_found )
206 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
208 /* parse the child elements - we only take care of <trackList> */
209 while( xml_ReaderRead( p_xml_reader ) == 1 )
211 i_node = xml_ReaderNodeType( p_xml_reader );
214 case XML_READER_NONE:
216 case XML_READER_STARTELEM:
217 /* element start tag */
218 psz_name = xml_ReaderName( p_xml_reader );
219 if( !psz_name || !*psz_name )
221 msg_Err( p_demux, "invalid xml stream" );
226 for( p_handler = pl_elements;
227 p_handler->name && strcmp( psz_name, p_handler->name );
229 if( !p_handler->name )
231 msg_Err( p_demux, "unexpected element <%s>", psz_name );
236 /* complex content is parsed in a separate function */
237 if( p_handler->type == COMPLEX_CONTENT )
239 if( p_handler->pf_handler.cmplx( p_demux,
256 case XML_READER_TEXT:
257 /* simple element content */
259 psz_value = xml_ReaderValue( p_xml_reader );
262 msg_Err( p_demux, "invalid xml stream" );
268 case XML_READER_ENDELEM:
269 /* element end tag */
270 psz_name = xml_ReaderName( p_xml_reader );
273 msg_Err( p_demux, "invalid xml stream" );
277 /* leave if the current parent node <playlist> is terminated */
278 if( !strcmp( psz_name, psz_element ) )
283 /* there MUST have been a start tag for that element name */
284 if( !p_handler || !p_handler->name
285 || strcmp( p_handler->name, psz_name ))
287 msg_Err( p_demux, "there's no open element left for <%s>",
293 if( p_handler->pf_handler.smpl )
295 p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
303 /* unknown/unexpected xml node */
304 msg_Err( p_demux, "unexpected xml node %i", i_node );
314 * \brief parses the tracklist node which only may contain <track>s
316 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
322 /* now parse the <track>s */
323 while( xml_ReaderRead( p_xml_reader ) == 1 )
325 i_node = xml_ReaderNodeType( p_xml_reader );
326 if( i_node == XML_READER_STARTELEM )
328 psz_name = xml_ReaderName( p_xml_reader );
331 msg_Err( p_demux, "unexpected end of xml data" );
335 if( strcmp( psz_name, "track") )
337 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
344 /* parse the track data in a separate function */
345 if( parse_track_node( p_demux, p_playlist, p_item, NULL,
346 p_xml_reader,"track" ) == VLC_TRUE )
349 else if( i_node == XML_READER_ENDELEM )
353 /* the <trackList> has to be terminated */
354 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
356 msg_Err( p_demux, "there's a missing </trackList>" );
360 psz_name = xml_ReaderName( p_xml_reader );
361 if( !psz_name || strcmp( psz_name, "trackList" ) )
363 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
369 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
375 * \brief parse one track element
376 * \param COMPLEX_INTERFACE
378 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
380 input_item_t *p_new_input = NULL;
383 char *psz_value=NULL;
384 xml_elem_hnd_t *p_handler=NULL;
386 xml_elem_hnd_t track_elements[] =
387 { {"location", SIMPLE_CONTENT, {NULL} },
388 {"identifier", SIMPLE_CONTENT, {NULL} },
389 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
390 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
391 {"annotation", SIMPLE_CONTENT, {NULL} },
392 {"info", SIMPLE_CONTENT, {NULL} },
393 {"image", SIMPLE_CONTENT, {NULL} },
394 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
395 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
396 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
397 {"link", SIMPLE_CONTENT, {NULL} },
398 {"meta", SIMPLE_CONTENT, {NULL} },
399 {"extension", COMPLEX_CONTENT, {.cmplx = skip_element} },
400 {NULL, UNKNOWN_CONTENT, {NULL} }
403 while( xml_ReaderRead( p_xml_reader ) == 1 )
405 i_node = xml_ReaderNodeType( p_xml_reader );
408 case XML_READER_NONE:
411 case XML_READER_STARTELEM:
412 /* element start tag */
413 psz_name = xml_ReaderName( p_xml_reader );
414 if( !psz_name || !*psz_name )
416 msg_Err( p_demux, "invalid xml stream" );
421 for( p_handler = track_elements;
422 p_handler->name && strcmp( psz_name, p_handler->name );
424 if( !p_handler->name )
426 msg_Err( p_demux, "unexpected element <%s>", psz_name );
431 /* complex content is parsed in a separate function */
432 if( p_handler->type == COMPLEX_CONTENT )
437 "at <%s> level no new item has been allocated",
442 if( p_handler->pf_handler.cmplx( p_demux,
459 case XML_READER_TEXT:
460 /* simple element content */
462 psz_value = xml_ReaderValue( p_xml_reader );
465 msg_Err( p_demux, "invalid xml stream" );
471 case XML_READER_ENDELEM:
472 /* element end tag */
473 psz_name = xml_ReaderName( p_xml_reader );
476 msg_Err( p_demux, "invalid xml stream" );
480 /* leave if the current parent node <track> is terminated */
481 if( !strcmp( psz_name, psz_element ) )
485 playlist_AddWhereverNeeded( p_playlist, p_new_input,
486 p_item, p_demux->p_sys->p_item_in_category,
487 (p_demux->p_sys->i_parent_id >0 ) ? VLC_TRUE:
488 VLC_FALSE, PLAYLIST_APPEND );
489 if( p_demux->p_sys->i_identifier <
490 p_demux->p_sys->i_tracklist_entries )
492 p_demux->p_sys->pp_tracklist[
493 p_demux->p_sys->i_identifier ] = p_new_input;
497 if( p_demux->p_sys->i_identifier >
498 p_demux->p_sys->i_tracklist_entries )
500 p_demux->p_sys->i_tracklist_entries =
501 p_demux->p_sys->i_identifier;
503 INSERT_ELEM( p_demux->p_sys->pp_tracklist,
504 p_demux->p_sys->i_tracklist_entries,
505 p_demux->p_sys->i_tracklist_entries,
510 /* there MUST have been a start tag for that element name */
511 if( !p_handler || !p_handler->name
512 || strcmp( p_handler->name, psz_name ))
514 msg_Err( p_demux, "there's no open element left for <%s>",
520 /* special case: location */
521 if( !strcmp( p_handler->name, "location" ) )
524 /* there MUST NOT be an item */
527 msg_Err( p_demux, "item <%s> already created",
532 psz_uri = decode_URI_duplicate( psz_value );
536 if( p_demux->p_sys->psz_base &&
537 !strstr( psz_uri, "://" ) )
539 char* psz_tmp = malloc(
540 strlen(p_demux->p_sys->psz_base) +
541 strlen(psz_uri) +1 );
544 msg_Err( p_demux, "out of memory");
547 sprintf( psz_tmp, "%s%s",
548 p_demux->p_sys->psz_base, psz_uri );
552 p_new_input = input_ItemNewExt( p_playlist, psz_uri,
554 p_new_input->p_meta = vlc_meta_New();
556 input_ItemCopyOptions( p_item->p_input, p_new_input );
567 else if( !strcmp( p_handler->name, "identifier" ) )
569 p_demux->p_sys->i_identifier = atoi( psz_value );
573 /* there MUST be an item */
576 msg_Err( p_demux, "item not yet created at <%s>",
581 if( p_handler->pf_handler.smpl )
583 p_handler->pf_handler.smpl( NULL, p_new_input,
594 /* unknown/unexpected xml node */
595 msg_Err( p_demux, "unexpected xml node %i", i_node );
601 msg_Err( p_demux, "unexpected end of xml data" );
607 * \brief handles the supported <track> sub-elements
609 static vlc_bool_t set_item_info SIMPLE_INTERFACE
611 /* exit if setting is impossible */
612 if( !psz_name || !psz_value || !p_input )
616 /* re-convert xml special characters inside psz_value */
617 resolve_xml_special_chars( psz_value );
619 /* handle each info element in a separate "if" clause */
620 if( !strcmp( psz_name, "title" ) )
622 p_input->psz_name = strdup( (char*)psz_value );
624 else if( !strcmp( psz_name, "creator" ) )
626 vlc_meta_SetArtist( p_input->p_meta, psz_value );
628 else if( !strcmp( psz_name, "album" ) )
630 vlc_meta_SetAlbum( p_input->p_meta, psz_value );
633 else if( !strcmp( psz_name, "trackNum" ) )
635 vlc_meta_SetTracknum( p_input->p_meta, psz_value );
637 else if( !strcmp( psz_name, "duration" ) )
639 long i_num = atol( psz_value );
640 p_input->i_duration = i_num*1000;
647 * \brief parse the extension node of a XSPF playlist
649 static vlc_bool_t parse_extension_node COMPLEX_INTERFACE
651 char *psz_name = NULL;
652 char *psz_value = NULL;
653 char *psz_title = NULL;
655 xml_elem_hnd_t *p_handler = NULL;
657 xml_elem_hnd_t pl_elements[] =
658 { {"node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
659 {"item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
660 {NULL, UNKNOWN_CONTENT, {NULL} }
663 /* read all extension node attributes */
664 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
666 psz_name = xml_ReaderName( p_xml_reader );
667 psz_value = xml_ReaderValue( p_xml_reader );
668 if( !psz_name || !psz_value )
670 msg_Err( p_demux, "invalid xml stream @ <node>" );
674 /* attribute: title */
675 if( !strcmp( psz_name, "title" ) )
677 psz_title = unescape_URI_duplicate( psz_value );
679 /* unknown attribute */
681 msg_Warn( p_demux, "invalid <node> attribute:\"%s\"", psz_name);
686 /* attribute title is mandatory except for <extension> */
687 if( !strcmp( psz_element, "node" ) && !psz_title )
689 msg_Warn( p_demux, "<node> requires \"title\" attribute" );
692 if( psz_title ) free( psz_title );
694 /* parse the child elements */
695 while( xml_ReaderRead( p_xml_reader ) == 1 )
697 i_node = xml_ReaderNodeType( p_xml_reader );
700 case XML_READER_NONE:
702 case XML_READER_STARTELEM:
703 /* element start tag */
704 psz_name = xml_ReaderName( p_xml_reader );
705 if( !psz_name || !*psz_name )
707 msg_Err( p_demux, "invalid xml stream" );
712 for( p_handler = pl_elements;
713 p_handler->name && strcmp( psz_name, p_handler->name );
715 if( !p_handler->name )
717 msg_Err( p_demux, "unexpected element <%s>", psz_name );
722 /* complex content is parsed in a separate function */
723 if( p_handler->type == COMPLEX_CONTENT )
725 if( p_handler->pf_handler.cmplx( p_demux,
742 case XML_READER_TEXT:
743 /* simple element content */
745 psz_value = xml_ReaderValue( p_xml_reader );
748 msg_Err( p_demux, "invalid xml stream" );
754 case XML_READER_ENDELEM:
755 /* element end tag */
756 psz_name = xml_ReaderName( p_xml_reader );
759 msg_Err( p_demux, "invalid xml stream" );
763 /* leave if the current parent node is terminated */
764 if( !strcmp( psz_name, psz_element ) )
769 /* there MUST have been a start tag for that element name */
770 if( !p_handler || !p_handler->name
771 || strcmp( p_handler->name, psz_name ))
773 msg_Err( p_demux, "there's no open element left for <%s>",
779 if( p_handler->pf_handler.smpl )
781 p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
789 /* unknown/unexpected xml node */
790 msg_Err( p_demux, "unexpected xml node %i", i_node );
800 * \brief parse the extension item node of a XSPF playlist
802 static vlc_bool_t parse_extitem_node COMPLEX_INTERFACE
804 char *psz_name = NULL;
805 char *psz_value = NULL;
808 /* read all extension item attributes */
809 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
811 psz_name = xml_ReaderName( p_xml_reader );
812 psz_value = xml_ReaderValue( p_xml_reader );
813 if( !psz_name || !psz_value )
815 msg_Err( p_demux, "invalid xml stream @ <item>" );
819 /* attribute: href */
820 if( !strcmp( psz_name, "href" ) )
822 i_href = atoi( psz_value );
824 /* unknown attribute */
826 msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
831 /* attribute href is mandatory */
834 msg_Warn( p_demux, "<item> requires \"href\" attribute" );
838 if( i_href > p_demux->p_sys->i_tracklist_entries )
840 msg_Warn( p_demux, "invalid \"href\" attribute" );
847 * \brief skips complex element content that we can't manage
849 static vlc_bool_t skip_element COMPLEX_INTERFACE
853 while( xml_ReaderRead( p_xml_reader ) == 1 )
855 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
857 psz_endname = xml_ReaderName( p_xml_reader );
860 if( !strcmp( psz_element, psz_endname ) )