]> git.sesse.net Git - vlc/blob - src/audio_output/mixer.c
f0810949441199cdf618dc6263ca5ffb769883a4
[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->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_mixer, "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     vlc_assert_locked( &p_aout->lock );
79
80     if( !p_aout->p_mixer )
81         return;
82
83     module_unneed( p_aout->p_mixer, p_aout->p_mixer->module );
84     vlc_object_release( p_aout->p_mixer );
85     p_aout->p_mixer = NULL;
86 }
87
88 /*****************************************************************************
89  * MixBuffer: try to prepare one output buffer
90  *****************************************************************************
91  * Please note that you must hold the mixer lock.
92  *****************************************************************************/
93 static int MixBuffer( aout_instance_t * p_aout, float volume )
94 {
95     aout_mixer_t *p_mixer = p_aout->p_mixer;
96     aout_mixer_input_t *p_input = p_mixer->input;
97     aout_fifo_t *p_fifo = &p_input->fifo;
98     mtime_t now = mdate();
99     const unsigned samples = p_aout->i_nb_samples;
100     /* FIXME: Remove this silly constraint. Just pass buffers as they come to
101      * "smart" audio outputs. */
102     assert( samples > 0 );
103
104     vlc_assert_locked( &p_aout->lock );
105
106     /* Retrieve the date of the next buffer. */
107     date_t exact_start_date = p_aout->fifo.end_date;
108     mtime_t start_date = date_Get( &exact_start_date );
109
110     if( start_date != 0 && start_date < now )
111     {
112         /* The output is _very_ late. This can only happen if the user
113          * pauses the stream (or if the decoder is buggy, which cannot
114          * happen :). */
115         msg_Warn( p_mixer, "output PTS is out of range (%"PRId64"), clearing out",
116                   mdate() - start_date );
117         aout_FifoSet( &p_aout->fifo, 0 );
118         date_Set( &exact_start_date, 0 );
119         start_date = 0;
120     }
121
122     /* See if we have enough data to prepare a new buffer for the audio output. */
123     aout_buffer_t *p_buffer = p_fifo->p_first;
124     if( p_buffer == NULL )
125         return -1;
126
127     /* Find the earliest start date available. */
128     if ( !start_date )
129     {
130         start_date = p_buffer->i_pts;
131         date_Set( &exact_start_date, start_date );
132     }
133     /* Compute the end date for the new buffer. */
134     mtime_t end_date = date_Increment( &exact_start_date, samples );
135
136     /* Check that start_date is available. */
137     mtime_t prev_date;
138     for( ;; )
139     {
140         /* Check for the continuity of start_date */
141         prev_date = p_buffer->i_pts + p_buffer->i_length;
142         if( prev_date >= start_date - 1 )
143             break;
144         /* We authorize a +-1 because rounding errors get compensated
145          * regularly. */
146         msg_Warn( p_mixer, "the mixer got a packet in the past (%"PRId64")",
147                   start_date - prev_date );
148         aout_BufferFree( aout_FifoPop( p_fifo ) );
149
150         p_buffer = p_fifo->p_first;
151         if( p_buffer == NULL )
152             return -1;
153     }
154
155     /* Check that we have enough samples. */
156     while( prev_date < end_date )
157     {
158         p_buffer = p_buffer->p_next;
159         if( p_buffer == NULL )
160             return -1;
161
162         /* Check that all buffers are contiguous. */
163         if( prev_date != p_buffer->i_pts )
164         {
165             msg_Warn( p_mixer,
166                       "buffer hole, dropping packets (%"PRId64")",
167                       p_buffer->i_pts - prev_date );
168
169             aout_buffer_t *p_deleted;
170             while( (p_deleted = p_fifo->p_first) != p_buffer )
171                 aout_BufferFree( aout_FifoPop( p_fifo ) );
172         }
173
174         prev_date = p_buffer->i_pts + p_buffer->i_length;
175     }
176
177     if( !AOUT_FMT_NON_LINEAR( &p_mixer->fmt ) )
178     {
179         p_buffer = p_fifo->p_first;
180
181         /* Additionally check that p_first_byte_to_mix is well located. */
182         const unsigned framesize = p_mixer->fmt.i_bytes_per_frame;
183         ssize_t delta = (start_date - p_buffer->i_pts)
184                       * p_mixer->fmt.i_rate / CLOCK_FREQ;
185         if( delta != 0 )
186             msg_Warn( p_mixer, "mixer start is not output end (%zd)", delta );
187         if( delta < 0 )
188         {
189             /* Is it really the best way to do it ? */
190             aout_FifoSet( &p_aout->fifo, 0 );
191             date_Set( &exact_start_date, 0 );
192             return -1;
193         }
194         if( delta > 0 )
195         {
196             p_buffer->i_nb_samples -= delta;
197             p_buffer->i_pts += delta * CLOCK_FREQ / p_mixer->fmt.i_rate;
198             p_buffer->i_length -= delta * CLOCK_FREQ / p_mixer->fmt.i_rate;
199             delta *= framesize;
200             p_buffer->p_buffer += delta;
201             p_buffer->i_buffer -= delta;
202         }
203
204         /* Build packet with adequate number of samples */
205         unsigned needed = samples * framesize;
206         p_buffer = block_Alloc( needed );
207         if( unlikely(p_buffer == NULL) )
208             /* XXX: should free input buffers */
209             return -1;
210         p_buffer->i_nb_samples = samples;
211
212         for( uint8_t *p_out = p_buffer->p_buffer; needed > 0; )
213         {
214             aout_buffer_t *p_inbuf = p_fifo->p_first;
215             if( unlikely(p_inbuf == NULL) )
216             {
217                 msg_Err( p_mixer, "internal amix error" );
218                 vlc_memset( p_out, 0, needed );
219                 break;
220             }
221
222             const uint8_t *p_in = p_inbuf->p_buffer;
223             size_t avail = p_inbuf->i_nb_samples * framesize;
224             if( avail > needed )
225             {
226                 vlc_memcpy( p_out, p_in, needed );
227                 p_fifo->p_first->p_buffer += needed;
228                 p_fifo->p_first->i_buffer -= needed;
229                 needed /= framesize;
230                 p_fifo->p_first->i_nb_samples -= needed;
231                 p_fifo->p_first->i_pts += needed * CLOCK_FREQ / p_mixer->fmt.i_rate;
232                 p_fifo->p_first->i_length -= needed * CLOCK_FREQ / p_mixer->fmt.i_rate;
233                 break;
234             }
235
236             vlc_memcpy( p_out, p_in, avail );
237             needed -= avail;
238             p_out += avail;
239             /* Next buffer */
240             aout_BufferFree( aout_FifoPop( p_fifo ) );
241         }
242     }
243     else
244         p_buffer = aout_FifoPop( p_fifo );
245
246     p_buffer->i_pts = start_date;
247     p_buffer->i_length = end_date - start_date;
248
249     /* Run the mixer. */
250     p_mixer->mix( p_mixer, p_buffer, volume );
251     aout_OutputPlay( p_aout, p_buffer );
252     return 0;
253 }
254
255 /*****************************************************************************
256  * aout_MixerRun: entry point for the mixer & post-filters processing
257  *****************************************************************************
258  * Please note that you must hold the mixer lock.
259  *****************************************************************************/
260 void aout_MixerRun( aout_instance_t * p_aout, float volume )
261 {
262     while( MixBuffer( p_aout, volume ) != -1 );
263 }