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/input.h>
35 #include "vlc_strings.h"
40 playlist_item_t *p_item_in_category;
42 input_item_t **pp_tracklist;
43 int i_tracklist_entries;
47 * \brief XSPF submodule initialization function
49 int E_(xspf_import_Activate)( vlc_object_t *p_this )
51 demux_t *p_demux = (demux_t *)p_this;
54 psz_ext = strrchr( p_demux->psz_path, '.' );
56 if( ( psz_ext && !strcasecmp( psz_ext, ".xspf") ) ||
57 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "xspf-open") ) )
65 msg_Dbg( p_demux, "using xspf playlist import");
67 p_demux->p_sys = (demux_sys_t*)malloc(sizeof(demux_sys_t ) );
69 p_demux->pf_control = xspf_import_Control;
70 p_demux->pf_demux = xspf_import_Demux;
76 * \brief demuxer function for XSPF parsing
78 int xspf_import_Demux( demux_t *p_demux )
80 int i_ret = VLC_SUCCESS;
82 xml_reader_t *p_xml_reader = NULL;
83 char *psz_name = NULL;
85 p_demux->p_sys->p_item_in_category = p_item_in_category;
86 p_demux->p_sys->i_parent_id = i_parent_id;
87 p_demux->p_sys->pp_tracklist = NULL;
88 p_demux->p_sys->i_tracklist_entries = 0;
90 /* create new xml parser from stream */
91 p_xml = xml_Create( p_demux );
96 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
101 /* locating the root node */
102 if( i_ret == VLC_SUCCESS )
106 if( xml_ReaderRead( p_xml_reader ) != 1 )
108 msg_Err( p_demux, "can't read xml stream" );
109 i_ret = VLC_EGENERIC;
111 } while( i_ret == VLC_SUCCESS &&
112 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
114 /* checking root node name */
115 if( i_ret == VLC_SUCCESS )
117 psz_name = xml_ReaderName( p_xml_reader );
118 if( !psz_name || strcmp( psz_name, "playlist" ) )
120 msg_Err( p_demux, "invalid root node name: %s", psz_name );
121 i_ret = VLC_EGENERIC;
126 i_ret = parse_playlist_node( p_demux, p_playlist, p_current, NULL,
127 p_xml_reader, "playlist" );
128 HANDLE_PLAY_AND_RELEASE;
130 xml_ReaderDelete( p_xml, p_xml_reader );
138 /** \brief dummy function for demux callback interface */
139 int xspf_import_Control( demux_t *p_demux, int i_query, va_list args )
145 * \brief parse the root node of a XSPF playlist
146 * \param p_demux demuxer instance
147 * \param p_playlist playlist instance
148 * \param p_item current playlist item
149 * \param p_input current input item
150 * \param p_xml_reader xml reader instance
151 * \param psz_element name of element to parse
153 static vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
156 char *psz_value=NULL;
157 vlc_bool_t b_version_found = VLC_FALSE;
159 xml_elem_hnd_t *p_handler=NULL;
161 xml_elem_hnd_t pl_elements[] =
162 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
163 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
164 {"annotation", SIMPLE_CONTENT, {NULL} },
165 {"info", SIMPLE_CONTENT, {NULL} },
166 {"location", SIMPLE_CONTENT, {NULL} },
167 {"identifier", SIMPLE_CONTENT, {NULL} },
168 {"image", SIMPLE_CONTENT, {NULL} },
169 {"date", SIMPLE_CONTENT, {NULL} },
170 {"license", SIMPLE_CONTENT, {NULL} },
171 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
172 {"link", SIMPLE_CONTENT, {NULL} },
173 {"meta", SIMPLE_CONTENT, {NULL} },
174 {"extension", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
175 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
176 {NULL, UNKNOWN_CONTENT, {NULL} }
179 /* read all playlist attributes */
180 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
182 psz_name = xml_ReaderName( p_xml_reader );
183 psz_value = xml_ReaderValue( p_xml_reader );
184 if( !psz_name || !psz_value )
186 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
190 /* attribute: version */
191 if( !strcmp( psz_name, "version" ) )
193 b_version_found = VLC_TRUE;
194 if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
195 msg_Warn( p_demux, "unsupported XSPF version" );
197 /* attribute: xmlns */
198 else if( !strcmp( psz_name, "xmlns" ) )
200 /* unknown attribute */
202 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
206 /* attribute version is mandatory !!! */
207 if( !b_version_found )
208 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
210 /* parse the child elements - we only take care of <trackList> */
211 while( xml_ReaderRead( p_xml_reader ) == 1 )
213 i_node = xml_ReaderNodeType( p_xml_reader );
216 case XML_READER_NONE:
218 case XML_READER_STARTELEM:
219 /* element start tag */
220 psz_name = xml_ReaderName( p_xml_reader );
221 if( !psz_name || !*psz_name )
223 msg_Err( p_demux, "invalid xml stream" );
228 for( p_handler = pl_elements;
229 p_handler->name && strcmp( psz_name, p_handler->name );
231 if( !p_handler->name )
233 msg_Err( p_demux, "unexpected element <%s>", psz_name );
238 /* complex content is parsed in a separate function */
239 if( p_handler->type == COMPLEX_CONTENT )
241 if( p_handler->pf_handler.cmplx( p_demux,
258 case XML_READER_TEXT:
259 /* simple element content */
261 psz_value = xml_ReaderValue( p_xml_reader );
264 msg_Err( p_demux, "invalid xml stream" );
270 case XML_READER_ENDELEM:
271 /* element end tag */
272 psz_name = xml_ReaderName( p_xml_reader );
275 msg_Err( p_demux, "invalid xml stream" );
279 /* leave if the current parent node <playlist> is terminated */
280 if( !strcmp( psz_name, psz_element ) )
285 /* there MUST have been a start tag for that element name */
286 if( !p_handler || !p_handler->name
287 || strcmp( p_handler->name, psz_name ))
289 msg_Err( p_demux, "there's no open element left for <%s>",
295 if( p_handler->pf_handler.smpl )
297 p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
305 /* unknown/unexpected xml node */
306 msg_Err( p_demux, "unexpected xml node %i", i_node );
316 * \brief parses the tracklist node which only may contain <track>s
318 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
324 /* now parse the <track>s */
325 while( xml_ReaderRead( p_xml_reader ) == 1 )
327 i_node = xml_ReaderNodeType( p_xml_reader );
328 if( i_node == XML_READER_STARTELEM )
330 psz_name = xml_ReaderName( p_xml_reader );
333 msg_Err( p_demux, "unexpected end of xml data" );
337 if( strcmp( psz_name, "track") )
339 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
346 /* parse the track data in a separate function */
347 if( parse_track_node( p_demux, p_playlist, p_item, NULL,
348 p_xml_reader,"track" ) == VLC_TRUE )
351 else if( i_node == XML_READER_ENDELEM )
355 /* the <trackList> has to be terminated */
356 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
358 msg_Err( p_demux, "there's a missing </trackList>" );
362 psz_name = xml_ReaderName( p_xml_reader );
363 if( !psz_name || strcmp( psz_name, "trackList" ) )
365 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
371 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
377 * \brief parse one track element
378 * \param COMPLEX_INTERFACE
380 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
382 input_item_t *p_new_input = NULL;
385 char *psz_value=NULL;
386 xml_elem_hnd_t *p_handler=NULL;
388 xml_elem_hnd_t track_elements[] =
389 { {"location", SIMPLE_CONTENT, {NULL} },
390 {"identifier", SIMPLE_CONTENT, {NULL} },
391 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
392 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
393 {"annotation", SIMPLE_CONTENT, {NULL} },
394 {"info", SIMPLE_CONTENT, {NULL} },
395 {"image", SIMPLE_CONTENT, {NULL} },
396 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
397 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
398 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
399 {"link", SIMPLE_CONTENT, {NULL} },
400 {"meta", SIMPLE_CONTENT, {NULL} },
401 {"extension", COMPLEX_CONTENT, {.cmplx = skip_element} },
402 {NULL, UNKNOWN_CONTENT, {NULL} }
405 while( xml_ReaderRead( p_xml_reader ) == 1 )
407 i_node = xml_ReaderNodeType( p_xml_reader );
410 case XML_READER_NONE:
413 case XML_READER_STARTELEM:
414 /* element start tag */
415 psz_name = xml_ReaderName( p_xml_reader );
416 if( !psz_name || !*psz_name )
418 msg_Err( p_demux, "invalid xml stream" );
423 for( p_handler = track_elements;
424 p_handler->name && strcmp( psz_name, p_handler->name );
426 if( !p_handler->name )
428 msg_Err( p_demux, "unexpected element <%s>", psz_name );
433 /* complex content is parsed in a separate function */
434 if( p_handler->type == COMPLEX_CONTENT )
439 "at <%s> level no new item has been allocated",
444 if( p_handler->pf_handler.cmplx( p_demux,
461 case XML_READER_TEXT:
462 /* simple element content */
464 psz_value = xml_ReaderValue( p_xml_reader );
467 msg_Err( p_demux, "invalid xml stream" );
473 case XML_READER_ENDELEM:
474 /* element end tag */
475 psz_name = xml_ReaderName( p_xml_reader );
478 msg_Err( p_demux, "invalid xml stream" );
482 /* leave if the current parent node <track> is terminated */
483 if( !strcmp( psz_name, psz_element ) )
487 /*playlist_AddWhereverNeeded( p_playlist, p_new_input,
488 p_item, p_demux->p_sys->p_item_in_category,
489 (p_demux->p_sys->i_parent_id >0 ) ? VLC_TRUE:
490 VLC_FALSE, PLAYLIST_APPEND );*/
491 INSERT_ELEM( p_demux->p_sys->pp_tracklist,
492 p_demux->p_sys->i_tracklist_entries,
493 p_demux->p_sys->i_tracklist_entries,
497 /* there MUST have been a start tag for that element name */
498 if( !p_handler || !p_handler->name
499 || strcmp( p_handler->name, psz_name ))
501 msg_Err( p_demux, "there's no open element left for <%s>",
507 /* special case: location */
508 if( !strcmp( p_handler->name, "location" ) )
511 /* there MUST NOT be an item */
514 msg_Err( p_demux, "item <%s> already created",
519 psz_uri = unescape_URI_duplicate( psz_value );
523 p_new_input = input_ItemNewExt( p_playlist, psz_uri,
525 p_new_input->p_meta = vlc_meta_New();
527 vlc_input_item_CopyOptions( p_item->p_input, p_new_input );
538 else if( !strcmp( p_handler->name, "identifier" ) )
543 /* there MUST be an item */
546 msg_Err( p_demux, "item not yet created at <%s>",
551 if( p_handler->pf_handler.smpl )
553 p_handler->pf_handler.smpl( NULL, p_input,
564 /* unknown/unexpected xml node */
565 msg_Err( p_demux, "unexpected xml node %i", i_node );
571 msg_Err( p_demux, "unexpected end of xml data" );
577 * \brief handles the supported <track> sub-elements
579 static vlc_bool_t set_item_info SIMPLE_INTERFACE
581 /* exit if setting is impossible */
582 if( !psz_name || !psz_value || !p_input )
585 /* re-convert xml special characters inside psz_value */
586 resolve_xml_special_chars( psz_value );
588 /* handle each info element in a separate "if" clause */
589 if( !strcmp( psz_name, "title" ) )
591 p_input->psz_name = strdup( (char*)psz_value );
593 else if( !strcmp( psz_name, "creator" ) )
595 vlc_meta_SetArtist( p_input->p_meta, psz_value );
597 else if( !strcmp( psz_name, "album" ) )
599 vlc_meta_SetAlbum( p_input->p_meta, psz_value );
602 else if( !strcmp( psz_name, "trackNum" ) )
604 vlc_meta_SetTracknum( p_input->p_meta, psz_value );
606 else if( !strcmp( psz_name, "duration" ) )
608 long i_num = atol( psz_value );
609 p_input->i_duration = i_num*1000;
616 * \brief parse the extension node of a XSPF playlist
618 static vlc_bool_t parse_extension_node COMPLEX_INTERFACE
620 char *psz_name = NULL;
621 char *psz_value = NULL;
623 xml_elem_hnd_t *p_handler = NULL;
625 xml_elem_hnd_t pl_elements[] =
626 { {"title", SIMPLE_CONTENT, {.cmplx = skip_element} },
627 {"node", COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
628 {"item", COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
629 {NULL, UNKNOWN_CONTENT, {NULL} }
632 fprintf( stderr, " <node>\n" );
634 /* parse the child elements */
635 while( xml_ReaderRead( p_xml_reader ) == 1 )
637 i_node = xml_ReaderNodeType( p_xml_reader );
640 case XML_READER_NONE:
642 case XML_READER_STARTELEM:
643 /* element start tag */
644 psz_name = xml_ReaderName( p_xml_reader );
645 if( !psz_name || !*psz_name )
647 msg_Err( p_demux, "invalid xml stream" );
652 for( p_handler = pl_elements;
653 p_handler->name && strcmp( psz_name, p_handler->name );
655 if( !p_handler->name )
657 msg_Err( p_demux, "unexpected element <%s>", psz_name );
662 /* complex content is parsed in a separate function */
663 if( p_handler->type == COMPLEX_CONTENT )
665 if( p_handler->pf_handler.cmplx( p_demux,
682 case XML_READER_TEXT:
683 /* simple element content */
685 psz_value = xml_ReaderValue( p_xml_reader );
688 msg_Err( p_demux, "invalid xml stream" );
694 case XML_READER_ENDELEM:
695 /* element end tag */
696 psz_name = xml_ReaderName( p_xml_reader );
699 msg_Err( p_demux, "invalid xml stream" );
703 /* leave if the current parent node is terminated */
704 if( !strcmp( psz_name, psz_element ) )
706 fprintf( stderr, " </node>\n" );
710 /* there MUST have been a start tag for that element name */
711 if( !p_handler || !p_handler->name
712 || strcmp( p_handler->name, psz_name ))
714 msg_Err( p_demux, "there's no open element left for <%s>",
720 if( p_handler->pf_handler.smpl )
722 p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
730 /* unknown/unexpected xml node */
731 msg_Err( p_demux, "unexpected xml node %i", i_node );
741 * \brief parse the extension item node of a XSPF playlist
743 static vlc_bool_t parse_extitem_node COMPLEX_INTERFACE
745 char *psz_name = NULL;
746 char *psz_value = NULL;
750 /* read all playlist attributes */
751 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
753 psz_name = xml_ReaderName( p_xml_reader );
754 psz_value = xml_ReaderValue( p_xml_reader );
755 if( !psz_name || !psz_value )
757 msg_Err( p_demux, "invalid xml stream @ <item>" );
761 /* attribute: href */
762 if( !strcmp( psz_name, "href" ) )
764 i_href = atoi( psz_value );
766 /* unknown attribute */
768 msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
773 /* attribute href is mandatory */
776 msg_Warn( p_demux, "<item> requires \"href\" attribute" );
780 if( i_href > p_demux->p_sys->i_tracklist_entries )
782 msg_Warn( p_demux, "invalid \"href\" attribute" );
786 fprintf( stderr, " %s\n", p_demux->p_sys->pp_tracklist[i_href]->psz_name );
788 /* parse the child elements */
789 while( xml_ReaderRead( p_xml_reader ) == 1 )
791 i_node = xml_ReaderNodeType( p_xml_reader );
794 case XML_READER_NONE:
796 case XML_READER_STARTELEM:
797 /* element start tag */
798 case XML_READER_TEXT:
799 /* simple element content */
800 msg_Err( p_demux, "invalid xml stream" );
803 case XML_READER_ENDELEM:
804 /* element end tag */
805 psz_name = xml_ReaderName( p_xml_reader );
808 msg_Err( p_demux, "invalid xml stream" );
812 /* leave if the current parent node is terminated */
813 if( !strcmp( psz_name, psz_element ) )
822 /* unknown/unexpected xml node */
823 msg_Err( p_demux, "unexpected xml node %i", i_node );
833 * \brief skips complex element content that we can't manage
835 static vlc_bool_t skip_element COMPLEX_INTERFACE
839 while( xml_ReaderRead( p_xml_reader ) == 1 )
841 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
843 psz_endname = xml_ReaderName( p_xml_reader );
846 if( !strcmp( psz_element, psz_endname ) )