1 /*****************************************************************************
2 * output.c : internal management of output streams for the audio output
3 *****************************************************************************
4 * Copyright (C) 2002-2004 VLC authors and VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc_common.h>
30 #include <vlc_modules.h>
34 #include "aout_internal.h"
37 static void aout_Destructor( vlc_object_t * p_this );
39 static int var_Copy (vlc_object_t *src, const char *name, vlc_value_t prev,
40 vlc_value_t value, void *data)
42 vlc_object_t *dst = data;
44 (void) src; (void) prev;
45 return var_Set (dst, name, value);
49 * Supply or update the current custom ("hardware") volume.
50 * @note This only makes sense after calling aout_VolumeHardInit().
51 * @param volume current custom volume
53 * @warning The caller (i.e. the audio output plug-in) is responsible for
54 * interlocking and synchronizing call to this function and to the
55 * audio_output_t.volume_set callback. This ensures that VLC gets correct
56 * volume information (possibly with a latency).
58 static void aout_VolumeNotify (audio_output_t *aout, float volume)
60 var_SetFloat (aout, "volume", volume);
63 static void aout_MuteNotify (audio_output_t *aout, bool mute)
65 var_SetBool (aout, "mute", mute);
68 static void aout_PolicyNotify (audio_output_t *aout, bool cork)
70 (cork ? var_IncInteger : var_DecInteger) (aout->p_parent, "corks");
73 static int aout_GainNotify (audio_output_t *aout, float gain)
75 aout_owner_t *owner = aout_owner (aout);
77 aout_assert_locked (aout);
78 aout_volume_SetVolume (owner->volume, gain);
79 /* XXX: ideally, return -1 if format cannot be amplified */
85 * Creates an audio output object and initializes an output module.
87 audio_output_t *aout_New (vlc_object_t *parent)
89 audio_output_t *aout = vlc_custom_create (parent, sizeof (aout_instance_t),
91 if (unlikely(aout == NULL))
94 aout_owner_t *owner = aout_owner (aout);
96 vlc_mutex_init (&owner->lock);
97 vlc_object_set_destructor (aout, aout_Destructor);
99 /* Audio output module callbacks */
100 var_Create (aout, "volume", VLC_VAR_FLOAT);
101 var_AddCallback (aout, "volume", var_Copy, parent);
102 var_Create (aout, "mute", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
103 var_AddCallback (aout, "mute", var_Copy, parent);
105 aout->event.volume_report = aout_VolumeNotify;
106 aout->event.mute_report = aout_MuteNotify;
107 aout->event.policy_report = aout_PolicyNotify;
108 aout->event.gain_request = aout_GainNotify;
110 /* Audio output module initialization */
113 aout->volume_set = NULL;
114 aout->mute_set = NULL;
115 owner->module = module_need (aout, "audio output", "$aout", false);
116 if (owner->module == NULL)
118 msg_Err (aout, "no suitable audio output module");
119 vlc_object_release (aout);
124 * Persistent audio output variables
126 vlc_value_t val, text;
127 module_config_t *cfg;
131 var_Create (aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
132 text.psz_string = _("Visualizations");
133 var_Change (aout, "visual", VLC_VAR_SETTEXT, &text, NULL);
134 val.psz_string = (char *)"";
135 text.psz_string = _("Disable");
136 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
137 val.psz_string = (char *)"spectrometer";
138 text.psz_string = _("Spectrometer");
139 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
140 val.psz_string = (char *)"scope";
141 text.psz_string = _("Scope");
142 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
143 val.psz_string = (char *)"spectrum";
144 text.psz_string = _("Spectrum");
145 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
146 val.psz_string = (char *)"vuMeter";
147 text.psz_string = _("Vu meter");
148 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
149 /* Look for goom plugin */
150 if (module_exists ("goom"))
152 val.psz_string = (char *)"goom";
153 text.psz_string = (char *)"Goom";
154 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
156 /* Look for libprojectM plugin */
157 if (module_exists ("projectm"))
159 val.psz_string = (char *)"projectm";
160 text.psz_string = (char*)"projectM";
161 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
163 /* Look for VSXu plugin */
164 if (module_exists ("vsxu"))
166 val.psz_string = (char *)"vsxu";
167 text.psz_string = (char*)"Vovoid VSXu";
168 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
170 str = var_GetNonEmptyString (aout, "effect-list");
173 var_SetString (aout, "visual", str);
178 var_Create (aout, "equalizer", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
179 text.psz_string = _("Equalizer");
180 var_Change (aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL);
181 val.psz_string = (char*)"";
182 text.psz_string = _("Disable");
183 var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
184 cfg = config_FindConfig (VLC_OBJECT(aout), "equalizer-preset");
185 if (likely(cfg != NULL))
186 for (unsigned i = 0; i < cfg->list_count; i++)
188 val.psz_string = cfg->list.psz[i];
189 text.psz_string = vlc_gettext(cfg->list_text[i]);
190 var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
193 var_Create (aout, "audio-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
194 text.psz_string = _("Audio filters");
195 var_Change (aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL);
198 var_Create (aout, "audio-visual", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
199 text.psz_string = _("Audio visualizations");
200 var_Change (aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL);
203 var_Create (aout, "audio-replay-gain-mode",
204 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
205 text.psz_string = _("Replay gain");
206 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL);
207 cfg = config_FindConfig (VLC_OBJECT(aout), "audio-replay-gain-mode");
208 if (likely(cfg != NULL))
209 for (unsigned i = 0; i < cfg->list_count; i++)
211 val.psz_string = cfg->list.psz[i];
212 text.psz_string = vlc_gettext(cfg->list_text[i]);
213 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
221 * Deinitializes an audio output module and destroys an audio output object.
223 void aout_Destroy (audio_output_t *aout)
225 aout_owner_t *owner = aout_owner (aout);
228 module_unneed (aout, owner->module);
229 /* Protect against late call from intf.c */
230 aout->volume_set = NULL;
231 aout->mute_set = NULL;
234 var_DelCallback (aout, "mute", var_Copy, aout->p_parent);
235 var_SetFloat (aout, "volume", -1.f);
236 var_DelCallback (aout, "volume", var_Copy, aout->p_parent);
237 vlc_object_release (aout);
241 * Destroys the audio output lock used (asynchronously) by interface functions.
243 static void aout_Destructor (vlc_object_t *obj)
245 audio_output_t *aout = (audio_output_t *)obj;
246 aout_owner_t *owner = aout_owner (aout);
248 vlc_mutex_destroy (&owner->lock);
252 * Gets the volume of the audio output stream (independent of mute).
253 * \return Current audio volume (0. = silent, 1. = nominal),
254 * or a strictly negative value if undefined.
256 float aout_VolumeGet (audio_output_t *aout)
258 return var_GetFloat (aout, "volume");
262 * Sets the volume of the audio output stream.
263 * \note The mute status is not changed.
264 * \return 0 on success, -1 on failure.
266 int aout_VolumeSet (audio_output_t *aout, float vol)
271 if (aout->volume_set != NULL)
272 ret = aout->volume_set (aout, vol);
278 * Gets the audio output stream mute flag.
279 * \return 0 if not muted, 1 if muted, -1 if undefined.
281 int aout_MuteGet (audio_output_t *aout)
283 return var_InheritBool (aout, "mute");
287 * Sets the audio output stream mute flag.
288 * \return 0 on success, -1 on failure.
290 int aout_MuteSet (audio_output_t *aout, bool mute)
295 if (aout->mute_set != NULL)
296 ret = aout->mute_set (aout, mute);
302 * Starts an audio output stream.
303 * \param fmtp audio output stream format [IN/OUT]
304 * \warning The caller must hold the audio output lock.
306 int aout_OutputNew (audio_output_t *aout, const audio_sample_format_t *fmtp)
308 aout_owner_t *owner = aout_owner (aout);
310 audio_sample_format_t fmt = *fmtp;
311 aout_FormatPrepare (&fmt);
313 aout_assert_locked (aout);
315 if (aout->start (aout, &fmt))
317 msg_Err (aout, "module not functional");
321 if (!var_Type (aout, "stereo-mode"))
322 var_Create (aout, "stereo-mode",
323 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT);
325 /* The user may have selected a different channels configuration. */
326 var_AddCallback (aout, "stereo-mode", aout_ChannelsRestart, NULL);
327 switch (var_GetInteger (aout, "stereo-mode"))
329 case AOUT_VAR_CHAN_RSTEREO:
330 fmt.i_original_channels |= AOUT_CHAN_REVERSESTEREO;
332 case AOUT_VAR_CHAN_STEREO:
333 fmt.i_original_channels = AOUT_CHANS_STEREO;
335 case AOUT_VAR_CHAN_LEFT:
336 fmt.i_original_channels = AOUT_CHAN_LEFT;
338 case AOUT_VAR_CHAN_RIGHT:
339 fmt.i_original_channels = AOUT_CHAN_RIGHT;
341 case AOUT_VAR_CHAN_DOLBYS:
342 fmt.i_original_channels = AOUT_CHANS_STEREO|AOUT_CHAN_DOLBYSTEREO;
346 if ((fmt.i_original_channels & AOUT_CHAN_PHYSMASK)
347 != AOUT_CHANS_STEREO)
350 vlc_value_t val, txt;
352 var_Change (aout, "stereo-mode", VLC_VAR_DELCHOICE, &val, NULL);
353 txt.psz_string = _("Stereo audio mode");
354 var_Change (aout, "stereo-mode", VLC_VAR_SETTEXT, &txt, NULL);
355 if (fmt.i_original_channels & AOUT_CHAN_DOLBYSTEREO)
357 val.i_int = AOUT_VAR_CHAN_DOLBYS;
358 txt.psz_string = _("Dolby Surround");
362 val.i_int = AOUT_VAR_CHAN_STEREO;
363 txt.psz_string = _("Stereo");
365 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
366 var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &val, NULL);
367 val.i_int = AOUT_VAR_CHAN_LEFT;
368 txt.psz_string = _("Left");
369 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
370 if (fmt.i_original_channels & AOUT_CHAN_DUALMONO)
371 { /* Go directly to the left channel. */
372 fmt.i_original_channels = AOUT_CHAN_LEFT;
373 var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &val, NULL);
375 val.i_int = AOUT_VAR_CHAN_RIGHT;
376 txt.psz_string = _("Right");
377 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
378 val.i_int = AOUT_VAR_CHAN_RSTEREO;
379 txt.psz_string = _("Reverse stereo");
380 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
384 aout_FormatPrepare (&fmt);
385 aout_FormatPrint (aout, "output", &fmt );
387 /* Choose the mixer format. */
388 owner->mixer_format = fmt;
389 if (!AOUT_FMT_LINEAR(&fmt))
390 owner->mixer_format.i_format = fmtp->i_format;
392 /* Most audio filters can only deal with single-precision,
393 * so lets always use that when hardware supports floating point. */
395 owner->mixer_format.i_format = VLC_CODEC_FL32;
397 /* Fallback to 16-bits. This avoids pointless conversion to and from
398 * 32-bits samples for the sole purpose of software mixing. */
399 owner->mixer_format.i_format = VLC_CODEC_S16N;
401 aout_FormatPrepare (&owner->mixer_format);
402 aout_FormatPrint (aout, "mixer", &owner->mixer_format);
404 /* Create converters. */
405 owner->nb_converters = 0;
406 if (aout_FiltersPipelineCreate (aout, owner->converters,
407 &owner->nb_converters,
408 sizeof (owner->converters) / sizeof (owner->converters[0]),
409 &owner->mixer_format, &fmt) < 0)
411 msg_Err (aout, "couldn't create audio output pipeline");
412 aout_OutputDelete (aout);
419 * Stops the audio output stream (undoes aout_OutputNew()).
420 * \note This can only be called after a succesful aout_OutputNew().
421 * \warning The caller must hold the audio output lock.
423 void aout_OutputDelete (audio_output_t *aout)
425 aout_owner_t *owner = aout_owner (aout);
427 aout_assert_locked (aout);
429 var_DelCallback (aout, "stereo-mode", aout_ChannelsRestart, NULL);
430 if (aout->stop != NULL)
432 aout_FiltersPipelineDestroy (owner->converters, owner->nb_converters);
436 * Plays a decoded audio buffer.
437 * \note This can only be called after a succesful aout_OutputNew().
438 * \warning The caller must hold the audio output lock.
440 void aout_OutputPlay (audio_output_t *aout, block_t *block)
442 aout_owner_t *owner = aout_owner (aout);
445 aout_assert_locked (aout);
447 block = aout_FiltersPipelinePlay (owner->converters, owner->nb_converters,
451 if (block->i_buffer == 0)
453 block_Release (block);
457 if (aout->time_get != NULL && aout->time_get (aout, &drift) == 0)
458 drift -= block->i_pts;
462 aout->play (aout, block);
464 * Notifies the audio input of the drift from the requested audio
465 * playback timestamp (@ref block_t.i_pts) to the anticipated playback time
466 * as reported by the audio output hardware.
467 * Depending on the drift amplitude, the input core may ignore the drift
468 * trigger upsampling or downsampling, or even discard samples.
469 * Future VLC versions may instead adjust the input decoding speed.
471 * The audio output plugin is responsible for estimating the time. Typically,
472 * the audio output can estimate the total buffer delay. Then:
473 * pts = mdate() + delay
475 if (drift < -AOUT_MAX_PTS_ADVANCE || +AOUT_MAX_PTS_DELAY < drift)
477 msg_Warn (aout, "not synchronized (%"PRId64" us), resampling",
479 if (date_Get (&owner->sync.date) != VLC_TS_INVALID)
480 date_Move (&owner->sync.date, drift);
485 * Notifies the audio output (if any) of pause/resume events.
486 * This enables the output to expedite pause, instead of waiting for its
488 * \note This can only be called after a succesful aout_OutputNew().
489 * \warning The caller must hold the audio output lock.
491 void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
493 aout_assert_locked( aout );
494 if( aout->pause != NULL )
495 aout->pause( aout, pause, date );
499 * Flushes or drains the audio output buffers.
500 * This enables the output to expedite seek and stop.
501 * \param wait if true, wait for buffer playback (i.e. drain),
502 * if false, discard the buffers immediately (i.e. flush)
503 * \note This can only be called after a succesful aout_OutputNew().
504 * \warning The caller must hold the audio output lock.
506 void aout_OutputFlush( audio_output_t *aout, bool wait )
508 aout_assert_locked( aout );
510 if( aout->flush != NULL )
511 aout->flush( aout, wait );