]> git.sesse.net Git - vlc/blob - modules/demux/playlist/b4s.c
* ALL: fixed a handful of bugs and memory leaks.
[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     demux_sys_t *p_sys;
63
64     char    *psz_ext;
65
66     psz_ext = strrchr ( p_demux->psz_path, '.' );
67
68     if( ( psz_ext && !strcasecmp( psz_ext, ".b4s") ) ||
69         ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "b4s-open") ) ||
70         ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "shout-b4s") ) )
71     {
72         ;
73     }
74     else
75     {
76         return VLC_EGENERIC;
77     }
78     msg_Dbg( p_demux, "using b4s playlist import");
79
80     p_demux->pf_control = Control;
81     p_demux->pf_demux = Demux;
82     p_demux->p_sys = p_sys = malloc( sizeof(demux_sys_t) );
83     if( p_sys == NULL )
84     {
85         msg_Err( p_demux, "Out of memory" );
86         return VLC_ENOMEM;
87     }
88     p_sys->b_shout = p_demux->psz_demux &&
89         !strcmp(p_demux->psz_demux, "shout-b4s");
90     p_sys->psz_prefix = FindPrefix( p_demux );
91     p_sys->p_playlist = NULL;
92     p_sys->p_xml = NULL;
93     p_sys->p_xml_reader = NULL;
94
95     return VLC_SUCCESS;
96 }
97
98 /*****************************************************************************
99  * Deactivate: frees unused data
100  *****************************************************************************/
101 void Close_B4S( vlc_object_t *p_this )
102 {
103     demux_t *p_demux = (demux_t *)p_this;
104     demux_sys_t *p_sys = p_demux->p_sys;
105
106     if( p_sys->psz_prefix ) free( p_sys->psz_prefix );
107     if( p_sys->p_playlist ) vlc_object_release( p_sys->p_playlist );
108     if( p_sys->p_xml_reader ) xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
109     if( p_sys->p_xml ) xml_Delete( p_sys->p_xml );
110     free( p_sys );
111 }
112
113 static int Demux( demux_t *p_demux )
114 {
115     demux_sys_t *p_sys = p_demux->p_sys;
116     playlist_t *p_playlist;
117     playlist_item_t *p_item, *p_current, *p_bitrate, *p_genre;
118
119     vlc_bool_t b_play;
120     int i_ret;
121
122     xml_t *p_xml;
123     xml_reader_t *p_xml_reader;
124     char *psz_elname = NULL;
125     int i_type, b_shoutcast;
126     char *psz_mrl = NULL, *psz_name = NULL, *psz_genre = NULL;
127     char *psz_now = NULL, *psz_listeners = NULL, *psz_bitrate = NULL;
128         
129
130     b_shoutcast = p_sys->b_shout;
131     
132     p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
133                                                  FIND_PARENT );
134     if( !p_playlist )
135     {
136         msg_Err( p_demux, "can't find playlist" );
137         return -1;
138     }
139     p_sys->p_playlist = p_playlist;
140
141     b_play = FindItem( p_demux, p_playlist, &p_current );
142
143     playlist_ItemToNode( p_playlist, p_current );
144     p_current->input.i_type = ITEM_TYPE_PLAYLIST;
145     if( b_shoutcast )
146     {
147         p_genre = playlist_NodeCreate( p_playlist, p_current->pp_parents[0]->i_view, "Genre", p_current );
148         playlist_CopyParents( p_current, p_genre );
149
150         p_bitrate = playlist_NodeCreate( p_playlist, p_current->pp_parents[0]->i_view, "Bitrate", p_current );
151         playlist_CopyParents( p_current, p_bitrate );
152     }
153     
154     p_xml = p_sys->p_xml = xml_Create( p_demux );
155     if( !p_xml ) return -1;
156
157     psz_elname = stream_ReadLine( p_demux->s );
158     if( psz_elname ) free( psz_elname );
159     psz_elname = 0;
160
161     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
162     if( !p_xml_reader ) return -1;
163     p_sys->p_xml_reader = p_xml_reader;
164
165     /* xml */
166     /* check root node */
167     if( xml_ReaderRead( p_xml_reader ) != 1 )
168     {
169         msg_Err( p_demux, "invalid file (no root node)" );
170         return -1;
171     }
172
173     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
174         ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
175         strcmp( psz_elname, "WinampXML" ) )
176     {
177         msg_Err( p_demux, "invalid root node %i, %s",
178                  xml_ReaderNodeType( p_xml_reader ), psz_elname );
179         if( psz_elname ) free( psz_elname );
180         return -1;
181     }
182     free( psz_elname );
183
184     /* root node should not have any attributes, and should only
185      * contain the "playlist node */
186
187     /* Skip until 1st child node */
188     while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 &&
189            xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
190     if( i_ret != 1 )
191     {
192         msg_Err( p_demux, "invalid file (no child node)" );
193         return -1;
194     }
195
196     if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
197         strcmp( psz_elname, "playlist" ) )
198     {
199         msg_Err( p_demux, "invalid child node %s", psz_elname );
200         if( psz_elname ) free( psz_elname );
201         return -1;
202     }
203     free( psz_elname ); psz_elname = 0;
204
205     // Read the attributes
206     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
207     {
208         char *psz_name = xml_ReaderName( p_xml_reader );
209         char *psz_value = xml_ReaderValue( p_xml_reader );
210         if( !psz_name || !psz_value ) return -1;
211         if( !strcmp( psz_name, "num_entries" ) )
212         {
213             msg_Dbg( p_demux, "playlist has %d entries", atoi(psz_value) );
214         }
215         else if( !strcmp( psz_name, "label" ) )
216         {
217             playlist_ItemSetName( p_current, psz_value );
218         }
219         else
220         {
221             msg_Warn( p_demux, "stray attribute %s with value %s in element"
222                       " 'playlist'", psz_name, psz_value );
223         }
224         free( psz_name );
225         free( psz_value );
226     }
227
228     while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
229     {
230         // Get the node type
231         i_type = xml_ReaderNodeType( p_xml_reader );
232         switch( i_type )
233         {
234             // Error
235             case -1:
236                 return -1;
237                 break;
238
239             case XML_READER_STARTELEM:
240             {
241                 // Read the element name
242                 if( psz_elname ) free( psz_elname );
243                 psz_elname = xml_ReaderName( p_xml_reader );
244                 if( !psz_elname ) return -1;
245                 
246
247                 // Read the attributes
248                 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
249                 {
250                     char *psz_name = xml_ReaderName( p_xml_reader );
251                     char *psz_value = xml_ReaderValue( p_xml_reader );
252                     if( !psz_name || !psz_value ) return -1;
253                     if( !strcmp( psz_elname, "entry" ) &&
254                         !strcmp( psz_name, "Playstring" ) )
255                     {
256                         psz_mrl = strdup( psz_value );
257                     }
258                     else
259                     {
260                         msg_Warn( p_demux, "unexpected attribure %s in element %s",
261                                   psz_name, psz_elname );
262                     }
263                     free( psz_name );
264                     free( psz_value );
265                 }
266                 break;
267             }
268             case XML_READER_TEXT:
269             {
270                 char *psz_text = xml_ReaderValue( p_xml_reader );
271                 if( IsWhitespace( psz_text ) )
272                 {
273                     free( psz_text );
274                     break;
275                 }
276                 if( !strcmp( psz_elname, "Name" ) )
277                 {
278                     psz_name = strdup( psz_text );
279                 }
280                 else if( !strcmp( psz_elname, "Genre" ) )
281                 {
282                     psz_genre = strdup( psz_text );
283                 }
284                 else if( !strcmp( psz_elname, "Nowplaying" ) )
285                 {
286                     psz_now = strdup( psz_text );
287                 }
288                 else if( !strcmp( psz_elname, "Listeners" ) )
289                 {
290                     psz_listeners = strdup( psz_text );
291                 }
292                 else if( !strcmp( psz_elname, "Bitrate" ) )
293                 {
294                     psz_bitrate = strdup( psz_text );
295                 }
296                 else if( !strcmp( psz_elname, "" ) )
297                 {
298                     ;
299                 }
300                 else
301                 {
302                     msg_Warn( p_demux, "unexpected text in element '%s'",
303                               psz_elname );
304                 }
305                 free( psz_text );
306                 break;
307             }
308             // End element
309             case XML_READER_ENDELEM:
310             {
311                 // Read the element name
312                 free( psz_elname );
313                 psz_elname = xml_ReaderName( p_xml_reader );
314                 if( !psz_elname ) return -1;
315                 if( !strcmp( psz_elname, "entry" ) )
316                 {
317                     p_item = playlist_ItemNew( p_playlist, psz_mrl, psz_name );
318                     if( psz_now )
319                     {
320                         vlc_input_item_AddInfo( &(p_item->input),
321                                                 _("Meta-information"),
322                                                 _( VLC_META_NOW_PLAYING ),
323                                                 "%s",
324                                                 psz_now );
325                     }
326                     if( psz_genre )
327                     {
328                         vlc_input_item_AddInfo( &p_item->input,
329                                                 _("Meta-information"),
330                                                 _( VLC_META_GENRE ),
331                                                 "%s",
332                                                 psz_genre );
333                     }
334                     if( psz_listeners )
335                     {
336                         vlc_input_item_AddInfo( &p_item->input,
337                                                 _("Meta-information"),
338                                                 _( "Listeners" ),
339                                                 "%s",
340                                                 psz_listeners );
341                     }
342                     if( psz_bitrate )
343                     {
344                         vlc_input_item_AddInfo( &p_item->input,
345                                                 _("Meta-information"),
346                                                 _( "Bitrate" ),
347                                                 "%s",
348                                                 psz_bitrate );
349                     }
350                     playlist_NodeAddItem( p_playlist, p_item,
351                                           p_current->pp_parents[0]->i_view,
352                                           p_current, PLAYLIST_APPEND,
353                                           PLAYLIST_END );
354
355                     /* We need to declare the parents of the node as the
356                      *                  * same of the parent's ones */
357                     playlist_CopyParents( p_current, p_item );
358                     
359                     vlc_input_item_CopyOptions( &p_current->input,
360                                                 &p_item->input );
361                     if( b_shoutcast )
362                         ShoutcastAdd( p_playlist, p_genre, p_bitrate, p_item,
363                                       psz_genre, psz_bitrate );
364 #define FREE(a) if( a ) free( a ); a = NULL;
365                     FREE( psz_name );
366                     FREE( psz_mrl );
367                     FREE( psz_genre );
368                     FREE( psz_bitrate );
369                     FREE( psz_listeners );
370                     FREE( psz_now );
371 #undef FREE
372                 }
373                 free( psz_elname );
374                 psz_elname = strdup("");
375
376                 break;
377             }
378         }
379     }
380
381     if( i_ret != 0 )
382     {
383         msg_Warn( p_demux, "error while parsing data" );
384     }
385     if( b_shoutcast )
386     {
387         vlc_mutex_lock( &p_playlist->object_lock );
388         playlist_NodeSort( p_playlist, p_bitrate, SORT_TITLE_NUMERIC, ORDER_NORMAL );
389         vlc_mutex_unlock( &p_playlist->object_lock );
390     }
391
392     /* Go back and play the playlist */
393     if( b_play )
394     {
395         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
396                           p_playlist->status.i_view,
397                           p_playlist->status.p_item, NULL );
398     }
399     
400     vlc_object_release( p_playlist );
401     p_sys->p_playlist = NULL;
402     return VLC_SUCCESS;
403 }
404
405 static int Control( demux_t *p_demux, int i_query, va_list args )
406 {
407     return VLC_EGENERIC;
408 }
409
410 static int IsWhitespace( char *psz_string )
411 {
412     while( *psz_string )
413     {
414         if( *psz_string != ' ' && *psz_string != '\t' && *psz_string != '\r' &&
415             *psz_string != '\n' )
416         {
417             return VLC_FALSE;
418         }
419         psz_string++;
420     }
421     return VLC_TRUE;
422 }
423
424 static void ShoutcastAdd( playlist_t *p_playlist, playlist_item_t* p_genre,
425                           playlist_item_t *p_bitrate, playlist_item_t *p_item,
426                           char *psz_genre, char *psz_bitrate )
427 {
428     playlist_item_t *p_parent;
429     if( psz_bitrate )
430     {
431         playlist_item_t *p_copy = playlist_ItemCopy(p_playlist,p_item);
432         p_parent = playlist_ChildSearchName( p_bitrate, psz_bitrate );
433         if( !p_parent )
434         {
435             p_parent = playlist_NodeCreate( p_playlist, p_genre->pp_parents[0]->i_view, psz_bitrate,
436                                             p_bitrate );
437             playlist_CopyParents( p_bitrate, p_parent );
438         }
439         playlist_NodeAddItem( p_playlist, p_copy, p_parent->pp_parents[0]->i_view, p_parent, PLAYLIST_APPEND, PLAYLIST_END  );
440         playlist_CopyParents( p_parent, p_copy );
441
442     }
443
444     if( psz_genre )
445     {
446         playlist_item_t *p_copy = playlist_ItemCopy(p_playlist,p_item);
447         p_parent = playlist_ChildSearchName( p_genre, psz_genre );
448         if( !p_parent )
449         {
450             p_parent = playlist_NodeCreate( p_playlist, p_genre->pp_parents[0]->i_view, psz_genre,
451                                             p_genre );
452             playlist_CopyParents( p_genre, p_parent );
453         }
454         playlist_NodeAddItem( p_playlist, p_copy, p_parent->pp_parents[0]->i_view, p_parent, PLAYLIST_APPEND, PLAYLIST_END );
455         playlist_CopyParents( p_parent, p_copy );
456     }
457 }