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);
101 /* Audio output module callbacks */
102 var_Create (aout, "volume", VLC_VAR_FLOAT);
103 var_AddCallback (aout, "volume", var_Copy, parent);
104 var_Create (aout, "mute", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
105 var_AddCallback (aout, "mute", var_Copy, parent);
107 aout->event.volume_report = aout_VolumeNotify;
108 aout->event.mute_report = aout_MuteNotify;
109 aout->event.policy_report = aout_PolicyNotify;
110 aout->event.gain_request = aout_GainNotify;
112 /* Audio output module initialization */
115 aout->volume_set = NULL;
116 aout->mute_set = NULL;
117 owner->module = module_need (aout, "audio output", "$aout", false);
118 if (owner->module == NULL)
120 msg_Err (aout, "no suitable audio output module");
121 vlc_object_release (aout);
126 * Persistent audio output variables
128 vlc_value_t val, text;
129 module_config_t *cfg;
133 var_Create (aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
134 text.psz_string = _("Visualizations");
135 var_Change (aout, "visual", VLC_VAR_SETTEXT, &text, NULL);
136 val.psz_string = (char *)"";
137 text.psz_string = _("Disable");
138 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
139 val.psz_string = (char *)"spectrometer";
140 text.psz_string = _("Spectrometer");
141 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
142 val.psz_string = (char *)"scope";
143 text.psz_string = _("Scope");
144 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
145 val.psz_string = (char *)"spectrum";
146 text.psz_string = _("Spectrum");
147 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
148 val.psz_string = (char *)"vuMeter";
149 text.psz_string = _("Vu meter");
150 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
151 /* Look for goom plugin */
152 if (module_exists ("goom"))
154 val.psz_string = (char *)"goom";
155 text.psz_string = (char *)"Goom";
156 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
158 /* Look for libprojectM plugin */
159 if (module_exists ("projectm"))
161 val.psz_string = (char *)"projectm";
162 text.psz_string = (char*)"projectM";
163 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
165 /* Look for VSXu plugin */
166 if (module_exists ("vsxu"))
168 val.psz_string = (char *)"vsxu";
169 text.psz_string = (char*)"Vovoid VSXu";
170 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
172 str = var_GetNonEmptyString (aout, "effect-list");
175 var_SetString (aout, "visual", str);
180 var_Create (aout, "equalizer", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
181 text.psz_string = _("Equalizer");
182 var_Change (aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL);
183 val.psz_string = (char*)"";
184 text.psz_string = _("Disable");
185 var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
186 cfg = config_FindConfig (VLC_OBJECT(aout), "equalizer-preset");
187 if (likely(cfg != NULL))
188 for (unsigned i = 0; i < cfg->list_count; i++)
190 val.psz_string = cfg->list.psz[i];
191 text.psz_string = vlc_gettext(cfg->list_text[i]);
192 var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
195 var_Create (aout, "audio-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
196 text.psz_string = _("Audio filters");
197 var_Change (aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL);
200 var_Create (aout, "audio-visual", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
201 text.psz_string = _("Audio visualizations");
202 var_Change (aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL);
205 var_Create (aout, "audio-replay-gain-mode",
206 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
207 text.psz_string = _("Replay gain");
208 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL);
209 cfg = config_FindConfig (VLC_OBJECT(aout), "audio-replay-gain-mode");
210 if (likely(cfg != NULL))
211 for (unsigned i = 0; i < cfg->list_count; i++)
213 val.psz_string = cfg->list.psz[i];
214 text.psz_string = vlc_gettext(cfg->list_text[i]);
215 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
223 * Deinitializes an audio output module and destroys an audio output object.
225 void aout_Destroy (audio_output_t *aout)
227 aout_owner_t *owner = aout_owner (aout);
230 module_unneed (aout, owner->module);
231 /* Protect against late call from intf.c */
232 aout->volume_set = NULL;
233 aout->mute_set = NULL;
236 var_DelCallback (aout, "mute", var_Copy, aout->p_parent);
237 var_SetFloat (aout, "volume", -1.f);
238 var_DelCallback (aout, "volume", var_Copy, aout->p_parent);
239 vlc_object_release (aout);
243 * Destroys the audio output lock used (asynchronously) by interface functions.
245 static void aout_Destructor (vlc_object_t *obj)
247 audio_output_t *aout = (audio_output_t *)obj;
248 aout_owner_t *owner = aout_owner (aout);
250 vlc_mutex_destroy (&owner->lock);
254 * Gets the volume of the audio output stream (independent of mute).
255 * \return Current audio volume (0. = silent, 1. = nominal),
256 * or a strictly negative value if undefined.
258 float aout_OutputVolumeGet (audio_output_t *aout)
260 return var_GetFloat (aout, "volume");
264 * Sets the volume of the audio output stream.
265 * \note The mute status is not changed.
266 * \return 0 on success, -1 on failure.
268 int aout_OutputVolumeSet (audio_output_t *aout, float vol)
273 if (aout->volume_set != NULL)
274 ret = aout->volume_set (aout, vol);
280 * Gets the audio output stream mute flag.
281 * \return 0 if not muted, 1 if muted, -1 if undefined.
283 int aout_OutputMuteGet (audio_output_t *aout)
285 return var_InheritBool (aout, "mute");
289 * Sets the audio output stream mute flag.
290 * \return 0 on success, -1 on failure.
292 int aout_OutputMuteSet (audio_output_t *aout, bool mute)
297 if (aout->mute_set != NULL)
298 ret = aout->mute_set (aout, mute);
304 * Starts an audio output stream.
305 * \param fmtp audio output stream format [IN/OUT]
306 * \warning The caller must hold the audio output lock.
308 int aout_OutputNew (audio_output_t *aout, const audio_sample_format_t *fmtp)
310 aout_owner_t *owner = aout_owner (aout);
312 audio_sample_format_t fmt = *fmtp;
313 aout_FormatPrepare (&fmt);
315 aout_assert_locked (aout);
317 if (aout->start (aout, &fmt))
319 msg_Err (aout, "module not functional");
323 if (!var_Type (aout, "stereo-mode"))
324 var_Create (aout, "stereo-mode",
325 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT);
327 /* The user may have selected a different channels configuration. */
328 var_AddCallback (aout, "stereo-mode", aout_ChannelsRestart, NULL);
329 switch (var_GetInteger (aout, "stereo-mode"))
331 case AOUT_VAR_CHAN_RSTEREO:
332 fmt.i_original_channels |= AOUT_CHAN_REVERSESTEREO;
334 case AOUT_VAR_CHAN_STEREO:
335 fmt.i_original_channels = AOUT_CHANS_STEREO;
337 case AOUT_VAR_CHAN_LEFT:
338 fmt.i_original_channels = AOUT_CHAN_LEFT;
340 case AOUT_VAR_CHAN_RIGHT:
341 fmt.i_original_channels = AOUT_CHAN_RIGHT;
343 case AOUT_VAR_CHAN_DOLBYS:
344 fmt.i_original_channels = AOUT_CHANS_STEREO|AOUT_CHAN_DOLBYSTEREO;
348 if ((fmt.i_original_channels & AOUT_CHAN_PHYSMASK)
349 != AOUT_CHANS_STEREO)
352 vlc_value_t val, txt;
354 var_Change (aout, "stereo-mode", VLC_VAR_DELCHOICE, &val, NULL);
355 txt.psz_string = _("Stereo audio mode");
356 var_Change (aout, "stereo-mode", VLC_VAR_SETTEXT, &txt, NULL);
357 if (fmt.i_original_channels & AOUT_CHAN_DOLBYSTEREO)
359 val.i_int = AOUT_VAR_CHAN_DOLBYS;
360 txt.psz_string = _("Dolby Surround");
364 val.i_int = AOUT_VAR_CHAN_STEREO;
365 txt.psz_string = _("Stereo");
367 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
368 var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &val, NULL);
369 val.i_int = AOUT_VAR_CHAN_LEFT;
370 txt.psz_string = _("Left");
371 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
372 if (fmt.i_original_channels & AOUT_CHAN_DUALMONO)
373 { /* Go directly to the left channel. */
374 fmt.i_original_channels = AOUT_CHAN_LEFT;
375 var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &val, NULL);
377 val.i_int = AOUT_VAR_CHAN_RIGHT;
378 txt.psz_string = _("Right");
379 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
380 val.i_int = AOUT_VAR_CHAN_RSTEREO;
381 txt.psz_string = _("Reverse stereo");
382 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
386 aout_FormatPrepare (&fmt);
387 aout_FormatPrint (aout, "output", &fmt );
389 /* Choose the mixer format. */
390 owner->mixer_format = fmt;
391 if (!AOUT_FMT_LINEAR(&fmt))
392 owner->mixer_format.i_format = fmtp->i_format;
394 /* Most audio filters can only deal with single-precision,
395 * so lets always use that when hardware supports floating point. */
397 owner->mixer_format.i_format = VLC_CODEC_FL32;
399 /* Fallback to 16-bits. This avoids pointless conversion to and from
400 * 32-bits samples for the sole purpose of software mixing. */
401 owner->mixer_format.i_format = VLC_CODEC_S16N;
403 aout_FormatPrepare (&owner->mixer_format);
404 aout_FormatPrint (aout, "mixer", &owner->mixer_format);
406 /* Create filters. */
407 owner->nb_filters = 0;
408 if (aout_FiltersCreatePipeline (aout, owner->filters, &owner->nb_filters,
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_FiltersDestroyPipeline (owner->filters, owner->nb_filters);
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 aout_FiltersPlay (owner->filters, owner->nb_filters, &block);
450 if (block->i_buffer == 0)
452 block_Release (block);
456 aout->play (aout, block, &drift);
458 * Notifies the audio input of the drift from the requested audio
459 * playback timestamp (@ref block_t.i_pts) to the anticipated playback time
460 * as reported by the audio output hardware.
461 * Depending on the drift amplitude, the input core may ignore the drift
462 * trigger upsampling or downsampling, or even discard samples.
463 * Future VLC versions may instead adjust the input decoding speed.
465 * The audio output plugin is responsible for estimating the drift. A negative
466 * value means playback is ahead of the intended time and a positive value
467 * means playback is late from the intended time. In most cases, the audio
468 * output can estimate the delay until playback of the next sample to be
469 * queued. Then, before the block is queued:
470 * drift = mdate() + delay - block->i_pts
471 * where mdate() + delay is the estimated time when the sample will be rendered
472 * and block->i_pts is the intended time.
474 if (drift < -AOUT_MAX_PTS_ADVANCE || +AOUT_MAX_PTS_DELAY < drift)
476 msg_Warn (aout, "not synchronized (%"PRId64" us), resampling",
478 if (date_Get (&owner->sync.date) != VLC_TS_INVALID)
479 date_Move (&owner->sync.date, drift);
484 * Notifies the audio output (if any) of pause/resume events.
485 * This enables the output to expedite pause, instead of waiting for its
487 * \note This can only be called after a succesful aout_OutputNew().
488 * \warning The caller must hold the audio output lock.
490 void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
492 aout_assert_locked( aout );
493 if( aout->pause != NULL )
494 aout->pause( aout, pause, date );
498 * Flushes or drains the audio output buffers.
499 * This enables the output to expedite seek and stop.
500 * \param wait if true, wait for buffer playback (i.e. drain),
501 * if false, discard the buffers immediately (i.e. flush)
502 * \note This can only be called after a succesful aout_OutputNew().
503 * \warning The caller must hold the audio output lock.
505 void aout_OutputFlush( audio_output_t *aout, bool wait )
507 aout_assert_locked( aout );
509 if( aout->flush != NULL )
510 aout->flush( aout, wait );