1 /*****************************************************************************
2 * asx.c : ASX playlist format import
3 *****************************************************************************
4 * Copyright (C) 2005-2006 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 <ctype.h> /* isspace() */
33 #include <vlc_demux.h>
35 #include <errno.h> /* ENOMEM */
36 #include <vlc_charset.h>
46 vlc_bool_t b_skip_ads;
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,
56 const char *psz_source_start,
57 const char *psz_source_end )
59 demux_sys_t *p_sys = p_demux->p_sys;
60 unsigned len = psz_source_end - psz_source_start;
62 if( *ppsz_string ) free( *ppsz_string );
64 char *buf = *ppsz_string = malloc ((len * (1 + !p_sys->b_utf8)) + 1);
70 memcpy (buf, psz_source_start, len);
71 (*ppsz_string)[len] = '\0';
72 EnsureUTF8 (*ppsz_string);
76 /* Latin-1 -> UTF-8 */
77 for (unsigned i = 0; i < len; i++)
79 unsigned char c = psz_source_start[i];
82 *buf++ = 0xc0 | (c >> 6);
83 *buf++ = 0x80 | (c & 0x3f);
90 buf = *ppsz_string = realloc (*ppsz_string, buf - *ppsz_string);
95 static char *SkipBlanks(char *s, size_t i_strlen )
97 while( i_strlen > 0 ) {
114 static int ParseTime(char *s, size_t i_strlen)
116 // need to parse hour:minutes:sec.fraction string
119 const char *end = s + i_strlen;
120 // skip leading spaces if any
121 s = SkipBlanks(s, i_strlen);
124 while( (s < end) && isdigit(*s) )
126 int newval = val*10 + (*s - '0');
137 s = SkipBlanks(s, end-s);
141 s = SkipBlanks(s, end-s);
142 result = result * 60;
144 while( (s < end) && isdigit(*s) )
146 int newval = val*10 + (*s - '0');
157 s = SkipBlanks(s, end-s);
161 s = SkipBlanks(s, end-s);
162 result = result * 60;
164 while( (s < end) && isdigit(*s) )
166 int newval = val*10 + (*s - '0');
177 // TODO: one day, we may need to parse fraction for sub-second resolution
183 /*****************************************************************************
184 * Import_ASX: main import function
185 *****************************************************************************/
186 int E_(Import_ASX)( vlc_object_t *p_this )
188 demux_t *p_demux = (demux_t *)p_this;
190 CHECK_PEEK( p_peek, 10 );
192 // skip over possible leading empty lines and empty spaces
193 p_peek = (uint8_t *)SkipBlanks((char *)p_peek, 6);
195 if( POKE( p_peek, "<asx", 4 ) || demux2_IsPathExtension( p_demux, ".asx" ) ||
196 demux2_IsPathExtension( p_demux, ".wax" ) || demux2_IsPathExtension( p_demux, ".wvx" ) ||
197 demux2_IsForced( p_demux, "asx-open" ) )
204 STANDARD_DEMUX_INIT_MSG( "found valid ASX playlist" );
205 p_demux->p_sys->psz_prefix = E_(FindPrefix)( p_demux );
206 p_demux->p_sys->psz_data = NULL;
207 p_demux->p_sys->i_data_len = -1;
208 p_demux->p_sys->b_utf8 = VLC_FALSE;
209 p_demux->p_sys->b_skip_ads = config_GetInt( p_demux, "playlist-skip-ads" );
214 /*****************************************************************************
215 * Deactivate: frees unused data
216 *****************************************************************************/
217 void E_(Close_ASX)( vlc_object_t *p_this )
219 demux_t *p_demux = (demux_t *)p_this;
220 demux_sys_t *p_sys = p_demux->p_sys;
222 if( p_sys->psz_prefix ) free( p_sys->psz_prefix );
223 if( p_sys->psz_data ) free( p_sys->psz_data );
227 static int Demux( demux_t *p_demux )
229 demux_sys_t *p_sys = p_demux->p_sys;
230 char *psz_parse = NULL;
231 char *psz_backup = NULL;
232 vlc_bool_t b_entry = VLC_FALSE;
233 input_item_t *p_input;
237 if( p_sys->i_data_len < 0 )
240 p_sys->i_data_len = stream_Size( p_demux->s ) +1; /* This is a cheat to prevent unnecessary realloc */
241 if( p_sys->i_data_len <= 0 && p_sys->i_data_len < 16384 ) p_sys->i_data_len = 1024;
242 p_sys->psz_data = malloc( p_sys->i_data_len * sizeof(char) +1);
244 /* load the complete file */
247 int i_read = stream_Read( p_demux->s, &p_sys->psz_data[i_pos], p_sys->i_data_len - i_pos );
248 p_sys->psz_data[i_read] = '\0';
250 if( i_read < p_sys->i_data_len - i_pos ) break; /* Done */
253 p_sys->i_data_len += 1024;
254 p_sys->psz_data = realloc( p_sys->psz_data, p_sys->i_data_len * sizeof( char * ) +1 );
256 if( p_sys->i_data_len <= 0 ) return VLC_EGENERIC;
259 psz_parse = p_sys->psz_data;
260 /* Find first element */
261 if( ( psz_parse = strcasestr( psz_parse, "<ASX" ) ) )
264 char *psz_string = NULL;
267 char *psz_base_asx = NULL;
268 char *psz_title_asx = NULL;
269 char *psz_artist_asx = NULL;
270 char *psz_copyright_asx = NULL;
271 char *psz_moreinfo_asx = NULL;
272 char *psz_abstract_asx = NULL;
274 char *psz_base_entry = NULL;
275 char *psz_title_entry = NULL;
276 char *psz_artist_entry = NULL;
277 char *psz_copyright_entry = NULL;
278 char *psz_moreinfo_entry = NULL;
279 char *psz_abstract_entry = NULL;
280 int i_entry_count = 0;
281 vlc_bool_t b_skip_entry = VLC_FALSE;
283 char *psz_href = NULL;
287 psz_parse = strcasestr( psz_parse, ">" );
289 while( psz_parse && ( psz_parse = strcasestr( psz_parse, "<" ) ) )
291 if( !strncasecmp( psz_parse, "<!--", 4 ) )
293 /* this is a comment */
294 if( ( psz_parse = strcasestr( psz_parse, "-->" ) ) )
298 else if( !strncasecmp( psz_parse, "<PARAM ", 7 ) )
300 vlc_bool_t b_encoding_flag = VLC_FALSE;
301 psz_parse = SkipBlanks(psz_parse+7, (unsigned)-1);
302 if( !strncasecmp( psz_parse, "name", 4 ) )
304 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
306 psz_backup = ++psz_parse;
307 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
309 i_strlen = psz_parse-psz_backup;
310 if( i_strlen < 1 ) continue;
311 msg_Dbg( p_demux, "param name strlen: %d", i_strlen);
312 psz_string = malloc( i_strlen *sizeof( char ) +1);
313 memcpy( psz_string, psz_backup, i_strlen );
314 psz_string[i_strlen] = '\0';
315 msg_Dbg( p_demux, "param name: %s", psz_string);
316 b_encoding_flag = !strcasecmp( psz_string, "encoding" );
324 if( !strncasecmp( psz_parse, "value", 5 ) )
326 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
328 psz_backup = ++psz_parse;
329 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
331 i_strlen = psz_parse-psz_backup;
332 if( i_strlen < 1 ) continue;
333 msg_Dbg( p_demux, "param value strlen: %d", i_strlen);
334 psz_string = malloc( i_strlen *sizeof( char ) +1);
335 memcpy( psz_string, psz_backup, i_strlen );
336 psz_string[i_strlen] = '\0';
337 msg_Dbg( p_demux, "param value: %s", psz_string);
338 if( b_encoding_flag && !strcasecmp( psz_string, "utf-8" ) ) p_sys->b_utf8 = VLC_TRUE;
345 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
349 else if( !strncasecmp( psz_parse, "<BANNER", 7 ) )
351 /* We skip this element */
352 if( ( psz_parse = strcasestr( psz_parse, "</BANNER>" ) ) )
356 else if( !strncasecmp( psz_parse, "<PREVIEWDURATION", 16 ) ||
357 !strncasecmp( psz_parse, "<LOGURL", 7 ) ||
358 !strncasecmp( psz_parse, "<Skin", 5 ) )
360 /* We skip this element */
361 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
365 else if( !strncasecmp( psz_parse, "<BASE ", 6 ) )
367 psz_parse = SkipBlanks(psz_parse+6, (unsigned)-1);
368 if( !strncasecmp( psz_parse, "HREF", 4 ) )
370 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
372 psz_backup = ++psz_parse;
373 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
375 StoreString( p_demux, (b_entry ? &psz_base_entry : &psz_base_asx), psz_backup, psz_parse );
381 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
385 else if( !strncasecmp( psz_parse, "<TITLE>", 7 ) )
387 psz_backup = psz_parse+=7;
388 if( ( psz_parse = strcasestr( psz_parse, "</TITLE>" ) ) )
390 StoreString( p_demux, (b_entry ? &psz_title_entry : &psz_title_asx), psz_backup, psz_parse );
395 else if( !strncasecmp( psz_parse, "<Author>", 8 ) )
397 psz_backup = psz_parse+=8;
398 if( ( psz_parse = strcasestr( psz_parse, "</Author>" ) ) )
400 StoreString( p_demux, (b_entry ? &psz_artist_entry : &psz_artist_asx), psz_backup, psz_parse );
405 else if( !strncasecmp( psz_parse, "<Copyright", 10 ) )
407 psz_backup = psz_parse+=11;
408 if( ( psz_parse = strcasestr( psz_parse, "</Copyright>" ) ) )
410 StoreString( p_demux, (b_entry ? &psz_copyright_entry : &psz_copyright_asx), psz_backup, psz_parse );
415 else if( !strncasecmp( psz_parse, "<MoreInfo ", 10 ) )
417 psz_parse = SkipBlanks(psz_parse+10, (unsigned)-1);
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 StoreString( p_demux, (b_entry ? &psz_moreinfo_entry : &psz_moreinfo_asx), psz_backup, psz_parse );
431 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
435 else if( !strncasecmp( psz_parse, "<ABSTRACT>", 10 ) )
437 psz_backup = psz_parse+=10;
438 if( ( psz_parse = strcasestr( psz_parse, "</ABSTRACT>" ) ) )
440 StoreString( p_demux, (b_entry ? &psz_abstract_entry : &psz_abstract_asx), psz_backup, psz_parse );
445 else if( !strncasecmp( psz_parse, "<EntryRef ", 10 ) )
447 psz_parse = SkipBlanks(psz_parse+10, (unsigned)-1);
448 if( !strncasecmp( psz_parse, "HREF", 4 ) )
450 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
452 psz_backup = ++psz_parse;
453 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
455 i_strlen = psz_parse-psz_backup;
456 if( i_strlen < 1 ) continue;
457 psz_string = malloc( i_strlen*sizeof( char ) +1);
458 memcpy( psz_string, psz_backup, i_strlen );
459 psz_string[i_strlen] = '\0';
460 p_input = input_ItemNew( p_playlist, psz_string, psz_title_asx );
461 input_ItemCopyOptions( p_current_input, p_input );
462 input_ItemAddSubItem( p_current_input, p_input );
469 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
473 else if( !strncasecmp( psz_parse, "</Entry>", 8 ) )
475 input_item_t *p_entry = NULL;
476 char *psz_name = NULL;
478 char * ppsz_options[2];
481 /* add a new entry */
485 msg_Err( p_demux, "end of entry without start?" );
491 msg_Err( p_demux, "entry without href?" );
495 if( p_sys->b_skip_ads && b_skip_entry )
497 msg_Dbg( p_demux, "skipped entry %d %s (%s)",
498 i_entry_count, ( psz_title_entry ? psz_title_entry : p_current_input->psz_name ), psz_href );
502 if( i_starttime || i_duration )
505 asprintf(ppsz_options+i_options, ":start-time=%d", i_starttime);
509 asprintf(ppsz_options+i_options, ":stop-time=%d", i_starttime + i_duration);
514 /* create the new entry */
515 asprintf( &psz_name, "%d %s", i_entry_count, ( psz_title_entry ? psz_title_entry : p_current_input->psz_name ) );
517 p_entry = input_ItemNewExt( p_playlist, psz_href, psz_name, i_options, (const char * const *)ppsz_options, -1 );
518 FREENULL( psz_name );
519 input_ItemCopyOptions( p_current_input, p_entry );
522 psz_name = ppsz_options[--i_options];
526 if( psz_title_entry ) input_item_SetTitle( p_entry, psz_title_entry );
527 if( psz_artist_entry ) input_item_SetArtist( p_entry, psz_artist_entry );
528 if( psz_copyright_entry ) input_item_SetCopyright( p_entry, psz_copyright_entry );
529 if( psz_moreinfo_entry ) input_item_SetURL( p_entry, psz_moreinfo_entry );
530 if( psz_abstract_entry ) input_item_SetDescription( p_entry, psz_abstract_entry );
531 input_ItemAddSubItem( p_current_input, p_entry );
536 FREENULL( psz_title_entry )
537 FREENULL( psz_base_entry )
538 FREENULL( psz_artist_entry )
539 FREENULL( psz_copyright_entry )
540 FREENULL( psz_moreinfo_entry )
541 FREENULL( psz_abstract_entry )
544 else if( !strncasecmp( psz_parse, "<Entry", 6 ) )
546 char *psz_clientskip;
550 msg_Err( p_demux, "We already are in an entry section" );
555 psz_clientskip = strcasestr( psz_parse, "clientskip=\"no\"" );
556 psz_parse = strcasestr( psz_parse, ">" );
558 /* If clientskip was enabled ... this is an ad */
559 b_skip_entry = (NULL != psz_clientskip) && (psz_clientskip < psz_parse);
561 // init entry details
567 else if( !strncasecmp( psz_parse, "<Ref ", 5 ) )
569 psz_parse = SkipBlanks(psz_parse+5, (unsigned)-1);
572 msg_Err( p_demux, "A ref outside an entry section" );
576 if( !strncasecmp( psz_parse, "HREF", 4 ) )
578 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
580 psz_backup = ++psz_parse;
581 psz_backup = SkipBlanks(psz_backup, (unsigned)-1);
582 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
585 i_strlen = psz_parse-psz_backup;
586 if( i_strlen < 1 ) continue;
589 psz_href = malloc( i_strlen*sizeof( char ) +1);
590 memcpy( psz_href, psz_backup, i_strlen );
591 psz_href[i_strlen] = '\0';
592 psz_tmp = psz_href + (i_strlen-1);
593 while( psz_tmp >= psz_href &&
594 ( *psz_tmp == '\r' || *psz_tmp == '\n' ) )
604 if( ( psz_parse = strcasestr( psz_parse, ">" ) ) )
608 else if( !strncasecmp( psz_parse, "<starttime ", 11 ) )
610 psz_parse = SkipBlanks(psz_parse+11, (unsigned)-1);
613 msg_Err( p_demux, "starttime outside an entry section" );
617 if( !strncasecmp( psz_parse, "value", 5 ) )
619 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
621 psz_backup = ++psz_parse;
622 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
624 i_strlen = psz_parse-psz_backup;
625 if( i_strlen < 1 ) continue;
627 i_starttime = ParseTime(psz_backup, i_strlen);
633 if( ( psz_parse = strcasestr( psz_parse, ">" ) ) )
637 else if( !strncasecmp( psz_parse, "<duration ", 11 ) )
639 psz_parse = SkipBlanks(psz_parse+5, (unsigned)-1);
642 msg_Err( p_demux, "duration outside an entry section" );
646 if( !strncasecmp( psz_parse, "value", 5 ) )
648 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
650 psz_backup = ++psz_parse;
651 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
653 i_strlen = psz_parse-psz_backup;
654 if( i_strlen < 1 ) continue;
656 i_duration = ParseTime(psz_backup, i_strlen);
662 if( ( psz_parse = strcasestr( psz_parse, ">" ) ) )
666 else if( !strncasecmp( psz_parse, "</ASX", 5 ) )
668 if( psz_title_asx ) input_item_SetTitle( p_current_input, psz_title_asx );
669 if( psz_artist_asx ) input_item_SetArtist( p_current_input, psz_artist_asx );
670 if( psz_copyright_asx ) input_item_SetCopyright( p_current_input, psz_copyright_asx );
671 if( psz_moreinfo_asx ) input_item_SetURL( p_current_input, psz_moreinfo_asx );
672 if( psz_abstract_asx ) input_item_SetDescription( p_current_input, psz_abstract_asx );
673 FREENULL( psz_base_asx );
674 FREENULL( psz_title_asx );
675 FREENULL( psz_artist_asx );
676 FREENULL( psz_copyright_asx );
677 FREENULL( psz_moreinfo_asx );
678 FREENULL( psz_abstract_asx );
684 /* FIXME Unsupported elements */
692 HANDLE_PLAY_AND_RELEASE;
693 return -1; /* Needed for correct operation of go back */
696 static int Control( demux_t *p_demux, int i_query, va_list args )