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 );
81 static int Demux( demux_t *p_demux )
84 char *psz_name = NULL;
85 char *psz_artist = NULL;
86 int i_parsed_duration = 0;
87 mtime_t i_duration = -1;
88 const char**ppsz_options = NULL;
90 vlc_bool_t b_cleanup = VLC_FALSE;
91 input_item_t *p_input;
95 psz_line = stream_ReadLine( p_demux->s );
98 char *psz_parse = psz_line;
100 /* Skip leading tabs and spaces */
101 while( *psz_parse == ' ' || *psz_parse == '\t' ||
102 *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
104 if( *psz_parse == '#' )
106 /* Parse extra info */
108 /* Skip leading tabs and spaces */
109 while( *psz_parse == ' ' || *psz_parse == '\t' ||
110 *psz_parse == '\n' || *psz_parse == '\r' ||
111 *psz_parse == '#' ) psz_parse++;
113 if( !*psz_parse ) goto error;
115 if( !strncasecmp( psz_parse, "EXTINF:", sizeof("EXTINF:") -1 ) )
118 psz_parse += sizeof("EXTINF:") - 1;
119 parseEXTINF( psz_parse, &psz_artist, &psz_name, &i_parsed_duration );
120 if ( i_parsed_duration >= 0 )
121 i_duration = i_parsed_duration * 1000000;
123 psz_name = strdup( psz_name );
125 psz_artist = strdup( psz_artist );
127 else if( !strncasecmp( psz_parse, "EXTVLCOPT:",
128 sizeof("EXTVLCOPT:") -1 ) )
131 const char *psz_option;
132 psz_parse += sizeof("EXTVLCOPT:") -1;
133 if( !*psz_parse ) goto error;
135 psz_option = strdup( psz_parse );
137 INSERT_ELEM( ppsz_options, i_options, i_options,
141 else if( !strncasecmp( psz_parse, "RTSPtext", sizeof("RTSPtext") -1 ) )
143 ;/* special case to handle QuickTime RTSPtext redirect files */
145 else if( *psz_parse )
148 if( !psz_name || !*psz_name )
150 /* Use filename as name for relative entries */
151 psz_name = strdup( psz_parse );
154 psz_mrl = E_(ProcessMRL)( psz_parse, p_demux->p_sys->psz_prefix );
156 b_cleanup = VLC_TRUE;
157 if( !psz_mrl ) goto error;
159 EnsureUTF8( psz_name );
160 EnsureUTF8( psz_mrl );
162 for( i = 0; i< i_options; i++ )
163 EnsureUTF8( (char*)ppsz_options[i] );
165 p_input = input_ItemNewExt( p_playlist, psz_mrl, psz_name,
166 i_options, ppsz_options, i_duration );
167 if ( psz_artist && *psz_artist )
168 input_ItemAddInfo( p_input, _(VLC_META_INFO_CAT),
169 _(VLC_META_ARTIST), "%s", psz_artist );
170 playlist_BothAddInput( p_playlist, p_input, p_item_in_category,
171 PLAYLIST_APPEND | PLAYLIST_SPREPARSE,
172 PLAYLIST_END, NULL, NULL );
178 /* Fetch another line */
180 psz_line = stream_ReadLine( p_demux->s );
181 if( !psz_line ) b_cleanup = VLC_TRUE;
186 while( i_options-- ) free( (char*)ppsz_options[i_options] );
187 if( ppsz_options ) free( ppsz_options );
188 ppsz_options = NULL; i_options = 0;
189 if( psz_name ) free( psz_name );
191 if ( psz_artist ) free( psz_artist );
193 i_parsed_duration = 0;
196 b_cleanup = VLC_FALSE;
199 HANDLE_PLAY_AND_RELEASE;
200 return -1; /* Needed for correct operation of go back */
203 static int Control( demux_t *p_demux, int i_query, va_list args )
208 static void parseEXTINF(char *psz_string, char **ppsz_artist,
209 char **ppsz_name, int *pi_duration)
212 char *psz_item = NULL;
214 end = psz_string + strlen( psz_string );
216 /* ignore whitespaces */
217 for (; psz_string < end && ( *psz_string == '\t' || *psz_string == ' ' );
220 /* duration: read to next comma */
221 psz_item = psz_string;
222 psz_string = strchr( psz_string, ',' );
226 *pi_duration = atoi( psz_item );
233 if ( psz_string < end ) /* continue parsing if possible */
236 /* analyse the remaining string */
237 psz_item = strstr( psz_string, " - " );
239 /* here we have the 0.8.2+ format with artist */
242 /* *** "EXTINF:time,artist - name" */
244 *ppsz_artist = psz_string;
245 *ppsz_name = psz_item + 3; /* points directly after ' - ' */
249 /* reaching this point means: 0.8.1- with artist or something without artist */
250 if ( *psz_string == ',' )
252 /* *** "EXTINF:time,,name" */
254 *ppsz_name = psz_string;
258 psz_item = psz_string;
259 psz_string = strchr( psz_string, ',' );
262 /* *** "EXTINF:time,artist,name" */
264 *ppsz_artist = psz_item;
265 *ppsz_name = psz_string+1;
269 /* *** "EXTINF:time,name" */
270 *ppsz_name = psz_item;