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 );
123 /** \brief dummy function for demux callback interface */
124 static int Control( demux_t *p_demux, int i_query, va_list args )
130 * \brief parse the root node of a XSPF playlist
131 * \param p_demux demuxer instance
132 * \param p_playlist playlist instance
133 * \param p_item current playlist item
134 * \param p_input current input item
135 * \param p_xml_reader xml reader instance
136 * \param psz_element name of element to parse
138 static vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
141 char *psz_value=NULL;
142 vlc_bool_t b_version_found = VLC_FALSE;
144 xml_elem_hnd_t *p_handler=NULL;
146 xml_elem_hnd_t pl_elements[] =
147 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
148 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
149 {"annotation", SIMPLE_CONTENT, {NULL} },
150 {"info", SIMPLE_CONTENT, {NULL} },
151 {"location", SIMPLE_CONTENT, {NULL} },
152 {"identifier", SIMPLE_CONTENT, {NULL} },
153 {"image", SIMPLE_CONTENT, {NULL} },
154 {"date", SIMPLE_CONTENT, {NULL} },
155 {"license", SIMPLE_CONTENT, {NULL} },
156 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
157 {"link", SIMPLE_CONTENT, {NULL} },
158 {"meta", SIMPLE_CONTENT, {NULL} },
159 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
160 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
161 {NULL, UNKNOWN_CONTENT, {NULL} }
164 /* read all playlist attributes */
165 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
167 psz_name = xml_ReaderName( p_xml_reader );
168 psz_value = xml_ReaderValue( p_xml_reader );
169 if( !psz_name || !psz_value )
171 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
175 /* attribute: version */
176 if( !strcmp( psz_name, "version" ) )
178 b_version_found = VLC_TRUE;
179 if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
180 msg_Warn( p_demux, "unsupported XSPF version" );
182 /* attribute: xmlns */
183 else if( !strcmp( psz_name, "xmlns" ) )
185 /* unknown attribute */
187 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
191 /* attribute version is mandatory !!! */
192 if( !b_version_found )
193 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
195 /* parse the child elements - we only take care of <trackList> */
196 while( xml_ReaderRead( p_xml_reader ) == 1 )
198 i_node = xml_ReaderNodeType( p_xml_reader );
201 case XML_READER_NONE:
203 case XML_READER_STARTELEM:
204 /* element start tag */
205 psz_name = xml_ReaderName( p_xml_reader );
206 if( !psz_name || !*psz_name )
208 msg_Err( p_demux, "invalid xml stream" );
213 for( p_handler = pl_elements;
214 p_handler->name && strcmp( psz_name, p_handler->name );
216 if( !p_handler->name )
218 msg_Err( p_demux, "unexpected element <%s>", psz_name );
223 /* complex content is parsed in a separate function */
224 if( p_handler->type == COMPLEX_CONTENT )
226 if( p_handler->pf_handler.cmplx( p_demux,
243 case XML_READER_TEXT:
244 /* simple element content */
246 psz_value = xml_ReaderValue( p_xml_reader );
249 msg_Err( p_demux, "invalid xml stream" );
255 case XML_READER_ENDELEM:
256 /* element end tag */
257 psz_name = xml_ReaderName( p_xml_reader );
260 msg_Err( p_demux, "invalid xml stream" );
264 /* leave if the current parent node <playlist> is terminated */
265 if( !strcmp( psz_name, psz_element ) )
270 /* there MUST have been a start tag for that element name */
271 if( !p_handler || !p_handler->name
272 || strcmp( p_handler->name, psz_name ))
274 msg_Err( p_demux, "there's no open element left for <%s>",
280 if( p_handler->pf_handler.smpl )
282 p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
290 /* unknown/unexpected xml node */
291 msg_Err( p_demux, "unexpected xml node %i", i_node );
301 * \brief parses the tracklist node which only may contain <track>s
303 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
309 /* now parse the <track>s */
310 while( xml_ReaderRead( p_xml_reader ) == 1 )
312 i_node = xml_ReaderNodeType( p_xml_reader );
313 if( i_node == XML_READER_STARTELEM )
315 psz_name = xml_ReaderName( p_xml_reader );
318 msg_Err( p_demux, "unexpected end of xml data" );
322 if( strcmp( psz_name, "track") )
324 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
331 /* parse the track data in a separate function */
332 if( parse_track_node( p_demux, p_playlist, p_item, NULL,
333 p_xml_reader,"track" ) == VLC_TRUE )
336 else if( i_node == XML_READER_ENDELEM )
340 /* the <trackList> has to be terminated */
341 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
343 msg_Err( p_demux, "there's a missing </trackList>" );
347 psz_name = xml_ReaderName( p_xml_reader );
348 if( !psz_name || strcmp( psz_name, "trackList" ) )
350 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
356 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
362 * \brief parse one track element
363 * \param COMPLEX_INTERFACE
365 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
367 input_item_t *p_new_input = NULL;
370 char *psz_value=NULL;
371 xml_elem_hnd_t *p_handler=NULL;
373 xml_elem_hnd_t track_elements[] =
374 { {"location", SIMPLE_CONTENT, {NULL} },
375 {"identifier", SIMPLE_CONTENT, {NULL} },
376 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
377 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
378 {"annotation", SIMPLE_CONTENT, {NULL} },
379 {"info", SIMPLE_CONTENT, {NULL} },
380 {"image", SIMPLE_CONTENT, {NULL} },
381 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
382 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
383 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
384 {"link", SIMPLE_CONTENT, {NULL} },
385 {"meta", SIMPLE_CONTENT, {NULL} },
386 {"extension", COMPLEX_CONTENT, {.cmplx = skip_element} },
387 {NULL, UNKNOWN_CONTENT, {NULL} }
390 while( xml_ReaderRead( p_xml_reader ) == 1 )
392 i_node = xml_ReaderNodeType( p_xml_reader );
395 case XML_READER_NONE:
398 case XML_READER_STARTELEM:
399 /* element start tag */
400 psz_name = xml_ReaderName( p_xml_reader );
401 if( !psz_name || !*psz_name )
403 msg_Err( p_demux, "invalid xml stream" );
408 for( p_handler = track_elements;
409 p_handler->name && strcmp( psz_name, p_handler->name );
411 if( !p_handler->name )
413 msg_Err( p_demux, "unexpected element <%s>", psz_name );
418 /* complex content is parsed in a separate function */
419 if( p_handler->type == COMPLEX_CONTENT )
424 "at <%s> level no new item has been allocated",
429 if( p_handler->pf_handler.cmplx( p_demux,
446 case XML_READER_TEXT:
447 /* simple element content */
449 psz_value = xml_ReaderValue( p_xml_reader );
452 msg_Err( p_demux, "invalid xml stream" );
458 case XML_READER_ENDELEM:
459 /* element end tag */
460 psz_name = xml_ReaderName( p_xml_reader );
463 msg_Err( p_demux, "invalid xml stream" );
467 /* leave if the current parent node <track> is terminated */
468 if( !strcmp( psz_name, psz_element ) )
472 playlist_AddWhereverNeeded( p_playlist, p_new_input,
473 p_item, p_demux->p_sys->p_item_in_category,
474 (p_demux->p_sys->i_parent_id >0 ) ? VLC_TRUE:
475 VLC_FALSE, PLAYLIST_APPEND );
476 if( p_demux->p_sys->i_identifier <
477 p_demux->p_sys->i_tracklist_entries )
479 p_demux->p_sys->pp_tracklist[
480 p_demux->p_sys->i_identifier ] = p_new_input;
484 if( p_demux->p_sys->i_identifier >
485 p_demux->p_sys->i_tracklist_entries )
487 p_demux->p_sys->i_tracklist_entries =
488 p_demux->p_sys->i_identifier;
490 INSERT_ELEM( p_demux->p_sys->pp_tracklist,
491 p_demux->p_sys->i_tracklist_entries,
492 p_demux->p_sys->i_tracklist_entries,
497 /* there MUST have been a start tag for that element name */
498 if( !p_handler || !p_handler->name
499 || strcmp( p_handler->name, psz_name ))
501 msg_Err( p_demux, "there's no open element left for <%s>",
507 /* special case: location */
508 if( !strcmp( p_handler->name, "location" ) )
511 /* there MUST NOT be an item */
514 msg_Err( p_demux, "item <%s> already created",
519 psz_uri = unescape_URI_duplicate( psz_value );
523 p_new_input = input_ItemNewExt( p_playlist, psz_uri,
525 p_new_input->p_meta = vlc_meta_New();
527 vlc_input_item_CopyOptions( p_item->p_input, p_new_input );
538 else if( !strcmp( p_handler->name, "identifier" ) )
540 p_demux->p_sys->i_identifier = atoi( psz_value );
544 /* there MUST be an item */
547 msg_Err( p_demux, "item not yet created at <%s>",
552 if( p_handler->pf_handler.smpl )
554 p_handler->pf_handler.smpl( NULL, p_new_input,
565 /* unknown/unexpected xml node */
566 msg_Err( p_demux, "unexpected xml node %i", i_node );
572 msg_Err( p_demux, "unexpected end of xml data" );
578 * \brief handles the supported <track> sub-elements
580 static vlc_bool_t set_item_info SIMPLE_INTERFACE
582 /* exit if setting is impossible */
583 if( !psz_name || !psz_value || !p_input )
587 /* re-convert xml special characters inside psz_value */
588 resolve_xml_special_chars( psz_value );
590 /* handle each info element in a separate "if" clause */
591 if( !strcmp( psz_name, "title" ) )
593 p_input->psz_name = strdup( (char*)psz_value );
595 else if( !strcmp( psz_name, "creator" ) )
597 vlc_meta_SetArtist( p_input->p_meta, psz_value );
599 else if( !strcmp( psz_name, "album" ) )
601 vlc_meta_SetAlbum( p_input->p_meta, psz_value );
604 else if( !strcmp( psz_name, "trackNum" ) )
606 vlc_meta_SetTracknum( p_input->p_meta, psz_value );
608 else if( !strcmp( psz_name, "duration" ) )
610 long i_num = atol( psz_value );
611 p_input->i_duration = i_num*1000;
618 * \brief parse the extension node of a XSPF playlist
620 static vlc_bool_t parse_extension_node COMPLEX_INTERFACE
622 char *psz_name = NULL;
623 char *psz_value = NULL;
624 char *psz_title = NULL;
626 xml_elem_hnd_t *p_handler = NULL;
628 xml_elem_hnd_t pl_elements[] =
629 { {"node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
630 {"item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
631 {NULL, UNKNOWN_CONTENT, {NULL} }
634 /* read all extension node attributes */
635 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
637 psz_name = xml_ReaderName( p_xml_reader );
638 psz_value = xml_ReaderValue( p_xml_reader );
639 if( !psz_name || !psz_value )
641 msg_Err( p_demux, "invalid xml stream @ <node>" );
645 /* attribute: title */
646 if( !strcmp( psz_name, "title" ) )
648 psz_title = unescape_URI_duplicate( psz_value );
650 /* unknown attribute */
652 msg_Warn( p_demux, "invalid <node> attribute:\"%s\"", psz_name);
657 /* attribute title is mandatory except for <extension> */
658 if( !strcmp( psz_element, "node" ) && !psz_title )
660 msg_Warn( p_demux, "<node> requires \"title\" attribute" );
663 if( psz_title ) free( psz_title );
665 /* parse the child elements */
666 while( xml_ReaderRead( p_xml_reader ) == 1 )
668 i_node = xml_ReaderNodeType( p_xml_reader );
671 case XML_READER_NONE:
673 case XML_READER_STARTELEM:
674 /* element start tag */
675 psz_name = xml_ReaderName( p_xml_reader );
676 if( !psz_name || !*psz_name )
678 msg_Err( p_demux, "invalid xml stream" );
683 for( p_handler = pl_elements;
684 p_handler->name && strcmp( psz_name, p_handler->name );
686 if( !p_handler->name )
688 msg_Err( p_demux, "unexpected element <%s>", psz_name );
693 /* complex content is parsed in a separate function */
694 if( p_handler->type == COMPLEX_CONTENT )
696 if( p_handler->pf_handler.cmplx( p_demux,
713 case XML_READER_TEXT:
714 /* simple element content */
716 psz_value = xml_ReaderValue( p_xml_reader );
719 msg_Err( p_demux, "invalid xml stream" );
725 case XML_READER_ENDELEM:
726 /* element end tag */
727 psz_name = xml_ReaderName( p_xml_reader );
730 msg_Err( p_demux, "invalid xml stream" );
734 /* leave if the current parent node is terminated */
735 if( !strcmp( psz_name, psz_element ) )
740 /* there MUST have been a start tag for that element name */
741 if( !p_handler || !p_handler->name
742 || strcmp( p_handler->name, psz_name ))
744 msg_Err( p_demux, "there's no open element left for <%s>",
750 if( p_handler->pf_handler.smpl )
752 p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
760 /* unknown/unexpected xml node */
761 msg_Err( p_demux, "unexpected xml node %i", i_node );
771 * \brief parse the extension item node of a XSPF playlist
773 static vlc_bool_t parse_extitem_node COMPLEX_INTERFACE
775 char *psz_name = NULL;
776 char *psz_value = NULL;
779 /* read all extension item attributes */
780 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
782 psz_name = xml_ReaderName( p_xml_reader );
783 psz_value = xml_ReaderValue( p_xml_reader );
784 if( !psz_name || !psz_value )
786 msg_Err( p_demux, "invalid xml stream @ <item>" );
790 /* attribute: href */
791 if( !strcmp( psz_name, "href" ) )
793 i_href = atoi( psz_value );
795 /* unknown attribute */
797 msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
802 /* attribute href is mandatory */
805 msg_Warn( p_demux, "<item> requires \"href\" attribute" );
809 if( i_href > p_demux->p_sys->i_tracklist_entries )
811 msg_Warn( p_demux, "invalid \"href\" attribute" );
818 * \brief skips complex element content that we can't manage
820 static vlc_bool_t skip_element COMPLEX_INTERFACE
824 while( xml_ReaderRead( p_xml_reader ) == 1 )
826 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
828 psz_endname = xml_ReaderName( p_xml_reader );
831 if( !strcmp( psz_element, psz_endname ) )