1 /*****************************************************************************
2 * pls.c : PLS playlist format import
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc/input.h>
34 #include <errno.h> /* ENOMEM */
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Demux( demux_t *p_demux);
46 static int Control( demux_t *p_demux, int i_query, va_list args );
48 /*****************************************************************************
49 * Import_PLS: main import function
50 *****************************************************************************/
51 int Import_PLS( vlc_object_t *p_this )
53 demux_t *p_demux = (demux_t *)p_this;
58 if( stream_Peek( p_demux->s , &p_peek, 7 ) < 7 )
60 msg_Err( p_demux, "cannot peek" );
63 psz_ext = strrchr ( p_demux->psz_path, '.' );
65 if( !strncasecmp( p_peek, "[playlist]", 10 ) )
69 else if( ( psz_ext && !strcasecmp( psz_ext, ".pls") ) ||
70 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "pls") ) )
76 msg_Warn(p_demux, "pls import module discarded");
80 msg_Dbg( p_demux, "found valid PLS playlist file");
82 p_demux->pf_control = Control;
83 p_demux->pf_demux = Demux;
84 p_demux->p_sys = malloc( sizeof(demux_sys_t) );
85 if( p_demux->p_sys == NULL )
87 msg_Err( p_demux, "Out of memory" );
90 p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
95 /*****************************************************************************
96 * Deactivate: frees unused data
97 *****************************************************************************/
98 void Close_PLS( vlc_object_t *p_this )
100 demux_t *p_demux = (demux_t *)p_this;
101 if( p_demux->p_sys->psz_prefix )
103 free( p_demux->p_sys->psz_prefix );
105 free( p_demux->p_sys );
108 static int Demux( demux_t *p_demux )
110 mtime_t i_duration = -1;
111 char *psz_name = NULL;
113 char *psz_mrl = NULL;
116 playlist_t *p_playlist;
121 playlist_item_t *p_parent;
123 p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
127 msg_Err( p_demux, "can't find playlist" );
130 p_parent = p_playlist->status.p_item;
131 p_parent->input.i_type = ITEM_TYPE_PLAYLIST;
133 // p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
134 /* Change the item to a node */
135 if( p_parent->i_children == -1)
137 playlist_ItemToNode( p_playlist,p_parent );
139 while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
141 if( !strncasecmp( psz_line, "[playlist]", sizeof("[playlist]")-1 ) )
147 psz_value = strchr( psz_line, '=' );
155 msg_Warn( p_demux, "invalid line in pls file" );
159 if( !strcasecmp( psz_key, "version" ) )
161 msg_Dbg( p_demux, "pls file version: %s", psz_value );
165 /* find the number part of of file1, title1 or length1 etc */
166 i_key_length = strlen( psz_key );
167 if( i_key_length >= 5 ) /* file1 type case */
169 i_new_item = atoi( psz_key + 4 );
170 if( i_new_item == 0 && i_key_length >= 6 ) /* title1 type case */
172 i_new_item = atoi( psz_key + 5 );
173 if( i_new_item == 0 && i_key_length >= 7 ) /* length1 type case */
175 i_new_item = atoi( psz_key + 6 );
179 if( i_new_item == 0 )
181 msg_Warn( p_demux, "couldn't find number of items" );
189 /* we found a new item, insert the previous */
190 if( i_item != i_new_item )
194 playlist_item_t *p_item = playlist_ItemNew( p_playlist, psz_mrl,
197 playlist_NodeAddItem( p_playlist,p_item,
198 p_parent->pp_parents[0]->i_view,
200 PLAYLIST_APPEND, PLAYLIST_END );
202 playlist_CopyParents( p_parent, p_item );
203 if( i_duration != -1 )
205 //playlist_SetDuration( p_playlist, i_position, i_duration );
213 msg_Warn( p_demux, "no file= part found for item %d", i_item );
224 if( !strncasecmp( psz_key, "file", sizeof("file") -1 ) )
226 psz_mrl = ProcessMRL( psz_value, p_demux->p_sys->psz_prefix );
228 else if( !strncasecmp( psz_key, "title", sizeof("title") -1 ) )
230 psz_name = strdup( psz_value );
232 else if( !strncasecmp( psz_key, "length", sizeof("length") -1 ) )
234 i_duration = atoi( psz_value );
235 if( i_duration != -1 )
237 i_duration *= 1000000;
242 msg_Warn( p_demux, "unknown key found in pls file: %s", psz_key );
246 /* Add last object */
249 playlist_item_t *p_item = playlist_ItemNew( p_playlist, psz_mrl,
252 playlist_NodeAddItem( p_playlist,p_item,
253 p_parent->pp_parents[0]->i_view,
255 PLAYLIST_APPEND, PLAYLIST_END );
257 playlist_CopyParents( p_parent, p_item );
258 if( i_duration != -1 )
260 //playlist_SetDuration( p_playlist, i_position, i_duration );
267 msg_Warn( p_demux, "no file= part found for item %d", i_item );
275 vlc_object_release( p_playlist );
279 static int Control( demux_t *p_demux, int i_query, va_list args )