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