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