]> git.sesse.net Git - vlc/blob - modules/demux/playlist/b4s.c
Fix invalid free() on exit
[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     stream_ReadLine( p_demux->s );
158     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
159     if( !p_xml_reader ) return -1;
160     p_sys->p_xml_reader = p_xml_reader;
161
162     /* xml */
163     /* check root node */
164     if( xml_ReaderRead( p_xml_reader ) != 1 )
165     {
166         msg_Err( p_demux, "invalid file (no root node)" );
167         return -1;
168     }
169
170     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
171         ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
172         strcmp( psz_elname, "WinampXML" ) )
173     {
174         msg_Err( p_demux, "invalid root node %i, %s",
175                  xml_ReaderNodeType( p_xml_reader ), psz_elname );
176         if( psz_elname ) free( psz_elname );
177         return -1;
178     }
179     free( psz_elname );
180
181     /* root node should not have any attributes, and should only
182      * contain the "playlist node */
183
184     /* Skip until 1st child node */
185     while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 &&
186            xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
187     if( i_ret != 1 )
188     {
189         msg_Err( p_demux, "invalid file (no child node)" );
190         return -1;
191     }
192
193     if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
194         strcmp( psz_elname, "playlist" ) )
195     {
196         msg_Err( p_demux, "invalid child node %s", psz_elname );
197         if( psz_elname ) free( psz_elname );
198         return -1;
199     }
200     free( psz_elname ); psz_elname = 0;
201
202     // Read the attributes
203     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
204     {
205         char *psz_name = xml_ReaderName( p_xml_reader );
206         char *psz_value = xml_ReaderValue( p_xml_reader );
207         if( !psz_name || !psz_value ) return -1;
208         if( !strcmp( psz_name, "num_entries" ) )
209         {
210             msg_Dbg( p_demux, "playlist has %d entries", atoi(psz_value) );
211         }
212         else if( !strcmp( psz_name, "label" ) )
213         {
214             playlist_ItemSetName( p_current, psz_value );
215         }
216         else
217         {
218             msg_Warn( p_demux, "stray attribute %s with value %s in element"
219                       " 'playlist'", psz_name, psz_value );
220         }
221         free( psz_name );
222         free( psz_value );
223     }
224
225     while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
226     {
227         // Get the node type
228         i_type = xml_ReaderNodeType( p_xml_reader );
229         switch( i_type )
230         {
231             // Error
232             case -1:
233                 return -1;
234                 break;
235
236             case XML_READER_STARTELEM:
237             {
238                 // Read the element name
239                 if( psz_elname ) free( psz_elname );
240                 psz_elname = xml_ReaderName( p_xml_reader );
241                 if( !psz_elname ) return -1;
242                 
243
244                 // Read the attributes
245                 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
246                 {
247                     char *psz_name = xml_ReaderName( p_xml_reader );
248                     char *psz_value = xml_ReaderValue( p_xml_reader );
249                     if( !psz_name || !psz_value ) return -1;
250                     if( !strcmp( psz_elname, "entry" ) &&
251                         !strcmp( psz_name, "Playstring" ) )
252                     {
253                         psz_mrl = strdup( psz_value );
254                     }
255                     else
256                     {
257                         msg_Warn( p_demux, "unexpected attribure %s in element %s",
258                                   psz_name, psz_elname );
259                     }
260                     free( psz_name );
261                     free( psz_value );
262                 }
263                 break;
264             }
265             case XML_READER_TEXT:
266             {
267                 char *psz_text = xml_ReaderValue( p_xml_reader );
268                 if( IsWhitespace( psz_text ) )
269                 {
270                     free( psz_text );
271                     break;
272                 }
273                 if( !strcmp( psz_elname, "Name" ) )
274                 {
275                     psz_name = strdup( psz_text );
276                 }
277                 else if( !strcmp( psz_elname, "Genre" ) )
278                 {
279                     psz_genre = strdup( psz_text );
280                 }
281                 else if( !strcmp( psz_elname, "Nowplaying" ) )
282                 {
283                     psz_now = strdup( psz_text );
284                 }
285                 else if( !strcmp( psz_elname, "Listeners" ) )
286                 {
287                     psz_listeners = strdup( psz_text );
288                 }
289                 else if( !strcmp( psz_elname, "Bitrate" ) )
290                 {
291                     psz_bitrate = strdup( psz_text );
292                 }
293                 else if( !strcmp( psz_elname, "" ) )
294                 {
295                     ;
296                 }
297                 else
298                 {
299                     msg_Warn( p_demux, "unexpected text in element '%s'",
300                               psz_elname );
301                 }
302                 free( psz_text );
303                 break;
304             }
305             // End element
306             case XML_READER_ENDELEM:
307             {
308                 // Read the element name
309                 free( psz_elname );
310                 psz_elname = xml_ReaderName( p_xml_reader );
311                 if( !psz_elname ) return -1;
312                 if( !strcmp( psz_elname, "entry" ) )
313                 {
314                     p_item = playlist_ItemNew( p_playlist, psz_mrl, psz_name );
315                     if( psz_now )
316                     {
317                         vlc_input_item_AddInfo( &(p_item->input),
318                                                 _("Meta-information"),
319                                                 _( VLC_META_NOW_PLAYING ),
320                                                 "%s",
321                                                 psz_now );
322                     }
323                     if( psz_genre )
324                     {
325                         vlc_input_item_AddInfo( &p_item->input,
326                                                 _("Meta-information"),
327                                                 _( VLC_META_GENRE ),
328                                                 "%s",
329                                                 psz_genre );
330                     }
331                     if( psz_listeners )
332                     {
333                         vlc_input_item_AddInfo( &p_item->input,
334                                                 _("Meta-information"),
335                                                 _( "Listeners" ),
336                                                 "%s",
337                                                 psz_listeners );
338                     }
339                     if( psz_bitrate )
340                     {
341                         vlc_input_item_AddInfo( &p_item->input,
342                                                 _("Meta-information"),
343                                                 _( "Bitrate" ),
344                                                 "%s",
345                                                 psz_bitrate );
346                     }
347                     playlist_NodeAddItem( p_playlist, p_item,
348                                           p_current->pp_parents[0]->i_view,
349                                           p_current, PLAYLIST_APPEND,
350                                           PLAYLIST_END );
351
352                     /* We need to declare the parents of the node as the
353                      *                  * same of the parent's ones */
354                     playlist_CopyParents( p_current, p_item );
355                     
356                     vlc_input_item_CopyOptions( &p_current->input,
357                                                 &p_item->input );
358                     if( b_shoutcast )
359                         ShoutcastAdd( p_playlist, p_genre, p_bitrate, p_item,
360                                       psz_genre, psz_bitrate );
361 #define FREE(a) if( a ) free( a ); a = NULL;
362                     FREE( psz_name );
363                     FREE( psz_mrl );
364                     FREE( psz_genre );
365                     FREE( psz_bitrate );
366                     FREE( psz_listeners );
367                     FREE( psz_now );
368 #undef FREE
369                 }
370                 free( psz_elname );
371                 psz_elname = strdup("");
372
373                 break;
374             }
375         }
376     }
377
378     if( i_ret != 0 )
379     {
380         msg_Warn( p_demux, "error while parsing data" );
381     }
382     if( b_shoutcast )
383     {
384         vlc_mutex_lock( &p_playlist->object_lock );
385         playlist_NodeSort( p_playlist, p_bitrate, SORT_TITLE_NUMERIC, ORDER_NORMAL );
386         vlc_mutex_unlock( &p_playlist->object_lock );
387     }
388
389     /* Go back and play the playlist */
390     if( b_play )
391     {
392         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
393                           p_playlist->status.i_view,
394                           p_playlist->status.p_item, NULL );
395     }
396     
397     vlc_object_release( p_playlist );
398     p_sys->p_playlist = NULL;
399     return VLC_SUCCESS;
400 }
401
402 static int Control( demux_t *p_demux, int i_query, va_list args )
403 {
404     return VLC_EGENERIC;
405 }
406
407 static int IsWhitespace( char *psz_string )
408 {
409     while( *psz_string )
410     {
411         if( *psz_string != ' ' && *psz_string != '\t' && *psz_string != '\r' &&
412             *psz_string != '\n' )
413         {
414             return VLC_FALSE;
415         }
416         psz_string++;
417     }
418     return VLC_TRUE;
419 }
420
421 static void ShoutcastAdd( playlist_t *p_playlist, playlist_item_t* p_genre,
422                           playlist_item_t *p_bitrate, playlist_item_t *p_item,
423                           char *psz_genre, char *psz_bitrate )
424 {
425     playlist_item_t *p_parent;
426     if( psz_bitrate )
427     {
428         playlist_item_t *p_copy = playlist_ItemCopy(p_playlist,p_item);
429         p_parent = playlist_ChildSearchName( p_bitrate, psz_bitrate );
430         if( !p_parent )
431         {
432             p_parent = playlist_NodeCreate( p_playlist, p_genre->pp_parents[0]->i_view, psz_bitrate,
433                                             p_bitrate );
434             playlist_CopyParents( p_bitrate, p_parent );
435         }
436         playlist_NodeAddItem( p_playlist, p_copy, p_parent->pp_parents[0]->i_view, p_parent, PLAYLIST_APPEND, PLAYLIST_END  );
437         playlist_CopyParents( p_parent, p_copy );
438
439     }
440
441     if( psz_genre )
442     {
443         playlist_item_t *p_copy = playlist_ItemCopy(p_playlist,p_item);
444         p_parent = playlist_ChildSearchName( p_genre, psz_genre );
445         if( !p_parent )
446         {
447             p_parent = playlist_NodeCreate( p_playlist, p_genre->pp_parents[0]->i_view, psz_genre,
448                                             p_genre );
449             playlist_CopyParents( p_genre, p_parent );
450         }
451         playlist_NodeAddItem( p_playlist, p_copy, p_parent->pp_parents[0]->i_view, p_parent, PLAYLIST_APPEND, PLAYLIST_END );
452         playlist_CopyParents( p_parent, p_copy );
453     }
454 }