]> git.sesse.net Git - vlc/blob - modules/demux/playlist/b4s.c
demux/playlist/*: Added a special "shoutcast" mode to b4s parser
[vlc] / modules / demux / playlist / b4s.c
1 /*****************************************************************************
2  * b4s.c : B4S playlist format import
3  *****************************************************************************
4  * Copyright (C) 2005 VideoLAN
5  * $Id$
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31 #include <vlc/intf.h>
32
33 #include <errno.h>                                                 /* ENOMEM */
34 #include "playlist.h"
35 #include "vlc_xml.h"
36
37 struct demux_sys_t
38 {
39     char *psz_prefix;
40     playlist_t *p_playlist;
41     xml_t *p_xml;
42     xml_reader_t *p_xml_reader;
43     int b_shout;
44 };
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 static int Demux( demux_t *p_demux);
50 static int Control( demux_t *p_demux, int i_query, va_list args );
51 static int IsWhitespace( char *psz_string );
52 static void ShoutcastAdd( playlist_t *p_playlist, playlist_item_t* p_genre,
53                           playlist_item_t *p_bitrate, playlist_item_t *p_item,
54                           char *psz_genre, char *psz_bitrate );
55
56 /*****************************************************************************
57  * Import_B4S: main import function
58  *****************************************************************************/
59 int Import_B4S( vlc_object_t *p_this )
60 {
61     demux_t *p_demux = (demux_t *)p_this;
62
63     char    *psz_ext;
64
65     psz_ext = strrchr ( p_demux->psz_path, '.' );
66
67     if( ( psz_ext && !strcasecmp( psz_ext, ".b4s") ) ||
68         ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "b4s-open") ) ||
69         ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "shout-b4s") ) )
70     {
71         ;
72     }
73     else
74     {
75         return VLC_EGENERIC;
76     }
77     msg_Dbg( p_demux, "using b4s playlist import");
78
79     p_demux->pf_control = Control;
80     p_demux->pf_demux = Demux;
81     p_demux->p_sys = malloc( sizeof(demux_sys_t) );
82     if( p_demux->p_sys == NULL )
83     {
84         msg_Err( p_demux, "Out of memory" );
85         return VLC_ENOMEM;
86     }
87     p_demux->p_sys->b_shout = p_demux->psz_demux &&
88         !strcmp(p_demux->psz_demux, "shout-b4s");
89     p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
90
91     return VLC_SUCCESS;
92 }
93
94 /*****************************************************************************
95  * Deactivate: frees unused data
96  *****************************************************************************/
97 void Close_B4S( vlc_object_t *p_this )
98 {
99     demux_t *p_demux = (demux_t *)p_this;
100     demux_sys_t *p_sys = p_demux->p_sys;
101
102     if( p_sys->psz_prefix ) free( p_sys->psz_prefix );
103     if( p_sys->p_playlist ) vlc_object_release( p_sys->p_playlist );
104     if( p_sys->p_xml_reader ) xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
105     if( p_sys->p_xml ) xml_Delete( p_sys->p_xml );
106     free( p_sys );
107 }
108
109 static int Demux( demux_t *p_demux )
110 {
111     demux_sys_t *p_sys = p_demux->p_sys;
112     playlist_t *p_playlist;
113     playlist_item_t *p_item, *p_current, *p_bitrate, *p_genre;
114
115     vlc_bool_t b_play;
116     int i_ret;
117
118     xml_t *p_xml;
119     xml_reader_t *p_xml_reader;
120     char *psz_elname = NULL;
121     int i_type, b_shoutcast;
122     char *psz_mrl = NULL, *psz_name = NULL, *psz_genre = NULL;
123     char *psz_now = NULL, *psz_listeners = NULL, *psz_bitrate = NULL;
124         
125
126     b_shoutcast = p_sys->b_shout;
127     
128     p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
129                                                  FIND_PARENT );
130     if( !p_playlist )
131     {
132         msg_Err( p_demux, "can't find playlist" );
133         return -1;
134     }
135     p_sys->p_playlist = p_playlist;
136
137     b_play = FindItem( p_demux, p_playlist, &p_current );
138
139     playlist_ItemToNode( p_playlist, p_current );
140     p_current->input.i_type = ITEM_TYPE_PLAYLIST;
141     if( b_shoutcast )
142     {
143         p_genre = playlist_NodeCreate( p_playlist, p_current->pp_parents[0]->i_view, "Genre", p_current );
144         playlist_CopyParents( p_current, p_genre );
145
146         p_bitrate = playlist_NodeCreate( p_playlist, p_current->pp_parents[0]->i_view, "Bitrate", p_current );
147         playlist_CopyParents( p_current, p_bitrate );
148     }
149     
150     p_xml = p_sys->p_xml = xml_Create( p_demux );
151     if( !p_xml ) return -1;
152
153     stream_ReadLine( p_demux->s );
154     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
155     if( !p_xml_reader ) return -1;
156     p_sys->p_xml_reader = p_xml_reader;
157
158     /* xml */
159     /* check root node */
160     if( xml_ReaderRead( p_xml_reader ) != 1 )
161     {
162         msg_Err( p_demux, "invalid file (no root node)" );
163         return -1;
164     }
165
166     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
167         ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
168         strcmp( psz_elname, "WinampXML" ) )
169     {
170         msg_Err( p_demux, "invalid root node %i, %s",
171                  xml_ReaderNodeType( p_xml_reader ), psz_elname );
172         if( psz_elname ) free( psz_elname );
173         return -1;
174     }
175     free( psz_elname );
176
177     /* root node should not have any attributes, and should only
178      * contain the "playlist node */
179
180     /* Skip until 1st child node */
181     while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 &&
182            xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
183     if( i_ret != 1 )
184     {
185         msg_Err( p_demux, "invalid file (no child node)" );
186         return -1;
187     }
188
189     if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
190         strcmp( psz_elname, "playlist" ) )
191     {
192         msg_Err( p_demux, "invalid child node %s", psz_elname );
193         if( psz_elname ) free( psz_elname );
194         return -1;
195     }
196     free( psz_elname ); psz_elname = 0;
197
198     // Read the attributes
199     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
200     {
201         char *psz_name = xml_ReaderName( p_xml_reader );
202         char *psz_value = xml_ReaderValue( p_xml_reader );
203         if( !psz_name || !psz_value ) return -1;
204         if( !strcmp( psz_name, "num_entries" ) )
205         {
206             msg_Dbg( p_demux, "playlist has %d entries", atoi(psz_value) );
207         }
208         else if( !strcmp( psz_name, "label" ) )
209         {
210             playlist_ItemSetName( p_current, psz_value );
211         }
212         else
213         {
214             msg_Warn( p_demux, "stray attribute %s with value %s in element"
215                       " 'playlist'", psz_name, psz_value );
216         }
217         free( psz_name );
218         free( psz_value );
219     }
220
221     while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
222     {
223         // Get the node type
224         i_type = xml_ReaderNodeType( 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             {
234                 // Read the element name
235                 if( psz_elname ) free( psz_elname );
236                 psz_elname = xml_ReaderName( p_xml_reader );
237                 if( !psz_elname ) return -1;
238                 
239
240                 // Read the attributes
241                 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
242                 {
243                     char *psz_name = xml_ReaderName( p_xml_reader );
244                     char *psz_value = xml_ReaderValue( p_xml_reader );
245                     if( !psz_name || !psz_value ) return -1;
246                     if( !strcmp( psz_elname, "entry" ) &&
247                         !strcmp( psz_name, "Playstring" ) )
248                     {
249                         psz_mrl = strdup( psz_value );
250                     }
251                     else
252                     {
253                         msg_Warn( p_demux, "unexpected attribure %s in element %s",
254                                   psz_name, psz_elname );
255                     }
256                     free( psz_name );
257                     free( psz_value );
258                 }
259                 break;
260             }
261             case XML_READER_TEXT:
262             {
263                 char *psz_text = xml_ReaderValue( p_xml_reader );
264                 if( IsWhitespace( psz_text ) )
265                 {
266                     free( psz_text );
267                     break;
268                 }
269                 if( !strcmp( psz_elname, "Name" ) )
270                 {
271                     psz_name = strdup( psz_text );
272                 }
273                 else if( !strcmp( psz_elname, "Genre" ) )
274                 {
275                     psz_genre = strdup( psz_text );
276                 }
277                 else if( !strcmp( psz_elname, "Nowplaying" ) )
278                 {
279                     psz_now = strdup( psz_text );
280                 }
281                 else if( !strcmp( psz_elname, "Listeners" ) )
282                 {
283                     psz_listeners = strdup( psz_text );
284                 }
285                 else if( !strcmp( psz_elname, "Bitrate" ) )
286                 {
287                     psz_bitrate = strdup( psz_text );
288                 }
289                 else if( !strcmp( psz_elname, "" ) )
290                 {
291                     ;
292                 }
293                 else
294                 {
295                     msg_Warn( p_demux, "unexpected text in element '%s'",
296                               psz_elname );
297                 }
298                 free( psz_text );
299                 break;
300             }
301             // End element
302             case XML_READER_ENDELEM:
303             {
304                 // Read the element name
305                 free( psz_elname );
306                 psz_elname = xml_ReaderName( p_xml_reader );
307                 if( !psz_elname ) return -1;
308                 if( !strcmp( psz_elname, "entry" ) )
309                 {
310                     p_item = playlist_ItemNew( p_playlist, psz_mrl, psz_name );
311                     if( psz_now )
312                     {
313                         vlc_input_item_AddInfo( &(p_item->input),
314                                                 _("Meta-information"),
315                                                 _( VLC_META_NOW_PLAYING ),
316                                                 "%s",
317                                                 psz_now );
318                     }
319                     if( psz_genre )
320                     {
321                         vlc_input_item_AddInfo( &p_item->input,
322                                                 _("Meta-information"),
323                                                 _( VLC_META_GENRE ),
324                                                 "%s",
325                                                 psz_genre );
326                     }
327                     if( psz_listeners )
328                     {
329                         vlc_input_item_AddInfo( &p_item->input,
330                                                 _("Meta-information"),
331                                                 _( "Listeners" ),
332                                                 "%s",
333                                                 psz_listeners );
334                     }
335                     if( psz_bitrate )
336                     {
337                         vlc_input_item_AddInfo( &p_item->input,
338                                                 _("Meta-information"),
339                                                 _( "Bitrate" ),
340                                                 "%s",
341                                                 psz_bitrate );
342                     }
343                     playlist_NodeAddItem( p_playlist, p_item,
344                                           p_current->pp_parents[0]->i_view,
345                                           p_current, PLAYLIST_APPEND,
346                                           PLAYLIST_END );
347
348                     /* We need to declare the parents of the node as the
349                      *                  * same of the parent's ones */
350                     playlist_CopyParents( p_current, p_item );
351                     
352                     vlc_input_item_CopyOptions( &p_current->input,
353                                                 &p_item->input );
354                     if( b_shoutcast )
355                         ShoutcastAdd( p_playlist, p_genre, p_bitrate, p_item,
356                                       psz_genre, psz_bitrate );
357 #define FREE(a) if( a ) free( a ); a = NULL;
358                     FREE( psz_name );
359                     FREE( psz_mrl );
360                     FREE( psz_genre );
361                     FREE( psz_bitrate );
362                     FREE( psz_listeners );
363                     FREE( psz_now );
364 #undef FREE
365                 }
366                 free( psz_elname );
367                 psz_elname = strdup("");
368
369                 break;
370             }
371         }
372     }
373
374     if( i_ret != 0 )
375     {
376         msg_Warn( p_demux, "error while parsing data" );
377     }
378     if( b_shoutcast )
379     {
380         vlc_mutex_lock( &p_playlist->object_lock );
381         playlist_NodeSort( p_playlist, p_bitrate, SORT_TITLE_NUMERIC, ORDER_NORMAL );
382         vlc_mutex_unlock( &p_playlist->object_lock );
383     }
384
385     /* Go back and play the playlist */
386     if( b_play )
387     {
388         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
389                           p_playlist->status.i_view,
390                           p_playlist->status.p_item, NULL );
391     }
392     
393     vlc_object_release( p_playlist );
394     p_sys->p_playlist = NULL;
395     return VLC_SUCCESS;
396 }
397
398 static int Control( demux_t *p_demux, int i_query, va_list args )
399 {
400     return VLC_EGENERIC;
401 }
402
403 static int IsWhitespace( char *psz_string )
404 {
405     while( *psz_string )
406     {
407         if( *psz_string != ' ' && *psz_string != '\t' && *psz_string != '\r' &&
408             *psz_string != '\n' )
409         {
410             return VLC_FALSE;
411         }
412         psz_string++;
413     }
414     return VLC_TRUE;
415 }
416
417 static void ShoutcastAdd( playlist_t *p_playlist, playlist_item_t* p_genre,
418                           playlist_item_t *p_bitrate, playlist_item_t *p_item,
419                           char *psz_genre, char *psz_bitrate )
420 {
421     playlist_item_t *p_parent;
422     if( psz_bitrate )
423     {
424         playlist_item_t *p_copy = playlist_ItemCopy(p_playlist,p_item);
425         p_parent = playlist_ChildSearchName( p_bitrate, psz_bitrate );
426         if( !p_parent )
427         {
428             p_parent = playlist_NodeCreate( p_playlist, p_genre->pp_parents[0]->i_view, psz_bitrate,
429                                             p_bitrate );
430             playlist_CopyParents( p_bitrate, p_parent );
431         }
432         playlist_NodeAddItem( p_playlist, p_copy, p_parent->pp_parents[0]->i_view, p_parent, PLAYLIST_APPEND, PLAYLIST_END  );
433         playlist_CopyParents( p_parent, p_copy );
434
435     }
436
437     if( psz_genre )
438     {
439         playlist_item_t *p_copy = playlist_ItemCopy(p_playlist,p_item);
440         p_parent = playlist_ChildSearchName( p_genre, psz_genre );
441         if( !p_parent )
442         {
443             p_parent = playlist_NodeCreate( p_playlist, p_genre->pp_parents[0]->i_view, psz_genre,
444                                             p_genre );
445             playlist_CopyParents( p_genre, p_parent );
446         }
447         playlist_NodeAddItem( p_playlist, p_copy, p_parent->pp_parents[0]->i_view, p_parent, PLAYLIST_APPEND, PLAYLIST_END );
448         playlist_CopyParents( p_parent, p_copy );
449     }
450 }