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
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
37 #include <vlc_strings.h>
44 input_item_t **pp_tracklist;
45 int i_tracklist_entries;
50 static int Control( demux_t *, int, va_list );
51 static int Demux( demux_t * );
54 * \brief XSPF submodule initialization function
56 int Import_xspf( vlc_object_t *p_this )
58 DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xspf", "xspf-open",
59 "using XSPF playlist reader" );
63 void Close_xspf( vlc_object_t *p_this )
65 demux_t *p_demux = (demux_t *)p_this;
66 FREENULL( p_demux->p_sys->pp_tracklist );
67 FREENULL( p_demux->p_sys->psz_base );
68 free( p_demux->p_sys );
72 * \brief demuxer function for XSPF parsing
74 int Demux( demux_t *p_demux )
78 xml_reader_t *p_xml_reader = NULL;
79 char *psz_name = NULL;
81 p_demux->p_sys->pp_tracklist = NULL;
82 p_demux->p_sys->i_tracklist_entries = 0;
83 p_demux->p_sys->i_identifier = 0;
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 */
102 if( xml_ReaderRead( p_xml_reader ) != 1 )
104 msg_Err( p_demux, "can't read xml stream" );
107 } while( i_ret == VLC_SUCCESS &&
108 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
110 /* checking root node name */
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 );
123 i_ret = parse_playlist_node( p_demux, p_current_input,
124 p_xml_reader, "playlist" ) ? 0 : -1;
127 for( i = 0 ; i < p_demux->p_sys->i_tracklist_entries ; i++ )
129 input_item_t *p_new_input = p_demux->p_sys->pp_tracklist[i];
132 input_ItemAddSubItem( p_current_input, p_new_input );
136 HANDLE_PLAY_AND_RELEASE;
138 xml_ReaderDelete( p_xml, p_xml_reader );
141 return i_ret; /* Needed for correct operation of go back */
144 /** \brief dummy function for demux callback interface */
145 static int Control( demux_t *p_demux, int i_query, va_list args )
147 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
152 * \brief parse the root node of a XSPF playlist
153 * \param p_demux demuxer instance
154 * \param p_input_item current input item
155 * \param p_xml_reader xml reader instance
156 * \param psz_element name of element to parse
158 static bool parse_playlist_node COMPLEX_INTERFACE
161 char *psz_value=NULL;
162 bool b_version_found = false;
164 xml_elem_hnd_t *p_handler=NULL;
166 xml_elem_hnd_t pl_elements[] =
167 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
168 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
169 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
170 {"info", SIMPLE_CONTENT, {NULL} },
171 {"location", SIMPLE_CONTENT, {NULL} },
172 {"identifier", SIMPLE_CONTENT, {NULL} },
173 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
174 {"date", SIMPLE_CONTENT, {NULL} },
175 {"license", SIMPLE_CONTENT, {NULL} },
176 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
177 {"link", SIMPLE_CONTENT, {NULL} },
178 {"meta", SIMPLE_CONTENT, {NULL} },
179 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
180 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
181 {NULL, UNKNOWN_CONTENT, {NULL} }
184 /* read all playlist attributes */
185 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
187 psz_name = xml_ReaderName( p_xml_reader );
188 psz_value = xml_ReaderValue( p_xml_reader );
189 if( !psz_name || !psz_value )
191 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
195 /* attribute: version */
196 if( !strcmp( psz_name, "version" ) )
198 b_version_found = true;
199 if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
200 msg_Warn( p_demux, "unsupported XSPF version" );
202 /* attribute: xmlns */
203 else if( !strcmp( psz_name, "xmlns" ) )
205 else if( !strcmp( psz_name, "xml:base" ) )
207 p_demux->p_sys->psz_base = decode_URI_duplicate( psz_value );
209 /* unknown attribute */
211 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
215 /* attribute version is mandatory !!! */
216 if( !b_version_found )
217 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
219 /* parse the child elements - we only take care of <trackList> */
220 while( xml_ReaderRead( p_xml_reader ) == 1 )
222 i_node = xml_ReaderNodeType( p_xml_reader );
225 case XML_READER_NONE:
227 case XML_READER_STARTELEM:
228 /* element start tag */
229 psz_name = xml_ReaderName( p_xml_reader );
230 if( !psz_name || !*psz_name )
232 msg_Err( p_demux, "invalid xml stream" );
237 for( p_handler = pl_elements;
238 p_handler->name && strcmp( psz_name, p_handler->name );
240 if( !p_handler->name )
242 msg_Err( p_demux, "unexpected element <%s>", psz_name );
247 /* complex content is parsed in a separate function */
248 if( p_handler->type == COMPLEX_CONTENT )
250 if( p_handler->pf_handler.cmplx( p_demux,
266 case XML_READER_TEXT:
267 /* simple element content */
269 psz_value = xml_ReaderValue( p_xml_reader );
272 msg_Err( p_demux, "invalid xml stream" );
278 case XML_READER_ENDELEM:
279 /* element end tag */
280 psz_name = xml_ReaderName( p_xml_reader );
283 msg_Err( p_demux, "invalid xml stream" );
287 /* leave if the current parent node <playlist> is terminated */
288 if( !strcmp( psz_name, psz_element ) )
293 /* there MUST have been a start tag for that element name */
294 if( !p_handler || !p_handler->name
295 || strcmp( p_handler->name, psz_name ))
297 msg_Err( p_demux, "there's no open element left for <%s>",
303 if( p_handler->pf_handler.smpl )
305 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
313 /* unknown/unexpected xml node */
314 msg_Err( p_demux, "unexpected xml node %i", i_node );
324 * \brief parses the tracklist node which only may contain <track>s
326 static bool parse_tracklist_node COMPLEX_INTERFACE
328 VLC_UNUSED(psz_element);
333 /* now parse the <track>s */
334 while( xml_ReaderRead( p_xml_reader ) == 1 )
336 i_node = xml_ReaderNodeType( p_xml_reader );
337 if( i_node == XML_READER_STARTELEM )
339 psz_name = xml_ReaderName( p_xml_reader );
342 msg_Err( p_demux, "unexpected end of xml data" );
346 if( strcmp( psz_name, "track") )
348 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
355 /* parse the track data in a separate function */
356 if( parse_track_node( p_demux, p_input_item,
357 p_xml_reader,"track" ) == true )
360 else if( i_node == XML_READER_ENDELEM )
364 /* the <trackList> has to be terminated */
365 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
367 msg_Err( p_demux, "there's a missing </trackList>" );
371 psz_name = xml_ReaderName( p_xml_reader );
372 if( !psz_name || strcmp( psz_name, "trackList" ) )
374 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
380 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
386 * \brief parse one track element
387 * \param COMPLEX_INTERFACE
389 static bool parse_track_node COMPLEX_INTERFACE
391 input_item_t *p_new_input = NULL;
394 char *psz_value=NULL;
395 xml_elem_hnd_t *p_handler=NULL;
397 xml_elem_hnd_t track_elements[] =
398 { {"location", SIMPLE_CONTENT, {NULL} },
399 {"identifier", SIMPLE_CONTENT, {NULL} },
400 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
401 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
402 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
403 {"info", SIMPLE_CONTENT, {NULL} },
404 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
405 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
406 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
407 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
408 {"link", SIMPLE_CONTENT, {NULL} },
409 {"meta", SIMPLE_CONTENT, {NULL} },
410 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
411 {NULL, UNKNOWN_CONTENT, {NULL} }
414 while( xml_ReaderRead( p_xml_reader ) == 1 )
416 i_node = xml_ReaderNodeType( p_xml_reader );
419 case XML_READER_NONE:
422 case XML_READER_STARTELEM:
423 /* element start tag */
424 psz_name = xml_ReaderName( p_xml_reader );
425 if( !psz_name || !*psz_name )
427 msg_Err( p_demux, "invalid xml stream" );
432 for( p_handler = track_elements;
433 p_handler->name && strcmp( psz_name, p_handler->name );
435 if( !p_handler->name )
437 msg_Err( p_demux, "unexpected element <%s>", psz_name );
442 /* complex content is parsed in a separate function */
443 if( p_handler->type == COMPLEX_CONTENT )
448 "at <%s> level no new item has been allocated",
453 if( p_handler->pf_handler.cmplx( p_demux,
469 case XML_READER_TEXT:
470 /* simple element content */
472 psz_value = xml_ReaderValue( p_xml_reader );
475 msg_Err( p_demux, "invalid xml stream" );
481 case XML_READER_ENDELEM:
482 /* element end tag */
483 psz_name = xml_ReaderName( p_xml_reader );
486 msg_Err( p_demux, "invalid xml stream" );
490 /* leave if the current parent node <track> is terminated */
491 if( !strcmp( psz_name, psz_element ) )
494 if( p_demux->p_sys->i_identifier <
495 p_demux->p_sys->i_tracklist_entries )
497 p_demux->p_sys->pp_tracklist[
498 p_demux->p_sys->i_identifier ] = p_new_input;
502 if( p_demux->p_sys->i_identifier >
503 p_demux->p_sys->i_tracklist_entries )
505 p_demux->p_sys->i_tracklist_entries =
506 p_demux->p_sys->i_identifier;
508 INSERT_ELEM( p_demux->p_sys->pp_tracklist,
509 p_demux->p_sys->i_tracklist_entries,
510 p_demux->p_sys->i_tracklist_entries,
515 /* there MUST have been a start tag for that element name */
516 if( !p_handler || !p_handler->name
517 || strcmp( p_handler->name, psz_name ))
519 msg_Err( p_demux, "there's no open element left for <%s>",
525 /* special case: location */
526 if( !strcmp( p_handler->name, "location" ) )
529 /* there MUST NOT be an item */
532 msg_Err( p_demux, "item <%s> already created",
537 psz_uri = decode_URI_duplicate( psz_value );
541 if( p_demux->p_sys->psz_base &&
542 !strstr( psz_uri, "://" ) )
544 char* psz_tmp = malloc(
545 strlen(p_demux->p_sys->psz_base) +
546 strlen(psz_uri) +1 );
549 sprintf( psz_tmp, "%s%s",
550 p_demux->p_sys->psz_base, psz_uri );
554 p_new_input = input_ItemNewExt( p_demux, psz_uri,
557 input_ItemCopyOptions( p_input_item, p_new_input );
568 else if( !strcmp( p_handler->name, "identifier" ) )
570 p_demux->p_sys->i_identifier = atoi( psz_value );
574 /* there MUST be an item */
577 msg_Err( p_demux, "item not yet created at <%s>",
582 if( p_handler->pf_handler.smpl )
584 p_handler->pf_handler.smpl( p_new_input,
595 /* unknown/unexpected xml node */
596 msg_Err( p_demux, "unexpected xml node %i", i_node );
602 msg_Err( p_demux, "unexpected end of xml data" );
608 * \brief handles the supported <track> sub-elements
610 static bool set_item_info SIMPLE_INTERFACE
612 /* exit if setting is impossible */
613 if( !psz_name || !psz_value || !p_input )
617 /* re-convert xml special characters inside psz_value */
618 resolve_xml_special_chars( psz_value );
620 /* handle each info element in a separate "if" clause */
621 if( !strcmp( psz_name, "title" ) )
623 input_item_SetTitle( p_input, psz_value );
625 else if( !strcmp( psz_name, "creator" ) )
627 input_item_SetArtist( p_input, psz_value );
629 else if( !strcmp( psz_name, "album" ) )
631 input_item_SetAlbum( p_input, psz_value );
634 else if( !strcmp( psz_name, "trackNum" ) )
636 input_item_SetTrackNum( p_input, psz_value );
638 else if( !strcmp( psz_name, "duration" ) )
640 long i_num = atol( psz_value );
641 input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
643 else if( !strcmp( psz_name, "annotation" ) )
645 input_item_SetDescription( p_input, psz_value );
647 else if( !strcmp( psz_name, "image" ) )
649 char *psz_uri = decode_URI_duplicate( psz_value );
650 input_item_SetArtURL( p_input, psz_uri );
657 * \brief handles the <option> elements
659 static bool set_option SIMPLE_INTERFACE
661 /* exit if setting is impossible */
662 if( !psz_name || !psz_value || !p_input )
665 /* re-convert xml special characters inside psz_value */
666 resolve_xml_special_chars( psz_value );
668 input_ItemAddOpt( p_input, psz_value, 0 );
674 * \brief parse the extension node of a XSPF playlist
676 static bool parse_extension_node COMPLEX_INTERFACE
678 char *psz_name = NULL;
679 char *psz_value = NULL;
680 char *psz_title = NULL;
681 char *psz_application = NULL;
683 xml_elem_hnd_t *p_handler = NULL;
684 input_item_t *p_new_input = NULL;
686 xml_elem_hnd_t pl_elements[] =
687 { {"node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
688 {"item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
689 {"option", SIMPLE_CONTENT, {.smpl = set_option} },
690 {NULL, UNKNOWN_CONTENT, {NULL} }
693 /* read all extension node attributes */
694 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
696 psz_name = xml_ReaderName( p_xml_reader );
697 psz_value = xml_ReaderValue( p_xml_reader );
698 if( !psz_name || !psz_value )
700 msg_Err( p_demux, "invalid xml stream @ <node>" );
704 /* attribute: title */
705 if( !strcmp( psz_name, "title" ) )
707 resolve_xml_special_chars( psz_value );
708 psz_title = strdup( psz_value );
710 /* extension attribute: application */
711 else if( !strcmp( psz_name, "application" ) )
713 psz_application = strdup( psz_value );
715 /* unknown attribute */
717 msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element, psz_name );
722 /* attribute title is mandatory except for <extension> */
723 if( !strcmp( psz_element, "node" ) )
727 msg_Warn( p_demux, "<node> requires \"title\" attribute" );
730 p_new_input = input_ItemNewWithType( VLC_OBJECT( p_demux ), "vlc://nop",
731 psz_title, 0, NULL, -1, ITEM_TYPE_DIRECTORY );
734 input_ItemAddSubItem( p_input_item, p_new_input );
735 p_input_item = p_new_input;
736 vlc_gc_decref( p_new_input );
740 else if( !strcmp( psz_element, "extension" ) )
742 if( !psz_application )
744 msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
747 else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
749 msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
750 free( psz_application );
754 free( psz_application );
756 /* parse the child elements */
757 while( xml_ReaderRead( p_xml_reader ) == 1 )
759 i_node = xml_ReaderNodeType( p_xml_reader );
762 case XML_READER_NONE:
764 case XML_READER_STARTELEM:
765 /* element start tag */
766 psz_name = xml_ReaderName( p_xml_reader );
767 if( !psz_name || !*psz_name )
769 msg_Err( p_demux, "invalid xml stream" );
774 for( p_handler = pl_elements;
775 p_handler->name && strcmp( psz_name, p_handler->name );
777 if( !p_handler->name )
779 msg_Err( p_demux, "unexpected element <%s>", psz_name );
784 /* complex content is parsed in a separate function */
785 if( p_handler->type == COMPLEX_CONTENT )
787 if( p_handler->pf_handler.cmplx( p_demux,
803 case XML_READER_TEXT:
804 /* simple element content */
806 psz_value = xml_ReaderValue( p_xml_reader );
809 msg_Err( p_demux, "invalid xml stream" );
815 case XML_READER_ENDELEM:
816 /* element end tag */
817 psz_name = xml_ReaderName( p_xml_reader );
820 msg_Err( p_demux, "invalid xml stream" );
824 /* leave if the current parent node is terminated */
825 if( !strcmp( psz_name, psz_element ) )
830 /* there MUST have been a start tag for that element name */
831 if( !p_handler || !p_handler->name
832 || strcmp( p_handler->name, psz_name ))
834 msg_Err( p_demux, "there's no open element left for <%s>",
840 if( p_handler->pf_handler.smpl )
842 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
850 /* unknown/unexpected xml node */
851 msg_Err( p_demux, "unexpected xml node %i", i_node );
861 * \brief parse the extension item node of a XSPF playlist
863 static bool parse_extitem_node COMPLEX_INTERFACE
865 VLC_UNUSED(psz_element);
866 input_item_t *p_new_input = NULL;
867 char *psz_name = NULL;
868 char *psz_value = NULL;
871 /* read all extension item attributes */
872 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
874 psz_name = xml_ReaderName( p_xml_reader );
875 psz_value = xml_ReaderValue( p_xml_reader );
876 if( !psz_name || !psz_value )
878 msg_Err( p_demux, "invalid xml stream @ <item>" );
882 /* attribute: href */
883 if( !strcmp( psz_name, "href" ) )
885 i_href = atoi( psz_value );
887 /* unknown attribute */
889 msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
894 /* attribute href is mandatory */
897 msg_Warn( p_demux, "<item> requires \"href\" attribute" );
901 if( i_href >= p_demux->p_sys->i_tracklist_entries )
903 msg_Warn( p_demux, "invalid \"href\" attribute" );
907 p_new_input = p_demux->p_sys->pp_tracklist[ i_href ];
910 input_ItemAddSubItem( p_input_item, p_new_input );
911 vlc_gc_decref( p_new_input );
912 p_demux->p_sys->pp_tracklist[i_href] = NULL;
915 /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
916 /* (libxml sends NONE) */
917 xml_ReaderRead( p_xml_reader );
923 * \brief skips complex element content that we can't manage
925 static bool skip_element COMPLEX_INTERFACE
927 VLC_UNUSED(p_demux); VLC_UNUSED(p_input_item);
930 while( xml_ReaderRead( p_xml_reader ) == 1 )
932 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
934 psz_endname = xml_ReaderName( p_xml_reader );
937 if( !strcmp( psz_element, psz_endname ) )