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>
33 #include "aout_internal.h"
36 static void aout_Destructor( vlc_object_t * p_this );
38 static int var_Copy (vlc_object_t *src, const char *name, vlc_value_t prev,
39 vlc_value_t value, void *data)
41 vlc_object_t *dst = data;
43 (void) src; (void) prev;
44 return var_Set (dst, name, value);
48 * Supply or update the current custom ("hardware") volume.
49 * @note This only makes sense after calling aout_VolumeHardInit().
50 * @param volume current custom volume
52 * @warning The caller (i.e. the audio output plug-in) is responsible for
53 * interlocking and synchronizing call to this function and to the
54 * audio_output_t.volume_set callback. This ensures that VLC gets correct
55 * volume information (possibly with a latency).
57 static void aout_VolumeNotify (audio_output_t *aout, float volume)
59 var_SetFloat (aout, "volume", volume);
62 static void aout_MuteNotify (audio_output_t *aout, bool mute)
64 var_SetBool (aout, "mute", mute);
67 static void aout_PolicyNotify (audio_output_t *aout, bool cork)
69 (cork ? var_IncInteger : var_DecInteger) (aout->p_parent, "corks");
72 static int aout_GainNotify (audio_output_t *aout, float gain)
74 aout_owner_t *owner = aout_owner (aout);
76 aout_assert_locked (aout);
77 aout_volume_SetVolume (owner->volume, gain);
78 /* XXX: ideally, return -1 if format cannot be amplified */
84 * Creates an audio output object and initializes an output module.
86 audio_output_t *aout_New (vlc_object_t *parent)
88 audio_output_t *aout = vlc_custom_create (parent, sizeof (aout_instance_t),
90 if (unlikely(aout == NULL))
93 aout_owner_t *owner = aout_owner (aout);
95 vlc_mutex_init (&owner->lock);
96 vlc_object_set_destructor (aout, aout_Destructor);
98 /* Audio output module callbacks */
99 var_Create (aout, "volume", VLC_VAR_FLOAT);
100 var_AddCallback (aout, "volume", var_Copy, parent);
101 var_Create (aout, "mute", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
102 var_AddCallback (aout, "mute", var_Copy, parent);
104 aout->event.volume_report = aout_VolumeNotify;
105 aout->event.mute_report = aout_MuteNotify;
106 aout->event.policy_report = aout_PolicyNotify;
107 aout->event.gain_request = aout_GainNotify;
109 /* Audio output module initialization */
112 aout->volume_set = NULL;
113 aout->mute_set = NULL;
114 owner->module = module_need (aout, "audio output", "$aout", false);
115 if (owner->module == NULL)
117 msg_Err (aout, "no suitable audio output module");
118 vlc_object_release (aout);
123 * Persistent audio output variables
125 vlc_value_t val, text;
126 module_config_t *cfg;
130 var_Create (aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
131 text.psz_string = _("Visualizations");
132 var_Change (aout, "visual", VLC_VAR_SETTEXT, &text, NULL);
133 val.psz_string = (char *)"";
134 text.psz_string = _("Disable");
135 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
136 val.psz_string = (char *)"spectrometer";
137 text.psz_string = _("Spectrometer");
138 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
139 val.psz_string = (char *)"scope";
140 text.psz_string = _("Scope");
141 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
142 val.psz_string = (char *)"spectrum";
143 text.psz_string = _("Spectrum");
144 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
145 val.psz_string = (char *)"vuMeter";
146 text.psz_string = _("Vu meter");
147 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
148 /* Look for goom plugin */
149 if (module_exists ("goom"))
151 val.psz_string = (char *)"goom";
152 text.psz_string = (char *)"Goom";
153 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
155 /* Look for libprojectM plugin */
156 if (module_exists ("projectm"))
158 val.psz_string = (char *)"projectm";
159 text.psz_string = (char*)"projectM";
160 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
162 /* Look for VSXu plugin */
163 if (module_exists ("vsxu"))
165 val.psz_string = (char *)"vsxu";
166 text.psz_string = (char*)"Vovoid VSXu";
167 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
169 str = var_GetNonEmptyString (aout, "effect-list");
172 var_SetString (aout, "visual", str);
177 var_Create (aout, "equalizer", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
178 text.psz_string = _("Equalizer");
179 var_Change (aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL);
180 val.psz_string = (char*)"";
181 text.psz_string = _("Disable");
182 var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
183 cfg = config_FindConfig (VLC_OBJECT(aout), "equalizer-preset");
184 if (likely(cfg != NULL))
185 for (unsigned i = 0; i < cfg->list_count; i++)
187 val.psz_string = cfg->list.psz[i];
188 text.psz_string = vlc_gettext(cfg->list_text[i]);
189 var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
192 var_Create (aout, "audio-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
193 text.psz_string = _("Audio filters");
194 var_Change (aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL);
197 var_Create (aout, "audio-visual", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
198 text.psz_string = _("Audio visualizations");
199 var_Change (aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL);
202 var_Create (aout, "audio-replay-gain-mode",
203 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
204 text.psz_string = _("Replay gain");
205 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL);
206 cfg = config_FindConfig (VLC_OBJECT(aout), "audio-replay-gain-mode");
207 if (likely(cfg != NULL))
208 for (unsigned i = 0; i < cfg->list_count; i++)
210 val.psz_string = cfg->list.psz[i];
211 text.psz_string = vlc_gettext(cfg->list_text[i]);
212 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
220 * Deinitializes an audio output module and destroys an audio output object.
222 void aout_Destroy (audio_output_t *aout)
224 aout_owner_t *owner = aout_owner (aout);
227 module_unneed (aout, owner->module);
228 /* Protect against late call from intf.c */
229 aout->volume_set = NULL;
230 aout->mute_set = NULL;
233 var_DelCallback (aout, "mute", var_Copy, aout->p_parent);
234 var_SetFloat (aout, "volume", -1.f);
235 var_DelCallback (aout, "volume", var_Copy, aout->p_parent);
236 vlc_object_release (aout);
240 * Destroys the audio output lock used (asynchronously) by interface functions.
242 static void aout_Destructor (vlc_object_t *obj)
244 audio_output_t *aout = (audio_output_t *)obj;
245 aout_owner_t *owner = aout_owner (aout);
247 vlc_mutex_destroy (&owner->lock);
251 * Gets the volume of the audio output stream (independent of mute).
252 * \return Current audio volume (0. = silent, 1. = nominal),
253 * or a strictly negative value if undefined.
255 float aout_VolumeGet (audio_output_t *aout)
257 return var_GetFloat (aout, "volume");
261 * Sets the volume of the audio output stream.
262 * \note The mute status is not changed.
263 * \return 0 on success, -1 on failure.
265 int aout_VolumeSet (audio_output_t *aout, float vol)
270 if (aout->volume_set != NULL)
271 ret = aout->volume_set (aout, vol);
277 * Gets the audio output stream mute flag.
278 * \return 0 if not muted, 1 if muted, -1 if undefined.
280 int aout_MuteGet (audio_output_t *aout)
282 return var_InheritBool (aout, "mute");
286 * Sets the audio output stream mute flag.
287 * \return 0 on success, -1 on failure.
289 int aout_MuteSet (audio_output_t *aout, bool mute)
294 if (aout->mute_set != NULL)
295 ret = aout->mute_set (aout, mute);
301 * Starts an audio output stream.
302 * \param fmt audio output stream format [IN/OUT]
303 * \warning The caller must hold the audio output lock.
305 int aout_OutputNew (audio_output_t *aout, audio_sample_format_t *restrict fmt)
307 aout_assert_locked (aout);
309 /* Ideally, the audio filters would be created before the audio output,
310 * and the ideal audio format would be the output of the filters chain.
311 * But that scheme would not really play well with digital pass-through. */
312 if (AOUT_FMT_LINEAR(fmt))
313 { /* Try to stay in integer domain if possible for no/slow FPU. */
314 fmt->i_format = (fmt->i_bitspersample > 16) ? VLC_CODEC_FL32
316 aout_FormatPrepare (fmt);
319 if (aout->start (aout, fmt))
321 msg_Err (aout, "module not functional");
325 if (!var_Type (aout, "stereo-mode"))
326 var_Create (aout, "stereo-mode",
327 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT);
329 /* The user may have selected a different channels configuration. */
330 var_AddCallback (aout, "stereo-mode", aout_ChannelsRestart, NULL);
331 switch (var_GetInteger (aout, "stereo-mode"))
333 case AOUT_VAR_CHAN_RSTEREO:
334 fmt->i_original_channels |= AOUT_CHAN_REVERSESTEREO;
336 case AOUT_VAR_CHAN_STEREO:
337 fmt->i_original_channels = AOUT_CHANS_STEREO;
339 case AOUT_VAR_CHAN_LEFT:
340 fmt->i_original_channels = AOUT_CHAN_LEFT;
342 case AOUT_VAR_CHAN_RIGHT:
343 fmt->i_original_channels = AOUT_CHAN_RIGHT;
345 case AOUT_VAR_CHAN_DOLBYS:
346 fmt->i_original_channels = AOUT_CHANS_STEREO|AOUT_CHAN_DOLBYSTEREO;
350 if ((fmt->i_original_channels & AOUT_CHAN_PHYSMASK)
351 != AOUT_CHANS_STEREO)
354 vlc_value_t val, txt;
356 var_Change (aout, "stereo-mode", VLC_VAR_DELCHOICE, &val, NULL);
357 txt.psz_string = _("Stereo audio mode");
358 var_Change (aout, "stereo-mode", VLC_VAR_SETTEXT, &txt, NULL);
359 if (fmt->i_original_channels & AOUT_CHAN_DOLBYSTEREO)
361 val.i_int = AOUT_VAR_CHAN_DOLBYS;
362 txt.psz_string = _("Dolby Surround");
366 val.i_int = AOUT_VAR_CHAN_STEREO;
367 txt.psz_string = _("Stereo");
369 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
370 var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &val, NULL);
371 val.i_int = AOUT_VAR_CHAN_LEFT;
372 txt.psz_string = _("Left");
373 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
374 if (fmt->i_original_channels & AOUT_CHAN_DUALMONO)
375 { /* Go directly to the left channel. */
376 fmt->i_original_channels = AOUT_CHAN_LEFT;
377 var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &val, NULL);
379 val.i_int = AOUT_VAR_CHAN_RIGHT;
380 txt.psz_string = _("Right");
381 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
382 val.i_int = AOUT_VAR_CHAN_RSTEREO;
383 txt.psz_string = _("Reverse stereo");
384 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
388 aout_FormatPrepare (fmt);
389 aout_FormatPrint (aout, "output", fmt);
394 * Stops the audio output stream (undoes aout_OutputNew()).
395 * \note This can only be called after a succesful aout_OutputNew().
396 * \warning The caller must hold the audio output lock.
398 void aout_OutputDelete (audio_output_t *aout)
400 aout_assert_locked (aout);
402 var_DelCallback (aout, "stereo-mode", aout_ChannelsRestart, NULL);
403 if (aout->stop != NULL)
407 int aout_OutputTimeGet (audio_output_t *aout, mtime_t *pts)
409 aout_assert_locked (aout);
411 if (aout->time_get == NULL)
413 return aout->time_get (aout, pts);
417 * Plays a decoded audio buffer.
418 * \note This can only be called after a succesful aout_OutputNew().
419 * \warning The caller must hold the audio output lock.
421 void aout_OutputPlay (audio_output_t *aout, block_t *block)
423 aout_assert_locked (aout);
424 aout->play (aout, block);
427 static void PauseDefault (audio_output_t *aout, bool pause, mtime_t date)
430 aout_OutputFlush (aout, false);
435 * Notifies the audio output (if any) of pause/resume events.
436 * This enables the output to expedite pause, instead of waiting for its
438 * \note This can only be called after a succesful aout_OutputNew().
439 * \warning The caller must hold the audio output lock.
441 void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
443 aout_assert_locked (aout);
444 ((aout->pause != NULL) ? aout->pause : PauseDefault) (aout, pause, date);
448 * Flushes or drains the audio output buffers.
449 * This enables the output to expedite seek and stop.
450 * \param wait if true, wait for buffer playback (i.e. drain),
451 * if false, discard the buffers immediately (i.e. flush)
452 * \note This can only be called after a succesful aout_OutputNew().
453 * \warning The caller must hold the audio output lock.
455 void aout_OutputFlush( audio_output_t *aout, bool wait )
457 aout_assert_locked( aout );
458 aout->flush (aout, wait);