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 *****************************************************************************/
31 #include <vlc_common.h>
33 #include <vlc_aout_intf.h>
35 #include <vlc_modules.h>
38 #include "aout_internal.h"
40 /*****************************************************************************
41 * aout_OutputNew : allocate a new output and rework the filter pipeline
42 *****************************************************************************
43 * This function is entered with the mixer lock.
44 *****************************************************************************/
45 int aout_OutputNew( audio_output_t * p_aout,
46 const audio_sample_format_t * p_format )
48 vlc_assert_locked( &p_aout->lock );
49 p_aout->format = *p_format;
51 /* Retrieve user defaults. */
52 int i_rate = var_InheritInteger( p_aout, "aout-rate" );
54 p_aout->format.i_rate = i_rate;
55 aout_FormatPrepare( &p_aout->format );
57 /* Find the best output plug-in. */
58 p_aout->module = module_need( p_aout, "audio output", "$aout", false );
59 if ( p_aout->module == NULL )
61 msg_Err( p_aout, "no suitable audio output module" );
65 if ( var_Type( p_aout, "audio-channels" ) ==
66 (VLC_VAR_INTEGER | VLC_VAR_HASCHOICE) )
68 /* The user may have selected a different channels configuration. */
69 switch( var_InheritInteger( p_aout, "audio-channels" ) )
71 case AOUT_VAR_CHAN_RSTEREO:
72 p_aout->format.i_original_channels |= AOUT_CHAN_REVERSESTEREO;
74 case AOUT_VAR_CHAN_STEREO:
75 p_aout->format.i_original_channels =
76 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
78 case AOUT_VAR_CHAN_LEFT:
79 p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
81 case AOUT_VAR_CHAN_RIGHT:
82 p_aout->format.i_original_channels = AOUT_CHAN_RIGHT;
84 case AOUT_VAR_CHAN_DOLBYS:
85 p_aout->format.i_original_channels =
86 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_DOLBYSTEREO;
90 else if ( p_aout->format.i_physical_channels == AOUT_CHAN_CENTER
91 && (p_aout->format.i_original_channels
92 & AOUT_CHAN_PHYSMASK) == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) )
94 vlc_value_t val, text;
96 /* Mono - create the audio-channels variable. */
97 var_Create( p_aout, "audio-channels",
98 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
99 text.psz_string = _("Audio Channels");
100 var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );
102 val.i_int = AOUT_VAR_CHAN_STEREO; text.psz_string = _("Stereo");
103 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
104 val.i_int = AOUT_VAR_CHAN_LEFT; text.psz_string = _("Left");
105 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
106 val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
107 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
108 if ( p_aout->format.i_original_channels & AOUT_CHAN_DUALMONO )
110 /* Go directly to the left channel. */
111 p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
112 var_SetInteger( p_aout, "audio-channels", AOUT_VAR_CHAN_LEFT );
114 var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
117 else if ( p_aout->format.i_physical_channels ==
118 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)
119 && (p_aout->format.i_original_channels &
120 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
122 vlc_value_t val, text;
124 /* Stereo - create the audio-channels variable. */
125 var_Create( p_aout, "audio-channels",
126 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
127 text.psz_string = _("Audio Channels");
128 var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );
130 if ( p_aout->format.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
132 val.i_int = AOUT_VAR_CHAN_DOLBYS;
133 text.psz_string = _("Dolby Surround");
137 val.i_int = AOUT_VAR_CHAN_STEREO;
138 text.psz_string = _("Stereo");
140 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
141 val.i_int = AOUT_VAR_CHAN_LEFT; text.psz_string = _("Left");
142 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
143 val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
144 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
145 val.i_int = AOUT_VAR_CHAN_RSTEREO; text.psz_string=_("Reverse stereo");
146 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
147 if ( p_aout->format.i_original_channels & AOUT_CHAN_DUALMONO )
149 /* Go directly to the left channel. */
150 p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
151 var_SetInteger( p_aout, "audio-channels", AOUT_VAR_CHAN_LEFT );
153 var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
156 var_TriggerCallback( p_aout, "intf-change" );
158 aout_FormatPrepare( &p_aout->format );
161 aout_FifoInit( p_aout, &p_aout->fifo, p_aout->format.i_rate );
162 aout_FormatPrint( p_aout, "output", &p_aout->format );
164 /* Choose the mixer format. */
165 p_aout->mixer_format = p_aout->format;
166 if ( AOUT_FMT_NON_LINEAR(&p_aout->format) )
167 p_aout->mixer_format.i_format = p_format->i_format;
169 /* Most audio filters can only deal with single-precision,
170 * so lets always use that when hardware supports floating point. */
172 p_aout->mixer_format.i_format = VLC_CODEC_FL32;
174 /* Otherwise, audio filters will not work. Use fixed-point if the input has
175 * more than 16-bits depth. */
176 if( p_format->i_bitspersample > 16 )
177 p_aout->mixer_format.i_format = VLC_CODEC_FI32;
179 /* Fallback to 16-bits. This avoids pointless conversion to and from
180 * 32-bits samples for the sole purpose of software mixing. */
181 p_aout->mixer_format.i_format = VLC_CODEC_S16N;
183 aout_FormatPrepare( &p_aout->mixer_format );
184 aout_FormatPrint( p_aout, "mixer", &p_aout->mixer_format );
186 /* Create filters. */
187 p_aout->i_nb_filters = 0;
188 if ( aout_FiltersCreatePipeline( p_aout, p_aout->pp_filters,
189 &p_aout->i_nb_filters,
190 &p_aout->mixer_format,
191 &p_aout->format ) < 0 )
193 msg_Err( p_aout, "couldn't create audio output pipeline" );
194 module_unneed( p_aout, p_aout->module );
195 p_aout->module = NULL;
201 /*****************************************************************************
202 * aout_OutputDelete : delete the output
203 *****************************************************************************
204 * This function is entered with the mixer lock.
205 *****************************************************************************/
206 void aout_OutputDelete( audio_output_t * p_aout )
208 vlc_assert_locked( &p_aout->lock );
210 if( p_aout->module == NULL )
213 module_unneed( p_aout, p_aout->module );
214 aout_VolumeNoneInit( p_aout ); /* clear volume callback */
215 p_aout->module = NULL;
216 aout_FiltersDestroyPipeline( p_aout->pp_filters, p_aout->i_nb_filters );
217 aout_FifoDestroy( &p_aout->fifo );
220 /*****************************************************************************
221 * aout_OutputPlay : play a buffer
222 *****************************************************************************
223 * This function is entered with the mixer lock.
224 *****************************************************************************/
225 void aout_OutputPlay( audio_output_t * p_aout, aout_buffer_t * p_buffer )
227 vlc_assert_locked( &p_aout->lock );
229 aout_FiltersPlay( p_aout->pp_filters, p_aout->i_nb_filters, &p_buffer );
232 if( p_buffer->i_buffer == 0 )
234 block_Release( p_buffer );
238 aout_FifoPush( &p_aout->fifo, p_buffer );
239 p_aout->pf_play( p_aout );
243 * Notifies the audio output (if any) of pause/resume events.
244 * This enables the output to expedite pause, instead of waiting for its
247 void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
249 vlc_assert_locked( &aout->lock );
251 if( aout->pf_pause != NULL )
252 aout->pf_pause( aout, pause, date );
255 mtime_t duration = date - aout->p_input->i_pause_date;
256 /* XXX: ^ onk onk! gruik! ^ */
257 aout_FifoMoveDates( &aout->fifo, duration );
262 * Flushes or drains the audio output buffers.
263 * This enables the output to expedite seek and stop.
264 * @param wait if true, wait for buffer playback (i.e. drain),
265 * if false, discard the buffers immediately (i.e. flush)
267 void aout_OutputFlush( audio_output_t *aout, bool wait )
269 vlc_assert_locked( &aout->lock );
271 if( aout->pf_flush != NULL )
272 aout->pf_flush( aout, wait );
273 aout_FifoReset( &aout->fifo );
277 /*** Volume handling ***/
280 * Dummy volume setter. This is the default volume setter.
282 static int aout_VolumeNoneSet (audio_output_t *aout, float volume, bool mute)
284 (void)aout; (void)volume; (void)mute;
289 * Configures the dummy volume setter.
290 * @note Audio output plugins for which volume is irrelevant
291 * should call this function during activation.
293 void aout_VolumeNoneInit (audio_output_t *aout)
295 /* aout_New() -safely- calls this function without the lock, before any
296 * other thread knows of this audio output instance.
297 vlc_assert_locked (&aout->lock); */
298 aout->pf_volume_set = aout_VolumeNoneSet;
302 * Volume setter for software volume.
304 static int aout_VolumeSoftSet (audio_output_t *aout, float volume, bool mute)
306 vlc_assert_locked (&aout->lock);
308 /* Cubic mapping from software volume to amplification factor.
309 * This provides a good tradeoff between low and high volume ranges.
311 * This code is only used for the VLC software mixer. If you change this
312 * formula, be sure to update the aout_VolumeHardInit()-based plugins also.
315 volume = volume * volume * volume;
319 aout->mixer_multiplier = volume;
324 * Configures the volume setter for software mixing
325 * and apply the default volume.
326 * @note Audio output plugins that cannot apply the volume
327 * should call this function during activation.
329 void aout_VolumeSoftInit (audio_output_t *aout)
331 audio_volume_t volume = var_InheritInteger (aout, "volume");
332 bool mute = var_InheritBool (aout, "mute");
334 vlc_assert_locked (&aout->lock);
335 aout->pf_volume_set = aout_VolumeSoftSet;
336 aout_VolumeSoftSet (aout, volume / (float)AOUT_VOLUME_DEFAULT, mute);
340 * Configures a custom volume setter. This is used by audio outputs that can
341 * control the hardware volume directly and/or emulate it internally.
342 * @param setter volume setter callback
344 void aout_VolumeHardInit (audio_output_t *aout, aout_volume_cb setter)
346 vlc_assert_locked (&aout->lock);
347 aout->pf_volume_set = setter;
351 * Supply or update the current custom ("hardware") volume.
352 * @note This only makes sense after calling aout_VolumeHardInit().
353 * @param setter volume setter callback
354 * @param volume current custom volume
355 * @param mute current mute flag
356 * @note Audio output plugins that cannot apply the volume
357 * should call this function during activation.
359 void aout_VolumeHardSet (audio_output_t *aout, float volume, bool mute)
362 /* REVISIT: This is tricky. We cannot acquire the volume lock as this gets
363 * called from the audio output (it would cause a lock inversion).
364 * We also should not override the input manager volume, but only the
365 * volume of the current audio output... FIXME */
366 msg_Err (aout, "%s(%f, %u)", __func__, volume, (unsigned)mute);
370 /*** Buffer management ***/
372 /*****************************************************************************
373 * aout_OutputNextBuffer : give the audio output plug-in the right buffer
374 *****************************************************************************
375 * If b_can_sleek is 1, the aout core functions won't try to resample
376 * new buffers to catch up - that is we suppose that the output plug-in can
377 * compensate it by itself. S/PDIF outputs should always set b_can_sleek = 1.
378 * This function is entered with no lock at all :-).
379 *****************************************************************************/
380 aout_buffer_t * aout_OutputNextBuffer( audio_output_t * p_aout,
384 aout_fifo_t *p_fifo = &p_aout->fifo;
385 aout_buffer_t * p_buffer;
386 mtime_t now = mdate();
390 /* Drop the audio sample if the audio output is really late.
391 * In the case of b_can_sleek, we don't use a resampler so we need to be
392 * a lot more severe. */
393 while( ((p_buffer = p_fifo->p_first) != NULL)
394 && p_buffer->i_pts < (b_can_sleek ? start_date : now) - AOUT_MAX_PTS_DELAY )
396 msg_Dbg( p_aout, "audio output is too slow (%"PRId64"), "
397 "trashing %"PRId64"us", now - p_buffer->i_pts,
398 p_buffer->i_length );
399 aout_BufferFree( aout_FifoPop( p_fifo ) );
402 if( p_buffer == NULL )
404 #if 0 /* This is bad because the audio output might just be trying to fill
405 * in its internal buffers. And anyway, it's up to the audio output
406 * to deal with this kind of starvation. */
408 /* Set date to 0, to allow the mixer to send a new buffer ASAP */
409 aout_FifoReset( &p_aout->fifo );
410 if ( !p_aout->b_starving )
412 "audio output is starving (no input), playing silence" );
413 p_aout->b_starving = true;
418 mtime_t delta = start_date - p_buffer->i_pts;
419 /* Here we suppose that all buffers have the same duration - this is
420 * generally true, and anyway if it's wrong it won't be a disaster.
422 if ( 0 > delta + p_buffer->i_length )
424 if ( !p_aout->b_starving )
425 msg_Dbg( p_aout, "audio output is starving (%"PRId64"), "
426 "playing silence", -delta );
427 p_aout->b_starving = true;
432 p_aout->b_starving = false;
433 p_buffer = aout_FifoPop( p_fifo );
436 && ( delta > AOUT_MAX_PTS_DELAY || delta < -AOUT_MAX_PTS_ADVANCE ) )
438 /* Try to compensate the drift by doing some resampling. */
439 msg_Warn( p_aout, "output date isn't PTS date, requesting "
440 "resampling (%"PRId64")", delta );
442 aout_FifoMoveDates( &p_aout->p_input->fifo, delta );
443 aout_FifoMoveDates( p_fifo, delta );
446 aout_unlock( p_aout );