sound synchronization.
* ./src/audio_output/output.c: reverted another change I previously did :-)
* oss.c : OSS /dev/dsp module for vlc
*****************************************************************************
* 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>
* Samuel Hocevar <sam@zoy.org>
/*****************************************************************************
- * GetBufInfo: buffer status query
+ * BufferDuration: buffer status query
*****************************************************************************
- * This function fills in the audio_buf_info structure :
- * - 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
+ * This function returns the duration in microseconfs of the current buffer.
*****************************************************************************/
-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;
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 );
- /* returns the allocated space in bytes */
- return ( (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes );
+ /* calculate number of available fragments (not partially used ones) */
+ 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;
}
/*****************************************************************************
static int OSSThread( aout_instance_t * p_aout )
{
struct aout_sys_t * p_sys = p_aout->output.p_sys;
+ mtime_t next_date = 0;
while ( !p_aout->b_die )
{
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 );
+ 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 */
- p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
- VLC_FALSE );
+ p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
}
else
{
{
p_bytes = p_buffer->p_buffer;
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
{
* p_aout->output.output.i_bytes_per_frame;
p_bytes = malloc( i_size );
memset( p_bytes, 0, i_size );
+ next_date = 0;
}
i_tmp = write( p_sys->i_fd, p_bytes, i_size );
* sdl.c : SDL audio output plugin for vlc
*****************************************************************************
* 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>
* Samuel Hocevar <sam@zoy.org>
*****************************************************************************/
struct aout_sys_t
{
- mtime_t call_time;
+ mtime_t next_date;
mtime_t buffer_time;
};
static int Open ( vlc_object_t *p_this )
{
aout_instance_t *p_aout = (aout_instance_t *)p_this;
- aout_sys_t * p_sys;
Uint32 i_flags = SDL_INIT_AUDIO;
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_play = Play;
if( SDL_Init( i_flags ) < 0 )
{
msg_Err( p_aout, "cannot initialize SDL (%s)", SDL_GetError() );
- free( p_sys );
return VLC_EGENERIC;
}
*****************************************************************************/
static int SetFormat( aout_instance_t *p_aout )
{
- aout_sys_t * p_sys = p_aout->output.p_sys;
-
/* TODO: finish and clean this */
SDL_AudioSpec desired;
p_aout->output.output.i_format = AOUT_FMT_S16_NE;
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 );
return VLC_SUCCESS;
*****************************************************************************/
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_CloseAudio();
SDL_QuitSubSystem( SDL_INIT_AUDIO );
-
- free( p_sys );
}
/*****************************************************************************
static void SDLCallback( void * _p_aout, byte_t * p_stream, int i_len )
{
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
- * unavoidable because SDL is completely unable to 1. tell us about its
- * latency, and 2. call SDLCallback at regular intervals. */
- 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 );
- }
+ vlc_mutex_lock( &p_aout->mixer_lock );
+ p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
+ vlc_mutex_unlock( &p_aout->mixer_lock );
if ( p_buffer != NULL )
{
* output.c : internal management of output streams for the audio output
*****************************************************************************
* 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>
*
vlc_mutex_lock( &p_aout->mixer_lock );
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",
start_date - p_buffer->start_date,