chromecast: add assert
[vlc.git] / src / audio_output / output.c
blob0549de044da4925c1e7122381f46b0622edff7fd
1 /*****************************************************************************
2 * output.c : internal management of output streams for the audio output
3 *****************************************************************************
4 * Copyright (C) 2002-2004 VLC authors and VideoLAN
5 * $Id$
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 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <stdlib.h>
29 #include <assert.h>
31 #include <vlc_common.h>
32 #include <vlc_aout.h>
33 #include <vlc_modules.h>
35 #include "libvlc.h"
36 #include "aout_internal.h"
38 static const char unset_str[1] = ""; /* Non-NULL constant string pointer */
40 struct aout_dev
42 aout_dev_t *next;
43 char *name;
44 char id[1];
48 /* Local functions */
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);
67 static int var_CopyDevice (vlc_object_t *src, const char *name,
68 vlc_value_t prev, vlc_value_t value, void *data)
70 vlc_object_t *dst = data;
72 (void) src; (void) name; (void) prev;
73 return var_Set (dst, "audio-device", value);
76 /**
77 * Supply or update the current custom ("hardware") volume.
78 * @note This only makes sense after calling aout_VolumeHardInit().
79 * @param volume current custom volume
81 * @warning The caller (i.e. the audio output plug-in) is responsible for
82 * interlocking and synchronizing call to this function and to the
83 * audio_output_t.volume_set callback. This ensures that VLC gets correct
84 * volume information (possibly with a latency).
86 static void aout_VolumeNotify (audio_output_t *aout, float volume)
88 var_SetFloat (aout, "volume", volume);
91 static void aout_MuteNotify (audio_output_t *aout, bool mute)
93 var_SetBool (aout, "mute", mute);
96 static void aout_PolicyNotify (audio_output_t *aout, bool cork)
98 (cork ? var_IncInteger : var_DecInteger) (aout->obj.parent, "corks");
101 static void aout_DeviceNotify (audio_output_t *aout, const char *id)
103 var_SetString (aout, "device", (id != NULL) ? id : "");
106 static void aout_HotplugNotify (audio_output_t *aout,
107 const char *id, const char *name)
109 aout_owner_t *owner = aout_owner (aout);
110 aout_dev_t *dev, **pp = &owner->dev.list;
112 vlc_mutex_lock (&owner->dev.lock);
113 while ((dev = *pp) != NULL)
115 if (!strcmp (id, dev->id))
116 break;
117 pp = &dev->next;
120 if (name != NULL)
122 if (dev == NULL) /* Added device */
124 dev = malloc (sizeof (*dev) + strlen (id));
125 if (unlikely(dev == NULL))
126 goto out;
127 dev->next = NULL;
128 strcpy (dev->id, id);
129 *pp = dev;
130 owner->dev.count++;
132 else /* Modified device */
133 free (dev->name);
134 dev->name = strdup (name);
136 else
138 if (dev != NULL) /* Removed device */
140 owner->dev.count--;
141 *pp = dev->next;
142 free (dev->name);
143 free (dev);
146 out:
147 vlc_mutex_unlock (&owner->dev.lock);
150 static void aout_RestartNotify (audio_output_t *aout, unsigned mode)
152 aout_RequestRestart (aout, mode);
155 static int aout_GainNotify (audio_output_t *aout, float gain)
157 aout_owner_t *owner = aout_owner (aout);
159 aout_OutputAssertLocked (aout);
160 aout_volume_SetVolume (owner->volume, gain);
161 /* XXX: ideally, return -1 if format cannot be amplified */
162 return 0;
165 static int FilterCallback (vlc_object_t *obj, const char *var,
166 vlc_value_t prev, vlc_value_t cur, void *data)
168 if (strcmp(prev.psz_string, cur.psz_string))
169 aout_InputRequestRestart ((audio_output_t *)obj);
170 (void) var; (void) data;
171 return VLC_SUCCESS;
174 static int StereoModeCallback (vlc_object_t *obj, const char *varname,
175 vlc_value_t oldval, vlc_value_t newval, void *data)
177 audio_output_t *aout = (audio_output_t *)obj;
178 (void)varname; (void)oldval; (void)newval; (void)data;
180 aout_RestartRequest (aout, AOUT_RESTART_STEREOMODE);
181 return 0;
184 static int ViewpointCallback (vlc_object_t *obj, const char *var,
185 vlc_value_t prev, vlc_value_t cur, void *data)
187 if( cur.p_address != NULL )
188 aout_ChangeViewpoint((audio_output_t *)obj, cur.p_address );
189 (void) var; (void) data; (void) prev;
190 return VLC_SUCCESS;
193 #undef aout_New
195 * Creates an audio output object and initializes an output module.
197 audio_output_t *aout_New (vlc_object_t *parent)
199 vlc_value_t val, text;
201 audio_output_t *aout = vlc_custom_create (parent, sizeof (aout_instance_t),
202 "audio output");
203 if (unlikely(aout == NULL))
204 return NULL;
206 aout_owner_t *owner = aout_owner (aout);
208 vlc_mutex_init (&owner->lock);
209 vlc_mutex_init (&owner->req.lock);
210 vlc_mutex_init (&owner->dev.lock);
211 vlc_mutex_init (&owner->vp.lock);
212 vlc_viewpoint_init (&owner->vp.value);
213 atomic_init (&owner->vp.update, false);
214 owner->req.device = (char *)unset_str;
215 owner->req.volume = -1.f;
216 owner->req.mute = -1;
218 vlc_object_set_destructor (aout, aout_Destructor);
220 /* Audio output module callbacks */
221 var_Create (aout, "volume", VLC_VAR_FLOAT);
222 var_AddCallback (aout, "volume", var_Copy, parent);
223 var_Create (aout, "mute", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
224 var_AddCallback (aout, "mute", var_Copy, parent);
225 var_Create (aout, "device", VLC_VAR_STRING);
226 var_AddCallback (aout, "device", var_CopyDevice, parent);
227 /* TODO: 3.0 HACK: only way to signal DTS_HD to aout modules. */
228 var_Create (aout, "dtshd", VLC_VAR_BOOL);
230 aout->event.volume_report = aout_VolumeNotify;
231 aout->event.mute_report = aout_MuteNotify;
232 aout->event.policy_report = aout_PolicyNotify;
233 aout->event.device_report = aout_DeviceNotify;
234 aout->event.hotplug_report = aout_HotplugNotify;
235 aout->event.gain_request = aout_GainNotify;
236 aout->event.restart_request = aout_RestartNotify;
238 /* Audio output module initialization */
239 aout->start = NULL;
240 aout->stop = NULL;
241 aout->volume_set = NULL;
242 aout->mute_set = NULL;
243 aout->device_select = NULL;
244 owner->module = module_need (aout, "audio output", "$aout", false);
245 if (owner->module == NULL)
247 msg_Err (aout, "no suitable audio output module");
248 vlc_object_release (aout);
249 return NULL;
253 * Persistent audio output variables
255 module_config_t *cfg;
256 char *str;
258 /* Visualizations */
259 var_Create (aout, "visual", VLC_VAR_STRING);
260 text.psz_string = _("Visualizations");
261 var_Change (aout, "visual", VLC_VAR_SETTEXT, &text, NULL);
262 val.psz_string = (char *)"";
263 text.psz_string = _("Disable");
264 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
265 val.psz_string = (char *)"spectrometer";
266 text.psz_string = _("Spectrometer");
267 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
268 val.psz_string = (char *)"scope";
269 text.psz_string = _("Scope");
270 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
271 val.psz_string = (char *)"spectrum";
272 text.psz_string = _("Spectrum");
273 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
274 val.psz_string = (char *)"vuMeter";
275 text.psz_string = _("VU meter");
276 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
277 /* Look for goom plugin */
278 if (module_exists ("goom"))
280 val.psz_string = (char *)"goom";
281 text.psz_string = (char *)"Goom";
282 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
284 /* Look for libprojectM plugin */
285 if (module_exists ("projectm"))
287 val.psz_string = (char *)"projectm";
288 text.psz_string = (char*)"projectM";
289 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
291 /* Look for VSXu plugin */
292 if (module_exists ("vsxu"))
294 val.psz_string = (char *)"vsxu";
295 text.psz_string = (char*)"Vovoid VSXu";
296 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
298 /* Look for glspectrum plugin */
299 if (module_exists ("glspectrum"))
301 val.psz_string = (char *)"glspectrum";
302 text.psz_string = (char*)"3D spectrum";
303 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
305 str = var_GetNonEmptyString (aout, "effect-list");
306 if (str != NULL)
308 var_SetString (aout, "visual", str);
309 free (str);
312 var_Create (aout, "audio-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
313 var_AddCallback (aout, "audio-filter", FilterCallback, NULL);
314 text.psz_string = _("Audio filters");
315 var_Change (aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL);
317 var_Create (aout, "viewpoint", VLC_VAR_ADDRESS );
318 var_AddCallback (aout, "viewpoint", ViewpointCallback, NULL);
320 var_Create (aout, "audio-visual", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
321 text.psz_string = _("Audio visualizations");
322 var_Change (aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL);
324 /* Replay gain */
325 var_Create (aout, "audio-replay-gain-mode",
326 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
327 text.psz_string = _("Replay gain");
328 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL);
329 cfg = config_FindConfig("audio-replay-gain-mode");
330 if (likely(cfg != NULL))
331 for (unsigned i = 0; i < cfg->list_count; i++)
333 val.psz_string = (char *)cfg->list.psz[i];
334 text.psz_string = vlc_gettext(cfg->list_text[i]);
335 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
336 &val, &text);
339 /* Stereo mode */
340 var_Create (aout, "stereo-mode", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
341 owner->initial_stereo_mode = var_GetInteger (aout, "stereo-mode");
343 var_AddCallback (aout, "stereo-mode", StereoModeCallback, NULL);
344 vlc_value_t txt;
345 txt.psz_string = _("Stereo audio mode");
346 var_Change (aout, "stereo-mode", VLC_VAR_SETTEXT, &txt, NULL);
348 /* Equalizer */
349 var_Create (aout, "equalizer-preamp", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
350 var_Create (aout, "equalizer-bands", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
351 var_Create (aout, "equalizer-preset", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
353 return aout;
357 * Deinitializes an audio output module and destroys an audio output object.
359 void aout_Destroy (audio_output_t *aout)
361 aout_owner_t *owner = aout_owner (aout);
363 aout_OutputLock (aout);
364 module_unneed (aout, owner->module);
365 /* Protect against late call from intf.c */
366 aout->volume_set = NULL;
367 aout->mute_set = NULL;
368 aout->device_select = NULL;
369 aout_OutputUnlock (aout);
371 var_DelCallback (aout, "viewpoint", ViewpointCallback, NULL);
372 var_DelCallback (aout, "audio-filter", FilterCallback, NULL);
373 var_DelCallback (aout, "device", var_CopyDevice, aout->obj.parent);
374 var_DelCallback (aout, "mute", var_Copy, aout->obj.parent);
375 var_SetFloat (aout, "volume", -1.f);
376 var_DelCallback (aout, "volume", var_Copy, aout->obj.parent);
377 var_DelCallback (aout, "stereo-mode", StereoModeCallback, NULL);
378 vlc_object_release (aout);
382 * Destroys the audio output lock used (asynchronously) by interface functions.
384 static void aout_Destructor (vlc_object_t *obj)
386 audio_output_t *aout = (audio_output_t *)obj;
387 aout_owner_t *owner = aout_owner (aout);
389 vlc_mutex_destroy (&owner->dev.lock);
390 for (aout_dev_t *dev = owner->dev.list, *next; dev != NULL; dev = next)
392 next = dev->next;
393 free (dev->name);
394 free (dev);
397 assert (owner->req.device == unset_str);
398 vlc_mutex_destroy (&owner->vp.lock);
399 vlc_mutex_destroy (&owner->req.lock);
400 vlc_mutex_destroy (&owner->lock);
403 static void aout_PrepareStereoMode (audio_output_t *aout,
404 audio_sample_format_t *restrict fmt,
405 aout_filters_cfg_t *filters_cfg,
406 audio_channel_type_t input_chan_type,
407 unsigned i_nb_input_channels,
408 int i_forced_stereo_mode)
410 /* Fill Stereo mode choices */
411 var_Change (aout, "stereo-mode", VLC_VAR_CLEARCHOICES, NULL, NULL);
412 vlc_value_t val, txt, default_val = { .i_int = AOUT_VAR_CHAN_UNSET };
413 val.i_int = 0;
415 if (!AOUT_FMT_LINEAR(fmt))
416 return;
418 if (i_nb_input_channels > 1)
420 val.i_int = AOUT_VAR_CHAN_MONO;
421 txt.psz_string = _("Mono");
422 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
425 if (i_nb_input_channels != 2)
427 val.i_int = AOUT_VAR_CHAN_UNSET;
428 txt.psz_string = _("Original");
429 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
431 if (fmt->i_chan_mode & AOUT_CHANMODE_DOLBYSTEREO)
433 val.i_int = AOUT_VAR_CHAN_DOLBYS;
434 txt.psz_string = _("Dolby Surround");
436 else
438 val.i_int = AOUT_VAR_CHAN_STEREO;
439 txt.psz_string = _("Stereo");
441 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
443 if (i_nb_input_channels == 2)
445 default_val.i_int = val.i_int; /* Stereo or Dolby Surround */
447 val.i_int = AOUT_VAR_CHAN_LEFT;
448 txt.psz_string = _("Left");
449 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
450 val.i_int = AOUT_VAR_CHAN_RIGHT;
451 txt.psz_string = _("Right");
452 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
454 val.i_int = AOUT_VAR_CHAN_RSTEREO;
455 txt.psz_string = _("Reverse stereo");
456 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
459 if (input_chan_type == AUDIO_CHANNEL_TYPE_AMBISONICS
460 || i_nb_input_channels > 2)
462 val.i_int = AOUT_VAR_CHAN_HEADPHONES;
463 txt.psz_string = _("Headphones");
464 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
466 if (i_forced_stereo_mode == AOUT_VAR_CHAN_UNSET
467 && aout->current_sink_info.headphones)
469 i_forced_stereo_mode = AOUT_VAR_CHAN_HEADPHONES;
470 default_val.i_int = val.i_int;
471 var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &default_val,
472 NULL);
476 /* The user may have selected a different channels configuration. */
477 switch (i_forced_stereo_mode)
479 case AOUT_VAR_CHAN_RSTEREO:
480 filters_cfg->remap[AOUT_CHANIDX_LEFT] = AOUT_CHANIDX_RIGHT;
481 filters_cfg->remap[AOUT_CHANIDX_RIGHT] = AOUT_CHANIDX_LEFT;
482 break;
483 case AOUT_VAR_CHAN_STEREO:
484 break;
485 case AOUT_VAR_CHAN_LEFT:
486 filters_cfg->remap[AOUT_CHANIDX_RIGHT] = AOUT_CHANIDX_DISABLE;
487 break;
488 case AOUT_VAR_CHAN_RIGHT:
489 filters_cfg->remap[AOUT_CHANIDX_LEFT] = AOUT_CHANIDX_DISABLE;
490 break;
491 case AOUT_VAR_CHAN_DOLBYS:
492 fmt->i_chan_mode = AOUT_CHANMODE_DOLBYSTEREO;
493 break;
494 case AOUT_VAR_CHAN_HEADPHONES:
495 filters_cfg->headphones = true;
496 break;
497 case AOUT_VAR_CHAN_MONO:
498 /* Remix all channels into one */
499 for (size_t i = 0; i < AOUT_CHANIDX_MAX; ++ i)
500 filters_cfg->remap[i] = AOUT_CHANIDX_LEFT;
501 break;
502 default:
503 if (i_nb_input_channels == 2
504 && fmt->i_chan_mode & AOUT_CHANMODE_DUALMONO)
505 { /* Go directly to the left channel. */
506 filters_cfg->remap[AOUT_CHANIDX_RIGHT] = AOUT_CHANIDX_DISABLE;
507 default_val.i_int = val.i_int = AOUT_VAR_CHAN_LEFT;
509 var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &default_val,
510 NULL);
511 break;
516 * Starts an audio output stream.
517 * \param fmt audio output stream format [IN/OUT]
518 * \warning The caller must hold the audio output lock.
520 int aout_OutputNew (audio_output_t *aout, audio_sample_format_t *restrict fmt,
521 aout_filters_cfg_t *filters_cfg)
523 aout_OutputAssertLocked (aout);
525 audio_channel_type_t input_chan_type = fmt->channel_type;
526 int i_forced_stereo_mode = AOUT_VAR_CHAN_UNSET;
527 unsigned i_nb_input_channels = fmt->i_channels;
529 /* Ideally, the audio filters would be created before the audio output,
530 * and the ideal audio format would be the output of the filters chain.
531 * But that scheme would not really play well with digital pass-through. */
532 if (AOUT_FMT_LINEAR(fmt))
534 if (fmt->channel_type == AUDIO_CHANNEL_TYPE_BITMAP
535 && aout_FormatNbChannels(fmt) == 0)
537 /* The output channel map is unknown, use the WAVE one. */
538 assert(fmt->i_channels > 0);
539 aout_SetWavePhysicalChannels(fmt);
542 if (fmt->channel_type == AUDIO_CHANNEL_TYPE_AMBISONICS)
544 /* Set the maximum of channels to render ambisonics contents. The
545 * aout module will still be free to select less channels in order
546 * to respect the sink setup. */
547 fmt->i_physical_channels = AOUT_CHANS_7_1;
550 /* Try to stay in integer domain if possible for no/slow FPU. */
551 fmt->i_format = (fmt->i_bitspersample > 16) ? VLC_CODEC_FL32
552 : VLC_CODEC_S16N;
554 i_forced_stereo_mode = var_GetInteger (aout, "stereo-mode");
555 if (i_forced_stereo_mode != AOUT_VAR_CHAN_UNSET)
557 if (i_forced_stereo_mode == AOUT_VAR_CHAN_LEFT
558 || i_forced_stereo_mode == AOUT_VAR_CHAN_RIGHT)
559 fmt->i_physical_channels = AOUT_CHAN_CENTER;
560 else
561 fmt->i_physical_channels = AOUT_CHANS_STEREO;
564 aout_FormatPrepare (fmt);
565 assert (aout_FormatNbChannels(fmt) > 0);
568 aout->current_sink_info.headphones = false;
570 if (aout->start (aout, fmt))
572 msg_Err (aout, "module not functional");
573 return -1;
576 aout_PrepareStereoMode (aout, fmt, filters_cfg, input_chan_type,
577 i_nb_input_channels, i_forced_stereo_mode);
579 aout_FormatPrepare (fmt);
580 assert (fmt->i_bytes_per_frame > 0 && fmt->i_frame_length > 0);
581 aout_FormatPrint (aout, "output", fmt);
582 return 0;
586 * Stops the audio output stream (undoes aout_OutputNew()).
587 * \note This can only be called after a successful aout_OutputNew().
588 * \warning The caller must hold the audio output lock.
590 void aout_OutputDelete (audio_output_t *aout)
592 aout_OutputAssertLocked (aout);
594 if (aout->stop != NULL)
595 aout->stop (aout);
598 int aout_OutputTimeGet (audio_output_t *aout, mtime_t *delay)
600 aout_OutputAssertLocked (aout);
602 if (aout->time_get == NULL)
603 return -1;
604 return aout->time_get (aout, delay);
608 * Plays a decoded audio buffer.
609 * \note This can only be called after a successful aout_OutputNew().
610 * \warning The caller must hold the audio output lock.
612 void aout_OutputPlay (audio_output_t *aout, block_t *block)
614 aout_OutputAssertLocked (aout);
615 #ifndef NDEBUG
616 aout_owner_t *owner = aout_owner (aout);
617 assert (owner->mixer_format.i_frame_length > 0);
618 assert (block->i_buffer == 0 || block->i_buffer / block->i_nb_samples ==
619 owner->mixer_format.i_bytes_per_frame /
620 owner->mixer_format.i_frame_length);
621 #endif
622 aout->play (aout, block);
625 static void PauseDefault (audio_output_t *aout, bool pause, mtime_t date)
627 if (pause)
628 aout_OutputFlush (aout, false);
629 (void) date;
633 * Notifies the audio output (if any) of pause/resume events.
634 * This enables the output to expedite pause, instead of waiting for its
635 * buffers to drain.
636 * \note This can only be called after a successful aout_OutputNew().
637 * \warning The caller must hold the audio output lock.
639 void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
641 aout_OutputAssertLocked (aout);
642 ((aout->pause != NULL) ? aout->pause : PauseDefault) (aout, pause, date);
646 * Flushes or drains the audio output buffers.
647 * This enables the output to expedite seek and stop.
648 * \param wait if true, wait for buffer playback (i.e. drain),
649 * if false, discard the buffers immediately (i.e. flush)
650 * \note This can only be called after a successful aout_OutputNew().
651 * \warning The caller must hold the audio output lock.
653 void aout_OutputFlush( audio_output_t *aout, bool wait )
655 aout_OutputAssertLocked( aout );
656 aout->flush (aout, wait);
659 static int aout_OutputVolumeSet (audio_output_t *aout, float vol)
661 aout_OutputAssertLocked (aout);
662 return (aout->volume_set != NULL) ? aout->volume_set (aout, vol) : -1;
665 static int aout_OutputMuteSet (audio_output_t *aout, bool mute)
667 aout_OutputAssertLocked (aout);
668 return (aout->mute_set != NULL) ? aout->mute_set (aout, mute) : -1;
671 static int aout_OutputDeviceSet (audio_output_t *aout, const char *id)
673 aout_OutputAssertLocked (aout);
674 return (aout->device_select != NULL) ? aout->device_select (aout, id) : -1;
677 void aout_OutputLock (audio_output_t *aout)
679 aout_owner_t *owner = aout_owner (aout);
681 vlc_mutex_lock (&owner->lock);
684 static int aout_OutputTryLock (audio_output_t *aout)
686 aout_owner_t *owner = aout_owner (aout);
688 return vlc_mutex_trylock (&owner->lock);
691 void aout_OutputUnlock (audio_output_t *aout)
693 aout_owner_t *owner = aout_owner (aout);
695 vlc_assert_locked (&owner->lock);
696 vlc_mutex_lock (&owner->req.lock);
698 if (owner->req.device != unset_str)
700 aout_OutputDeviceSet (aout, owner->req.device);
701 free (owner->req.device);
702 owner->req.device = (char *)unset_str;
705 if (owner->req.volume >= 0.f)
707 aout_OutputVolumeSet (aout, owner->req.volume);
708 owner->req.volume = -1.f;
711 if (owner->req.mute >= 0)
713 aout_OutputMuteSet (aout, owner->req.mute);
714 owner->req.mute = -1;
717 vlc_mutex_unlock (&owner->lock);
718 /* If another thread is blocked waiting for owner->req.lock at this point,
719 * this aout_OutputUnlock() call will not see and apply its change request.
720 * The other thread will need to apply the change request itself, which
721 * implies it is able to (try-)lock owner->lock. Therefore this thread must
722 * release owner->lock _before_ owner->req.lock. Do not reorder!!! */
723 vlc_mutex_unlock (&owner->req.lock);
727 * Gets the volume of the audio output stream (independent of mute).
728 * \return Current audio volume (0. = silent, 1. = nominal),
729 * or a strictly negative value if undefined.
731 float aout_VolumeGet (audio_output_t *aout)
733 return var_GetFloat (aout, "volume");
737 * Sets the volume of the audio output stream.
738 * \note The mute status is not changed.
739 * \return 0 on success, -1 on failure (TODO).
741 int aout_VolumeSet (audio_output_t *aout, float vol)
743 aout_owner_t *owner = aout_owner (aout);
745 assert (vol >= 0.f);
746 vlc_mutex_lock (&owner->req.lock);
747 owner->req.volume = vol;
748 vlc_mutex_unlock (&owner->req.lock);
750 if (aout_OutputTryLock (aout) == 0)
751 aout_OutputUnlock (aout);
752 return 0;
756 * Raises the volume.
757 * \param value how much to increase (> 0) or decrease (< 0) the volume
758 * \param volp if non-NULL, will contain contain the resulting volume
760 int aout_VolumeUpdate (audio_output_t *aout, int value, float *volp)
762 int ret = -1;
763 float stepSize = var_InheritFloat (aout, "volume-step") / (float)AOUT_VOLUME_DEFAULT;
764 float delta = value * stepSize;
765 float vol = aout_VolumeGet (aout);
767 if (vol >= 0.f)
769 vol += delta;
770 if (vol < 0.f)
771 vol = 0.f;
772 if (vol > 2.f)
773 vol = 2.f;
774 vol = (roundf (vol / stepSize)) * stepSize;
775 if (volp != NULL)
776 *volp = vol;
777 ret = aout_VolumeSet (aout, vol);
779 return ret;
783 * Gets the audio output stream mute flag.
784 * \return 0 if not muted, 1 if muted, -1 if undefined.
786 int aout_MuteGet (audio_output_t *aout)
788 return var_InheritBool (aout, "mute");
792 * Sets the audio output stream mute flag.
793 * \return 0 on success, -1 on failure (TODO).
795 int aout_MuteSet (audio_output_t *aout, bool mute)
797 aout_owner_t *owner = aout_owner (aout);
799 vlc_mutex_lock (&owner->req.lock);
800 owner->req.mute = mute;
801 vlc_mutex_unlock (&owner->req.lock);
803 if (aout_OutputTryLock (aout) == 0)
804 aout_OutputUnlock (aout);
805 return 0;
809 * Gets the currently selected device.
810 * \return the selected device ID (caller must free() it)
811 * NULL if no device is selected or in case of error.
813 char *aout_DeviceGet (audio_output_t *aout)
815 return var_GetNonEmptyString (aout, "device");
819 * Selects an audio output device.
820 * \param id device ID to select, or NULL for the default device
821 * \return zero on success, non-zero on error (TODO).
823 int aout_DeviceSet (audio_output_t *aout, const char *id)
825 aout_owner_t *owner = aout_owner (aout);
827 char *dev = NULL;
828 if (id != NULL)
830 dev = strdup (id);
831 if (unlikely(dev == NULL))
832 return -1;
835 vlc_mutex_lock (&owner->req.lock);
836 if (owner->req.device != unset_str)
837 free (owner->req.device);
838 owner->req.device = dev;
839 vlc_mutex_unlock (&owner->req.lock);
841 if (aout_OutputTryLock (aout) == 0)
842 aout_OutputUnlock (aout);
843 return 0;
847 * Enumerates possible audio output devices.
849 * The function will heap-allocate two tables of heap-allocated strings;
850 * the caller is responsible for freeing all strings and both tables.
852 * \param ids pointer to a table of device identifiers [OUT]
853 * \param names pointer to a table of device human-readable descriptions [OUT]
854 * \return the number of devices, or negative on error.
855 * \note In case of error, *ids and *names are undefined.
857 int aout_DevicesList (audio_output_t *aout, char ***ids, char ***names)
859 aout_owner_t *owner = aout_owner (aout);
860 char **tabid, **tabname;
861 unsigned i = 0;
863 vlc_mutex_lock (&owner->dev.lock);
864 tabid = vlc_alloc (owner->dev.count, sizeof (*tabid));
865 tabname = vlc_alloc (owner->dev.count, sizeof (*tabname));
867 if (unlikely(tabid == NULL || tabname == NULL))
868 goto error;
870 *ids = tabid;
871 *names = tabname;
873 for (aout_dev_t *dev = owner->dev.list; dev != NULL; dev = dev->next)
875 tabid[i] = strdup(dev->id);
876 if (unlikely(tabid[i] == NULL))
877 goto error;
879 tabname[i] = strdup(dev->name);
880 if (unlikely(tabname[i] == NULL))
882 free(tabid[i]);
883 goto error;
886 i++;
888 vlc_mutex_unlock (&owner->dev.lock);
890 return i;
892 error:
893 vlc_mutex_unlock(&owner->dev.lock);
894 while (i > 0)
896 i--;
897 free(tabname[i]);
898 free(tabid[i]);
900 free(tabname);
901 free(tabid);
902 return -1;