00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <stdlib.h>
00030
00031 #include <vlc/vlc.h>
00032 #include <vlc/input.h>
00033 #include <vlc_playlist.h>
00034
00035
00036
00037
00038 #define MAX_LINE 8192
00039
00040 #define TYPE_UNKNOWN 0
00041 #define TYPE_M3U 1
00042 #define TYPE_ASX 2
00043 #define TYPE_HTML 3
00044 #define TYPE_PLS 4
00045 #define TYPE_B4S 5
00046 #define TYPE_WMP 6
00047 #define TYPE_RTSP 7
00048
00049 struct demux_sys_t
00050 {
00051 int i_type;
00052 };
00053
00054
00055
00056
00057 static int Activate ( vlc_object_t * );
00058 static void Deactivate( vlc_object_t * );
00059 static int Demux ( demux_t * );
00060 static int Control ( demux_t *, int, va_list );
00061
00062
00063
00064
00065 vlc_module_begin();
00066 set_category( CAT_INPUT );
00067 set_subcategory( SUBCAT_INPUT_DEMUX );
00068 set_description( _("Playlist metademux") );
00069 set_capability( "demux2", 5 );
00070 set_callbacks( Activate, Deactivate );
00071 add_shortcut( "m3u" );
00072 add_shortcut( "asx" );
00073 add_shortcut( "html" );
00074 add_shortcut( "pls" );
00075 add_shortcut( "b4s" );
00076 vlc_module_end();
00077
00078
00079
00080
00081 static int Activate( vlc_object_t * p_this )
00082 {
00083 demux_t *p_demux = (demux_t *)p_this;
00084 char *psz_ext;
00085 int i_type = TYPE_UNKNOWN;
00086 int i_type2 = TYPE_UNKNOWN;
00087
00088 p_demux->pf_control = Control;
00089 p_demux->pf_demux = Demux;
00090
00091
00092 psz_ext = strrchr ( p_demux->psz_path, '.' );
00093
00094 if( ( psz_ext && !strcasecmp( psz_ext, ".m3u") ) ||
00095
00096 ( psz_ext && !strcasecmp( psz_ext, ".ram") ) ||
00097 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "m3u") ) )
00098 {
00099 i_type = TYPE_M3U;
00100 }
00101 else if( ( psz_ext && !strcasecmp( psz_ext, ".asx") ) ||
00102 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "asx") ) )
00103 {
00104 i_type = TYPE_ASX;
00105 }
00106 else if( ( psz_ext && !strcasecmp( psz_ext, ".html") ) ||
00107 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "html") ) )
00108 {
00109 i_type = TYPE_HTML;
00110 }
00111 else if( ( psz_ext && !strcasecmp( psz_ext, ".pls") ) ||
00112 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "pls") ) )
00113 {
00114 i_type = TYPE_PLS;
00115 }
00116 else if( ( psz_ext && !strcasecmp( psz_ext, ".b4s") ) ||
00117 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "b4s") ) )
00118 {
00119 i_type = TYPE_B4S;
00120 }
00121
00122
00123
00124
00125
00126
00127 if( i_type != TYPE_M3U )
00128 {
00129 char *p_peek;
00130 int i_size = stream_Peek( p_demux->s, (uint8_t **)&p_peek, MAX_LINE );
00131 i_size -= sizeof("[Reference]") - 1;
00132
00133 if( i_size > 0 )
00134 {
00135 while( i_size &&
00136 strncasecmp(p_peek, "[playlist]", sizeof("[playlist]") - 1)
00137 && strncasecmp( p_peek, "[Reference]", sizeof("[Reference]") - 1 )
00138 && strncasecmp( p_peek, "<html>", sizeof("<html>") - 1 )
00139 && strncasecmp( p_peek, "<asx", sizeof("<asx") - 1 )
00140 && strncasecmp( p_peek, "rtsptext", sizeof("rtsptext") - 1 )
00141 && strncasecmp( p_peek, "<?xml", sizeof("<?xml") -1 ) )
00142 {
00143 p_peek++;
00144 i_size--;
00145 }
00146 if( !i_size )
00147 {
00148 ;
00149 }
00150 else if( !strncasecmp( p_peek, "[playlist]", sizeof("[playlist]") -1 ) )
00151 {
00152 i_type2 = TYPE_PLS;
00153 }
00154 else if( !strncasecmp( p_peek, "[Reference]", sizeof("[Reference]") -1 ) )
00155 {
00156 i_type2 = TYPE_WMP;
00157 }
00158 else if( !strncasecmp( p_peek, "<html>", sizeof("<html>") -1 ) )
00159 {
00160 i_type2 = TYPE_HTML;
00161 }
00162 else if( !strncasecmp( p_peek, "<asx", sizeof("<asx") -1 ) )
00163 {
00164 i_type2 = TYPE_ASX;
00165 }
00166 else if( !strncasecmp( p_peek, "rtsptext", sizeof("rtsptext") -1 ) )
00167 {
00168 i_type2 = TYPE_RTSP;
00169 }
00170 #if 0
00171 else if( !strncasecmp( p_peek, "<?xml", sizeof("<?xml") -1 ) )
00172 {
00173 i_type2 = TYPE_B4S;
00174 }
00175 #endif
00176 }
00177 }
00178 if( i_type == TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN)
00179 {
00180 return VLC_EGENERIC;
00181 }
00182 if( i_type != TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN )
00183 {
00184 i_type = TYPE_M3U;
00185 }
00186 else
00187 {
00188 i_type = i_type2;
00189 }
00190
00191
00192 p_demux->p_sys = malloc( sizeof( demux_sys_t ) );
00193 p_demux->p_sys->i_type = i_type;
00194 msg_Dbg( p_this, "Playlist type: %d - %d", i_type, i_type2 );
00195
00196 return VLC_SUCCESS;
00197 }
00198
00199
00200
00201
00202 static void Deactivate( vlc_object_t *p_this )
00203 {
00204 demux_t *p_demux = (demux_t *)p_this;
00205 free( p_demux->p_sys );
00206 }
00207
00208
00209
00210
00211 static void XMLSpecialChars ( char *str )
00212 {
00213 char *src = str;
00214 char *dst = str;
00215
00216 while( *src )
00217 {
00218 if( *src == '&' )
00219 {
00220 if( !strncasecmp( src, "à", 6 ) ) *dst++ = 'à';
00221 else if( !strncasecmp( src, "î", 6 ) ) *dst++ = 'î';
00222 else if( !strncasecmp( src, "'", 6 ) ) *dst++ = '\'';
00223 else if( !strncasecmp( src, "è", 6 ) ) *dst++ = 'è';
00224 else if( !strncasecmp( src, "é", 6 ) ) *dst++ = 'é';
00225 else if( !strncasecmp( src, "ê", 6 ) ) *dst++ = 'ê';
00226 else
00227 {
00228 *dst++ = '?';
00229 }
00230 src += 6;
00231 }
00232 else
00233 {
00234 *dst++ = *src++;
00235 }
00236 }
00237
00238 *dst = '\0';
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 static int ParseLine( demux_t *p_demux, char *psz_line, char *psz_data,
00253 vlc_bool_t *pb_done )
00254 {
00255 demux_sys_t *p_m3u = p_demux->p_sys;
00256 char *psz_bol, *psz_name;
00257
00258 psz_bol = psz_line;
00259 *pb_done = VLC_FALSE;
00260
00261
00262 while( *psz_bol == ' ' || *psz_bol == '\t' ||
00263 *psz_bol == '\n' || *psz_bol == '\r' )
00264 {
00265 psz_bol++;
00266 }
00267
00268 if( p_m3u->i_type == TYPE_M3U )
00269 {
00270
00271 if( *psz_bol == '#' )
00272 {
00273 while( *psz_bol &&
00274 strncasecmp( psz_bol, "EXTINF:",
00275 sizeof("EXTINF:") - 1 ) &&
00276 strncasecmp( psz_bol, "EXTVLCOPT:",
00277 sizeof("EXTVLCOPT:") - 1 ) ) psz_bol++;
00278
00279 if( !*psz_bol ) return 0;
00280
00281 if( !strncasecmp( psz_bol, "EXTINF:", sizeof("EXTINF:") - 1 ) )
00282 {
00283 psz_bol = strchr( psz_bol, ',' );
00284 if ( !psz_bol ) return 0;
00285 psz_bol++;
00286
00287
00288 strcpy( psz_data , psz_bol );
00289 return 2;
00290 }
00291 else
00292 {
00293 psz_bol = strchr( psz_bol, ':' );
00294 if ( !psz_bol ) return 0;
00295 psz_bol++;
00296
00297 strcpy( psz_data , psz_bol );
00298 return 3;
00299 }
00300 }
00301
00302 }
00303 else if( p_m3u->i_type == TYPE_PLS )
00304 {
00305
00306
00307 if( !strncasecmp( psz_bol, "File", sizeof("File") - 1 ) )
00308 {
00309 psz_bol += sizeof("File") - 1;
00310 psz_bol = strchr( psz_bol, '=' );
00311 if ( !psz_bol ) return 0;
00312 psz_bol++;
00313 }
00314 else
00315 {
00316 return 0;
00317 }
00318 }
00319 else if( p_m3u->i_type == TYPE_WMP )
00320 {
00321
00322
00323 if( !strncasecmp( psz_bol, "Ref", sizeof("Ref") - 1 ) )
00324 {
00325 psz_bol += sizeof("Ref") - 1;
00326 psz_bol = strchr( psz_bol, '=' );
00327 if ( !psz_bol ) return 0;
00328 psz_bol++;
00329 if( !strncasecmp( psz_bol, "http://", sizeof("http://") -1 ) )
00330 {
00331 psz_bol[0] = 'm'; psz_bol[1] = 'm'; psz_bol[2] = 's'; psz_bol[3] = 'h';
00332 }
00333 }
00334 else
00335 {
00336 return 0;
00337 }
00338 }
00339 else if( p_m3u->i_type == TYPE_ASX )
00340 {
00341
00342
00343
00344 char *psz_eol;
00345
00346 while( *psz_bol &&
00347 strncasecmp( psz_bol, "ref", sizeof("ref") - 1 ) )
00348 psz_bol++;
00349
00350 if( !*psz_bol ) return 0;
00351
00352 while( *psz_bol &&
00353 strncasecmp( psz_bol, "href", sizeof("href") - 1 ) )
00354 psz_bol++;
00355
00356 if( !*psz_bol ) return 0;
00357
00358 while( *psz_bol &&
00359 strncasecmp( psz_bol, "mms://",
00360 sizeof("mms://") - 1 ) &&
00361 strncasecmp( psz_bol, "mmsu://",
00362 sizeof("mmsu://") - 1 ) &&
00363 strncasecmp( psz_bol, "mmst://",
00364 sizeof("mmst://") - 1 ) &&
00365 strncasecmp( psz_bol, "http://",
00366 sizeof("http://") - 1 ) &&
00367 strncasecmp( psz_bol, "file://",
00368 sizeof("file://") - 1 ) )
00369 psz_bol++;
00370
00371 if( !*psz_bol ) return 0;
00372
00373 psz_eol = strchr( psz_bol, '"');
00374 if( !psz_eol )
00375 return 0;
00376
00377 *psz_eol = '\0';
00378 }
00379 else if( p_m3u->i_type == TYPE_HTML )
00380 {
00381
00382
00383
00384 char *psz_eol;
00385
00386 while( *psz_bol &&
00387 strncasecmp( psz_bol, "param", sizeof("param") - 1 ) )
00388 psz_bol++;
00389
00390 if( !*psz_bol ) return 0;
00391
00392 while( *psz_bol &&
00393 strncasecmp( psz_bol, "filename", sizeof("filename") - 1 ) )
00394 psz_bol++;
00395
00396 if( !*psz_bol ) return 0;
00397
00398 while( *psz_bol &&
00399 strncasecmp( psz_bol, "http://",
00400 sizeof("http://") - 1 ) )
00401 psz_bol++;
00402
00403 if( !*psz_bol ) return 0;
00404
00405 psz_eol = strchr( psz_bol, '"');
00406 if( !psz_eol )
00407 return 0;
00408
00409 *psz_eol = '\0';
00410
00411 }
00412 else if( p_m3u->i_type == TYPE_B4S )
00413 {
00414
00415 char *psz_eol;
00416
00417 msg_Dbg( p_demux, "b4s line=%s", psz_line );
00418
00419
00420
00421
00422
00423 if( strstr ( psz_bol, "<Name>" ) )
00424 {
00425
00426 while ( *psz_bol &&
00427 strncasecmp( psz_bol,"Name",sizeof("Name") -1 ) )
00428 psz_bol++;
00429
00430 if( !*psz_bol ) return 0;
00431
00432 psz_bol = psz_bol + 5 ;
00433
00434
00435 if( !psz_bol ) return 0;
00436
00437
00438 psz_eol = strchr(psz_bol, '<' );
00439 if( !psz_eol) return 0;
00440
00441 *psz_eol='\0';
00442
00443 XMLSpecialChars( psz_bol );
00444
00445 strcpy( psz_data, psz_bol );
00446 return 2;
00447 }
00448 else if( strstr( psz_bol, "</entry>" ) || strstr( psz_bol, "</Entry>" ))
00449 {
00450 *pb_done = VLC_TRUE;
00451 return 0;
00452 }
00453
00454
00455
00456 while( *psz_bol &&
00457 strncasecmp( psz_bol,"Playstring",sizeof("Playstring") -1 ) )
00458 psz_bol++;
00459
00460 if( !*psz_bol ) return 0;
00461
00462 psz_bol = strchr( psz_bol, '=' );
00463 if ( !psz_bol ) return 0;
00464
00465 psz_bol += 2;
00466
00467 psz_eol= strchr(psz_bol, '"');
00468 if( !psz_eol ) return 0;
00469
00470 *psz_eol= '\0';
00471
00472
00473 XMLSpecialChars( psz_bol );
00474 }
00475 else if( p_m3u->i_type == TYPE_RTSP )
00476 {
00477
00478
00479 if( strncasecmp( psz_bol, "rtsp://", sizeof("rtsp://") - 1 ) )
00480 return 0;
00481 }
00482 else
00483 {
00484 msg_Warn( p_demux, "unknown file type" );
00485 return 0;
00486 }
00487
00488
00489 if ( !*psz_bol ) return 0;
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501 psz_name = psz_bol;
00502 while( *psz_name && *psz_name!=':' )
00503 {
00504 psz_name++;
00505 }
00506 #ifdef WIN32
00507 if ( *psz_name && ( psz_name == psz_bol + 1 ) )
00508 {
00509
00510
00511
00512 if ( *(psz_name+1) == '/' )
00513 {
00514 if ( *(psz_name+2) != '/' )
00515 while ( *psz_name ) *psz_name++;
00516 }
00517 else while ( *psz_name ) *psz_name++;
00518 }
00519 #endif
00520
00521
00522
00523 #ifndef WIN32
00524 if( !*psz_name && *psz_bol != '/' )
00525
00526 #else
00527 if( !*psz_name
00528 && *psz_bol!='/'
00529 && *psz_bol!='\\'
00530 && *(psz_bol+1)!=':' )
00531
00532
00533 #endif
00534 {
00535
00536 char *psz_path = strdup( p_demux->psz_path );
00537
00538 #ifndef WIN32
00539 psz_name = strrchr( psz_path, '/' );
00540 #else
00541 psz_name = strrchr( psz_path, '\\' );
00542 if ( ! psz_name ) psz_name = strrchr( psz_path, '/' );
00543 #endif
00544 if( psz_name ) *psz_name = '\0';
00545 else *psz_path = '\0';
00546 #ifndef WIN32
00547 psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
00548 sprintf( psz_name, "%s/%s", psz_path, psz_bol );
00549 #else
00550 if ( *psz_path != '\0' )
00551 {
00552 psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
00553 sprintf( psz_name, "%s\\%s", psz_path, psz_bol );
00554 }
00555 else psz_name = strdup( psz_bol );
00556 #endif
00557 free( psz_path );
00558 }
00559 else
00560 {
00561 psz_name = strdup( psz_bol );
00562 }
00563
00564 strcpy(psz_data, psz_name ) ;
00565
00566 free( psz_name );
00567
00568 if( p_m3u->i_type != TYPE_B4S )
00569 {
00570 *pb_done = VLC_TRUE;
00571 }
00572
00573 return 1;
00574 }
00575
00576 static void ProcessLine ( demux_t *p_demux, playlist_t *p_playlist,
00577 playlist_item_t *p_parent,
00578 char *psz_line, char **ppsz_uri, char **ppsz_name,
00579 int *pi_options, char ***pppsz_options,
00580 vlc_bool_t b_flush )
00581 {
00582 char psz_data[MAX_LINE];
00583 vlc_bool_t b_done;
00584
00585 switch( ParseLine( p_demux, psz_line, psz_data, &b_done ) )
00586 {
00587 case 1:
00588 if( *ppsz_uri ) free( *ppsz_uri );
00589 *ppsz_uri = strdup( psz_data );
00590 break;
00591 case 2:
00592 if( *ppsz_name ) free( *ppsz_name );
00593 *ppsz_name = strdup( psz_data );
00594 break;
00595 case 3:
00596 (*pi_options)++;
00597 *pppsz_options = realloc( *pppsz_options,
00598 sizeof(char *) * *pi_options );
00599 (*pppsz_options)[*pi_options - 1] = strdup( psz_data );
00600 break;
00601 case 0:
00602 default:
00603 break;
00604 }
00605
00606 if( (b_done || b_flush) && *ppsz_uri )
00607 {
00608 playlist_item_t *p_item =
00609 playlist_ItemNew( p_playlist, *ppsz_uri, *ppsz_name );
00610 int i;
00611
00612 for( i = 0; i < *pi_options; i++ )
00613 {
00614 playlist_ItemAddOption( p_item, *pppsz_options[i] );
00615 }
00616
00617 playlist_NodeAddItem( p_playlist, p_item,
00618 p_parent->pp_parents[0]->i_view,
00619 p_parent, PLAYLIST_APPEND, PLAYLIST_END );
00620
00621
00622
00623 playlist_CopyParents( p_parent, p_item );
00624
00625 vlc_input_item_CopyOptions( &p_parent->input, &p_item->input );
00626
00627 if( *ppsz_name ) free( *ppsz_name ); *ppsz_name = NULL;
00628 free( *ppsz_uri ); *ppsz_uri = NULL;
00629
00630 for( ; *pi_options; (*pi_options)-- )
00631 {
00632 free( (*pppsz_options)[*pi_options - 1] );
00633 if( *pi_options == 1 ) free( *pppsz_options );
00634 }
00635 *pppsz_options = NULL;
00636 }
00637 }
00638
00639 static vlc_bool_t FindItem( demux_t *p_demux, playlist_t *p_playlist,
00640 playlist_item_t **pp_item )
00641 {
00642 vlc_bool_t b_play;
00643
00644 if( &p_playlist->status.p_item->input ==
00645 ((input_thread_t *)p_demux->p_parent)->input.p_item )
00646 {
00647 msg_Dbg( p_playlist, "starting playlist playback" );
00648 *pp_item = p_playlist->status.p_item;
00649 b_play = VLC_TRUE;
00650 }
00651 else
00652 {
00653 input_item_t *p_current =
00654 ((input_thread_t*)p_demux->p_parent)->input.p_item;
00655 *pp_item = playlist_LockItemGetByInput( p_playlist, p_current );
00656
00657 if( !*pp_item )
00658 msg_Dbg( p_playlist, "unable to find item in playlist");
00659
00660 b_play = VLC_FALSE;
00661 }
00662
00663 return b_play;
00664 }
00665
00666
00667
00668
00669
00670
00671 static int Demux( demux_t *p_demux )
00672 {
00673 demux_sys_t *p_m3u = p_demux->p_sys;
00674
00675 char psz_line[MAX_LINE];
00676 char p_buf[MAX_LINE], eol_tok;
00677 int i_size, i_bufpos, i_linepos = 0;
00678 vlc_bool_t b_discard = VLC_FALSE;
00679
00680 char *psz_name = NULL;
00681 char *psz_uri = NULL;
00682 int i_options = 0;
00683 char **ppsz_options = NULL;
00684
00685 playlist_t *p_playlist;
00686 playlist_item_t *p_parent;
00687 vlc_bool_t b_play;
00688
00689 p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
00690 FIND_ANYWHERE );
00691 if( !p_playlist )
00692 {
00693 msg_Err( p_demux, "can't find playlist" );
00694 return -1;
00695 }
00696
00697 b_play = FindItem( p_demux, p_playlist, &p_parent );
00698 playlist_ItemToNode( p_playlist, p_parent );
00699 p_parent->input.i_type = ITEM_TYPE_PLAYLIST;
00700
00701
00702
00703 if( p_m3u->i_type == TYPE_ASX || p_m3u->i_type == TYPE_HTML )
00704 eol_tok = '>';
00705 else
00706 eol_tok = '\n';
00707
00708 while( ( i_size = stream_Read( p_demux->s, p_buf, MAX_LINE ) ) )
00709 {
00710 i_bufpos = 0;
00711
00712 while( i_size )
00713 {
00714
00715 while( p_buf[i_bufpos] != eol_tok && i_size )
00716 {
00717 if( i_linepos == MAX_LINE || b_discard == VLC_TRUE )
00718 {
00719
00720 i_linepos = 0;
00721 b_discard = VLC_TRUE;
00722 }
00723 else
00724 {
00725 if ( eol_tok != '\n' || p_buf[i_bufpos] != '\r' )
00726 {
00727 psz_line[i_linepos] = p_buf[i_bufpos];
00728 i_linepos++;
00729 }
00730 }
00731
00732 i_size--; i_bufpos++;
00733 }
00734
00735
00736 if( !i_size ) continue;
00737
00738 i_size--; i_bufpos++;
00739 b_discard = VLC_FALSE;
00740
00741
00742 if( !i_linepos ) continue;
00743
00744 psz_line[i_linepos] = '\0';
00745 i_linepos = 0;
00746
00747 ProcessLine( p_demux, p_playlist, p_parent,
00748 psz_line, &psz_uri, &psz_name,
00749 &i_options, &ppsz_options, VLC_FALSE );
00750 }
00751 }
00752
00753 if( i_linepos && b_discard != VLC_TRUE && eol_tok == '\n' )
00754 {
00755 psz_line[i_linepos] = '\0';
00756
00757 ProcessLine( p_demux, p_playlist, p_parent,
00758 psz_line, &psz_uri, &psz_name,
00759 &i_options, &ppsz_options, VLC_TRUE );
00760 }
00761
00762 if( psz_uri ) free( psz_uri );
00763 if( psz_name ) free( psz_name );
00764 for( ; i_options; i_options-- )
00765 {
00766 free( ppsz_options[i_options - 1] );
00767 if( i_options == 1 ) free( ppsz_options );
00768 }
00769
00770
00771 if( b_play )
00772 {
00773 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
00774 p_playlist->status.i_view,
00775 p_playlist->status.p_item, NULL );
00776 }
00777
00778 vlc_object_release( p_playlist );
00779
00780 return 0;
00781 }
00782
00783 static int Control( demux_t *p_demux, int i_query, va_list args )
00784 {
00785 return VLC_EGENERIC;
00786 }