]> git.sesse.net Git - vlc/blob - modules/demux/m3u.c
3668de9207a2ab65c80c8705e91be91052c20730
[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 VideoLAN
5  * $Id: m3u.c,v 1.25 2004/01/05 13:07:02 zorglub Exp $
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
8  *          Gildas Bazin <gbazin@netcourrier.com>
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 1024
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
47 struct demux_sys_t
48 {
49     int i_type;                                   /* playlist type (m3u/asx) */
50 };
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static int  Activate  ( vlc_object_t * );
56 static void Deactivate( vlc_object_t * );
57 static int  Demux ( input_thread_t * );
58
59 /*****************************************************************************
60  * Module descriptor
61  *****************************************************************************/
62 vlc_module_begin();
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" );
71 vlc_module_end();
72
73 /*****************************************************************************
74  * Activate: initializes m3u demux structures
75  *****************************************************************************/
76 static int Activate( vlc_object_t * p_this )
77 {
78     input_thread_t *p_input = (input_thread_t *)p_this;
79     char           *psz_ext;
80     int             i_type  = TYPE_UNKNOWN;
81     int             i_type2 = TYPE_UNKNOWN;
82
83     /* Initialize access plug-in structures. */
84     if( p_input->i_mtu == 0 )
85     {
86         /* Improve speed. */
87         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
88     }
89
90     p_input->pf_demux = Demux;
91     p_input->pf_rewind = NULL;
92
93     /* Check for m3u/asx file extension or if the demux has been forced */
94     psz_ext = strrchr ( p_input->psz_name, '.' );
95
96     if( ( psz_ext && !strcasecmp( psz_ext, ".m3u") ) ||
97         ( p_input->psz_demux && !strcmp(p_input->psz_demux, "m3u") ) )
98     {
99         i_type = TYPE_M3U;
100     }
101     else if( ( psz_ext && !strcasecmp( psz_ext, ".asx") ) ||
102              ( p_input->psz_demux && !strcmp(p_input->psz_demux, "asx") ) )
103     {
104         i_type = TYPE_ASX;
105     }
106     else if( ( psz_ext && !strcasecmp( psz_ext, ".html") ) ||
107              ( p_input->psz_demux && !strcmp(p_input->psz_demux, "html") ) )
108     {
109         i_type = TYPE_HTML;
110     }
111     else if( ( psz_ext && !strcasecmp( psz_ext, ".pls") ) ||
112              ( p_input->psz_demux && !strcmp(p_input->psz_demux, "pls") ) )
113     {
114         i_type = TYPE_PLS;
115     }
116     else if( ( psz_ext && !strcasecmp( psz_ext, ".b4s") ) ||
117              ( p_input->psz_demux && !strcmp(p_input->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 som asx files that have
125      * another extention */
126     /* XXX we double check for file != m3u as some asx ... are just m3u file */
127     if( i_type != TYPE_M3U )
128     {
129         byte_t *p_peek;
130         int i_size = input_Peek( p_input, &p_peek, MAX_LINE );
131         i_size -= sizeof("[playlist]") - 1;
132         if ( i_size > 0 ) {
133             while ( i_size
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 ) )
138             {
139                 p_peek++;
140                 i_size--;
141             }
142             if ( !i_size )
143             {
144                 ;
145             }
146             else if ( !strncasecmp( p_peek, "[playlist]", sizeof("[playlist]") -1 ) )
147             {
148                 i_type2 = TYPE_PLS;
149             }
150             else if ( !strncasecmp( p_peek, "<html>", sizeof("<html>") -1 ) )
151             {
152                 i_type2 = TYPE_HTML;
153             }
154             else if ( !strncasecmp( p_peek, "<asx", sizeof("<asx") -1 ) )
155             {
156                 i_type2 = TYPE_ASX;
157             }
158             else if ( !strncasecmp( p_peek, "<?xml", sizeof("<?xml") -1 ) )
159             {
160                 i_type2 = TYPE_B4S;
161             }
162         }
163     }
164     if ( i_type == TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN)
165     {
166         return VLC_EGENERIC;
167     }
168     if ( i_type  != TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN )
169     {
170         i_type = TYPE_M3U;
171     }
172     else
173     {
174         i_type = i_type2;
175     }
176
177     /* Allocate p_m3u */
178     p_input->p_demux_data = malloc( sizeof( demux_sys_t ) );
179     p_input->p_demux_data->i_type = i_type;
180
181     return VLC_SUCCESS;
182 }
183
184 /*****************************************************************************
185  * Deactivate: frees unused data
186  *****************************************************************************/
187 static void Deactivate( vlc_object_t *p_this )
188 {
189     input_thread_t *p_input = (input_thread_t *)p_this;
190
191     free( p_input->p_demux_data );
192 }
193
194 /*****************************************************************************
195  * XMLSpecialChars: Handle the special chars in a XML file.
196  * ***************************************************************************/
197 static void XMLSpecialChars ( char *str )
198 {
199     char *src = str;
200     char *dst = str;
201
202     while( *src )
203     {
204         if( *src == '&' )
205         {
206             if( !strncasecmp( src, "&#xe0;", 6 ) ) *dst++ = 'à';
207             else if( !strncasecmp( src, "&#xee;", 6 ) ) *dst++ = 'î';
208             else if( !strncasecmp( src, "&apos;", 6 ) ) *dst++ = '\'';
209             else if( !strncasecmp( src, "&#xe8;", 6 ) ) *dst++ = 'è';
210             else if( !strncasecmp( src, "&#xe9;", 6 ) ) *dst++ = 'é';
211             else if( !strncasecmp( src, "&#xea;", 6 ) ) *dst++ = 'ê';
212             else
213             {
214                 *dst++ = '?';
215             }
216             src += 6;
217         }
218         else
219         {
220             *dst++ = *src++;
221         }
222     }
223
224     *dst = '\0';
225 }
226
227
228 /*****************************************************************************
229  * ParseLine: read a "line" from the file and add any entries found
230  * to the playlist. Returns:
231  * 0 if nothing was found
232  * 1 if a URI was found (it is then copied in psz_data)
233  * 2 if a name was found (  "  )
234  *
235  * XXX psz_data has the same length that psz_line so no problem if you don't
236  * expand it
237  *    psz_line is \0 terminated
238  ******************************************************************************/
239 static int ParseLine ( input_thread_t *p_input, char *psz_line, char *psz_data, vlc_bool_t *pb_next )
240 {
241     demux_sys_t   *p_m3u = p_input->p_demux_data;
242
243     char          *psz_bol, *psz_name;
244
245     psz_bol = psz_line;
246
247     *pb_next = VLC_FALSE;
248
249     /* Remove unnecessary tabs or spaces at the beginning of line */
250     while( *psz_bol == ' ' || *psz_bol == '\t' ||
251            *psz_bol == '\n' || *psz_bol == '\r' )
252     {
253         psz_bol++;
254     }
255
256     if( p_m3u->i_type == TYPE_M3U )
257     {
258         /* Check for comment line */
259         if( *psz_bol == '#' )
260         {
261             while( *psz_bol &&
262                    strncasecmp( psz_bol, "EXTINF:", sizeof("EXTINF:") - 1 ) )
263                psz_bol++;
264             if( !*psz_bol ) return 0;
265
266             psz_bol = strchr( psz_bol, ',' );
267             if ( !psz_bol ) return 0;
268             psz_bol++;
269             /* From now, we have a name line */
270
271             strcpy( psz_data , psz_bol );
272             return 2;
273         }
274         /* If we don't have a comment, the line is directly the URI */
275     }
276     else if ( p_m3u->i_type == TYPE_PLS )
277     {
278         /* We are dealing with .pls files from shoutcast
279          * We are looking for lines like "File1=http://..." */
280         if( !strncasecmp( psz_bol, "File", sizeof("File") - 1 ) )
281         {
282             psz_bol += sizeof("File") - 1;
283             psz_bol = strchr( psz_bol, '=' );
284             if ( !psz_bol ) return 0;
285             psz_bol++;
286         }
287         else
288         {
289             return 0;
290         }
291     }
292     else if ( p_m3u->i_type == TYPE_ASX )
293     {
294         /* We are dealing with ASX files.
295          * We are looking for "<ref href=" xml markups that
296          * begins with "mms://", "http://" or "file://" */
297         char *psz_eol;
298
299         while( *psz_bol &&
300                strncasecmp( psz_bol, "ref", sizeof("ref") - 1 ) )
301             psz_bol++;
302
303         if( !*psz_bol ) return 0;
304
305         while( *psz_bol &&
306                strncasecmp( psz_bol, "href", sizeof("href") - 1 ) )
307             psz_bol++;
308
309         if( !*psz_bol ) return 0;
310
311         while( *psz_bol &&
312                strncasecmp( psz_bol, "mms://",
313                             sizeof("mms://") - 1 ) &&
314                strncasecmp( psz_bol, "mmsu://",
315                             sizeof("mmsu://") - 1 ) &&
316                strncasecmp( psz_bol, "mmst://",
317                             sizeof("mmst://") - 1 ) &&
318                strncasecmp( psz_bol, "http://",
319                             sizeof("http://") - 1 ) &&
320                strncasecmp( psz_bol, "file://",
321                             sizeof("file://") - 1 ) )
322             psz_bol++;
323
324         if( !*psz_bol ) return 0;
325
326         psz_eol = strchr( psz_bol, '"');
327         if( !psz_eol )
328           return 0;
329
330         *psz_eol = '\0';
331     }
332     else if ( p_m3u->i_type == TYPE_HTML )
333     {
334         /* We are dealing with a html file with embedded
335          * video.  We are looking for "<param name="filename"
336          * value=" html markups that begin with "http://" */
337         char *psz_eol;
338
339         while( *psz_bol &&
340                strncasecmp( psz_bol, "param", sizeof("param") - 1 ) )
341             psz_bol++;
342
343         if( !*psz_bol ) return 0;
344
345         while( *psz_bol &&
346                strncasecmp( psz_bol, "filename", sizeof("filename") - 1 ) )
347             psz_bol++;
348
349         if( !*psz_bol ) return 0;
350
351         while( *psz_bol &&
352                strncasecmp( psz_bol, "http://",
353                             sizeof("http://") - 1 ) )
354             psz_bol++;
355
356         if( !*psz_bol ) return 0;
357
358         psz_eol = strchr( psz_bol, '"');
359         if( !psz_eol )
360           return 0;
361
362         *psz_eol = '\0';
363
364     }
365     else if ( p_m3u->i_type == TYPE_B4S )
366     {
367
368         char *psz_eol;
369
370         msg_Dbg( p_input, "b4s line=%s", psz_line );
371         /* We are dealing with a B4S file from Winamp 3 */
372
373         /* First, search for name *
374          * <Name>Blabla</Name> */
375
376         if( strstr ( psz_bol, "<Name>" ) )
377         {
378             /* We have a name */
379             while ( *psz_bol &&
380                     strncasecmp( psz_bol,"Name",sizeof("Name") -1 ) )
381                 psz_bol++;
382
383             if( !*psz_bol ) return 0;
384
385             psz_bol = psz_bol + 5 ;
386             /* We are now at the beginning of the name */
387
388             if( !psz_bol ) return 0;
389
390
391             psz_eol = strchr(psz_bol, '<' );
392             if( !psz_eol) return 0;
393
394             *psz_eol='\0';
395
396             XMLSpecialChars( psz_bol );
397
398             strcpy( psz_data, psz_bol );
399             return 2;
400         }
401         else if( strstr( psz_bol, "</entry>" ) || strstr( psz_bol, "</Entry>" ))
402         {
403             *pb_next = VLC_TRUE;
404             return 0;
405         }
406
407          /* We are looking for <entry Playstring="blabla"> */
408
409
410         while ( *psz_bol &&
411                 strncasecmp( psz_bol,"Playstring",sizeof("Playstring") -1 ) )
412             psz_bol++;
413
414         if( !*psz_bol ) return 0;
415
416         psz_bol = strchr( psz_bol, '=' );
417         if ( !psz_bol ) return 0;
418
419         psz_bol += 2;
420
421         psz_eol= strchr(psz_bol, '"');
422         if( !psz_eol ) return 0;
423
424         *psz_eol= '\0';
425
426         /* Handle the XML special characters */
427         XMLSpecialChars( psz_bol );
428     }
429     else
430     {
431         msg_Warn( p_input, "unknown file type" );
432         return 0;
433     }
434
435     /* empty line */
436     if ( !*psz_bol ) return 0;
437
438     /*
439      * From now on, we know we've got a meaningful line
440      */
441
442     /* check for a protocol name */
443     /* for URL, we should look for "://"
444      * for MRL (Media Resource Locator) ([[<access>][/<demux>]:][<source>]),
445      * we should look for ":"
446      * so we end up looking simply for ":"*/
447     /* PB: on some file systems, ':' are valid characters though*/
448     psz_name = psz_bol;
449     while( *psz_name && *psz_name!=':' )
450     {
451         psz_name++;
452     }
453 #ifdef WIN32
454     if ( *psz_name && ( psz_name == psz_bol + 1 ) )
455     {
456         /* if it is not an URL,
457          * as it is unlikely to be an MRL (PB: if it is ?)
458          * it should be an absolute file name with the drive letter */
459         if ( *(psz_name+1) == '/' )/* "*:/" */
460         {
461             if ( *(psz_name+2) != '/' )/* not "*://" */
462                 while ( *psz_name ) *psz_name++;/* so now (*psz_name==0) */
463         }
464         else while ( *psz_name ) *psz_name++;/* "*:*"*/
465     }
466 #endif
467
468     /* if the line doesn't specify a protocol name,
469      * check if the line has an absolute or relative path */
470 #ifndef WIN32
471     if( !*psz_name && *psz_bol != '/' )
472          /* If this line doesn't begin with a '/' */
473 #else
474     if( !*psz_name
475             && *psz_bol!='/'
476             && *psz_bol!='\\'
477             && *(psz_bol+1)!=':' )
478          /* if this line doesn't begin with
479           *  "/" or "\" or "*:" or "*:\" or "*:/" or "\\" */
480 #endif
481     {
482         /* assume the path is relative to the path of the m3u file. */
483         char *psz_path = strdup( p_input->psz_name );
484
485 #ifndef WIN32
486         psz_name = strrchr( psz_path, '/' );
487 #else
488         psz_name = strrchr( psz_path, '\\' );
489         if ( ! psz_name ) psz_name = strrchr( psz_path, '/' );
490 #endif
491         if( psz_name ) *psz_name = '\0';
492         else *psz_path = '\0';
493 #ifndef WIN32
494         psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
495         sprintf( psz_name, "%s/%s", psz_path, psz_bol );
496 #else
497         if ( *psz_path != '\0' )
498         {
499             psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
500             sprintf( psz_name, "%s\\%s", psz_path, psz_bol );
501         }
502         else psz_name = strdup( psz_bol );
503 #endif
504         free( psz_path );
505     }
506     else
507     {
508         psz_name = strdup( psz_bol );
509     }
510
511     strcpy(psz_data, psz_name ) ;
512
513     free( psz_name );
514
515     if( p_m3u->i_type != TYPE_B4S )
516     {
517        *pb_next = VLC_TRUE;
518     }
519
520     return 1;
521 }
522
523 static void ProcessLine ( input_thread_t *p_input, playlist_t *p_playlist,
524                           char *psz_line,
525                           char **ppsz_uri, char **ppsz_name,
526                           int *pi_position )
527 {
528     char          psz_data[MAX_LINE];
529     vlc_bool_t    b_next;
530
531     switch( ParseLine( p_input, psz_line, psz_data, &b_next ) )
532     {
533         case 1:
534             if( *ppsz_uri )
535             {
536                 free( *ppsz_uri );
537             }
538             *ppsz_uri = strdup( psz_data );
539             break;
540         case 2:
541             if( *ppsz_name )
542             {
543                 free( *ppsz_name );
544             }
545             *ppsz_name = strdup( psz_data );
546             break;
547         case 0:
548         default:
549             break;
550     }
551
552     if( b_next && *ppsz_uri )
553     {
554         playlist_Add( p_playlist, *ppsz_uri,
555                          *ppsz_name ? *ppsz_name : *ppsz_uri,
556                           PLAYLIST_INSERT, *pi_position );
557         (*pi_position)++;
558         if( *ppsz_name )
559         {
560             free( *ppsz_name );
561         }
562         free( *ppsz_uri );
563         *ppsz_name = NULL;
564         *ppsz_uri  = NULL;
565     }
566 }
567
568 /*****************************************************************************
569  * Demux: reads and demuxes data packets
570  *****************************************************************************
571  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
572  *****************************************************************************/
573 static int Demux ( input_thread_t *p_input )
574 {
575     demux_sys_t   *p_m3u = p_input->p_demux_data;
576
577     data_packet_t *p_data;
578     char          psz_line[MAX_LINE];
579     char          *p_buf, eol_tok;
580     int           i_size, i_bufpos, i_linepos = 0;
581     playlist_t    *p_playlist;
582     vlc_bool_t    b_discard = VLC_FALSE;
583
584
585     char          *psz_name = NULL;
586     char          *psz_uri  = NULL;
587
588     int           i_position;
589
590     p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
591                                                  FIND_ANYWHERE );
592     if( !p_playlist )
593     {
594         msg_Err( p_input, "can't find playlist" );
595         return -1;
596     }
597
598     p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
599     i_position = p_playlist->i_index + 1;
600
601     /* Depending on wether we are dealing with an m3u/asf file, the end of
602      * line token will be different */
603     if( p_m3u->i_type == TYPE_ASX || p_m3u->i_type == TYPE_HTML )
604         eol_tok = '>';
605     else
606         eol_tok = '\n';
607
608     while( ( i_size = input_SplitBuffer( p_input, &p_data, MAX_LINE ) ) > 0 )
609     {
610         i_bufpos = 0; p_buf = p_data->p_payload_start;
611
612         while( i_size )
613         {
614             /* Build a line < MAX_LINE */
615             while( p_buf[i_bufpos] != eol_tok && i_size )
616             {
617                 if( i_linepos == MAX_LINE || b_discard == VLC_TRUE )
618                 {
619                     /* line is bigger than MAX_LINE, discard it */
620                     i_linepos = 0;
621                     b_discard = VLC_TRUE;
622                 }
623                 else
624                 {
625                     if ( eol_tok != '\n' || p_buf[i_bufpos] != '\r' )
626                     {
627                         psz_line[i_linepos] = p_buf[i_bufpos];
628                         i_linepos++;
629                     }
630                 }
631
632                 i_size--; i_bufpos++;
633             }
634
635             /* Check if we need more data */
636             if( !i_size ) continue;
637
638             i_size--; i_bufpos++;
639             b_discard = VLC_FALSE;
640
641             /* Check for empty line */
642             if( !i_linepos ) continue;
643
644             psz_line[i_linepos] = '\0';
645             i_linepos = 0;
646
647             ProcessLine( p_input, p_playlist, psz_line, &psz_uri, &psz_name,
648                          &i_position );
649         }
650
651         input_DeletePacket( p_input->p_method_data, p_data );
652     }
653
654     if ( i_linepos && b_discard != VLC_TRUE && eol_tok == '\n' )
655     {
656         psz_line[i_linepos] = '\0';
657
658         ProcessLine( p_input, p_playlist, psz_line, &psz_uri, &psz_name,
659                      &i_position );
660         /* is there a pendding uri without b_next */
661         if( psz_uri )
662         {
663             playlist_Add( p_playlist, psz_uri, psz_uri,
664                           PLAYLIST_INSERT, i_position );
665         }
666     }
667
668     if( psz_uri )
669     {
670         free( psz_uri );
671     }
672     if( psz_name )
673     {
674         free( psz_name );
675     }
676
677     vlc_object_release( p_playlist );
678
679     return 0;
680 }