]> git.sesse.net Git - vlc/blob - src/audio_output/input.c
0e0b9cbdf30c36cf5e3a66d30c2b8e6b01570291
[vlc] / src / audio_output / input.c
1 /*****************************************************************************
2  * input.c : internal management of input streams for the audio output
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: input.c,v 1.6 2002/08/19 23:12:57 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 #include "audio_output.h"
33 #include "aout_internal.h"
34
35 /*****************************************************************************
36  * aout_InputNew : allocate a new input and rework the filter pipeline
37  *****************************************************************************/
38 static aout_input_t * InputNew( aout_instance_t * p_aout,
39                                 audio_sample_format_t * p_format )
40 {
41     aout_input_t * p_input = malloc(sizeof(aout_input_t));
42
43     if ( p_input == NULL ) return NULL;
44
45     vlc_mutex_lock( &p_aout->mixer_lock );
46
47     if ( p_aout->i_nb_inputs == 0 )
48     {
49         /* Recreate the output using the new format. */
50         if ( aout_OutputNew( p_aout, p_format ) < 0 )
51         {
52             free( p_input );
53             return NULL;
54         }
55     }
56     else
57     {
58         aout_MixerDelete( p_aout );
59     }
60
61     memcpy( &p_input->input, p_format,
62             sizeof(audio_sample_format_t) );
63     aout_FormatPrepare( &p_input->input );
64
65     /* Prepare FIFO. */
66     aout_FifoInit( p_aout, &p_input->fifo );
67     p_input->p_first_byte_to_mix = NULL;
68
69     /* Create filters. */
70     if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
71                                      &p_input->i_nb_filters, &p_input->input,
72                                      &p_aout->mixer.mixer ) < 0 )
73     {
74         msg_Err( p_aout, "couldn't set an input pipeline" );
75
76         aout_FifoDestroy( p_aout, &p_input->fifo );
77
78         free( p_input );
79
80         if ( !p_aout->i_nb_inputs )
81         {
82             aout_OutputDelete( p_aout );
83         }
84         return NULL;
85     }
86
87     p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
88     p_aout->i_nb_inputs++;
89
90     if ( aout_MixerNew( p_aout ) < 0 )
91     {
92         p_aout->i_nb_inputs--;
93         aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
94                                      p_input->i_nb_filters );
95         aout_FifoDestroy( p_aout, &p_input->fifo );
96
97         free( p_input );
98
99         if ( !p_aout->i_nb_inputs )
100         {
101             aout_OutputDelete( p_aout );
102         }
103         else
104         {
105             aout_MixerNew( p_aout );
106         }
107         vlc_mutex_unlock( &p_aout->mixer_lock );
108
109         return NULL;
110     }
111
112     vlc_mutex_unlock( &p_aout->mixer_lock );
113
114     /* Prepare hints for the buffer allocator. */
115     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
116     p_input->input_alloc.i_bytes_per_sec = -1;
117
118     aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
119                              p_input->i_nb_filters,
120                              &p_input->input_alloc );
121
122     /* i_bytes_per_sec is still == -1 if no filters */
123     p_input->input_alloc.i_bytes_per_sec = __MAX(
124                                     p_input->input_alloc.i_bytes_per_sec,
125                                     p_input->input.i_bytes_per_frame
126                                      * p_input->input.i_rate
127                                      / p_input->input.i_frame_length );
128     /* Allocate in the heap, it is more convenient for the decoder. */
129     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
130
131     msg_Dbg( p_aout, "input 0x%x created", p_input );
132     return p_input;
133 }
134
135 aout_input_t * __aout_InputNew( vlc_object_t * p_this,
136                                 aout_instance_t ** pp_aout,
137                                 audio_sample_format_t * p_format )
138 {
139     /* Create an audio output if there is none. */
140     *pp_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
141
142     if( *pp_aout == NULL )
143     {
144         msg_Dbg( p_this, "no aout present, spawning one" );
145
146         *pp_aout = aout_NewInstance( p_this );
147         /* Everything failed, I'm a loser, I just wanna die */
148         if( *pp_aout == NULL )
149         {
150             return NULL;
151         }
152     }
153     else
154     {
155         vlc_object_release( *pp_aout );
156     }
157
158     return InputNew( *pp_aout, p_format );
159 }
160
161 /*****************************************************************************
162  * aout_InputDelete : delete an input
163  *****************************************************************************/
164 void aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
165 {
166     int i_input;
167
168     vlc_mutex_lock( &p_aout->mixer_lock );
169
170     for ( i_input = 0; i_input < p_aout->i_nb_inputs; i_input++ )
171     {
172         if ( p_aout->pp_inputs[i_input] == p_input )
173         {
174             break;
175         }
176     }
177
178     if ( i_input == p_aout->i_nb_inputs )
179     {
180         msg_Err( p_aout, "cannot find an input to delete" );
181         return;
182     }
183
184     /* Remove the input from the list. */
185     memmove( &p_aout->pp_inputs[i_input], &p_aout->pp_inputs[i_input + 1],
186              (AOUT_MAX_INPUTS - i_input - 1) * sizeof(aout_input_t *) );
187     p_aout->i_nb_inputs--;
188
189     vlc_mutex_unlock( &p_aout->mixer_lock );
190
191     aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
192                                  p_input->i_nb_filters );
193     aout_FifoDestroy( p_aout, &p_input->fifo );
194
195     free( p_input );
196
197     if ( !p_aout->i_nb_inputs )
198     {
199         aout_OutputDelete( p_aout );
200         aout_MixerDelete( p_aout );
201     }
202
203     msg_Dbg( p_aout, "input 0x%x destroyed", p_input );
204 }
205
206 /*****************************************************************************
207  * aout_InputPlay : play a buffer
208  *****************************************************************************/
209 void aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
210                      aout_buffer_t * p_buffer )
211 {
212     mtime_t start_date, duration;
213
214     vlc_mutex_lock( &p_aout->input_lock );
215     while( p_aout->b_change_requested )
216     {
217         vlc_cond_wait( &p_aout->input_signal, &p_aout->input_lock );
218     }
219     p_aout->i_inputs_active++;
220     vlc_mutex_unlock( &p_aout->input_lock );
221
222     /* We don't care if someone changes the start date behind our back after
223      * aout_FifoNextStart. aout_FifoPush will deal with that, and we will
224      * compensate with the next incoming buffer. */
225     start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
226
227     if ( start_date != 0 && start_date < mdate() )
228     {
229         /* The decoder is _very_ late. This can only happen if the user
230          * pauses the stream (or if the decoder is buggy, which cannot
231          * happen :). */
232         msg_Warn( p_aout, "Computed PTS is out of range (%lld), clearing out",
233                   start_date );
234         vlc_mutex_lock( &p_aout->mixer_lock );
235         aout_FifoSet( p_aout, &p_input->fifo, 0 );
236         vlc_mutex_unlock( &p_aout->mixer_lock );
237         start_date = 0;
238     } 
239
240     if ( p_buffer->start_date < mdate() )
241     {
242         /* The decoder gives us f*cked up PTS. It's its business, but we
243          * can't present it anyway, so drop the buffer. */
244         msg_Warn( p_aout, "PTS is out of range (%lld), dropping buffer",
245                   mdate() - p_buffer->start_date );
246         aout_BufferFree( p_buffer );
247
248         vlc_mutex_lock( &p_aout->input_lock );
249         p_aout->i_inputs_active--;
250         vlc_cond_broadcast( &p_aout->input_signal );
251         vlc_mutex_unlock( &p_aout->input_lock );
252         return;
253     }
254
255     if ( start_date == 0 ) start_date = p_buffer->start_date;
256
257     if ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
258           || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE )
259     {
260         /* Can happen in several circumstances :
261          * 1. A problem at the input (clock drift)
262          * 2. A small pause triggered by the user
263          * 3. Some delay in the output stage, causing a loss of lip
264          *    synchronization
265          * Solution : resample the buffer to avoid a scratch.
266          */
267         audio_sample_format_t new_output;
268         int i_ratio, i_nb_filters;
269         mtime_t old_duration;
270         aout_filter_t * pp_filters[AOUT_MAX_FILTERS];
271         aout_alloc_t dummy_alloc;
272         mtime_t drift = p_buffer->start_date - start_date;
273
274         msg_Warn( p_aout, "buffer is %lld %s, resampling",
275                          drift > 0 ? drift : -drift,
276                          drift > 0 ? "in advance" : "late" );
277         memcpy( &new_output, &p_aout->mixer.mixer,
278                 sizeof(audio_sample_format_t) );
279         old_duration = p_buffer->end_date - p_buffer->start_date;
280         duration = p_buffer->end_date - start_date;
281         i_ratio = duration * 100 / old_duration;
282         /* If the ratio is too != 100, the sound quality will be awful. */
283         if ( i_ratio < 90 /* % */ )
284         {
285             duration = old_duration * 90 / 100;
286         }
287         if ( i_ratio > 110 /* % */ )
288         {
289             duration = old_duration * 110 / 100;
290         }
291         new_output.i_rate = new_output.i_rate * old_duration / duration;
292
293         if ( aout_FiltersCreatePipeline( p_aout, pp_filters,
294                                          &i_nb_filters, &p_input->input,
295                                          &new_output ) < 0 )
296         {
297             msg_Err( p_aout, "couldn't set an input pipeline for resampling" );
298             vlc_mutex_lock( &p_aout->mixer_lock );
299             aout_FifoSet( p_aout, &p_input->fifo, 0 );
300             vlc_mutex_unlock( &p_aout->mixer_lock );
301             aout_BufferFree( p_buffer );
302
303             vlc_mutex_lock( &p_aout->input_lock );
304             p_aout->i_inputs_active--;
305             vlc_cond_broadcast( &p_aout->input_signal );
306             vlc_mutex_unlock( &p_aout->input_lock );
307             return;
308         }
309
310         dummy_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
311         dummy_alloc.i_bytes_per_sec = -1;
312         aout_FiltersHintBuffers( p_aout, pp_filters, i_nb_filters,
313                                  &dummy_alloc );
314
315         aout_FiltersPlay( p_aout, pp_filters, i_nb_filters,
316                           &p_buffer );
317
318         aout_FiltersDestroyPipeline( p_aout, pp_filters,
319                                      i_nb_filters );
320     }
321     else
322     {
323         /* No resampling needed (except maybe the one imposed by the
324          * output). */
325         duration = p_buffer->end_date - p_buffer->start_date;
326         aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
327                           &p_buffer );
328     }
329
330     /* Adding the start date will be managed by aout_FifoPush(). */
331     p_buffer->start_date = start_date;
332     p_buffer->end_date = start_date + duration;
333     vlc_mutex_lock( &p_aout->mixer_lock );
334     aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
335     vlc_mutex_unlock( &p_aout->mixer_lock );
336
337     vlc_mutex_lock( &p_aout->input_lock );
338     p_aout->i_inputs_active--;
339     vlc_cond_broadcast( &p_aout->input_signal );
340     vlc_mutex_unlock( &p_aout->input_lock );
341 }