]> git.sesse.net Git - vlc/blob - src/audio_output/output.c
9258fc927559d77409365349848bf9ddc4422641
[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 the VideoLAN team
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
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_aout.h>
33 #include <vlc_aout_intf.h>
34 #include <vlc_cpu.h>
35 #include <vlc_modules.h>
36
37 #include "libvlc.h"
38 #include "aout_internal.h"
39
40 /*****************************************************************************
41  * aout_OutputNew : allocate a new output and rework the filter pipeline
42  *****************************************************************************
43  * This function is entered with the mixer lock.
44  *****************************************************************************/
45 int aout_OutputNew( audio_output_t * p_aout,
46                     const audio_sample_format_t * p_format )
47 {
48     vlc_assert_locked( &p_aout->lock );
49     p_aout->format = *p_format;
50
51     /* Retrieve user defaults. */
52     int i_rate = var_InheritInteger( p_aout, "aout-rate" );
53     if ( i_rate != 0 )
54         p_aout->format.i_rate = i_rate;
55     aout_FormatPrepare( &p_aout->format );
56
57     /* Find the best output plug-in. */
58     p_aout->module = module_need( p_aout, "audio output", "$aout", false );
59     if ( p_aout->module == NULL )
60     {
61         msg_Err( p_aout, "no suitable audio output module" );
62         return -1;
63     }
64
65     if ( var_Type( p_aout, "audio-channels" ) ==
66              (VLC_VAR_INTEGER | VLC_VAR_HASCHOICE) )
67     {
68         /* The user may have selected a different channels configuration. */
69         switch( var_InheritInteger( p_aout, "audio-channels" ) )
70         {
71             case AOUT_VAR_CHAN_RSTEREO:
72                 p_aout->format.i_original_channels |= AOUT_CHAN_REVERSESTEREO;
73                 break;
74             case AOUT_VAR_CHAN_STEREO:
75                 p_aout->format.i_original_channels =
76                                               AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
77                 break;
78             case AOUT_VAR_CHAN_LEFT:
79                 p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
80                 break;
81             case AOUT_VAR_CHAN_RIGHT:
82                 p_aout->format.i_original_channels = AOUT_CHAN_RIGHT;
83                 break;
84             case AOUT_VAR_CHAN_DOLBYS:
85                 p_aout->format.i_original_channels =
86                       AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_DOLBYSTEREO;
87                 break;
88         }
89     }
90     else if ( p_aout->format.i_physical_channels == AOUT_CHAN_CENTER
91               && (p_aout->format.i_original_channels
92                    & AOUT_CHAN_PHYSMASK) == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) )
93     {
94         vlc_value_t val, text;
95
96         /* Mono - create the audio-channels variable. */
97         var_Create( p_aout, "audio-channels",
98                     VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
99         text.psz_string = _("Audio Channels");
100         var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );
101
102         val.i_int = AOUT_VAR_CHAN_STEREO; text.psz_string = _("Stereo");
103         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
104         val.i_int = AOUT_VAR_CHAN_LEFT; text.psz_string = _("Left");
105         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
106         val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
107         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
108         if ( p_aout->format.i_original_channels & AOUT_CHAN_DUALMONO )
109         {
110             /* Go directly to the left channel. */
111             p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
112             var_SetInteger( p_aout, "audio-channels", AOUT_VAR_CHAN_LEFT );
113         }
114         var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
115                          NULL );
116     }
117     else if ( p_aout->format.i_physical_channels ==
118                (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)
119                 && (p_aout->format.i_original_channels &
120                      (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
121     {
122         vlc_value_t val, text;
123
124         /* Stereo - create the audio-channels variable. */
125         var_Create( p_aout, "audio-channels",
126                     VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
127         text.psz_string = _("Audio Channels");
128         var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );
129
130         if ( p_aout->format.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
131         {
132             val.i_int = AOUT_VAR_CHAN_DOLBYS;
133             text.psz_string = _("Dolby Surround");
134         }
135         else
136         {
137             val.i_int = AOUT_VAR_CHAN_STEREO;
138             text.psz_string = _("Stereo");
139         }
140         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
141         val.i_int = AOUT_VAR_CHAN_LEFT; text.psz_string = _("Left");
142         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
143         val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
144         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
145         val.i_int = AOUT_VAR_CHAN_RSTEREO; text.psz_string=_("Reverse stereo");
146         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
147         if ( p_aout->format.i_original_channels & AOUT_CHAN_DUALMONO )
148         {
149             /* Go directly to the left channel. */
150             p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
151             var_SetInteger( p_aout, "audio-channels", AOUT_VAR_CHAN_LEFT );
152         }
153         var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
154                          NULL );
155     }
156     var_TriggerCallback( p_aout, "intf-change" );
157
158     aout_FormatPrepare( &p_aout->format );
159
160     /* Prepare FIFO. */
161     aout_FifoInit( p_aout, &p_aout->fifo, p_aout->format.i_rate );
162     aout_FormatPrint( p_aout, "output", &p_aout->format );
163
164     /* Choose the mixer format. */
165     p_aout->mixer_format = p_aout->format;
166     if ( AOUT_FMT_NON_LINEAR(&p_aout->format) )
167         p_aout->mixer_format.i_format = p_format->i_format;
168     else
169     /* Most audio filters can only deal with single-precision,
170      * so lets always use that when hardware supports floating point. */
171     if( HAVE_FPU )
172         p_aout->mixer_format.i_format = VLC_CODEC_FL32;
173     else
174     /* Otherwise, audio filters will not work. Use fixed-point if the input has
175      * more than 16-bits depth. */
176     if( p_format->i_bitspersample > 16 )
177         p_aout->mixer_format.i_format = VLC_CODEC_FI32;
178     else
179     /* Fallback to 16-bits. This avoids pointless conversion to and from
180      * 32-bits samples for the sole purpose of software mixing. */
181         p_aout->mixer_format.i_format = VLC_CODEC_S16N;
182
183     aout_FormatPrepare( &p_aout->mixer_format );
184     aout_FormatPrint( p_aout, "mixer", &p_aout->mixer_format );
185
186     /* Create filters. */
187     p_aout->i_nb_filters = 0;
188     if ( aout_FiltersCreatePipeline( p_aout, p_aout->pp_filters,
189                                      &p_aout->i_nb_filters,
190                                      &p_aout->mixer_format,
191                                      &p_aout->format ) < 0 )
192     {
193         msg_Err( p_aout, "couldn't create audio output pipeline" );
194         module_unneed( p_aout, p_aout->module );
195         p_aout->module = NULL;
196         return -1;
197     }
198     return 0;
199 }
200
201 /*****************************************************************************
202  * aout_OutputDelete : delete the output
203  *****************************************************************************
204  * This function is entered with the mixer lock.
205  *****************************************************************************/
206 void aout_OutputDelete( audio_output_t * p_aout )
207 {
208     vlc_assert_locked( &p_aout->lock );
209
210     if( p_aout->module == NULL )
211         return;
212
213     module_unneed( p_aout, p_aout->module );
214     aout_VolumeNoneInit( p_aout ); /* clear volume callback */
215     p_aout->module = NULL;
216     aout_FiltersDestroyPipeline( p_aout->pp_filters, p_aout->i_nb_filters );
217     aout_FifoDestroy( &p_aout->fifo );
218 }
219
220 /*****************************************************************************
221  * aout_OutputPlay : play a buffer
222  *****************************************************************************
223  * This function is entered with the mixer lock.
224  *****************************************************************************/
225 void aout_OutputPlay( audio_output_t * p_aout, aout_buffer_t * p_buffer )
226 {
227     vlc_assert_locked( &p_aout->lock );
228
229     aout_FiltersPlay( p_aout->pp_filters, p_aout->i_nb_filters, &p_buffer );
230     if( !p_buffer )
231         return;
232     if( p_buffer->i_buffer == 0 )
233     {
234         block_Release( p_buffer );
235         return;
236     }
237
238     aout_FifoPush( &p_aout->fifo, p_buffer );
239     p_aout->pf_play( p_aout );
240 }
241
242 /**
243  * Notifies the audio output (if any) of pause/resume events.
244  * This enables the output to expedite pause, instead of waiting for its
245  * buffers to drain.
246  */
247 void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
248 {
249     vlc_assert_locked( &aout->lock );
250
251     if( aout->pf_pause != NULL )
252         aout->pf_pause( aout, pause, date );
253 }
254
255 /**
256  * Flushes or drains the audio output buffers.
257  * This enables the output to expedite seek and stop.
258  * @param wait if true, wait for buffer playback (i.e. drain),
259  *             if false, discard the buffers immediately (i.e. flush)
260  */
261 void aout_OutputFlush( audio_output_t *aout, bool wait )
262 {
263     vlc_assert_locked( &aout->lock );
264
265     if( aout->pf_flush != NULL )
266         aout->pf_flush( aout, wait );
267 }
268
269
270 /*** Volume handling ***/
271
272 /**
273  * Dummy volume setter. This is the default volume setter.
274  */
275 static int aout_VolumeNoneSet (audio_output_t *aout, float volume, bool mute)
276 {
277     (void)aout; (void)volume; (void)mute;
278     return -1;
279 }
280
281 /**
282  * Configures the dummy volume setter.
283  * @note Audio output plugins for which volume is irrelevant
284  * should call this function during activation.
285  */
286 void aout_VolumeNoneInit (audio_output_t *aout)
287 {
288     /* aout_New() -safely- calls this function without the lock, before any
289      * other thread knows of this audio output instance.
290     vlc_assert_locked (&aout->lock); */
291     aout->pf_volume_set = aout_VolumeNoneSet;
292 }
293
294 /**
295  * Volume setter for software volume.
296  */
297 static int aout_VolumeSoftSet (audio_output_t *aout, float volume, bool mute)
298 {
299     vlc_assert_locked (&aout->lock);
300
301     /* Cubic mapping from software volume to amplification factor.
302      * This provides a good tradeoff between low and high volume ranges.
303      *
304      * This code is only used for the VLC software mixer. If you change this
305      * formula, be sure to update the aout_VolumeHardInit()-based plugins also.
306      */
307     if (!mute)
308         volume = volume * volume * volume;
309     else
310         volume = 0.;
311
312     aout->mixer_multiplier = volume;
313     return 0;
314 }
315
316 /**
317  * Configures the volume setter for software mixing
318  * and apply the default volume.
319  * @note Audio output plugins that cannot apply the volume
320  * should call this function during activation.
321  */
322 void aout_VolumeSoftInit (audio_output_t *aout)
323 {
324     audio_volume_t volume = var_InheritInteger (aout, "volume");
325     bool mute = var_InheritBool (aout, "mute");
326
327     vlc_assert_locked (&aout->lock);
328     aout->pf_volume_set = aout_VolumeSoftSet;
329     aout_VolumeSoftSet (aout, volume / (float)AOUT_VOLUME_DEFAULT, mute);
330 }
331
332 /**
333  * Configures a custom volume setter. This is used by audio outputs that can
334  * control the hardware volume directly and/or emulate it internally.
335  * @param setter volume setter callback
336  */
337 void aout_VolumeHardInit (audio_output_t *aout, aout_volume_cb setter)
338 {
339     vlc_assert_locked (&aout->lock);
340     aout->pf_volume_set = setter;
341 }
342
343 /**
344  * Supply or update the current custom ("hardware") volume.
345  * @note This only makes sense after calling aout_VolumeHardInit().
346  * @param setter volume setter callback
347  * @param volume current custom volume
348  * @param mute current mute flag
349  * @note Audio output plugins that cannot apply the volume
350  * should call this function during activation.
351  */
352 void aout_VolumeHardSet (audio_output_t *aout, float volume, bool mute)
353 {
354 #warning FIXME
355     /* REVISIT: This is tricky. We cannot acquire the volume lock as this gets
356      * called from the audio output (it would cause a lock inversion).
357      * We also should not override the input manager volume, but only the
358      * volume of the current audio output... FIXME */
359     msg_Err (aout, "%s(%f, %u)", __func__, volume, (unsigned)mute);
360 }
361
362
363 /*** Buffer management ***/
364
365 /*****************************************************************************
366  * aout_OutputNextBuffer : give the audio output plug-in the right buffer
367  *****************************************************************************
368  * If b_can_sleek is 1, the aout core functions won't try to resample
369  * new buffers to catch up - that is we suppose that the output plug-in can
370  * compensate it by itself. S/PDIF outputs should always set b_can_sleek = 1.
371  * This function is entered with no lock at all :-).
372  *****************************************************************************/
373 aout_buffer_t * aout_OutputNextBuffer( audio_output_t * p_aout,
374                                        mtime_t start_date,
375                                        bool b_can_sleek )
376 {
377     aout_fifo_t *p_fifo = &p_aout->fifo;
378     aout_buffer_t * p_buffer;
379     mtime_t now = mdate();
380
381     aout_lock( p_aout );
382
383     /* Drop the audio sample if the audio output is really late.
384      * In the case of b_can_sleek, we don't use a resampler so we need to be
385      * a lot more severe. */
386     while( ((p_buffer = p_fifo->p_first) != NULL)
387      && p_buffer->i_pts < (b_can_sleek ? start_date : now) - AOUT_MAX_PTS_DELAY )
388     {
389         msg_Dbg( p_aout, "audio output is too slow (%"PRId64"), "
390                  "trashing %"PRId64"us", now - p_buffer->i_pts,
391                  p_buffer->i_length );
392         aout_BufferFree( aout_FifoPop( p_fifo ) );
393     }
394
395     if( p_buffer == NULL )
396     {
397 #if 0 /* This is bad because the audio output might just be trying to fill
398        * in its internal buffers. And anyway, it's up to the audio output
399        * to deal with this kind of starvation. */
400
401         /* Set date to 0, to allow the mixer to send a new buffer ASAP */
402         aout_FifoReset( &p_aout->fifo );
403         if ( !p_aout->b_starving )
404             msg_Dbg( p_aout,
405                  "audio output is starving (no input), playing silence" );
406         p_aout->b_starving = true;
407 #endif
408         goto out;
409     }
410
411     mtime_t delta = start_date - p_buffer->i_pts;
412     /* Here we suppose that all buffers have the same duration - this is
413      * generally true, and anyway if it's wrong it won't be a disaster.
414      */
415     if ( 0 > delta + p_buffer->i_length )
416     {
417         if ( !p_aout->b_starving )
418             msg_Dbg( p_aout, "audio output is starving (%"PRId64"), "
419                      "playing silence", -delta );
420         p_aout->b_starving = true;
421         p_buffer = NULL;
422         goto out;
423     }
424
425     p_aout->b_starving = false;
426     p_buffer = aout_FifoPop( p_fifo );
427
428     if( !b_can_sleek
429      && ( delta > AOUT_MAX_PTS_DELAY || delta < -AOUT_MAX_PTS_ADVANCE ) )
430     {
431         /* Try to compensate the drift by doing some resampling. */
432         msg_Warn( p_aout, "output date isn't PTS date, requesting "
433                   "resampling (%"PRId64")", delta );
434
435         aout_FifoMoveDates( &p_aout->p_input->fifo, delta );
436         aout_FifoMoveDates( p_fifo, delta );
437     }
438 out:
439     aout_unlock( p_aout );
440     return p_buffer;
441 }