]> git.sesse.net Git - vlc/blobdiff - src/audio_output/input.c
Fix mem leak caught by valgrind.
[vlc] / src / audio_output / input.c
index 7109ce1a1e5a8ab607764cfdcba366ea6b33bb1d..e0eb882ab9511905466cb210008d33ba0277181b 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * input.c : internal management of input streams for the audio output
  *****************************************************************************
- * Copyright (C) 2002 VideoLAN
- * $Id: input.c,v 1.35 2003/08/18 13:16:43 zorglub Exp $
+ * Copyright (C) 2002-2004 VideoLAN
+ * $Id: input.c,v 1.43 2004/01/06 12:02:05 zorglub Exp $
  *
  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  *
@@ -28,6 +28,7 @@
 #include <string.h>
 
 #include <vlc/vlc.h>
+#include <vlc/input.h>                 /* for input_thread_t and i_pts_delay */
 
 #ifdef HAVE_ALLOCA_H
 #   include <alloca.h>
 #include "audio_output.h"
 #include "aout_internal.h"
 
-typedef struct user_filter_t
-{
-    char *psz_name;
-    aout_filter_t * p_filter;
-    audio_sample_format_t filter_inter_format;
-    struct user_filter_t *p_next;
-} user_filter_t;
-           
+static int VisualizationCallback( vlc_object_t *, char const *,
+                                vlc_value_t, vlc_value_t, void * );
+static aout_filter_t * allocateUserChannelMixer( aout_instance_t *,
+                                                 audio_sample_format_t *,
+                                                 audio_sample_format_t * );
 
 /*****************************************************************************
  * aout_InputNew : allocate a new input and rework the filter pipeline
  *****************************************************************************/
 int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input )
 {
-    audio_sample_format_t intermediate_format;
-#if 0
-    headphone_intermediate_format;
-    aout_filter_t * p_headphone_filter;
-    vlc_bool_t b_use_headphone_filter = VLC_FALSE;
-#endif
-    
-    vlc_bool_t b_end=VLC_FALSE;
-    
-    user_filter_t* p_first_filter=NULL;
-    user_filter_t* p_current_filter=NULL;
-    
-    char * psz_filters, *psz_eof;
-    
+    audio_sample_format_t user_filter_format;
+    audio_sample_format_t intermediate_format;/* input of resampler */
+    vlc_value_t val, text;
+    char * psz_filters;
+    aout_filter_t * p_user_channel_mixer;
+
     aout_FormatPrint( p_aout, "input", &p_input->input );
 
     /* Prepare FIFO. */
@@ -75,243 +65,147 @@ int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input )
             sizeof(audio_sample_format_t) );
     intermediate_format.i_rate = p_input->input.i_rate;
 
-
-    /* Build the list of user filters */
-    psz_filters = config_GetPsz( p_aout , "audio-filter" );
-        
-    p_first_filter = (user_filter_t *)malloc( sizeof( user_filter_t ) );
-    if( !p_first_filter )
-    {
-        msg_Err( p_aout, "Out of memory" );
-        return -1;
-    } 
-    p_current_filter = p_first_filter;
-    p_current_filter->p_next = NULL;
-    memcpy( &p_current_filter->filter_inter_format,
-                        &p_aout->mixer.mixer,sizeof(audio_sample_format_t));
-    p_current_filter->filter_inter_format.i_rate= p_input->input.i_rate;
-    
-    if(psz_filters != NULL)
-    {
-        msg_Dbg(p_aout,"Building list of user filters"); 
-        while(1)
-        {    
-            psz_eof = strchr( psz_filters , ','  );
-            if( !psz_eof )
-            {
-                b_end = VLC_TRUE;
-                psz_eof = strchr( psz_filters,'\0');
-            }
-            if( psz_eof )
-            {
-                *psz_eof = '\0';
-            }
-            
-            msg_Dbg(p_aout,"Adding user filter: %s",psz_filters);
-
-            /* Append the new filter to the list */
-            p_current_filter->p_next =
-                    (user_filter_t *)malloc(sizeof(user_filter_t));
-            if( !p_current_filter->p_next )
-            {
-                msg_Err( p_aout, "Out of memory" );
-                return -1;
-            }
-            
-            memcpy( &p_current_filter->p_next->filter_inter_format,
-                    &p_current_filter->filter_inter_format,
-                    sizeof(audio_sample_format_t) );
-            
-            p_current_filter->p_next->filter_inter_format.i_rate = 
-                    p_current_filter->filter_inter_format.i_rate;
-            
-            p_current_filter->p_next->
-                    filter_inter_format.i_physical_channels =
-                    p_current_filter->filter_inter_format.i_physical_channels;
-            
-            p_current_filter->p_next->
-                    filter_inter_format.i_original_channels =
-                    p_current_filter->filter_inter_format.i_original_channels;
-
-            p_current_filter->p_next->filter_inter_format.i_bytes_per_frame =
-             p_current_filter->p_next->
-                filter_inter_format.i_bytes_per_frame *
-                aout_FormatNbChannels(&p_current_filter->p_next->
-                                      filter_inter_format) / 
-                aout_FormatNbChannels( &intermediate_format);
-    
-            /* Go to next filter */
-            p_current_filter = p_current_filter->p_next;
-            
-            p_current_filter->p_next=  NULL;
-            
-            p_current_filter->psz_name = strdup(psz_filters);
-            
-            psz_filters = psz_eof;
-            
-            psz_filters++;
-           
-           if(!*psz_filters || b_end == VLC_TRUE)  
-                break;
-        }
-    }
-
-#if 0 
-    /* Headphone filter add-ons. */
-
-    memcpy( &headphone_intermediate_format, &p_aout->mixer.mixer,
-           sizeof(audio_sample_format_t) );
-    headphone_intermediate_format.i_rate = 
-                p_input->input.i_rate;
-    if ( config_GetInt( p_aout , "headphone-opt" ) )
+    /* Try to use the channel mixer chosen by the user */
+    memcpy ( &user_filter_format, &intermediate_format,
+             sizeof(audio_sample_format_t) );
+    user_filter_format.i_physical_channels = p_input->input.i_physical_channels;
+    user_filter_format.i_original_channels = p_input->input.i_original_channels;
+    user_filter_format.i_bytes_per_frame = user_filter_format.i_bytes_per_frame
+                              * aout_FormatNbChannels( &user_filter_format )
+                              / aout_FormatNbChannels( &intermediate_format );
+    p_user_channel_mixer = allocateUserChannelMixer( p_aout, &user_filter_format,
+                                                   &intermediate_format );
+    /* If it failed, let the main pipeline do channel mixing */
+    if ( ! p_user_channel_mixer )
     {
-        /* Do we use heaphone filter ? */
-        if ( intermediate_format.i_physical_channels
-                == ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT )
-            && ( intermediate_format.i_format != VLC_FOURCC('f','l','3','2')
-                || intermediate_format.i_format != VLC_FOURCC('f','i','3','2')
-                ) )
-        {
-            b_use_headphone_filter = VLC_TRUE;
-        }
-    }
-    if ( b_use_headphone_filter == VLC_TRUE )
-    {
-        /* Split the filter pipeline. */
-        headphone_intermediate_format.i_physical_channels = 
-                p_input->input.i_physical_channels;
-        headphone_intermediate_format.i_original_channels = 
-                  p_input->input.i_original_channels;
-        headphone_intermediate_format.i_bytes_per_frame =
-                headphone_intermediate_format.i_bytes_per_frame
-                * aout_FormatNbChannels( &headphone_intermediate_format )
-                / aout_FormatNbChannels( &intermediate_format );
+        memcpy ( &user_filter_format, &intermediate_format,
+                 sizeof(audio_sample_format_t) );
     }
 
     /* Create filters. */
     if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
-                                     &p_input->i_nb_filters, &p_input->input,
-                                     &intermediate_format ) < 0 )
+                                     &p_input->i_nb_filters,
+                                     &p_input->input,
+                                     &user_filter_format
+                                     ) < 0 )
     {
         msg_Err( p_aout, "couldn't set an input pipeline" );
 
         aout_FifoDestroy( p_aout, &p_input->fifo );
         p_input->b_error = 1;
-
         return -1;
     }
-#endif
-    /* Create filters. */
-    if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
-                                     &p_input->i_nb_filters,
-                                     &p_input->input,
-                                     &intermediate_format
-                                     ) < 0 )
+
+    /* Now add user filters */
+    if( var_Type( p_aout, "visual" ) == 0 )
     {
-        msg_Err( p_aout, "couldn't set an input pipeline" );
+        var_Create( p_aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
+        text.psz_string = _("Visualizations");
+        var_Change( p_aout, "visual", VLC_VAR_SETTEXT, &text, NULL );
+        val.psz_string = ""; text.psz_string = _("Disable");
+        var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
+        val.psz_string = "random"; text.psz_string = _("Random");
+        var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
+        val.psz_string = "scope"; text.psz_string = _("Scope");
+        var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
+        val.psz_string = "spectrum"; text.psz_string = _("Spectrum");
+        var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
+        /* val.psz_string = "goom"; text.psz_string = _("Goom");
+        var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );*/
+        if( var_Get( p_aout, "effect-list", &val ) == VLC_SUCCESS )
+        {
+            var_Set( p_aout, "visual", val );
+            if( val.psz_string ) free( val.psz_string );
+        }
+        var_AddCallback( p_aout, "visual", VisualizationCallback, NULL );
+    }
 
-        aout_FifoDestroy( p_aout, &p_input->fifo );
-        p_input->b_error = 1;
-     }
+    if( var_Type( p_aout, "audio-filter" ) == 0 )
+    {
+        var_Create( p_aout, "audio-filter",
+                    VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+        text.psz_string = _("Audio filters");
+        var_Change( p_aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL );
+    }
 
-    if( p_first_filter->p_next)   
+    var_Get( p_aout, "audio-filter", &val );
+    psz_filters = val.psz_string;
+    if( psz_filters && *psz_filters )
     {
-        msg_Dbg(p_aout,"Searching user filters...");
-        p_current_filter = p_first_filter->p_next;
-    
-        /* Ok, we now start to get the filters, from the first one */ 
-        while( p_current_filter )
+        char *psz_parser = psz_filters;
+        char *psz_next;
+
+        while( psz_parser && *psz_parser )
         {
+            aout_filter_t * p_filter;
+
+            if( p_input->i_nb_filters >= AOUT_MAX_FILTERS )
+            {
+                msg_Dbg( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
+                break;
+            }
+
+            while( *psz_parser == ' ' && *psz_parser == ',' )
+            {
+                psz_parser++;
+            }
+            if( ( psz_next = strchr( psz_parser , ','  ) ) )
+            {
+                *psz_next++ = '\0';
+            }
+            if( *psz_parser =='\0' )
+            {
+                break;
+            }
+
+            msg_Dbg( p_aout, "user filter %s", psz_parser );
+
             /* Create a VLC object */
-            p_current_filter->p_filter = vlc_object_create( p_aout,
-                        sizeof(aout_filter_t) );
-            if(p_current_filter->p_filter == NULL )
-            { 
-                msg_Err( p_aout, "couldn't open the requested filter module" );
-                aout_FifoDestroy( p_aout, &p_input->fifo );
-                p_input->b_error = 1;
-                return -1;
+            p_filter = vlc_object_create( p_aout, sizeof(aout_filter_t) );
+            if( p_filter == NULL )
+            {
+                msg_Err( p_aout, "cannot add user filter %s (skipped)",
+                         psz_parser );
+                psz_parser = psz_next;
+                continue;
             }
-            vlc_object_attach( p_current_filter->p_filter , p_aout );
-            memcpy(&p_current_filter->p_filter->input,
-                   &p_current_filter->filter_inter_format,
-                   sizeof(audio_sample_format_t) );
-
-           if( p_current_filter->p_next) 
-           {
-                memcpy(&p_current_filter->p_filter->output,
-                       &p_current_filter->p_next->filter_inter_format,
-                       sizeof(audio_sample_format_t) );
-           }
-           else
-           {
-                 memcpy(&p_current_filter->p_filter->output,
-                        &intermediate_format,
-                        sizeof(audio_sample_format_t) );
-           }
-           p_current_filter->p_filter->p_module =
-                     module_Need(p_current_filter->p_filter,"audio filter",
-                                 p_current_filter->psz_name);
-           if(p_current_filter->p_filter->p_module== NULL )
-           {
-                vlc_object_detach( p_current_filter->p_filter);
-                vlc_object_destroy( p_current_filter->p_filter);
-  
-                msg_Err( p_aout, "couldn't open the requested module" );
-                aout_FifoDestroy( p_aout, &p_input->fifo );
-                p_input->b_error = 1;
-                return -1;
+
+            vlc_object_attach( p_filter , p_aout );
+            memcpy( &p_filter->input, &user_filter_format,
+                    sizeof(audio_sample_format_t) );
+            memcpy( &p_filter->output, &user_filter_format,
+                    sizeof(audio_sample_format_t) );
+
+            p_filter->p_module =
+                module_Need( p_filter,"audio filter", psz_parser );
+
+            if( p_filter->p_module== NULL )
+            {
+                msg_Err( p_aout, "cannot add user filter %s (skipped)",
+                         psz_parser );
+
+                vlc_object_detach( p_filter );
+                vlc_object_destroy( p_filter );
+                psz_parser = psz_next;
+                continue;
+
             }
-            /* success */ 
-            p_current_filter->p_filter->b_continuity = VLC_FALSE;
-            p_input->pp_filters[p_input->i_nb_filters++] =
-                                p_current_filter->p_filter;
+            p_filter->b_continuity = VLC_FALSE;
+
+            p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
 
-            /* Go to next */
-            p_current_filter = p_current_filter->p_next;
+            /* next filter if any */
+            psz_parser = psz_next;
         }
     }
+    if( psz_filters ) free( psz_filters );
 
-#if 0
-     /* Headphone filter add-ons. */
-    if ( b_use_headphone_filter == VLC_TRUE )
+    /* Attach the user channel mixer */
+    if ( p_user_channel_mixer )
     {
-        /* create a vlc object */
-        p_headphone_filter = vlc_object_create( p_aout
-                , sizeof(aout_filter_t) );
-        if ( p_headphone_filter == NULL )
-        {
-            msg_Err( p_aout, "couldn't open the headphone virtual spatialization module" );
-            aout_FifoDestroy( p_aout, &p_input->fifo );
-            p_input->b_error = 1;
-            return -1;
-        }
-        vlc_object_attach( p_headphone_filter, p_aout );
-
-        /* find the headphone filter */
-        memcpy( &p_headphone_filter->input, &headphone_intermediate_format
-                , sizeof(audio_sample_format_t) );
-        memcpy( &p_headphone_filter->output, &intermediate_format
-                , sizeof(audio_sample_format_t) );
-        p_headphone_filter->p_module = module_Need( p_headphone_filter, "audio filter"
-                , "headphone" );
-        if ( p_headphone_filter->p_module == NULL )
-        {
-            vlc_object_detach( p_headphone_filter );
-            vlc_object_destroy( p_headphone_filter );
-
-            msg_Err( p_aout, "couldn't open the headphone virtual spatialization module" );
-            aout_FifoDestroy( p_aout, &p_input->fifo );
-            p_input->b_error = 1;
-            return -1;
-        }
-
-        /* success */
-        p_headphone_filter->b_continuity = VLC_FALSE;
-        p_input->pp_filters[p_input->i_nb_filters++] = p_headphone_filter;
+        p_input->pp_filters[p_input->i_nb_filters++] = p_user_channel_mixer;
     }
-#endif
-    
+
     /* Prepare hints for the buffer allocator. */
     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
     p_input->input_alloc.i_bytes_per_sec = -1;
@@ -341,6 +235,7 @@ int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input )
             aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
                                          p_input->i_nb_filters );
             aout_FifoDestroy( p_aout, &p_input->fifo );
+            var_Destroy( p_aout, "visual" );
             p_input->b_error = 1;
 
             return -1;
@@ -550,3 +445,98 @@ int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
 
     return 0;
 }
+
+static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
+                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
+{
+    aout_instance_t *p_aout = (aout_instance_t *)p_this;
+    vlc_value_t val;
+    char *psz_mode = newval.psz_string;
+    char *psz_filter;
+
+    var_Get( p_aout, "audio-filter", &val );
+    psz_filter = val.psz_string;
+
+    if( !psz_mode || !*psz_mode )
+    {
+        val.psz_string = "";
+        var_Set( p_aout, "audio-filter", val );
+    }
+    else
+    {
+        if( !psz_filter || !*psz_filter )
+        {
+            val.psz_string = "visual";
+            var_Set( p_aout, "audio-filter", val );
+        }
+        else
+        {
+            if( strstr( psz_filter, "visual" ) == NULL )
+            {
+                psz_filter = realloc( psz_filter, strlen( psz_filter ) + 20 );
+                strcat( psz_filter, ",visual" );
+            }
+            val.psz_string = psz_filter;
+            var_Set( p_aout, "audio-filter", val );
+        }
+    }
+
+    if( psz_mode && *psz_mode )
+    {
+        vlc_value_t val;
+        val.psz_string = psz_mode;
+        var_Create( p_aout, "effect-list", VLC_VAR_STRING );
+        var_Set( p_aout, "effect-list", val);
+    }
+
+    if( psz_filter ) free( psz_filter );
+
+    aout_Restart( p_aout );
+
+    return VLC_SUCCESS;
+}
+
+static aout_filter_t * allocateUserChannelMixer( aout_instance_t * p_aout,
+                                     audio_sample_format_t * p_input_format,
+                                     audio_sample_format_t * p_output_format )
+{
+    aout_filter_t * p_channel_mixer;
+
+    /* Retreive user preferred channel mixer */
+    char * psz_name = config_GetPsz( p_aout, "audio-channel-mixer" );
+
+    /* Not specified => let the main pipeline do the mixing */
+    if ( ! psz_name ) return NULL;
+
+    /* Debug information */
+    aout_FormatsPrint( p_aout, "channel mixer", p_input_format,
+                       p_output_format );
+
+    /* Create a VLC object */
+    p_channel_mixer = vlc_object_create( p_aout, sizeof(aout_filter_t) );
+    if( p_channel_mixer == NULL )
+    {
+        msg_Err( p_aout, "cannot add user channel mixer %s", psz_name );
+        return NULL;
+    }
+    vlc_object_attach( p_channel_mixer , p_aout );
+
+    /* Attach the suitable module */
+    memcpy( &p_channel_mixer->input, p_input_format,
+                    sizeof(audio_sample_format_t) );
+    memcpy( &p_channel_mixer->output, p_output_format,
+                    sizeof(audio_sample_format_t) );
+    p_channel_mixer->p_module =
+                    module_Need( p_channel_mixer,"audio filter", psz_name );
+    if( p_channel_mixer->p_module== NULL )
+    {
+        msg_Err( p_aout, "cannot add user channel mixer %s", psz_name );
+        vlc_object_detach( p_channel_mixer );
+        vlc_object_destroy( p_channel_mixer );
+        return NULL;
+    }
+    p_channel_mixer->b_continuity = VLC_FALSE;
+
+    /* Ok */
+    return p_channel_mixer;
+}