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->pp_tracklist = NULL;
79 p_demux->p_sys->i_tracklist_entries = 0;
80 p_demux->p_sys->i_identifier = 0;
81 p_demux->p_sys->psz_base = NULL;
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 if( i_ret == VLC_SUCCESS )
120 i_ret = parse_playlist_node( p_demux, p_playlist, p_current_input,
121 p_xml_reader, "playlist" )
122 ? VLC_SUCCESS : VLC_EGENERIC;
123 HANDLE_PLAY_AND_RELEASE;
125 xml_ReaderDelete( p_xml, p_xml_reader );
128 return i_ret; /* Needed for correct operation of go back */
131 /** \brief dummy function for demux callback interface */
132 static int Control( demux_t *p_demux, int i_query, va_list args )
138 * \brief parse the root node of a XSPF playlist
139 * \param p_demux demuxer instance
140 * \param p_playlist playlist instance
141 * \param p_input_item 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, {.smpl = set_item_info} },
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_input_item, 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_input_item,
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, {.smpl = set_item_info} },
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 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 if( p_demux->p_sys->psz_base &&
530 !strstr( psz_uri, "://" ) )
532 char* psz_tmp = malloc(
533 strlen(p_demux->p_sys->psz_base) +
534 strlen(psz_uri) +1 );
537 msg_Err( p_demux, "out of memory");
540 sprintf( psz_tmp, "%s%s",
541 p_demux->p_sys->psz_base, psz_uri );
545 p_new_input = input_ItemNewExt( p_playlist, psz_uri,
548 input_ItemCopyOptions( p_input_item, p_new_input );
559 else if( !strcmp( p_handler->name, "identifier" ) )
561 p_demux->p_sys->i_identifier = atoi( psz_value );
565 /* there MUST be an item */
568 msg_Err( p_demux, "item not yet created at <%s>",
573 if( p_handler->pf_handler.smpl )
575 p_handler->pf_handler.smpl( p_new_input,
586 /* unknown/unexpected xml node */
587 msg_Err( p_demux, "unexpected xml node %i", i_node );
593 msg_Err( p_demux, "unexpected end of xml data" );
599 * \brief handles the supported <track> sub-elements
601 static vlc_bool_t set_item_info SIMPLE_INTERFACE
603 /* exit if setting is impossible */
604 if( !psz_name || !psz_value || !p_input )
608 /* re-convert xml special characters inside psz_value */
609 resolve_xml_special_chars( psz_value );
611 /* handle each info element in a separate "if" clause */
612 if( !strcmp( psz_name, "title" ) )
614 input_item_SetTitle( p_input, psz_value );
616 else if( !strcmp( psz_name, "creator" ) )
618 input_item_SetArtist( p_input, psz_value );
620 else if( !strcmp( psz_name, "album" ) )
622 input_item_SetAlbum( p_input, psz_value );
625 else if( !strcmp( psz_name, "trackNum" ) )
627 input_item_SetTrackNum( p_input, psz_value );
629 else if( !strcmp( psz_name, "duration" ) )
631 long i_num = atol( psz_value );
632 input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
634 else if( !strcmp( psz_name, "annotation" ) )
636 input_item_SetDescription( p_input, psz_value );
638 else if( !strcmp( psz_name, "image" ) )
640 char *psz_uri = decode_URI_duplicate( psz_value );
641 input_item_SetArtURL( p_input, psz_uri );
649 * \brief parse the extension node of a XSPF playlist
651 static vlc_bool_t parse_extension_node COMPLEX_INTERFACE
653 char *psz_name = NULL;
654 char *psz_value = NULL;
655 char *psz_title = NULL;
656 char *psz_application = NULL;
658 xml_elem_hnd_t *p_handler = NULL;
659 input_item_t *p_new_input = NULL;
661 xml_elem_hnd_t pl_elements[] =
662 { {"node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
663 {"item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
664 {NULL, UNKNOWN_CONTENT, {NULL} }
667 /* read all extension node attributes */
668 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
670 psz_name = xml_ReaderName( p_xml_reader );
671 psz_value = xml_ReaderValue( p_xml_reader );
672 if( !psz_name || !psz_value )
674 msg_Err( p_demux, "invalid xml stream @ <node>" );
678 /* attribute: title */
679 if( !strcmp( psz_name, "title" ) )
681 resolve_xml_special_chars( psz_value );
682 psz_title = strdup( psz_value );
684 /* extension attribute: application */
685 else if( !strcmp( psz_name, "application" ) )
687 psz_application = strdup( psz_value );
689 /* unknown attribute */
691 msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element, psz_name );
696 /* attribute title is mandatory except for <extension> */
697 if( !strcmp( psz_element, "node" ) )
701 msg_Warn( p_demux, "<node> requires \"title\" attribute" );
704 p_new_input = input_ItemNewWithType( VLC_OBJECT( p_playlist ), "vlc:skip",
705 psz_title, 0, NULL, -1, ITEM_TYPE_DIRECTORY );
708 input_ItemAddSubItem( p_input_item, p_new_input );
709 p_input_item = p_new_input;
713 else if( !strcmp( psz_element, "extension" ) )
715 if( !psz_application )
717 msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
720 else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
722 msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
723 free( psz_application );
727 free( psz_application );
729 /* parse the child elements */
730 while( xml_ReaderRead( p_xml_reader ) == 1 )
732 i_node = xml_ReaderNodeType( p_xml_reader );
735 case XML_READER_NONE:
737 case XML_READER_STARTELEM:
738 /* element start tag */
739 psz_name = xml_ReaderName( p_xml_reader );
740 if( !psz_name || !*psz_name )
742 msg_Err( p_demux, "invalid xml stream" );
747 for( p_handler = pl_elements;
748 p_handler->name && strcmp( psz_name, p_handler->name );
750 if( !p_handler->name )
752 msg_Err( p_demux, "unexpected element <%s>", psz_name );
757 /* complex content is parsed in a separate function */
758 if( p_handler->type == COMPLEX_CONTENT )
760 if( p_handler->pf_handler.cmplx( p_demux,
777 case XML_READER_TEXT:
778 /* simple element content */
780 psz_value = xml_ReaderValue( p_xml_reader );
783 msg_Err( p_demux, "invalid xml stream" );
789 case XML_READER_ENDELEM:
790 /* element end tag */
791 psz_name = xml_ReaderName( p_xml_reader );
794 msg_Err( p_demux, "invalid xml stream" );
798 /* leave if the current parent node is terminated */
799 if( !strcmp( psz_name, psz_element ) )
804 /* there MUST have been a start tag for that element name */
805 if( !p_handler || !p_handler->name
806 || strcmp( p_handler->name, psz_name ))
808 msg_Err( p_demux, "there's no open element left for <%s>",
814 if( p_handler->pf_handler.smpl )
816 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
824 /* unknown/unexpected xml node */
825 msg_Err( p_demux, "unexpected xml node %i", i_node );
835 * \brief parse the extension item node of a XSPF playlist
837 static vlc_bool_t parse_extitem_node COMPLEX_INTERFACE
839 input_item_t *p_new_input = NULL;
840 char *psz_name = NULL;
841 char *psz_value = NULL;
844 /* read all extension item attributes */
845 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
847 psz_name = xml_ReaderName( p_xml_reader );
848 psz_value = xml_ReaderValue( p_xml_reader );
849 if( !psz_name || !psz_value )
851 msg_Err( p_demux, "invalid xml stream @ <item>" );
855 /* attribute: href */
856 if( !strcmp( psz_name, "href" ) )
858 i_href = atoi( psz_value );
860 /* unknown attribute */
862 msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
867 /* attribute href is mandatory */
870 msg_Warn( p_demux, "<item> requires \"href\" attribute" );
874 if( i_href >= p_demux->p_sys->i_tracklist_entries )
876 msg_Warn( p_demux, "invalid \"href\" attribute" );
880 p_new_input = p_demux->p_sys->pp_tracklist[ i_href ];
883 input_ItemAddSubItem( p_input_item, p_new_input );
886 /* fix for #1293 - XTAG sends ENDELEM for self closing tag */
887 /* (libxml sends NONE) */
888 xml_ReaderRead( p_xml_reader );
894 * \brief skips complex element content that we can't manage
896 static vlc_bool_t skip_element COMPLEX_INTERFACE
900 while( xml_ReaderRead( p_xml_reader ) == 1 )
902 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
904 psz_endname = xml_ReaderName( p_xml_reader );
907 if( !strcmp( psz_element, psz_endname ) )