]> git.sesse.net Git - vlc/blob - modules/demux/m3u.c
* modules/misc/network/ipv6.c: gave a quick try to implementing ttl for ipv6.
[vlc] / modules / demux / m3u.c
1 /*****************************************************************************
2  * m3u.c: a meta demux to parse pls, m3u and asx playlists
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: m3u.c,v 1.18 2003/04/06 20:08:11 sigmunau Exp $
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>                                              /* strdup() */
30 #include <errno.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/input.h>
34 #include <vlc_playlist.h>
35
36 #include <sys/types.h>
37
38 /*****************************************************************************
39  * Constants and structures
40  *****************************************************************************/
41 #define MAX_LINE 1024
42
43 #define TYPE_UNKNOWN 0
44 #define TYPE_M3U 1
45 #define TYPE_ASX 2
46 #define TYPE_HTML 3
47 #define TYPE_PLS 4
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 ( input_thread_t * );
60
61 /*****************************************************************************
62  * Module descriptor
63  *****************************************************************************/
64 vlc_module_begin();
65     set_description( _("playlist metademux") );
66     set_capability( "demux", 180 );
67     set_callbacks( Activate, Deactivate );
68     add_shortcut( "m3u" );
69     add_shortcut( "asx" );
70     add_shortcut( "html" );
71     add_shortcut( "pls" );
72 vlc_module_end();
73
74 /*****************************************************************************
75  * Activate: initializes m3u demux structures
76  *****************************************************************************/
77 static int Activate( vlc_object_t * p_this )
78 {
79     input_thread_t *p_input = (input_thread_t *)p_this;
80     char           *psz_ext;
81     demux_sys_t    *p_m3u;
82     int             i_type = 0;
83     int             i_type2 = 0;
84
85     /* Initialize access plug-in structures. */
86     if( p_input->i_mtu == 0 )
87     {
88         /* Improve speed. */
89         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
90     }
91
92     p_input->pf_demux = Demux;
93     p_input->pf_rewind = NULL;
94
95     /* Check for m3u/asx file extension or if the demux has been forced */
96     psz_ext = strrchr ( p_input->psz_name, '.' );
97
98     if( ( psz_ext && !strcasecmp( psz_ext, ".m3u") ) ||
99         ( p_input->psz_demux && !strcmp(p_input->psz_demux, "m3u") ) )
100     {
101         i_type = TYPE_M3U;
102     }
103     else if( ( psz_ext && !strcasecmp( psz_ext, ".asx") ) ||
104              ( p_input->psz_demux && !strcmp(p_input->psz_demux, "asx") ) )
105     {
106         i_type = TYPE_ASX;
107     }
108     else if( ( psz_ext && !strcasecmp( psz_ext, ".html") ) ||
109              ( p_input->psz_demux && !strcmp(p_input->psz_demux, "html") ) )
110     {
111         i_type = TYPE_HTML;
112     }
113     else if( ( psz_ext && !strcasecmp( psz_ext, ".pls") ) ||
114              ( p_input->psz_demux && !strcmp(p_input->psz_demux, "pls") ) )
115     {
116         i_type = TYPE_PLS;
117     }
118
119     /* we had no luck looking at the file extention, so we have a look
120      * at the content. This is useful for .asp, .php and similar files
121      * that are actually html. Also useful for som asx files that have
122      * another extention */
123     if( i_type != TYPE_M3U )
124     {
125         byte_t *p_peek;
126         int i_size = input_Peek( p_input, &p_peek, MAX_LINE );
127         i_size -= sizeof("[playlist]") - 1;
128         if ( i_size > 0 ) {
129             while ( i_size
130                     && strncasecmp( p_peek, "[playlist]", sizeof("[playlist]") - 1 )
131                     && strncasecmp( p_peek, "<html>", sizeof("<html>") - 1 )
132                     && strncasecmp( p_peek, "<asx", sizeof("<asx") - 1 ) )
133             {
134                 p_peek++;
135                 i_size--;
136             }
137             if ( !i_size )
138             {
139                 ;
140             }
141             else if ( !strncasecmp( p_peek, "[playlist]", sizeof("[playlist]") -1 ) )
142             {
143                 i_type2 = TYPE_PLS;
144             }
145             else if ( !strncasecmp( p_peek, "<html>", sizeof("<html>") -1 ) )
146             {
147                 i_type2 = TYPE_HTML;
148             }
149             else if ( !strncasecmp( p_peek, "<asx", sizeof("<asx") -1 ) )
150             {
151                 i_type2 = TYPE_ASX;
152             }
153         }
154     }
155     if ( !i_type && !i_type2 )
156     {
157         return -1;
158     }
159     if ( i_type  && !i_type2 )
160     {
161         i_type = TYPE_M3U;
162     }
163     else
164     {
165         i_type = i_type2;
166     }
167
168     /* Allocate p_m3u */
169     if( !( p_m3u = malloc( sizeof( demux_sys_t ) ) ) )
170     {
171         msg_Err( p_input, "out of memory" );
172         return -1;
173     }
174     p_input->p_demux_data = p_m3u;
175
176     p_m3u->i_type = i_type;
177
178     return 0;
179 }
180
181 /*****************************************************************************
182  * Deactivate: frees unused data
183  *****************************************************************************/
184 static void Deactivate( vlc_object_t *p_this )
185 {
186     input_thread_t *p_input = (input_thread_t *)p_this;
187     demux_sys_t *p_m3u = (demux_sys_t *)p_input->p_demux_data  ; 
188
189     free( p_m3u );
190 }
191
192 /*****************************************************************************
193  * ProcessLine: read a "line" from the file and add any entries found
194  * to the playlist. Return number of items added ( 0 or 1 )
195  *****************************************************************************/
196 static int ProcessLine ( input_thread_t *p_input , demux_sys_t *p_m3u
197         , playlist_t *p_playlist , char psz_line[MAX_LINE], int i_position )
198 {
199     char          *psz_bol, *psz_name;
200     psz_bol = psz_line;
201
202     /* Remove unnecessary tabs or spaces at the beginning of line */
203     while( *psz_bol == ' ' || *psz_bol == '\t' ||
204            *psz_bol == '\n' || *psz_bol == '\r' )
205         psz_bol++;
206
207     if( p_m3u->i_type == TYPE_M3U )
208     {
209         /* Check for comment line */
210         if( *psz_bol == '#' )
211             /*line is comment or extended info, ignored for now */
212             return 0;
213     }
214     else if ( p_m3u->i_type == TYPE_PLS )
215     {
216         /* We are dealing with .pls files from shoutcast
217          * We are looking for lines like "File1=http://..." */
218         if( !strncasecmp( psz_bol, "File", sizeof("File") - 1 ) )
219         {
220             psz_bol += sizeof("File") - 1;
221             psz_bol = strchr( psz_bol, '=' );
222             if ( !psz_bol ) return 0;
223             psz_bol++;
224         }
225         else
226         {
227             return 0;
228         }
229     }
230     else if ( p_m3u->i_type == TYPE_ASX )
231     {
232         /* We are dealing with ASX files.
233          * We are looking for "<ref href=" xml markups that
234          * begins with "mms://", "http://" or "file://" */
235         char *psz_eol;
236
237         while( *psz_bol &&
238                strncasecmp( psz_bol, "ref", sizeof("ref") - 1 ) )
239             psz_bol++;
240
241         if( !*psz_bol ) return 0;
242
243         while( *psz_bol &&
244                strncasecmp( psz_bol, "href", sizeof("href") - 1 ) )
245             psz_bol++;
246
247         if( !*psz_bol ) return 0;
248
249         while( *psz_bol &&
250                strncasecmp( psz_bol, "mms://",
251                             sizeof("mms://") - 1 ) &&
252                strncasecmp( psz_bol, "mmsu://",
253                             sizeof("mmsu://") - 1 ) &&
254                strncasecmp( psz_bol, "mmst://",
255                             sizeof("mmst://") - 1 ) &&
256                strncasecmp( psz_bol, "http://",
257                             sizeof("http://") - 1 ) &&
258                strncasecmp( psz_bol, "file://",
259                             sizeof("file://") - 1 ) )
260             psz_bol++;
261
262         if( !*psz_bol ) return 0;
263
264         psz_eol = strchr( psz_bol, '"');
265         if( !psz_eol )
266           return 0;
267
268         *psz_eol = '\0';
269     }
270     else if ( p_m3u->i_type == TYPE_HTML )
271     {
272         /* We are dealing with a html file with embedded
273          * video.  We are looking for "<param name="filename"
274          * value=" html markups that begin with "http://" */
275         char *psz_eol;
276
277         while( *psz_bol &&
278                strncasecmp( psz_bol, "param", sizeof("param") - 1 ) )
279             psz_bol++;
280
281         if( !*psz_bol ) return 0;
282
283         while( *psz_bol &&
284                strncasecmp( psz_bol, "filename", sizeof("filename") - 1 ) )
285             psz_bol++;
286
287         if( !*psz_bol ) return 0;
288
289         while( *psz_bol &&
290                strncasecmp( psz_bol, "http://",
291                             sizeof("http://") - 1 ) )
292             psz_bol++;
293
294         if( !*psz_bol ) return 0;
295
296         psz_eol = strchr( psz_bol, '"');
297         if( !psz_eol )
298           return 0;
299
300         *psz_eol = '\0';
301
302     }
303     else
304     {
305         msg_Warn( p_input, "unknown file type" );
306         return 0;
307     }
308
309     /* empty line */
310     if ( !*psz_bol ) return 0;
311
312     /*
313      * From now on, we know we've got a meaningful line
314      */
315
316     /* check for a protocol name */
317     /* for URL, we should look for "://"
318      * for MRL (Media Resource Locator) ([[<access>][/<demux>]:][<source>]),
319      * we should look for ":"
320      * so we end up looking simply for ":"*/
321     /* PB: on some file systems, ':' are valid characters though*/
322     psz_name = psz_bol;
323     while( *psz_name && *psz_name!=':' )
324     {
325         psz_name++;
326     }
327 #ifdef WIN32
328     if ( *psz_name && ( psz_name == psz_bol + 1 ) )
329     {
330         /* if it is not an URL,
331          * as it is unlikely to be an MRL (PB: if it is ?)
332          * it should be an absolute file name with the drive letter */
333         if ( *(psz_name+1) == '/' )/* "*:/" */
334         {
335             if ( *(psz_name+2) != '/' )/* not "*://" */
336                 while ( *psz_name ) *psz_name++;/* so now (*psz_name==0) */
337         }
338         else while ( *psz_name ) *psz_name++;/* "*:*"*/
339     }
340 #endif
341
342     /* if the line doesn't specify a protocol name,
343      * check if the line has an absolute or relative path */
344 #ifndef WIN32
345     if( !*psz_name && *psz_bol != '/' )
346          /* If this line doesn't begin with a '/' */
347 #else
348     if( !*psz_name
349             && *psz_bol!='/'
350             && *psz_bol!='\\'
351             && *(psz_bol+1)!=':' )
352          /* if this line doesn't begin with
353           *  "/" or "\" or "*:" or "*:\" or "*:/" or "\\" */
354 #endif
355     {
356         /* assume the path is relative to the path of the m3u file. */
357         char *psz_path = strdup( p_input->psz_name );
358
359 #ifndef WIN32
360         psz_name = strrchr( psz_path, '/' );
361 #else
362         psz_name = strrchr( psz_path, '\\' );
363         if ( ! psz_name ) psz_name = strrchr( psz_path, '/' );
364 #endif
365         if( psz_name ) *psz_name = '\0';
366         else *psz_path = '\0';
367 #ifndef WIN32
368         psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
369         sprintf( psz_name, "%s/%s", psz_path, psz_bol );
370 #else
371         if ( *psz_path != '\0' )
372         {
373             psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
374             sprintf( psz_name, "%s\\%s", psz_path, psz_bol );
375         }
376         else psz_name = strdup( psz_bol );
377 #endif
378         free( psz_path );
379     }
380     else
381     {
382         psz_name = strdup( psz_bol );
383     }
384
385     playlist_Add( p_playlist, psz_name,
386                   PLAYLIST_INSERT, i_position );
387
388     free( psz_name );
389     return 1;
390 }
391
392 /*****************************************************************************
393  * Demux: reads and demuxes data packets
394  *****************************************************************************
395  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
396  *****************************************************************************/
397 static int Demux ( input_thread_t *p_input )
398 {
399     data_packet_t *p_data;
400     char          *p_buf, psz_line[MAX_LINE], eol_tok;
401     int           i_size, i_bufpos, i_linepos = 0;
402     playlist_t    *p_playlist;
403     int           i_position;
404     vlc_bool_t    b_discard = VLC_FALSE;
405
406     demux_sys_t   *p_m3u = (demux_sys_t *)p_input->p_demux_data;
407
408     p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
409                                                  FIND_ANYWHERE );
410     if( !p_playlist )
411     {
412         msg_Err( p_input, "can't find playlist" );
413         return -1;
414     }
415
416     p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
417     i_position = p_playlist->i_index + 1;
418
419     /* Depending on wether we are dealing with an m3u/asf file, the end of
420      * line token will be different */
421     if( p_m3u->i_type == TYPE_ASX || p_m3u->i_type == TYPE_HTML )
422         eol_tok = '>';
423     else
424         eol_tok = '\n';
425
426     while( ( i_size = input_SplitBuffer( p_input, &p_data, MAX_LINE ) ) > 0 )
427     {
428         i_bufpos = 0; p_buf = p_data->p_payload_start;
429
430         while( i_size )
431         {
432             /* Build a line < MAX_LINE */
433             while( p_buf[i_bufpos] != eol_tok && i_size )
434             {
435                 if( i_linepos == MAX_LINE || b_discard == VLC_TRUE )
436                 {
437                     /* line is bigger than MAX_LINE, discard it */
438                     i_linepos = 0;
439                     b_discard = VLC_TRUE;
440                 }
441                 else
442                 {
443                     if ( eol_tok != '\n' || p_buf[i_bufpos] != '\r' )
444                     {
445                         psz_line[i_linepos] = p_buf[i_bufpos];
446                         i_linepos++;
447                     }
448                 }
449
450                 i_size--; i_bufpos++;
451             }
452
453             /* Check if we need more data */
454             if( !i_size ) continue;
455
456             i_size--; i_bufpos++;
457             b_discard = VLC_FALSE;
458
459             /* Check for empty line */
460             if( !i_linepos ) continue;
461
462             psz_line[i_linepos] = '\0';
463             i_linepos = 0;
464             i_position += ProcessLine ( p_input, p_m3u , p_playlist ,
465                                         psz_line, i_position );
466         }
467
468         input_DeletePacket( p_input->p_method_data, p_data );
469     }
470
471     if ( i_linepos && b_discard != VLC_TRUE && eol_tok == '\n' )
472     {
473         psz_line[i_linepos] = '\0';
474         i_linepos = 0;
475         i_position += ProcessLine ( p_input, p_m3u , p_playlist , psz_line,
476                                     i_position );
477     }
478
479     vlc_object_release( p_playlist );
480
481     return 0;
482 }