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 *****************************************************************************/
31 #include <stdlib.h> /* malloc(), free() */
32 #include <ctype.h> /* isspace() */
35 #include <vlc/input.h>
37 #include <errno.h> /* ENOMEM */
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;
89 uint8_t *p_peek, *p_peek_stop;
90 CHECK_PEEK( p_peek, 10 );
92 p_peek_stop = p_peek+6;
94 // skip over possible leading empty lines
95 while( (p_peek < p_peek_stop) && (*p_peek == '\n' || *p_peek == '\r')) ++p_peek;
97 if( POKE( p_peek, "<asx", 4 ) || isExtension( p_demux, ".asx" ) ||
98 isExtension( p_demux, ".wax" ) || isExtension( p_demux, ".wvx" ) ||
99 isDemux( p_demux, "asx-open" ) )
106 STANDARD_DEMUX_INIT_MSG( "found valid ASX playlist" );
107 p_demux->p_sys->psz_prefix = E_(FindPrefix)( p_demux );
108 p_demux->p_sys->psz_data = NULL;
109 p_demux->p_sys->i_data_len = -1;
110 p_demux->p_sys->b_utf8 = VLC_FALSE;
115 /*****************************************************************************
116 * Deactivate: frees unused data
117 *****************************************************************************/
118 void E_(Close_ASX)( vlc_object_t *p_this )
120 demux_t *p_demux = (demux_t *)p_this;
121 demux_sys_t *p_sys = p_demux->p_sys;
123 if( p_sys->psz_prefix ) free( p_sys->psz_prefix );
124 if( p_sys->psz_data ) free( p_sys->psz_data );
128 static int Demux( demux_t *p_demux )
130 demux_sys_t *p_sys = p_demux->p_sys;
131 char *psz_parse = NULL;
132 char *psz_backup = NULL;
133 vlc_bool_t b_entry = VLC_FALSE;
138 if( p_sys->i_data_len < 0 )
141 p_sys->i_data_len = stream_Size( p_demux->s ) +1; /* This is a cheat to prevent unnecessary realloc */
142 if( p_sys->i_data_len <= 0 && p_sys->i_data_len < 16384 ) p_sys->i_data_len = 1024;
143 p_sys->psz_data = malloc( p_sys->i_data_len * sizeof(char) +1);
145 /* load the complete file */
148 int i_read = stream_Read( p_demux->s, &p_sys->psz_data[i_pos], p_sys->i_data_len - i_pos );
149 p_sys->psz_data[i_read] = '\0';
151 if( i_read < p_sys->i_data_len - i_pos ) break; /* Done */
154 p_sys->i_data_len += 1024;
155 p_sys->psz_data = realloc( p_sys->psz_data, p_sys->i_data_len * sizeof( char * ) +1 );
157 if( p_sys->i_data_len <= 0 ) return VLC_EGENERIC;
160 psz_parse = p_sys->psz_data;
161 /* Find first element */
162 if( ( psz_parse = strcasestr( psz_parse, "<ASX" ) ) )
165 char *psz_string = NULL;
168 char *psz_base_asx = NULL;
169 char *psz_title_asx = NULL;
170 char *psz_artist_asx = NULL;
171 char *psz_copyright_asx = NULL;
172 char *psz_moreinfo_asx = NULL;
173 char *psz_abstract_asx = NULL;
175 char *psz_base_entry = NULL;
176 char *psz_title_entry = NULL;
177 char *psz_artist_entry = NULL;
178 char *psz_copyright_entry = NULL;
179 char *psz_moreinfo_entry = NULL;
180 char *psz_abstract_entry = NULL;
181 int i_entry_count = 0;
183 psz_parse = strcasestr( psz_parse, ">" );
185 while( ( psz_parse = strcasestr( psz_parse, "<" ) ) && psz_parse && *psz_parse )
187 if( !strncasecmp( psz_parse, "<!--", 4 ) )
189 /* this is a comment */
190 if( ( psz_parse = strcasestr( psz_parse, "-->" ) ) )
194 else if( !strncasecmp( psz_parse, "<PARAM ", 7 ) )
196 vlc_bool_t b_encoding_flag = VLC_FALSE;
198 if( !strncasecmp( psz_parse, "name", 4 ) )
200 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
202 psz_backup = ++psz_parse;
203 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
205 i_strlen = psz_parse-psz_backup;
206 if( i_strlen < 1 ) continue;
207 msg_Dbg( p_demux, "param name strlen: %d", i_strlen);
208 psz_string = malloc( i_strlen *sizeof( char ) +1);
209 memcpy( psz_string, psz_backup, i_strlen );
210 psz_string[i_strlen] = '\0';
211 msg_Dbg( p_demux, "param name: %s", psz_string);
212 b_encoding_flag = !strcasecmp( psz_string, "encoding" );
220 if( !strncasecmp( psz_parse, "value", 5 ) )
222 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
224 psz_backup = ++psz_parse;
225 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
227 i_strlen = psz_parse-psz_backup;
228 if( i_strlen < 1 ) continue;
229 msg_Dbg( p_demux, "param value strlen: %d", i_strlen);
230 psz_string = malloc( i_strlen *sizeof( char ) +1);
231 memcpy( psz_string, psz_backup, i_strlen );
232 psz_string[i_strlen] = '\0';
233 msg_Dbg( p_demux, "param value: %s", psz_string);
234 if( b_encoding_flag && !strcasecmp( psz_string, "utf-8" ) ) p_sys->b_utf8 = VLC_TRUE;
241 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
245 else if( !strncasecmp( psz_parse, "<BANNER", 7 ) )
247 /* We skip this element */
248 if( ( psz_parse = strcasestr( psz_parse, "</BANNER>" ) ) )
252 else if( !strncasecmp( psz_parse, "<PREVIEWDURATION", 16 ) ||
253 !strncasecmp( psz_parse, "<LOGURL", 7 ) ||
254 !strncasecmp( psz_parse, "<Skin", 5 ) )
256 /* We skip this element */
257 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
261 else if( !strncasecmp( psz_parse, "<BASE ", 6 ) )
264 if( !strncasecmp( psz_parse, "HREF", 4 ) )
266 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
268 psz_backup = ++psz_parse;
269 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
271 StoreString( p_demux, (b_entry ? &psz_base_entry : &psz_base_asx), psz_backup, psz_parse );
277 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
281 else if( !strncasecmp( psz_parse, "<TITLE>", 7 ) )
283 psz_backup = psz_parse+=7;
284 if( ( psz_parse = strcasestr( psz_parse, "</TITLE>" ) ) )
286 StoreString( p_demux, (b_entry ? &psz_title_entry : &psz_title_asx), psz_backup, psz_parse );
291 else if( !strncasecmp( psz_parse, "<Author>", 8 ) )
293 psz_backup = psz_parse+=8;
294 if( ( psz_parse = strcasestr( psz_parse, "</Author>" ) ) )
296 StoreString( p_demux, (b_entry ? &psz_artist_entry : &psz_artist_asx), psz_backup, psz_parse );
301 else if( !strncasecmp( psz_parse, "<Copyright", 10 ) )
303 psz_backup = psz_parse+=11;
304 if( ( psz_parse = strcasestr( psz_parse, "</Copyright>" ) ) )
306 StoreString( p_demux, (b_entry ? &psz_copyright_entry : &psz_copyright_asx), psz_backup, psz_parse );
311 else if( !strncasecmp( psz_parse, "<MoreInfo ", 10 ) )
314 if( !strncasecmp( psz_parse, "HREF", 4 ) )
316 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
318 psz_backup = ++psz_parse;
319 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
321 StoreString( p_demux, (b_entry ? &psz_moreinfo_entry : &psz_moreinfo_asx), psz_backup, psz_parse );
327 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
331 else if( !strncasecmp( psz_parse, "<ABSTRACT>", 10 ) )
333 psz_backup = psz_parse+=10;
334 if( ( psz_parse = strcasestr( psz_parse, "</ABSTRACT>" ) ) )
336 StoreString( p_demux, (b_entry ? &psz_abstract_entry : &psz_abstract_asx), psz_backup, psz_parse );
341 else if( !strncasecmp( psz_parse, "<EntryRef ", 10 ) )
344 if( !strncasecmp( psz_parse, "HREF", 4 ) )
346 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
348 psz_backup = ++psz_parse;
349 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
351 i_strlen = psz_parse-psz_backup;
352 if( i_strlen < 1 ) continue;
353 psz_string = malloc( i_strlen*sizeof( char ) +1);
354 memcpy( psz_string, psz_backup, i_strlen );
355 psz_string[i_strlen] = '\0';
356 p_input = input_ItemNew( p_playlist, psz_string, psz_title_asx );
357 input_ItemCopyOptions( p_current->p_input, p_input );
358 playlist_AddWhereverNeeded( p_playlist, p_input, p_current,
359 p_item_in_category, (i_parent_id > 0 )? VLC_TRUE : VLC_FALSE,
367 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
371 else if( !strncasecmp( psz_parse, "</Entry>", 8 ) )
373 /* add a new entry */
377 msg_Err( p_demux, "end of entry without start?" );
381 FREENULL( psz_title_entry )
382 FREENULL( psz_base_entry )
383 FREENULL( psz_artist_entry )
384 FREENULL( psz_copyright_entry )
385 FREENULL( psz_moreinfo_entry )
386 FREENULL( psz_abstract_entry )
389 else if( !strncasecmp( psz_parse, "<Entry", 6 ) )
394 msg_Err( p_demux, "We already are in an entry section" );
399 psz_parse = strcasestr( psz_parse, ">" );
401 else if( !strncasecmp( psz_parse, "<Ref ", 5 ) )
406 msg_Err( p_demux, "A ref outside an entry section" );
410 if( !strncasecmp( psz_parse, "HREF", 4 ) )
412 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
414 psz_backup = ++psz_parse;
415 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
417 input_item_t *p_entry = NULL;
418 char *psz_name = NULL;
419 i_strlen = psz_parse-psz_backup;
420 if( i_strlen < 1 ) continue;
421 psz_string = malloc( i_strlen*sizeof( char ) +1);
422 memcpy( psz_string, psz_backup, i_strlen );
423 psz_string[i_strlen] = '\0';
425 /* create the new entry */
426 asprintf( &psz_name, "%d %s", i_entry_count, ( psz_title_entry ? psz_title_entry : p_current->p_input->psz_name ) );
427 p_entry = input_ItemNew( p_playlist, psz_string, psz_name );
428 FREENULL( psz_name );
430 input_ItemCopyOptions( p_current->p_input, p_entry );
431 p_entry->p_meta = vlc_meta_New();
432 if( psz_title_entry ) vlc_meta_SetTitle( p_entry->p_meta, psz_title_entry );
433 if( psz_artist_entry ) vlc_meta_SetArtist( p_entry->p_meta, psz_artist_entry );
434 if( psz_copyright_entry ) vlc_meta_SetCopyright( p_entry->p_meta, psz_copyright_entry );
435 if( psz_moreinfo_entry ) vlc_meta_SetURL( p_entry->p_meta, psz_moreinfo_entry );
436 if( psz_abstract_entry ) vlc_meta_SetDescription( p_entry->p_meta, psz_abstract_entry );
438 playlist_AddWhereverNeeded( p_playlist, p_entry, p_current,
439 p_item_in_category, (i_parent_id > 0 )? VLC_TRUE : VLC_FALSE,
447 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
451 else if( !strncasecmp( psz_parse, "</ASX", 5 ) )
453 vlc_mutex_lock( &p_current->p_input->lock );
454 if( !p_current->p_input->p_meta ) p_current->p_input->p_meta = vlc_meta_New();
455 if( psz_title_asx ) vlc_meta_SetTitle( p_current->p_input->p_meta, psz_title_asx );
456 if( psz_artist_asx ) vlc_meta_SetArtist( p_current->p_input->p_meta, psz_artist_asx );
457 if( psz_copyright_asx ) vlc_meta_SetCopyright( p_current->p_input->p_meta, psz_copyright_asx );
458 if( psz_moreinfo_asx ) vlc_meta_SetURL( p_current->p_input->p_meta, psz_moreinfo_asx );
459 if( psz_abstract_asx ) vlc_meta_SetDescription( p_current->p_input->p_meta, psz_abstract_asx );
460 vlc_mutex_unlock( &p_current->p_input->lock );
461 FREENULL( psz_base_asx );
462 FREENULL( psz_title_asx );
463 FREENULL( psz_artist_asx );
464 FREENULL( psz_copyright_asx );
465 FREENULL( psz_moreinfo_asx );
466 FREENULL( psz_abstract_asx );
472 /* FIXME Unsupported elements */
482 HANDLE_PLAY_AND_RELEASE;
486 static int Control( demux_t *p_demux, int i_query, va_list args )