]> git.sesse.net Git - vlc/blob - modules/demux/playlist/m3u.c
m3u: assume m3u with BOM at start as utf-8 encoded.
[vlc] / modules / demux / playlist / m3u.c
1 /*****************************************************************************
2  * m3u.c : M3U playlist format import
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
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
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., 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     CHECK_PEEK( p_peek, 8 );
70     char *(*pf_dup) (const char *);
71
72     if( POKE( p_peek, "RTSPtext", 8 ) /* QuickTime */
73      || POKE( p_peek, "\xef\xbb\xbf" "#EXTM3U", 10) /* BOM at start */
74      || demux_IsPathExtension( p_demux, ".m3u8" )
75      || demux_IsForced( p_demux, "m3u8" ) )
76         pf_dup = CheckUnicode; /* UTF-8 */
77     else
78     if( POKE( p_peek, "#EXTM3U", 7 )
79      || demux_IsPathExtension( p_demux, ".m3u" )
80      || demux_IsPathExtension( p_demux, ".vlc" )
81      || demux_IsForced( p_demux, "m3u" )
82      || ContainsURL( p_demux ) )
83         pf_dup = GuessEncoding;
84     else
85         return VLC_EGENERIC;
86
87     STANDARD_DEMUX_INIT_MSG( "found valid M3U playlist" );
88     p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
89     p_demux->p_sys->pf_dup = pf_dup;
90
91     return VLC_SUCCESS;
92 }
93
94 static bool ContainsURL( demux_t *p_demux )
95 {
96     const uint8_t *p_peek, *p_peek_end;
97     int i_peek;
98
99     i_peek = stream_Peek( p_demux->s, &p_peek, 1024 );
100     if( i_peek <= 0 ) return false;
101     p_peek_end = p_peek + i_peek;
102
103     while( p_peek + sizeof( "https://" ) < p_peek_end )
104     {
105         /* One line starting with a URL is enough */
106         if( !strncasecmp( (const char *)p_peek, "http://", 7 ) ||
107             !strncasecmp( (const char *)p_peek, "mms://", 6 ) ||
108             !strncasecmp( (const char *)p_peek, "rtsp://", 7 ) ||
109             !strncasecmp( (const char *)p_peek, "https://", 8 ) ||
110             !strncasecmp( (const char *)p_peek, "ftp://", 6 ) )
111         {
112             return true;
113         }
114         /* Comments and blank lines are ignored */
115         else if( *p_peek != '#' && *p_peek != '\n' && *p_peek != '\r')
116         {
117             return false;
118         }
119
120         while( p_peek < p_peek_end && *p_peek != '\n' )
121             p_peek++;
122         if ( *p_peek == '\n' )
123             p_peek++;
124     }
125     return false;
126 }
127
128 /*****************************************************************************
129  * Deactivate: frees unused data
130  *****************************************************************************/
131 void Close_M3U( vlc_object_t *p_this )
132 {
133     demux_t *p_demux = (demux_t *)p_this;
134     free( p_demux->p_sys->psz_prefix );
135     free( p_demux->p_sys );
136 }
137
138
139 static int Demux( demux_t *p_demux )
140 {
141     char       *psz_line;
142     char       *psz_name = NULL;
143     char       *psz_artist = NULL;
144     char       *psz_album_art = NULL;
145     int        i_parsed_duration = 0;
146     mtime_t    i_duration = -1;
147     const char**ppsz_options = NULL;
148     char *    (*pf_dup) (const char *) = p_demux->p_sys->pf_dup;
149     int        i_options = 0;
150     bool b_cleanup = false;
151     input_item_t *p_input;
152
153     input_item_t *p_current_input = GetCurrentItem(p_demux);
154     input_item_node_t *p_subitems = input_item_node_Create( p_current_input );
155
156     psz_line = stream_ReadLine( p_demux->s );
157     while( psz_line )
158     {
159         char *psz_parse = psz_line;
160
161         /* Skip leading tabs and spaces */
162         while( *psz_parse == ' ' || *psz_parse == '\t' ||
163                *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
164
165         if( *psz_parse == '#' )
166         {
167             /* Parse extra info */
168
169             /* Skip leading tabs and spaces */
170             while( *psz_parse == ' ' || *psz_parse == '\t' ||
171                    *psz_parse == '\n' || *psz_parse == '\r' ||
172                    *psz_parse == '#' ) psz_parse++;
173
174             if( !*psz_parse ) goto error;
175
176             if( !strncasecmp( psz_parse, "EXTINF:", sizeof("EXTINF:") -1 ) )
177             {
178                 /* Extended info */
179                 psz_parse += sizeof("EXTINF:") - 1;
180                 free(psz_name);
181                 free(psz_artist);
182                 parseEXTINF( psz_parse, &psz_artist, &psz_name, &i_parsed_duration );
183                 if( i_parsed_duration >= 0 )
184                     i_duration = i_parsed_duration * INT64_C(1000000);
185                 if( psz_name )
186                     psz_name = pf_dup( psz_name );
187                 if( psz_artist )
188                     psz_artist = pf_dup( psz_artist );
189             }
190             else if( !strncasecmp( psz_parse, "EXTVLCOPT:",
191                                    sizeof("EXTVLCOPT:") -1 ) )
192             {
193                 /* VLC Option */
194                 char *psz_option;
195                 psz_parse += sizeof("EXTVLCOPT:") -1;
196                 if( !*psz_parse ) goto error;
197
198                 psz_option = pf_dup( psz_parse );
199                 if( psz_option )
200                     INSERT_ELEM( ppsz_options, i_options, i_options,
201                                  psz_option );
202             }
203             /* Special case for jamendo which provide the albumart */
204             else if( !strncasecmp( psz_parse, "EXTALBUMARTURL:",
205                      sizeof( "EXTALBUMARTURL:" ) -1 ) )
206             {
207                 psz_parse += sizeof( "EXTALBUMARTURL:" ) - 1;
208                 free( psz_album_art );
209                 psz_album_art = pf_dup( psz_parse );
210             }
211         }
212         else if( !strncasecmp( psz_parse, "RTSPtext", sizeof("RTSPtext") -1 ) )
213         {
214             ;/* special case to handle QuickTime RTSPtext redirect files */
215         }
216         else if( *psz_parse )
217         {
218             char *psz_mrl;
219
220             psz_parse = pf_dup( psz_parse );
221             if( !psz_name && psz_parse )
222                 /* Use filename as name for relative entries */
223                 psz_name = strdup( psz_parse );
224
225             psz_mrl = ProcessMRL( psz_parse, p_demux->p_sys->psz_prefix );
226
227             b_cleanup = true;
228             if( !psz_mrl )
229             {
230                 free( psz_parse );
231                 goto error;
232             }
233
234             p_input = input_item_NewExt( psz_mrl, psz_name,
235                                         i_options, ppsz_options, 0, i_duration );
236
237             free( psz_parse );
238             free( psz_mrl );
239
240             if ( !EMPTY_STR(psz_artist) )
241                 input_item_SetArtist( p_input, psz_artist );
242             if( psz_name ) input_item_SetTitle( p_input, psz_name );
243             if( !EMPTY_STR(psz_album_art) )
244                 input_item_SetArtURL( p_input, psz_album_art );
245
246             input_item_node_AppendItem( p_subitems, p_input );
247             vlc_gc_decref( p_input );
248         }
249
250  error:
251
252         /* Fetch another line */
253         free( psz_line );
254         psz_line = stream_ReadLine( p_demux->s );
255         if( !psz_line ) b_cleanup = true;
256
257         if( b_cleanup )
258         {
259             /* Cleanup state */
260             while( i_options-- ) free( (char*)ppsz_options[i_options] );
261             FREENULL( ppsz_options );
262             i_options = 0;
263             FREENULL( psz_name );
264             FREENULL( psz_artist );
265             FREENULL( psz_album_art );
266             i_parsed_duration = 0;
267             i_duration = -1;
268
269             b_cleanup = false;
270         }
271     }
272     input_item_node_PostAndDelete( p_subitems );
273     vlc_gc_decref(p_current_input);
274     var_Destroy( p_demux, "m3u-extvlcopt" );
275     return 0; /* Needed for correct operation of go back */
276 }
277
278 static void parseEXTINF(char *psz_string, char **ppsz_artist,
279                         char **ppsz_name, int *pi_duration)
280 {
281     char *end = NULL;
282     char *psz_item = NULL;
283
284     end = psz_string + strlen( psz_string );
285
286     /* ignore whitespaces */
287     for (; psz_string < end && ( *psz_string == '\t' || *psz_string == ' ' );
288          psz_string++ );
289
290     /* duration: read to next comma */
291     psz_item = psz_string;
292     psz_string = strchr( psz_string, ',' );
293     if ( psz_string )
294     {
295         *psz_string = '\0';
296         *pi_duration = atoi( psz_item );
297     }
298     else
299     {
300         return;
301     }
302
303     if ( psz_string < end )               /* continue parsing if possible */
304         psz_string++;
305
306     /* analyse the remaining string */
307     psz_item = strstr( psz_string, " - " );
308
309     /* here we have the 0.8.2+ format with artist */
310     if ( psz_item )
311     {
312         /* *** "EXTINF:time,artist - name" */
313         *psz_item = '\0';
314         *ppsz_artist = psz_string;
315         *ppsz_name = psz_item + 3;          /* points directly after ' - ' */
316         return;
317     }
318
319     /* reaching this point means: 0.8.1- with artist or something without artist */
320     if ( *psz_string == ',' )
321     {
322         /* *** "EXTINF:time,,name" */
323         psz_string++;
324         *ppsz_name = psz_string;
325         return;
326     }
327
328     psz_item = psz_string;
329     psz_string = strchr( psz_string, ',' );
330     if ( psz_string )
331     {
332         /* *** "EXTINF:time,artist,name" */
333         *psz_string = '\0';
334         *ppsz_artist = psz_item;
335         *ppsz_name = psz_string+1;
336     }
337     else
338     {
339         /* *** "EXTINF:time,name" */
340         *ppsz_name = psz_item;
341     }
342     return;
343 }
344