1 /*****************************************************************************
2 * shoutcast.c: Winamp >=5.2 shoutcast demuxer
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
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="
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Demux( demux_t *p_demux);
49 static int DemuxGenre( demux_t *p_demux, xml_reader_t *p_xml_reader,
50 input_item_node_t *p_input_node );
51 static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader,
52 input_item_node_t *p_input_node, bool b_adult );
54 /*****************************************************************************
55 * Import_Shoutcast: main import function
56 *****************************************************************************/
57 int Import_Shoutcast( vlc_object_t *p_this )
59 demux_t *p_demux = (demux_t *)p_this;
61 if( !demux_IsForced( p_demux, "shout-winamp" ) )
64 p_demux->pf_demux = Demux;
65 p_demux->pf_control = Control;
66 msg_Dbg( p_demux, "using shoutcast playlist reader" );
71 static int Demux( demux_t *p_demux )
73 xml_reader_t *p_xml_reader = NULL;
76 input_item_t *p_current_input = GetCurrentItem(p_demux);
77 input_item_node_t *p_input_node = NULL;
79 p_xml_reader = xml_ReaderCreate( p_demux, p_demux->s );
84 if( xml_ReaderNextNode( p_xml_reader, &node ) != XML_READER_STARTELEM )
86 msg_Err( p_demux, "invalid file (no root node)" );
90 if( strcmp( node, "genrelist" ) && strcmp( node, "stationlist" ) )
92 msg_Err( p_demux, "invalid root node <%s>", node );
96 p_input_node = input_item_node_Create( p_current_input );
98 if( !strcmp( node, "genrelist" ) )
100 /* we're reading a genre list */
101 if( DemuxGenre( p_demux, p_xml_reader, p_input_node ) )
106 /* we're reading a station list */
107 if( DemuxStation( p_demux, p_xml_reader, p_input_node,
108 var_InheritBool( p_demux, "shoutcast-show-adult" ) ) )
112 input_item_node_PostAndDelete( p_input_node );
115 i_ret = 0; /* Needed for correct operation of go back */
119 xml_ReaderDelete( p_xml_reader );
120 if( p_input_node ) input_item_node_Delete( p_input_node );
121 vlc_gc_decref(p_current_input);
126 * <genre name="the name"></genre>
130 static int DemuxGenre( demux_t *p_demux, xml_reader_t *p_xml_reader,
131 input_item_node_t *p_input_node )
134 char *psz_name = NULL; /* genre name */
137 while( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
141 case XML_READER_STARTELEM:
143 if( !strcmp( node, "genre" ) )
145 // Read the attributes
146 const char *name, *value;
147 while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) )
149 if( !strcmp( name, "name" ) )
152 psz_name = strdup( value );
156 "unexpected attribute %s in <%s>",
163 case XML_READER_ENDELEM:
164 if( !strcmp( node, "genre" ) && psz_name != NULL )
168 if( asprintf( &psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
171 input_item_t *p_input;
172 p_input = input_item_New( psz_mrl, psz_name );
173 input_item_CopyOptions( p_input_node->p_item, p_input );
175 input_item_node_AppendItem( p_input_node, p_input );
176 vlc_gc_decref( p_input );
178 FREENULL( psz_name );
190 * <tunein base="/sbin/tunein-station.pls"></tunein>
191 * <station name="the name"
195 * genre="A big genre string"
196 * ct="current track name/author/..."
197 * lc="listener count"></station>
202 * <tunein base="/sbin/tunein-station.pls"></tunein>
203 * <station name="the name"
207 * load="server load ?"
208 * ct="current track name/author/..."
209 * genre="A big genre string"
210 * lc="listener count"></station>
213 static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader,
214 input_item_node_t *p_input_node, bool b_adult )
216 char *psz_base = NULL; /* */
218 char *psz_name = NULL; /* genre name */
219 char *psz_mt = NULL; /* mime type */
220 char *psz_id = NULL; /* id */
221 char *psz_br = NULL; /* bit rate */
222 char *psz_genre = NULL; /* genre */
223 char *psz_ct = NULL; /* current track */
224 char *psz_lc = NULL; /* listener count */
226 /* If these are set then it's *not* a radio but a TV */
227 char *psz_rt = NULL; /* rating for shoutcast TV */
228 char *psz_load = NULL; /* load for shoutcast TV */
230 const char *node; /* tag name */
233 while( (i_type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
237 case XML_READER_STARTELEM:
238 // Read the attributes
239 if( !strcmp( node, "tunein" ) )
241 const char *name, *value;
242 while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) )
244 if( !strcmp( name, "base" ) )
247 psz_base = strdup( value );
251 "unexpected attribute %s in <%s>",
255 else if( !strcmp( node, "station" ) )
257 const char *name, *value;
258 while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) )
261 if( !strcmp( name, "name" ) )
263 else if ( !strcmp( name, "mt" ) )
265 else if ( !strcmp( name, "id" ) )
267 else if ( !strcmp( name, "br" ) )
269 else if ( !strcmp( name, "genre" ) )
271 else if ( !strcmp( name, "ct" ) )
273 else if ( !strcmp( name, "lc" ) )
275 else if ( !strcmp( name, "rt" ) )
277 else if ( !strcmp( name, "load" ) )
282 *p = strdup( value );
286 "unexpected attribute %s in <%s>",
293 case XML_READER_ENDELEM:
294 if( !strcmp( node, "station" ) &&
295 ( psz_base || ( psz_rt && psz_load &&
296 ( b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
298 char *psz_mrl = NULL;
299 if( psz_rt || psz_load )
302 if( asprintf( &psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
309 if( asprintf( &psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
310 psz_base, psz_id ) == -1 )
314 /* Create the item */
315 input_item_t *p_input;
316 p_input = input_item_New( psz_mrl, psz_name );
317 input_item_CopyOptions( p_input_node->p_item, p_input );
320 #define SADD_INFO( type, field ) \
322 input_item_AddInfo( p_input, _("Shoutcast"), \
323 vlc_gettext(type), "%s", field )
324 SADD_INFO( N_("Mime"), psz_mt );
325 SADD_INFO( N_("Bitrate"), psz_br );
326 SADD_INFO( N_("Listeners"), psz_lc );
327 SADD_INFO( N_("Load"), psz_load );
329 input_item_SetGenre( p_input, psz_genre );
331 input_item_SetNowPlaying( p_input, psz_ct );
333 input_item_SetRating( p_input, psz_rt );
334 input_item_node_AppendItem( p_input_node, p_input );
335 vlc_gc_decref( p_input );
336 FREENULL( psz_base );
337 FREENULL( psz_name );
341 FREENULL( psz_genre );
345 FREENULL( psz_load );
350 /* FIXME: leaks on missing ENDELEMENT? */