shoutcast.c 15.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*****************************************************************************
 * shoutcast.c: Winamp >=5.2 shoutcast demuxer
 *****************************************************************************
 * Copyright (C) 2006 the VideoLAN team
 * $Id$
 *
 * Authors: Antoine Cellerier <dionoea -@t- videolan -Dot- org>
 *          based on b4s.c by Sigmund Augdal Helberg <dnumgis@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

29
30
31
32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <vlc_common.h>
zorglub's avatar
zorglub committed
34
#include <vlc_demux.h>
35
36

#include "playlist.h"
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
37
#include <vlc_xml.h>
38
39
40

struct demux_sys_t
{
41
    input_item_t *p_current_input;
42
43
44

    xml_t *p_xml;
    xml_reader_t *p_xml_reader;
45

46
    bool b_adult;
47
48
49
50
51
};

/* duplicate from modules/services_discovery/shout.c */
#define SHOUTCAST_BASE_URL "http/shout-winamp://www.shoutcast.com/sbin/newxml.phtml"
#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
56
57
58
59
60
61
62
63
64
65

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int Demux( demux_t *p_demux);
static int Control( demux_t *p_demux, int i_query, va_list args );

static int DemuxGenre( demux_t *p_demux );
static int DemuxStation( demux_t *p_demux );

/*****************************************************************************
 * Import_Shoutcast: main import function
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
66
int Import_Shoutcast( vlc_object_t *p_this )
67
68
69
{
    demux_t *p_demux = (demux_t *)p_this;

70
    if( !demux_IsForced( p_demux, "shout-winamp" ) )
71
        return VLC_EGENERIC;
72

73
74
75
    STANDARD_DEMUX_INIT_MSG( "using shoutcast playlist reader" );
    p_demux->p_sys->p_xml = NULL;
    p_demux->p_sys->p_xml_reader = NULL;
76

77
78
79
    /* Do we want to list adult content ? */
    var_Create( p_demux, "shoutcast-show-adult",
                VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
80
    p_demux->p_sys->b_adult = var_GetBool( p_demux, "shoutcast-show-adult" );
81

82
83
84
85
86
87
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Deactivate: frees unused data
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
88
void Close_Shoutcast( vlc_object_t *p_this )
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{
    demux_t *p_demux = (demux_t *)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;

    if( p_sys->p_xml_reader )
        xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
    if( p_sys->p_xml )
        xml_Delete( p_sys->p_xml );
    free( p_sys );
}

static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    xml_t *p_xml;
    xml_reader_t *p_xml_reader;
    char *psz_eltname = NULL;
106
    INIT_PLAYLIST_STUFF;
107
    p_sys->p_current_input = p_current_input;
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129

    p_xml = p_sys->p_xml = xml_Create( p_demux );
    if( !p_xml ) return -1;

    p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
    if( !p_xml_reader ) return -1;
    p_sys->p_xml_reader = p_xml_reader;

    /* check root node */
    if( xml_ReaderRead( p_xml_reader ) != 1 )
    {
        msg_Err( p_demux, "invalid file (no root node)" );
        return -1;
    }

    if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
        ( psz_eltname = xml_ReaderName( p_xml_reader ) ) == NULL ||
        ( strcmp( psz_eltname, "genrelist" )
          && strcmp( psz_eltname, "stationlist" ) ) )
    {
        msg_Err( p_demux, "invalid root node %i, %s",
                 xml_ReaderNodeType( p_xml_reader ), psz_eltname );
130
        free( psz_eltname );
131
132
133
134
135
136
        return -1;
    }

    if( !strcmp( psz_eltname, "genrelist" ) )
    {
        /* we're reading a genre list */
dionoea's avatar
dionoea committed
137
138
        free( psz_eltname );
        if( DemuxGenre( p_demux ) ) return -1;
139
140
141
142
    }
    else
    {
        /* we're reading a station list */
dionoea's avatar
dionoea committed
143
144
        free( psz_eltname );
        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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
}

#define GET_VALUE( a ) \
                        if( !strcmp( psz_attrname, #a ) ) \
                        { \
                            psz_ ## a = strdup( psz_attrvalue ); \
                        }
/* <genrelist>
 *   <genre name="the name"></genre>
 *   ...
 * </genrelist>
 **/
static int DemuxGenre( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    char *psz_name = NULL; /* genre name */
    char *psz_eltname = NULL; /* tag name */
166
    input_item_t *p_input;
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

    while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
    {
        int i_type;

        // Get the node type
        i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
        switch( i_type )
        {
            // Error
            case -1:
                return -1;
                break;

            case XML_READER_STARTELEM:
                // Read the element name
                psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
                if( !psz_eltname ) return -1;

                if( !strcmp( psz_eltname, "genre" ) )
                {
                    // Read the attributes
                    while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
                    {
                        char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
                        char *psz_attrvalue =
                            xml_ReaderValue( p_sys->p_xml_reader );
194
195
                        if( !psz_attrname || !psz_attrvalue )
                        {
zorglub's avatar
zorglub committed
196
197
                            FREENULL(psz_attrname);
                            FREENULL(psz_attrvalue);
198
199
200
201
                            free(psz_eltname);
                            /*FIXME: isn't return a bit too much. what about break*/
                            return -1;
                        }
202
203
204
205
206
207

                        GET_VALUE( name )
                        else
                        {
                            msg_Warn( p_demux,
                                      "unexpected attribure %s in element %s",
208
                                      psz_attrname,psz_eltname );
209
210
211
212
213
                        }
                        free( psz_attrname );
                        free( psz_attrvalue );
                    }
                }
214
                free( psz_eltname ); psz_eltname = NULL;
215
216
217
218
219
220
221
222
223
224
225
226
                break;

            case XML_READER_TEXT:
                break;

            // End element
            case XML_READER_ENDELEM:
                // Read the element name
                psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
                if( !psz_eltname ) return -1;
                if( !strcmp( psz_eltname, "genre" ) )
                {
227
228
229
230
                    char* psz_mrl;
                    if( asprintf( &psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
                             psz_name ) != -1 )
                    {
231
                        p_input = input_item_New( p_demux, psz_mrl, psz_name );
ivoire's avatar
ivoire committed
232
                        input_item_CopyOptions( p_sys->p_current_input, p_input );
233
                        free( psz_mrl );
ivoire's avatar
ivoire committed
234
                        input_item_AddSubItem( p_sys->p_current_input, p_input );
235
236
                        vlc_gc_decref( p_input );
                    }
zorglub's avatar
zorglub committed
237
                    FREENULL( psz_name );
238
                }
zorglub's avatar
zorglub committed
239
                FREENULL( psz_eltname );
240
241
242
243
244
245
                break;
        }
    }
    return 0;
}

246
247
/* radio stations:
 * <stationlist>
248
249
250
251
252
253
254
255
256
 *   <tunein base="/sbin/tunein-station.pls"></tunein>
 *   <station name="the name"
 *            mt="mime type"
 *            id="the id"
 *            br="bit rate"
 *            genre="A big genre string"
 *            ct="current track name/author/..."
 *            lc="listener count"></station>
 * </stationlist>
257
258
259
260
261
262
263
264
265
266
267
268
269
 *
 * TV stations:
 * <stationlist>
 *   <tunein base="/sbin/tunein-station.pls"></tunein>
 *   <station name="the name"
 *            id="the id"
 *            br="bit rate"
 *            rt="rating"
 *            load="server load ?"
 *            ct="current track name/author/..."
 *            genre="A big genre string"
 *            lc="listener count"></station>
 * </stationlist>
270
271
272
273
 **/
static int DemuxStation( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
274
    input_item_t *p_input;
275
276
277
278
279
280
281
282
283
284
285

    char *psz_base = NULL; /* */

    char *psz_name = NULL; /* genre name */
    char *psz_mt = NULL; /* mime type */
    char *psz_id = NULL; /* id */
    char *psz_br = NULL; /* bit rate */
    char *psz_genre = NULL; /* genre */
    char *psz_ct = NULL; /* current track */
    char *psz_lc = NULL; /* listener count */

286
287
288
289
    /* If these are set then it's *not* a radio but a TV */
    char *psz_rt = NULL; /* rating for shoutcast TV */
    char *psz_load = NULL; /* load for shoutcast TV */

290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
    char *psz_eltname = NULL; /* tag name */

    while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
    {
        int i_type;

        // Get the node type
        i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
        switch( i_type )
        {
            // Error
            case -1:
                return -1;
                break;

            case XML_READER_STARTELEM:
                // Read the element name
                psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
                if( !psz_eltname ) return -1;

                // Read the attributes
                if( !strcmp( psz_eltname, "tunein" ) )
                {
                    while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
                    {
                        char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
                        char *psz_attrvalue =
                            xml_ReaderValue( p_sys->p_xml_reader );
318
319
                        if( !psz_attrname || !psz_attrvalue )
                        {
320
321
322
                            free( psz_eltname );
                            free( psz_attrname );
                            free( psz_attrvalue );
323
324
                            return -1;
                        }
325
326
327
328
329
330

                        GET_VALUE( base )
                        else
                        {
                            msg_Warn( p_demux,
                                      "unexpected attribure %s in element %s",
331
                                      psz_attrname, psz_eltname );
332
333
334
335
336
337
338
339
340
341
342
343
                        }
                        free( psz_attrname );
                        free( psz_attrvalue );
                    }
                }
                else if( !strcmp( psz_eltname, "station" ) )
                {
                    while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
                    {
                        char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
                        char *psz_attrvalue =
                            xml_ReaderValue( p_sys->p_xml_reader );
344
345
                        if( !psz_attrname || !psz_attrvalue )
                        {
346
347
348
                            free( psz_eltname );
                            free( psz_attrname );
                            free( psz_attrvalue );
349
350
                            return -1;
                        }
351
352
353
354
355
356
357
358

                        GET_VALUE( name )
                        else GET_VALUE( mt )
                        else GET_VALUE( id )
                        else GET_VALUE( br )
                        else GET_VALUE( genre )
                        else GET_VALUE( ct )
                        else GET_VALUE( lc )
359
360
                        else GET_VALUE( rt )
                        else GET_VALUE( load )
361
362
363
                        else
                        {
                            msg_Warn( p_demux,
364
365
                                      "unexpected attribute %s in element %s",
                                      psz_attrname, psz_eltname );
366
367
368
369
370
                        }
                        free( psz_attrname );
                        free( psz_attrvalue );
                    }
                }
371
                free( psz_eltname );
372
373
374
375
376
377
378
379
380
381
                break;

            case XML_READER_TEXT:
                break;

            // End element
            case XML_READER_ENDELEM:
                // Read the element name
                psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
                if( !psz_eltname ) return -1;
382
                if( !strcmp( psz_eltname, "station" ) &&
383
384
                    ( psz_base || ( psz_rt && psz_load &&
                    ( p_sys->b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
385
                {
386
387
388
389
                    char *psz_mrl = NULL;
                    if( psz_rt || psz_load )
                    {
                        /* tv */
390
391
392
                        if( asprintf( &psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
                                 psz_id ) == -1)
                            psz_mrl = NULL;
393
394
395
396
                    }
                    else
                    {
                        /* radio */
397
398
399
                        if( asprintf( &psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
                             psz_base, psz_id ) == -1 )
                            psz_mrl = NULL;
400
                    }
401
402
                    p_input = input_item_New( p_demux, psz_mrl, psz_name );
                    input_item_CopyOptions( p_sys->p_current_input, p_input );
403
404
                    free( psz_mrl );

405
406
407
408
#define SADD_INFO( type, field ) \
                    if( field ) \
                        input_item_AddInfo( p_input, _("Shoutcast"), \
                                            vlc_gettext(type), "%s", field )
409
410
411
412
                    SADD_INFO( N_("Mime"), psz_mt );
                    SADD_INFO( N_("Bitrate"), psz_br );
                    SADD_INFO( N_("Listeners"), psz_lc );
                    SADD_INFO( N_("Load"), psz_load );
dionoea's avatar
Ahem...    
dionoea committed
413
                    if( psz_genre )
414
                        input_item_SetGenre( p_input, psz_genre );
dionoea's avatar
Ahem...    
dionoea committed
415
                    if( psz_ct )
416
                        input_item_SetNowPlaying( p_input, psz_ct );
dionoea's avatar
Ahem...    
dionoea committed
417
                    if( psz_rt )
418
                        input_item_SetRating( p_input, psz_rt );
419
                    input_item_AddSubItem( p_sys->p_current_input, p_input );
420
                    vlc_gc_decref( p_input );
zorglub's avatar
zorglub committed
421
                    FREENULL( psz_name );
422
423
424
425
426
427
428
                    FREENULL( psz_mt );
                    FREENULL( psz_id );
                    FREENULL( psz_br );
                    FREENULL( psz_genre );
                    FREENULL( psz_ct );
                    FREENULL( psz_lc );
                    FREENULL( psz_rt );
429
430
431
432
433
434
435
436
437
438
                }
                free( psz_eltname );
                break;
        }
    }
    return 0;
}

static int Control( demux_t *p_demux, int i_query, va_list args )
{
439
    VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
440
441
    return VLC_EGENERIC;
}