es: replace i_original_channels with i_chan_mode
[vlc.git] / src / audio_output / output.c
blobe362b81e83888d56705d4a9db227d65208f2eecd
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_OUTPUT);
181 return 0;
184 #undef aout_New
186 * Creates an audio output object and initializes an output module.
188 audio_output_t *aout_New (vlc_object_t *parent)
190 vlc_value_t val, text;
192 audio_output_t *aout = vlc_custom_create (parent, sizeof (aout_instance_t),
193 "audio output");
194 if (unlikely(aout == NULL))
195 return NULL;
197 aout_owner_t *owner = aout_owner (aout);
199 vlc_mutex_init (&owner->lock);
200 vlc_mutex_init (&owner->req.lock);
201 vlc_mutex_init (&owner->dev.lock);
202 owner->req.device = (char *)unset_str;
203 owner->req.volume = -1.f;
204 owner->req.mute = -1;
206 vlc_object_set_destructor (aout, aout_Destructor);
208 /* Audio output module callbacks */
209 var_Create (aout, "volume", VLC_VAR_FLOAT);
210 var_AddCallback (aout, "volume", var_Copy, parent);
211 var_Create (aout, "mute", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
212 var_AddCallback (aout, "mute", var_Copy, parent);
213 var_Create (aout, "device", VLC_VAR_STRING);
214 var_AddCallback (aout, "device", var_CopyDevice, parent);
216 aout->event.volume_report = aout_VolumeNotify;
217 aout->event.mute_report = aout_MuteNotify;
218 aout->event.policy_report = aout_PolicyNotify;
219 aout->event.device_report = aout_DeviceNotify;
220 aout->event.hotplug_report = aout_HotplugNotify;
221 aout->event.gain_request = aout_GainNotify;
222 aout->event.restart_request = aout_RestartNotify;
224 /* Audio output module initialization */
225 aout->start = NULL;
226 aout->stop = NULL;
227 aout->volume_set = NULL;
228 aout->mute_set = NULL;
229 aout->device_select = NULL;
230 owner->module = module_need (aout, "audio output", "$aout", false);
231 if (owner->module == NULL)
233 msg_Err (aout, "no suitable audio output module");
234 vlc_object_release (aout);
235 return NULL;
239 * Persistent audio output variables
241 module_config_t *cfg;
242 char *str;
244 /* Visualizations */
245 var_Create (aout, "visual", VLC_VAR_STRING);
246 text.psz_string = _("Visualizations");
247 var_Change (aout, "visual", VLC_VAR_SETTEXT, &text, NULL);
248 val.psz_string = (char *)"";
249 text.psz_string = _("Disable");
250 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
251 val.psz_string = (char *)"spectrometer";
252 text.psz_string = _("Spectrometer");
253 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
254 val.psz_string = (char *)"scope";
255 text.psz_string = _("Scope");
256 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
257 val.psz_string = (char *)"spectrum";
258 text.psz_string = _("Spectrum");
259 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
260 val.psz_string = (char *)"vuMeter";
261 text.psz_string = _("VU meter");
262 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
263 /* Look for goom plugin */
264 if (module_exists ("goom"))
266 val.psz_string = (char *)"goom";
267 text.psz_string = (char *)"Goom";
268 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
270 /* Look for libprojectM plugin */
271 if (module_exists ("projectm"))
273 val.psz_string = (char *)"projectm";
274 text.psz_string = (char*)"projectM";
275 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
277 /* Look for VSXu plugin */
278 if (module_exists ("vsxu"))
280 val.psz_string = (char *)"vsxu";
281 text.psz_string = (char*)"Vovoid VSXu";
282 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
284 /* Look for glspectrum plugin */
285 if (module_exists ("glspectrum"))
287 val.psz_string = (char *)"glspectrum";
288 text.psz_string = (char*)"3D spectrum";
289 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
291 str = var_GetNonEmptyString (aout, "effect-list");
292 if (str != NULL)
294 var_SetString (aout, "visual", str);
295 free (str);
298 var_Create (aout, "audio-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
299 var_AddCallback (aout, "audio-filter", FilterCallback, NULL);
300 text.psz_string = _("Audio filters");
301 var_Change (aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL);
304 var_Create (aout, "audio-visual", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
305 text.psz_string = _("Audio visualizations");
306 var_Change (aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL);
308 /* Replay gain */
309 var_Create (aout, "audio-replay-gain-mode",
310 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
311 text.psz_string = _("Replay gain");
312 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL);
313 cfg = config_FindConfig (VLC_OBJECT(aout), "audio-replay-gain-mode");
314 if (likely(cfg != NULL))
315 for (unsigned i = 0; i < cfg->list_count; i++)
317 val.psz_string = (char *)cfg->list.psz[i];
318 text.psz_string = vlc_gettext(cfg->list_text[i]);
319 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
320 &val, &text);
323 /* Stereo mode */
324 var_Create (aout, "stereo-mode", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
325 var_AddCallback (aout, "stereo-mode", StereoModeCallback, NULL);
326 vlc_value_t txt;
327 txt.psz_string = _("Stereo audio mode");
328 var_Change (aout, "stereo-mode", VLC_VAR_SETTEXT, &txt, NULL);
330 /* Equalizer */
331 var_Create (aout, "equalizer-preamp", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
332 var_Create (aout, "equalizer-bands", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
333 var_Create (aout, "equalizer-preset", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
335 return aout;
339 * Deinitializes an audio output module and destroys an audio output object.
341 void aout_Destroy (audio_output_t *aout)
343 aout_owner_t *owner = aout_owner (aout);
345 aout_OutputLock (aout);
346 module_unneed (aout, owner->module);
347 /* Protect against late call from intf.c */
348 aout->volume_set = NULL;
349 aout->mute_set = NULL;
350 aout->device_select = NULL;
351 aout_OutputUnlock (aout);
353 var_DelCallback (aout, "audio-filter", FilterCallback, NULL);
354 var_DelCallback (aout, "device", var_CopyDevice, aout->obj.parent);
355 var_DelCallback (aout, "mute", var_Copy, aout->obj.parent);
356 var_SetFloat (aout, "volume", -1.f);
357 var_DelCallback (aout, "volume", var_Copy, aout->obj.parent);
358 var_DelCallback (aout, "stereo-mode", StereoModeCallback, NULL);
359 vlc_object_release (aout);
363 * Destroys the audio output lock used (asynchronously) by interface functions.
365 static void aout_Destructor (vlc_object_t *obj)
367 audio_output_t *aout = (audio_output_t *)obj;
368 aout_owner_t *owner = aout_owner (aout);
370 vlc_mutex_destroy (&owner->dev.lock);
371 for (aout_dev_t *dev = owner->dev.list, *next; dev != NULL; dev = next)
373 next = dev->next;
374 free (dev->name);
375 free (dev);
378 assert (owner->req.device == unset_str);
379 vlc_mutex_destroy (&owner->req.lock);
380 vlc_mutex_destroy (&owner->lock);
384 * Starts an audio output stream.
385 * \param fmt audio output stream format [IN/OUT]
386 * \warning The caller must hold the audio output lock.
388 int aout_OutputNew (audio_output_t *aout, audio_sample_format_t *restrict fmt,
389 int *remap)
391 aout_OutputAssertLocked (aout);
393 int i_forced_stereo_mode = var_GetInteger (aout, "stereo-mode");
394 bool b_stereo_original = fmt->i_physical_channels == AOUT_CHANS_STEREO;
395 if (i_forced_stereo_mode != AOUT_VAR_CHAN_UNSET)
397 if (i_forced_stereo_mode == AOUT_VAR_CHAN_LEFT
398 || i_forced_stereo_mode == AOUT_VAR_CHAN_RIGHT)
399 fmt->i_physical_channels = AOUT_CHAN_CENTER;
400 else
401 fmt->i_physical_channels = AOUT_CHANS_STEREO;
404 /* Ideally, the audio filters would be created before the audio output,
405 * and the ideal audio format would be the output of the filters chain.
406 * But that scheme would not really play well with digital pass-through. */
407 if (AOUT_FMT_LINEAR(fmt))
408 { /* Try to stay in integer domain if possible for no/slow FPU. */
409 fmt->i_format = (fmt->i_bitspersample > 16) ? VLC_CODEC_FL32
410 : VLC_CODEC_S16N;
411 aout_FormatPrepare (fmt);
414 if (aout->start (aout, fmt))
416 msg_Err (aout, "module not functional");
417 return -1;
420 /* Fill Stereo mode choices */
421 var_Change (aout, "stereo-mode", VLC_VAR_CLEARCHOICES, NULL, NULL);
422 vlc_value_t val, txt, default_val;
423 val.i_int = 0;
424 if (!b_stereo_original)
426 val.i_int = AOUT_VAR_CHAN_UNSET;
427 txt.psz_string = _("Original");
428 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
430 if (fmt->i_chan_mode & AOUT_CHANMODE_DOLBYSTEREO)
432 val.i_int = AOUT_VAR_CHAN_DOLBYS;
433 txt.psz_string = _("Dolby Surround");
435 else
437 val.i_int = AOUT_VAR_CHAN_STEREO;
438 txt.psz_string = _("Stereo");
440 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
441 default_val.i_int = b_stereo_original ? val.i_int : AOUT_VAR_CHAN_UNSET;
443 if (b_stereo_original)
445 val.i_int = AOUT_VAR_CHAN_LEFT;
446 txt.psz_string = _("Left");
447 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
448 val.i_int = AOUT_VAR_CHAN_RIGHT;
449 txt.psz_string = _("Right");
450 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
452 val.i_int = AOUT_VAR_CHAN_RSTEREO;
453 txt.psz_string = _("Reverse stereo");
454 var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
456 /* The user may have selected a different channels configuration. */
457 switch (i_forced_stereo_mode)
459 case AOUT_VAR_CHAN_RSTEREO:
460 remap[AOUT_CHANIDX_LEFT] = AOUT_CHANIDX_RIGHT;
461 remap[AOUT_CHANIDX_RIGHT] = AOUT_CHANIDX_LEFT;
462 break;
463 case AOUT_VAR_CHAN_STEREO:
464 break;
465 case AOUT_VAR_CHAN_LEFT:
466 remap[AOUT_CHANIDX_RIGHT] = AOUT_CHANIDX_DISABLE;
467 break;
468 case AOUT_VAR_CHAN_RIGHT:
469 remap[AOUT_CHANIDX_LEFT] = AOUT_CHANIDX_DISABLE;
470 break;
471 case AOUT_VAR_CHAN_DOLBYS:
472 fmt->i_chan_mode = AOUT_CHANMODE_DOLBYSTEREO;
473 break;
474 default:
475 if (b_stereo_original && fmt->i_chan_mode & AOUT_CHANMODE_DUALMONO)
476 { /* Go directly to the left channel. */
477 remap[AOUT_CHANIDX_RIGHT] = AOUT_CHANIDX_DISABLE;
478 val.i_int = AOUT_VAR_CHAN_LEFT;
479 var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &val, NULL);
481 else
482 var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &default_val,
483 NULL);
484 break;
487 aout_FormatPrepare (fmt);
488 assert (aout_FormatNbChannels(fmt) > 0);
489 assert (fmt->i_bytes_per_frame > 0 && fmt->i_frame_length > 0);
490 aout_FormatPrint (aout, "output", fmt);
491 return 0;
495 * Stops the audio output stream (undoes aout_OutputNew()).
496 * \note This can only be called after a successful aout_OutputNew().
497 * \warning The caller must hold the audio output lock.
499 void aout_OutputDelete (audio_output_t *aout)
501 aout_OutputAssertLocked (aout);
503 if (aout->stop != NULL)
504 aout->stop (aout);
507 int aout_OutputTimeGet (audio_output_t *aout, mtime_t *delay)
509 aout_OutputAssertLocked (aout);
511 if (aout->time_get == NULL)
512 return -1;
513 return aout->time_get (aout, delay);
517 * Plays a decoded audio buffer.
518 * \note This can only be called after a successful aout_OutputNew().
519 * \warning The caller must hold the audio output lock.
521 void aout_OutputPlay (audio_output_t *aout, block_t *block)
523 aout_OutputAssertLocked (aout);
524 #ifndef NDEBUG
525 aout_owner_t *owner = aout_owner (aout);
526 assert (owner->mixer_format.i_frame_length > 0);
527 assert (block->i_buffer == 0 || block->i_buffer / block->i_nb_samples ==
528 owner->mixer_format.i_bytes_per_frame /
529 owner->mixer_format.i_frame_length);
530 #endif
531 aout->play (aout, block);
534 static void PauseDefault (audio_output_t *aout, bool pause, mtime_t date)
536 if (pause)
537 aout_OutputFlush (aout, false);
538 (void) date;
542 * Notifies the audio output (if any) of pause/resume events.
543 * This enables the output to expedite pause, instead of waiting for its
544 * buffers to drain.
545 * \note This can only be called after a successful aout_OutputNew().
546 * \warning The caller must hold the audio output lock.
548 void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
550 aout_OutputAssertLocked (aout);
551 ((aout->pause != NULL) ? aout->pause : PauseDefault) (aout, pause, date);
555 * Flushes or drains the audio output buffers.
556 * This enables the output to expedite seek and stop.
557 * \param wait if true, wait for buffer playback (i.e. drain),
558 * if false, discard the buffers immediately (i.e. flush)
559 * \note This can only be called after a successful aout_OutputNew().
560 * \warning The caller must hold the audio output lock.
562 void aout_OutputFlush( audio_output_t *aout, bool wait )
564 aout_OutputAssertLocked( aout );
565 aout->flush (aout, wait);
568 static int aout_OutputVolumeSet (audio_output_t *aout, float vol)
570 aout_OutputAssertLocked (aout);
571 return (aout->volume_set != NULL) ? aout->volume_set (aout, vol) : -1;
574 static int aout_OutputMuteSet (audio_output_t *aout, bool mute)
576 aout_OutputAssertLocked (aout);
577 return (aout->mute_set != NULL) ? aout->mute_set (aout, mute) : -1;
580 static int aout_OutputDeviceSet (audio_output_t *aout, const char *id)
582 aout_OutputAssertLocked (aout);
583 return (aout->device_select != NULL) ? aout->device_select (aout, id) : -1;
586 void aout_OutputLock (audio_output_t *aout)
588 aout_owner_t *owner = aout_owner (aout);
590 vlc_mutex_lock (&owner->lock);
593 static int aout_OutputTryLock (audio_output_t *aout)
595 aout_owner_t *owner = aout_owner (aout);
597 return vlc_mutex_trylock (&owner->lock);
600 void aout_OutputUnlock (audio_output_t *aout)
602 aout_owner_t *owner = aout_owner (aout);
604 vlc_assert_locked (&owner->lock);
605 vlc_mutex_lock (&owner->req.lock);
607 if (owner->req.device != unset_str)
609 aout_OutputDeviceSet (aout, owner->req.device);
610 free (owner->req.device);
611 owner->req.device = (char *)unset_str;
614 if (owner->req.volume >= 0.f)
616 aout_OutputVolumeSet (aout, owner->req.volume);
617 owner->req.volume = -1.f;
620 if (owner->req.mute >= 0)
622 aout_OutputMuteSet (aout, owner->req.mute);
623 owner->req.mute = -1;
626 vlc_mutex_unlock (&owner->lock);
627 /* If another thread is blocked waiting for owner->req.lock at this point,
628 * this aout_OutputUnlock() call will not see and apply its change request.
629 * The other thread will need to apply the change request itself, which
630 * implies it is able to (try-)lock owner->lock. Therefore this thread must
631 * release owner->lock _before_ owner->req.lock. Do not reorder!!! */
632 vlc_mutex_unlock (&owner->req.lock);
636 * Gets the volume of the audio output stream (independent of mute).
637 * \return Current audio volume (0. = silent, 1. = nominal),
638 * or a strictly negative value if undefined.
640 float aout_VolumeGet (audio_output_t *aout)
642 return var_GetFloat (aout, "volume");
646 * Sets the volume of the audio output stream.
647 * \note The mute status is not changed.
648 * \return 0 on success, -1 on failure (TODO).
650 int aout_VolumeSet (audio_output_t *aout, float vol)
652 aout_owner_t *owner = aout_owner (aout);
654 assert (vol >= 0.f);
655 vlc_mutex_lock (&owner->req.lock);
656 owner->req.volume = vol;
657 vlc_mutex_unlock (&owner->req.lock);
659 if (aout_OutputTryLock (aout) == 0)
660 aout_OutputUnlock (aout);
661 return 0;
665 * Gets the audio output stream mute flag.
666 * \return 0 if not muted, 1 if muted, -1 if undefined.
668 int aout_MuteGet (audio_output_t *aout)
670 return var_InheritBool (aout, "mute");
674 * Sets the audio output stream mute flag.
675 * \return 0 on success, -1 on failure (TODO).
677 int aout_MuteSet (audio_output_t *aout, bool mute)
679 aout_owner_t *owner = aout_owner (aout);
681 vlc_mutex_lock (&owner->req.lock);
682 owner->req.mute = mute;
683 vlc_mutex_unlock (&owner->req.lock);
685 if (aout_OutputTryLock (aout) == 0)
686 aout_OutputUnlock (aout);
687 return 0;
691 * Gets the currently selected device.
692 * \return the selected device ID (caller must free() it)
693 * NULL if no device is selected or in case of error.
695 char *aout_DeviceGet (audio_output_t *aout)
697 return var_GetNonEmptyString (aout, "device");
701 * Selects an audio output device.
702 * \param id device ID to select, or NULL for the default device
703 * \return zero on success, non-zero on error (TODO).
705 int aout_DeviceSet (audio_output_t *aout, const char *id)
707 aout_owner_t *owner = aout_owner (aout);
709 char *dev = NULL;
710 if (id != NULL)
712 dev = strdup (id);
713 if (unlikely(dev == NULL))
714 return -1;
717 vlc_mutex_lock (&owner->req.lock);
718 if (owner->req.device != unset_str)
719 free (owner->req.device);
720 owner->req.device = dev;
721 vlc_mutex_unlock (&owner->req.lock);
723 if (aout_OutputTryLock (aout) == 0)
724 aout_OutputUnlock (aout);
725 return 0;
729 * Enumerates possible audio output devices.
731 * The function will heap-allocate two tables of heap-allocated strings;
732 * the caller is responsible for freeing all strings and both tables.
734 * \param ids pointer to a table of device identifiers [OUT]
735 * \param names pointer to a table of device human-readable descriptions [OUT]
736 * \return the number of devices, or negative on error.
737 * \note In case of error, *ids and *names are undefined.
739 int aout_DevicesList (audio_output_t *aout, char ***ids, char ***names)
741 aout_owner_t *owner = aout_owner (aout);
742 char **tabid, **tabname;
743 unsigned i = 0;
745 vlc_mutex_lock (&owner->dev.lock);
746 tabid = malloc (sizeof (*tabid) * owner->dev.count);
747 tabname = malloc (sizeof (*tabname) * owner->dev.count);
749 if (unlikely(tabid == NULL || tabname == NULL))
750 goto error;
752 *ids = tabid;
753 *names = tabname;
755 for (aout_dev_t *dev = owner->dev.list; dev != NULL; dev = dev->next)
757 tabid[i] = strdup(dev->id);
758 if (unlikely(tabid[i] == NULL))
759 goto error;
761 tabname[i] = strdup(dev->name);
762 if (unlikely(tabname[i] == NULL))
764 free(tabid[i]);
765 goto error;
768 i++;
770 vlc_mutex_unlock (&owner->dev.lock);
772 return i;
774 error:
775 vlc_mutex_unlock(&owner->dev.lock);
776 while (i > 0)
778 i--;
779 free(tabname[i]);
780 free(tabid[i]);
782 free(tabname);
783 free(tabid);
784 return -1;