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
    /* Do we want to list adult content ? */
78
    p_demux->p_sys->b_adult = var_CreateGetBool( p_demux, "shoutcast-show-adult" );
79

80
81
82
83
84
85
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Deactivate: frees unused data
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
86
void Close_Shoutcast( vlc_object_t *p_this )
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
{
    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;
104
    INIT_PLAYLIST_STUFF;
105
    p_sys->p_current_input = p_current_input;
106
107

    p_xml = p_sys->p_xml = xml_Create( p_demux );
108
109
    if( !p_xml )
        goto error;
110
111

    p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
112
113
    if( !p_xml_reader )
        goto error;
114
115
116
117
118
119
    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)" );
120
        goto error;
121
122
123
124
125
126
127
128
129
    }

    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
        goto error;
132
133
134
135
136
    }

    if( !strcmp( psz_eltname, "genrelist" ) )
    {
        /* we're reading a genre list */
dionoea's avatar
dionoea committed
137
        free( psz_eltname );
138
139
        if( DemuxGenre( p_demux ) )
            goto error;
140
141
142
143
    }
    else
    {
        /* we're reading a station list */
dionoea's avatar
dionoea committed
144
        free( psz_eltname );
145
146
        if( DemuxStation( p_demux ) )
            goto error;
147
148
    }

149
    HANDLE_PLAY_AND_RELEASE;
150
    return 0; /* Needed for correct operation of go back */
151
152
153
154

error:
    HANDLE_PLAY_AND_RELEASE;
    return -1;
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
}

#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 */
172
    input_item_t *p_input;
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

    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 );
200
201
                        if( !psz_attrname || !psz_attrvalue )
                        {
zorglub's avatar
zorglub committed
202
203
                            FREENULL(psz_attrname);
                            FREENULL(psz_attrvalue);
204
205
206
207
                            free(psz_eltname);
                            /*FIXME: isn't return a bit too much. what about break*/
                            return -1;
                        }
208
209
210
211
212
213

                        GET_VALUE( name )
                        else
                        {
                            msg_Warn( p_demux,
                                      "unexpected attribure %s in element %s",
214
                                      psz_attrname,psz_eltname );
215
216
217
218
219
                        }
                        free( psz_attrname );
                        free( psz_attrvalue );
                    }
                }
220
                free( psz_eltname ); psz_eltname = NULL;
221
222
223
224
225
226
227
228
229
230
231
232
                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" ) )
                {
233
234
235
236
                    char* psz_mrl;
                    if( asprintf( &psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
                             psz_name ) != -1 )
                    {
237
                        p_input = input_item_New( p_demux, psz_mrl, psz_name );
ivoire's avatar
ivoire committed
238
                        input_item_CopyOptions( p_sys->p_current_input, p_input );
239
                        free( psz_mrl );
ivoire's avatar
ivoire committed
240
                        input_item_AddSubItem( p_sys->p_current_input, p_input );
241
242
                        vlc_gc_decref( p_input );
                    }
zorglub's avatar
zorglub committed
243
                    FREENULL( psz_name );
244
                }
zorglub's avatar
zorglub committed
245
                FREENULL( psz_eltname );
246
247
248
249
250
251
                break;
        }
    }
    return 0;
}

252
253
/* radio stations:
 * <stationlist>
254
255
256
257
258
259
260
261
262
 *   <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>
263
264
265
266
267
268
269
270
271
272
273
274
275
 *
 * 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>
276
277
278
279
 **/
static int DemuxStation( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
280
    input_item_t *p_input;
281
282
283
284
285
286
287
288
289
290
291

    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 */

292
293
294
295
    /* 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 */

296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
    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 );
324
325
                        if( !psz_attrname || !psz_attrvalue )
                        {
326
327
328
                            free( psz_eltname );
                            free( psz_attrname );
                            free( psz_attrvalue );
329
330
                            return -1;
                        }
331
332
333
334
335
336

                        GET_VALUE( base )
                        else
                        {
                            msg_Warn( p_demux,
                                      "unexpected attribure %s in element %s",
337
                                      psz_attrname, psz_eltname );
338
339
340
341
342
343
344
345
346
347
348
349
                        }
                        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 );
350
351
                        if( !psz_attrname || !psz_attrvalue )
                        {
352
353
354
                            free( psz_eltname );
                            free( psz_attrname );
                            free( psz_attrvalue );
355
356
                            return -1;
                        }
357
358
359
360
361
362
363
364

                        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 )
365
366
                        else GET_VALUE( rt )
                        else GET_VALUE( load )
367
368
369
                        else
                        {
                            msg_Warn( p_demux,
370
371
                                      "unexpected attribute %s in element %s",
                                      psz_attrname, psz_eltname );
372
373
374
375
376
                        }
                        free( psz_attrname );
                        free( psz_attrvalue );
                    }
                }
377
                free( psz_eltname );
378
379
380
381
382
383
384
385
386
387
                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;
388
                if( !strcmp( psz_eltname, "station" ) &&
389
390
                    ( psz_base || ( psz_rt && psz_load &&
                    ( p_sys->b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
391
                {
392
393
394
395
                    char *psz_mrl = NULL;
                    if( psz_rt || psz_load )
                    {
                        /* tv */
396
397
398
                        if( asprintf( &psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
                                 psz_id ) == -1)
                            psz_mrl = NULL;
399
400
401
402
                    }
                    else
                    {
                        /* radio */
403
404
405
                        if( asprintf( &psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
                             psz_base, psz_id ) == -1 )
                            psz_mrl = NULL;
406
                    }
407
408
                    p_input = input_item_New( p_demux, psz_mrl, psz_name );
                    input_item_CopyOptions( p_sys->p_current_input, p_input );
409
410
                    free( psz_mrl );

411
412
413
414
#define SADD_INFO( type, field ) \
                    if( field ) \
                        input_item_AddInfo( p_input, _("Shoutcast"), \
                                            vlc_gettext(type), "%s", field )
415
416
417
418
                    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
419
                    if( psz_genre )
420
                        input_item_SetGenre( p_input, psz_genre );
dionoea's avatar
Ahem...    
dionoea committed
421
                    if( psz_ct )
422
                        input_item_SetNowPlaying( p_input, psz_ct );
dionoea's avatar
Ahem...    
dionoea committed
423
                    if( psz_rt )
424
                        input_item_SetRating( p_input, psz_rt );
425
                    input_item_AddSubItem( p_sys->p_current_input, p_input );
426
                    vlc_gc_decref( p_input );
zorglub's avatar
zorglub committed
427
                    FREENULL( psz_name );
428
429
430
431
432
433
434
                    FREENULL( psz_mt );
                    FREENULL( psz_id );
                    FREENULL( psz_br );
                    FREENULL( psz_genre );
                    FREENULL( psz_ct );
                    FREENULL( psz_lc );
                    FREENULL( psz_rt );
435
436
437
438
439
440
441
442
443
444
                }
                free( psz_eltname );
                break;
        }
    }
    return 0;
}

static int Control( demux_t *p_demux, int i_query, va_list args )
{
445
    VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
446
447
    return VLC_EGENERIC;
}