1 /*****************************************************************************
2 * b4s.c : B4S playlist format import
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <ctype.h> /* isspace() */
31 #include <vlc/input.h>
34 #include <errno.h> /* ENOMEM */
41 playlist_t *p_playlist;
43 xml_reader_t *p_xml_reader;
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Demux( demux_t *p_demux);
51 static int Control( demux_t *p_demux, int i_query, va_list args );
52 static char *GetNextToken(char *psz_cur_string);
53 static int IsWhitespace( char *psz_string );
54 static void ShoutcastAdd( playlist_t *p_playlist, playlist_item_t* p_genre,
55 playlist_item_t *p_bitrate, playlist_item_t *p_item,
56 char *psz_genre, char *psz_bitrate );
58 /*****************************************************************************
59 * Import_B4S: main import function
60 *****************************************************************************/
61 int E_(Import_B4S)( vlc_object_t *p_this )
63 demux_t *p_demux = (demux_t *)p_this;
68 psz_ext = strrchr ( p_demux->psz_path, '.' );
70 if( ( psz_ext && !strcasecmp( psz_ext, ".b4s") ) ||
71 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "b4s-open") ) ||
72 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "shout-b4s") ) )
80 msg_Dbg( p_demux, "using b4s playlist import");
82 p_demux->pf_control = Control;
83 p_demux->pf_demux = Demux;
84 p_demux->p_sys = p_sys = malloc( sizeof(demux_sys_t) );
87 msg_Err( p_demux, "Out of memory" );
90 p_sys->b_shout = p_demux->psz_demux &&
91 !strcmp(p_demux->psz_demux, "shout-b4s");
92 p_sys->psz_prefix = E_(FindPrefix)( p_demux );
93 p_sys->p_playlist = NULL;
95 p_sys->p_xml_reader = NULL;
100 /*****************************************************************************
101 * Deactivate: frees unused data
102 *****************************************************************************/
103 void E_(Close_B4S)( vlc_object_t *p_this )
105 demux_t *p_demux = (demux_t *)p_this;
106 demux_sys_t *p_sys = p_demux->p_sys;
108 if( p_sys->psz_prefix ) free( p_sys->psz_prefix );
109 if( p_sys->p_playlist ) vlc_object_release( p_sys->p_playlist );
110 if( p_sys->p_xml_reader ) xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
111 if( p_sys->p_xml ) xml_Delete( p_sys->p_xml );
115 static int Demux( demux_t *p_demux )
117 demux_sys_t *p_sys = p_demux->p_sys;
118 playlist_t *p_playlist;
119 playlist_item_t *p_item, *p_current;
120 playlist_item_t *p_bitrate = NULL, *p_genre = NULL;
126 xml_reader_t *p_xml_reader;
127 char *psz_elname = NULL;
128 int i_type, b_shoutcast;
129 char *psz_mrl = NULL, *psz_name = NULL, *psz_genre = NULL;
130 char *psz_now = NULL, *psz_listeners = NULL, *psz_bitrate = NULL;
133 b_shoutcast = p_sys->b_shout;
135 p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
139 msg_Err( p_demux, "can't find playlist" );
142 p_sys->p_playlist = p_playlist;
144 b_play = E_(FindItem)( p_demux, p_playlist, &p_current );
146 playlist_ItemToNode( p_playlist, p_current );
147 p_current->input.i_type = ITEM_TYPE_PLAYLIST;
150 p_genre = playlist_NodeCreate( p_playlist, p_current->pp_parents[0]->i_view, "Genre", p_current );
151 playlist_CopyParents( p_current, p_genre );
153 p_bitrate = playlist_NodeCreate( p_playlist, p_current->pp_parents[0]->i_view, "Bitrate", p_current );
154 playlist_CopyParents( p_current, p_bitrate );
157 p_xml = p_sys->p_xml = xml_Create( p_demux );
158 if( !p_xml ) return -1;
160 psz_elname = stream_ReadLine( p_demux->s );
161 if( psz_elname ) free( psz_elname );
164 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
165 if( !p_xml_reader ) return -1;
166 p_sys->p_xml_reader = p_xml_reader;
169 /* check root node */
170 if( xml_ReaderRead( p_xml_reader ) != 1 )
172 msg_Err( p_demux, "invalid file (no root node)" );
176 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
177 ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
178 strcmp( psz_elname, "WinampXML" ) )
180 msg_Err( p_demux, "invalid root node %i, %s",
181 xml_ReaderNodeType( p_xml_reader ), psz_elname );
182 if( psz_elname ) free( psz_elname );
187 /* root node should not have any attributes, and should only
188 * contain the "playlist node */
190 /* Skip until 1st child node */
191 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 &&
192 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
195 msg_Err( p_demux, "invalid file (no child node)" );
199 if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
200 strcmp( psz_elname, "playlist" ) )
202 msg_Err( p_demux, "invalid child node %s", psz_elname );
203 if( psz_elname ) free( psz_elname );
206 free( psz_elname ); psz_elname = 0;
208 // Read the attributes
209 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
211 char *psz_name = xml_ReaderName( p_xml_reader );
212 char *psz_value = xml_ReaderValue( p_xml_reader );
213 if( !psz_name || !psz_value ) return -1;
214 if( !strcmp( psz_name, "num_entries" ) )
216 msg_Dbg( p_demux, "playlist has %d entries", atoi(psz_value) );
218 else if( !strcmp( psz_name, "label" ) )
220 playlist_ItemSetName( p_current, psz_value );
224 msg_Warn( p_demux, "stray attribute %s with value %s in element"
225 " 'playlist'", psz_name, psz_value );
231 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
234 i_type = xml_ReaderNodeType( p_xml_reader );
242 case XML_READER_STARTELEM:
244 // Read the element name
245 if( psz_elname ) free( psz_elname );
246 psz_elname = xml_ReaderName( p_xml_reader );
247 if( !psz_elname ) return -1;
250 // Read the attributes
251 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
253 char *psz_name = xml_ReaderName( p_xml_reader );
254 char *psz_value = xml_ReaderValue( p_xml_reader );
255 if( !psz_name || !psz_value ) return -1;
256 if( !strcmp( psz_elname, "entry" ) &&
257 !strcmp( psz_name, "Playstring" ) )
259 psz_mrl = strdup( psz_value );
263 msg_Warn( p_demux, "unexpected attribure %s in element %s",
264 psz_name, psz_elname );
271 case XML_READER_TEXT:
273 char *psz_text = xml_ReaderValue( p_xml_reader );
274 if( IsWhitespace( psz_text ) )
279 if( !strcmp( psz_elname, "Name" ) )
281 psz_name = strdup( psz_text );
283 else if( !strcmp( psz_elname, "Genre" ) )
285 psz_genre = strdup( psz_text );
287 else if( !strcmp( psz_elname, "Nowplaying" ) )
289 psz_now = strdup( psz_text );
291 else if( !strcmp( psz_elname, "Listeners" ) )
293 psz_listeners = strdup( psz_text );
295 else if( !strcmp( psz_elname, "Bitrate" ) )
297 psz_bitrate = strdup( psz_text );
299 else if( !strcmp( psz_elname, "" ) )
305 msg_Warn( p_demux, "unexpected text in element '%s'",
312 case XML_READER_ENDELEM:
314 // Read the element name
316 psz_elname = xml_ReaderName( p_xml_reader );
317 if( !psz_elname ) return -1;
318 if( !strcmp( psz_elname, "entry" ) )
320 p_item = playlist_ItemNew( p_playlist, psz_mrl, psz_name );
323 vlc_input_item_AddInfo( &(p_item->input),
324 _(VLC_META_INFO_CAT),
325 _( VLC_META_NOW_PLAYING ),
331 vlc_input_item_AddInfo( &p_item->input,
332 _(VLC_META_INFO_CAT),
339 vlc_input_item_AddInfo( &p_item->input,
340 _(VLC_META_INFO_CAT),
347 vlc_input_item_AddInfo( &p_item->input,
348 _(VLC_META_INFO_CAT),
353 playlist_NodeAddItem( p_playlist, p_item,
354 p_current->pp_parents[0]->i_view,
355 p_current, PLAYLIST_APPEND,
358 /* We need to declare the parents of the node as the
359 * * same of the parent's ones */
360 playlist_CopyParents( p_current, p_item );
362 vlc_input_item_CopyOptions( &p_current->input,
366 char *psz_genreToken;
367 char *psz_otherToken;
370 psz_genreToken = psz_genre;
372 /* split up the combined genre string form
373 shoutcast and add the individual genres */
374 while ( psz_genreToken &&
375 ( psz_otherToken = GetNextToken(psz_genreToken )))
377 if( strlen(psz_genreToken)>2 )
378 /* We dont want genres below 2 letters,
379 this gets rid of alot of junk*/
381 /* lowercase everything */
382 for( i=0; psz_genreToken[i]!=0; i++ )
384 tolower(psz_genreToken[i]);
385 /* Make first letter uppercase, purely cosmetical */
387 toupper( psz_genreToken[0] );
388 ShoutcastAdd( p_playlist, p_genre,
390 psz_genreToken, psz_bitrate );
392 psz_genreToken = psz_otherToken;
397 #define FREE(a) if( a ) free( a ); a = NULL;
402 FREE( psz_listeners );
407 psz_elname = strdup("");
416 msg_Warn( p_demux, "error while parsing data" );
420 vlc_mutex_lock( &p_playlist->object_lock );
421 playlist_NodeSort( p_playlist, p_bitrate, SORT_TITLE_NUMERIC, ORDER_NORMAL );
422 vlc_mutex_unlock( &p_playlist->object_lock );
425 /* Go back and play the playlist */
426 if( b_play && p_playlist->status.p_item &&
427 p_playlist->status.p_item->i_children > 0 )
429 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
430 p_playlist->status.i_view,
431 p_playlist->status.p_item,
432 p_playlist->status.p_item->pp_children[0] );
435 vlc_object_release( p_playlist );
436 p_sys->p_playlist = NULL;
440 static int Control( demux_t *p_demux, int i_query, va_list args )
446 * Get a in-string pointer to the start of the next token from a
447 * string terminating the pointer returned by a previous call.
449 * \param psz_cur_string The string to search for the token from
450 * \return a pointer to withing psz_cur_string, or NULL if no token
452 * \note The returned pointer may contain more than one
453 * token, Run GetNextToken once more to terminate the token properly
455 static char *GetNextToken(char *psz_cur_string) {
456 while (*psz_cur_string && !isspace(*psz_cur_string))
458 if (!*psz_cur_string)
460 *psz_cur_string++ = '\0';
461 while (*psz_cur_string && isspace(*psz_cur_string))
463 return psz_cur_string;
466 static int IsWhitespace( char *psz_string )
470 if( *psz_string != ' ' && *psz_string != '\t' && *psz_string != '\r' &&
471 *psz_string != '\n' )
480 static void ShoutcastAdd( playlist_t *p_playlist, playlist_item_t* p_genre,
481 playlist_item_t *p_bitrate, playlist_item_t *p_item,
482 char *psz_genre, char *psz_bitrate )
484 playlist_item_t *p_parent;
487 playlist_item_t *p_copy = playlist_ItemCopy(p_playlist,p_item);
488 p_parent = playlist_ChildSearchName( p_bitrate, psz_bitrate );
491 p_parent = playlist_NodeCreate( p_playlist, p_genre->pp_parents[0]->i_view, psz_bitrate,
493 playlist_CopyParents( p_bitrate, p_parent );
495 playlist_NodeAddItem( p_playlist, p_copy, p_parent->pp_parents[0]->i_view, p_parent, PLAYLIST_APPEND, PLAYLIST_END );
496 playlist_CopyParents( p_parent, p_copy );
502 playlist_item_t *p_copy = playlist_ItemCopy(p_playlist,p_item);
503 p_parent = playlist_ChildSearchName( p_genre, psz_genre );
506 p_parent = playlist_NodeCreate( p_playlist, p_genre->pp_parents[0]->i_view, psz_genre,
508 playlist_CopyParents( p_genre, p_parent );
510 playlist_NodeAddItem( p_playlist, p_copy, p_parent->pp_parents[0]->i_view, p_parent, PLAYLIST_APPEND, PLAYLIST_END );
511 playlist_CopyParents( p_parent, p_copy );