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