1 /*****************************************************************************
2 * shoutcast.c: Winamp >=5.2 shoutcast demuxer
3 *****************************************************************************
4 * Copyright (C) 2006 VLC authors and VideoLAN
7 * Authors: Antoine Cellerier <dionoea -@t- videolan -Dot- org>
8 * based on b4s.c by Sigmund Augdal Helberg <dnumgis@videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
38 #include <vlc_strings.h>
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="
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Demux( demux_t *p_demux);
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 );
55 /*****************************************************************************
56 * Import_Shoutcast: main import function
57 *****************************************************************************/
58 int Import_Shoutcast( vlc_object_t *p_this )
60 demux_t *p_demux = (demux_t *)p_this;
62 if( !demux_IsForced( p_demux, "shout-winamp" ) )
65 p_demux->pf_demux = Demux;
66 p_demux->pf_control = Control;
67 msg_Dbg( p_demux, "using shoutcast playlist reader" );
72 static int Demux( demux_t *p_demux )
74 xml_reader_t *p_xml_reader = NULL;
77 input_item_t *p_current_input = GetCurrentItem(p_demux);
78 input_item_node_t *p_input_node = NULL;
80 p_xml_reader = xml_ReaderCreate( p_demux, p_demux->s );
85 if( xml_ReaderNextNode( p_xml_reader, &node ) != XML_READER_STARTELEM )
87 msg_Err( p_demux, "invalid file (no root node)" );
91 if( strcmp( node, "genrelist" ) && strcmp( node, "stationlist" ) )
93 msg_Err( p_demux, "invalid root node <%s>", node );
97 p_input_node = input_item_node_Create( p_current_input );
99 if( !strcmp( node, "genrelist" ) )
101 /* we're reading a genre list */
102 if( DemuxGenre( p_demux, p_xml_reader, p_input_node ) )
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" ) ) )
113 input_item_node_PostAndDelete( p_input_node );
116 i_ret = 0; /* Needed for correct operation of go back */
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);
127 * <genre name="the name"></genre>
131 static int DemuxGenre( demux_t *p_demux, xml_reader_t *p_xml_reader,
132 input_item_node_t *p_input_node )
135 char *psz_name = NULL; /* genre name */
138 while( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
142 case XML_READER_STARTELEM:
144 if( !strcmp( node, "genre" ) )
146 // Read the attributes
147 const char *name, *value;
148 while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) )
150 if( !strcmp( name, "name" ) )
153 psz_name = strdup( value );
157 "unexpected attribute %s in <%s>",
164 case XML_READER_ENDELEM:
165 if( !strcmp( node, "genre" ) && psz_name != NULL )
169 if( asprintf( &psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
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 );
177 input_item_node_AppendItem( p_input_node, p_input );
178 vlc_gc_decref( p_input );
180 FREENULL( psz_name );
192 * <tunein base="/sbin/tunein-station.pls"></tunein>
193 * <station name="the name"
197 * genre="A big genre string"
198 * ct="current track name/author/..."
199 * lc="listener count"></station>
204 * <tunein base="/sbin/tunein-station.pls"></tunein>
205 * <station name="the name"
209 * load="server load ?"
210 * ct="current track name/author/..."
211 * genre="A big genre string"
212 * lc="listener count"></station>
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 )
218 char *psz_base = NULL; /* */
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 */
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 */
232 const char *node; /* tag name */
235 while( (i_type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
239 case XML_READER_STARTELEM:
240 // Read the attributes
241 if( !strcmp( node, "tunein" ) )
243 const char *name, *value;
244 while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) )
246 if( !strcmp( name, "base" ) )
249 psz_base = strdup( value );
253 "unexpected attribute %s in <%s>",
257 else if( !strcmp( node, "station" ) )
259 const char *name, *value;
260 while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) )
263 if( !strcmp( name, "name" ) )
265 else if ( !strcmp( name, "mt" ) )
267 else if ( !strcmp( name, "id" ) )
269 else if ( !strcmp( name, "br" ) )
271 else if ( !strcmp( name, "genre" ) )
273 else if ( !strcmp( name, "ct" ) )
275 else if ( !strcmp( name, "lc" ) )
277 else if ( !strcmp( name, "rt" ) )
279 else if ( !strcmp( name, "load" ) )
284 *p = strdup( value );
288 "unexpected attribute %s in <%s>",
295 case XML_READER_ENDELEM:
296 if( !strcmp( node, "station" ) &&
297 ( psz_base || ( psz_rt && psz_load &&
298 ( b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
300 char *psz_mrl = NULL;
301 if( psz_rt || psz_load )
304 if( asprintf( &psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
311 if( asprintf( &psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
312 psz_base, psz_id ) == -1 )
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 );
323 #define SADD_INFO( type, 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 );
332 input_item_SetGenre( p_input, psz_genre );
334 input_item_SetNowPlaying( p_input, psz_ct );
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 );
344 FREENULL( psz_genre );
348 FREENULL( psz_load );
353 /* FIXME: leaks on missing ENDELEMENT? */