Fix potential overflow in Visualizer effect am: 57ac66340a am: 63a995164c am: 38dcbab6b3 am: 624981438d am: 94cc18ee82 am: 3f11c1f175 am: 6a3492d5b5 am: d18f614902 am: 3a3a4f81a4 am: 27ff940a86 am: f3497becae am: 15d69ba580
am: 0f3d9ca91e

Change-Id: Ib38c8fcda1ee304eeb3b9aa563ea6180085174a7
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 35b726e..e68535f 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -2063,14 +2063,16 @@
             else
                 ret = pcm_write(out->pcm, (void *)buffer, bytes);
 
-            if (ret == 0)
-                out->written += bytes / (out->config.channels * sizeof(short));
-
             release_out_focus(out, ns);
         }
     }
 
 exit:
+    // For PCM we always consume the buffer and return #bytes regardless of ret.
+    if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
+        out->written += bytes / (out->config.channels * sizeof(short));
+    }
+
     pthread_mutex_unlock(&out->lock);
 
     if (ret != 0) {
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index 441e2a1..b84353d 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -1010,24 +1010,37 @@
     list_init(&operator_info_list);
 
     set_platform_defaults(my_data);
+    bool card_verifed[MAX_SND_CARD] = {0};
+    const int retry_limit = property_get_int32("audio.snd_card.open.retries", RETRY_NUMBER);
 
-    while (snd_card_num < MAX_SND_CARD) {
-        adev->mixer = mixer_open(snd_card_num);
+    for (;;) {
+        if (snd_card_num >= MAX_SND_CARD) {
+            if (retry_num++ >= retry_limit) {
+                ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
+                goto init_failed;
+            }
 
-        while (!adev->mixer && retry_num < RETRY_NUMBER) {
+            snd_card_num = 0;
             usleep(RETRY_US);
-            adev->mixer = mixer_open(snd_card_num);
-            retry_num++;
+            continue;
         }
 
+        if (card_verifed[snd_card_num]) {
+            ++snd_card_num;
+            continue;
+        }
+
+        adev->mixer = mixer_open(snd_card_num);
+
         if (!adev->mixer) {
             ALOGE("%s: Unable to open the mixer card: %d", __func__,
-                   snd_card_num);
-            retry_num = 0;
-            snd_card_num++;
+               snd_card_num);
+            ++snd_card_num;
             continue;
         }
 
+        card_verifed[snd_card_num] = true;
+
         snd_card_name = mixer_get_name(adev->mixer);
         my_data->hw_info = hw_info_init(snd_card_name);
 
@@ -1106,8 +1119,9 @@
                         min(strlen(snd_card_name), strlen(my_data->snd_card_name))) != 0) {
             ALOGI("%s: found valid sound card %s, but not primary sound card %s",
                    __func__, snd_card_name, my_data->snd_card_name);
-            retry_num = 0;
-            snd_card_num++;
+            ++snd_card_num;
+            mixer_close(adev->mixer);
+            adev->mixer = NULL;
             hw_info_deinit(my_data->hw_info);
             my_data->hw_info = NULL;
             continue;
@@ -1120,6 +1134,10 @@
 
         if (!adev->audio_route) {
             ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
+            mixer_close(adev->mixer);
+            adev->mixer = NULL;
+            hw_info_deinit(my_data->hw_info);
+            my_data->hw_info = NULL;
             goto init_failed;
         }
         adev->snd_card = snd_card_num;
@@ -1127,11 +1145,6 @@
         break;
     }
 
-    if (snd_card_num >= MAX_SND_CARD) {
-        ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
-        goto init_failed;
-    }
-
     //set max volume step for voice call
     property_get("ro.config.vc_call_vol_steps", value, TOSTRING(MAX_VOL_INDEX));
     my_data->max_vol_index = atoi(value);
diff --git a/post_proc/volume_listener.c b/post_proc/volume_listener.c
index 3367b76..4a48a25 100644
--- a/post_proc/volume_listener.c
+++ b/post_proc/volume_listener.c
@@ -32,7 +32,8 @@
 #define VOL_FLAG ( EFFECT_FLAG_TYPE_INSERT | \
                    EFFECT_FLAG_VOLUME_IND | \
                    EFFECT_FLAG_DEVICE_IND | \
-                   EFFECT_FLAG_OFFLOAD_SUPPORTED)
+                   EFFECT_FLAG_OFFLOAD_SUPPORTED | \
+                   EFFECT_FLAG_NO_PROCESS)
 
 #define PRINT_STREAM_TYPE(i) ALOGV("descriptor found and is of stream type %s ",\
                                                             i == MUSIC?"MUSIC": \
@@ -332,41 +333,6 @@
     return sample;
 }
 
-static int vol_effect_process(effect_handle_t self,
-                              audio_buffer_t *in_buffer,
-                              audio_buffer_t *out_buffer)
-{
-    int status = 0;
-    ALOGV("%s Called ", __func__);
-
-    vol_listener_context_t *context = (vol_listener_context_t *)self;
-    pthread_mutex_lock(&vol_listner_init_lock);
-
-    if (context->state != VOL_LISTENER_STATE_ACTIVE) {
-        ALOGE("%s: state is not active .. return error", __func__);
-        status = -EINVAL;
-        goto exit;
-    }
-
-    // calculation based on channel count 2
-    if (in_buffer->raw != out_buffer->raw) {
-        if (context->config.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
-            size_t i;
-            for (i = 0; i < out_buffer->frameCount*2; i++) {
-                out_buffer->s16[i] = clamp16(out_buffer->s16[i] + in_buffer->s16[i]);
-            }
-        } else {
-            memcpy(out_buffer->raw, in_buffer->raw, out_buffer->frameCount * 2 * sizeof(int16_t));
-        }
-
-    }
-
-exit:
-    pthread_mutex_unlock(&vol_listner_init_lock);
-    return status;
-}
-
-
 static int vol_effect_command(effect_handle_t self,
                               uint32_t cmd_code, uint32_t cmd_size,
                               void *p_cmd_data, uint32_t *reply_size,
@@ -818,7 +784,7 @@
 
 /* effect_handle_t interface implementation for volume listener effect */
 static const struct effect_interface_s effect_interface = {
-    vol_effect_process,
+    NULL,
     vol_effect_command,
     vol_effect_get_descriptor,
     NULL,