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->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 vlc_assert_locked( &p_aout->lock );
80 if( !p_aout->p_mixer )
83 module_unneed( p_aout->p_mixer, p_aout->p_mixer->module );
84 vlc_object_release( p_aout->p_mixer );
85 p_aout->p_mixer = NULL;
88 /*****************************************************************************
89 * MixBuffer: try to prepare one output buffer
90 *****************************************************************************
91 * Please note that you must hold the mixer lock.
92 *****************************************************************************/
93 static int MixBuffer( aout_instance_t * p_aout, float volume )
95 aout_mixer_t *p_mixer = p_aout->p_mixer;
96 aout_mixer_input_t *p_input = p_mixer->input;
97 aout_fifo_t *p_fifo = &p_input->fifo;
98 mtime_t now = mdate();
99 const unsigned samples = p_aout->i_nb_samples;
100 /* FIXME: Remove this silly constraint. Just pass buffers as they come to
101 * "smart" audio outputs. */
102 assert( samples > 0 );
104 vlc_assert_locked( &p_aout->lock );
106 /* Retrieve the date of the next buffer. */
107 date_t exact_start_date = p_aout->fifo.end_date;
108 mtime_t start_date = date_Get( &exact_start_date );
110 if( start_date != 0 && start_date < now )
112 /* The output is _very_ late. This can only happen if the user
113 * pauses the stream (or if the decoder is buggy, which cannot
115 msg_Warn( p_mixer, "output PTS is out of range (%"PRId64"), clearing out",
116 mdate() - start_date );
117 aout_FifoSet( &p_aout->fifo, 0 );
118 date_Set( &exact_start_date, 0 );
122 /* See if we have enough data to prepare a new buffer for the audio output. */
123 aout_buffer_t *p_buffer = p_fifo->p_first;
124 if( p_buffer == NULL )
127 /* Find the earliest start date available. */
130 start_date = p_buffer->i_pts;
131 date_Set( &exact_start_date, start_date );
133 /* Compute the end date for the new buffer. */
134 mtime_t end_date = date_Increment( &exact_start_date, samples );
136 /* Check that start_date is available. */
140 /* Check for the continuity of start_date */
141 prev_date = p_buffer->i_pts + p_buffer->i_length;
142 if( prev_date >= start_date - 1 )
144 /* We authorize a +-1 because rounding errors get compensated
146 msg_Warn( p_mixer, "the mixer got a packet in the past (%"PRId64")",
147 start_date - prev_date );
148 aout_BufferFree( aout_FifoPop( p_fifo ) );
150 p_buffer = p_fifo->p_first;
151 if( p_buffer == NULL )
155 /* Check that we have enough samples. */
156 while( prev_date < end_date )
158 p_buffer = p_buffer->p_next;
159 if( p_buffer == NULL )
162 /* Check that all buffers are contiguous. */
163 if( prev_date != p_buffer->i_pts )
166 "buffer hole, dropping packets (%"PRId64")",
167 p_buffer->i_pts - prev_date );
169 aout_buffer_t *p_deleted;
170 while( (p_deleted = p_fifo->p_first) != p_buffer )
171 aout_BufferFree( aout_FifoPop( p_fifo ) );
174 prev_date = p_buffer->i_pts + p_buffer->i_length;
177 if( !AOUT_FMT_NON_LINEAR( &p_mixer->fmt ) )
179 p_buffer = p_fifo->p_first;
181 /* Additionally check that p_first_byte_to_mix is well located. */
182 const unsigned framesize = p_mixer->fmt.i_bytes_per_frame;
183 ssize_t delta = (start_date - p_buffer->i_pts)
184 * p_mixer->fmt.i_rate / CLOCK_FREQ;
186 msg_Warn( p_mixer, "mixer start is not output end (%zd)", delta );
189 /* Is it really the best way to do it ? */
190 aout_FifoSet( &p_aout->fifo, 0 );
191 date_Set( &exact_start_date, 0 );
196 p_buffer->i_nb_samples -= delta;
197 p_buffer->i_pts += delta * CLOCK_FREQ / p_mixer->fmt.i_rate;
198 p_buffer->i_length -= delta * CLOCK_FREQ / p_mixer->fmt.i_rate;
200 p_buffer->p_buffer += delta;
201 p_buffer->i_buffer -= delta;
204 /* Build packet with adequate number of samples */
205 unsigned needed = samples * framesize;
206 p_buffer = block_Alloc( needed );
207 if( unlikely(p_buffer == NULL) )
208 /* XXX: should free input buffers */
210 p_buffer->i_nb_samples = samples;
212 for( uint8_t *p_out = p_buffer->p_buffer; needed > 0; )
214 aout_buffer_t *p_inbuf = p_fifo->p_first;
215 if( unlikely(p_inbuf == NULL) )
217 msg_Err( p_mixer, "internal amix error" );
218 vlc_memset( p_out, 0, needed );
222 const uint8_t *p_in = p_inbuf->p_buffer;
223 size_t avail = p_inbuf->i_nb_samples * framesize;
226 vlc_memcpy( p_out, p_in, needed );
227 p_fifo->p_first->p_buffer += needed;
228 p_fifo->p_first->i_buffer -= needed;
230 p_fifo->p_first->i_nb_samples -= needed;
231 p_fifo->p_first->i_pts += needed * CLOCK_FREQ / p_mixer->fmt.i_rate;
232 p_fifo->p_first->i_length -= needed * CLOCK_FREQ / p_mixer->fmt.i_rate;
236 vlc_memcpy( p_out, p_in, avail );
240 aout_BufferFree( aout_FifoPop( p_fifo ) );
244 p_buffer = aout_FifoPop( p_fifo );
246 p_buffer->i_pts = start_date;
247 p_buffer->i_length = end_date - start_date;
250 p_mixer->mix( p_mixer, p_buffer, volume );
251 aout_OutputPlay( p_aout, p_buffer );
255 /*****************************************************************************
256 * aout_MixerRun: entry point for the mixer & post-filters processing
257 *****************************************************************************
258 * Please note that you must hold the mixer lock.
259 *****************************************************************************/
260 void aout_MixerRun( aout_instance_t * p_aout, float volume )
262 while( MixBuffer( p_aout, volume ) != -1 );