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);
48 static int Control( demux_t *p_demux, int i_query, va_list args );
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 /*****************************************************************************
73 * Deactivate: frees unused data
74 *****************************************************************************/
75 void Close_Shoutcast( vlc_object_t *p_this )
80 static int Demux( demux_t *p_demux )
83 xml_reader_t *p_xml_reader = NULL;
84 char *psz_eltname = NULL;
86 input_item_t *p_current_input = GetCurrentItem(p_demux);
88 p_xml = xml_Create( p_demux );
92 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
97 if( xml_ReaderRead( p_xml_reader ) != 1 )
99 msg_Err( p_demux, "invalid file (no root node)" );
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" ) ) )
108 msg_Err( p_demux, "invalid root node %i, %s",
109 xml_ReaderNodeType( p_xml_reader ), psz_eltname );
113 input_item_node_t *p_input_node = input_item_node_Create( p_current_input );
115 if( !strcmp( psz_eltname, "genrelist" ) )
117 /* we're reading a genre list */
118 if( DemuxGenre( p_demux, p_xml_reader, p_input_node ) )
123 /* we're reading a station list */
124 if( DemuxStation( p_demux, p_xml_reader, p_input_node,
125 var_CreateGetBool( p_demux, "shoutcast-show-adult" ) ) )
129 input_item_AddSubItemTree( p_input_node );
131 i_ret = 0; /* Needed for correct operation of go back */
135 xml_ReaderDelete( p_xml, p_xml_reader );
139 input_item_node_Delete( p_input_node );
140 vlc_gc_decref(p_current_input);
144 #define GET_VALUE( a ) \
145 if( !strcmp( psz_attrname, #a ) ) \
148 psz_ ## a = psz_attrvalue; \
151 * <genre name="the name"></genre>
155 static int DemuxGenre( demux_t *p_demux, xml_reader_t *p_xml_reader,
156 input_item_node_t *p_input_node )
158 char *psz_name = NULL; /* genre name */
161 while( xml_ReaderRead( p_xml_reader ) == 1 )
164 switch( xml_ReaderNodeType( p_xml_reader ) )
170 case XML_READER_STARTELEM:
172 // Read the element name
173 char *psz_eltname = xml_ReaderName( p_xml_reader );
177 if( !strcmp( psz_eltname, "genre" ) )
179 // Read the attributes
180 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
182 char *psz_attrname = xml_ReaderName( p_xml_reader );
183 char *psz_attrvalue =
184 xml_ReaderValue( p_xml_reader );
185 if( !psz_attrname || !psz_attrvalue )
187 free( psz_attrname );
188 free( psz_attrvalue );
197 "unexpected attribure %s in element %s",
198 psz_attrname, psz_eltname );
199 free( psz_attrvalue );
201 free( psz_attrname );
208 case XML_READER_TEXT:
212 case XML_READER_ENDELEM:
214 // Read the element name
215 char *psz_eltname = xml_ReaderName( p_xml_reader );
219 if( !strcmp( psz_eltname, "genre" ) )
222 if( asprintf( &psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
225 input_item_t *p_input;
226 p_input = input_item_New( p_demux, psz_mrl, psz_name );
227 input_item_CopyOptions( p_input_node->p_item, p_input );
229 input_item_node_AppendItem( p_input_node, p_input );
230 vlc_gc_decref( p_input );
232 FREENULL( psz_name );
248 * <tunein base="/sbin/tunein-station.pls"></tunein>
249 * <station name="the name"
253 * genre="A big genre string"
254 * ct="current track name/author/..."
255 * lc="listener count"></station>
260 * <tunein base="/sbin/tunein-station.pls"></tunein>
261 * <station name="the name"
265 * load="server load ?"
266 * ct="current track name/author/..."
267 * genre="A big genre string"
268 * lc="listener count"></station>
271 static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader,
272 input_item_node_t *p_input_node, bool b_adult )
274 char *psz_base = NULL; /* */
276 char *psz_name = NULL; /* genre name */
277 char *psz_mt = NULL; /* mime type */
278 char *psz_id = NULL; /* id */
279 char *psz_br = NULL; /* bit rate */
280 char *psz_genre = NULL; /* genre */
281 char *psz_ct = NULL; /* current track */
282 char *psz_lc = NULL; /* listener count */
284 /* If these are set then it's *not* a radio but a TV */
285 char *psz_rt = NULL; /* rating for shoutcast TV */
286 char *psz_load = NULL; /* load for shoutcast TV */
288 char *psz_eltname = NULL; /* tag name */
290 while( xml_ReaderRead( p_xml_reader ) == 1 )
295 i_type = xml_ReaderNodeType( p_xml_reader );
303 case XML_READER_STARTELEM:
304 // Read the element name
305 psz_eltname = xml_ReaderName( p_xml_reader );
306 if( !psz_eltname ) return -1;
308 // Read the attributes
309 if( !strcmp( psz_eltname, "tunein" ) )
311 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
313 char *psz_attrname = xml_ReaderName( p_xml_reader );
314 char *psz_attrvalue =
315 xml_ReaderValue( p_xml_reader );
316 if( !psz_attrname || !psz_attrvalue )
319 free( psz_attrname );
320 free( psz_attrvalue );
328 "unexpected attribure %s in element %s",
329 psz_attrname, psz_eltname );
330 free( psz_attrvalue );
332 free( psz_attrname );
335 else if( !strcmp( psz_eltname, "station" ) )
337 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
339 char *psz_attrname = xml_ReaderName( p_xml_reader );
340 char *psz_attrvalue =
341 xml_ReaderValue( p_xml_reader );
342 if( !psz_attrname || !psz_attrvalue )
345 free( psz_attrname );
346 free( psz_attrvalue );
354 else GET_VALUE( genre )
358 else GET_VALUE( load )
362 "unexpected attribute %s in element %s",
363 psz_attrname, psz_eltname );
364 free( psz_attrvalue );
366 free( psz_attrname );
372 case XML_READER_TEXT:
376 case XML_READER_ENDELEM:
377 // Read the element name
378 psz_eltname = xml_ReaderName( p_xml_reader );
379 if( !psz_eltname ) return -1;
380 if( !strcmp( psz_eltname, "station" ) &&
381 ( psz_base || ( psz_rt && psz_load &&
382 ( b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
384 char *psz_mrl = NULL;
385 if( psz_rt || psz_load )
388 if( asprintf( &psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
395 if( asprintf( &psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
396 psz_base, psz_id ) == -1 )
400 /* Create the item */
401 input_item_t *p_input;
402 p_input = input_item_New( p_demux, psz_mrl, psz_name );
403 input_item_CopyOptions( p_input_node->p_item, p_input );
406 #define SADD_INFO( type, field ) \
408 input_item_AddInfo( p_input, _("Shoutcast"), \
409 vlc_gettext(type), "%s", field )
410 SADD_INFO( N_("Mime"), psz_mt );
411 SADD_INFO( N_("Bitrate"), psz_br );
412 SADD_INFO( N_("Listeners"), psz_lc );
413 SADD_INFO( N_("Load"), psz_load );
415 input_item_SetGenre( p_input, psz_genre );
417 input_item_SetNowPlaying( p_input, psz_ct );
419 input_item_SetRating( p_input, psz_rt );
420 input_item_node_AppendItem( p_input_node, p_input );
421 vlc_gc_decref( p_input );
422 FREENULL( psz_base );
423 FREENULL( psz_name );
427 FREENULL( psz_genre );
431 FREENULL( psz_load );
440 static int Control( demux_t *p_demux, int i_query, va_list args )
442 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);