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