1 /*****************************************************************************
2 * output.c : internal management of output streams for the audio output
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 *****************************************************************************/
29 #include "aout_internal.h"
31 /*****************************************************************************
32 * aout_OutputNew : allocate a new output and rework the filter pipeline
33 *****************************************************************************
34 * This function is entered with the mixer lock.
35 *****************************************************************************/
36 int aout_OutputNew( aout_instance_t * p_aout,
37 audio_sample_format_t * p_format )
39 /* Retrieve user defaults. */
40 int i_rate = config_GetInt( p_aout, "aout-rate" );
41 vlc_value_t val, text;
42 /* kludge to avoid a fpu error when rate is 0... */
43 if( i_rate == 0 ) i_rate = -1;
45 memcpy( &p_aout->output.output, p_format, sizeof(audio_sample_format_t) );
47 p_aout->output.output.i_rate = i_rate;
48 aout_FormatPrepare( &p_aout->output.output );
50 vlc_mutex_lock( &p_aout->output_fifo_lock );
52 /* Find the best output plug-in. */
53 p_aout->output.p_module = module_Need( p_aout, "audio output", "$aout", 0);
54 if ( p_aout->output.p_module == NULL )
56 msg_Err( p_aout, "no suitable audio output module" );
57 vlc_mutex_unlock( &p_aout->output_fifo_lock );
61 if ( var_Type( p_aout, "audio-channels" ) ==
62 (VLC_VAR_INTEGER | VLC_VAR_HASCHOICE) )
64 /* The user may have selected a different channels configuration. */
65 var_Get( p_aout, "audio-channels", &val );
67 if ( val.i_int == AOUT_VAR_CHAN_RSTEREO )
69 p_aout->output.output.i_original_channels |=
70 AOUT_CHAN_REVERSESTEREO;
72 else if ( val.i_int == AOUT_VAR_CHAN_STEREO )
74 p_aout->output.output.i_original_channels =
75 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
77 else if ( val.i_int == AOUT_VAR_CHAN_LEFT )
79 p_aout->output.output.i_original_channels = AOUT_CHAN_LEFT;
81 else if ( val.i_int == AOUT_VAR_CHAN_RIGHT )
83 p_aout->output.output.i_original_channels = AOUT_CHAN_RIGHT;
85 else if ( val.i_int == AOUT_VAR_CHAN_DOLBYS )
87 p_aout->output.output.i_original_channels
88 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_DOLBYSTEREO;
91 else if ( p_aout->output.output.i_physical_channels == AOUT_CHAN_CENTER
92 && (p_aout->output.output.i_original_channels
93 & AOUT_CHAN_PHYSMASK) == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) )
95 /* Mono - create the audio-channels variable. */
96 var_Create( p_aout, "audio-channels",
97 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
98 text.psz_string = _("Audio Channels");
99 var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );
101 val.i_int = AOUT_VAR_CHAN_STEREO; text.psz_string = _("Stereo");
102 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
103 val.i_int = AOUT_VAR_CHAN_LEFT; text.psz_string = _("Left");
104 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
105 val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
106 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
107 if ( p_aout->output.output.i_original_channels & AOUT_CHAN_DUALMONO )
109 /* Go directly to the left channel. */
110 p_aout->output.output.i_original_channels = AOUT_CHAN_LEFT;
111 val.i_int = AOUT_VAR_CHAN_LEFT;
112 var_Set( p_aout, "audio-channels", val );
114 var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
117 else if ( p_aout->output.output.i_physical_channels ==
118 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)
119 && (p_aout->output.output.i_original_channels &
120 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
122 /* Stereo - create the audio-channels variable. */
123 var_Create( p_aout, "audio-channels",
124 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
125 text.psz_string = _("Audio Channels");
126 var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );
128 if ( p_aout->output.output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
130 val.i_int = AOUT_VAR_CHAN_DOLBYS;
131 text.psz_string = _("Dolby Surround");
135 val.i_int = AOUT_VAR_CHAN_STEREO;
136 text.psz_string = _("Stereo");
138 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
139 val.i_int = AOUT_VAR_CHAN_LEFT; text.psz_string = _("Left");
140 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
141 val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
142 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
143 val.i_int = AOUT_VAR_CHAN_RSTEREO; text.psz_string=_("Reverse stereo");
144 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
145 if ( p_aout->output.output.i_original_channels & AOUT_CHAN_DUALMONO )
147 /* Go directly to the left channel. */
148 p_aout->output.output.i_original_channels = AOUT_CHAN_LEFT;
149 val.i_int = AOUT_VAR_CHAN_LEFT;
150 var_Set( p_aout, "audio-channels", val );
152 var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
155 val.b_bool = VLC_TRUE;
156 var_Set( p_aout, "intf-change", val );
158 aout_FormatPrepare( &p_aout->output.output );
161 aout_FifoInit( p_aout, &p_aout->output.fifo,
162 p_aout->output.output.i_rate );
164 vlc_mutex_unlock( &p_aout->output_fifo_lock );
166 aout_FormatPrint( p_aout, "output", &p_aout->output.output );
168 /* Calculate the resulting mixer output format. */
169 memcpy( &p_aout->mixer.mixer, &p_aout->output.output,
170 sizeof(audio_sample_format_t) );
171 if ( !AOUT_FMT_NON_LINEAR(&p_aout->output.output) )
173 /* Non-S/PDIF mixer only deals with float32 or fixed32. */
174 p_aout->mixer.mixer.i_format
175 = (vlc_CPU() & CPU_CAPABILITY_FPU) ?
176 VLC_FOURCC('f','l','3','2') :
177 VLC_FOURCC('f','i','3','2');
178 aout_FormatPrepare( &p_aout->mixer.mixer );
182 p_aout->mixer.mixer.i_format = p_format->i_format;
185 aout_FormatPrint( p_aout, "mixer", &p_aout->mixer.mixer );
187 /* Create filters. */
188 p_aout->output.i_nb_filters = 0;
189 if ( aout_FiltersCreatePipeline( p_aout, p_aout->output.pp_filters,
190 &p_aout->output.i_nb_filters,
191 &p_aout->mixer.mixer,
192 &p_aout->output.output ) < 0 )
194 msg_Err( p_aout, "couldn't create audio output pipeline" );
195 module_Unneed( p_aout, p_aout->output.p_module );
199 /* Prepare hints for the buffer allocator. */
200 p_aout->mixer.output_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
201 p_aout->mixer.output_alloc.i_bytes_per_sec
202 = p_aout->mixer.mixer.i_bytes_per_frame
203 * p_aout->mixer.mixer.i_rate
204 / p_aout->mixer.mixer.i_frame_length;
206 aout_FiltersHintBuffers( p_aout, p_aout->output.pp_filters,
207 p_aout->output.i_nb_filters,
208 &p_aout->mixer.output_alloc );
210 p_aout->output.b_error = 0;
214 /*****************************************************************************
215 * aout_OutputDelete : delete the output
216 *****************************************************************************
217 * This function is entered with the mixer lock.
218 *****************************************************************************/
219 void aout_OutputDelete( aout_instance_t * p_aout )
221 if ( p_aout->output.b_error )
226 module_Unneed( p_aout, p_aout->output.p_module );
228 aout_FiltersDestroyPipeline( p_aout, p_aout->output.pp_filters,
229 p_aout->output.i_nb_filters );
231 vlc_mutex_lock( &p_aout->output_fifo_lock );
232 aout_FifoDestroy( p_aout, &p_aout->output.fifo );
233 vlc_mutex_unlock( &p_aout->output_fifo_lock );
235 p_aout->output.b_error = VLC_TRUE;
238 /*****************************************************************************
239 * aout_OutputPlay : play a buffer
240 *****************************************************************************
241 * This function is entered with the mixer lock.
242 *****************************************************************************/
243 void aout_OutputPlay( aout_instance_t * p_aout, aout_buffer_t * p_buffer )
245 aout_FiltersPlay( p_aout, p_aout->output.pp_filters,
246 p_aout->output.i_nb_filters,
249 if( p_buffer->i_nb_bytes == 0 )
251 aout_BufferFree( p_buffer );
255 vlc_mutex_lock( &p_aout->output_fifo_lock );
256 aout_FifoPush( p_aout, &p_aout->output.fifo, p_buffer );
257 p_aout->output.pf_play( p_aout );
258 vlc_mutex_unlock( &p_aout->output_fifo_lock );
261 /*****************************************************************************
262 * aout_OutputNextBuffer : give the audio output plug-in the right buffer
263 *****************************************************************************
264 * If b_can_sleek is 1, the aout core functions won't try to resample
265 * new buffers to catch up - that is we suppose that the output plug-in can
266 * compensate it by itself. S/PDIF outputs should always set b_can_sleek = 1.
267 * This function is entered with no lock at all :-).
268 *****************************************************************************/
269 aout_buffer_t * aout_OutputNextBuffer( aout_instance_t * p_aout,
271 vlc_bool_t b_can_sleek )
273 aout_buffer_t * p_buffer;
275 vlc_mutex_lock( &p_aout->output_fifo_lock );
277 p_buffer = p_aout->output.fifo.p_first;
279 /* Drop the audio sample if the audio output is really late.
280 * In the case of b_can_sleek, we don't use a resampler so we need to be
281 * a lot more severe. */
282 while ( p_buffer && p_buffer->start_date <
283 (b_can_sleek ? start_date : mdate()) - AOUT_PTS_TOLERANCE )
285 msg_Dbg( p_aout, "audio output is too slow ("I64Fd"), "
286 "trashing "I64Fd"us", mdate() - p_buffer->start_date,
287 p_buffer->end_date - p_buffer->start_date );
288 p_buffer = p_buffer->p_next;
289 aout_BufferFree( p_aout->output.fifo.p_first );
290 p_aout->output.fifo.p_first = p_buffer;
293 if ( p_buffer == NULL )
295 p_aout->output.fifo.pp_last = &p_aout->output.fifo.p_first;
297 #if 0 /* This is bad because the audio output might just be trying to fill
298 * in its internal buffers. And anyway, it's up to the audio output
299 * to deal with this kind of starvation. */
301 /* Set date to 0, to allow the mixer to send a new buffer ASAP */
302 aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
303 if ( !p_aout->output.b_starving )
305 "audio output is starving (no input), playing silence" );
306 p_aout->output.b_starving = 1;
309 vlc_mutex_unlock( &p_aout->output_fifo_lock );
313 /* Here we suppose that all buffers have the same duration - this is
314 * generally true, and anyway if it's wrong it won't be a disaster.
316 if ( p_buffer->start_date > start_date
317 + (p_buffer->end_date - p_buffer->start_date) )
319 * + AOUT_PTS_TOLERANCE )
320 * There is no reason to want that, it just worsen the scheduling of
321 * an audio sample after an output starvation (ie. on start or on resume)
325 const mtime_t i_delta = p_buffer->start_date - start_date;
326 vlc_mutex_unlock( &p_aout->output_fifo_lock );
328 if ( !p_aout->output.b_starving )
329 msg_Dbg( p_aout, "audio output is starving ("I64Fd"), "
330 "playing silence", i_delta );
331 p_aout->output.b_starving = 1;
335 p_aout->output.b_starving = 0;
338 ( (p_buffer->start_date - start_date > AOUT_PTS_TOLERANCE)
339 || (start_date - p_buffer->start_date > AOUT_PTS_TOLERANCE) ) )
341 /* Try to compensate the drift by doing some resampling. */
343 mtime_t difference = start_date - p_buffer->start_date;
344 msg_Warn( p_aout, "output date isn't PTS date, requesting "
345 "resampling ("I64Fd")", difference );
347 vlc_mutex_lock( &p_aout->input_fifos_lock );
348 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
350 aout_fifo_t * p_fifo = &p_aout->pp_inputs[i]->fifo;
352 aout_FifoMoveDates( p_aout, p_fifo, difference );
355 aout_FifoMoveDates( p_aout, &p_aout->output.fifo, difference );
356 vlc_mutex_unlock( &p_aout->input_fifos_lock );
359 p_aout->output.fifo.p_first = p_buffer->p_next;
360 if ( p_buffer->p_next == NULL )
362 p_aout->output.fifo.pp_last = &p_aout->output.fifo.p_first;
365 vlc_mutex_unlock( &p_aout->output_fifo_lock );