hal: fix voice call device routing issue

adev->voice_device_set flag is to indicate voice call
device routing update from policymanager to HAL. It is
set to true in voice_start_call and reset in update_calls()
which causes mismatch in flag update during back to back
voice calls scenario. Update adev->voice_device_set flag
in voice_stop_call instead of update_calls(). Rename
voice_device_set flag to in_call for readability.

Change-Id: Ie07105671f254899890bdb4c0635c7dc1f55dbff
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 87ea0d3..66c50e8 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -670,7 +670,7 @@
          * usecase. This is to avoid switching devices for voice call when
          * check_usecases_codec_backend() is called below.
          */
-        if (voice_is_in_call(adev)) {
+        if (adev->voice.in_call && adev->mode == AUDIO_MODE_IN_CALL) {
             vc_usecase = get_usecase_from_list(adev,
                                                get_voice_usecase_id_from_list(adev));
             if ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
@@ -1628,17 +1628,18 @@
                 select_devices(adev, out->usecase);
 
             if ((adev->mode == AUDIO_MODE_IN_CALL) &&
-                    !voice_is_in_call(adev) &&
-                    (out == adev->primary_output)) {
+                !adev->voice.in_call &&
+                (out == adev->primary_output)) {
                 ret = voice_start_call(adev);
-            } else if (voice_is_in_call(adev) &&
-                            (out == adev->primary_output)) {
+            } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
+                       adev->voice.in_call &&
+                       (out == adev->primary_output)) {
                 voice_update_devices_for_all_voice_usecases(adev);
             }
         }
 
         if ((adev->mode == AUDIO_MODE_NORMAL) &&
-                voice_is_in_call(adev) &&
+                adev->voice.in_call &&
                 (out == adev->primary_output)) {
             ret = voice_stop_call(adev);
         }
diff --git a/hal/voice.c b/hal/voice.c
index aa8c29a..33e0d2c 100644
--- a/hal/voice.c
+++ b/hal/voice.c
@@ -192,26 +192,27 @@
     }
 
     session->state.current = CALL_ACTIVE;
-    return 0;
+    goto done;
 
 error_start_voice:
     stop_call(adev, usecase_id);
 
+done:
     ALOGD("%s: exit: status(%d)", __func__, ret);
     return ret;
 }
 
-bool voice_is_in_call(struct audio_device *adev)
+bool voice_is_call_state_active(struct audio_device *adev)
 {
-    bool in_call = false;
+    bool call_state = false;
     int ret = 0;
 
-    ret = voice_extn_is_in_call(adev, &in_call);
+    ret = voice_extn_is_call_state_active(adev, &call_state);
     if (ret == -ENOSYS) {
-        in_call = (adev->voice.session[VOICE_SESS_IDX].state.current == CALL_ACTIVE) ? true : false;
+        call_state = (adev->voice.session[VOICE_SESS_IDX].state.current == CALL_ACTIVE) ? true : false;
     }
 
-    return in_call;
+    return call_state;
 }
 
 bool voice_is_in_call_rec_stream(struct stream_in *in)
@@ -247,7 +248,7 @@
     int usecase_id;
     int rec_mode = INCALL_REC_NONE;
 
-    if (voice_is_in_call(adev)) {
+    if (voice_is_call_state_active(adev)) {
         switch (in->source) {
         case AUDIO_SOURCE_VOICE_UPLINK:
             if (audio_extn_compr_cap_enabled() &&
@@ -373,6 +374,7 @@
     if (ret == -ENOSYS) {
         ret = start_call(adev, USECASE_VOICE_CALL);
     }
+    adev->voice.in_call = true;
 
     return ret;
 }
@@ -381,6 +383,7 @@
 {
     int ret = 0;
 
+    adev->voice.in_call = false;
     ret = voice_extn_stop_call(adev);
     if (ret == -ENOSYS) {
         ret = stop_call(adev, USECASE_VOICE_CALL);
@@ -434,7 +437,7 @@
         if (tty_mode != adev->voice.tty_mode) {
             adev->voice.tty_mode = tty_mode;
             adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
-            if (voice_is_in_call(adev))
+            if (voice_is_call_state_active(adev))
                voice_update_devices_for_all_voice_usecases(adev);
         }
     }
@@ -463,7 +466,7 @@
     adev->voice.tty_mode = TTY_MODE_OFF;
     adev->voice.volume = 1.0f;
     adev->voice.mic_mute = false;
-    adev->voice.voice_device_set = false;
+    adev->voice.in_call = false;
     for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
         adev->voice.session[i].pcm_rx = NULL;
         adev->voice.session[i].pcm_tx = NULL;
diff --git a/hal/voice.h b/hal/voice.h
index 4df2d6a..5d1ae40 100644
--- a/hal/voice.h
+++ b/hal/voice.h
@@ -60,8 +60,8 @@
     int tty_mode;
     bool mic_mute;
     float volume;
-    bool voice_device_set;
     bool is_in_call;
+    bool in_call;
 };
 
 enum {
@@ -77,8 +77,8 @@
 void voice_get_parameters(struct audio_device *adev, struct str_parms *query,
                           struct str_parms *reply);
 void voice_init(struct audio_device *adev);
-bool voice_is_in_call(struct audio_device *adev);
 bool voice_is_in_call_rec_stream(struct stream_in *in);
+bool voice_is_call_state_active(struct audio_device *adev);
 int voice_set_mic_mute(struct audio_device *dev, bool state);
 bool voice_get_mic_mute(struct audio_device *dev);
 int voice_set_volume(struct audio_device *adev, float volume);
diff --git a/hal/voice_extn/voice_extn.c b/hal/voice_extn/voice_extn.c
index b73eb31..89abc3a 100644
--- a/hal/voice_extn/voice_extn.c
+++ b/hal/voice_extn/voice_extn.c
@@ -73,7 +73,7 @@
 
 extern int start_call(struct audio_device *adev, audio_usecase_t usecase_id);
 extern int stop_call(struct audio_device *adev, audio_usecase_t usecase_id);
-int voice_extn_is_in_call(struct audio_device *adev, bool *in_call);
+int voice_extn_is_call_state_active(struct audio_device *adev, bool *is_call_active);
 
 static bool is_valid_call_state(int call_state)
 {
@@ -153,7 +153,6 @@
     struct voice_session *session = NULL;
     int fd = 0;
     int ret = 0;
-    bool is_in_call = false;
 
     ALOGD("%s: enter:", __func__);
 
@@ -213,10 +212,6 @@
                     ALOGE("%s: voice_end_call() failed for usecase: %d\n",
                           __func__, usecase_id);
                 } else {
-                    voice_extn_is_in_call(adev, &is_in_call);
-                    if (!is_in_call) {
-                        adev->voice.voice_device_set = false;
-                    }
                     session->state.current = session->state.new;
                 }
                 break;
@@ -288,8 +283,7 @@
 {
     struct voice_session *session = NULL;
     int i = 0;
-    bool is_in_call;
-    int no_of_calls_active = 0;
+    bool is_call_active;
 
     for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
         if (vsid == adev->voice.session[i].vsid) {
@@ -298,21 +292,16 @@
         }
     }
 
-    for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
-        if (CALL_INACTIVE != adev->voice.session[i].state.current)
-            no_of_calls_active++;
-    }
-
     if (session) {
         session->state.new = call_state;
-        voice_extn_is_in_call(adev, &is_in_call);
-        ALOGD("%s is_in_call:%d voice_device_set:%d, mode:%d\n",
-              __func__, is_in_call, adev->voice.voice_device_set, adev->mode);
+        voice_extn_is_call_state_active(adev, &is_call_active);
+        ALOGD("%s is_call_active:%d in_call:%d, mode:%d\n",
+              __func__, is_call_active, adev->voice.in_call, adev->mode);
         /* Dont start voice call before device routing for voice usescases has
          * occured, otherwise voice calls will be started unintendedly on
          * speaker.
          */
-        if (is_in_call || adev->voice.voice_device_set) {
+        if (is_call_active || adev->voice.in_call) {
             /* Device routing is not triggered for voice calls on the subsequent
              * subs, Hence update the call states if voice call is already
              * active on other sub.
@@ -334,16 +323,16 @@
     return 0;
 }
 
-int voice_extn_is_in_call(struct audio_device *adev, bool *in_call)
+int voice_extn_is_call_state_active(struct audio_device *adev, bool *is_call_active)
 {
     struct voice_session *session = NULL;
     int i = 0;
-    *in_call = false;
+    *is_call_active = false;
 
     for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
         session = &adev->voice.session[i];
         if(session->state.current != CALL_INACTIVE){
-            *in_call = true;
+            *is_call_active = true;
             break;
         }
     }
@@ -415,7 +404,6 @@
      * udpated.
      */
     ALOGV("%s: enter:", __func__);
-    adev->voice.voice_device_set = true;
     return update_calls(adev);
 }
 
@@ -462,6 +450,7 @@
         err = str_parms_get_int(parms, AUDIO_PARAMETER_KEY_CALL_STATE, &value);
         if (err >= 0) {
             call_state = value;
+            str_parms_del(parms, AUDIO_PARAMETER_KEY_CALL_STATE);
         } else {
             ALOGE("%s: call_state key not found", __func__);
             ret = -EINVAL;
diff --git a/hal/voice_extn/voice_extn.h b/hal/voice_extn/voice_extn.h
index 4a9c610..73da18a 100644
--- a/hal/voice_extn/voice_extn.h
+++ b/hal/voice_extn/voice_extn.h
@@ -32,8 +32,9 @@
 void voice_extn_get_parameters(const struct audio_device *adev,
                                struct str_parms *query,
                                struct str_parms *reply);
-int voice_extn_is_in_call(struct audio_device *adev, bool *in_call);
 int voice_extn_is_in_call_rec_stream(struct stream_in *in, bool *in_call_rec);
+int voice_extn_is_call_state_active(struct audio_device *adev,
+                                    bool *is_call_active);
 int voice_extn_get_active_session_id(struct audio_device *adev,
                                      uint32_t *session_id);
 void voice_extn_in_get_parameters(struct stream_in *in,
@@ -76,7 +77,8 @@
 {
 }
 
-static int voice_extn_is_in_call(struct audio_device *adev, bool *in_call)
+static int voice_extn_is_call_state_active(struct audio_device *adev,
+                                           bool *is_call_active)
 {
     return -ENOSYS;
 }