]> git.sesse.net Git - vlc/blob - src/audio_output/output.c
Remember not to include anything before vlc/vlc.h
[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 #include <vlc/vlc.h>
28 #include <vlc_aout.h>
29 #include "aout_internal.h"
30
31 /*****************************************************************************
32  * aout_OutputNew : allocate a new output and rework the filter pipeline
33  *****************************************************************************
34  * This function is entered with the mixer lock.
35  *****************************************************************************/
36 int aout_OutputNew( aout_instance_t * p_aout,
37                     audio_sample_format_t * p_format )
38 {
39     /* Retrieve user defaults. */
40     int i_rate = config_GetInt( p_aout, "aout-rate" );
41     vlc_value_t val, text;
42     /* kludge to avoid a fpu error when rate is 0... */
43     if( i_rate == 0 ) i_rate = -1;
44
45     memcpy( &p_aout->output.output, p_format, sizeof(audio_sample_format_t) );
46     if ( i_rate != -1 )
47         p_aout->output.output.i_rate = i_rate;
48     aout_FormatPrepare( &p_aout->output.output );
49
50     vlc_mutex_lock( &p_aout->output_fifo_lock );
51
52     /* Find the best output plug-in. */
53     p_aout->output.p_module = module_Need( p_aout, "audio output", "$aout", 0);
54     if ( p_aout->output.p_module == NULL )
55     {
56         msg_Err( p_aout, "no suitable audio output module" );
57         vlc_mutex_unlock( &p_aout->output_fifo_lock );
58         return -1;
59     }
60
61     if ( var_Type( p_aout, "audio-channels" ) ==
62              (VLC_VAR_INTEGER | VLC_VAR_HASCHOICE) )
63     {
64         /* The user may have selected a different channels configuration. */
65         var_Get( p_aout, "audio-channels", &val );
66
67         if ( val.i_int == AOUT_VAR_CHAN_RSTEREO )
68         {
69             p_aout->output.output.i_original_channels |=
70                                         AOUT_CHAN_REVERSESTEREO;
71         }
72         else if ( val.i_int == AOUT_VAR_CHAN_STEREO )
73         {
74             p_aout->output.output.i_original_channels =
75                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
76         }
77         else if ( val.i_int == AOUT_VAR_CHAN_LEFT )
78         {
79             p_aout->output.output.i_original_channels = AOUT_CHAN_LEFT;
80         }
81         else if ( val.i_int == AOUT_VAR_CHAN_RIGHT )
82         {
83             p_aout->output.output.i_original_channels = AOUT_CHAN_RIGHT;
84         }
85         else if ( val.i_int == AOUT_VAR_CHAN_DOLBYS )
86         {
87             p_aout->output.output.i_original_channels
88                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_DOLBYSTEREO;
89         }
90     }
91     else if ( p_aout->output.output.i_physical_channels == AOUT_CHAN_CENTER
92               && (p_aout->output.output.i_original_channels
93                    & AOUT_CHAN_PHYSMASK) == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) )
94     {
95         /* Mono - create the audio-channels variable. */
96         var_Create( p_aout, "audio-channels",
97                     VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
98         text.psz_string = _("Audio Channels");
99         var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );
100
101         val.i_int = AOUT_VAR_CHAN_STEREO; text.psz_string = _("Stereo");
102         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
103         val.i_int = AOUT_VAR_CHAN_LEFT; text.psz_string = _("Left");
104         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
105         val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
106         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
107         if ( p_aout->output.output.i_original_channels & AOUT_CHAN_DUALMONO )
108         {
109             /* Go directly to the left channel. */
110             p_aout->output.output.i_original_channels = AOUT_CHAN_LEFT;
111             val.i_int = AOUT_VAR_CHAN_LEFT;
112             var_Set( p_aout, "audio-channels", val );
113         }
114         var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
115                          NULL );
116     }
117     else if ( p_aout->output.output.i_physical_channels ==
118                (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)
119                 && (p_aout->output.output.i_original_channels &
120                      (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
121     {
122         /* Stereo - create the audio-channels variable. */
123         var_Create( p_aout, "audio-channels",
124                     VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
125         text.psz_string = _("Audio Channels");
126         var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );
127
128         if ( p_aout->output.output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
129         {
130             val.i_int = AOUT_VAR_CHAN_DOLBYS;
131             text.psz_string = _("Dolby Surround");
132         }
133         else
134         {
135             val.i_int = AOUT_VAR_CHAN_STEREO;
136             text.psz_string = _("Stereo");
137         }
138         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
139         val.i_int = AOUT_VAR_CHAN_LEFT; text.psz_string = _("Left");
140         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
141         val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
142         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
143         val.i_int = AOUT_VAR_CHAN_RSTEREO; text.psz_string=_("Reverse stereo");
144         var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
145         if ( p_aout->output.output.i_original_channels & AOUT_CHAN_DUALMONO )
146         {
147             /* Go directly to the left channel. */
148             p_aout->output.output.i_original_channels = AOUT_CHAN_LEFT;
149             val.i_int = AOUT_VAR_CHAN_LEFT;
150             var_Set( p_aout, "audio-channels", val );
151         }
152         var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
153                          NULL );
154     }
155     val.b_bool = VLC_TRUE;
156     var_Set( p_aout, "intf-change", val );
157
158     aout_FormatPrepare( &p_aout->output.output );
159
160     /* Prepare FIFO. */
161     aout_FifoInit( p_aout, &p_aout->output.fifo,
162                    p_aout->output.output.i_rate );
163
164     vlc_mutex_unlock( &p_aout->output_fifo_lock );
165
166     aout_FormatPrint( p_aout, "output", &p_aout->output.output );
167
168     /* Calculate the resulting mixer output format. */
169     memcpy( &p_aout->mixer.mixer, &p_aout->output.output,
170             sizeof(audio_sample_format_t) );
171     if ( !AOUT_FMT_NON_LINEAR(&p_aout->output.output) )
172     {
173         /* Non-S/PDIF mixer only deals with float32 or fixed32. */
174         p_aout->mixer.mixer.i_format
175                      = (vlc_CPU() & CPU_CAPABILITY_FPU) ?
176                         VLC_FOURCC('f','l','3','2') :
177                         VLC_FOURCC('f','i','3','2');
178         aout_FormatPrepare( &p_aout->mixer.mixer );
179     }
180     else
181     {
182         p_aout->mixer.mixer.i_format = p_format->i_format;
183     }
184
185     aout_FormatPrint( p_aout, "mixer", &p_aout->mixer.mixer );
186
187     /* Create filters. */
188     p_aout->output.i_nb_filters = 0;
189     if ( aout_FiltersCreatePipeline( p_aout, p_aout->output.pp_filters,
190                                      &p_aout->output.i_nb_filters,
191                                      &p_aout->mixer.mixer,
192                                      &p_aout->output.output ) < 0 )
193     {
194         msg_Err( p_aout, "couldn't create audio output pipeline" );
195         module_Unneed( p_aout, p_aout->output.p_module );
196         return -1;
197     }
198
199     /* Prepare hints for the buffer allocator. */
200     p_aout->mixer.output_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
201     p_aout->mixer.output_alloc.i_bytes_per_sec
202                         = p_aout->mixer.mixer.i_bytes_per_frame
203                            * p_aout->mixer.mixer.i_rate
204                            / p_aout->mixer.mixer.i_frame_length;
205
206     aout_FiltersHintBuffers( p_aout, p_aout->output.pp_filters,
207                              p_aout->output.i_nb_filters,
208                              &p_aout->mixer.output_alloc );
209
210     p_aout->output.b_error = 0;
211     return 0;
212 }
213
214 /*****************************************************************************
215  * aout_OutputDelete : delete the output
216  *****************************************************************************
217  * This function is entered with the mixer lock.
218  *****************************************************************************/
219 void aout_OutputDelete( aout_instance_t * p_aout )
220 {
221     if ( p_aout->output.b_error )
222     {
223         return;
224     }
225
226     module_Unneed( p_aout, p_aout->output.p_module );
227
228     aout_FiltersDestroyPipeline( p_aout, p_aout->output.pp_filters,
229                                  p_aout->output.i_nb_filters );
230
231     vlc_mutex_lock( &p_aout->output_fifo_lock );
232     aout_FifoDestroy( p_aout, &p_aout->output.fifo );
233     vlc_mutex_unlock( &p_aout->output_fifo_lock );
234
235     p_aout->output.b_error = VLC_TRUE;
236 }
237
238 /*****************************************************************************
239  * aout_OutputPlay : play a buffer
240  *****************************************************************************
241  * This function is entered with the mixer lock.
242  *****************************************************************************/
243 void aout_OutputPlay( aout_instance_t * p_aout, aout_buffer_t * p_buffer )
244 {
245     aout_FiltersPlay( p_aout, p_aout->output.pp_filters,
246                       p_aout->output.i_nb_filters,
247                       &p_buffer );
248
249     if( p_buffer->i_nb_bytes == 0 )
250     {
251         aout_BufferFree( p_buffer );
252         return;
253     }
254
255     vlc_mutex_lock( &p_aout->output_fifo_lock );
256     aout_FifoPush( p_aout, &p_aout->output.fifo, p_buffer );
257     p_aout->output.pf_play( p_aout );
258     vlc_mutex_unlock( &p_aout->output_fifo_lock );
259 }
260
261 /*****************************************************************************
262  * aout_OutputNextBuffer : give the audio output plug-in the right buffer
263  *****************************************************************************
264  * If b_can_sleek is 1, the aout core functions won't try to resample
265  * new buffers to catch up - that is we suppose that the output plug-in can
266  * compensate it by itself. S/PDIF outputs should always set b_can_sleek = 1.
267  * This function is entered with no lock at all :-).
268  *****************************************************************************/
269 aout_buffer_t * aout_OutputNextBuffer( aout_instance_t * p_aout,
270                                        mtime_t start_date,
271                                        vlc_bool_t b_can_sleek )
272 {
273     aout_buffer_t * p_buffer;
274
275     vlc_mutex_lock( &p_aout->output_fifo_lock );
276
277     p_buffer = p_aout->output.fifo.p_first;
278
279     /* Drop the audio sample if the audio output is really late.
280      * In the case of b_can_sleek, we don't use a resampler so we need to be
281      * a lot more severe. */
282     while ( p_buffer && p_buffer->start_date <
283             (b_can_sleek ? start_date : mdate()) - AOUT_PTS_TOLERANCE )
284     {
285         msg_Dbg( p_aout, "audio output is too slow ("I64Fd"), "
286                  "trashing "I64Fd"us", mdate() - p_buffer->start_date,
287                  p_buffer->end_date - p_buffer->start_date );
288         p_buffer = p_buffer->p_next;
289         aout_BufferFree( p_aout->output.fifo.p_first );
290         p_aout->output.fifo.p_first = p_buffer;
291     }
292
293     if ( p_buffer == NULL )
294     {
295         p_aout->output.fifo.pp_last = &p_aout->output.fifo.p_first;
296
297 #if 0 /* This is bad because the audio output might just be trying to fill
298        * in its internal buffers. And anyway, it's up to the audio output
299        * to deal with this kind of starvation. */
300
301         /* Set date to 0, to allow the mixer to send a new buffer ASAP */
302         aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
303         if ( !p_aout->output.b_starving )
304             msg_Dbg( p_aout,
305                  "audio output is starving (no input), playing silence" );
306         p_aout->output.b_starving = 1;
307 #endif
308
309         vlc_mutex_unlock( &p_aout->output_fifo_lock );
310         return NULL;
311     }
312
313     /* Here we suppose that all buffers have the same duration - this is
314      * generally true, and anyway if it's wrong it won't be a disaster.
315      */
316     if ( p_buffer->start_date > start_date
317                          + (p_buffer->end_date - p_buffer->start_date) )
318     /*
319      *                   + AOUT_PTS_TOLERANCE )
320      * There is no reason to want that, it just worsen the scheduling of
321      * an audio sample after an output starvation (ie. on start or on resume)
322      * --Gibalou
323      */
324     {
325         const mtime_t i_delta = p_buffer->start_date - start_date;
326         vlc_mutex_unlock( &p_aout->output_fifo_lock );
327
328         if ( !p_aout->output.b_starving )
329             msg_Dbg( p_aout, "audio output is starving ("I64Fd"), "
330                      "playing silence", i_delta );
331         p_aout->output.b_starving = 1;
332         return NULL;
333     }
334
335     p_aout->output.b_starving = 0;
336
337     if ( !b_can_sleek &&
338           ( (p_buffer->start_date - start_date > AOUT_PTS_TOLERANCE)
339              || (start_date - p_buffer->start_date > AOUT_PTS_TOLERANCE) ) )
340     {
341         /* Try to compensate the drift by doing some resampling. */
342         int i;
343         mtime_t difference = start_date - p_buffer->start_date;
344         msg_Warn( p_aout, "output date isn't PTS date, requesting "
345                   "resampling ("I64Fd")", difference );
346
347         vlc_mutex_lock( &p_aout->input_fifos_lock );
348         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
349         {
350             aout_fifo_t * p_fifo = &p_aout->pp_inputs[i]->fifo;
351
352             aout_FifoMoveDates( p_aout, p_fifo, difference );
353         }
354
355         aout_FifoMoveDates( p_aout, &p_aout->output.fifo, difference );
356         vlc_mutex_unlock( &p_aout->input_fifos_lock );
357     }
358
359     p_aout->output.fifo.p_first = p_buffer->p_next;
360     if ( p_buffer->p_next == NULL )
361     {
362         p_aout->output.fifo.pp_last = &p_aout->output.fifo.p_first;
363     }
364
365     vlc_mutex_unlock( &p_aout->output_fifo_lock );
366     return p_buffer;
367 }