]> git.sesse.net Git - vlc/blob - src/audio_output/input.c
Simplify + fix a potential overflow.
[vlc] / src / audio_output / input.c
1 /*****************************************************************************
2  * input.c : internal management of input streams for the audio output
3  *****************************************************************************
4  * Copyright (C) 2002-2007 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
28 #include <vlc/vlc.h>
29
30 #include <stdio.h>
31 #include <stdlib.h>                            /* calloc(), malloc(), free() */
32 #include <string.h>
33
34 #include <vlc_input.h>                 /* for input_thread_t and i_pts_delay */
35
36 #ifdef HAVE_ALLOCA_H
37 #   include <alloca.h>
38 #endif
39 #include <vlc_aout.h>
40
41 #include "aout_internal.h"
42
43 /** FIXME: Ugly but needed to access the counters */
44 #include "input/input_internal.h"
45
46 static void inputFailure( aout_instance_t *, aout_input_t *, const char * );
47 static void inputDrop( aout_instance_t *, aout_input_t * );
48 static int VisualizationCallback( vlc_object_t *, char const *,
49                                   vlc_value_t, vlc_value_t, void * );
50 static int EqualizerCallback( vlc_object_t *, char const *,
51                               vlc_value_t, vlc_value_t, void * );
52
53 /*****************************************************************************
54  * aout_InputNew : allocate a new input and rework the filter pipeline
55  *****************************************************************************/
56 int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input )
57 {
58     audio_sample_format_t chain_input_format;
59     audio_sample_format_t chain_output_format;
60     vlc_value_t val, text;
61     char * psz_filters, *psz_visual;
62     int i_visual;
63
64     aout_FormatPrint( p_aout, "input", &p_input->input );
65
66     p_input->i_nb_resamplers = p_input->i_nb_filters = 0;
67
68     /* Prepare FIFO. */
69     aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
70     p_input->p_first_byte_to_mix = NULL;
71
72     /* Prepare format structure */
73     memcpy( &chain_input_format, &p_input->input,
74             sizeof(audio_sample_format_t) );
75     memcpy( &chain_output_format, &p_aout->mixer.mixer,
76             sizeof(audio_sample_format_t) );
77     chain_output_format.i_rate = p_input->input.i_rate;
78     aout_FormatPrepare( &chain_output_format );
79
80     /* Now add user filters */
81     if( var_Type( p_aout, "visual" ) == 0 )
82     {
83         module_t *p_module;
84         var_Create( p_aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
85         text.psz_string = _("Visualizations");
86         var_Change( p_aout, "visual", VLC_VAR_SETTEXT, &text, NULL );
87         val.psz_string = ""; text.psz_string = _("Disable");
88         var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
89         val.psz_string = "spectrometer"; text.psz_string = _("Spectrometer");
90         var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
91         val.psz_string = "scope"; text.psz_string = _("Scope");
92         var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
93         val.psz_string = "spectrum"; text.psz_string = _("Spectrum");
94         var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
95
96         /* Look for goom plugin */
97         p_module = config_FindModule( VLC_OBJECT(p_aout), "goom" );
98         if( p_module )
99         {
100             val.psz_string = "goom"; text.psz_string = "Goom";
101             var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
102         }
103
104         /* Look for galaktos plugin */
105         p_module = config_FindModule( VLC_OBJECT(p_aout), "galaktos" );
106         if( p_module )
107         {
108             val.psz_string = "galaktos"; text.psz_string = "GaLaktos";
109             var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
110         }
111
112         if( var_Get( p_aout, "effect-list", &val ) == VLC_SUCCESS )
113         {
114             var_Set( p_aout, "visual", val );
115             if( val.psz_string ) free( val.psz_string );
116         }
117         var_AddCallback( p_aout, "visual", VisualizationCallback, NULL );
118     }
119
120     if( var_Type( p_aout, "equalizer" ) == 0 )
121     {
122         module_config_t *p_config;
123         int i;
124
125         p_config = config_FindConfig( VLC_OBJECT(p_aout), "equalizer-preset" );
126         if( p_config && p_config->i_list )
127         {
128                var_Create( p_aout, "equalizer",
129                            VLC_VAR_STRING | VLC_VAR_HASCHOICE );
130             text.psz_string = _("Equalizer");
131             var_Change( p_aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL );
132
133             val.psz_string = ""; text.psz_string = _("Disable");
134             var_Change( p_aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text );
135
136             for( i = 0; i < p_config->i_list; i++ )
137             {
138                 val.psz_string = (char *)p_config->ppsz_list[i];
139                 text.psz_string = (char *)p_config->ppsz_list_text[i];
140                 var_Change( p_aout, "equalizer", VLC_VAR_ADDCHOICE,
141                             &val, &text );
142             }
143
144             var_AddCallback( p_aout, "equalizer", EqualizerCallback, NULL );
145         }
146     }
147
148     if( var_Type( p_aout, "audio-filter" ) == 0 )
149     {
150         var_Create( p_aout, "audio-filter",
151                     VLC_VAR_STRING | VLC_VAR_DOINHERIT );
152         text.psz_string = _("Audio filters");
153         var_Change( p_aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL );
154     }
155     if( var_Type( p_aout, "audio-visual" ) == 0 )
156     {
157         var_Create( p_aout, "audio-visual",
158                     VLC_VAR_STRING | VLC_VAR_DOINHERIT );
159         text.psz_string = _("Audio visualizations");
160         var_Change( p_aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL );
161     }
162
163     var_Get( p_aout, "audio-filter", &val );
164     psz_filters = val.psz_string;
165     var_Get( p_aout, "audio-visual", &val );
166     psz_visual = val.psz_string;
167
168     /* parse user filter lists */
169     for( i_visual = 0; i_visual < 2; i_visual++ )
170     {
171         char *psz_next = NULL;
172         char *psz_parser = i_visual ? psz_visual : psz_filters;
173
174         if( psz_parser == NULL || !*psz_parser )
175             continue;
176
177         while( psz_parser && *psz_parser )
178         {
179             aout_filter_t * p_filter = NULL;
180
181             if( p_input->i_nb_filters >= AOUT_MAX_FILTERS )
182             {
183                 msg_Dbg( p_aout, "max filters reached (%d)", AOUT_MAX_FILTERS );
184                 break;
185             }
186
187             while( *psz_parser == ' ' && *psz_parser == ':' )
188             {
189                 psz_parser++;
190             }
191             if( ( psz_next = strchr( psz_parser , ':'  ) ) )
192             {
193                 *psz_next++ = '\0';
194             }
195             if( *psz_parser =='\0' )
196             {
197                 break;
198             }
199
200             /* Create a VLC object */
201             p_filter = vlc_object_create( p_aout, sizeof(aout_filter_t) );
202             if( p_filter == NULL )
203             {
204                 msg_Err( p_aout, "cannot add user filter %s (skipped)",
205                          psz_parser );
206                 psz_parser = psz_next;
207                 continue;
208             }
209
210             vlc_object_attach( p_filter , p_aout );
211
212             /* try to find the requested filter */
213             if( i_visual == 1 ) /* this can only be a visualization module */
214             {
215                 /* request format */
216                 memcpy( &p_filter->input, &chain_output_format,
217                         sizeof(audio_sample_format_t) );
218                 memcpy( &p_filter->output, &chain_output_format,
219                         sizeof(audio_sample_format_t) );
220
221                 p_filter->p_module = module_Need( p_filter, "visualization",
222                                                   psz_parser, VLC_TRUE );
223             }
224             else /* this can be a audio filter module as well as a visualization module */
225             {
226                 /* request format */
227                 memcpy( &p_filter->input, &chain_input_format,
228                         sizeof(audio_sample_format_t) );
229                 memcpy( &p_filter->output, &chain_output_format,
230                         sizeof(audio_sample_format_t) );
231
232                 p_filter->p_module = module_Need( p_filter, "audio filter",
233                                               psz_parser, VLC_TRUE );
234
235                 if ( p_filter->p_module == NULL )
236                 {
237                     /* if the filter requested a special format, retry */
238                     if ( !( AOUT_FMTS_IDENTICAL( &p_filter->input,
239                                                  &chain_input_format )
240                             && AOUT_FMTS_IDENTICAL( &p_filter->output,
241                                                     &chain_output_format ) ) )
242                     {
243                         aout_FormatPrepare( &p_filter->input );
244                         aout_FormatPrepare( &p_filter->output );
245                         p_filter->p_module = module_Need( p_filter,
246                                                           "audio filter",
247                                                           psz_parser, VLC_TRUE );
248                     }
249                     /* try visual filters */
250                     else
251                     {
252                         memcpy( &p_filter->input, &chain_output_format,
253                                 sizeof(audio_sample_format_t) );
254                         memcpy( &p_filter->output, &chain_output_format,
255                                 sizeof(audio_sample_format_t) );
256                         p_filter->p_module = module_Need( p_filter,
257                                                           "visualization",
258                                                           psz_parser, VLC_TRUE );
259                     }
260                 }
261             }
262
263             /* failure */
264             if ( p_filter->p_module == NULL )
265             {
266                 msg_Err( p_aout, "cannot add user filter %s (skipped)",
267                          psz_parser );
268
269                 vlc_object_detach( p_filter );
270                 vlc_object_destroy( p_filter );
271
272                 psz_parser = psz_next;
273                 continue;
274             }
275
276             /* complete the filter chain if necessary */
277             if ( !AOUT_FMTS_IDENTICAL( &chain_input_format, &p_filter->input ) )
278             {
279                 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
280                                                  &p_input->i_nb_filters,
281                                                  &chain_input_format,
282                                                  &p_filter->input ) < 0 )
283                 {
284                     msg_Err( p_aout, "cannot add user filter %s (skipped)",
285                              psz_parser );
286
287                     module_Unneed( p_filter, p_filter->p_module );
288                     vlc_object_detach( p_filter );
289                     vlc_object_destroy( p_filter );
290
291                     psz_parser = psz_next;
292                     continue;
293                 }
294             }
295
296             /* success */
297             p_filter->b_continuity = VLC_FALSE;
298             p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
299             memcpy( &chain_input_format, &p_filter->output,
300                     sizeof( audio_sample_format_t ) );
301
302             /* next filter if any */
303             psz_parser = psz_next;
304         }
305     }
306     if( psz_filters ) free( psz_filters );
307     if( psz_visual ) free( psz_visual );
308
309     /* complete the filter chain if necessary */
310     if ( !AOUT_FMTS_IDENTICAL( &chain_input_format, &chain_output_format ) )
311     {
312         if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
313                                          &p_input->i_nb_filters,
314                                          &chain_input_format,
315                                          &chain_output_format ) < 0 )
316         {
317             inputFailure( p_aout, p_input, "couldn't set an input pipeline" );
318             return -1;
319         }
320     }
321
322     /* Prepare hints for the buffer allocator. */
323     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
324     p_input->input_alloc.i_bytes_per_sec = -1;
325
326     /* Create resamplers. */
327     if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
328     {
329         chain_output_format.i_rate = (__MAX(p_input->input.i_rate,
330                                             p_aout->mixer.mixer.i_rate)
331                                  * (100 + AOUT_MAX_RESAMPLING)) / 100;
332         if ( chain_output_format.i_rate == p_aout->mixer.mixer.i_rate )
333         {
334             /* Just in case... */
335             chain_output_format.i_rate++;
336         }
337         if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
338                                          &p_input->i_nb_resamplers,
339                                          &chain_output_format,
340                                          &p_aout->mixer.mixer ) < 0 )
341         {
342             inputFailure( p_aout, p_input, "couldn't set a resampler pipeline");
343             return -1;
344         }
345
346         aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
347                                  p_input->i_nb_resamplers,
348                                  &p_input->input_alloc );
349         p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
350
351         /* Setup the initial rate of the resampler */
352         p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
353     }
354     p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
355
356     aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
357                              p_input->i_nb_filters,
358                              &p_input->input_alloc );
359     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
360
361     /* i_bytes_per_sec is still == -1 if no filters */
362     p_input->input_alloc.i_bytes_per_sec = __MAX(
363                                     p_input->input_alloc.i_bytes_per_sec,
364                                     (int)(p_input->input.i_bytes_per_frame
365                                      * p_input->input.i_rate
366                                      / p_input->input.i_frame_length) );
367
368     /* Success */
369     p_input->b_error = VLC_FALSE;
370     p_input->b_restart = VLC_FALSE;
371
372     return 0;
373 }
374
375 /*****************************************************************************
376  * aout_InputDelete : delete an input
377  *****************************************************************************
378  * This function must be entered with the mixer lock.
379  *****************************************************************************/
380 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
381 {
382     if ( p_input->b_error ) return 0;
383
384     aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
385                                  p_input->i_nb_filters );
386     p_input->i_nb_filters = 0;
387     aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
388                                  p_input->i_nb_resamplers );
389     p_input->i_nb_resamplers = 0;
390     aout_FifoDestroy( p_aout, &p_input->fifo );
391
392     return 0;
393 }
394
395 /*****************************************************************************
396  * aout_InputPlay : play a buffer
397  *****************************************************************************
398  * This function must be entered with the input lock.
399  *****************************************************************************/
400 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
401                     aout_buffer_t * p_buffer )
402 {
403     mtime_t start_date;
404
405     if( p_input->b_restart )
406     {
407         aout_fifo_t fifo, dummy_fifo;
408         byte_t      *p_first_byte_to_mix;
409
410         vlc_mutex_lock( &p_aout->mixer_lock );
411
412         /* A little trick to avoid loosing our input fifo */
413         aout_FifoInit( p_aout, &dummy_fifo, p_aout->mixer.mixer.i_rate );
414         p_first_byte_to_mix = p_input->p_first_byte_to_mix;
415         fifo = p_input->fifo;
416         p_input->fifo = dummy_fifo;
417         aout_InputDelete( p_aout, p_input );
418         aout_InputNew( p_aout, p_input );
419         p_input->p_first_byte_to_mix = p_first_byte_to_mix;
420         p_input->fifo = fifo;
421
422         vlc_mutex_unlock( &p_aout->mixer_lock );
423     }
424
425     /* We don't care if someone changes the start date behind our back after
426      * this. We'll deal with that when pushing the buffer, and compensate
427      * with the next incoming buffer. */
428     vlc_mutex_lock( &p_aout->input_fifos_lock );
429     start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
430     vlc_mutex_unlock( &p_aout->input_fifos_lock );
431
432     if ( start_date != 0 && start_date < mdate() )
433     {
434         /* The decoder is _very_ late. This can only happen if the user
435          * pauses the stream (or if the decoder is buggy, which cannot
436          * happen :). */
437         msg_Warn( p_aout, "computed PTS is out of range ("I64Fd"), "
438                   "clearing out", mdate() - start_date );
439         vlc_mutex_lock( &p_aout->input_fifos_lock );
440         aout_FifoSet( p_aout, &p_input->fifo, 0 );
441         p_input->p_first_byte_to_mix = NULL;
442         vlc_mutex_unlock( &p_aout->input_fifos_lock );
443         if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
444             msg_Warn( p_aout, "timing screwed, stopping resampling" );
445         p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
446         if ( p_input->i_nb_resamplers != 0 )
447         {
448             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
449             p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
450         }
451         start_date = 0;
452         inputDrop( p_aout, p_input );
453     }
454
455     if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
456     {
457         /* The decoder gives us f*cked up PTS. It's its business, but we
458          * can't present it anyway, so drop the buffer. */
459         msg_Warn( p_aout, "PTS is out of range ("I64Fd"), dropping buffer",
460                   mdate() - p_buffer->start_date );
461
462         inputDrop( p_aout, p_input );
463         aout_BufferFree( p_buffer );
464         p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
465         if ( p_input->i_nb_resamplers != 0 )
466         {
467             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
468             p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
469         }
470         return 0;
471     }
472
473     /* If the audio drift is too big then it's not worth trying to resample
474      * the audio. */
475     if ( start_date != 0 &&
476          ( start_date < p_buffer->start_date - 3 * AOUT_PTS_TOLERANCE ) )
477     {
478         msg_Warn( p_aout, "audio drift is too big ("I64Fd"), clearing out",
479                   start_date - p_buffer->start_date );
480         vlc_mutex_lock( &p_aout->input_fifos_lock );
481         aout_FifoSet( p_aout, &p_input->fifo, 0 );
482         p_input->p_first_byte_to_mix = NULL;
483         vlc_mutex_unlock( &p_aout->input_fifos_lock );
484         if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
485             msg_Warn( p_aout, "timing screwed, stopping resampling" );
486         p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
487         if ( p_input->i_nb_resamplers != 0 )
488         {
489             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
490             p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
491         }
492         start_date = 0;
493     }
494     else if ( start_date != 0 &&
495               ( start_date > p_buffer->start_date + 3 * AOUT_PTS_TOLERANCE ) )
496     {
497         msg_Warn( p_aout, "audio drift is too big ("I64Fd"), dropping buffer",
498                   start_date - p_buffer->start_date );
499         aout_BufferFree( p_buffer );
500         inputDrop( p_aout, p_input );
501         return 0;
502     }
503
504     if ( start_date == 0 ) start_date = p_buffer->start_date;
505
506     /* Run pre-filters. */
507
508     aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
509                       &p_buffer );
510
511     /* Run the resampler if needed.
512      * We first need to calculate the output rate of this resampler. */
513     if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
514          ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
515            || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
516          p_input->i_nb_resamplers > 0 )
517     {
518         /* Can happen in several circumstances :
519          * 1. A problem at the input (clock drift)
520          * 2. A small pause triggered by the user
521          * 3. Some delay in the output stage, causing a loss of lip
522          *    synchronization
523          * Solution : resample the buffer to avoid a scratch.
524          */
525         mtime_t drift = p_buffer->start_date - start_date;
526
527         p_input->i_resamp_start_date = mdate();
528         p_input->i_resamp_start_drift = (int)drift;
529
530         if ( drift > 0 )
531             p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
532         else
533             p_input->i_resampling_type = AOUT_RESAMPLING_UP;
534
535         msg_Warn( p_aout, "buffer is "I64Fd" %s, triggering %ssampling",
536                           drift > 0 ? drift : -drift,
537                           drift > 0 ? "in advance" : "late",
538                           drift > 0 ? "down" : "up");
539     }
540
541     if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
542     {
543         /* Resampling has been triggered previously (because of dates
544          * mismatch). We want the resampling to happen progressively so
545          * it isn't too audible to the listener. */
546
547         if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
548         {
549             p_input->pp_resamplers[0]->input.i_rate += 2; /* Hz */
550         }
551         else
552         {
553             p_input->pp_resamplers[0]->input.i_rate -= 2; /* Hz */
554         }
555
556         /* Check if everything is back to normal, in which case we can stop the
557          * resampling */
558         if( p_input->pp_resamplers[0]->input.i_rate ==
559               p_input->input.i_rate )
560         {
561             p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
562             msg_Warn( p_aout, "resampling stopped after "I64Fi" usec "
563                       "(drift: "I64Fi")",
564                       mdate() - p_input->i_resamp_start_date,
565                       p_buffer->start_date - start_date);
566         }
567         else if( abs( (int)(p_buffer->start_date - start_date) ) <
568                  abs( p_input->i_resamp_start_drift ) / 2 )
569         {
570             /* if we reduced the drift from half, then it is time to switch
571              * back the resampling direction. */
572             if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
573                 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
574             else
575                 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
576             p_input->i_resamp_start_drift = 0;
577         }
578         else if( p_input->i_resamp_start_drift &&
579                  ( abs( (int)(p_buffer->start_date - start_date) ) >
580                    abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
581         {
582             /* If the drift is increasing and not decreasing, than something
583              * is bad. We'd better stop the resampling right now. */
584             msg_Warn( p_aout, "timing screwed, stopping resampling" );
585             p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
586             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
587         }
588     }
589
590     /* Adding the start date will be managed by aout_FifoPush(). */
591     p_buffer->end_date = start_date +
592         (p_buffer->end_date - p_buffer->start_date);
593     p_buffer->start_date = start_date;
594
595     /* Actually run the resampler now. */
596     if ( p_input->i_nb_resamplers > 0 )
597     {
598         aout_FiltersPlay( p_aout, p_input->pp_resamplers,
599                           p_input->i_nb_resamplers,
600                           &p_buffer );
601     }
602
603     vlc_mutex_lock( &p_aout->input_fifos_lock );
604     aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
605     vlc_mutex_unlock( &p_aout->input_fifos_lock );
606
607     return 0;
608 }
609
610 /*****************************************************************************
611  * static functions
612  *****************************************************************************/
613
614 static void inputFailure( aout_instance_t * p_aout, aout_input_t * p_input,
615                           const char * psz_error_message )
616 {
617     /* error message */
618     msg_Err( p_aout, "%s", psz_error_message );
619
620     /* clean up */
621     aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
622                                  p_input->i_nb_filters );
623     aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
624                                  p_input->i_nb_resamplers );
625     aout_FifoDestroy( p_aout, &p_input->fifo );
626     var_Destroy( p_aout, "visual" );
627     var_Destroy( p_aout, "equalizer" );
628     var_Destroy( p_aout, "audio-filter" );
629     var_Destroy( p_aout, "audio-visual" );
630
631     /* error flag */
632     p_input->b_error = 1;
633 }
634
635 static void inputDrop( aout_instance_t *p_aout, aout_input_t *p_input )
636 {
637     if( !p_input->p_input_thread )
638         return;
639
640     vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
641     stats_UpdateInteger( p_aout, p_input->p_input_thread->p->counters.p_lost_abuffers, 1, NULL );
642     vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
643 }
644
645 static int ChangeFiltersString( aout_instance_t * p_aout, const char* psz_variable,
646                                  const char *psz_name, vlc_bool_t b_add )
647 {
648     vlc_value_t val;
649     char *psz_parser;
650
651     var_Get( p_aout, psz_variable, &val );
652
653     if( !val.psz_string ) val.psz_string = strdup("");
654
655     psz_parser = strstr( val.psz_string, psz_name );
656
657     if( b_add )
658     {
659         if( !psz_parser )
660         {
661             psz_parser = val.psz_string;
662             asprintf( &val.psz_string, (*val.psz_string) ? "%s:%s" : "%s%s",
663                       val.psz_string, psz_name );
664             free( psz_parser );
665         }
666         else
667         {
668             return 0;
669         }
670     }
671     else
672     {
673         if( psz_parser )
674         {
675             memmove( psz_parser, psz_parser + strlen(psz_name) +
676                      (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
677                      strlen(psz_parser + strlen(psz_name)) + 1 );
678         }
679         else
680         {
681             free( val.psz_string );
682             return 0;
683         }
684     }
685
686     var_Set( p_aout, psz_variable, val );
687     free( val.psz_string );
688     return 1;
689 }
690
691 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
692                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
693 {
694     aout_instance_t *p_aout = (aout_instance_t *)p_this;
695     char *psz_mode = newval.psz_string;
696     vlc_value_t val;
697     int i;
698     (void)psz_cmd; (void)oldval; (void)p_data;
699
700     if( !psz_mode || !*psz_mode )
701     {
702         ChangeFiltersString( p_aout, "audio-visual", "goom", VLC_FALSE );
703         ChangeFiltersString( p_aout, "audio-visual", "visual", VLC_FALSE );
704         ChangeFiltersString( p_aout, "audio-visual", "galaktos", VLC_FALSE );
705     }
706     else
707     {
708         if( !strcmp( "goom", psz_mode ) )
709         {
710             ChangeFiltersString( p_aout, "audio-visual", "visual", VLC_FALSE );
711             ChangeFiltersString( p_aout, "audio-visual", "goom", VLC_TRUE );
712             ChangeFiltersString( p_aout, "audio-visual", "galaktos", VLC_FALSE);
713         }
714         else if( !strcmp( "galaktos", psz_mode ) )
715         {
716             ChangeFiltersString( p_aout, "audio-visual", "visual", VLC_FALSE );
717             ChangeFiltersString( p_aout, "audio-visual", "goom", VLC_FALSE );
718             ChangeFiltersString( p_aout, "audio-visual", "galaktos", VLC_TRUE );
719         }
720         else
721         {
722             val.psz_string = psz_mode;
723             var_Create( p_aout, "effect-list", VLC_VAR_STRING );
724             var_Set( p_aout, "effect-list", val );
725
726             ChangeFiltersString( p_aout, "audio-visual", "goom", VLC_FALSE );
727             ChangeFiltersString( p_aout, "audio-visual", "visual", VLC_TRUE );
728             ChangeFiltersString( p_aout, "audio-visual", "galaktos", VLC_FALSE);
729         }
730     }
731
732     /* That sucks */
733     for( i = 0; i < p_aout->i_nb_inputs; i++ )
734     {
735         p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
736     }
737
738     return VLC_SUCCESS;
739 }
740
741 static int EqualizerCallback( vlc_object_t *p_this, char const *psz_cmd,
742                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
743 {
744     aout_instance_t *p_aout = (aout_instance_t *)p_this;
745     char *psz_mode = newval.psz_string;
746     vlc_value_t val;
747     int i;
748     int i_ret;
749     (void)psz_cmd; (void)oldval; (void)p_data;
750
751     if( !psz_mode || !*psz_mode )
752     {
753         i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
754                                      VLC_FALSE );
755     }
756     else
757     {
758         val.psz_string = psz_mode;
759         var_Create( p_aout, "equalizer-preset", VLC_VAR_STRING );
760         var_Set( p_aout, "equalizer-preset", val );
761         i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
762                                      VLC_TRUE );
763
764     }
765
766     /* That sucks */
767     if( i_ret == 1 )
768     {
769         for( i = 0; i < p_aout->i_nb_inputs; i++ )
770         {
771             p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
772         }
773     }
774
775     return VLC_SUCCESS;
776 }