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 Import_B4S( vlc_object_t *p_this )
61 demux_t *p_demux = (demux_t *)p_this;
65 psz_ext = strrchr ( p_demux->psz_path, '.' );
67 if( ( psz_ext && !strcasecmp( psz_ext, ".b4s") ) ||
68 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "b4s-open") ) ||
69 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "shout-b4s") ) )
77 msg_Dbg( p_demux, "using b4s playlist import");
79 p_demux->pf_control = Control;
80 p_demux->pf_demux = Demux;
81 p_demux->p_sys = malloc( sizeof(demux_sys_t) );
82 if( p_demux->p_sys == NULL )
84 msg_Err( p_demux, "Out of memory" );
87 p_demux->p_sys->b_shout = p_demux->psz_demux &&
88 !strcmp(p_demux->psz_demux, "shout-b4s");
89 p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
94 /*****************************************************************************
95 * Deactivate: frees unused data
96 *****************************************************************************/
97 void Close_B4S( vlc_object_t *p_this )
99 demux_t *p_demux = (demux_t *)p_this;
100 demux_sys_t *p_sys = p_demux->p_sys;
102 if( p_sys->psz_prefix ) free( p_sys->psz_prefix );
103 if( p_sys->p_playlist ) vlc_object_release( p_sys->p_playlist );
104 if( p_sys->p_xml_reader ) xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
105 if( p_sys->p_xml ) xml_Delete( p_sys->p_xml );
109 static int Demux( demux_t *p_demux )
111 demux_sys_t *p_sys = p_demux->p_sys;
112 playlist_t *p_playlist;
113 playlist_item_t *p_item, *p_current, *p_bitrate, *p_genre;
119 xml_reader_t *p_xml_reader;
120 char *psz_elname = NULL;
121 int i_type, b_shoutcast;
122 char *psz_mrl = NULL, *psz_name = NULL, *psz_genre = NULL;
123 char *psz_now = NULL, *psz_listeners = NULL, *psz_bitrate = NULL;
126 b_shoutcast = p_sys->b_shout;
128 p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
132 msg_Err( p_demux, "can't find playlist" );
135 p_sys->p_playlist = p_playlist;
137 b_play = FindItem( p_demux, p_playlist, &p_current );
139 playlist_ItemToNode( p_playlist, p_current );
140 p_current->input.i_type = ITEM_TYPE_PLAYLIST;
143 p_genre = playlist_NodeCreate( p_playlist, p_current->pp_parents[0]->i_view, "Genre", p_current );
144 playlist_CopyParents( p_current, p_genre );
146 p_bitrate = playlist_NodeCreate( p_playlist, p_current->pp_parents[0]->i_view, "Bitrate", p_current );
147 playlist_CopyParents( p_current, p_bitrate );
150 p_xml = p_sys->p_xml = xml_Create( p_demux );
151 if( !p_xml ) return -1;
153 stream_ReadLine( p_demux->s );
154 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
155 if( !p_xml_reader ) return -1;
156 p_sys->p_xml_reader = p_xml_reader;
159 /* check root node */
160 if( xml_ReaderRead( p_xml_reader ) != 1 )
162 msg_Err( p_demux, "invalid file (no root node)" );
166 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
167 ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
168 strcmp( psz_elname, "WinampXML" ) )
170 msg_Err( p_demux, "invalid root node %i, %s",
171 xml_ReaderNodeType( p_xml_reader ), psz_elname );
172 if( psz_elname ) free( psz_elname );
177 /* root node should not have any attributes, and should only
178 * contain the "playlist node */
180 /* Skip until 1st child node */
181 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 &&
182 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
185 msg_Err( p_demux, "invalid file (no child node)" );
189 if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
190 strcmp( psz_elname, "playlist" ) )
192 msg_Err( p_demux, "invalid child node %s", psz_elname );
193 if( psz_elname ) free( psz_elname );
196 free( psz_elname ); psz_elname = 0;
198 // Read the attributes
199 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
201 char *psz_name = xml_ReaderName( p_xml_reader );
202 char *psz_value = xml_ReaderValue( p_xml_reader );
203 if( !psz_name || !psz_value ) return -1;
204 if( !strcmp( psz_name, "num_entries" ) )
206 msg_Dbg( p_demux, "playlist has %d entries", atoi(psz_value) );
208 else if( !strcmp( psz_name, "label" ) )
210 playlist_ItemSetName( p_current, psz_value );
214 msg_Warn( p_demux, "stray attribute %s with value %s in element"
215 " 'playlist'", psz_name, psz_value );
221 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
224 i_type = xml_ReaderNodeType( p_xml_reader );
232 case XML_READER_STARTELEM:
234 // Read the element name
235 if( psz_elname ) free( psz_elname );
236 psz_elname = xml_ReaderName( p_xml_reader );
237 if( !psz_elname ) return -1;
240 // Read the attributes
241 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
243 char *psz_name = xml_ReaderName( p_xml_reader );
244 char *psz_value = xml_ReaderValue( p_xml_reader );
245 if( !psz_name || !psz_value ) return -1;
246 if( !strcmp( psz_elname, "entry" ) &&
247 !strcmp( psz_name, "Playstring" ) )
249 psz_mrl = strdup( psz_value );
253 msg_Warn( p_demux, "unexpected attribure %s in element %s",
254 psz_name, psz_elname );
261 case XML_READER_TEXT:
263 char *psz_text = xml_ReaderValue( p_xml_reader );
264 if( IsWhitespace( psz_text ) )
269 if( !strcmp( psz_elname, "Name" ) )
271 psz_name = strdup( psz_text );
273 else if( !strcmp( psz_elname, "Genre" ) )
275 psz_genre = strdup( psz_text );
277 else if( !strcmp( psz_elname, "Nowplaying" ) )
279 psz_now = strdup( psz_text );
281 else if( !strcmp( psz_elname, "Listeners" ) )
283 psz_listeners = strdup( psz_text );
285 else if( !strcmp( psz_elname, "Bitrate" ) )
287 psz_bitrate = strdup( psz_text );
289 else if( !strcmp( psz_elname, "" ) )
295 msg_Warn( p_demux, "unexpected text in element '%s'",
302 case XML_READER_ENDELEM:
304 // Read the element name
306 psz_elname = xml_ReaderName( p_xml_reader );
307 if( !psz_elname ) return -1;
308 if( !strcmp( psz_elname, "entry" ) )
310 p_item = playlist_ItemNew( p_playlist, psz_mrl, psz_name );
313 vlc_input_item_AddInfo( &(p_item->input),
314 _("Meta-information"),
315 _( VLC_META_NOW_PLAYING ),
321 vlc_input_item_AddInfo( &p_item->input,
322 _("Meta-information"),
329 vlc_input_item_AddInfo( &p_item->input,
330 _("Meta-information"),
337 vlc_input_item_AddInfo( &p_item->input,
338 _("Meta-information"),
343 playlist_NodeAddItem( p_playlist, p_item,
344 p_current->pp_parents[0]->i_view,
345 p_current, PLAYLIST_APPEND,
348 /* We need to declare the parents of the node as the
349 * * same of the parent's ones */
350 playlist_CopyParents( p_current, p_item );
352 vlc_input_item_CopyOptions( &p_current->input,
355 ShoutcastAdd( p_playlist, p_genre, p_bitrate, p_item,
356 psz_genre, psz_bitrate );
357 #define FREE(a) if( a ) free( a ); a = NULL;
362 FREE( psz_listeners );
367 psz_elname = strdup("");
376 msg_Warn( p_demux, "error while parsing data" );
380 vlc_mutex_lock( &p_playlist->object_lock );
381 playlist_NodeSort( p_playlist, p_bitrate, SORT_TITLE_NUMERIC, ORDER_NORMAL );
382 vlc_mutex_unlock( &p_playlist->object_lock );
385 /* Go back and play the playlist */
388 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
389 p_playlist->status.i_view,
390 p_playlist->status.p_item, NULL );
393 vlc_object_release( p_playlist );
394 p_sys->p_playlist = NULL;
398 static int Control( demux_t *p_demux, int i_query, va_list args )
403 static int IsWhitespace( char *psz_string )
407 if( *psz_string != ' ' && *psz_string != '\t' && *psz_string != '\r' &&
408 *psz_string != '\n' )
417 static void ShoutcastAdd( playlist_t *p_playlist, playlist_item_t* p_genre,
418 playlist_item_t *p_bitrate, playlist_item_t *p_item,
419 char *psz_genre, char *psz_bitrate )
421 playlist_item_t *p_parent;
424 playlist_item_t *p_copy = playlist_ItemCopy(p_playlist,p_item);
425 p_parent = playlist_ChildSearchName( p_bitrate, psz_bitrate );
428 p_parent = playlist_NodeCreate( p_playlist, p_genre->pp_parents[0]->i_view, psz_bitrate,
430 playlist_CopyParents( p_bitrate, p_parent );
432 playlist_NodeAddItem( p_playlist, p_copy, p_parent->pp_parents[0]->i_view, p_parent, PLAYLIST_APPEND, PLAYLIST_END );
433 playlist_CopyParents( p_parent, p_copy );
439 playlist_item_t *p_copy = playlist_ItemCopy(p_playlist,p_item);
440 p_parent = playlist_ChildSearchName( p_genre, psz_genre );
443 p_parent = playlist_NodeCreate( p_playlist, p_genre->pp_parents[0]->i_view, psz_genre,
445 playlist_CopyParents( p_genre, p_parent );
447 playlist_NodeAddItem( p_playlist, p_copy, p_parent->pp_parents[0]->i_view, p_parent, PLAYLIST_APPEND, PLAYLIST_END );
448 playlist_CopyParents( p_parent, p_copy );