1
/*****************************************************************************
2
* m3u.c: a meta demux to parse pls, m3u, asx et b4s playlists
3
*****************************************************************************
4
* Copyright (C) 2001-2004 VideoLAN
5
* $Id: m3u.c 7442 2004-04-23 12:46:34Z gbazin $
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
49
int i_type; /* playlist type (m3u/asx) */
52
/*****************************************************************************
54
*****************************************************************************/
55
static int Activate ( vlc_object_t * );
56
static void Deactivate( vlc_object_t * );
57
static int Demux ( input_thread_t * );
59
/*****************************************************************************
61
*****************************************************************************/
63
set_description( _("Playlist metademux") );
64
set_capability( "demux", 180 );
65
set_callbacks( Activate, Deactivate );
66
add_shortcut( "m3u" );
67
add_shortcut( "asx" );
68
add_shortcut( "html" );
69
add_shortcut( "pls" );
70
add_shortcut( "b4s" );
73
/*****************************************************************************
74
* Activate: initializes m3u demux structures
75
*****************************************************************************/
76
static int Activate( vlc_object_t * p_this )
78
input_thread_t *p_input = (input_thread_t *)p_this;
80
int i_type = TYPE_UNKNOWN;
81
int i_type2 = TYPE_UNKNOWN;
83
/* Initialize access plug-in structures. */
84
if( p_input->i_mtu == 0 )
87
p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
90
p_input->pf_demux = Demux;
91
p_input->pf_rewind = NULL;
93
/* Check for m3u/asx file extension or if the demux has been forced */
94
psz_ext = strrchr ( p_input->psz_name, '.' );
96
if( ( psz_ext && !strcasecmp( psz_ext, ".m3u") ) ||
97
( p_input->psz_demux && !strcmp(p_input->psz_demux, "m3u") ) )
101
else if( ( psz_ext && !strcasecmp( psz_ext, ".asx") ) ||
102
( p_input->psz_demux && !strcmp(p_input->psz_demux, "asx") ) )
106
else if( ( psz_ext && !strcasecmp( psz_ext, ".html") ) ||
107
( p_input->psz_demux && !strcmp(p_input->psz_demux, "html") ) )
111
else if( ( psz_ext && !strcasecmp( psz_ext, ".pls") ) ||
112
( p_input->psz_demux && !strcmp(p_input->psz_demux, "pls") ) )
116
else if( ( psz_ext && !strcasecmp( psz_ext, ".b4s") ) ||
117
( p_input->psz_demux && !strcmp(p_input->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
/* XXX we double check for file != m3u as some asx ... are just m3u file */
127
if( i_type != TYPE_M3U )
130
int i_size = input_Peek( p_input, &p_peek, MAX_LINE );
131
i_size -= sizeof("[playlist]") - 1;
134
&& strncasecmp( p_peek, "[playlist]", sizeof("[playlist]") - 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, "<html>", sizeof("<html>") -1 ) )
154
else if ( !strncasecmp( p_peek, "<asx", sizeof("<asx") -1 ) )
159
else if ( !strncasecmp( p_peek, "<?xml", sizeof("<?xml") -1 ) )
166
if ( i_type == TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN)
170
if ( i_type != TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN )
180
p_input->p_demux_data = malloc( sizeof( demux_sys_t ) );
181
p_input->p_demux_data->i_type = i_type;
186
/*****************************************************************************
187
* Deactivate: frees unused data
188
*****************************************************************************/
189
static void Deactivate( vlc_object_t *p_this )
191
input_thread_t *p_input = (input_thread_t *)p_this;
193
free( p_input->p_demux_data );
196
/*****************************************************************************
197
* XMLSpecialChars: Handle the special chars in a XML file.
198
* ***************************************************************************/
199
static void XMLSpecialChars ( char *str )
208
if( !strncasecmp( src, "à", 6 ) ) *dst++ = 'ļæ½';
209
else if( !strncasecmp( src, "î", 6 ) ) *dst++ = 'ļæ½';
210
else if( !strncasecmp( src, "'", 6 ) ) *dst++ = '\'';
211
else if( !strncasecmp( src, "è", 6 ) ) *dst++ = 'ļæ½';
212
else if( !strncasecmp( src, "é", 6 ) ) *dst++ = 'ļæ½';
213
else if( !strncasecmp( src, "ê", 6 ) ) *dst++ = 'ļæ½';
229
/*****************************************************************************
230
* ParseLine: read a "line" from the file and add any entries found
231
* to the playlist. Returns:
232
* 0 if nothing was found
233
* 1 if a URI was found (it is then copied in psz_data)
234
* 2 if a name was found ( " )
236
* XXX psz_data has the same length that psz_line so no problem if you don't
238
* psz_line is \0 terminated
239
*****************************************************************************/
240
static int ParseLine( input_thread_t *p_input, char *psz_line, char *psz_data,
241
vlc_bool_t *pb_next )
243
demux_sys_t *p_m3u = p_input->p_demux_data;
244
char *psz_bol, *psz_name;
248
*pb_next = VLC_FALSE;
250
/* Remove unnecessary tabs or spaces at the beginning of line */
251
while( *psz_bol == ' ' || *psz_bol == '\t' ||
252
*psz_bol == '\n' || *psz_bol == '\r' )
257
if( p_m3u->i_type == TYPE_M3U )
259
/* Check for comment line */
260
if( *psz_bol == '#' )
263
strncasecmp( psz_bol, "EXTINF:",
264
sizeof("EXTINF:") - 1 ) &&
265
strncasecmp( psz_bol, "EXTVLCOPT:",
266
sizeof("EXTVLCOPT:") - 1 ) ) psz_bol++;
268
if( !*psz_bol ) return 0;
270
if( !strncasecmp( psz_bol, "EXTINF:", sizeof("EXTINF:") - 1 ) )
272
psz_bol = strchr( psz_bol, ',' );
273
if ( !psz_bol ) return 0;
276
/* From now, we have a name line */
277
strcpy( psz_data , psz_bol );
282
psz_bol = strchr( psz_bol, ':' );
283
if ( !psz_bol ) return 0;
286
strcpy( psz_data , psz_bol );
290
/* If we don't have a comment, the line is directly the URI */
292
else if ( p_m3u->i_type == TYPE_PLS )
294
/* We are dealing with .pls files from shoutcast
295
* We are looking for lines like "File1=http://..." */
296
if( !strncasecmp( psz_bol, "File", sizeof("File") - 1 ) )
298
psz_bol += sizeof("File") - 1;
299
psz_bol = strchr( psz_bol, '=' );
300
if ( !psz_bol ) return 0;
308
else if ( p_m3u->i_type == TYPE_ASX )
310
/* We are dealing with ASX files.
311
* We are looking for "<ref href=" xml markups that
312
* begins with "mms://", "http://" or "file://" */
316
strncasecmp( psz_bol, "ref", sizeof("ref") - 1 ) )
319
if( !*psz_bol ) return 0;
322
strncasecmp( psz_bol, "href", sizeof("href") - 1 ) )
325
if( !*psz_bol ) return 0;
328
strncasecmp( psz_bol, "mms://",
329
sizeof("mms://") - 1 ) &&
330
strncasecmp( psz_bol, "mmsu://",
331
sizeof("mmsu://") - 1 ) &&
332
strncasecmp( psz_bol, "mmst://",
333
sizeof("mmst://") - 1 ) &&
334
strncasecmp( psz_bol, "http://",
335
sizeof("http://") - 1 ) &&
336
strncasecmp( psz_bol, "file://",
337
sizeof("file://") - 1 ) )
340
if( !*psz_bol ) return 0;
342
psz_eol = strchr( psz_bol, '"');
348
else if ( p_m3u->i_type == TYPE_HTML )
350
/* We are dealing with a html file with embedded
351
* video. We are looking for "<param name="filename"
352
* value=" html markups that begin with "http://" */
356
strncasecmp( psz_bol, "param", sizeof("param") - 1 ) )
359
if( !*psz_bol ) return 0;
362
strncasecmp( psz_bol, "filename", sizeof("filename") - 1 ) )
365
if( !*psz_bol ) return 0;
368
strncasecmp( psz_bol, "http://",
369
sizeof("http://") - 1 ) )
372
if( !*psz_bol ) return 0;
374
psz_eol = strchr( psz_bol, '"');
381
else if ( p_m3u->i_type == TYPE_B4S )
386
msg_Dbg( p_input, "b4s line=%s", psz_line );
387
/* We are dealing with a B4S file from Winamp 3 */
389
/* First, search for name *
390
* <Name>Blabla</Name> */
392
if( strstr ( psz_bol, "<Name>" ) )
396
strncasecmp( psz_bol,"Name",sizeof("Name") -1 ) )
399
if( !*psz_bol ) return 0;
401
psz_bol = psz_bol + 5 ;
402
/* We are now at the beginning of the name */
404
if( !psz_bol ) return 0;
407
psz_eol = strchr(psz_bol, '<' );
408
if( !psz_eol) return 0;
412
XMLSpecialChars( psz_bol );
414
strcpy( psz_data, psz_bol );
417
else if( strstr( psz_bol, "</entry>" ) || strstr( psz_bol, "</Entry>" ))
423
/* We are looking for <entry Playstring="blabla"> */
427
strncasecmp( psz_bol,"Playstring",sizeof("Playstring") -1 ) )
430
if( !*psz_bol ) return 0;
432
psz_bol = strchr( psz_bol, '=' );
433
if ( !psz_bol ) return 0;
437
psz_eol= strchr(psz_bol, '"');
438
if( !psz_eol ) return 0;
442
/* Handle the XML special characters */
443
XMLSpecialChars( psz_bol );
447
msg_Warn( p_input, "unknown file type" );
452
if ( !*psz_bol ) return 0;
455
* From now on, we know we've got a meaningful line
458
/* check for a protocol name */
459
/* for URL, we should look for "://"
460
* for MRL (Media Resource Locator) ([[<access>][/<demux>]:][<source>]),
461
* we should look for ":"
462
* so we end up looking simply for ":"*/
463
/* PB: on some file systems, ':' are valid characters though*/
465
while( *psz_name && *psz_name!=':' )
470
if ( *psz_name && ( psz_name == psz_bol + 1 ) )
472
/* if it is not an URL,
473
* as it is unlikely to be an MRL (PB: if it is ?)
474
* it should be an absolute file name with the drive letter */
475
if ( *(psz_name+1) == '/' )/* "*:/" */
477
if ( *(psz_name+2) != '/' )/* not "*://" */
478
while ( *psz_name ) *psz_name++;/* so now (*psz_name==0) */
480
else while ( *psz_name ) *psz_name++;/* "*:*"*/
484
/* if the line doesn't specify a protocol name,
485
* check if the line has an absolute or relative path */
487
if( !*psz_name && *psz_bol != '/' )
488
/* If this line doesn't begin with a '/' */
493
&& *(psz_bol+1)!=':' )
494
/* if this line doesn't begin with
495
* "/" or "\" or "*:" or "*:\" or "*:/" or "\\" */
498
/* assume the path is relative to the path of the m3u file. */
499
char *psz_path = strdup( p_input->psz_name );
502
psz_name = strrchr( psz_path, '/' );
504
psz_name = strrchr( psz_path, '\\' );
505
if ( ! psz_name ) psz_name = strrchr( psz_path, '/' );
507
if( psz_name ) *psz_name = '\0';
508
else *psz_path = '\0';
510
psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
511
sprintf( psz_name, "%s/%s", psz_path, psz_bol );
513
if ( *psz_path != '\0' )
515
psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
516
sprintf( psz_name, "%s\\%s", psz_path, psz_bol );
518
else psz_name = strdup( psz_bol );
524
psz_name = strdup( psz_bol );
527
strcpy(psz_data, psz_name ) ;
531
if( p_m3u->i_type != TYPE_B4S )
539
static void ProcessLine ( input_thread_t *p_input, playlist_t *p_playlist,
541
char **ppsz_uri, char **ppsz_name,
542
int *pi_options, char ***pppsz_options,
545
char psz_data[MAX_LINE];
548
switch( ParseLine( p_input, psz_line, psz_data, &b_next ) )
555
*ppsz_uri = strdup( psz_data );
562
*ppsz_name = strdup( psz_data );
566
*pppsz_options = realloc( *pppsz_options,
567
sizeof(char *) * *pi_options );
568
(*pppsz_options)[*pi_options - 1] = strdup( psz_data );
575
if( b_next && *ppsz_uri )
577
playlist_AddExt( p_playlist, *ppsz_uri, *ppsz_name,
578
PLAYLIST_INSERT, *pi_position,
579
-1, (const char **)*pppsz_options, *pi_options );
582
if( *ppsz_name ) free( *ppsz_name ); *ppsz_name = NULL;
583
free( *ppsz_uri ); *ppsz_uri = NULL;
585
for( ; *pi_options; (*pi_options)-- )
587
free( (*pppsz_options)[*pi_options - 1] );
588
if( *pi_options == 1 ) free( *pppsz_options );
590
*pppsz_options = NULL;
594
/*****************************************************************************
595
* Demux: reads and demuxes data packets
596
*****************************************************************************
597
* Returns -1 in case of error, 0 in case of EOF, 1 otherwise
598
*****************************************************************************/
599
static int Demux ( input_thread_t *p_input )
601
demux_sys_t *p_m3u = p_input->p_demux_data;
603
data_packet_t *p_data;
604
char psz_line[MAX_LINE];
605
char *p_buf, eol_tok;
606
int i_size, i_bufpos, i_linepos = 0;
607
playlist_t *p_playlist;
608
vlc_bool_t b_discard = VLC_FALSE;
610
char *psz_name = NULL;
611
char *psz_uri = NULL;
613
char **ppsz_options = NULL;
617
p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
621
msg_Err( p_input, "can't find playlist" );
625
p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
626
i_position = p_playlist->i_index + 1;
628
/* Depending on wether we are dealing with an m3u/asf file, the end of
629
* line token will be different */
630
if( p_m3u->i_type == TYPE_ASX || p_m3u->i_type == TYPE_HTML )
635
while( ( i_size = input_SplitBuffer( p_input, &p_data, MAX_LINE ) ) > 0 )
637
i_bufpos = 0; p_buf = p_data->p_payload_start;
641
/* Build a line < MAX_LINE */
642
while( p_buf[i_bufpos] != eol_tok && i_size )
644
if( i_linepos == MAX_LINE || b_discard == VLC_TRUE )
646
/* line is bigger than MAX_LINE, discard it */
648
b_discard = VLC_TRUE;
652
if ( eol_tok != '\n' || p_buf[i_bufpos] != '\r' )
654
psz_line[i_linepos] = p_buf[i_bufpos];
659
i_size--; i_bufpos++;
662
/* Check if we need more data */
663
if( !i_size ) continue;
665
i_size--; i_bufpos++;
666
b_discard = VLC_FALSE;
668
/* Check for empty line */
669
if( !i_linepos ) continue;
671
psz_line[i_linepos] = '\0';
674
ProcessLine( p_input, p_playlist, psz_line, &psz_uri, &psz_name,
675
&i_options, &ppsz_options, &i_position );
678
input_DeletePacket( p_input->p_method_data, p_data );
681
if ( i_linepos && b_discard != VLC_TRUE && eol_tok == '\n' )
683
psz_line[i_linepos] = '\0';
685
ProcessLine( p_input, p_playlist, psz_line, &psz_uri, &psz_name,
686
&i_options, &ppsz_options, &i_position );
688
/* Is there a pendding uri without b_next */
691
playlist_AddExt( p_playlist, psz_uri, psz_name,
692
PLAYLIST_INSERT, i_position,
693
-1, (const char **)ppsz_options, i_options );
697
if( psz_uri ) free( psz_uri );
698
if( psz_name ) free( psz_name );
699
for( ; i_options; i_options-- )
701
free( ppsz_options[i_options - 1] );
702
if( i_options == 1 ) free( ppsz_options );
705
vlc_object_release( p_playlist );