1 /*****************************************************************************
2 * m3u.c: a meta demux to parse pls, m3u, asx et b4s playlists
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
7 * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
8 * Gildas Bazin <gbazin@netcourrier.com>
9 * Clément Stenac <zorglub@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
32 #include <vlc/input.h>
33 #include <vlc_playlist.h>
35 /*****************************************************************************
36 * Constants and structures
37 *****************************************************************************/
40 #define TYPE_UNKNOWN 0
49 int i_type; /* playlist type (m3u/asx) */
52 /*****************************************************************************
54 *****************************************************************************/
55 static int Activate ( vlc_object_t * );
56 static void Deactivate( vlc_object_t * );
57 static int Demux ( input_thread_t * );
59 /*****************************************************************************
61 *****************************************************************************/
63 set_description( _("Playlist metademux") );
64 set_capability( "demux", 180 );
65 set_callbacks( Activate, Deactivate );
66 add_shortcut( "m3u" );
67 add_shortcut( "asx" );
68 add_shortcut( "html" );
69 add_shortcut( "pls" );
70 add_shortcut( "b4s" );
73 /*****************************************************************************
74 * Activate: initializes m3u demux structures
75 *****************************************************************************/
76 static int Activate( vlc_object_t * p_this )
78 input_thread_t *p_input = (input_thread_t *)p_this;
80 int i_type = TYPE_UNKNOWN;
81 int i_type2 = TYPE_UNKNOWN;
83 /* Initialize access plug-in structures. */
84 if( p_input->i_mtu == 0 )
87 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
90 p_input->pf_demux = Demux;
91 p_input->pf_rewind = NULL;
93 /* Check for m3u/asx file extension or if the demux has been forced */
94 psz_ext = strrchr ( p_input->psz_name, '.' );
96 if( ( psz_ext && !strcasecmp( psz_ext, ".m3u") ) ||
97 ( p_input->psz_demux && !strcmp(p_input->psz_demux, "m3u") ) )
101 else if( ( psz_ext && !strcasecmp( psz_ext, ".asx") ) ||
102 ( p_input->psz_demux && !strcmp(p_input->psz_demux, "asx") ) )
106 else if( ( psz_ext && !strcasecmp( psz_ext, ".html") ) ||
107 ( p_input->psz_demux && !strcmp(p_input->psz_demux, "html") ) )
111 else if( ( psz_ext && !strcasecmp( psz_ext, ".pls") ) ||
112 ( p_input->psz_demux && !strcmp(p_input->psz_demux, "pls") ) )
116 else if( ( psz_ext && !strcasecmp( psz_ext, ".b4s") ) ||
117 ( p_input->psz_demux && !strcmp(p_input->psz_demux, "b4s") ) )
122 /* we had no luck looking at the file extention, so we have a look
123 * at the content. This is useful for .asp, .php and similar files
124 * that are actually html. Also useful for some asx files that have
125 * another extension */
126 /* XXX we double check for file != m3u as some asx ... are just m3u file */
127 if( i_type != TYPE_M3U )
130 int i_size = input_Peek( p_input, &p_peek, MAX_LINE );
131 i_size -= sizeof("[playlist]") - 1;
134 && strncasecmp( p_peek, "[playlist]", sizeof("[playlist]") - 1 )
135 && strncasecmp( p_peek, "<html>", sizeof("<html>") - 1 )
136 && strncasecmp( p_peek, "<asx", sizeof("<asx") - 1 )
137 && strncasecmp( p_peek, "<?xml", sizeof("<?xml") -1 ) )
146 else if ( !strncasecmp( p_peek, "[playlist]", sizeof("[playlist]") -1 ) )
150 else if ( !strncasecmp( p_peek, "<html>", sizeof("<html>") -1 ) )
154 else if ( !strncasecmp( p_peek, "<asx", sizeof("<asx") -1 ) )
159 else if ( !strncasecmp( p_peek, "<?xml", sizeof("<?xml") -1 ) )
166 if ( i_type == TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN)
170 if ( i_type != TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN )
180 p_input->p_demux_data = malloc( sizeof( demux_sys_t ) );
181 p_input->p_demux_data->i_type = i_type;
186 /*****************************************************************************
187 * Deactivate: frees unused data
188 *****************************************************************************/
189 static void Deactivate( vlc_object_t *p_this )
191 input_thread_t *p_input = (input_thread_t *)p_this;
193 free( p_input->p_demux_data );
196 /*****************************************************************************
197 * XMLSpecialChars: Handle the special chars in a XML file.
198 * ***************************************************************************/
199 static void XMLSpecialChars ( char *str )
208 if( !strncasecmp( src, "à", 6 ) ) *dst++ = 'à';
209 else if( !strncasecmp( src, "î", 6 ) ) *dst++ = 'î';
210 else if( !strncasecmp( src, "'", 6 ) ) *dst++ = '\'';
211 else if( !strncasecmp( src, "è", 6 ) ) *dst++ = 'è';
212 else if( !strncasecmp( src, "é", 6 ) ) *dst++ = 'é';
213 else if( !strncasecmp( src, "ê", 6 ) ) *dst++ = 'ê';
230 /*****************************************************************************
231 * ParseLine: read a "line" from the file and add any entries found
232 * to the playlist. Returns:
233 * 0 if nothing was found
234 * 1 if a URI was found (it is then copied in psz_data)
235 * 2 if a name was found ( " )
237 * XXX psz_data has the same length that psz_line so no problem if you don't
239 * psz_line is \0 terminated
240 ******************************************************************************/
241 static int ParseLine ( input_thread_t *p_input, char *psz_line, char *psz_data, vlc_bool_t *pb_next )
243 demux_sys_t *p_m3u = p_input->p_demux_data;
245 char *psz_bol, *psz_name;
249 *pb_next = VLC_FALSE;
251 /* Remove unnecessary tabs or spaces at the beginning of line */
252 while( *psz_bol == ' ' || *psz_bol == '\t' ||
253 *psz_bol == '\n' || *psz_bol == '\r' )
258 if( p_m3u->i_type == TYPE_M3U )
260 /* Check for comment line */
261 if( *psz_bol == '#' )
264 strncasecmp( psz_bol, "EXTINF:", sizeof("EXTINF:") - 1 ) )
266 if( !*psz_bol ) return 0;
268 psz_bol = strchr( psz_bol, ',' );
269 if ( !psz_bol ) return 0;
271 /* From now, we have a name line */
273 strcpy( psz_data , psz_bol );
276 /* If we don't have a comment, the line is directly the URI */
278 else if ( p_m3u->i_type == TYPE_PLS )
280 /* We are dealing with .pls files from shoutcast
281 * We are looking for lines like "File1=http://..." */
282 if( !strncasecmp( psz_bol, "File", sizeof("File") - 1 ) )
284 psz_bol += sizeof("File") - 1;
285 psz_bol = strchr( psz_bol, '=' );
286 if ( !psz_bol ) return 0;
294 else if ( p_m3u->i_type == TYPE_ASX )
296 /* We are dealing with ASX files.
297 * We are looking for "<ref href=" xml markups that
298 * begins with "mms://", "http://" or "file://" */
302 strncasecmp( psz_bol, "ref", sizeof("ref") - 1 ) )
305 if( !*psz_bol ) return 0;
308 strncasecmp( psz_bol, "href", sizeof("href") - 1 ) )
311 if( !*psz_bol ) return 0;
314 strncasecmp( psz_bol, "mms://",
315 sizeof("mms://") - 1 ) &&
316 strncasecmp( psz_bol, "mmsu://",
317 sizeof("mmsu://") - 1 ) &&
318 strncasecmp( psz_bol, "mmst://",
319 sizeof("mmst://") - 1 ) &&
320 strncasecmp( psz_bol, "http://",
321 sizeof("http://") - 1 ) &&
322 strncasecmp( psz_bol, "file://",
323 sizeof("file://") - 1 ) )
326 if( !*psz_bol ) return 0;
328 psz_eol = strchr( psz_bol, '"');
334 else if ( p_m3u->i_type == TYPE_HTML )
336 /* We are dealing with a html file with embedded
337 * video. We are looking for "<param name="filename"
338 * value=" html markups that begin with "http://" */
342 strncasecmp( psz_bol, "param", sizeof("param") - 1 ) )
345 if( !*psz_bol ) return 0;
348 strncasecmp( psz_bol, "filename", sizeof("filename") - 1 ) )
351 if( !*psz_bol ) return 0;
354 strncasecmp( psz_bol, "http://",
355 sizeof("http://") - 1 ) )
358 if( !*psz_bol ) return 0;
360 psz_eol = strchr( psz_bol, '"');
367 else if ( p_m3u->i_type == TYPE_B4S )
372 msg_Dbg( p_input, "b4s line=%s", psz_line );
373 /* We are dealing with a B4S file from Winamp 3 */
375 /* First, search for name *
376 * <Name>Blabla</Name> */
378 if( strstr ( psz_bol, "<Name>" ) )
382 strncasecmp( psz_bol,"Name",sizeof("Name") -1 ) )
385 if( !*psz_bol ) return 0;
387 psz_bol = psz_bol + 5 ;
388 /* We are now at the beginning of the name */
390 if( !psz_bol ) return 0;
393 psz_eol = strchr(psz_bol, '<' );
394 if( !psz_eol) return 0;
398 XMLSpecialChars( psz_bol );
400 strcpy( psz_data, psz_bol );
403 else if( strstr( psz_bol, "</entry>" ) || strstr( psz_bol, "</Entry>" ))
409 /* We are looking for <entry Playstring="blabla"> */
413 strncasecmp( psz_bol,"Playstring",sizeof("Playstring") -1 ) )
416 if( !*psz_bol ) return 0;
418 psz_bol = strchr( psz_bol, '=' );
419 if ( !psz_bol ) return 0;
423 psz_eol= strchr(psz_bol, '"');
424 if( !psz_eol ) return 0;
428 /* Handle the XML special characters */
429 XMLSpecialChars( psz_bol );
433 msg_Warn( p_input, "unknown file type" );
438 if ( !*psz_bol ) return 0;
441 * From now on, we know we've got a meaningful line
444 /* check for a protocol name */
445 /* for URL, we should look for "://"
446 * for MRL (Media Resource Locator) ([[<access>][/<demux>]:][<source>]),
447 * we should look for ":"
448 * so we end up looking simply for ":"*/
449 /* PB: on some file systems, ':' are valid characters though*/
451 while( *psz_name && *psz_name!=':' )
456 if ( *psz_name && ( psz_name == psz_bol + 1 ) )
458 /* if it is not an URL,
459 * as it is unlikely to be an MRL (PB: if it is ?)
460 * it should be an absolute file name with the drive letter */
461 if ( *(psz_name+1) == '/' )/* "*:/" */
463 if ( *(psz_name+2) != '/' )/* not "*://" */
464 while ( *psz_name ) *psz_name++;/* so now (*psz_name==0) */
466 else while ( *psz_name ) *psz_name++;/* "*:*"*/
470 /* if the line doesn't specify a protocol name,
471 * check if the line has an absolute or relative path */
473 if( !*psz_name && *psz_bol != '/' )
474 /* If this line doesn't begin with a '/' */
479 && *(psz_bol+1)!=':' )
480 /* if this line doesn't begin with
481 * "/" or "\" or "*:" or "*:\" or "*:/" or "\\" */
484 /* assume the path is relative to the path of the m3u file. */
485 char *psz_path = strdup( p_input->psz_name );
488 psz_name = strrchr( psz_path, '/' );
490 psz_name = strrchr( psz_path, '\\' );
491 if ( ! psz_name ) psz_name = strrchr( psz_path, '/' );
493 if( psz_name ) *psz_name = '\0';
494 else *psz_path = '\0';
496 psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
497 sprintf( psz_name, "%s/%s", psz_path, psz_bol );
499 if ( *psz_path != '\0' )
501 psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
502 sprintf( psz_name, "%s\\%s", psz_path, psz_bol );
504 else psz_name = strdup( psz_bol );
510 psz_name = strdup( psz_bol );
513 strcpy(psz_data, psz_name ) ;
517 if( p_m3u->i_type != TYPE_B4S )
525 static void ProcessLine ( input_thread_t *p_input, playlist_t *p_playlist,
527 char **ppsz_uri, char **ppsz_name,
530 char psz_data[MAX_LINE];
533 switch( ParseLine( p_input, psz_line, psz_data, &b_next ) )
540 *ppsz_uri = strdup( psz_data );
547 *ppsz_name = strdup( psz_data );
554 if( b_next && *ppsz_uri )
556 playlist_Add( p_playlist, *ppsz_uri,
557 *ppsz_name ? *ppsz_name : *ppsz_uri,
558 PLAYLIST_INSERT, *pi_position );
570 /*****************************************************************************
571 * Demux: reads and demuxes data packets
572 *****************************************************************************
573 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
574 *****************************************************************************/
575 static int Demux ( input_thread_t *p_input )
577 demux_sys_t *p_m3u = p_input->p_demux_data;
579 data_packet_t *p_data;
580 char psz_line[MAX_LINE];
581 char *p_buf, eol_tok;
582 int i_size, i_bufpos, i_linepos = 0;
583 playlist_t *p_playlist;
584 vlc_bool_t b_discard = VLC_FALSE;
587 char *psz_name = NULL;
588 char *psz_uri = NULL;
592 p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
596 msg_Err( p_input, "can't find playlist" );
600 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
601 i_position = p_playlist->i_index + 1;
603 /* Depending on wether we are dealing with an m3u/asf file, the end of
604 * line token will be different */
605 if( p_m3u->i_type == TYPE_ASX || p_m3u->i_type == TYPE_HTML )
610 while( ( i_size = input_SplitBuffer( p_input, &p_data, MAX_LINE ) ) > 0 )
612 i_bufpos = 0; p_buf = p_data->p_payload_start;
616 /* Build a line < MAX_LINE */
617 while( p_buf[i_bufpos] != eol_tok && i_size )
619 if( i_linepos == MAX_LINE || b_discard == VLC_TRUE )
621 /* line is bigger than MAX_LINE, discard it */
623 b_discard = VLC_TRUE;
627 if ( eol_tok != '\n' || p_buf[i_bufpos] != '\r' )
629 psz_line[i_linepos] = p_buf[i_bufpos];
634 i_size--; i_bufpos++;
637 /* Check if we need more data */
638 if( !i_size ) continue;
640 i_size--; i_bufpos++;
641 b_discard = VLC_FALSE;
643 /* Check for empty line */
644 if( !i_linepos ) continue;
646 psz_line[i_linepos] = '\0';
649 ProcessLine( p_input, p_playlist, psz_line, &psz_uri, &psz_name,
653 input_DeletePacket( p_input->p_method_data, p_data );
656 if ( i_linepos && b_discard != VLC_TRUE && eol_tok == '\n' )
658 psz_line[i_linepos] = '\0';
660 ProcessLine( p_input, p_playlist, psz_line, &psz_uri, &psz_name,
662 /* is there a pendding uri without b_next */
665 playlist_Add( p_playlist, psz_uri, psz_uri,
666 PLAYLIST_INSERT, i_position );
679 vlc_object_release( p_playlist );