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
34 #include <vlc_demux.h>
38 #include "vlc_strings.h"
44 playlist_item_t *p_item_in_category;
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 E_(Import_xspf)( vlc_object_t *p_this )
59 DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xspf", "xspf-open",
60 "using XSPF playlist reader" );
64 void E_(Close_xspf)( vlc_object_t *p_this )
66 demux_t *p_demux = (demux_t *)p_this;
67 FREENULL( p_demux->p_sys->pp_tracklist );
68 FREENULL( p_demux->p_sys->psz_base );
69 free( p_demux->p_sys );
73 * \brief demuxer function for XSPF parsing
75 int Demux( demux_t *p_demux )
79 xml_reader_t *p_xml_reader = NULL;
80 char *psz_name = NULL;
82 p_demux->p_sys->pp_tracklist = NULL;
83 p_demux->p_sys->i_tracklist_entries = 0;
84 p_demux->p_sys->i_identifier = 0;
85 p_demux->p_sys->psz_base = NULL;
87 /* create new xml parser from stream */
88 p_xml = xml_Create( p_demux );
93 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
98 /* locating the root node */
103 if( xml_ReaderRead( p_xml_reader ) != 1 )
105 msg_Err( p_demux, "can't read xml stream" );
108 } while( i_ret == VLC_SUCCESS &&
109 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
111 /* checking root node name */
114 psz_name = xml_ReaderName( p_xml_reader );
115 if( !psz_name || strcmp( psz_name, "playlist" ) )
117 msg_Err( p_demux, "invalid root node name: %s", psz_name );
124 i_ret = parse_playlist_node( p_demux, p_playlist, p_current_input,
125 p_xml_reader, "playlist" ) ? 0 : -1;
128 for( i = 0 ; i < p_demux->p_sys->i_tracklist_entries ; i++ )
130 input_item_t *p_new_input = p_demux->p_sys->pp_tracklist[i];
133 input_ItemAddSubItem( p_current_input, p_new_input );
137 HANDLE_PLAY_AND_RELEASE;
139 xml_ReaderDelete( p_xml, p_xml_reader );
142 return i_ret; /* Needed for correct operation of go back */
145 /** \brief dummy function for demux callback interface */
146 static int Control( demux_t *p_demux, int i_query, va_list args )
152 * \brief parse the root node of a XSPF playlist
153 * \param p_demux demuxer instance
154 * \param p_playlist playlist instance
155 * \param p_input_item current input item
156 * \param p_xml_reader xml reader instance
157 * \param psz_element name of element to parse
159 static vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
162 char *psz_value=NULL;
163 vlc_bool_t b_version_found = VLC_FALSE;
165 xml_elem_hnd_t *p_handler=NULL;
167 xml_elem_hnd_t pl_elements[] =
168 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
169 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
170 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
171 {"info", SIMPLE_CONTENT, {NULL} },
172 {"location", SIMPLE_CONTENT, {NULL} },
173 {"identifier", SIMPLE_CONTENT, {NULL} },
174 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
175 {"date", SIMPLE_CONTENT, {NULL} },
176 {"license", SIMPLE_CONTENT, {NULL} },
177 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
178 {"link", SIMPLE_CONTENT, {NULL} },
179 {"meta", SIMPLE_CONTENT, {NULL} },
180 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
181 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
182 {NULL, UNKNOWN_CONTENT, {NULL} }
185 /* read all playlist attributes */
186 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
188 psz_name = xml_ReaderName( p_xml_reader );
189 psz_value = xml_ReaderValue( p_xml_reader );
190 if( !psz_name || !psz_value )
192 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
196 /* attribute: version */
197 if( !strcmp( psz_name, "version" ) )
199 b_version_found = VLC_TRUE;
200 if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
201 msg_Warn( p_demux, "unsupported XSPF version" );
203 /* attribute: xmlns */
204 else if( !strcmp( psz_name, "xmlns" ) )
206 else if( !strcmp( psz_name, "xml:base" ) )
208 p_demux->p_sys->psz_base = decode_URI_duplicate( psz_value );
210 /* unknown attribute */
212 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
216 /* attribute version is mandatory !!! */
217 if( !b_version_found )
218 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
220 /* parse the child elements - we only take care of <trackList> */
221 while( xml_ReaderRead( p_xml_reader ) == 1 )
223 i_node = xml_ReaderNodeType( p_xml_reader );
226 case XML_READER_NONE:
228 case XML_READER_STARTELEM:
229 /* element start tag */
230 psz_name = xml_ReaderName( p_xml_reader );
231 if( !psz_name || !*psz_name )
233 msg_Err( p_demux, "invalid xml stream" );
238 for( p_handler = pl_elements;
239 p_handler->name && strcmp( psz_name, p_handler->name );
241 if( !p_handler->name )
243 msg_Err( p_demux, "unexpected element <%s>", psz_name );
248 /* complex content is parsed in a separate function */
249 if( p_handler->type == COMPLEX_CONTENT )
251 if( p_handler->pf_handler.cmplx( p_demux,
268 case XML_READER_TEXT:
269 /* simple element content */
271 psz_value = xml_ReaderValue( p_xml_reader );
274 msg_Err( p_demux, "invalid xml stream" );
280 case XML_READER_ENDELEM:
281 /* element end tag */
282 psz_name = xml_ReaderName( p_xml_reader );
285 msg_Err( p_demux, "invalid xml stream" );
289 /* leave if the current parent node <playlist> is terminated */
290 if( !strcmp( psz_name, psz_element ) )
295 /* there MUST have been a start tag for that element name */
296 if( !p_handler || !p_handler->name
297 || strcmp( p_handler->name, psz_name ))
299 msg_Err( p_demux, "there's no open element left for <%s>",
305 if( p_handler->pf_handler.smpl )
307 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
315 /* unknown/unexpected xml node */
316 msg_Err( p_demux, "unexpected xml node %i", i_node );
326 * \brief parses the tracklist node which only may contain <track>s
328 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
334 /* now parse the <track>s */
335 while( xml_ReaderRead( p_xml_reader ) == 1 )
337 i_node = xml_ReaderNodeType( p_xml_reader );
338 if( i_node == XML_READER_STARTELEM )
340 psz_name = xml_ReaderName( p_xml_reader );
343 msg_Err( p_demux, "unexpected end of xml data" );
347 if( strcmp( psz_name, "track") )
349 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
356 /* parse the track data in a separate function */
357 if( parse_track_node( p_demux, p_playlist, p_input_item,
358 p_xml_reader,"track" ) == VLC_TRUE )
361 else if( i_node == XML_READER_ENDELEM )
365 /* the <trackList> has to be terminated */
366 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
368 msg_Err( p_demux, "there's a missing </trackList>" );
372 psz_name = xml_ReaderName( p_xml_reader );
373 if( !psz_name || strcmp( psz_name, "trackList" ) )
375 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
381 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
387 * \brief parse one track element
388 * \param COMPLEX_INTERFACE
390 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
392 input_item_t *p_new_input = NULL;
395 char *psz_value=NULL;
396 xml_elem_hnd_t *p_handler=NULL;
398 xml_elem_hnd_t track_elements[] =
399 { {"location", SIMPLE_CONTENT, {NULL} },
400 {"identifier", SIMPLE_CONTENT, {NULL} },
401 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
402 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
403 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
404 {"info", SIMPLE_CONTENT, {NULL} },
405 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
406 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
407 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
408 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
409 {"link", SIMPLE_CONTENT, {NULL} },
410 {"meta", SIMPLE_CONTENT, {NULL} },
411 {"extension", COMPLEX_CONTENT, {.cmplx = skip_element} },
412 {NULL, UNKNOWN_CONTENT, {NULL} }
415 while( xml_ReaderRead( p_xml_reader ) == 1 )
417 i_node = xml_ReaderNodeType( p_xml_reader );
420 case XML_READER_NONE:
423 case XML_READER_STARTELEM:
424 /* element start tag */
425 psz_name = xml_ReaderName( p_xml_reader );
426 if( !psz_name || !*psz_name )
428 msg_Err( p_demux, "invalid xml stream" );
433 for( p_handler = track_elements;
434 p_handler->name && strcmp( psz_name, p_handler->name );
436 if( !p_handler->name )
438 msg_Err( p_demux, "unexpected element <%s>", psz_name );
443 /* complex content is parsed in a separate function */
444 if( p_handler->type == COMPLEX_CONTENT )
449 "at <%s> level no new item has been allocated",
454 if( p_handler->pf_handler.cmplx( p_demux,
471 case XML_READER_TEXT:
472 /* simple element content */
474 psz_value = xml_ReaderValue( p_xml_reader );
477 msg_Err( p_demux, "invalid xml stream" );
483 case XML_READER_ENDELEM:
484 /* element end tag */
485 psz_name = xml_ReaderName( p_xml_reader );
488 msg_Err( p_demux, "invalid xml stream" );
492 /* leave if the current parent node <track> is terminated */
493 if( !strcmp( psz_name, psz_element ) )
496 if( p_demux->p_sys->i_identifier <
497 p_demux->p_sys->i_tracklist_entries )
499 p_demux->p_sys->pp_tracklist[
500 p_demux->p_sys->i_identifier ] = p_new_input;
504 if( p_demux->p_sys->i_identifier >
505 p_demux->p_sys->i_tracklist_entries )
507 p_demux->p_sys->i_tracklist_entries =
508 p_demux->p_sys->i_identifier;
510 INSERT_ELEM( p_demux->p_sys->pp_tracklist,
511 p_demux->p_sys->i_tracklist_entries,
512 p_demux->p_sys->i_tracklist_entries,
517 /* there MUST have been a start tag for that element name */
518 if( !p_handler || !p_handler->name
519 || strcmp( p_handler->name, psz_name ))
521 msg_Err( p_demux, "there's no open element left for <%s>",
527 /* special case: location */
528 if( !strcmp( p_handler->name, "location" ) )
531 /* there MUST NOT be an item */
534 msg_Err( p_demux, "item <%s> already created",
539 psz_uri = decode_URI_duplicate( psz_value );
543 if( p_demux->p_sys->psz_base &&
544 !strstr( psz_uri, "://" ) )
546 char* psz_tmp = malloc(
547 strlen(p_demux->p_sys->psz_base) +
548 strlen(psz_uri) +1 );
551 msg_Err( p_demux, "out of memory");
554 sprintf( psz_tmp, "%s%s",
555 p_demux->p_sys->psz_base, psz_uri );
559 /* FIXME: We are leaking that one */
560 p_new_input = input_ItemNewExt( p_playlist, psz_uri,
563 input_ItemCopyOptions( p_input_item, p_new_input );
574 else if( !strcmp( p_handler->name, "identifier" ) )
576 p_demux->p_sys->i_identifier = atoi( psz_value );
580 /* there MUST be an item */
583 msg_Err( p_demux, "item not yet created at <%s>",
588 if( p_handler->pf_handler.smpl )
590 p_handler->pf_handler.smpl( p_new_input,
601 /* unknown/unexpected xml node */
602 msg_Err( p_demux, "unexpected xml node %i", i_node );
608 msg_Err( p_demux, "unexpected end of xml data" );
614 * \brief handles the supported <track> sub-elements
616 static vlc_bool_t set_item_info SIMPLE_INTERFACE
618 /* exit if setting is impossible */
619 if( !psz_name || !psz_value || !p_input )
623 /* re-convert xml special characters inside psz_value */
624 resolve_xml_special_chars( psz_value );
626 /* handle each info element in a separate "if" clause */
627 if( !strcmp( psz_name, "title" ) )
629 input_item_SetTitle( p_input, psz_value );
631 else if( !strcmp( psz_name, "creator" ) )
633 input_item_SetArtist( p_input, psz_value );
635 else if( !strcmp( psz_name, "album" ) )
637 input_item_SetAlbum( p_input, psz_value );
640 else if( !strcmp( psz_name, "trackNum" ) )
642 input_item_SetTrackNum( p_input, psz_value );
644 else if( !strcmp( psz_name, "duration" ) )
646 long i_num = atol( psz_value );
647 input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
649 else if( !strcmp( psz_name, "annotation" ) )
651 input_item_SetDescription( p_input, psz_value );
653 else if( !strcmp( psz_name, "image" ) )
655 char *psz_uri = decode_URI_duplicate( psz_value );
656 input_item_SetArtURL( p_input, psz_uri );
664 * \brief parse the extension node of a XSPF playlist
666 static vlc_bool_t parse_extension_node COMPLEX_INTERFACE
668 char *psz_name = NULL;
669 char *psz_value = NULL;
670 char *psz_title = NULL;
671 char *psz_application = NULL;
673 xml_elem_hnd_t *p_handler = NULL;
674 input_item_t *p_new_input = NULL;
676 xml_elem_hnd_t pl_elements[] =
677 { {"node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
678 {"item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
679 {NULL, UNKNOWN_CONTENT, {NULL} }
682 /* read all extension node attributes */
683 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
685 psz_name = xml_ReaderName( p_xml_reader );
686 psz_value = xml_ReaderValue( p_xml_reader );
687 if( !psz_name || !psz_value )
689 msg_Err( p_demux, "invalid xml stream @ <node>" );
693 /* attribute: title */
694 if( !strcmp( psz_name, "title" ) )
696 resolve_xml_special_chars( psz_value );
697 psz_title = strdup( psz_value );
699 /* extension attribute: application */
700 else if( !strcmp( psz_name, "application" ) )
702 psz_application = strdup( psz_value );
704 /* unknown attribute */
706 msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element, psz_name );
711 /* attribute title is mandatory except for <extension> */
712 if( !strcmp( psz_element, "node" ) )
716 msg_Warn( p_demux, "<node> requires \"title\" attribute" );
719 p_new_input = input_ItemNewWithType( VLC_OBJECT( p_playlist ), "vlc:nop",
720 psz_title, 0, NULL, -1, ITEM_TYPE_DIRECTORY );
723 input_ItemAddSubItem( p_input_item, p_new_input );
724 p_input_item = p_new_input;
728 else if( !strcmp( psz_element, "extension" ) )
730 if( !psz_application )
732 msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
735 else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
737 msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
738 free( psz_application );
742 free( psz_application );
744 /* parse the child elements */
745 while( xml_ReaderRead( p_xml_reader ) == 1 )
747 i_node = xml_ReaderNodeType( p_xml_reader );
750 case XML_READER_NONE:
752 case XML_READER_STARTELEM:
753 /* element start tag */
754 psz_name = xml_ReaderName( p_xml_reader );
755 if( !psz_name || !*psz_name )
757 msg_Err( p_demux, "invalid xml stream" );
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 );
772 /* complex content is parsed in a separate function */
773 if( p_handler->type == COMPLEX_CONTENT )
775 if( p_handler->pf_handler.cmplx( p_demux,
792 case XML_READER_TEXT:
793 /* simple element content */
795 psz_value = xml_ReaderValue( p_xml_reader );
798 msg_Err( p_demux, "invalid xml stream" );
804 case XML_READER_ENDELEM:
805 /* element end tag */
806 psz_name = xml_ReaderName( p_xml_reader );
809 msg_Err( p_demux, "invalid xml stream" );
813 /* leave if the current parent node is terminated */
814 if( !strcmp( psz_name, psz_element ) )
819 /* there MUST have been a start tag for that element name */
820 if( !p_handler || !p_handler->name
821 || strcmp( p_handler->name, psz_name ))
823 msg_Err( p_demux, "there's no open element left for <%s>",
829 if( p_handler->pf_handler.smpl )
831 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
839 /* unknown/unexpected xml node */
840 msg_Err( p_demux, "unexpected xml node %i", i_node );
850 * \brief parse the extension item node of a XSPF playlist
852 static vlc_bool_t parse_extitem_node COMPLEX_INTERFACE
854 input_item_t *p_new_input = NULL;
855 char *psz_name = NULL;
856 char *psz_value = NULL;
859 /* read all extension item attributes */
860 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
862 psz_name = xml_ReaderName( p_xml_reader );
863 psz_value = xml_ReaderValue( p_xml_reader );
864 if( !psz_name || !psz_value )
866 msg_Err( p_demux, "invalid xml stream @ <item>" );
870 /* attribute: href */
871 if( !strcmp( psz_name, "href" ) )
873 i_href = atoi( psz_value );
875 /* unknown attribute */
877 msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
882 /* attribute href is mandatory */
885 msg_Warn( p_demux, "<item> requires \"href\" attribute" );
889 if( i_href >= p_demux->p_sys->i_tracklist_entries )
891 msg_Warn( p_demux, "invalid \"href\" attribute" );
895 p_new_input = p_demux->p_sys->pp_tracklist[ i_href ];
898 input_ItemAddSubItem( p_input_item, p_new_input );
899 p_demux->p_sys->pp_tracklist[i_href] = NULL;
902 /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
903 /* (libxml sends NONE) */
904 xml_ReaderRead( p_xml_reader );
910 * \brief skips complex element content that we can't manage
912 static vlc_bool_t skip_element COMPLEX_INTERFACE
916 while( xml_ReaderRead( p_xml_reader ) == 1 )
918 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
920 psz_endname = xml_ReaderName( p_xml_reader );
923 if( !strcmp( psz_element, psz_endname ) )