1 /*****************************************************************************
2 * m3u.c: a meta demux to parse pls, m3u, asx et b4s playlists
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
7 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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( "asx" );
72 add_shortcut( "html" );
73 add_shortcut( "b4s" );
76 /*****************************************************************************
77 * Activate: initializes m3u demux structures
78 *****************************************************************************/
79 static int Activate( vlc_object_t * p_this )
81 demux_t *p_demux = (demux_t *)p_this;
83 int i_type = TYPE_UNKNOWN;
84 int i_type2 = TYPE_UNKNOWN;
86 p_demux->pf_control = Control;
87 p_demux->pf_demux = Demux;
89 /* Check for m3u/asx file extension or if the demux has been forced */
90 psz_ext = strrchr ( p_demux->psz_path, '.' );
92 if(( psz_ext && !strcasecmp( psz_ext, ".ram") ) )
96 else if( ( psz_ext && !strcasecmp( psz_ext, ".asx") ) ||
97 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "asx") ) )
101 else if( ( psz_ext && !strcasecmp( psz_ext, ".html") ) ||
102 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "html") ) )
106 else if( ( psz_ext && !strcasecmp( psz_ext, ".b4s") ) ||
107 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "b4s") ) )
112 /* we had no luck looking at the file extension, so we have a look
113 * at the content. This is useful for .asp, .php and similar files
114 * that are actually html. Also useful for some asx files that have
115 * another extension */
116 /* We double check for file != m3u as some asx are just m3u file */
117 if( i_type != TYPE_M3U )
120 int i_size = stream_Peek( p_demux->s, (uint8_t **)&p_peek, MAX_LINE );
121 i_size -= sizeof("[Reference]") - 1;
126 strncasecmp(p_peek, "[playlist]", sizeof("[playlist]") - 1)
127 && strncasecmp( p_peek, "[Reference]", sizeof("[Reference]") - 1 )
128 && strncasecmp( p_peek, "<html>", sizeof("<html>") - 1 )
129 && strncasecmp( p_peek, "<asx", sizeof("<asx") - 1 )
130 && strncasecmp( p_peek, "rtsptext", sizeof("rtsptext") - 1 )
131 && strncasecmp( p_peek, "<?xml", sizeof("<?xml") -1 ) )
140 else if( !strncasecmp( p_peek, "[playlist]", sizeof("[playlist]") -1 ) )
144 else if( !strncasecmp( p_peek, "[Reference]", sizeof("[Reference]") -1 ) )
148 else if( !strncasecmp( p_peek, "<html>", sizeof("<html>") -1 ) )
152 else if( !strncasecmp( p_peek, "<asx", sizeof("<asx") -1 ) )
156 else if( !strncasecmp( p_peek, "rtsptext", sizeof("rtsptext") -1 ) )
161 else if( !strncasecmp( p_peek, "<?xml", sizeof("<?xml") -1 ) )
168 if( i_type == TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN)
172 if( i_type != TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN )
182 p_demux->p_sys = malloc( sizeof( demux_sys_t ) );
183 p_demux->p_sys->i_type = i_type;
184 msg_Dbg( p_this, "playlist type: %d - %d", i_type, i_type2 );
189 /*****************************************************************************
190 * Deactivate: frees unused data
191 *****************************************************************************/
192 static void Deactivate( vlc_object_t *p_this )
194 demux_t *p_demux = (demux_t *)p_this;
195 free( p_demux->p_sys );
198 /*****************************************************************************
199 * XMLSpecialChars: Handle the special chars in a XML file.
200 * ***************************************************************************/
201 static void XMLSpecialChars ( char *str )
211 * - should probably accept any sequence, rather than only those
212 * commonly found in French.
213 * - output may have to be UTF-8 encoded (cannot assume Latin-1)
215 if( !strncasecmp( src, "à", 6 ) ) *dst++ = '\xe0';
216 else if( !strncasecmp( src, "î", 6 ) ) *dst++ = '\xee';
217 else if( !strncasecmp( src, "'", 6 ) ) *dst++ = '\'';
218 else if( !strncasecmp( src, "è", 6 ) ) *dst++ = '\xe8';
219 else if( !strncasecmp( src, "é", 6 ) ) *dst++ = '\xe9';
220 else if( !strncasecmp( src, "ê", 6 ) ) *dst++ = '\xea';
236 /*****************************************************************************
237 * ParseLine: read a "line" from the file and add any entries found
238 * to the playlist. Returns:
239 * 0 if nothing was found
240 * 1 if a URI was found (it is then copied in psz_data)
241 * 2 if a name was found ( " )
243 * XXX psz_data has the same length that psz_line so no problem if you don't
245 * psz_line is \0 terminated
246 *****************************************************************************/
247 static int ParseLine( demux_t *p_demux, char *psz_line, char *psz_data,
248 vlc_bool_t *pb_done )
250 demux_sys_t *p_m3u = p_demux->p_sys;
251 char *psz_bol, *psz_name;
254 *pb_done = VLC_FALSE;
256 /* Remove unnecessary tabs or spaces at the beginning of line */
257 while( *psz_bol == ' ' || *psz_bol == '\t' ||
258 *psz_bol == '\n' || *psz_bol == '\r' )
263 if( p_m3u->i_type == TYPE_M3U )
265 /* Check for comment line */
266 if( *psz_bol == '#' )
269 strncasecmp( psz_bol, "EXTINF:",
270 sizeof("EXTINF:") - 1 ) &&
271 strncasecmp( psz_bol, "EXTVLCOPT:",
272 sizeof("EXTVLCOPT:") - 1 ) ) psz_bol++;
274 if( !*psz_bol ) return 0;
276 if( !strncasecmp( psz_bol, "EXTINF:", sizeof("EXTINF:") - 1 ) )
278 psz_bol = strchr( psz_bol, ',' );
279 if ( !psz_bol ) return 0;
282 /* From now, we have a name line */
283 strcpy( psz_data , psz_bol );
288 psz_bol = strchr( psz_bol, ':' );
289 if ( !psz_bol ) return 0;
292 strcpy( psz_data , psz_bol );
296 /* If we don't have a comment, the line is directly the URI */
298 else if( p_m3u->i_type == TYPE_PLS )
300 /* We are dealing with .pls files from shoutcast
301 * We are looking for lines like "File1=http://..." */
302 if( !strncasecmp( psz_bol, "File", sizeof("File") - 1 ) )
304 psz_bol += sizeof("File") - 1;
305 psz_bol = strchr( psz_bol, '=' );
306 if ( !psz_bol ) return 0;
314 else if( p_m3u->i_type == TYPE_WMP )
316 /* We are dealing with some weird WMP stream playlist format
317 * Hurray for idiotic M$. Lines look like: "Ref1=http://..." */
318 if( !strncasecmp( psz_bol, "Ref", sizeof("Ref") - 1 ) )
320 psz_bol += sizeof("Ref") - 1;
321 psz_bol = strchr( psz_bol, '=' );
322 if ( !psz_bol ) return 0;
324 if( !strncasecmp( psz_bol, "http://", sizeof("http://") -1 ) )
327 psz_bol[0] = 'm'; psz_bol[1] = 'm'; psz_bol[2] = 's';
335 else if( p_m3u->i_type == TYPE_ASX )
337 /* We are dealing with ASX files.
338 * We are looking for "<ref href=" xml markups that
339 * begins with "mms://", "http://" or "file://" */
343 strncasecmp( psz_bol, "ref", sizeof("ref") - 1 ) )
346 if( !*psz_bol ) return 0;
349 strncasecmp( psz_bol, "href", sizeof("href") - 1 ) )
352 if( !*psz_bol ) return 0;
355 strncasecmp( psz_bol, "mms://",
356 sizeof("mms://") - 1 ) &&
357 strncasecmp( psz_bol, "mmsu://",
358 sizeof("mmsu://") - 1 ) &&
359 strncasecmp( psz_bol, "mmst://",
360 sizeof("mmst://") - 1 ) &&
361 strncasecmp( psz_bol, "http://",
362 sizeof("http://") - 1 ) &&
363 strncasecmp( psz_bol, "file://",
364 sizeof("file://") - 1 ) )
367 if( !*psz_bol ) return 0;
369 psz_eol = strchr( psz_bol, '"');
375 else if( p_m3u->i_type == TYPE_HTML )
377 /* We are dealing with a html file with embedded
378 * video. We are looking for "<param name="filename"
379 * value=" html markups that begin with "http://" */
383 strncasecmp( psz_bol, "param", sizeof("param") - 1 ) )
386 if( !*psz_bol ) return 0;
389 strncasecmp( psz_bol, "filename", sizeof("filename") - 1 ) )
392 if( !*psz_bol ) return 0;
395 strncasecmp( psz_bol, "http://",
396 sizeof("http://") - 1 ) )
399 if( !*psz_bol ) return 0;
401 psz_eol = strchr( psz_bol, '"');
408 else if( p_m3u->i_type == TYPE_B4S )
413 msg_Dbg( p_demux, "b4s line=%s", psz_line );
414 /* We are dealing with a B4S file from Winamp 3 */
416 /* First, search for name *
417 * <Name>Blabla</Name> */
419 if( strstr ( psz_bol, "<Name>" ) )
423 strncasecmp( psz_bol,"Name",sizeof("Name") -1 ) )
426 if( !*psz_bol ) return 0;
428 psz_bol = psz_bol + 5 ;
429 /* We are now at the beginning of the name */
431 if( !psz_bol ) return 0;
434 psz_eol = strchr(psz_bol, '<' );
435 if( !psz_eol) return 0;
439 XMLSpecialChars( psz_bol );
441 strcpy( psz_data, psz_bol );
444 else if( strstr( psz_bol, "</entry>" ) || strstr( psz_bol, "</Entry>" ))
450 /* We are looking for <entry Playstring="blabla"> */
453 strncasecmp( psz_bol,"Playstring",sizeof("Playstring") -1 ) )
456 if( !*psz_bol ) return 0;
458 psz_bol = strchr( psz_bol, '=' );
459 if ( !psz_bol ) return 0;
463 psz_eol= strchr(psz_bol, '"');
464 if( !psz_eol ) return 0;
468 /* Handle the XML special characters */
469 XMLSpecialChars( psz_bol );
471 else if( p_m3u->i_type == TYPE_RTSP )
473 /* We are dealing with rtsptext reference files
474 * Ignore anthying that doesn't start with rtsp://..." */
475 if( strncasecmp( psz_bol, "rtsp://", sizeof("rtsp://") - 1 ) )
476 /* ignore */ return 0;
480 msg_Warn( p_demux, "unknown file type" );
485 if ( !*psz_bol ) return 0;
488 * From now on, we know we've got a meaningful line
491 /* check for a protocol name */
492 /* for URL, we should look for "://"
493 * for MRL (Media Resource Locator) ([[<access>][/<demux>]:][<source>]),
494 * we should look for ":"
495 * so we end up looking simply for ":"*/
496 /* PB: on some file systems, ':' are valid characters though*/
498 while( *psz_name && *psz_name!=':' )
503 if ( *psz_name && ( psz_name == psz_bol + 1 ) )
505 /* if it is not an URL,
506 * as it is unlikely to be an MRL (PB: if it is ?)
507 * it should be an absolute file name with the drive letter */
508 if ( *(psz_name+1) == '/' )/* "*:/" */
510 if ( *(psz_name+2) != '/' )/* not "*://" */
511 while ( *psz_name ) *psz_name++;/* so now (*psz_name==0) */
513 else while ( *psz_name ) *psz_name++;/* "*:*"*/
517 /* if the line doesn't specify a protocol name,
518 * check if the line has an absolute or relative path */
520 if( !*psz_name && *psz_bol != '/' )
521 /* If this line doesn't begin with a '/' */
526 && *(psz_bol+1)!=':' )
527 /* if this line doesn't begin with
528 * "/" or "\" or "*:" or "*:\" or "*:/" or "\\" */
531 /* assume the path is relative to the path of the m3u file. */
532 char *psz_path = strdup( p_demux->psz_path );
535 psz_name = strrchr( psz_path, '/' );
537 psz_name = strrchr( psz_path, '\\' );
538 if ( ! psz_name ) psz_name = strrchr( psz_path, '/' );
540 if( psz_name ) *psz_name = '\0';
541 else *psz_path = '\0';
543 psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
544 sprintf( psz_name, "%s/%s", psz_path, psz_bol );
546 if ( *psz_path != '\0' )
548 psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
549 sprintf( psz_name, "%s\\%s", psz_path, psz_bol );
551 else psz_name = strdup( psz_bol );
557 psz_name = strdup( psz_bol );
560 strcpy(psz_data, psz_name ) ;
564 if( p_m3u->i_type != TYPE_B4S )
572 static void ProcessLine ( demux_t *p_demux, playlist_t *p_playlist,
573 playlist_item_t *p_parent,
574 char *psz_line, char **ppsz_uri, char **ppsz_name,
575 int *pi_options, char ***pppsz_options,
578 char psz_data[MAX_LINE];
581 switch( ParseLine( p_demux, psz_line, psz_data, &b_done ) )
584 if( *ppsz_uri ) free( *ppsz_uri );
585 *ppsz_uri = strdup( psz_data );
588 if( *ppsz_name ) free( *ppsz_name );
589 *ppsz_name = strdup( psz_data );
593 *pppsz_options = realloc( *pppsz_options,
594 sizeof(char *) * *pi_options );
595 (*pppsz_options)[*pi_options - 1] = strdup( psz_data );
602 if( (b_done || b_flush) && *ppsz_uri )
604 playlist_item_t *p_item =
605 playlist_ItemNew( p_playlist, *ppsz_uri, *ppsz_name );
608 for( i = 0; i < *pi_options; i++ )
610 playlist_ItemAddOption( p_item, *pppsz_options[i] );
613 playlist_NodeAddItem( p_playlist, p_item,
614 p_parent->pp_parents[0]->i_view,
615 p_parent, PLAYLIST_APPEND, PLAYLIST_END );
617 /* We need to declare the parents of the node as the
618 * same of the parent's ones */
619 playlist_CopyParents( p_parent, p_item );
621 vlc_input_item_CopyOptions( &p_parent->input, &p_item->input );
623 if( *ppsz_name ) free( *ppsz_name ); *ppsz_name = NULL;
624 free( *ppsz_uri ); *ppsz_uri = NULL;
626 for( ; *pi_options; (*pi_options)-- )
628 free( (*pppsz_options)[*pi_options - 1] );
629 if( *pi_options == 1 ) free( *pppsz_options );
631 *pppsz_options = NULL;
635 static vlc_bool_t FindItem( demux_t *p_demux, playlist_t *p_playlist,
636 playlist_item_t **pp_item )
640 if( &p_playlist->status.p_item->input ==
641 ((input_thread_t *)p_demux->p_parent)->input.p_item )
643 msg_Dbg( p_playlist, "starting playlist playback" );
644 *pp_item = p_playlist->status.p_item;
649 input_item_t *p_current =
650 ((input_thread_t*)p_demux->p_parent)->input.p_item;
651 *pp_item = playlist_LockItemGetByInput( p_playlist, p_current );
654 msg_Dbg( p_playlist, "unable to find item in playlist");
662 /*****************************************************************************
663 * Demux: reads and demuxes data packets
664 *****************************************************************************
665 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
666 *****************************************************************************/
667 static int Demux( demux_t *p_demux )
669 demux_sys_t *p_m3u = p_demux->p_sys;
671 char psz_line[MAX_LINE];
672 char p_buf[MAX_LINE], eol_tok;
673 int i_size, i_bufpos, i_linepos = 0;
674 vlc_bool_t b_discard = VLC_FALSE;
676 char *psz_name = NULL;
677 char *psz_uri = NULL;
679 char **ppsz_options = NULL;
681 playlist_t *p_playlist;
682 playlist_item_t *p_parent;
685 p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
689 msg_Err( p_demux, "can't find playlist" );
693 b_play = FindItem( p_demux, p_playlist, &p_parent );
694 playlist_ItemToNode( p_playlist, p_parent );
695 p_parent->input.i_type = ITEM_TYPE_PLAYLIST;
697 /* Depending on wether we are dealing with an m3u/asf file, the end of
698 * line token will be different */
699 if( p_m3u->i_type == TYPE_ASX || p_m3u->i_type == TYPE_HTML )
704 while( ( i_size = stream_Read( p_demux->s, p_buf, MAX_LINE ) ) )
710 /* Build a line < MAX_LINE */
711 while( p_buf[i_bufpos] != eol_tok && i_size )
713 if( i_linepos == MAX_LINE || b_discard == VLC_TRUE )
715 /* line is bigger than MAX_LINE, discard it */
717 b_discard = VLC_TRUE;
721 if ( eol_tok != '\n' || p_buf[i_bufpos] != '\r' )
723 psz_line[i_linepos] = p_buf[i_bufpos];
728 i_size--; i_bufpos++;
731 /* Check if we need more data */
732 if( !i_size ) continue;
734 i_size--; i_bufpos++;
735 b_discard = VLC_FALSE;
737 /* Check for empty line */
738 if( !i_linepos ) continue;
740 psz_line[i_linepos] = '\0';
743 ProcessLine( p_demux, p_playlist, p_parent,
744 psz_line, &psz_uri, &psz_name,
745 &i_options, &ppsz_options, VLC_FALSE );
749 if( i_linepos && b_discard != VLC_TRUE && eol_tok == '\n' )
751 psz_line[i_linepos] = '\0';
753 ProcessLine( p_demux, p_playlist, p_parent,
754 psz_line, &psz_uri, &psz_name,
755 &i_options, &ppsz_options, VLC_TRUE );
758 if( psz_uri ) free( psz_uri );
759 if( psz_name ) free( psz_name );
760 for( ; i_options; i_options-- )
762 free( ppsz_options[i_options - 1] );
763 if( i_options == 1 ) free( ppsz_options );
766 /* Go back and play the playlist */
769 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
770 p_playlist->status.i_view,
771 p_playlist->status.p_item, NULL );
774 vlc_object_release( p_playlist );
779 static int Control( demux_t *p_demux, int i_query, va_list args )