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>
41 input_item_t *p_current_input;
44 xml_reader_t *p_xml_reader;
49 /* duplicate from modules/services_discovery/shout.c */
50 #define SHOUTCAST_BASE_URL "http/shout-winamp://www.shoutcast.com/sbin/newxml.phtml"
51 #define SHOUTCAST_TUNEIN_BASE_URL "http://www.shoutcast.com"
52 #define SHOUTCAST_TV_TUNEIN_URL "http://www.shoutcast.com/sbin/tunein-tvstation.pls?id="
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Demux( demux_t *p_demux);
58 static int Control( demux_t *p_demux, int i_query, va_list args );
60 static int DemuxGenre( demux_t *p_demux );
61 static int DemuxStation( demux_t *p_demux );
63 /*****************************************************************************
64 * Import_Shoutcast: main import function
65 *****************************************************************************/
66 int Import_Shoutcast( vlc_object_t *p_this )
68 demux_t *p_demux = (demux_t *)p_this;
70 if( !demux_IsForced( p_demux, "shout-winamp" ) )
73 STANDARD_DEMUX_INIT_MSG( "using shoutcast playlist reader" );
74 p_demux->p_sys->p_xml = NULL;
75 p_demux->p_sys->p_xml_reader = NULL;
77 /* Do we want to list adult content ? */
78 var_Create( p_demux, "shoutcast-show-adult",
79 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
80 p_demux->p_sys->b_adult = var_GetBool( p_demux, "shoutcast-show-adult" );
85 /*****************************************************************************
86 * Deactivate: frees unused data
87 *****************************************************************************/
88 void Close_Shoutcast( vlc_object_t *p_this )
90 demux_t *p_demux = (demux_t *)p_this;
91 demux_sys_t *p_sys = p_demux->p_sys;
93 if( p_sys->p_xml_reader )
94 xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
96 xml_Delete( p_sys->p_xml );
100 static int Demux( demux_t *p_demux )
102 demux_sys_t *p_sys = p_demux->p_sys;
104 xml_reader_t *p_xml_reader;
105 char *psz_eltname = NULL;
107 p_sys->p_current_input = p_current_input;
109 p_xml = p_sys->p_xml = xml_Create( p_demux );
110 if( !p_xml ) return -1;
112 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
113 if( !p_xml_reader ) return -1;
114 p_sys->p_xml_reader = p_xml_reader;
116 /* check root node */
117 if( xml_ReaderRead( p_xml_reader ) != 1 )
119 msg_Err( p_demux, "invalid file (no root node)" );
123 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
124 ( psz_eltname = xml_ReaderName( p_xml_reader ) ) == NULL ||
125 ( strcmp( psz_eltname, "genrelist" )
126 && strcmp( psz_eltname, "stationlist" ) ) )
128 msg_Err( p_demux, "invalid root node %i, %s",
129 xml_ReaderNodeType( p_xml_reader ), psz_eltname );
134 if( !strcmp( psz_eltname, "genrelist" ) )
136 /* we're reading a genre list */
138 if( DemuxGenre( p_demux ) ) return -1;
142 /* we're reading a station list */
144 if( DemuxStation( p_demux ) ) return -1;
147 HANDLE_PLAY_AND_RELEASE;
148 return 0; /* Needed for correct operation of go back */
151 #define GET_VALUE( a ) \
152 if( !strcmp( psz_attrname, #a ) ) \
154 psz_ ## a = strdup( psz_attrvalue ); \
157 * <genre name="the name"></genre>
161 static int DemuxGenre( demux_t *p_demux )
163 demux_sys_t *p_sys = p_demux->p_sys;
164 char *psz_name = NULL; /* genre name */
165 char *psz_eltname = NULL; /* tag name */
166 input_item_t *p_input;
168 while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
173 i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
181 case XML_READER_STARTELEM:
182 // Read the element name
183 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
184 if( !psz_eltname ) return -1;
186 if( !strcmp( psz_eltname, "genre" ) )
188 // Read the attributes
189 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
191 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
192 char *psz_attrvalue =
193 xml_ReaderValue( p_sys->p_xml_reader );
194 if( !psz_attrname || !psz_attrvalue )
196 FREENULL(psz_attrname);
197 FREENULL(psz_attrvalue);
199 /*FIXME: isn't return a bit too much. what about break*/
207 "unexpected attribure %s in element %s",
208 psz_attrname,psz_eltname );
210 free( psz_attrname );
211 free( psz_attrvalue );
214 free( psz_eltname ); psz_eltname = NULL;
217 case XML_READER_TEXT:
221 case XML_READER_ENDELEM:
222 // Read the element name
223 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
224 if( !psz_eltname ) return -1;
225 if( !strcmp( psz_eltname, "genre" ) )
228 if( asprintf( &psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
231 p_input = input_item_New( p_demux, psz_mrl, psz_name );
232 input_item_CopyOptions( p_sys->p_current_input, p_input );
234 input_item_AddSubItem( p_sys->p_current_input, p_input );
235 vlc_gc_decref( p_input );
237 FREENULL( psz_name );
239 FREENULL( psz_eltname );
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 )
273 demux_sys_t *p_sys = p_demux->p_sys;
274 input_item_t *p_input;
276 char *psz_base = NULL; /* */
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 */
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 */
290 char *psz_eltname = NULL; /* tag name */
292 while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
297 i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
305 case XML_READER_STARTELEM:
306 // Read the element name
307 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
308 if( !psz_eltname ) return -1;
310 // Read the attributes
311 if( !strcmp( psz_eltname, "tunein" ) )
313 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
315 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
316 char *psz_attrvalue =
317 xml_ReaderValue( p_sys->p_xml_reader );
318 if( !psz_attrname || !psz_attrvalue )
321 free( psz_attrname );
322 free( psz_attrvalue );
330 "unexpected attribure %s in element %s",
331 psz_attrname, psz_eltname );
333 free( psz_attrname );
334 free( psz_attrvalue );
337 else if( !strcmp( psz_eltname, "station" ) )
339 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
341 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
342 char *psz_attrvalue =
343 xml_ReaderValue( p_sys->p_xml_reader );
344 if( !psz_attrname || !psz_attrvalue )
347 free( psz_attrname );
348 free( psz_attrvalue );
356 else GET_VALUE( genre )
360 else GET_VALUE( load )
364 "unexpected attribute %s in element %s",
365 psz_attrname, psz_eltname );
367 free( psz_attrname );
368 free( psz_attrvalue );
374 case XML_READER_TEXT:
378 case XML_READER_ENDELEM:
379 // Read the element name
380 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
381 if( !psz_eltname ) return -1;
382 if( !strcmp( psz_eltname, "station" ) &&
383 ( psz_base || ( psz_rt && psz_load &&
384 ( p_sys->b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
386 char *psz_mrl = NULL;
387 if( psz_rt || psz_load )
390 if( asprintf( &psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
397 if( asprintf( &psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
398 psz_base, psz_id ) == -1 )
401 p_input = input_item_New( p_demux, psz_mrl, psz_name );
402 input_item_CopyOptions( p_sys->p_current_input, p_input );
405 #define SADD_INFO( type, field ) \
407 input_item_AddInfo( p_input, _("Shoutcast"), \
408 vlc_gettext(type), "%s", field )
409 SADD_INFO( N_("Mime"), psz_mt );
410 SADD_INFO( N_("Bitrate"), psz_br );
411 SADD_INFO( N_("Listeners"), psz_lc );
412 SADD_INFO( N_("Load"), psz_load );
414 input_item_SetGenre( p_input, psz_genre );
416 input_item_SetNowPlaying( p_input, psz_ct );
418 input_item_SetRating( p_input, psz_rt );
419 input_item_AddSubItem( p_sys->p_current_input, p_input );
420 vlc_gc_decref( p_input );
421 FREENULL( psz_name );
425 FREENULL( psz_genre );
437 static int Control( demux_t *p_demux, int i_query, va_list args )
439 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);