]> git.sesse.net Git - vlc/blob - modules/demux/playlist/shoutcast.c
Translate meta data category properly
[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_New( p_demux, psz_mrl, psz_name );
232                         input_item_CopyOptions( p_sys->p_current_input, p_input );
233                         free( psz_mrl );
234                         input_item_AddSubItem( p_sys->p_current_input, p_input );
235                         vlc_gc_decref( p_input );
236                     }
237                     FREENULL( psz_name );
238                 }
239                 FREENULL( psz_eltname );
240                 break;
241         }
242     }
243     return 0;
244 }
245
246 /* radio stations:
247  * <stationlist>
248  *   <tunein base="/sbin/tunein-station.pls"></tunein>
249  *   <station name="the name"
250  *            mt="mime type"
251  *            id="the id"
252  *            br="bit rate"
253  *            genre="A big genre string"
254  *            ct="current track name/author/..."
255  *            lc="listener count"></station>
256  * </stationlist>
257  *
258  * TV stations:
259  * <stationlist>
260  *   <tunein base="/sbin/tunein-station.pls"></tunein>
261  *   <station name="the name"
262  *            id="the id"
263  *            br="bit rate"
264  *            rt="rating"
265  *            load="server load ?"
266  *            ct="current track name/author/..."
267  *            genre="A big genre string"
268  *            lc="listener count"></station>
269  * </stationlist>
270  **/
271 static int DemuxStation( demux_t *p_demux )
272 {
273     demux_sys_t *p_sys = p_demux->p_sys;
274     input_item_t *p_input;
275
276     char *psz_base = NULL; /* */
277
278     char *psz_name = NULL; /* genre name */
279     char *psz_mt = NULL; /* mime type */
280     char *psz_id = NULL; /* id */
281     char *psz_br = NULL; /* bit rate */
282     char *psz_genre = NULL; /* genre */
283     char *psz_ct = NULL; /* current track */
284     char *psz_lc = NULL; /* listener count */
285
286     /* If these are set then it's *not* a radio but a TV */
287     char *psz_rt = NULL; /* rating for shoutcast TV */
288     char *psz_load = NULL; /* load for shoutcast TV */
289
290     char *psz_eltname = NULL; /* tag name */
291
292     while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
293     {
294         int i_type;
295
296         // Get the node type
297         i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
298         switch( i_type )
299         {
300             // Error
301             case -1:
302                 return -1;
303                 break;
304
305             case XML_READER_STARTELEM:
306                 // Read the element name
307                 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
308                 if( !psz_eltname ) return -1;
309
310                 // Read the attributes
311                 if( !strcmp( psz_eltname, "tunein" ) )
312                 {
313                     while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
314                     {
315                         char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
316                         char *psz_attrvalue =
317                             xml_ReaderValue( p_sys->p_xml_reader );
318                         if( !psz_attrname || !psz_attrvalue )
319                         {
320                             free( psz_eltname );
321                             free( psz_attrname );
322                             free( psz_attrvalue );
323                             return -1;
324                         }
325
326                         GET_VALUE( base )
327                         else
328                         {
329                             msg_Warn( p_demux,
330                                       "unexpected attribure %s in element %s",
331                                       psz_attrname, psz_eltname );
332                         }
333                         free( psz_attrname );
334                         free( psz_attrvalue );
335                     }
336                 }
337                 else if( !strcmp( psz_eltname, "station" ) )
338                 {
339                     while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
340                     {
341                         char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
342                         char *psz_attrvalue =
343                             xml_ReaderValue( p_sys->p_xml_reader );
344                         if( !psz_attrname || !psz_attrvalue )
345                         {
346                             free( psz_eltname );
347                             free( psz_attrname );
348                             free( psz_attrvalue );
349                             return -1;
350                         }
351
352                         GET_VALUE( name )
353                         else GET_VALUE( mt )
354                         else GET_VALUE( id )
355                         else GET_VALUE( br )
356                         else GET_VALUE( genre )
357                         else GET_VALUE( ct )
358                         else GET_VALUE( lc )
359                         else GET_VALUE( rt )
360                         else GET_VALUE( load )
361                         else
362                         {
363                             msg_Warn( p_demux,
364                                       "unexpected attribute %s in element %s",
365                                       psz_attrname, psz_eltname );
366                         }
367                         free( psz_attrname );
368                         free( psz_attrvalue );
369                     }
370                 }
371                 free( psz_eltname );
372                 break;
373
374             case XML_READER_TEXT:
375                 break;
376
377             // End element
378             case XML_READER_ENDELEM:
379                 // Read the element name
380                 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
381                 if( !psz_eltname ) return -1;
382                 if( !strcmp( psz_eltname, "station" ) &&
383                     ( psz_base || ( psz_rt && psz_load &&
384                     ( p_sys->b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
385                 {
386                     char *psz_mrl = NULL;
387                     if( psz_rt || psz_load )
388                     {
389                         /* tv */
390                         if( asprintf( &psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
391                                  psz_id ) == -1)
392                             psz_mrl = NULL;
393                     }
394                     else
395                     {
396                         /* radio */
397                         if( asprintf( &psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
398                              psz_base, psz_id ) == -1 )
399                             psz_mrl = NULL;
400                     }
401                     p_input = input_item_New( p_demux, psz_mrl, psz_name );
402                     input_item_CopyOptions( p_sys->p_current_input, p_input );
403                     free( psz_mrl );
404
405 #define SADD_INFO( type, field ) if( field ) { input_item_AddInfo( \
406                     p_input, _("Shoutcast"), gettext(type), "%s", field ) ; }
407                     SADD_INFO( N_("Mime"), psz_mt );
408                     SADD_INFO( N_("Bitrate"), psz_br );
409                     SADD_INFO( N_("Listeners"), psz_lc );
410                     SADD_INFO( N_("Load"), psz_load );
411                     if( psz_genre )
412                         input_item_SetGenre( p_input, psz_genre );
413                     if( psz_ct )
414                         input_item_SetNowPlaying( p_input, psz_ct );
415                     if( psz_rt )
416                         input_item_SetRating( p_input, psz_rt );
417                     input_item_AddSubItem( p_sys->p_current_input, p_input );
418                     vlc_gc_decref( p_input );
419                     FREENULL( psz_name );
420                     FREENULL( psz_mt );
421                     FREENULL( psz_id );
422                     FREENULL( psz_br );
423                     FREENULL( psz_genre );
424                     FREENULL( psz_ct );
425                     FREENULL( psz_lc );
426                     FREENULL( psz_rt );
427                 }
428                 free( psz_eltname );
429                 break;
430         }
431     }
432     return 0;
433 }
434
435 static int Control( demux_t *p_demux, int i_query, va_list args )
436 {
437     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
438     return VLC_EGENERIC;
439 }