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@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_description( _("Playlist metademux") );
67 set_capability( "demux2", 5 );
68 set_callbacks( Activate, Deactivate );
69 add_shortcut( "m3u" );
70 add_shortcut( "asx" );
71 add_shortcut( "html" );
72 add_shortcut( "pls" );
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, ".m3u") ) ||
93 /* a .ram file can contain a single rtsp link */
94 ( psz_ext && !strcasecmp( psz_ext, ".ram") ) ||
95 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "m3u") ) )
99 else if( ( psz_ext && !strcasecmp( psz_ext, ".asx") ) ||
100 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "asx") ) )
104 else if( ( psz_ext && !strcasecmp( psz_ext, ".html") ) ||
105 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "html") ) )
109 else if( ( psz_ext && !strcasecmp( psz_ext, ".pls") ) ||
110 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "pls") ) )
114 else if( ( psz_ext && !strcasecmp( psz_ext, ".b4s") ) ||
115 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "b4s") ) )
120 /* we had no luck looking at the file extention, so we have a look
121 * at the content. This is useful for .asp, .php and similar files
122 * that are actually html. Also useful for some asx files that have
123 * another extension */
124 /* We double check for file != m3u as some asx are just m3u file */
125 if( i_type != TYPE_M3U )
128 int i_size = stream_Peek( p_demux->s, &p_peek, MAX_LINE );
129 i_size -= sizeof("[Reference]") - 1;
134 strncasecmp(p_peek, "[playlist]", sizeof("[playlist]") - 1)
135 && strncasecmp( p_peek, "[Reference]", sizeof("[Reference]") - 1 )
136 && strncasecmp( p_peek, "<html>", sizeof("<html>") - 1 )
137 && strncasecmp( p_peek, "<asx", sizeof("<asx") - 1 )
138 && strncasecmp( p_peek, "rtsptext", sizeof("rtsptext") - 1 )
139 && strncasecmp( p_peek, "<?xml", sizeof("<?xml") -1 ) )
148 else if( !strncasecmp( p_peek, "[playlist]", sizeof("[playlist]") -1 ) )
152 else if( !strncasecmp( p_peek, "[Reference]", sizeof("[Reference]") -1 ) )
156 else if( !strncasecmp( p_peek, "<html>", sizeof("<html>") -1 ) )
160 else if( !strncasecmp( p_peek, "<asx", sizeof("<asx") -1 ) )
164 else if( !strncasecmp( p_peek, "rtsptext", sizeof("rtsptext") -1 ) )
169 else if( !strncasecmp( p_peek, "<?xml", sizeof("<?xml") -1 ) )
176 if( i_type == TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN)
180 if( i_type != TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN )
190 p_demux->p_sys = malloc( sizeof( demux_sys_t ) );
191 p_demux->p_sys->i_type = i_type;
192 msg_Dbg( p_this, "Playlist type: %d - %d", i_type, i_type2 );
197 /*****************************************************************************
198 * Deactivate: frees unused data
199 *****************************************************************************/
200 static void Deactivate( vlc_object_t *p_this )
202 demux_t *p_demux = (demux_t *)p_this;
203 free( p_demux->p_sys );
206 /*****************************************************************************
207 * XMLSpecialChars: Handle the special chars in a XML file.
208 * ***************************************************************************/
209 static void XMLSpecialChars ( char *str )
218 if( !strncasecmp( src, "à", 6 ) ) *dst++ = 'à';
219 else if( !strncasecmp( src, "î", 6 ) ) *dst++ = 'î';
220 else 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++ = 'ê';
239 /*****************************************************************************
240 * ParseLine: read a "line" from the file and add any entries found
241 * to the playlist. Returns:
242 * 0 if nothing was found
243 * 1 if a URI was found (it is then copied in psz_data)
244 * 2 if a name was found ( " )
246 * XXX psz_data has the same length that psz_line so no problem if you don't
248 * psz_line is \0 terminated
249 *****************************************************************************/
250 static int ParseLine( demux_t *p_demux, char *psz_line, char *psz_data,
251 vlc_bool_t *pb_done )
253 demux_sys_t *p_m3u = p_demux->p_sys;
254 char *psz_bol, *psz_name;
257 *pb_done = VLC_FALSE;
259 /* Remove unnecessary tabs or spaces at the beginning of line */
260 while( *psz_bol == ' ' || *psz_bol == '\t' ||
261 *psz_bol == '\n' || *psz_bol == '\r' )
266 if( p_m3u->i_type == TYPE_M3U )
268 /* Check for comment line */
269 if( *psz_bol == '#' )
272 strncasecmp( psz_bol, "EXTINF:",
273 sizeof("EXTINF:") - 1 ) &&
274 strncasecmp( psz_bol, "EXTVLCOPT:",
275 sizeof("EXTVLCOPT:") - 1 ) ) psz_bol++;
277 if( !*psz_bol ) return 0;
279 if( !strncasecmp( psz_bol, "EXTINF:", sizeof("EXTINF:") - 1 ) )
281 psz_bol = strchr( psz_bol, ',' );
282 if ( !psz_bol ) return 0;
285 /* From now, we have a name line */
286 strcpy( psz_data , psz_bol );
291 psz_bol = strchr( psz_bol, ':' );
292 if ( !psz_bol ) return 0;
295 strcpy( psz_data , psz_bol );
299 /* If we don't have a comment, the line is directly the URI */
301 else if( p_m3u->i_type == TYPE_PLS )
303 /* We are dealing with .pls files from shoutcast
304 * We are looking for lines like "File1=http://..." */
305 if( !strncasecmp( psz_bol, "File", sizeof("File") - 1 ) )
307 psz_bol += sizeof("File") - 1;
308 psz_bol = strchr( psz_bol, '=' );
309 if ( !psz_bol ) return 0;
317 else if( p_m3u->i_type == TYPE_WMP )
319 /* We are dealing with some weird WMP stream playlist format
320 * Hurray for idiotic M$. Lines look like: "Ref1=http://..." */
321 if( !strncasecmp( psz_bol, "Ref", sizeof("Ref") - 1 ) )
323 psz_bol += sizeof("Ref") - 1;
324 psz_bol = strchr( psz_bol, '=' );
325 if ( !psz_bol ) return 0;
327 if( !strncasecmp( psz_bol, "http://", sizeof("http://") -1 ) )
329 psz_bol[0] = 'm'; psz_bol[1] = 'm'; psz_bol[2] = 's'; psz_bol[3] = 'h';
337 else if( p_m3u->i_type == TYPE_ASX )
339 /* We are dealing with ASX files.
340 * We are looking for "<ref href=" xml markups that
341 * begins with "mms://", "http://" or "file://" */
345 strncasecmp( psz_bol, "ref", sizeof("ref") - 1 ) )
348 if( !*psz_bol ) return 0;
351 strncasecmp( psz_bol, "href", sizeof("href") - 1 ) )
354 if( !*psz_bol ) return 0;
357 strncasecmp( psz_bol, "mms://",
358 sizeof("mms://") - 1 ) &&
359 strncasecmp( psz_bol, "mmsu://",
360 sizeof("mmsu://") - 1 ) &&
361 strncasecmp( psz_bol, "mmst://",
362 sizeof("mmst://") - 1 ) &&
363 strncasecmp( psz_bol, "http://",
364 sizeof("http://") - 1 ) &&
365 strncasecmp( psz_bol, "file://",
366 sizeof("file://") - 1 ) )
369 if( !*psz_bol ) return 0;
371 psz_eol = strchr( psz_bol, '"');
377 else if( p_m3u->i_type == TYPE_HTML )
379 /* We are dealing with a html file with embedded
380 * video. We are looking for "<param name="filename"
381 * value=" html markups that begin with "http://" */
385 strncasecmp( psz_bol, "param", sizeof("param") - 1 ) )
388 if( !*psz_bol ) return 0;
391 strncasecmp( psz_bol, "filename", sizeof("filename") - 1 ) )
394 if( !*psz_bol ) return 0;
397 strncasecmp( psz_bol, "http://",
398 sizeof("http://") - 1 ) )
401 if( !*psz_bol ) return 0;
403 psz_eol = strchr( psz_bol, '"');
410 else if( p_m3u->i_type == TYPE_B4S )
415 msg_Dbg( p_demux, "b4s line=%s", psz_line );
416 /* We are dealing with a B4S file from Winamp 3 */
418 /* First, search for name *
419 * <Name>Blabla</Name> */
421 if( strstr ( psz_bol, "<Name>" ) )
425 strncasecmp( psz_bol,"Name",sizeof("Name") -1 ) )
428 if( !*psz_bol ) return 0;
430 psz_bol = psz_bol + 5 ;
431 /* We are now at the beginning of the name */
433 if( !psz_bol ) return 0;
436 psz_eol = strchr(psz_bol, '<' );
437 if( !psz_eol) return 0;
441 XMLSpecialChars( psz_bol );
443 strcpy( psz_data, psz_bol );
446 else if( strstr( psz_bol, "</entry>" ) || strstr( psz_bol, "</Entry>" ))
452 /* We are looking for <entry Playstring="blabla"> */
455 strncasecmp( psz_bol,"Playstring",sizeof("Playstring") -1 ) )
458 if( !*psz_bol ) return 0;
460 psz_bol = strchr( psz_bol, '=' );
461 if ( !psz_bol ) return 0;
465 psz_eol= strchr(psz_bol, '"');
466 if( !psz_eol ) return 0;
470 /* Handle the XML special characters */
471 XMLSpecialChars( psz_bol );
473 else if( p_m3u->i_type == TYPE_RTSP )
475 /* We are dealing with rtsptext reference files
476 * Ignore anthying that doesn't start with rtsp://..." */
477 if( strncasecmp( psz_bol, "rtsp://", sizeof("rtsp://") - 1 ) )
478 /* ignore */ return 0;
482 msg_Warn( p_demux, "unknown file type" );
487 if ( !*psz_bol ) return 0;
490 * From now on, we know we've got a meaningful line
493 /* check for a protocol name */
494 /* for URL, we should look for "://"
495 * for MRL (Media Resource Locator) ([[<access>][/<demux>]:][<source>]),
496 * we should look for ":"
497 * so we end up looking simply for ":"*/
498 /* PB: on some file systems, ':' are valid characters though*/
500 while( *psz_name && *psz_name!=':' )
505 if ( *psz_name && ( psz_name == psz_bol + 1 ) )
507 /* if it is not an URL,
508 * as it is unlikely to be an MRL (PB: if it is ?)
509 * it should be an absolute file name with the drive letter */
510 if ( *(psz_name+1) == '/' )/* "*:/" */
512 if ( *(psz_name+2) != '/' )/* not "*://" */
513 while ( *psz_name ) *psz_name++;/* so now (*psz_name==0) */
515 else while ( *psz_name ) *psz_name++;/* "*:*"*/
519 /* if the line doesn't specify a protocol name,
520 * check if the line has an absolute or relative path */
522 if( !*psz_name && *psz_bol != '/' )
523 /* If this line doesn't begin with a '/' */
528 && *(psz_bol+1)!=':' )
529 /* if this line doesn't begin with
530 * "/" or "\" or "*:" or "*:\" or "*:/" or "\\" */
533 /* assume the path is relative to the path of the m3u file. */
534 char *psz_path = strdup( p_demux->psz_path );
537 psz_name = strrchr( psz_path, '/' );
539 psz_name = strrchr( psz_path, '\\' );
540 if ( ! psz_name ) psz_name = strrchr( psz_path, '/' );
542 if( psz_name ) *psz_name = '\0';
543 else *psz_path = '\0';
545 psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
546 sprintf( psz_name, "%s/%s", psz_path, psz_bol );
548 if ( *psz_path != '\0' )
550 psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
551 sprintf( psz_name, "%s\\%s", psz_path, psz_bol );
553 else psz_name = strdup( psz_bol );
559 psz_name = strdup( psz_bol );
562 strcpy(psz_data, psz_name ) ;
566 if( p_m3u->i_type != TYPE_B4S )
574 static void ProcessLine ( demux_t *p_demux, playlist_t *p_playlist,
575 playlist_item_t *p_parent,
576 char *psz_line, char **ppsz_uri, char **ppsz_name,
577 int *pi_options, char ***pppsz_options,
580 char psz_data[MAX_LINE];
583 switch( ParseLine( p_demux, psz_line, psz_data, &b_done ) )
586 if( *ppsz_uri ) free( *ppsz_uri );
587 *ppsz_uri = strdup( psz_data );
590 if( *ppsz_name ) free( *ppsz_name );
591 *ppsz_name = strdup( psz_data );
595 *pppsz_options = realloc( *pppsz_options,
596 sizeof(char *) * *pi_options );
597 (*pppsz_options)[*pi_options - 1] = strdup( psz_data );
604 if( (b_done || b_flush) && *ppsz_uri )
606 playlist_item_t *p_item =
607 playlist_ItemNew( p_playlist, *ppsz_uri, *ppsz_name );
610 for( i = 0; i < *pi_options; i++ )
612 playlist_ItemAddOption( p_item, *pppsz_options[i] );
615 playlist_NodeAddItem( p_playlist, p_item,
616 p_parent->pp_parents[0]->i_view,
617 p_parent, PLAYLIST_APPEND, PLAYLIST_END );
619 /* We need to declare the parents of the node as the
620 * same of the parent's ones */
621 playlist_CopyParents( p_parent, p_item );
623 vlc_input_item_CopyOptions( &p_parent->input, &p_item->input );
625 if( *ppsz_name ) free( *ppsz_name ); *ppsz_name = NULL;
626 free( *ppsz_uri ); *ppsz_uri = NULL;
628 for( ; *pi_options; (*pi_options)-- )
630 free( (*pppsz_options)[*pi_options - 1] );
631 if( *pi_options == 1 ) free( *pppsz_options );
633 *pppsz_options = NULL;
637 static vlc_bool_t FindItem( demux_t *p_demux, playlist_t *p_playlist,
638 playlist_item_t **pp_item )
642 if( &p_playlist->status.p_item->input ==
643 ((input_thread_t *)p_demux->p_parent)->input.p_item )
645 msg_Dbg( p_playlist, "starting playlist playback" );
646 *pp_item = p_playlist->status.p_item;
651 input_item_t *p_current =
652 ((input_thread_t*)p_demux->p_parent)->input.p_item;
653 *pp_item = playlist_ItemGetByInput( p_playlist, p_current );
656 msg_Dbg( p_playlist, "unable to find item in playlist");
664 /*****************************************************************************
665 * Demux: reads and demuxes data packets
666 *****************************************************************************
667 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
668 *****************************************************************************/
669 static int Demux( demux_t *p_demux )
671 demux_sys_t *p_m3u = p_demux->p_sys;
673 char psz_line[MAX_LINE];
674 char p_buf[MAX_LINE], eol_tok;
675 int i_size, i_bufpos, i_linepos = 0;
676 vlc_bool_t b_discard = VLC_FALSE;
678 char *psz_name = NULL;
679 char *psz_uri = NULL;
681 char **ppsz_options = NULL;
683 playlist_t *p_playlist;
684 playlist_item_t *p_parent;
687 p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
691 msg_Err( p_demux, "can't find playlist" );
695 b_play = FindItem( p_demux, p_playlist, &p_parent );
696 playlist_ItemToNode( p_playlist, p_parent );
697 p_parent->input.i_type = ITEM_TYPE_PLAYLIST;
699 /* Depending on wether we are dealing with an m3u/asf file, the end of
700 * line token will be different */
701 if( p_m3u->i_type == TYPE_ASX || p_m3u->i_type == TYPE_HTML )
706 while( ( i_size = stream_Read( p_demux->s, p_buf, MAX_LINE ) ) )
712 /* Build a line < MAX_LINE */
713 while( p_buf[i_bufpos] != eol_tok && i_size )
715 if( i_linepos == MAX_LINE || b_discard == VLC_TRUE )
717 /* line is bigger than MAX_LINE, discard it */
719 b_discard = VLC_TRUE;
723 if ( eol_tok != '\n' || p_buf[i_bufpos] != '\r' )
725 psz_line[i_linepos] = p_buf[i_bufpos];
730 i_size--; i_bufpos++;
733 /* Check if we need more data */
734 if( !i_size ) continue;
736 i_size--; i_bufpos++;
737 b_discard = VLC_FALSE;
739 /* Check for empty line */
740 if( !i_linepos ) continue;
742 psz_line[i_linepos] = '\0';
745 ProcessLine( p_demux, p_playlist, p_parent,
746 psz_line, &psz_uri, &psz_name,
747 &i_options, &ppsz_options, VLC_FALSE );
751 if( i_linepos && b_discard != VLC_TRUE && eol_tok == '\n' )
753 psz_line[i_linepos] = '\0';
755 ProcessLine( p_demux, p_playlist, p_parent,
756 psz_line, &psz_uri, &psz_name,
757 &i_options, &ppsz_options, VLC_TRUE );
760 if( psz_uri ) free( psz_uri );
761 if( psz_name ) free( psz_name );
762 for( ; i_options; i_options-- )
764 free( ppsz_options[i_options - 1] );
765 if( i_options == 1 ) free( ppsz_options );
768 /* Go back and play the playlist */
771 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
772 p_playlist->status.i_view,
773 p_playlist->status.p_item, NULL );
776 vlc_object_release( p_playlist );
781 static int Control( demux_t *p_demux, int i_query, va_list args )