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;
86 input_item_t *p_current_input = GetCurrentItem(p_demux);
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 vlc_gc_decref(p_current_input);
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;
162 xml_elem_hnd_t *p_handler = NULL;
164 xml_elem_hnd_t pl_elements[] =
165 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
166 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
167 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
168 {"info", SIMPLE_CONTENT, {NULL} },
169 {"location", SIMPLE_CONTENT, {NULL} },
170 {"identifier", SIMPLE_CONTENT, {NULL} },
171 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
172 {"date", SIMPLE_CONTENT, {NULL} },
173 {"license", SIMPLE_CONTENT, {NULL} },
174 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
175 {"link", SIMPLE_CONTENT, {NULL} },
176 {"meta", SIMPLE_CONTENT, {NULL} },
177 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
178 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
179 {NULL, UNKNOWN_CONTENT, {NULL} }
182 /* read all playlist attributes */
183 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
185 psz_name = xml_ReaderName( p_xml_reader );
186 psz_value = xml_ReaderValue( p_xml_reader );
187 if( !psz_name || !psz_value )
189 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);
213 /* attribute version is mandatory !!! */
214 if( !b_version_found )
215 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
217 /* 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" );
236 for( p_handler = pl_elements;
237 p_handler->name && strcmp( psz_name, p_handler->name );
239 if( !p_handler->name )
241 msg_Err( p_demux, "unexpected element <%s>", psz_name );
245 /* complex content is parsed in a separate function */
246 if( p_handler->type == COMPLEX_CONTENT )
249 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" );
274 case XML_READER_ENDELEM:
275 /* element end tag */
276 psz_name = xml_ReaderName( p_xml_reader );
279 msg_Err( p_demux, "invalid xml stream" );
282 /* leave if the current parent node <playlist> is terminated */
283 if( !strcmp( psz_name, psz_element ) )
288 /* there MUST have been a start tag for that element name */
289 if( !p_handler || !p_handler->name
290 || strcmp( p_handler->name, psz_name ))
292 msg_Err( p_demux, "there's no open element left for <%s>",
297 if( p_handler->pf_handler.smpl )
299 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
307 /* unknown/unexpected xml node */
308 msg_Err( p_demux, "unexpected xml node %i", i_node );
320 * \brief parses the tracklist node which only may contain <track>s
322 static bool parse_tracklist_node COMPLEX_INTERFACE
324 VLC_UNUSED(psz_element);
328 /* now parse the <track>s */
329 while( xml_ReaderRead( p_xml_reader ) == 1 )
331 int i_node = xml_ReaderNodeType( p_xml_reader );
332 if( i_node == XML_READER_STARTELEM )
334 char *psz_eltname = xml_ReaderName( p_xml_reader );
337 msg_Err( p_demux, "unexpected end of xml data" );
341 if( strcmp( psz_eltname, "track") )
343 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
350 /* parse the track data in a separate function */
351 if( parse_track_node( p_demux, p_input_item, p_xml_reader,
355 else if( i_node == XML_READER_ENDELEM )
359 /* the <trackList> has to be terminated */
360 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
362 msg_Err( p_demux, "there's a missing </trackList>" );
365 psz_name = xml_ReaderName( p_xml_reader );
366 if( !psz_name || strcmp( psz_name, "trackList" ) )
368 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
374 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
379 * \brief parse one track element
380 * \param COMPLEX_INTERFACE
382 static bool parse_track_node COMPLEX_INTERFACE
385 char *psz_name = NULL;
386 char *psz_value = NULL;
387 xml_elem_hnd_t *p_handler = NULL;
389 xml_elem_hnd_t track_elements[] =
390 { {"location", SIMPLE_CONTENT, {NULL} },
391 {"identifier", SIMPLE_CONTENT, {NULL} },
392 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
393 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
394 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
395 {"info", SIMPLE_CONTENT, {NULL} },
396 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
397 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
398 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
399 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
400 {"link", SIMPLE_CONTENT, {NULL} },
401 {"meta", SIMPLE_CONTENT, {NULL} },
402 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
403 {NULL, UNKNOWN_CONTENT, {NULL} }
406 input_item_t *p_new_input = input_item_New( p_demux, NULL, NULL );
410 /* malloc has failed for input_item_New, so bailout early */
414 /* reset i_track_id */
415 p_demux->p_sys->i_track_id = -1;
417 while( xml_ReaderRead( p_xml_reader ) == 1 )
419 i_node = xml_ReaderNodeType( p_xml_reader );
422 case XML_READER_NONE:
425 case XML_READER_STARTELEM:
426 /* element start tag */
427 psz_name = xml_ReaderName( p_xml_reader );
428 if( !psz_name || !*psz_name )
430 msg_Err( p_demux, "invalid xml stream" );
435 for( p_handler = track_elements;
436 p_handler->name && strcmp( psz_name, p_handler->name );
438 if( !p_handler->name )
440 msg_Err( p_demux, "unexpected element <%s>", psz_name );
445 /* complex content is parsed in a separate function */
446 if( p_handler->type == COMPLEX_CONTENT )
448 if( p_handler->pf_handler.cmplx( p_demux,
464 case XML_READER_TEXT:
465 /* simple element content */
467 psz_value = xml_ReaderValue( p_xml_reader );
470 msg_Err( p_demux, "invalid xml stream" );
476 case XML_READER_ENDELEM:
477 /* element end tag */
478 psz_name = xml_ReaderName( p_xml_reader );
481 msg_Err( p_demux, "invalid xml stream" );
486 /* leave if the current parent node <track> is terminated */
487 if( !strcmp( psz_name, psz_element ) )
491 /* Make sure we have a URI */
492 char *psz_uri = input_item_GetURI( p_new_input );
495 input_item_SetURI( p_new_input, "vlc://nop" );
499 if( p_demux->p_sys->i_track_id < 0 )
501 input_item_AddSubItem( p_input_item, p_new_input );
502 vlc_gc_decref( p_new_input );
506 if( p_demux->p_sys->i_track_id >=
507 p_demux->p_sys->i_tracklist_entries )
510 pp = realloc( p_demux->p_sys->pp_tracklist,
511 (p_demux->p_sys->i_track_id + 1) * sizeof(*pp) );
514 p_demux->p_sys->pp_tracklist = pp;
515 while( p_demux->p_sys->i_track_id >=
516 p_demux->p_sys->i_tracklist_entries )
517 pp[p_demux->p_sys->i_tracklist_entries++] = NULL;
520 p_demux->p_sys->pp_tracklist[
521 p_demux->p_sys->i_track_id ] = p_new_input;
524 /* there MUST have been a start tag for that element name */
525 if( !p_handler || !p_handler->name
526 || strcmp( p_handler->name, psz_name ))
528 msg_Err( p_demux, "there's no open element left for <%s>",
534 /* special case: location */
535 if( !strcmp( p_handler->name, "location" ) )
537 /* FIXME: This is broken. Scheme-relative (//...) locations
538 * and anchors (#...) are not resolved correctly. Also,
539 * host-relative (/...) and directory-relative locations
540 * ("relative path" in vernacular) should be resolved.
541 * Last, psz_base should default to the XSPF resource
542 * location if missing (not the current working directory).
544 if( p_demux->p_sys->psz_base && !strstr( psz_value, "://" ) )
547 if( asprintf( &psz_tmp, "%s%s",
548 p_demux->p_sys->psz_base, psz_value ) == -1 )
553 input_item_SetURI( p_new_input, psz_tmp );
557 input_item_SetURI( p_new_input, psz_value );
558 input_item_CopyOptions( p_input_item, p_new_input );
564 /* there MUST be an item */
565 if( p_handler->pf_handler.smpl )
567 p_handler->pf_handler.smpl( p_new_input,
578 /* unknown/unexpected xml node */
579 msg_Err( p_demux, "unexpected xml node %i", i_node );
585 msg_Err( p_demux, "unexpected end of xml data" );
591 * \brief handles the supported <track> sub-elements
593 static bool set_item_info SIMPLE_INTERFACE
595 /* exit if setting is impossible */
596 if( !psz_name || !psz_value || !p_input )
599 /* re-convert xml special characters inside psz_value */
600 resolve_xml_special_chars( psz_value );
602 /* handle each info element in a separate "if" clause */
603 if( !strcmp( psz_name, "title" ) )
605 input_item_SetTitle( p_input, psz_value );
607 else if( !strcmp( psz_name, "creator" ) )
609 input_item_SetArtist( p_input, psz_value );
611 else if( !strcmp( psz_name, "album" ) )
613 input_item_SetAlbum( p_input, psz_value );
616 else if( !strcmp( psz_name, "trackNum" ) )
618 input_item_SetTrackNum( p_input, psz_value );
620 else if( !strcmp( psz_name, "duration" ) )
622 long i_num = atol( psz_value );
623 input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
625 else if( !strcmp( psz_name, "annotation" ) )
627 input_item_SetDescription( p_input, psz_value );
629 else if( !strcmp( psz_name, "image" ) )
631 input_item_SetArtURL( p_input, psz_value );
637 * \brief handles the <vlc:option> elements
639 static bool set_option SIMPLE_INTERFACE
641 /* exit if setting is impossible */
642 if( !psz_name || !psz_value || !p_input )
645 /* re-convert xml special characters inside psz_value */
646 resolve_xml_special_chars( psz_value );
648 input_item_AddOption( p_input, psz_value, 0 );
654 * \brief parse the extension node of a XSPF playlist
656 static bool parse_extension_node COMPLEX_INTERFACE
658 char *psz_name = NULL;
659 char *psz_value = NULL;
660 char *psz_title = NULL;
661 char *psz_application = NULL;
663 bool b_release_input_item = false;
664 xml_elem_hnd_t *p_handler = NULL;
665 input_item_t *p_new_input = NULL;
667 xml_elem_hnd_t pl_elements[] =
668 { {"vlc:node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
669 {"vlc:item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
670 {"vlc:id", SIMPLE_CONTENT, {NULL} },
671 {"vlc:option", SIMPLE_CONTENT, {.smpl = set_option} },
672 {NULL, UNKNOWN_CONTENT, {NULL} }
675 /* read all extension node attributes */
676 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
678 psz_name = xml_ReaderName( p_xml_reader );
679 psz_value = xml_ReaderValue( p_xml_reader );
680 if( !psz_name || !psz_value )
682 msg_Err( p_demux, "invalid xml stream @ <vlc:node>" );
686 /* attribute: title */
687 if( !strcmp( psz_name, "title" ) )
689 resolve_xml_special_chars( psz_value );
690 psz_title = psz_value;
692 /* extension attribute: application */
693 else if( !strcmp( psz_name, "application" ) )
695 psz_application = psz_value;
697 /* unknown attribute */
700 msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element,
708 /* attribute title is mandatory except for <extension> */
709 if( !strcmp( psz_element, "vlc:node" ) )
713 msg_Warn( p_demux, "<vlc:node> requires \"title\" attribute" );
716 p_new_input = input_item_NewWithType( VLC_OBJECT( p_demux ),
717 "vlc://nop", psz_title, 0, NULL, 0, -1,
718 ITEM_TYPE_DIRECTORY );
721 input_item_AddSubItem( p_input_item, p_new_input );
722 p_input_item = p_new_input;
723 b_release_input_item = true;
727 else if( !strcmp( psz_element, "extension" ) )
729 if( !psz_application )
731 msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
734 else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
736 msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
737 free( psz_application );
741 free( psz_application );
743 /* parse the child elements */
744 while( xml_ReaderRead( p_xml_reader ) == 1 )
746 i_node = xml_ReaderNodeType( p_xml_reader );
749 case XML_READER_NONE:
751 case XML_READER_STARTELEM:
752 /* element start tag */
753 psz_name = xml_ReaderName( p_xml_reader );
754 if( !psz_name || !*psz_name )
756 msg_Err( p_demux, "invalid xml stream" );
758 if( b_release_input_item ) vlc_gc_decref( p_new_input );
762 for( p_handler = pl_elements;
763 p_handler->name && strcmp( psz_name, p_handler->name );
765 if( !p_handler->name )
767 msg_Err( p_demux, "unexpected element <%s>", psz_name );
769 if( b_release_input_item ) vlc_gc_decref( p_new_input );
773 /* complex content is parsed in a separate function */
774 if( p_handler->type == COMPLEX_CONTENT )
776 if( p_handler->pf_handler.cmplx( p_demux,
787 if( b_release_input_item ) vlc_gc_decref( p_new_input );
793 case XML_READER_TEXT:
794 /* simple element content */
796 psz_value = xml_ReaderValue( p_xml_reader );
799 msg_Err( p_demux, "invalid xml stream" );
801 if( b_release_input_item ) vlc_gc_decref( p_new_input );
806 case XML_READER_ENDELEM:
807 /* element end tag */
808 psz_name = xml_ReaderName( p_xml_reader );
811 msg_Err( p_demux, "invalid xml stream" );
813 if( b_release_input_item ) vlc_gc_decref( p_new_input );
816 /* leave if the current parent node is terminated */
817 if( !strcmp( psz_name, psz_element ) )
820 if( b_release_input_item ) vlc_gc_decref( p_new_input );
823 /* there MUST have been a start tag for that element name */
824 if( !p_handler || !p_handler->name
825 || strcmp( p_handler->name, psz_name ))
827 msg_Err( p_demux, "there's no open element left for <%s>",
830 if( b_release_input_item ) vlc_gc_decref( p_new_input );
834 /* special tag <vlc:id> */
835 if( !strcmp( p_handler->name, "vlc:id" ) )
837 p_demux->p_sys->i_track_id = atoi( psz_value );
839 else if( p_handler->pf_handler.smpl )
841 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
849 /* unknown/unexpected xml node */
850 msg_Err( p_demux, "unexpected xml node %i", i_node );
852 if( b_release_input_item ) vlc_gc_decref( p_new_input );
857 if( b_release_input_item ) vlc_gc_decref( p_new_input );
862 * \brief parse the extension item node of a XSPF playlist
864 static bool parse_extitem_node COMPLEX_INTERFACE
866 VLC_UNUSED(psz_element);
867 input_item_t *p_new_input = NULL;
870 /* read all extension item attributes */
871 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
873 char *psz_name = xml_ReaderName( p_xml_reader );
874 char *psz_value = xml_ReaderValue( p_xml_reader );
875 if( !psz_name || !psz_value )
877 msg_Err( p_demux, "invalid xml stream @ <vlc:item>" );
882 /* attribute: href */
883 if( !strcmp( psz_name, "tid" ) )
885 i_tid = atoi( psz_value );
887 /* unknown attribute */
889 msg_Warn( p_demux, "invalid <vlc:item> attribute:\"%s\"", psz_name);
895 /* attribute href is mandatory */
898 msg_Warn( p_demux, "<vlc:item> requires \"tid\" attribute" );
902 if( i_tid >= p_demux->p_sys->i_tracklist_entries )
904 msg_Warn( p_demux, "invalid \"tid\" attribute" );
908 p_new_input = p_demux->p_sys->pp_tracklist[ i_tid ];
911 input_item_AddSubItem( p_input_item, p_new_input );
912 vlc_gc_decref( p_new_input );
913 p_demux->p_sys->pp_tracklist[i_tid] = NULL;
916 /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
917 /* (libxml sends NONE) */
918 xml_ReaderRead( p_xml_reader );
924 * \brief skips complex element content that we can't manage
926 static bool skip_element COMPLEX_INTERFACE
928 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 char *psz_endname = xml_ReaderName( p_xml_reader );
937 if( !strcmp( psz_element, psz_endname ) )