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;
48 /* duplicate from modules/services_discovery/shout.c */
49 #define SHOUTCAST_BASE_URL "http/shout-winamp://www.shoutcast.com/sbin/newxml.phtml"
50 #define SHOUTCAST_TUNEIN_BASE_URL "http://www.shoutcast.com"
51 #define SHOUTCAST_TV_TUNEIN_URL "http://www.shoutcast.com/sbin/tunein-tvstation.pls?id="
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Demux( demux_t *p_demux);
57 static int Control( demux_t *p_demux, int i_query, va_list args );
59 static void ShoutcastAdd( playlist_t *p_playlist, playlist_item_t* p_genre,
60 playlist_item_t *p_bitrate, playlist_item_t *p_item,
61 char *psz_genre, char *psz_bitrate );
64 static int DemuxGenre( demux_t *p_demux );
65 static int DemuxStation( demux_t *p_demux );
67 /*****************************************************************************
68 * Import_Shoutcast: main import function
69 *****************************************************************************/
70 int E_(Import_Shoutcast)( vlc_object_t *p_this )
72 demux_t *p_demux = (demux_t *)p_this;
77 psz_ext = strrchr ( p_demux->psz_path, '.' );
79 if( !p_demux->psz_demux || strcmp(p_demux->psz_demux, "shout-winamp") )
83 msg_Dbg( p_demux, "using shoutcast playlist import");
85 p_demux->pf_control = Control;
86 p_demux->pf_demux = Demux;
87 p_demux->p_sys = p_sys = malloc( sizeof(demux_sys_t) );
90 msg_Err( p_demux, "out of memory" );
94 p_sys->p_playlist = NULL;
96 p_sys->p_xml_reader = NULL;
101 /*****************************************************************************
102 * Deactivate: frees unused data
103 *****************************************************************************/
104 void E_(Close_Shoutcast)( vlc_object_t *p_this )
106 demux_t *p_demux = (demux_t *)p_this;
107 demux_sys_t *p_sys = p_demux->p_sys;
109 if( p_sys->p_playlist )
110 vlc_object_release( p_sys->p_playlist );
111 if( p_sys->p_xml_reader )
112 xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
114 xml_Delete( p_sys->p_xml );
118 static int Demux( demux_t *p_demux )
120 demux_sys_t *p_sys = p_demux->p_sys;
121 playlist_t *p_playlist;
126 xml_reader_t *p_xml_reader;
128 char *psz_eltname = NULL;
131 p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
135 msg_Err( p_demux, "can't find playlist" );
138 p_sys->p_playlist = p_playlist;
140 b_play = E_(FindItem)( p_demux, p_playlist, &p_sys->p_current );
142 msg_Warn( p_demux, "item: %s", p_sys->p_current->input.psz_name );
143 playlist_ItemToNode( p_playlist, p_sys->p_current );
144 p_sys->p_current->input.i_type = ITEM_TYPE_PLAYLIST;
146 p_xml = p_sys->p_xml = xml_Create( p_demux );
147 if( !p_xml ) return -1;
149 /* psz_eltname = stream_ReadLine( p_demux->s );
150 if( psz_eltname ) free( psz_eltname );
153 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
154 if( !p_xml_reader ) return -1;
155 p_sys->p_xml_reader = p_xml_reader;
158 /* check root node */
159 if( xml_ReaderRead( p_xml_reader ) != 1 )
161 msg_Err( p_demux, "invalid file (no root node)" );
165 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
166 ( psz_eltname = xml_ReaderName( p_xml_reader ) ) == NULL ||
167 ( strcmp( psz_eltname, "genrelist" )
168 && strcmp( psz_eltname, "stationlist" ) ) )
170 msg_Err( p_demux, "invalid root node %i, %s",
171 xml_ReaderNodeType( p_xml_reader ), psz_eltname );
172 if( psz_eltname ) free( psz_eltname );
176 if( !strcmp( psz_eltname, "genrelist" ) )
178 /* we're reading a genre list */
179 if( DemuxGenre( p_demux ) ) return -1;
183 /* we're reading a station list */
184 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;
203 #define GET_VALUE( a ) \
204 if( !strcmp( psz_attrname, #a ) ) \
206 psz_ ## a = strdup( psz_attrvalue ); \
209 * <genre name="the name"></genre>
213 static int DemuxGenre( demux_t *p_demux )
215 demux_sys_t *p_sys = p_demux->p_sys;
216 char *psz_name = NULL; /* genre name */
217 char *psz_eltname = NULL; /* tag name */
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 if( psz_eltname ) free( psz_eltname );
235 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
236 if( !psz_eltname ) return -1;
239 if( !strcmp( psz_eltname, "genre" ) )
241 // Read the attributes
242 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
244 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
245 char *psz_attrvalue =
246 xml_ReaderValue( p_sys->p_xml_reader );
247 if( !psz_attrname || !psz_attrvalue ) return -1;
253 "unexpected attribure %s in element %s",
257 free( psz_attrname );
258 free( psz_attrvalue );
263 case XML_READER_TEXT:
267 case XML_READER_ENDELEM:
268 // Read the element name
270 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
271 if( !psz_eltname ) return -1;
272 if( !strcmp( psz_eltname, "genre" ) )
274 playlist_item_t *p_item;
275 char *psz_mrl = malloc( strlen( SHOUTCAST_BASE_URL )
276 + strlen( "?genre=" ) + strlen( psz_name ) + 1 );
277 sprintf( psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
279 p_item = playlist_ItemNew( p_sys->p_playlist, psz_mrl,
282 playlist_NodeAddItem( p_sys->p_playlist, p_item,
283 p_sys->p_current->pp_parents[0]->i_view,
284 p_sys->p_current, PLAYLIST_APPEND,
287 /* We need to declare the parents of the node as the
288 * * same of the parent's ones */
289 playlist_CopyParents( p_sys->p_current, p_item );
291 vlc_input_item_CopyOptions( &p_sys->p_current->input,
294 #define FREE(a) if( a ) free( a ); a = NULL;
299 psz_eltname = strdup("");
309 * <tunein base="/sbin/tunein-station.pls"></tunein>
310 * <station name="the name"
314 * genre="A big genre string"
315 * ct="current track name/author/..."
316 * lc="listener count"></station>
321 * <tunein base="/sbin/tunein-station.pls"></tunein>
322 * <station name="the name"
326 * load="server load ?"
327 * ct="current track name/author/..."
328 * genre="A big genre string"
329 * lc="listener count"></station>
332 static int DemuxStation( demux_t *p_demux )
334 demux_sys_t *p_sys = p_demux->p_sys;
336 char *psz_base = NULL; /* */
338 char *psz_name = NULL; /* genre name */
339 char *psz_mt = NULL; /* mime type */
340 char *psz_id = NULL; /* id */
341 char *psz_br = NULL; /* bit rate */
342 char *psz_genre = NULL; /* genre */
343 char *psz_ct = NULL; /* current track */
344 char *psz_lc = NULL; /* listener count */
346 /* If these are set then it's *not* a radio but a TV */
347 char *psz_rt = NULL; /* rating for shoutcast TV */
348 char *psz_load = NULL; /* load for shoutcast TV */
350 char *psz_eltname = NULL; /* tag name */
352 while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
357 i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
365 case XML_READER_STARTELEM:
366 // Read the element name
367 if( psz_eltname ) free( psz_eltname );
368 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
369 if( !psz_eltname ) return -1;
371 // Read the attributes
372 if( !strcmp( psz_eltname, "tunein" ) )
374 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
376 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
377 char *psz_attrvalue =
378 xml_ReaderValue( p_sys->p_xml_reader );
379 if( !psz_attrname || !psz_attrvalue ) return -1;
385 "unexpected attribure %s in element %s",
389 free( psz_attrname );
390 free( psz_attrvalue );
393 else if( !strcmp( psz_eltname, "station" ) )
395 while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
397 char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
398 char *psz_attrvalue =
399 xml_ReaderValue( p_sys->p_xml_reader );
400 if( !psz_attrname || !psz_attrvalue ) return -1;
406 else GET_VALUE( genre )
410 else GET_VALUE( load )
414 "unexpected attribure %s in element %s",
418 free( psz_attrname );
419 free( psz_attrvalue );
424 case XML_READER_TEXT:
428 case XML_READER_ENDELEM:
429 // Read the element name
431 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
432 if( !psz_eltname ) return -1;
433 if( !strcmp( psz_eltname, "station" ) &&
434 ( psz_base || ( psz_rt && psz_load ) ) )
436 playlist_item_t *p_item;
437 char *psz_mrl = NULL;
438 if( psz_rt || psz_load )
441 psz_mrl = malloc( strlen( SHOUTCAST_TV_TUNEIN_URL )
442 + strlen( psz_id ) + 1 );
443 sprintf( psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
449 psz_mrl = malloc( strlen( SHOUTCAST_TUNEIN_BASE_URL )
450 + strlen( psz_base ) + strlen( "?id=" )
451 + strlen( psz_id ) + 1 );
452 sprintf( psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
455 msg_Warn( p_demux, "%s", psz_mrl );
456 p_item = playlist_ItemNew( p_sys->p_playlist, psz_mrl,
462 vlc_input_item_AddInfo( &p_item->input,
470 vlc_input_item_AddInfo( &p_item->input,
478 vlc_input_item_AddInfo( &p_item->input,
479 _(VLC_META_INFO_CAT),
486 vlc_input_item_AddInfo( &p_item->input,
487 _(VLC_META_INFO_CAT),
488 _(VLC_META_NOW_PLAYING),
494 vlc_input_item_AddInfo( &p_item->input,
502 vlc_input_item_AddInfo( &p_item->input,
510 vlc_input_item_AddInfo( &p_item->input,
517 playlist_NodeAddItem( p_sys->p_playlist, p_item,
518 p_sys->p_current->pp_parents[0]->i_view,
519 p_sys->p_current, PLAYLIST_APPEND,
522 /* We need to declare the parents of the node as the
523 * * same of the parent's ones */
524 playlist_CopyParents( p_sys->p_current, p_item );
526 vlc_input_item_CopyOptions( &p_sys->p_current->input,
529 #define FREE(a) if( a ) free( a ); a = NULL;
541 psz_eltname = strdup("");
549 static int Control( demux_t *p_demux, int i_query, va_list args )