hal: Fix VoIP Rx no sound after openOutput if setVolume is not received

HAL sets cached stream volume for VoIP with start output
or device switches. When an output is opened, all stream
members are initialized to 0. After opening an output
if we receive a write before setVolume is called, HAL
sets VoIP stream volume, which is 0 in this case.
Hence Rx is muted. Certain apps do not call setVolume
as frequently as they should leading to the call being
muted until app sends a volume change.

Fix this behavior by not setting VoIP volume as 0 from HAL
until a valid volume is set by client. Let default volume
get applied in lower layers for such scenarios.

Bug: 133829194
Change-Id: I281916295d9458b62a1c6b4b82b6a80116fbbcb9
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 9691ce8..a5df951 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -93,6 +93,7 @@
 #define MMAP_PLAYBACK_VOLUME_MAX 0x2000
 #define PCM_PLAYBACK_VOLUME_MAX 0x2000
 #define DSD_VOLUME_MIN_DB (-110)
+#define INVALID_OUT_VOLUME -1
 
 #define RECORD_GAIN_MIN 0.0f
 #define RECORD_GAIN_MAX 1.0f
@@ -779,6 +780,11 @@
            (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY);
 }
 
+static inline bool is_valid_volume(float left, float right)
+{
+    return ((left >= 0.0f && right >= 0.0f) ? true : false);
+}
+
 static int enable_audio_route_for_voice_usecases(struct audio_device *adev,
                                                  struct audio_usecase *uc_info)
 {
@@ -4820,6 +4826,12 @@
     struct mixer_ctl *ctl;
     long set_values[4];
 
+    if (!is_valid_volume(left, right)) {
+        ALOGE("%s: Invalid stream volume for left=%f, right=%f",
+                   __func__, left, right);
+        return -EINVAL;
+    }
+
     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
     if (!ctl) {
         ALOGE("%s: Could not get ctl for mixer cmd - %s",
@@ -6980,6 +6992,8 @@
                                         AUDIO_CHANNEL_OUT_MONO : AUDIO_CHANNEL_OUT_STEREO;
                 out->usecase = USECASE_AUDIO_PLAYBACK_VOIP;
                 out->format = AUDIO_FORMAT_PCM_16_BIT;
+                out->volume_l = INVALID_OUT_VOLUME;
+                out->volume_r = INVALID_OUT_VOLUME;
 
                 out->config = default_pcm_config_voip_copp;
                 out->config.period_size = VOIP_IO_BUF_SIZE(out->sample_rate, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2;