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 str = var_GetNonEmptyString (aout, "effect-list");
258 var_SetString (aout, "visual", str);
263 var_Create (aout, "equalizer", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
264 text.psz_string = _("Equalizer");
265 var_Change (aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL);
266 val.psz_string = (char*)"";
267 text.psz_string = _("Disable");
268 var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
269 cfg = config_FindConfig (VLC_OBJECT(aout), "equalizer-preset");
270 if (likely(cfg != NULL))
271 for (unsigned i = 0; i < cfg->list_count; i++)
273 val.psz_string = cfg->list.psz[i];
274 text.psz_string = vlc_gettext(cfg->list_text[i]);
275 var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
278 var_Create (aout, "audio-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
279 text.psz_string = _("Audio filters");
280 var_Change (aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL);
283 var_Create (aout, "audio-visual", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
284 text.psz_string = _("Audio visualizations");
285 var_Change (aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL);
288 var_Create (aout, "audio-replay-gain-mode",
289 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
290 text.psz_string = _("Replay gain");
291 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL);
292 cfg = config_FindConfig (VLC_OBJECT(aout), "audio-replay-gain-mode");
293 if (likely(cfg != NULL))
294 for (unsigned i = 0; i < cfg->list_count; i++)
296 val.psz_string = cfg->list.psz[i];
297 text.psz_string = vlc_gettext(cfg->list_text[i]);
298 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
306 * Deinitializes an audio output module and destroys an audio output object.
308 void aout_Destroy (audio_output_t *aout)
310 aout_owner_t *owner = aout_owner (aout);
312 aout_OutputLock (aout);
313 module_unneed (aout, owner->module);
314 /* Protect against late call from intf.c */
315 aout->volume_set = NULL;
316 aout->mute_set = NULL;
317 aout_OutputUnlock (aout);
319 var_DelCallback (aout, "mute", var_Copy, aout->p_parent);
320 var_SetFloat (aout, "volume", -1.f);
321 var_DelCallback (aout, "volume", var_Copy, aout->p_parent);
322 vlc_object_release (aout);
326 * Destroys the audio output lock used (asynchronously) by interface functions.
328 static void aout_Destructor (vlc_object_t *obj)
330 audio_output_t *aout = (audio_output_t *)obj;
331 aout_owner_t *owner = aout_owner (aout);
333 vlc_mutex_destroy (&owner->dev.lock);
334 for (aout_dev_t *dev = owner->dev.list, *next; dev != NULL; dev = next)
341 assert (owner->req.device == unset_str);
342 vlc_mutex_destroy (&owner->req.lock);
343 vlc_mutex_destroy (&owner->lock);
347 * Starts an audio output stream.
348 * \param fmt audio output stream format [IN/OUT]
349 * \warning The caller must hold the audio output lock.
351 int aout_OutputNew (audio_output_t *aout, audio_sample_format_t *restrict fmt)
353 aout_OutputAssertLocked (aout);
355 /* Ideally, the audio filters would be created before the audio output,
356 * and the ideal audio format would be the output of the filters chain.
357 * But that scheme would not really play well with digital pass-through. */
358 if (AOUT_FMT_LINEAR(fmt))
359 { /* Try to stay in integer domain if possible for no/slow FPU. */
360 fmt->i_format = (fmt->i_bitspersample > 16) ? VLC_CODEC_FL32
362 aout_FormatPrepare (fmt);
365 if (aout->start (aout, fmt))
367 msg_Err (aout, "module not functional");
371 if (!var_Type (aout, "stereo-mode"))
372 var_Create (aout, "stereo-mode",
373 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT);
375 /* The user may have selected a different channels configuration. */
376 var_AddCallback (aout, "stereo-mode", aout_ChannelsRestart, NULL);
377 switch (var_GetInteger (aout, "stereo-mode"))
379 case AOUT_VAR_CHAN_RSTEREO:
380 fmt->i_original_channels |= AOUT_CHAN_REVERSESTEREO;
382 case AOUT_VAR_CHAN_STEREO:
383 fmt->i_original_channels = AOUT_CHANS_STEREO;
385 case AOUT_VAR_CHAN_LEFT:
386 fmt->i_original_channels = AOUT_CHAN_LEFT;
388 case AOUT_VAR_CHAN_RIGHT:
389 fmt->i_original_channels = AOUT_CHAN_RIGHT;
391 case AOUT_VAR_CHAN_DOLBYS:
392 fmt->i_original_channels = AOUT_CHANS_STEREO|AOUT_CHAN_DOLBYSTEREO;
396 if ((fmt->i_original_channels & AOUT_CHAN_PHYSMASK)
397 != AOUT_CHANS_STEREO)
400 vlc_value_t val, txt;
402 var_Change (aout, "stereo-mode", VLC_VAR_DELCHOICE, &val, NULL);
403 txt.psz_string = _("Stereo audio mode");
404 var_Change (aout, "stereo-mode", VLC_VAR_SETTEXT, &txt, NULL);
405 if (fmt->i_original_channels & AOUT_CHAN_DOLBYSTEREO)
407 val.i_int = AOUT_VAR_CHAN_DOLBYS;
408 txt.psz_string = _("Dolby Surround");
412 val.i_int = AOUT_VAR_CHAN_STEREO;
413 txt.psz_string = _("Stereo");
415 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
416 var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &val, NULL);
417 val.i_int = AOUT_VAR_CHAN_LEFT;
418 txt.psz_string = _("Left");
419 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
420 if (fmt->i_original_channels & AOUT_CHAN_DUALMONO)
421 { /* Go directly to the left channel. */
422 fmt->i_original_channels = AOUT_CHAN_LEFT;
423 var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &val, NULL);
425 val.i_int = AOUT_VAR_CHAN_RIGHT;
426 txt.psz_string = _("Right");
427 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
428 val.i_int = AOUT_VAR_CHAN_RSTEREO;
429 txt.psz_string = _("Reverse stereo");
430 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
434 aout_FormatPrepare (fmt);
435 aout_FormatPrint (aout, "output", fmt);
440 * Stops the audio output stream (undoes aout_OutputNew()).
441 * \note This can only be called after a succesful aout_OutputNew().
442 * \warning The caller must hold the audio output lock.
444 void aout_OutputDelete (audio_output_t *aout)
446 aout_OutputAssertLocked (aout);
448 var_DelCallback (aout, "stereo-mode", aout_ChannelsRestart, NULL);
449 if (aout->stop != NULL)
453 int aout_OutputTimeGet (audio_output_t *aout, mtime_t *delay)
455 aout_OutputAssertLocked (aout);
457 if (aout->time_get == NULL)
459 return aout->time_get (aout, delay);
463 * Plays a decoded audio buffer.
464 * \note This can only be called after a succesful aout_OutputNew().
465 * \warning The caller must hold the audio output lock.
467 void aout_OutputPlay (audio_output_t *aout, block_t *block)
469 aout_OutputAssertLocked (aout);
470 aout->play (aout, block);
473 static void PauseDefault (audio_output_t *aout, bool pause, mtime_t date)
476 aout_OutputFlush (aout, false);
481 * Notifies the audio output (if any) of pause/resume events.
482 * This enables the output to expedite pause, instead of waiting for its
484 * \note This can only be called after a succesful aout_OutputNew().
485 * \warning The caller must hold the audio output lock.
487 void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
489 aout_OutputAssertLocked (aout);
490 ((aout->pause != NULL) ? aout->pause : PauseDefault) (aout, pause, date);
494 * Flushes or drains the audio output buffers.
495 * This enables the output to expedite seek and stop.
496 * \param wait if true, wait for buffer playback (i.e. drain),
497 * if false, discard the buffers immediately (i.e. flush)
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_OutputFlush( audio_output_t *aout, bool wait )
503 aout_OutputAssertLocked( aout );
504 aout->flush (aout, wait);
507 static int aout_OutputVolumeSet (audio_output_t *aout, float vol)
509 aout_OutputAssertLocked (aout);
510 return (aout->volume_set != NULL) ? aout->volume_set (aout, vol) : -1;
513 static int aout_OutputMuteSet (audio_output_t *aout, bool mute)
515 aout_OutputAssertLocked (aout);
516 return (aout->mute_set != NULL) ? aout->mute_set (aout, mute) : -1;
519 static int aout_OutputDeviceSet (audio_output_t *aout, const char *id)
521 aout_OutputAssertLocked (aout);
522 return (aout->device_select != NULL) ? aout->device_select (aout, id) : -1;
525 void aout_OutputLock (audio_output_t *aout)
527 aout_owner_t *owner = aout_owner (aout);
529 vlc_mutex_lock (&owner->lock);
532 static int aout_OutputTryLock (audio_output_t *aout)
534 aout_owner_t *owner = aout_owner (aout);
536 return vlc_mutex_trylock (&owner->lock);
539 void aout_OutputUnlock (audio_output_t *aout)
541 aout_owner_t *owner = aout_owner (aout);
543 vlc_assert_locked (&owner->lock);
544 vlc_mutex_lock (&owner->req.lock);
546 if (owner->req.device != unset_str)
548 aout_OutputDeviceSet (aout, owner->req.device);
549 free (owner->req.device);
550 owner->req.device = (char *)unset_str;
553 if (owner->req.volume >= 0.f)
555 aout_OutputVolumeSet (aout, owner->req.volume);
556 owner->req.volume = -1.f;
559 if (owner->req.mute >= 0)
561 aout_OutputMuteSet (aout, owner->req.mute);
562 owner->req.mute = -1;
565 vlc_mutex_unlock (&owner->lock);
566 /* If another thread is blocked waiting for owner->req.lock at this point,
567 * this aout_OutputUnlock() call will not see and apply its change request.
568 * The other thread will need to apply the change request itself, which
569 * implies it is able to (try-)lock owner->lock. Therefore this thread must
570 * release owner->lock _before_ owner->req.lock. Do not reorder!!! */
571 vlc_mutex_unlock (&owner->req.lock);
575 * Gets the volume of the audio output stream (independent of mute).
576 * \return Current audio volume (0. = silent, 1. = nominal),
577 * or a strictly negative value if undefined.
579 float aout_VolumeGet (audio_output_t *aout)
581 return var_GetFloat (aout, "volume");
585 * Sets the volume of the audio output stream.
586 * \note The mute status is not changed.
587 * \return 0 on success, -1 on failure (TODO).
589 int aout_VolumeSet (audio_output_t *aout, float vol)
591 aout_owner_t *owner = aout_owner (aout);
594 vlc_mutex_lock (&owner->req.lock);
595 owner->req.volume = vol;
596 vlc_mutex_unlock (&owner->req.lock);
598 if (aout_OutputTryLock (aout) == 0)
599 aout_OutputUnlock (aout);
604 * Gets the audio output stream mute flag.
605 * \return 0 if not muted, 1 if muted, -1 if undefined.
607 int aout_MuteGet (audio_output_t *aout)
609 return var_InheritBool (aout, "mute");
613 * Sets the audio output stream mute flag.
614 * \return 0 on success, -1 on failure (TODO).
616 int aout_MuteSet (audio_output_t *aout, bool mute)
618 aout_owner_t *owner = aout_owner (aout);
620 vlc_mutex_lock (&owner->req.lock);
621 owner->req.mute = mute;
622 vlc_mutex_unlock (&owner->req.lock);
624 if (aout_OutputTryLock (aout) == 0)
625 aout_OutputUnlock (aout);
630 * Gets the currently selected device.
631 * \return the selected device ID (caller must free() it)
632 * NULL if no device is selected or in case of error.
634 char *aout_DeviceGet (audio_output_t *aout)
636 return var_GetNonEmptyString (aout, "device");
640 * Selects an audio output device.
641 * \param id device ID to select, or NULL for the default device
642 * \return zero on success, non-zero on error (TODO).
644 int aout_DeviceSet (audio_output_t *aout, const char *id)
646 aout_owner_t *owner = aout_owner (aout);
652 if (unlikely(dev == NULL))
656 vlc_mutex_lock (&owner->req.lock);
657 if (owner->req.device != unset_str)
658 free (owner->req.device);
659 owner->req.device = dev;
660 vlc_mutex_unlock (&owner->req.lock);
662 if (aout_OutputTryLock (aout) == 0)
663 aout_OutputUnlock (aout);
668 * Enumerates possible audio output devices.
670 * The function will heap-allocate two tables of heap-allocated strings;
671 * the caller is responsible for freeing all strings and both tables.
673 * \param ids pointer to a table of device identifiers [OUT]
674 * \param names pointer to a table of device human-readable descriptions [OUT]
675 * \return the number of devices, or negative on error.
676 * \note In case of error, *ids and *names are undefined.
678 int aout_DevicesList (audio_output_t *aout, char ***ids, char ***names)
680 aout_owner_t *owner = aout_owner (aout);
681 char **tabid, **tabname;
684 vlc_mutex_lock (&owner->dev.lock);
685 count = owner->dev.count;
686 tabid = xmalloc (sizeof (*tabid) * count);
687 tabname = xmalloc (sizeof (*tabname) * count);
690 for (aout_dev_t *dev = owner->dev.list; dev != NULL; dev = dev->next)
692 *(tabid++) = xstrdup (dev->id);
693 *(tabname++) = xstrdup (dev->name);
695 vlc_mutex_unlock (&owner->dev.lock);