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 <stdlib.h> /* malloc(), free() */
29 #include <ctype.h> /* isspace() */
32 #include <vlc/input.h>
35 #include <errno.h> /* ENOMEM */
41 playlist_t *p_playlist;
42 playlist_item_t *p_current;
45 xml_reader_t *p_xml_reader;
50 /* duplicate from modules/services_discovery/shout.c */
51 #define SHOUTCAST_BASE_URL "http/shout-winamp://www.shoutcast.com/sbin/newxml.phtml"
52 #define SHOUTCAST_TUNEIN_BASE_URL "http://www.shoutcast.com"
53 #define SHOUTCAST_TV_TUNEIN_URL "http://www.shoutcast.com/sbin/tunein-tvstation.pls?id="
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Demux( demux_t *p_demux);
59 static int Control( demux_t *p_demux, int i_query, va_list args );
61 static int DemuxGenre( demux_t *p_demux );
62 static int DemuxStation( demux_t *p_demux );
64 /*****************************************************************************
65 * Import_Shoutcast: main import function
66 *****************************************************************************/
67 int E_(Import_Shoutcast)( vlc_object_t *p_this )
69 demux_t *p_demux = (demux_t *)p_this;
74 psz_ext = strrchr ( p_demux->psz_path, '.' );
76 if( !p_demux->psz_demux || strcmp(p_demux->psz_demux, "shout-winamp") )
80 msg_Dbg( p_demux, "using shoutcast playlist import");
82 p_demux->pf_control = Control;
83 p_demux->pf_demux = Demux;
84 p_demux->p_sys = p_sys = malloc( sizeof(demux_sys_t) );
87 msg_Err( p_demux, "out of memory" );
91 p_sys->p_playlist = NULL;
93 p_sys->p_xml_reader = NULL;
95 /* Do we want to list adult content ? */
96 var_Create( p_demux, "shoutcast-show-adult",
97 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
98 p_sys->b_adult = var_GetBool( p_demux, "shoutcast-show-adult" );
103 /*****************************************************************************
104 * Deactivate: frees unused data
105 *****************************************************************************/
106 void E_(Close_Shoutcast)( vlc_object_t *p_this )
108 demux_t *p_demux = (demux_t *)p_this;
109 demux_sys_t *p_sys = p_demux->p_sys;
111 if( p_sys->p_playlist )
112 vlc_object_release( p_sys->p_playlist );
113 if( p_sys->p_xml_reader )
114 xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
116 xml_Delete( p_sys->p_xml );
120 static int Demux( demux_t *p_demux )
122 demux_sys_t *p_sys = p_demux->p_sys;
123 playlist_t *p_playlist;
128 xml_reader_t *p_xml_reader;
130 char *psz_eltname = NULL;
133 p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
137 msg_Err( p_demux, "can't find playlist" );
140 p_sys->p_playlist = p_playlist;
142 b_play = E_(FindItem)( p_demux, p_playlist, &p_sys->p_current );
144 msg_Warn( p_demux, "item: %s", p_sys->p_current->input.psz_name );
145 playlist_ItemToNode( p_playlist, p_sys->p_current );
146 p_sys->p_current->input.i_type = ITEM_TYPE_PLAYLIST;
148 p_xml = p_sys->p_xml = xml_Create( p_demux );
149 if( !p_xml ) return -1;
151 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
152 if( !p_xml_reader ) return -1;
153 p_sys->p_xml_reader = p_xml_reader;
156 /* check root node */
157 if( xml_ReaderRead( p_xml_reader ) != 1 )
159 msg_Err( p_demux, "invalid file (no root node)" );
163 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
164 ( psz_eltname = xml_ReaderName( p_xml_reader ) ) == NULL ||
165 ( strcmp( psz_eltname, "genrelist" )
166 && strcmp( psz_eltname, "stationlist" ) ) )
168 msg_Err( p_demux, "invalid root node %i, %s",
169 xml_ReaderNodeType( p_xml_reader ), psz_eltname );
170 if( psz_eltname ) free( psz_eltname );
174 if( !strcmp( psz_eltname, "genrelist" ) )
176 /* we're reading a genre list */
178 if( DemuxGenre( p_demux ) ) return -1;
182 /* we're reading a station list */
184 if( DemuxStation( p_demux ) ) return -1;
187 /* Go back and play the playlist */
188 if( b_play && p_playlist->status.p_item &&
189 p_playlist->status.p_item->i_children > 0 )
191 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
192 p_playlist->status.i_view,
193 p_playlist->status.p_item,
194 p_playlist->status.p_item->pp_children[0] );
197 vlc_object_release( p_playlist );
198 p_sys->p_playlist = NULL;
202 #define GET_VALUE( a ) \
203 if( !strcmp( psz_attrname, #a ) ) \
205 psz_ ## a = strdup( psz_attrvalue ); \
208 * <genre name="the name"></genre>
212 static int DemuxGenre( demux_t *p_demux )
214 demux_sys_t *p_sys = p_demux->p_sys;
215 char *psz_name = NULL; /* genre name */
216 char *psz_eltname = NULL; /* tag name */
218 #define FREE(a) if( a ) free( a ); a = NULL;
219 while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
224 i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
232 case XML_READER_STARTELEM:
233 // Read the element name
234 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
235 if( !psz_eltname ) return -1;
238 if( !strcmp( psz_eltname, "genre" ) )
240 // Read the attributes
241 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
243 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
244 char *psz_attrvalue =
245 xml_ReaderValue( p_sys->p_xml_reader );
246 if( !psz_attrname || !psz_attrvalue )
251 /*FIXME: isn't return a bit too much. what about break*/
259 "unexpected attribure %s in element %s",
263 free( psz_attrname );
264 free( psz_attrvalue );
267 free( psz_eltname ); psz_eltname = NULL;
270 case XML_READER_TEXT:
274 case XML_READER_ENDELEM:
275 // Read the element name
276 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
277 if( !psz_eltname ) return -1;
278 if( !strcmp( psz_eltname, "genre" ) )
280 playlist_item_t *p_item;
281 char *psz_mrl = malloc( strlen( SHOUTCAST_BASE_URL )
282 + strlen( "?genre=" ) + strlen( psz_name ) + 1 );
283 sprintf( psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
285 p_item = playlist_ItemNew( p_sys->p_playlist, psz_mrl,
288 playlist_NodeAddItem( p_sys->p_playlist, p_item,
289 p_sys->p_current->pp_parents[0]->i_view,
290 p_sys->p_current, PLAYLIST_APPEND,
293 /* We need to declare the parents of the node as the
294 * * same of the parent's ones */
295 playlist_CopyParents( p_sys->p_current, p_item );
297 vlc_input_item_CopyOptions( &p_sys->p_current->input,
311 * <tunein base="/sbin/tunein-station.pls"></tunein>
312 * <station name="the name"
316 * genre="A big genre string"
317 * ct="current track name/author/..."
318 * lc="listener count"></station>
323 * <tunein base="/sbin/tunein-station.pls"></tunein>
324 * <station name="the name"
328 * load="server load ?"
329 * ct="current track name/author/..."
330 * genre="A big genre string"
331 * lc="listener count"></station>
334 static int DemuxStation( demux_t *p_demux )
336 demux_sys_t *p_sys = p_demux->p_sys;
338 char *psz_base = NULL; /* */
340 char *psz_name = NULL; /* genre name */
341 char *psz_mt = NULL; /* mime type */
342 char *psz_id = NULL; /* id */
343 char *psz_br = NULL; /* bit rate */
344 char *psz_genre = NULL; /* genre */
345 char *psz_ct = NULL; /* current track */
346 char *psz_lc = NULL; /* listener count */
348 /* If these are set then it's *not* a radio but a TV */
349 char *psz_rt = NULL; /* rating for shoutcast TV */
350 char *psz_load = NULL; /* load for shoutcast TV */
352 char *psz_eltname = NULL; /* tag name */
354 while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
359 i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
367 case XML_READER_STARTELEM:
368 // Read the element name
369 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
370 if( !psz_eltname ) return -1;
372 // Read the attributes
373 if( !strcmp( psz_eltname, "tunein" ) )
375 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
377 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
378 char *psz_attrvalue =
379 xml_ReaderValue( p_sys->p_xml_reader );
380 if( !psz_attrname || !psz_attrvalue )
392 "unexpected attribure %s in element %s",
396 free( psz_attrname );
397 free( psz_attrvalue );
400 else if( !strcmp( psz_eltname, "station" ) )
402 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
404 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
405 char *psz_attrvalue =
406 xml_ReaderValue( p_sys->p_xml_reader );
407 if( !psz_attrname || !psz_attrvalue )
419 else GET_VALUE( genre )
423 else GET_VALUE( load )
427 "unexpected attribure %s in element %s",
431 free( psz_attrname );
432 free( psz_attrvalue );
438 case XML_READER_TEXT:
442 case XML_READER_ENDELEM:
443 // Read the element name
444 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
445 if( !psz_eltname ) return -1;
446 if( !strcmp( psz_eltname, "station" ) &&
447 ( psz_base || ( psz_rt && psz_load &&
448 ( p_sys->b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
450 playlist_item_t *p_item;
451 char *psz_mrl = NULL;
452 if( psz_rt || psz_load )
455 psz_mrl = malloc( strlen( SHOUTCAST_TV_TUNEIN_URL )
456 + strlen( psz_id ) + 1 );
457 sprintf( psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
463 psz_mrl = malloc( strlen( SHOUTCAST_TUNEIN_BASE_URL )
464 + strlen( psz_base ) + strlen( "?id=" )
465 + strlen( psz_id ) + 1 );
466 sprintf( psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
469 p_item = playlist_ItemNew( p_sys->p_playlist, psz_mrl,
475 vlc_input_item_AddInfo( &p_item->input,
483 vlc_input_item_AddInfo( &p_item->input,
491 vlc_input_item_AddInfo( &p_item->input,
492 _(VLC_META_INFO_CAT),
499 vlc_input_item_AddInfo( &p_item->input,
500 _(VLC_META_INFO_CAT),
501 _(VLC_META_NOW_PLAYING),
507 vlc_input_item_AddInfo( &p_item->input,
515 vlc_input_item_AddInfo( &p_item->input,
523 vlc_input_item_AddInfo( &p_item->input,
530 playlist_NodeAddItem( p_sys->p_playlist, p_item,
531 p_sys->p_current->pp_parents[0]->i_view,
532 p_sys->p_current, PLAYLIST_APPEND,
535 /* We need to declare the parents of the node as the
536 * * same of the parent's ones */
537 playlist_CopyParents( p_sys->p_current, p_item );
539 vlc_input_item_CopyOptions( &p_sys->p_current->input,
559 static int Control( demux_t *p_demux, int i_query, va_list args )