Commit a01f44d5 authored by Gildas Bazin's avatar Gildas Bazin

* modules/demux/playlist, src/input/stream.c: fixed a shitload of bugs in...

* modules/demux/playlist, src/input/stream.c: fixed a shitload of bugs in playlist demuxers + simplified a lot.
parent b1ddf918
...@@ -37,8 +37,6 @@ ...@@ -37,8 +37,6 @@
struct demux_sys_t struct demux_sys_t
{ {
char *psz_prefix; char *psz_prefix;
char **ppsz_options;
int i_options;
}; };
/***************************************************************************** /*****************************************************************************
...@@ -59,7 +57,6 @@ int Import_M3U( vlc_object_t *p_this ) ...@@ -59,7 +57,6 @@ int Import_M3U( vlc_object_t *p_this )
if( stream_Peek( p_demux->s , &p_peek, 7 ) < 7 ) if( stream_Peek( p_demux->s , &p_peek, 7 ) < 7 )
{ {
msg_Err( p_demux, "cannot peek" );
return VLC_EGENERIC; return VLC_EGENERIC;
} }
psz_ext = strrchr ( p_demux->psz_path, '.' ); psz_ext = strrchr ( p_demux->psz_path, '.' );
...@@ -69,14 +66,14 @@ int Import_M3U( vlc_object_t *p_this ) ...@@ -69,14 +66,14 @@ int Import_M3U( vlc_object_t *p_this )
; ;
} }
else if( ( psz_ext && !strcasecmp( psz_ext, ".m3u") ) || else if( ( psz_ext && !strcasecmp( psz_ext, ".m3u") ) ||
( psz_ext && !strcasecmp( psz_ext, ".ram") ) || /* a .ram file can contain a single rtsp link */ ( psz_ext && !strcasecmp( psz_ext, ".ram") ) ||
/* A .ram file can contain a single rtsp link */
( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "m3u") ) ) ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "m3u") ) )
{ {
; ;
} }
else else
{ {
msg_Warn(p_demux, "m3u import module discarded");
return VLC_EGENERIC; return VLC_EGENERIC;
} }
msg_Dbg( p_demux, "found valid M3U playlist file"); msg_Dbg( p_demux, "found valid M3U playlist file");
...@@ -91,9 +88,6 @@ int Import_M3U( vlc_object_t *p_this ) ...@@ -91,9 +88,6 @@ int Import_M3U( vlc_object_t *p_this )
} }
p_demux->p_sys->psz_prefix = FindPrefix( p_demux ); p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
p_demux->p_sys->ppsz_options = NULL;
p_demux->p_sys->i_options = 0 ;
return VLC_SUCCESS; return VLC_SUCCESS;
} }
...@@ -103,26 +97,23 @@ int Import_M3U( vlc_object_t *p_this ) ...@@ -103,26 +97,23 @@ int Import_M3U( vlc_object_t *p_this )
void Close_M3U( vlc_object_t *p_this ) void Close_M3U( vlc_object_t *p_this )
{ {
demux_t *p_demux = (demux_t *)p_this; demux_t *p_demux = (demux_t *)p_this;
if( p_demux->p_sys->psz_prefix )
{ if( p_demux->p_sys->psz_prefix ) free( p_demux->p_sys->psz_prefix );
free( p_demux->p_sys->psz_prefix );
}
free( p_demux->p_sys ); free( p_demux->p_sys );
} }
static int Demux( demux_t *p_demux ) static int Demux( demux_t *p_demux )
{ {
mtime_t i_duration = -1; playlist_t *p_playlist;
char *psz_name = NULL; char *psz_line;
char *psz_line; int i_position;
char *psz_parse;
char *psz_duration; char *psz_name = NULL;
char *psz_mrl; mtime_t i_duration = -1;
playlist_t *p_playlist; char **ppsz_options = NULL;
int i_position; int i_options = 0;
char *psz_option = NULL;
int i; vlc_bool_t b_cleanup = VLC_FALSE;
p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST, p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
FIND_PARENT ); FIND_PARENT );
...@@ -132,89 +123,101 @@ static int Demux( demux_t *p_demux ) ...@@ -132,89 +123,101 @@ static int Demux( demux_t *p_demux )
return -1; return -1;
} }
vlc_mutex_lock( &p_playlist->object_lock );
p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE; p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
i_position = p_playlist->i_index + 1; i_position = p_playlist->i_index + 1;
while( ( psz_line = stream_ReadLine( p_demux->s ) ) ) vlc_mutex_unlock( &p_playlist->object_lock );
psz_line = stream_ReadLine( p_demux->s );
while( psz_line )
{ {
char *psz_parse = psz_line;
/* Skip leading tabs and spaces */ /* Skip leading tabs and spaces */
while( *psz_line == ' ' || *psz_line == '\t' || while( *psz_parse == ' ' || *psz_parse == '\t' ||
*psz_line == '\n' || *psz_line == '\r' ) *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
{
psz_line++;
}
if( *psz_line == '#' ) if( *psz_parse == '#' )
{ {
/* parse extra info */ /* Parse extra info */
psz_parse = psz_line;
while( *psz_parse && /* Skip leading tabs and spaces */
strncasecmp( psz_parse, "EXTINF:", sizeof("EXTINF:") - 1 ) && while( *psz_parse == ' ' || *psz_parse == '\t' ||
strncasecmp( psz_parse, "EXTVLCOPT:",sizeof("EXTVLCOPT:") -1)) *psz_parse == '\n' || *psz_parse == '\r' ||
psz_parse++; *psz_parse == '#' ) psz_parse++;
if( *psz_parse )
if( !*psz_parse ) goto error;
if( !strncasecmp( psz_parse, "EXTINF:", sizeof("EXTINF:") -1 ) )
{ {
if( !strncasecmp( psz_parse, "EXTINF:", sizeof("EXTINF:") - 1 ) ) /* Extended info */
char *psz_duration;
psz_parse += sizeof("EXTINF:") - 1;
while( *psz_parse == '\t' || *psz_parse == ' ' ) psz_parse++;
psz_duration = psz_parse;
psz_parse = strchr( psz_parse, ',' );
if( psz_parse )
{ {
psz_parse += sizeof("EXTINF:") - 1; *psz_parse = '\0';
while( *psz_parse == '\t' || *psz_parse == ' ' )
psz_parse++;
psz_duration = psz_parse;
psz_parse = strchr( psz_parse, ',' );
if ( psz_parse )
{
i_duration *= 1000000;
*psz_parse = '\0';
psz_parse++;
psz_name = strdup( psz_parse );
i_duration = atoi( psz_duration );
if( i_duration != -1 )
{
i_duration *= 1000000;
}
}
}
else
{
/* Option line */
psz_parse = strchr( psz_parse, ':' );
if( !psz_parse ) return 0;
psz_parse++; psz_parse++;
psz_name = strdup( psz_parse );
i_duration = atoi( psz_duration );
if( i_duration != -1 ) i_duration *= 1000000;
}
}
else if( !strncasecmp( psz_parse, "EXTVLCOPT:",
sizeof("EXTVLCOPT:") -1 ) )
psz_option = strdup( psz_parse ); {
INSERT_ELEM( p_demux->p_sys->ppsz_options, /* VLC Option */
p_demux->p_sys->i_options, char *psz_option;
p_demux->p_sys->i_options, psz_parse += sizeof("EXTVLCOPT:") -1;
if( !*psz_parse ) goto error;
psz_option = strdup( psz_parse );
if( psz_option )
INSERT_ELEM( ppsz_options, i_options, i_options,
psz_option ); psz_option );
}
} }
} }
else else if( *psz_parse )
{ {
psz_mrl = ProcessMRL( psz_line, p_demux->p_sys->psz_prefix ); char *psz_mrl =
ProcessMRL( psz_parse, p_demux->p_sys->psz_prefix );
b_cleanup = VLC_TRUE;
if( !psz_mrl ) goto error;
playlist_AddExt( p_playlist, psz_mrl, psz_name, playlist_AddExt( p_playlist, psz_mrl, psz_name,
PLAYLIST_INSERT, i_position, i_duration, PLAYLIST_INSERT, i_position, i_duration,
(const char **)p_demux->p_sys->ppsz_options, (const char **)ppsz_options, i_options );
p_demux->p_sys->i_options );
for( i = 0 ; i < p_demux->p_sys->i_options ; i++)
{
char *psz_option = p_demux->p_sys->ppsz_options[i];
REMOVE_ELEM( p_demux->p_sys->ppsz_options,
p_demux->p_sys->i_options,
i );
if( psz_option ) free( psz_option );
}
free( psz_mrl );
i_position++; i_position++;
free( psz_mrl );
}
error:
/* Fetch another line */
free( psz_line );
psz_line = stream_ReadLine( p_demux->s );
if( !psz_line ) b_cleanup = VLC_TRUE;
if( b_cleanup )
{
/* Cleanup state */
while( i_options-- ) free( ppsz_options[i_options] );
if( ppsz_options ) free( ppsz_options );
ppsz_options = NULL; i_options = 0;
if( psz_name ) free( psz_name );
psz_name = NULL;
i_duration = -1; i_duration = -1;
if( psz_name )
{ b_cleanup = VLC_FALSE;
free( psz_name );
psz_name = NULL;
}
} }
free( psz_line);
} }
vlc_object_release( p_playlist ); vlc_object_release( p_playlist );
return VLC_SUCCESS; return VLC_SUCCESS;
} }
......
...@@ -41,15 +41,16 @@ vlc_module_begin(); ...@@ -41,15 +41,16 @@ vlc_module_begin();
set_callbacks( Import_Old , NULL ); set_callbacks( Import_Old , NULL );
add_submodule(); add_submodule();
set_description( _("M3U playlist import") ); set_description( _("M3U playlist import") );
add_shortcut( "m3u-open" ); add_shortcut( "m3u-open" );
set_capability( "demux2" , 10 ); set_capability( "demux2" , 10 );
set_callbacks( Import_M3U , Close_M3U ); set_callbacks( Import_M3U , Close_M3U );
add_submodule(); add_submodule();
set_description( _("PLS playlist import") ); set_description( _("PLS playlist import") );
add_shortcut( "pls-open" ); add_shortcut( "pls-open" );
set_capability( "demux2" , 10 ); set_capability( "demux2" , 10 );
set_callbacks( Import_PLS , Close_PLS ); set_callbacks( Import_PLS , Close_PLS );
vlc_module_end(); vlc_module_end();
...@@ -66,10 +67,11 @@ char *FindPrefix( demux_t *p_demux ) ...@@ -66,10 +67,11 @@ char *FindPrefix( demux_t *p_demux )
psz_name = strrchr( psz_path, '/' ); psz_name = strrchr( psz_path, '/' );
#else #else
psz_name = strrchr( psz_path, '\\' ); psz_name = strrchr( psz_path, '\\' );
if ( ! psz_name ) psz_name = strrchr( psz_path, '/' ); if( !psz_name ) psz_name = strrchr( psz_path, '/' );
#endif #endif
if( psz_name ) *psz_name = '\0'; if( psz_name ) psz_name[1] = '\0';
else *psz_path = '\0'; else *psz_path = '\0';
return psz_path; return psz_path;
} }
...@@ -79,63 +81,24 @@ char *FindPrefix( demux_t *p_demux ) ...@@ -79,63 +81,24 @@ char *FindPrefix( demux_t *p_demux )
*/ */
char *ProcessMRL( char *psz_mrl, char *psz_prefix ) char *ProcessMRL( char *psz_mrl, char *psz_prefix )
{ {
char *psz_name; /* Check for a protocol name.
/* check for a protocol name */ * for URL, we should look for "://"
/* for URL, we should look for "://"
* for MRL (Media Resource Locator) ([[<access>][/<demux>]:][<source>]), * for MRL (Media Resource Locator) ([[<access>][/<demux>]:][<source>]),
* we should look for ":" * we should look for ":", so we end up looking simply for ":"
* so we end up looking simply for ":"*/ * PB: on some file systems, ':' are valid characters though */
/* PB: on some file systems, ':' are valid characters though*/
psz_name = psz_mrl;
while( *psz_name && *psz_name!=':' )
{
psz_name++;
}
#ifdef WIN32
if ( *psz_name && ( psz_name == psz_mrl + 1 ) )
{
/* if it is not an URL,
* as it is unlikely to be an MRL (PB: if it is ?)
* it should be an absolute file name with the drive letter */
if ( *(psz_name+1) == '/' )/* "*:/" */
{
if ( *(psz_name+2) != '/' )/* not "*://" */
while ( *psz_name ) *psz_name++;/* so now (*psz_name==0) */
}
else while ( *psz_name ) *psz_name++;/* "*:*"*/
}
#endif
/* if the line doesn't specify a protocol name, /* Simple cases first */
* check if the line has an absolute or relative path */ if( !psz_mrl || !*psz_mrl ) return NULL;
#ifndef WIN32 if( !psz_prefix || !*psz_prefix ) return strdup( psz_mrl );
if( !*psz_name && *psz_mrl != '/' )
/* If this line doesn't begin with a '/' */ /* Check if the line specifies an absolute path */
#else if( *psz_mrl == '/' || *psz_mrl == '\\' ) return strdup( psz_mrl );
if( !*psz_name
&& *psz_mrl!='/'
&& *psz_mrl!='\\'
&& *(psz_mrl+1)!=':' )
/* if this line doesn't begin with
* "/" or "\" or "*:" or "*:\" or "*:/" or "\\" */
#endif
{
#ifndef WIN32
psz_name = malloc( strlen(psz_prefix) + strlen(psz_mrl) + 2 );
sprintf( psz_name, "%s/%s", psz_prefix, psz_mrl );
#else
if ( *psz_prefix != '\0' )
{
psz_name = malloc( strlen(psz_prefix) + strlen(psz_mrl) + 2 );
sprintf( psz_name, "%s\\%s", psz_prefix, psz_mrl );
}
else psz_name = strdup( psz_mrl );
#endif
}
else
{
psz_name = strdup( psz_mrl );
}
return psz_name;
}
/* Check if the line specifies an mrl/url
* (and on win32, contains a drive letter) */
if( strchr( psz_mrl, ':' ) ) return strdup( psz_mrl );
/* This a relative path, prepend the prefix */
asprintf( &psz_mrl, "%s%s", psz_prefix, psz_mrl );
return psz_mrl;
}
...@@ -1152,76 +1152,65 @@ static void AStreamPrebufferStream( stream_t *s ) ...@@ -1152,76 +1152,65 @@ static void AStreamPrebufferStream( stream_t *s )
* \param s Stream handle to read from * \param s Stream handle to read from
* \return A null-terminated string. This must be freed, * \return A null-terminated string. This must be freed,
*/ */
#define STREAM_PROBE_LINE 1024 #define STREAM_PROBE_LINE 2048
#define STREAM_LINE_MAX (2048*100)
char *stream_ReadLine( stream_t *s ) char *stream_ReadLine( stream_t *s )
{ {
char *p_line = NULL; char *p_line = NULL;
int i_line = 0; int i_line = 0, i_read = 0;
for( ;; ) while( i_read < STREAM_LINE_MAX )
{ {
char *psz_lf, *psz_cr; char *psz_eol;
uint8_t *p_data; uint8_t *p_data;
int i_data; int i_data;
int i_read;
/* Probe new data */ /* Probe new data */
i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE ); i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
if( i_data <= 0 ) break; /* No more data */
if( i_data <= 0 ) /* No data */ /* Check if there is an EOL */
goto exit; if( ( psz_eol = memchr( p_data, '\n', i_data ) ) )
/* See if there is a '\n' */
psz_lf = memchr( p_data, '\n', __MAX( i_data - 1, 1 ) );
psz_cr = memchr( p_data, '\r', __MAX( i_data - 1, 1 ) );
if( psz_lf && !psz_cr && p_data[i_data-1] == '\r' )
{ {
psz_cr = &p_data[i_data-1]; i_data = (psz_eol - (char *)p_data) + 1;
} p_line = realloc( p_line, i_line + i_data + 1 );
if( psz_cr && !psz_lf && p_data[i_data-1] == '\n' ) i_data = stream_Read( s, &p_line[i_line], i_data );
{ if( i_data <= 0 ) break; /* Hmmm */
psz_lf = &p_data[i_data-1]; i_line += (i_data - 1);
} i_read += i_data;
if( psz_lf || psz_cr ) /* Remove trailing LF/CR */
{ while( stream_Peek( s, &p_data, 1 ) == 1 &&
char *psz; i_read < STREAM_LINE_MAX )
if( !psz_lf ) {
psz = psz_cr; if( p_data[0] != '\n' && p_data[0] != '\r' ) break;
else if( !psz_cr )
psz = psz_lf;
else
psz = __MIN( psz_cr, psz_lf );
i_read = (psz - (char*)p_data) + 1; i_data = stream_Read( s, NULL, 1 );
p_line = realloc( p_line, i_read + 1 ); /* +1 for easy \0 append */ if( i_data <= 0 ) break; /* Hmmm */
i_read = stream_Read( s, &p_line[i_line], i_read ); i_read += i_data;
if( i_read > 0 ) }
i_line += i_read;
goto exit; /* We have our line */
break;
} }
/* Read data (+1 for easy \0 append) */ /* Read data (+1 for easy \0 append) */
p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + 1 ); p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + 1 );
i_read = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE ); i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
if( i_data <= 0 ) break; /* Hmmm */
if( i_read <= 0 ) i_line += i_data;
goto exit; i_read += i_data;
i_line += i_read;
} }
exit: /* Remove trailing CR */
while( i_line > 0 && while( i_line > 0 && p_line[i_line-1] == '\r' ) i_line--;
( p_line[i_line-1] == '\n' || p_line[i_line-1] == '\r' ) )
{
i_line--;
}
if( i_line > 0 ) if( i_line > 0 )
{
p_line[i_line] = '\0'; p_line[i_line] = '\0';
return p_line;
}
return p_line; if( p_line ) free( p_line );
return NULL;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment