]> git.sesse.net Git - vlc/blob - modules/demux/playlist/pls.c
Revert [23949].
[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 <vlc/vlc.h>
29 #include <vlc_demux.h>
30
31 #include "playlist.h"
32
33 struct demux_sys_t
34 {
35     char *psz_prefix;
36 };
37
38 /*****************************************************************************
39  * Local prototypes
40  *****************************************************************************/
41 static int Demux( demux_t *p_demux);
42 static int Control( demux_t *p_demux, int i_query, va_list args );
43
44 /*****************************************************************************
45  * Import_PLS: main import function
46  *****************************************************************************/
47 int E_(Import_PLS)( vlc_object_t *p_this )
48 {
49     demux_t *p_demux = (demux_t *)p_this;
50     const uint8_t *p_peek;
51     CHECK_PEEK( p_peek, 10 );
52
53     if( POKE( p_peek, "[playlist]", 10 ) || POKE( p_peek, "[Reference]", 10 ) ||
54         demux2_IsPathExtension( p_demux, ".pls" )   || demux2_IsForced( p_demux, "pls" ) )
55     {
56         ;
57     }
58     else return VLC_EGENERIC;
59
60     STANDARD_DEMUX_INIT_MSG(  "found valid PLS playlist file");
61     p_demux->p_sys->psz_prefix = E_(FindPrefix)( p_demux );
62
63     return VLC_SUCCESS;
64 }
65
66 /*****************************************************************************
67  * Deactivate: frees unused data
68  *****************************************************************************/
69 void E_(Close_PLS)( vlc_object_t *p_this )
70 {
71     demux_t *p_demux = (demux_t *)p_this;
72     if( p_demux->p_sys->psz_prefix )
73     {
74         free( p_demux->p_sys->psz_prefix );
75     }
76     free( p_demux->p_sys );
77 }
78
79 static int Demux( demux_t *p_demux )
80 {
81     mtime_t        i_duration = -1;
82     char          *psz_name = NULL;
83     char          *psz_line;
84     char          *psz_mrl = NULL;
85     char          *psz_mrl_orig = NULL;
86     char          *psz_key;
87     char          *psz_value;
88     int            i_item = -1;
89     int            i_new_item = 0;
90     int            i_key_length;
91     input_item_t *p_input;
92
93     INIT_PLAYLIST_STUFF;
94
95     while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
96     {
97         if( !strncasecmp( psz_line, "[playlist]", sizeof("[playlist]")-1 ) ||
98             !strncasecmp( psz_line, "[Reference]", sizeof("[Reference]")-1 ) )
99         {
100             free( psz_line );
101             continue;
102         }
103         psz_key = psz_line;
104         psz_value = strchr( psz_line, '=' );
105         if( psz_value )
106         {
107             *psz_value='\0';
108             psz_value++;
109         }
110         else
111         {
112             msg_Warn( p_demux, "invalid line in pls file" );
113             free( psz_line );
114             continue;
115         }
116         if( !strcasecmp( psz_key, "version" ) )
117         {
118             msg_Dbg( p_demux, "pls file version: %s", psz_value );
119             free( psz_line );
120             continue;
121         }
122         if( !strcasecmp( psz_key, "numberofentries" ) )
123         {
124             msg_Dbg( p_demux, "pls should have %d entries", atoi(psz_value) );
125             free( psz_line);
126             continue;
127         }
128         /* find the number part of of file1, title1 or length1 etc */
129         i_key_length = strlen( psz_key );
130         if( i_key_length >= 4 ) /* Ref1 type case */
131         {
132             i_new_item = atoi( psz_key + 3 );
133             if( i_new_item == 0 && i_key_length >= 5 ) /* file1 type case */
134             {
135                 i_new_item = atoi( psz_key + 4 );
136                 if( i_new_item == 0 && i_key_length >= 6 ) /* title1 type case */
137                 {
138                     i_new_item = atoi( psz_key + 5 );
139                     if( i_new_item == 0 && i_key_length >= 7 ) /* length1 type case */
140                     {
141                         i_new_item = atoi( psz_key + 6 );
142                     }
143                 }
144             }
145         }
146         if( i_new_item == 0 )
147         {
148             msg_Warn( p_demux, "couldn't find number of items" );
149             free( psz_line );
150             continue;
151         }
152         if( i_item == -1 )
153         {
154             i_item = i_new_item;
155         }
156         /* we found a new item, insert the previous */
157         if( i_item != i_new_item )
158         {
159             if( psz_mrl )
160             {
161                 p_input = input_ItemNewExt( p_playlist, psz_mrl, psz_name,
162                                             0, NULL, -1 );
163                 input_ItemCopyOptions( p_current_input, p_input );
164                 input_ItemAddSubItem( p_current_input, p_input );
165                 vlc_gc_decref( p_input );
166             }
167             else
168             {
169                 msg_Warn( p_demux, "no file= part found for item %d", i_item );
170             }
171             if( psz_name )
172             {
173                 free( psz_name );
174                 psz_name = NULL;
175             }
176             i_duration = -1;
177             i_item = i_new_item;
178             i_new_item = 0;
179         }
180         if( !strncasecmp( psz_key, "file", sizeof("file") -1 ) ||
181             !strncasecmp( psz_key, "Ref", sizeof("Ref") -1 ) )
182         {
183             if( psz_mrl_orig )
184                 free( psz_mrl_orig );
185             psz_mrl_orig =
186             psz_mrl = E_(ProcessMRL)( psz_value, p_demux->p_sys->psz_prefix );
187
188             if( !strncasecmp( psz_key, "Ref", sizeof("Ref") -1 ) )
189             {
190                 if( !strncasecmp( psz_mrl, "http://", sizeof("http://") -1 ) )
191                 {
192                     psz_mrl++;
193                     psz_mrl[0] = 'm';
194                     psz_mrl[1] = 'm';
195                     psz_mrl[2] = 's';
196                 }
197             }
198         }
199         else if( !strncasecmp( psz_key, "title", sizeof("title") -1 ) )
200         {
201             if( psz_name )
202                 free( psz_name );
203             psz_name = strdup( psz_value );
204         }
205         else if( !strncasecmp( psz_key, "length", sizeof("length") -1 ) )
206         {
207             i_duration = atoi( psz_value );
208             if( i_duration != -1 )
209             {
210                 i_duration *= 1000000;
211             }
212         }
213         else
214         {
215             msg_Warn( p_demux, "unknown key found in pls file: %s", psz_key );
216         }
217         free( psz_line );
218     }
219     /* Add last object */
220     if( psz_mrl )
221     {
222         p_input = input_ItemNewExt( p_playlist, psz_mrl, psz_name,0, NULL, -1 );
223         input_ItemCopyOptions( p_current_input, p_input );
224         input_ItemAddSubItem( p_current_input, p_input );
225         vlc_gc_decref( p_input );
226         free( psz_mrl_orig );
227         psz_mrl = NULL;
228     }
229     else
230     {
231         msg_Warn( p_demux, "no file= part found for item %d", i_item );
232     }
233     if( psz_name )
234     {
235         free( psz_name );
236         psz_name = NULL;
237     }
238
239     HANDLE_PLAY_AND_RELEASE;
240     return 0; /* Needed for correct operation of go back */
241 }
242
243 static int Control( demux_t *p_demux, int i_query, va_list args )
244 {
245     return VLC_EGENERIC;
246 }