1
/*****************************************************************************
2
* m3u.c: a meta demux to parse pls, m3u, asx et b4s playlists
3
*****************************************************************************
4
* Copyright (C) 2001-2006 the VideoLAN team
5
* $Id: 161136aacaa2919403ebccf6d912896e440d2721 $
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( "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 extension, 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 )
221
* - should probably accept any sequence, rather than only those
222
* commonly found in French.
223
* - output may have to be UTF-8 encoded (cannot assume Latin-1)
225
if( !strncasecmp( src, "à", 6 ) ) *dst++ = '\xe0';
226
else if( !strncasecmp( src, "î", 6 ) ) *dst++ = '\xee';
227
else if( !strncasecmp( src, "'", 6 ) ) *dst++ = '\'';
228
else if( !strncasecmp( src, "è", 6 ) ) *dst++ = '\xe8';
229
else if( !strncasecmp( src, "é", 6 ) ) *dst++ = '\xe9';
230
else if( !strncasecmp( src, "ê", 6 ) ) *dst++ = '\xea';
246
/*****************************************************************************
247
* ParseLine: read a "line" from the file and add any entries found
248
* to the playlist. Returns:
249
* 0 if nothing was found
250
* 1 if a URI was found (it is then copied in psz_data)
251
* 2 if a name was found ( " )
253
* XXX psz_data has the same length that psz_line so no problem if you don't
255
* psz_line is \0 terminated
256
*****************************************************************************/
257
static int ParseLine( demux_t *p_demux, char *psz_line, char *psz_data,
258
vlc_bool_t *pb_done )
260
demux_sys_t *p_m3u = p_demux->p_sys;
261
char *psz_bol, *psz_name;
264
*pb_done = VLC_FALSE;
266
/* Remove unnecessary tabs or spaces at the beginning of line */
267
while( *psz_bol == ' ' || *psz_bol == '\t' ||
268
*psz_bol == '\n' || *psz_bol == '\r' )
273
if( p_m3u->i_type == TYPE_M3U )
275
/* Check for comment line */
276
if( *psz_bol == '#' )
279
strncasecmp( psz_bol, "EXTINF:",
280
sizeof("EXTINF:") - 1 ) &&
281
strncasecmp( psz_bol, "EXTVLCOPT:",
282
sizeof("EXTVLCOPT:") - 1 ) ) psz_bol++;
284
if( !*psz_bol ) return 0;
286
if( !strncasecmp( psz_bol, "EXTINF:", sizeof("EXTINF:") - 1 ) )
288
psz_bol = strchr( psz_bol, ',' );
289
if ( !psz_bol ) return 0;
292
/* From now, we have a name line */
293
strcpy( psz_data , psz_bol );
298
psz_bol = strchr( psz_bol, ':' );
299
if ( !psz_bol ) return 0;
302
strcpy( psz_data , psz_bol );
306
/* If we don't have a comment, the line is directly the URI */
308
else if( p_m3u->i_type == TYPE_PLS )
310
/* We are dealing with .pls files from shoutcast
311
* We are looking for lines like "File1=http://..." */
312
if( !strncasecmp( psz_bol, "File", sizeof("File") - 1 ) )
314
psz_bol += sizeof("File") - 1;
315
psz_bol = strchr( psz_bol, '=' );
316
if ( !psz_bol ) return 0;
324
else if( p_m3u->i_type == TYPE_WMP )
326
/* We are dealing with some weird WMP stream playlist format
327
* Hurray for idiotic M$. Lines look like: "Ref1=http://..." */
328
if( !strncasecmp( psz_bol, "Ref", sizeof("Ref") - 1 ) )
330
psz_bol += sizeof("Ref") - 1;
331
psz_bol = strchr( psz_bol, '=' );
332
if ( !psz_bol ) return 0;
334
if( !strncasecmp( psz_bol, "http://", sizeof("http://") -1 ) )
337
psz_bol[0] = 'm'; psz_bol[1] = 'm'; psz_bol[2] = 's';
345
else if( p_m3u->i_type == TYPE_ASX )
347
/* We are dealing with ASX files.
348
* We are looking for "<ref href=" xml markups that
349
* begins with "mms://", "http://" or "file://" */
353
strncasecmp( psz_bol, "ref", sizeof("ref") - 1 ) )
356
if( !*psz_bol ) return 0;
359
strncasecmp( psz_bol, "href", sizeof("href") - 1 ) )
362
if( !*psz_bol ) return 0;
365
strncasecmp( psz_bol, "mms://",
366
sizeof("mms://") - 1 ) &&
367
strncasecmp( psz_bol, "mmsu://",
368
sizeof("mmsu://") - 1 ) &&
369
strncasecmp( psz_bol, "mmst://",
370
sizeof("mmst://") - 1 ) &&
371
strncasecmp( psz_bol, "http://",
372
sizeof("http://") - 1 ) &&
373
strncasecmp( psz_bol, "file://",
374
sizeof("file://") - 1 ) )
377
if( !*psz_bol ) return 0;
379
psz_eol = strchr( psz_bol, '"');
385
else if( p_m3u->i_type == TYPE_HTML )
387
/* We are dealing with a html file with embedded
388
* video. We are looking for "<param name="filename"
389
* value=" html markups that begin with "http://" */
393
strncasecmp( psz_bol, "param", sizeof("param") - 1 ) )
396
if( !*psz_bol ) return 0;
399
strncasecmp( psz_bol, "filename", sizeof("filename") - 1 ) )
402
if( !*psz_bol ) return 0;
405
strncasecmp( psz_bol, "http://",
406
sizeof("http://") - 1 ) )
409
if( !*psz_bol ) return 0;
411
psz_eol = strchr( psz_bol, '"');
418
else if( p_m3u->i_type == TYPE_B4S )
423
msg_Dbg( p_demux, "b4s line=%s", psz_line );
424
/* We are dealing with a B4S file from Winamp 3 */
426
/* First, search for name *
427
* <Name>Blabla</Name> */
429
if( strstr ( psz_bol, "<Name>" ) )
433
strncasecmp( psz_bol,"Name",sizeof("Name") -1 ) )
436
if( !*psz_bol ) return 0;
438
psz_bol = psz_bol + 5 ;
439
/* We are now at the beginning of the name */
441
if( !psz_bol ) return 0;
444
psz_eol = strchr(psz_bol, '<' );
445
if( !psz_eol) return 0;
449
XMLSpecialChars( psz_bol );
451
strcpy( psz_data, psz_bol );
454
else if( strstr( psz_bol, "</entry>" ) || strstr( psz_bol, "</Entry>" ))
460
/* We are looking for <entry Playstring="blabla"> */
463
strncasecmp( psz_bol,"Playstring",sizeof("Playstring") -1 ) )
466
if( !*psz_bol ) return 0;
468
psz_bol = strchr( psz_bol, '=' );
469
if ( !psz_bol ) return 0;
473
psz_eol= strchr(psz_bol, '"');
474
if( !psz_eol ) return 0;
478
/* Handle the XML special characters */
479
XMLSpecialChars( psz_bol );
481
else if( p_m3u->i_type == TYPE_RTSP )
483
/* We are dealing with rtsptext reference files
484
* Ignore anthying that doesn't start with rtsp://..." */
485
if( strncasecmp( psz_bol, "rtsp://", sizeof("rtsp://") - 1 ) )
486
/* ignore */ return 0;
490
msg_Warn( p_demux, "unknown file type" );
495
if ( !*psz_bol ) return 0;
498
* From now on, we know we've got a meaningful line
501
/* check for a protocol name */
502
/* for URL, we should look for "://"
503
* for MRL (Media Resource Locator) ([[<access>][/<demux>]:][<source>]),
504
* we should look for ":"
505
* so we end up looking simply for ":"*/
506
/* PB: on some file systems, ':' are valid characters though*/
508
while( *psz_name && *psz_name!=':' )
513
if ( *psz_name && ( psz_name == psz_bol + 1 ) )
515
/* if it is not an URL,
516
* as it is unlikely to be an MRL (PB: if it is ?)
517
* it should be an absolute file name with the drive letter */
518
if ( *(psz_name+1) == '/' )/* "*:/" */
520
if ( *(psz_name+2) != '/' )/* not "*://" */
521
while ( *psz_name ) *psz_name++;/* so now (*psz_name==0) */
523
else while ( *psz_name ) *psz_name++;/* "*:*"*/
527
/* if the line doesn't specify a protocol name,
528
* check if the line has an absolute or relative path */
530
if( !*psz_name && *psz_bol != '/' )
531
/* If this line doesn't begin with a '/' */
536
&& *(psz_bol+1)!=':' )
537
/* if this line doesn't begin with
538
* "/" or "\" or "*:" or "*:\" or "*:/" or "\\" */
541
/* assume the path is relative to the path of the m3u file. */
542
char *psz_path = strdup( p_demux->psz_path );
545
psz_name = strrchr( psz_path, '/' );
547
psz_name = strrchr( psz_path, '\\' );
548
if ( ! psz_name ) psz_name = strrchr( psz_path, '/' );
550
if( psz_name ) *psz_name = '\0';
551
else *psz_path = '\0';
553
psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
554
sprintf( psz_name, "%s/%s", psz_path, psz_bol );
556
if ( *psz_path != '\0' )
558
psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
559
sprintf( psz_name, "%s\\%s", psz_path, psz_bol );
561
else psz_name = strdup( psz_bol );
567
psz_name = strdup( psz_bol );
570
strcpy(psz_data, psz_name ) ;
574
if( p_m3u->i_type != TYPE_B4S )
582
static void ProcessLine ( demux_t *p_demux, playlist_t *p_playlist,
583
playlist_item_t *p_parent,
584
char *psz_line, char **ppsz_uri, char **ppsz_name,
585
int *pi_options, char ***pppsz_options,
588
char psz_data[MAX_LINE];
591
switch( ParseLine( p_demux, psz_line, psz_data, &b_done ) )
594
if( *ppsz_uri ) free( *ppsz_uri );
595
*ppsz_uri = strdup( psz_data );
598
if( *ppsz_name ) free( *ppsz_name );
599
*ppsz_name = strdup( psz_data );
603
*pppsz_options = realloc( *pppsz_options,
604
sizeof(char *) * *pi_options );
605
(*pppsz_options)[*pi_options - 1] = strdup( psz_data );
612
if( (b_done || b_flush) && *ppsz_uri )
614
playlist_item_t *p_item =
615
playlist_ItemNew( p_playlist, *ppsz_uri, *ppsz_name );
618
for( i = 0; i < *pi_options; i++ )
620
playlist_ItemAddOption( p_item, *pppsz_options[i] );
623
playlist_NodeAddItem( p_playlist, p_item,
624
p_parent->pp_parents[0]->i_view,
625
p_parent, PLAYLIST_APPEND, PLAYLIST_END );
627
/* We need to declare the parents of the node as the
628
* same of the parent's ones */
629
playlist_CopyParents( p_parent, p_item );
631
vlc_input_item_CopyOptions( &p_parent->input, &p_item->input );
633
if( *ppsz_name ) free( *ppsz_name ); *ppsz_name = NULL;
634
free( *ppsz_uri ); *ppsz_uri = NULL;
636
for( ; *pi_options; (*pi_options)-- )
638
free( (*pppsz_options)[*pi_options - 1] );
639
if( *pi_options == 1 ) free( *pppsz_options );
641
*pppsz_options = NULL;
645
static vlc_bool_t FindItem( demux_t *p_demux, playlist_t *p_playlist,
646
playlist_item_t **pp_item )
650
if( &p_playlist->status.p_item->input ==
651
((input_thread_t *)p_demux->p_parent)->input.p_item )
653
msg_Dbg( p_playlist, "starting playlist playback" );
654
*pp_item = p_playlist->status.p_item;
659
input_item_t *p_current =
660
((input_thread_t*)p_demux->p_parent)->input.p_item;
661
*pp_item = playlist_LockItemGetByInput( p_playlist, p_current );
664
msg_Dbg( p_playlist, "unable to find item in playlist");
672
/*****************************************************************************
673
* Demux: reads and demuxes data packets
674
*****************************************************************************
675
* Returns -1 in case of error, 0 in case of EOF, 1 otherwise
676
*****************************************************************************/
677
static int Demux( demux_t *p_demux )
679
demux_sys_t *p_m3u = p_demux->p_sys;
681
char psz_line[MAX_LINE];
682
char p_buf[MAX_LINE], eol_tok;
683
int i_size, i_bufpos, i_linepos = 0;
684
vlc_bool_t b_discard = VLC_FALSE;
686
char *psz_name = NULL;
687
char *psz_uri = NULL;
689
char **ppsz_options = NULL;
691
playlist_t *p_playlist;
692
playlist_item_t *p_parent;
695
p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
699
msg_Err( p_demux, "can't find playlist" );
703
b_play = FindItem( p_demux, p_playlist, &p_parent );
704
playlist_ItemToNode( p_playlist, p_parent );
705
p_parent->input.i_type = ITEM_TYPE_PLAYLIST;
707
/* Depending on wether we are dealing with an m3u/asf file, the end of
708
* line token will be different */
709
if( p_m3u->i_type == TYPE_ASX || p_m3u->i_type == TYPE_HTML )
714
while( ( i_size = stream_Read( p_demux->s, p_buf, MAX_LINE ) ) )
720
/* Build a line < MAX_LINE */
721
while( p_buf[i_bufpos] != eol_tok && i_size )
723
if( i_linepos == MAX_LINE || b_discard == VLC_TRUE )
725
/* line is bigger than MAX_LINE, discard it */
727
b_discard = VLC_TRUE;
731
if ( eol_tok != '\n' || p_buf[i_bufpos] != '\r' )
733
psz_line[i_linepos] = p_buf[i_bufpos];
738
i_size--; i_bufpos++;
741
/* Check if we need more data */
742
if( !i_size ) continue;
744
i_size--; i_bufpos++;
745
b_discard = VLC_FALSE;
747
/* Check for empty line */
748
if( !i_linepos ) continue;
750
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_FALSE );
759
if( i_linepos && b_discard != VLC_TRUE && eol_tok == '\n' )
761
psz_line[i_linepos] = '\0';
763
ProcessLine( p_demux, p_playlist, p_parent,
764
psz_line, &psz_uri, &psz_name,
765
&i_options, &ppsz_options, VLC_TRUE );
768
if( psz_uri ) free( psz_uri );
769
if( psz_name ) free( psz_name );
770
for( ; i_options; i_options-- )
772
free( ppsz_options[i_options - 1] );
773
if( i_options == 1 ) free( ppsz_options );
776
/* Go back and play the playlist */
779
playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
780
p_playlist->status.i_view,
781
p_playlist->status.p_item, NULL );
784
vlc_object_release( p_playlist );
789
static int Control( demux_t *p_demux, int i_query, va_list args )