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