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