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 /* FIXME: We are leaking that one */
556 p_new_input = input_ItemNewExt( p_playlist, psz_uri,
559 input_ItemCopyOptions( p_input_item, p_new_input );
570 else if( !strcmp( p_handler->name, "identifier" ) )
572 p_demux->p_sys->i_identifier = atoi( psz_value );
576 /* there MUST be an item */
579 msg_Err( p_demux, "item not yet created at <%s>",
584 if( p_handler->pf_handler.smpl )
586 p_handler->pf_handler.smpl( p_new_input,
597 /* unknown/unexpected xml node */
598 msg_Err( p_demux, "unexpected xml node %i", i_node );
604 msg_Err( p_demux, "unexpected end of xml data" );
610 * \brief handles the supported <track> sub-elements
612 static vlc_bool_t set_item_info SIMPLE_INTERFACE
614 /* exit if setting is impossible */
615 if( !psz_name || !psz_value || !p_input )
619 /* re-convert xml special characters inside psz_value */
620 resolve_xml_special_chars( psz_value );
622 /* handle each info element in a separate "if" clause */
623 if( !strcmp( psz_name, "title" ) )
625 input_item_SetTitle( p_input, psz_value );
627 else if( !strcmp( psz_name, "creator" ) )
629 input_item_SetArtist( p_input, psz_value );
631 else if( !strcmp( psz_name, "album" ) )
633 input_item_SetAlbum( p_input, psz_value );
636 else if( !strcmp( psz_name, "trackNum" ) )
638 input_item_SetTrackNum( p_input, psz_value );
640 else if( !strcmp( psz_name, "duration" ) )
642 long i_num = atol( psz_value );
643 input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
645 else if( !strcmp( psz_name, "annotation" ) )
647 input_item_SetDescription( p_input, psz_value );
649 else if( !strcmp( psz_name, "image" ) )
651 char *psz_uri = decode_URI_duplicate( psz_value );
652 input_item_SetArtURL( p_input, psz_uri );
660 * \brief parse the extension node of a XSPF playlist
662 static vlc_bool_t parse_extension_node COMPLEX_INTERFACE
664 char *psz_name = NULL;
665 char *psz_value = NULL;
666 char *psz_title = NULL;
667 char *psz_application = NULL;
669 xml_elem_hnd_t *p_handler = NULL;
670 input_item_t *p_new_input = NULL;
672 xml_elem_hnd_t pl_elements[] =
673 { {"node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
674 {"item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
675 {NULL, UNKNOWN_CONTENT, {NULL} }
678 /* read all extension node attributes */
679 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
681 psz_name = xml_ReaderName( p_xml_reader );
682 psz_value = xml_ReaderValue( p_xml_reader );
683 if( !psz_name || !psz_value )
685 msg_Err( p_demux, "invalid xml stream @ <node>" );
689 /* attribute: title */
690 if( !strcmp( psz_name, "title" ) )
692 resolve_xml_special_chars( psz_value );
693 psz_title = strdup( psz_value );
695 /* extension attribute: application */
696 else if( !strcmp( psz_name, "application" ) )
698 psz_application = strdup( psz_value );
700 /* unknown attribute */
702 msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element, psz_name );
707 /* attribute title is mandatory except for <extension> */
708 if( !strcmp( psz_element, "node" ) )
712 msg_Warn( p_demux, "<node> requires \"title\" attribute" );
715 p_new_input = input_ItemNewWithType( VLC_OBJECT( p_playlist ), "vlc:nop",
716 psz_title, 0, NULL, -1, ITEM_TYPE_DIRECTORY );
719 input_ItemAddSubItem( p_input_item, p_new_input );
720 p_input_item = p_new_input;
724 else if( !strcmp( psz_element, "extension" ) )
726 if( !psz_application )
728 msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
731 else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
733 msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
734 free( psz_application );
738 free( psz_application );
740 /* parse the child elements */
741 while( xml_ReaderRead( p_xml_reader ) == 1 )
743 i_node = xml_ReaderNodeType( p_xml_reader );
746 case XML_READER_NONE:
748 case XML_READER_STARTELEM:
749 /* element start tag */
750 psz_name = xml_ReaderName( p_xml_reader );
751 if( !psz_name || !*psz_name )
753 msg_Err( p_demux, "invalid xml stream" );
758 for( p_handler = pl_elements;
759 p_handler->name && strcmp( psz_name, p_handler->name );
761 if( !p_handler->name )
763 msg_Err( p_demux, "unexpected element <%s>", psz_name );
768 /* complex content is parsed in a separate function */
769 if( p_handler->type == COMPLEX_CONTENT )
771 if( p_handler->pf_handler.cmplx( p_demux,
788 case XML_READER_TEXT:
789 /* simple element content */
791 psz_value = xml_ReaderValue( p_xml_reader );
794 msg_Err( p_demux, "invalid xml stream" );
800 case XML_READER_ENDELEM:
801 /* element end tag */
802 psz_name = xml_ReaderName( p_xml_reader );
805 msg_Err( p_demux, "invalid xml stream" );
809 /* leave if the current parent node is terminated */
810 if( !strcmp( psz_name, psz_element ) )
815 /* there MUST have been a start tag for that element name */
816 if( !p_handler || !p_handler->name
817 || strcmp( p_handler->name, psz_name ))
819 msg_Err( p_demux, "there's no open element left for <%s>",
825 if( p_handler->pf_handler.smpl )
827 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
835 /* unknown/unexpected xml node */
836 msg_Err( p_demux, "unexpected xml node %i", i_node );
846 * \brief parse the extension item node of a XSPF playlist
848 static vlc_bool_t parse_extitem_node COMPLEX_INTERFACE
850 input_item_t *p_new_input = NULL;
851 char *psz_name = NULL;
852 char *psz_value = NULL;
855 /* read all extension item attributes */
856 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
858 psz_name = xml_ReaderName( p_xml_reader );
859 psz_value = xml_ReaderValue( p_xml_reader );
860 if( !psz_name || !psz_value )
862 msg_Err( p_demux, "invalid xml stream @ <item>" );
866 /* attribute: href */
867 if( !strcmp( psz_name, "href" ) )
869 i_href = atoi( psz_value );
871 /* unknown attribute */
873 msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
878 /* attribute href is mandatory */
881 msg_Warn( p_demux, "<item> requires \"href\" attribute" );
885 if( i_href >= p_demux->p_sys->i_tracklist_entries )
887 msg_Warn( p_demux, "invalid \"href\" attribute" );
891 p_new_input = p_demux->p_sys->pp_tracklist[ i_href ];
894 input_ItemAddSubItem( p_input_item, p_new_input );
895 p_demux->p_sys->pp_tracklist[i_href] = NULL;
898 /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
899 /* (libxml sends NONE) */
900 xml_ReaderRead( p_xml_reader );
906 * \brief skips complex element content that we can't manage
908 static vlc_bool_t skip_element COMPLEX_INTERFACE
912 while( xml_ReaderRead( p_xml_reader ) == 1 )
914 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
916 psz_endname = xml_ReaderName( p_xml_reader );
919 if( !strcmp( psz_element, psz_endname ) )