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_(Import_xspf)( vlc_object_t *p_this )
56 DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xspf", "xspf-open",
57 "using XSPF playlist reader" );
61 void E_(Close_xspf)( vlc_object_t *p_this )
63 demux_t *p_demux = (demux_t *)p_this;
64 free( p_demux->p_sys );
68 * \brief demuxer function for XSPF parsing
70 int Demux( demux_t *p_demux )
72 int i_ret = VLC_SUCCESS;
74 xml_reader_t *p_xml_reader = NULL;
75 char *psz_name = NULL;
77 p_demux->p_sys->p_item_in_category = p_item_in_category;
78 p_demux->p_sys->i_parent_id = i_parent_id;
79 p_demux->p_sys->pp_tracklist = NULL;
80 p_demux->p_sys->i_tracklist_entries = 0;
81 p_demux->p_sys->i_identifier = -1;
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 */
95 if( i_ret == VLC_SUCCESS )
99 if( xml_ReaderRead( p_xml_reader ) != 1 )
101 msg_Err( p_demux, "can't read xml stream" );
102 i_ret = VLC_EGENERIC;
104 } while( i_ret == VLC_SUCCESS &&
105 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
107 /* checking root node name */
108 if( i_ret == VLC_SUCCESS )
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 );
114 i_ret = VLC_EGENERIC;
119 i_ret = parse_playlist_node( p_demux, p_playlist, p_current, NULL,
120 p_xml_reader, "playlist" );
121 HANDLE_PLAY_AND_RELEASE;
123 xml_ReaderDelete( p_xml, p_xml_reader );
129 /** \brief dummy function for demux callback interface */
130 static int Control( demux_t *p_demux, int i_query, va_list args )
136 * \brief parse the root node of a XSPF playlist
137 * \param p_demux demuxer instance
138 * \param p_playlist playlist instance
139 * \param p_item current playlist item
140 * \param p_input current input item
141 * \param p_xml_reader xml reader instance
142 * \param psz_element name of element to parse
144 static vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
147 char *psz_value=NULL;
148 vlc_bool_t b_version_found = VLC_FALSE;
150 xml_elem_hnd_t *p_handler=NULL;
152 xml_elem_hnd_t pl_elements[] =
153 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
154 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
155 {"annotation", SIMPLE_CONTENT, {NULL} },
156 {"info", SIMPLE_CONTENT, {NULL} },
157 {"location", SIMPLE_CONTENT, {NULL} },
158 {"identifier", SIMPLE_CONTENT, {NULL} },
159 {"image", SIMPLE_CONTENT, {NULL} },
160 {"date", SIMPLE_CONTENT, {NULL} },
161 {"license", SIMPLE_CONTENT, {NULL} },
162 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
163 {"link", SIMPLE_CONTENT, {NULL} },
164 {"meta", SIMPLE_CONTENT, {NULL} },
165 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
166 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
167 {NULL, UNKNOWN_CONTENT, {NULL} }
170 /* read all playlist attributes */
171 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
173 psz_name = xml_ReaderName( p_xml_reader );
174 psz_value = xml_ReaderValue( p_xml_reader );
175 if( !psz_name || !psz_value )
177 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
181 /* attribute: version */
182 if( !strcmp( psz_name, "version" ) )
184 b_version_found = VLC_TRUE;
185 if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
186 msg_Warn( p_demux, "unsupported XSPF version" );
188 /* attribute: xmlns */
189 else if( !strcmp( psz_name, "xmlns" ) )
191 /* unknown attribute */
193 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
197 /* attribute version is mandatory !!! */
198 if( !b_version_found )
199 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
201 /* parse the child elements - we only take care of <trackList> */
202 while( xml_ReaderRead( p_xml_reader ) == 1 )
204 i_node = xml_ReaderNodeType( p_xml_reader );
207 case XML_READER_NONE:
209 case XML_READER_STARTELEM:
210 /* element start tag */
211 psz_name = xml_ReaderName( p_xml_reader );
212 if( !psz_name || !*psz_name )
214 msg_Err( p_demux, "invalid xml stream" );
219 for( p_handler = pl_elements;
220 p_handler->name && strcmp( psz_name, p_handler->name );
222 if( !p_handler->name )
224 msg_Err( p_demux, "unexpected element <%s>", psz_name );
229 /* complex content is parsed in a separate function */
230 if( p_handler->type == COMPLEX_CONTENT )
232 if( p_handler->pf_handler.cmplx( p_demux,
249 case XML_READER_TEXT:
250 /* simple element content */
252 psz_value = xml_ReaderValue( p_xml_reader );
255 msg_Err( p_demux, "invalid xml stream" );
261 case XML_READER_ENDELEM:
262 /* element end tag */
263 psz_name = xml_ReaderName( p_xml_reader );
266 msg_Err( p_demux, "invalid xml stream" );
270 /* leave if the current parent node <playlist> is terminated */
271 if( !strcmp( psz_name, psz_element ) )
276 /* there MUST have been a start tag for that element name */
277 if( !p_handler || !p_handler->name
278 || strcmp( p_handler->name, psz_name ))
280 msg_Err( p_demux, "there's no open element left for <%s>",
286 if( p_handler->pf_handler.smpl )
288 p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
296 /* unknown/unexpected xml node */
297 msg_Err( p_demux, "unexpected xml node %i", i_node );
307 * \brief parses the tracklist node which only may contain <track>s
309 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
315 /* now parse the <track>s */
316 while( xml_ReaderRead( p_xml_reader ) == 1 )
318 i_node = xml_ReaderNodeType( p_xml_reader );
319 if( i_node == XML_READER_STARTELEM )
321 psz_name = xml_ReaderName( p_xml_reader );
324 msg_Err( p_demux, "unexpected end of xml data" );
328 if( strcmp( psz_name, "track") )
330 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
337 /* parse the track data in a separate function */
338 if( parse_track_node( p_demux, p_playlist, p_item, NULL,
339 p_xml_reader,"track" ) == VLC_TRUE )
342 else if( i_node == XML_READER_ENDELEM )
346 /* the <trackList> has to be terminated */
347 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
349 msg_Err( p_demux, "there's a missing </trackList>" );
353 psz_name = xml_ReaderName( p_xml_reader );
354 if( !psz_name || strcmp( psz_name, "trackList" ) )
356 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
362 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
368 * \brief parse one track element
369 * \param COMPLEX_INTERFACE
371 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
373 input_item_t *p_new_input = NULL;
376 char *psz_value=NULL;
377 xml_elem_hnd_t *p_handler=NULL;
379 xml_elem_hnd_t track_elements[] =
380 { {"location", SIMPLE_CONTENT, {NULL} },
381 {"identifier", SIMPLE_CONTENT, {NULL} },
382 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
383 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
384 {"annotation", SIMPLE_CONTENT, {NULL} },
385 {"info", SIMPLE_CONTENT, {NULL} },
386 {"image", SIMPLE_CONTENT, {NULL} },
387 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
388 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
389 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
390 {"link", SIMPLE_CONTENT, {NULL} },
391 {"meta", SIMPLE_CONTENT, {NULL} },
392 {"extension", COMPLEX_CONTENT, {.cmplx = skip_element} },
393 {NULL, UNKNOWN_CONTENT, {NULL} }
396 while( xml_ReaderRead( p_xml_reader ) == 1 )
398 i_node = xml_ReaderNodeType( p_xml_reader );
401 case XML_READER_NONE:
404 case XML_READER_STARTELEM:
405 /* element start tag */
406 psz_name = xml_ReaderName( p_xml_reader );
407 if( !psz_name || !*psz_name )
409 msg_Err( p_demux, "invalid xml stream" );
414 for( p_handler = track_elements;
415 p_handler->name && strcmp( psz_name, p_handler->name );
417 if( !p_handler->name )
419 msg_Err( p_demux, "unexpected element <%s>", psz_name );
424 /* complex content is parsed in a separate function */
425 if( p_handler->type == COMPLEX_CONTENT )
430 "at <%s> level no new item has been allocated",
435 if( p_handler->pf_handler.cmplx( p_demux,
452 case XML_READER_TEXT:
453 /* simple element content */
455 psz_value = xml_ReaderValue( p_xml_reader );
458 msg_Err( p_demux, "invalid xml stream" );
464 case XML_READER_ENDELEM:
465 /* element end tag */
466 psz_name = xml_ReaderName( p_xml_reader );
469 msg_Err( p_demux, "invalid xml stream" );
473 /* leave if the current parent node <track> is terminated */
474 if( !strcmp( psz_name, psz_element ) )
478 playlist_AddWhereverNeeded( p_playlist, p_new_input,
479 p_item, p_demux->p_sys->p_item_in_category,
480 (p_demux->p_sys->i_parent_id >0 ) ? VLC_TRUE:
481 VLC_FALSE, PLAYLIST_APPEND );
482 if( p_demux->p_sys->i_identifier <
483 p_demux->p_sys->i_tracklist_entries )
485 p_demux->p_sys->pp_tracklist[
486 p_demux->p_sys->i_identifier ] = p_new_input;
490 if( p_demux->p_sys->i_identifier >
491 p_demux->p_sys->i_tracklist_entries )
493 p_demux->p_sys->i_tracklist_entries =
494 p_demux->p_sys->i_identifier;
496 INSERT_ELEM( p_demux->p_sys->pp_tracklist,
497 p_demux->p_sys->i_tracklist_entries,
498 p_demux->p_sys->i_tracklist_entries,
503 /* there MUST have been a start tag for that element name */
504 if( !p_handler || !p_handler->name
505 || strcmp( p_handler->name, psz_name ))
507 msg_Err( p_demux, "there's no open element left for <%s>",
513 /* special case: location */
514 if( !strcmp( p_handler->name, "location" ) )
517 /* there MUST NOT be an item */
520 msg_Err( p_demux, "item <%s> already created",
525 psz_uri = decode_URI_duplicate( psz_value );
529 p_new_input = input_ItemNewExt( p_playlist, psz_uri,
531 p_new_input->p_meta = vlc_meta_New();
533 input_ItemCopyOptions( p_item->p_input, p_new_input );
544 else if( !strcmp( p_handler->name, "identifier" ) )
546 p_demux->p_sys->i_identifier = atoi( psz_value );
550 /* there MUST be an item */
553 msg_Err( p_demux, "item not yet created at <%s>",
558 if( p_handler->pf_handler.smpl )
560 p_handler->pf_handler.smpl( NULL, p_new_input,
571 /* unknown/unexpected xml node */
572 msg_Err( p_demux, "unexpected xml node %i", i_node );
578 msg_Err( p_demux, "unexpected end of xml data" );
584 * \brief handles the supported <track> sub-elements
586 static vlc_bool_t set_item_info SIMPLE_INTERFACE
588 /* exit if setting is impossible */
589 if( !psz_name || !psz_value || !p_input )
593 /* re-convert xml special characters inside psz_value */
594 resolve_xml_special_chars( psz_value );
596 /* handle each info element in a separate "if" clause */
597 if( !strcmp( psz_name, "title" ) )
599 p_input->psz_name = strdup( (char*)psz_value );
601 else if( !strcmp( psz_name, "creator" ) )
603 vlc_meta_SetArtist( p_input->p_meta, psz_value );
605 else if( !strcmp( psz_name, "album" ) )
607 vlc_meta_SetAlbum( p_input->p_meta, psz_value );
610 else if( !strcmp( psz_name, "trackNum" ) )
612 vlc_meta_SetTracknum( p_input->p_meta, psz_value );
614 else if( !strcmp( psz_name, "duration" ) )
616 long i_num = atol( psz_value );
617 p_input->i_duration = i_num*1000;
624 * \brief parse the extension node of a XSPF playlist
626 static vlc_bool_t parse_extension_node COMPLEX_INTERFACE
628 char *psz_name = NULL;
629 char *psz_value = NULL;
630 char *psz_title = NULL;
632 xml_elem_hnd_t *p_handler = NULL;
634 xml_elem_hnd_t pl_elements[] =
635 { {"node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
636 {"item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
637 {NULL, UNKNOWN_CONTENT, {NULL} }
640 /* read all extension node attributes */
641 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
643 psz_name = xml_ReaderName( p_xml_reader );
644 psz_value = xml_ReaderValue( p_xml_reader );
645 if( !psz_name || !psz_value )
647 msg_Err( p_demux, "invalid xml stream @ <node>" );
651 /* attribute: title */
652 if( !strcmp( psz_name, "title" ) )
654 psz_title = unescape_URI_duplicate( psz_value );
656 /* unknown attribute */
658 msg_Warn( p_demux, "invalid <node> attribute:\"%s\"", psz_name);
663 /* attribute title is mandatory except for <extension> */
664 if( !strcmp( psz_element, "node" ) && !psz_title )
666 msg_Warn( p_demux, "<node> requires \"title\" attribute" );
669 if( psz_title ) free( psz_title );
671 /* parse the child elements */
672 while( xml_ReaderRead( p_xml_reader ) == 1 )
674 i_node = xml_ReaderNodeType( p_xml_reader );
677 case XML_READER_NONE:
679 case XML_READER_STARTELEM:
680 /* element start tag */
681 psz_name = xml_ReaderName( p_xml_reader );
682 if( !psz_name || !*psz_name )
684 msg_Err( p_demux, "invalid xml stream" );
689 for( p_handler = pl_elements;
690 p_handler->name && strcmp( psz_name, p_handler->name );
692 if( !p_handler->name )
694 msg_Err( p_demux, "unexpected element <%s>", psz_name );
699 /* complex content is parsed in a separate function */
700 if( p_handler->type == COMPLEX_CONTENT )
702 if( p_handler->pf_handler.cmplx( p_demux,
719 case XML_READER_TEXT:
720 /* simple element content */
722 psz_value = xml_ReaderValue( p_xml_reader );
725 msg_Err( p_demux, "invalid xml stream" );
731 case XML_READER_ENDELEM:
732 /* element end tag */
733 psz_name = xml_ReaderName( p_xml_reader );
736 msg_Err( p_demux, "invalid xml stream" );
740 /* leave if the current parent node is terminated */
741 if( !strcmp( psz_name, psz_element ) )
746 /* there MUST have been a start tag for that element name */
747 if( !p_handler || !p_handler->name
748 || strcmp( p_handler->name, psz_name ))
750 msg_Err( p_demux, "there's no open element left for <%s>",
756 if( p_handler->pf_handler.smpl )
758 p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
766 /* unknown/unexpected xml node */
767 msg_Err( p_demux, "unexpected xml node %i", i_node );
777 * \brief parse the extension item node of a XSPF playlist
779 static vlc_bool_t parse_extitem_node COMPLEX_INTERFACE
781 char *psz_name = NULL;
782 char *psz_value = NULL;
785 /* read all extension item attributes */
786 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
788 psz_name = xml_ReaderName( p_xml_reader );
789 psz_value = xml_ReaderValue( p_xml_reader );
790 if( !psz_name || !psz_value )
792 msg_Err( p_demux, "invalid xml stream @ <item>" );
796 /* attribute: href */
797 if( !strcmp( psz_name, "href" ) )
799 i_href = atoi( psz_value );
801 /* unknown attribute */
803 msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
808 /* attribute href is mandatory */
811 msg_Warn( p_demux, "<item> requires \"href\" attribute" );
815 if( i_href > p_demux->p_sys->i_tracklist_entries )
817 msg_Warn( p_demux, "invalid \"href\" attribute" );
824 * \brief skips complex element content that we can't manage
826 static vlc_bool_t skip_element COMPLEX_INTERFACE
830 while( xml_ReaderRead( p_xml_reader ) == 1 )
832 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
834 psz_endname = xml_ReaderName( p_xml_reader );
837 if( !strcmp( psz_element, psz_endname ) )