1 /*****************************************************************************
2 * m3u.c: a meta demux to parse pls, m3u, asx et b4s playlists
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
5 * $Id: m3u.c,v 1.26 2004/01/25 20:05:28 hartman Exp $
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 ) )
158 else if ( !strncasecmp( p_peek, "<?xml", sizeof("<?xml") -1 ) )
164 if ( i_type == TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN)
168 if ( i_type != TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN )
178 p_input->p_demux_data = malloc( sizeof( demux_sys_t ) );
179 p_input->p_demux_data->i_type = i_type;
184 /*****************************************************************************
185 * Deactivate: frees unused data
186 *****************************************************************************/
187 static void Deactivate( vlc_object_t *p_this )
189 input_thread_t *p_input = (input_thread_t *)p_this;
191 free( p_input->p_demux_data );
194 /*****************************************************************************
195 * XMLSpecialChars: Handle the special chars in a XML file.
196 * ***************************************************************************/
197 static void XMLSpecialChars ( char *str )
206 if( !strncasecmp( src, "à", 6 ) ) *dst++ = 'à';
207 else if( !strncasecmp( src, "î", 6 ) ) *dst++ = 'î';
208 else 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++ = 'ê';
228 /*****************************************************************************
229 * ParseLine: read a "line" from the file and add any entries found
230 * to the playlist. Returns:
231 * 0 if nothing was found
232 * 1 if a URI was found (it is then copied in psz_data)
233 * 2 if a name was found ( " )
235 * XXX psz_data has the same length that psz_line so no problem if you don't
237 * psz_line is \0 terminated
238 ******************************************************************************/
239 static int ParseLine ( input_thread_t *p_input, char *psz_line, char *psz_data, vlc_bool_t *pb_next )
241 demux_sys_t *p_m3u = p_input->p_demux_data;
243 char *psz_bol, *psz_name;
247 *pb_next = VLC_FALSE;
249 /* Remove unnecessary tabs or spaces at the beginning of line */
250 while( *psz_bol == ' ' || *psz_bol == '\t' ||
251 *psz_bol == '\n' || *psz_bol == '\r' )
256 if( p_m3u->i_type == TYPE_M3U )
258 /* Check for comment line */
259 if( *psz_bol == '#' )
262 strncasecmp( psz_bol, "EXTINF:", sizeof("EXTINF:") - 1 ) )
264 if( !*psz_bol ) return 0;
266 psz_bol = strchr( psz_bol, ',' );
267 if ( !psz_bol ) return 0;
269 /* From now, we have a name line */
271 strcpy( psz_data , psz_bol );
274 /* If we don't have a comment, the line is directly the URI */
276 else if ( p_m3u->i_type == TYPE_PLS )
278 /* We are dealing with .pls files from shoutcast
279 * We are looking for lines like "File1=http://..." */
280 if( !strncasecmp( psz_bol, "File", sizeof("File") - 1 ) )
282 psz_bol += sizeof("File") - 1;
283 psz_bol = strchr( psz_bol, '=' );
284 if ( !psz_bol ) return 0;
292 else if ( p_m3u->i_type == TYPE_ASX )
294 /* We are dealing with ASX files.
295 * We are looking for "<ref href=" xml markups that
296 * begins with "mms://", "http://" or "file://" */
300 strncasecmp( psz_bol, "ref", sizeof("ref") - 1 ) )
303 if( !*psz_bol ) return 0;
306 strncasecmp( psz_bol, "href", sizeof("href") - 1 ) )
309 if( !*psz_bol ) return 0;
312 strncasecmp( psz_bol, "mms://",
313 sizeof("mms://") - 1 ) &&
314 strncasecmp( psz_bol, "mmsu://",
315 sizeof("mmsu://") - 1 ) &&
316 strncasecmp( psz_bol, "mmst://",
317 sizeof("mmst://") - 1 ) &&
318 strncasecmp( psz_bol, "http://",
319 sizeof("http://") - 1 ) &&
320 strncasecmp( psz_bol, "file://",
321 sizeof("file://") - 1 ) )
324 if( !*psz_bol ) return 0;
326 psz_eol = strchr( psz_bol, '"');
332 else if ( p_m3u->i_type == TYPE_HTML )
334 /* We are dealing with a html file with embedded
335 * video. We are looking for "<param name="filename"
336 * value=" html markups that begin with "http://" */
340 strncasecmp( psz_bol, "param", sizeof("param") - 1 ) )
343 if( !*psz_bol ) return 0;
346 strncasecmp( psz_bol, "filename", sizeof("filename") - 1 ) )
349 if( !*psz_bol ) return 0;
352 strncasecmp( psz_bol, "http://",
353 sizeof("http://") - 1 ) )
356 if( !*psz_bol ) return 0;
358 psz_eol = strchr( psz_bol, '"');
365 else if ( p_m3u->i_type == TYPE_B4S )
370 msg_Dbg( p_input, "b4s line=%s", psz_line );
371 /* We are dealing with a B4S file from Winamp 3 */
373 /* First, search for name *
374 * <Name>Blabla</Name> */
376 if( strstr ( psz_bol, "<Name>" ) )
380 strncasecmp( psz_bol,"Name",sizeof("Name") -1 ) )
383 if( !*psz_bol ) return 0;
385 psz_bol = psz_bol + 5 ;
386 /* We are now at the beginning of the name */
388 if( !psz_bol ) return 0;
391 psz_eol = strchr(psz_bol, '<' );
392 if( !psz_eol) return 0;
396 XMLSpecialChars( psz_bol );
398 strcpy( psz_data, psz_bol );
401 else if( strstr( psz_bol, "</entry>" ) || strstr( psz_bol, "</Entry>" ))
407 /* We are looking for <entry Playstring="blabla"> */
411 strncasecmp( psz_bol,"Playstring",sizeof("Playstring") -1 ) )
414 if( !*psz_bol ) return 0;
416 psz_bol = strchr( psz_bol, '=' );
417 if ( !psz_bol ) return 0;
421 psz_eol= strchr(psz_bol, '"');
422 if( !psz_eol ) return 0;
426 /* Handle the XML special characters */
427 XMLSpecialChars( psz_bol );
431 msg_Warn( p_input, "unknown file type" );
436 if ( !*psz_bol ) return 0;
439 * From now on, we know we've got a meaningful line
442 /* check for a protocol name */
443 /* for URL, we should look for "://"
444 * for MRL (Media Resource Locator) ([[<access>][/<demux>]:][<source>]),
445 * we should look for ":"
446 * so we end up looking simply for ":"*/
447 /* PB: on some file systems, ':' are valid characters though*/
449 while( *psz_name && *psz_name!=':' )
454 if ( *psz_name && ( psz_name == psz_bol + 1 ) )
456 /* if it is not an URL,
457 * as it is unlikely to be an MRL (PB: if it is ?)
458 * it should be an absolute file name with the drive letter */
459 if ( *(psz_name+1) == '/' )/* "*:/" */
461 if ( *(psz_name+2) != '/' )/* not "*://" */
462 while ( *psz_name ) *psz_name++;/* so now (*psz_name==0) */
464 else while ( *psz_name ) *psz_name++;/* "*:*"*/
468 /* if the line doesn't specify a protocol name,
469 * check if the line has an absolute or relative path */
471 if( !*psz_name && *psz_bol != '/' )
472 /* If this line doesn't begin with a '/' */
477 && *(psz_bol+1)!=':' )
478 /* if this line doesn't begin with
479 * "/" or "\" or "*:" or "*:\" or "*:/" or "\\" */
482 /* assume the path is relative to the path of the m3u file. */
483 char *psz_path = strdup( p_input->psz_name );
486 psz_name = strrchr( psz_path, '/' );
488 psz_name = strrchr( psz_path, '\\' );
489 if ( ! psz_name ) psz_name = strrchr( psz_path, '/' );
491 if( psz_name ) *psz_name = '\0';
492 else *psz_path = '\0';
494 psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
495 sprintf( psz_name, "%s/%s", psz_path, psz_bol );
497 if ( *psz_path != '\0' )
499 psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
500 sprintf( psz_name, "%s\\%s", psz_path, psz_bol );
502 else psz_name = strdup( psz_bol );
508 psz_name = strdup( psz_bol );
511 strcpy(psz_data, psz_name ) ;
515 if( p_m3u->i_type != TYPE_B4S )
523 static void ProcessLine ( input_thread_t *p_input, playlist_t *p_playlist,
525 char **ppsz_uri, char **ppsz_name,
528 char psz_data[MAX_LINE];
531 switch( ParseLine( p_input, psz_line, psz_data, &b_next ) )
538 *ppsz_uri = strdup( psz_data );
545 *ppsz_name = strdup( psz_data );
552 if( b_next && *ppsz_uri )
554 playlist_Add( p_playlist, *ppsz_uri,
555 *ppsz_name ? *ppsz_name : *ppsz_uri,
556 PLAYLIST_INSERT, *pi_position );
568 /*****************************************************************************
569 * Demux: reads and demuxes data packets
570 *****************************************************************************
571 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
572 *****************************************************************************/
573 static int Demux ( input_thread_t *p_input )
575 demux_sys_t *p_m3u = p_input->p_demux_data;
577 data_packet_t *p_data;
578 char psz_line[MAX_LINE];
579 char *p_buf, eol_tok;
580 int i_size, i_bufpos, i_linepos = 0;
581 playlist_t *p_playlist;
582 vlc_bool_t b_discard = VLC_FALSE;
585 char *psz_name = NULL;
586 char *psz_uri = NULL;
590 p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
594 msg_Err( p_input, "can't find playlist" );
598 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
599 i_position = p_playlist->i_index + 1;
601 /* Depending on wether we are dealing with an m3u/asf file, the end of
602 * line token will be different */
603 if( p_m3u->i_type == TYPE_ASX || p_m3u->i_type == TYPE_HTML )
608 while( ( i_size = input_SplitBuffer( p_input, &p_data, MAX_LINE ) ) > 0 )
610 i_bufpos = 0; p_buf = p_data->p_payload_start;
614 /* Build a line < MAX_LINE */
615 while( p_buf[i_bufpos] != eol_tok && i_size )
617 if( i_linepos == MAX_LINE || b_discard == VLC_TRUE )
619 /* line is bigger than MAX_LINE, discard it */
621 b_discard = VLC_TRUE;
625 if ( eol_tok != '\n' || p_buf[i_bufpos] != '\r' )
627 psz_line[i_linepos] = p_buf[i_bufpos];
632 i_size--; i_bufpos++;
635 /* Check if we need more data */
636 if( !i_size ) continue;
638 i_size--; i_bufpos++;
639 b_discard = VLC_FALSE;
641 /* Check for empty line */
642 if( !i_linepos ) continue;
644 psz_line[i_linepos] = '\0';
647 ProcessLine( p_input, p_playlist, psz_line, &psz_uri, &psz_name,
651 input_DeletePacket( p_input->p_method_data, p_data );
654 if ( i_linepos && b_discard != VLC_TRUE && eol_tok == '\n' )
656 psz_line[i_linepos] = '\0';
658 ProcessLine( p_input, p_playlist, psz_line, &psz_uri, &psz_name,
660 /* is there a pendding uri without b_next */
663 playlist_Add( p_playlist, psz_uri, psz_uri,
664 PLAYLIST_INSERT, i_position );
677 vlc_object_release( p_playlist );