]> git.sesse.net Git - vlc/blob - modules/demux/m3u.c
- support for Windows style file names for relative paths
[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.10 2002/12/14 01:05:53 babal 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( !strcasecmp( psz_ext, ".m3u") ||
94         ( p_input->psz_demux && !strncmp(p_input->psz_demux, "m3u", 3) ) )
95     {
96         i_type = TYPE_M3U;
97     }
98     else if( !strcasecmp( psz_ext, ".asx") ||
99              ( p_input->psz_demux && !strncmp(p_input->psz_demux, "asx", 3) ) )
100     {
101         i_type = TYPE_ASX;
102     }
103     else if( !strcasecmp( psz_ext, ".html") ||
104              ( p_input->psz_demux && !strncmp(p_input->psz_demux, "html", 4) ) )
105     {
106         i_type = TYPE_HTML;
107     }
108
109     /* we had no luck looking at the file extention, so we have a look
110      * at the content. This is useful for .asp, .php and similar files
111      * that are actually html. Also useful for som asx files that have
112      * another extention */
113     if( !i_type )
114     {
115         byte_t *p_peek;
116         int i_size = input_Peek( p_input, &p_peek, MAX_LINE );
117         i_size -= sizeof("<html>") - 1;
118         if ( i_size > 0 ) {
119             while ( i_size
120                     && strncasecmp( p_peek, "<html>", sizeof("<html>") - 1 )
121                     && strncasecmp( p_peek, "<asx", sizeof("<asx") - 1 ) )
122             {
123                 p_peek++;
124                 i_size--;
125             }
126             if ( !i_size )
127             {
128                 return -1;
129             }
130             else if ( !strncasecmp( p_peek, "<html>", sizeof("<html>") -1 ) )
131             {
132                 i_type = TYPE_HTML;
133             }
134             else if ( !strncasecmp( p_peek, "<asx", sizeof("<asx") -1 ) )
135             {
136                 i_type = TYPE_ASX;
137             }
138         }
139     }
140     /* Allocate p_m3u */
141     if( !( p_m3u = malloc( sizeof( demux_sys_t ) ) ) )
142     {
143         msg_Err( p_input, "out of memory" );
144         return -1;
145     }
146     p_input->p_demux_data = p_m3u;
147
148     p_m3u->i_type = i_type;
149
150     return 0;
151 }
152
153 /*****************************************************************************
154  * Deactivate: frees unused data
155  *****************************************************************************/
156 static void Deactivate( vlc_object_t *p_this )
157 {
158     input_thread_t *p_input = (input_thread_t *)p_this;
159     demux_sys_t *p_m3u = (demux_sys_t *)p_input->p_demux_data  ; 
160
161     free( p_m3u );
162 }
163
164 /*****************************************************************************
165  * Demux: reads and demuxes data packets
166  *****************************************************************************
167  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
168  *****************************************************************************/
169 static void ProcessLine ( input_thread_t *p_input , demux_sys_t *p_m3u
170         , playlist_t *p_playlist , char psz_line[MAX_LINE] )
171 {
172     char          *psz_bol, *psz_name;
173
174     psz_bol = psz_line;
175
176     /* Remove unnecessary tabs or spaces at the beginning of line */
177     while( *psz_bol == ' ' || *psz_bol == '\t' ||
178            *psz_bol == '\n' || *psz_bol == '\r' )
179         psz_bol++;
180
181     if( p_m3u->i_type == TYPE_M3U )
182     {
183         /* Check for comment line */
184         if( *psz_bol == '#' )
185             /*line is comment or extended info, ignored for now */
186             return;
187     }
188     else if ( p_m3u->i_type == TYPE_ASX )
189     {
190         /* We are dealing with ASX files.
191          * We are looking for "<ref href=" xml markups that
192          * begins with "mms://", "http://" or "file://" */
193         char *psz_eol;
194
195         while( *psz_bol &&
196                strncasecmp( psz_bol, "ref", sizeof("ref") - 1 ) )
197             psz_bol++;
198
199         if( !*psz_bol ) return;
200
201         while( *psz_bol &&
202                strncasecmp( psz_bol, "href", sizeof("href") - 1 ) )
203             psz_bol++;
204
205         if( !*psz_bol ) return;
206
207         while( *psz_bol &&
208                strncasecmp( psz_bol, "mms://",
209                             sizeof("mms://") - 1 ) &&
210                strncasecmp( psz_bol, "http://",
211                             sizeof("http://") - 1 ) &&
212                strncasecmp( psz_bol, "file://",
213                             sizeof("file://") - 1 ) )
214             psz_bol++;
215
216         if( !*psz_bol ) return;
217
218         psz_eol = strchr( psz_bol, '"');
219         if( !psz_eol )
220           return;
221
222         *psz_eol = '\0';
223     }
224     else
225     {
226         /* We are dealing with a html file with embedded
227          * video.  We are looking for "<param name="filename"
228          * value=" html markups that begin with "http://" */
229         char *psz_eol;
230
231         while( *psz_bol &&
232                strncasecmp( psz_bol, "param", sizeof("param") - 1 ) )
233             psz_bol++;
234
235         if( !*psz_bol ) return;
236
237         while( *psz_bol &&
238                strncasecmp( psz_bol, "filename", sizeof("filename") - 1 ) )
239             psz_bol++;
240
241         if( !*psz_bol ) return;
242
243         while( *psz_bol &&
244                strncasecmp( psz_bol, "http://",
245                             sizeof("http://") - 1 ) )
246             psz_bol++;
247
248         if( !*psz_bol ) return;
249
250         psz_eol = strchr( psz_bol, '"');
251         if( !psz_eol )
252           return;
253
254         *psz_eol = '\0';
255
256     }
257
258     /*
259      * From now on, we know we've got a meaningful line
260      */
261
262     /* Check if the line has an absolute or relative path */
263     psz_name = psz_bol;
264     while( *psz_name && strncmp( psz_name, "://", sizeof("://") - 1 ) )
265     {
266         psz_name++;
267     }
268 #ifndef WIN32
269     if( !*psz_name && *psz_bol != '/' )
270 #else
271     if( !*psz_name && (strlen(psz_bol) < 2 ||
272                 ( *(psz_bol+1) != ':' &&
273                   strncmp( psz_bol, "\\\\", 2 ) ) ) )
274 #endif
275     {
276         /* the line doesn't specify a protocol name.
277          * If this line doesn't begin with a '/' then assume the path
278          * is relative to the path of the m3u file. */
279         char *psz_path = strdup( p_input->psz_name );
280
281 #ifndef WIN32
282         psz_name = strrchr( psz_path, '/' );
283 #else
284         psz_name = strrchr( psz_path, '\\' );
285         if ( ! psz_name ) psz_name = strrchr( psz_path, '/' );
286 #endif
287         if( psz_name ) *psz_name = '\0';
288         else *psz_path = '\0';
289 #ifndef WIN32
290         psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
291         sprintf( psz_name, "%s/%s", psz_path, psz_bol );
292 #else
293         if ( *psz_path != '\0' )
294         {
295             psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
296             sprintf( psz_name, "%s\\%s", psz_path, psz_bol );
297         }
298         else psz_name = strdup( psz_bol );
299 #endif
300         free( psz_path );
301     }
302     else
303     {
304         psz_name = strdup( psz_bol );
305     }
306
307     playlist_Add( p_playlist, psz_name,
308                   PLAYLIST_APPEND, PLAYLIST_END );
309
310     free( psz_name );
311 }
312
313 static int Demux ( input_thread_t *p_input )
314 {
315     data_packet_t *p_data;
316     char          *p_buf, psz_line[MAX_LINE], eol_tok;
317     int           i_size, i_bufpos, i_linepos = 0;
318     playlist_t    *p_playlist;
319     vlc_bool_t    b_discard = VLC_FALSE;
320
321     demux_sys_t   *p_m3u = (demux_sys_t *)p_input->p_demux_data;
322
323     p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
324                                                  FIND_ANYWHERE );
325     if( !p_playlist )
326     {
327         msg_Err( p_input, "can't find playlist" );
328         return -1;
329     }
330
331     p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
332
333     /* Depending on wether we are dealing with an m3u/asf file, the end of
334      * line token will be different */
335     if( p_m3u->i_type == TYPE_ASX || p_m3u->i_type == TYPE_HTML )
336         eol_tok = '>';
337     else
338         eol_tok = '\n';
339
340     while( ( i_size = input_SplitBuffer( p_input, &p_data, MAX_LINE ) ) > 0 )
341     {
342         i_bufpos = 0; p_buf = p_data->p_payload_start;
343
344         while( i_size )
345         {
346             /* Build a line < MAX_LINE */
347             while( p_buf[i_bufpos] != eol_tok && i_size )
348             {
349                 if( i_linepos == MAX_LINE || b_discard == VLC_TRUE )
350                 {
351                     /* line is bigger than MAX_LINE, discard it */
352                     i_linepos = 0;
353                     b_discard = VLC_TRUE;
354                 }
355                 else
356                 {
357                     if ( eol_tok != '\n' || p_buf[i_bufpos] != '\r' )
358                     {
359                         psz_line[i_linepos] = p_buf[i_bufpos];
360                         i_linepos++;
361                     }
362                 }
363
364                 i_size--; i_bufpos++;
365             }
366
367             /* Check if we need more data */
368             if( !i_size ) continue;
369
370             i_size--; i_bufpos++;
371             b_discard = VLC_FALSE;
372
373             /* Check for empty line */
374             if( !i_linepos ) continue;
375
376             psz_line[i_linepos] = '\0';
377             i_linepos = 0;
378             ProcessLine ( p_input, p_m3u , p_playlist , psz_line );
379         }
380
381         input_DeletePacket( p_input->p_method_data, p_data );
382     }
383
384     if ( i_linepos && b_discard != VLC_TRUE && eol_tok == '\n' )
385     {
386         psz_line[i_linepos] = '\0';
387         i_linepos = 0;
388         ProcessLine ( p_input, p_m3u , p_playlist , psz_line );
389     }
390
391     vlc_object_release( p_playlist );
392
393     return 0;
394 }