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 *****************************************************************************/
28 #include <ctype.h> /* isspace() */
31 #include <vlc/input.h>
34 #include <errno.h> /* ENOMEM */
40 playlist_t *p_playlist;
41 playlist_item_t *p_current;
42 playlist_item_t *p_item_in_category;
46 xml_reader_t *p_xml_reader;
51 /* duplicate from modules/services_discovery/shout.c */
52 #define SHOUTCAST_BASE_URL "http/shout-winamp://www.shoutcast.com/sbin/newxml.phtml"
53 #define SHOUTCAST_TUNEIN_BASE_URL "http://www.shoutcast.com"
54 #define SHOUTCAST_TV_TUNEIN_URL "http://www.shoutcast.com/sbin/tunein-tvstation.pls?id="
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Demux( demux_t *p_demux);
60 static int Control( demux_t *p_demux, int i_query, va_list args );
62 static int DemuxGenre( demux_t *p_demux );
63 static int DemuxStation( demux_t *p_demux );
65 /*****************************************************************************
66 * Import_Shoutcast: main import function
67 *****************************************************************************/
68 int E_(Import_Shoutcast)( vlc_object_t *p_this )
70 demux_t *p_demux = (demux_t *)p_this;
72 if( !isDemux( p_demux, "shout-winamp" ) )
75 STANDARD_DEMUX_INIT_MSG( "using shoutcast playlist reader" );
76 p_demux->p_sys->p_playlist = NULL;
77 p_demux->p_sys->p_xml = NULL;
78 p_demux->p_sys->p_xml_reader = NULL;
80 /* Do we want to list adult content ? */
81 var_Create( p_demux, "shoutcast-show-adult",
82 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
83 p_demux->p_sys->b_adult = var_GetBool( p_demux, "shoutcast-show-adult" );
88 /*****************************************************************************
89 * Deactivate: frees unused data
90 *****************************************************************************/
91 void E_(Close_Shoutcast)( vlc_object_t *p_this )
93 demux_t *p_demux = (demux_t *)p_this;
94 demux_sys_t *p_sys = p_demux->p_sys;
96 if( p_sys->p_playlist )
97 vlc_object_release( p_sys->p_playlist );
98 if( p_sys->p_xml_reader )
99 xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
101 xml_Delete( p_sys->p_xml );
105 static int Demux( demux_t *p_demux )
107 demux_sys_t *p_sys = p_demux->p_sys;
109 xml_reader_t *p_xml_reader;
110 char *psz_eltname = NULL;
112 p_sys->p_playlist = p_playlist;
113 p_sys->p_current = p_current;
114 p_sys->i_parent_id = i_parent_id;
115 p_sys->p_item_in_category = p_item_in_category;
117 p_xml = p_sys->p_xml = xml_Create( p_demux );
118 if( !p_xml ) return -1;
120 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
121 if( !p_xml_reader ) return -1;
122 p_sys->p_xml_reader = p_xml_reader;
124 /* check root node */
125 if( xml_ReaderRead( p_xml_reader ) != 1 )
127 msg_Err( p_demux, "invalid file (no root node)" );
131 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
132 ( psz_eltname = xml_ReaderName( p_xml_reader ) ) == NULL ||
133 ( strcmp( psz_eltname, "genrelist" )
134 && strcmp( psz_eltname, "stationlist" ) ) )
136 msg_Err( p_demux, "invalid root node %i, %s",
137 xml_ReaderNodeType( p_xml_reader ), psz_eltname );
138 if( psz_eltname ) free( psz_eltname );
142 if( !strcmp( psz_eltname, "genrelist" ) )
144 /* we're reading a genre list */
146 if( DemuxGenre( p_demux ) ) return -1;
150 /* we're reading a station list */
152 if( DemuxStation( p_demux ) ) return -1;
155 HANDLE_PLAY_AND_RELEASE;
156 p_sys->p_playlist = NULL;
160 #define GET_VALUE( a ) \
161 if( !strcmp( psz_attrname, #a ) ) \
163 psz_ ## a = strdup( psz_attrvalue ); \
166 * <genre name="the name"></genre>
170 static int DemuxGenre( demux_t *p_demux )
172 demux_sys_t *p_sys = p_demux->p_sys;
173 char *psz_name = NULL; /* genre name */
174 char *psz_eltname = NULL; /* tag name */
175 input_item_t *p_input;
177 while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
182 i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
190 case XML_READER_STARTELEM:
191 // Read the element name
192 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
193 if( !psz_eltname ) return -1;
195 if( !strcmp( psz_eltname, "genre" ) )
197 // Read the attributes
198 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
200 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
201 char *psz_attrvalue =
202 xml_ReaderValue( p_sys->p_xml_reader );
203 if( !psz_attrname || !psz_attrvalue )
205 FREENULL(psz_attrname);
206 FREENULL(psz_attrvalue);
208 /*FIXME: isn't return a bit too much. what about break*/
216 "unexpected attribure %s in element %s",
217 psz_attrname,psz_eltname );
219 free( psz_attrname );
220 free( psz_attrvalue );
223 free( psz_eltname ); psz_eltname = NULL;
226 case XML_READER_TEXT:
230 case XML_READER_ENDELEM:
231 // Read the element name
232 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
233 if( !psz_eltname ) return -1;
234 if( !strcmp( psz_eltname, "genre" ) )
236 char *psz_mrl = malloc( strlen( SHOUTCAST_BASE_URL )
237 + strlen( "?genre=" ) + strlen( psz_name ) + 1 );
238 sprintf( psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
240 p_input = input_ItemNewExt( p_sys->p_playlist, psz_mrl,
241 psz_name, 0, NULL, -1 );
242 vlc_input_item_CopyOptions( p_sys->p_current->p_input,
245 playlist_AddWhereverNeeded( p_sys->p_playlist, p_input,
246 p_sys->p_current, p_sys->p_item_in_category,
247 (p_sys->i_parent_id > 0 ) ? VLC_TRUE: VLC_FALSE,
249 FREENULL( psz_name );
251 FREENULL( psz_eltname );
260 * <tunein base="/sbin/tunein-station.pls"></tunein>
261 * <station name="the name"
265 * genre="A big genre string"
266 * ct="current track name/author/..."
267 * lc="listener count"></station>
272 * <tunein base="/sbin/tunein-station.pls"></tunein>
273 * <station name="the name"
277 * load="server load ?"
278 * ct="current track name/author/..."
279 * genre="A big genre string"
280 * lc="listener count"></station>
283 static int DemuxStation( demux_t *p_demux )
285 demux_sys_t *p_sys = p_demux->p_sys;
286 input_item_t *p_input;
288 char *psz_base = NULL; /* */
290 char *psz_name = NULL; /* genre name */
291 char *psz_mt = NULL; /* mime type */
292 char *psz_id = NULL; /* id */
293 char *psz_br = NULL; /* bit rate */
294 char *psz_genre = NULL; /* genre */
295 char *psz_ct = NULL; /* current track */
296 char *psz_lc = NULL; /* listener count */
298 /* If these are set then it's *not* a radio but a TV */
299 char *psz_rt = NULL; /* rating for shoutcast TV */
300 char *psz_load = NULL; /* load for shoutcast TV */
302 char *psz_eltname = NULL; /* tag name */
304 while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
309 i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
317 case XML_READER_STARTELEM:
318 // Read the element name
319 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
320 if( !psz_eltname ) return -1;
322 // Read the attributes
323 if( !strcmp( psz_eltname, "tunein" ) )
325 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
327 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
328 char *psz_attrvalue =
329 xml_ReaderValue( p_sys->p_xml_reader );
330 if( !psz_attrname || !psz_attrvalue )
333 FREENULL(psz_attrname);
334 FREENULL(psz_attrvalue);
342 "unexpected attribure %s in element %s",
343 psz_attrname, psz_eltname );
345 free( psz_attrname );
346 free( psz_attrvalue );
349 else if( !strcmp( psz_eltname, "station" ) )
351 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
353 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
354 char *psz_attrvalue =
355 xml_ReaderValue( p_sys->p_xml_reader );
356 if( !psz_attrname || !psz_attrvalue )
359 FREENULL(psz_attrname);
360 FREENULL(psz_attrvalue);
368 else GET_VALUE( genre )
372 else GET_VALUE( load )
376 "unexpected attribute %s in element %s",
377 psz_attrname, psz_eltname );
379 free( psz_attrname );
380 free( psz_attrvalue );
386 case XML_READER_TEXT:
390 case XML_READER_ENDELEM:
391 // Read the element name
392 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
393 if( !psz_eltname ) return -1;
394 if( !strcmp( psz_eltname, "station" ) &&
395 ( psz_base || ( psz_rt && psz_load &&
396 ( p_sys->b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
398 playlist_item_t *p_item;
399 char *psz_mrl = NULL;
400 if( psz_rt || psz_load )
403 psz_mrl = malloc( strlen( SHOUTCAST_TV_TUNEIN_URL )
404 + strlen( psz_id ) + 1 );
405 sprintf( psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
411 psz_mrl = malloc( strlen( SHOUTCAST_TUNEIN_BASE_URL )
412 + strlen( psz_base ) + strlen( "?id=" )
413 + strlen( psz_id ) + 1 );
414 sprintf( psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
417 p_input = input_ItemNewExt( p_sys->p_playlist, psz_mrl,
418 psz_name , 0, NULL, -1 );
421 vlc_input_item_CopyOptions( p_sys->p_current->p_input,
424 #define SADD_INFO( type, field ) if( field ) { vlc_input_item_AddInfo( \
425 p_input, _("Shoutcast"), _(type), "%s", field ) ; }
426 SADD_INFO( "Mime type", psz_mt );
427 SADD_INFO( "Bitrate", psz_br );
428 SADD_INFO( "Listeners", psz_lc );
429 SADD_INFO( "Load", psz_load );
430 p_input->p_meta = vlc_meta_New();
432 vlc_meta_SetGenre( p_input->p_meta, psz_genre );
434 vlc_meta_SetNowPlaying( p_input->p_meta, psz_ct );
436 vlc_meta_SetRating( p_input->p_meta, psz_rt );
438 playlist_AddWhereverNeeded( p_sys->p_playlist, p_input,
439 p_sys->p_current, p_sys->p_item_in_category,
440 (p_sys->i_parent_id > 0 ) ? VLC_TRUE: VLC_FALSE,
443 FREENULL( psz_name );
447 FREENULL( psz_genre )
459 static int Control( demux_t *p_demux, int i_query, va_list args )