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 *****************************************************************************/
31 #include <vlc_common.h>
33 #include <vlc_modules.h>
36 #include "aout_internal.h"
38 static const char unset_str[1] = ""; /* Non-NULL constant string pointer */
49 static void aout_OutputAssertLocked (audio_output_t *aout)
51 aout_owner_t *owner = aout_owner (aout);
53 vlc_assert_locked (&owner->lock);
56 static void aout_Destructor( vlc_object_t * p_this );
58 static int var_Copy (vlc_object_t *src, const char *name, vlc_value_t prev,
59 vlc_value_t value, void *data)
61 vlc_object_t *dst = data;
63 (void) src; (void) prev;
64 return var_Set (dst, name, value);
68 * Supply or update the current custom ("hardware") volume.
69 * @note This only makes sense after calling aout_VolumeHardInit().
70 * @param volume current custom volume
72 * @warning The caller (i.e. the audio output plug-in) is responsible for
73 * interlocking and synchronizing call to this function and to the
74 * audio_output_t.volume_set callback. This ensures that VLC gets correct
75 * volume information (possibly with a latency).
77 static void aout_VolumeNotify (audio_output_t *aout, float volume)
79 var_SetFloat (aout, "volume", volume);
82 static void aout_MuteNotify (audio_output_t *aout, bool mute)
84 var_SetBool (aout, "mute", mute);
87 static void aout_PolicyNotify (audio_output_t *aout, bool cork)
89 (cork ? var_IncInteger : var_DecInteger) (aout->p_parent, "corks");
92 static void aout_DeviceNotify (audio_output_t *aout, const char *id)
94 var_SetString (aout, "device", (id != NULL) ? id : "");
97 static void aout_HotplugNotify (audio_output_t *aout,
98 const char *id, const char *name)
100 aout_owner_t *owner = aout_owner (aout);
101 aout_dev_t *dev, **pp = &owner->dev.list;
103 vlc_mutex_lock (&owner->dev.lock);
104 while ((dev = *pp) != NULL)
106 if (!strcmp (id, dev->id))
113 if (dev == NULL) /* Added device */
115 dev = malloc (sizeof (*dev) + strlen (id));
116 if (unlikely(dev == NULL))
119 strcpy (dev->id, id);
123 else /* Modified device */
125 dev->name = strdup (name);
129 if (dev != NULL) /* Removed device */
138 vlc_mutex_unlock (&owner->dev.lock);
141 static void aout_RestartNotify (audio_output_t *aout, unsigned mode)
143 aout_RequestRestart (aout, mode);
146 static int aout_GainNotify (audio_output_t *aout, float gain)
148 aout_owner_t *owner = aout_owner (aout);
150 aout_OutputAssertLocked (aout);
151 aout_volume_SetVolume (owner->volume, gain);
152 /* XXX: ideally, return -1 if format cannot be amplified */
158 * Creates an audio output object and initializes an output module.
160 audio_output_t *aout_New (vlc_object_t *parent)
162 vlc_value_t val, text;
164 audio_output_t *aout = vlc_custom_create (parent, sizeof (aout_instance_t),
166 if (unlikely(aout == NULL))
169 aout_owner_t *owner = aout_owner (aout);
171 vlc_mutex_init (&owner->lock);
172 vlc_mutex_init (&owner->req.lock);
173 vlc_mutex_init (&owner->dev.lock);
174 owner->req.device = (char *)unset_str;
175 owner->req.volume = -1.f;
176 owner->req.mute = -1;
178 vlc_object_set_destructor (aout, aout_Destructor);
180 /* Audio output module callbacks */
181 var_Create (aout, "volume", VLC_VAR_FLOAT);
182 var_AddCallback (aout, "volume", var_Copy, parent);
183 var_Create (aout, "mute", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
184 var_AddCallback (aout, "mute", var_Copy, parent);
185 var_Create (aout, "device", VLC_VAR_STRING);
187 aout->event.volume_report = aout_VolumeNotify;
188 aout->event.mute_report = aout_MuteNotify;
189 aout->event.policy_report = aout_PolicyNotify;
190 aout->event.device_report = aout_DeviceNotify;
191 aout->event.hotplug_report = aout_HotplugNotify;
192 aout->event.gain_request = aout_GainNotify;
193 aout->event.restart_request = aout_RestartNotify;
195 /* Audio output module initialization */
198 aout->volume_set = NULL;
199 aout->mute_set = NULL;
200 aout->device_select = NULL;
201 owner->module = module_need (aout, "audio output", "$aout", false);
202 if (owner->module == NULL)
204 msg_Err (aout, "no suitable audio output module");
205 vlc_object_release (aout);
210 * Persistent audio output variables
212 module_config_t *cfg;
216 var_Create (aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
217 text.psz_string = _("Visualizations");
218 var_Change (aout, "visual", VLC_VAR_SETTEXT, &text, NULL);
219 val.psz_string = (char *)"";
220 text.psz_string = _("Disable");
221 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
222 val.psz_string = (char *)"spectrometer";
223 text.psz_string = _("Spectrometer");
224 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
225 val.psz_string = (char *)"scope";
226 text.psz_string = _("Scope");
227 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
228 val.psz_string = (char *)"spectrum";
229 text.psz_string = _("Spectrum");
230 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
231 val.psz_string = (char *)"vuMeter";
232 text.psz_string = _("Vu meter");
233 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
234 /* Look for goom plugin */
235 if (module_exists ("goom"))
237 val.psz_string = (char *)"goom";
238 text.psz_string = (char *)"Goom";
239 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
241 /* Look for libprojectM plugin */
242 if (module_exists ("projectm"))
244 val.psz_string = (char *)"projectm";
245 text.psz_string = (char*)"projectM";
246 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
248 /* Look for VSXu plugin */
249 if (module_exists ("vsxu"))
251 val.psz_string = (char *)"vsxu";
252 text.psz_string = (char*)"Vovoid VSXu";
253 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
255 /* Look for glspectrum plugin */
256 if (module_exists ("glspectrum"))
258 val.psz_string = (char *)"glspectrum";
259 text.psz_string = (char*)"3D spectrum";
260 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
262 str = var_GetNonEmptyString (aout, "effect-list");
265 var_SetString (aout, "visual", str);
270 var_Create (aout, "equalizer", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
271 text.psz_string = _("Equalizer");
272 var_Change (aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL);
273 val.psz_string = (char*)"";
274 text.psz_string = _("Disable");
275 var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
276 cfg = config_FindConfig (VLC_OBJECT(aout), "equalizer-preset");
277 if (likely(cfg != NULL))
278 for (unsigned i = 0; i < cfg->list_count; i++)
280 val.psz_string = cfg->list.psz[i];
281 text.psz_string = vlc_gettext(cfg->list_text[i]);
282 var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
285 var_Create (aout, "audio-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
286 text.psz_string = _("Audio filters");
287 var_Change (aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL);
290 var_Create (aout, "audio-visual", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
291 text.psz_string = _("Audio visualizations");
292 var_Change (aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL);
295 var_Create (aout, "audio-replay-gain-mode",
296 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
297 text.psz_string = _("Replay gain");
298 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL);
299 cfg = config_FindConfig (VLC_OBJECT(aout), "audio-replay-gain-mode");
300 if (likely(cfg != NULL))
301 for (unsigned i = 0; i < cfg->list_count; i++)
303 val.psz_string = cfg->list.psz[i];
304 text.psz_string = vlc_gettext(cfg->list_text[i]);
305 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
309 var_Create (aout, "equalizer-preamp", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
310 var_Create (aout, "equalizer-bands", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
316 * Deinitializes an audio output module and destroys an audio output object.
318 void aout_Destroy (audio_output_t *aout)
320 aout_owner_t *owner = aout_owner (aout);
322 aout_OutputLock (aout);
323 module_unneed (aout, owner->module);
324 /* Protect against late call from intf.c */
325 aout->volume_set = NULL;
326 aout->mute_set = NULL;
327 aout_OutputUnlock (aout);
329 var_DelCallback (aout, "mute", var_Copy, aout->p_parent);
330 var_SetFloat (aout, "volume", -1.f);
331 var_DelCallback (aout, "volume", var_Copy, aout->p_parent);
332 vlc_object_release (aout);
336 * Destroys the audio output lock used (asynchronously) by interface functions.
338 static void aout_Destructor (vlc_object_t *obj)
340 audio_output_t *aout = (audio_output_t *)obj;
341 aout_owner_t *owner = aout_owner (aout);
343 vlc_mutex_destroy (&owner->dev.lock);
344 for (aout_dev_t *dev = owner->dev.list, *next; dev != NULL; dev = next)
351 assert (owner->req.device == unset_str);
352 vlc_mutex_destroy (&owner->req.lock);
353 vlc_mutex_destroy (&owner->lock);
357 * Starts an audio output stream.
358 * \param fmt audio output stream format [IN/OUT]
359 * \warning The caller must hold the audio output lock.
361 int aout_OutputNew (audio_output_t *aout, audio_sample_format_t *restrict fmt)
363 aout_OutputAssertLocked (aout);
365 /* Ideally, the audio filters would be created before the audio output,
366 * and the ideal audio format would be the output of the filters chain.
367 * But that scheme would not really play well with digital pass-through. */
368 if (AOUT_FMT_LINEAR(fmt))
369 { /* Try to stay in integer domain if possible for no/slow FPU. */
370 fmt->i_format = (fmt->i_bitspersample > 16) ? VLC_CODEC_FL32
372 aout_FormatPrepare (fmt);
375 if (aout->start (aout, fmt))
377 msg_Err (aout, "module not functional");
381 if (!var_Type (aout, "stereo-mode"))
383 var_Create (aout, "stereo-mode",
384 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT);
387 txt.psz_string = _("Stereo audio mode");
388 var_Change (aout, "stereo-mode", VLC_VAR_SETTEXT, &txt, NULL);
391 /* The user may have selected a different channels configuration. */
392 var_AddCallback (aout, "stereo-mode", aout_ChannelsRestart, NULL);
393 switch (var_GetInteger (aout, "stereo-mode"))
395 case AOUT_VAR_CHAN_RSTEREO:
396 fmt->i_original_channels |= AOUT_CHAN_REVERSESTEREO;
398 case AOUT_VAR_CHAN_STEREO:
399 fmt->i_original_channels = AOUT_CHANS_STEREO;
401 case AOUT_VAR_CHAN_LEFT:
402 fmt->i_original_channels = AOUT_CHAN_LEFT;
404 case AOUT_VAR_CHAN_RIGHT:
405 fmt->i_original_channels = AOUT_CHAN_RIGHT;
407 case AOUT_VAR_CHAN_DOLBYS:
408 fmt->i_original_channels = AOUT_CHANS_STEREO|AOUT_CHAN_DOLBYSTEREO;
412 if ((fmt->i_original_channels & AOUT_CHAN_PHYSMASK)
413 != AOUT_CHANS_STEREO)
416 vlc_value_t val, txt;
418 var_Change (aout, "stereo-mode", VLC_VAR_DELCHOICE, &val, NULL);
419 if (fmt->i_original_channels & AOUT_CHAN_DOLBYSTEREO)
421 val.i_int = AOUT_VAR_CHAN_DOLBYS;
422 txt.psz_string = _("Dolby Surround");
426 val.i_int = AOUT_VAR_CHAN_STEREO;
427 txt.psz_string = _("Stereo");
429 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
430 var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &val, NULL);
431 val.i_int = AOUT_VAR_CHAN_LEFT;
432 txt.psz_string = _("Left");
433 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
434 if (fmt->i_original_channels & AOUT_CHAN_DUALMONO)
435 { /* Go directly to the left channel. */
436 fmt->i_original_channels = AOUT_CHAN_LEFT;
437 var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &val, NULL);
439 val.i_int = AOUT_VAR_CHAN_RIGHT;
440 txt.psz_string = _("Right");
441 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
442 val.i_int = AOUT_VAR_CHAN_RSTEREO;
443 txt.psz_string = _("Reverse stereo");
444 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
448 aout_FormatPrepare (fmt);
449 aout_FormatPrint (aout, "output", fmt);
454 * Stops the audio output stream (undoes aout_OutputNew()).
455 * \note This can only be called after a succesful aout_OutputNew().
456 * \warning The caller must hold the audio output lock.
458 void aout_OutputDelete (audio_output_t *aout)
460 aout_OutputAssertLocked (aout);
462 var_DelCallback (aout, "stereo-mode", aout_ChannelsRestart, NULL);
463 if (aout->stop != NULL)
467 int aout_OutputTimeGet (audio_output_t *aout, mtime_t *delay)
469 aout_OutputAssertLocked (aout);
471 if (aout->time_get == NULL)
473 return aout->time_get (aout, delay);
477 * Plays a decoded audio buffer.
478 * \note This can only be called after a succesful aout_OutputNew().
479 * \warning The caller must hold the audio output lock.
481 void aout_OutputPlay (audio_output_t *aout, block_t *block)
483 aout_OutputAssertLocked (aout);
484 aout->play (aout, block);
487 static void PauseDefault (audio_output_t *aout, bool pause, mtime_t date)
490 aout_OutputFlush (aout, false);
495 * Notifies the audio output (if any) of pause/resume events.
496 * This enables the output to expedite pause, instead of waiting for its
498 * \note This can only be called after a succesful aout_OutputNew().
499 * \warning The caller must hold the audio output lock.
501 void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
503 aout_OutputAssertLocked (aout);
504 ((aout->pause != NULL) ? aout->pause : PauseDefault) (aout, pause, date);
508 * Flushes or drains the audio output buffers.
509 * This enables the output to expedite seek and stop.
510 * \param wait if true, wait for buffer playback (i.e. drain),
511 * if false, discard the buffers immediately (i.e. flush)
512 * \note This can only be called after a succesful aout_OutputNew().
513 * \warning The caller must hold the audio output lock.
515 void aout_OutputFlush( audio_output_t *aout, bool wait )
517 aout_OutputAssertLocked( aout );
518 aout->flush (aout, wait);
521 static int aout_OutputVolumeSet (audio_output_t *aout, float vol)
523 aout_OutputAssertLocked (aout);
524 return (aout->volume_set != NULL) ? aout->volume_set (aout, vol) : -1;
527 static int aout_OutputMuteSet (audio_output_t *aout, bool mute)
529 aout_OutputAssertLocked (aout);
530 return (aout->mute_set != NULL) ? aout->mute_set (aout, mute) : -1;
533 static int aout_OutputDeviceSet (audio_output_t *aout, const char *id)
535 aout_OutputAssertLocked (aout);
536 return (aout->device_select != NULL) ? aout->device_select (aout, id) : -1;
539 void aout_OutputLock (audio_output_t *aout)
541 aout_owner_t *owner = aout_owner (aout);
543 vlc_mutex_lock (&owner->lock);
546 static int aout_OutputTryLock (audio_output_t *aout)
548 aout_owner_t *owner = aout_owner (aout);
550 return vlc_mutex_trylock (&owner->lock);
553 void aout_OutputUnlock (audio_output_t *aout)
555 aout_owner_t *owner = aout_owner (aout);
557 vlc_assert_locked (&owner->lock);
558 vlc_mutex_lock (&owner->req.lock);
560 if (owner->req.device != unset_str)
562 aout_OutputDeviceSet (aout, owner->req.device);
563 free (owner->req.device);
564 owner->req.device = (char *)unset_str;
567 if (owner->req.volume >= 0.f)
569 aout_OutputVolumeSet (aout, owner->req.volume);
570 owner->req.volume = -1.f;
573 if (owner->req.mute >= 0)
575 aout_OutputMuteSet (aout, owner->req.mute);
576 owner->req.mute = -1;
579 vlc_mutex_unlock (&owner->lock);
580 /* If another thread is blocked waiting for owner->req.lock at this point,
581 * this aout_OutputUnlock() call will not see and apply its change request.
582 * The other thread will need to apply the change request itself, which
583 * implies it is able to (try-)lock owner->lock. Therefore this thread must
584 * release owner->lock _before_ owner->req.lock. Do not reorder!!! */
585 vlc_mutex_unlock (&owner->req.lock);
589 * Gets the volume of the audio output stream (independent of mute).
590 * \return Current audio volume (0. = silent, 1. = nominal),
591 * or a strictly negative value if undefined.
593 float aout_VolumeGet (audio_output_t *aout)
595 return var_GetFloat (aout, "volume");
599 * Sets the volume of the audio output stream.
600 * \note The mute status is not changed.
601 * \return 0 on success, -1 on failure (TODO).
603 int aout_VolumeSet (audio_output_t *aout, float vol)
605 aout_owner_t *owner = aout_owner (aout);
608 vlc_mutex_lock (&owner->req.lock);
609 owner->req.volume = vol;
610 vlc_mutex_unlock (&owner->req.lock);
612 if (aout_OutputTryLock (aout) == 0)
613 aout_OutputUnlock (aout);
618 * Gets the audio output stream mute flag.
619 * \return 0 if not muted, 1 if muted, -1 if undefined.
621 int aout_MuteGet (audio_output_t *aout)
623 return var_InheritBool (aout, "mute");
627 * Sets the audio output stream mute flag.
628 * \return 0 on success, -1 on failure (TODO).
630 int aout_MuteSet (audio_output_t *aout, bool mute)
632 aout_owner_t *owner = aout_owner (aout);
634 vlc_mutex_lock (&owner->req.lock);
635 owner->req.mute = mute;
636 vlc_mutex_unlock (&owner->req.lock);
638 if (aout_OutputTryLock (aout) == 0)
639 aout_OutputUnlock (aout);
644 * Gets the currently selected device.
645 * \return the selected device ID (caller must free() it)
646 * NULL if no device is selected or in case of error.
648 char *aout_DeviceGet (audio_output_t *aout)
650 return var_GetNonEmptyString (aout, "device");
654 * Selects an audio output device.
655 * \param id device ID to select, or NULL for the default device
656 * \return zero on success, non-zero on error (TODO).
658 int aout_DeviceSet (audio_output_t *aout, const char *id)
660 aout_owner_t *owner = aout_owner (aout);
666 if (unlikely(dev == NULL))
670 vlc_mutex_lock (&owner->req.lock);
671 if (owner->req.device != unset_str)
672 free (owner->req.device);
673 owner->req.device = dev;
674 vlc_mutex_unlock (&owner->req.lock);
676 if (aout_OutputTryLock (aout) == 0)
677 aout_OutputUnlock (aout);
682 * Enumerates possible audio output devices.
684 * The function will heap-allocate two tables of heap-allocated strings;
685 * the caller is responsible for freeing all strings and both tables.
687 * \param ids pointer to a table of device identifiers [OUT]
688 * \param names pointer to a table of device human-readable descriptions [OUT]
689 * \return the number of devices, or negative on error.
690 * \note In case of error, *ids and *names are undefined.
692 int aout_DevicesList (audio_output_t *aout, char ***ids, char ***names)
694 aout_owner_t *owner = aout_owner (aout);
695 char **tabid, **tabname;
698 vlc_mutex_lock (&owner->dev.lock);
699 count = owner->dev.count;
700 tabid = xmalloc (sizeof (*tabid) * count);
701 tabname = xmalloc (sizeof (*tabname) * count);
704 for (aout_dev_t *dev = owner->dev.list; dev != NULL; dev = dev->next)
706 *(tabid++) = xstrdup (dev->id);
707 *(tabname++) = xstrdup (dev->name);
709 vlc_mutex_unlock (&owner->dev.lock);