]> git.sesse.net Git - vlc/blob - modules/demux/playlist/shoutcast.c
c007e9d1d0a0527dc0e45302208bdeb1f2f5f381
[vlc] / modules / demux / playlist / shoutcast.c
1 /*****************************************************************************
2  * shoutcast.c: Winamp >=5.2 shoutcast demuxer
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -@t- videolan -Dot- org>
8  *          based on b4s.c by Sigmund Augdal Helberg <dnumgis@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
35
36 #include "playlist.h"
37 #include <vlc_xml.h>
38
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="
43
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 );
49
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 );
54
55 /*****************************************************************************
56  * Import_Shoutcast: main import function
57  *****************************************************************************/
58 int Import_Shoutcast( vlc_object_t *p_this )
59 {
60     demux_t *p_demux = (demux_t *)p_this;
61
62     if( !demux_IsForced( p_demux, "shout-winamp" ) )
63         return VLC_EGENERIC;
64
65     p_demux->pf_demux = Demux;
66     p_demux->pf_control = Control;
67     msg_Dbg( p_demux, "using shoutcast playlist reader" );
68
69     return VLC_SUCCESS;
70 }
71
72 /*****************************************************************************
73  * Deactivate: frees unused data
74  *****************************************************************************/
75 void Close_Shoutcast( vlc_object_t *p_this )
76 {
77     (void)p_this;
78 }
79
80 static int Demux( demux_t *p_demux )
81 {
82     xml_t *p_xml;
83     xml_reader_t *p_xml_reader = NULL;
84     char *psz_eltname = NULL;
85     int i_ret = -1;
86     input_item_t *p_current_input = GetCurrentItem(p_demux);
87
88     p_xml = xml_Create( p_demux );
89     if( !p_xml )
90         goto error;
91
92     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
93     if( !p_xml_reader )
94         goto error;
95
96     /* check root node */
97     if( xml_ReaderRead( p_xml_reader ) != 1 )
98     {
99         msg_Err( p_demux, "invalid file (no root node)" );
100         goto error;
101     }
102
103     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
104         ( psz_eltname = xml_ReaderName( p_xml_reader ) ) == NULL ||
105         ( strcmp( psz_eltname, "genrelist" )
106           && strcmp( psz_eltname, "stationlist" ) ) )
107     {
108         msg_Err( p_demux, "invalid root node %i, %s",
109                  xml_ReaderNodeType( p_xml_reader ), psz_eltname );
110         goto error;
111     }
112
113     input_item_node_t *p_input_node = input_item_node_Create( p_current_input );
114
115     if( !strcmp( psz_eltname, "genrelist" ) )
116     {
117         /* we're reading a genre list */
118         if( DemuxGenre( p_demux, p_xml_reader, p_input_node ) )
119             goto error;
120     }
121     else
122     {
123         /* we're reading a station list */
124         if( DemuxStation( p_demux, p_xml_reader, p_input_node,
125                 var_CreateGetBool( p_demux, "shoutcast-show-adult" ) ) )
126             goto error;
127     }
128
129     input_item_AddSubItemTree( p_input_node );
130
131     i_ret = 0; /* Needed for correct operation of go back */
132
133 error:
134     if( p_xml_reader )
135         xml_ReaderDelete( p_xml, p_xml_reader );
136     if( p_xml )
137         xml_Delete( p_xml );
138     free( psz_eltname );
139     input_item_node_Delete( p_input_node );
140     vlc_gc_decref(p_current_input);
141     return i_ret;
142 }
143
144 #define GET_VALUE( a ) \
145                         if( !strcmp( psz_attrname, #a ) ) \
146                         { \
147                             free(psz_ ## a); \
148                             psz_ ## a = psz_attrvalue; \
149                         }
150 /* <genrelist>
151  *   <genre name="the name"></genre>
152  *   ...
153  * </genrelist>
154  **/
155 static int DemuxGenre( demux_t *p_demux, xml_reader_t *p_xml_reader,
156                        input_item_node_t *p_input_node )
157 {
158     char *psz_name = NULL; /* genre name */
159     int i_ret = -1;
160
161     while( xml_ReaderRead( p_xml_reader ) == 1 )
162     {
163         // Get the node type
164         switch( xml_ReaderNodeType( p_xml_reader ) )
165         {
166             // Error
167             case -1:
168                 goto error;
169
170             case XML_READER_STARTELEM:
171             {
172                 // Read the element name
173                 char *psz_eltname = xml_ReaderName( p_xml_reader );
174                 if( !psz_eltname )
175                     goto error;
176
177                 if( !strcmp( psz_eltname, "genre" ) )
178                 {
179                     // Read the attributes
180                     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
181                     {
182                         char *psz_attrname = xml_ReaderName( p_xml_reader );
183                         char *psz_attrvalue =
184                             xml_ReaderValue( p_xml_reader );
185                         if( !psz_attrname || !psz_attrvalue )
186                         {
187                             free( psz_attrname );
188                             free( psz_attrvalue );
189                             free( psz_eltname );
190                             break;
191                         }
192
193                         GET_VALUE( name )
194                         else
195                         {
196                             msg_Warn( p_demux,
197                                       "unexpected attribure %s in element %s",
198                                       psz_attrname, psz_eltname );
199                             free( psz_attrvalue );
200                         }
201                         free( psz_attrname );
202                     }
203                 }
204                 free( psz_eltname );
205                 break;
206             }
207
208             case XML_READER_TEXT:
209                 break;
210
211             // End element
212             case XML_READER_ENDELEM:
213             {
214                 // Read the element name
215                 char *psz_eltname = xml_ReaderName( p_xml_reader );
216                 if( !psz_eltname )
217                     goto error;
218
219                 if( !strcmp( psz_eltname, "genre" ) )
220                 {
221                     char* psz_mrl;
222                     if( asprintf( &psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
223                              psz_name ) != -1 )
224                     {
225                         input_item_t *p_input;
226                         p_input = input_item_New( p_demux, psz_mrl, psz_name );
227                         input_item_CopyOptions( p_input_node->p_item, p_input );
228                         free( psz_mrl );
229                         input_item_AddSubItem( p_input_node->p_item, p_input );
230                         input_item_node_AppendItem( p_input_node, p_input );
231                         vlc_gc_decref( p_input );
232                     }
233                     FREENULL( psz_name );
234                 }
235                 free( psz_eltname );
236                 break;
237             }
238         }
239     }
240     i_ret = 0;
241
242 error:
243     free( psz_name );
244     return i_ret;
245 }
246
247 /* radio stations:
248  * <stationlist>
249  *   <tunein base="/sbin/tunein-station.pls"></tunein>
250  *   <station name="the name"
251  *            mt="mime type"
252  *            id="the id"
253  *            br="bit rate"
254  *            genre="A big genre string"
255  *            ct="current track name/author/..."
256  *            lc="listener count"></station>
257  * </stationlist>
258  *
259  * TV stations:
260  * <stationlist>
261  *   <tunein base="/sbin/tunein-station.pls"></tunein>
262  *   <station name="the name"
263  *            id="the id"
264  *            br="bit rate"
265  *            rt="rating"
266  *            load="server load ?"
267  *            ct="current track name/author/..."
268  *            genre="A big genre string"
269  *            lc="listener count"></station>
270  * </stationlist>
271  **/
272 static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader,
273                          input_item_node_t *p_input_node, bool b_adult )
274 {
275     char *psz_base = NULL; /* */
276
277     char *psz_name = NULL; /* genre name */
278     char *psz_mt = NULL; /* mime type */
279     char *psz_id = NULL; /* id */
280     char *psz_br = NULL; /* bit rate */
281     char *psz_genre = NULL; /* genre */
282     char *psz_ct = NULL; /* current track */
283     char *psz_lc = NULL; /* listener count */
284
285     /* If these are set then it's *not* a radio but a TV */
286     char *psz_rt = NULL; /* rating for shoutcast TV */
287     char *psz_load = NULL; /* load for shoutcast TV */
288
289     char *psz_eltname = NULL; /* tag name */
290
291     while( xml_ReaderRead( p_xml_reader ) == 1 )
292     {
293         int i_type;
294
295         // Get the node type
296         i_type = xml_ReaderNodeType( p_xml_reader );
297         switch( i_type )
298         {
299             // Error
300             case -1:
301                 return -1;
302                 break;
303
304             case XML_READER_STARTELEM:
305                 // Read the element name
306                 psz_eltname = xml_ReaderName( p_xml_reader );
307                 if( !psz_eltname ) return -1;
308
309                 // Read the attributes
310                 if( !strcmp( psz_eltname, "tunein" ) )
311                 {
312                     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
313                     {
314                         char *psz_attrname = xml_ReaderName( p_xml_reader );
315                         char *psz_attrvalue =
316                             xml_ReaderValue( p_xml_reader );
317                         if( !psz_attrname || !psz_attrvalue )
318                         {
319                             free( psz_eltname );
320                             free( psz_attrname );
321                             free( psz_attrvalue );
322                             return -1;
323                         }
324
325                         GET_VALUE( base )
326                         else
327                         {
328                             msg_Warn( p_demux,
329                                       "unexpected attribure %s in element %s",
330                                       psz_attrname, psz_eltname );
331                             free( psz_attrvalue );
332                         }
333                         free( psz_attrname );
334                     }
335                 }
336                 else if( !strcmp( psz_eltname, "station" ) )
337                 {
338                     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
339                     {
340                         char *psz_attrname = xml_ReaderName( p_xml_reader );
341                         char *psz_attrvalue =
342                             xml_ReaderValue( p_xml_reader );
343                         if( !psz_attrname || !psz_attrvalue )
344                         {
345                             free( psz_eltname );
346                             free( psz_attrname );
347                             free( psz_attrvalue );
348                             return -1;
349                         }
350
351                         GET_VALUE( name )
352                         else GET_VALUE( mt )
353                         else GET_VALUE( id )
354                         else GET_VALUE( br )
355                         else GET_VALUE( genre )
356                         else GET_VALUE( ct )
357                         else GET_VALUE( lc )
358                         else GET_VALUE( rt )
359                         else GET_VALUE( load )
360                         else
361                         {
362                             msg_Warn( p_demux,
363                                       "unexpected attribute %s in element %s",
364                                       psz_attrname, psz_eltname );
365                             free( psz_attrvalue );
366                         }
367                         free( psz_attrname );
368                     }
369                 }
370                 free( psz_eltname );
371                 break;
372
373             case XML_READER_TEXT:
374                 break;
375
376             // End element
377             case XML_READER_ENDELEM:
378                 // Read the element name
379                 psz_eltname = xml_ReaderName( p_xml_reader );
380                 if( !psz_eltname ) return -1;
381                 if( !strcmp( psz_eltname, "station" ) &&
382                     ( psz_base || ( psz_rt && psz_load &&
383                     ( b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
384                 {
385                     char *psz_mrl = NULL;
386                     if( psz_rt || psz_load )
387                     {
388                         /* tv */
389                         if( asprintf( &psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
390                                  psz_id ) == -1)
391                             psz_mrl = NULL;
392                     }
393                     else
394                     {
395                         /* radio */
396                         if( asprintf( &psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
397                              psz_base, psz_id ) == -1 )
398                             psz_mrl = NULL;
399                     }
400
401                     /* Create the item */
402                     input_item_t *p_input;
403                     p_input = input_item_New( p_demux, psz_mrl, psz_name );
404                     input_item_CopyOptions( p_input_node->p_item, p_input );
405                     free( psz_mrl );
406
407 #define SADD_INFO( type, field ) \
408                     if( field ) \
409                         input_item_AddInfo( p_input, _("Shoutcast"), \
410                                             vlc_gettext(type), "%s", field )
411                     SADD_INFO( N_("Mime"), psz_mt );
412                     SADD_INFO( N_("Bitrate"), psz_br );
413                     SADD_INFO( N_("Listeners"), psz_lc );
414                     SADD_INFO( N_("Load"), psz_load );
415                     if( psz_genre )
416                         input_item_SetGenre( p_input, psz_genre );
417                     if( psz_ct )
418                         input_item_SetNowPlaying( p_input, psz_ct );
419                     if( psz_rt )
420                         input_item_SetRating( p_input, psz_rt );
421                     input_item_AddSubItem( p_input_node->p_item, p_input );
422                     input_item_node_AppendItem( p_input_node, p_input );
423                     vlc_gc_decref( p_input );
424                     FREENULL( psz_base );
425                     FREENULL( psz_name );
426                     FREENULL( psz_mt );
427                     FREENULL( psz_id );
428                     FREENULL( psz_br );
429                     FREENULL( psz_genre );
430                     FREENULL( psz_ct );
431                     FREENULL( psz_lc );
432                     FREENULL( psz_rt );
433                     FREENULL( psz_load );
434                 }
435                 free( psz_eltname );
436                 break;
437         }
438     }
439     return 0;
440 }
441
442 static int Control( demux_t *p_demux, int i_query, va_list args )
443 {
444     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
445     return VLC_EGENERIC;
446 }