]> git.sesse.net Git - vlc/blob - src/audio_output/output.c
aout: remove has-choices flag on "device", not applicable anymore
[vlc] / src / audio_output / output.c
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$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_aout.h>
30 #include <vlc_modules.h>
31
32 #include "libvlc.h"
33 #include "aout_internal.h"
34
35 /* Local functions */
36 static void aout_Destructor( vlc_object_t * p_this );
37
38 static int var_Copy (vlc_object_t *src, const char *name, vlc_value_t prev,
39                      vlc_value_t value, void *data)
40 {
41     vlc_object_t *dst = data;
42
43     (void) src; (void) prev;
44     return var_Set (dst, name, value);
45 }
46
47 /**
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
51  *
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).
56  */
57 static void aout_VolumeNotify (audio_output_t *aout, float volume)
58 {
59     var_SetFloat (aout, "volume", volume);
60 }
61
62 static void aout_MuteNotify (audio_output_t *aout, bool mute)
63 {
64     var_SetBool (aout, "mute", mute);
65 }
66
67 static void aout_PolicyNotify (audio_output_t *aout, bool cork)
68 {
69     (cork ? var_IncInteger : var_DecInteger) (aout->p_parent, "corks");
70 }
71
72 static void aout_DeviceNotify (audio_output_t *aout, const char *id)
73 {
74     var_SetString (aout, "device", (id != NULL) ? id : "");
75 }
76
77 static void aout_RestartNotify (audio_output_t *aout, unsigned mode)
78 {
79     aout_RequestRestart (aout, mode);
80 }
81
82 static int aout_GainNotify (audio_output_t *aout, float gain)
83 {
84     aout_owner_t *owner = aout_owner (aout);
85
86     aout_assert_locked (aout);
87     aout_volume_SetVolume (owner->volume, gain);
88     /* XXX: ideally, return -1 if format cannot be amplified */
89     return 0;
90 }
91
92 #undef aout_New
93 /**
94  * Creates an audio output object and initializes an output module.
95  */
96 audio_output_t *aout_New (vlc_object_t *parent)
97 {
98     vlc_value_t val, text;
99
100     audio_output_t *aout = vlc_custom_create (parent, sizeof (aout_instance_t),
101                                               "audio output");
102     if (unlikely(aout == NULL))
103         return NULL;
104
105     aout_owner_t *owner = aout_owner (aout);
106
107     vlc_mutex_init (&owner->lock);
108     vlc_object_set_destructor (aout, aout_Destructor);
109
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);
116
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;
123
124     /* Audio output module initialization */
125     aout->start = NULL;
126     aout->stop = NULL;
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)
133     {
134         msg_Err (aout, "no suitable audio output module");
135         vlc_object_release (aout);
136         return NULL;
137     }
138
139     /*
140      * Persistent audio output variables
141      */
142     module_config_t *cfg;
143     char *str;
144
145     /* Visualizations */
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"))
166     {
167         val.psz_string = (char *)"goom";
168         text.psz_string = (char *)"Goom";
169         var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
170     }
171     /* Look for libprojectM plugin */
172     if (module_exists ("projectm"))
173     {
174         val.psz_string = (char *)"projectm";
175         text.psz_string = (char*)"projectM";
176         var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
177     }
178     /* Look for VSXu plugin */
179     if (module_exists ("vsxu"))
180     {
181         val.psz_string = (char *)"vsxu";
182         text.psz_string = (char*)"Vovoid VSXu";
183         var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
184     }
185     str = var_GetNonEmptyString (aout, "effect-list");
186     if (str != NULL)
187     {
188         var_SetString (aout, "visual", str);
189         free (str);
190     }
191
192     /* Equalizer */
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++)
202         {
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);
206         }
207
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);
211
212
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);
216
217     /* Replay gain */
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++)
225         {
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,
229                             &val, &text);
230         }
231
232     return aout;
233 }
234
235 /**
236  * Deinitializes an audio output module and destroys an audio output object.
237  */
238 void aout_Destroy (audio_output_t *aout)
239 {
240     aout_owner_t *owner = aout_owner (aout);
241
242     aout_lock (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;
247     aout_unlock (aout);
248
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);
253 }
254
255 /**
256  * Destroys the audio output lock used (asynchronously) by interface functions.
257  */
258 static void aout_Destructor (vlc_object_t *obj)
259 {
260     audio_output_t *aout = (audio_output_t *)obj;
261     aout_owner_t *owner = aout_owner (aout);
262
263     vlc_mutex_destroy (&owner->lock);
264 }
265
266 /**
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.
270  */
271 float aout_VolumeGet (audio_output_t *aout)
272 {
273     return var_GetFloat (aout, "volume");
274 }
275
276 /**
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.
280  */
281 int aout_VolumeSet (audio_output_t *aout, float vol)
282 {
283     int ret = -1;
284
285     aout_lock (aout);
286     if (aout->volume_set != NULL)
287         ret = aout->volume_set (aout, vol);
288     aout_unlock (aout);
289     return ret;
290 }
291
292 /**
293  * Gets the audio output stream mute flag.
294  * \return 0 if not muted, 1 if muted, -1 if undefined.
295  */
296 int aout_MuteGet (audio_output_t *aout)
297 {
298     return var_InheritBool (aout, "mute");
299 }
300
301 /**
302  * Sets the audio output stream mute flag.
303  * \return 0 on success, -1 on failure.
304  */
305 int aout_MuteSet (audio_output_t *aout, bool mute)
306 {
307     int ret = -1;
308
309     aout_lock (aout);
310     if (aout->mute_set != NULL)
311         ret = aout->mute_set (aout, mute);
312     aout_unlock (aout);
313     return ret;
314 }
315
316 /**
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.
320  */
321 char *aout_DeviceGet (audio_output_t *aout)
322 {
323     return var_GetNonEmptyString (aout, "device");
324 }
325
326 /**
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.
330  */
331 int aout_DeviceSet (audio_output_t *aout, const char *id)
332 {
333     int ret = -1;
334
335     aout_lock (aout);
336     if (aout->device_select != NULL)
337         ret = aout->device_select (aout, id);
338     aout_unlock (aout);
339     return ret;
340 }
341
342 /**
343  * Enumerates possible audio output devices.
344  *
345  * The function will heap-allocate two tables of heap-allocated strings;
346  * the caller is responsible for freeing all strings and both tables.
347  *
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.
352  */
353 int aout_DevicesList (audio_output_t *aout, char ***ids, char ***names)
354 {
355     int ret = -1;
356
357     aout_lock (aout);
358     if (aout->device_enum != NULL)
359         ret = aout->device_enum (aout, ids, names);
360     aout_unlock (aout);
361     return ret;
362 }
363
364
365 /**
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.
369  */
370 int aout_OutputNew (audio_output_t *aout, audio_sample_format_t *restrict fmt)
371 {
372     aout_assert_locked (aout);
373
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
380                                                     : VLC_CODEC_S16N;
381         aout_FormatPrepare (fmt);
382     }
383
384     if (aout->start (aout, fmt))
385     {
386         msg_Err (aout, "module not functional");
387         return -1;
388     }
389
390     if (!var_Type (aout, "stereo-mode"))
391         var_Create (aout, "stereo-mode",
392                     VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT);
393
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"))
397     {
398         case AOUT_VAR_CHAN_RSTEREO:
399             fmt->i_original_channels |= AOUT_CHAN_REVERSESTEREO;
400             break;
401         case AOUT_VAR_CHAN_STEREO:
402             fmt->i_original_channels = AOUT_CHANS_STEREO;
403             break;
404         case AOUT_VAR_CHAN_LEFT:
405             fmt->i_original_channels = AOUT_CHAN_LEFT;
406             break;
407         case AOUT_VAR_CHAN_RIGHT:
408             fmt->i_original_channels = AOUT_CHAN_RIGHT;
409             break;
410         case AOUT_VAR_CHAN_DOLBYS:
411             fmt->i_original_channels = AOUT_CHANS_STEREO|AOUT_CHAN_DOLBYSTEREO;
412             break;
413         default:
414         {
415             if ((fmt->i_original_channels & AOUT_CHAN_PHYSMASK)
416                                                           != AOUT_CHANS_STEREO)
417                  break;
418
419             vlc_value_t val, txt;
420             val.i_int = 0;
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)
425             {
426                 val.i_int = AOUT_VAR_CHAN_DOLBYS;
427                 txt.psz_string = _("Dolby Surround");
428             }
429             else
430             {
431                 val.i_int = AOUT_VAR_CHAN_STEREO;
432                 txt.psz_string = _("Stereo");
433             }
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);
443             }
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);
450         }
451     }
452
453     aout_FormatPrepare (fmt);
454     aout_FormatPrint (aout, "output", fmt);
455     return 0;
456 }
457
458 /**
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.
462  */
463 void aout_OutputDelete (audio_output_t *aout)
464 {
465     aout_assert_locked (aout);
466
467     var_DelCallback (aout, "stereo-mode", aout_ChannelsRestart, NULL);
468     if (aout->stop != NULL)
469         aout->stop (aout);
470 }
471
472 int aout_OutputTimeGet (audio_output_t *aout, mtime_t *delay)
473 {
474     aout_assert_locked (aout);
475
476     if (aout->time_get == NULL)
477         return -1;
478     return aout->time_get (aout, delay);
479 }
480
481 /**
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.
485  */
486 void aout_OutputPlay (audio_output_t *aout, block_t *block)
487 {
488     aout_assert_locked (aout);
489     aout->play (aout, block);
490 }
491
492 static void PauseDefault (audio_output_t *aout, bool pause, mtime_t date)
493 {
494     if (pause)
495         aout_OutputFlush (aout, false);
496     (void) date;
497 }
498
499 /**
500  * Notifies the audio output (if any) of pause/resume events.
501  * This enables the output to expedite pause, instead of waiting for its
502  * buffers to drain.
503  * \note This can only be called after a succesful aout_OutputNew().
504  * \warning The caller must hold the audio output lock.
505  */
506 void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
507 {
508     aout_assert_locked (aout);
509     ((aout->pause != NULL) ? aout->pause : PauseDefault) (aout, pause, date);
510 }
511
512 /**
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.
519  */
520 void aout_OutputFlush( audio_output_t *aout, bool wait )
521 {
522     aout_assert_locked( aout );
523     aout->flush (aout, wait);
524 }