Revert "Remove libvlc_free"
[vlc/asuraparaju-public.git] / modules / demux / playlist / shoutcast.c
bloba8c2e5d92c02eb56fb4079917617d2d73b3ec192
1 /*****************************************************************************
2 * shoutcast.c: Winamp >=5.2 shoutcast demuxer
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
36 #include "playlist.h"
37 #include <vlc_xml.h>
39 /* duplicate from modules/services_discovery/shout.c */
40 #define SHOUTCAST_BASE_URL "http/shout-winamp://www.shoutcast.com/sbin/newxml.phtml"
41 #define SHOUTCAST_TUNEIN_BASE_URL "http://www.shoutcast.com"
42 #define SHOUTCAST_TV_TUNEIN_URL "http://www.shoutcast.com/sbin/tunein-tvstation.pls?id="
44 /*****************************************************************************
45 * Local prototypes
46 *****************************************************************************/
47 static int Demux( demux_t *p_demux);
48 static int Control( demux_t *p_demux, int i_query, va_list args );
50 static int DemuxGenre( demux_t *p_demux, xml_reader_t *p_xml_reader,
51 input_item_node_t *p_input_node );
52 static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader,
53 input_item_node_t *p_input_node, bool b_adult );
55 /*****************************************************************************
56 * Import_Shoutcast: main import function
57 *****************************************************************************/
58 int Import_Shoutcast( vlc_object_t *p_this )
60 demux_t *p_demux = (demux_t *)p_this;
62 if( !demux_IsForced( p_demux, "shout-winamp" ) )
63 return VLC_EGENERIC;
65 p_demux->pf_demux = Demux;
66 p_demux->pf_control = Control;
67 msg_Dbg( p_demux, "using shoutcast playlist reader" );
69 return VLC_SUCCESS;
72 /*****************************************************************************
73 * Deactivate: frees unused data
74 *****************************************************************************/
75 void Close_Shoutcast( vlc_object_t *p_this )
77 (void)p_this;
80 static int Demux( demux_t *p_demux )
82 xml_reader_t *p_xml_reader = NULL;
83 char *psz_eltname = NULL;
84 int i_ret = -1;
85 input_item_t *p_current_input = GetCurrentItem(p_demux);
86 input_item_node_t *p_input_node = NULL;
88 p_xml_reader = xml_ReaderCreate( p_demux, p_demux->s );
89 if( !p_xml_reader )
90 goto error;
92 /* check root node */
93 if( xml_ReaderRead( p_xml_reader ) != 1 )
95 msg_Err( p_demux, "invalid file (no root node)" );
96 goto error;
99 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
100 ( psz_eltname = xml_ReaderName( p_xml_reader ) ) == NULL ||
101 ( strcmp( psz_eltname, "genrelist" )
102 && strcmp( psz_eltname, "stationlist" ) ) )
104 msg_Err( p_demux, "invalid root node %i, %s",
105 xml_ReaderNodeType( p_xml_reader ), psz_eltname );
106 goto error;
109 p_input_node = input_item_node_Create( p_current_input );
111 if( !strcmp( psz_eltname, "genrelist" ) )
113 /* we're reading a genre list */
114 if( DemuxGenre( p_demux, p_xml_reader, p_input_node ) )
115 goto error;
117 else
119 /* we're reading a station list */
120 if( DemuxStation( p_demux, p_xml_reader, p_input_node,
121 var_InheritBool( p_demux, "shoutcast-show-adult" ) ) )
122 goto error;
125 input_item_node_PostAndDelete( p_input_node );
126 p_input_node = NULL;
128 i_ret = 0; /* Needed for correct operation of go back */
130 error:
131 if( p_xml_reader )
132 xml_ReaderDelete( p_xml_reader );
133 free( psz_eltname );
134 if( p_input_node ) input_item_node_Delete( p_input_node );
135 vlc_gc_decref(p_current_input);
136 return i_ret;
139 #define GET_VALUE( a ) \
140 if( !strcmp( psz_attrname, #a ) ) \
142 free(psz_ ## a); \
143 psz_ ## a = psz_attrvalue; \
145 /* <genrelist>
146 * <genre name="the name"></genre>
147 * ...
148 * </genrelist>
150 static int DemuxGenre( demux_t *p_demux, xml_reader_t *p_xml_reader,
151 input_item_node_t *p_input_node )
153 char *psz_name = NULL; /* genre name */
154 int i_ret = -1;
156 while( xml_ReaderRead( p_xml_reader ) == 1 )
158 // Get the node type
159 switch( xml_ReaderNodeType( p_xml_reader ) )
161 // Error
162 case -1:
163 goto error;
165 case XML_READER_STARTELEM:
167 // Read the element name
168 char *psz_eltname = xml_ReaderName( p_xml_reader );
169 if( !psz_eltname )
170 goto error;
172 if( !strcmp( psz_eltname, "genre" ) )
174 // Read the attributes
175 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
177 char *psz_attrname = xml_ReaderName( p_xml_reader );
178 char *psz_attrvalue =
179 xml_ReaderValue( p_xml_reader );
180 if( !psz_attrname || !psz_attrvalue )
182 free( psz_attrname );
183 free( psz_attrvalue );
184 free( psz_eltname );
185 break;
188 GET_VALUE( name )
189 else
191 msg_Warn( p_demux,
192 "unexpected attribute %s in element %s",
193 psz_attrname, psz_eltname );
194 free( psz_attrvalue );
196 free( psz_attrname );
199 free( psz_eltname );
200 break;
203 case XML_READER_TEXT:
204 break;
206 // End element
207 case XML_READER_ENDELEM:
209 // Read the element name
210 char *psz_eltname = xml_ReaderName( p_xml_reader );
211 if( !psz_eltname )
212 goto error;
214 if( !strcmp( psz_eltname, "genre" ) )
216 char* psz_mrl;
217 if( asprintf( &psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
218 psz_name ) != -1 )
220 input_item_t *p_input;
221 p_input = input_item_New( p_demux, psz_mrl, psz_name );
222 input_item_CopyOptions( p_input_node->p_item, p_input );
223 free( psz_mrl );
224 input_item_node_AppendItem( p_input_node, p_input );
225 vlc_gc_decref( p_input );
227 FREENULL( psz_name );
229 free( psz_eltname );
230 break;
234 i_ret = 0;
236 error:
237 free( psz_name );
238 return i_ret;
241 /* radio stations:
242 * <stationlist>
243 * <tunein base="/sbin/tunein-station.pls"></tunein>
244 * <station name="the name"
245 * mt="mime type"
246 * id="the id"
247 * br="bit rate"
248 * genre="A big genre string"
249 * ct="current track name/author/..."
250 * lc="listener count"></station>
251 * </stationlist>
253 * TV stations:
254 * <stationlist>
255 * <tunein base="/sbin/tunein-station.pls"></tunein>
256 * <station name="the name"
257 * id="the id"
258 * br="bit rate"
259 * rt="rating"
260 * load="server load ?"
261 * ct="current track name/author/..."
262 * genre="A big genre string"
263 * lc="listener count"></station>
264 * </stationlist>
266 static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader,
267 input_item_node_t *p_input_node, bool b_adult )
269 char *psz_base = NULL; /* */
271 char *psz_name = NULL; /* genre name */
272 char *psz_mt = NULL; /* mime type */
273 char *psz_id = NULL; /* id */
274 char *psz_br = NULL; /* bit rate */
275 char *psz_genre = NULL; /* genre */
276 char *psz_ct = NULL; /* current track */
277 char *psz_lc = NULL; /* listener count */
279 /* If these are set then it's *not* a radio but a TV */
280 char *psz_rt = NULL; /* rating for shoutcast TV */
281 char *psz_load = NULL; /* load for shoutcast TV */
283 char *psz_eltname = NULL; /* tag name */
285 while( xml_ReaderRead( p_xml_reader ) == 1 )
287 int i_type;
289 // Get the node type
290 i_type = xml_ReaderNodeType( p_xml_reader );
291 switch( i_type )
293 // Error
294 case -1:
295 return -1;
296 break;
298 case XML_READER_STARTELEM:
299 // Read the element name
300 psz_eltname = xml_ReaderName( p_xml_reader );
301 if( !psz_eltname ) return -1;
303 // Read the attributes
304 if( !strcmp( psz_eltname, "tunein" ) )
306 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
308 char *psz_attrname = xml_ReaderName( p_xml_reader );
309 char *psz_attrvalue =
310 xml_ReaderValue( p_xml_reader );
311 if( !psz_attrname || !psz_attrvalue )
313 free( psz_eltname );
314 free( psz_attrname );
315 free( psz_attrvalue );
316 return -1;
319 GET_VALUE( base )
320 else
322 msg_Warn( p_demux,
323 "unexpected attribute %s in element %s",
324 psz_attrname, psz_eltname );
325 free( psz_attrvalue );
327 free( psz_attrname );
330 else if( !strcmp( psz_eltname, "station" ) )
332 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
334 char *psz_attrname = xml_ReaderName( p_xml_reader );
335 char *psz_attrvalue =
336 xml_ReaderValue( p_xml_reader );
337 if( !psz_attrname || !psz_attrvalue )
339 free( psz_eltname );
340 free( psz_attrname );
341 free( psz_attrvalue );
342 return -1;
345 GET_VALUE( name )
346 else GET_VALUE( mt )
347 else GET_VALUE( id )
348 else GET_VALUE( br )
349 else GET_VALUE( genre )
350 else GET_VALUE( ct )
351 else GET_VALUE( lc )
352 else GET_VALUE( rt )
353 else GET_VALUE( load )
354 else
356 msg_Warn( p_demux,
357 "unexpected attribute %s in element %s",
358 psz_attrname, psz_eltname );
359 free( psz_attrvalue );
361 free( psz_attrname );
364 free( psz_eltname );
365 break;
367 case XML_READER_TEXT:
368 break;
370 // End element
371 case XML_READER_ENDELEM:
372 // Read the element name
373 psz_eltname = xml_ReaderName( p_xml_reader );
374 if( !psz_eltname ) return -1;
375 if( !strcmp( psz_eltname, "station" ) &&
376 ( psz_base || ( psz_rt && psz_load &&
377 ( b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
379 char *psz_mrl = NULL;
380 if( psz_rt || psz_load )
382 /* tv */
383 if( asprintf( &psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
384 psz_id ) == -1)
385 psz_mrl = NULL;
387 else
389 /* radio */
390 if( asprintf( &psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
391 psz_base, psz_id ) == -1 )
392 psz_mrl = NULL;
395 /* Create the item */
396 input_item_t *p_input;
397 p_input = input_item_New( p_demux, psz_mrl, psz_name );
398 input_item_CopyOptions( p_input_node->p_item, p_input );
399 free( psz_mrl );
401 #define SADD_INFO( type, field ) \
402 if( field ) \
403 input_item_AddInfo( p_input, _("Shoutcast"), \
404 vlc_gettext(type), "%s", field )
405 SADD_INFO( N_("Mime"), psz_mt );
406 SADD_INFO( N_("Bitrate"), psz_br );
407 SADD_INFO( N_("Listeners"), psz_lc );
408 SADD_INFO( N_("Load"), psz_load );
409 if( psz_genre )
410 input_item_SetGenre( p_input, psz_genre );
411 if( psz_ct )
412 input_item_SetNowPlaying( p_input, psz_ct );
413 if( psz_rt )
414 input_item_SetRating( p_input, psz_rt );
415 input_item_node_AppendItem( p_input_node, p_input );
416 vlc_gc_decref( p_input );
417 FREENULL( psz_base );
418 FREENULL( psz_name );
419 FREENULL( psz_mt );
420 FREENULL( psz_id );
421 FREENULL( psz_br );
422 FREENULL( psz_genre );
423 FREENULL( psz_ct );
424 FREENULL( psz_lc );
425 FREENULL( psz_rt );
426 FREENULL( psz_load );
428 free( psz_eltname );
429 break;
432 return 0;
435 static int Control( demux_t *p_demux, int i_query, va_list args )
437 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
438 return VLC_EGENERIC;