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_demux.h>
34 #include "vlc_strings.h"
40 playlist_item_t *p_item_in_category;
41 input_item_t **pp_tracklist;
42 int i_tracklist_entries;
47 static int Control( demux_t *, int, va_list );
48 static int Demux( demux_t * );
51 * \brief XSPF submodule initialization function
53 int E_(Import_xspf)( vlc_object_t *p_this )
55 DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xspf", "xspf-open",
56 "using XSPF playlist reader" );
60 void E_(Close_xspf)( vlc_object_t *p_this )
62 demux_t *p_demux = (demux_t *)p_this;
63 FREENULL( p_demux->p_sys->pp_tracklist );
64 FREENULL( p_demux->p_sys->psz_base );
65 free( p_demux->p_sys );
69 * \brief demuxer function for XSPF parsing
71 int Demux( demux_t *p_demux )
73 int i_ret = VLC_SUCCESS;
75 xml_reader_t *p_xml_reader = NULL;
76 char *psz_name = NULL;
78 p_demux->p_sys->p_item_in_category = p_item_in_category;
79 p_demux->p_sys->pp_tracklist = NULL;
80 p_demux->p_sys->i_tracklist_entries = 0;
81 p_demux->p_sys->i_identifier = 0;
82 p_demux->p_sys->psz_base = NULL;
84 /* create new xml parser from stream */
85 p_xml = xml_Create( p_demux );
90 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
95 /* locating the root node */
96 if( i_ret == VLC_SUCCESS )
100 if( xml_ReaderRead( p_xml_reader ) != 1 )
102 msg_Err( p_demux, "can't read xml stream" );
103 i_ret = VLC_EGENERIC;
105 } while( i_ret == VLC_SUCCESS &&
106 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
108 /* checking root node name */
109 if( i_ret == VLC_SUCCESS )
111 psz_name = xml_ReaderName( p_xml_reader );
112 if( !psz_name || strcmp( psz_name, "playlist" ) )
114 msg_Err( p_demux, "invalid root node name: %s", psz_name );
115 i_ret = VLC_EGENERIC;
120 i_ret = parse_playlist_node( p_demux, p_playlist, p_current, NULL,
121 p_xml_reader, "playlist" );
122 HANDLE_PLAY_AND_RELEASE;
124 xml_ReaderDelete( p_xml, p_xml_reader );
127 return -1; /* Needed for correct operation of go back */
130 /** \brief dummy function for demux callback interface */
131 static int Control( demux_t *p_demux, int i_query, va_list args )
137 * \brief parse the root node of a XSPF playlist
138 * \param p_demux demuxer instance
139 * \param p_playlist playlist instance
140 * \param p_item current playlist item
141 * \param p_input current input item
142 * \param p_xml_reader xml reader instance
143 * \param psz_element name of element to parse
145 static vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
148 char *psz_value=NULL;
149 vlc_bool_t b_version_found = VLC_FALSE;
151 xml_elem_hnd_t *p_handler=NULL;
153 xml_elem_hnd_t pl_elements[] =
154 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
155 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
156 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
157 {"info", SIMPLE_CONTENT, {NULL} },
158 {"location", SIMPLE_CONTENT, {NULL} },
159 {"identifier", SIMPLE_CONTENT, {NULL} },
160 {"image", SIMPLE_CONTENT, {NULL} },
161 {"date", SIMPLE_CONTENT, {NULL} },
162 {"license", SIMPLE_CONTENT, {NULL} },
163 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
164 {"link", SIMPLE_CONTENT, {NULL} },
165 {"meta", SIMPLE_CONTENT, {NULL} },
166 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
167 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
168 {NULL, UNKNOWN_CONTENT, {NULL} }
171 /* read all playlist attributes */
172 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
174 psz_name = xml_ReaderName( p_xml_reader );
175 psz_value = xml_ReaderValue( p_xml_reader );
176 if( !psz_name || !psz_value )
178 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
182 /* attribute: version */
183 if( !strcmp( psz_name, "version" ) )
185 b_version_found = VLC_TRUE;
186 if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
187 msg_Warn( p_demux, "unsupported XSPF version" );
189 /* attribute: xmlns */
190 else if( !strcmp( psz_name, "xmlns" ) )
192 else if( !strcmp( psz_name, "xml:base" ) )
194 p_demux->p_sys->psz_base = decode_URI_duplicate( psz_value );
196 /* unknown attribute */
198 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
202 /* attribute version is mandatory !!! */
203 if( !b_version_found )
204 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
206 /* parse the child elements - we only take care of <trackList> */
207 while( xml_ReaderRead( p_xml_reader ) == 1 )
209 i_node = xml_ReaderNodeType( p_xml_reader );
212 case XML_READER_NONE:
214 case XML_READER_STARTELEM:
215 /* element start tag */
216 psz_name = xml_ReaderName( p_xml_reader );
217 if( !psz_name || !*psz_name )
219 msg_Err( p_demux, "invalid xml stream" );
224 for( p_handler = pl_elements;
225 p_handler->name && strcmp( psz_name, p_handler->name );
227 if( !p_handler->name )
229 msg_Err( p_demux, "unexpected element <%s>", psz_name );
234 /* complex content is parsed in a separate function */
235 if( p_handler->type == COMPLEX_CONTENT )
237 if( p_handler->pf_handler.cmplx( p_demux,
254 case XML_READER_TEXT:
255 /* simple element content */
257 psz_value = xml_ReaderValue( p_xml_reader );
260 msg_Err( p_demux, "invalid xml stream" );
266 case XML_READER_ENDELEM:
267 /* element end tag */
268 psz_name = xml_ReaderName( p_xml_reader );
271 msg_Err( p_demux, "invalid xml stream" );
275 /* leave if the current parent node <playlist> is terminated */
276 if( !strcmp( psz_name, psz_element ) )
281 /* there MUST have been a start tag for that element name */
282 if( !p_handler || !p_handler->name
283 || strcmp( p_handler->name, psz_name ))
285 msg_Err( p_demux, "there's no open element left for <%s>",
291 if( p_handler->pf_handler.smpl )
293 p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
301 /* unknown/unexpected xml node */
302 msg_Err( p_demux, "unexpected xml node %i", i_node );
312 * \brief parses the tracklist node which only may contain <track>s
314 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
320 /* now parse the <track>s */
321 while( xml_ReaderRead( p_xml_reader ) == 1 )
323 i_node = xml_ReaderNodeType( p_xml_reader );
324 if( i_node == XML_READER_STARTELEM )
326 psz_name = xml_ReaderName( p_xml_reader );
329 msg_Err( p_demux, "unexpected end of xml data" );
333 if( strcmp( psz_name, "track") )
335 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
342 /* parse the track data in a separate function */
343 if( parse_track_node( p_demux, p_playlist, p_item, NULL,
344 p_xml_reader,"track" ) == VLC_TRUE )
347 else if( i_node == XML_READER_ENDELEM )
351 /* the <trackList> has to be terminated */
352 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
354 msg_Err( p_demux, "there's a missing </trackList>" );
358 psz_name = xml_ReaderName( p_xml_reader );
359 if( !psz_name || strcmp( psz_name, "trackList" ) )
361 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
367 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
373 * \brief parse one track element
374 * \param COMPLEX_INTERFACE
376 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
378 input_item_t *p_new_input = NULL;
381 char *psz_value=NULL;
382 xml_elem_hnd_t *p_handler=NULL;
384 xml_elem_hnd_t track_elements[] =
385 { {"location", SIMPLE_CONTENT, {NULL} },
386 {"identifier", SIMPLE_CONTENT, {NULL} },
387 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
388 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
389 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
390 {"info", SIMPLE_CONTENT, {NULL} },
391 {"image", SIMPLE_CONTENT, {NULL} },
392 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
393 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
394 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
395 {"link", SIMPLE_CONTENT, {NULL} },
396 {"meta", SIMPLE_CONTENT, {NULL} },
397 {"extension", COMPLEX_CONTENT, {.cmplx = skip_element} },
398 {NULL, UNKNOWN_CONTENT, {NULL} }
401 while( xml_ReaderRead( p_xml_reader ) == 1 )
403 i_node = xml_ReaderNodeType( p_xml_reader );
406 case XML_READER_NONE:
409 case XML_READER_STARTELEM:
410 /* element start tag */
411 psz_name = xml_ReaderName( p_xml_reader );
412 if( !psz_name || !*psz_name )
414 msg_Err( p_demux, "invalid xml stream" );
419 for( p_handler = track_elements;
420 p_handler->name && strcmp( psz_name, p_handler->name );
422 if( !p_handler->name )
424 msg_Err( p_demux, "unexpected element <%s>", psz_name );
429 /* complex content is parsed in a separate function */
430 if( p_handler->type == COMPLEX_CONTENT )
435 "at <%s> level no new item has been allocated",
440 if( p_handler->pf_handler.cmplx( p_demux,
457 case XML_READER_TEXT:
458 /* simple element content */
460 psz_value = xml_ReaderValue( p_xml_reader );
463 msg_Err( p_demux, "invalid xml stream" );
469 case XML_READER_ENDELEM:
470 /* element end tag */
471 psz_name = xml_ReaderName( p_xml_reader );
474 msg_Err( p_demux, "invalid xml stream" );
478 /* leave if the current parent node <track> is terminated */
479 if( !strcmp( psz_name, psz_element ) )
482 playlist_BothAddInput( p_playlist, p_new_input,
483 p_demux->p_sys->p_item_in_category,
484 PLAYLIST_APPEND | PLAYLIST_SPREPARSE,
485 PLAYLIST_END, NULL, NULL, VLC_FALSE );
486 if( p_demux->p_sys->i_identifier <
487 p_demux->p_sys->i_tracklist_entries )
489 p_demux->p_sys->pp_tracklist[
490 p_demux->p_sys->i_identifier ] = p_new_input;
494 if( p_demux->p_sys->i_identifier >
495 p_demux->p_sys->i_tracklist_entries )
497 p_demux->p_sys->i_tracklist_entries =
498 p_demux->p_sys->i_identifier;
500 INSERT_ELEM( p_demux->p_sys->pp_tracklist,
501 p_demux->p_sys->i_tracklist_entries,
502 p_demux->p_sys->i_tracklist_entries,
507 /* there MUST have been a start tag for that element name */
508 if( !p_handler || !p_handler->name
509 || strcmp( p_handler->name, psz_name ))
511 msg_Err( p_demux, "there's no open element left for <%s>",
517 /* special case: location */
518 if( !strcmp( p_handler->name, "location" ) )
521 /* there MUST NOT be an item */
524 msg_Err( p_demux, "item <%s> already created",
529 psz_uri = decode_URI_duplicate( psz_value );
533 if( p_demux->p_sys->psz_base &&
534 !strstr( psz_uri, "://" ) )
536 char* psz_tmp = malloc(
537 strlen(p_demux->p_sys->psz_base) +
538 strlen(psz_uri) +1 );
541 msg_Err( p_demux, "out of memory");
544 sprintf( psz_tmp, "%s%s",
545 p_demux->p_sys->psz_base, psz_uri );
549 p_new_input = input_ItemNewExt( p_playlist, psz_uri,
551 p_new_input->p_meta = vlc_meta_New();
553 input_ItemCopyOptions( p_item->p_input, p_new_input );
564 else if( !strcmp( p_handler->name, "identifier" ) )
566 p_demux->p_sys->i_identifier = atoi( psz_value );
570 /* there MUST be an item */
573 msg_Err( p_demux, "item not yet created at <%s>",
578 if( p_handler->pf_handler.smpl )
580 p_handler->pf_handler.smpl( NULL, p_new_input,
591 /* unknown/unexpected xml node */
592 msg_Err( p_demux, "unexpected xml node %i", i_node );
598 msg_Err( p_demux, "unexpected end of xml data" );
604 * \brief handles the supported <track> sub-elements
606 static vlc_bool_t set_item_info SIMPLE_INTERFACE
608 /* exit if setting is impossible */
609 if( !psz_name || !psz_value || !p_input )
613 /* re-convert xml special characters inside psz_value */
614 resolve_xml_special_chars( psz_value );
616 /* handle each info element in a separate "if" clause */
617 if( !strcmp( psz_name, "title" ) )
619 p_input->psz_name = strdup( (char*)psz_value );
621 else if( !strcmp( psz_name, "creator" ) )
623 vlc_meta_SetArtist( p_input->p_meta, psz_value );
625 else if( !strcmp( psz_name, "album" ) )
627 vlc_meta_SetAlbum( p_input->p_meta, psz_value );
630 else if( !strcmp( psz_name, "trackNum" ) )
632 vlc_meta_SetTracknum( p_input->p_meta, psz_value );
634 else if( !strcmp( psz_name, "duration" ) )
636 long i_num = atol( psz_value );
637 p_input->i_duration = i_num*1000;
639 else if( !strcmp( psz_name, "annotation" ) )
641 vlc_meta_SetDescription( p_input->p_meta, psz_value );
648 * \brief parse the extension node of a XSPF playlist
650 static vlc_bool_t parse_extension_node COMPLEX_INTERFACE
652 char *psz_name = NULL;
653 char *psz_value = NULL;
654 char *psz_title = NULL;
656 xml_elem_hnd_t *p_handler = NULL;
658 xml_elem_hnd_t pl_elements[] =
659 { {"node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
660 {"item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
661 {NULL, UNKNOWN_CONTENT, {NULL} }
664 /* read all extension node attributes */
665 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
667 psz_name = xml_ReaderName( p_xml_reader );
668 psz_value = xml_ReaderValue( p_xml_reader );
669 if( !psz_name || !psz_value )
671 msg_Err( p_demux, "invalid xml stream @ <node>" );
675 /* attribute: title */
676 if( !strcmp( psz_name, "title" ) )
678 psz_title = unescape_URI_duplicate( psz_value );
680 /* unknown attribute */
682 msg_Warn( p_demux, "invalid <node> attribute:\"%s\"", psz_name);
687 /* attribute title is mandatory except for <extension> */
688 if( !strcmp( psz_element, "node" ) && !psz_title )
690 msg_Warn( p_demux, "<node> requires \"title\" attribute" );
693 if( psz_title ) free( psz_title );
695 /* parse the child elements */
696 while( xml_ReaderRead( p_xml_reader ) == 1 )
698 i_node = xml_ReaderNodeType( p_xml_reader );
701 case XML_READER_NONE:
703 case XML_READER_STARTELEM:
704 /* element start tag */
705 psz_name = xml_ReaderName( p_xml_reader );
706 if( !psz_name || !*psz_name )
708 msg_Err( p_demux, "invalid xml stream" );
713 for( p_handler = pl_elements;
714 p_handler->name && strcmp( psz_name, p_handler->name );
716 if( !p_handler->name )
718 msg_Err( p_demux, "unexpected element <%s>", psz_name );
723 /* complex content is parsed in a separate function */
724 if( p_handler->type == COMPLEX_CONTENT )
726 if( p_handler->pf_handler.cmplx( p_demux,
743 case XML_READER_TEXT:
744 /* simple element content */
746 psz_value = xml_ReaderValue( p_xml_reader );
749 msg_Err( p_demux, "invalid xml stream" );
755 case XML_READER_ENDELEM:
756 /* element end tag */
757 psz_name = xml_ReaderName( p_xml_reader );
760 msg_Err( p_demux, "invalid xml stream" );
764 /* leave if the current parent node is terminated */
765 if( !strcmp( psz_name, psz_element ) )
770 /* there MUST have been a start tag for that element name */
771 if( !p_handler || !p_handler->name
772 || strcmp( p_handler->name, psz_name ))
774 msg_Err( p_demux, "there's no open element left for <%s>",
780 if( p_handler->pf_handler.smpl )
782 p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
790 /* unknown/unexpected xml node */
791 msg_Err( p_demux, "unexpected xml node %i", i_node );
801 * \brief parse the extension item node of a XSPF playlist
803 static vlc_bool_t parse_extitem_node COMPLEX_INTERFACE
805 char *psz_name = NULL;
806 char *psz_value = NULL;
809 /* read all extension item attributes */
810 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
812 psz_name = xml_ReaderName( p_xml_reader );
813 psz_value = xml_ReaderValue( p_xml_reader );
814 if( !psz_name || !psz_value )
816 msg_Err( p_demux, "invalid xml stream @ <item>" );
820 /* attribute: href */
821 if( !strcmp( psz_name, "href" ) )
823 i_href = atoi( psz_value );
825 /* unknown attribute */
827 msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
832 /* attribute href is mandatory */
835 msg_Warn( p_demux, "<item> requires \"href\" attribute" );
839 if( i_href > p_demux->p_sys->i_tracklist_entries )
841 msg_Warn( p_demux, "invalid \"href\" attribute" );
848 * \brief skips complex element content that we can't manage
850 static vlc_bool_t skip_element COMPLEX_INTERFACE
854 while( xml_ReaderRead( p_xml_reader ) == 1 )
856 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
858 psz_endname = xml_ReaderName( p_xml_reader );
861 if( !strcmp( psz_element, psz_endname ) )