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 );
256 * Flushes or drains the audio output buffers.
257 * This enables the output to expedite seek and stop.
258 * @param wait if true, wait for buffer playback (i.e. drain),
259 * if false, discard the buffers immediately (i.e. flush)
261 void aout_OutputFlush( audio_output_t *aout, bool wait )
263 vlc_assert_locked( &aout->lock );
265 if( aout->pf_flush != NULL )
266 aout->pf_flush( aout, wait );
270 /*** Volume handling ***/
273 * Dummy volume setter. This is the default volume setter.
275 static int aout_VolumeNoneSet (audio_output_t *aout, float volume, bool mute)
277 (void)aout; (void)volume; (void)mute;
282 * Configures the dummy volume setter.
283 * @note Audio output plugins for which volume is irrelevant
284 * should call this function during activation.
286 void aout_VolumeNoneInit (audio_output_t *aout)
288 /* aout_New() -safely- calls this function without the lock, before any
289 * other thread knows of this audio output instance.
290 vlc_assert_locked (&aout->lock); */
291 aout->pf_volume_set = aout_VolumeNoneSet;
295 * Volume setter for software volume.
297 static int aout_VolumeSoftSet (audio_output_t *aout, float volume, bool mute)
299 vlc_assert_locked (&aout->lock);
301 /* Cubic mapping from software volume to amplification factor.
302 * This provides a good tradeoff between low and high volume ranges.
304 * This code is only used for the VLC software mixer. If you change this
305 * formula, be sure to update the aout_VolumeHardInit()-based plugins also.
308 volume = volume * volume * volume;
312 aout->mixer_multiplier = volume;
317 * Configures the volume setter for software mixing
318 * and apply the default volume.
319 * @note Audio output plugins that cannot apply the volume
320 * should call this function during activation.
322 void aout_VolumeSoftInit (audio_output_t *aout)
324 audio_volume_t volume = var_InheritInteger (aout, "volume");
325 bool mute = var_InheritBool (aout, "mute");
327 vlc_assert_locked (&aout->lock);
328 aout->pf_volume_set = aout_VolumeSoftSet;
329 aout_VolumeSoftSet (aout, volume / (float)AOUT_VOLUME_DEFAULT, mute);
333 * Configures a custom volume setter. This is used by audio outputs that can
334 * control the hardware volume directly and/or emulate it internally.
335 * @param setter volume setter callback
337 void aout_VolumeHardInit (audio_output_t *aout, aout_volume_cb setter)
339 vlc_assert_locked (&aout->lock);
340 aout->pf_volume_set = setter;
344 * Supply or update the current custom ("hardware") volume.
345 * @note This only makes sense after calling aout_VolumeHardInit().
346 * @param setter volume setter callback
347 * @param volume current custom volume
348 * @param mute current mute flag
349 * @note Audio output plugins that cannot apply the volume
350 * should call this function during activation.
352 void aout_VolumeHardSet (audio_output_t *aout, float volume, bool mute)
355 /* REVISIT: This is tricky. We cannot acquire the volume lock as this gets
356 * called from the audio output (it would cause a lock inversion).
357 * We also should not override the input manager volume, but only the
358 * volume of the current audio output... FIXME */
359 msg_Err (aout, "%s(%f, %u)", __func__, volume, (unsigned)mute);
363 /*** Buffer management ***/
365 /*****************************************************************************
366 * aout_OutputNextBuffer : give the audio output plug-in the right buffer
367 *****************************************************************************
368 * If b_can_sleek is 1, the aout core functions won't try to resample
369 * new buffers to catch up - that is we suppose that the output plug-in can
370 * compensate it by itself. S/PDIF outputs should always set b_can_sleek = 1.
371 * This function is entered with no lock at all :-).
372 *****************************************************************************/
373 aout_buffer_t * aout_OutputNextBuffer( audio_output_t * p_aout,
377 aout_fifo_t *p_fifo = &p_aout->fifo;
378 aout_buffer_t * p_buffer;
379 mtime_t now = mdate();
383 /* Drop the audio sample if the audio output is really late.
384 * In the case of b_can_sleek, we don't use a resampler so we need to be
385 * a lot more severe. */
386 while( ((p_buffer = p_fifo->p_first) != NULL)
387 && p_buffer->i_pts < (b_can_sleek ? start_date : now) - AOUT_MAX_PTS_DELAY )
389 msg_Dbg( p_aout, "audio output is too slow (%"PRId64"), "
390 "trashing %"PRId64"us", now - p_buffer->i_pts,
391 p_buffer->i_length );
392 aout_BufferFree( aout_FifoPop( p_fifo ) );
395 if( p_buffer == NULL )
397 #if 0 /* This is bad because the audio output might just be trying to fill
398 * in its internal buffers. And anyway, it's up to the audio output
399 * to deal with this kind of starvation. */
401 /* Set date to 0, to allow the mixer to send a new buffer ASAP */
402 aout_FifoReset( &p_aout->fifo );
403 if ( !p_aout->b_starving )
405 "audio output is starving (no input), playing silence" );
406 p_aout->b_starving = true;
411 mtime_t delta = start_date - p_buffer->i_pts;
412 /* Here we suppose that all buffers have the same duration - this is
413 * generally true, and anyway if it's wrong it won't be a disaster.
415 if ( 0 > delta + p_buffer->i_length )
417 if ( !p_aout->b_starving )
418 msg_Dbg( p_aout, "audio output is starving (%"PRId64"), "
419 "playing silence", -delta );
420 p_aout->b_starving = true;
425 p_aout->b_starving = false;
426 p_buffer = aout_FifoPop( p_fifo );
429 && ( delta > AOUT_MAX_PTS_DELAY || delta < -AOUT_MAX_PTS_ADVANCE ) )
431 /* Try to compensate the drift by doing some resampling. */
432 msg_Warn( p_aout, "output date isn't PTS date, requesting "
433 "resampling (%"PRId64")", delta );
435 aout_FifoMoveDates( &p_aout->p_input->fifo, delta );
436 aout_FifoMoveDates( p_fifo, delta );
439 aout_unlock( p_aout );