2 /*******************************************************************************
3 * xspf.c : XSPF playlist import functions
4 *******************************************************************************
5 * Copyright (C) 2006 the VideoLAN team
8 * Authors: Daniel Stränger <vlc at schmaller dot de>
9 * Yoann Peronneau <yoann@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 ******************************************************************************/
26 * \file modules/demux/playlist/xspf.c
27 * \brief XSPF playlist import functions
34 #include <vlc_common.h>
35 #include <vlc_demux.h>
38 #include <vlc_strings.h>
45 input_item_t **pp_tracklist;
46 int i_tracklist_entries;
51 static int Control( demux_t *, int, va_list );
52 static int Demux( demux_t * );
55 * \brief XSPF submodule initialization function
57 int Import_xspf( vlc_object_t *p_this )
59 DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xspf", "xspf-open",
60 "using XSPF playlist reader" );
64 void Close_xspf( vlc_object_t *p_this )
66 demux_t *p_demux = (demux_t *)p_this;
68 for(i = 0; i < p_demux->p_sys->i_tracklist_entries; i++)
70 if(p_demux->p_sys->pp_tracklist[i])
71 vlc_gc_decref( p_demux->p_sys->pp_tracklist[i] );
73 FREENULL( p_demux->p_sys->pp_tracklist );
74 FREENULL( p_demux->p_sys->psz_base );
75 free( p_demux->p_sys );
79 * \brief demuxer function for XSPF parsing
81 int Demux( demux_t *p_demux )
85 xml_reader_t *p_xml_reader = NULL;
86 char *psz_name = NULL;
88 p_demux->p_sys->pp_tracklist = NULL;
89 p_demux->p_sys->i_tracklist_entries = 0;
90 p_demux->p_sys->i_identifier = 0;
91 p_demux->p_sys->psz_base = NULL;
93 /* create new xml parser from stream */
94 p_xml = xml_Create( p_demux );
99 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
104 /* locating the root node */
109 if( xml_ReaderRead( p_xml_reader ) != 1 )
111 msg_Err( p_demux, "can't read xml stream" );
114 } while( i_ret == VLC_SUCCESS &&
115 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
117 /* checking root node name */
120 psz_name = xml_ReaderName( p_xml_reader );
121 if( !psz_name || strcmp( psz_name, "playlist" ) )
123 msg_Err( p_demux, "invalid root node name: %s", psz_name );
130 i_ret = parse_playlist_node( p_demux, p_current_input,
131 p_xml_reader, "playlist" ) ? 0 : -1;
134 for( i = 0 ; i < p_demux->p_sys->i_tracklist_entries ; i++ )
136 input_item_t *p_new_input = p_demux->p_sys->pp_tracklist[i];
139 input_ItemAddSubItem( p_current_input, p_new_input );
143 HANDLE_PLAY_AND_RELEASE;
145 xml_ReaderDelete( p_xml, p_xml_reader );
148 return i_ret; /* Needed for correct operation of go back */
151 /** \brief dummy function for demux callback interface */
152 static int Control( demux_t *p_demux, int i_query, va_list args )
154 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
159 * \brief parse the root node of a XSPF playlist
160 * \param p_demux demuxer instance
161 * \param p_input_item current input item
162 * \param p_xml_reader xml reader instance
163 * \param psz_element name of element to parse
165 static bool parse_playlist_node COMPLEX_INTERFACE
168 char *psz_value=NULL;
169 bool b_version_found = false;
171 xml_elem_hnd_t *p_handler=NULL;
173 xml_elem_hnd_t pl_elements[] =
174 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
175 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
176 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
177 {"info", SIMPLE_CONTENT, {NULL} },
178 {"location", SIMPLE_CONTENT, {NULL} },
179 {"identifier", SIMPLE_CONTENT, {NULL} },
180 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
181 {"date", SIMPLE_CONTENT, {NULL} },
182 {"license", SIMPLE_CONTENT, {NULL} },
183 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
184 {"link", SIMPLE_CONTENT, {NULL} },
185 {"meta", SIMPLE_CONTENT, {NULL} },
186 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
187 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
188 {NULL, UNKNOWN_CONTENT, {NULL} }
191 /* read all playlist attributes */
192 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
194 psz_name = xml_ReaderName( p_xml_reader );
195 psz_value = xml_ReaderValue( p_xml_reader );
196 if( !psz_name || !psz_value )
198 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
202 /* attribute: version */
203 if( !strcmp( psz_name, "version" ) )
205 b_version_found = true;
206 if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
207 msg_Warn( p_demux, "unsupported XSPF version" );
209 /* attribute: xmlns */
210 else if( !strcmp( psz_name, "xmlns" ) )
212 else if( !strcmp( psz_name, "xml:base" ) )
214 p_demux->p_sys->psz_base = decode_URI_duplicate( psz_value );
216 /* unknown attribute */
218 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
222 /* attribute version is mandatory !!! */
223 if( !b_version_found )
224 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
226 /* parse the child elements - we only take care of <trackList> */
227 while( xml_ReaderRead( p_xml_reader ) == 1 )
229 i_node = xml_ReaderNodeType( p_xml_reader );
232 case XML_READER_NONE:
234 case XML_READER_STARTELEM:
235 /* element start tag */
236 psz_name = xml_ReaderName( p_xml_reader );
237 if( !psz_name || !*psz_name )
239 msg_Err( p_demux, "invalid xml stream" );
244 for( p_handler = pl_elements;
245 p_handler->name && strcmp( psz_name, p_handler->name );
247 if( !p_handler->name )
249 msg_Err( p_demux, "unexpected element <%s>", psz_name );
254 /* complex content is parsed in a separate function */
255 if( p_handler->type == COMPLEX_CONTENT )
257 if( p_handler->pf_handler.cmplx( p_demux,
273 case XML_READER_TEXT:
274 /* simple element content */
276 psz_value = xml_ReaderValue( p_xml_reader );
279 msg_Err( p_demux, "invalid xml stream" );
285 case XML_READER_ENDELEM:
286 /* element end tag */
287 psz_name = xml_ReaderName( p_xml_reader );
290 msg_Err( p_demux, "invalid xml stream" );
294 /* leave if the current parent node <playlist> is terminated */
295 if( !strcmp( psz_name, psz_element ) )
300 /* there MUST have been a start tag for that element name */
301 if( !p_handler || !p_handler->name
302 || strcmp( p_handler->name, psz_name ))
304 msg_Err( p_demux, "there's no open element left for <%s>",
310 if( p_handler->pf_handler.smpl )
312 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
320 /* unknown/unexpected xml node */
321 msg_Err( p_demux, "unexpected xml node %i", i_node );
331 * \brief parses the tracklist node which only may contain <track>s
333 static bool parse_tracklist_node COMPLEX_INTERFACE
335 VLC_UNUSED(psz_element);
340 /* now parse the <track>s */
341 while( xml_ReaderRead( p_xml_reader ) == 1 )
343 i_node = xml_ReaderNodeType( p_xml_reader );
344 if( i_node == XML_READER_STARTELEM )
346 psz_name = xml_ReaderName( p_xml_reader );
349 msg_Err( p_demux, "unexpected end of xml data" );
353 if( strcmp( psz_name, "track") )
355 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
362 /* parse the track data in a separate function */
363 if( parse_track_node( p_demux, p_input_item,
364 p_xml_reader,"track" ) == true )
367 else if( i_node == XML_READER_ENDELEM )
371 /* the <trackList> has to be terminated */
372 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
374 msg_Err( p_demux, "there's a missing </trackList>" );
378 psz_name = xml_ReaderName( p_xml_reader );
379 if( !psz_name || strcmp( psz_name, "trackList" ) )
381 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
387 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
393 * \brief parse one track element
394 * \param COMPLEX_INTERFACE
396 static bool parse_track_node COMPLEX_INTERFACE
398 input_item_t *p_new_input = NULL;
401 char *psz_value=NULL;
402 xml_elem_hnd_t *p_handler=NULL;
404 xml_elem_hnd_t track_elements[] =
405 { {"location", SIMPLE_CONTENT, {NULL} },
406 {"identifier", SIMPLE_CONTENT, {NULL} },
407 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
408 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
409 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
410 {"info", SIMPLE_CONTENT, {NULL} },
411 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
412 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
413 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
414 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
415 {"link", SIMPLE_CONTENT, {NULL} },
416 {"meta", SIMPLE_CONTENT, {NULL} },
417 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
418 {NULL, UNKNOWN_CONTENT, {NULL} }
421 while( xml_ReaderRead( p_xml_reader ) == 1 )
423 i_node = xml_ReaderNodeType( p_xml_reader );
426 case XML_READER_NONE:
429 case XML_READER_STARTELEM:
430 /* element start tag */
431 psz_name = xml_ReaderName( p_xml_reader );
432 if( !psz_name || !*psz_name )
434 msg_Err( p_demux, "invalid xml stream" );
439 for( p_handler = track_elements;
440 p_handler->name && strcmp( psz_name, p_handler->name );
442 if( !p_handler->name )
444 msg_Err( p_demux, "unexpected element <%s>", psz_name );
449 /* complex content is parsed in a separate function */
450 if( p_handler->type == COMPLEX_CONTENT )
455 "at <%s> level no new item has been allocated",
460 if( p_handler->pf_handler.cmplx( p_demux,
476 case XML_READER_TEXT:
477 /* simple element content */
479 psz_value = xml_ReaderValue( p_xml_reader );
482 msg_Err( p_demux, "invalid xml stream" );
488 case XML_READER_ENDELEM:
489 /* element end tag */
490 psz_name = xml_ReaderName( p_xml_reader );
493 msg_Err( p_demux, "invalid xml stream" );
497 /* leave if the current parent node <track> is terminated */
498 if( !strcmp( psz_name, psz_element ) )
501 if( p_demux->p_sys->i_identifier <
502 p_demux->p_sys->i_tracklist_entries )
504 p_demux->p_sys->pp_tracklist[
505 p_demux->p_sys->i_identifier ] = p_new_input;
509 if( p_demux->p_sys->i_identifier >
510 p_demux->p_sys->i_tracklist_entries )
512 p_demux->p_sys->i_tracklist_entries =
513 p_demux->p_sys->i_identifier;
515 INSERT_ELEM( p_demux->p_sys->pp_tracklist,
516 p_demux->p_sys->i_tracklist_entries,
517 p_demux->p_sys->i_tracklist_entries,
522 /* there MUST have been a start tag for that element name */
523 if( !p_handler || !p_handler->name
524 || strcmp( p_handler->name, psz_name ))
526 msg_Err( p_demux, "there's no open element left for <%s>",
532 /* special case: location */
533 if( !strcmp( p_handler->name, "location" ) )
536 /* there MUST NOT be an item */
539 msg_Err( p_demux, "item <%s> already created",
544 psz_uri = decode_URI_duplicate( psz_value );
548 if( p_demux->p_sys->psz_base &&
549 !strstr( psz_uri, "://" ) )
551 char* psz_tmp = malloc(
552 strlen(p_demux->p_sys->psz_base) +
553 strlen(psz_uri) +1 );
556 sprintf( psz_tmp, "%s%s",
557 p_demux->p_sys->psz_base, psz_uri );
561 p_new_input = input_ItemNewExt( p_demux, psz_uri,
564 input_ItemCopyOptions( p_input_item, p_new_input );
575 else if( !strcmp( p_handler->name, "identifier" ) )
577 p_demux->p_sys->i_identifier = atoi( psz_value );
581 /* there MUST be an item */
584 msg_Err( p_demux, "item not yet created at <%s>",
589 if( p_handler->pf_handler.smpl )
591 p_handler->pf_handler.smpl( p_new_input,
602 /* unknown/unexpected xml node */
603 msg_Err( p_demux, "unexpected xml node %i", i_node );
609 msg_Err( p_demux, "unexpected end of xml data" );
615 * \brief handles the supported <track> sub-elements
617 static bool set_item_info SIMPLE_INTERFACE
619 /* exit if setting is impossible */
620 if( !psz_name || !psz_value || !p_input )
624 /* re-convert xml special characters inside psz_value */
625 resolve_xml_special_chars( psz_value );
627 /* handle each info element in a separate "if" clause */
628 if( !strcmp( psz_name, "title" ) )
630 input_item_SetTitle( p_input, psz_value );
632 else if( !strcmp( psz_name, "creator" ) )
634 input_item_SetArtist( p_input, psz_value );
636 else if( !strcmp( psz_name, "album" ) )
638 input_item_SetAlbum( p_input, psz_value );
641 else if( !strcmp( psz_name, "trackNum" ) )
643 input_item_SetTrackNum( p_input, psz_value );
645 else if( !strcmp( psz_name, "duration" ) )
647 long i_num = atol( psz_value );
648 input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
650 else if( !strcmp( psz_name, "annotation" ) )
652 input_item_SetDescription( p_input, psz_value );
654 else if( !strcmp( psz_name, "image" ) )
656 char *psz_uri = decode_URI_duplicate( psz_value );
657 input_item_SetArtURL( p_input, psz_uri );
664 * \brief handles the <option> elements
666 static bool set_option SIMPLE_INTERFACE
668 /* exit if setting is impossible */
669 if( !psz_name || !psz_value || !p_input )
672 /* re-convert xml special characters inside psz_value */
673 resolve_xml_special_chars( psz_value );
675 input_ItemAddOpt( p_input, psz_value, 0 );
681 * \brief parse the extension node of a XSPF playlist
683 static bool parse_extension_node COMPLEX_INTERFACE
685 char *psz_name = NULL;
686 char *psz_value = NULL;
687 char *psz_title = NULL;
688 char *psz_application = NULL;
690 xml_elem_hnd_t *p_handler = NULL;
691 input_item_t *p_new_input = NULL;
693 xml_elem_hnd_t pl_elements[] =
694 { {"node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
695 {"item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
696 {"option", SIMPLE_CONTENT, {.smpl = set_option} },
697 {NULL, UNKNOWN_CONTENT, {NULL} }
700 /* read all extension node attributes */
701 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
703 psz_name = xml_ReaderName( p_xml_reader );
704 psz_value = xml_ReaderValue( p_xml_reader );
705 if( !psz_name || !psz_value )
707 msg_Err( p_demux, "invalid xml stream @ <node>" );
711 /* attribute: title */
712 if( !strcmp( psz_name, "title" ) )
714 resolve_xml_special_chars( psz_value );
715 psz_title = strdup( psz_value );
717 /* extension attribute: application */
718 else if( !strcmp( psz_name, "application" ) )
720 psz_application = strdup( psz_value );
722 /* unknown attribute */
724 msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element, psz_name );
729 /* attribute title is mandatory except for <extension> */
730 if( !strcmp( psz_element, "node" ) )
734 msg_Warn( p_demux, "<node> requires \"title\" attribute" );
737 p_new_input = input_ItemNewWithType( VLC_OBJECT( p_demux ), "vlc://nop",
738 psz_title, 0, NULL, -1, ITEM_TYPE_DIRECTORY );
741 input_ItemAddSubItem( p_input_item, p_new_input );
742 p_input_item = p_new_input;
743 vlc_gc_decref( p_new_input );
747 else if( !strcmp( psz_element, "extension" ) )
749 if( !psz_application )
751 msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
754 else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
756 msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
757 free( psz_application );
761 free( psz_application );
763 /* parse the child elements */
764 while( xml_ReaderRead( p_xml_reader ) == 1 )
766 i_node = xml_ReaderNodeType( p_xml_reader );
769 case XML_READER_NONE:
771 case XML_READER_STARTELEM:
772 /* element start tag */
773 psz_name = xml_ReaderName( p_xml_reader );
774 if( !psz_name || !*psz_name )
776 msg_Err( p_demux, "invalid xml stream" );
781 for( p_handler = pl_elements;
782 p_handler->name && strcmp( psz_name, p_handler->name );
784 if( !p_handler->name )
786 msg_Err( p_demux, "unexpected element <%s>", psz_name );
791 /* complex content is parsed in a separate function */
792 if( p_handler->type == COMPLEX_CONTENT )
794 if( p_handler->pf_handler.cmplx( p_demux,
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" );
822 case XML_READER_ENDELEM:
823 /* element end tag */
824 psz_name = xml_ReaderName( p_xml_reader );
827 msg_Err( p_demux, "invalid xml stream" );
831 /* leave if the current parent node is terminated */
832 if( !strcmp( psz_name, psz_element ) )
837 /* there MUST have been a start tag for that element name */
838 if( !p_handler || !p_handler->name
839 || strcmp( p_handler->name, psz_name ))
841 msg_Err( p_demux, "there's no open element left for <%s>",
847 if( p_handler->pf_handler.smpl )
849 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
857 /* unknown/unexpected xml node */
858 msg_Err( p_demux, "unexpected xml node %i", i_node );
868 * \brief parse the extension item node of a XSPF playlist
870 static bool parse_extitem_node COMPLEX_INTERFACE
872 VLC_UNUSED(psz_element);
873 input_item_t *p_new_input = NULL;
874 char *psz_name = NULL;
875 char *psz_value = NULL;
878 /* read all extension item attributes */
879 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
881 psz_name = xml_ReaderName( p_xml_reader );
882 psz_value = xml_ReaderValue( p_xml_reader );
883 if( !psz_name || !psz_value )
885 msg_Err( p_demux, "invalid xml stream @ <item>" );
889 /* attribute: href */
890 if( !strcmp( psz_name, "href" ) )
892 i_href = atoi( psz_value );
894 /* unknown attribute */
896 msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
901 /* attribute href is mandatory */
904 msg_Warn( p_demux, "<item> requires \"href\" attribute" );
908 if( i_href >= p_demux->p_sys->i_tracklist_entries )
910 msg_Warn( p_demux, "invalid \"href\" attribute" );
914 p_new_input = p_demux->p_sys->pp_tracklist[ i_href ];
917 input_ItemAddSubItem( p_input_item, p_new_input );
918 vlc_gc_decref( p_new_input );
919 p_demux->p_sys->pp_tracklist[i_href] = NULL;
922 /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
923 /* (libxml sends NONE) */
924 xml_ReaderRead( p_xml_reader );
930 * \brief skips complex element content that we can't manage
932 static bool skip_element COMPLEX_INTERFACE
934 VLC_UNUSED(p_demux); VLC_UNUSED(p_input_item);
937 while( xml_ReaderRead( p_xml_reader ) == 1 )
939 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
941 psz_endname = xml_ReaderName( p_xml_reader );
944 if( !strcmp( psz_element, psz_endname ) )