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
50 int i_type; /* playlist type (m3u/asx) */
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Activate ( vlc_object_t * );
57 static void Deactivate( vlc_object_t * );
58 static int Demux ( demux_t * );
59 static int Control ( demux_t *, int, va_list );
61 /*****************************************************************************
63 *****************************************************************************/
65 set_description( _("Playlist metademux") );
66 set_capability( "demux2", 10 );
67 set_callbacks( Activate, Deactivate );
68 add_shortcut( "m3u" );
69 add_shortcut( "asx" );
70 add_shortcut( "html" );
71 add_shortcut( "pls" );
72 add_shortcut( "b4s" );
75 /*****************************************************************************
76 * Activate: initializes m3u demux structures
77 *****************************************************************************/
78 static int Activate( vlc_object_t * p_this )
80 demux_t *p_demux = (demux_t *)p_this;
82 int i_type = TYPE_UNKNOWN;
83 int i_type2 = TYPE_UNKNOWN;
85 p_demux->pf_control = Control;
86 p_demux->pf_demux = Demux;
88 /* Check for m3u/asx file extension or if the demux has been forced */
89 psz_ext = strrchr ( p_demux->psz_path, '.' );
91 if( ( psz_ext && !strcasecmp( psz_ext, ".m3u") ) ||
92 /* a .ram file can contain a single rtsp link */
93 ( psz_ext && !strcasecmp( psz_ext, ".ram") ) ||
94 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "m3u") ) )
98 else if( ( psz_ext && !strcasecmp( psz_ext, ".asx") ) ||
99 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "asx") ) )
103 else if( ( psz_ext && !strcasecmp( psz_ext, ".html") ) ||
104 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "html") ) )
108 else if( ( psz_ext && !strcasecmp( psz_ext, ".pls") ) ||
109 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "pls") ) )
113 else if( ( psz_ext && !strcasecmp( psz_ext, ".b4s") ) ||
114 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "b4s") ) )
119 /* we had no luck looking at the file extention, so we have a look
120 * at the content. This is useful for .asp, .php and similar files
121 * that are actually html. Also useful for some asx files that have
122 * another extension */
123 /* XXX we double check for file != m3u as some asx ... are just m3u file */
124 if( i_type != TYPE_M3U )
127 int i_size = stream_Peek( p_demux->s, &p_peek, MAX_LINE );
128 i_size -= sizeof("[Reference]") - 1;
133 strncasecmp(p_peek, "[playlist]", sizeof("[playlist]") - 1)
134 && strncasecmp( p_peek, "[Reference]", sizeof("[Reference]") - 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, "[Reference]", sizeof("[Reference]") -1 ) )
154 else if( !strncasecmp( p_peek, "<html>", sizeof("<html>") -1 ) )
158 else if( !strncasecmp( p_peek, "<asx", sizeof("<asx") -1 ) )
163 else if( !strncasecmp( p_peek, "<?xml", sizeof("<?xml") -1 ) )
170 if( i_type == TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN)
174 if( i_type != TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN )
184 p_demux->p_sys = malloc( sizeof( demux_sys_t ) );
185 p_demux->p_sys->i_type = i_type;
190 /*****************************************************************************
191 * Deactivate: frees unused data
192 *****************************************************************************/
193 static void Deactivate( vlc_object_t *p_this )
195 demux_t *p_demux = (demux_t *)p_this;
196 free( p_demux->p_sys );
199 /*****************************************************************************
200 * XMLSpecialChars: Handle the special chars in a XML file.
201 * ***************************************************************************/
202 static void XMLSpecialChars ( char *str )
211 if( !strncasecmp( src, "à", 6 ) ) *dst++ = 'à';
212 else if( !strncasecmp( src, "î", 6 ) ) *dst++ = 'î';
213 else if( !strncasecmp( src, "'", 6 ) ) *dst++ = '\'';
214 else if( !strncasecmp( src, "è", 6 ) ) *dst++ = 'è';
215 else if( !strncasecmp( src, "é", 6 ) ) *dst++ = 'é';
216 else if( !strncasecmp( src, "ê", 6 ) ) *dst++ = 'ê';
232 /*****************************************************************************
233 * ParseLine: read a "line" from the file and add any entries found
234 * to the playlist. Returns:
235 * 0 if nothing was found
236 * 1 if a URI was found (it is then copied in psz_data)
237 * 2 if a name was found ( " )
239 * XXX psz_data has the same length that psz_line so no problem if you don't
241 * psz_line is \0 terminated
242 *****************************************************************************/
243 static int ParseLine( demux_t *p_demux, char *psz_line, char *psz_data,
244 vlc_bool_t *pb_next )
246 demux_sys_t *p_m3u = p_demux->p_sys;
247 char *psz_bol, *psz_name;
251 *pb_next = VLC_FALSE;
253 /* Remove unnecessary tabs or spaces at the beginning of line */
254 while( *psz_bol == ' ' || *psz_bol == '\t' ||
255 *psz_bol == '\n' || *psz_bol == '\r' )
260 if( p_m3u->i_type == TYPE_M3U )
262 /* Check for comment line */
263 if( *psz_bol == '#' )
266 strncasecmp( psz_bol, "EXTINF:",
267 sizeof("EXTINF:") - 1 ) &&
268 strncasecmp( psz_bol, "EXTVLCOPT:",
269 sizeof("EXTVLCOPT:") - 1 ) ) psz_bol++;
271 if( !*psz_bol ) return 0;
273 if( !strncasecmp( psz_bol, "EXTINF:", sizeof("EXTINF:") - 1 ) )
275 psz_bol = strchr( psz_bol, ',' );
276 if ( !psz_bol ) return 0;
279 /* From now, we have a name line */
280 strcpy( psz_data , psz_bol );
285 psz_bol = strchr( psz_bol, ':' );
286 if ( !psz_bol ) return 0;
289 strcpy( psz_data , psz_bol );
293 /* If we don't have a comment, the line is directly the URI */
295 else if ( p_m3u->i_type == TYPE_PLS )
297 /* We are dealing with .pls files from shoutcast
298 * We are looking for lines like "File1=http://..." */
299 if( !strncasecmp( psz_bol, "File", sizeof("File") - 1 ) )
301 psz_bol += sizeof("File") - 1;
302 psz_bol = strchr( psz_bol, '=' );
303 if ( !psz_bol ) return 0;
311 else if ( p_m3u->i_type == TYPE_WMP )
313 /* We are dealing with some weird WMP stream playlist format
314 * Hurray for idiotic M$. Lines look like: "Ref1=http://..." */
315 if( !strncasecmp( psz_bol, "Ref", sizeof("Ref") - 1 ) )
317 psz_bol += sizeof("Ref") - 1;
318 psz_bol = strchr( psz_bol, '=' );
319 if ( !psz_bol ) return 0;
321 if( !strncasecmp( psz_bol, "http://", sizeof("http://") -1 ) )
323 psz_bol[0] = 'm'; psz_bol[1] = 'm'; psz_bol[2] = 's'; psz_bol[3] = 'h';
331 else if ( p_m3u->i_type == TYPE_ASX )
333 /* We are dealing with ASX files.
334 * We are looking for "<ref href=" xml markups that
335 * begins with "mms://", "http://" or "file://" */
339 strncasecmp( psz_bol, "ref", sizeof("ref") - 1 ) )
342 if( !*psz_bol ) return 0;
345 strncasecmp( psz_bol, "href", sizeof("href") - 1 ) )
348 if( !*psz_bol ) return 0;
351 strncasecmp( psz_bol, "mms://",
352 sizeof("mms://") - 1 ) &&
353 strncasecmp( psz_bol, "mmsu://",
354 sizeof("mmsu://") - 1 ) &&
355 strncasecmp( psz_bol, "mmst://",
356 sizeof("mmst://") - 1 ) &&
357 strncasecmp( psz_bol, "http://",
358 sizeof("http://") - 1 ) &&
359 strncasecmp( psz_bol, "file://",
360 sizeof("file://") - 1 ) )
363 if( !*psz_bol ) return 0;
365 psz_eol = strchr( psz_bol, '"');
371 else if ( p_m3u->i_type == TYPE_HTML )
373 /* We are dealing with a html file with embedded
374 * video. We are looking for "<param name="filename"
375 * value=" html markups that begin with "http://" */
379 strncasecmp( psz_bol, "param", sizeof("param") - 1 ) )
382 if( !*psz_bol ) return 0;
385 strncasecmp( psz_bol, "filename", sizeof("filename") - 1 ) )
388 if( !*psz_bol ) return 0;
391 strncasecmp( psz_bol, "http://",
392 sizeof("http://") - 1 ) )
395 if( !*psz_bol ) return 0;
397 psz_eol = strchr( psz_bol, '"');
404 else if ( p_m3u->i_type == TYPE_B4S )
409 msg_Dbg( p_demux, "b4s line=%s", psz_line );
410 /* We are dealing with a B4S file from Winamp 3 */
412 /* First, search for name *
413 * <Name>Blabla</Name> */
415 if( strstr ( psz_bol, "<Name>" ) )
419 strncasecmp( psz_bol,"Name",sizeof("Name") -1 ) )
422 if( !*psz_bol ) return 0;
424 psz_bol = psz_bol + 5 ;
425 /* We are now at the beginning of the name */
427 if( !psz_bol ) return 0;
430 psz_eol = strchr(psz_bol, '<' );
431 if( !psz_eol) return 0;
435 XMLSpecialChars( psz_bol );
437 strcpy( psz_data, psz_bol );
440 else if( strstr( psz_bol, "</entry>" ) || strstr( psz_bol, "</Entry>" ))
446 /* We are looking for <entry Playstring="blabla"> */
450 strncasecmp( psz_bol,"Playstring",sizeof("Playstring") -1 ) )
453 if( !*psz_bol ) return 0;
455 psz_bol = strchr( psz_bol, '=' );
456 if ( !psz_bol ) return 0;
460 psz_eol= strchr(psz_bol, '"');
461 if( !psz_eol ) return 0;
465 /* Handle the XML special characters */
466 XMLSpecialChars( psz_bol );
470 msg_Warn( p_demux, "unknown file type" );
475 if ( !*psz_bol ) return 0;
478 * From now on, we know we've got a meaningful line
481 /* check for a protocol name */
482 /* for URL, we should look for "://"
483 * for MRL (Media Resource Locator) ([[<access>][/<demux>]:][<source>]),
484 * we should look for ":"
485 * so we end up looking simply for ":"*/
486 /* PB: on some file systems, ':' are valid characters though*/
488 while( *psz_name && *psz_name!=':' )
493 if ( *psz_name && ( psz_name == psz_bol + 1 ) )
495 /* if it is not an URL,
496 * as it is unlikely to be an MRL (PB: if it is ?)
497 * it should be an absolute file name with the drive letter */
498 if ( *(psz_name+1) == '/' )/* "*:/" */
500 if ( *(psz_name+2) != '/' )/* not "*://" */
501 while ( *psz_name ) *psz_name++;/* so now (*psz_name==0) */
503 else while ( *psz_name ) *psz_name++;/* "*:*"*/
507 /* if the line doesn't specify a protocol name,
508 * check if the line has an absolute or relative path */
510 if( !*psz_name && *psz_bol != '/' )
511 /* If this line doesn't begin with a '/' */
516 && *(psz_bol+1)!=':' )
517 /* if this line doesn't begin with
518 * "/" or "\" or "*:" or "*:\" or "*:/" or "\\" */
521 /* assume the path is relative to the path of the m3u file. */
522 char *psz_path = strdup( p_demux->psz_path );
525 psz_name = strrchr( psz_path, '/' );
527 psz_name = strrchr( psz_path, '\\' );
528 if ( ! psz_name ) psz_name = strrchr( psz_path, '/' );
530 if( psz_name ) *psz_name = '\0';
531 else *psz_path = '\0';
533 psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
534 sprintf( psz_name, "%s/%s", psz_path, psz_bol );
536 if ( *psz_path != '\0' )
538 psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
539 sprintf( psz_name, "%s\\%s", psz_path, psz_bol );
541 else psz_name = strdup( psz_bol );
547 psz_name = strdup( psz_bol );
550 strcpy(psz_data, psz_name ) ;
554 if( p_m3u->i_type != TYPE_B4S )
562 static void ProcessLine ( demux_t *p_demux, playlist_t *p_playlist,
563 char *psz_line, char **ppsz_uri, char **ppsz_name,
564 int *pi_options, char ***pppsz_options,
567 char psz_data[MAX_LINE];
570 switch( ParseLine( p_demux, psz_line, psz_data, &b_next ) )
577 *ppsz_uri = strdup( psz_data );
584 *ppsz_name = strdup( psz_data );
588 *pppsz_options = realloc( *pppsz_options,
589 sizeof(char *) * *pi_options );
590 (*pppsz_options)[*pi_options - 1] = strdup( psz_data );
597 if( b_next && *ppsz_uri )
599 playlist_AddExt( p_playlist, *ppsz_uri, *ppsz_name,
600 PLAYLIST_INSERT, *pi_position,
601 -1, (const char **)*pppsz_options, *pi_options );
604 if( *ppsz_name ) free( *ppsz_name ); *ppsz_name = NULL;
605 free( *ppsz_uri ); *ppsz_uri = NULL;
607 for( ; *pi_options; (*pi_options)-- )
609 free( (*pppsz_options)[*pi_options - 1] );
610 if( *pi_options == 1 ) free( *pppsz_options );
612 *pppsz_options = NULL;
616 /*****************************************************************************
617 * Demux: reads and demuxes data packets
618 *****************************************************************************
619 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
620 *****************************************************************************/
621 static int Demux( demux_t *p_demux )
623 demux_sys_t *p_m3u = p_demux->p_sys;
625 char psz_line[MAX_LINE];
626 char p_buf[MAX_LINE], eol_tok;
627 int i_size, i_bufpos, i_linepos = 0;
628 playlist_t *p_playlist;
629 vlc_bool_t b_discard = VLC_FALSE;
631 char *psz_name = NULL;
632 char *psz_uri = NULL;
634 char **ppsz_options = NULL;
638 p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
642 msg_Err( p_demux, "can't find playlist" );
646 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
647 i_position = p_playlist->i_index + 1;
649 /* Depending on wether we are dealing with an m3u/asf file, the end of
650 * line token will be different */
651 if( p_m3u->i_type == TYPE_ASX || p_m3u->i_type == TYPE_HTML )
656 while( ( i_size = stream_Read( p_demux->s, p_buf, MAX_LINE ) ) )
662 /* Build a line < MAX_LINE */
663 while( p_buf[i_bufpos] != eol_tok && i_size )
665 if( i_linepos == MAX_LINE || b_discard == VLC_TRUE )
667 /* line is bigger than MAX_LINE, discard it */
669 b_discard = VLC_TRUE;
673 if ( eol_tok != '\n' || p_buf[i_bufpos] != '\r' )
675 psz_line[i_linepos] = p_buf[i_bufpos];
680 i_size--; i_bufpos++;
683 /* Check if we need more data */
684 if( !i_size ) continue;
686 i_size--; i_bufpos++;
687 b_discard = VLC_FALSE;
689 /* Check for empty line */
690 if( !i_linepos ) continue;
692 psz_line[i_linepos] = '\0';
695 ProcessLine( p_demux, p_playlist, psz_line, &psz_uri, &psz_name,
696 &i_options, &ppsz_options, &i_position );
700 if ( i_linepos && b_discard != VLC_TRUE && eol_tok == '\n' )
702 psz_line[i_linepos] = '\0';
704 ProcessLine( p_demux, p_playlist, psz_line, &psz_uri, &psz_name,
705 &i_options, &ppsz_options, &i_position );
707 /* Is there a pendding uri without b_next */
710 playlist_AddExt( p_playlist, psz_uri, psz_name,
711 PLAYLIST_INSERT, i_position,
712 -1, (const char **)ppsz_options, i_options );
716 if( psz_uri ) free( psz_uri );
717 if( psz_name ) free( psz_name );
718 for( ; i_options; i_options-- )
720 free( ppsz_options[i_options - 1] );
721 if( i_options == 1 ) free( ppsz_options );
724 vlc_object_release( p_playlist );
729 static int Control( demux_t *p_demux, int i_query, va_list args )