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