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( !strstr( psz_uri, "://" ) )
538 char* psz_tmp = malloc(
539 strlen(p_demux->p_sys->psz_base) +
540 strlen(psz_uri) +1 );
543 msg_Err( p_demux, "out of memory");
546 sprintf( psz_tmp, "%s%s",
547 p_demux->p_sys->psz_base, psz_uri );
551 p_new_input = input_ItemNewExt( p_playlist, psz_uri,
553 p_new_input->p_meta = vlc_meta_New();
555 input_ItemCopyOptions( p_item->p_input, p_new_input );
566 else if( !strcmp( p_handler->name, "identifier" ) )
568 p_demux->p_sys->i_identifier = atoi( psz_value );
572 /* there MUST be an item */
575 msg_Err( p_demux, "item not yet created at <%s>",
580 if( p_handler->pf_handler.smpl )
582 p_handler->pf_handler.smpl( NULL, p_new_input,
593 /* unknown/unexpected xml node */
594 msg_Err( p_demux, "unexpected xml node %i", i_node );
600 msg_Err( p_demux, "unexpected end of xml data" );
606 * \brief handles the supported <track> sub-elements
608 static vlc_bool_t set_item_info SIMPLE_INTERFACE
610 /* exit if setting is impossible */
611 if( !psz_name || !psz_value || !p_input )
615 /* re-convert xml special characters inside psz_value */
616 resolve_xml_special_chars( psz_value );
618 /* handle each info element in a separate "if" clause */
619 if( !strcmp( psz_name, "title" ) )
621 p_input->psz_name = strdup( (char*)psz_value );
623 else if( !strcmp( psz_name, "creator" ) )
625 vlc_meta_SetArtist( p_input->p_meta, psz_value );
627 else if( !strcmp( psz_name, "album" ) )
629 vlc_meta_SetAlbum( p_input->p_meta, psz_value );
632 else if( !strcmp( psz_name, "trackNum" ) )
634 vlc_meta_SetTracknum( p_input->p_meta, psz_value );
636 else if( !strcmp( psz_name, "duration" ) )
638 long i_num = atol( psz_value );
639 p_input->i_duration = i_num*1000;
646 * \brief parse the extension node of a XSPF playlist
648 static vlc_bool_t parse_extension_node COMPLEX_INTERFACE
650 char *psz_name = NULL;
651 char *psz_value = NULL;
652 char *psz_title = NULL;
654 xml_elem_hnd_t *p_handler = NULL;
656 xml_elem_hnd_t pl_elements[] =
657 { {"node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
658 {"item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
659 {NULL, UNKNOWN_CONTENT, {NULL} }
662 /* read all extension node attributes */
663 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
665 psz_name = xml_ReaderName( p_xml_reader );
666 psz_value = xml_ReaderValue( p_xml_reader );
667 if( !psz_name || !psz_value )
669 msg_Err( p_demux, "invalid xml stream @ <node>" );
673 /* attribute: title */
674 if( !strcmp( psz_name, "title" ) )
676 psz_title = unescape_URI_duplicate( psz_value );
678 /* unknown attribute */
680 msg_Warn( p_demux, "invalid <node> attribute:\"%s\"", psz_name);
685 /* attribute title is mandatory except for <extension> */
686 if( !strcmp( psz_element, "node" ) && !psz_title )
688 msg_Warn( p_demux, "<node> requires \"title\" attribute" );
691 if( psz_title ) free( psz_title );
693 /* parse the child elements */
694 while( xml_ReaderRead( p_xml_reader ) == 1 )
696 i_node = xml_ReaderNodeType( p_xml_reader );
699 case XML_READER_NONE:
701 case XML_READER_STARTELEM:
702 /* element start tag */
703 psz_name = xml_ReaderName( p_xml_reader );
704 if( !psz_name || !*psz_name )
706 msg_Err( p_demux, "invalid xml stream" );
711 for( p_handler = pl_elements;
712 p_handler->name && strcmp( psz_name, p_handler->name );
714 if( !p_handler->name )
716 msg_Err( p_demux, "unexpected element <%s>", psz_name );
721 /* complex content is parsed in a separate function */
722 if( p_handler->type == COMPLEX_CONTENT )
724 if( p_handler->pf_handler.cmplx( p_demux,
741 case XML_READER_TEXT:
742 /* simple element content */
744 psz_value = xml_ReaderValue( p_xml_reader );
747 msg_Err( p_demux, "invalid xml stream" );
753 case XML_READER_ENDELEM:
754 /* element end tag */
755 psz_name = xml_ReaderName( p_xml_reader );
758 msg_Err( p_demux, "invalid xml stream" );
762 /* leave if the current parent node is terminated */
763 if( !strcmp( psz_name, psz_element ) )
768 /* there MUST have been a start tag for that element name */
769 if( !p_handler || !p_handler->name
770 || strcmp( p_handler->name, psz_name ))
772 msg_Err( p_demux, "there's no open element left for <%s>",
778 if( p_handler->pf_handler.smpl )
780 p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
788 /* unknown/unexpected xml node */
789 msg_Err( p_demux, "unexpected xml node %i", i_node );
799 * \brief parse the extension item node of a XSPF playlist
801 static vlc_bool_t parse_extitem_node COMPLEX_INTERFACE
803 char *psz_name = NULL;
804 char *psz_value = NULL;
807 /* read all extension item attributes */
808 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
810 psz_name = xml_ReaderName( p_xml_reader );
811 psz_value = xml_ReaderValue( p_xml_reader );
812 if( !psz_name || !psz_value )
814 msg_Err( p_demux, "invalid xml stream @ <item>" );
818 /* attribute: href */
819 if( !strcmp( psz_name, "href" ) )
821 i_href = atoi( psz_value );
823 /* unknown attribute */
825 msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
830 /* attribute href is mandatory */
833 msg_Warn( p_demux, "<item> requires \"href\" attribute" );
837 if( i_href > p_demux->p_sys->i_tracklist_entries )
839 msg_Warn( p_demux, "invalid \"href\" attribute" );
846 * \brief skips complex element content that we can't manage
848 static vlc_bool_t skip_element COMPLEX_INTERFACE
852 while( xml_ReaderRead( p_xml_reader ) == 1 )
854 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
856 psz_endname = xml_ReaderName( p_xml_reader );
859 if( !strcmp( psz_element, psz_endname ) )