1 /*****************************************************************************
2 * m3u.c : M3U playlist format import
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
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 CHECK_PEEK( p_peek, 8 );
70 char *(*pf_dup) (const char *);
72 if( POKE( p_peek, "RTSPtext", 8 ) /* QuickTime */
73 || demux_IsPathExtension( p_demux, ".m3u8" )
74 || demux_IsForced( p_demux, "m3u8" ) )
75 pf_dup = CheckUnicode; /* UTF-8 */
77 if( POKE( p_peek, "#EXTM3U", 7 )
78 || demux_IsPathExtension( p_demux, ".m3u" )
79 || demux_IsPathExtension( p_demux, ".vlc" )
80 || demux_IsForced( p_demux, "m3u" )
81 || ContainsURL( p_demux ) )
82 pf_dup = GuessEncoding;
86 STANDARD_DEMUX_INIT_MSG( "found valid M3U playlist" );
87 p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
88 p_demux->p_sys->pf_dup = pf_dup;
93 static bool ContainsURL( demux_t *p_demux )
95 const uint8_t *p_peek, *p_peek_end;
98 i_peek = stream_Peek( p_demux->s, &p_peek, 1024 );
99 if( i_peek <= 0 ) return false;
100 p_peek_end = p_peek + i_peek;
102 while( p_peek + sizeof( "https://" ) < p_peek_end )
104 /* One line starting with a URL is enough */
105 if( !strncasecmp( (const char *)p_peek, "http://", 7 ) ||
106 !strncasecmp( (const char *)p_peek, "mms://", 6 ) ||
107 !strncasecmp( (const char *)p_peek, "rtsp://", 7 ) ||
108 !strncasecmp( (const char *)p_peek, "https://", 8 ) ||
109 !strncasecmp( (const char *)p_peek, "ftp://", 6 ) )
113 /* Comments and blank lines are ignored */
114 else if( *p_peek != '#' && *p_peek != '\n' && *p_peek != '\r')
119 while( p_peek < p_peek_end && *p_peek != '\n' )
121 if ( *p_peek == '\n' )
127 /*****************************************************************************
128 * Deactivate: frees unused data
129 *****************************************************************************/
130 void Close_M3U( vlc_object_t *p_this )
132 demux_t *p_demux = (demux_t *)p_this;
133 free( p_demux->p_sys->psz_prefix );
134 free( p_demux->p_sys );
138 static int Demux( demux_t *p_demux )
141 char *psz_name = NULL;
142 char *psz_artist = NULL;
143 char *psz_album_art = NULL;
144 int i_parsed_duration = 0;
145 mtime_t i_duration = -1;
146 const char**ppsz_options = NULL;
147 char * (*pf_dup) (const char *) = p_demux->p_sys->pf_dup;
149 bool b_cleanup = false;
150 input_item_t *p_input;
152 input_item_t *p_current_input = GetCurrentItem(p_demux);
153 input_item_node_t *p_subitems = input_item_node_Create( p_current_input );
155 psz_line = stream_ReadLine( p_demux->s );
158 char *psz_parse = psz_line;
160 /* Skip leading tabs and spaces */
161 while( *psz_parse == ' ' || *psz_parse == '\t' ||
162 *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
164 if( *psz_parse == '#' )
166 /* Parse extra info */
168 /* Skip leading tabs and spaces */
169 while( *psz_parse == ' ' || *psz_parse == '\t' ||
170 *psz_parse == '\n' || *psz_parse == '\r' ||
171 *psz_parse == '#' ) psz_parse++;
173 if( !*psz_parse ) goto error;
175 if( !strncasecmp( psz_parse, "EXTINF:", sizeof("EXTINF:") -1 ) )
178 psz_parse += sizeof("EXTINF:") - 1;
181 parseEXTINF( psz_parse, &psz_artist, &psz_name, &i_parsed_duration );
182 if( i_parsed_duration >= 0 )
183 i_duration = i_parsed_duration * INT64_C(1000000);
185 psz_name = pf_dup( psz_name );
187 psz_artist = pf_dup( psz_artist );
189 else if( !strncasecmp( psz_parse, "EXTVLCOPT:",
190 sizeof("EXTVLCOPT:") -1 ) )
194 psz_parse += sizeof("EXTVLCOPT:") -1;
195 if( !*psz_parse ) goto error;
197 psz_option = pf_dup( psz_parse );
199 INSERT_ELEM( ppsz_options, i_options, i_options,
202 /* Special case for jamendo which provide the albumart */
203 else if( !strncasecmp( psz_parse, "EXTALBUMARTURL:",
204 sizeof( "EXTALBUMARTURL:" ) -1 ) )
206 psz_parse += sizeof( "EXTALBUMARTURL:" ) - 1;
207 free( psz_album_art );
208 psz_album_art = pf_dup( psz_parse );
211 else if( !strncasecmp( psz_parse, "RTSPtext", sizeof("RTSPtext") -1 ) )
213 ;/* special case to handle QuickTime RTSPtext redirect files */
215 else if( *psz_parse )
219 psz_parse = pf_dup( psz_parse );
220 if( !psz_name && psz_parse )
221 /* Use filename as name for relative entries */
222 psz_name = strdup( psz_parse );
224 psz_mrl = ProcessMRL( psz_parse, p_demux->p_sys->psz_prefix );
233 p_input = input_item_NewExt( psz_mrl, psz_name,
234 i_options, ppsz_options, 0, i_duration );
239 if ( !EMPTY_STR(psz_artist) )
240 input_item_SetArtist( p_input, psz_artist );
241 if( psz_name ) input_item_SetTitle( p_input, psz_name );
242 if( !EMPTY_STR(psz_album_art) )
243 input_item_SetArtURL( p_input, psz_album_art );
245 input_item_node_AppendItem( p_subitems, p_input );
246 vlc_gc_decref( p_input );
251 /* Fetch another line */
253 psz_line = stream_ReadLine( p_demux->s );
254 if( !psz_line ) b_cleanup = true;
259 while( i_options-- ) free( (char*)ppsz_options[i_options] );
260 FREENULL( ppsz_options );
262 FREENULL( psz_name );
263 FREENULL( psz_artist );
264 FREENULL( psz_album_art );
265 i_parsed_duration = 0;
271 input_item_node_PostAndDelete( p_subitems );
272 vlc_gc_decref(p_current_input);
273 var_Destroy( p_demux, "m3u-extvlcopt" );
274 return 0; /* Needed for correct operation of go back */
277 static void parseEXTINF(char *psz_string, char **ppsz_artist,
278 char **ppsz_name, int *pi_duration)
281 char *psz_item = NULL;
283 end = psz_string + strlen( psz_string );
285 /* ignore whitespaces */
286 for (; psz_string < end && ( *psz_string == '\t' || *psz_string == ' ' );
289 /* duration: read to next comma */
290 psz_item = psz_string;
291 psz_string = strchr( psz_string, ',' );
295 *pi_duration = atoi( psz_item );
302 if ( psz_string < end ) /* continue parsing if possible */
305 /* analyse the remaining string */
306 psz_item = strstr( psz_string, " - " );
308 /* here we have the 0.8.2+ format with artist */
311 /* *** "EXTINF:time,artist - name" */
313 *ppsz_artist = psz_string;
314 *ppsz_name = psz_item + 3; /* points directly after ' - ' */
318 /* reaching this point means: 0.8.1- with artist or something without artist */
319 if ( *psz_string == ',' )
321 /* *** "EXTINF:time,,name" */
323 *ppsz_name = psz_string;
327 psz_item = psz_string;
328 psz_string = strchr( psz_string, ',' );
331 /* *** "EXTINF:time,artist,name" */
333 *ppsz_artist = psz_item;
334 *ppsz_name = psz_string+1;
338 /* *** "EXTINF:time,name" */
339 *ppsz_name = psz_item;