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