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 playlist_t *p_playlist;
108 playlist_item_t *p_item, *p_current;
113 xml_reader_t *p_xml_reader;
114 char *psz_elname = NULL;
116 char *psz_mrl = NULL, *psz_name = NULL, *psz_genre = NULL;
117 char *psz_now = NULL, *psz_listeners = NULL, *psz_bitrate = NULL;
118 demux_sys_t *p_sys = p_demux->p_sys;
121 p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
125 msg_Err( p_demux, "can't find playlist" );
128 p_sys->p_playlist = p_playlist;
130 b_play = FindItem( p_demux, p_playlist, &p_current );
132 playlist_ItemToNode( p_playlist, p_current );
133 p_current->input.i_type = ITEM_TYPE_PLAYLIST;
135 p_xml = xml_Create( p_demux );
136 if( !p_xml ) return -1;
137 p_sys->p_xml = p_xml;
139 stream_ReadLine( p_demux->s );
140 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
141 if( !p_xml_reader ) return -1;
142 p_sys->p_xml_reader = p_xml_reader;
145 /* check root node */
146 i_ret = xml_ReaderRead( p_xml_reader );
148 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
149 ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
150 strcmp( psz_elname, "WinampXML" ) )
152 msg_Err( p_demux, "invalid file %i,%s", xml_ReaderNodeType( p_xml_reader ), psz_elname );
153 if( psz_elname ) free( psz_elname );
158 /* root node should not have any attributes, and should only
159 * contain the "playlist node */
160 i_ret = xml_ReaderRead( p_xml_reader );
161 if( i_ret == 1 && xml_ReaderNodeType( p_xml_reader ) == XML_READER_TEXT )
163 i_ret = xml_ReaderRead( p_xml_reader );
166 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
167 ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
168 strcmp( psz_elname, "playlist" ) )
170 msg_Err( p_demux, "invalid file %i,%s", xml_ReaderNodeType( p_xml_reader ), psz_elname );
171 msg_Err( p_demux, "invalid file" );
172 if( psz_elname ) free( psz_elname );
175 // Read the attributes
176 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
178 char *psz_name = xml_ReaderName( p_xml_reader );
179 char *psz_value = xml_ReaderValue( p_xml_reader );
180 if( !psz_name || !psz_value ) return -1;
181 if( !strcmp( psz_name, "num_entries" ) )
183 msg_Dbg( p_demux, "playlist has %d entries", atoi(psz_value) );
185 else if( !strcmp( psz_name, "label" ) )
187 playlist_ItemSetName( p_current, psz_value );
191 msg_Warn( p_demux, "stray attribute %s with value %s in element"
192 " 'playlist'", psz_name, psz_value );
198 i_ret = xml_ReaderRead( p_xml_reader );
202 i_type = xml_ReaderNodeType( p_xml_reader );
210 case XML_READER_STARTELEM:
212 // Read the element name
214 psz_elname = xml_ReaderName( p_xml_reader );
215 if( !psz_elname ) return -1;
218 // Read the attributes
219 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
221 char *psz_name = xml_ReaderName( p_xml_reader );
222 char *psz_value = xml_ReaderValue( p_xml_reader );
223 if( !psz_name || !psz_value ) return -1;
224 if( !strcmp( psz_elname, "entry" ) &&
225 !strcmp( psz_name, "Playstring" ) )
227 psz_mrl = strdup( psz_value );
231 msg_Warn( p_demux, "unexpected attribure %s in element %s",
232 psz_name, psz_elname );
239 case XML_READER_TEXT:
241 char *psz_text = xml_ReaderValue( p_xml_reader );
242 if( IsWhitespace( psz_text ) )
247 if( !strcmp( psz_elname, "Name" ) )
249 psz_name = strdup( psz_text );
251 else if( !strcmp( psz_elname, "Genre" ) )
253 psz_genre = strdup( psz_text );
255 else if( !strcmp( psz_elname, "Nowplaying" ) )
257 psz_now = strdup( psz_text );
259 else if( !strcmp( psz_elname, "Listeners" ) )
261 psz_listeners = strdup( psz_text );
263 else if( !strcmp( psz_elname, "Bitrate" ) )
265 psz_bitrate = strdup( psz_text );
267 else if( !strcmp( psz_elname, "" ) )
273 msg_Warn( p_demux, "unexpected text in element '%s'",
280 case XML_READER_ENDELEM:
282 // Read the element name
284 psz_elname = xml_ReaderName( p_xml_reader );
285 if( !psz_elname ) return -1;
286 if( !strcmp( psz_elname, "entry" ) )
288 p_item = playlist_ItemNew( p_playlist, psz_mrl, psz_name );
291 vlc_input_item_AddInfo( &(p_item->input),
292 _("Meta-information"),
293 _( VLC_META_NOW_PLAYING ),
299 vlc_input_item_AddInfo( &p_item->input,
300 _("Meta-information"),
307 vlc_input_item_AddInfo( &p_item->input,
308 _("Meta-information"),
315 vlc_input_item_AddInfo( &p_item->input,
316 _("Meta-information"),
321 playlist_NodeAddItem( p_playlist, p_item,
322 p_current->pp_parents[0]->i_view,
323 p_current, PLAYLIST_APPEND,
326 /* We need to declare the parents of the node as the
327 * * same of the parent's ones */
328 playlist_CopyParents( p_current, p_item );
330 vlc_input_item_CopyOptions( &p_current->input,
332 #define FREE(a) if( a ) free( a ); a = NULL;
337 FREE( psz_listeners );
342 psz_elname = strdup("");
347 i_ret = xml_ReaderRead( p_xml_reader );
349 if( i_ret != 0 ) return -1;
352 /* Go back and play the playlist */
355 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
356 p_playlist->status.i_view,
357 p_playlist->status.p_item, NULL );
360 vlc_object_release( p_playlist );
361 p_sys->p_playlist = NULL;
365 static int Control( demux_t *p_demux, int i_query, va_list args )
370 static int IsWhitespace( char *psz_string )
374 if( *psz_string != ' ' && *psz_string != '\t' && *psz_string != '\r' &&
375 *psz_string != '\n' )