1 /*****************************************************************************
2 * m3u.c: a meta demux to parse pls, m3u, asx et b4s playlists
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
7 * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
8 * Gildas Bazin <gbazin@videolan.org>
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
51 int i_type; /* playlist type (m3u/asx) */
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Activate ( vlc_object_t * );
58 static void Deactivate( vlc_object_t * );
59 static int Demux ( demux_t * );
60 static int Control ( demux_t *, int, va_list );
62 /*****************************************************************************
64 *****************************************************************************/
66 set_category( CAT_INPUT );
67 set_subcategory( SUBCAT_INPUT_DEMUX );
68 set_description( _("Playlist metademux") );
69 set_capability( "demux2", 5 );
70 set_callbacks( Activate, Deactivate );
71 add_shortcut( "m3u" );
72 add_shortcut( "asx" );
73 add_shortcut( "html" );
74 add_shortcut( "pls" );
75 add_shortcut( "b4s" );
78 /*****************************************************************************
79 * Activate: initializes m3u demux structures
80 *****************************************************************************/
81 static int Activate( vlc_object_t * p_this )
83 demux_t *p_demux = (demux_t *)p_this;
85 int i_type = TYPE_UNKNOWN;
86 int i_type2 = TYPE_UNKNOWN;
88 p_demux->pf_control = Control;
89 p_demux->pf_demux = Demux;
91 /* Check for m3u/asx file extension or if the demux has been forced */
92 psz_ext = strrchr ( p_demux->psz_path, '.' );
94 if( ( psz_ext && !strcasecmp( psz_ext, ".m3u") ) ||
95 /* a .ram file can contain a single rtsp link */
96 ( psz_ext && !strcasecmp( psz_ext, ".ram") ) ||
97 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "m3u") ) )
101 else if( ( psz_ext && !strcasecmp( psz_ext, ".asx") ) ||
102 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "asx") ) )
106 else if( ( psz_ext && !strcasecmp( psz_ext, ".html") ) ||
107 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "html") ) )
111 else if( ( psz_ext && !strcasecmp( psz_ext, ".pls") ) ||
112 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "pls") ) )
116 else if( ( psz_ext && !strcasecmp( psz_ext, ".b4s") ) ||
117 ( p_demux->psz_demux && !strcmp(p_demux->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 /* We double check for file != m3u as some asx are just m3u file */
127 if( i_type != TYPE_M3U )
130 int i_size = stream_Peek( p_demux->s, (uint8_t **)&p_peek, MAX_LINE );
131 i_size -= sizeof("[Reference]") - 1;
136 strncasecmp(p_peek, "[playlist]", sizeof("[playlist]") - 1)
137 && strncasecmp( p_peek, "[Reference]", sizeof("[Reference]") - 1 )
138 && strncasecmp( p_peek, "<html>", sizeof("<html>") - 1 )
139 && strncasecmp( p_peek, "<asx", sizeof("<asx") - 1 )
140 && strncasecmp( p_peek, "rtsptext", sizeof("rtsptext") - 1 )
141 && strncasecmp( p_peek, "<?xml", sizeof("<?xml") -1 ) )
150 else if( !strncasecmp( p_peek, "[playlist]", sizeof("[playlist]") -1 ) )
154 else if( !strncasecmp( p_peek, "[Reference]", sizeof("[Reference]") -1 ) )
158 else if( !strncasecmp( p_peek, "<html>", sizeof("<html>") -1 ) )
162 else if( !strncasecmp( p_peek, "<asx", sizeof("<asx") -1 ) )
166 else if( !strncasecmp( p_peek, "rtsptext", sizeof("rtsptext") -1 ) )
171 else if( !strncasecmp( p_peek, "<?xml", sizeof("<?xml") -1 ) )
178 if( i_type == TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN)
182 if( i_type != TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN )
192 p_demux->p_sys = malloc( sizeof( demux_sys_t ) );
193 p_demux->p_sys->i_type = i_type;
194 msg_Dbg( p_this, "Playlist type: %d - %d", i_type, i_type2 );
199 /*****************************************************************************
200 * Deactivate: frees unused data
201 *****************************************************************************/
202 static void Deactivate( vlc_object_t *p_this )
204 demux_t *p_demux = (demux_t *)p_this;
205 free( p_demux->p_sys );
208 /*****************************************************************************
209 * XMLSpecialChars: Handle the special chars in a XML file.
210 * ***************************************************************************/
211 static void XMLSpecialChars ( char *str )
220 if( !strncasecmp( src, "à", 6 ) ) *dst++ = 'à';
221 else if( !strncasecmp( src, "î", 6 ) ) *dst++ = 'î';
222 else if( !strncasecmp( src, "'", 6 ) ) *dst++ = '\'';
223 else if( !strncasecmp( src, "è", 6 ) ) *dst++ = 'è';
224 else if( !strncasecmp( src, "é", 6 ) ) *dst++ = 'é';
225 else if( !strncasecmp( src, "ê", 6 ) ) *dst++ = 'ê';
241 /*****************************************************************************
242 * ParseLine: read a "line" from the file and add any entries found
243 * to the playlist. Returns:
244 * 0 if nothing was found
245 * 1 if a URI was found (it is then copied in psz_data)
246 * 2 if a name was found ( " )
248 * XXX psz_data has the same length that psz_line so no problem if you don't
250 * psz_line is \0 terminated
251 *****************************************************************************/
252 static int ParseLine( demux_t *p_demux, char *psz_line, char *psz_data,
253 vlc_bool_t *pb_done )
255 demux_sys_t *p_m3u = p_demux->p_sys;
256 char *psz_bol, *psz_name;
259 *pb_done = VLC_FALSE;
261 /* Remove unnecessary tabs or spaces at the beginning of line */
262 while( *psz_bol == ' ' || *psz_bol == '\t' ||
263 *psz_bol == '\n' || *psz_bol == '\r' )
268 if( p_m3u->i_type == TYPE_M3U )
270 /* Check for comment line */
271 if( *psz_bol == '#' )
274 strncasecmp( psz_bol, "EXTINF:",
275 sizeof("EXTINF:") - 1 ) &&
276 strncasecmp( psz_bol, "EXTVLCOPT:",
277 sizeof("EXTVLCOPT:") - 1 ) ) psz_bol++;
279 if( !*psz_bol ) return 0;
281 if( !strncasecmp( psz_bol, "EXTINF:", sizeof("EXTINF:") - 1 ) )
283 psz_bol = strchr( psz_bol, ',' );
284 if ( !psz_bol ) return 0;
287 /* From now, we have a name line */
288 strcpy( psz_data , psz_bol );
293 psz_bol = strchr( psz_bol, ':' );
294 if ( !psz_bol ) return 0;
297 strcpy( psz_data , psz_bol );
301 /* If we don't have a comment, the line is directly the URI */
303 else if( p_m3u->i_type == TYPE_PLS )
305 /* We are dealing with .pls files from shoutcast
306 * We are looking for lines like "File1=http://..." */
307 if( !strncasecmp( psz_bol, "File", sizeof("File") - 1 ) )
309 psz_bol += sizeof("File") - 1;
310 psz_bol = strchr( psz_bol, '=' );
311 if ( !psz_bol ) return 0;
319 else if( p_m3u->i_type == TYPE_WMP )
321 /* We are dealing with some weird WMP stream playlist format
322 * Hurray for idiotic M$. Lines look like: "Ref1=http://..." */
323 if( !strncasecmp( psz_bol, "Ref", sizeof("Ref") - 1 ) )
325 psz_bol += sizeof("Ref") - 1;
326 psz_bol = strchr( psz_bol, '=' );
327 if ( !psz_bol ) return 0;
329 if( !strncasecmp( psz_bol, "http://", sizeof("http://") -1 ) )
331 psz_bol[0] = 'm'; psz_bol[1] = 'm'; psz_bol[2] = 's'; psz_bol[3] = 'h';
339 else if( p_m3u->i_type == TYPE_ASX )
341 /* We are dealing with ASX files.
342 * We are looking for "<ref href=" xml markups that
343 * begins with "mms://", "http://" or "file://" */
347 strncasecmp( psz_bol, "ref", sizeof("ref") - 1 ) )
350 if( !*psz_bol ) return 0;
353 strncasecmp( psz_bol, "href", sizeof("href") - 1 ) )
356 if( !*psz_bol ) return 0;
359 strncasecmp( psz_bol, "mms://",
360 sizeof("mms://") - 1 ) &&
361 strncasecmp( psz_bol, "mmsu://",
362 sizeof("mmsu://") - 1 ) &&
363 strncasecmp( psz_bol, "mmst://",
364 sizeof("mmst://") - 1 ) &&
365 strncasecmp( psz_bol, "http://",
366 sizeof("http://") - 1 ) &&
367 strncasecmp( psz_bol, "file://",
368 sizeof("file://") - 1 ) )
371 if( !*psz_bol ) return 0;
373 psz_eol = strchr( psz_bol, '"');
379 else if( p_m3u->i_type == TYPE_HTML )
381 /* We are dealing with a html file with embedded
382 * video. We are looking for "<param name="filename"
383 * value=" html markups that begin with "http://" */
387 strncasecmp( psz_bol, "param", sizeof("param") - 1 ) )
390 if( !*psz_bol ) return 0;
393 strncasecmp( psz_bol, "filename", sizeof("filename") - 1 ) )
396 if( !*psz_bol ) return 0;
399 strncasecmp( psz_bol, "http://",
400 sizeof("http://") - 1 ) )
403 if( !*psz_bol ) return 0;
405 psz_eol = strchr( psz_bol, '"');
412 else if( p_m3u->i_type == TYPE_B4S )
417 msg_Dbg( p_demux, "b4s line=%s", psz_line );
418 /* We are dealing with a B4S file from Winamp 3 */
420 /* First, search for name *
421 * <Name>Blabla</Name> */
423 if( strstr ( psz_bol, "<Name>" ) )
427 strncasecmp( psz_bol,"Name",sizeof("Name") -1 ) )
430 if( !*psz_bol ) return 0;
432 psz_bol = psz_bol + 5 ;
433 /* We are now at the beginning of the name */
435 if( !psz_bol ) return 0;
438 psz_eol = strchr(psz_bol, '<' );
439 if( !psz_eol) return 0;
443 XMLSpecialChars( psz_bol );
445 strcpy( psz_data, psz_bol );
448 else if( strstr( psz_bol, "</entry>" ) || strstr( psz_bol, "</Entry>" ))
454 /* We are looking for <entry Playstring="blabla"> */
457 strncasecmp( psz_bol,"Playstring",sizeof("Playstring") -1 ) )
460 if( !*psz_bol ) return 0;
462 psz_bol = strchr( psz_bol, '=' );
463 if ( !psz_bol ) return 0;
467 psz_eol= strchr(psz_bol, '"');
468 if( !psz_eol ) return 0;
472 /* Handle the XML special characters */
473 XMLSpecialChars( psz_bol );
475 else if( p_m3u->i_type == TYPE_RTSP )
477 /* We are dealing with rtsptext reference files
478 * Ignore anthying that doesn't start with rtsp://..." */
479 if( strncasecmp( psz_bol, "rtsp://", sizeof("rtsp://") - 1 ) )
480 /* ignore */ return 0;
484 msg_Warn( p_demux, "unknown file type" );
489 if ( !*psz_bol ) return 0;
492 * From now on, we know we've got a meaningful line
495 /* check for a protocol name */
496 /* for URL, we should look for "://"
497 * for MRL (Media Resource Locator) ([[<access>][/<demux>]:][<source>]),
498 * we should look for ":"
499 * so we end up looking simply for ":"*/
500 /* PB: on some file systems, ':' are valid characters though*/
502 while( *psz_name && *psz_name!=':' )
507 if ( *psz_name && ( psz_name == psz_bol + 1 ) )
509 /* if it is not an URL,
510 * as it is unlikely to be an MRL (PB: if it is ?)
511 * it should be an absolute file name with the drive letter */
512 if ( *(psz_name+1) == '/' )/* "*:/" */
514 if ( *(psz_name+2) != '/' )/* not "*://" */
515 while ( *psz_name ) *psz_name++;/* so now (*psz_name==0) */
517 else while ( *psz_name ) *psz_name++;/* "*:*"*/
521 /* if the line doesn't specify a protocol name,
522 * check if the line has an absolute or relative path */
524 if( !*psz_name && *psz_bol != '/' )
525 /* If this line doesn't begin with a '/' */
530 && *(psz_bol+1)!=':' )
531 /* if this line doesn't begin with
532 * "/" or "\" or "*:" or "*:\" or "*:/" or "\\" */
535 /* assume the path is relative to the path of the m3u file. */
536 char *psz_path = strdup( p_demux->psz_path );
539 psz_name = strrchr( psz_path, '/' );
541 psz_name = strrchr( psz_path, '\\' );
542 if ( ! psz_name ) psz_name = strrchr( psz_path, '/' );
544 if( psz_name ) *psz_name = '\0';
545 else *psz_path = '\0';
547 psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
548 sprintf( psz_name, "%s/%s", psz_path, psz_bol );
550 if ( *psz_path != '\0' )
552 psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
553 sprintf( psz_name, "%s\\%s", psz_path, psz_bol );
555 else psz_name = strdup( psz_bol );
561 psz_name = strdup( psz_bol );
564 strcpy(psz_data, psz_name ) ;
568 if( p_m3u->i_type != TYPE_B4S )
576 static void ProcessLine ( demux_t *p_demux, playlist_t *p_playlist,
577 playlist_item_t *p_parent,
578 char *psz_line, char **ppsz_uri, char **ppsz_name,
579 int *pi_options, char ***pppsz_options,
582 char psz_data[MAX_LINE];
585 switch( ParseLine( p_demux, psz_line, psz_data, &b_done ) )
588 if( *ppsz_uri ) free( *ppsz_uri );
589 *ppsz_uri = strdup( psz_data );
592 if( *ppsz_name ) free( *ppsz_name );
593 *ppsz_name = strdup( psz_data );
597 *pppsz_options = realloc( *pppsz_options,
598 sizeof(char *) * *pi_options );
599 (*pppsz_options)[*pi_options - 1] = strdup( psz_data );
606 if( (b_done || b_flush) && *ppsz_uri )
608 playlist_item_t *p_item =
609 playlist_ItemNew( p_playlist, *ppsz_uri, *ppsz_name );
612 for( i = 0; i < *pi_options; i++ )
614 playlist_ItemAddOption( p_item, *pppsz_options[i] );
617 playlist_NodeAddItem( p_playlist, p_item,
618 p_parent->pp_parents[0]->i_view,
619 p_parent, PLAYLIST_APPEND, PLAYLIST_END );
621 /* We need to declare the parents of the node as the
622 * same of the parent's ones */
623 playlist_CopyParents( p_parent, p_item );
625 vlc_input_item_CopyOptions( &p_parent->input, &p_item->input );
627 if( *ppsz_name ) free( *ppsz_name ); *ppsz_name = NULL;
628 free( *ppsz_uri ); *ppsz_uri = NULL;
630 for( ; *pi_options; (*pi_options)-- )
632 free( (*pppsz_options)[*pi_options - 1] );
633 if( *pi_options == 1 ) free( *pppsz_options );
635 *pppsz_options = NULL;
639 static vlc_bool_t FindItem( demux_t *p_demux, playlist_t *p_playlist,
640 playlist_item_t **pp_item )
644 if( &p_playlist->status.p_item->input ==
645 ((input_thread_t *)p_demux->p_parent)->input.p_item )
647 msg_Dbg( p_playlist, "starting playlist playback" );
648 *pp_item = p_playlist->status.p_item;
653 input_item_t *p_current =
654 ((input_thread_t*)p_demux->p_parent)->input.p_item;
655 *pp_item = playlist_LockItemGetByInput( p_playlist, p_current );
658 msg_Dbg( p_playlist, "unable to find item in playlist");
666 /*****************************************************************************
667 * Demux: reads and demuxes data packets
668 *****************************************************************************
669 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
670 *****************************************************************************/
671 static int Demux( demux_t *p_demux )
673 demux_sys_t *p_m3u = p_demux->p_sys;
675 char psz_line[MAX_LINE];
676 char p_buf[MAX_LINE], eol_tok;
677 int i_size, i_bufpos, i_linepos = 0;
678 vlc_bool_t b_discard = VLC_FALSE;
680 char *psz_name = NULL;
681 char *psz_uri = NULL;
683 char **ppsz_options = NULL;
685 playlist_t *p_playlist;
686 playlist_item_t *p_parent;
689 p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
693 msg_Err( p_demux, "can't find playlist" );
697 b_play = FindItem( p_demux, p_playlist, &p_parent );
698 playlist_ItemToNode( p_playlist, p_parent );
699 p_parent->input.i_type = ITEM_TYPE_PLAYLIST;
701 /* Depending on wether we are dealing with an m3u/asf file, the end of
702 * line token will be different */
703 if( p_m3u->i_type == TYPE_ASX || p_m3u->i_type == TYPE_HTML )
708 while( ( i_size = stream_Read( p_demux->s, p_buf, MAX_LINE ) ) )
714 /* Build a line < MAX_LINE */
715 while( p_buf[i_bufpos] != eol_tok && i_size )
717 if( i_linepos == MAX_LINE || b_discard == VLC_TRUE )
719 /* line is bigger than MAX_LINE, discard it */
721 b_discard = VLC_TRUE;
725 if ( eol_tok != '\n' || p_buf[i_bufpos] != '\r' )
727 psz_line[i_linepos] = p_buf[i_bufpos];
732 i_size--; i_bufpos++;
735 /* Check if we need more data */
736 if( !i_size ) continue;
738 i_size--; i_bufpos++;
739 b_discard = VLC_FALSE;
741 /* Check for empty line */
742 if( !i_linepos ) continue;
744 psz_line[i_linepos] = '\0';
747 ProcessLine( p_demux, p_playlist, p_parent,
748 psz_line, &psz_uri, &psz_name,
749 &i_options, &ppsz_options, VLC_FALSE );
753 if( i_linepos && b_discard != VLC_TRUE && eol_tok == '\n' )
755 psz_line[i_linepos] = '\0';
757 ProcessLine( p_demux, p_playlist, p_parent,
758 psz_line, &psz_uri, &psz_name,
759 &i_options, &ppsz_options, VLC_TRUE );
762 if( psz_uri ) free( psz_uri );
763 if( psz_name ) free( psz_name );
764 for( ; i_options; i_options-- )
766 free( ppsz_options[i_options - 1] );
767 if( i_options == 1 ) free( ppsz_options );
770 /* Go back and play the playlist */
773 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
774 p_playlist->status.i_view,
775 p_playlist->status.p_item, NULL );
778 vlc_object_release( p_playlist );
783 static int Control( demux_t *p_demux, int i_query, va_list args )