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