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;
66 demux_sys_t *p_sys = p_demux->p_sys;
67 for( int i = 0; i < p_sys->i_tracklist_entries; i++ )
69 if( p_sys->pp_tracklist[i] )
70 vlc_gc_decref( p_sys->pp_tracklist[i] );
72 free( p_sys->pp_tracklist );
73 free( p_sys->psz_base );
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;
86 input_item_t *p_current_input = GetCurrentItem(p_demux);
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 );
97 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
101 /* locating the root node */
104 if( xml_ReaderRead( p_xml_reader ) != 1 )
106 msg_Err( p_demux, "can't read xml stream" );
109 } while( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
111 /* checking root node name */
112 psz_name = xml_ReaderName( p_xml_reader );
113 if( !psz_name || strcmp( psz_name, "playlist" ) )
115 msg_Err( p_demux, "invalid root node name: %s", psz_name );
121 input_item_node_t *p_subitems =
122 input_item_node_Create( p_current_input );
124 i_ret = parse_playlist_node( p_demux, p_subitems,
125 p_xml_reader, "playlist" ) ? 0 : -1;
127 for( int i = 0 ; i < p_demux->p_sys->i_tracklist_entries ; i++ )
129 input_item_t *p_new_input = p_demux->p_sys->pp_tracklist[i];
132 input_item_AddSubItem( p_current_input, p_new_input );
133 input_item_node_AppendItem( p_subitems, p_new_input );
137 input_item_AddSubItemTree( p_subitems );
138 input_item_node_Delete( p_subitems );
141 vlc_gc_decref(p_current_input);
143 xml_ReaderDelete( p_xml, p_xml_reader );
146 return i_ret; /* Needed for correct operation of go back */
149 /** \brief dummy function for demux callback interface */
150 static int Control( demux_t *p_demux, int i_query, va_list args )
152 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
157 * \brief parse the root node of a XSPF playlist
158 * \param p_demux demuxer instance
159 * \param p_input_item current input item
160 * \param p_xml_reader xml reader instance
161 * \param psz_element name of element to parse
163 static bool parse_playlist_node COMPLEX_INTERFACE
165 input_item_t *p_input_item = p_input_node->p_item;
166 char *psz_name = NULL;
167 char *psz_value = NULL;
168 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>" );
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" ) || !strcmp( psz_name, "xmlns:vlc" ) )
211 else if( !strcmp( psz_name, "xml:base" ) )
213 p_demux->p_sys->psz_base = strdup( psz_value );
215 /* unknown attribute */
217 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> */
229 while( xml_ReaderRead( p_xml_reader ) == 1 )
231 i_node = xml_ReaderNodeType( p_xml_reader );
234 case XML_READER_NONE:
236 case XML_READER_STARTELEM:
237 /* element start tag */
238 psz_name = xml_ReaderName( p_xml_reader );
239 if( !psz_name || !*psz_name )
241 msg_Err( p_demux, "invalid xml stream" );
245 for( p_handler = pl_elements;
246 p_handler->name && strcmp( psz_name, p_handler->name );
248 if( !p_handler->name )
250 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 )
258 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" );
283 case XML_READER_ENDELEM:
284 /* element end tag */
285 psz_name = xml_ReaderName( p_xml_reader );
288 msg_Err( p_demux, "invalid xml stream" );
291 /* leave if the current parent node <playlist> is terminated */
292 if( !strcmp( psz_name, psz_element ) )
297 /* there MUST have been a start tag for that element name */
298 if( !p_handler || !p_handler->name
299 || strcmp( p_handler->name, psz_name ))
301 msg_Err( p_demux, "there's no open element left for <%s>",
306 if( p_handler->pf_handler.smpl )
308 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
316 /* unknown/unexpected xml node */
317 msg_Err( p_demux, "unexpected xml node %i", i_node );
329 * \brief parses the tracklist node which only may contain <track>s
331 static bool parse_tracklist_node COMPLEX_INTERFACE
333 VLC_UNUSED(psz_element);
337 /* now parse the <track>s */
338 while( xml_ReaderRead( p_xml_reader ) == 1 )
340 int i_node = xml_ReaderNodeType( p_xml_reader );
341 if( i_node == XML_READER_STARTELEM )
343 char *psz_eltname = xml_ReaderName( p_xml_reader );
346 msg_Err( p_demux, "unexpected end of xml data" );
350 if( strcmp( psz_eltname, "track") )
352 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
359 /* parse the track data in a separate function */
360 if( parse_track_node( p_demux, p_input_node,
361 p_xml_reader, "track" ) )
364 else if( i_node == XML_READER_ENDELEM )
368 /* the <trackList> has to be terminated */
369 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
371 msg_Err( p_demux, "there's a missing </trackList>" );
374 psz_name = xml_ReaderName( p_xml_reader );
375 if( !psz_name || strcmp( psz_name, "trackList" ) )
377 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
383 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
388 * \brief parse one track element
389 * \param COMPLEX_INTERFACE
391 static bool parse_track_node COMPLEX_INTERFACE
393 input_item_t *p_input_item = p_input_node->p_item;
394 char *psz_name = NULL;
395 char *psz_value = NULL;
396 xml_elem_hnd_t *p_handler = NULL;
397 demux_sys_t *p_sys = p_demux->p_sys;
400 xml_elem_hnd_t track_elements[] =
401 { {"location", SIMPLE_CONTENT, {NULL} },
402 {"identifier", SIMPLE_CONTENT, {NULL} },
403 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
404 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
405 {"annotation", SIMPLE_CONTENT, {.smpl = set_item_info} },
406 {"info", SIMPLE_CONTENT, {NULL} },
407 {"image", SIMPLE_CONTENT, {.smpl = set_item_info} },
408 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
409 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
410 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
411 {"link", SIMPLE_CONTENT, {NULL} },
412 {"meta", SIMPLE_CONTENT, {NULL} },
413 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
414 {NULL, UNKNOWN_CONTENT, {NULL} }
417 input_item_t *p_new_input = input_item_New( p_demux, NULL, NULL );
418 input_item_node_t *p_new_node = input_item_node_Create( p_new_input );
422 /* malloc has failed for input_item_New, so bailout early */
426 /* reset i_track_id */
427 p_sys->i_track_id = -1;
429 while( xml_ReaderRead( p_xml_reader ) == 1 )
431 int i_node = xml_ReaderNodeType( p_xml_reader );
434 case XML_READER_NONE:
437 case XML_READER_STARTELEM:
438 /* element start tag */
439 psz_name = xml_ReaderName( p_xml_reader );
440 if( !psz_name || !*psz_name )
442 msg_Err( p_demux, "invalid xml stream" );
446 for( p_handler = track_elements;
447 p_handler->name && strcmp( psz_name, p_handler->name );
449 if( !p_handler->name )
451 msg_Err( p_demux, "unexpected element <%s>", psz_name );
455 /* complex content is parsed in a separate function */
456 if( p_handler->type == COMPLEX_CONTENT )
460 bool b_res = p_handler->pf_handler.cmplx( p_demux,
475 case XML_READER_TEXT:
476 /* simple element content */
478 psz_value = xml_ReaderValue( p_xml_reader );
481 msg_Err( p_demux, "invalid xml stream" );
486 case XML_READER_ENDELEM:
487 /* element end tag */
488 psz_name = xml_ReaderName( p_xml_reader );
491 msg_Err( p_demux, "invalid xml stream" );
495 /* leave if the current parent node <track> is terminated */
496 if( !strcmp( psz_name, psz_element ) )
501 /* Make sure we have a URI */
502 char *psz_uri = input_item_GetURI( p_new_input );
505 input_item_SetURI( p_new_input, "vlc://nop" );
509 if( p_sys->i_track_id < 0 )
511 input_item_AddSubItem( p_input_item, p_new_input );
512 input_item_node_AppendNode( p_input_node, p_new_node );
513 vlc_gc_decref( p_new_input );
517 if( p_sys->i_track_id >= p_sys->i_tracklist_entries )
520 pp = realloc( p_sys->pp_tracklist,
521 (p_sys->i_track_id + 1) * sizeof(*pp) );
524 p_sys->pp_tracklist = pp;
525 while( p_sys->i_track_id >= p_sys->i_tracklist_entries )
526 pp[p_sys->i_tracklist_entries++] = NULL;
529 p_sys->pp_tracklist[ 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>",
541 /* special case: location */
542 if( !strcmp( p_handler->name, "location" ) )
544 /* FIXME: This is broken. Scheme-relative (//...) locations
545 * and anchors (#...) are not resolved correctly. Also,
546 * host-relative (/...) and directory-relative locations
547 * ("relative path" in vernacular) should be resolved.
548 * Last, psz_base should default to the XSPF resource
549 * location if missing (not the current working directory).
551 if( p_sys->psz_base && !strstr( psz_value, "://" ) )
554 if( asprintf( &psz_tmp, "%s%s", p_sys->psz_base,
559 input_item_SetURI( p_new_input, psz_tmp );
563 input_item_SetURI( p_new_input, psz_value );
564 input_item_CopyOptions( p_input_item, p_new_input );
568 /* there MUST be an item */
569 if( p_handler->pf_handler.smpl )
571 p_handler->pf_handler.smpl( p_new_input,
582 /* unknown/unexpected xml node */
583 msg_Err( p_demux, "unexpected xml node %i", i_node );
587 msg_Err( p_demux, "unexpected end of xml data" );
596 * \brief handles the supported <track> sub-elements
598 static bool set_item_info SIMPLE_INTERFACE
600 /* exit if setting is impossible */
601 if( !psz_name || !psz_value || !p_input )
604 /* re-convert xml special characters inside psz_value */
605 resolve_xml_special_chars( psz_value );
607 /* handle each info element in a separate "if" clause */
608 if( !strcmp( psz_name, "title" ) )
610 input_item_SetTitle( p_input, psz_value );
612 else if( !strcmp( psz_name, "creator" ) )
614 input_item_SetArtist( p_input, psz_value );
616 else if( !strcmp( psz_name, "album" ) )
618 input_item_SetAlbum( p_input, psz_value );
621 else if( !strcmp( psz_name, "trackNum" ) )
623 input_item_SetTrackNum( p_input, psz_value );
625 else if( !strcmp( psz_name, "duration" ) )
627 long i_num = atol( psz_value );
628 input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
630 else if( !strcmp( psz_name, "annotation" ) )
632 input_item_SetDescription( p_input, psz_value );
634 else if( !strcmp( psz_name, "image" ) )
636 input_item_SetArtURL( p_input, psz_value );
642 * \brief handles the <vlc:option> elements
644 static bool set_option SIMPLE_INTERFACE
646 /* exit if setting is impossible */
647 if( !psz_name || !psz_value || !p_input )
650 /* re-convert xml special characters inside psz_value */
651 resolve_xml_special_chars( psz_value );
653 input_item_AddOption( p_input, psz_value, 0 );
659 * \brief parse the extension node of a XSPF playlist
661 static bool parse_extension_node COMPLEX_INTERFACE
663 input_item_t *p_input_item = p_input_node->p_item;
664 char *psz_name = NULL;
665 char *psz_value = NULL;
666 char *psz_title = NULL;
667 char *psz_application = NULL;
669 bool b_release_input_item = false;
670 xml_elem_hnd_t *p_handler = NULL;
671 input_item_t *p_new_input = NULL;
673 xml_elem_hnd_t pl_elements[] =
674 { {"vlc:node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
675 {"vlc:item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
676 {"vlc:id", SIMPLE_CONTENT, {NULL} },
677 {"vlc:option", SIMPLE_CONTENT, {.smpl = set_option} },
678 {NULL, UNKNOWN_CONTENT, {NULL} }
681 /* read all extension node attributes */
682 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
684 psz_name = xml_ReaderName( p_xml_reader );
685 psz_value = xml_ReaderValue( p_xml_reader );
686 if( !psz_name || !psz_value )
688 msg_Err( p_demux, "invalid xml stream @ <vlc:node>" );
692 /* attribute: title */
693 if( !strcmp( psz_name, "title" ) )
695 resolve_xml_special_chars( psz_value );
696 psz_title = psz_value;
698 /* extension attribute: application */
699 else if( !strcmp( psz_name, "application" ) )
701 psz_application = psz_value;
703 /* unknown attribute */
706 msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element,
714 /* attribute title is mandatory except for <extension> */
715 if( !strcmp( psz_element, "vlc:node" ) )
719 msg_Warn( p_demux, "<vlc:node> requires \"title\" attribute" );
722 p_new_input = input_item_NewWithType( VLC_OBJECT( p_demux ),
723 "vlc://nop", psz_title, 0, NULL, 0, -1,
724 ITEM_TYPE_DIRECTORY );
727 input_item_AddSubItem( p_input_item, p_new_input );
729 input_item_node_AppendItem( p_input_node, p_new_input );
730 p_input_item = p_new_input;
731 b_release_input_item = true;
735 else if( !strcmp( psz_element, "extension" ) )
737 if( !psz_application )
739 msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
742 else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
744 msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
745 free( psz_application );
749 free( psz_application );
752 /* parse the child elements */
753 while( xml_ReaderRead( p_xml_reader ) == 1 )
755 i_node = xml_ReaderNodeType( p_xml_reader );
758 case XML_READER_NONE:
760 case XML_READER_STARTELEM:
761 /* element start tag */
762 psz_name = xml_ReaderName( p_xml_reader );
763 if( !psz_name || !*psz_name )
765 msg_Err( p_demux, "invalid xml stream" );
767 if( b_release_input_item ) vlc_gc_decref( p_new_input );
771 for( p_handler = pl_elements;
772 p_handler->name && strcmp( psz_name, p_handler->name );
774 if( !p_handler->name )
776 msg_Err( p_demux, "unexpected element <%s>", psz_name );
778 if( b_release_input_item ) vlc_gc_decref( p_new_input );
782 /* complex content is parsed in a separate function */
783 if( p_handler->type == COMPLEX_CONTENT )
785 if( p_handler->pf_handler.cmplx( p_demux,
796 if( b_release_input_item ) vlc_gc_decref( p_new_input );
802 case XML_READER_TEXT:
803 /* simple element content */
805 psz_value = xml_ReaderValue( p_xml_reader );
808 msg_Err( p_demux, "invalid xml stream" );
810 if( b_release_input_item ) vlc_gc_decref( p_new_input );
815 case XML_READER_ENDELEM:
816 /* element end tag */
817 psz_name = xml_ReaderName( p_xml_reader );
820 msg_Err( p_demux, "invalid xml stream" );
822 if( b_release_input_item ) vlc_gc_decref( p_new_input );
825 /* leave if the current parent node is terminated */
826 if( !strcmp( psz_name, psz_element ) )
829 if( b_release_input_item ) vlc_gc_decref( p_new_input );
832 /* there MUST have been a start tag for that element name */
833 if( !p_handler || !p_handler->name
834 || strcmp( p_handler->name, psz_name ))
836 msg_Err( p_demux, "there's no open element left for <%s>",
839 if( b_release_input_item ) vlc_gc_decref( p_new_input );
843 /* special tag <vlc:id> */
844 if( !strcmp( p_handler->name, "vlc:id" ) )
846 p_demux->p_sys->i_track_id = atoi( psz_value );
848 else if( p_handler->pf_handler.smpl )
850 p_handler->pf_handler.smpl( p_input_item, p_handler->name,
858 /* unknown/unexpected xml node */
859 msg_Err( p_demux, "unexpected xml node %i", i_node );
861 if( b_release_input_item ) vlc_gc_decref( p_new_input );
866 if( b_release_input_item ) vlc_gc_decref( p_new_input );
871 * \brief parse the extension item node of a XSPF playlist
873 static bool parse_extitem_node COMPLEX_INTERFACE
875 VLC_UNUSED(psz_element);
876 input_item_t *p_new_input = NULL;
879 /* read all extension item attributes */
880 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
882 char *psz_name = xml_ReaderName( p_xml_reader );
883 char *psz_value = xml_ReaderValue( p_xml_reader );
884 if( !psz_name || !psz_value )
886 msg_Err( p_demux, "invalid xml stream @ <vlc:item>" );
891 /* attribute: href */
892 if( !strcmp( psz_name, "tid" ) )
894 i_tid = atoi( psz_value );
896 /* unknown attribute */
898 msg_Warn( p_demux, "invalid <vlc:item> attribute:\"%s\"", psz_name);
904 /* attribute href is mandatory */
907 msg_Warn( p_demux, "<vlc:item> requires \"tid\" attribute" );
911 if( i_tid >= p_demux->p_sys->i_tracklist_entries )
913 msg_Warn( p_demux, "invalid \"tid\" attribute" );
917 p_new_input = p_demux->p_sys->pp_tracklist[ i_tid ];
920 input_item_AddSubItem( p_input_node->p_item, p_new_input );
921 input_item_node_AppendItem( p_input_node, p_new_input );
922 vlc_gc_decref( p_new_input );
923 p_demux->p_sys->pp_tracklist[i_tid] = NULL;
926 /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
927 /* (libxml sends NONE) */
928 xml_ReaderRead( p_xml_reader );
934 * \brief skips complex element content that we can't manage
936 static bool skip_element COMPLEX_INTERFACE
938 VLC_UNUSED(p_demux); VLC_UNUSED(p_input_node);
940 while( xml_ReaderRead( p_xml_reader ) == 1 )
942 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
944 char *psz_endname = xml_ReaderName( p_xml_reader );
947 if( !strcmp( psz_element, psz_endname ) )