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 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc_demux.h>
32 #include <vlc_charset.h>
34 #include <errno.h> /* ENOMEM */
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Demux( demux_t *p_demux);
46 static int Control( demux_t *p_demux, int i_query, va_list args );
47 static void parseEXTINF( char *psz_string, char **ppsz_artist, char **ppsz_name, int *pi_duration );
49 /*****************************************************************************
50 * Import_M3U: main import function
51 *****************************************************************************/
52 int E_(Import_M3U)( vlc_object_t *p_this )
54 demux_t *p_demux = (demux_t *)p_this;
56 CHECK_PEEK( p_peek, 8 );
58 if(! ( POKE( p_peek, "#EXTM3U", 7 ) || POKE( p_peek, "RTSPtext", 8 ) ||
59 isExtension( p_demux, ".m3u" ) || isExtension( p_demux, ".vlc" ) ||
60 /* A .ram file can contain a single rtsp link */
61 isExtension( p_demux, ".ram" ) || isExtension( p_demux, ".rm" ) ||
62 isDemux( p_demux, "m3u" ) ) )
65 STANDARD_DEMUX_INIT_MSG( "found valid M3U playlist" );
66 p_demux->p_sys->psz_prefix = E_(FindPrefix)( p_demux );
71 /*****************************************************************************
72 * Deactivate: frees unused data
73 *****************************************************************************/
74 void E_(Close_M3U)( vlc_object_t *p_this )
76 demux_t *p_demux = (demux_t *)p_this;
77 free( p_demux->p_sys->psz_prefix );
78 free( p_demux->p_sys );
83 static inline char *MaybeFromLocaleDup (const char *str)
88 return IsUTF8 (str) ? strdup (str) : FromLocaleDup (str);
92 static inline void MaybeFromLocaleRep (char **str)
94 char *const orig_str = *str;
96 if ((orig_str != NULL) && !IsUTF8 (orig_str))
98 *str = FromLocaleDup (orig_str);
104 static int Demux( demux_t *p_demux )
107 char *psz_name = NULL;
108 char *psz_artist = NULL;
109 int i_parsed_duration = 0;
110 mtime_t i_duration = -1;
111 const char**ppsz_options = NULL;
113 vlc_bool_t b_cleanup = VLC_FALSE;
114 input_item_t *p_input;
118 psz_line = stream_ReadLine( p_demux->s );
121 char *psz_parse = psz_line;
123 /* Skip leading tabs and spaces */
124 while( *psz_parse == ' ' || *psz_parse == '\t' ||
125 *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
127 if( *psz_parse == '#' )
129 /* Parse extra info */
131 /* Skip leading tabs and spaces */
132 while( *psz_parse == ' ' || *psz_parse == '\t' ||
133 *psz_parse == '\n' || *psz_parse == '\r' ||
134 *psz_parse == '#' ) psz_parse++;
136 if( !*psz_parse ) goto error;
138 if( !strncasecmp( psz_parse, "EXTINF:", sizeof("EXTINF:") -1 ) )
141 psz_parse += sizeof("EXTINF:") - 1;
142 parseEXTINF( psz_parse, &psz_artist, &psz_name, &i_parsed_duration );
143 if( i_parsed_duration >= 0 )
144 i_duration = i_parsed_duration * I64C(1000000);
146 psz_name = strdup( psz_name );
148 psz_artist = strdup( psz_artist );
150 else if( !strncasecmp( psz_parse, "EXTVLCOPT:",
151 sizeof("EXTVLCOPT:") -1 ) )
155 psz_parse += sizeof("EXTVLCOPT:") -1;
156 if( !*psz_parse ) goto error;
158 psz_option = MaybeFromLocaleDup( psz_parse );
160 INSERT_ELEM( ppsz_options, i_options, i_options,
164 else if( !strncasecmp( psz_parse, "RTSPtext", sizeof("RTSPtext") -1 ) )
166 ;/* special case to handle QuickTime RTSPtext redirect files */
168 else if( *psz_parse )
171 if( !psz_name || !*psz_name )
173 /* Use filename as name for relative entries */
174 psz_name = MaybeFromLocaleDup( psz_parse );
177 psz_mrl = E_(ProcessMRL)( psz_parse, p_demux->p_sys->psz_prefix );
178 MaybeFromLocaleRep( &psz_mrl );
180 b_cleanup = VLC_TRUE;
181 if( !psz_mrl ) goto error;
183 p_input = input_ItemNewExt( p_playlist, psz_mrl, psz_name,
184 i_options, ppsz_options, i_duration );
185 if ( psz_artist && *psz_artist )
186 input_ItemAddInfo( p_input, _(VLC_META_INFO_CAT),
187 _(VLC_META_ARTIST), "%s", psz_artist );
188 playlist_BothAddInput( p_playlist, p_input, p_item_in_category,
189 PLAYLIST_APPEND | PLAYLIST_SPREPARSE,
190 PLAYLIST_END, NULL, NULL, VLC_FALSE );
196 /* Fetch another line */
198 psz_line = stream_ReadLine( p_demux->s );
199 if( !psz_line ) b_cleanup = VLC_TRUE;
204 while( i_options-- ) free( (char*)ppsz_options[i_options] );
205 if( ppsz_options ) free( ppsz_options );
206 ppsz_options = NULL; i_options = 0;
207 if( psz_name ) free( psz_name );
209 if ( psz_artist ) free( psz_artist );
211 i_parsed_duration = 0;
214 b_cleanup = VLC_FALSE;
217 HANDLE_PLAY_AND_RELEASE;
218 return -1; /* Needed for correct operation of go back */
221 static int Control( demux_t *p_demux, int i_query, va_list args )
226 static void parseEXTINF(char *psz_string, char **ppsz_artist,
227 char **ppsz_name, int *pi_duration)
230 char *psz_item = NULL;
232 end = psz_string + strlen( psz_string );
234 /* ignore whitespaces */
235 for (; psz_string < end && ( *psz_string == '\t' || *psz_string == ' ' );
238 /* duration: read to next comma */
239 psz_item = psz_string;
240 psz_string = strchr( psz_string, ',' );
244 *pi_duration = atoi( psz_item );
251 if ( psz_string < end ) /* continue parsing if possible */
254 /* analyse the remaining string */
255 psz_item = strstr( psz_string, " - " );
257 /* here we have the 0.8.2+ format with artist */
260 /* *** "EXTINF:time,artist - name" */
262 *ppsz_artist = psz_string;
263 *ppsz_name = psz_item + 3; /* points directly after ' - ' */
267 /* reaching this point means: 0.8.1- with artist or something without artist */
268 if ( *psz_string == ',' )
270 /* *** "EXTINF:time,,name" */
272 *ppsz_name = psz_string;
276 psz_item = psz_string;
277 psz_string = strchr( psz_string, ',' );
280 /* *** "EXTINF:time,artist,name" */
282 *ppsz_artist = psz_item;
283 *ppsz_name = psz_string+1;
287 /* *** "EXTINF:time,name" */
288 *ppsz_name = psz_item;