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 )
123 demux_sys_t *p_sys = p_demux->p_sys;
124 playlist_t *p_playlist;
129 xml_reader_t *p_xml_reader;
131 char *psz_eltname = NULL;
134 p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
138 msg_Err( p_demux, "can't find playlist" );
141 p_sys->p_playlist = p_playlist;
143 b_play = E_(FindItem)( p_demux, p_playlist, &p_sys->p_current );
145 msg_Warn( p_demux, "item: %s", p_sys->p_current->input.psz_name );
146 playlist_ItemToNode( p_playlist, p_sys->p_current );
147 p_sys->p_current->input.i_type = ITEM_TYPE_PLAYLIST;
149 p_xml = p_sys->p_xml = xml_Create( p_demux );
150 if( !p_xml ) return -1;
152 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
153 if( !p_xml_reader ) return -1;
154 p_sys->p_xml_reader = p_xml_reader;
157 /* check root node */
158 if( xml_ReaderRead( p_xml_reader ) != 1 )
160 msg_Err( p_demux, "invalid file (no root node)" );
164 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
165 ( psz_eltname = xml_ReaderName( p_xml_reader ) ) == NULL ||
166 ( strcmp( psz_eltname, "genrelist" )
167 && strcmp( psz_eltname, "stationlist" ) ) )
169 msg_Err( p_demux, "invalid root node %i, %s",
170 xml_ReaderNodeType( p_xml_reader ), psz_eltname );
171 if( psz_eltname ) free( psz_eltname );
175 if( !strcmp( psz_eltname, "genrelist" ) )
177 /* we're reading a genre list */
179 if( DemuxGenre( p_demux ) ) return -1;
183 /* we're reading a station list */
185 if( DemuxStation( p_demux ) ) return -1;
188 /* Go back and play the playlist */
189 if( b_play && p_playlist->status.p_item &&
190 p_playlist->status.p_item->i_children > 0 )
192 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
193 p_playlist->status.i_view,
194 p_playlist->status.p_item,
195 p_playlist->status.p_item->pp_children[0] );
198 vlc_object_release( p_playlist );
199 p_sys->p_playlist = NULL;
204 #define GET_VALUE( a ) \
205 if( !strcmp( psz_attrname, #a ) ) \
207 psz_ ## a = strdup( psz_attrvalue ); \
210 * <genre name="the name"></genre>
214 static int DemuxGenre( demux_t *p_demux )
217 demux_sys_t *p_sys = p_demux->p_sys;
218 char *psz_name = NULL; /* genre name */
219 char *psz_eltname = NULL; /* tag name */
221 #define FREE(a) if( a ) free( a ); a = NULL;
222 while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
227 i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
235 case XML_READER_STARTELEM:
236 // Read the element name
237 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
238 if( !psz_eltname ) return -1;
241 if( !strcmp( psz_eltname, "genre" ) )
243 // Read the attributes
244 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
246 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
247 char *psz_attrvalue =
248 xml_ReaderValue( p_sys->p_xml_reader );
249 if( !psz_attrname || !psz_attrvalue )
254 /*FIXME: isn't return a bit too much. what about break*/
262 "unexpected attribure %s in element %s",
266 free( psz_attrname );
267 free( psz_attrvalue );
270 free( psz_eltname ); psz_eltname = NULL;
273 case XML_READER_TEXT:
277 case XML_READER_ENDELEM:
278 // Read the element name
279 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
280 if( !psz_eltname ) return -1;
281 if( !strcmp( psz_eltname, "genre" ) )
283 playlist_item_t *p_item;
284 char *psz_mrl = malloc( strlen( SHOUTCAST_BASE_URL )
285 + strlen( "?genre=" ) + strlen( psz_name ) + 1 );
286 sprintf( psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
288 p_item = playlist_ItemNew( p_sys->p_playlist, psz_mrl,
291 playlist_NodeAddItem( p_sys->p_playlist, p_item,
292 p_sys->p_current->pp_parents[0]->i_view,
293 p_sys->p_current, PLAYLIST_APPEND,
296 /* We need to declare the parents of the node as the
297 * * same of the parent's ones */
298 playlist_CopyParents( p_sys->p_current, p_item );
300 vlc_input_item_CopyOptions( &p_sys->p_current->input,
315 * <tunein base="/sbin/tunein-station.pls"></tunein>
316 * <station name="the name"
320 * genre="A big genre string"
321 * ct="current track name/author/..."
322 * lc="listener count"></station>
327 * <tunein base="/sbin/tunein-station.pls"></tunein>
328 * <station name="the name"
332 * load="server load ?"
333 * ct="current track name/author/..."
334 * genre="A big genre string"
335 * lc="listener count"></station>
338 static int DemuxStation( demux_t *p_demux )
341 demux_sys_t *p_sys = p_demux->p_sys;
343 char *psz_base = NULL; /* */
345 char *psz_name = NULL; /* genre name */
346 char *psz_mt = NULL; /* mime type */
347 char *psz_id = NULL; /* id */
348 char *psz_br = NULL; /* bit rate */
349 char *psz_genre = NULL; /* genre */
350 char *psz_ct = NULL; /* current track */
351 char *psz_lc = NULL; /* listener count */
353 /* If these are set then it's *not* a radio but a TV */
354 char *psz_rt = NULL; /* rating for shoutcast TV */
355 char *psz_load = NULL; /* load for shoutcast TV */
357 char *psz_eltname = NULL; /* tag name */
359 while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
364 i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
372 case XML_READER_STARTELEM:
373 // Read the element name
374 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
375 if( !psz_eltname ) return -1;
377 // Read the attributes
378 if( !strcmp( psz_eltname, "tunein" ) )
380 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
382 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
383 char *psz_attrvalue =
384 xml_ReaderValue( p_sys->p_xml_reader );
385 if( !psz_attrname || !psz_attrvalue )
397 "unexpected attribure %s in element %s",
401 free( psz_attrname );
402 free( psz_attrvalue );
405 else if( !strcmp( psz_eltname, "station" ) )
407 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
409 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
410 char *psz_attrvalue =
411 xml_ReaderValue( p_sys->p_xml_reader );
412 if( !psz_attrname || !psz_attrvalue )
424 else GET_VALUE( genre )
428 else GET_VALUE( load )
432 "unexpected attribure %s in element %s",
436 free( psz_attrname );
437 free( psz_attrvalue );
443 case XML_READER_TEXT:
447 case XML_READER_ENDELEM:
448 // Read the element name
449 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
450 if( !psz_eltname ) return -1;
451 if( !strcmp( psz_eltname, "station" ) &&
452 ( psz_base || ( psz_rt && psz_load &&
453 ( p_sys->b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
455 playlist_item_t *p_item;
456 char *psz_mrl = NULL;
457 if( psz_rt || psz_load )
460 psz_mrl = malloc( strlen( SHOUTCAST_TV_TUNEIN_URL )
461 + strlen( psz_id ) + 1 );
462 sprintf( psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
468 psz_mrl = malloc( strlen( SHOUTCAST_TUNEIN_BASE_URL )
469 + strlen( psz_base ) + strlen( "?id=" )
470 + strlen( psz_id ) + 1 );
471 sprintf( psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
474 p_item = playlist_ItemNew( p_sys->p_playlist, psz_mrl,
480 vlc_input_item_AddInfo( &p_item->input,
488 vlc_input_item_AddInfo( &p_item->input,
496 vlc_input_item_AddInfo( &p_item->input,
497 _(VLC_META_INFO_CAT),
504 vlc_input_item_AddInfo( &p_item->input,
505 _(VLC_META_INFO_CAT),
506 _(VLC_META_NOW_PLAYING),
512 vlc_input_item_AddInfo( &p_item->input,
520 vlc_input_item_AddInfo( &p_item->input,
521 _(VLC_META_INFO_CAT),
528 vlc_input_item_AddInfo( &p_item->input,
535 playlist_NodeAddItem( p_sys->p_playlist, p_item,
536 p_sys->p_current->pp_parents[0]->i_view,
537 p_sys->p_current, PLAYLIST_APPEND,
540 /* We need to declare the parents of the node as the
541 * * same of the parent's ones */
542 playlist_CopyParents( p_sys->p_current, p_item );
544 vlc_input_item_CopyOptions( &p_sys->p_current->input,
565 static int Control( demux_t *p_demux, int i_query, va_list args )