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 #define FREE(a) if( a ) free( a ); a = NULL;
178 while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
183 i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
191 case XML_READER_STARTELEM:
192 // Read the element name
193 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
194 if( !psz_eltname ) return -1;
196 if( !strcmp( psz_eltname, "genre" ) )
198 // Read the attributes
199 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
201 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
202 char *psz_attrvalue =
203 xml_ReaderValue( p_sys->p_xml_reader );
204 if( !psz_attrname || !psz_attrvalue )
209 /*FIXME: isn't return a bit too much. what about break*/
217 "unexpected attribure %s in element %s",
218 psz_attrname,psz_eltname );
220 free( psz_attrname );
221 free( psz_attrvalue );
224 free( psz_eltname ); psz_eltname = NULL;
227 case XML_READER_TEXT:
231 case XML_READER_ENDELEM:
232 // Read the element name
233 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
234 if( !psz_eltname ) return -1;
235 if( !strcmp( psz_eltname, "genre" ) )
237 char *psz_mrl = malloc( strlen( SHOUTCAST_BASE_URL )
238 + strlen( "?genre=" ) + strlen( psz_name ) + 1 );
239 sprintf( psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
241 p_input = input_ItemNewExt( p_sys->p_playlist, psz_mrl,
242 psz_name, 0, NULL, -1 );
243 vlc_input_item_CopyOptions( p_sys->p_current->p_input,
246 playlist_AddWhereverNeeded( p_sys->p_playlist, p_input,
247 p_sys->p_current, p_sys->p_item_in_category,
248 (p_sys->i_parent_id > 0 ) ? VLC_TRUE: VLC_FALSE,
261 * <tunein base="/sbin/tunein-station.pls"></tunein>
262 * <station name="the name"
266 * genre="A big genre string"
267 * ct="current track name/author/..."
268 * lc="listener count"></station>
273 * <tunein base="/sbin/tunein-station.pls"></tunein>
274 * <station name="the name"
278 * load="server load ?"
279 * ct="current track name/author/..."
280 * genre="A big genre string"
281 * lc="listener count"></station>
284 static int DemuxStation( demux_t *p_demux )
286 demux_sys_t *p_sys = p_demux->p_sys;
287 input_item_t *p_input;
289 char *psz_base = NULL; /* */
291 char *psz_name = NULL; /* genre name */
292 char *psz_mt = NULL; /* mime type */
293 char *psz_id = NULL; /* id */
294 char *psz_br = NULL; /* bit rate */
295 char *psz_genre = NULL; /* genre */
296 char *psz_ct = NULL; /* current track */
297 char *psz_lc = NULL; /* listener count */
299 /* If these are set then it's *not* a radio but a TV */
300 char *psz_rt = NULL; /* rating for shoutcast TV */
301 char *psz_load = NULL; /* load for shoutcast TV */
303 char *psz_eltname = NULL; /* tag name */
305 while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
310 i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
318 case XML_READER_STARTELEM:
319 // Read the element name
320 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
321 if( !psz_eltname ) return -1;
323 // Read the attributes
324 if( !strcmp( psz_eltname, "tunein" ) )
326 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
328 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
329 char *psz_attrvalue =
330 xml_ReaderValue( p_sys->p_xml_reader );
331 if( !psz_attrname || !psz_attrvalue )
343 "unexpected attribure %s in element %s",
344 psz_attrname, psz_eltname );
346 free( psz_attrname );
347 free( psz_attrvalue );
350 else if( !strcmp( psz_eltname, "station" ) )
352 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
354 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
355 char *psz_attrvalue =
356 xml_ReaderValue( p_sys->p_xml_reader );
357 if( !psz_attrname || !psz_attrvalue )
369 else GET_VALUE( genre )
373 else GET_VALUE( load )
377 "unexpected attribute %s in element %s",
378 psz_attrname, psz_eltname );
380 free( psz_attrname );
381 free( psz_attrvalue );
387 case XML_READER_TEXT:
391 case XML_READER_ENDELEM:
392 // Read the element name
393 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
394 if( !psz_eltname ) return -1;
395 if( !strcmp( psz_eltname, "station" ) &&
396 ( psz_base || ( psz_rt && psz_load &&
397 ( p_sys->b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
399 playlist_item_t *p_item;
400 char *psz_mrl = NULL;
401 if( psz_rt || psz_load )
404 psz_mrl = malloc( strlen( SHOUTCAST_TV_TUNEIN_URL )
405 + strlen( psz_id ) + 1 );
406 sprintf( psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
412 psz_mrl = malloc( strlen( SHOUTCAST_TUNEIN_BASE_URL )
413 + strlen( psz_base ) + strlen( "?id=" )
414 + strlen( psz_id ) + 1 );
415 sprintf( psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
418 p_input = input_ItemNewExt( p_sys->p_playlist, psz_mrl,
419 psz_name , 0, NULL, -1 );
422 vlc_input_item_CopyOptions( p_sys->p_current->p_input,
425 #define SADD_INFO( type, field ) if( field ) { vlc_input_item_AddInfo( \
426 p_input, _("Shoutcast"), _(type), "%s", field ) ; }
427 SADD_INFO( "Mime type", psz_mt );
428 SADD_INFO( "Bitrate", psz_br );
429 SADD_INFO( "Listeners", psz_lc );
430 SADD_INFO( "Load", psz_load );
431 p_input->p_meta = vlc_meta_New();
433 vlc_meta_SetGenre( p_input->p_meta, psz_genre );
435 vlc_meta_SetNowPlaying( p_input->p_meta, psz_ct );
437 vlc_meta_SetRating( p_input->p_meta, psz_rt );
439 playlist_AddWhereverNeeded( p_sys->p_playlist, p_input,
440 p_sys->p_current, p_sys->p_item_in_category,
441 (p_sys->i_parent_id > 0 ) ? VLC_TRUE: VLC_FALSE,
461 static int Control( demux_t *p_demux, int i_query, va_list args )