]> git.sesse.net Git - vlc/blob - modules/demux/playlist/pls.c
* Almost fix shoutcast (still some problems in "flat" view)
[vlc] / modules / demux / playlist / pls.c
1 /*****************************************************************************
2  * pls.c : PLS playlist format import
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  * Authors: 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/input.h>
32 #include <vlc/intf.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
48 /*****************************************************************************
49  * Import_PLS: main import function
50  *****************************************************************************/
51 int E_(Import_PLS)( vlc_object_t *p_this )
52 {
53     demux_t *p_demux = (demux_t *)p_this;
54
55     uint8_t *p_peek;
56     char    *psz_ext;
57
58     if( stream_Peek( p_demux->s , &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
59     psz_ext = strrchr ( p_demux->psz_path, '.' );
60
61     if( !strncasecmp( (char *)p_peek, "[playlist]", 10 ) )
62     {
63         ;
64     }
65     else if( ( psz_ext && !strcasecmp( psz_ext, ".pls") ) ||
66              ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "pls") ) )
67     {
68         ;
69     }
70     else return VLC_EGENERIC;
71
72     msg_Dbg( p_demux, "found valid PLS playlist file");
73
74     p_demux->pf_control = Control;
75     p_demux->pf_demux = Demux;
76     p_demux->p_sys = malloc( sizeof(demux_sys_t) );
77     if( p_demux->p_sys == NULL )
78     {
79         msg_Err( p_demux, "out of memory" );
80         return VLC_ENOMEM;
81     }
82     p_demux->p_sys->psz_prefix = E_(FindPrefix)( p_demux );
83
84     return VLC_SUCCESS;
85 }
86
87 /*****************************************************************************
88  * Deactivate: frees unused data
89  *****************************************************************************/
90 void E_(Close_PLS)( vlc_object_t *p_this )
91 {
92     demux_t *p_demux = (demux_t *)p_this;
93     if( p_demux->p_sys->psz_prefix )
94     {
95         free( p_demux->p_sys->psz_prefix );
96     }
97     free( p_demux->p_sys );
98 }
99
100 static int Demux( demux_t *p_demux )
101 {
102     mtime_t        i_duration = -1;
103     char          *psz_name = NULL;
104     char          *psz_line;
105     char          *psz_mrl = NULL;
106     char          *psz_key;
107     char          *psz_value;
108     int            i_position;
109     int            i_item = -1;
110     int            i_new_item = 0;
111     int            i_key_length;
112
113     INIT_PLAYLIST_STUFF;
114
115     while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
116     {
117         if( !strncasecmp( psz_line, "[playlist]", sizeof("[playlist]")-1 ) )
118         {
119             free( psz_line );
120             continue;
121         }
122         psz_key = psz_line;
123         psz_value = strchr( psz_line, '=' );
124         if( psz_value )
125         {
126             *psz_value='\0';
127             psz_value++;
128         }
129         else
130         {
131             msg_Warn( p_demux, "invalid line in pls file" );
132             free( psz_line );
133             continue;
134         }
135         if( !strcasecmp( psz_key, "version" ) )
136         {
137             msg_Dbg( p_demux, "pls file version: %s", psz_value );
138             free( psz_line );
139             continue;
140         }
141         /* find the number part of of file1, title1 or length1 etc */
142         i_key_length = strlen( psz_key );
143         if( i_key_length >= 5 ) /* file1 type case */
144         {
145             i_new_item = atoi( psz_key + 4 );
146             if( i_new_item == 0 && i_key_length >= 6 ) /* title1 type case */
147             {
148                 i_new_item = atoi( psz_key + 5 );
149                 if( i_new_item == 0 && i_key_length >= 7 ) /* length1 type case */
150                 {
151                     i_new_item = atoi( psz_key + 6 );
152                 }
153             }
154         }
155         if( i_new_item == 0 )
156         {
157             msg_Warn( p_demux, "couldn't find number of items" );
158             free( psz_line );
159             continue;
160         }
161         if( i_item == -1 )
162         {
163             i_item = i_new_item;
164         }
165         /* we found a new item, insert the previous */
166         if( i_item != i_new_item )
167         {
168             if( psz_mrl )
169             {
170                 p_input = input_ItemNewExt( p_playlist, psz_mrl, psz_name,
171                                             0, NULL, -1 );
172                 vlc_input_item_CopyOptions( p_current->p_input, p_input );
173                 playlist_AddWhereverNeeded( p_playlist, p_input, p_current,
174                                 p_item_in_category, (i_parent_id > 0 ) ?
175                                 VLC_TRUE: VLC_FALSE, PLAYLIST_APPEND );
176             }
177             else
178             {
179                 msg_Warn( p_demux, "no file= part found for item %d", i_item );
180             }
181             if( psz_name )
182             {
183                 free( psz_name );
184                 psz_name = NULL;
185             }
186             i_duration = -1;
187             i_item = i_new_item;
188             i_new_item = 0;
189         }
190         if( !strncasecmp( psz_key, "file", sizeof("file") -1 ) )
191         {
192             psz_mrl = E_(ProcessMRL)( psz_value, p_demux->p_sys->psz_prefix );
193         }
194         else if( !strncasecmp( psz_key, "title", sizeof("title") -1 ) )
195         {
196             psz_name = strdup( psz_value );
197         }
198         else if( !strncasecmp( psz_key, "length", sizeof("length") -1 ) )
199         {
200             i_duration = atoi( psz_value );
201             if( i_duration != -1 )
202             {
203                 i_duration *= 1000000;
204             }
205         }
206         else
207         {
208             msg_Warn( p_demux, "unknown key found in pls file: %s", psz_key );
209         }
210         free( psz_line );
211     }
212     /* Add last object */
213     if( psz_mrl )
214     {
215         p_input = input_ItemNewExt( p_playlist, psz_mrl, psz_name,0, NULL, -1 );
216         vlc_input_item_CopyOptions( p_current->p_input, p_input );
217         playlist_AddWhereverNeeded( p_playlist, p_input, p_current,
218                                 p_item_in_category, (i_parent_id > 0 ) ?
219                                 VLC_TRUE: VLC_FALSE, PLAYLIST_APPEND );
220         free( psz_mrl );
221         psz_mrl = NULL;
222     }
223     else
224     {
225         msg_Warn( p_demux, "no file= part found for item %d", i_item );
226     }
227     if( psz_name )
228     {
229         free( psz_name );
230         psz_name = NULL;
231     }
232
233     HANDLE_PLAY_AND_RELEASE;
234     return VLC_SUCCESS;
235 }
236
237 static int Control( demux_t *p_demux, int i_query, va_list args )
238 {
239     return VLC_EGENERIC;
240 }