]> git.sesse.net Git - vlc/blob - src/audio_output/output.c
aout: only (directly) access output FIFO from output functions
[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     if( !pause )
254     {
255         mtime_t duration = date - aout->p_input->i_pause_date;
256         /* XXX: ^ onk onk! gruik! ^ */
257         aout_FifoMoveDates( &aout->fifo, duration );
258     }
259 }
260
261 /**
262  * Flushes or drains the audio output buffers.
263  * This enables the output to expedite seek and stop.
264  * @param wait if true, wait for buffer playback (i.e. drain),
265  *             if false, discard the buffers immediately (i.e. flush)
266  */
267 void aout_OutputFlush( audio_output_t *aout, bool wait )
268 {
269     vlc_assert_locked( &aout->lock );
270
271     if( aout->pf_flush != NULL )
272         aout->pf_flush( aout, wait );
273     aout_FifoReset( &aout->fifo );
274 }
275
276
277 /*** Volume handling ***/
278
279 /**
280  * Dummy volume setter. This is the default volume setter.
281  */
282 static int aout_VolumeNoneSet (audio_output_t *aout, float volume, bool mute)
283 {
284     (void)aout; (void)volume; (void)mute;
285     return -1;
286 }
287
288 /**
289  * Configures the dummy volume setter.
290  * @note Audio output plugins for which volume is irrelevant
291  * should call this function during activation.
292  */
293 void aout_VolumeNoneInit (audio_output_t *aout)
294 {
295     /* aout_New() -safely- calls this function without the lock, before any
296      * other thread knows of this audio output instance.
297     vlc_assert_locked (&aout->lock); */
298     aout->pf_volume_set = aout_VolumeNoneSet;
299 }
300
301 /**
302  * Volume setter for software volume.
303  */
304 static int aout_VolumeSoftSet (audio_output_t *aout, float volume, bool mute)
305 {
306     vlc_assert_locked (&aout->lock);
307
308     /* Cubic mapping from software volume to amplification factor.
309      * This provides a good tradeoff between low and high volume ranges.
310      *
311      * This code is only used for the VLC software mixer. If you change this
312      * formula, be sure to update the aout_VolumeHardInit()-based plugins also.
313      */
314     if (!mute)
315         volume = volume * volume * volume;
316     else
317         volume = 0.;
318
319     aout->mixer_multiplier = volume;
320     return 0;
321 }
322
323 /**
324  * Configures the volume setter for software mixing
325  * and apply the default volume.
326  * @note Audio output plugins that cannot apply the volume
327  * should call this function during activation.
328  */
329 void aout_VolumeSoftInit (audio_output_t *aout)
330 {
331     audio_volume_t volume = var_InheritInteger (aout, "volume");
332     bool mute = var_InheritBool (aout, "mute");
333
334     vlc_assert_locked (&aout->lock);
335     aout->pf_volume_set = aout_VolumeSoftSet;
336     aout_VolumeSoftSet (aout, volume / (float)AOUT_VOLUME_DEFAULT, mute);
337 }
338
339 /**
340  * Configures a custom volume setter. This is used by audio outputs that can
341  * control the hardware volume directly and/or emulate it internally.
342  * @param setter volume setter callback
343  */
344 void aout_VolumeHardInit (audio_output_t *aout, aout_volume_cb setter)
345 {
346     vlc_assert_locked (&aout->lock);
347     aout->pf_volume_set = setter;
348 }
349
350 /**
351  * Supply or update the current custom ("hardware") volume.
352  * @note This only makes sense after calling aout_VolumeHardInit().
353  * @param setter volume setter callback
354  * @param volume current custom volume
355  * @param mute current mute flag
356  * @note Audio output plugins that cannot apply the volume
357  * should call this function during activation.
358  */
359 void aout_VolumeHardSet (audio_output_t *aout, float volume, bool mute)
360 {
361 #warning FIXME
362     /* REVISIT: This is tricky. We cannot acquire the volume lock as this gets
363      * called from the audio output (it would cause a lock inversion).
364      * We also should not override the input manager volume, but only the
365      * volume of the current audio output... FIXME */
366     msg_Err (aout, "%s(%f, %u)", __func__, volume, (unsigned)mute);
367 }
368
369
370 /*** Buffer management ***/
371
372 /*****************************************************************************
373  * aout_OutputNextBuffer : give the audio output plug-in the right buffer
374  *****************************************************************************
375  * If b_can_sleek is 1, the aout core functions won't try to resample
376  * new buffers to catch up - that is we suppose that the output plug-in can
377  * compensate it by itself. S/PDIF outputs should always set b_can_sleek = 1.
378  * This function is entered with no lock at all :-).
379  *****************************************************************************/
380 aout_buffer_t * aout_OutputNextBuffer( audio_output_t * p_aout,
381                                        mtime_t start_date,
382                                        bool b_can_sleek )
383 {
384     aout_fifo_t *p_fifo = &p_aout->fifo;
385     aout_buffer_t * p_buffer;
386     mtime_t now = mdate();
387
388     aout_lock( p_aout );
389
390     /* Drop the audio sample if the audio output is really late.
391      * In the case of b_can_sleek, we don't use a resampler so we need to be
392      * a lot more severe. */
393     while( ((p_buffer = p_fifo->p_first) != NULL)
394      && p_buffer->i_pts < (b_can_sleek ? start_date : now) - AOUT_MAX_PTS_DELAY )
395     {
396         msg_Dbg( p_aout, "audio output is too slow (%"PRId64"), "
397                  "trashing %"PRId64"us", now - p_buffer->i_pts,
398                  p_buffer->i_length );
399         aout_BufferFree( aout_FifoPop( p_fifo ) );
400     }
401
402     if( p_buffer == NULL )
403     {
404 #if 0 /* This is bad because the audio output might just be trying to fill
405        * in its internal buffers. And anyway, it's up to the audio output
406        * to deal with this kind of starvation. */
407
408         /* Set date to 0, to allow the mixer to send a new buffer ASAP */
409         aout_FifoReset( &p_aout->fifo );
410         if ( !p_aout->b_starving )
411             msg_Dbg( p_aout,
412                  "audio output is starving (no input), playing silence" );
413         p_aout->b_starving = true;
414 #endif
415         goto out;
416     }
417
418     mtime_t delta = start_date - p_buffer->i_pts;
419     /* Here we suppose that all buffers have the same duration - this is
420      * generally true, and anyway if it's wrong it won't be a disaster.
421      */
422     if ( 0 > delta + p_buffer->i_length )
423     {
424         if ( !p_aout->b_starving )
425             msg_Dbg( p_aout, "audio output is starving (%"PRId64"), "
426                      "playing silence", -delta );
427         p_aout->b_starving = true;
428         p_buffer = NULL;
429         goto out;
430     }
431
432     p_aout->b_starving = false;
433     p_buffer = aout_FifoPop( p_fifo );
434
435     if( !b_can_sleek
436      && ( delta > AOUT_MAX_PTS_DELAY || delta < -AOUT_MAX_PTS_ADVANCE ) )
437     {
438         /* Try to compensate the drift by doing some resampling. */
439         msg_Warn( p_aout, "output date isn't PTS date, requesting "
440                   "resampling (%"PRId64")", delta );
441
442         aout_FifoMoveDates( &p_aout->p_input->fifo, delta );
443         aout_FifoMoveDates( p_fifo, delta );
444     }
445 out:
446     aout_unlock( p_aout );
447     return p_buffer;
448 }