[vlc-commits] audiobargraph: use variable & callback for audio values (refs #9491)

Rémi Denis-Courmont git at videolan.org
Thu Mar 20 22:11:03 CET 2014


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Thu Mar 20 23:07:22 2014 +0200| [fe52be6f720c83a7a6bdea0ed4bbaa5d39fdea97] | committer: Rémi Denis-Courmont

audiobargraph: use variable & callback for audio values (refs #9491)

Also fix potential buffer overflow with snprintf().

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=fe52be6f720c83a7a6bdea0ed4bbaa5d39fdea97
---

 modules/audio_filter/audiobargraph_a.c |   28 +++++++++++++++-------------
 modules/video_filter/audiobargraph_v.c |   28 ++++++++++++++++------------
 2 files changed, 31 insertions(+), 25 deletions(-)

diff --git a/modules/audio_filter/audiobargraph_a.c b/modules/audio_filter/audiobargraph_a.c
index a83f9e0..31d141f9 100644
--- a/modules/audio_filter/audiobargraph_a.c
+++ b/modules/audio_filter/audiobargraph_a.c
@@ -159,6 +159,7 @@ static int Open( vlc_object_t *p_this )
     p_filter->pf_audio_filter = DoWork;
 
     var_Create( p_filter->p_libvlc, "audiobargraph_v-alarm", VLC_VAR_BOOL );
+    var_Create( p_filter->p_libvlc, "audiobargraph_v-i_values", VLC_VAR_STRING );
 
     return VLC_SUCCESS;
 }
@@ -169,12 +170,11 @@ static int Open( vlc_object_t *p_this )
 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
 {
     filter_sys_t *p_sys = p_filter->p_sys;
-    int i, j;
+    int i;
     float *p_sample = (float *)p_in_buf->p_buffer;
     float i_value[AOUT_CHAN_MAX];
     float ch;
     float max = 0.0;
-    char message[255];
     int nbChannels = 0;
     ValueDate_t* current = NULL;
     float sum;
@@ -190,7 +190,7 @@ static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
     /* 1 - Compute the peack values */
     for ( i = 0 ; i < (int)(p_in_buf->i_nb_samples); i++ )
     {
-        for (j=0; j<nbChannels; j++) {
+        for (int j = 0; j<nbChannels; j++) {
             ch = (*p_sample++);
             if (ch > i_value[j])
                 i_value[j] = ch;
@@ -255,18 +255,20 @@ static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
     if (p_sys->bargraph) {
         /* 6 - sent the message with the values for the BarGraph */
         if ((nbChannels > 0) && (p_sys->counter%(p_sys->bargraph_repetition) == 0)) {
-            j=snprintf(message,255,"@audiobargraph_v audiobargraph_v-i_values ");
-            for (i=0; i<(nbChannels-1); i++) {
-                j+=snprintf(message+j,255,"%f:", i_value[i]);
-            }
-            snprintf(message+j,255,"%f\n", i_value[nbChannels-1]);
-            msg_Dbg( p_filter, "message values : %s", message );
+            char message[256];
+            size_t j = 0;
 
-            //test = send(p_sys->TCPconnection,message,strlen(message),0);
-            //net_Write(p_filter, p_sys->TCPconnection, NULL, message, strlen(message));
+            for (i = 0; i < nbChannels; i++) {
+                if (j >= sizeof (message))
+                    break;
+                j += snprintf(message + j, sizeof (message),"%f:", i_value[i]);
+            }
 
-            net_Write(p_filter, p_sys->TCPconnection, NULL, message, strlen(message));
+            message[--j] = '\0';
+            msg_Dbg( p_filter, "values: %s", message );
 
+            var_SetString(p_filter->p_libvlc, "audiobargraph_v-i_values",
+                          message);
         }
     }
 
@@ -278,7 +280,6 @@ static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
         p_sys->counter = 0;
     }
 
-    //free(message);
     p_sys->counter++;
 
     return p_in_buf;
@@ -293,6 +294,7 @@ static void Close( vlc_object_t *p_this )
     filter_sys_t *p_sys = p_filter->p_sys;
     ValueDate_t* current;
 
+    var_Destroy( p_filter->p_libvlc, "audiobargraph_v-i_values" );
     var_Destroy( p_filter->p_libvlc, "audiobargraph_v-alarm" );
 
     p_sys->last = NULL;
diff --git a/modules/video_filter/audiobargraph_v.c b/modules/video_filter/audiobargraph_v.c
index 64d5dbd..8df7a10 100644
--- a/modules/video_filter/audiobargraph_v.c
+++ b/modules/video_filter/audiobargraph_v.c
@@ -45,9 +45,6 @@
  * Module descriptor
  *****************************************************************************/
 
-#define I_VALUES_TEXT N_("Value of the audio channels levels")
-#define I_VALUES_LONGTEXT N_("Value of the audio level of each channels between 0 and 1. " \
-    "Each level should be separated with ':'.")
 #define POSX_TEXT N_("X coordinate")
 #define POSX_LONGTEXT N_("X coordinate of the bargraph." )
 #define POSY_TEXT N_("Y coordinate")
@@ -86,7 +83,7 @@ vlc_module_begin ()
     set_shortname( N_("Audio Bar Graph Video") )
     add_shortcut( "audiobargraph_v" )
 
-    add_string( CFG_PREFIX "i_values", NULL, I_VALUES_TEXT, I_VALUES_LONGTEXT, false )
+    add_obsolete_string( CFG_PREFIX "i_values" )
     add_integer( CFG_PREFIX "x", 0, POSX_TEXT, POSX_LONGTEXT, true )
     add_integer( CFG_PREFIX "y", 0, POSY_TEXT, POSY_LONGTEXT, true )
     add_integer_with_range( CFG_PREFIX "transparency", 255, 0, 255,
@@ -146,11 +143,10 @@ struct filter_sys_t
 };
 
 static const char *const ppsz_filter_options[] = {
-    "i_values", "x", "y", "transparency", "position", "barWidth", NULL
+    "x", "y", "transparency", "position", "barWidth", NULL
 };
 
 static const char *const ppsz_filter_callbacks[] = {
-    "audiobargraph_v-i_values",
     "audiobargraph_v-x",
     "audiobargraph_v-y",
     "audiobargraph_v-transparency",
@@ -238,10 +234,9 @@ static int OpenCommon( vlc_object_t *p_this, bool b_sub )
     p_BarGraph->i_alpha = var_CreateGetIntegerCommand( p_filter,
                                                         "audiobargraph_v-transparency" );
     p_BarGraph->i_alpha = VLC_CLIP( p_BarGraph->i_alpha, 0, 255 );
-    i_values = var_CreateGetStringCommand( p_filter, "audiobargraph_v-i_values" );
     //p_BarGraph->nbChannels = 0;
     //p_BarGraph->i_values = NULL;
-    parse_i_values(p_BarGraph, i_values);
+    parse_i_values(p_BarGraph, &(char){ 0 });
     p_BarGraph->alarm = false;
 
     p_BarGraph->barWidth = var_CreateGetIntegerCommand( p_filter, "audiobargraph_v-barWidth" );
@@ -254,10 +249,15 @@ static int OpenCommon( vlc_object_t *p_this, bool b_sub )
     vlc_mutex_init( &p_sys->lock );
 
     var_Create(p_filter->p_libvlc, "audiobargraph_v-alarm", VLC_VAR_BOOL);
+    var_Create(p_filter->p_libvlc, "audiobargraph_v-i_values", VLC_VAR_STRING);
+
     var_AddCallback(p_filter->p_libvlc, "audiobargraph_v-alarm",
                     BarGraphCallback, p_sys);
+    var_AddCallback(p_filter->p_libvlc, "audiobargraph_v-i_values",
+                    BarGraphCallback, p_sys);
 
     var_TriggerCallback(p_filter->p_libvlc, "audiobargraph_v-alarm");
+    var_TriggerCallback(p_filter->p_libvlc, "audiobargraph_v-i_values");
 
     for( int i = 0; ppsz_filter_callbacks[i]; i++ )
         var_AddCallback( p_filter, ppsz_filter_callbacks[i],
@@ -290,8 +290,11 @@ static void Close( vlc_object_t *p_this )
         var_DelCallback( p_filter, ppsz_filter_callbacks[i],
                          BarGraphCallback, p_sys );
 
+    var_DelCallback(p_filter->p_libvlc, "audiobargraph_v-i_values",
+                    BarGraphCallback, p_sys);
     var_DelCallback(p_filter->p_libvlc, "audiobargraph_v-alarm",
                     BarGraphCallback, p_sys);
+    var_Destroy(p_filter->p_libvlc, "audiobargraph_v-i_values");
     var_Destroy(p_filter->p_libvlc, "audiobargraph_v-alarm");
 
     if( p_sys->p_blend )
@@ -495,16 +498,17 @@ static int BarGraphCallback( vlc_object_t *p_this, char const *psz_var,
             picture_Release( p_BarGraph->p_pic );
             p_BarGraph->p_pic = NULL;
         }
-        char *psz_i_values = strdup( newval.psz_string );
+
+        char *psz = xstrdup( newval.psz_string ? newval.psz_string : "" );
         free(p_BarGraph->i_values);
         //p_BarGraph->i_values = NULL;
         //p_BarGraph->nbChannels = 0;
         // in case many answer are received at the same time, only keep one
-        res = strchr(psz_i_values, '@');
+        res = strchr(psz, '@');
         if (res)
             *res = 0;
-        parse_i_values( p_BarGraph, psz_i_values);
-        free( psz_i_values );
+        parse_i_values( p_BarGraph, psz);
+        free( psz );
         LoadBarGraph(p_this,p_BarGraph);
     }
     else if ( !strcmp( psz_var, "audiobargraph_v-alarm" ) )



More information about the vlc-commits mailing list