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
30 #include <vlc_demux.h>
34 #include "vlc_strings.h"
40 playlist_item_t *p_item_in_category;
41 input_item_t **pp_tracklist;
42 int i_tracklist_entries;
47 static int Control( demux_t *, int, va_list );
48 static int Demux( demux_t * );
51 * \brief XSPF submodule initialization function
53 int E_(Import_xspf)( vlc_object_t *p_this )
55 DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xspf", "xspf-open",
56 "using XSPF playlist reader" );
60 void E_(Close_xspf)( vlc_object_t *p_this )
62 demux_t *p_demux = (demux_t *)p_this;
63 FREENULL( p_demux->p_sys->pp_tracklist );
64 FREENULL( p_demux->p_sys->psz_base );
65 free( p_demux->p_sys );
69 * \brief demuxer function for XSPF parsing
71 int Demux( demux_t *p_demux )
75 xml_reader_t *p_xml_reader = NULL;
76 char *psz_name = NULL;
78 p_demux->p_sys->pp_tracklist = NULL;
79 p_demux->p_sys->i_tracklist_entries = 0;
80 p_demux->p_sys->i_identifier = 0;
81 p_demux->p_sys->psz_base = NULL;
83 /* create new xml parser from stream */
84 p_xml = xml_Create( p_demux );
89 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
94 /* locating the root node */
99 if( xml_ReaderRead( p_xml_reader ) != 1 )
101 msg_Err( p_demux, "can't read xml stream" );
104 } while( i_ret == VLC_SUCCESS &&
105 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
107 /* checking root node name */
110 psz_name = xml_ReaderName( p_xml_reader );
111 if( !psz_name || strcmp( psz_name, "playlist" ) )
113 msg_Err( p_demux, "invalid root node name: %s", psz_name );
120 i_ret = parse_playlist_node( p_demux, p_playlist, p_current_input,
121 p_xml_reader, "playlist" ) ? 0 : -1;
124 for( i = 0 ; i < p_demux->p_sys->i_tracklist_entries ; i++ )
126 input_item_t *p_new_input = p_demux->p_sys->pp_tracklist[i];
129 input_ItemAddSubItem( 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 )
148 * \brief parse the root node of a XSPF playlist
149 * \param p_demux demuxer instance
150 * \param p_playlist playlist 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 vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
158 char *psz_value=NULL;
159 vlc_bool_t b_version_found = VLC_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 = VLC_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" ) )
202 else if( !strcmp( psz_name, "xml:base" ) )
204 p_demux->p_sys->psz_base = decode_URI_duplicate( 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,
264 case XML_READER_TEXT:
265 /* simple element content */
267 psz_value = xml_ReaderValue( p_xml_reader );
270 msg_Err( p_demux, "invalid xml stream" );
276 case XML_READER_ENDELEM:
277 /* element end tag */
278 psz_name = xml_ReaderName( p_xml_reader );
281 msg_Err( p_demux, "invalid xml stream" );
285 /* leave if the current parent node <playlist> is terminated */
286 if( !strcmp( psz_name, psz_element ) )
291 /* there MUST have been a start tag for that element name */
292 if( !p_handler || !p_handler->name
293 || strcmp( p_handler->name, psz_name ))
295 msg_Err( p_demux, "there's no open element left for <%s>",
301 if( p_handler->pf_handler.smpl )
303 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
311 /* unknown/unexpected xml node */
312 msg_Err( p_demux, "unexpected xml node %i", i_node );
322 * \brief parses the tracklist node which only may contain <track>s
324 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
330 /* now parse the <track>s */
331 while( xml_ReaderRead( p_xml_reader ) == 1 )
333 i_node = xml_ReaderNodeType( p_xml_reader );
334 if( i_node == XML_READER_STARTELEM )
336 psz_name = xml_ReaderName( p_xml_reader );
339 msg_Err( p_demux, "unexpected end of xml data" );
343 if( strcmp( psz_name, "track") )
345 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
352 /* parse the track data in a separate function */
353 if( parse_track_node( p_demux, p_playlist, p_input_item,
354 p_xml_reader,"track" ) == VLC_TRUE )
357 else if( i_node == XML_READER_ENDELEM )
361 /* the <trackList> has to be terminated */
362 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
364 msg_Err( p_demux, "there's a missing </trackList>" );
368 psz_name = xml_ReaderName( p_xml_reader );
369 if( !psz_name || strcmp( psz_name, "trackList" ) )
371 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
377 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
383 * \brief parse one track element
384 * \param COMPLEX_INTERFACE
386 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
388 input_item_t *p_new_input = NULL;
391 char *psz_value=NULL;
392 xml_elem_hnd_t *p_handler=NULL;
394 xml_elem_hnd_t track_elements[] =
395 { {"location", SIMPLE_CONTENT, {NULL} },
396 {"identifier", SIMPLE_CONTENT, {NULL} },
397 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
398 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
399 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
400 {"info", SIMPLE_CONTENT, {NULL} },
401 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
402 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
403 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
404 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
405 {"link", SIMPLE_CONTENT, {NULL} },
406 {"meta", SIMPLE_CONTENT, {NULL} },
407 {"extension", COMPLEX_CONTENT, {.cmplx = skip_element} },
408 {NULL, UNKNOWN_CONTENT, {NULL} }
411 while( xml_ReaderRead( p_xml_reader ) == 1 )
413 i_node = xml_ReaderNodeType( p_xml_reader );
416 case XML_READER_NONE:
419 case XML_READER_STARTELEM:
420 /* element start tag */
421 psz_name = xml_ReaderName( p_xml_reader );
422 if( !psz_name || !*psz_name )
424 msg_Err( p_demux, "invalid xml stream" );
429 for( p_handler = track_elements;
430 p_handler->name && strcmp( psz_name, p_handler->name );
432 if( !p_handler->name )
434 msg_Err( p_demux, "unexpected element <%s>", psz_name );
439 /* complex content is parsed in a separate function */
440 if( p_handler->type == COMPLEX_CONTENT )
445 "at <%s> level no new item has been allocated",
450 if( p_handler->pf_handler.cmplx( p_demux,
467 case XML_READER_TEXT:
468 /* simple element content */
470 psz_value = xml_ReaderValue( p_xml_reader );
473 msg_Err( p_demux, "invalid xml stream" );
479 case XML_READER_ENDELEM:
480 /* element end tag */
481 psz_name = xml_ReaderName( p_xml_reader );
484 msg_Err( p_demux, "invalid xml stream" );
488 /* leave if the current parent node <track> is terminated */
489 if( !strcmp( psz_name, psz_element ) )
492 if( p_demux->p_sys->i_identifier <
493 p_demux->p_sys->i_tracklist_entries )
495 p_demux->p_sys->pp_tracklist[
496 p_demux->p_sys->i_identifier ] = p_new_input;
500 if( p_demux->p_sys->i_identifier >
501 p_demux->p_sys->i_tracklist_entries )
503 p_demux->p_sys->i_tracklist_entries =
504 p_demux->p_sys->i_identifier;
506 INSERT_ELEM( p_demux->p_sys->pp_tracklist,
507 p_demux->p_sys->i_tracklist_entries,
508 p_demux->p_sys->i_tracklist_entries,
513 /* there MUST have been a start tag for that element name */
514 if( !p_handler || !p_handler->name
515 || strcmp( p_handler->name, psz_name ))
517 msg_Err( p_demux, "there's no open element left for <%s>",
523 /* special case: location */
524 if( !strcmp( p_handler->name, "location" ) )
527 /* there MUST NOT be an item */
530 msg_Err( p_demux, "item <%s> already created",
535 psz_uri = decode_URI_duplicate( psz_value );
539 if( p_demux->p_sys->psz_base &&
540 !strstr( psz_uri, "://" ) )
542 char* psz_tmp = malloc(
543 strlen(p_demux->p_sys->psz_base) +
544 strlen(psz_uri) +1 );
547 msg_Err( p_demux, "out of memory");
550 sprintf( psz_tmp, "%s%s",
551 p_demux->p_sys->psz_base, psz_uri );
555 p_new_input = input_ItemNewExt( p_playlist, psz_uri,
558 input_ItemCopyOptions( p_input_item, p_new_input );
569 else if( !strcmp( p_handler->name, "identifier" ) )
571 p_demux->p_sys->i_identifier = atoi( psz_value );
575 /* there MUST be an item */
578 msg_Err( p_demux, "item not yet created at <%s>",
583 if( p_handler->pf_handler.smpl )
585 p_handler->pf_handler.smpl( p_new_input,
596 /* unknown/unexpected xml node */
597 msg_Err( p_demux, "unexpected xml node %i", i_node );
603 msg_Err( p_demux, "unexpected end of xml data" );
609 * \brief handles the supported <track> sub-elements
611 static vlc_bool_t set_item_info SIMPLE_INTERFACE
613 /* exit if setting is impossible */
614 if( !psz_name || !psz_value || !p_input )
618 /* re-convert xml special characters inside psz_value */
619 resolve_xml_special_chars( psz_value );
621 /* handle each info element in a separate "if" clause */
622 if( !strcmp( psz_name, "title" ) )
624 input_item_SetTitle( p_input, psz_value );
626 else if( !strcmp( psz_name, "creator" ) )
628 input_item_SetArtist( p_input, psz_value );
630 else if( !strcmp( psz_name, "album" ) )
632 input_item_SetAlbum( p_input, psz_value );
635 else if( !strcmp( psz_name, "trackNum" ) )
637 input_item_SetTrackNum( p_input, psz_value );
639 else if( !strcmp( psz_name, "duration" ) )
641 long i_num = atol( psz_value );
642 input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
644 else if( !strcmp( psz_name, "annotation" ) )
646 input_item_SetDescription( p_input, psz_value );
648 else if( !strcmp( psz_name, "image" ) )
650 char *psz_uri = decode_URI_duplicate( psz_value );
651 input_item_SetArtURL( p_input, psz_uri );
659 * \brief parse the extension node of a XSPF playlist
661 static vlc_bool_t parse_extension_node COMPLEX_INTERFACE
663 char *psz_name = NULL;
664 char *psz_value = NULL;
665 char *psz_title = NULL;
666 char *psz_application = NULL;
668 xml_elem_hnd_t *p_handler = NULL;
669 input_item_t *p_new_input = NULL;
671 xml_elem_hnd_t pl_elements[] =
672 { {"node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
673 {"item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
674 {NULL, UNKNOWN_CONTENT, {NULL} }
677 /* read all extension node attributes */
678 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
680 psz_name = xml_ReaderName( p_xml_reader );
681 psz_value = xml_ReaderValue( p_xml_reader );
682 if( !psz_name || !psz_value )
684 msg_Err( p_demux, "invalid xml stream @ <node>" );
688 /* attribute: title */
689 if( !strcmp( psz_name, "title" ) )
691 resolve_xml_special_chars( psz_value );
692 psz_title = strdup( psz_value );
694 /* extension attribute: application */
695 else if( !strcmp( psz_name, "application" ) )
697 psz_application = strdup( psz_value );
699 /* unknown attribute */
701 msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element, psz_name );
706 /* attribute title is mandatory except for <extension> */
707 if( !strcmp( psz_element, "node" ) )
711 msg_Warn( p_demux, "<node> requires \"title\" attribute" );
714 p_new_input = input_ItemNewWithType( VLC_OBJECT( p_playlist ), "vlc:skip",
715 psz_title, 0, NULL, -1, ITEM_TYPE_DIRECTORY );
718 input_ItemAddSubItem( p_input_item, p_new_input );
719 p_input_item = p_new_input;
723 else if( !strcmp( psz_element, "extension" ) )
725 if( !psz_application )
727 msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
730 else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
732 msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
733 free( psz_application );
737 free( psz_application );
739 /* parse the child elements */
740 while( xml_ReaderRead( p_xml_reader ) == 1 )
742 i_node = xml_ReaderNodeType( p_xml_reader );
745 case XML_READER_NONE:
747 case XML_READER_STARTELEM:
748 /* element start tag */
749 psz_name = xml_ReaderName( p_xml_reader );
750 if( !psz_name || !*psz_name )
752 msg_Err( p_demux, "invalid xml stream" );
757 for( p_handler = pl_elements;
758 p_handler->name && strcmp( psz_name, p_handler->name );
760 if( !p_handler->name )
762 msg_Err( p_demux, "unexpected element <%s>", psz_name );
767 /* complex content is parsed in a separate function */
768 if( p_handler->type == COMPLEX_CONTENT )
770 if( p_handler->pf_handler.cmplx( p_demux,
787 case XML_READER_TEXT:
788 /* simple element content */
790 psz_value = xml_ReaderValue( p_xml_reader );
793 msg_Err( p_demux, "invalid xml stream" );
799 case XML_READER_ENDELEM:
800 /* element end tag */
801 psz_name = xml_ReaderName( p_xml_reader );
804 msg_Err( p_demux, "invalid xml stream" );
808 /* leave if the current parent node is terminated */
809 if( !strcmp( psz_name, psz_element ) )
814 /* there MUST have been a start tag for that element name */
815 if( !p_handler || !p_handler->name
816 || strcmp( p_handler->name, psz_name ))
818 msg_Err( p_demux, "there's no open element left for <%s>",
824 if( p_handler->pf_handler.smpl )
826 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
834 /* unknown/unexpected xml node */
835 msg_Err( p_demux, "unexpected xml node %i", i_node );
845 * \brief parse the extension item node of a XSPF playlist
847 static vlc_bool_t parse_extitem_node COMPLEX_INTERFACE
849 input_item_t *p_new_input = NULL;
850 char *psz_name = NULL;
851 char *psz_value = NULL;
854 /* read all extension item attributes */
855 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
857 psz_name = xml_ReaderName( p_xml_reader );
858 psz_value = xml_ReaderValue( p_xml_reader );
859 if( !psz_name || !psz_value )
861 msg_Err( p_demux, "invalid xml stream @ <item>" );
865 /* attribute: href */
866 if( !strcmp( psz_name, "href" ) )
868 i_href = atoi( psz_value );
870 /* unknown attribute */
872 msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
877 /* attribute href is mandatory */
880 msg_Warn( p_demux, "<item> requires \"href\" attribute" );
884 if( i_href >= p_demux->p_sys->i_tracklist_entries )
886 msg_Warn( p_demux, "invalid \"href\" attribute" );
890 p_new_input = p_demux->p_sys->pp_tracklist[ i_href ];
893 input_ItemAddSubItem( p_input_item, p_new_input );
894 p_demux->p_sys->pp_tracklist[i_href] = NULL;
897 /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
898 /* (libxml sends NONE) */
899 xml_ReaderRead( p_xml_reader );
905 * \brief skips complex element content that we can't manage
907 static vlc_bool_t skip_element COMPLEX_INTERFACE
911 while( xml_ReaderRead( p_xml_reader ) == 1 )
913 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
915 psz_endname = xml_ReaderName( p_xml_reader );
918 if( !strcmp( psz_element, psz_endname ) )