1 /*****************************************************************************
2 * b4s.c : B4S playlist format import
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
5 * $Id: m3u.c 10101 2005-03-02 16:47:31Z robux4 $
7 * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
33 #include <errno.h> /* ENOMEM */
40 playlist_t *p_playlist;
42 xml_reader_t *p_xml_reader;
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Demux( demux_t *p_demux);
49 static int Control( demux_t *p_demux, int i_query, va_list args );
50 static int IsWhitespace( char *psz_string );
52 /*****************************************************************************
53 * Import_B4S: main import function
54 *****************************************************************************/
55 int Import_B4S( vlc_object_t *p_this )
57 demux_t *p_demux = (demux_t *)p_this;
61 psz_ext = strrchr ( p_demux->psz_path, '.' );
63 if( ( psz_ext && !strcasecmp( psz_ext, ".b4s") ) ||
64 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "b4s") ) )
72 msg_Dbg( p_demux, "using b4s playlist import");
74 p_demux->pf_control = Control;
75 p_demux->pf_demux = Demux;
76 p_demux->p_sys = malloc( sizeof(demux_sys_t) );
77 if( p_demux->p_sys == NULL )
79 msg_Err( p_demux, "Out of memory" );
82 p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
87 /*****************************************************************************
88 * Deactivate: frees unused data
89 *****************************************************************************/
90 void Close_B4S( vlc_object_t *p_this )
92 demux_t *p_demux = (demux_t *)p_this;
93 demux_sys_t *p_sys = p_demux->p_sys;
95 if( p_sys->psz_prefix ) free( p_sys->psz_prefix );
96 if( p_sys->p_playlist ) vlc_object_release( p_sys->p_playlist );
97 if( p_sys->p_xml_reader ) xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
98 if( p_sys->p_xml ) xml_Delete( p_sys->p_xml );
102 static int Demux( demux_t *p_demux )
104 demux_sys_t *p_sys = p_demux->p_sys;
105 playlist_t *p_playlist;
106 playlist_item_t *p_item, *p_current;
112 xml_reader_t *p_xml_reader;
113 char *psz_elname = NULL;
115 char *psz_mrl = NULL, *psz_name = NULL, *psz_genre = NULL;
116 char *psz_now = NULL, *psz_listeners = NULL, *psz_bitrate = NULL;
119 p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
123 msg_Err( p_demux, "can't find playlist" );
126 p_sys->p_playlist = p_playlist;
128 b_play = FindItem( p_demux, p_playlist, &p_current );
130 playlist_ItemToNode( p_playlist, p_current );
131 p_current->input.i_type = ITEM_TYPE_PLAYLIST;
133 p_xml = p_sys->p_xml = xml_Create( p_demux );
134 if( !p_xml ) return -1;
136 stream_ReadLine( p_demux->s );
137 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
138 if( !p_xml_reader ) return -1;
139 p_sys->p_xml_reader = p_xml_reader;
142 /* check root node */
143 if( xml_ReaderRead( p_xml_reader ) != 1 )
145 msg_Err( p_demux, "invalid file (no root node)" );
149 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
150 ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
151 strcmp( psz_elname, "WinampXML" ) )
153 msg_Err( p_demux, "invalid root node %i, %s",
154 xml_ReaderNodeType( p_xml_reader ), psz_elname );
155 if( psz_elname ) free( psz_elname );
160 /* root node should not have any attributes, and should only
161 * contain the "playlist node */
163 /* Skip until 1st child node */
164 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 &&
165 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
168 msg_Err( p_demux, "invalid file (no child node)" );
172 if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
173 strcmp( psz_elname, "playlist" ) )
175 msg_Err( p_demux, "invalid child node %s", psz_elname );
176 if( psz_elname ) free( psz_elname );
179 free( psz_elname ); psz_elname = 0;
181 // Read the attributes
182 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
184 char *psz_name = xml_ReaderName( p_xml_reader );
185 char *psz_value = xml_ReaderValue( p_xml_reader );
186 if( !psz_name || !psz_value ) return -1;
187 if( !strcmp( psz_name, "num_entries" ) )
189 msg_Dbg( p_demux, "playlist has %d entries", atoi(psz_value) );
191 else if( !strcmp( psz_name, "label" ) )
193 playlist_ItemSetName( p_current, psz_value );
197 msg_Warn( p_demux, "stray attribute %s with value %s in element"
198 " 'playlist'", psz_name, psz_value );
204 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
207 i_type = xml_ReaderNodeType( p_xml_reader );
215 case XML_READER_STARTELEM:
217 // Read the element name
218 if( psz_elname ) free( psz_elname );
219 psz_elname = xml_ReaderName( p_xml_reader );
220 if( !psz_elname ) return -1;
223 // Read the attributes
224 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
226 char *psz_name = xml_ReaderName( p_xml_reader );
227 char *psz_value = xml_ReaderValue( p_xml_reader );
228 if( !psz_name || !psz_value ) return -1;
229 if( !strcmp( psz_elname, "entry" ) &&
230 !strcmp( psz_name, "Playstring" ) )
232 psz_mrl = strdup( psz_value );
236 msg_Warn( p_demux, "unexpected attribure %s in element %s",
237 psz_name, psz_elname );
244 case XML_READER_TEXT:
246 char *psz_text = xml_ReaderValue( p_xml_reader );
247 if( IsWhitespace( psz_text ) )
252 if( !strcmp( psz_elname, "Name" ) )
254 psz_name = strdup( psz_text );
256 else if( !strcmp( psz_elname, "Genre" ) )
258 psz_genre = strdup( psz_text );
260 else if( !strcmp( psz_elname, "Nowplaying" ) )
262 psz_now = strdup( psz_text );
264 else if( !strcmp( psz_elname, "Listeners" ) )
266 psz_listeners = strdup( psz_text );
268 else if( !strcmp( psz_elname, "Bitrate" ) )
270 psz_bitrate = strdup( psz_text );
272 else if( !strcmp( psz_elname, "" ) )
278 msg_Warn( p_demux, "unexpected text in element '%s'",
285 case XML_READER_ENDELEM:
287 // Read the element name
289 psz_elname = xml_ReaderName( p_xml_reader );
290 if( !psz_elname ) return -1;
291 if( !strcmp( psz_elname, "entry" ) )
293 p_item = playlist_ItemNew( p_playlist, psz_mrl, psz_name );
296 vlc_input_item_AddInfo( &(p_item->input),
297 _("Meta-information"),
298 _( VLC_META_NOW_PLAYING ),
304 vlc_input_item_AddInfo( &p_item->input,
305 _("Meta-information"),
312 vlc_input_item_AddInfo( &p_item->input,
313 _("Meta-information"),
320 vlc_input_item_AddInfo( &p_item->input,
321 _("Meta-information"),
326 playlist_NodeAddItem( p_playlist, p_item,
327 p_current->pp_parents[0]->i_view,
328 p_current, PLAYLIST_APPEND,
331 /* We need to declare the parents of the node as the
332 * * same of the parent's ones */
333 playlist_CopyParents( p_current, p_item );
335 vlc_input_item_CopyOptions( &p_current->input,
337 #define FREE(a) if( a ) free( a ); a = NULL;
342 FREE( psz_listeners );
347 psz_elname = strdup("");
356 msg_Warn( p_demux, "error while parsing data" );
359 /* Go back and play the playlist */
362 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
363 p_playlist->status.i_view,
364 p_playlist->status.p_item, NULL );
367 vlc_object_release( p_playlist );
368 p_sys->p_playlist = NULL;
372 static int Control( demux_t *p_demux, int i_query, va_list args )
377 static int IsWhitespace( char *psz_string )
381 if( *psz_string != ' ' && *psz_string != '\t' && *psz_string != '\r' &&
382 *psz_string != '\n' )