1 /*****************************************************************************
2 * b4s.c : B4S playlist format import
3 *****************************************************************************
4 * Copyright (C) 2005 VideoLAN
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;
46 /*****************************************************************************
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 );
56 /*****************************************************************************
57 * Import_B4S: main import function
58 *****************************************************************************/
59 int E_(Import_B4S)( vlc_object_t *p_this )
61 demux_t *p_demux = (demux_t *)p_this;
66 psz_ext = strrchr ( p_demux->psz_path, '.' );
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") ) )
78 msg_Dbg( p_demux, "using b4s playlist import");
80 p_demux->pf_control = Control;
81 p_demux->pf_demux = Demux;
82 p_demux->p_sys = p_sys = malloc( sizeof(demux_sys_t) );
85 msg_Err( p_demux, "Out of memory" );
88 p_sys->b_shout = p_demux->psz_demux &&
89 !strcmp(p_demux->psz_demux, "shout-b4s");
90 p_sys->psz_prefix = E_(FindPrefix)( p_demux );
91 p_sys->p_playlist = NULL;
93 p_sys->p_xml_reader = NULL;
98 /*****************************************************************************
99 * Deactivate: frees unused data
100 *****************************************************************************/
101 void E_(Close_B4S)( vlc_object_t *p_this )
103 demux_t *p_demux = (demux_t *)p_this;
104 demux_sys_t *p_sys = p_demux->p_sys;
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 );
113 static int Demux( demux_t *p_demux )
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;
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;
131 b_shoutcast = p_sys->b_shout;
133 p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
137 msg_Err( p_demux, "can't find playlist" );
140 p_sys->p_playlist = p_playlist;
142 b_play = E_(FindItem)( p_demux, p_playlist, &p_current );
144 playlist_ItemToNode( p_playlist, p_current );
145 p_current->input.i_type = ITEM_TYPE_PLAYLIST;
148 p_genre = playlist_NodeCreate( p_playlist, p_current->pp_parents[0]->i_view, "Genre", p_current );
149 playlist_CopyParents( p_current, p_genre );
151 p_bitrate = playlist_NodeCreate( p_playlist, p_current->pp_parents[0]->i_view, "Bitrate", p_current );
152 playlist_CopyParents( p_current, p_bitrate );
155 p_xml = p_sys->p_xml = xml_Create( p_demux );
156 if( !p_xml ) return -1;
158 psz_elname = stream_ReadLine( p_demux->s );
159 if( psz_elname ) free( psz_elname );
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;
167 /* check root node */
168 if( xml_ReaderRead( p_xml_reader ) != 1 )
170 msg_Err( p_demux, "invalid file (no root node)" );
174 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
175 ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
176 strcmp( psz_elname, "WinampXML" ) )
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 );
185 /* root node should not have any attributes, and should only
186 * contain the "playlist node */
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 );
193 msg_Err( p_demux, "invalid file (no child node)" );
197 if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
198 strcmp( psz_elname, "playlist" ) )
200 msg_Err( p_demux, "invalid child node %s", psz_elname );
201 if( psz_elname ) free( psz_elname );
204 free( psz_elname ); psz_elname = 0;
206 // Read the attributes
207 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
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" ) )
214 msg_Dbg( p_demux, "playlist has %d entries", atoi(psz_value) );
216 else if( !strcmp( psz_name, "label" ) )
218 playlist_ItemSetName( p_current, psz_value );
222 msg_Warn( p_demux, "stray attribute %s with value %s in element"
223 " 'playlist'", psz_name, psz_value );
229 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
232 i_type = xml_ReaderNodeType( p_xml_reader );
240 case XML_READER_STARTELEM:
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;
248 // Read the attributes
249 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
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" ) )
257 psz_mrl = strdup( psz_value );
261 msg_Warn( p_demux, "unexpected attribure %s in element %s",
262 psz_name, psz_elname );
269 case XML_READER_TEXT:
271 char *psz_text = xml_ReaderValue( p_xml_reader );
272 if( IsWhitespace( psz_text ) )
277 if( !strcmp( psz_elname, "Name" ) )
279 psz_name = strdup( psz_text );
281 else if( !strcmp( psz_elname, "Genre" ) )
283 psz_genre = strdup( psz_text );
285 else if( !strcmp( psz_elname, "Nowplaying" ) )
287 psz_now = strdup( psz_text );
289 else if( !strcmp( psz_elname, "Listeners" ) )
291 psz_listeners = strdup( psz_text );
293 else if( !strcmp( psz_elname, "Bitrate" ) )
295 psz_bitrate = strdup( psz_text );
297 else if( !strcmp( psz_elname, "" ) )
303 msg_Warn( p_demux, "unexpected text in element '%s'",
310 case XML_READER_ENDELEM:
312 // Read the element name
314 psz_elname = xml_ReaderName( p_xml_reader );
315 if( !psz_elname ) return -1;
316 if( !strcmp( psz_elname, "entry" ) )
318 p_item = playlist_ItemNew( p_playlist, psz_mrl, psz_name );
321 vlc_input_item_AddInfo( &(p_item->input),
322 _("Meta-information"),
323 _( VLC_META_NOW_PLAYING ),
329 vlc_input_item_AddInfo( &p_item->input,
330 _("Meta-information"),
337 vlc_input_item_AddInfo( &p_item->input,
338 _("Meta-information"),
345 vlc_input_item_AddInfo( &p_item->input,
346 _("Meta-information"),
351 playlist_NodeAddItem( p_playlist, p_item,
352 p_current->pp_parents[0]->i_view,
353 p_current, PLAYLIST_APPEND,
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 );
360 vlc_input_item_CopyOptions( &p_current->input,
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;
370 FREE( psz_listeners );
375 psz_elname = strdup("");
384 msg_Warn( p_demux, "error while parsing data" );
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 );
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 )
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] );
403 vlc_object_release( p_playlist );
404 p_sys->p_playlist = NULL;
408 static int Control( demux_t *p_demux, int i_query, va_list args )
413 static int IsWhitespace( char *psz_string )
417 if( *psz_string != ' ' && *psz_string != '\t' && *psz_string != '\r' &&
418 *psz_string != '\n' )
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 )
431 playlist_item_t *p_parent;
434 playlist_item_t *p_copy = playlist_ItemCopy(p_playlist,p_item);
435 p_parent = playlist_ChildSearchName( p_bitrate, psz_bitrate );
438 p_parent = playlist_NodeCreate( p_playlist, p_genre->pp_parents[0]->i_view, psz_bitrate,
440 playlist_CopyParents( p_bitrate, p_parent );
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 );
449 playlist_item_t *p_copy = playlist_ItemCopy(p_playlist,p_item);
450 p_parent = playlist_ChildSearchName( p_genre, psz_genre );
453 p_parent = playlist_NodeCreate( p_playlist, p_genre->pp_parents[0]->i_view, psz_genre,
455 playlist_CopyParents( p_genre, p_parent );
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 );