]> git.sesse.net Git - vlc/blob - modules/demux/playlist/shoutcast.c
d3686c13e2792db82897e81ce427c980398c885a
[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 struct demux_sys_t
40 {
41     input_item_t *p_current_input;
42
43     xml_t *p_xml;
44     xml_reader_t *p_xml_reader;
45
46     bool b_adult;
47 };
48
49 /* duplicate from modules/services_discovery/shout.c */
50 #define SHOUTCAST_BASE_URL "http/shout-winamp://www.shoutcast.com/sbin/newxml.phtml"
51 #define SHOUTCAST_TUNEIN_BASE_URL "http://www.shoutcast.com"
52 #define SHOUTCAST_TV_TUNEIN_URL "http://www.shoutcast.com/sbin/tunein-tvstation.pls?id="
53
54 /*****************************************************************************
55  * Local prototypes
56  *****************************************************************************/
57 static int Demux( demux_t *p_demux);
58 static int Control( demux_t *p_demux, int i_query, va_list args );
59
60 static int DemuxGenre( demux_t *p_demux );
61 static int DemuxStation( demux_t *p_demux );
62
63 /*****************************************************************************
64  * Import_Shoutcast: main import function
65  *****************************************************************************/
66 int Import_Shoutcast( vlc_object_t *p_this )
67 {
68     demux_t *p_demux = (demux_t *)p_this;
69
70     if( !demux_IsForced( p_demux, "shout-winamp" ) )
71         return VLC_EGENERIC;
72
73     STANDARD_DEMUX_INIT_MSG( "using shoutcast playlist reader" );
74     p_demux->p_sys->p_xml = NULL;
75     p_demux->p_sys->p_xml_reader = NULL;
76
77     /* Do we want to list adult content ? */
78     var_Create( p_demux, "shoutcast-show-adult",
79                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
80     p_demux->p_sys->b_adult = var_GetBool( p_demux, "shoutcast-show-adult" );
81
82     return VLC_SUCCESS;
83 }
84
85 /*****************************************************************************
86  * Deactivate: frees unused data
87  *****************************************************************************/
88 void Close_Shoutcast( vlc_object_t *p_this )
89 {
90     demux_t *p_demux = (demux_t *)p_this;
91     demux_sys_t *p_sys = p_demux->p_sys;
92
93     if( p_sys->p_xml_reader )
94         xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
95     if( p_sys->p_xml )
96         xml_Delete( p_sys->p_xml );
97     free( p_sys );
98 }
99
100 static int Demux( demux_t *p_demux )
101 {
102     demux_sys_t *p_sys = p_demux->p_sys;
103     xml_t *p_xml;
104     xml_reader_t *p_xml_reader;
105     char *psz_eltname = NULL;
106     INIT_PLAYLIST_STUFF;
107     p_sys->p_current_input = p_current_input;
108
109     p_xml = p_sys->p_xml = xml_Create( p_demux );
110     if( !p_xml ) return -1;
111
112     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
113     if( !p_xml_reader ) return -1;
114     p_sys->p_xml_reader = p_xml_reader;
115
116     /* check root node */
117     if( xml_ReaderRead( p_xml_reader ) != 1 )
118     {
119         msg_Err( p_demux, "invalid file (no root node)" );
120         return -1;
121     }
122
123     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
124         ( psz_eltname = xml_ReaderName( p_xml_reader ) ) == NULL ||
125         ( strcmp( psz_eltname, "genrelist" )
126           && strcmp( psz_eltname, "stationlist" ) ) )
127     {
128         msg_Err( p_demux, "invalid root node %i, %s",
129                  xml_ReaderNodeType( p_xml_reader ), psz_eltname );
130         free( psz_eltname );
131         return -1;
132     }
133
134     if( !strcmp( psz_eltname, "genrelist" ) )
135     {
136         /* we're reading a genre list */
137         free( psz_eltname );
138         if( DemuxGenre( p_demux ) ) return -1;
139     }
140     else
141     {
142         /* we're reading a station list */
143         free( psz_eltname );
144         if( DemuxStation( p_demux ) ) return -1;
145     }
146
147     HANDLE_PLAY_AND_RELEASE;
148     return 0; /* Needed for correct operation of go back */
149 }
150
151 #define GET_VALUE( a ) \
152                         if( !strcmp( psz_attrname, #a ) ) \
153                         { \
154                             psz_ ## a = strdup( psz_attrvalue ); \
155                         }
156 /* <genrelist>
157  *   <genre name="the name"></genre>
158  *   ...
159  * </genrelist>
160  **/
161 static int DemuxGenre( demux_t *p_demux )
162 {
163     demux_sys_t *p_sys = p_demux->p_sys;
164     char *psz_name = NULL; /* genre name */
165     char *psz_eltname = NULL; /* tag name */
166     input_item_t *p_input;
167
168     while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
169     {
170         int i_type;
171
172         // Get the node type
173         i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
174         switch( i_type )
175         {
176             // Error
177             case -1:
178                 return -1;
179                 break;
180
181             case XML_READER_STARTELEM:
182                 // Read the element name
183                 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
184                 if( !psz_eltname ) return -1;
185
186                 if( !strcmp( psz_eltname, "genre" ) )
187                 {
188                     // Read the attributes
189                     while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
190                     {
191                         char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
192                         char *psz_attrvalue =
193                             xml_ReaderValue( p_sys->p_xml_reader );
194                         if( !psz_attrname || !psz_attrvalue )
195                         {
196                             FREENULL(psz_attrname);
197                             FREENULL(psz_attrvalue);
198                             free(psz_eltname);
199                             /*FIXME: isn't return a bit too much. what about break*/
200                             return -1;
201                         }
202
203                         GET_VALUE( name )
204                         else
205                         {
206                             msg_Warn( p_demux,
207                                       "unexpected attribure %s in element %s",
208                                       psz_attrname,psz_eltname );
209                         }
210                         free( psz_attrname );
211                         free( psz_attrvalue );
212                     }
213                 }
214                 free( psz_eltname ); psz_eltname = NULL;
215                 break;
216
217             case XML_READER_TEXT:
218                 break;
219
220             // End element
221             case XML_READER_ENDELEM:
222                 // Read the element name
223                 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
224                 if( !psz_eltname ) return -1;
225                 if( !strcmp( psz_eltname, "genre" ) )
226                 {
227                     char* psz_mrl;
228                     if( asprintf( &psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
229                              psz_name ) != -1 )
230                     {
231                         p_input = input_item_NewExt( p_demux, psz_mrl,
232                                                     psz_name, 0, NULL, -1 );
233                         input_item_CopyOptions( p_sys->p_current_input, p_input );
234                         free( psz_mrl );
235                         input_item_AddSubItem( p_sys->p_current_input, p_input );
236                         vlc_gc_decref( p_input );
237                     }
238                     FREENULL( psz_name );
239                 }
240                 FREENULL( psz_eltname );
241                 break;
242         }
243     }
244     return 0;
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 )
273 {
274     demux_sys_t *p_sys = p_demux->p_sys;
275     input_item_t *p_input;
276
277     char *psz_base = NULL; /* */
278
279     char *psz_name = NULL; /* genre name */
280     char *psz_mt = NULL; /* mime type */
281     char *psz_id = NULL; /* id */
282     char *psz_br = NULL; /* bit rate */
283     char *psz_genre = NULL; /* genre */
284     char *psz_ct = NULL; /* current track */
285     char *psz_lc = NULL; /* listener count */
286
287     /* If these are set then it's *not* a radio but a TV */
288     char *psz_rt = NULL; /* rating for shoutcast TV */
289     char *psz_load = NULL; /* load for shoutcast TV */
290
291     char *psz_eltname = NULL; /* tag name */
292
293     while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
294     {
295         int i_type;
296
297         // Get the node type
298         i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
299         switch( i_type )
300         {
301             // Error
302             case -1:
303                 return -1;
304                 break;
305
306             case XML_READER_STARTELEM:
307                 // Read the element name
308                 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
309                 if( !psz_eltname ) return -1;
310
311                 // Read the attributes
312                 if( !strcmp( psz_eltname, "tunein" ) )
313                 {
314                     while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
315                     {
316                         char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
317                         char *psz_attrvalue =
318                             xml_ReaderValue( p_sys->p_xml_reader );
319                         if( !psz_attrname || !psz_attrvalue )
320                         {
321                             free( psz_eltname );
322                             free( psz_attrname );
323                             free( psz_attrvalue );
324                             return -1;
325                         }
326
327                         GET_VALUE( base )
328                         else
329                         {
330                             msg_Warn( p_demux,
331                                       "unexpected attribure %s in element %s",
332                                       psz_attrname, psz_eltname );
333                         }
334                         free( psz_attrname );
335                         free( psz_attrvalue );
336                     }
337                 }
338                 else if( !strcmp( psz_eltname, "station" ) )
339                 {
340                     while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
341                     {
342                         char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
343                         char *psz_attrvalue =
344                             xml_ReaderValue( p_sys->p_xml_reader );
345                         if( !psz_attrname || !psz_attrvalue )
346                         {
347                             free( psz_eltname );
348                             free( psz_attrname );
349                             free( psz_attrvalue );
350                             return -1;
351                         }
352
353                         GET_VALUE( name )
354                         else GET_VALUE( mt )
355                         else GET_VALUE( id )
356                         else GET_VALUE( br )
357                         else GET_VALUE( genre )
358                         else GET_VALUE( ct )
359                         else GET_VALUE( lc )
360                         else GET_VALUE( rt )
361                         else GET_VALUE( load )
362                         else
363                         {
364                             msg_Warn( p_demux,
365                                       "unexpected attribute %s in element %s",
366                                       psz_attrname, psz_eltname );
367                         }
368                         free( psz_attrname );
369                         free( psz_attrvalue );
370                     }
371                 }
372                 free( psz_eltname );
373                 break;
374
375             case XML_READER_TEXT:
376                 break;
377
378             // End element
379             case XML_READER_ENDELEM:
380                 // Read the element name
381                 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
382                 if( !psz_eltname ) return -1;
383                 if( !strcmp( psz_eltname, "station" ) &&
384                     ( psz_base || ( psz_rt && psz_load &&
385                     ( p_sys->b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
386                 {
387                     char *psz_mrl = NULL;
388                     if( psz_rt || psz_load )
389                     {
390                         /* tv */
391                         if( asprintf( &psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
392                                  psz_id ) == -1)
393                             psz_mrl = NULL;
394                     }
395                     else
396                     {
397                         /* radio */
398                         if( asprintf( &psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
399                              psz_base, psz_id ) == -1 )
400                             psz_mrl = NULL;
401                     }
402                     p_input = input_item_NewExt( p_demux, psz_mrl,
403                                                 psz_name , 0, NULL, -1 );
404                     free( psz_mrl );
405
406                     input_item_CopyOptions( p_sys->p_current_input,
407                                                 p_input );
408
409 #define SADD_INFO( type, field ) if( field ) { input_item_AddInfo( \
410                     p_input, _("Shoutcast"), _(type), "%s", field ) ; }
411                     SADD_INFO( "Mime type", psz_mt );
412                     SADD_INFO( "Bitrate", psz_br );
413                     SADD_INFO( "Listeners", psz_lc );
414                     SADD_INFO( "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_sys->p_current_input, p_input );
422                     vlc_gc_decref( p_input );
423                     FREENULL( psz_name );
424                     FREENULL( psz_mt );
425                     FREENULL( psz_id );
426                     FREENULL( psz_br );
427                     FREENULL( psz_genre );
428                     FREENULL( psz_ct );
429                     FREENULL( psz_lc );
430                     FREENULL( psz_rt );
431                 }
432                 free( psz_eltname );
433                 break;
434         }
435     }
436     return 0;
437 }
438
439 static int Control( demux_t *p_demux, int i_query, va_list args )
440 {
441     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
442     return VLC_EGENERIC;
443 }