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