]> git.sesse.net Git - vlc/blob - src/audio_output/mixer.c
audio_output/output.c: Make sure we are properly locking the fifo lock when appropria...
[vlc] / src / audio_output / mixer.c
1 /*****************************************************************************
2  * mixer.c : audio output mixing operations
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 <stdlib.h>                            /* calloc(), malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31
32 #ifdef HAVE_ALLOCA_H
33 #   include <alloca.h>
34 #endif
35 #include <vlc_aout.h>
36 #include "aout_internal.h"
37 /*****************************************************************************
38  * aout_MixerNew: prepare a mixer plug-in
39  *****************************************************************************
40  * Please note that you must hold the mixer lock.
41  *****************************************************************************/
42 int aout_MixerNew( aout_instance_t * p_aout )
43 {
44     p_aout->mixer.p_module = module_Need( p_aout, "audio mixer", NULL, 0 );
45     if ( p_aout->mixer.p_module == NULL )
46     {
47         msg_Err( p_aout, "no suitable audio mixer" );
48         return -1;
49     }
50     p_aout->mixer.b_error = 0;
51     return 0;
52 }
53
54 /*****************************************************************************
55  * aout_MixerDelete: delete the mixer
56  *****************************************************************************
57  * Please note that you must hold the mixer lock.
58  *****************************************************************************/
59 void aout_MixerDelete( aout_instance_t * p_aout )
60 {
61     if ( p_aout->mixer.b_error ) return;
62     module_Unneed( p_aout, p_aout->mixer.p_module );
63     p_aout->mixer.b_error = 1;
64 }
65
66 /*****************************************************************************
67  * MixBuffer: try to prepare one output buffer
68  *****************************************************************************
69  * Please note that you must hold the mixer lock.
70  *****************************************************************************/
71 static int MixBuffer( aout_instance_t * p_aout )
72 {
73     int             i, i_first_input = 0;
74     aout_buffer_t * p_output_buffer;
75     mtime_t start_date, end_date;
76     audio_date_t exact_start_date;
77
78     if ( p_aout->mixer.b_error )
79     {
80         /* Free all incoming buffers. */
81         vlc_mutex_lock( &p_aout->input_fifos_lock );
82         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
83         {
84             aout_input_t * p_input = p_aout->pp_inputs[i];
85             aout_buffer_t * p_buffer = p_input->fifo.p_first;
86             if ( p_input->b_error ) continue;
87             while ( p_buffer != NULL )
88             {
89                 aout_buffer_t * p_next = p_buffer->p_next;
90                 aout_BufferFree( p_buffer );
91                 p_buffer = p_next;
92             }
93         }
94         vlc_mutex_unlock( &p_aout->input_fifos_lock );
95         return -1;
96     }
97
98
99     vlc_mutex_lock( &p_aout->output_fifo_lock );
100     vlc_mutex_lock( &p_aout->input_fifos_lock );
101
102     /* Retrieve the date of the next buffer. */
103     memcpy( &exact_start_date, &p_aout->output.fifo.end_date,
104             sizeof(audio_date_t) );
105     start_date = aout_DateGet( &exact_start_date );
106
107     if ( start_date != 0 && start_date < mdate() )
108     {
109         /* The output is _very_ late. This can only happen if the user
110          * pauses the stream (or if the decoder is buggy, which cannot
111          * happen :). */
112         msg_Warn( p_aout, "output PTS is out of range ("I64Fd"), clearing out",
113                   mdate() - start_date );
114         aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
115         aout_DateSet( &exact_start_date, 0 );
116         start_date = 0;
117     }
118
119     vlc_mutex_unlock( &p_aout->output_fifo_lock );
120
121     /* See if we have enough data to prepare a new buffer for the audio
122      * output. First : start date. */
123     if ( !start_date )
124     {
125         /* Find the latest start date available. */
126         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
127         {
128             aout_input_t * p_input = p_aout->pp_inputs[i];
129             aout_fifo_t * p_fifo = &p_input->fifo;
130             aout_buffer_t * p_buffer;
131
132             if ( p_input->b_error ) continue;
133
134             p_buffer = p_fifo->p_first;
135             while ( p_buffer != NULL && p_buffer->start_date < mdate() )
136             {
137                 msg_Warn( p_aout, "input PTS is out of range ("I64Fd"), "
138                           "trashing", mdate() - p_buffer->start_date );
139                 p_buffer = aout_FifoPop( p_aout, p_fifo );
140                 aout_BufferFree( p_buffer );
141                 if( p_input->p_input_thread )
142                 {
143 //                    stats_UpdateInteger( p_input->p_input_thread,
144 //                                         "lost_abuffers", 1 );
145                 }
146                 p_buffer = p_fifo->p_first;
147                 p_input->p_first_byte_to_mix = NULL;
148             }
149
150             if ( p_buffer == NULL )
151             {
152                 break;
153             }
154
155             if ( !start_date || start_date < p_buffer->start_date )
156             {
157                 aout_DateSet( &exact_start_date, p_buffer->start_date );
158                 start_date = p_buffer->start_date;
159             }
160         }
161
162         if ( i < p_aout->i_nb_inputs )
163         {
164             /* Interrupted before the end... We can't run. */
165             vlc_mutex_unlock( &p_aout->input_fifos_lock );
166             return -1;
167         }
168     }
169     aout_DateIncrement( &exact_start_date, p_aout->output.i_nb_samples );
170     end_date = aout_DateGet( &exact_start_date );
171
172     /* Check that start_date and end_date are available for all input
173      * streams. */
174     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
175     {
176         aout_input_t * p_input = p_aout->pp_inputs[i];
177         aout_fifo_t * p_fifo = &p_input->fifo;
178         aout_buffer_t * p_buffer;
179         mtime_t prev_date;
180         vlc_bool_t b_drop_buffers;
181
182         if ( p_input->b_error )
183         {
184             if ( i_first_input == i ) i_first_input++;
185             continue;
186         }
187
188         p_buffer = p_fifo->p_first;
189         if ( p_buffer == NULL )
190         {
191             break;
192         }
193
194         /* Check for the continuity of start_date */
195         while ( p_buffer != NULL && p_buffer->end_date < start_date - 1 )
196         {
197             /* We authorize a +-1 because rounding errors get compensated
198              * regularly. */
199             aout_buffer_t * p_next = p_buffer->p_next;
200             msg_Warn( p_aout, "the mixer got a packet in the past ("I64Fd")",
201                       start_date - p_buffer->end_date );
202             aout_BufferFree( p_buffer );
203             if( p_input->p_input_thread )
204             {
205 //                stats_UpdateInteger( p_input->p_input_thread,
206 //                                     "lost_abuffers", 1 );
207             }
208             p_fifo->p_first = p_buffer = p_next;
209             p_input->p_first_byte_to_mix = NULL;
210         }
211         if ( p_buffer == NULL )
212         {
213             p_fifo->pp_last = &p_fifo->p_first;
214             break;
215         }
216
217         /* Check that we have enough samples. */
218         for ( ; ; )
219         {
220             p_buffer = p_fifo->p_first;
221             if ( p_buffer == NULL ) break;
222             if ( p_buffer->end_date >= end_date ) break;
223
224             /* Check that all buffers are contiguous. */
225             prev_date = p_fifo->p_first->end_date;
226             p_buffer = p_buffer->p_next;
227             b_drop_buffers = 0;
228             for ( ; p_buffer != NULL; p_buffer = p_buffer->p_next )
229             {
230                 if ( prev_date != p_buffer->start_date )
231                 {
232                     msg_Warn( p_aout,
233                               "buffer hole, dropping packets ("I64Fd")",
234                               p_buffer->start_date - prev_date );
235                     b_drop_buffers = 1;
236                     break;
237                 }
238                 if ( p_buffer->end_date >= end_date ) break;
239                 prev_date = p_buffer->end_date;
240             }
241             if ( b_drop_buffers )
242             {
243                 aout_buffer_t * p_deleted = p_fifo->p_first;
244                 while ( p_deleted != NULL && p_deleted != p_buffer )
245                 {
246                     aout_buffer_t * p_next = p_deleted->p_next;
247                     aout_BufferFree( p_deleted );
248                     p_deleted = p_next;
249                 }
250                 p_fifo->p_first = p_deleted; /* == p_buffer */
251             }
252             else break;
253         }
254         if ( p_buffer == NULL ) break;
255
256         p_buffer = p_fifo->p_first;
257         if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
258         {
259             /* Additionally check that p_first_byte_to_mix is well
260              * located. */
261             mtime_t i_nb_bytes = (start_date - p_buffer->start_date)
262                             * p_aout->mixer.mixer.i_bytes_per_frame
263                             * p_aout->mixer.mixer.i_rate
264                             / p_aout->mixer.mixer.i_frame_length
265                             / 1000000;
266             ptrdiff_t mixer_nb_bytes;
267
268             if ( p_input->p_first_byte_to_mix == NULL )
269             {
270                 p_input->p_first_byte_to_mix = p_buffer->p_buffer;
271             }
272             mixer_nb_bytes = p_input->p_first_byte_to_mix - p_buffer->p_buffer;
273
274             if ( !((i_nb_bytes + p_aout->mixer.mixer.i_bytes_per_frame
275                      > mixer_nb_bytes) &&
276                    (i_nb_bytes < p_aout->mixer.mixer.i_bytes_per_frame
277                      + mixer_nb_bytes)) )
278             {
279                 msg_Warn( p_aout, "mixer start isn't output start ("I64Fd")",
280                           i_nb_bytes - mixer_nb_bytes );
281
282                 /* Round to the nearest multiple */
283                 i_nb_bytes /= p_aout->mixer.mixer.i_bytes_per_frame;
284                 i_nb_bytes *= p_aout->mixer.mixer.i_bytes_per_frame;
285                 if( i_nb_bytes < 0 )
286                 {
287                     /* Is it really the best way to do it ? */
288                     aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
289                     aout_DateSet( &exact_start_date, 0 );
290                     break;
291                 }
292
293                 p_input->p_first_byte_to_mix = p_buffer->p_buffer + i_nb_bytes;
294             }
295         }
296     }
297
298     if ( i < p_aout->i_nb_inputs || i_first_input == p_aout->i_nb_inputs )
299     {
300         /* Interrupted before the end... We can't run. */
301         vlc_mutex_unlock( &p_aout->input_fifos_lock );
302         return -1;
303     }
304
305     /* Run the mixer. */
306     aout_BufferAlloc( &p_aout->mixer.output_alloc,
307                       ((uint64_t)p_aout->output.i_nb_samples * 1000000)
308                         / p_aout->output.output.i_rate,
309                       /* This is a bit kludgy, but is actually only used
310                        * for the S/PDIF dummy mixer : */
311                       p_aout->pp_inputs[i_first_input]->fifo.p_first,
312                       p_output_buffer );
313     if ( p_output_buffer == NULL )
314     {
315         msg_Err( p_aout, "out of memory" );
316         vlc_mutex_unlock( &p_aout->input_fifos_lock );
317         return -1;
318     }
319     /* This is again a bit kludgy - for the S/PDIF mixer. */
320     if ( p_aout->mixer.output_alloc.i_alloc_type != AOUT_ALLOC_NONE )
321     {
322         p_output_buffer->i_nb_samples = p_aout->output.i_nb_samples;
323         p_output_buffer->i_nb_bytes = p_aout->output.i_nb_samples
324                               * p_aout->mixer.mixer.i_bytes_per_frame
325                               / p_aout->mixer.mixer.i_frame_length;
326     }
327     p_output_buffer->start_date = start_date;
328     p_output_buffer->end_date = end_date;
329
330     p_aout->mixer.pf_do_work( p_aout, p_output_buffer );
331
332     vlc_mutex_unlock( &p_aout->input_fifos_lock );
333
334     aout_OutputPlay( p_aout, p_output_buffer );
335
336     return 0;
337 }
338
339 /*****************************************************************************
340  * aout_MixerRun: entry point for the mixer & post-filters processing
341  *****************************************************************************
342  * Please note that you must hold the mixer lock.
343  *****************************************************************************/
344 void aout_MixerRun( aout_instance_t * p_aout )
345 {
346     while( MixBuffer( p_aout ) != -1 );
347 }
348
349 /*****************************************************************************
350  * aout_MixerMultiplierSet: set p_aout->mixer.f_multiplier
351  *****************************************************************************
352  * Please note that we assume that you own the mixer lock when entering this
353  * function. This function returns -1 on error.
354  *****************************************************************************/
355 int aout_MixerMultiplierSet( aout_instance_t * p_aout, float f_multiplier )
356 {
357     float f_old = p_aout->mixer.f_multiplier;
358     vlc_bool_t b_new_mixer = 0;
359
360     if ( !p_aout->mixer.b_error )
361     {
362         aout_MixerDelete( p_aout );
363         b_new_mixer = 1;
364     }
365
366     p_aout->mixer.f_multiplier = f_multiplier;
367
368     if ( b_new_mixer && aout_MixerNew( p_aout ) )
369     {
370         p_aout->mixer.f_multiplier = f_old;
371         aout_MixerNew( p_aout );
372         return -1;
373     }
374
375     return 0;
376 }
377
378 /*****************************************************************************
379  * aout_MixerMultiplierGet: get p_aout->mixer.f_multiplier
380  *****************************************************************************
381  * Please note that we assume that you own the mixer lock when entering this
382  * function. This function returns -1 on error.
383  *****************************************************************************/
384 int aout_MixerMultiplierGet( aout_instance_t * p_aout, float * pf_multiplier )
385 {
386     *pf_multiplier = p_aout->mixer.f_multiplier;
387     return 0;
388 }
389