]> git.sesse.net Git - vlc/blob - src/audio_output/output.c
ce40ec9ee47ad39ac95265d9cc4ae4faa7b919d1
[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 int aout_GainNotify (audio_output_t *aout, float gain)
73 {
74     aout_owner_t *owner = aout_owner (aout);
75
76     aout_assert_locked (aout);
77     aout_volume_SetVolume (owner->volume, gain);
78     /* XXX: ideally, return -1 if format cannot be amplified */
79     return 0;
80 }
81
82 #undef aout_New
83 /**
84  * Creates an audio output object and initializes an output module.
85  */
86 audio_output_t *aout_New (vlc_object_t *parent)
87 {
88     audio_output_t *aout = vlc_custom_create (parent, sizeof (aout_instance_t),
89                                               "audio output");
90     if (unlikely(aout == NULL))
91         return NULL;
92
93     aout_owner_t *owner = aout_owner (aout);
94
95     vlc_mutex_init (&owner->lock);
96     vlc_object_set_destructor (aout, aout_Destructor);
97
98     /* Audio output module callbacks */
99     var_Create (aout, "volume", VLC_VAR_FLOAT);
100     var_AddCallback (aout, "volume", var_Copy, parent);
101     var_Create (aout, "mute", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
102     var_AddCallback (aout, "mute", var_Copy, parent);
103
104     aout->event.volume_report = aout_VolumeNotify;
105     aout->event.mute_report = aout_MuteNotify;
106     aout->event.policy_report = aout_PolicyNotify;
107     aout->event.gain_request = aout_GainNotify;
108
109     /* Audio output module initialization */
110     aout->start = NULL;
111     aout->stop = NULL;
112     aout->volume_set = NULL;
113     aout->mute_set = NULL;
114     owner->module = module_need (aout, "audio output", "$aout", false);
115     if (owner->module == NULL)
116     {
117         msg_Err (aout, "no suitable audio output module");
118         vlc_object_release (aout);
119         return NULL;
120     }
121
122     /*
123      * Persistent audio output variables
124      */
125     vlc_value_t val, text;
126     module_config_t *cfg;
127     char *str;
128
129     /* Visualizations */
130     var_Create (aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
131     text.psz_string = _("Visualizations");
132     var_Change (aout, "visual", VLC_VAR_SETTEXT, &text, NULL);
133     val.psz_string = (char *)"";
134     text.psz_string = _("Disable");
135     var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
136     val.psz_string = (char *)"spectrometer";
137     text.psz_string = _("Spectrometer");
138     var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
139     val.psz_string = (char *)"scope";
140     text.psz_string = _("Scope");
141     var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
142     val.psz_string = (char *)"spectrum";
143     text.psz_string = _("Spectrum");
144     var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
145     val.psz_string = (char *)"vuMeter";
146     text.psz_string = _("Vu meter");
147     var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
148     /* Look for goom plugin */
149     if (module_exists ("goom"))
150     {
151         val.psz_string = (char *)"goom";
152         text.psz_string = (char *)"Goom";
153         var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
154     }
155     /* Look for libprojectM plugin */
156     if (module_exists ("projectm"))
157     {
158         val.psz_string = (char *)"projectm";
159         text.psz_string = (char*)"projectM";
160         var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
161     }
162     /* Look for VSXu plugin */
163     if (module_exists ("vsxu"))
164     {
165         val.psz_string = (char *)"vsxu";
166         text.psz_string = (char*)"Vovoid VSXu";
167         var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
168     }
169     str = var_GetNonEmptyString (aout, "effect-list");
170     if (str != NULL)
171     {
172         var_SetString (aout, "visual", str);
173         free (str);
174     }
175
176     /* Equalizer */
177     var_Create (aout, "equalizer", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
178     text.psz_string = _("Equalizer");
179     var_Change (aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL);
180     val.psz_string = (char*)"";
181     text.psz_string = _("Disable");
182     var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
183     cfg = config_FindConfig (VLC_OBJECT(aout), "equalizer-preset");
184     if (likely(cfg != NULL))
185         for (unsigned i = 0; i < cfg->list_count; i++)
186         {
187             val.psz_string = cfg->list.psz[i];
188             text.psz_string = vlc_gettext(cfg->list_text[i]);
189             var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
190         }
191
192     var_Create (aout, "audio-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
193     text.psz_string = _("Audio filters");
194     var_Change (aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL);
195
196
197     var_Create (aout, "audio-visual", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
198     text.psz_string = _("Audio visualizations");
199     var_Change (aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL);
200
201     /* Replay gain */
202     var_Create (aout, "audio-replay-gain-mode",
203                 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
204     text.psz_string = _("Replay gain");
205     var_Change (aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL);
206     cfg = config_FindConfig (VLC_OBJECT(aout), "audio-replay-gain-mode");
207     if (likely(cfg != NULL))
208         for (unsigned i = 0; i < cfg->list_count; i++)
209         {
210             val.psz_string = cfg->list.psz[i];
211             text.psz_string = vlc_gettext(cfg->list_text[i]);
212             var_Change (aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
213                             &val, &text);
214         }
215
216     return aout;
217 }
218
219 /**
220  * Deinitializes an audio output module and destroys an audio output object.
221  */
222 void aout_Destroy (audio_output_t *aout)
223 {
224     aout_owner_t *owner = aout_owner (aout);
225
226     aout_lock (aout);
227     module_unneed (aout, owner->module);
228     /* Protect against late call from intf.c */
229     aout->volume_set = NULL;
230     aout->mute_set = NULL;
231     aout_unlock (aout);
232
233     var_DelCallback (aout, "mute", var_Copy, aout->p_parent);
234     var_SetFloat (aout, "volume", -1.f);
235     var_DelCallback (aout, "volume", var_Copy, aout->p_parent);
236     vlc_object_release (aout);
237 }
238
239 /**
240  * Destroys the audio output lock used (asynchronously) by interface functions.
241  */
242 static void aout_Destructor (vlc_object_t *obj)
243 {
244     audio_output_t *aout = (audio_output_t *)obj;
245     aout_owner_t *owner = aout_owner (aout);
246
247     vlc_mutex_destroy (&owner->lock);
248 }
249
250 /**
251  * Gets the volume of the audio output stream (independent of mute).
252  * \return Current audio volume (0. = silent, 1. = nominal),
253  * or a strictly negative value if undefined.
254  */
255 float aout_VolumeGet (audio_output_t *aout)
256 {
257     return var_GetFloat (aout, "volume");
258 }
259
260 /**
261  * Sets the volume of the audio output stream.
262  * \note The mute status is not changed.
263  * \return 0 on success, -1 on failure.
264  */
265 int aout_VolumeSet (audio_output_t *aout, float vol)
266 {
267     int ret = -1;
268
269     aout_lock (aout);
270     if (aout->volume_set != NULL)
271         ret = aout->volume_set (aout, vol);
272     aout_unlock (aout);
273     return ret;
274 }
275
276 /**
277  * Gets the audio output stream mute flag.
278  * \return 0 if not muted, 1 if muted, -1 if undefined.
279  */
280 int aout_MuteGet (audio_output_t *aout)
281 {
282     return var_InheritBool (aout, "mute");
283 }
284
285 /**
286  * Sets the audio output stream mute flag.
287  * \return 0 on success, -1 on failure.
288  */
289 int aout_MuteSet (audio_output_t *aout, bool mute)
290 {
291     int ret = -1;
292
293     aout_lock (aout);
294     if (aout->mute_set != NULL)
295         ret = aout->mute_set (aout, mute);
296     aout_unlock (aout);
297     return ret;
298 }
299
300 /**
301  * Starts an audio output stream.
302  * \param fmt audio output stream format [IN/OUT]
303  * \warning The caller must hold the audio output lock.
304  */
305 int aout_OutputNew (audio_output_t *aout, audio_sample_format_t *restrict fmt)
306 {
307     aout_assert_locked (aout);
308
309     /* Ideally, the audio filters would be created before the audio output,
310      * and the ideal audio format would be the output of the filters chain.
311      * But that scheme would not really play well with digital pass-through. */
312     if (AOUT_FMT_LINEAR(fmt))
313     {   /* Try to stay in integer domain if possible for no/slow FPU. */
314         fmt->i_format = (fmt->i_bitspersample > 16) ? VLC_CODEC_FL32
315                                                     : VLC_CODEC_S16N;
316         aout_FormatPrepare (fmt);
317     }
318
319     if (aout->start (aout, fmt))
320     {
321         msg_Err (aout, "module not functional");
322         return -1;
323     }
324
325     if (!var_Type (aout, "stereo-mode"))
326         var_Create (aout, "stereo-mode",
327                     VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT);
328
329     /* The user may have selected a different channels configuration. */
330     var_AddCallback (aout, "stereo-mode", aout_ChannelsRestart, NULL);
331     switch (var_GetInteger (aout, "stereo-mode"))
332     {
333         case AOUT_VAR_CHAN_RSTEREO:
334             fmt->i_original_channels |= AOUT_CHAN_REVERSESTEREO;
335             break;
336         case AOUT_VAR_CHAN_STEREO:
337             fmt->i_original_channels = AOUT_CHANS_STEREO;
338             break;
339         case AOUT_VAR_CHAN_LEFT:
340             fmt->i_original_channels = AOUT_CHAN_LEFT;
341             break;
342         case AOUT_VAR_CHAN_RIGHT:
343             fmt->i_original_channels = AOUT_CHAN_RIGHT;
344             break;
345         case AOUT_VAR_CHAN_DOLBYS:
346             fmt->i_original_channels = AOUT_CHANS_STEREO|AOUT_CHAN_DOLBYSTEREO;
347             break;
348         default:
349         {
350             if ((fmt->i_original_channels & AOUT_CHAN_PHYSMASK)
351                                                           != AOUT_CHANS_STEREO)
352                  break;
353
354             vlc_value_t val, txt;
355             val.i_int = 0;
356             var_Change (aout, "stereo-mode", VLC_VAR_DELCHOICE, &val, NULL);
357             txt.psz_string = _("Stereo audio mode");
358             var_Change (aout, "stereo-mode", VLC_VAR_SETTEXT, &txt, NULL);
359             if (fmt->i_original_channels & AOUT_CHAN_DOLBYSTEREO)
360             {
361                 val.i_int = AOUT_VAR_CHAN_DOLBYS;
362                 txt.psz_string = _("Dolby Surround");
363             }
364             else
365             {
366                 val.i_int = AOUT_VAR_CHAN_STEREO;
367                 txt.psz_string = _("Stereo");
368             }
369             var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
370             var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &val, NULL);
371             val.i_int = AOUT_VAR_CHAN_LEFT;
372             txt.psz_string = _("Left");
373             var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
374             if (fmt->i_original_channels & AOUT_CHAN_DUALMONO)
375             {   /* Go directly to the left channel. */
376                 fmt->i_original_channels = AOUT_CHAN_LEFT;
377                 var_Change (aout, "stereo-mode", VLC_VAR_SETVALUE, &val, NULL);
378             }
379             val.i_int = AOUT_VAR_CHAN_RIGHT;
380             txt.psz_string = _("Right");
381             var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
382             val.i_int = AOUT_VAR_CHAN_RSTEREO;
383             txt.psz_string = _("Reverse stereo");
384             var_Change (aout, "stereo-mode", VLC_VAR_ADDCHOICE, &val, &txt);
385         }
386     }
387
388     aout_FormatPrepare (fmt);
389     aout_FormatPrint (aout, "output", fmt);
390     return 0;
391 }
392
393 /**
394  * Stops the audio output stream (undoes aout_OutputNew()).
395  * \note This can only be called after a succesful aout_OutputNew().
396  * \warning The caller must hold the audio output lock.
397  */
398 void aout_OutputDelete (audio_output_t *aout)
399 {
400     aout_assert_locked (aout);
401
402     var_DelCallback (aout, "stereo-mode", aout_ChannelsRestart, NULL);
403     if (aout->stop != NULL)
404         aout->stop (aout);
405 }
406
407 int aout_OutputTimeGet (audio_output_t *aout, mtime_t *pts)
408 {
409     aout_assert_locked (aout);
410
411     if (aout->time_get == NULL)
412         return -1;
413     return aout->time_get (aout, pts);
414 }
415
416 /**
417  * Plays a decoded audio buffer.
418  * \note This can only be called after a succesful aout_OutputNew().
419  * \warning The caller must hold the audio output lock.
420  */
421 void aout_OutputPlay (audio_output_t *aout, block_t *block)
422 {
423     aout_assert_locked (aout);
424     aout->play (aout, block);
425 }
426
427 static void PauseDefault (audio_output_t *aout, bool pause, mtime_t date)
428 {
429     if (pause)
430         aout_OutputFlush (aout, false);
431     (void) date;
432 }
433
434 /**
435  * Notifies the audio output (if any) of pause/resume events.
436  * This enables the output to expedite pause, instead of waiting for its
437  * buffers to drain.
438  * \note This can only be called after a succesful aout_OutputNew().
439  * \warning The caller must hold the audio output lock.
440  */
441 void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
442 {
443     aout_assert_locked (aout);
444     ((aout->pause != NULL) ? aout->pause : PauseDefault) (aout, pause, date);
445 }
446
447 /**
448  * Flushes or drains the audio output buffers.
449  * This enables the output to expedite seek and stop.
450  * \param wait if true, wait for buffer playback (i.e. drain),
451  *             if false, discard the buffers immediately (i.e. flush)
452  * \note This can only be called after a succesful aout_OutputNew().
453  * \warning The caller must hold the audio output lock.
454  */
455 void aout_OutputFlush( audio_output_t *aout, bool wait )
456 {
457     aout_assert_locked( aout );
458     aout->flush (aout, wait);
459 }