]> git.sesse.net Git - vlc/blob - src/audio_output/mixer.c
287df6422a4e0af04126d56306d9751b05c7817f
[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 #include <vlc_modules.h>
36
37 #include <vlc_aout.h>
38 #include "aout_internal.h"
39 /*****************************************************************************
40  * aout_MixerNew: prepare a mixer plug-in
41  *****************************************************************************
42  * Please note that you must hold the mixer lock.
43  *****************************************************************************/
44 int aout_MixerNew( aout_instance_t * p_aout )
45 {
46     assert( !p_aout->p_mixer );
47     vlc_assert_locked( &p_aout->input_fifos_lock );
48
49     aout_mixer_t *p_mixer = vlc_object_create( p_aout, sizeof(*p_mixer) );
50     if( !p_mixer )
51         return VLC_EGENERIC;
52
53     p_mixer->fmt = p_aout->mixer_format;
54     p_mixer->input = &p_aout->p_input->mixer;
55     p_mixer->mix = NULL;
56     p_mixer->sys = NULL;
57
58     p_mixer->module = module_need( p_mixer, "audio mixer", NULL, false );
59     if( !p_mixer->module )
60     {
61         msg_Err( p_aout, "no suitable audio mixer" );
62         vlc_object_release( p_mixer );
63         return VLC_EGENERIC;
64     }
65
66     /* */
67     p_aout->p_mixer = p_mixer;
68     return VLC_SUCCESS;
69 }
70
71 /*****************************************************************************
72  * aout_MixerDelete: delete the mixer
73  *****************************************************************************
74  * Please note that you must hold the mixer lock.
75  *****************************************************************************/
76 void aout_MixerDelete( aout_instance_t * p_aout )
77 {
78     if( !p_aout->p_mixer )
79         return;
80
81     module_unneed( p_aout->p_mixer, p_aout->p_mixer->module );
82
83     vlc_object_release( p_aout->p_mixer );
84
85     /* */
86     p_aout->p_mixer = NULL;
87 }
88
89 /*****************************************************************************
90  * MixBuffer: try to prepare one output buffer
91  *****************************************************************************
92  * Please note that you must hold the mixer lock.
93  *****************************************************************************/
94 static int MixBuffer( aout_instance_t * p_aout, float volume )
95 {
96     aout_mixer_t *p_mixer = p_aout->p_mixer;
97
98     assert( p_aout->p_input != NULL );
99
100     aout_input_t *p_input = p_aout->p_input;
101     if( p_input->b_paused )
102         return -1;
103
104     aout_fifo_t * p_fifo = &p_input->mixer.fifo;
105     mtime_t now = mdate();
106
107     aout_lock_input_fifos( p_aout );
108     aout_lock_output_fifo( p_aout );
109
110     /* Retrieve the date of the next buffer. */
111     date_t exact_start_date = p_aout->output.fifo.end_date;
112     mtime_t start_date = date_Get( &exact_start_date );
113
114     if( start_date != 0 && start_date < now )
115     {
116         /* The output is _very_ late. This can only happen if the user
117          * pauses the stream (or if the decoder is buggy, which cannot
118          * happen :). */
119         msg_Warn( p_aout, "output PTS is out of range (%"PRId64"), clearing out",
120                   mdate() - start_date );
121         aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
122         date_Set( &exact_start_date, 0 );
123         start_date = 0;
124     }
125
126     aout_unlock_output_fifo( p_aout );
127
128     /* See if we have enough data to prepare a new buffer for the audio
129      * output. First : start date. */
130     if ( !start_date )
131     {
132         /* Find the latest start date available. */
133         aout_buffer_t *p_buffer;
134         for( ;; )
135         {
136             p_buffer = p_fifo->p_first;
137             if( p_buffer == NULL )
138                 goto giveup;
139             if( p_buffer->i_pts >= now )
140                 break;
141
142             msg_Warn( p_aout, "input PTS is out of range (%"PRId64"), "
143                       "trashing", now - p_buffer->i_pts );
144             aout_BufferFree( aout_FifoPop( p_aout, p_fifo ) );
145             p_input->mixer.begin = NULL;
146         }
147
148         date_Set( &exact_start_date, p_buffer->i_pts );
149         start_date = p_buffer->i_pts;
150     }
151
152     date_Increment( &exact_start_date, p_aout->output.i_nb_samples );
153     mtime_t end_date = date_Get( &exact_start_date );
154
155     /* Check that start_date is available. */
156     aout_buffer_t *p_buffer = p_fifo->p_first;
157     mtime_t prev_date;
158
159     for( ;; )
160     {
161         if( p_buffer == NULL )
162             goto giveup;
163
164         /* Check for the continuity of start_date */
165         prev_date = p_buffer->i_pts + p_buffer->i_length;
166         if( prev_date >= start_date - 1 )
167             break;
168         /* We authorize a +-1 because rounding errors get compensated
169          * regularly. */
170         msg_Warn( p_aout, "the mixer got a packet in the past (%"PRId64")",
171                   start_date - prev_date );
172         aout_BufferFree( aout_FifoPop( p_aout, p_fifo ) );
173         p_input->mixer.begin = NULL;
174         p_buffer = p_fifo->p_first;
175     }
176
177     /* Check that we have enough samples. */
178     while( prev_date < end_date )
179     {
180         p_buffer = p_buffer->p_next;
181         if( p_buffer == NULL )
182             goto giveup;
183
184         /* Check that all buffers are contiguous. */
185         if( prev_date != p_buffer->i_pts )
186         {
187             msg_Warn( p_aout,
188                       "buffer hole, dropping packets (%"PRId64")",
189                       p_buffer->i_pts - prev_date );
190
191             aout_buffer_t *p_deleted;
192             while( (p_deleted = p_fifo->p_first) != p_buffer )
193                 aout_BufferFree( aout_FifoPop( p_aout, p_fifo ) );
194         }
195
196         prev_date = p_buffer->i_pts + p_buffer->i_length;
197     }
198
199     p_buffer = p_fifo->p_first;
200     if( !AOUT_FMT_NON_LINEAR( &p_mixer->fmt ) )
201     {
202         /* Additionally check that p_first_byte_to_mix is well located. */
203         mtime_t i_buffer = (start_date - p_buffer->i_pts)
204                          * p_mixer->fmt.i_bytes_per_frame
205                          * p_mixer->fmt.i_rate
206                          / p_mixer->fmt.i_frame_length
207                          / CLOCK_FREQ;
208         if( p_input->mixer.begin == NULL )
209             p_input->mixer.begin = p_buffer->p_buffer;
210
211         ptrdiff_t bytes = p_input->mixer.begin - p_buffer->p_buffer;
212         if( !((i_buffer + p_mixer->fmt.i_bytes_per_frame > bytes)
213          && (i_buffer < p_mixer->fmt.i_bytes_per_frame + bytes)) )
214         {
215             msg_Warn( p_aout, "mixer start is not output start (%"PRId64")",
216                       i_buffer - bytes );
217
218             /* Round to the nearest multiple */
219             i_buffer /= p_mixer->fmt.i_bytes_per_frame;
220             i_buffer *= p_mixer->fmt.i_bytes_per_frame;
221             if( i_buffer < 0 )
222             {
223                 /* Is it really the best way to do it ? */
224                 aout_lock_output_fifo( p_aout );
225                 aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
226                 date_Set( &exact_start_date, 0 );
227                 aout_unlock_output_fifo( p_aout );
228                 goto giveup;
229             }
230             p_input->mixer.begin = p_buffer->p_buffer + i_buffer;
231         }
232     }
233
234     /* Run the mixer. */
235     p_buffer = p_mixer->mix( p_aout->p_mixer, p_aout->output.i_nb_samples,
236                              volume );
237     aout_unlock_input_fifos( p_aout );
238
239     if( unlikely(p_buffer == NULL) )
240         return -1;
241
242     p_buffer->i_pts = start_date;
243     p_buffer->i_length = end_date - start_date;
244     aout_OutputPlay( p_aout, p_buffer );
245     return 0;
246
247 giveup:
248     /* Interrupted before the end... We can't run. */
249     aout_unlock_input_fifos( p_aout );
250     return -1;
251 }
252
253 /*****************************************************************************
254  * aout_MixerRun: entry point for the mixer & post-filters processing
255  *****************************************************************************
256  * Please note that you must hold the mixer lock.
257  *****************************************************************************/
258 void aout_MixerRun( aout_instance_t * p_aout, float volume )
259 {
260     while( MixBuffer( p_aout, volume ) != -1 );
261 }