1 /*****************************************************************************
2 * mixer.c : audio output mixing operations
3 *****************************************************************************
4 * Copyright (C) 2002-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_common.h>
35 #include <vlc_modules.h>
38 #include "aout_internal.h"
39 /*****************************************************************************
40 * aout_MixerNew: prepare a mixer plug-in
41 *****************************************************************************
42 * Please note that you must hold the mixer lock.
43 *****************************************************************************/
44 int aout_MixerNew( aout_instance_t * p_aout )
46 assert( !p_aout->p_mixer );
47 vlc_assert_locked( &p_aout->input_fifos_lock );
49 aout_mixer_t *p_mixer = vlc_object_create( p_aout, sizeof(*p_mixer) );
53 p_mixer->fmt = p_aout->mixer_format;
54 p_mixer->input = &p_aout->p_input->mixer;
58 p_mixer->module = module_need( p_mixer, "audio mixer", NULL, false );
59 if( !p_mixer->module )
61 msg_Err( p_mixer, "no suitable audio mixer" );
62 vlc_object_release( p_mixer );
67 p_aout->p_mixer = p_mixer;
71 /*****************************************************************************
72 * aout_MixerDelete: delete the mixer
73 *****************************************************************************
74 * Please note that you must hold the mixer lock.
75 *****************************************************************************/
76 void aout_MixerDelete( aout_instance_t * p_aout )
78 if( !p_aout->p_mixer )
81 module_unneed( p_aout->p_mixer, p_aout->p_mixer->module );
83 vlc_object_release( p_aout->p_mixer );
86 p_aout->p_mixer = NULL;
89 /*****************************************************************************
90 * MixBuffer: try to prepare one output buffer
91 *****************************************************************************
92 * Please note that you must hold the mixer lock.
93 *****************************************************************************/
94 static int MixBuffer( aout_instance_t * p_aout, float volume )
96 aout_mixer_t *p_mixer = p_aout->p_mixer;
97 aout_mixer_input_t *p_input = p_mixer->input;
98 aout_fifo_t *p_fifo = &p_input->fifo;
99 mtime_t now = mdate();
101 aout_lock_input_fifos( p_aout );
102 aout_lock_output_fifo( p_aout );
104 /* Retrieve the date of the next buffer. */
105 date_t exact_start_date = p_aout->output.fifo.end_date;
106 mtime_t start_date = date_Get( &exact_start_date );
108 if( start_date != 0 && start_date < now )
110 /* The output is _very_ late. This can only happen if the user
111 * pauses the stream (or if the decoder is buggy, which cannot
113 msg_Warn( p_mixer, "output PTS is out of range (%"PRId64"), clearing out",
114 mdate() - start_date );
115 aout_FifoSet( &p_aout->output.fifo, 0 );
116 date_Set( &exact_start_date, 0 );
120 aout_unlock_output_fifo( p_aout );
122 /* See if we have enough data to prepare a new buffer for the audio
123 * output. First : start date. */
126 /* Find the latest start date available. */
127 aout_buffer_t *p_buffer;
130 p_buffer = p_fifo->p_first;
131 if( p_buffer == NULL )
133 if( p_buffer->i_pts >= now )
136 msg_Warn( p_mixer, "input PTS is out of range (%"PRId64"), "
137 "trashing", now - p_buffer->i_pts );
138 aout_BufferFree( aout_FifoPop( p_fifo ) );
141 date_Set( &exact_start_date, p_buffer->i_pts );
142 start_date = p_buffer->i_pts;
145 date_Increment( &exact_start_date, p_aout->output.i_nb_samples );
146 mtime_t end_date = date_Get( &exact_start_date );
148 /* Check that start_date is available. */
149 aout_buffer_t *p_buffer = p_fifo->p_first;
154 if( p_buffer == NULL )
157 /* Check for the continuity of start_date */
158 prev_date = p_buffer->i_pts + p_buffer->i_length;
159 if( prev_date >= start_date - 1 )
161 /* We authorize a +-1 because rounding errors get compensated
163 msg_Warn( p_mixer, "the mixer got a packet in the past (%"PRId64")",
164 start_date - prev_date );
165 aout_BufferFree( aout_FifoPop( p_fifo ) );
166 p_buffer = p_fifo->p_first;
169 /* Check that we have enough samples. */
170 while( prev_date < end_date )
172 p_buffer = p_buffer->p_next;
173 if( p_buffer == NULL )
176 /* Check that all buffers are contiguous. */
177 if( prev_date != p_buffer->i_pts )
180 "buffer hole, dropping packets (%"PRId64")",
181 p_buffer->i_pts - prev_date );
183 aout_buffer_t *p_deleted;
184 while( (p_deleted = p_fifo->p_first) != p_buffer )
185 aout_BufferFree( aout_FifoPop( p_fifo ) );
188 prev_date = p_buffer->i_pts + p_buffer->i_length;
191 if( !AOUT_FMT_NON_LINEAR( &p_mixer->fmt ) )
193 p_buffer = p_fifo->p_first;
195 /* Additionally check that p_first_byte_to_mix is well located. */
196 const unsigned framesize = p_mixer->fmt.i_bytes_per_frame;
197 ssize_t delta = (start_date - p_buffer->i_pts)
198 * p_mixer->fmt.i_rate / CLOCK_FREQ;
200 msg_Warn( p_mixer, "mixer start is not output end (%zd)", delta );
203 /* Is it really the best way to do it ? */
204 aout_lock_output_fifo( p_aout );
205 aout_FifoSet( &p_aout->output.fifo, 0 );
206 date_Set( &exact_start_date, 0 );
207 aout_unlock_output_fifo( p_aout );
212 p_buffer->i_nb_samples -= delta;
213 p_buffer->i_pts += delta * CLOCK_FREQ / p_mixer->fmt.i_rate;
214 p_buffer->i_length -= delta * CLOCK_FREQ / p_mixer->fmt.i_rate;
216 p_buffer->p_buffer += delta;
217 p_buffer->i_buffer -= delta;
220 /* Build packet with adequate number of samples */
221 const unsigned samples = p_aout->output.i_nb_samples;
222 unsigned needed = samples * framesize;
223 p_buffer = block_Alloc( needed );
224 if( unlikely(p_buffer == NULL) )
225 /* XXX: should free input buffers */
227 p_buffer->i_nb_samples = samples;
229 for( uint8_t *p_out = p_buffer->p_buffer; needed > 0; )
231 aout_buffer_t *p_inbuf = p_fifo->p_first;
232 if( unlikely(p_inbuf == NULL) )
234 msg_Err( p_mixer, "internal amix error" );
235 vlc_memset( p_out, 0, needed );
239 const uint8_t *p_in = p_inbuf->p_buffer;
240 size_t avail = p_inbuf->i_nb_samples * framesize;
243 vlc_memcpy( p_out, p_in, needed );
244 p_fifo->p_first->p_buffer += needed;
245 p_fifo->p_first->i_buffer -= needed;
247 p_fifo->p_first->i_nb_samples -= needed;
248 p_fifo->p_first->i_pts += needed * CLOCK_FREQ / p_mixer->fmt.i_rate;
249 p_fifo->p_first->i_length -= needed * CLOCK_FREQ / p_mixer->fmt.i_rate;
253 vlc_memcpy( p_out, p_in, avail );
257 aout_BufferFree( aout_FifoPop( p_fifo ) );
261 p_buffer = aout_FifoPop( p_fifo );
263 p_buffer->i_pts = start_date;
264 p_buffer->i_length = end_date - start_date;
267 p_mixer->mix( p_mixer, p_buffer, volume );
268 aout_unlock_input_fifos( p_aout );
269 aout_OutputPlay( p_aout, p_buffer );
273 /* Interrupted before the end... We can't run. */
274 aout_unlock_input_fifos( p_aout );
278 /*****************************************************************************
279 * aout_MixerRun: entry point for the mixer & post-filters processing
280 *****************************************************************************
281 * Please note that you must hold the mixer lock.
282 *****************************************************************************/
283 void aout_MixerRun( aout_instance_t * p_aout, float volume )
285 while( MixBuffer( p_aout, volume ) != -1 );