Commit 3e8d108f authored by Gildas Bazin's avatar Gildas Bazin

* modules/access/mms/mmstu.c: fixed a few corner cases (like an infinite loop...

* modules/access/mms/mmstu.c: fixed a few corner cases (like an infinite loop when input is stopped).
parent 506a686f
...@@ -110,6 +110,10 @@ static int mms_ReceivePacket( input_thread_t * ); ...@@ -110,6 +110,10 @@ static int mms_ReceivePacket( input_thread_t * );
* crire ce genre de trucs :p), et crire en anglais, bordel de * crire ce genre de trucs :p), et crire en anglais, bordel de
* merde :p. * merde :p.
*/ */
/*
* Alors la ouai 'est fou les gens qui crivent des commentaires sans les
* signer. Ca mriterait un coup de pied dans le cul a :)
*/
int E_(MMSTUOpen)( input_thread_t *p_input ) int E_(MMSTUOpen)( input_thread_t *p_input )
{ {
...@@ -940,20 +944,18 @@ static int NetFillBuffer( input_thread_t *p_input ) ...@@ -940,20 +944,18 @@ static int NetFillBuffer( input_thread_t *p_input )
{ {
#ifdef UNDER_CE #ifdef UNDER_CE
return -1; return -1;
#else #else
access_sys_t *p_sys = p_input->p_access_data; access_sys_t *p_sys = p_input->p_access_data;
struct timeval timeout; struct timeval timeout;
fd_set fds; fd_set fds_r, fds_e;
int i_ret; int i_ret;
/* FIXME when using udp */ /* FIXME when using udp */
ssize_t i_tcp, i_udp; ssize_t i_tcp, i_udp;
ssize_t i_tcp_read, i_udp_read; ssize_t i_tcp_read, i_udp_read;
int i_handle_max; int i_handle_max;
int i_try; int i_try = 0;
/* Initialize file descriptor set */
FD_ZERO( &fds );
i_tcp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_tcp; i_tcp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_tcp;
...@@ -967,111 +969,99 @@ static int NetFillBuffer( input_thread_t *p_input ) ...@@ -967,111 +969,99 @@ static int NetFillBuffer( input_thread_t *p_input )
} }
i_handle_max = 0; i_handle_max = 0;
if( i_tcp > 0 ) if( i_tcp > 0 )
{
FD_SET( p_sys->socket_tcp.i_handle, &fds );
i_handle_max = __MAX( i_handle_max, p_sys->socket_tcp.i_handle ); i_handle_max = __MAX( i_handle_max, p_sys->socket_tcp.i_handle );
}
if( i_udp > 0 ) if( i_udp > 0 )
{
FD_SET( p_sys->socket_udp.i_handle, &fds );
i_handle_max = __MAX( i_handle_max, p_sys->socket_udp.i_handle ); i_handle_max = __MAX( i_handle_max, p_sys->socket_udp.i_handle );
}
if( i_handle_max == 0 ) if( i_handle_max == 0 )
{ {
msg_Warn( p_input, "nothing to read %d:%d", i_tcp, i_udp ); msg_Warn( p_input, "nothing to read %d:%d", i_tcp, i_udp );
return( 0 ); return 0;
} }
else else
{ {
// msg_Warn( p_input, "ask for tcp:%d udp:%d", i_tcp, i_udp ); /* msg_Warn( p_input, "ask for tcp:%d udp:%d", i_tcp, i_udp ); */
} }
/* We'll wait 0.5 second if nothing happens */
timeout.tv_sec = 0;
timeout.tv_usec = 500000;
i_try = 0;
/* Find if some data is available */ /* Find if some data is available */
while( (i_ret = select( i_handle_max + 1, &fds, do
NULL, NULL, &timeout )) == 0
|| (i_ret < 0 && errno == EINTR) )
{ {
i_try++; i_try++;
FD_ZERO( &fds );
if( i_tcp > 0 ) FD_SET( p_sys->socket_tcp.i_handle, &fds );
if( i_udp > 0 ) FD_SET( p_sys->socket_udp.i_handle, &fds );
timeout.tv_sec = 0;
timeout.tv_usec = 500000;
if( i_try > 2 && ( p_sys->i_buffer_tcp > 0 || p_sys->i_buffer_udp > 0 ) ) /* Initialize file descriptor set */
FD_ZERO( &fds_r );
FD_ZERO( &fds_e );
if( i_tcp > 0 )
{ {
return 0; FD_SET( p_sys->socket_tcp.i_handle, &fds_r );
FD_SET( p_sys->socket_tcp.i_handle, &fds_e );
} }
if( p_input->b_die || p_input->b_error ) if( i_udp > 0 )
{ {
return 0; FD_SET( p_sys->socket_udp.i_handle, &fds_r );
FD_SET( p_sys->socket_udp.i_handle, &fds_e );
} }
msg_Dbg( p_input, "NetFillBuffer: trying again (select)" );
/* We'll wait 0.5 second if nothing happens */
timeout.tv_sec = 0;
timeout.tv_usec = 500000;
if( i_try > 3 && (p_sys->i_buffer_tcp > 0 || p_sys->i_buffer_udp > 0) )
{
return -1;
} }
if( p_input->b_die || p_input->b_error ) return -1;
//msg_Dbg( p_input, "NetFillBuffer: trying again (select)" );
} while( !(i_ret = select(i_handle_max +1, &fds_r, 0, &fds_e, &timeout)) ||
(i_ret < 0 && errno == EINTR) );
if( i_ret < 0 ) if( i_ret < 0 )
{ {
msg_Err( p_input, "network select error (%s)", strerror(errno) ); msg_Err( p_input, "network select error (%s)", strerror(errno) );
return -1; return -1;
} }
if( i_tcp > 0 && FD_ISSET( p_sys->socket_tcp.i_handle, &fds ) ) i_tcp_read = i_udp_read = 0;
if( i_tcp > 0 && FD_ISSET( p_sys->socket_tcp.i_handle, &fds_r ) )
{ {
i_tcp_read = i_tcp_read =
recv( p_sys->socket_tcp.i_handle, recv( p_sys->socket_tcp.i_handle,
p_sys->buffer_tcp + p_sys->i_buffer_tcp, p_sys->buffer_tcp + p_sys->i_buffer_tcp,
i_tcp + MMS_BUFFER_SIZE/2, 0 ); i_tcp + MMS_BUFFER_SIZE/2, 0 );
} }
else
{
i_tcp_read = 0;
}
if( i_udp > 0 && FD_ISSET( p_sys->socket_udp.i_handle, &fds ) ) if( i_udp > 0 && FD_ISSET( p_sys->socket_udp.i_handle, &fds_r ) )
{ {
i_udp_read = recv( p_sys->socket_udp.i_handle, i_udp_read = recv( p_sys->socket_udp.i_handle,
p_sys->buffer_udp + p_sys->i_buffer_udp, p_sys->buffer_udp + p_sys->i_buffer_udp,
i_udp + MMS_BUFFER_SIZE/2, 0 ); i_udp + MMS_BUFFER_SIZE/2, 0 );
} }
else
{
i_udp_read = 0;
}
#if MMS_DEBUG #if MMS_DEBUG
if( p_sys->i_proto == MMS_PROTO_UDP ) if( p_sys->i_proto == MMS_PROTO_UDP )
{ {
msg_Dbg( p_input, msg_Dbg( p_input, "filling buffer TCP:%d+%d UDP:%d+%d",
"filling buffer TCP:%d+%d UDP:%d+%d", p_sys->i_buffer_tcp, i_tcp_read,
p_sys->i_buffer_tcp, p_sys->i_buffer_udp, i_udp_read );
i_tcp_read,
p_sys->i_buffer_udp,
i_udp_read );
} }
else else
{ {
msg_Dbg( p_input, msg_Dbg( p_input, "filling buffer TCP:%d+%d",
"filling buffer TCP:%d+%d", p_sys->i_buffer_tcp, i_tcp_read );
p_sys->i_buffer_tcp,
i_tcp_read );
} }
#endif #endif
if( i_tcp_read > 0 )
{
p_sys->i_buffer_tcp += i_tcp_read;
}
if( i_udp_read > 0 )
{
p_sys->i_buffer_udp += i_udp_read;
}
return( i_tcp_read + i_udp_read); if( i_tcp_read > 0 ) p_sys->i_buffer_tcp += i_tcp_read;
if( i_udp_read > 0 ) p_sys->i_buffer_udp += i_udp_read;
return i_tcp_read + i_udp_read;
#endif #endif
} }
...@@ -1271,7 +1261,8 @@ static int mms_ReceivePacket( input_thread_t *p_input ) ...@@ -1271,7 +1261,8 @@ static int mms_ReceivePacket( input_thread_t *p_input )
{ {
if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface ) if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
{ {
if( GetDWLE( p_sys->buffer_tcp + 8 ) + 16 <= p_sys->i_buffer_tcp ) if( GetDWLE( p_sys->buffer_tcp + 8 ) + 16 <=
p_sys->i_buffer_tcp )
{ {
b_refill = VLC_FALSE; b_refill = VLC_FALSE;
} }
...@@ -1290,7 +1281,7 @@ static int mms_ReceivePacket( input_thread_t *p_input ) ...@@ -1290,7 +1281,7 @@ static int mms_ReceivePacket( input_thread_t *p_input )
if( b_refill && NetFillBuffer( p_input ) < 0 ) if( b_refill && NetFillBuffer( p_input ) < 0 )
{ {
msg_Warn( p_input, "cannot fill buffer" ); msg_Warn( p_input, "cannot fill buffer" );
continue; return -1;
} }
i_packet_tcp_type = -1; i_packet_tcp_type = -1;
...@@ -1303,24 +1294,19 @@ static int mms_ReceivePacket( input_thread_t *p_input ) ...@@ -1303,24 +1294,19 @@ static int mms_ReceivePacket( input_thread_t *p_input )
if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface ) if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
{ {
i_packet_tcp_type = i_packet_tcp_type =
mms_ParseCommand( p_input, mms_ParseCommand( p_input, p_sys->buffer_tcp,
p_sys->buffer_tcp, p_sys->i_buffer_tcp, &i_used );
p_sys->i_buffer_tcp,
&i_used );
} }
else else
{ {
i_packet_tcp_type = i_packet_tcp_type =
mms_ParsePacket( p_input, mms_ParsePacket( p_input, p_sys->buffer_tcp,
p_sys->buffer_tcp, p_sys->i_buffer_tcp, &i_used );
p_sys->i_buffer_tcp,
&i_used );
} }
if( i_used > 0 && i_used < MMS_BUFFER_SIZE ) if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
{ {
memmove( p_sys->buffer_tcp, memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
p_sys->buffer_tcp + i_used,
MMS_BUFFER_SIZE - i_used ); MMS_BUFFER_SIZE - i_used );
} }
p_sys->i_buffer_tcp -= i_used; p_sys->i_buffer_tcp -= i_used;
...@@ -1330,22 +1316,18 @@ static int mms_ReceivePacket( input_thread_t *p_input ) ...@@ -1330,22 +1316,18 @@ static int mms_ReceivePacket( input_thread_t *p_input )
int i_used; int i_used;
i_packet_udp_type = i_packet_udp_type =
mms_ParsePacket( p_input, mms_ParsePacket( p_input, p_sys->buffer_udp,
p_sys->buffer_udp, p_sys->i_buffer_udp, &i_used );
p_sys->i_buffer_udp,
&i_used );
if( i_used > 0 && i_used < MMS_BUFFER_SIZE ) if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
{ {
memmove( p_sys->buffer_udp, memmove( p_sys->buffer_udp, p_sys->buffer_udp + i_used,
p_sys->buffer_udp + i_used,
MMS_BUFFER_SIZE - i_used ); MMS_BUFFER_SIZE - i_used );
} }
p_sys->i_buffer_udp -= i_used; p_sys->i_buffer_udp -= i_used;
} }
if( i_packet_tcp_type == MMS_PACKET_CMD && if( i_packet_tcp_type == MMS_PACKET_CMD && p_sys->i_command == 0x1b )
p_sys->i_command == 0x1b )
{ {
mms_CommandSend( p_input, 0x1b, 0, 0, NULL, 0 ); mms_CommandSend( p_input, 0x1b, 0, 0, NULL, 0 );
i_packet_tcp_type = -1; i_packet_tcp_type = -1;
...@@ -1353,11 +1335,11 @@ static int mms_ReceivePacket( input_thread_t *p_input ) ...@@ -1353,11 +1335,11 @@ static int mms_ReceivePacket( input_thread_t *p_input )
if( i_packet_tcp_type != -1 ) if( i_packet_tcp_type != -1 )
{ {
return( i_packet_tcp_type ); return i_packet_tcp_type;
} }
else if( i_packet_udp_type != -1 ) else if( i_packet_udp_type != -1 )
{ {
return( i_packet_udp_type ); return i_packet_udp_type;
} }
} }
} }
...@@ -1374,18 +1356,15 @@ static int mms_ReceiveCommand( input_thread_t *p_input ) ...@@ -1374,18 +1356,15 @@ static int mms_ReceiveCommand( input_thread_t *p_input )
if( NetFillBuffer( p_input ) < 0 ) if( NetFillBuffer( p_input ) < 0 )
{ {
msg_Warn( p_input, "cannot fill buffer" ); msg_Warn( p_input, "cannot fill buffer" );
continue; return VLC_EGENERIC;
} }
if( p_sys->i_buffer_tcp > 0 ) if( p_sys->i_buffer_tcp > 0 )
{ {
i_status = mms_ParseCommand( p_input, i_status = mms_ParseCommand( p_input, p_sys->buffer_tcp,
p_sys->buffer_tcp, p_sys->i_buffer_tcp, &i_used );
p_sys->i_buffer_tcp,
&i_used );
if( i_used < MMS_BUFFER_SIZE ) if( i_used < MMS_BUFFER_SIZE )
{ {
memmove( p_sys->buffer_tcp, memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
p_sys->buffer_tcp + i_used,
MMS_BUFFER_SIZE - i_used ); MMS_BUFFER_SIZE - i_used );
} }
p_sys->i_buffer_tcp -= i_used; p_sys->i_buffer_tcp -= i_used;
...@@ -1416,7 +1395,8 @@ static int mms_ReceiveCommand( input_thread_t *p_input ) ...@@ -1416,7 +1395,8 @@ static int mms_ReceiveCommand( input_thread_t *p_input )
#define MMS_RETRY_MAX 10 #define MMS_RETRY_MAX 10
#define MMS_RETRY_SLEEP 50000 #define MMS_RETRY_SLEEP 50000
static int mms_CommandRead( input_thread_t *p_input, int i_command1, int i_command2 ) static int mms_CommandRead( input_thread_t *p_input, int i_command1,
int i_command2 )
{ {
access_sys_t *p_sys = p_input->p_access_data; access_sys_t *p_sys = p_input->p_access_data;
int i_count; int i_count;
...@@ -1434,7 +1414,8 @@ static int mms_CommandRead( input_thread_t *p_input, int i_command1, int i_comma ...@@ -1434,7 +1414,8 @@ static int mms_CommandRead( input_thread_t *p_input, int i_command1, int i_comma
{ {
return VLC_SUCCESS; return VLC_SUCCESS;
} }
else if( p_sys->i_command == i_command1 || p_sys->i_command == i_command2 ) else if( p_sys->i_command == i_command1 ||
p_sys->i_command == i_command2 )
{ {
return VLC_SUCCESS; return VLC_SUCCESS;
} }
...@@ -1455,7 +1436,7 @@ static int mms_CommandRead( input_thread_t *p_input, int i_command1, int i_comma ...@@ -1455,7 +1436,7 @@ static int mms_CommandRead( input_thread_t *p_input, int i_command1, int i_comma
} }
} }
} }
msg_Warn( p_input, "failed to receive command (abording)" ); msg_Warn( p_input, "failed to receive command (aborting)" );
return VLC_EGENERIC; return VLC_EGENERIC;
} }
...@@ -1474,8 +1455,8 @@ static int mms_HeaderMediaRead( input_thread_t *p_input, int i_type ) ...@@ -1474,8 +1455,8 @@ static int mms_HeaderMediaRead( input_thread_t *p_input, int i_type )
if( i_status < 0 ) if( i_status < 0 )
{ {
i_count++; i_count++;
msg_Warn( p_input, msg_Warn( p_input, "cannot receive header (%d/%d)",
"cannot receive header (%d/%d)", i_count, MMS_RETRY_MAX ); i_count, MMS_RETRY_MAX );
msleep( MMS_RETRY_SLEEP ); msleep( MMS_RETRY_SLEEP );
} }
else if( i_status == i_type || i_type == MMS_PACKET_ANY ) else if( i_status == i_type || i_type == MMS_PACKET_ANY )
...@@ -1507,9 +1488,10 @@ static int mms_HeaderMediaRead( input_thread_t *p_input, int i_type ) ...@@ -1507,9 +1488,10 @@ static int mms_HeaderMediaRead( input_thread_t *p_input, int i_type )
} }
} }
} }
msg_Err( p_input,
"cannot receive %s (abording)", msg_Err( p_input, "cannot receive %s (aborting)",
( i_type == MMS_PACKET_HEADER ) ? "header" : "media data" ); ( i_type == MMS_PACKET_HEADER ) ? "header" : "media data" );
return -1; return -1;
} }
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