]> git.sesse.net Git - vlc/blob - modules/demux/playlist/shoutcast.c
Add option (--shoutcast-show-adult) to show porn^WNC17 rated video streams. Default...
[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 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <ctype.h>                                              /* isspace() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33 #include <vlc/intf.h>
34
35 #include <errno.h>                                                 /* ENOMEM */
36 #include "playlist.h"
37 #include "vlc_xml.h"
38
39 struct demux_sys_t
40 {
41     playlist_t *p_playlist;
42     playlist_item_t *p_current;
43
44     xml_t *p_xml;
45     xml_reader_t *p_xml_reader;
46
47     vlc_bool_t 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 E_(Import_Shoutcast)( vlc_object_t *p_this )
68 {
69     demux_t *p_demux = (demux_t *)p_this;
70     demux_sys_t *p_sys;
71
72     char    *psz_ext;
73
74     psz_ext = strrchr ( p_demux->psz_path, '.' );
75
76     if( !p_demux->psz_demux || strcmp(p_demux->psz_demux, "shout-winamp") )
77     {
78         return VLC_EGENERIC;
79     }
80     msg_Dbg( p_demux, "using shoutcast playlist import");
81
82     p_demux->pf_control = Control;
83     p_demux->pf_demux = Demux;
84     p_demux->p_sys = p_sys = malloc( sizeof(demux_sys_t) );
85     if( p_sys == NULL )
86     {
87         msg_Err( p_demux, "out of memory" );
88         return VLC_ENOMEM;
89     }
90
91     p_sys->p_playlist = NULL;
92     p_sys->p_xml = NULL;
93     p_sys->p_xml_reader = NULL;
94
95     /* Do we want to list adult content ? */
96     var_Create( p_demux, "shoutcast-show-adult",
97                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
98     p_sys->b_adult = var_GetBool( p_demux, "shoutcast-show-adult" );
99
100     return VLC_SUCCESS;
101 }
102
103 /*****************************************************************************
104  * Deactivate: frees unused data
105  *****************************************************************************/
106 void E_(Close_Shoutcast)( vlc_object_t *p_this )
107 {
108     demux_t *p_demux = (demux_t *)p_this;
109     demux_sys_t *p_sys = p_demux->p_sys;
110
111     if( p_sys->p_playlist )
112         vlc_object_release( p_sys->p_playlist );
113     if( p_sys->p_xml_reader )
114         xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
115     if( p_sys->p_xml )
116         xml_Delete( p_sys->p_xml );
117     free( p_sys );
118 }
119
120 static int Demux( demux_t *p_demux )
121 {
122     demux_sys_t *p_sys = p_demux->p_sys;
123     playlist_t *p_playlist;
124
125     vlc_bool_t b_play;
126
127     xml_t *p_xml;
128     xml_reader_t *p_xml_reader;
129
130     char *psz_eltname = NULL;
131
132
133     p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
134                                                  FIND_ANYWHERE );
135     if( !p_playlist )
136     {
137         msg_Err( p_demux, "can't find playlist" );
138         return -1;
139     }
140     p_sys->p_playlist = p_playlist;
141
142     b_play = E_(FindItem)( p_demux, p_playlist, &p_sys->p_current );
143
144     msg_Warn( p_demux, "item: %s", p_sys->p_current->input.psz_name );
145     playlist_ItemToNode( p_playlist, p_sys->p_current );
146     p_sys->p_current->input.i_type = ITEM_TYPE_PLAYLIST;
147
148     p_xml = p_sys->p_xml = xml_Create( p_demux );
149     if( !p_xml ) return -1;
150
151     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
152     if( !p_xml_reader ) return -1;
153     p_sys->p_xml_reader = p_xml_reader;
154
155     /* xml */
156     /* check root node */
157     if( xml_ReaderRead( p_xml_reader ) != 1 )
158     {
159         msg_Err( p_demux, "invalid file (no root node)" );
160         return -1;
161     }
162
163     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
164         ( psz_eltname = xml_ReaderName( p_xml_reader ) ) == NULL ||
165         ( strcmp( psz_eltname, "genrelist" )
166           && strcmp( psz_eltname, "stationlist" ) ) )
167     {
168         msg_Err( p_demux, "invalid root node %i, %s",
169                  xml_ReaderNodeType( p_xml_reader ), psz_eltname );
170         if( psz_eltname ) free( psz_eltname );
171         return -1;
172     }
173
174     if( !strcmp( psz_eltname, "genrelist" ) )
175     {
176         /* we're reading a genre list */
177         free( psz_eltname );
178         if( DemuxGenre( p_demux ) ) return -1;
179     }
180     else
181     {
182         /* we're reading a station list */
183         free( psz_eltname );
184         if( DemuxStation( p_demux ) ) return -1;
185     }
186
187     /* Go back and play the playlist */
188     if( b_play && p_playlist->status.p_item &&
189         p_playlist->status.p_item->i_children > 0 )
190     {
191         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
192                           p_playlist->status.i_view,
193                           p_playlist->status.p_item,
194                           p_playlist->status.p_item->pp_children[0] );
195     }
196
197     vlc_object_release( p_playlist );
198     p_sys->p_playlist = NULL;
199     return VLC_SUCCESS;
200 }
201
202 #define GET_VALUE( a ) \
203                         if( !strcmp( psz_attrname, #a ) ) \
204                         { \
205                             psz_ ## a = strdup( psz_attrvalue ); \
206                         }
207 /* <genrelist>
208  *   <genre name="the name"></genre>
209  *   ...
210  * </genrelist>
211  **/
212 static int DemuxGenre( demux_t *p_demux )
213 {
214     demux_sys_t *p_sys = p_demux->p_sys;
215     char *psz_name = NULL; /* genre name */
216     char *psz_eltname = NULL; /* tag name */
217
218 #define FREE(a) if( a ) free( a ); a = NULL;
219     while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
220     {
221         int i_type;
222
223         // Get the node type
224         i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
225         switch( i_type )
226         {
227             // Error
228             case -1:
229                 return -1;
230                 break;
231
232             case XML_READER_STARTELEM:
233                 // Read the element name
234                 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
235                 if( !psz_eltname ) return -1;
236
237
238                 if( !strcmp( psz_eltname, "genre" ) )
239                 {
240                     // Read the attributes
241                     while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
242                     {
243                         char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
244                         char *psz_attrvalue =
245                             xml_ReaderValue( p_sys->p_xml_reader );
246                         if( !psz_attrname || !psz_attrvalue )
247                         {
248                             FREE(psz_attrname);
249                             FREE(psz_attrvalue);
250                             free(psz_eltname);
251                             /*FIXME: isn't return a bit too much. what about break*/
252                             return -1;
253                         }
254
255                         GET_VALUE( name )
256                         else
257                         {
258                             msg_Warn( p_demux,
259                                       "unexpected attribure %s in element %s",
260                                       psz_attrname,
261                                       psz_eltname );
262                         }
263                         free( psz_attrname );
264                         free( psz_attrvalue );
265                     }
266                 }
267                 free( psz_eltname ); psz_eltname = NULL;
268                 break;
269
270             case XML_READER_TEXT:
271                 break;
272
273             // End element
274             case XML_READER_ENDELEM:
275                 // Read the element name
276                 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
277                 if( !psz_eltname ) return -1;
278                 if( !strcmp( psz_eltname, "genre" ) )
279                 {
280                     playlist_item_t *p_item;
281                     char *psz_mrl = malloc( strlen( SHOUTCAST_BASE_URL )
282                             + strlen( "?genre=" ) + strlen( psz_name ) + 1 );
283                     sprintf( psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
284                              psz_name );
285                     p_item = playlist_ItemNew( p_sys->p_playlist, psz_mrl,
286                                                psz_name );
287                     free( psz_mrl );
288                     playlist_NodeAddItem( p_sys->p_playlist, p_item,
289                                           p_sys->p_current->pp_parents[0]->i_view,
290                                           p_sys->p_current, PLAYLIST_APPEND,
291                                           PLAYLIST_END );
292
293                     /* We need to declare the parents of the node as the
294                      *                  * same of the parent's ones */
295                     playlist_CopyParents( p_sys->p_current, p_item );
296
297                     vlc_input_item_CopyOptions( &p_sys->p_current->input,
298                                                 &p_item->input );
299
300                     FREE( psz_name );
301                 }
302                 FREE( psz_eltname );
303                 break;
304         }
305     }
306     return 0;
307 }
308
309 /* radio stations:
310  * <stationlist>
311  *   <tunein base="/sbin/tunein-station.pls"></tunein>
312  *   <station name="the name"
313  *            mt="mime type"
314  *            id="the id"
315  *            br="bit rate"
316  *            genre="A big genre string"
317  *            ct="current track name/author/..."
318  *            lc="listener count"></station>
319  * </stationlist>
320  *
321  * TV stations:
322  * <stationlist>
323  *   <tunein base="/sbin/tunein-station.pls"></tunein>
324  *   <station name="the name"
325  *            id="the id"
326  *            br="bit rate"
327  *            rt="rating"
328  *            load="server load ?"
329  *            ct="current track name/author/..."
330  *            genre="A big genre string"
331  *            lc="listener count"></station>
332  * </stationlist>
333  **/
334 static int DemuxStation( demux_t *p_demux )
335 {
336     demux_sys_t *p_sys = p_demux->p_sys;
337
338     char *psz_base = NULL; /* */
339
340     char *psz_name = NULL; /* genre name */
341     char *psz_mt = NULL; /* mime type */
342     char *psz_id = NULL; /* id */
343     char *psz_br = NULL; /* bit rate */
344     char *psz_genre = NULL; /* genre */
345     char *psz_ct = NULL; /* current track */
346     char *psz_lc = NULL; /* listener count */
347
348     /* If these are set then it's *not* a radio but a TV */
349     char *psz_rt = NULL; /* rating for shoutcast TV */
350     char *psz_load = NULL; /* load for shoutcast TV */
351
352     char *psz_eltname = NULL; /* tag name */
353
354     while( xml_ReaderRead( p_sys->p_xml_reader ) == 1 )
355     {
356         int i_type;
357
358         // Get the node type
359         i_type = xml_ReaderNodeType( p_sys->p_xml_reader );
360         switch( i_type )
361         {
362             // Error
363             case -1:
364                 return -1;
365                 break;
366
367             case XML_READER_STARTELEM:
368                 // Read the element name
369                 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
370                 if( !psz_eltname ) return -1;
371
372                 // Read the attributes
373                 if( !strcmp( psz_eltname, "tunein" ) )
374                 {
375                     while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
376                     {
377                         char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
378                         char *psz_attrvalue =
379                             xml_ReaderValue( p_sys->p_xml_reader );
380                         if( !psz_attrname || !psz_attrvalue )
381                         {
382                             free(psz_eltname);
383                             FREE(psz_attrname);
384                             FREE(psz_attrvalue);
385                             return -1;
386                         }
387
388                         GET_VALUE( base )
389                         else
390                         {
391                             msg_Warn( p_demux,
392                                       "unexpected attribure %s in element %s",
393                                       psz_attrname,
394                                       psz_eltname );
395                         }
396                         free( psz_attrname );
397                         free( psz_attrvalue );
398                     }
399                 }
400                 else if( !strcmp( psz_eltname, "station" ) )
401                 {
402                     while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
403                     {
404                         char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
405                         char *psz_attrvalue =
406                             xml_ReaderValue( p_sys->p_xml_reader );
407                         if( !psz_attrname || !psz_attrvalue )
408                         {
409                             free(psz_eltname);
410                             FREE(psz_attrname);
411                             FREE(psz_attrvalue);
412                             return -1;
413                         }
414
415                         GET_VALUE( name )
416                         else GET_VALUE( mt )
417                         else GET_VALUE( id )
418                         else GET_VALUE( br )
419                         else GET_VALUE( genre )
420                         else GET_VALUE( ct )
421                         else GET_VALUE( lc )
422                         else GET_VALUE( rt )
423                         else GET_VALUE( load )
424                         else
425                         {
426                             msg_Warn( p_demux,
427                                       "unexpected attribure %s in element %s",
428                                       psz_attrname,
429                                       psz_eltname );
430                         }
431                         free( psz_attrname );
432                         free( psz_attrvalue );
433                     }
434                 }
435                 free(psz_eltname);
436                 break;
437
438             case XML_READER_TEXT:
439                 break;
440
441             // End element
442             case XML_READER_ENDELEM:
443                 // Read the element name
444                 psz_eltname = xml_ReaderName( p_sys->p_xml_reader );
445                 if( !psz_eltname ) return -1;
446                 if( !strcmp( psz_eltname, "station" ) &&
447                     ( psz_base || ( psz_rt && psz_load &&
448                     ( p_sys->b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
449                 {
450                     playlist_item_t *p_item;
451                     char *psz_mrl = NULL;
452                     if( psz_rt || psz_load )
453                     {
454                         /* tv */
455                         psz_mrl = malloc( strlen( SHOUTCAST_TV_TUNEIN_URL )
456                                           + strlen( psz_id ) + 1 );
457                         sprintf( psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
458                                  psz_id );
459                     }
460                     else
461                     {
462                         /* radio */
463                         psz_mrl = malloc( strlen( SHOUTCAST_TUNEIN_BASE_URL )
464                             + strlen( psz_base ) + strlen( "?id=" )
465                             + strlen( psz_id ) + 1 );
466                         sprintf( psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
467                              psz_base, psz_id );
468                     }
469                     p_item = playlist_ItemNew( p_sys->p_playlist, psz_mrl,
470                                                psz_name );
471                     free( psz_mrl );
472
473                     if( psz_mt )
474                     {
475                         vlc_input_item_AddInfo( &p_item->input,
476                                                 _( "Shoutcast" ),
477                                                 _( "Mime type" ),
478                                                 "%s",
479                                                 psz_mt );
480                     }
481                     else if( psz_br )
482                     {
483                         vlc_input_item_AddInfo( &p_item->input,
484                                                 _( "Shoutcast" ),
485                                                 _( "Bitrate" ),
486                                                 "%s",
487                                                 psz_br );
488                     }
489                     else if( psz_genre )
490                     {
491                         vlc_input_item_AddInfo( &p_item->input,
492                                                 _(VLC_META_INFO_CAT),
493                                                 _(VLC_META_GENRE),
494                                                 "%s",
495                                                 psz_genre );
496                     }
497                     else if( psz_ct )
498                     {
499                         vlc_input_item_AddInfo( &p_item->input,
500                                                 _(VLC_META_INFO_CAT),
501                                                 _(VLC_META_NOW_PLAYING),
502                                                 "%s",
503                                                 psz_ct );
504                     }
505                     else if( psz_lc )
506                     {
507                         vlc_input_item_AddInfo( &p_item->input,
508                                                 _( "Shoutcast" ),
509                                                 _( "Listeners" ),
510                                                 "%s",
511                                                 psz_lc );
512                     }
513                     else if( psz_rt )
514                     {
515                         vlc_input_item_AddInfo( &p_item->input,
516                                                 _( "Shoutcast" ),
517                                                 _( "Rating" ),
518                                                 "%s",
519                                                 psz_rt );
520                     }
521                     else if( psz_load )
522                     {
523                         vlc_input_item_AddInfo( &p_item->input,
524                                                 _( "Shoutcast" ),
525                                                 _( "Load" ),
526                                                 "%s",
527                                                 psz_load );
528                     }
529
530                     playlist_NodeAddItem( p_sys->p_playlist, p_item,
531                                           p_sys->p_current->pp_parents[0]->i_view,
532                                           p_sys->p_current, PLAYLIST_APPEND,
533                                           PLAYLIST_END );
534
535                     /* We need to declare the parents of the node as the
536                      *                  * same of the parent's ones */
537                     playlist_CopyParents( p_sys->p_current, p_item );
538
539                     vlc_input_item_CopyOptions( &p_sys->p_current->input,
540                                                 &p_item->input );
541
542                     FREE( psz_name );
543                     FREE( psz_mt )
544                     FREE( psz_id )
545                     FREE( psz_br )
546                     FREE( psz_genre )
547                     FREE( psz_ct )
548                     FREE( psz_lc )
549                     FREE( psz_rt )
550                 }
551                 free( psz_eltname );
552                 break;
553         }
554     }
555     return 0;
556 }
557 #undef FREE
558
559 static int Control( demux_t *p_demux, int i_query, va_list args )
560 {
561     return VLC_EGENERIC;
562 }