]> git.sesse.net Git - vlc/blob - modules/demux/playlist/m3u.c
08ce56c66fd8e75e59442d646b8724332b987550
[vlc] / modules / demux / playlist / m3u.c
1 /*****************************************************************************
2  * m3u.c : M3U playlist format import
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Sigmund Augdal Helberg <dnumgis@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc_demux.h>
32 #include <vlc_charset.h>
33
34 #include <errno.h>                                                 /* ENOMEM */
35 #include "playlist.h"
36
37 struct demux_sys_t
38 {
39     char *psz_prefix;
40 };
41
42 /*****************************************************************************
43  * Local prototypes
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 );
48
49 /*****************************************************************************
50  * Import_M3U: main import function
51  *****************************************************************************/
52 int E_(Import_M3U)( vlc_object_t *p_this )
53 {
54     demux_t *p_demux = (demux_t *)p_this;
55     uint8_t *p_peek;
56     CHECK_PEEK( p_peek, 8 );
57
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" ) ) )
63         return VLC_EGENERIC;
64
65     STANDARD_DEMUX_INIT_MSG( "found valid M3U playlist" );
66     p_demux->p_sys->psz_prefix = E_(FindPrefix)( p_demux );
67
68     return VLC_SUCCESS;
69 }
70
71 /*****************************************************************************
72  * Deactivate: frees unused data
73  *****************************************************************************/
74 void E_(Close_M3U)( vlc_object_t *p_this )
75 {
76     demux_t *p_demux = (demux_t *)p_this;
77     free( p_demux->p_sys->psz_prefix );
78     free( p_demux->p_sys );
79 }
80
81 static int Demux( demux_t *p_demux )
82 {
83     char       *psz_line;
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;
89     int        i_options = 0, i;
90     vlc_bool_t b_cleanup = VLC_FALSE;
91     input_item_t *p_input;
92
93     INIT_PLAYLIST_STUFF;
94
95     psz_line = stream_ReadLine( p_demux->s );
96     while( psz_line )
97     {
98         char *psz_parse = psz_line;
99
100         /* Skip leading tabs and spaces */
101         while( *psz_parse == ' ' || *psz_parse == '\t' ||
102                *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
103
104         if( *psz_parse == '#' )
105         {
106             /* Parse extra info */
107
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++;
112
113             if( !*psz_parse ) goto error;
114
115             if( !strncasecmp( psz_parse, "EXTINF:", sizeof("EXTINF:") -1 ) )
116             {
117                 /* Extended info */
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;
122                 if ( psz_name )
123                     psz_name = strdup( psz_name );
124                 if ( psz_artist )
125                     psz_artist = strdup( psz_artist );
126             }
127             else if( !strncasecmp( psz_parse, "EXTVLCOPT:",
128                                    sizeof("EXTVLCOPT:") -1 ) )
129             {
130                 /* VLC Option */
131                 const char *psz_option;
132                 psz_parse += sizeof("EXTVLCOPT:") -1;
133                 if( !*psz_parse ) goto error;
134
135                 psz_option = strdup( psz_parse );
136                 if( psz_option )
137                     INSERT_ELEM( ppsz_options, i_options, i_options,
138                                  psz_option );
139             }
140         }
141         else if( !strncasecmp( psz_parse, "RTSPtext", sizeof("RTSPtext") -1 ) )
142         {
143             ;/* special case to handle QuickTime RTSPtext redirect files */
144         }
145         else if( *psz_parse )
146         {
147             char *psz_mrl;
148             if( !psz_name || !*psz_name )
149             {
150                 /* Use filename as name for relative entries */
151                 psz_name = strdup( psz_parse );
152             }
153
154             psz_mrl = E_(ProcessMRL)( psz_parse, p_demux->p_sys->psz_prefix );
155
156             b_cleanup = VLC_TRUE;
157             if( !psz_mrl ) goto error;
158
159             EnsureUTF8( psz_name );
160             EnsureUTF8( psz_mrl );
161
162             for( i = 0; i< i_options; i++ )
163                 EnsureUTF8( (char*)ppsz_options[i] );
164
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 );
173             free( psz_mrl );
174         }
175
176  error:
177
178         /* Fetch another line */
179         free( psz_line );
180         psz_line = stream_ReadLine( p_demux->s );
181         if( !psz_line ) b_cleanup = VLC_TRUE;
182
183         if( b_cleanup )
184         {
185             /* Cleanup state */
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 );
190             psz_name = NULL;
191             if ( psz_artist ) free( psz_artist );
192             psz_artist = NULL;
193             i_parsed_duration = 0;
194             i_duration = -1;
195
196             b_cleanup = VLC_FALSE;
197         }
198     }
199     HANDLE_PLAY_AND_RELEASE;
200     return -1; /* Needed for correct operation of go back */
201 }
202
203 static int Control( demux_t *p_demux, int i_query, va_list args )
204 {
205     return VLC_EGENERIC;
206 }
207
208 static void parseEXTINF(char *psz_string, char **ppsz_artist,
209                         char **ppsz_name, int *pi_duration)
210 {
211     char *end = NULL;
212     char *psz_item = NULL;
213
214     end = psz_string + strlen( psz_string );
215
216     /* ignore whitespaces */
217     for (; psz_string < end && ( *psz_string == '\t' || *psz_string == ' ' ); 
218          psz_string++ );
219
220     /* duration: read to next comma */
221     psz_item = psz_string;
222     psz_string = strchr( psz_string, ',' );
223     if ( psz_string )
224     {
225         *psz_string = '\0';
226         *pi_duration = atoi( psz_item );
227     }
228     else
229     {
230         return;
231     }
232
233     if ( psz_string < end )               /* continue parsing if possible */
234         psz_string++;
235
236     /* analyse the remaining string */
237     psz_item = strstr( psz_string, " - " );
238
239     /* here we have the 0.8.2+ format with artist */
240     if ( psz_item )
241     {
242         /* *** "EXTINF:time,artist - name" */
243         *psz_item = '\0';
244         *ppsz_artist = psz_string;
245         *ppsz_name = psz_item + 3;          /* points directly after ' - ' */
246         return;
247     }
248
249     /* reaching this point means: 0.8.1- with artist or something without artist */
250     if ( *psz_string == ',' )
251     {
252         /* *** "EXTINF:time,,name" */
253         psz_string++;
254         *ppsz_name = psz_string;
255         return;
256     }
257
258     psz_item = psz_string;
259     psz_string = strchr( psz_string, ',' );
260     if ( psz_string )
261     {
262         /* *** "EXTINF:time,artist,name" */
263         *psz_string = '\0';
264         *ppsz_artist = psz_item;
265         *ppsz_name = psz_string+1;
266     }
267     else
268     {
269         /* *** "EXTINF:time,name" */
270         *ppsz_name = psz_item;
271     }
272     return;
273 }
274