1 /*****************************************************************************
2 * pls.c : PLS playlist format import
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_demux.h>
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
;
54 const uint8_t *p_peek
;
55 CHECK_PEEK( p_peek
, 10 );
57 if( POKE( p_peek
, "[playlist]", 10 ) || POKE( p_peek
, "[Reference]", 10 ) ||
58 demux_IsPathExtension( p_demux
, ".pls" ) || demux_IsForced( p_demux
, "pls" ) )
62 else return VLC_EGENERIC
;
64 STANDARD_DEMUX_INIT_MSG( "found valid PLS playlist file");
65 p_demux
->p_sys
->psz_prefix
= FindPrefix( p_demux
);
70 /*****************************************************************************
71 * Deactivate: frees unused data
72 *****************************************************************************/
73 void Close_PLS( vlc_object_t
*p_this
)
75 demux_t
*p_demux
= (demux_t
*)p_this
;
76 free( p_demux
->p_sys
->psz_prefix
);
77 free( p_demux
->p_sys
);
80 static int Demux( demux_t
*p_demux
)
82 mtime_t i_duration
= -1;
83 char *psz_name
= NULL
;
86 char *psz_mrl_orig
= NULL
;
90 input_item_t
*p_input
;
92 input_item_t
*p_current_input
= GetCurrentItem(p_demux
);
94 input_item_node_t
*p_subitems
= input_item_node_Create( p_current_input
);
96 while( ( psz_line
= stream_ReadLine( p_demux
->s
) ) )
98 if( !strncasecmp( psz_line
, "[playlist]", sizeof("[playlist]")-1 ) ||
99 !strncasecmp( psz_line
, "[Reference]", sizeof("[Reference]")-1 ) )
105 psz_value
= strchr( psz_line
, '=' );
116 if( !strcasecmp( psz_key
, "version" ) )
118 msg_Dbg( p_demux
, "pls file version: %s", psz_value
);
122 if( !strcasecmp( psz_key
, "numberofentries" ) )
124 msg_Dbg( p_demux
, "pls should have %d entries", atoi(psz_value
) );
129 /* find the number part of of file1, title1 or length1 etc */
131 if( sscanf( psz_key
, "%*[^0-9]%d", &i_new_item
) != 1 )
133 msg_Warn( p_demux
, "couldn't find number of items" );
140 else if( i_item
!= i_new_item
)
142 /* we found a new item, insert the previous */
145 p_input
= input_item_New( p_demux
, psz_mrl
, psz_name
);
146 input_item_CopyOptions( p_current_input
, p_input
);
147 input_item_node_AppendItem( p_subitems
, p_input
);
148 vlc_gc_decref( p_input
);
149 free( psz_mrl_orig
);
150 psz_mrl_orig
= psz_mrl
= NULL
;
154 msg_Warn( p_demux
, "no file= part found for item %d", i_item
);
162 if( !strncasecmp( psz_key
, "file", sizeof("file") -1 ) ||
163 !strncasecmp( psz_key
, "Ref", sizeof("Ref") -1 ) )
165 free( psz_mrl_orig
);
167 psz_mrl
= ProcessMRL( psz_value
, p_demux
->p_sys
->psz_prefix
);
169 if( !strncasecmp( psz_key
, "Ref", sizeof("Ref") -1 ) )
171 if( !strncasecmp( psz_mrl
, "http://", sizeof("http://") -1 ) )
172 memcpy( psz_mrl
, "mmsh", 4 );
175 else if( !strncasecmp( psz_key
, "title", sizeof("title") -1 ) )
178 psz_name
= strdup( psz_value
);
180 else if( !strncasecmp( psz_key
, "length", sizeof("length") -1 ) )
182 i_duration
= atoll( psz_value
);
183 if( i_duration
!= -1 )
185 i_duration
*= 1000000;
190 msg_Warn( p_demux
, "unknown key found in pls file: %s", psz_key
);
194 /* Add last object */
197 p_input
= input_item_New( p_demux
, psz_mrl
, psz_name
);
198 input_item_CopyOptions( p_current_input
, p_input
);
199 input_item_node_AppendItem( p_subitems
, p_input
);
200 vlc_gc_decref( p_input
);
201 free( psz_mrl_orig
);
205 msg_Warn( p_demux
, "no file= part found for item %d", i_item
);
210 input_item_node_PostAndDelete( p_subitems
);
212 vlc_gc_decref(p_current_input
);
213 return 0; /* Needed for correct operation of go back */
216 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
218 VLC_UNUSED(p_demux
); VLC_UNUSED(i_query
); VLC_UNUSED(args
);