]> git.sesse.net Git - vlc/blob - modules/demux/playlist/m3u.c
354e1992c6e6f25983646c740b3fb669913e9439
[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
82 /* Gruik! */
83 static inline char *MaybeFromLocaleDup (const char *str)
84 {
85     if (str == NULL)
86         return NULL;
87
88     return IsUTF8 (str) ? strdup (str) : FromLocaleDup (str);
89 }
90
91
92 static inline void MaybeFromLocaleRep (char **str)
93 {
94     char *const orig_str = *str;
95
96     if ((orig_str != NULL) && !IsUTF8 (orig_str))
97     {
98         *str = FromLocaleDup (orig_str);
99         free (orig_str);
100     }
101 }
102
103
104 static int Demux( demux_t *p_demux )
105 {
106     char       *psz_line;
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;
112     int        i_options = 0;
113     vlc_bool_t b_cleanup = VLC_FALSE;
114     input_item_t *p_input;
115
116     INIT_PLAYLIST_STUFF;
117
118     psz_line = stream_ReadLine( p_demux->s );
119     while( psz_line )
120     {
121         char *psz_parse = psz_line;
122
123         /* Skip leading tabs and spaces */
124         while( *psz_parse == ' ' || *psz_parse == '\t' ||
125                *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
126
127         if( *psz_parse == '#' )
128         {
129             /* Parse extra info */
130
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++;
135
136             if( !*psz_parse ) goto error;
137
138             if( !strncasecmp( psz_parse, "EXTINF:", sizeof("EXTINF:") -1 ) )
139             {
140                 /* Extended info */
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 * 1000000;
145                 if ( psz_name )
146                     psz_name = strdup( psz_name );
147                 if ( psz_artist )
148                     psz_artist = strdup( psz_artist );
149             }
150             else if( !strncasecmp( psz_parse, "EXTVLCOPT:",
151                                    sizeof("EXTVLCOPT:") -1 ) )
152             {
153                 /* VLC Option */
154                 char *psz_option;
155                 psz_parse += sizeof("EXTVLCOPT:") -1;
156                 if( !*psz_parse ) goto error;
157
158                 psz_option = MaybeFromLocaleDup( psz_parse );
159                 if( psz_option )
160                     INSERT_ELEM( ppsz_options, i_options, i_options,
161                                  psz_option );
162             }
163         }
164         else if( !strncasecmp( psz_parse, "RTSPtext", sizeof("RTSPtext") -1 ) )
165         {
166             ;/* special case to handle QuickTime RTSPtext redirect files */
167         }
168         else if( *psz_parse )
169         {
170             char *psz_mrl;
171             if( !psz_name || !*psz_name )
172             {
173                 /* Use filename as name for relative entries */
174                 psz_name = MaybeFromLocaleDup( psz_parse );
175             }
176
177             psz_mrl = E_(ProcessMRL)( psz_parse, p_demux->p_sys->psz_prefix );
178             MaybeFromLocaleRep( &psz_mrl );
179
180             b_cleanup = VLC_TRUE;
181             if( !psz_mrl ) goto error;
182
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 );
191             free( psz_mrl );
192         }
193
194  error:
195
196         /* Fetch another line */
197         free( psz_line );
198         psz_line = stream_ReadLine( p_demux->s );
199         if( !psz_line ) b_cleanup = VLC_TRUE;
200
201         if( b_cleanup )
202         {
203             /* Cleanup state */
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 );
208             psz_name = NULL;
209             if ( psz_artist ) free( psz_artist );
210             psz_artist = NULL;
211             i_parsed_duration = 0;
212             i_duration = -1;
213
214             b_cleanup = VLC_FALSE;
215         }
216     }
217     HANDLE_PLAY_AND_RELEASE;
218     return -1; /* Needed for correct operation of go back */
219 }
220
221 static int Control( demux_t *p_demux, int i_query, va_list args )
222 {
223     return VLC_EGENERIC;
224 }
225
226 static void parseEXTINF(char *psz_string, char **ppsz_artist,
227                         char **ppsz_name, int *pi_duration)
228 {
229     char *end = NULL;
230     char *psz_item = NULL;
231
232     end = psz_string + strlen( psz_string );
233
234     /* ignore whitespaces */
235     for (; psz_string < end && ( *psz_string == '\t' || *psz_string == ' ' ); 
236          psz_string++ );
237
238     /* duration: read to next comma */
239     psz_item = psz_string;
240     psz_string = strchr( psz_string, ',' );
241     if ( psz_string )
242     {
243         *psz_string = '\0';
244         *pi_duration = atoi( psz_item );
245     }
246     else
247     {
248         return;
249     }
250
251     if ( psz_string < end )               /* continue parsing if possible */
252         psz_string++;
253
254     /* analyse the remaining string */
255     psz_item = strstr( psz_string, " - " );
256
257     /* here we have the 0.8.2+ format with artist */
258     if ( psz_item )
259     {
260         /* *** "EXTINF:time,artist - name" */
261         *psz_item = '\0';
262         *ppsz_artist = psz_string;
263         *ppsz_name = psz_item + 3;          /* points directly after ' - ' */
264         return;
265     }
266
267     /* reaching this point means: 0.8.1- with artist or something without artist */
268     if ( *psz_string == ',' )
269     {
270         /* *** "EXTINF:time,,name" */
271         psz_string++;
272         *ppsz_name = psz_string;
273         return;
274     }
275
276     psz_item = psz_string;
277     psz_string = strchr( psz_string, ',' );
278     if ( psz_string )
279     {
280         /* *** "EXTINF:time,artist,name" */
281         *psz_string = '\0';
282         *ppsz_artist = psz_item;
283         *ppsz_name = psz_string+1;
284     }
285     else
286     {
287         /* *** "EXTINF:time,name" */
288         *ppsz_name = psz_item;
289     }
290     return;
291 }
292