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 */
40 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Demux( demux_t *p_demux);
54 static int Control( demux_t *p_demux, int i_query, va_list args );
56 static int StoreString( demux_t *p_demux, char **ppsz_string, char *psz_source_start, char *psz_source_end )
58 demux_sys_t *p_sys = p_demux->p_sys;
59 int i_strlen = psz_source_end-psz_source_start;
63 if( *ppsz_string ) free( *ppsz_string );
64 *ppsz_string = malloc( i_strlen*sizeof( char ) +1);
65 memcpy( *ppsz_string, psz_source_start, i_strlen );
66 (*ppsz_string)[i_strlen] = '\0';
69 EnsureUTF8( *ppsz_string );
73 psz_temp = FromLocaleDup( *ppsz_string );
77 *ppsz_string = psz_temp;
78 } else EnsureUTF8( *ppsz_string );
83 /*****************************************************************************
84 * Import_ASX: main import function
85 *****************************************************************************/
86 int E_(Import_ASX)( vlc_object_t *p_this )
88 demux_t *p_demux = (demux_t *)p_this;
93 psz_ext = strrchr ( p_demux->psz_path, '.' );
95 if( ( psz_ext && !strcasecmp( psz_ext, ".asx") ) ||
96 ( psz_ext && !strcasecmp( psz_ext, ".wax") ) ||
97 ( psz_ext && !strcasecmp( psz_ext, ".wvx") ) ||
98 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "asx-open") ) )
106 msg_Dbg( p_demux, "using asx playlist import");
108 p_demux->pf_control = Control;
109 p_demux->pf_demux = Demux;
110 p_demux->p_sys = p_sys = malloc( sizeof(demux_sys_t) );
113 msg_Err( p_demux, "out of memory" );
116 p_sys->psz_prefix = E_(FindPrefix)( p_demux );
117 p_sys->psz_data = NULL;
118 p_sys->i_data_len = -1;
119 p_sys->b_utf8 = VLC_FALSE;
124 /*****************************************************************************
125 * Deactivate: frees unused data
126 *****************************************************************************/
127 void E_(Close_ASX)( vlc_object_t *p_this )
129 demux_t *p_demux = (demux_t *)p_this;
130 demux_sys_t *p_sys = p_demux->p_sys;
132 if( p_sys->psz_prefix ) free( p_sys->psz_prefix );
133 if( p_sys->psz_data ) free( p_sys->psz_data );
137 static int Demux( demux_t *p_demux )
139 demux_sys_t *p_sys = p_demux->p_sys;
140 char *psz_parse = NULL;
141 char *psz_backup = NULL;
142 vlc_bool_t b_entry = VLC_FALSE;
147 if( p_sys->i_data_len < 0 )
150 p_sys->i_data_len = stream_Size( p_demux->s ) +1; /* This is a cheat to prevent unnecessary realloc */
151 if( p_sys->i_data_len <= 0 && p_sys->i_data_len < 16384 ) p_sys->i_data_len = 1024;
152 p_sys->psz_data = malloc( p_sys->i_data_len * sizeof(char) +1);
154 /* load the complete file */
157 int i_read = stream_Read( p_demux->s, &p_sys->psz_data[i_pos], p_sys->i_data_len - i_pos );
158 p_sys->psz_data[i_read] = '\0';
160 if( i_read < p_sys->i_data_len - i_pos ) break; /* Done */
163 p_sys->i_data_len += 1024;
164 p_sys->psz_data = realloc( p_sys->psz_data, p_sys->i_data_len * sizeof( char * ) +1 );
166 if( p_sys->i_data_len <= 0 ) return VLC_EGENERIC;
169 psz_parse = p_sys->psz_data;
170 /* Find first element */
171 if( ( psz_parse = strcasestr( psz_parse, "<ASX" ) ) )
174 char *psz_string = NULL;
177 char *psz_base_asx = NULL;
178 char *psz_title_asx = NULL;
179 char *psz_author_asx = NULL;
180 char *psz_copyright_asx = NULL;
181 char *psz_moreinfo_asx = NULL;
182 char *psz_abstract_asx = NULL;
184 char *psz_base_entry = NULL;
185 char *psz_title_entry = NULL;
186 char *psz_author_entry = NULL;
187 char *psz_copyright_entry = NULL;
188 char *psz_moreinfo_entry = NULL;
189 char *psz_abstract_entry = NULL;
190 int i_entry_count = 0;
192 psz_parse = strcasestr( psz_parse, ">" );
194 while( ( psz_parse = strcasestr( psz_parse, "<" ) ) && psz_parse && *psz_parse )
196 if( !strncasecmp( psz_parse, "<!--", 4 ) )
198 /* this is a comment */
199 if( ( psz_parse = strcasestr( psz_parse, "-->" ) ) )
203 else if( !strncasecmp( psz_parse, "<PARAM ", 7 ) )
205 vlc_bool_t b_encoding_flag = VLC_FALSE;
207 if( !strncasecmp( psz_parse, "name", 4 ) )
209 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
211 psz_backup = ++psz_parse;
212 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
214 i_strlen = psz_parse-psz_backup;
215 if( i_strlen < 1 ) continue;
216 msg_Dbg( p_demux, "param name strlen: %d", i_strlen);
217 psz_string = malloc( i_strlen *sizeof( char ) +1);
218 memcpy( psz_string, psz_backup, i_strlen );
219 psz_string[i_strlen] = '\0';
220 msg_Dbg( p_demux, "param name: %s", psz_string);
221 b_encoding_flag = !strcasecmp( psz_string, "encoding" );
229 if( !strncasecmp( psz_parse, "value", 5 ) )
231 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
233 psz_backup = ++psz_parse;
234 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
236 i_strlen = psz_parse-psz_backup;
237 if( i_strlen < 1 ) continue;
238 msg_Dbg( p_demux, "param value strlen: %d", i_strlen);
239 psz_string = malloc( i_strlen *sizeof( char ) +1);
240 memcpy( psz_string, psz_backup, i_strlen );
241 psz_string[i_strlen] = '\0';
242 msg_Dbg( p_demux, "param value: %s", psz_string);
243 if( b_encoding_flag && !strcasecmp( psz_string, "utf-8" ) ) p_sys->b_utf8 = VLC_TRUE;
250 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
254 else if( !strncasecmp( psz_parse, "<BANNER", 7 ) )
256 /* We skip this element */
257 if( ( psz_parse = strcasestr( psz_parse, "</BANNER>" ) ) )
261 else if( !strncasecmp( psz_parse, "<PREVIEWDURATION", 16 ) ||
262 !strncasecmp( psz_parse, "<LOGURL", 7 ) ||
263 !strncasecmp( psz_parse, "<Skin", 5 ) )
265 /* We skip this element */
266 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
270 else if( !strncasecmp( psz_parse, "<BASE ", 6 ) )
273 if( !strncasecmp( psz_parse, "HREF", 4 ) )
275 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
277 psz_backup = ++psz_parse;
278 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
280 StoreString( p_demux, (b_entry ? &psz_base_entry : &psz_base_asx), psz_backup, psz_parse );
286 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
290 else if( !strncasecmp( psz_parse, "<TITLE>", 7 ) )
292 psz_backup = psz_parse+=7;
293 if( ( psz_parse = strcasestr( psz_parse, "</TITLE>" ) ) )
295 StoreString( p_demux, (b_entry ? &psz_title_entry : &psz_title_asx), psz_backup, psz_parse );
300 else if( !strncasecmp( psz_parse, "<Author>", 8 ) )
302 psz_backup = psz_parse+=8;
303 if( ( psz_parse = strcasestr( psz_parse, "</Author>" ) ) )
305 StoreString( p_demux, (b_entry ? &psz_author_entry : &psz_author_asx), psz_backup, psz_parse );
310 else if( !strncasecmp( psz_parse, "<Copyright", 10 ) )
312 psz_backup = psz_parse+=11;
313 if( ( psz_parse = strcasestr( psz_parse, "</Copyright>" ) ) )
315 StoreString( p_demux, (b_entry ? &psz_copyright_entry : &psz_copyright_asx), psz_backup, psz_parse );
320 else if( !strncasecmp( psz_parse, "<MoreInfo ", 10 ) )
323 if( !strncasecmp( psz_parse, "HREF", 4 ) )
325 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
327 psz_backup = ++psz_parse;
328 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
330 StoreString( p_demux, (b_entry ? &psz_moreinfo_entry : &psz_moreinfo_asx), psz_backup, psz_parse );
336 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
340 else if( !strncasecmp( psz_parse, "<ABSTRACT>", 10 ) )
342 psz_backup = psz_parse+=10;
343 if( ( psz_parse = strcasestr( psz_parse, "</ABSTRACT>" ) ) )
345 StoreString( p_demux, (b_entry ? &psz_abstract_entry : &psz_abstract_asx), psz_backup, psz_parse );
350 else if( !strncasecmp( psz_parse, "<EntryRef ", 10 ) )
353 if( !strncasecmp( psz_parse, "HREF", 4 ) )
355 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
357 psz_backup = ++psz_parse;
358 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
360 i_strlen = psz_parse-psz_backup;
361 if( i_strlen < 1 ) continue;
362 psz_string = malloc( i_strlen*sizeof( char ) +1);
363 memcpy( psz_string, psz_backup, i_strlen );
364 psz_string[i_strlen] = '\0';
365 p_input = input_ItemNew( p_playlist, psz_string, psz_title_asx );
366 vlc_input_item_CopyOptions( p_current->p_input, p_input );
367 playlist_AddWhereverNeeded( p_playlist, p_input, p_current,
368 p_item_in_category, (i_parent_id > 0 )? VLC_TRUE : VLC_FALSE,
376 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
380 else if( !strncasecmp( psz_parse, "</Entry>", 8 ) )
382 /* add a new entry */
386 msg_Err( p_demux, "end of entry without start?" );
390 FREE( psz_title_entry )
391 FREE( psz_base_entry )
392 FREE( psz_author_entry )
393 FREE( psz_copyright_entry )
394 FREE( psz_moreinfo_entry )
395 FREE( psz_abstract_entry )
398 else if( !strncasecmp( psz_parse, "<Entry>", 7 ) )
403 msg_Err( p_demux, "We already are in an entry section" );
409 else if( !strncasecmp( psz_parse, "<Ref ", 5 ) )
414 msg_Err( p_demux, "A ref outside an entry section" );
418 if( !strncasecmp( psz_parse, "HREF", 4 ) )
420 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
422 psz_backup = ++psz_parse;
423 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
425 input_item_t *p_entry = NULL;
426 char *psz_name = NULL;
427 i_strlen = psz_parse-psz_backup;
428 if( i_strlen < 1 ) continue;
429 psz_string = malloc( i_strlen*sizeof( char ) +1);
430 memcpy( psz_string, psz_backup, i_strlen );
431 psz_string[i_strlen] = '\0';
433 /* create the new entry */
434 asprintf( &psz_name, "%d %s", i_entry_count, ( psz_title_entry ? psz_title_entry : p_current->p_input->psz_name ) );
435 p_entry = input_ItemNew( p_playlist, psz_string, psz_name );
438 vlc_input_item_CopyOptions( p_current->p_input, p_entry );
439 p_entry->p_meta = vlc_meta_New();
440 if( psz_title_entry ) vlc_meta_SetTitle( p_entry->p_meta, psz_title_entry );
441 if( psz_author_entry ) vlc_meta_SetAuthor( p_entry->p_meta, psz_author_entry );
442 if( psz_copyright_entry ) vlc_meta_SetCopyright( p_entry->p_meta, psz_copyright_entry );
443 if( psz_moreinfo_entry ) vlc_meta_SetURL( p_entry->p_meta, psz_moreinfo_entry );
444 if( psz_abstract_entry ) vlc_meta_SetDescription( p_entry->p_meta, psz_abstract_entry );
446 playlist_AddWhereverNeeded( p_playlist, p_entry, p_current,
447 p_item_in_category, (i_parent_id > 0 )? VLC_TRUE : VLC_FALSE,
455 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
459 else if( !strncasecmp( psz_parse, "</ASX", 5 ) )
461 vlc_mutex_lock( &p_current->p_input->lock );
462 if( !p_current->p_input->p_meta ) p_current->p_input->p_meta = vlc_meta_New();
463 if( psz_title_asx ) vlc_meta_SetTitle( p_current->p_input->p_meta, psz_title_asx );
464 if( psz_author_asx ) vlc_meta_SetAuthor( p_current->p_input->p_meta, psz_author_asx );
465 if( psz_copyright_asx ) vlc_meta_SetCopyright( p_current->p_input->p_meta, psz_copyright_asx );
466 if( psz_moreinfo_asx ) vlc_meta_SetURL( p_current->p_input->p_meta, psz_moreinfo_asx );
467 if( psz_abstract_asx ) vlc_meta_SetDescription( p_current->p_input->p_meta, psz_abstract_asx );
468 vlc_mutex_unlock( &p_current->p_input->lock );
469 FREE( psz_base_asx );
470 FREE( psz_title_asx );
471 FREE( psz_author_asx );
472 FREE( psz_copyright_asx );
473 FREE( psz_moreinfo_asx );
474 FREE( psz_abstract_asx );
480 /* FIXME Unsupported elements */
490 HANDLE_PLAY_AND_RELEASE;
494 static int Control( demux_t *p_demux, int i_query, va_list args )