]> git.sesse.net Git - vlc/blob - modules/demux/playlist/xspf.c
34ae342a83379538e6e12e9ef5e091aa0a066a04
[vlc] / modules / demux / playlist / xspf.c
1 /******************************************************************************
2  * Copyright (C) 2006 Daniel Stränger <vlc at schmaller dot de>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
17  *******************************************************************************/
18 /**
19  * \file modules/demux/playlist/xspf.c
20  * \brief XSPF playlist import functions
21  */
22
23 #include <vlc/vlc.h>
24 #include <vlc/input.h>
25 #include <vlc/intf.h>
26
27 #include "playlist.h"
28 #include "vlc_xml.h"
29 #include "vlc_strings.h"
30 #include "xspf.h"
31
32 /**
33  * \brief XSPF submodule initialization function
34  */
35 int E_(xspf_import_Activate)( vlc_object_t *p_this )
36 {
37     demux_t *p_demux = (demux_t *)p_this;
38     char    *psz_ext;
39
40     psz_ext = strrchr ( p_demux->psz_path, '.' );
41
42     if( ( psz_ext && !strcasecmp( psz_ext, ".xspf") ) ||
43         ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "xspf-open") ) )
44     {
45         ;
46     }
47     else
48     {
49         return VLC_EGENERIC;
50     }
51     msg_Dbg( p_demux, "using xspf playlist import");
52
53     p_demux->pf_control = xspf_import_Control;
54     p_demux->pf_demux = xspf_import_Demux;
55
56     return VLC_SUCCESS;
57 }
58
59 /**
60  * \brief demuxer function for XSPF parsing
61  */
62 int xspf_import_Demux( demux_t *p_demux )
63 {
64     playlist_t *p_playlist = NULL;
65     playlist_item_t *p_current = NULL;
66
67     vlc_bool_t b_play;
68     int i_ret = VLC_SUCCESS;
69
70     xml_t *p_xml = NULL;
71     xml_reader_t *p_xml_reader = NULL;
72     char *psz_name = NULL;
73
74     /* create new xml parser from stream */
75     p_xml = xml_Create( p_demux );
76     if( !p_xml )
77         i_ret = VLC_ENOMOD;
78     else
79     {
80         p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
81         if( !p_xml_reader )
82             i_ret = VLC_EGENERIC;
83     }
84
85     /* start with parsing the root node */
86     if ( i_ret == VLC_SUCCESS )
87         if ( xml_ReaderRead( p_xml_reader ) != 1 )
88         {
89             msg_Err( p_demux, "can't read xml stream" );
90             i_ret = VLC_EGENERIC;
91         }
92     /* checking root nody type */
93     if ( i_ret == VLC_SUCCESS )
94         if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM )
95         {
96             msg_Err( p_demux, "invalid root node type: %i", xml_ReaderNodeType( p_xml_reader ) );
97             i_ret = VLC_EGENERIC;
98         }
99     /* checking root node name */
100     if ( i_ret == VLC_SUCCESS )
101         psz_name = xml_ReaderName( p_xml_reader );
102     if ( !psz_name || strcmp( psz_name, "playlist" ) )
103     {
104         msg_Err( p_demux, "invalid root node name: %s", psz_name );
105         i_ret = VLC_EGENERIC;
106     }
107     FREE_NAME();
108
109     /* get the playlist ... */
110     if ( i_ret == VLC_SUCCESS )
111     {
112         p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST, FIND_PARENT );
113         if( !p_playlist )
114         {
115             msg_Err( p_demux, "can't find playlist" );
116             i_ret = VLC_ENOOBJ;
117         }
118     }
119     /* ... and its current item (to convert it to a node) */
120     if ( i_ret == VLC_SUCCESS )
121     {
122         b_play = E_(FindItem)( p_demux, p_playlist, &p_current );
123         playlist_ItemToNode( p_playlist, p_current );
124         p_current->input.i_type = ITEM_TYPE_PLAYLIST;
125         /* parse the playlist node */
126         i_ret = parse_playlist_node( p_demux, p_playlist, p_current,
127                                      p_xml_reader, "playlist" );
128         /* true/false - success/egeneric mapping */
129         i_ret = ( i_ret==VLC_TRUE ? VLC_SUCCESS : VLC_EGENERIC );
130
131         if( b_play )
132         {
133             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
134                               p_playlist->status.i_view,
135                               p_playlist->status.p_item, NULL );
136         }
137     }
138
139     if ( p_playlist )
140         vlc_object_release( p_playlist );
141     if ( p_xml_reader )
142         xml_ReaderDelete( p_xml, p_xml_reader );
143     if ( p_xml )
144         xml_Delete( p_xml );
145
146     return i_ret;
147 }
148
149 /** \brief dummy function for demux callback interface */
150 int xspf_import_Control( demux_t *p_demux, int i_query, va_list args )
151 {
152     return VLC_EGENERIC;
153 }
154
155 /**
156  * \brief parse the root node of a XSPF playlist
157  * \param p_demux demuxer instance
158  * \param p_playlist playlist instance
159  * \param p_item current playlist node
160  * \param p_xml_reader xml reader instance
161  * \param psz_element name of element to parse
162  */
163 static vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
164 {
165     char *psz_name=NULL;
166     char *psz_value=NULL;
167     vlc_bool_t b_version_found = VLC_FALSE;
168     int i_node;
169     xml_elem_hnd_t *p_handler=NULL;
170
171     xml_elem_hnd_t pl_elements[] =
172         { {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
173           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
174           {"annotation",   SIMPLE_CONTENT,  {NULL} },
175           {"info",         SIMPLE_CONTENT,  {NULL} },
176           {"location",     SIMPLE_CONTENT,  {NULL} },
177           {"identifier",   SIMPLE_CONTENT,  {NULL} },
178           {"image",        SIMPLE_CONTENT,  {NULL} },
179           {"date",         SIMPLE_CONTENT,  {NULL} },
180           {"license",      SIMPLE_CONTENT,  {NULL} },
181           {"attribution",  COMPLEX_CONTENT, {.cmplx = skip_element} },
182           {"link",         SIMPLE_CONTENT,  {NULL} },
183           {"meta",         SIMPLE_CONTENT,  {NULL} },
184           {"extension",    COMPLEX_CONTENT, {.cmplx = skip_element} },
185           {"trackList",    COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
186           {NULL,           UNKNOWN_CONTENT, {NULL} }
187         };
188
189     /* read all playlist attributes */
190     while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
191     {
192         psz_name = xml_ReaderName ( p_xml_reader );
193         psz_value = xml_ReaderValue ( p_xml_reader );
194         if ( !psz_name || !psz_value )
195         {
196             msg_Err( p_demux, "invalid xml stream @ <playlist>" );
197             FREE_ATT();
198             return VLC_FALSE;
199         }
200         /* attribute: version */
201         if ( !strcmp( psz_name, "version" ) )
202         {
203             b_version_found = VLC_TRUE;
204             if ( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
205                 msg_Warn( p_demux, "unsupported XSPF version" );
206         }
207         /* attribute: xmlns */
208         else if ( !strcmp ( psz_name, "xmlns" ) )
209             ;
210         /* unknown attribute */
211         else
212             msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
213
214         FREE_ATT();
215     }
216     /* attribute version is mandatory !!! */
217     if ( !b_version_found )
218         msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
219
220     /* parse the child elements - we only take care of <trackList> */
221     while ( xml_ReaderRead( p_xml_reader ) == 1 )
222     {
223         i_node = xml_ReaderNodeType( p_xml_reader );
224         switch ( i_node )
225         {
226             case XML_READER_NONE:
227                 break;
228             case XML_READER_STARTELEM:
229                 /*  element start tag  */
230                 psz_name = xml_ReaderName( p_xml_reader );
231                 if ( !psz_name || !*psz_name )
232                 {
233                     msg_Err( p_demux, "invalid xml stream" );
234                     FREE_ATT();
235                     return VLC_FALSE;
236                 }
237                 /* choose handler */
238                 for( p_handler = pl_elements;
239                      p_handler->name && strcmp( psz_name, p_handler->name );
240                      p_handler++ );
241                 if ( !p_handler->name )
242                 {
243                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
244                     FREE_ATT();
245                     return VLC_FALSE;
246                 }
247                 FREE_NAME();
248                 /* complex content is parsed in a separate function */
249                 if ( p_handler->type == COMPLEX_CONTENT )
250                 {
251                     if ( p_handler->pf_handler.cmplx( p_demux,
252                                                       p_playlist,
253                                                       p_item,
254                                                       p_xml_reader,
255                                                       p_handler->name ) )
256                     {
257                         p_handler = NULL;
258                         FREE_ATT();
259                     }
260                     else
261                     {
262                         FREE_ATT();
263                         return VLC_FALSE;
264                     }
265                 }
266                 break;
267
268             case XML_READER_TEXT:
269                 /* simple element content */
270                 FREE_ATT();
271                 psz_value = xml_ReaderValue( p_xml_reader );
272                 if ( !psz_value )
273                 {
274                     msg_Err( p_demux, "invalid xml stream" );
275                     FREE_ATT();
276                     return VLC_FALSE;
277                 }
278                 break;
279
280             case XML_READER_ENDELEM:
281                 /* element end tag */
282                 psz_name = xml_ReaderName( p_xml_reader );
283                 if ( !psz_name )
284                 {
285                     msg_Err( p_demux, "invalid xml stream" );
286                     FREE_ATT();
287                     return VLC_FALSE;
288                 }
289                 /* leave if the current parent node <playlist> is terminated */
290                 if ( !strcmp( psz_name, psz_element ) )
291                 {
292                     FREE_ATT();
293                     return VLC_TRUE;
294                 }
295                 /* there MUST have been a start tag for that element name */
296                 if ( !p_handler || !p_handler->name
297                      || strcmp( p_handler->name, psz_name ))
298                 {
299                     msg_Err( p_demux, "there's no open element left for <%s>",
300                              psz_name );
301                     FREE_ATT();
302                     return VLC_FALSE;
303                 }
304
305                 if ( p_handler->pf_handler.smpl )
306                 {
307                     p_handler->pf_handler.smpl( p_item, p_handler->name,
308                                                 psz_value );
309                 }
310                 FREE_ATT();
311                 p_handler = NULL;
312                 break;
313
314             default:
315                 /* unknown/unexpected xml node */
316                 msg_Err( p_demux, "unexpected xml node %i", i_node );
317                 FREE_ATT();
318                 return VLC_FALSE;
319         }
320         FREE_NAME();
321     }
322     return VLC_FALSE;
323 }
324
325 /**
326  * \brief parses the tracklist node which only may contain <track>s
327  */
328 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
329 {
330     char *psz_name=NULL;
331     int i_node;
332     int i_ntracks = 0;
333
334     /* now parse the <track>s */
335     while ( xml_ReaderRead( p_xml_reader ) == 1 )
336     {
337         i_node = xml_ReaderNodeType( p_xml_reader );
338         if ( i_node == XML_READER_STARTELEM )
339         {
340             psz_name = xml_ReaderName( p_xml_reader );
341             if ( !psz_name )
342             {
343                 msg_Err( p_demux, "unexpected end of xml data" );
344                 FREE_NAME();
345                 return VLC_FALSE;
346             }
347             if ( strcmp( psz_name, "track") )
348             {
349                 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
350                          psz_name );
351                 FREE_NAME();
352                 return VLC_FALSE;
353             }
354             FREE_NAME();
355
356             /* parse the track data in a separate function */
357             if ( parse_track_node( p_demux, p_playlist, p_item, p_xml_reader,
358                                    "track" ) == VLC_TRUE )
359                 i_ntracks++;
360         }
361         else if ( i_node == XML_READER_ENDELEM )
362             break;
363     }
364
365     /* the <trackList> has to be terminated */
366     if ( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
367     {
368         msg_Err( p_demux, "there's a missing </trackList>" );
369         FREE_NAME();
370         return VLC_FALSE;
371     }
372     psz_name = xml_ReaderName( p_xml_reader );
373     if ( !psz_name || strcmp( psz_name, "trackList" ) )
374     {
375         msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
376         FREE_NAME();
377         return VLC_FALSE;
378     }
379     FREE_NAME();
380
381     msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
382
383     return VLC_TRUE;
384 }
385
386 /**
387  * \brief parse one track element
388  * \param COMPLEX_INTERFACE
389  */
390 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
391 {
392     playlist_item_t *p_new=NULL;
393     int i_node;
394     char *psz_name=NULL;
395     char *psz_value=NULL;
396     xml_elem_hnd_t *p_handler=NULL;
397
398     xml_elem_hnd_t track_elements[] =
399         { {"location",     SIMPLE_CONTENT,  {NULL} },
400           {"identifier",   SIMPLE_CONTENT,  {NULL} },
401           {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
402           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
403           {"annotation",   SIMPLE_CONTENT,  {NULL} },
404           {"info",         SIMPLE_CONTENT,  {NULL} },
405           {"image",        SIMPLE_CONTENT,  {NULL} },
406           {"album",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
407           {"trackNum",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
408           {"duration",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
409           {"link",         SIMPLE_CONTENT,  {NULL} },
410           {"meta",         SIMPLE_CONTENT,  {NULL} },
411           {"extension",    COMPLEX_CONTENT, {.cmplx = skip_element} },
412           {NULL,           UNKNOWN_CONTENT, {NULL} }
413         };
414
415     while ( xml_ReaderRead( p_xml_reader ) == 1 )
416     {
417         i_node = xml_ReaderNodeType( p_xml_reader );
418         switch ( i_node )
419         {
420             case XML_READER_NONE:
421                 break;
422
423             case XML_READER_STARTELEM:
424                 /*  element start tag  */
425                 psz_name = xml_ReaderName( p_xml_reader );
426                 if ( !psz_name || !*psz_name )
427                 {
428                     msg_Err( p_demux, "invalid xml stream" );
429                     FREE_ATT();
430                     return VLC_FALSE;
431                 }
432                 /* choose handler */
433                 for( p_handler = track_elements;
434                      p_handler->name && strcmp( psz_name, p_handler->name );
435                      p_handler++ );
436                 if ( !p_handler->name )
437                 {
438                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
439                     FREE_ATT();
440                     return VLC_FALSE;
441                 }
442                 FREE_NAME();
443                 /* complex content is parsed in a separate function */
444                 if ( p_handler->type == COMPLEX_CONTENT )
445                 {
446                     if ( !p_new )
447                     {
448                         msg_Err( p_demux,
449                                  "at <%s> level no new item has been allocated",
450                                  p_handler->name );
451                         FREE_ATT();
452                         return VLC_FALSE;
453                     }
454                     if ( p_handler->pf_handler.cmplx( p_demux,
455                                                       p_playlist,
456                                                       p_new,
457                                                       p_xml_reader,
458                                                       p_handler->name ) )
459                     {
460                         p_handler = NULL;
461                         FREE_ATT();
462                     }
463                     else
464                     {
465                         FREE_ATT();
466                         return VLC_FALSE;
467                     }
468                 }
469                 break;
470
471             case XML_READER_TEXT:
472                 /* simple element content */
473                 FREE_ATT();
474                 psz_value = xml_ReaderValue( p_xml_reader );
475                 if ( !psz_value )
476                 {
477                     msg_Err( p_demux, "invalid xml stream" );
478                     FREE_ATT();
479                     return VLC_FALSE;
480                 }
481                 break;
482
483             case XML_READER_ENDELEM:
484                 /* element end tag */
485                 psz_name = xml_ReaderName( p_xml_reader );
486                 if ( !psz_name )
487                 {
488                     msg_Err( p_demux, "invalid xml stream" );
489                     FREE_ATT();
490                     return VLC_FALSE;
491                 }
492                 /* leave if the current parent node <track> is terminated */
493                 if ( !strcmp( psz_name, psz_element ) )
494                 {
495                     FREE_ATT();
496                     return VLC_TRUE;
497                 }
498                 /* there MUST have been a start tag for that element name */
499                 if ( !p_handler || !p_handler->name
500                      || strcmp( p_handler->name, psz_name ))
501                 {
502                     msg_Err( p_demux, "there's no open element left for <%s>",
503                              psz_name );
504                     FREE_ATT();
505                     return VLC_FALSE;
506                 }
507
508                 /* special case: location */
509                 if ( !strcmp( p_handler->name, "location" ) )
510                 {
511                     /* there MUST NOT be an item */
512                     if ( p_new )
513                     {
514                         msg_Err( p_demux,
515                                  "a new item has just been created <%s>",
516                                  psz_name );
517                         FREE_ATT();
518                         return VLC_FALSE;
519                     }
520                     /* create it now */
521                     if ( insert_new_item( p_playlist, p_item,
522                                           &p_new, psz_value ) )
523                     {
524                         FREE_ATT();
525                         p_handler = NULL;
526                     }
527                     else
528                     {
529                         FREE_ATT();
530                         return VLC_FALSE;
531                     }
532                 }
533                 else
534                 {
535                     /* there MUST be an item */
536                     if ( !p_new )
537                     {
538                         msg_Err( p_demux,
539                                  "an item hasn't been created yet <%s>",
540                                  psz_name );
541                         FREE_ATT();
542                         return VLC_FALSE;
543                     }
544                     if ( p_handler->pf_handler.smpl )
545                     {
546                         p_handler->pf_handler.smpl( p_new, p_handler->name,
547                                                     psz_value );
548                         FREE_ATT();
549                     }
550                 }
551                 FREE_ATT();
552                 p_handler = NULL;
553                 break;
554
555             default:
556                 /* unknown/unexpected xml node */
557                 msg_Err( p_demux, "unexpected xml node %i", i_node );
558                 FREE_ATT();
559                 return VLC_FALSE;
560         }
561         FREE_NAME();
562     }
563     msg_Err( p_demux, "unexpected end of xml data" );
564     FREE_ATT();
565     return VLC_FALSE;
566 }
567
568 /**
569  * \brief handles the supported <track> sub-elements
570  */
571 static vlc_bool_t set_item_info SIMPLE_INTERFACE
572 {
573     /* exit if setting is impossible */
574     if ( !psz_name || !psz_value || !p_item )
575         return VLC_FALSE;
576
577     /* re-convert xml special characters inside psz_value */
578     resolve_xml_special_chars ( psz_value );
579
580     /* handle each info element in a separate "if" clause */
581     if ( !strcmp( psz_name, "title" ) )
582     {
583         if ( playlist_ItemSetName ( p_item, (char *)psz_value ) == VLC_SUCCESS )
584             return VLC_TRUE;
585         return VLC_FALSE;
586     }
587     else if ( !strcmp( psz_name, "creator" ) )
588     {
589         if ( vlc_input_item_AddInfo( &(p_item->input),
590                                      _(VLC_META_INFO_CAT), _(VLC_META_ARTIST),
591                                      "%s", psz_value ) == VLC_SUCCESS )
592             return VLC_TRUE;
593         return VLC_FALSE;
594
595     }
596     else if ( !strcmp( psz_name, "album" ) )
597     {
598         if ( vlc_input_item_AddInfo( &(p_item->input),
599                                      _(VLC_META_INFO_CAT),
600                                      _(VLC_META_COLLECTION),
601                                      "%s", psz_value ) == VLC_SUCCESS )
602             return VLC_TRUE;
603         return VLC_FALSE;
604
605     } else if ( !strcmp( psz_name, "trackNum" ) )
606     {
607         long i_num = atol( psz_value );
608         if ( i_num > 0
609              && vlc_input_item_AddInfo( &(p_item->input),
610                                          _(VLC_META_INFO_CAT),
611                                          _(VLC_META_SEQ_NUM),
612                                          "%s", psz_value ) == VLC_SUCCESS )
613                 return VLC_TRUE;
614         return VLC_FALSE;
615
616     } else if ( !strcmp( psz_name, "duration" ) )
617     {
618         long i_num = atol( psz_value );
619         if ( i_num > 0
620              && playlist_ItemSetDuration( p_item, i_num*1000 ) == VLC_SUCCESS )
621                 return VLC_TRUE;
622         return VLC_FALSE;
623
624     }
625
626     return VLC_TRUE;
627 }
628
629 /**
630  * \brief skips complex element content that we can't manage
631  */
632 static vlc_bool_t skip_element COMPLEX_INTERFACE
633 {
634     char *psz_endname;
635
636     while ( xml_ReaderRead( p_xml_reader ) == 1 )
637     {
638         if ( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
639         {
640             psz_endname = xml_ReaderName( p_xml_reader );
641             if ( !psz_endname )
642                 return VLC_FALSE;
643             if ( !strcmp( psz_element, psz_endname ) )
644             {
645                 free( psz_endname );
646                 return VLC_TRUE;
647             }
648             else
649                 free( psz_endname );
650         }
651     }
652     return VLC_FALSE;
653 }
654
655 /**
656  * \brief creates a new playlist item from the given mrl
657  */
658 static vlc_bool_t insert_new_item( playlist_t *p_pl, playlist_item_t *p_cur,
659                                    playlist_item_t **pp_new, char *psz_location )
660 {
661     char *psz_uri=NULL;
662     psz_uri = unescape_URI_duplicate( psz_location );
663
664     if ( psz_uri )
665     {
666         *pp_new = playlist_ItemNew( p_pl, psz_uri, NULL );
667         free( psz_uri );
668         psz_uri = NULL;
669     }
670
671     if ( !*pp_new )
672         return VLC_FALSE;
673
674     playlist_NodeAddItem( p_pl,  *pp_new,         p_cur->pp_parents[0]->i_view,
675                           p_cur, PLAYLIST_APPEND, PLAYLIST_END );
676
677     playlist_CopyParents( p_cur, *pp_new );
678
679     vlc_input_item_CopyOptions( &p_cur->input, &((*pp_new)->input) );
680
681     return VLC_TRUE;
682 }