]> git.sesse.net Git - vlc/blob - modules/demux/m3u.c
5a0da19a44f1620b2dc771650a086082396a489e
[vlc] / modules / demux / m3u.c
1 /*****************************************************************************
2  * m3u.c: a meta demux to parse pls, m3u, asx et b4s playlists
3  *****************************************************************************
4  * Copyright (C) 2001-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *          Clément Stenac <zorglub@via.ecp.fr>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                      /* malloc(), free() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33 #include <vlc_playlist.h>
34
35 /*****************************************************************************
36  * Constants and structures
37  *****************************************************************************/
38 #define MAX_LINE 8192
39
40 #define TYPE_UNKNOWN 0
41 #define TYPE_M3U 1
42 #define TYPE_ASX 2
43 #define TYPE_HTML 3
44 #define TYPE_PLS 4
45 #define TYPE_B4S 5
46 #define TYPE_WMP 6
47 #define TYPE_RTSP 7
48
49 struct demux_sys_t
50 {
51     int i_type;                                   /* playlist type (m3u/asx) */
52 };
53
54 /*****************************************************************************
55  * Local prototypes
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 );
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 vlc_module_begin();
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" );
76 vlc_module_end();
77
78 /*****************************************************************************
79  * Activate: initializes m3u demux structures
80  *****************************************************************************/
81 static int Activate( vlc_object_t * p_this )
82 {
83     demux_t *p_demux = (demux_t *)p_this;
84     char    *psz_ext;
85     int     i_type  = TYPE_UNKNOWN;
86     int     i_type2 = TYPE_UNKNOWN;
87
88     p_demux->pf_control = Control;
89     p_demux->pf_demux = Demux;
90
91     /* Check for m3u/asx file extension or if the demux has been forced */
92     psz_ext = strrchr ( p_demux->psz_path, '.' );
93
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") ) )
98     {
99         i_type = TYPE_M3U;
100     }
101     else if( ( psz_ext && !strcasecmp( psz_ext, ".asx") ) ||
102              ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "asx") ) )
103     {
104         i_type = TYPE_ASX;
105     }
106     else if( ( psz_ext && !strcasecmp( psz_ext, ".html") ) ||
107              ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "html") ) )
108     {
109         i_type = TYPE_HTML;
110     }
111     else if( ( psz_ext && !strcasecmp( psz_ext, ".pls") ) ||
112              ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "pls") ) )
113     {
114         i_type = TYPE_PLS;
115     }
116     else if( ( psz_ext && !strcasecmp( psz_ext, ".b4s") ) ||
117              ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "b4s") ) )
118     {
119         i_type = TYPE_B4S;
120     }
121
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 )
128     {
129         char *p_peek;
130         int i_size = stream_Peek( p_demux->s, (uint8_t *)&p_peek, MAX_LINE );
131         i_size -= sizeof("[Reference]") - 1;
132
133         if( i_size > 0 )
134         {
135             while( i_size &&
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 ) )
142             {
143                 p_peek++;
144                 i_size--;
145             }
146             if( !i_size )
147             {
148                 ;
149             }
150             else if( !strncasecmp( p_peek, "[playlist]", sizeof("[playlist]") -1 ) )
151             {
152                 i_type2 = TYPE_PLS;
153             }
154             else if( !strncasecmp( p_peek, "[Reference]", sizeof("[Reference]") -1 ) )
155             {
156                 i_type2 = TYPE_WMP;
157             }
158             else if( !strncasecmp( p_peek, "<html>", sizeof("<html>") -1 ) )
159             {
160                 i_type2 = TYPE_HTML;
161             }
162             else if( !strncasecmp( p_peek, "<asx", sizeof("<asx") -1 ) )
163             {
164                 i_type2 = TYPE_ASX;
165             }
166             else if( !strncasecmp( p_peek, "rtsptext", sizeof("rtsptext") -1 ) )
167             {
168                 i_type2 = TYPE_RTSP;
169             }
170 #if 0
171             else if( !strncasecmp( p_peek, "<?xml", sizeof("<?xml") -1 ) )
172             {
173                 i_type2 = TYPE_B4S;
174             }
175 #endif
176         }
177     }
178     if( i_type == TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN)
179     {
180         return VLC_EGENERIC;
181     }
182     if( i_type  != TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN )
183     {
184         i_type = TYPE_M3U;
185     }
186     else
187     {
188         i_type = i_type2;
189     }
190
191     /* Allocate p_m3u */
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 );
195
196     return VLC_SUCCESS;
197 }
198
199 /*****************************************************************************
200  * Deactivate: frees unused data
201  *****************************************************************************/
202 static void Deactivate( vlc_object_t *p_this )
203 {
204     demux_t *p_demux = (demux_t *)p_this;
205     free( p_demux->p_sys );
206 }
207
208 /*****************************************************************************
209  * XMLSpecialChars: Handle the special chars in a XML file.
210  * ***************************************************************************/
211 static void XMLSpecialChars ( char *str )
212 {
213     char *src = str;
214     char *dst = str;
215
216     while( *src )
217     {
218         if( *src == '&' )
219         {
220             if( !strncasecmp( src, "&#xe0;", 6 ) ) *dst++ = 'à';
221             else if( !strncasecmp( src, "&#xee;", 6 ) ) *dst++ = 'î';
222             else if( !strncasecmp( src, "&apos;", 6 ) ) *dst++ = '\'';
223             else if( !strncasecmp( src, "&#xe8;", 6 ) ) *dst++ = 'è';
224             else if( !strncasecmp( src, "&#xe9;", 6 ) ) *dst++ = 'é';
225             else if( !strncasecmp( src, "&#xea;", 6 ) ) *dst++ = 'ê';
226             else
227             {
228                 *dst++ = '?';
229             }
230             src += 6;
231         }
232         else
233         {
234             *dst++ = *src++;
235         }
236     }
237
238     *dst = '\0';
239 }
240
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 (  "  )
247  *
248  * XXX psz_data has the same length that psz_line so no problem if you don't
249  * expand it
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 )
254 {
255     demux_sys_t *p_m3u = p_demux->p_sys;
256     char        *psz_bol, *psz_name;
257
258     psz_bol = psz_line;
259     *pb_done = VLC_FALSE;
260
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' )
264     {
265         psz_bol++;
266     }
267
268     if( p_m3u->i_type == TYPE_M3U )
269     {
270         /* Check for comment line */
271         if( *psz_bol == '#' )
272         {
273             while( *psz_bol &&
274                    strncasecmp( psz_bol, "EXTINF:",
275                                 sizeof("EXTINF:") - 1 ) &&
276                    strncasecmp( psz_bol, "EXTVLCOPT:",
277                                 sizeof("EXTVLCOPT:") - 1 ) ) psz_bol++;
278
279             if( !*psz_bol ) return 0;
280
281             if( !strncasecmp( psz_bol, "EXTINF:", sizeof("EXTINF:") - 1 ) )
282             {
283                 psz_bol = strchr( psz_bol, ',' );
284                 if ( !psz_bol ) return 0;
285                 psz_bol++;
286
287                 /* From now, we have a name line */
288                 strcpy( psz_data , psz_bol );
289                 return 2;
290             }
291             else
292             {
293                 psz_bol = strchr( psz_bol, ':' );
294                 if ( !psz_bol ) return 0;
295                 psz_bol++;
296
297                 strcpy( psz_data , psz_bol );
298                 return 3;
299             }
300         }
301         /* If we don't have a comment, the line is directly the URI */
302     }
303     else if( p_m3u->i_type == TYPE_PLS )
304     {
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 ) )
308         {
309             psz_bol += sizeof("File") - 1;
310             psz_bol = strchr( psz_bol, '=' );
311             if ( !psz_bol ) return 0;
312             psz_bol++;
313         }
314         else
315         {
316             return 0;
317         }
318     }
319     else if( p_m3u->i_type == TYPE_WMP )
320     {
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 ) )
324         {
325             psz_bol += sizeof("Ref") - 1;
326             psz_bol = strchr( psz_bol, '=' );
327             if ( !psz_bol ) return 0;
328             psz_bol++;
329             if( !strncasecmp( psz_bol, "http://", sizeof("http://") -1 ) )
330             {
331                 psz_bol[0] = 'm'; psz_bol[1] = 'm'; psz_bol[2] = 's'; psz_bol[3] = 'h';
332             }
333         }
334         else
335         {
336             return 0;
337         }
338     }
339     else if( p_m3u->i_type == TYPE_ASX )
340     {
341         /* We are dealing with ASX files.
342          * We are looking for "<ref href=" xml markups that
343          * begins with "mms://", "http://" or "file://" */
344         char *psz_eol;
345
346         while( *psz_bol &&
347                strncasecmp( psz_bol, "ref", sizeof("ref") - 1 ) )
348             psz_bol++;
349
350         if( !*psz_bol ) return 0;
351
352         while( *psz_bol &&
353                strncasecmp( psz_bol, "href", sizeof("href") - 1 ) )
354             psz_bol++;
355
356         if( !*psz_bol ) return 0;
357
358         while( *psz_bol &&
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 ) )
369             psz_bol++;
370
371         if( !*psz_bol ) return 0;
372
373         psz_eol = strchr( psz_bol, '"');
374         if( !psz_eol )
375           return 0;
376
377         *psz_eol = '\0';
378     }
379     else if( p_m3u->i_type == TYPE_HTML )
380     {
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://" */
384         char *psz_eol;
385
386         while( *psz_bol &&
387                strncasecmp( psz_bol, "param", sizeof("param") - 1 ) )
388             psz_bol++;
389
390         if( !*psz_bol ) return 0;
391
392         while( *psz_bol &&
393                strncasecmp( psz_bol, "filename", sizeof("filename") - 1 ) )
394             psz_bol++;
395
396         if( !*psz_bol ) return 0;
397
398         while( *psz_bol &&
399                strncasecmp( psz_bol, "http://",
400                             sizeof("http://") - 1 ) )
401             psz_bol++;
402
403         if( !*psz_bol ) return 0;
404
405         psz_eol = strchr( psz_bol, '"');
406         if( !psz_eol )
407           return 0;
408
409         *psz_eol = '\0';
410
411     }
412     else if( p_m3u->i_type == TYPE_B4S )
413     {
414
415         char *psz_eol;
416
417         msg_Dbg( p_demux, "b4s line=%s", psz_line );
418         /* We are dealing with a B4S file from Winamp 3 */
419
420         /* First, search for name *
421          * <Name>Blabla</Name> */
422
423         if( strstr ( psz_bol, "<Name>" ) )
424         {
425             /* We have a name */
426             while ( *psz_bol &&
427                     strncasecmp( psz_bol,"Name",sizeof("Name") -1 ) )
428                 psz_bol++;
429
430             if( !*psz_bol ) return 0;
431
432             psz_bol = psz_bol + 5 ;
433             /* We are now at the beginning of the name */
434
435             if( !psz_bol ) return 0;
436
437
438             psz_eol = strchr(psz_bol, '<' );
439             if( !psz_eol) return 0;
440
441             *psz_eol='\0';
442
443             XMLSpecialChars( psz_bol );
444
445             strcpy( psz_data, psz_bol );
446             return 2;
447         }
448         else if( strstr( psz_bol, "</entry>" ) || strstr( psz_bol, "</Entry>" ))
449         {
450             *pb_done = VLC_TRUE;
451             return 0;
452         }
453
454         /* We are looking for <entry Playstring="blabla"> */
455
456         while( *psz_bol &&
457                strncasecmp( psz_bol,"Playstring",sizeof("Playstring") -1 ) )
458             psz_bol++;
459
460         if( !*psz_bol ) return 0;
461
462         psz_bol = strchr( psz_bol, '=' );
463         if ( !psz_bol ) return 0;
464
465         psz_bol += 2;
466
467         psz_eol= strchr(psz_bol, '"');
468         if( !psz_eol ) return 0;
469
470         *psz_eol= '\0';
471
472         /* Handle the XML special characters */
473         XMLSpecialChars( psz_bol );
474     }
475     else if( p_m3u->i_type == TYPE_RTSP )
476     {
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;
481     }
482     else
483     {
484         msg_Warn( p_demux, "unknown file type" );
485         return 0;
486     }
487
488     /* empty line */
489     if ( !*psz_bol ) return 0;
490
491     /*
492      * From now on, we know we've got a meaningful line
493      */
494
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*/
501     psz_name = psz_bol;
502     while( *psz_name && *psz_name!=':' )
503     {
504         psz_name++;
505     }
506 #ifdef WIN32
507     if ( *psz_name && ( psz_name == psz_bol + 1 ) )
508     {
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) == '/' )/* "*:/" */
513         {
514             if ( *(psz_name+2) != '/' )/* not "*://" */
515                 while ( *psz_name ) *psz_name++;/* so now (*psz_name==0) */
516         }
517         else while ( *psz_name ) *psz_name++;/* "*:*"*/
518     }
519 #endif
520
521     /* if the line doesn't specify a protocol name,
522      * check if the line has an absolute or relative path */
523 #ifndef WIN32
524     if( !*psz_name && *psz_bol != '/' )
525          /* If this line doesn't begin with a '/' */
526 #else
527     if( !*psz_name
528             && *psz_bol!='/'
529             && *psz_bol!='\\'
530             && *(psz_bol+1)!=':' )
531          /* if this line doesn't begin with
532           *  "/" or "\" or "*:" or "*:\" or "*:/" or "\\" */
533 #endif
534     {
535         /* assume the path is relative to the path of the m3u file. */
536         char *psz_path = strdup( p_demux->psz_path );
537
538 #ifndef WIN32
539         psz_name = strrchr( psz_path, '/' );
540 #else
541         psz_name = strrchr( psz_path, '\\' );
542         if ( ! psz_name ) psz_name = strrchr( psz_path, '/' );
543 #endif
544         if( psz_name ) *psz_name = '\0';
545         else *psz_path = '\0';
546 #ifndef WIN32
547         psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
548         sprintf( psz_name, "%s/%s", psz_path, psz_bol );
549 #else
550         if ( *psz_path != '\0' )
551         {
552             psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
553             sprintf( psz_name, "%s\\%s", psz_path, psz_bol );
554         }
555         else psz_name = strdup( psz_bol );
556 #endif
557         free( psz_path );
558     }
559     else
560     {
561         psz_name = strdup( psz_bol );
562     }
563
564     strcpy(psz_data, psz_name ) ;
565
566     free( psz_name );
567
568     if( p_m3u->i_type != TYPE_B4S )
569     {
570        *pb_done = VLC_TRUE;
571     }
572
573     return 1;
574 }
575
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,
580                           vlc_bool_t b_flush )
581 {
582     char psz_data[MAX_LINE];
583     vlc_bool_t b_done;
584
585     switch( ParseLine( p_demux, psz_line, psz_data, &b_done ) )
586     {
587         case 1:
588             if( *ppsz_uri ) free( *ppsz_uri );
589             *ppsz_uri = strdup( psz_data );
590             break;
591         case 2:
592             if( *ppsz_name ) free( *ppsz_name );
593             *ppsz_name = strdup( psz_data );
594             break;
595         case 3:
596             (*pi_options)++;
597             *pppsz_options = realloc( *pppsz_options,
598                                       sizeof(char *) * *pi_options );
599             (*pppsz_options)[*pi_options - 1] = strdup( psz_data );
600             break;
601         case 0:
602         default:
603             break;
604     }
605
606     if( (b_done || b_flush) && *ppsz_uri )
607     {
608         playlist_item_t *p_item =
609             playlist_ItemNew( p_playlist, *ppsz_uri, *ppsz_name );
610         int i;
611
612         for( i = 0; i < *pi_options; i++ )
613         {
614             playlist_ItemAddOption( p_item, *pppsz_options[i] );
615         }
616
617         playlist_NodeAddItem( p_playlist, p_item,
618                               p_parent->pp_parents[0]->i_view,
619                               p_parent, PLAYLIST_APPEND, PLAYLIST_END );
620
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 );
624
625         vlc_input_item_CopyOptions( &p_parent->input, &p_item->input );
626
627         if( *ppsz_name ) free( *ppsz_name ); *ppsz_name = NULL;
628         free( *ppsz_uri ); *ppsz_uri  = NULL;
629
630         for( ; *pi_options; (*pi_options)-- )
631         {
632             free( (*pppsz_options)[*pi_options - 1] );
633             if( *pi_options == 1 ) free( *pppsz_options );
634         }
635         *pppsz_options = NULL;
636     }
637 }
638
639 static vlc_bool_t FindItem( demux_t *p_demux, playlist_t *p_playlist,
640                             playlist_item_t **pp_item )
641 {
642      vlc_bool_t b_play;
643
644      if( &p_playlist->status.p_item->input ==
645          ((input_thread_t *)p_demux->p_parent)->input.p_item )
646      {
647          msg_Dbg( p_playlist, "starting playlist playback" );
648          *pp_item = p_playlist->status.p_item;
649          b_play = VLC_TRUE;
650      }
651      else
652      {
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 );
656
657          if( !*pp_item )
658              msg_Dbg( p_playlist, "unable to find item in playlist");
659
660          b_play = VLC_FALSE;
661      }
662
663      return b_play;
664 }
665
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 )
672 {
673     demux_sys_t   *p_m3u = p_demux->p_sys;
674
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;
679
680     char          *psz_name = NULL;
681     char          *psz_uri  = NULL;
682     int           i_options = 0;
683     char          **ppsz_options = NULL;
684
685     playlist_t      *p_playlist;
686     playlist_item_t *p_parent;
687     vlc_bool_t      b_play;
688
689     p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
690                                                  FIND_ANYWHERE );
691     if( !p_playlist )
692     {
693         msg_Err( p_demux, "can't find playlist" );
694         return -1;
695     }
696
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;
700
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 )
704         eol_tok = '>';
705     else
706         eol_tok = '\n';
707
708     while( ( i_size = stream_Read( p_demux->s, p_buf, MAX_LINE ) ) )
709     {
710         i_bufpos = 0;
711
712         while( i_size )
713         {
714             /* Build a line < MAX_LINE */
715             while( p_buf[i_bufpos] != eol_tok && i_size )
716             {
717                 if( i_linepos == MAX_LINE || b_discard == VLC_TRUE )
718                 {
719                     /* line is bigger than MAX_LINE, discard it */
720                     i_linepos = 0;
721                     b_discard = VLC_TRUE;
722                 }
723                 else
724                 {
725                     if ( eol_tok != '\n' || p_buf[i_bufpos] != '\r' )
726                     {
727                         psz_line[i_linepos] = p_buf[i_bufpos];
728                         i_linepos++;
729                     }
730                 }
731
732                 i_size--; i_bufpos++;
733             }
734
735             /* Check if we need more data */
736             if( !i_size ) continue;
737
738             i_size--; i_bufpos++;
739             b_discard = VLC_FALSE;
740
741             /* Check for empty line */
742             if( !i_linepos ) continue;
743
744             psz_line[i_linepos] = '\0';
745             i_linepos = 0;
746
747             ProcessLine( p_demux, p_playlist, p_parent,
748                          psz_line, &psz_uri, &psz_name,
749                          &i_options, &ppsz_options, VLC_FALSE );
750         }
751     }
752
753     if( i_linepos && b_discard != VLC_TRUE && eol_tok == '\n' )
754     {
755         psz_line[i_linepos] = '\0';
756
757         ProcessLine( p_demux, p_playlist, p_parent,
758                      psz_line, &psz_uri, &psz_name,
759                      &i_options, &ppsz_options, VLC_TRUE );
760     }
761
762     if( psz_uri ) free( psz_uri );
763     if( psz_name ) free( psz_name );
764     for( ; i_options; i_options-- )
765     {
766         free( ppsz_options[i_options - 1] );
767         if( i_options == 1 ) free( ppsz_options );
768     }
769
770     /* Go back and play the playlist */
771     if( b_play )
772     {
773         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
774                           p_playlist->status.i_view,
775                           p_playlist->status.p_item, NULL );
776     }
777
778     vlc_object_release( p_playlist );
779
780     return 0;
781 }
782
783 static int Control( demux_t *p_demux, int i_query, va_list args )
784 {
785     return VLC_EGENERIC;
786 }