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 *****************************************************************************/
31 #include <vlc_demux.h>
34 #include <vlc_charset.h>
44 vlc_bool_t b_skip_ads;
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Demux( demux_t *p_demux);
51 static int Control( demux_t *p_demux, int i_query, va_list args );
53 static int StoreString( demux_t *p_demux, char **ppsz_string,
54 const char *psz_source_start,
55 const char *psz_source_end )
57 demux_sys_t *p_sys = p_demux->p_sys;
58 unsigned len = psz_source_end - psz_source_start;
60 if( *ppsz_string ) free( *ppsz_string );
62 char *buf = *ppsz_string = malloc ((len * (1 + !p_sys->b_utf8)) + 1);
68 memcpy (buf, psz_source_start, len);
69 (*ppsz_string)[len] = '\0';
70 EnsureUTF8 (*ppsz_string);
74 /* Latin-1 -> UTF-8 */
75 for (unsigned i = 0; i < len; i++)
77 unsigned char c = psz_source_start[i];
80 *buf++ = 0xc0 | (c >> 6);
81 *buf++ = 0x80 | (c & 0x3f);
88 buf = *ppsz_string = realloc (*ppsz_string, buf - *ppsz_string);
93 static char *SkipBlanks(char *s, size_t i_strlen )
95 while( i_strlen > 0 ) {
112 static int ParseTime(char *s, size_t i_strlen)
114 // need to parse hour:minutes:sec.fraction string
117 const char *end = s + i_strlen;
118 // skip leading spaces if any
119 s = SkipBlanks(s, i_strlen);
122 while( (s < end) && isdigit(*s) )
124 int newval = val*10 + (*s - '0');
135 s = SkipBlanks(s, end-s);
139 s = SkipBlanks(s, end-s);
140 result = result * 60;
142 while( (s < end) && isdigit(*s) )
144 int newval = val*10 + (*s - '0');
155 s = SkipBlanks(s, end-s);
159 s = SkipBlanks(s, end-s);
160 result = result * 60;
162 while( (s < end) && isdigit(*s) )
164 int newval = val*10 + (*s - '0');
175 // TODO: one day, we may need to parse fraction for sub-second resolution
181 /*****************************************************************************
182 * Import_ASX: main import function
183 *****************************************************************************/
184 int E_(Import_ASX)( vlc_object_t *p_this )
186 demux_t *p_demux = (demux_t *)p_this;
187 const uint8_t *p_peek;
188 CHECK_PEEK( p_peek, 10 );
190 // skip over possible leading empty lines and empty spaces
191 p_peek = (uint8_t *)SkipBlanks((char *)p_peek, 6);
193 if( POKE( p_peek, "<asx", 4 ) || demux2_IsPathExtension( p_demux, ".asx" ) ||
194 demux2_IsPathExtension( p_demux, ".wax" ) || demux2_IsPathExtension( p_demux, ".wvx" ) ||
195 demux2_IsForced( p_demux, "asx-open" ) )
202 STANDARD_DEMUX_INIT_MSG( "found valid ASX playlist" );
203 p_demux->p_sys->psz_prefix = E_(FindPrefix)( p_demux );
204 p_demux->p_sys->psz_data = NULL;
205 p_demux->p_sys->i_data_len = -1;
206 p_demux->p_sys->b_utf8 = VLC_FALSE;
207 p_demux->p_sys->b_skip_ads = config_GetInt( p_demux, "playlist-skip-ads" );
212 /*****************************************************************************
213 * Deactivate: frees unused data
214 *****************************************************************************/
215 void E_(Close_ASX)( vlc_object_t *p_this )
217 demux_t *p_demux = (demux_t *)p_this;
218 demux_sys_t *p_sys = p_demux->p_sys;
220 if( p_sys->psz_prefix ) free( p_sys->psz_prefix );
221 if( p_sys->psz_data ) free( p_sys->psz_data );
225 static int Demux( demux_t *p_demux )
227 demux_sys_t *p_sys = p_demux->p_sys;
228 char *psz_parse = NULL;
229 char *psz_backup = NULL;
230 vlc_bool_t b_entry = VLC_FALSE;
231 input_item_t *p_input;
235 if( p_sys->i_data_len < 0 )
238 p_sys->i_data_len = stream_Size( p_demux->s ) +1; /* This is a cheat to prevent unnecessary realloc */
239 if( p_sys->i_data_len <= 0 && p_sys->i_data_len < 16384 ) p_sys->i_data_len = 1024;
240 p_sys->psz_data = malloc( p_sys->i_data_len * sizeof(char) +1);
242 /* load the complete file */
245 int i_read = stream_Read( p_demux->s, &p_sys->psz_data[i_pos], p_sys->i_data_len - i_pos );
246 p_sys->psz_data[i_read] = '\0';
248 if( i_read < p_sys->i_data_len - i_pos ) break; /* Done */
251 p_sys->i_data_len += 1024;
252 p_sys->psz_data = realloc( p_sys->psz_data, p_sys->i_data_len * sizeof( char * ) +1 );
254 if( p_sys->i_data_len <= 0 ) return -1;
257 psz_parse = p_sys->psz_data;
258 /* Find first element */
259 if( ( psz_parse = strcasestr( psz_parse, "<ASX" ) ) )
262 char *psz_string = NULL;
265 char *psz_base_asx = NULL;
266 char *psz_title_asx = NULL;
267 char *psz_artist_asx = NULL;
268 char *psz_copyright_asx = NULL;
269 char *psz_moreinfo_asx = NULL;
270 char *psz_abstract_asx = NULL;
272 char *psz_base_entry = NULL;
273 char *psz_title_entry = NULL;
274 char *psz_artist_entry = NULL;
275 char *psz_copyright_entry = NULL;
276 char *psz_moreinfo_entry = NULL;
277 char *psz_abstract_entry = NULL;
278 int i_entry_count = 0;
279 vlc_bool_t b_skip_entry = VLC_FALSE;
281 char *psz_href = NULL;
285 psz_parse = strcasestr( psz_parse, ">" );
287 while( psz_parse && ( psz_parse = strcasestr( psz_parse, "<" ) ) )
289 if( !strncasecmp( psz_parse, "<!--", 4 ) )
291 /* this is a comment */
292 if( ( psz_parse = strcasestr( psz_parse, "-->" ) ) )
296 else if( !strncasecmp( psz_parse, "<PARAM ", 7 ) )
298 vlc_bool_t b_encoding_flag = VLC_FALSE;
299 psz_parse = SkipBlanks(psz_parse+7, (unsigned)-1);
300 if( !strncasecmp( psz_parse, "name", 4 ) )
302 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
304 psz_backup = ++psz_parse;
305 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
307 i_strlen = psz_parse-psz_backup;
308 if( i_strlen < 1 ) continue;
309 msg_Dbg( p_demux, "param name strlen: %d", i_strlen);
310 psz_string = malloc( i_strlen *sizeof( char ) +1);
311 memcpy( psz_string, psz_backup, i_strlen );
312 psz_string[i_strlen] = '\0';
313 msg_Dbg( p_demux, "param name: %s", psz_string);
314 b_encoding_flag = !strcasecmp( psz_string, "encoding" );
322 if( !strncasecmp( psz_parse, "value", 5 ) )
324 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
326 psz_backup = ++psz_parse;
327 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
329 i_strlen = psz_parse-psz_backup;
330 if( i_strlen < 1 ) continue;
331 msg_Dbg( p_demux, "param value strlen: %d", i_strlen);
332 psz_string = malloc( i_strlen *sizeof( char ) +1);
333 memcpy( psz_string, psz_backup, i_strlen );
334 psz_string[i_strlen] = '\0';
335 msg_Dbg( p_demux, "param value: %s", psz_string);
336 if( b_encoding_flag && !strcasecmp( psz_string, "utf-8" ) ) p_sys->b_utf8 = VLC_TRUE;
343 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
347 else if( !strncasecmp( psz_parse, "<BANNER", 7 ) )
349 /* We skip this element */
350 if( ( psz_parse = strcasestr( psz_parse, "</BANNER>" ) ) )
354 else if( !strncasecmp( psz_parse, "<PREVIEWDURATION", 16 ) ||
355 !strncasecmp( psz_parse, "<LOGURL", 7 ) ||
356 !strncasecmp( psz_parse, "<Skin", 5 ) )
358 /* We skip this element */
359 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
363 else if( !strncasecmp( psz_parse, "<BASE ", 6 ) )
365 psz_parse = SkipBlanks(psz_parse+6, (unsigned)-1);
366 if( !strncasecmp( psz_parse, "HREF", 4 ) )
368 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
370 psz_backup = ++psz_parse;
371 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
373 StoreString( p_demux, (b_entry ? &psz_base_entry : &psz_base_asx), psz_backup, psz_parse );
379 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
383 else if( !strncasecmp( psz_parse, "<TITLE>", 7 ) )
385 psz_backup = psz_parse+=7;
386 if( ( psz_parse = strcasestr( psz_parse, "</TITLE>" ) ) )
388 StoreString( p_demux, (b_entry ? &psz_title_entry : &psz_title_asx), psz_backup, psz_parse );
393 else if( !strncasecmp( psz_parse, "<Author>", 8 ) )
395 psz_backup = psz_parse+=8;
396 if( ( psz_parse = strcasestr( psz_parse, "</Author>" ) ) )
398 StoreString( p_demux, (b_entry ? &psz_artist_entry : &psz_artist_asx), psz_backup, psz_parse );
403 else if( !strncasecmp( psz_parse, "<Copyright", 10 ) )
405 psz_backup = psz_parse+=11;
406 if( ( psz_parse = strcasestr( psz_parse, "</Copyright>" ) ) )
408 StoreString( p_demux, (b_entry ? &psz_copyright_entry : &psz_copyright_asx), psz_backup, psz_parse );
413 else if( !strncasecmp( psz_parse, "<MoreInfo ", 10 ) )
415 psz_parse = SkipBlanks(psz_parse+10, (unsigned)-1);
416 if( !strncasecmp( psz_parse, "HREF", 4 ) )
418 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
420 psz_backup = ++psz_parse;
421 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
423 StoreString( p_demux, (b_entry ? &psz_moreinfo_entry : &psz_moreinfo_asx), psz_backup, psz_parse );
429 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
433 else if( !strncasecmp( psz_parse, "<ABSTRACT>", 10 ) )
435 psz_backup = psz_parse+=10;
436 if( ( psz_parse = strcasestr( psz_parse, "</ABSTRACT>" ) ) )
438 StoreString( p_demux, (b_entry ? &psz_abstract_entry : &psz_abstract_asx), psz_backup, psz_parse );
443 else if( !strncasecmp( psz_parse, "<EntryRef ", 10 ) )
445 psz_parse = SkipBlanks(psz_parse+10, (unsigned)-1);
446 if( !strncasecmp( psz_parse, "HREF", 4 ) )
448 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
450 psz_backup = ++psz_parse;
451 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
453 i_strlen = psz_parse-psz_backup;
454 if( i_strlen < 1 ) continue;
455 psz_string = malloc( i_strlen*sizeof( char ) +1);
456 memcpy( psz_string, psz_backup, i_strlen );
457 psz_string[i_strlen] = '\0';
458 p_input = input_ItemNew( p_playlist, psz_string, psz_title_asx );
459 input_ItemCopyOptions( p_current_input, p_input );
460 input_ItemAddSubItem( p_current_input, p_input );
467 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
471 else if( !strncasecmp( psz_parse, "</Entry>", 8 ) )
473 input_item_t *p_entry = NULL;
474 char *psz_name = NULL;
476 char * ppsz_options[2];
479 /* add a new entry */
483 msg_Err( p_demux, "end of entry without start?" );
489 msg_Err( p_demux, "entry without href?" );
493 if( p_sys->b_skip_ads && b_skip_entry )
495 msg_Dbg( p_demux, "skipped entry %d %s (%s)",
496 i_entry_count, ( psz_title_entry ? psz_title_entry : p_current_input->psz_name ), psz_href );
500 if( i_starttime || i_duration )
503 asprintf(ppsz_options+i_options, ":start-time=%d", i_starttime);
507 asprintf(ppsz_options+i_options, ":stop-time=%d", i_starttime + i_duration);
512 /* create the new entry */
513 asprintf( &psz_name, "%d %s", i_entry_count, ( psz_title_entry ? psz_title_entry : p_current_input->psz_name ) );
515 p_entry = input_ItemNewExt( p_playlist, psz_href, psz_name, i_options, (const char * const *)ppsz_options, -1 );
516 FREENULL( psz_name );
517 input_ItemCopyOptions( p_current_input, p_entry );
520 psz_name = ppsz_options[--i_options];
524 if( psz_title_entry ) input_item_SetTitle( p_entry, psz_title_entry );
525 if( psz_artist_entry ) input_item_SetArtist( p_entry, psz_artist_entry );
526 if( psz_copyright_entry ) input_item_SetCopyright( p_entry, psz_copyright_entry );
527 if( psz_moreinfo_entry ) input_item_SetURL( p_entry, psz_moreinfo_entry );
528 if( psz_abstract_entry ) input_item_SetDescription( p_entry, psz_abstract_entry );
529 input_ItemAddSubItem( p_current_input, p_entry );
530 vlc_gc_decref( p_entry );
534 FREENULL( psz_href );
535 FREENULL( psz_title_entry );
536 FREENULL( psz_base_entry );
537 FREENULL( psz_artist_entry );
538 FREENULL( psz_copyright_entry );
539 FREENULL( psz_moreinfo_entry );
540 FREENULL( psz_abstract_entry );
543 else if( !strncasecmp( psz_parse, "<Entry", 6 ) )
545 char *psz_clientskip;
549 msg_Err( p_demux, "We already are in an entry section" );
554 psz_clientskip = strcasestr( psz_parse, "clientskip=\"no\"" );
555 psz_parse = strcasestr( psz_parse, ">" );
557 /* If clientskip was enabled ... this is an ad */
558 b_skip_entry = (NULL != psz_clientskip) && (psz_clientskip < psz_parse);
560 // init entry details
566 else if( !strncasecmp( psz_parse, "<Ref ", 5 ) )
568 psz_parse = SkipBlanks(psz_parse+5, (unsigned)-1);
571 msg_Err( p_demux, "A ref outside an entry section" );
575 if( !strncasecmp( psz_parse, "HREF", 4 ) )
577 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
579 psz_backup = ++psz_parse;
580 psz_backup = SkipBlanks(psz_backup, (unsigned)-1);
581 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
584 i_strlen = psz_parse-psz_backup;
585 if( i_strlen < 1 ) continue;
588 psz_href = malloc( i_strlen*sizeof( char ) +1);
589 memcpy( psz_href, psz_backup, i_strlen );
590 psz_href[i_strlen] = '\0';
591 psz_tmp = psz_href + (i_strlen-1);
592 while( psz_tmp >= psz_href &&
593 ( *psz_tmp == '\r' || *psz_tmp == '\n' ) )
603 if( ( psz_parse = strcasestr( psz_parse, ">" ) ) )
607 else if( !strncasecmp( psz_parse, "<starttime ", 11 ) )
609 psz_parse = SkipBlanks(psz_parse+11, (unsigned)-1);
612 msg_Err( p_demux, "starttime outside an entry section" );
616 if( !strncasecmp( psz_parse, "value", 5 ) )
618 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
620 psz_backup = ++psz_parse;
621 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
623 i_strlen = psz_parse-psz_backup;
624 if( i_strlen < 1 ) continue;
626 i_starttime = ParseTime(psz_backup, i_strlen);
632 if( ( psz_parse = strcasestr( psz_parse, ">" ) ) )
636 else if( !strncasecmp( psz_parse, "<duration ", 11 ) )
638 psz_parse = SkipBlanks(psz_parse+5, (unsigned)-1);
641 msg_Err( p_demux, "duration outside an entry section" );
645 if( !strncasecmp( psz_parse, "value", 5 ) )
647 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
649 psz_backup = ++psz_parse;
650 if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
652 i_strlen = psz_parse-psz_backup;
653 if( i_strlen < 1 ) continue;
655 i_duration = ParseTime(psz_backup, i_strlen);
661 if( ( psz_parse = strcasestr( psz_parse, ">" ) ) )
665 else if( !strncasecmp( psz_parse, "</ASX", 5 ) )
667 if( psz_title_asx ) input_item_SetTitle( p_current_input, psz_title_asx );
668 if( psz_artist_asx ) input_item_SetArtist( p_current_input, psz_artist_asx );
669 if( psz_copyright_asx ) input_item_SetCopyright( p_current_input, psz_copyright_asx );
670 if( psz_moreinfo_asx ) input_item_SetURL( p_current_input, psz_moreinfo_asx );
671 if( psz_abstract_asx ) input_item_SetDescription( p_current_input, psz_abstract_asx );
672 FREENULL( psz_base_asx );
673 FREENULL( psz_title_asx );
674 FREENULL( psz_artist_asx );
675 FREENULL( psz_copyright_asx );
676 FREENULL( psz_moreinfo_asx );
677 FREENULL( psz_abstract_asx );
683 /* FIXME Unsupported elements */
691 HANDLE_PLAY_AND_RELEASE;
692 return 0; /* Needed for correct operation of go back */
695 static int Control( demux_t *p_demux, int i_query, va_list args )