]> git.sesse.net Git - vlc/blob - modules/demux/playlist/shoutcast.c
Resolving XML special chars on xml based format
[vlc] / modules / demux / playlist / shoutcast.c
1 /*****************************************************************************
2  * shoutcast.c: Winamp >=5.2 shoutcast demuxer
3  *****************************************************************************
4  * Copyright (C) 2006 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -@t- videolan -Dot- org>
8  *          based on b4s.c by Sigmund Augdal Helberg <dnumgis@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
35
36 #include "playlist.h"
37 #include <vlc_xml.h>
38 #include <vlc_strings.h>
39
40 /* duplicate from modules/services_discovery/shout.c */
41 #define SHOUTCAST_BASE_URL "http/shout-winamp://www.shoutcast.com/sbin/newxml.phtml"
42 #define SHOUTCAST_TUNEIN_BASE_URL "http://www.shoutcast.com"
43 #define SHOUTCAST_TV_TUNEIN_URL "http://www.shoutcast.com/sbin/tunein-tvstation.pls?id="
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 static int Demux( demux_t *p_demux);
49
50 static int DemuxGenre( demux_t *p_demux, xml_reader_t *p_xml_reader,
51                        input_item_node_t *p_input_node );
52 static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader,
53                          input_item_node_t *p_input_node, bool b_adult );
54
55 /*****************************************************************************
56  * Import_Shoutcast: main import function
57  *****************************************************************************/
58 int Import_Shoutcast( vlc_object_t *p_this )
59 {
60     demux_t *p_demux = (demux_t *)p_this;
61
62     if( !demux_IsForced( p_demux, "shout-winamp" ) )
63         return VLC_EGENERIC;
64
65     p_demux->pf_demux = Demux;
66     p_demux->pf_control = Control;
67     msg_Dbg( p_demux, "using shoutcast playlist reader" );
68
69     return VLC_SUCCESS;
70 }
71
72 static int Demux( demux_t *p_demux )
73 {
74     xml_reader_t *p_xml_reader = NULL;
75     const char *node;
76     int i_ret = -1;
77     input_item_t *p_current_input = GetCurrentItem(p_demux);
78     input_item_node_t *p_input_node = NULL;
79
80     p_xml_reader = xml_ReaderCreate( p_demux, p_demux->s );
81     if( !p_xml_reader )
82         goto error;
83
84     /* check root node */
85     if( xml_ReaderNextNode( p_xml_reader, &node ) != XML_READER_STARTELEM )
86     {
87         msg_Err( p_demux, "invalid file (no root node)" );
88         goto error;
89     }
90
91     if( strcmp( node, "genrelist" ) && strcmp( node, "stationlist" ) )
92     {
93         msg_Err( p_demux, "invalid root node <%s>", node );
94         goto error;
95     }
96
97     p_input_node = input_item_node_Create( p_current_input );
98
99     if( !strcmp( node, "genrelist" ) )
100     {
101         /* we're reading a genre list */
102         if( DemuxGenre( p_demux, p_xml_reader, p_input_node ) )
103             goto error;
104     }
105     else
106     {
107         /* we're reading a station list */
108         if( DemuxStation( p_demux, p_xml_reader, p_input_node,
109                 var_InheritBool( p_demux, "shoutcast-show-adult" ) ) )
110             goto error;
111     }
112
113     input_item_node_PostAndDelete( p_input_node );
114     p_input_node = NULL;
115
116     i_ret = 0; /* Needed for correct operation of go back */
117
118 error:
119     if( p_xml_reader )
120         xml_ReaderDelete( p_xml_reader );
121     if( p_input_node ) input_item_node_Delete( p_input_node );
122     vlc_gc_decref(p_current_input);
123     return i_ret;
124 }
125
126 /* <genrelist>
127  *   <genre name="the name"></genre>
128  *   ...
129  * </genrelist>
130  **/
131 static int DemuxGenre( demux_t *p_demux, xml_reader_t *p_xml_reader,
132                        input_item_node_t *p_input_node )
133 {
134     const char *node;
135     char *psz_name = NULL; /* genre name */
136     int type;
137
138     while( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
139     {
140         switch( type )
141         {
142             case XML_READER_STARTELEM:
143             {
144                 if( !strcmp( node, "genre" ) )
145                 {
146                     // Read the attributes
147                     const char *name, *value;
148                     while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) )
149                     {
150                         if( !strcmp( name, "name" ) )
151                         {
152                             free(psz_name);
153                             psz_name = strdup( value );
154                         }
155                         else
156                             msg_Warn( p_demux,
157                                       "unexpected attribute %s in <%s>",
158                                       name, node );
159                     }
160                 }
161                 break;
162             }
163
164             case XML_READER_ENDELEM:
165                 if( !strcmp( node, "genre" ) && psz_name != NULL )
166                 {
167                     char* psz_mrl;
168
169                     if( asprintf( &psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
170                                   psz_name ) != -1 )
171                     {
172                         input_item_t *p_input;
173                         resolve_xml_special_chars( psz_mrl );
174                         p_input = input_item_New( psz_mrl, psz_name );
175                         input_item_CopyOptions( p_input_node->p_item, p_input );
176                         free( psz_mrl );
177                         input_item_node_AppendItem( p_input_node, p_input );
178                         vlc_gc_decref( p_input );
179                     }
180                     FREENULL( psz_name );
181                 }
182                 break;
183         }
184     }
185
186     free( psz_name );
187     return 0;
188 }
189
190 /* radio stations:
191  * <stationlist>
192  *   <tunein base="/sbin/tunein-station.pls"></tunein>
193  *   <station name="the name"
194  *            mt="mime type"
195  *            id="the id"
196  *            br="bit rate"
197  *            genre="A big genre string"
198  *            ct="current track name/author/..."
199  *            lc="listener count"></station>
200  * </stationlist>
201  *
202  * TV stations:
203  * <stationlist>
204  *   <tunein base="/sbin/tunein-station.pls"></tunein>
205  *   <station name="the name"
206  *            id="the id"
207  *            br="bit rate"
208  *            rt="rating"
209  *            load="server load ?"
210  *            ct="current track name/author/..."
211  *            genre="A big genre string"
212  *            lc="listener count"></station>
213  * </stationlist>
214  **/
215 static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader,
216                          input_item_node_t *p_input_node, bool b_adult )
217 {
218     char *psz_base = NULL; /* */
219
220     char *psz_name = NULL; /* genre name */
221     char *psz_mt = NULL; /* mime type */
222     char *psz_id = NULL; /* id */
223     char *psz_br = NULL; /* bit rate */
224     char *psz_genre = NULL; /* genre */
225     char *psz_ct = NULL; /* current track */
226     char *psz_lc = NULL; /* listener count */
227
228     /* If these are set then it's *not* a radio but a TV */
229     char *psz_rt = NULL; /* rating for shoutcast TV */
230     char *psz_load = NULL; /* load for shoutcast TV */
231
232     const char *node; /* tag name */
233     int i_type;
234
235     while( (i_type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
236     {
237         switch( i_type )
238         {
239             case XML_READER_STARTELEM:
240                 // Read the attributes
241                 if( !strcmp( node, "tunein" ) )
242                 {
243                     const char *name, *value;
244                     while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) )
245                     {
246                         if( !strcmp( name, "base" ) )
247                         {
248                             free( psz_base );
249                             psz_base = strdup( value );
250                         }
251                         else
252                             msg_Warn( p_demux,
253                                       "unexpected attribute %s in <%s>",
254                                       name, node );
255                     }
256                 }
257                 else if( !strcmp( node, "station" ) )
258                 {
259                     const char *name, *value;
260                     while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) )
261                     {
262                         char **p = NULL;
263                         if( !strcmp( name, "name" ) )
264                             p = &psz_name;
265                         else if ( !strcmp( name, "mt" ) )
266                             p = &psz_mt;
267                         else if ( !strcmp( name, "id" ) )
268                             p = &psz_id;
269                         else if ( !strcmp( name, "br" ) )
270                             p = &psz_br;
271                         else if ( !strcmp( name, "genre" ) )
272                             p = &psz_genre;
273                         else if ( !strcmp( name, "ct" ) )
274                             p = &psz_ct;
275                         else if ( !strcmp( name, "lc" ) )
276                             p = &psz_lc;
277                         else if ( !strcmp( name, "rt" ) )
278                             p = &psz_rt;
279                         else if ( !strcmp( name, "load" ) )
280                             p = &psz_load;
281                         if( p != NULL )
282                         {
283                             free( *p );
284                             *p = strdup( value );
285                         }
286                         else
287                             msg_Warn( p_demux,
288                                       "unexpected attribute %s in <%s>",
289                                       name, node );
290                     }
291                 }
292                 break;
293
294             // End element
295             case XML_READER_ENDELEM:
296                 if( !strcmp( node, "station" ) &&
297                     ( psz_base || ( psz_rt && psz_load &&
298                     ( b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
299                 {
300                     char *psz_mrl = NULL;
301                     if( psz_rt || psz_load )
302                     {
303                         /* tv */
304                         if( asprintf( &psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
305                                  psz_id ) == -1)
306                             psz_mrl = NULL;
307                     }
308                     else
309                     {
310                         /* radio */
311                         if( asprintf( &psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
312                              psz_base, psz_id ) == -1 )
313                             psz_mrl = NULL;
314                     }
315
316                     /* Create the item */
317                     input_item_t *p_input;
318                     resolve_xml_special_chars( psz_mrl );
319                     p_input = input_item_New( psz_mrl, psz_name );
320                     input_item_CopyOptions( p_input_node->p_item, p_input );
321                     free( psz_mrl );
322
323 #define SADD_INFO( type, field ) \
324                     if( field ) \
325                         input_item_AddInfo( p_input, _("Shoutcast"), \
326                                             vlc_gettext(type), "%s", field )
327                     SADD_INFO( N_("Mime"), psz_mt );
328                     SADD_INFO( N_("Bitrate"), psz_br );
329                     SADD_INFO( N_("Listeners"), psz_lc );
330                     SADD_INFO( N_("Load"), psz_load );
331                     if( psz_genre )
332                         input_item_SetGenre( p_input, psz_genre );
333                     if( psz_ct )
334                         input_item_SetNowPlaying( p_input, psz_ct );
335                     if( psz_rt )
336                         input_item_SetRating( p_input, psz_rt );
337                     input_item_node_AppendItem( p_input_node, p_input );
338                     vlc_gc_decref( p_input );
339                     FREENULL( psz_base );
340                     FREENULL( psz_name );
341                     FREENULL( psz_mt );
342                     FREENULL( psz_id );
343                     FREENULL( psz_br );
344                     FREENULL( psz_genre );
345                     FREENULL( psz_ct );
346                     FREENULL( psz_lc );
347                     FREENULL( psz_rt );
348                     FREENULL( psz_load );
349                 }
350                 break;
351         }
352     }
353     /* FIXME: leaks on missing ENDELEMENT? */
354     return 0;
355 }