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;
67 for(i = 0; i < p_demux->p_sys->i_tracklist_entries; i++)
69 if(p_demux->p_sys->pp_tracklist[i])
70 vlc_gc_decref( p_demux->p_sys->pp_tracklist[i] );
72 free( p_demux->p_sys->pp_tracklist );
73 free( p_demux->p_sys->psz_base );
74 free( p_demux->p_sys );
78 * \brief demuxer function for XSPF parsing
80 int Demux( demux_t *p_demux )
84 xml_reader_t *p_xml_reader = NULL;
85 char *psz_name = NULL;
87 p_demux->p_sys->pp_tracklist = NULL;
88 p_demux->p_sys->i_tracklist_entries = 0;
89 p_demux->p_sys->i_track_id = -1;
90 p_demux->p_sys->psz_base = NULL;
92 /* create new xml parser from stream */
93 p_xml = xml_Create( p_demux );
97 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
101 /* locating the root node */
104 if( xml_ReaderRead( p_xml_reader ) != 1 )
106 msg_Err( p_demux, "can't read xml stream" );
109 } while( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
111 /* checking root node name */
112 psz_name = xml_ReaderName( p_xml_reader );
113 if( !psz_name || strcmp( psz_name, "playlist" ) )
115 msg_Err( p_demux, "invalid root node name: %s", psz_name );
120 i_ret = parse_playlist_node( p_demux, p_current_input,
121 p_xml_reader, "playlist" ) ? 0 : -1;
123 for( int i = 0 ; i < p_demux->p_sys->i_tracklist_entries ; i++ )
125 input_item_t *p_new_input = p_demux->p_sys->pp_tracklist[i];
128 input_item_AddSubItem( p_current_input, p_new_input );
133 HANDLE_PLAY_AND_RELEASE;
135 xml_ReaderDelete( p_xml, p_xml_reader );
138 return i_ret; /* Needed for correct operation of go back */
141 /** \brief dummy function for demux callback interface */
142 static int Control( demux_t *p_demux, int i_query, va_list args )
144 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
149 * \brief parse the root node of a XSPF playlist
150 * \param p_demux demuxer instance
151 * \param p_input_item current input item
152 * \param p_xml_reader xml reader instance
153 * \param psz_element name of element to parse
155 static bool parse_playlist_node COMPLEX_INTERFACE
157 char *psz_name = NULL;
158 char *psz_value = NULL;
159 bool b_version_found = false;
161 xml_elem_hnd_t *p_handler = NULL;
163 xml_elem_hnd_t pl_elements[] =
164 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
165 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
166 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
167 {"info", SIMPLE_CONTENT, {NULL} },
168 {"location", SIMPLE_CONTENT, {NULL} },
169 {"identifier", SIMPLE_CONTENT, {NULL} },
170 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
171 {"date", SIMPLE_CONTENT, {NULL} },
172 {"license", SIMPLE_CONTENT, {NULL} },
173 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
174 {"link", SIMPLE_CONTENT, {NULL} },
175 {"meta", SIMPLE_CONTENT, {NULL} },
176 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
177 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
178 {NULL, UNKNOWN_CONTENT, {NULL} }
181 /* read all playlist attributes */
182 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
184 psz_name = xml_ReaderName( p_xml_reader );
185 psz_value = xml_ReaderValue( p_xml_reader );
186 if( !psz_name || !psz_value )
188 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
192 /* attribute: version */
193 if( !strcmp( psz_name, "version" ) )
195 b_version_found = true;
196 if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
197 msg_Warn( p_demux, "unsupported XSPF version" );
199 /* attribute: xmlns */
200 else if( !strcmp( psz_name, "xmlns" ) || !strcmp( psz_name, "xmlns:vlc" ) )
202 else if( !strcmp( psz_name, "xml:base" ) )
204 p_demux->p_sys->psz_base = strdup( psz_value );
206 /* unknown attribute */
208 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
212 /* attribute version is mandatory !!! */
213 if( !b_version_found )
214 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
216 /* parse the child elements - we only take care of <trackList> */
217 while( xml_ReaderRead( p_xml_reader ) == 1 )
219 i_node = xml_ReaderNodeType( p_xml_reader );
222 case XML_READER_NONE:
224 case XML_READER_STARTELEM:
225 /* element start tag */
226 psz_name = xml_ReaderName( p_xml_reader );
227 if( !psz_name || !*psz_name )
229 msg_Err( p_demux, "invalid xml stream" );
234 for( p_handler = pl_elements;
235 p_handler->name && strcmp( psz_name, p_handler->name );
237 if( !p_handler->name )
239 msg_Err( p_demux, "unexpected element <%s>", psz_name );
244 /* complex content is parsed in a separate function */
245 if( p_handler->type == COMPLEX_CONTENT )
247 if( p_handler->pf_handler.cmplx( p_demux,
263 case XML_READER_TEXT:
264 /* simple element content */
266 psz_value = xml_ReaderValue( p_xml_reader );
269 msg_Err( p_demux, "invalid xml stream" );
275 case XML_READER_ENDELEM:
276 /* element end tag */
277 psz_name = xml_ReaderName( p_xml_reader );
280 msg_Err( p_demux, "invalid xml stream" );
284 /* leave if the current parent node <playlist> is terminated */
285 if( !strcmp( psz_name, psz_element ) )
290 /* there MUST have been a start tag for that element name */
291 if( !p_handler || !p_handler->name
292 || strcmp( p_handler->name, psz_name ))
294 msg_Err( p_demux, "there's no open element left for <%s>",
300 if( p_handler->pf_handler.smpl )
302 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
310 /* unknown/unexpected xml node */
311 msg_Err( p_demux, "unexpected xml node %i", i_node );
321 * \brief parses the tracklist node which only may contain <track>s
323 static bool parse_tracklist_node COMPLEX_INTERFACE
325 VLC_UNUSED(psz_element);
329 /* now parse the <track>s */
330 while( xml_ReaderRead( p_xml_reader ) == 1 )
332 int i_node = xml_ReaderNodeType( p_xml_reader );
333 if( i_node == XML_READER_STARTELEM )
335 char *psz_eltname = xml_ReaderName( p_xml_reader );
338 msg_Err( p_demux, "unexpected end of xml data" );
342 if( strcmp( psz_eltname, "track") )
344 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
351 /* parse the track data in a separate function */
352 if( parse_track_node( p_demux, p_input_item, p_xml_reader,
356 else if( i_node == XML_READER_ENDELEM )
360 /* the <trackList> has to be terminated */
361 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
363 msg_Err( p_demux, "there's a missing </trackList>" );
366 psz_name = xml_ReaderName( p_xml_reader );
367 if( !psz_name || strcmp( psz_name, "trackList" ) )
369 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
375 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
380 * \brief parse one track element
381 * \param COMPLEX_INTERFACE
383 static bool parse_track_node COMPLEX_INTERFACE
386 char *psz_name = NULL;
387 char *psz_value = NULL;
388 xml_elem_hnd_t *p_handler = NULL;
390 xml_elem_hnd_t track_elements[] =
391 { {"location", SIMPLE_CONTENT, {NULL} },
392 {"identifier", SIMPLE_CONTENT, {NULL} },
393 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
394 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
395 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
396 {"info", SIMPLE_CONTENT, {NULL} },
397 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
398 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
399 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
400 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
401 {"link", SIMPLE_CONTENT, {NULL} },
402 {"meta", SIMPLE_CONTENT, {NULL} },
403 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
404 {NULL, UNKNOWN_CONTENT, {NULL} }
407 input_item_t *p_new_input = input_item_New( p_demux, NULL, NULL );
411 /* malloc has failed for input_item_New, so bailout early */
415 /* reset i_track_id */
416 p_demux->p_sys->i_track_id = -1;
418 while( xml_ReaderRead( p_xml_reader ) == 1 )
420 i_node = xml_ReaderNodeType( p_xml_reader );
423 case XML_READER_NONE:
426 case XML_READER_STARTELEM:
427 /* element start tag */
428 psz_name = xml_ReaderName( p_xml_reader );
429 if( !psz_name || !*psz_name )
431 msg_Err( p_demux, "invalid xml stream" );
436 for( p_handler = track_elements;
437 p_handler->name && strcmp( psz_name, p_handler->name );
439 if( !p_handler->name )
441 msg_Err( p_demux, "unexpected element <%s>", psz_name );
446 /* complex content is parsed in a separate function */
447 if( p_handler->type == COMPLEX_CONTENT )
452 "at <%s> level no new item has been allocated",
457 if( p_handler->pf_handler.cmplx( p_demux,
473 case XML_READER_TEXT:
474 /* simple element content */
476 psz_value = xml_ReaderValue( p_xml_reader );
479 msg_Err( p_demux, "invalid xml stream" );
485 case XML_READER_ENDELEM:
486 /* element end tag */
487 psz_name = xml_ReaderName( p_xml_reader );
490 msg_Err( p_demux, "invalid xml stream" );
495 /* leave if the current parent node <track> is terminated */
496 if( !strcmp( psz_name, psz_element ) )
500 /* Make sure we have a URI */
501 char *psz_uri = input_item_GetURI( p_new_input );
504 input_item_SetURI( p_new_input, "vlc://nop" );
508 if( p_demux->p_sys->i_track_id < 0 )
510 input_item_AddSubItem( p_input_item, p_new_input );
511 vlc_gc_decref( p_new_input );
515 if( p_demux->p_sys->i_track_id >=
516 p_demux->p_sys->i_tracklist_entries )
519 pp = realloc( p_demux->p_sys->pp_tracklist,
520 (p_demux->p_sys->i_track_id + 1) * sizeof(*pp) );
523 p_demux->p_sys->pp_tracklist = pp;
524 while( p_demux->p_sys->i_track_id >=
525 p_demux->p_sys->i_tracklist_entries )
526 pp[p_demux->p_sys->i_tracklist_entries++] = NULL;
529 p_demux->p_sys->pp_tracklist[
530 p_demux->p_sys->i_track_id ] = p_new_input;
533 /* there MUST have been a start tag for that element name */
534 if( !p_handler || !p_handler->name
535 || strcmp( p_handler->name, psz_name ))
537 msg_Err( p_demux, "there's no open element left for <%s>",
543 /* special case: location */
544 if( !strcmp( p_handler->name, "location" ) )
546 /* FIXME: This is broken. Scheme-relative (//...) locations
547 * and anchors (#...) are not resolved correctly. Also,
548 * host-relative (/...) and directory-relative locations
549 * ("relative path" in vernacular) should be resolved.
550 * Last, psz_base should default to the XSPF resource
551 * location if missing (not the current working directory).
553 if( p_demux->p_sys->psz_base && !strstr( psz_value, "://" ) )
556 if( asprintf( &psz_tmp, "%s%s",
557 p_demux->p_sys->psz_base, psz_value ) == -1 )
562 input_item_SetURI( p_new_input, psz_tmp );
566 input_item_SetURI( p_new_input, psz_value );
567 input_item_CopyOptions( p_input_item, p_new_input );
573 /* there MUST be an item */
576 msg_Err( p_demux, "item not yet created at <%s>",
581 if( p_handler->pf_handler.smpl )
583 p_handler->pf_handler.smpl( p_new_input,
594 /* unknown/unexpected xml node */
595 msg_Err( p_demux, "unexpected xml node %i", i_node );
601 msg_Err( p_demux, "unexpected end of xml data" );
607 * \brief handles the supported <track> sub-elements
609 static bool set_item_info SIMPLE_INTERFACE
611 /* exit if setting is impossible */
612 if( !psz_name || !psz_value || !p_input )
616 /* re-convert xml special characters inside psz_value */
617 resolve_xml_special_chars( psz_value );
619 /* handle each info element in a separate "if" clause */
620 if( !strcmp( psz_name, "title" ) )
622 input_item_SetTitle( p_input, psz_value );
624 else if( !strcmp( psz_name, "creator" ) )
626 input_item_SetArtist( p_input, psz_value );
628 else if( !strcmp( psz_name, "album" ) )
630 input_item_SetAlbum( p_input, psz_value );
633 else if( !strcmp( psz_name, "trackNum" ) )
635 input_item_SetTrackNum( p_input, psz_value );
637 else if( !strcmp( psz_name, "duration" ) )
639 long i_num = atol( psz_value );
640 input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
642 else if( !strcmp( psz_name, "annotation" ) )
644 input_item_SetDescription( p_input, psz_value );
646 else if( !strcmp( psz_name, "image" ) )
648 input_item_SetArtURL( p_input, psz_value );
654 * \brief handles the <vlc:option> elements
656 static bool set_option SIMPLE_INTERFACE
658 /* exit if setting is impossible */
659 if( !psz_name || !psz_value || !p_input )
662 /* re-convert xml special characters inside psz_value */
663 resolve_xml_special_chars( psz_value );
665 input_item_AddOption( p_input, psz_value, 0 );
671 * \brief parse the extension node of a XSPF playlist
673 static bool parse_extension_node COMPLEX_INTERFACE
675 char *psz_name = NULL;
676 char *psz_value = NULL;
677 char *psz_title = NULL;
678 char *psz_application = NULL;
680 bool b_release_input_item = false;
681 xml_elem_hnd_t *p_handler = NULL;
682 input_item_t *p_new_input = NULL;
684 xml_elem_hnd_t pl_elements[] =
685 { {"vlc:node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
686 {"vlc:item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
687 {"vlc:id", SIMPLE_CONTENT, {NULL} },
688 {"vlc:option", SIMPLE_CONTENT, {.smpl = set_option} },
689 {NULL, UNKNOWN_CONTENT, {NULL} }
692 /* read all extension node attributes */
693 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
695 psz_name = xml_ReaderName( p_xml_reader );
696 psz_value = xml_ReaderValue( p_xml_reader );
697 if( !psz_name || !psz_value )
699 msg_Err( p_demux, "invalid xml stream @ <vlc:node>" );
703 /* attribute: title */
704 if( !strcmp( psz_name, "title" ) )
706 resolve_xml_special_chars( psz_value );
707 psz_title = psz_value;
709 /* extension attribute: application */
710 else if( !strcmp( psz_name, "application" ) )
712 psz_application = psz_value;
714 /* unknown attribute */
717 msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element,
725 /* attribute title is mandatory except for <extension> */
726 if( !strcmp( psz_element, "vlc:node" ) )
730 msg_Warn( p_demux, "<vlc:node> requires \"title\" attribute" );
733 p_new_input = input_item_NewWithType( VLC_OBJECT( p_demux ),
734 "vlc://nop", psz_title, 0, NULL, 0, -1,
735 ITEM_TYPE_DIRECTORY );
738 input_item_AddSubItem( p_input_item, p_new_input );
739 p_input_item = p_new_input;
740 b_release_input_item = true;
744 else if( !strcmp( psz_element, "extension" ) )
746 if( !psz_application )
748 msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
751 else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
753 msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
754 free( psz_application );
758 free( psz_application );
760 /* parse the child elements */
761 while( xml_ReaderRead( p_xml_reader ) == 1 )
763 i_node = xml_ReaderNodeType( p_xml_reader );
766 case XML_READER_NONE:
768 case XML_READER_STARTELEM:
769 /* element start tag */
770 psz_name = xml_ReaderName( p_xml_reader );
771 if( !psz_name || !*psz_name )
773 msg_Err( p_demux, "invalid xml stream" );
775 if( b_release_input_item ) vlc_gc_decref( p_new_input );
779 for( p_handler = pl_elements;
780 p_handler->name && strcmp( psz_name, p_handler->name );
782 if( !p_handler->name )
784 msg_Err( p_demux, "unexpected element <%s>", psz_name );
786 if( b_release_input_item ) vlc_gc_decref( p_new_input );
790 /* complex content is parsed in a separate function */
791 if( p_handler->type == COMPLEX_CONTENT )
793 if( p_handler->pf_handler.cmplx( p_demux,
804 if( b_release_input_item ) vlc_gc_decref( p_new_input );
810 case XML_READER_TEXT:
811 /* simple element content */
813 psz_value = xml_ReaderValue( p_xml_reader );
816 msg_Err( p_demux, "invalid xml stream" );
818 if( b_release_input_item ) vlc_gc_decref( p_new_input );
823 case XML_READER_ENDELEM:
824 /* element end tag */
825 psz_name = xml_ReaderName( p_xml_reader );
828 msg_Err( p_demux, "invalid xml stream" );
830 if( b_release_input_item ) vlc_gc_decref( p_new_input );
833 /* leave if the current parent node is terminated */
834 if( !strcmp( psz_name, psz_element ) )
837 if( b_release_input_item ) vlc_gc_decref( p_new_input );
840 /* there MUST have been a start tag for that element name */
841 if( !p_handler || !p_handler->name
842 || strcmp( p_handler->name, psz_name ))
844 msg_Err( p_demux, "there's no open element left for <%s>",
847 if( b_release_input_item ) vlc_gc_decref( p_new_input );
851 /* special tag <vlc:id> */
852 if( !strcmp( p_handler->name, "vlc:id" ) )
854 p_demux->p_sys->i_track_id = atoi( psz_value );
856 else if( p_handler->pf_handler.smpl )
858 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
866 /* unknown/unexpected xml node */
867 msg_Err( p_demux, "unexpected xml node %i", i_node );
869 if( b_release_input_item ) vlc_gc_decref( p_new_input );
874 if( b_release_input_item ) vlc_gc_decref( p_new_input );
879 * \brief parse the extension item node of a XSPF playlist
881 static bool parse_extitem_node COMPLEX_INTERFACE
883 VLC_UNUSED(psz_element);
884 input_item_t *p_new_input = NULL;
885 char *psz_name = NULL;
886 char *psz_value = NULL;
889 /* read all extension item attributes */
890 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
892 psz_name = xml_ReaderName( p_xml_reader );
893 psz_value = xml_ReaderValue( p_xml_reader );
894 if( !psz_name || !psz_value )
896 msg_Err( p_demux, "invalid xml stream @ <vlc:item>" );
900 /* attribute: href */
901 if( !strcmp( psz_name, "tid" ) )
903 i_tid = atoi( psz_value );
905 /* unknown attribute */
907 msg_Warn( p_demux, "invalid <vlc:item> attribute:\"%s\"", psz_name);
912 /* attribute href is mandatory */
915 msg_Warn( p_demux, "<vlc:item> requires \"tid\" attribute" );
919 if( i_tid >= p_demux->p_sys->i_tracklist_entries )
921 msg_Warn( p_demux, "invalid \"tid\" attribute" );
925 p_new_input = p_demux->p_sys->pp_tracklist[ i_tid ];
928 input_item_AddSubItem( p_input_item, p_new_input );
929 vlc_gc_decref( p_new_input );
930 p_demux->p_sys->pp_tracklist[i_tid] = NULL;
933 /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
934 /* (libxml sends NONE) */
935 xml_ReaderRead( p_xml_reader );
941 * \brief skips complex element content that we can't manage
943 static bool skip_element COMPLEX_INTERFACE
945 VLC_UNUSED(p_demux); VLC_UNUSED(p_input_item);
948 while( xml_ReaderRead( p_xml_reader ) == 1 )
950 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
952 psz_endname = xml_ReaderName( p_xml_reader );
955 if( !strcmp( psz_element, psz_endname ) )