1 /*****************************************************************************
2 * mixer.c : audio output mixing operations
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: mixer.c,v 1.3 2002/08/12 22:12:51 massiot Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* calloc(), malloc(), free() */
36 #include "audio_output.h"
37 #include "aout_internal.h"
39 /*****************************************************************************
40 * aout_MixerNew: prepare a mixer plug-in
41 *****************************************************************************/
42 int aout_MixerNew( aout_instance_t * p_aout )
44 p_aout->mixer.p_module = module_Need( p_aout, "audio mixer", NULL );
45 if ( p_aout->mixer.p_module == NULL )
47 msg_Err( p_aout, "no suitable aout mixer" );
53 /*****************************************************************************
54 * aout_MixerDelete: delete the mixer
55 *****************************************************************************/
56 void aout_MixerDelete( aout_instance_t * p_aout )
58 module_Unneed( p_aout, p_aout->mixer.p_module );
61 /*****************************************************************************
62 * aout_MixerRun: entry point for the mixer & post-filters processing
63 *****************************************************************************/
64 void aout_MixerRun( aout_instance_t * p_aout )
67 aout_buffer_t * p_output_buffer;
69 /* See if we have enough data to prepare a new buffer for the audio
71 mtime_t wanted_date = 0, first_date = 0;
73 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
75 aout_fifo_t * p_fifo = &p_aout->pp_inputs[i]->fifo;
76 aout_buffer_t * p_buffer;
77 vlc_mutex_lock( &p_fifo->lock );
78 for ( p_buffer = p_fifo->p_first; p_buffer != NULL;
79 p_buffer = p_buffer->p_next )
83 if ( !p_aout->output.last_date )
85 first_date = p_buffer->start_date;
86 wanted_date = p_buffer->start_date
87 + (mtime_t)p_aout->output.i_nb_samples * 1000000
88 / p_aout->output.output.i_rate;
92 first_date = p_aout->output.last_date;
93 wanted_date = p_aout->output.last_date
94 + (mtime_t)p_aout->output.i_nb_samples * 1000000
95 / p_aout->output.output.i_rate;
99 if ( p_buffer->end_date >= wanted_date ) break;
101 vlc_mutex_unlock( &p_fifo->lock );
102 if ( p_buffer == NULL ) break;
105 if ( i < p_aout->i_nb_inputs )
107 /* Interrupted before the end... We can't run. */
112 aout_BufferAlloc( &p_aout->mixer.output_alloc,
113 ((u64)p_aout->output.i_nb_samples * 1000000)
114 / p_aout->output.output.i_rate,
115 /* This is a bit kludgy, but is actually only used
116 * for the S/PDIF dummy mixer : */
117 p_aout->pp_inputs[0]->fifo.p_first,
119 if ( p_output_buffer == NULL )
121 msg_Err( p_aout, "out of memory" );
124 p_output_buffer->i_nb_samples = p_aout->output.i_nb_samples;
125 p_output_buffer->start_date = first_date;
126 p_output_buffer->end_date = wanted_date;
127 p_aout->output.last_date = wanted_date;
129 p_aout->mixer.pf_do_work( p_aout, p_output_buffer );
131 aout_OutputPlay( p_aout, p_output_buffer );