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