]> git.sesse.net Git - vlc/blob - modules/demux/playlist/pls.c
ASF: better logging and more consistent with MKV/MP4 demux
[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
47 /*****************************************************************************
48  * Import_PLS: main import function
49  *****************************************************************************/
50 int Import_PLS( vlc_object_t *p_this )
51 {
52     demux_t *p_demux = (demux_t *)p_this;
53     const uint8_t *p_peek;
54     CHECK_PEEK( p_peek, 10 );
55
56     if( POKE( p_peek, "[playlist]", 10 ) || POKE( p_peek, "[Reference]", 10 ) ||
57         demux_IsPathExtension( p_demux, ".pls" )   || demux_IsForced( p_demux, "pls" ) )
58     {
59         ;
60     }
61     else return VLC_EGENERIC;
62
63     STANDARD_DEMUX_INIT_MSG(  "found valid PLS playlist file");
64     p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
65
66     return VLC_SUCCESS;
67 }
68
69 /*****************************************************************************
70  * Deactivate: frees unused data
71  *****************************************************************************/
72 void Close_PLS( vlc_object_t *p_this )
73 {
74     demux_t *p_demux = (demux_t *)p_this;
75     free( p_demux->p_sys->psz_prefix );
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     input_item_t *p_input;
90
91     input_item_t *p_current_input = GetCurrentItem(p_demux);
92
93     input_item_node_t *p_subitems = input_item_node_Create( p_current_input );
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             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         if( !strcasecmp( psz_key, "numberofentries" ) )
122         {
123             msg_Dbg( p_demux, "pls should have %d entries", atoi(psz_value) );
124             free( psz_line);
125             continue;
126         }
127
128         /* find the number part of of file1, title1 or length1 etc */
129         int i_new_item;
130         if( sscanf( psz_key, "%*[^0-9]%d", &i_new_item ) != 1 )
131         {
132             msg_Warn( p_demux, "couldn't find number of items" );
133             free( psz_line );
134             continue;
135         }
136
137         if( i_item == -1 )
138             i_item = i_new_item;
139         else if( i_item != i_new_item )
140         {
141             /* we found a new item, insert the previous */
142             if( psz_mrl )
143             {
144                 p_input = input_item_New( psz_mrl, psz_name );
145                 input_item_CopyOptions( p_current_input, p_input );
146                 input_item_node_AppendItem( p_subitems, p_input );
147                 vlc_gc_decref( p_input );
148                 free( psz_mrl_orig );
149                 psz_mrl_orig = psz_mrl = NULL;
150             }
151             else
152             {
153                 msg_Warn( p_demux, "no file= part found for item %d", i_item );
154             }
155             free( psz_name );
156             psz_name = NULL;
157             i_duration = -1;
158             i_item = i_new_item;
159         }
160
161         if( !strncasecmp( psz_key, "file", sizeof("file") -1 ) ||
162             !strncasecmp( psz_key, "Ref", sizeof("Ref") -1 ) )
163         {
164             free( psz_mrl_orig );
165             psz_mrl_orig =
166             psz_mrl = ProcessMRL( psz_value, p_demux->p_sys->psz_prefix );
167
168             if( !strncasecmp( psz_key, "Ref", sizeof("Ref") -1 ) )
169             {
170                 if( !strncasecmp( psz_mrl, "http://", sizeof("http://") -1 ) )
171                     memcpy( psz_mrl, "mmsh", 4 );
172             }
173         }
174         else if( !strncasecmp( psz_key, "title", sizeof("title") -1 ) )
175         {
176             free( psz_name );
177             psz_name = strdup( psz_value );
178         }
179         else if( !strncasecmp( psz_key, "length", sizeof("length") -1 ) )
180         {
181             i_duration = atoll( psz_value );
182             if( i_duration != -1 )
183             {
184                 i_duration *= 1000000;
185             }
186         }
187         else
188         {
189             msg_Warn( p_demux, "unknown key found in pls file: %s", psz_key );
190         }
191         free( psz_line );
192     }
193     /* Add last object */
194     if( psz_mrl )
195     {
196         p_input = input_item_New( psz_mrl, psz_name );
197         input_item_CopyOptions( p_current_input, p_input );
198         input_item_node_AppendItem( p_subitems, p_input );
199         vlc_gc_decref( p_input );
200         free( psz_mrl_orig );
201     }
202     else
203     {
204         msg_Warn( p_demux, "no file= part found for item %d", i_item );
205     }
206     free( psz_name );
207     psz_name = NULL;
208
209     input_item_node_PostAndDelete( p_subitems );
210
211     vlc_gc_decref(p_current_input);
212     return 0; /* Needed for correct operation of go back */
213 }