]> git.sesse.net Git - vlc/blob - modules/demux/playlist/m3u.c
b4s: memory leak
[vlc] / modules / demux / playlist / m3u.c
1 /*****************************************************************************
2  * m3u.c : M3U playlist format import
3  *****************************************************************************
4  * Copyright (C) 2004 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Sigmund Augdal Helberg <dnumgis@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
35 #include <vlc_charset.h>
36
37 #include "playlist.h"
38
39 struct demux_sys_t
40 {
41     char *psz_prefix;
42     char *(*pf_dup) (const char *);
43 };
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 static int Demux( demux_t *p_demux);
49 static void parseEXTINF( char *psz_string, char **ppsz_artist, char **ppsz_name, int *pi_duration );
50 static bool ContainsURL( demux_t *p_demux );
51
52 static char *GuessEncoding (const char *str)
53 {
54     return IsUTF8 (str) ? strdup (str) : FromLatin1 (str);
55 }
56
57 static char *CheckUnicode (const char *str)
58 {
59     return IsUTF8 (str) ? strdup (str): NULL;
60 }
61
62 /*****************************************************************************
63  * Import_M3U: main import function
64  *****************************************************************************/
65 int Import_M3U( vlc_object_t *p_this )
66 {
67     demux_t *p_demux = (demux_t *)p_this;
68     const uint8_t *p_peek;
69     char *(*pf_dup) (const char *) = GuessEncoding;
70     int offset = 0;
71
72     if( stream_Peek( p_demux->s, &p_peek, 3 ) == 3
73      && !memcmp( p_peek, "\xef\xbb\xbf", 3) )
74     {
75         pf_dup = CheckUnicode; /* UTF-8 Byte Order Mark */
76         offset = 3;
77     }
78
79     if( demux_IsPathExtension( p_demux, ".m3u8" )
80      || demux_IsForced( p_demux, "m3u8" )
81      || CheckContentType( p_demux->s, "application/vnd.apple.mpegurl" ) )
82         pf_dup = CheckUnicode; /* UTF-8 file type */
83     else
84     if( demux_IsPathExtension( p_demux, ".m3u" )
85      || demux_IsPathExtension( p_demux, ".vlc" )
86      || demux_IsForced( p_demux, "m3u" )
87      || ContainsURL( p_demux )
88      || CheckContentType( p_demux->s, "audio/x-mpegurl") )
89         ; /* Guess encoding */
90     else
91     {
92         if( stream_Peek( p_demux->s, &p_peek, 8 + offset ) < (8 + offset) )
93             return VLC_EGENERIC;
94
95         p_peek += offset;
96
97         if( !strncasecmp( (const char *)p_peek, "RTSPtext", 8 ) ) /* QuickTime */
98             pf_dup = CheckUnicode; /* UTF-8 */
99         else
100         if( !memcmp( p_peek, "#EXTM3U", 7 ) )
101             ; /* Guess encoding */
102         else
103             return VLC_EGENERIC;
104     }
105
106     stream_Seek( p_demux->s, offset );
107
108     STANDARD_DEMUX_INIT_MSG( "found valid M3U playlist" );
109     p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
110     p_demux->p_sys->pf_dup = pf_dup;
111
112     return VLC_SUCCESS;
113 }
114
115 static bool ContainsURL( demux_t *p_demux )
116 {
117     const uint8_t *p_peek, *p_peek_end;
118     int i_peek;
119
120     i_peek = stream_Peek( p_demux->s, &p_peek, 1024 );
121     if( i_peek <= 0 ) return false;
122     p_peek_end = p_peek + i_peek;
123
124     while( p_peek + sizeof( "https://" ) < p_peek_end )
125     {
126         /* One line starting with a URL is enough */
127         if( !strncasecmp( (const char *)p_peek, "http://", 7 ) ||
128             !strncasecmp( (const char *)p_peek, "mms://", 6 ) ||
129             !strncasecmp( (const char *)p_peek, "rtsp://", 7 ) ||
130             !strncasecmp( (const char *)p_peek, "https://", 8 ) ||
131             !strncasecmp( (const char *)p_peek, "ftp://", 6 ) ||
132             !strncasecmp( (const char *)p_peek, "ftps://", 7 ) ||
133             !strncasecmp( (const char *)p_peek, "ftpes://", 8 ) )
134         {
135             return true;
136         }
137         /* Comments and blank lines are ignored */
138         else if( *p_peek != '#' && *p_peek != '\n' && *p_peek != '\r')
139         {
140             return false;
141         }
142
143         while( p_peek < p_peek_end && *p_peek != '\n' )
144             p_peek++;
145         if ( *p_peek == '\n' )
146             p_peek++;
147     }
148     return false;
149 }
150
151 /*****************************************************************************
152  * Deactivate: frees unused data
153  *****************************************************************************/
154 void Close_M3U( vlc_object_t *p_this )
155 {
156     demux_t *p_demux = (demux_t *)p_this;
157     free( p_demux->p_sys->psz_prefix );
158     free( p_demux->p_sys );
159 }
160
161
162 static int Demux( demux_t *p_demux )
163 {
164     char       *psz_line;
165     char       *psz_name = NULL;
166     char       *psz_artist = NULL;
167     char       *psz_album_art = NULL;
168     int        i_parsed_duration = 0;
169     mtime_t    i_duration = -1;
170     const char**ppsz_options = NULL;
171     char *    (*pf_dup) (const char *) = p_demux->p_sys->pf_dup;
172     int        i_options = 0;
173     bool b_cleanup = false;
174     input_item_t *p_input;
175
176     input_item_t *p_current_input = GetCurrentItem(p_demux);
177     input_item_node_t *p_subitems = input_item_node_Create( p_current_input );
178
179     psz_line = stream_ReadLine( p_demux->s );
180     while( psz_line )
181     {
182         char *psz_parse = psz_line;
183
184         /* Skip leading tabs and spaces */
185         while( *psz_parse == ' ' || *psz_parse == '\t' ||
186                *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
187
188         if( *psz_parse == '#' )
189         {
190             /* Parse extra info */
191
192             /* Skip leading tabs and spaces */
193             while( *psz_parse == ' ' || *psz_parse == '\t' ||
194                    *psz_parse == '\n' || *psz_parse == '\r' ||
195                    *psz_parse == '#' ) psz_parse++;
196
197             if( !*psz_parse ) goto error;
198
199             if( !strncasecmp( psz_parse, "EXTINF:", sizeof("EXTINF:") -1 ) )
200             {
201                 /* Extended info */
202                 psz_parse += sizeof("EXTINF:") - 1;
203                 FREENULL( psz_name );
204                 FREENULL( psz_artist );
205                 parseEXTINF( psz_parse, &psz_artist, &psz_name, &i_parsed_duration );
206                 if( i_parsed_duration >= 0 )
207                     i_duration = i_parsed_duration * INT64_C(1000000);
208                 if( psz_name )
209                     psz_name = pf_dup( psz_name );
210                 if( psz_artist )
211                     psz_artist = pf_dup( psz_artist );
212             }
213             else if( !strncasecmp( psz_parse, "EXTVLCOPT:",
214                                    sizeof("EXTVLCOPT:") -1 ) )
215             {
216                 /* VLC Option */
217                 char *psz_option;
218                 psz_parse += sizeof("EXTVLCOPT:") -1;
219                 if( !*psz_parse ) goto error;
220
221                 psz_option = pf_dup( psz_parse );
222                 if( psz_option )
223                     INSERT_ELEM( ppsz_options, i_options, i_options,
224                                  psz_option );
225             }
226             /* Special case for jamendo which provide the albumart */
227             else if( !strncasecmp( psz_parse, "EXTALBUMARTURL:",
228                      sizeof( "EXTALBUMARTURL:" ) -1 ) )
229             {
230                 psz_parse += sizeof( "EXTALBUMARTURL:" ) - 1;
231                 free( psz_album_art );
232                 psz_album_art = pf_dup( psz_parse );
233             }
234         }
235         else if( !strncasecmp( psz_parse, "RTSPtext", sizeof("RTSPtext") -1 ) )
236         {
237             ;/* special case to handle QuickTime RTSPtext redirect files */
238         }
239         else if( *psz_parse )
240         {
241             char *psz_mrl;
242
243             psz_parse = pf_dup( psz_parse );
244             if( !psz_name && psz_parse )
245                 /* Use filename as name for relative entries */
246                 psz_name = strdup( psz_parse );
247
248             psz_mrl = ProcessMRL( psz_parse, p_demux->p_sys->psz_prefix );
249
250             b_cleanup = true;
251             if( !psz_mrl )
252             {
253                 free( psz_parse );
254                 goto error;
255             }
256
257             p_input = input_item_NewExt( psz_mrl, psz_name,
258                                         i_options, ppsz_options, 0, i_duration );
259
260             free( psz_parse );
261             free( psz_mrl );
262
263             if ( !EMPTY_STR(psz_artist) )
264                 input_item_SetArtist( p_input, psz_artist );
265             if( psz_name ) input_item_SetTitle( p_input, psz_name );
266             if( !EMPTY_STR(psz_album_art) )
267                 input_item_SetArtURL( p_input, psz_album_art );
268
269             input_item_node_AppendItem( p_subitems, p_input );
270             vlc_gc_decref( p_input );
271         }
272
273  error:
274
275         /* Fetch another line */
276         free( psz_line );
277         psz_line = stream_ReadLine( p_demux->s );
278         if( !psz_line ) b_cleanup = true;
279
280         if( b_cleanup )
281         {
282             /* Cleanup state */
283             while( i_options-- ) free( (char*)ppsz_options[i_options] );
284             FREENULL( ppsz_options );
285             i_options = 0;
286             FREENULL( psz_name );
287             FREENULL( psz_artist );
288             FREENULL( psz_album_art );
289             i_parsed_duration = 0;
290             i_duration = -1;
291
292             b_cleanup = false;
293         }
294     }
295     input_item_node_PostAndDelete( p_subitems );
296     vlc_gc_decref(p_current_input);
297     var_Destroy( p_demux, "m3u-extvlcopt" );
298     return 0; /* Needed for correct operation of go back */
299 }
300
301 static void parseEXTINF(char *psz_string, char **ppsz_artist,
302                         char **ppsz_name, int *pi_duration)
303 {
304     char *end = NULL;
305     char *psz_item = NULL;
306
307     end = psz_string + strlen( psz_string );
308
309     /* ignore whitespaces */
310     for (; psz_string < end && ( *psz_string == '\t' || *psz_string == ' ' );
311          psz_string++ );
312
313     /* duration: read to next comma */
314     psz_item = psz_string;
315     psz_string = strchr( psz_string, ',' );
316     if ( psz_string )
317     {
318         *psz_string = '\0';
319         *pi_duration = atoi( psz_item );
320     }
321     else
322     {
323         return;
324     }
325
326     if ( psz_string < end )               /* continue parsing if possible */
327         psz_string++;
328
329     /* analyse the remaining string */
330     psz_item = strstr( psz_string, " - " );
331
332     /* here we have the 0.8.2+ format with artist */
333     if ( psz_item )
334     {
335         /* *** "EXTINF:time,artist - name" */
336         *psz_item = '\0';
337         *ppsz_artist = psz_string;
338         *ppsz_name = psz_item + 3;          /* points directly after ' - ' */
339         return;
340     }
341
342     /* reaching this point means: 0.8.1- with artist or something without artist */
343     if ( *psz_string == ',' )
344     {
345         /* *** "EXTINF:time,,name" */
346         psz_string++;
347         *ppsz_name = psz_string;
348         return;
349     }
350
351     psz_item = psz_string;
352     psz_string = strchr( psz_string, ',' );
353     if ( psz_string )
354     {
355         /* *** "EXTINF:time,artist,name" */
356         *psz_string = '\0';
357         *ppsz_artist = psz_item;
358         *ppsz_name = psz_string+1;
359     }
360     else
361     {
362         /* *** "EXTINF:time,name" */
363         *ppsz_name = psz_item;
364     }
365     return;
366 }
367