1 /*****************************************************************************
2 * asx.c : ASX playlist format import
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /* See also: http://msdn.microsoft.com/library/en-us/wmplay10/mmp_sdk/windowsmediametafilereference.asp
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <stdlib.h> /* malloc(), free() */
31 #include <ctype.h> /* isspace() */
34 #include <vlc/input.h>
36 #include <errno.h> /* ENOMEM */
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Demux( demux_t *p_demux);
53 static int Control( demux_t *p_demux, int i_query, va_list args );
55 static int StoreString( demux_t *p_demux, char **ppsz_string, char *psz_source_start, char *psz_source_end )
57 demux_sys_t *p_sys = p_demux->p_sys;
58 int i_strlen = psz_source_end-psz_source_start;
62 if( *ppsz_string ) free( *ppsz_string );
63 *ppsz_string = malloc( i_strlen*sizeof( char ) +1);
64 memcpy( *ppsz_string, psz_source_start, i_strlen );
65 (*ppsz_string)[i_strlen] = '\0';
68 EnsureUTF8( *ppsz_string );
72 psz_temp = FromLocaleDup( *ppsz_string );
76 *ppsz_string = psz_temp;
77 } else EnsureUTF8( *ppsz_string );
82 /*****************************************************************************
83 * Import_ASX: main import function
84 *****************************************************************************/
85 int E_(Import_ASX)( vlc_object_t *p_this )
87 demux_t *p_demux = (demux_t *)p_this;
88 uint8_t *p_peek, *p_peek_stop;
89 CHECK_PEEK( p_peek, 10 );
91 p_peek_stop = p_peek+6;
93 // skip over possible leading empty lines
94 while( (p_peek < p_peek_stop) && (*p_peek == '\n' || *p_peek == '\r')) ++p_peek;
96 if( POKE( p_peek, "<asx", 4 ) || isExtension( p_demux, ".asx" ) ||
97 isExtension( p_demux, ".wax" ) || isExtension( p_demux, ".wvx" ) ||
98 isDemux( p_demux, "asx-open" ) )
105 STANDARD_DEMUX_INIT_MSG( "found valid ASX playlist" );
106 p_demux->p_sys->psz_prefix = E_(FindPrefix)( p_demux );
107 p_demux->p_sys->psz_data = NULL;
108 p_demux->p_sys->i_data_len = -1;
109 p_demux->p_sys->b_utf8 = VLC_FALSE;
114 /*****************************************************************************
115 * Deactivate: frees unused data
116 *****************************************************************************/
117 void E_(Close_ASX)( vlc_object_t *p_this )
119 demux_t *p_demux = (demux_t *)p_this;
120 demux_sys_t *p_sys = p_demux->p_sys;
122 if( p_sys->psz_prefix ) free( p_sys->psz_prefix );
123 if( p_sys->psz_data ) free( p_sys->psz_data );
127 static int Demux( demux_t *p_demux )
129 demux_sys_t *p_sys = p_demux->p_sys;
130 char *psz_parse = NULL;
131 char *psz_backup = NULL;
132 vlc_bool_t b_entry = VLC_FALSE;
137 if( p_sys->i_data_len < 0 )
140 p_sys->i_data_len = stream_Size( p_demux->s ) +1; /* This is a cheat to prevent unnecessary realloc */
141 if( p_sys->i_data_len <= 0 && p_sys->i_data_len < 16384 ) p_sys->i_data_len = 1024;
142 p_sys->psz_data = malloc( p_sys->i_data_len * sizeof(char) +1);
144 /* load the complete file */
147 int i_read = stream_Read( p_demux->s, &p_sys->psz_data[i_pos], p_sys->i_data_len - i_pos );
148 p_sys->psz_data[i_read] = '\0';
150 if( i_read < p_sys->i_data_len - i_pos ) break; /* Done */
153 p_sys->i_data_len += 1024;
154 p_sys->psz_data = realloc( p_sys->psz_data, p_sys->i_data_len * sizeof( char * ) +1 );
156 if( p_sys->i_data_len <= 0 ) return VLC_EGENERIC;
159 psz_parse = p_sys->psz_data;
160 /* Find first element */
161 if( ( psz_parse = strcasestr( psz_parse, "<ASX" ) ) )
164 char *psz_string = NULL;
167 char *psz_base_asx = NULL;
168 char *psz_title_asx = NULL;
169 char *psz_author_asx = NULL;
170 char *psz_copyright_asx = NULL;
171 char *psz_moreinfo_asx = NULL;
172 char *psz_abstract_asx = NULL;
174 char *psz_base_entry = NULL;
175 char *psz_title_entry = NULL;
176 char *psz_author_entry = NULL;
177 char *psz_copyright_entry = NULL;
178 char *psz_moreinfo_entry = NULL;
179 char *psz_abstract_entry = NULL;
180 int i_entry_count = 0;
182 psz_parse = strcasestr( psz_parse, ">" );
184 while( ( psz_parse = strcasestr( psz_parse, "<" ) ) && psz_parse && *psz_parse )
186 if( !strncasecmp( psz_parse, "<!--", 4 ) )
188 /* this is a comment */
189 if( ( psz_parse = strcasestr( psz_parse, "-->" ) ) )
193 else if( !strncasecmp( psz_parse, "<PARAM ", 7 ) )
195 vlc_bool_t b_encoding_flag = VLC_FALSE;
197 if( !strncasecmp( psz_parse, "name", 4 ) )
199 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
201 psz_backup = ++psz_parse;
202 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
204 i_strlen = psz_parse-psz_backup;
205 if( i_strlen < 1 ) continue;
206 msg_Dbg( p_demux, "param name strlen: %d", i_strlen);
207 psz_string = malloc( i_strlen *sizeof( char ) +1);
208 memcpy( psz_string, psz_backup, i_strlen );
209 psz_string[i_strlen] = '\0';
210 msg_Dbg( p_demux, "param name: %s", psz_string);
211 b_encoding_flag = !strcasecmp( psz_string, "encoding" );
219 if( !strncasecmp( psz_parse, "value", 5 ) )
221 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
223 psz_backup = ++psz_parse;
224 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
226 i_strlen = psz_parse-psz_backup;
227 if( i_strlen < 1 ) continue;
228 msg_Dbg( p_demux, "param value strlen: %d", i_strlen);
229 psz_string = malloc( i_strlen *sizeof( char ) +1);
230 memcpy( psz_string, psz_backup, i_strlen );
231 psz_string[i_strlen] = '\0';
232 msg_Dbg( p_demux, "param value: %s", psz_string);
233 if( b_encoding_flag && !strcasecmp( psz_string, "utf-8" ) ) p_sys->b_utf8 = VLC_TRUE;
240 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
244 else if( !strncasecmp( psz_parse, "<BANNER", 7 ) )
246 /* We skip this element */
247 if( ( psz_parse = strcasestr( psz_parse, "</BANNER>" ) ) )
251 else if( !strncasecmp( psz_parse, "<PREVIEWDURATION", 16 ) ||
252 !strncasecmp( psz_parse, "<LOGURL", 7 ) ||
253 !strncasecmp( psz_parse, "<Skin", 5 ) )
255 /* We skip this element */
256 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
260 else if( !strncasecmp( psz_parse, "<BASE ", 6 ) )
263 if( !strncasecmp( psz_parse, "HREF", 4 ) )
265 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
267 psz_backup = ++psz_parse;
268 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
270 StoreString( p_demux, (b_entry ? &psz_base_entry : &psz_base_asx), psz_backup, psz_parse );
276 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
280 else if( !strncasecmp( psz_parse, "<TITLE>", 7 ) )
282 psz_backup = psz_parse+=7;
283 if( ( psz_parse = strcasestr( psz_parse, "</TITLE>" ) ) )
285 StoreString( p_demux, (b_entry ? &psz_title_entry : &psz_title_asx), psz_backup, psz_parse );
290 else if( !strncasecmp( psz_parse, "<Author>", 8 ) )
292 psz_backup = psz_parse+=8;
293 if( ( psz_parse = strcasestr( psz_parse, "</Author>" ) ) )
295 StoreString( p_demux, (b_entry ? &psz_author_entry : &psz_author_asx), psz_backup, psz_parse );
300 else if( !strncasecmp( psz_parse, "<Copyright", 10 ) )
302 psz_backup = psz_parse+=11;
303 if( ( psz_parse = strcasestr( psz_parse, "</Copyright>" ) ) )
305 StoreString( p_demux, (b_entry ? &psz_copyright_entry : &psz_copyright_asx), psz_backup, psz_parse );
310 else if( !strncasecmp( psz_parse, "<MoreInfo ", 10 ) )
313 if( !strncasecmp( psz_parse, "HREF", 4 ) )
315 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
317 psz_backup = ++psz_parse;
318 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
320 StoreString( p_demux, (b_entry ? &psz_moreinfo_entry : &psz_moreinfo_asx), psz_backup, psz_parse );
326 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
330 else if( !strncasecmp( psz_parse, "<ABSTRACT>", 10 ) )
332 psz_backup = psz_parse+=10;
333 if( ( psz_parse = strcasestr( psz_parse, "</ABSTRACT>" ) ) )
335 StoreString( p_demux, (b_entry ? &psz_abstract_entry : &psz_abstract_asx), psz_backup, psz_parse );
340 else if( !strncasecmp( psz_parse, "<EntryRef ", 10 ) )
343 if( !strncasecmp( psz_parse, "HREF", 4 ) )
345 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
347 psz_backup = ++psz_parse;
348 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
350 i_strlen = psz_parse-psz_backup;
351 if( i_strlen < 1 ) continue;
352 psz_string = malloc( i_strlen*sizeof( char ) +1);
353 memcpy( psz_string, psz_backup, i_strlen );
354 psz_string[i_strlen] = '\0';
355 p_input = input_ItemNew( p_playlist, psz_string, psz_title_asx );
356 input_ItemCopyOptions( p_current->p_input, p_input );
357 playlist_AddWhereverNeeded( p_playlist, p_input, p_current,
358 p_item_in_category, (i_parent_id > 0 )? VLC_TRUE : VLC_FALSE,
366 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
370 else if( !strncasecmp( psz_parse, "</Entry>", 8 ) )
372 /* add a new entry */
376 msg_Err( p_demux, "end of entry without start?" );
380 FREENULL( psz_title_entry )
381 FREENULL( psz_base_entry )
382 FREENULL( psz_author_entry )
383 FREENULL( psz_copyright_entry )
384 FREENULL( psz_moreinfo_entry )
385 FREENULL( psz_abstract_entry )
388 else if( !strncasecmp( psz_parse, "<Entry", 6 ) )
393 msg_Err( p_demux, "We already are in an entry section" );
398 psz_parse = strcasestr( psz_parse, ">" );
400 else if( !strncasecmp( psz_parse, "<Ref ", 5 ) )
405 msg_Err( p_demux, "A ref outside an entry section" );
409 if( !strncasecmp( psz_parse, "HREF", 4 ) )
411 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
413 psz_backup = ++psz_parse;
414 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
416 input_item_t *p_entry = NULL;
417 char *psz_name = NULL;
418 i_strlen = psz_parse-psz_backup;
419 if( i_strlen < 1 ) continue;
420 psz_string = malloc( i_strlen*sizeof( char ) +1);
421 memcpy( psz_string, psz_backup, i_strlen );
422 psz_string[i_strlen] = '\0';
424 /* create the new entry */
425 asprintf( &psz_name, "%d %s", i_entry_count, ( psz_title_entry ? psz_title_entry : p_current->p_input->psz_name ) );
426 p_entry = input_ItemNew( p_playlist, psz_string, psz_name );
427 FREENULL( psz_name );
429 input_ItemCopyOptions( p_current->p_input, p_entry );
430 p_entry->p_meta = vlc_meta_New();
431 if( psz_title_entry ) vlc_meta_SetTitle( p_entry->p_meta, psz_title_entry );
432 if( psz_author_entry ) vlc_meta_SetAuthor( p_entry->p_meta, psz_author_entry );
433 if( psz_copyright_entry ) vlc_meta_SetCopyright( p_entry->p_meta, psz_copyright_entry );
434 if( psz_moreinfo_entry ) vlc_meta_SetURL( p_entry->p_meta, psz_moreinfo_entry );
435 if( psz_abstract_entry ) vlc_meta_SetDescription( p_entry->p_meta, psz_abstract_entry );
437 playlist_AddWhereverNeeded( p_playlist, p_entry, p_current,
438 p_item_in_category, (i_parent_id > 0 )? VLC_TRUE : VLC_FALSE,
446 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
450 else if( !strncasecmp( psz_parse, "</ASX", 5 ) )
452 vlc_mutex_lock( &p_current->p_input->lock );
453 if( !p_current->p_input->p_meta ) p_current->p_input->p_meta = vlc_meta_New();
454 if( psz_title_asx ) vlc_meta_SetTitle( p_current->p_input->p_meta, psz_title_asx );
455 if( psz_author_asx ) vlc_meta_SetAuthor( p_current->p_input->p_meta, psz_author_asx );
456 if( psz_copyright_asx ) vlc_meta_SetCopyright( p_current->p_input->p_meta, psz_copyright_asx );
457 if( psz_moreinfo_asx ) vlc_meta_SetURL( p_current->p_input->p_meta, psz_moreinfo_asx );
458 if( psz_abstract_asx ) vlc_meta_SetDescription( p_current->p_input->p_meta, psz_abstract_asx );
459 vlc_mutex_unlock( &p_current->p_input->lock );
460 FREENULL( psz_base_asx );
461 FREENULL( psz_title_asx );
462 FREENULL( psz_author_asx );
463 FREENULL( psz_copyright_asx );
464 FREENULL( psz_moreinfo_asx );
465 FREENULL( psz_abstract_asx );
471 /* FIXME Unsupported elements */
481 HANDLE_PLAY_AND_RELEASE;
485 static int Control( demux_t *p_demux, int i_query, va_list args )