1 /*****************************************************************************
2 * m3u.c : M3U playlist format import
3 *****************************************************************************
4 * Copyright (C) 2004 VLC authors and VideoLAN
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Sigmund Augdal Helberg <dnumgis@videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
35 #include <vlc_charset.h>
42 char *(*pf_dup) (const char *);
45 /*****************************************************************************
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 );
52 static char *GuessEncoding (const char *str)
54 return IsUTF8 (str) ? strdup (str) : FromLatin1 (str);
57 static char *CheckUnicode (const char *str)
59 return IsUTF8 (str) ? strdup (str): NULL;
62 /*****************************************************************************
63 * Import_M3U: main import function
64 *****************************************************************************/
65 int Import_M3U( vlc_object_t *p_this )
67 demux_t *p_demux = (demux_t *)p_this;
68 const uint8_t *p_peek;
69 char *(*pf_dup) (const char *) = GuessEncoding;
72 if( stream_Peek( p_demux->s, &p_peek, 3 ) == 3
73 && !memcmp( p_peek, "\xef\xbb\xbf", 3) )
75 pf_dup = CheckUnicode; /* UTF-8 Byte Order Mark */
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 */
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 */
92 if( stream_Peek( p_demux->s, &p_peek, 8 + offset ) < (8 + offset) )
97 if( !strncasecmp( (const char *)p_peek, "RTSPtext", 8 ) ) /* QuickTime */
98 pf_dup = CheckUnicode; /* UTF-8 */
100 if( !memcmp( p_peek, "#EXTM3U", 7 ) )
101 ; /* Guess encoding */
106 stream_Seek( p_demux->s, offset );
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;
115 static bool ContainsURL( demux_t *p_demux )
117 const uint8_t *p_peek, *p_peek_end;
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;
124 while( p_peek + sizeof( "https://" ) < p_peek_end )
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 ) )
137 /* Comments and blank lines are ignored */
138 else if( *p_peek != '#' && *p_peek != '\n' && *p_peek != '\r')
143 while( p_peek < p_peek_end && *p_peek != '\n' )
145 if ( *p_peek == '\n' )
151 /*****************************************************************************
152 * Deactivate: frees unused data
153 *****************************************************************************/
154 void Close_M3U( vlc_object_t *p_this )
156 demux_t *p_demux = (demux_t *)p_this;
157 free( p_demux->p_sys->psz_prefix );
158 free( p_demux->p_sys );
162 static int Demux( demux_t *p_demux )
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;
173 bool b_cleanup = false;
174 input_item_t *p_input;
176 input_item_t *p_current_input = GetCurrentItem(p_demux);
177 input_item_node_t *p_subitems = input_item_node_Create( p_current_input );
179 psz_line = stream_ReadLine( p_demux->s );
182 char *psz_parse = psz_line;
184 /* Skip leading tabs and spaces */
185 while( *psz_parse == ' ' || *psz_parse == '\t' ||
186 *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
188 if( *psz_parse == '#' )
190 /* Parse extra info */
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++;
197 if( !*psz_parse ) goto error;
199 if( !strncasecmp( psz_parse, "EXTINF:", sizeof("EXTINF:") -1 ) )
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);
209 psz_name = pf_dup( psz_name );
211 psz_artist = pf_dup( psz_artist );
213 else if( !strncasecmp( psz_parse, "EXTVLCOPT:",
214 sizeof("EXTVLCOPT:") -1 ) )
218 psz_parse += sizeof("EXTVLCOPT:") -1;
219 if( !*psz_parse ) goto error;
221 psz_option = pf_dup( psz_parse );
223 INSERT_ELEM( ppsz_options, i_options, i_options,
226 /* Special case for jamendo which provide the albumart */
227 else if( !strncasecmp( psz_parse, "EXTALBUMARTURL:",
228 sizeof( "EXTALBUMARTURL:" ) -1 ) )
230 psz_parse += sizeof( "EXTALBUMARTURL:" ) - 1;
231 free( psz_album_art );
232 psz_album_art = pf_dup( psz_parse );
235 else if( !strncasecmp( psz_parse, "RTSPtext", sizeof("RTSPtext") -1 ) )
237 ;/* special case to handle QuickTime RTSPtext redirect files */
239 else if( *psz_parse )
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 );
248 psz_mrl = ProcessMRL( psz_parse, p_demux->p_sys->psz_prefix );
257 p_input = input_item_NewExt( psz_mrl, psz_name,
258 i_options, ppsz_options, 0, i_duration );
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 );
269 input_item_node_AppendItem( p_subitems, p_input );
270 vlc_gc_decref( p_input );
275 /* Fetch another line */
277 psz_line = stream_ReadLine( p_demux->s );
278 if( !psz_line ) b_cleanup = true;
283 while( i_options-- ) free( (char*)ppsz_options[i_options] );
284 FREENULL( ppsz_options );
286 FREENULL( psz_name );
287 FREENULL( psz_artist );
288 FREENULL( psz_album_art );
289 i_parsed_duration = 0;
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 */
301 static void parseEXTINF(char *psz_string, char **ppsz_artist,
302 char **ppsz_name, int *pi_duration)
305 char *psz_item = NULL;
307 end = psz_string + strlen( psz_string );
309 /* ignore whitespaces */
310 for (; psz_string < end && ( *psz_string == '\t' || *psz_string == ' ' );
313 /* duration: read to next comma */
314 psz_item = psz_string;
315 psz_string = strchr( psz_string, ',' );
319 *pi_duration = atoi( psz_item );
326 if ( psz_string < end ) /* continue parsing if possible */
329 /* analyse the remaining string */
330 psz_item = strstr( psz_string, " - " );
332 /* here we have the 0.8.2+ format with artist */
335 /* *** "EXTINF:time,artist - name" */
337 *ppsz_artist = psz_string;
338 *ppsz_name = psz_item + 3; /* points directly after ' - ' */
342 /* reaching this point means: 0.8.1- with artist or something without artist */
343 if ( *psz_string == ',' )
345 /* *** "EXTINF:time,,name" */
347 *ppsz_name = psz_string;
351 psz_item = psz_string;
352 psz_string = strchr( psz_string, ',' );
355 /* *** "EXTINF:time,artist,name" */
357 *ppsz_artist = psz_item;
358 *ppsz_name = psz_string+1;
362 /* *** "EXTINF:time,name" */
363 *ppsz_name = psz_item;