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/input.h>
35 #include <errno.h> /* ENOMEM */
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Demux( demux_t *p_demux);
47 static int Control( demux_t *p_demux, int i_query, va_list args );
48 static void parseEXTINF( char *psz_string, char **ppsz_artist, char **ppsz_name, int *pi_duration );
50 /*****************************************************************************
51 * Import_M3U: main import function
52 *****************************************************************************/
53 int E_(Import_M3U)( vlc_object_t *p_this )
55 demux_t *p_demux = (demux_t *)p_this;
57 CHECK_PEEK( p_peek, 8 );
59 if( POKE( p_peek, "#EXTM3U", 7 ) || POKE( p_peek, "RTSPtext", 8 ) ||
60 isExtension( p_demux, ".m3u" ) || isExtension( p_demux, ".vlc" ) ||
61 /* A .ram file can contain a single rtsp link */
62 isExtension( p_demux, ".ram" ) || isExtension( p_demux, ".rm" ) ||
63 isDemux( p_demux, "m3u" ) )
70 STANDARD_DEMUX_INIT_MSG( "found valid M3U playlist" );
71 p_demux->p_sys->psz_prefix = E_(FindPrefix)( p_demux );
76 /*****************************************************************************
77 * Deactivate: frees unused data
78 *****************************************************************************/
79 void E_(Close_M3U)( vlc_object_t *p_this )
81 demux_t *p_demux = (demux_t *)p_this;
83 if( p_demux->p_sys->psz_prefix ) free( p_demux->p_sys->psz_prefix );
84 free( p_demux->p_sys );
87 static int Demux( demux_t *p_demux )
90 char *psz_name = NULL;
91 char *psz_artist = NULL;
92 int i_parsed_duration = 0;
93 mtime_t i_duration = -1;
94 const char**ppsz_options = NULL;
96 vlc_bool_t b_cleanup = VLC_FALSE;
100 psz_line = stream_ReadLine( p_demux->s );
103 char *psz_parse = psz_line;
105 /* Skip leading tabs and spaces */
106 while( *psz_parse == ' ' || *psz_parse == '\t' ||
107 *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
109 if( *psz_parse == '#' )
111 /* Parse extra info */
113 /* Skip leading tabs and spaces */
114 while( *psz_parse == ' ' || *psz_parse == '\t' ||
115 *psz_parse == '\n' || *psz_parse == '\r' ||
116 *psz_parse == '#' ) psz_parse++;
118 if( !*psz_parse ) goto error;
120 if( !strncasecmp( psz_parse, "EXTINF:", sizeof("EXTINF:") -1 ) )
123 psz_parse += sizeof("EXTINF:") - 1;
124 parseEXTINF( psz_parse, &psz_artist, &psz_name, &i_parsed_duration );
125 if ( i_parsed_duration >= 0 )
126 i_duration = i_parsed_duration * 1000000;
128 psz_name = strdup( psz_name );
130 psz_artist = strdup( psz_artist );
132 else if( !strncasecmp( psz_parse, "EXTVLCOPT:",
133 sizeof("EXTVLCOPT:") -1 ) )
136 const char *psz_option;
137 psz_parse += sizeof("EXTVLCOPT:") -1;
138 if( !*psz_parse ) goto error;
140 psz_option = strdup( psz_parse );
142 INSERT_ELEM( ppsz_options, i_options, i_options,
146 else if( !strncasecmp( psz_parse, "RTSPtext", sizeof("RTSPtext") -1 ) )
148 ;/* special case to handle QuickTime RTSPtext redirect files */
150 else if( *psz_parse )
153 if( !psz_name || !*psz_name )
155 /* Use filename as name for relative entries */
156 psz_name = strdup( psz_parse );
159 psz_mrl = E_(ProcessMRL)( psz_parse, p_demux->p_sys->psz_prefix );
161 b_cleanup = VLC_TRUE;
162 if( !psz_mrl ) goto error;
164 EnsureUTF8( psz_name );
165 EnsureUTF8( psz_mrl );
167 for( i = 0; i< i_options; i++ )
168 EnsureUTF8( (char*)ppsz_options[i] );
170 p_input = input_ItemNewExt( p_playlist, psz_mrl, psz_name,
171 i_options, ppsz_options, i_duration );
172 if ( psz_artist && *psz_artist )
173 vlc_input_item_AddInfo( p_input, _(VLC_META_INFO_CAT),
174 _(VLC_META_ARTIST), "%s", psz_artist );
175 msg_Dbg( p_demux, "Adding %s\n", p_input->psz_uri );
176 playlist_AddWhereverNeeded( p_playlist, p_input, p_current,
177 p_item_in_category, (i_parent_id > 0 )? VLC_TRUE : VLC_FALSE,
184 /* Fetch another line */
186 psz_line = stream_ReadLine( p_demux->s );
187 if( !psz_line ) b_cleanup = VLC_TRUE;
192 while( i_options-- ) free( (char*)ppsz_options[i_options] );
193 if( ppsz_options ) free( ppsz_options );
194 ppsz_options = NULL; i_options = 0;
195 if( psz_name ) free( psz_name );
197 if ( psz_artist ) free( psz_artist );
199 i_parsed_duration = 0;
202 b_cleanup = VLC_FALSE;
205 HANDLE_PLAY_AND_RELEASE;
209 static int Control( demux_t *p_demux, int i_query, va_list args )
214 static void parseEXTINF(char *psz_string, char **ppsz_artist,
215 char **ppsz_name, int *pi_duration)
218 char *psz_item = NULL;
220 end = psz_string + strlen( psz_string );
222 /* ignore whitespaces */
223 for (; psz_string < end && ( *psz_string == '\t' || *psz_string == ' ' );
226 /* duration: read to next comma */
227 psz_item = psz_string;
228 psz_string = strchr( psz_string, ',' );
232 *pi_duration = atoi( psz_item );
239 if ( psz_string < end ) /* continue parsing if possible */
242 /* analyse the remaining string */
243 psz_item = strstr( psz_string, " - " );
245 /* here we have the 0.8.2+ format with artist */
248 /* *** "EXTINF:time,artist - name" */
250 *ppsz_artist = psz_string;
251 *ppsz_name = psz_item + 3; /* points directly after ' - ' */
255 /* reaching this point means: 0.8.1- with artist or something without artist */
256 if ( *psz_string == ',' )
258 /* *** "EXTINF:time,,name" */
260 *ppsz_name = psz_string;
264 psz_item = psz_string;
265 psz_string = strchr( psz_string, ',' );
268 /* *** "EXTINF:time,artist,name" */
270 *ppsz_artist = psz_item;
271 *ppsz_name = psz_string+1;
275 /* *** "EXTINF:time,name" */
276 *ppsz_name = psz_item;