|
|
|
@ -2,7 +2,7 @@ |
|
|
|
* mixer.c : audio output mixing operations |
|
|
|
***************************************************************************** |
|
|
|
* Copyright (C) 2002 VideoLAN |
|
|
|
* $Id: mixer.c,v 1.7 2002/08/19 21:54:37 massiot Exp $ |
|
|
|
* $Id: mixer.c,v 1.8 2002/08/19 23:12:57 massiot Exp $ |
|
|
|
* |
|
|
|
* Authors: Christophe Massiot <massiot@via.ecp.fr> |
|
|
|
* |
|
|
|
@ -59,19 +59,17 @@ void aout_MixerDelete( aout_instance_t * p_aout ) |
|
|
|
} |
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* aout_MixerRun: entry point for the mixer & post-filters processing |
|
|
|
* MixBuffer: try to prepare one output buffer |
|
|
|
*****************************************************************************/ |
|
|
|
void aout_MixerRun( aout_instance_t * p_aout ) |
|
|
|
static int MixBuffer( aout_instance_t * p_aout ) |
|
|
|
{ |
|
|
|
int i; |
|
|
|
aout_buffer_t * p_output_buffer; |
|
|
|
mtime_t start_date, end_date; |
|
|
|
|
|
|
|
/* Retrieve the date of the next buffer. We don't use aout_FifoNextStart
|
|
|
|
* because we need to keep the lock on the FIFO, to prevent the aout |
|
|
|
* thread from triggering resampling while we are running. */ |
|
|
|
vlc_mutex_lock( &p_aout->output.fifo.lock ); |
|
|
|
start_date = p_aout->output.fifo.end_date; |
|
|
|
/* Retrieve the date of the next buffer. */ |
|
|
|
vlc_mutex_lock( &p_aout->mixer_lock ); |
|
|
|
start_date = aout_FifoNextStart( p_aout, &p_aout->output.fifo ); |
|
|
|
if ( start_date != 0 && start_date < mdate() ) |
|
|
|
{ |
|
|
|
/* The output is _very_ late. This can only happen if the user
|
|
|
|
@ -81,11 +79,42 @@ void aout_MixerRun( aout_instance_t * p_aout ) |
|
|
|
start_date ); |
|
|
|
start_date = p_aout->output.fifo.end_date = 0; |
|
|
|
} |
|
|
|
|
|
|
|
/* See if we have enough data to prepare a new buffer for the audio
|
|
|
|
* output. First : start date. */ |
|
|
|
if ( !start_date ) |
|
|
|
{ |
|
|
|
/* Find the latest start date available. */ |
|
|
|
for ( i = 0; i < p_aout->i_nb_inputs; i++ ) |
|
|
|
{ |
|
|
|
aout_input_t * p_input = p_aout->pp_inputs[i]; |
|
|
|
aout_fifo_t * p_fifo = &p_input->fifo; |
|
|
|
aout_buffer_t * p_buffer; |
|
|
|
|
|
|
|
p_buffer = p_fifo->p_first; |
|
|
|
if ( p_buffer == NULL ) |
|
|
|
{ |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
if ( !start_date || start_date < p_buffer->start_date ) |
|
|
|
{ |
|
|
|
start_date = p_buffer->start_date; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if ( i < p_aout->i_nb_inputs ) |
|
|
|
{ |
|
|
|
/* Interrupted before the end... We can't run. */ |
|
|
|
vlc_mutex_unlock( &p_aout->mixer_lock ); |
|
|
|
return -1; |
|
|
|
} |
|
|
|
} |
|
|
|
end_date = start_date + (mtime_t)p_aout->output.i_nb_samples * 1000000 |
|
|
|
/ p_aout->output.output.i_rate; |
|
|
|
|
|
|
|
/* See if we have enough data to prepare a new buffer for the audio
|
|
|
|
* output. */ |
|
|
|
/* Check that start_date and end_date are available for all input
|
|
|
|
* streams. */ |
|
|
|
for ( i = 0; i < p_aout->i_nb_inputs; i++ ) |
|
|
|
{ |
|
|
|
aout_input_t * p_input = p_aout->pp_inputs[i]; |
|
|
|
@ -94,73 +123,60 @@ void aout_MixerRun( aout_instance_t * p_aout ) |
|
|
|
mtime_t prev_date; |
|
|
|
vlc_bool_t b_drop_buffers; |
|
|
|
|
|
|
|
vlc_mutex_lock( &p_fifo->lock ); |
|
|
|
|
|
|
|
p_buffer = p_fifo->p_first; |
|
|
|
if ( p_buffer == NULL ) |
|
|
|
{ |
|
|
|
vlc_mutex_unlock( &p_fifo->lock ); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
if ( !start_date ) |
|
|
|
/* Check for the continuity of start_date */ |
|
|
|
while ( p_buffer != NULL && p_buffer->end_date < start_date ) |
|
|
|
{ |
|
|
|
start_date = p_buffer->start_date; |
|
|
|
end_date += p_buffer->start_date; |
|
|
|
p_input->p_first_byte_to_mix = p_buffer->p_buffer; |
|
|
|
aout_buffer_t * p_next = p_buffer->p_next; |
|
|
|
msg_Err( p_aout, "the mixer got a packet in the past (%lld)", |
|
|
|
start_date - p_buffer->end_date ); |
|
|
|
aout_BufferFree( p_buffer ); |
|
|
|
p_fifo->p_first = p_buffer = p_next; |
|
|
|
p_input->p_first_byte_to_mix = NULL; |
|
|
|
} |
|
|
|
else |
|
|
|
if ( p_buffer == NULL ) |
|
|
|
{ |
|
|
|
/* Check for the continuity of start_date */ |
|
|
|
while ( p_buffer != NULL && p_buffer->end_date < start_date ) |
|
|
|
{ |
|
|
|
aout_buffer_t * p_next = p_buffer->p_next; |
|
|
|
msg_Err( p_aout, "the mixer got a packet in the past (%lld)", |
|
|
|
start_date - p_buffer->end_date ); |
|
|
|
aout_BufferFree( p_buffer ); |
|
|
|
p_fifo->p_first = p_buffer = p_next; |
|
|
|
p_input->p_first_byte_to_mix = NULL; |
|
|
|
} |
|
|
|
if ( p_buffer == NULL ) |
|
|
|
p_fifo->pp_last = &p_fifo->p_first; |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) ) |
|
|
|
{ |
|
|
|
/* Additionally check that p_first_byte_to_mix is well
|
|
|
|
* located. */ |
|
|
|
unsigned long i_nb_bytes = (start_date - p_buffer->start_date) |
|
|
|
* p_aout->mixer.mixer.i_bytes_per_frame |
|
|
|
* p_aout->mixer.mixer.i_rate |
|
|
|
/ p_aout->mixer.mixer.i_frame_length |
|
|
|
/ 1000000; |
|
|
|
ptrdiff_t mixer_nb_bytes; |
|
|
|
|
|
|
|
if ( p_input->p_first_byte_to_mix == NULL ) |
|
|
|
{ |
|
|
|
p_fifo->pp_last = &p_fifo->p_first; |
|
|
|
vlc_mutex_unlock( &p_fifo->lock ); |
|
|
|
break; |
|
|
|
p_input->p_first_byte_to_mix = p_buffer->p_buffer; |
|
|
|
} |
|
|
|
mixer_nb_bytes = p_input->p_first_byte_to_mix |
|
|
|
- p_buffer->p_buffer; |
|
|
|
|
|
|
|
if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) ) |
|
|
|
if ( i_nb_bytes + p_aout->mixer.mixer.i_bytes_per_frame |
|
|
|
< mixer_nb_bytes || |
|
|
|
i_nb_bytes - p_aout->mixer.mixer.i_bytes_per_frame |
|
|
|
> mixer_nb_bytes ) |
|
|
|
{ |
|
|
|
/* Additionally check that p_first_byte_to_mix is well
|
|
|
|
* located. */ |
|
|
|
unsigned long i_nb_bytes = (start_date - p_buffer->start_date) |
|
|
|
* p_aout->mixer.mixer.i_bytes_per_frame |
|
|
|
* p_aout->mixer.mixer.i_rate |
|
|
|
/ p_aout->mixer.mixer.i_frame_length |
|
|
|
/ 1000000; |
|
|
|
ptrdiff_t mixer_nb_bytes; |
|
|
|
|
|
|
|
if ( p_input->p_first_byte_to_mix == NULL ) |
|
|
|
{ |
|
|
|
p_input->p_first_byte_to_mix = p_buffer->p_buffer; |
|
|
|
} |
|
|
|
mixer_nb_bytes = p_input->p_first_byte_to_mix |
|
|
|
- p_buffer->p_buffer; |
|
|
|
msg_Warn( p_aout, |
|
|
|
"mixer start isn't output start (%ld)", |
|
|
|
i_nb_bytes - mixer_nb_bytes ); |
|
|
|
|
|
|
|
if ( i_nb_bytes + p_aout->mixer.mixer.i_bytes_per_frame |
|
|
|
< mixer_nb_bytes || |
|
|
|
i_nb_bytes - p_aout->mixer.mixer.i_bytes_per_frame |
|
|
|
> mixer_nb_bytes ) |
|
|
|
{ |
|
|
|
msg_Warn( p_aout, |
|
|
|
"mixer start isn't output start (%ld)", |
|
|
|
i_nb_bytes - mixer_nb_bytes ); |
|
|
|
|
|
|
|
/* Round to the nearest multiple */ |
|
|
|
i_nb_bytes /= p_aout->mixer.mixer.i_bytes_per_frame; |
|
|
|
i_nb_bytes *= p_aout->mixer.mixer.i_bytes_per_frame; |
|
|
|
p_input->p_first_byte_to_mix = p_buffer->p_buffer |
|
|
|
+ i_nb_bytes; |
|
|
|
} |
|
|
|
/* Round to the nearest multiple */ |
|
|
|
i_nb_bytes /= p_aout->mixer.mixer.i_bytes_per_frame; |
|
|
|
i_nb_bytes *= p_aout->mixer.mixer.i_bytes_per_frame; |
|
|
|
p_input->p_first_byte_to_mix = p_buffer->p_buffer |
|
|
|
+ i_nb_bytes; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -180,7 +196,7 @@ void aout_MixerRun( aout_instance_t * p_aout ) |
|
|
|
if ( prev_date != p_buffer->start_date ) |
|
|
|
{ |
|
|
|
msg_Warn( p_aout, |
|
|
|
"buffer discontinuity, dropping packets (%lld)", |
|
|
|
"buffer hole, dropping packets (%lld)", |
|
|
|
p_buffer->start_date - prev_date ); |
|
|
|
b_drop_buffers = 1; |
|
|
|
break; |
|
|
|
@ -201,20 +217,16 @@ void aout_MixerRun( aout_instance_t * p_aout ) |
|
|
|
} |
|
|
|
else break; |
|
|
|
} |
|
|
|
vlc_mutex_unlock( &p_fifo->lock ); |
|
|
|
if ( p_buffer == NULL ) break; |
|
|
|
} |
|
|
|
|
|
|
|
if ( i < p_aout->i_nb_inputs ) |
|
|
|
{ |
|
|
|
/* Interrupted before the end... We can't run. */ |
|
|
|
vlc_mutex_unlock( &p_aout->output.fifo.lock ); |
|
|
|
return; |
|
|
|
vlc_mutex_unlock( &p_aout->mixer_lock ); |
|
|
|
return -1; |
|
|
|
} |
|
|
|
|
|
|
|
p_aout->b_mixer_active = 1; |
|
|
|
vlc_mutex_unlock( &p_aout->output.fifo.lock ); |
|
|
|
|
|
|
|
/* Run the mixer. */ |
|
|
|
aout_BufferAlloc( &p_aout->mixer.output_alloc, |
|
|
|
((u64)p_aout->output.i_nb_samples * 1000000) |
|
|
|
@ -226,22 +238,29 @@ void aout_MixerRun( aout_instance_t * p_aout ) |
|
|
|
if ( p_output_buffer == NULL ) |
|
|
|
{ |
|
|
|
msg_Err( p_aout, "out of memory" ); |
|
|
|
return; |
|
|
|
vlc_mutex_unlock( &p_aout->mixer_lock ); |
|
|
|
return -1; |
|
|
|
} |
|
|
|
p_output_buffer->i_nb_samples = p_aout->output.i_nb_samples; |
|
|
|
p_output_buffer->i_nb_bytes = p_aout->output.i_nb_samples |
|
|
|
* p_aout->output.output.i_bytes_per_frame |
|
|
|
/ p_aout->output.output.i_frame_length; |
|
|
|
* p_aout->mixer.mixer.i_bytes_per_frame |
|
|
|
/ p_aout->mixer.mixer.i_frame_length; |
|
|
|
p_output_buffer->start_date = start_date; |
|
|
|
p_output_buffer->end_date = end_date; |
|
|
|
|
|
|
|
p_aout->mixer.pf_do_work( p_aout, p_output_buffer ); |
|
|
|
|
|
|
|
vlc_mutex_unlock( &p_aout->mixer_lock ); |
|
|
|
|
|
|
|
aout_OutputPlay( p_aout, p_output_buffer ); |
|
|
|
|
|
|
|
vlc_mutex_lock( &p_aout->output.fifo.lock ); |
|
|
|
p_aout->b_mixer_active = 0; |
|
|
|
vlc_cond_signal( &p_aout->mixer_signal ); |
|
|
|
vlc_mutex_unlock( &p_aout->output.fifo.lock ); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* aout_MixerRun: entry point for the mixer & post-filters processing |
|
|
|
*****************************************************************************/ |
|
|
|
void aout_MixerRun( aout_instance_t * p_aout ) |
|
|
|
{ |
|
|
|
while( MixBuffer( p_aout ) != -1 ); |
|
|
|
} |
|
|
|
|