]> git.sesse.net Git - vlc/blob - modules/demux/playlist/shoutcast.c
asx.c: check for </MoreInfo> tag also
[vlc] / modules / demux / playlist / shoutcast.c
1 /*****************************************************************************
2  * shoutcast.c: Winamp >=5.2 shoutcast demuxer
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
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
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.
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 General Public License for more details.
19  *
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  *****************************************************************************/
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
39 /* duplicate from modules/services_discovery/shout.c */
40 #define SHOUTCAST_BASE_URL "http/shout-winamp://www.shoutcast.com/sbin/newxml.phtml"
41 #define SHOUTCAST_TUNEIN_BASE_URL "http://www.shoutcast.com"
42 #define SHOUTCAST_TV_TUNEIN_URL "http://www.shoutcast.com/sbin/tunein-tvstation.pls?id="
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47 static int Demux( demux_t *p_demux);
48 static int Control( demux_t *p_demux, int i_query, va_list args );
49
50 static int DemuxGenre( demux_t *p_demux, xml_reader_t *p_xml_reader,
51                        input_item_t *p_current_input );
52 static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader,
53                          input_item_t *p_current_input, 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 /*****************************************************************************
73  * Deactivate: frees unused data
74  *****************************************************************************/
75 void Close_Shoutcast( vlc_object_t *p_this )
76 {
77     (void)p_this;
78 }
79
80 static int Demux( demux_t *p_demux )
81 {
82     xml_t *p_xml;
83     xml_reader_t *p_xml_reader = NULL;
84     char *psz_eltname = NULL;
85     int i_ret = -1;
86     input_item_t *p_current_input = GetCurrentItem(p_demux);
87
88     p_xml = xml_Create( p_demux );
89     if( !p_xml )
90         goto error;
91
92     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
93     if( !p_xml_reader )
94         goto error;
95
96     /* check root node */
97     if( xml_ReaderRead( p_xml_reader ) != 1 )
98     {
99         msg_Err( p_demux, "invalid file (no root node)" );
100         goto error;
101     }
102
103     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
104         ( psz_eltname = xml_ReaderName( p_xml_reader ) ) == NULL ||
105         ( strcmp( psz_eltname, "genrelist" )
106           && strcmp( psz_eltname, "stationlist" ) ) )
107     {
108         msg_Err( p_demux, "invalid root node %i, %s",
109                  xml_ReaderNodeType( p_xml_reader ), psz_eltname );
110         goto error;
111     }
112
113     if( !strcmp( psz_eltname, "genrelist" ) )
114     {
115         /* we're reading a genre list */
116         if( DemuxGenre( p_demux, p_xml_reader, p_current_input ) )
117             goto error;
118     }
119     else
120     {
121         /* we're reading a station list */
122         if( DemuxStation( p_demux, p_xml_reader, p_current_input,
123                 var_CreateGetBool( p_demux, "shoutcast-show-adult" ) ) )
124             goto error;
125     }
126
127     i_ret = 0; /* Needed for correct operation of go back */
128
129 error:
130     if( p_xml_reader )
131         xml_ReaderDelete( p_xml, p_xml_reader );
132     if( p_xml )
133         xml_Delete( p_xml );
134     free( psz_eltname );
135     vlc_gc_decref(p_current_input);
136     return i_ret;
137 }
138
139 #define GET_VALUE( a ) \
140                         if( !strcmp( psz_attrname, #a ) ) \
141                         { \
142                             psz_ ## a = psz_attrvalue; \
143                         }
144 /* <genrelist>
145  *   <genre name="the name"></genre>
146  *   ...
147  * </genrelist>
148  **/
149 static int DemuxGenre( demux_t *p_demux, xml_reader_t *p_xml_reader,
150                        input_item_t *p_current_input )
151 {
152     char *psz_name = NULL; /* genre name */
153     int i_ret = -1;
154
155     while( xml_ReaderRead( p_xml_reader ) == 1 )
156     {
157         // Get the node type
158         switch( xml_ReaderNodeType( p_xml_reader ) )
159         {
160             // Error
161             case -1:
162                 goto error;
163
164             case XML_READER_STARTELEM:
165             {
166                 // Read the element name
167                 char *psz_eltname = xml_ReaderName( p_xml_reader );
168                 if( !psz_eltname )
169                     goto error;
170
171                 if( !strcmp( psz_eltname, "genre" ) )
172                 {
173                     // Read the attributes
174                     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
175                     {
176                         char *psz_attrname = xml_ReaderName( p_xml_reader );
177                         char *psz_attrvalue =
178                             xml_ReaderValue( p_xml_reader );
179                         if( !psz_attrname || !psz_attrvalue )
180                         {
181                             free( psz_attrname );
182                             free( psz_attrvalue );
183                             free( psz_eltname );
184                             break;
185                         }
186
187                         GET_VALUE( name )
188                         else
189                         {
190                             msg_Warn( p_demux,
191                                       "unexpected attribure %s in element %s",
192                                       psz_attrname, psz_eltname );
193                             free( psz_attrvalue );
194                         }
195                         free( psz_attrname );
196                     }
197                 }
198                 free( psz_eltname );
199                 break;
200             }
201
202             case XML_READER_TEXT:
203                 break;
204
205             // End element
206             case XML_READER_ENDELEM:
207             {
208                 // Read the element name
209                 char *psz_eltname = xml_ReaderName( p_xml_reader );
210                 if( !psz_eltname )
211                     goto error;
212
213                 if( !strcmp( psz_eltname, "genre" ) )
214                 {
215                     char* psz_mrl;
216                     if( asprintf( &psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
217                              psz_name ) != -1 )
218                     {
219                         input_item_t *p_input;
220                         p_input = input_item_New( p_demux, psz_mrl, psz_name );
221                         input_item_CopyOptions( p_current_input, p_input );
222                         free( psz_mrl );
223                         input_item_AddSubItem( p_current_input, p_input );
224                         vlc_gc_decref( p_input );
225                     }
226                     FREENULL( psz_name );
227                 }
228                 free( psz_eltname );
229                 break;
230             }
231         }
232     }
233     i_ret = 0;
234
235 error:
236     free( psz_name );
237     return i_ret;
238 }
239
240 /* radio stations:
241  * <stationlist>
242  *   <tunein base="/sbin/tunein-station.pls"></tunein>
243  *   <station name="the name"
244  *            mt="mime type"
245  *            id="the id"
246  *            br="bit rate"
247  *            genre="A big genre string"
248  *            ct="current track name/author/..."
249  *            lc="listener count"></station>
250  * </stationlist>
251  *
252  * TV stations:
253  * <stationlist>
254  *   <tunein base="/sbin/tunein-station.pls"></tunein>
255  *   <station name="the name"
256  *            id="the id"
257  *            br="bit rate"
258  *            rt="rating"
259  *            load="server load ?"
260  *            ct="current track name/author/..."
261  *            genre="A big genre string"
262  *            lc="listener count"></station>
263  * </stationlist>
264  **/
265 static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader,
266                          input_item_t *p_current_input, bool b_adult )
267 {
268     char *psz_base = NULL; /* */
269
270     char *psz_name = NULL; /* genre name */
271     char *psz_mt = NULL; /* mime type */
272     char *psz_id = NULL; /* id */
273     char *psz_br = NULL; /* bit rate */
274     char *psz_genre = NULL; /* genre */
275     char *psz_ct = NULL; /* current track */
276     char *psz_lc = NULL; /* listener count */
277
278     /* If these are set then it's *not* a radio but a TV */
279     char *psz_rt = NULL; /* rating for shoutcast TV */
280     char *psz_load = NULL; /* load for shoutcast TV */
281
282     char *psz_eltname = NULL; /* tag name */
283
284     while( xml_ReaderRead( p_xml_reader ) == 1 )
285     {
286         int i_type;
287
288         // Get the node type
289         i_type = xml_ReaderNodeType( p_xml_reader );
290         switch( i_type )
291         {
292             // Error
293             case -1:
294                 return -1;
295                 break;
296
297             case XML_READER_STARTELEM:
298                 // Read the element name
299                 psz_eltname = xml_ReaderName( p_xml_reader );
300                 if( !psz_eltname ) return -1;
301
302                 // Read the attributes
303                 if( !strcmp( psz_eltname, "tunein" ) )
304                 {
305                     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
306                     {
307                         char *psz_attrname = xml_ReaderName( p_xml_reader );
308                         char *psz_attrvalue =
309                             xml_ReaderValue( p_xml_reader );
310                         if( !psz_attrname || !psz_attrvalue )
311                         {
312                             free( psz_eltname );
313                             free( psz_attrname );
314                             free( psz_attrvalue );
315                             return -1;
316                         }
317
318                         GET_VALUE( base )
319                         else
320                         {
321                             msg_Warn( p_demux,
322                                       "unexpected attribure %s in element %s",
323                                       psz_attrname, psz_eltname );
324                             free( psz_attrvalue );
325                         }
326                         free( psz_attrname );
327                     }
328                 }
329                 else if( !strcmp( psz_eltname, "station" ) )
330                 {
331                     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
332                     {
333                         char *psz_attrname = xml_ReaderName( p_xml_reader );
334                         char *psz_attrvalue =
335                             xml_ReaderValue( p_xml_reader );
336                         if( !psz_attrname || !psz_attrvalue )
337                         {
338                             free( psz_eltname );
339                             free( psz_attrname );
340                             free( psz_attrvalue );
341                             return -1;
342                         }
343
344                         GET_VALUE( name )
345                         else GET_VALUE( mt )
346                         else GET_VALUE( id )
347                         else GET_VALUE( br )
348                         else GET_VALUE( genre )
349                         else GET_VALUE( ct )
350                         else GET_VALUE( lc )
351                         else GET_VALUE( rt )
352                         else GET_VALUE( load )
353                         else
354                         {
355                             msg_Warn( p_demux,
356                                       "unexpected attribute %s in element %s",
357                                       psz_attrname, psz_eltname );
358                             free( psz_attrvalue );
359                         }
360                         free( psz_attrname );
361                     }
362                 }
363                 free( psz_eltname );
364                 break;
365
366             case XML_READER_TEXT:
367                 break;
368
369             // End element
370             case XML_READER_ENDELEM:
371                 // Read the element name
372                 psz_eltname = xml_ReaderName( p_xml_reader );
373                 if( !psz_eltname ) return -1;
374                 if( !strcmp( psz_eltname, "station" ) &&
375                     ( psz_base || ( psz_rt && psz_load &&
376                     ( b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
377                 {
378                     char *psz_mrl = NULL;
379                     if( psz_rt || psz_load )
380                     {
381                         /* tv */
382                         if( asprintf( &psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
383                                  psz_id ) == -1)
384                             psz_mrl = NULL;
385                     }
386                     else
387                     {
388                         /* radio */
389                         if( asprintf( &psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
390                              psz_base, psz_id ) == -1 )
391                             psz_mrl = NULL;
392                     }
393
394                     /* Create the item */
395                     input_item_t *p_input;
396                     p_input = input_item_New( p_demux, psz_mrl, psz_name );
397                     input_item_CopyOptions( p_current_input, p_input );
398                     free( psz_mrl );
399
400 #define SADD_INFO( type, field ) \
401                     if( field ) \
402                         input_item_AddInfo( p_input, _("Shoutcast"), \
403                                             vlc_gettext(type), "%s", field )
404                     SADD_INFO( N_("Mime"), psz_mt );
405                     SADD_INFO( N_("Bitrate"), psz_br );
406                     SADD_INFO( N_("Listeners"), psz_lc );
407                     SADD_INFO( N_("Load"), psz_load );
408                     if( psz_genre )
409                         input_item_SetGenre( p_input, psz_genre );
410                     if( psz_ct )
411                         input_item_SetNowPlaying( p_input, psz_ct );
412                     if( psz_rt )
413                         input_item_SetRating( p_input, psz_rt );
414                     input_item_AddSubItem( p_current_input, p_input );
415                     vlc_gc_decref( p_input );
416                     FREENULL( psz_name );
417                     FREENULL( psz_mt );
418                     FREENULL( psz_id );
419                     FREENULL( psz_br );
420                     FREENULL( psz_genre );
421                     FREENULL( psz_ct );
422                     FREENULL( psz_lc );
423                     FREENULL( psz_rt );
424                 }
425                 free( psz_eltname );
426                 break;
427         }
428     }
429     return 0;
430 }
431
432 static int Control( demux_t *p_demux, int i_query, va_list args )
433 {
434     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
435     return VLC_EGENERIC;
436 }