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" ) )
227 char *psz_mrl = malloc( strlen( SHOUTCAST_BASE_URL )
228 + strlen( "?genre=" ) + strlen( psz_name ) + 1 );
229 sprintf( psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
231 p_input = input_item_NewExt( p_demux, psz_mrl,
232 psz_name, 0, NULL, -1 );
233 input_item_CopyOptions( p_sys->p_current_input,
236 input_item_AddSubItem( p_sys->p_current_input, p_input );
237 vlc_gc_decref( p_input );
238 FREENULL( psz_name );
240 FREENULL( psz_eltname );
249 * <tunein base="/sbin/tunein-station.pls"></tunein>
250 * <station name="the name"
254 * genre="A big genre string"
255 * ct="current track name/author/..."
256 * lc="listener count"></station>
261 * <tunein base="/sbin/tunein-station.pls"></tunein>
262 * <station name="the name"
266 * load="server load ?"
267 * ct="current track name/author/..."
268 * genre="A big genre string"
269 * lc="listener count"></station>
272 static int DemuxStation( demux_t *p_demux )
274 demux_sys_t *p_sys = p_demux->p_sys;
275 input_item_t *p_input;
277 char *psz_base = NULL; /* */
279 char *psz_name = NULL; /* genre name */
280 char *psz_mt = NULL; /* mime type */
281 char *psz_id = NULL; /* id */
282 char *psz_br = NULL; /* bit rate */
283 char *psz_genre = NULL; /* genre */
284 char *psz_ct = NULL; /* current track */
285 char *psz_lc = NULL; /* listener count */
287 /* If these are set then it's *not* a radio but a TV */
288 char *psz_rt = NULL; /* rating for shoutcast TV */
289 char *psz_load = NULL; /* load for shoutcast TV */
291 char *psz_eltname = NULL; /* tag name */
293 while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
298 i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
306 case XML_READER_STARTELEM:
307 // Read the element name
308 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
309 if( !psz_eltname ) return -1;
311 // Read the attributes
312 if( !strcmp( psz_eltname, "tunein" ) )
314 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
316 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
317 char *psz_attrvalue =
318 xml_ReaderValue( p_sys->p_xml_reader );
319 if( !psz_attrname || !psz_attrvalue )
322 FREENULL(psz_attrname);
323 FREENULL(psz_attrvalue);
331 "unexpected attribure %s in element %s",
332 psz_attrname, psz_eltname );
334 free( psz_attrname );
335 free( psz_attrvalue );
338 else if( !strcmp( psz_eltname, "station" ) )
340 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
342 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
343 char *psz_attrvalue =
344 xml_ReaderValue( p_sys->p_xml_reader );
345 if( !psz_attrname || !psz_attrvalue )
348 FREENULL(psz_attrname);
349 FREENULL(psz_attrvalue);
357 else GET_VALUE( genre )
361 else GET_VALUE( load )
365 "unexpected attribute %s in element %s",
366 psz_attrname, psz_eltname );
368 free( psz_attrname );
369 free( psz_attrvalue );
375 case XML_READER_TEXT:
379 case XML_READER_ENDELEM:
380 // Read the element name
381 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
382 if( !psz_eltname ) return -1;
383 if( !strcmp( psz_eltname, "station" ) &&
384 ( psz_base || ( psz_rt && psz_load &&
385 ( p_sys->b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
387 char *psz_mrl = NULL;
388 if( psz_rt || psz_load )
391 psz_mrl = malloc( strlen( SHOUTCAST_TV_TUNEIN_URL )
392 + strlen( psz_id ) + 1 );
393 sprintf( psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
399 psz_mrl = malloc( strlen( SHOUTCAST_TUNEIN_BASE_URL )
400 + strlen( psz_base ) + strlen( "?id=" )
401 + strlen( psz_id ) + 1 );
402 sprintf( psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
405 p_input = input_item_NewExt( p_demux, psz_mrl,
406 psz_name , 0, NULL, -1 );
409 input_item_CopyOptions( p_sys->p_current_input,
412 #define SADD_INFO( type, field ) if( field ) { input_item_AddInfo( \
413 p_input, _("Shoutcast"), _(type), "%s", field ) ; }
414 SADD_INFO( "Mime type", psz_mt );
415 SADD_INFO( "Bitrate", psz_br );
416 SADD_INFO( "Listeners", psz_lc );
417 SADD_INFO( "Load", psz_load );
419 input_item_SetGenre( p_input, psz_genre );
421 input_item_SetNowPlaying( p_input, psz_ct );
423 input_item_SetRating( p_input, psz_rt );
424 input_item_AddSubItem( p_sys->p_current_input, p_input );
425 vlc_gc_decref( p_input );
426 FREENULL( psz_name );
430 FREENULL( psz_genre );
442 static int Control( demux_t *p_demux, int i_query, va_list args )
444 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);