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_aout, "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;
98 assert( p_aout->p_input != NULL );
100 aout_input_t *p_input = p_aout->p_input;
101 if( p_input->b_paused )
104 aout_fifo_t * p_fifo = &p_input->mixer.fifo;
105 mtime_t now = mdate();
107 aout_lock_input_fifos( p_aout );
108 aout_lock_output_fifo( p_aout );
110 /* Retrieve the date of the next buffer. */
111 date_t exact_start_date = p_aout->output.fifo.end_date;
112 mtime_t start_date = date_Get( &exact_start_date );
114 if( start_date != 0 && start_date < now )
116 /* The output is _very_ late. This can only happen if the user
117 * pauses the stream (or if the decoder is buggy, which cannot
119 msg_Warn( p_aout, "output PTS is out of range (%"PRId64"), clearing out",
120 mdate() - start_date );
121 aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
122 date_Set( &exact_start_date, 0 );
126 aout_unlock_output_fifo( p_aout );
128 /* See if we have enough data to prepare a new buffer for the audio
129 * output. First : start date. */
132 /* Find the latest start date available. */
133 aout_buffer_t *p_buffer;
136 p_buffer = p_fifo->p_first;
137 if( p_buffer == NULL )
139 if( p_buffer->i_pts >= now )
142 msg_Warn( p_aout, "input PTS is out of range (%"PRId64"), "
143 "trashing", now - p_buffer->i_pts );
144 aout_BufferFree( aout_FifoPop( p_aout, p_fifo ) );
145 p_input->mixer.begin = NULL;
148 date_Set( &exact_start_date, p_buffer->i_pts );
149 start_date = p_buffer->i_pts;
152 date_Increment( &exact_start_date, p_aout->output.i_nb_samples );
153 mtime_t end_date = date_Get( &exact_start_date );
155 /* Check that start_date is available. */
156 aout_buffer_t *p_buffer = p_fifo->p_first;
161 if( p_buffer == NULL )
164 /* Check for the continuity of start_date */
165 prev_date = p_buffer->i_pts + p_buffer->i_length;
166 if( prev_date >= start_date - 1 )
168 /* We authorize a +-1 because rounding errors get compensated
170 msg_Warn( p_aout, "the mixer got a packet in the past (%"PRId64")",
171 start_date - prev_date );
172 aout_BufferFree( aout_FifoPop( p_aout, p_fifo ) );
173 p_input->mixer.begin = NULL;
174 p_buffer = p_fifo->p_first;
177 /* Check that we have enough samples. */
178 while( prev_date < end_date )
180 p_buffer = p_buffer->p_next;
181 if( p_buffer == NULL )
184 /* Check that all buffers are contiguous. */
185 if( prev_date != p_buffer->i_pts )
188 "buffer hole, dropping packets (%"PRId64")",
189 p_buffer->i_pts - prev_date );
191 aout_buffer_t *p_deleted;
192 while( (p_deleted = p_fifo->p_first) != p_buffer )
193 aout_BufferFree( aout_FifoPop( p_aout, p_fifo ) );
196 prev_date = p_buffer->i_pts + p_buffer->i_length;
199 p_buffer = p_fifo->p_first;
200 if( !AOUT_FMT_NON_LINEAR( &p_mixer->fmt ) )
202 /* Additionally check that p_first_byte_to_mix is well located. */
203 mtime_t i_buffer = (start_date - p_buffer->i_pts)
204 * p_mixer->fmt.i_bytes_per_frame
205 * p_mixer->fmt.i_rate
206 / p_mixer->fmt.i_frame_length
208 if( p_input->mixer.begin == NULL )
209 p_input->mixer.begin = p_buffer->p_buffer;
211 ptrdiff_t bytes = p_input->mixer.begin - p_buffer->p_buffer;
212 if( !((i_buffer + p_mixer->fmt.i_bytes_per_frame > bytes)
213 && (i_buffer < p_mixer->fmt.i_bytes_per_frame + bytes)) )
215 msg_Warn( p_aout, "mixer start is not output start (%"PRId64")",
218 /* Round to the nearest multiple */
219 i_buffer /= p_mixer->fmt.i_bytes_per_frame;
220 i_buffer *= p_mixer->fmt.i_bytes_per_frame;
223 /* Is it really the best way to do it ? */
224 aout_lock_output_fifo( p_aout );
225 aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
226 date_Set( &exact_start_date, 0 );
227 aout_unlock_output_fifo( p_aout );
230 p_input->mixer.begin = p_buffer->p_buffer + i_buffer;
235 p_buffer = p_mixer->mix( p_aout->p_mixer, p_aout->output.i_nb_samples,
237 aout_unlock_input_fifos( p_aout );
239 if( unlikely(p_buffer == NULL) )
242 p_buffer->i_pts = start_date;
243 p_buffer->i_length = end_date - start_date;
244 aout_OutputPlay( p_aout, p_buffer );
248 /* Interrupted before the end... We can't run. */
249 aout_unlock_input_fifos( p_aout );
253 /*****************************************************************************
254 * aout_MixerRun: entry point for the mixer & post-filters processing
255 *****************************************************************************
256 * Please note that you must hold the mixer lock.
257 *****************************************************************************/
258 void aout_MixerRun( aout_instance_t * p_aout, float volume )
260 while( MixBuffer( p_aout, volume ) != -1 );