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"
40 playlist_item_t *p_item_in_category;
42 input_item_t **pp_tracklist;
43 int i_tracklist_entries;
48 * \brief XSPF submodule initialization function
50 int E_(xspf_import_Activate)( vlc_object_t *p_this )
52 demux_t *p_demux = (demux_t *)p_this;
55 psz_ext = strrchr( p_demux->psz_path, '.' );
57 if( ( psz_ext && !strcasecmp( psz_ext, ".xspf") ) ||
58 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "xspf-open") ) )
66 msg_Dbg( p_demux, "using xspf playlist import");
68 p_demux->p_sys = (demux_sys_t*)malloc(sizeof(demux_sys_t ) );
70 p_demux->pf_control = xspf_import_Control;
71 p_demux->pf_demux = xspf_import_Demux;
77 * \brief demuxer function for XSPF parsing
79 int xspf_import_Demux( demux_t *p_demux )
81 int i_ret = VLC_SUCCESS;
83 xml_reader_t *p_xml_reader = NULL;
84 char *psz_name = NULL;
86 p_demux->p_sys->p_item_in_category = p_item_in_category;
87 p_demux->p_sys->i_parent_id = i_parent_id;
88 p_demux->p_sys->pp_tracklist = NULL;
89 p_demux->p_sys->i_tracklist_entries = 0;
90 p_demux->p_sys->i_identifier = -1;
92 /* create new xml parser from stream */
93 p_xml = xml_Create( p_demux );
98 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
100 i_ret = VLC_EGENERIC;
103 /* locating the root node */
104 if( i_ret == VLC_SUCCESS )
108 if( xml_ReaderRead( p_xml_reader ) != 1 )
110 msg_Err( p_demux, "can't read xml stream" );
111 i_ret = VLC_EGENERIC;
113 } while( i_ret == VLC_SUCCESS &&
114 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
116 /* checking root node name */
117 if( i_ret == VLC_SUCCESS )
119 psz_name = xml_ReaderName( p_xml_reader );
120 if( !psz_name || strcmp( psz_name, "playlist" ) )
122 msg_Err( p_demux, "invalid root node name: %s", psz_name );
123 i_ret = VLC_EGENERIC;
128 i_ret = parse_playlist_node( p_demux, p_playlist, p_current, NULL,
129 p_xml_reader, "playlist" );
130 HANDLE_PLAY_AND_RELEASE;
132 xml_ReaderDelete( p_xml, p_xml_reader );
140 /** \brief dummy function for demux callback interface */
141 int xspf_import_Control( demux_t *p_demux, int i_query, va_list args )
147 * \brief parse the root node of a XSPF playlist
148 * \param p_demux demuxer instance
149 * \param p_playlist playlist instance
150 * \param p_item current playlist item
151 * \param p_input current input item
152 * \param p_xml_reader xml reader instance
153 * \param psz_element name of element to parse
155 static vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
158 char *psz_value=NULL;
159 vlc_bool_t b_version_found = VLC_FALSE;
161 xml_elem_hnd_t *p_handler=NULL;
163 xml_elem_hnd_t pl_elements[] =
164 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
165 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
166 {"annotation", SIMPLE_CONTENT, {NULL} },
167 {"info", SIMPLE_CONTENT, {NULL} },
168 {"location", SIMPLE_CONTENT, {NULL} },
169 {"identifier", SIMPLE_CONTENT, {NULL} },
170 {"image", SIMPLE_CONTENT, {NULL} },
171 {"date", SIMPLE_CONTENT, {NULL} },
172 {"license", SIMPLE_CONTENT, {NULL} },
173 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
174 {"link", SIMPLE_CONTENT, {NULL} },
175 {"meta", SIMPLE_CONTENT, {NULL} },
176 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
177 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
178 {NULL, UNKNOWN_CONTENT, {NULL} }
181 /* read all playlist attributes */
182 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
184 psz_name = xml_ReaderName( p_xml_reader );
185 psz_value = xml_ReaderValue( p_xml_reader );
186 if( !psz_name || !psz_value )
188 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
192 /* attribute: version */
193 if( !strcmp( psz_name, "version" ) )
195 b_version_found = VLC_TRUE;
196 if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
197 msg_Warn( p_demux, "unsupported XSPF version" );
199 /* attribute: xmlns */
200 else if( !strcmp( psz_name, "xmlns" ) )
202 /* unknown attribute */
204 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
208 /* attribute version is mandatory !!! */
209 if( !b_version_found )
210 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
212 /* parse the child elements - we only take care of <trackList> */
213 while( xml_ReaderRead( p_xml_reader ) == 1 )
215 i_node = xml_ReaderNodeType( p_xml_reader );
218 case XML_READER_NONE:
220 case XML_READER_STARTELEM:
221 /* element start tag */
222 psz_name = xml_ReaderName( p_xml_reader );
223 if( !psz_name || !*psz_name )
225 msg_Err( p_demux, "invalid xml stream" );
230 for( p_handler = pl_elements;
231 p_handler->name && strcmp( psz_name, p_handler->name );
233 if( !p_handler->name )
235 msg_Err( p_demux, "unexpected element <%s>", psz_name );
240 /* complex content is parsed in a separate function */
241 if( p_handler->type == COMPLEX_CONTENT )
243 if( p_handler->pf_handler.cmplx( p_demux,
260 case XML_READER_TEXT:
261 /* simple element content */
263 psz_value = xml_ReaderValue( p_xml_reader );
266 msg_Err( p_demux, "invalid xml stream" );
272 case XML_READER_ENDELEM:
273 /* element end tag */
274 psz_name = xml_ReaderName( p_xml_reader );
277 msg_Err( p_demux, "invalid xml stream" );
281 /* leave if the current parent node <playlist> is terminated */
282 if( !strcmp( psz_name, psz_element ) )
287 /* there MUST have been a start tag for that element name */
288 if( !p_handler || !p_handler->name
289 || strcmp( p_handler->name, psz_name ))
291 msg_Err( p_demux, "there's no open element left for <%s>",
297 if( p_handler->pf_handler.smpl )
299 p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
307 /* unknown/unexpected xml node */
308 msg_Err( p_demux, "unexpected xml node %i", i_node );
318 * \brief parses the tracklist node which only may contain <track>s
320 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
326 /* now parse the <track>s */
327 while( xml_ReaderRead( p_xml_reader ) == 1 )
329 i_node = xml_ReaderNodeType( p_xml_reader );
330 if( i_node == XML_READER_STARTELEM )
332 psz_name = xml_ReaderName( p_xml_reader );
335 msg_Err( p_demux, "unexpected end of xml data" );
339 if( strcmp( psz_name, "track") )
341 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
348 /* parse the track data in a separate function */
349 if( parse_track_node( p_demux, p_playlist, p_item, NULL,
350 p_xml_reader,"track" ) == VLC_TRUE )
353 else if( i_node == XML_READER_ENDELEM )
357 /* the <trackList> has to be terminated */
358 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
360 msg_Err( p_demux, "there's a missing </trackList>" );
364 psz_name = xml_ReaderName( p_xml_reader );
365 if( !psz_name || strcmp( psz_name, "trackList" ) )
367 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
373 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
379 * \brief parse one track element
380 * \param COMPLEX_INTERFACE
382 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
384 input_item_t *p_new_input = NULL;
387 char *psz_value=NULL;
388 xml_elem_hnd_t *p_handler=NULL;
390 xml_elem_hnd_t track_elements[] =
391 { {"location", SIMPLE_CONTENT, {NULL} },
392 {"identifier", SIMPLE_CONTENT, {NULL} },
393 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
394 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
395 {"annotation", SIMPLE_CONTENT, {NULL} },
396 {"info", SIMPLE_CONTENT, {NULL} },
397 {"image", SIMPLE_CONTENT, {NULL} },
398 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
399 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
400 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
401 {"link", SIMPLE_CONTENT, {NULL} },
402 {"meta", SIMPLE_CONTENT, {NULL} },
403 {"extension", COMPLEX_CONTENT, {.cmplx = skip_element} },
404 {NULL, UNKNOWN_CONTENT, {NULL} }
407 while( xml_ReaderRead( p_xml_reader ) == 1 )
409 i_node = xml_ReaderNodeType( p_xml_reader );
412 case XML_READER_NONE:
415 case XML_READER_STARTELEM:
416 /* element start tag */
417 psz_name = xml_ReaderName( p_xml_reader );
418 if( !psz_name || !*psz_name )
420 msg_Err( p_demux, "invalid xml stream" );
425 for( p_handler = track_elements;
426 p_handler->name && strcmp( psz_name, p_handler->name );
428 if( !p_handler->name )
430 msg_Err( p_demux, "unexpected element <%s>", psz_name );
435 /* complex content is parsed in a separate function */
436 if( p_handler->type == COMPLEX_CONTENT )
441 "at <%s> level no new item has been allocated",
446 if( p_handler->pf_handler.cmplx( p_demux,
463 case XML_READER_TEXT:
464 /* simple element content */
466 psz_value = xml_ReaderValue( p_xml_reader );
469 msg_Err( p_demux, "invalid xml stream" );
475 case XML_READER_ENDELEM:
476 /* element end tag */
477 psz_name = xml_ReaderName( p_xml_reader );
480 msg_Err( p_demux, "invalid xml stream" );
484 /* leave if the current parent node <track> is terminated */
485 if( !strcmp( psz_name, psz_element ) )
489 playlist_AddWhereverNeeded( p_playlist, p_new_input,
490 p_item, p_demux->p_sys->p_item_in_category,
491 (p_demux->p_sys->i_parent_id >0 ) ? VLC_TRUE:
492 VLC_FALSE, PLAYLIST_APPEND );
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 = unescape_URI_duplicate( psz_value );
540 p_new_input = input_ItemNewExt( p_playlist, psz_uri,
542 p_new_input->p_meta = vlc_meta_New();
544 vlc_input_item_CopyOptions( p_item->p_input, p_new_input );
555 else if( !strcmp( p_handler->name, "identifier" ) )
557 p_demux->p_sys->i_identifier = atoi( psz_value );
561 /* there MUST be an item */
564 msg_Err( p_demux, "item not yet created at <%s>",
569 if( p_handler->pf_handler.smpl )
571 p_handler->pf_handler.smpl( NULL, p_input,
582 /* unknown/unexpected xml node */
583 msg_Err( p_demux, "unexpected xml node %i", i_node );
589 msg_Err( p_demux, "unexpected end of xml data" );
595 * \brief handles the supported <track> sub-elements
597 static vlc_bool_t set_item_info SIMPLE_INTERFACE
599 /* exit if setting is impossible */
600 if( !psz_name || !psz_value || !p_input )
603 /* re-convert xml special characters inside psz_value */
604 resolve_xml_special_chars( psz_value );
606 /* handle each info element in a separate "if" clause */
607 if( !strcmp( psz_name, "title" ) )
609 p_input->psz_name = strdup( (char*)psz_value );
611 else if( !strcmp( psz_name, "creator" ) )
613 vlc_meta_SetArtist( p_input->p_meta, psz_value );
615 else if( !strcmp( psz_name, "album" ) )
617 vlc_meta_SetAlbum( p_input->p_meta, psz_value );
620 else if( !strcmp( psz_name, "trackNum" ) )
622 vlc_meta_SetTracknum( p_input->p_meta, psz_value );
624 else if( !strcmp( psz_name, "duration" ) )
626 long i_num = atol( psz_value );
627 p_input->i_duration = i_num*1000;
634 * \brief parse the extension node of a XSPF playlist
636 static vlc_bool_t parse_extension_node COMPLEX_INTERFACE
638 char *psz_name = NULL;
639 char *psz_value = NULL;
640 char *psz_title = NULL;
642 xml_elem_hnd_t *p_handler = NULL;
644 xml_elem_hnd_t pl_elements[] =
645 { {"node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
646 {"item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
647 {NULL, UNKNOWN_CONTENT, {NULL} }
650 /* read all extension node attributes */
651 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
653 psz_name = xml_ReaderName( p_xml_reader );
654 psz_value = xml_ReaderValue( p_xml_reader );
655 if( !psz_name || !psz_value )
657 msg_Err( p_demux, "invalid xml stream @ <node>" );
661 /* attribute: title */
662 if( !strcmp( psz_name, "title" ) )
664 psz_title = unescape_URI_duplicate( psz_value );
666 /* unknown attribute */
668 msg_Warn( p_demux, "invalid <node> attribute:\"%s\"", psz_name);
673 /* attribute title is mandatory except for <extension> */
674 if( !strcmp( psz_element, "node" ) && !psz_title )
676 msg_Warn( p_demux, "<node> requires \"title\" attribute" );
680 if( !strcmp( psz_element, "node" ) )
682 fprintf( stderr, " node: %s\n", psz_title );
684 if( psz_title ) free( psz_title );
686 /* parse the child elements */
687 while( xml_ReaderRead( p_xml_reader ) == 1 )
689 i_node = xml_ReaderNodeType( p_xml_reader );
692 case XML_READER_NONE:
694 case XML_READER_STARTELEM:
695 /* element start tag */
696 psz_name = xml_ReaderName( p_xml_reader );
697 if( !psz_name || !*psz_name )
699 msg_Err( p_demux, "invalid xml stream" );
704 for( p_handler = pl_elements;
705 p_handler->name && strcmp( psz_name, p_handler->name );
707 if( !p_handler->name )
709 msg_Err( p_demux, "unexpected element <%s>", psz_name );
714 /* complex content is parsed in a separate function */
715 if( p_handler->type == COMPLEX_CONTENT )
717 if( p_handler->pf_handler.cmplx( p_demux,
734 case XML_READER_TEXT:
735 /* simple element content */
737 psz_value = xml_ReaderValue( p_xml_reader );
740 msg_Err( p_demux, "invalid xml stream" );
746 case XML_READER_ENDELEM:
747 /* element end tag */
748 psz_name = xml_ReaderName( p_xml_reader );
751 msg_Err( p_demux, "invalid xml stream" );
755 /* leave if the current parent node is terminated */
756 if( !strcmp( psz_name, psz_element ) )
758 fprintf( stderr, " </node>\n" );
762 /* there MUST have been a start tag for that element name */
763 if( !p_handler || !p_handler->name
764 || strcmp( p_handler->name, psz_name ))
766 msg_Err( p_demux, "there's no open element left for <%s>",
772 if( p_handler->pf_handler.smpl )
774 p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
782 /* unknown/unexpected xml node */
783 msg_Err( p_demux, "unexpected xml node %i", i_node );
793 * \brief parse the extension item node of a XSPF playlist
795 static vlc_bool_t parse_extitem_node COMPLEX_INTERFACE
797 char *psz_name = NULL;
798 char *psz_value = NULL;
801 /* read all extension item attributes */
802 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
804 psz_name = xml_ReaderName( p_xml_reader );
805 psz_value = xml_ReaderValue( p_xml_reader );
806 if( !psz_name || !psz_value )
808 msg_Err( p_demux, "invalid xml stream @ <item>" );
812 /* attribute: href */
813 if( !strcmp( psz_name, "href" ) )
815 i_href = atoi( psz_value );
817 /* unknown attribute */
819 msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
824 /* attribute href is mandatory */
827 msg_Warn( p_demux, "<item> requires \"href\" attribute" );
831 if( i_href > p_demux->p_sys->i_tracklist_entries )
833 msg_Warn( p_demux, "invalid \"href\" attribute" );
837 fprintf( stderr, " %s\n", p_demux->p_sys->pp_tracklist[i_href]->psz_name );
843 * \brief skips complex element content that we can't manage
845 static vlc_bool_t skip_element COMPLEX_INTERFACE
849 while( xml_ReaderRead( p_xml_reader ) == 1 )
851 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
853 psz_endname = xml_ReaderName( p_xml_reader );
856 if( !strcmp( psz_element, psz_endname ) )