Commit fd94f31b authored by Sam Hocevar's avatar Sam Hocevar

* ./modules/audio_output/oss.c, ./modules/audio_output/sdl.c: improved

    sound synchronization.
  * ./src/audio_output/output.c: reverted another change I previously did :-)
parent 51e99753
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* oss.c : OSS /dev/dsp module for vlc * oss.c : OSS /dev/dsp module for vlc
***************************************************************************** *****************************************************************************
* Copyright (C) 2000-2002 VideoLAN * Copyright (C) 2000-2002 VideoLAN
* $Id: oss.c,v 1.18 2002/08/25 09:40:00 sam Exp $ * $Id: oss.c,v 1.19 2002/08/25 16:55:55 sam Exp $
* *
* Authors: Michel Kaempf <maxx@via.ecp.fr> * Authors: Michel Kaempf <maxx@via.ecp.fr>
* Samuel Hocevar <sam@zoy.org> * Samuel Hocevar <sam@zoy.org>
...@@ -277,24 +277,31 @@ static void Close( vlc_object_t * p_this ) ...@@ -277,24 +277,31 @@ static void Close( vlc_object_t * p_this )
/***************************************************************************** /*****************************************************************************
* GetBufInfo: buffer status query * BufferDuration: buffer status query
***************************************************************************** *****************************************************************************
* This function fills in the audio_buf_info structure : * This function returns the duration in microseconfs of the current buffer.
* - returns : number of available fragments (not partially used ones)
* - int fragstotal : total number of fragments allocated
* - int fragsize : size of a fragment in bytes
* - int bytes : available space in bytes (includes partially used fragments)
* Note! 'bytes' could be more than fragments*fragsize
*****************************************************************************/ *****************************************************************************/
static int GetBufInfo( aout_instance_t * p_aout ) static mtime_t BufferDuration( aout_instance_t * p_aout )
{ {
struct aout_sys_t * p_sys = p_aout->output.p_sys; struct aout_sys_t * p_sys = p_aout->output.p_sys;
audio_buf_info audio_buf; audio_buf_info audio_buf;
int i_bytes;
/* Fill the audio_buf_info structure:
* - fragstotal: total number of fragments allocated
* - fragsize: size of a fragment in bytes
* - bytes: available space in bytes (includes partially used fragments)
* Note! 'bytes' could be more than fragments*fragsize */
ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ); ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
/* returns the allocated space in bytes */ /* calculate number of available fragments (not partially used ones) */
return ( (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes ); i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
/* Return the fragment duration */
return (mtime_t)i_bytes * 1000000
/ p_aout->output.output.i_bytes_per_frame
/ p_aout->output.output.i_rate
* p_aout->output.output.i_frame_length;
} }
/***************************************************************************** /*****************************************************************************
...@@ -303,6 +310,7 @@ static int GetBufInfo( aout_instance_t * p_aout ) ...@@ -303,6 +310,7 @@ static int GetBufInfo( aout_instance_t * p_aout )
static int OSSThread( aout_instance_t * p_aout ) static int OSSThread( aout_instance_t * p_aout )
{ {
struct aout_sys_t * p_sys = p_aout->output.p_sys; struct aout_sys_t * p_sys = p_aout->output.p_sys;
mtime_t next_date = 0;
while ( !p_aout->b_die ) while ( !p_aout->b_die )
{ {
...@@ -318,25 +326,30 @@ static int OSSThread( aout_instance_t * p_aout ) ...@@ -318,25 +326,30 @@ static int OSSThread( aout_instance_t * p_aout )
if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF ) if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
{ {
mtime_t buffered; mtime_t buffered = BufferDuration( p_aout );
do /* Wait a bit - we don't want our buffer to be full */
while( buffered > AOUT_PTS_TOLERANCE * 2 )
{ {
buffered = (mtime_t)GetBufInfo( p_aout ) * 1000000
/ p_aout->output.output.i_bytes_per_frame
/ p_aout->output.output.i_rate
* p_aout->output.output.i_frame_length;
if( buffered < 50000 )
{
break;
}
msleep( buffered / 2 - 10000 ); msleep( buffered / 2 - 10000 );
buffered = BufferDuration( p_aout );
}
} while( VLC_TRUE ); if( !next_date )
{
/* This is the _real_ presentation date */
next_date = mdate() + buffered;
}
else
{
/* Give a hint to the audio output about our drift, but
* not too much because we want to make it happy with our
* nicely calculated dates. */
next_date = ( (next_date * 7) + (mdate() + buffered) ) / 8;
}
/* Next buffer will be played at mdate()+buffered */ /* Next buffer will be played at mdate()+buffered */
p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered, p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
VLC_FALSE );
} }
else else
{ {
...@@ -347,6 +360,9 @@ static int OSSThread( aout_instance_t * p_aout ) ...@@ -347,6 +360,9 @@ static int OSSThread( aout_instance_t * p_aout )
{ {
p_bytes = p_buffer->p_buffer; p_bytes = p_buffer->p_buffer;
i_size = p_buffer->i_nb_bytes; i_size = p_buffer->i_nb_bytes;
/* This is theoretical ... we'll see next iteration whether
* we're drifting */
next_date += p_buffer->end_date - p_buffer->start_date;
} }
else else
{ {
...@@ -354,6 +370,7 @@ static int OSSThread( aout_instance_t * p_aout ) ...@@ -354,6 +370,7 @@ static int OSSThread( aout_instance_t * p_aout )
* p_aout->output.output.i_bytes_per_frame; * p_aout->output.output.i_bytes_per_frame;
p_bytes = malloc( i_size ); p_bytes = malloc( i_size );
memset( p_bytes, 0, i_size ); memset( p_bytes, 0, i_size );
next_date = 0;
} }
i_tmp = write( p_sys->i_fd, p_bytes, i_size ); i_tmp = write( p_sys->i_fd, p_bytes, i_size );
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* sdl.c : SDL audio output plugin for vlc * sdl.c : SDL audio output plugin for vlc
***************************************************************************** *****************************************************************************
* Copyright (C) 2000-2002 VideoLAN * Copyright (C) 2000-2002 VideoLAN
* $Id: sdl.c,v 1.6 2002/08/25 09:40:00 sam Exp $ * $Id: sdl.c,v 1.7 2002/08/25 16:55:55 sam Exp $
* *
* Authors: Michel Kaempf <maxx@via.ecp.fr> * Authors: Michel Kaempf <maxx@via.ecp.fr>
* Samuel Hocevar <sam@zoy.org> * Samuel Hocevar <sam@zoy.org>
...@@ -48,7 +48,7 @@ ...@@ -48,7 +48,7 @@
*****************************************************************************/ *****************************************************************************/
struct aout_sys_t struct aout_sys_t
{ {
mtime_t call_time; mtime_t next_date;
mtime_t buffer_time; mtime_t buffer_time;
}; };
...@@ -78,7 +78,6 @@ vlc_module_end(); ...@@ -78,7 +78,6 @@ vlc_module_end();
static int Open ( vlc_object_t *p_this ) static int Open ( vlc_object_t *p_this )
{ {
aout_instance_t *p_aout = (aout_instance_t *)p_this; aout_instance_t *p_aout = (aout_instance_t *)p_this;
aout_sys_t * p_sys;
Uint32 i_flags = SDL_INIT_AUDIO; Uint32 i_flags = SDL_INIT_AUDIO;
...@@ -87,14 +86,6 @@ static int Open ( vlc_object_t *p_this ) ...@@ -87,14 +86,6 @@ static int Open ( vlc_object_t *p_this )
return VLC_EGENERIC; return VLC_EGENERIC;
} }
/* Allocate structure */
p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
if( p_sys == NULL )
{
msg_Err( p_aout, "out of memory" );
return VLC_ENOMEM;
}
p_aout->output.pf_setformat = SetFormat; p_aout->output.pf_setformat = SetFormat;
p_aout->output.pf_play = Play; p_aout->output.pf_play = Play;
...@@ -112,7 +103,6 @@ static int Open ( vlc_object_t *p_this ) ...@@ -112,7 +103,6 @@ static int Open ( vlc_object_t *p_this )
if( SDL_Init( i_flags ) < 0 ) if( SDL_Init( i_flags ) < 0 )
{ {
msg_Err( p_aout, "cannot initialize SDL (%s)", SDL_GetError() ); msg_Err( p_aout, "cannot initialize SDL (%s)", SDL_GetError() );
free( p_sys );
return VLC_EGENERIC; return VLC_EGENERIC;
} }
...@@ -124,8 +114,6 @@ static int Open ( vlc_object_t *p_this ) ...@@ -124,8 +114,6 @@ static int Open ( vlc_object_t *p_this )
*****************************************************************************/ *****************************************************************************/
static int SetFormat( aout_instance_t *p_aout ) static int SetFormat( aout_instance_t *p_aout )
{ {
aout_sys_t * p_sys = p_aout->output.p_sys;
/* TODO: finish and clean this */ /* TODO: finish and clean this */
SDL_AudioSpec desired; SDL_AudioSpec desired;
...@@ -145,10 +133,6 @@ static int SetFormat( aout_instance_t *p_aout ) ...@@ -145,10 +133,6 @@ static int SetFormat( aout_instance_t *p_aout )
p_aout->output.output.i_format = AOUT_FMT_S16_NE; p_aout->output.output.i_format = AOUT_FMT_S16_NE;
p_aout->output.i_nb_samples = FRAME_SIZE; p_aout->output.i_nb_samples = FRAME_SIZE;
p_sys->call_time = 0;
p_sys->buffer_time = (mtime_t)FRAME_SIZE * 1000000
/ p_aout->output.output.i_rate;
SDL_PauseAudio( 0 ); SDL_PauseAudio( 0 );
return VLC_SUCCESS; return VLC_SUCCESS;
...@@ -166,14 +150,9 @@ static void Play( aout_instance_t * p_aout ) ...@@ -166,14 +150,9 @@ static void Play( aout_instance_t * p_aout )
*****************************************************************************/ *****************************************************************************/
static void Close ( vlc_object_t *p_this ) static void Close ( vlc_object_t *p_this )
{ {
aout_instance_t *p_aout = (aout_instance_t *)p_this;
aout_sys_t * p_sys = p_aout->output.p_sys;
SDL_PauseAudio( 1 ); SDL_PauseAudio( 1 );
SDL_CloseAudio(); SDL_CloseAudio();
SDL_QuitSubSystem( SDL_INIT_AUDIO ); SDL_QuitSubSystem( SDL_INIT_AUDIO );
free( p_sys );
} }
/***************************************************************************** /*****************************************************************************
...@@ -182,35 +161,15 @@ static void Close ( vlc_object_t *p_this ) ...@@ -182,35 +161,15 @@ static void Close ( vlc_object_t *p_this )
static void SDLCallback( void * _p_aout, byte_t * p_stream, int i_len ) static void SDLCallback( void * _p_aout, byte_t * p_stream, int i_len )
{ {
aout_instance_t * p_aout = (aout_instance_t *)_p_aout; aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
aout_sys_t * p_sys = p_aout->output.p_sys; aout_buffer_t * p_buffer;
aout_buffer_t * p_buffer; /* SDL is unable to call us at regular times, or tell us its current
* hardware latency, or the buffer state. So we just pop data and throw
* it at SDL's face. Nah. */
/* We try to stay around call_time + buffer_time/2. This is kludgy but vlc_mutex_lock( &p_aout->mixer_lock );
* unavoidable because SDL is completely unable to 1. tell us about its p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
* latency, and 2. call SDLCallback at regular intervals. */ vlc_mutex_unlock( &p_aout->mixer_lock );
if( mdate() < p_sys->call_time + p_sys->buffer_time / 2 )
{
/* We can't wait too much, because SDL will be lost, and we can't
* wait too little, because we are not sure that there will be
* samples in the queue. */
mwait( p_sys->call_time + p_sys->buffer_time / 4 );
p_sys->call_time += p_sys->buffer_time;
}
else
{
p_sys->call_time = mdate() + p_sys->buffer_time / 4;
}
/* Tell the output we're playing samples at call_time + 2*buffer_time */
p_buffer = aout_OutputNextBuffer( p_aout, p_sys->call_time
+ 2 * p_sys->buffer_time, VLC_TRUE );
if ( i_len != FRAME_SIZE * sizeof(s16)
* p_aout->output.output.i_channels )
{
msg_Err( p_aout, "SDL doesn't know its buffer size (%d)", i_len );
}
if ( p_buffer != NULL ) if ( p_buffer != NULL )
{ {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* output.c : internal management of output streams for the audio output * output.c : internal management of output streams for the audio output
***************************************************************************** *****************************************************************************
* Copyright (C) 2002 VideoLAN * Copyright (C) 2002 VideoLAN
* $Id: output.c,v 1.11 2002/08/25 09:40:00 sam Exp $ * $Id: output.c,v 1.12 2002/08/25 16:55:55 sam Exp $
* *
* Authors: Christophe Massiot <massiot@via.ecp.fr> * Authors: Christophe Massiot <massiot@via.ecp.fr>
* *
...@@ -171,7 +171,7 @@ aout_buffer_t * aout_OutputNextBuffer( aout_instance_t * p_aout, ...@@ -171,7 +171,7 @@ aout_buffer_t * aout_OutputNextBuffer( aout_instance_t * p_aout,
vlc_mutex_lock( &p_aout->mixer_lock ); vlc_mutex_lock( &p_aout->mixer_lock );
p_buffer = p_aout->output.fifo.p_first; p_buffer = p_aout->output.fifo.p_first;
while ( p_buffer && p_buffer->start_date < start_date && p_buffer->p_next ) while ( p_buffer && p_buffer->start_date < start_date )
{ {
msg_Dbg( p_aout, "audio output is too slow (%lld), trashing %lldus", msg_Dbg( p_aout, "audio output is too slow (%lld), trashing %lldus",
start_date - p_buffer->start_date, start_date - p_buffer->start_date,
......
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