]> git.sesse.net Git - vlc/blob - modules/demux/playlist/m3u.c
modules/demux/playlist/*.c: Use the new playlist independant way to announce sub...
[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            demux2_IsPathExtension( p_demux, ".m3u" ) || demux2_IsPathExtension( p_demux, ".vlc" ) ||
60            /* A .ram file can contain a single rtsp link */
61            demux2_IsPathExtension( p_demux, ".ram" ) || demux2_IsPathExtension( p_demux, ".rm" ) ||
62            demux2_IsForced( 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 * I64C(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             input_ItemAddSubItem( p_current_input, p_input );
189             free( psz_mrl );
190         }
191
192  error:
193
194         /* Fetch another line */
195         free( psz_line );
196         psz_line = stream_ReadLine( p_demux->s );
197         if( !psz_line ) b_cleanup = VLC_TRUE;
198
199         if( b_cleanup )
200         {
201             /* Cleanup state */
202             while( i_options-- ) free( (char*)ppsz_options[i_options] );
203             if( ppsz_options ) free( ppsz_options );
204             ppsz_options = NULL; i_options = 0;
205             if( psz_name ) free( psz_name );
206             psz_name = NULL;
207             if ( psz_artist ) free( psz_artist );
208             psz_artist = NULL;
209             i_parsed_duration = 0;
210             i_duration = -1;
211
212             b_cleanup = VLC_FALSE;
213         }
214     }
215     HANDLE_PLAY_AND_RELEASE;
216     return -1; /* Needed for correct operation of go back */
217 }
218
219 static int Control( demux_t *p_demux, int i_query, va_list args )
220 {
221     return VLC_EGENERIC;
222 }
223
224 static void parseEXTINF(char *psz_string, char **ppsz_artist,
225                         char **ppsz_name, int *pi_duration)
226 {
227     char *end = NULL;
228     char *psz_item = NULL;
229
230     end = psz_string + strlen( psz_string );
231
232     /* ignore whitespaces */
233     for (; psz_string < end && ( *psz_string == '\t' || *psz_string == ' ' );
234          psz_string++ );
235
236     /* duration: read to next comma */
237     psz_item = psz_string;
238     psz_string = strchr( psz_string, ',' );
239     if ( psz_string )
240     {
241         *psz_string = '\0';
242         *pi_duration = atoi( psz_item );
243     }
244     else
245     {
246         return;
247     }
248
249     if ( psz_string < end )               /* continue parsing if possible */
250         psz_string++;
251
252     /* analyse the remaining string */
253     psz_item = strstr( psz_string, " - " );
254
255     /* here we have the 0.8.2+ format with artist */
256     if ( psz_item )
257     {
258         /* *** "EXTINF:time,artist - name" */
259         *psz_item = '\0';
260         *ppsz_artist = psz_string;
261         *ppsz_name = psz_item + 3;          /* points directly after ' - ' */
262         return;
263     }
264
265     /* reaching this point means: 0.8.1- with artist or something without artist */
266     if ( *psz_string == ',' )
267     {
268         /* *** "EXTINF:time,,name" */
269         psz_string++;
270         *ppsz_name = psz_string;
271         return;
272     }
273
274     psz_item = psz_string;
275     psz_string = strchr( psz_string, ',' );
276     if ( psz_string )
277     {
278         /* *** "EXTINF:time,artist,name" */
279         *psz_string = '\0';
280         *ppsz_artist = psz_item;
281         *ppsz_name = psz_string+1;
282     }
283     else
284     {
285         /* *** "EXTINF:time,name" */
286         *ppsz_name = psz_item;
287     }
288     return;
289 }
290