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;
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 );
98 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
103 /* locating the root node */
108 if( xml_ReaderRead( p_xml_reader ) != 1 )
110 msg_Err( p_demux, "can't read xml stream" );
113 } while( i_ret == VLC_SUCCESS &&
114 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
116 /* checking root node name */
119 psz_name = xml_ReaderName( p_xml_reader );
120 if( !psz_name || strcmp( psz_name, "playlist" ) )
122 msg_Err( p_demux, "invalid root node name: %s", psz_name );
129 i_ret = parse_playlist_node( p_demux, p_current_input,
130 p_xml_reader, "playlist" ) ? 0 : -1;
133 for( i = 0 ; i < p_demux->p_sys->i_tracklist_entries ; i++ )
135 input_item_t *p_new_input = p_demux->p_sys->pp_tracklist[i];
138 input_item_AddSubItem( p_current_input, p_new_input );
142 HANDLE_PLAY_AND_RELEASE;
144 xml_ReaderDelete( p_xml, p_xml_reader );
147 return i_ret; /* Needed for correct operation of go back */
150 /** \brief dummy function for demux callback interface */
151 static int Control( demux_t *p_demux, int i_query, va_list args )
153 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
158 * \brief parse the root node of a XSPF playlist
159 * \param p_demux demuxer instance
160 * \param p_input_item current input item
161 * \param p_xml_reader xml reader instance
162 * \param psz_element name of element to parse
164 static bool parse_playlist_node COMPLEX_INTERFACE
166 char *psz_name = NULL;
167 char *psz_value = NULL;
168 bool b_version_found = false;
170 xml_elem_hnd_t *p_handler = NULL;
172 xml_elem_hnd_t pl_elements[] =
173 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
174 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
175 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
176 {"info", SIMPLE_CONTENT, {NULL} },
177 {"location", SIMPLE_CONTENT, {NULL} },
178 {"identifier", SIMPLE_CONTENT, {NULL} },
179 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
180 {"date", SIMPLE_CONTENT, {NULL} },
181 {"license", SIMPLE_CONTENT, {NULL} },
182 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
183 {"link", SIMPLE_CONTENT, {NULL} },
184 {"meta", SIMPLE_CONTENT, {NULL} },
185 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
186 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
187 {NULL, UNKNOWN_CONTENT, {NULL} }
190 /* read all playlist attributes */
191 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
193 psz_name = xml_ReaderName( p_xml_reader );
194 psz_value = xml_ReaderValue( p_xml_reader );
195 if( !psz_name || !psz_value )
197 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
201 /* attribute: version */
202 if( !strcmp( psz_name, "version" ) )
204 b_version_found = true;
205 if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
206 msg_Warn( p_demux, "unsupported XSPF version" );
208 /* attribute: xmlns */
209 else if( !strcmp( psz_name, "xmlns" ) )
211 else if( !strcmp( psz_name, "xml:base" ) )
213 p_demux->p_sys->psz_base = decode_URI_duplicate( psz_value );
215 /* unknown attribute */
217 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
221 /* attribute version is mandatory !!! */
222 if( !b_version_found )
223 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
225 /* parse the child elements - we only take care of <trackList> */
226 while( xml_ReaderRead( p_xml_reader ) == 1 )
228 i_node = xml_ReaderNodeType( p_xml_reader );
231 case XML_READER_NONE:
233 case XML_READER_STARTELEM:
234 /* element start tag */
235 psz_name = xml_ReaderName( p_xml_reader );
236 if( !psz_name || !*psz_name )
238 msg_Err( p_demux, "invalid xml stream" );
243 for( p_handler = pl_elements;
244 p_handler->name && strcmp( psz_name, p_handler->name );
246 if( !p_handler->name )
248 msg_Err( p_demux, "unexpected element <%s>", psz_name );
253 /* complex content is parsed in a separate function */
254 if( p_handler->type == COMPLEX_CONTENT )
256 if( p_handler->pf_handler.cmplx( p_demux,
272 case XML_READER_TEXT:
273 /* simple element content */
275 psz_value = xml_ReaderValue( p_xml_reader );
278 msg_Err( p_demux, "invalid xml stream" );
284 case XML_READER_ENDELEM:
285 /* element end tag */
286 psz_name = xml_ReaderName( p_xml_reader );
289 msg_Err( p_demux, "invalid xml stream" );
293 /* leave if the current parent node <playlist> is terminated */
294 if( !strcmp( psz_name, psz_element ) )
299 /* there MUST have been a start tag for that element name */
300 if( !p_handler || !p_handler->name
301 || strcmp( p_handler->name, psz_name ))
303 msg_Err( p_demux, "there's no open element left for <%s>",
309 if( p_handler->pf_handler.smpl )
311 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
319 /* unknown/unexpected xml node */
320 msg_Err( p_demux, "unexpected xml node %i", i_node );
330 * \brief parses the tracklist node which only may contain <track>s
332 static bool parse_tracklist_node COMPLEX_INTERFACE
334 VLC_UNUSED(psz_element);
335 char *psz_name = NULL;
339 /* now parse the <track>s */
340 while( xml_ReaderRead( p_xml_reader ) == 1 )
342 i_node = xml_ReaderNodeType( p_xml_reader );
343 if( i_node == XML_READER_STARTELEM )
345 psz_name = xml_ReaderName( p_xml_reader );
348 msg_Err( p_demux, "unexpected end of xml data" );
352 if( strcmp( psz_name, "track") )
354 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
361 /* parse the track data in a separate function */
362 if( parse_track_node( p_demux, p_input_item,
363 p_xml_reader,"track" ) == true )
366 else if( i_node == XML_READER_ENDELEM )
370 /* the <trackList> has to be terminated */
371 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
373 msg_Err( p_demux, "there's a missing </trackList>" );
377 psz_name = xml_ReaderName( p_xml_reader );
378 if( !psz_name || strcmp( psz_name, "trackList" ) )
380 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
386 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
392 * \brief parse one track element
393 * \param COMPLEX_INTERFACE
395 static bool parse_track_node COMPLEX_INTERFACE
397 input_item_t *p_new_input = NULL;
399 char *psz_name = NULL;
400 char *psz_value = NULL;
401 xml_elem_hnd_t *p_handler = NULL;
403 xml_elem_hnd_t track_elements[] =
404 { {"location", SIMPLE_CONTENT, {NULL} },
405 {"identifier", SIMPLE_CONTENT, {NULL} },
406 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
407 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
408 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
409 {"info", SIMPLE_CONTENT, {NULL} },
410 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
411 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
412 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
413 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
414 {"link", SIMPLE_CONTENT, {NULL} },
415 {"meta", SIMPLE_CONTENT, {NULL} },
416 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
417 {NULL, UNKNOWN_CONTENT, {NULL} }
420 /* reset i_track_id */
421 p_demux->p_sys->i_track_id = -1;
423 while( xml_ReaderRead( p_xml_reader ) == 1 )
425 i_node = xml_ReaderNodeType( p_xml_reader );
428 case XML_READER_NONE:
431 case XML_READER_STARTELEM:
432 /* element start tag */
433 psz_name = xml_ReaderName( p_xml_reader );
434 if( !psz_name || !*psz_name )
436 msg_Err( p_demux, "invalid xml stream" );
441 for( p_handler = track_elements;
442 p_handler->name && strcmp( psz_name, p_handler->name );
444 if( !p_handler->name )
446 msg_Err( p_demux, "unexpected element <%s>", psz_name );
451 /* complex content is parsed in a separate function */
452 if( p_handler->type == COMPLEX_CONTENT )
457 "at <%s> level no new item has been allocated",
462 if( p_handler->pf_handler.cmplx( p_demux,
478 case XML_READER_TEXT:
479 /* simple element content */
481 psz_value = xml_ReaderValue( p_xml_reader );
484 msg_Err( p_demux, "invalid xml stream" );
490 case XML_READER_ENDELEM:
491 /* element end tag */
492 psz_name = xml_ReaderName( p_xml_reader );
495 msg_Err( p_demux, "invalid xml stream" );
499 /* leave if the current parent node <track> is terminated */
500 if( !strcmp( psz_name, psz_element ) )
504 if( p_demux->p_sys->i_track_id < 0 )
508 input_item_AddSubItem( p_input_item, p_new_input );
509 vlc_gc_decref( p_new_input );
514 if( p_demux->p_sys->i_track_id >=
515 p_demux->p_sys->i_tracklist_entries )
518 pp = realloc( p_demux->p_sys->pp_tracklist,
519 (p_demux->p_sys->i_track_id + 1) * sizeof(*pp) );
522 p_demux->p_sys->pp_tracklist = pp;
523 while( p_demux->p_sys->i_track_id >=
524 p_demux->p_sys->i_tracklist_entries )
525 pp[p_demux->p_sys->i_tracklist_entries++] = NULL;
528 p_demux->p_sys->pp_tracklist[
529 p_demux->p_sys->i_track_id ] = p_new_input;
532 /* there MUST have been a start tag for that element name */
533 if( !p_handler || !p_handler->name
534 || strcmp( p_handler->name, psz_name ))
536 msg_Err( p_demux, "there's no open element left for <%s>",
542 /* special case: location */
543 if( !strcmp( p_handler->name, "location" ) )
545 char *psz_uri = NULL;
546 /* there MUST NOT be an item */
549 msg_Err( p_demux, "item <%s> already created",
554 psz_uri = decode_URI_duplicate( psz_value );
558 if( p_demux->p_sys->psz_base &&
559 !strstr( psz_uri, "://" ) )
561 char* psz_tmp = malloc(
562 strlen(p_demux->p_sys->psz_base) +
563 strlen(psz_uri) +1 );
566 sprintf( psz_tmp, "%s%s",
567 p_demux->p_sys->psz_base, psz_uri );
571 p_new_input = input_item_NewExt( p_demux, psz_uri,
574 input_item_CopyOptions( p_input_item, p_new_input );
587 /* there MUST be an item */
590 msg_Err( p_demux, "item not yet created at <%s>",
595 if( p_handler->pf_handler.smpl )
597 p_handler->pf_handler.smpl( p_new_input,
608 /* unknown/unexpected xml node */
609 msg_Err( p_demux, "unexpected xml node %i", i_node );
615 msg_Err( p_demux, "unexpected end of xml data" );
621 * \brief handles the supported <track> sub-elements
623 static bool set_item_info SIMPLE_INTERFACE
625 /* exit if setting is impossible */
626 if( !psz_name || !psz_value || !p_input )
630 /* re-convert xml special characters inside psz_value */
631 resolve_xml_special_chars( psz_value );
633 /* handle each info element in a separate "if" clause */
634 if( !strcmp( psz_name, "title" ) )
636 input_item_SetTitle( p_input, psz_value );
638 else if( !strcmp( psz_name, "creator" ) )
640 input_item_SetArtist( p_input, psz_value );
642 else if( !strcmp( psz_name, "album" ) )
644 input_item_SetAlbum( p_input, psz_value );
647 else if( !strcmp( psz_name, "trackNum" ) )
649 input_item_SetTrackNum( p_input, psz_value );
651 else if( !strcmp( psz_name, "duration" ) )
653 long i_num = atol( psz_value );
654 input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
656 else if( !strcmp( psz_name, "annotation" ) )
658 input_item_SetDescription( p_input, psz_value );
660 else if( !strcmp( psz_name, "image" ) )
662 char *psz_uri = decode_URI_duplicate( psz_value );
663 input_item_SetArtURL( p_input, psz_uri );
670 * \brief handles the <vlc:option> elements
672 static bool set_option SIMPLE_INTERFACE
674 /* exit if setting is impossible */
675 if( !psz_name || !psz_value || !p_input )
678 /* re-convert xml special characters inside psz_value */
679 resolve_xml_special_chars( psz_value );
681 input_item_AddOpt( p_input, psz_value, 0 );
687 * \brief parse the extension node of a XSPF playlist
689 static bool parse_extension_node COMPLEX_INTERFACE
691 char *psz_name = NULL;
692 char *psz_value = NULL;
693 char *psz_title = NULL;
694 char *psz_application = NULL;
696 bool b_release_input_item = false;
697 xml_elem_hnd_t *p_handler = NULL;
698 input_item_t *p_new_input = NULL;
700 xml_elem_hnd_t pl_elements[] =
701 { {"vlc:node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
702 {"vlc:item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
703 {"vlc:id", SIMPLE_CONTENT, {NULL} },
704 {"vlc:option", SIMPLE_CONTENT, {.smpl = set_option} },
705 {NULL, UNKNOWN_CONTENT, {NULL} }
708 /* read all extension node attributes */
709 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
711 psz_name = xml_ReaderName( p_xml_reader );
712 psz_value = xml_ReaderValue( p_xml_reader );
713 if( !psz_name || !psz_value )
715 msg_Err( p_demux, "invalid xml stream @ <vlc:node>" );
719 /* attribute: title */
720 if( !strcmp( psz_name, "title" ) )
722 resolve_xml_special_chars( psz_value );
723 psz_title = strdup( psz_value );
725 /* extension attribute: application */
726 else if( !strcmp( psz_name, "application" ) )
728 psz_application = strdup( psz_value );
730 /* unknown attribute */
732 msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element,
738 /* attribute title is mandatory except for <extension> */
739 if( !strcmp( psz_element, "vlc:node" ) )
743 msg_Warn( p_demux, "<vlc:node> requires \"title\" attribute" );
746 p_new_input = input_item_NewWithType( VLC_OBJECT( p_demux ),
747 "vlc://nop", psz_title, 0, NULL, -1,
748 ITEM_TYPE_DIRECTORY );
751 input_item_AddSubItem( p_input_item, p_new_input );
752 p_input_item = p_new_input;
753 b_release_input_item = true;
757 else if( !strcmp( psz_element, "extension" ) )
759 if( !psz_application )
761 msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
764 else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
766 msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
767 free( psz_application );
771 free( psz_application );
773 /* parse the child elements */
774 while( xml_ReaderRead( p_xml_reader ) == 1 )
776 i_node = xml_ReaderNodeType( p_xml_reader );
779 case XML_READER_NONE:
781 case XML_READER_STARTELEM:
782 /* element start tag */
783 psz_name = xml_ReaderName( p_xml_reader );
784 if( !psz_name || !*psz_name )
786 msg_Err( p_demux, "invalid xml stream" );
788 if( b_release_input_item ) vlc_gc_decref( p_new_input );
792 for( p_handler = pl_elements;
793 p_handler->name && strcmp( psz_name, p_handler->name );
795 if( !p_handler->name )
797 msg_Err( p_demux, "unexpected element <%s>", psz_name );
799 if( b_release_input_item ) vlc_gc_decref( p_new_input );
803 /* complex content is parsed in a separate function */
804 if( p_handler->type == COMPLEX_CONTENT )
806 if( p_handler->pf_handler.cmplx( p_demux,
817 if( b_release_input_item ) vlc_gc_decref( p_new_input );
823 case XML_READER_TEXT:
824 /* simple element content */
826 psz_value = xml_ReaderValue( p_xml_reader );
829 msg_Err( p_demux, "invalid xml stream" );
831 if( b_release_input_item ) vlc_gc_decref( p_new_input );
836 case XML_READER_ENDELEM:
837 /* element end tag */
838 psz_name = xml_ReaderName( p_xml_reader );
841 msg_Err( p_demux, "invalid xml stream" );
843 if( b_release_input_item ) vlc_gc_decref( p_new_input );
846 /* leave if the current parent node is terminated */
847 if( !strcmp( psz_name, psz_element ) )
850 if( b_release_input_item ) vlc_gc_decref( p_new_input );
853 /* there MUST have been a start tag for that element name */
854 if( !p_handler || !p_handler->name
855 || strcmp( p_handler->name, psz_name ))
857 msg_Err( p_demux, "there's no open element left for <%s>",
860 if( b_release_input_item ) vlc_gc_decref( p_new_input );
864 /* special tag <vlc:id> */
865 if( !strcmp( p_handler->name, "vlc:id" ) )
867 p_demux->p_sys->i_track_id = atoi( psz_value );
869 else if( p_handler->pf_handler.smpl )
871 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
879 /* unknown/unexpected xml node */
880 msg_Err( p_demux, "unexpected xml node %i", i_node );
882 if( b_release_input_item ) vlc_gc_decref( p_new_input );
887 if( b_release_input_item ) vlc_gc_decref( p_new_input );
892 * \brief parse the extension item node of a XSPF playlist
894 static bool parse_extitem_node COMPLEX_INTERFACE
896 VLC_UNUSED(psz_element);
897 input_item_t *p_new_input = NULL;
898 char *psz_name = NULL;
899 char *psz_value = NULL;
902 /* read all extension item attributes */
903 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
905 psz_name = xml_ReaderName( p_xml_reader );
906 psz_value = xml_ReaderValue( p_xml_reader );
907 if( !psz_name || !psz_value )
909 msg_Err( p_demux, "invalid xml stream @ <vlc:item>" );
913 /* attribute: href */
914 if( !strcmp( psz_name, "tid" ) )
916 i_tid = atoi( psz_value );
918 /* unknown attribute */
920 msg_Warn( p_demux, "invalid <vlc:item> attribute:\"%s\"", psz_name);
925 /* attribute href is mandatory */
928 msg_Warn( p_demux, "<vlc:item> requires \"tid\" attribute" );
932 if( i_tid >= p_demux->p_sys->i_tracklist_entries )
934 msg_Warn( p_demux, "invalid \"tid\" attribute" );
938 p_new_input = p_demux->p_sys->pp_tracklist[ i_tid ];
941 input_item_AddSubItem( p_input_item, p_new_input );
942 vlc_gc_decref( p_new_input );
943 p_demux->p_sys->pp_tracklist[i_tid] = NULL;
946 /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
947 /* (libxml sends NONE) */
948 xml_ReaderRead( p_xml_reader );
954 * \brief skips complex element content that we can't manage
956 static bool skip_element COMPLEX_INTERFACE
958 VLC_UNUSED(p_demux); VLC_UNUSED(p_input_item);
961 while( xml_ReaderRead( p_xml_reader ) == 1 )
963 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
965 psz_endname = xml_ReaderName( p_xml_reader );
968 if( !strcmp( psz_element, psz_endname ) )