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 void aout_DeviceNotify (audio_output_t *aout, const char *id)
74 var_SetString (aout, "device", (id != NULL) ? id : "");
77 static void aout_RestartNotify (audio_output_t *aout, unsigned mode)
79 aout_RequestRestart (aout, mode);
82 static int aout_GainNotify (audio_output_t *aout, float gain)
84 aout_owner_t *owner = aout_owner (aout);
86 aout_assert_locked (aout);
87 aout_volume_SetVolume (owner->volume, gain);
88 /* XXX: ideally, return -1 if format cannot be amplified */
94 * Creates an audio output object and initializes an output module.
96 audio_output_t *aout_New (vlc_object_t *parent)
98 vlc_value_t val, text;
100 audio_output_t *aout = vlc_custom_create (parent, sizeof (aout_instance_t),
102 if (unlikely(aout == NULL))
105 aout_owner_t *owner = aout_owner (aout);
107 vlc_mutex_init (&owner->lock);
108 vlc_object_set_destructor (aout, aout_Destructor);
110 /* Audio output module callbacks */
111 var_Create (aout, "volume", VLC_VAR_FLOAT);
112 var_AddCallback (aout, "volume", var_Copy, parent);
113 var_Create (aout, "mute", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
114 var_AddCallback (aout, "mute", var_Copy, parent);
115 var_Create (aout, "device", VLC_VAR_STRING);
117 aout->event.volume_report = aout_VolumeNotify;
118 aout->event.mute_report = aout_MuteNotify;
119 aout->event.device_report = aout_DeviceNotify;
120 aout->event.policy_report = aout_PolicyNotify;
121 aout->event.gain_request = aout_GainNotify;
122 aout->event.restart_request = aout_RestartNotify;
124 /* Audio output module initialization */
127 aout->volume_set = NULL;
128 aout->mute_set = NULL;
129 aout->device_enum = NULL;
130 aout->device_select = NULL;
131 owner->module = module_need (aout, "audio output", "$aout", false);
132 if (owner->module == NULL)
134 msg_Err (aout, "no suitable audio output module");
135 vlc_object_release (aout);
140 * Persistent audio output variables
142 module_config_t *cfg;
146 var_Create (aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
147 text.psz_string = _("Visualizations");
148 var_Change (aout, "visual", VLC_VAR_SETTEXT, &text, NULL);
149 val.psz_string = (char *)"";
150 text.psz_string = _("Disable");
151 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
152 val.psz_string = (char *)"spectrometer";
153 text.psz_string = _("Spectrometer");
154 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
155 val.psz_string = (char *)"scope";
156 text.psz_string = _("Scope");
157 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
158 val.psz_string = (char *)"spectrum";
159 text.psz_string = _("Spectrum");
160 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
161 val.psz_string = (char *)"vuMeter";
162 text.psz_string = _("Vu meter");
163 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
164 /* Look for goom plugin */
165 if (module_exists ("goom"))
167 val.psz_string = (char *)"goom";
168 text.psz_string = (char *)"Goom";
169 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
171 /* Look for libprojectM plugin */
172 if (module_exists ("projectm"))
174 val.psz_string = (char *)"projectm";
175 text.psz_string = (char*)"projectM";
176 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
178 /* Look for VSXu plugin */
179 if (module_exists ("vsxu"))
181 val.psz_string = (char *)"vsxu";
182 text.psz_string = (char*)"Vovoid VSXu";
183 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
185 str = var_GetNonEmptyString (aout, "effect-list");
188 var_SetString (aout, "visual", str);
193 var_Create (aout, "equalizer", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
194 text.psz_string = _("Equalizer");
195 var_Change (aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL);
196 val.psz_string = (char*)"";
197 text.psz_string = _("Disable");
198 var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
199 cfg = config_FindConfig (VLC_OBJECT(aout), "equalizer-preset");
200 if (likely(cfg != NULL))
201 for (unsigned i = 0; i < cfg->list_count; i++)
203 val.psz_string = cfg->list.psz[i];
204 text.psz_string = vlc_gettext(cfg->list_text[i]);
205 var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
208 var_Create (aout, "audio-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
209 text.psz_string = _("Audio filters");
210 var_Change (aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL);
213 var_Create (aout, "audio-visual", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
214 text.psz_string = _("Audio visualizations");
215 var_Change (aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL);
218 var_Create (aout, "audio-replay-gain-mode",
219 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
220 text.psz_string = _("Replay gain");
221 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL);
222 cfg = config_FindConfig (VLC_OBJECT(aout), "audio-replay-gain-mode");
223 if (likely(cfg != NULL))
224 for (unsigned i = 0; i < cfg->list_count; i++)
226 val.psz_string = cfg->list.psz[i];
227 text.psz_string = vlc_gettext(cfg->list_text[i]);
228 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
236 * Deinitializes an audio output module and destroys an audio output object.
238 void aout_Destroy (audio_output_t *aout)
240 aout_owner_t *owner = aout_owner (aout);
243 module_unneed (aout, owner->module);
244 /* Protect against late call from intf.c */
245 aout->volume_set = NULL;
246 aout->mute_set = NULL;
249 var_DelCallback (aout, "mute", var_Copy, aout->p_parent);
250 var_SetFloat (aout, "volume", -1.f);
251 var_DelCallback (aout, "volume", var_Copy, aout->p_parent);
252 vlc_object_release (aout);
256 * Destroys the audio output lock used (asynchronously) by interface functions.
258 static void aout_Destructor (vlc_object_t *obj)
260 audio_output_t *aout = (audio_output_t *)obj;
261 aout_owner_t *owner = aout_owner (aout);
263 vlc_mutex_destroy (&owner->lock);
267 * Gets the volume of the audio output stream (independent of mute).
268 * \return Current audio volume (0. = silent, 1. = nominal),
269 * or a strictly negative value if undefined.
271 float aout_VolumeGet (audio_output_t *aout)
273 return var_GetFloat (aout, "volume");
277 * Sets the volume of the audio output stream.
278 * \note The mute status is not changed.
279 * \return 0 on success, -1 on failure.
281 int aout_VolumeSet (audio_output_t *aout, float vol)
286 if (aout->volume_set != NULL)
287 ret = aout->volume_set (aout, vol);
293 * Gets the audio output stream mute flag.
294 * \return 0 if not muted, 1 if muted, -1 if undefined.
296 int aout_MuteGet (audio_output_t *aout)
298 return var_InheritBool (aout, "mute");
302 * Sets the audio output stream mute flag.
303 * \return 0 on success, -1 on failure.
305 int aout_MuteSet (audio_output_t *aout, bool mute)
310 if (aout->mute_set != NULL)
311 ret = aout->mute_set (aout, mute);
317 * Gets the currently selected device.
318 * \return the selected device ID (caller must free() it)
319 * NULL if no device is selected or in case of error.
321 char *aout_DeviceGet (audio_output_t *aout)
323 return var_GetNonEmptyString (aout, "device");
327 * Selects an audio output device.
328 * \param id device ID to select, or NULL for the default device
329 * \return zero on success, non-zero on error.
331 int aout_DeviceSet (audio_output_t *aout, const char *id)
336 if (aout->device_select != NULL)
337 ret = aout->device_select (aout, id);
343 * Enumerates possible audio output devices.
345 * The function will heap-allocate two tables of heap-allocated strings;
346 * the caller is responsible for freeing all strings and both tables.
348 * \param ids pointer to a table of device identifiers [OUT]
349 * \param names pointer to a table of device human-readable descriptions [OUT]
350 * \return the number of devices, or negative on error.
351 * \note In case of error, *ids and *names are undefined.
353 int aout_DevicesList (audio_output_t *aout, char ***ids, char ***names)
358 if (aout->device_enum != NULL)
359 ret = aout->device_enum (aout, ids, names);
366 * Starts an audio output stream.
367 * \param fmt audio output stream format [IN/OUT]
368 * \warning The caller must hold the audio output lock.
370 int aout_OutputNew (audio_output_t *aout, audio_sample_format_t *restrict fmt)
372 aout_assert_locked (aout);
374 /* Ideally, the audio filters would be created before the audio output,
375 * and the ideal audio format would be the output of the filters chain.
376 * But that scheme would not really play well with digital pass-through. */
377 if (AOUT_FMT_LINEAR(fmt))
378 { /* Try to stay in integer domain if possible for no/slow FPU. */
379 fmt->i_format = (fmt->i_bitspersample > 16) ? VLC_CODEC_FL32
381 aout_FormatPrepare (fmt);
384 if (aout->start (aout, fmt))
386 msg_Err (aout, "module not functional");
390 if (!var_Type (aout, "stereo-mode"))
391 var_Create (aout, "stereo-mode",
392 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT);
394 /* The user may have selected a different channels configuration. */
395 var_AddCallback (aout, "stereo-mode", aout_ChannelsRestart, NULL);
396 switch (var_GetInteger (aout, "stereo-mode"))
398 case AOUT_VAR_CHAN_RSTEREO:
399 fmt->i_original_channels |= AOUT_CHAN_REVERSESTEREO;
401 case AOUT_VAR_CHAN_STEREO:
402 fmt->i_original_channels = AOUT_CHANS_STEREO;
404 case AOUT_VAR_CHAN_LEFT:
405 fmt->i_original_channels = AOUT_CHAN_LEFT;
407 case AOUT_VAR_CHAN_RIGHT:
408 fmt->i_original_channels = AOUT_CHAN_RIGHT;
410 case AOUT_VAR_CHAN_DOLBYS:
411 fmt->i_original_channels = AOUT_CHANS_STEREO|AOUT_CHAN_DOLBYSTEREO;
415 if ((fmt->i_original_channels & AOUT_CHAN_PHYSMASK)
416 != AOUT_CHANS_STEREO)
419 vlc_value_t val, txt;
421 var_Change (aout, "stereo-mode", VLC_VAR_DELCHOICE, &val, NULL);
422 txt.psz_string = _("Stereo audio mode");
423 var_Change (aout, "stereo-mode", VLC_VAR_SETTEXT, &txt, NULL);
424 if (fmt->i_original_channels & AOUT_CHAN_DOLBYSTEREO)
426 val.i_int = AOUT_VAR_CHAN_DOLBYS;
427 txt.psz_string = _("Dolby Surround");
431 val.i_int = AOUT_VAR_CHAN_STEREO;
432 txt.psz_string = _("Stereo");
434 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
435 var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &val, NULL);
436 val.i_int = AOUT_VAR_CHAN_LEFT;
437 txt.psz_string = _("Left");
438 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
439 if (fmt->i_original_channels & AOUT_CHAN_DUALMONO)
440 { /* Go directly to the left channel. */
441 fmt->i_original_channels = AOUT_CHAN_LEFT;
442 var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &val, NULL);
444 val.i_int = AOUT_VAR_CHAN_RIGHT;
445 txt.psz_string = _("Right");
446 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
447 val.i_int = AOUT_VAR_CHAN_RSTEREO;
448 txt.psz_string = _("Reverse stereo");
449 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
453 aout_FormatPrepare (fmt);
454 aout_FormatPrint (aout, "output", fmt);
459 * Stops the audio output stream (undoes aout_OutputNew()).
460 * \note This can only be called after a succesful aout_OutputNew().
461 * \warning The caller must hold the audio output lock.
463 void aout_OutputDelete (audio_output_t *aout)
465 aout_assert_locked (aout);
467 var_DelCallback (aout, "stereo-mode", aout_ChannelsRestart, NULL);
468 if (aout->stop != NULL)
472 int aout_OutputTimeGet (audio_output_t *aout, mtime_t *delay)
474 aout_assert_locked (aout);
476 if (aout->time_get == NULL)
478 return aout->time_get (aout, delay);
482 * Plays a decoded audio buffer.
483 * \note This can only be called after a succesful aout_OutputNew().
484 * \warning The caller must hold the audio output lock.
486 void aout_OutputPlay (audio_output_t *aout, block_t *block)
488 aout_assert_locked (aout);
489 aout->play (aout, block);
492 static void PauseDefault (audio_output_t *aout, bool pause, mtime_t date)
495 aout_OutputFlush (aout, false);
500 * Notifies the audio output (if any) of pause/resume events.
501 * This enables the output to expedite pause, instead of waiting for its
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_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
508 aout_assert_locked (aout);
509 ((aout->pause != NULL) ? aout->pause : PauseDefault) (aout, pause, date);
513 * Flushes or drains the audio output buffers.
514 * This enables the output to expedite seek and stop.
515 * \param wait if true, wait for buffer playback (i.e. drain),
516 * if false, discard the buffers immediately (i.e. flush)
517 * \note This can only be called after a succesful aout_OutputNew().
518 * \warning The caller must hold the audio output lock.
520 void aout_OutputFlush( audio_output_t *aout, bool wait )
522 aout_assert_locked( aout );
523 aout->flush (aout, wait);