]> git.sesse.net Git - vlc/blob - modules/demux/m3u.c
* modules/demux/m3u.c: should manage entries with 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.4 2002/11/18 13:08:35 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
46 struct demux_sys_t
47 {
48     int i_type;                                   /* playlist type (m3u/asx) */
49 };
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 static int  Activate  ( vlc_object_t * );
55 static void Deactivate( vlc_object_t * );
56 static int  Demux ( input_thread_t * );
57
58 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61 vlc_module_begin();
62     set_description( "m3u/asx metademux" );
63     set_capability( "demux", 10 );
64     set_callbacks( Activate, Deactivate );
65     add_shortcut( "m3u" );
66     add_shortcut( "asx" );
67 vlc_module_end();
68
69 /*****************************************************************************
70  * Activate: initializes m3u demux structures
71  *****************************************************************************/
72 static int Activate( vlc_object_t * p_this )
73 {
74     input_thread_t *p_input = (input_thread_t *)p_this;
75     char           *psz_ext;
76     demux_sys_t    *p_m3u;
77     int            i_type = 0;
78
79     /* Initialize access plug-in structures. */
80     if( p_input->i_mtu == 0 )
81     {
82         /* Improve speed. */
83         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
84     }
85
86     p_input->pf_demux = Demux;
87     p_input->pf_rewind = NULL;
88
89     /* Check for m3u/asx file extension */
90     psz_ext = strrchr ( p_input->psz_name, '.' );
91     if( !strcasecmp( psz_ext, ".m3u") ||
92         ( p_input->psz_demux && !strncmp(p_input->psz_demux, "m3u", 3) ) )
93     {
94         i_type = TYPE_M3U;
95     }
96     else if( !strcasecmp( psz_ext, ".asx") ||
97              ( p_input->psz_demux && !strncmp(p_input->psz_demux, "asx", 3) ) )
98     {
99         i_type = TYPE_ASX;
100     }
101
102     if( !i_type )
103         return -1;
104
105     /* Allocate p_m3u */
106     if( !( p_m3u = malloc( sizeof( demux_sys_t ) ) ) )
107     {
108         msg_Err( p_input, "out of memory" );
109         return -1;
110     }
111     p_input->p_demux_data = p_m3u;
112
113     p_m3u->i_type = i_type;
114
115     return 0;
116 }
117
118 /*****************************************************************************
119  * Deactivate: frees unused data
120  *****************************************************************************/
121 static void Deactivate( vlc_object_t *p_this )
122 {
123     input_thread_t *p_input = (input_thread_t *)p_this;
124     demux_sys_t *p_m3u = (demux_sys_t *)p_input->p_demux_data  ; 
125
126     free( p_m3u );
127 }
128
129 /*****************************************************************************
130  * Demux: reads and demuxes data packets
131  *****************************************************************************
132  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
133  *****************************************************************************/
134 static int Demux ( input_thread_t *p_input )
135 {
136     data_packet_t *p_data;
137     char          *p_buf, psz_line[MAX_LINE], *psz_bol, *psz_name, eol_tok;
138     int           i_size, i_bufpos, i_linepos = 0;
139     playlist_t    *p_playlist;
140     vlc_bool_t    b_discard = VLC_FALSE;
141
142     demux_sys_t   *p_m3u = (demux_sys_t *)p_input->p_demux_data;
143
144     p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
145                                                  FIND_ANYWHERE );
146     if( !p_playlist )
147     {
148         msg_Err( p_input, "can't find playlist" );
149         return -1;
150     }
151
152     p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
153
154     /* Depending on wether we are dealing with an m3u/asf file, the end of
155      * line token will be different */
156     if( p_m3u->i_type == TYPE_ASX )
157         eol_tok = '>';
158     else  
159         eol_tok = '\n';
160
161     while( ( i_size = input_SplitBuffer( p_input, &p_data, MAX_LINE ) ) > 0 )
162     {
163         i_bufpos = 0; p_buf = p_data->p_payload_start;
164
165         while( i_size )
166         {
167             /* Build a line < MAX_LINE */
168             while( p_buf[i_bufpos] != eol_tok && i_size )
169             {
170                 if( i_linepos == MAX_LINE || b_discard == VLC_TRUE )
171                 {
172                     /* line is bigger than MAX_LINE, discard it */
173                     i_linepos = 0;
174                     b_discard = VLC_TRUE;
175                 }
176                 else
177                 {
178                     psz_line[i_linepos] = p_buf[i_bufpos];
179                     i_linepos++;
180                 }
181
182                 i_size--; i_bufpos++;
183             }
184
185             /* Check if we need more data */
186             if( !i_size ) continue;
187
188             i_size--; i_bufpos++;
189             b_discard = VLC_FALSE;
190
191             /* Check for empty line */
192             if( !i_linepos ) continue;
193
194             psz_line[i_linepos] = '\0';
195             psz_bol = psz_line;
196             i_linepos = 0;
197
198             /* Remove unnecessary tabs or spaces at the beginning of line */
199             while( *psz_bol == ' ' || *psz_bol == '\t' ||
200                    *psz_bol == '\n' || *psz_bol == '\r' )
201                 psz_bol++;
202
203             if( p_m3u->i_type == TYPE_M3U )
204             {
205                 /* Check for comment line */
206                 if( *psz_bol == '#' )
207                     /*line is comment or extended info, ignored for now */
208                     continue;
209             }
210             else
211             {
212                 /* We are dealing with ASX files.
213                  * We are looking for "href" or "param" html markups that
214                  * begins with "mms://" */
215                 char *psz_eol;
216
217                 while( *psz_bol &&
218                        strncasecmp( psz_bol, "href", sizeof("href") - 1 ) &&
219                        strncasecmp( psz_bol, "param", sizeof("param") - 1 ) )
220                     psz_bol++;
221
222                 if( !*psz_bol ) continue;
223
224                 while( *psz_bol && strncasecmp( psz_bol, "mms://",
225                                                sizeof("mms://") - 1 )  )
226                     psz_bol++;
227
228                 if( !*psz_bol ) continue;
229
230                 psz_eol = strchr( psz_bol, '"');
231                 if( !psz_eol )
232                   continue;
233
234                 *psz_eol = '\0';
235             }
236
237             /*
238              * From now on, we know we've got a meaningful line
239              */
240
241             /* Check if the line has an absolute or relative path */
242             psz_name = psz_bol;
243             while( *psz_name && strncmp( psz_bol, "://", sizeof("://") - 1 ) )
244             {
245                 psz_name++;
246             }
247
248             if( !*psz_name )
249             {
250                 /* the line doesn't specify a protocol name.
251                  * If this line doesn't begin with a '/' then assume the path
252                  * is relative to the path of the m3u file. */
253                 char *psz_path = strdup( p_input->psz_name );
254
255                 psz_name = strrchr( psz_path, '/' );
256                 if( psz_name ) *psz_name = '\0';
257                 else *psz_path = '\0';
258                 psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
259                 sprintf( psz_name, "%s/%s", psz_path, psz_bol );
260                 free( psz_path );
261             }
262             else
263             {
264                 psz_name = strdup( psz_bol );
265             }
266
267             playlist_Add( p_playlist, psz_name,
268                           PLAYLIST_APPEND, PLAYLIST_END );
269
270             free( psz_name );
271
272             continue;
273         }
274
275         input_DeletePacket( p_input->p_method_data, p_data );
276     }
277
278     vlc_object_release( p_playlist );
279
280     return 0;
281 }