]> git.sesse.net Git - vlc/blob - src/audio_output/mixer.c
* ./configure: Fixed double detection of gethostbyname.
[vlc] / src / audio_output / mixer.c
1 /*****************************************************************************
2  * mixer.c : audio output mixing operations
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: mixer.c,v 1.6 2002/08/19 21:31:11 massiot Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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
36 #include "audio_output.h"
37 #include "aout_internal.h"
38
39 /*****************************************************************************
40  * aout_MixerNew: prepare a mixer plug-in
41  *****************************************************************************/
42 int aout_MixerNew( aout_instance_t * p_aout )
43 {
44     p_aout->mixer.p_module = module_Need( p_aout, "audio mixer", NULL );
45     if ( p_aout->mixer.p_module == NULL )
46     {
47         msg_Err( p_aout, "no suitable aout mixer" );
48         return -1;
49     }
50     return 0;
51 }
52
53 /*****************************************************************************
54  * aout_MixerDelete: delete the mixer
55  *****************************************************************************/
56 void aout_MixerDelete( aout_instance_t * p_aout )
57 {
58     module_Unneed( p_aout, p_aout->mixer.p_module );
59 }
60
61 /*****************************************************************************
62  * aout_MixerRun: entry point for the mixer & post-filters processing
63  *****************************************************************************/
64 void aout_MixerRun( aout_instance_t * p_aout )
65 {
66     int             i;
67     aout_buffer_t * p_output_buffer;
68     mtime_t start_date, end_date;
69
70     /* Retrieve the date of the next buffer. We don't use aout_FifoNextStart
71      * because we need to keep the lock on the FIFO, to prevent the aout
72      * thread from triggering resampling while we are running. */
73     vlc_mutex_lock( &p_aout->output.fifo.lock );
74     start_date = p_aout->output.fifo.end_date;
75     if ( start_date != 0 && start_date < mdate() )
76     {
77         /* The output is _very_ late. This can only happen if the user
78          * pauses the stream (or if the decoder is buggy, which cannot
79          * happen :). */
80         msg_Warn( p_aout, "Output PTS is out of range (%lld), clearing out",
81                   start_date );
82         start_date = p_aout->output.fifo.end_date = 0;
83     } 
84     end_date = start_date + (mtime_t)p_aout->output.i_nb_samples * 1000000
85                              / p_aout->output.output.i_rate;
86
87     /* See if we have enough data to prepare a new buffer for the audio
88      * output. */
89     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
90     {
91         aout_input_t * p_input = p_aout->pp_inputs[i];
92         aout_fifo_t * p_fifo = &p_input->fifo;
93         aout_buffer_t * p_buffer;
94         mtime_t prev_date;
95         boolean_t b_drop_buffers;
96
97         vlc_mutex_lock( &p_fifo->lock );
98
99         p_buffer = p_fifo->p_first;
100         if ( p_buffer == NULL )
101         {
102             vlc_mutex_unlock( &p_fifo->lock );
103             break;
104         }
105
106         if ( !start_date )
107         {
108             start_date = p_buffer->start_date;
109             end_date += p_buffer->start_date;
110             p_input->p_first_byte_to_mix = p_buffer->p_buffer;
111         }
112         else
113         {
114             /* Check for the continuity of start_date */
115             while ( p_buffer != NULL && p_buffer->end_date < start_date )
116             {
117                 aout_buffer_t * p_next = p_buffer->p_next;
118                 msg_Err( p_aout, "the mixer got a packet in the past (%lld)",
119                          start_date - p_buffer->end_date );
120                 aout_BufferFree( p_buffer );
121                 p_fifo->p_first = p_buffer = p_next;
122                 p_input->p_first_byte_to_mix = NULL;
123             }
124             if ( p_buffer == NULL )
125             {
126                 p_fifo->pp_last = &p_fifo->p_first;
127                 vlc_mutex_unlock( &p_fifo->lock );
128                 break;
129             }
130
131             if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
132             {
133                 /* Additionally check that p_first_byte_to_mix is well
134                  * located. */
135                 unsigned long i_nb_bytes = (start_date - p_buffer->start_date)
136                                 * p_aout->mixer.mixer.i_bytes_per_frame
137                                 * p_aout->mixer.mixer.i_rate
138                                 / p_aout->mixer.mixer.i_frame_length
139                                 / 1000000;
140                 ptrdiff_t mixer_nb_bytes;
141
142                 if ( p_input->p_first_byte_to_mix == NULL )
143                 {
144                     p_input->p_first_byte_to_mix = p_buffer->p_buffer;
145                 }
146                 mixer_nb_bytes = p_input->p_first_byte_to_mix
147                                   - p_buffer->p_buffer;
148
149                 if ( i_nb_bytes + p_aout->mixer.mixer.i_bytes_per_frame
150                       < mixer_nb_bytes ||
151                      i_nb_bytes - p_aout->mixer.mixer.i_bytes_per_frame
152                       > mixer_nb_bytes )
153                 {
154                     msg_Warn( p_aout,
155                               "mixer start isn't output start (%ld)",
156                               i_nb_bytes - mixer_nb_bytes );
157
158                     /* Round to the nearest multiple */
159                     i_nb_bytes /= p_aout->mixer.mixer.i_bytes_per_frame;
160                     i_nb_bytes *= p_aout->mixer.mixer.i_bytes_per_frame;
161                     p_input->p_first_byte_to_mix = p_buffer->p_buffer
162                                                     + i_nb_bytes;
163                 }
164             }
165         }
166
167         /* Check that we have enough samples. */
168         for ( ; ; )
169         {
170             p_buffer = p_fifo->p_first;
171             if ( p_buffer == NULL ) break;
172             if ( p_buffer->end_date >= end_date ) break;
173
174             /* Check that all buffers are contiguous. */
175             prev_date = p_fifo->p_first->end_date;
176             p_buffer = p_buffer->p_next;
177             b_drop_buffers = 0;
178             for ( ; p_buffer != NULL; p_buffer = p_buffer->p_next )
179             {
180                 if ( prev_date != p_buffer->start_date )
181                 {
182                     msg_Warn( p_aout,
183                               "buffer discontinuity, dropping packets (%lld)",
184                               p_buffer->start_date - prev_date );
185                     b_drop_buffers = 1;
186                     break;
187                 }
188                 if ( p_buffer->end_date >= end_date ) break;
189                 prev_date = p_buffer->end_date;
190             }
191             if ( b_drop_buffers )
192             {
193                 aout_buffer_t * p_deleted = p_fifo->p_first;
194                 while ( p_deleted != NULL && p_deleted != p_buffer )
195                 {
196                     aout_buffer_t * p_next = p_deleted->p_next;
197                     aout_BufferFree( p_deleted );
198                     p_deleted = p_next;
199                 }
200                 p_fifo->p_first = p_deleted; /* == p_buffer */
201             }
202             else break;
203         }
204         vlc_mutex_unlock( &p_fifo->lock );
205         if ( p_buffer == NULL ) break;
206     }
207
208     if ( i < p_aout->i_nb_inputs )
209     {
210         /* Interrupted before the end... We can't run. */
211         vlc_mutex_unlock( &p_aout->output.fifo.lock );
212         return;
213     }
214
215     p_aout->b_mixer_active = 1;
216     vlc_mutex_unlock( &p_aout->output.fifo.lock );
217
218     /* Run the mixer. */
219     aout_BufferAlloc( &p_aout->mixer.output_alloc,
220                       ((u64)p_aout->output.i_nb_samples * 1000000)
221                         / p_aout->output.output.i_rate,
222                       /* This is a bit kludgy, but is actually only used
223                        * for the S/PDIF dummy mixer : */
224                       p_aout->pp_inputs[0]->fifo.p_first,
225                       p_output_buffer );
226     if ( p_output_buffer == NULL )
227     {
228         msg_Err( p_aout, "out of memory" );
229         return;
230     }
231     p_output_buffer->i_nb_samples = p_aout->output.i_nb_samples;
232     p_output_buffer->i_nb_bytes = p_aout->output.i_nb_samples
233                               * p_aout->output.output.i_bytes_per_frame
234                               / p_aout->output.output.i_frame_length;
235     p_output_buffer->start_date = start_date;
236     p_output_buffer->end_date = end_date;
237
238     p_aout->mixer.pf_do_work( p_aout, p_output_buffer );
239
240     aout_OutputPlay( p_aout, p_output_buffer );
241
242     vlc_mutex_lock( &p_aout->output.fifo.lock );
243     p_aout->b_mixer_active = 0;
244     vlc_cond_signal( &p_aout->mixer_signal );
245     vlc_mutex_unlock( &p_aout->output.fifo.lock );
246 }
247