]> git.sesse.net Git - vlc/blob - modules/demux/m3u.c
* ALL: i18n updates and fixes.
[vlc] / modules / demux / m3u.c
1 /*****************************************************************************
2  * m3u.c: a meta demux to parse m3u and asx playlists
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: m3u.c,v 1.13 2003/02/27 13:19:43 gbazin 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_M3U 1
44 #define TYPE_ASX 2
45 #define TYPE_HTML 3
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( _("m3u/asx metademux") );
64     set_capability( "demux", 10 );
65     set_callbacks( Activate, Deactivate );
66     add_shortcut( "m3u" );
67     add_shortcut( "asx" );
68     add_shortcut( "html" );
69 vlc_module_end();
70
71 /*****************************************************************************
72  * Activate: initializes m3u demux structures
73  *****************************************************************************/
74 static int Activate( vlc_object_t * p_this )
75 {
76     input_thread_t *p_input = (input_thread_t *)p_this;
77     char           *psz_ext;
78     demux_sys_t    *p_m3u;
79     int            i_type = 0;
80
81     /* Initialize access plug-in structures. */
82     if( p_input->i_mtu == 0 )
83     {
84         /* Improve speed. */
85         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
86     }
87
88     p_input->pf_demux = Demux;
89     p_input->pf_rewind = NULL;
90
91     /* Check for m3u/asx file extension */
92     psz_ext = strrchr ( p_input->psz_name, '.' );
93     if( psz_ext )
94     {
95         if( !strcasecmp( psz_ext, ".m3u") ||
96             ( p_input->psz_demux && !strncmp(p_input->psz_demux, "m3u", 3) ) )
97         {
98             i_type = TYPE_M3U;
99         }
100         else if( !strcasecmp( psz_ext, ".asx") ||
101                  ( p_input->psz_demux && !strncmp(p_input->psz_demux, "asx", 3) ) )
102         {
103             i_type = TYPE_ASX;
104         }
105         else if( !strcasecmp( psz_ext, ".html") ||
106                  ( p_input->psz_demux && !strncmp(p_input->psz_demux, "html", 4) ) )
107         {
108             i_type = TYPE_HTML;
109         }
110     }
111
112     /* we had no luck looking at the file extention, so we have a look
113      * at the content. This is useful for .asp, .php and similar files
114      * that are actually html. Also useful for som asx files that have
115      * another extention */
116     if( !i_type )
117     {
118         byte_t *p_peek;
119         int i_size = input_Peek( p_input, &p_peek, MAX_LINE );
120         i_size -= sizeof("<html>") - 1;
121         if ( i_size > 0 ) {
122             while ( i_size
123                     && strncasecmp( p_peek, "<html>", sizeof("<html>") - 1 )
124                     && strncasecmp( p_peek, "<asx", sizeof("<asx") - 1 ) )
125             {
126                 p_peek++;
127                 i_size--;
128             }
129             if ( !i_size )
130             {
131                 return -1;
132             }
133             else if ( !strncasecmp( p_peek, "<html>", sizeof("<html>") -1 ) )
134             {
135                 i_type = TYPE_HTML;
136             }
137             else if ( !strncasecmp( p_peek, "<asx", sizeof("<asx") -1 ) )
138             {
139                 i_type = TYPE_ASX;
140             }
141         }
142     }
143     /* Allocate p_m3u */
144     if( !( p_m3u = malloc( sizeof( demux_sys_t ) ) ) )
145     {
146         msg_Err( p_input, "out of memory" );
147         return -1;
148     }
149     p_input->p_demux_data = p_m3u;
150
151     p_m3u->i_type = i_type;
152
153     return 0;
154 }
155
156 /*****************************************************************************
157  * Deactivate: frees unused data
158  *****************************************************************************/
159 static void Deactivate( vlc_object_t *p_this )
160 {
161     input_thread_t *p_input = (input_thread_t *)p_this;
162     demux_sys_t *p_m3u = (demux_sys_t *)p_input->p_demux_data  ; 
163
164     free( p_m3u );
165 }
166
167 /*****************************************************************************
168  * Demux: reads and demuxes data packets
169  *****************************************************************************
170  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
171  *****************************************************************************/
172 static void ProcessLine ( input_thread_t *p_input , demux_sys_t *p_m3u
173         , playlist_t *p_playlist , char psz_line[MAX_LINE] )
174 {
175     char          *psz_bol, *psz_name;
176
177     psz_bol = psz_line;
178
179     /* Remove unnecessary tabs or spaces at the beginning of line */
180     while( *psz_bol == ' ' || *psz_bol == '\t' ||
181            *psz_bol == '\n' || *psz_bol == '\r' )
182         psz_bol++;
183
184     if( p_m3u->i_type == TYPE_M3U )
185     {
186         /* Check for comment line */
187         if( *psz_bol == '#' )
188             /*line is comment or extended info, ignored for now */
189             return;
190     }
191     else if ( p_m3u->i_type == TYPE_ASX )
192     {
193         /* We are dealing with ASX files.
194          * We are looking for "<ref href=" xml markups that
195          * begins with "mms://", "http://" or "file://" */
196         char *psz_eol;
197
198         while( *psz_bol &&
199                strncasecmp( psz_bol, "ref", sizeof("ref") - 1 ) )
200             psz_bol++;
201
202         if( !*psz_bol ) return;
203
204         while( *psz_bol &&
205                strncasecmp( psz_bol, "href", sizeof("href") - 1 ) )
206             psz_bol++;
207
208         if( !*psz_bol ) return;
209
210         while( *psz_bol &&
211                strncasecmp( psz_bol, "mms://",
212                             sizeof("mms://") - 1 ) &&
213                strncasecmp( psz_bol, "http://",
214                             sizeof("http://") - 1 ) &&
215                strncasecmp( psz_bol, "file://",
216                             sizeof("file://") - 1 ) )
217             psz_bol++;
218
219         if( !*psz_bol ) return;
220
221         psz_eol = strchr( psz_bol, '"');
222         if( !psz_eol )
223           return;
224
225         *psz_eol = '\0';
226     }
227     else
228     {
229         /* We are dealing with a html file with embedded
230          * video.  We are looking for "<param name="filename"
231          * value=" html markups that begin with "http://" */
232         char *psz_eol;
233
234         while( *psz_bol &&
235                strncasecmp( psz_bol, "param", sizeof("param") - 1 ) )
236             psz_bol++;
237
238         if( !*psz_bol ) return;
239
240         while( *psz_bol &&
241                strncasecmp( psz_bol, "filename", sizeof("filename") - 1 ) )
242             psz_bol++;
243
244         if( !*psz_bol ) return;
245
246         while( *psz_bol &&
247                strncasecmp( psz_bol, "http://",
248                             sizeof("http://") - 1 ) )
249             psz_bol++;
250
251         if( !*psz_bol ) return;
252
253         psz_eol = strchr( psz_bol, '"');
254         if( !psz_eol )
255           return;
256
257         *psz_eol = '\0';
258
259     }
260
261     /* empty line */
262     if ( !*psz_bol ) return;
263
264     /*
265      * From now on, we know we've got a meaningful line
266      */
267
268     /* check for a protocol name */
269     /* for URL, we should look for "://"
270      * for MRL (Media Resource Locator) ([[<access>][/<demux>]:][<source>]),
271      * we should look for ":"
272      * so we end up looking simply for ":"*/
273     /* PB: on some file systems, ':' are valid characters though*/
274     psz_name = psz_bol;
275     while( *psz_name && *psz_name!=':' )
276     {
277         psz_name++;
278     }
279 #ifdef WIN32
280     if ( *psz_name && ( psz_name == psz_bol + 1 ) )
281     {
282         /* if it is not an URL,
283          * as it is unlikely to be an MRL (PB: if it is ?)
284          * it should be an absolute file name with the drive letter */
285         if ( *(psz_name+1) == '/' )/* "*:/" */
286         {
287             if ( *(psz_name+2) != '/' )/* not "*://" */
288                 while ( *psz_name ) *psz_name++;/* so now (*psz_name==0) */
289         }
290         else while ( *psz_name ) *psz_name++;/* "*:*"*/
291     }
292 #endif
293
294     /* if the line doesn't specify a protocol name,
295      * check if the line has an absolute or relative path */
296 #ifndef WIN32
297     if( !*psz_name && *psz_bol != '/' )
298          /* If this line doesn't begin with a '/' */
299 #else
300     if( !*psz_name
301             && *psz_bol!='/'
302             && *psz_bol!='\\'
303             && *(psz_bol+1)!=':' )
304          /* if this line doesn't begin with
305           *  "/" or "\" or "*:" or "*:\" or "*:/" or "\\" */
306 #endif
307     {
308         /* assume the path is relative to the path of the m3u file. */
309         char *psz_path = strdup( p_input->psz_name );
310
311 #ifndef WIN32
312         psz_name = strrchr( psz_path, '/' );
313 #else
314         psz_name = strrchr( psz_path, '\\' );
315         if ( ! psz_name ) psz_name = strrchr( psz_path, '/' );
316 #endif
317         if( psz_name ) *psz_name = '\0';
318         else *psz_path = '\0';
319 #ifndef WIN32
320         psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
321         sprintf( psz_name, "%s/%s", psz_path, psz_bol );
322 #else
323         if ( *psz_path != '\0' )
324         {
325             psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
326             sprintf( psz_name, "%s\\%s", psz_path, psz_bol );
327         }
328         else psz_name = strdup( psz_bol );
329 #endif
330         free( psz_path );
331     }
332     else
333     {
334         psz_name = strdup( psz_bol );
335     }
336
337     playlist_Add( p_playlist, psz_name,
338                   PLAYLIST_APPEND, PLAYLIST_END );
339
340     free( psz_name );
341 }
342
343 static int Demux ( input_thread_t *p_input )
344 {
345     data_packet_t *p_data;
346     char          *p_buf, psz_line[MAX_LINE], eol_tok;
347     int           i_size, i_bufpos, i_linepos = 0;
348     playlist_t    *p_playlist;
349     vlc_bool_t    b_discard = VLC_FALSE;
350
351     demux_sys_t   *p_m3u = (demux_sys_t *)p_input->p_demux_data;
352
353     p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
354                                                  FIND_ANYWHERE );
355     if( !p_playlist )
356     {
357         msg_Err( p_input, "can't find playlist" );
358         return -1;
359     }
360
361     p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
362
363     /* Depending on wether we are dealing with an m3u/asf file, the end of
364      * line token will be different */
365     if( p_m3u->i_type == TYPE_ASX || p_m3u->i_type == TYPE_HTML )
366         eol_tok = '>';
367     else
368         eol_tok = '\n';
369
370     while( ( i_size = input_SplitBuffer( p_input, &p_data, MAX_LINE ) ) > 0 )
371     {
372         i_bufpos = 0; p_buf = p_data->p_payload_start;
373
374         while( i_size )
375         {
376             /* Build a line < MAX_LINE */
377             while( p_buf[i_bufpos] != eol_tok && i_size )
378             {
379                 if( i_linepos == MAX_LINE || b_discard == VLC_TRUE )
380                 {
381                     /* line is bigger than MAX_LINE, discard it */
382                     i_linepos = 0;
383                     b_discard = VLC_TRUE;
384                 }
385                 else
386                 {
387                     if ( eol_tok != '\n' || p_buf[i_bufpos] != '\r' )
388                     {
389                         psz_line[i_linepos] = p_buf[i_bufpos];
390                         i_linepos++;
391                     }
392                 }
393
394                 i_size--; i_bufpos++;
395             }
396
397             /* Check if we need more data */
398             if( !i_size ) continue;
399
400             i_size--; i_bufpos++;
401             b_discard = VLC_FALSE;
402
403             /* Check for empty line */
404             if( !i_linepos ) continue;
405
406             psz_line[i_linepos] = '\0';
407             i_linepos = 0;
408             ProcessLine ( p_input, p_m3u , p_playlist , psz_line );
409         }
410
411         input_DeletePacket( p_input->p_method_data, p_data );
412     }
413
414     if ( i_linepos && b_discard != VLC_TRUE && eol_tok == '\n' )
415     {
416         psz_line[i_linepos] = '\0';
417         i_linepos = 0;
418         ProcessLine ( p_input, p_m3u , p_playlist , psz_line );
419     }
420
421     vlc_object_release( p_playlist );
422
423     return 0;
424 }