]> git.sesse.net Git - vlc/blob - modules/demux/playlist/m3u.c
libav*: move avformat/avcodec init to a single header file
[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
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
35 #include <vlc_charset.h>
36
37 #include "playlist.h"
38
39 struct demux_sys_t
40 {
41     char *psz_prefix;
42     char *(*pf_dup) (const char *);
43 };
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 static int Demux( demux_t *p_demux);
49 static void parseEXTINF( char *psz_string, char **ppsz_artist, char **ppsz_name, int *pi_duration );
50 static bool ContainsURL( demux_t *p_demux );
51
52 static char *GuessEncoding (const char *str)
53 {
54     return IsUTF8 (str) ? strdup (str) : FromLatin1 (str);
55 }
56
57 static char *CheckUnicode (const char *str)
58 {
59     return IsUTF8 (str) ? strdup (str): NULL;
60 }
61
62 /*****************************************************************************
63  * Import_M3U: main import function
64  *****************************************************************************/
65 int Import_M3U( vlc_object_t *p_this )
66 {
67     demux_t *p_demux = (demux_t *)p_this;
68     const uint8_t *p_peek;
69     CHECK_PEEK( p_peek, 8 );
70     char *(*pf_dup) (const char *);
71
72     if( POKE( p_peek, "RTSPtext", 8 ) /* QuickTime */
73      || demux_IsPathExtension( p_demux, ".m3u8" )
74      || demux_IsForced( p_demux, "m3u8" ) )
75         pf_dup = CheckUnicode; /* UTF-8 */
76     else
77     if( POKE( p_peek, "#EXTM3U", 7 )
78      || demux_IsPathExtension( p_demux, ".m3u" )
79      || demux_IsPathExtension( p_demux, ".vlc" )
80      || demux_IsForced( p_demux, "m3u" )
81      || ContainsURL( p_demux ) )
82         pf_dup = GuessEncoding;
83     else
84         return VLC_EGENERIC;
85
86     STANDARD_DEMUX_INIT_MSG( "found valid M3U playlist" );
87     p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
88     p_demux->p_sys->pf_dup = pf_dup;
89
90     return VLC_SUCCESS;
91 }
92
93 static bool ContainsURL( demux_t *p_demux )
94 {
95     const uint8_t *p_peek, *p_peek_end;
96     int i_peek;
97
98     i_peek = stream_Peek( p_demux->s, &p_peek, 1024 );
99     if( i_peek <= 0 ) return false;
100     p_peek_end = p_peek + i_peek;
101
102     while( p_peek + sizeof( "https://" ) < p_peek_end )
103     {
104         /* One line starting with a URL is enough */
105         if( !strncasecmp( (const char *)p_peek, "http://", 7 ) ||
106             !strncasecmp( (const char *)p_peek, "mms://", 6 ) ||
107             !strncasecmp( (const char *)p_peek, "rtsp://", 7 ) ||
108             !strncasecmp( (const char *)p_peek, "https://", 8 ) ||
109             !strncasecmp( (const char *)p_peek, "ftp://", 6 ) )
110         {
111             return true;
112         }
113         /* Comments and blank lines are ignored */
114         else if( *p_peek != '#' && *p_peek != '\n' && *p_peek != '\r')
115         {
116             return false;
117         }
118
119         while( p_peek < p_peek_end && *p_peek != '\n' )
120             p_peek++;
121         if ( *p_peek == '\n' )
122             p_peek++;
123     }
124     return false;
125 }
126
127 /*****************************************************************************
128  * Deactivate: frees unused data
129  *****************************************************************************/
130 void Close_M3U( vlc_object_t *p_this )
131 {
132     demux_t *p_demux = (demux_t *)p_this;
133     free( p_demux->p_sys->psz_prefix );
134     free( p_demux->p_sys );
135 }
136
137
138 static int Demux( demux_t *p_demux )
139 {
140     char       *psz_line;
141     char       *psz_name = NULL;
142     char       *psz_artist = NULL;
143     char       *psz_album_art = NULL;
144     int        i_parsed_duration = 0;
145     mtime_t    i_duration = -1;
146     const char**ppsz_options = NULL;
147     char *    (*pf_dup) (const char *) = p_demux->p_sys->pf_dup;
148     int        i_options = 0;
149     bool b_cleanup = false;
150     input_item_t *p_input;
151
152     input_item_t *p_current_input = GetCurrentItem(p_demux);
153     input_item_node_t *p_subitems = input_item_node_Create( p_current_input );
154
155     psz_line = stream_ReadLine( p_demux->s );
156     while( psz_line )
157     {
158         char *psz_parse = psz_line;
159
160         /* Skip leading tabs and spaces */
161         while( *psz_parse == ' ' || *psz_parse == '\t' ||
162                *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
163
164         if( *psz_parse == '#' )
165         {
166             /* Parse extra info */
167
168             /* Skip leading tabs and spaces */
169             while( *psz_parse == ' ' || *psz_parse == '\t' ||
170                    *psz_parse == '\n' || *psz_parse == '\r' ||
171                    *psz_parse == '#' ) psz_parse++;
172
173             if( !*psz_parse ) goto error;
174
175             if( !strncasecmp( psz_parse, "EXTINF:", sizeof("EXTINF:") -1 ) )
176             {
177                 /* Extended info */
178                 psz_parse += sizeof("EXTINF:") - 1;
179                 free(psz_name);
180                 free(psz_artist);
181                 parseEXTINF( psz_parse, &psz_artist, &psz_name, &i_parsed_duration );
182                 if( i_parsed_duration >= 0 )
183                     i_duration = i_parsed_duration * INT64_C(1000000);
184                 if( psz_name )
185                     psz_name = pf_dup( psz_name );
186                 if( psz_artist )
187                     psz_artist = pf_dup( psz_artist );
188             }
189             else if( !strncasecmp( psz_parse, "EXTVLCOPT:",
190                                    sizeof("EXTVLCOPT:") -1 ) )
191             {
192                 /* VLC Option */
193                 char *psz_option;
194                 psz_parse += sizeof("EXTVLCOPT:") -1;
195                 if( !*psz_parse ) goto error;
196
197                 psz_option = pf_dup( psz_parse );
198                 if( psz_option )
199                     INSERT_ELEM( ppsz_options, i_options, i_options,
200                                  psz_option );
201             }
202             /* Special case for jamendo which provide the albumart */
203             else if( !strncasecmp( psz_parse, "EXTALBUMARTURL:",
204                      sizeof( "EXTALBUMARTURL:" ) -1 ) )
205             {
206                 psz_parse += sizeof( "EXTALBUMARTURL:" ) - 1;
207                 free( psz_album_art );
208                 psz_album_art = pf_dup( psz_parse );
209             }
210         }
211         else if( !strncasecmp( psz_parse, "RTSPtext", sizeof("RTSPtext") -1 ) )
212         {
213             ;/* special case to handle QuickTime RTSPtext redirect files */
214         }
215         else if( *psz_parse )
216         {
217             char *psz_mrl;
218
219             psz_parse = pf_dup( psz_parse );
220             if( !psz_name && psz_parse )
221                 /* Use filename as name for relative entries */
222                 psz_name = strdup( psz_parse );
223
224             psz_mrl = ProcessMRL( psz_parse, p_demux->p_sys->psz_prefix );
225
226             b_cleanup = true;
227             if( !psz_mrl )
228             {
229                 free( psz_parse );
230                 goto error;
231             }
232
233             p_input = input_item_NewExt( psz_mrl, psz_name,
234                                         i_options, ppsz_options, 0, i_duration );
235
236             free( psz_parse );
237             free( psz_mrl );
238
239             if ( !EMPTY_STR(psz_artist) )
240                 input_item_SetArtist( p_input, psz_artist );
241             if( psz_name ) input_item_SetTitle( p_input, psz_name );
242             if( !EMPTY_STR(psz_album_art) )
243                 input_item_SetArtURL( p_input, psz_album_art );
244
245             input_item_node_AppendItem( p_subitems, p_input );
246             vlc_gc_decref( p_input );
247         }
248
249  error:
250
251         /* Fetch another line */
252         free( psz_line );
253         psz_line = stream_ReadLine( p_demux->s );
254         if( !psz_line ) b_cleanup = true;
255
256         if( b_cleanup )
257         {
258             /* Cleanup state */
259             while( i_options-- ) free( (char*)ppsz_options[i_options] );
260             FREENULL( ppsz_options );
261             i_options = 0;
262             FREENULL( psz_name );
263             FREENULL( psz_artist );
264             FREENULL( psz_album_art );
265             i_parsed_duration = 0;
266             i_duration = -1;
267
268             b_cleanup = false;
269         }
270     }
271     input_item_node_PostAndDelete( p_subitems );
272     vlc_gc_decref(p_current_input);
273     var_Destroy( p_demux, "m3u-extvlcopt" );
274     return 0; /* Needed for correct operation of go back */
275 }
276
277 static void parseEXTINF(char *psz_string, char **ppsz_artist,
278                         char **ppsz_name, int *pi_duration)
279 {
280     char *end = NULL;
281     char *psz_item = NULL;
282
283     end = psz_string + strlen( psz_string );
284
285     /* ignore whitespaces */
286     for (; psz_string < end && ( *psz_string == '\t' || *psz_string == ' ' );
287          psz_string++ );
288
289     /* duration: read to next comma */
290     psz_item = psz_string;
291     psz_string = strchr( psz_string, ',' );
292     if ( psz_string )
293     {
294         *psz_string = '\0';
295         *pi_duration = atoi( psz_item );
296     }
297     else
298     {
299         return;
300     }
301
302     if ( psz_string < end )               /* continue parsing if possible */
303         psz_string++;
304
305     /* analyse the remaining string */
306     psz_item = strstr( psz_string, " - " );
307
308     /* here we have the 0.8.2+ format with artist */
309     if ( psz_item )
310     {
311         /* *** "EXTINF:time,artist - name" */
312         *psz_item = '\0';
313         *ppsz_artist = psz_string;
314         *ppsz_name = psz_item + 3;          /* points directly after ' - ' */
315         return;
316     }
317
318     /* reaching this point means: 0.8.1- with artist or something without artist */
319     if ( *psz_string == ',' )
320     {
321         /* *** "EXTINF:time,,name" */
322         psz_string++;
323         *ppsz_name = psz_string;
324         return;
325     }
326
327     psz_item = psz_string;
328     psz_string = strchr( psz_string, ',' );
329     if ( psz_string )
330     {
331         /* *** "EXTINF:time,artist,name" */
332         *psz_string = '\0';
333         *ppsz_artist = psz_item;
334         *ppsz_name = psz_string+1;
335     }
336     else
337     {
338         /* *** "EXTINF:time,name" */
339         *ppsz_name = psz_item;
340     }
341     return;
342 }
343