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