anorak: hal: Add support to always use Speaker for VoIP call.

Update audioStream to choose only speaker for VoIP call use-cases
for targets that do no support handset profile for VoIP.

Change-Id: I9116559d5c6d14f4ac498feec962acc936001043
diff --git a/configs/anorak/anorak.mk b/configs/anorak/anorak.mk
index 752e492..090ee01 100644
--- a/configs/anorak/anorak.mk
+++ b/configs/anorak/anorak.mk
@@ -436,7 +436,8 @@
 vendor.audio.feature.audiozoom.enable=false \
 vendor.audio.feature.snd_mon.enable=true \
 vendor.audio.feature.dmabuf.cma.memory.enable=true \
-vendor.audio.hdr.record.enable=false
+vendor.audio.hdr.record.enable=false \
+vendor.audio.feature.handset.profile.disable=true
 
 
 PRODUCT_PACKAGES_ENG += \
diff --git a/configs/kalama/kalama.mk b/configs/kalama/kalama.mk
index 55ac0d8..7196d11 100644
--- a/configs/kalama/kalama.mk
+++ b/configs/kalama/kalama.mk
@@ -464,7 +464,8 @@
 vendor.audio.feature.audiozoom.enable=false \
 vendor.audio.feature.snd_mon.enable=true \
 vendor.audio.feature.dmabuf.cma.memory.enable=false \
-vendor.audio.hdr.record.enable=false
+vendor.audio.hdr.record.enable=false \
+vendor.audio.feature.handset.profile.disable=false
 
 # set dynamic sensor operation timeout in ms
 PRODUCT_PROPERTY_OVERRIDES += \
diff --git a/hal/AudioStream.cpp b/hal/AudioStream.cpp
index 20b1126..bb0af27 100644
--- a/hal/AudioStream.cpp
+++ b/hal/AudioStream.cpp
@@ -2348,6 +2348,7 @@
 
 int StreamOutPrimary::RouteStream(const std::set<audio_devices_t>& new_devices, bool force_device_switch __unused) {
     int ret = 0, noPalDevices = 0;
+    bool skipDeviceSet = false;
     pal_device_id_t * deviceId = nullptr;
     struct pal_device* deviceIdConfigs = nullptr;
     pal_param_device_capability_t *device_cap_query = nullptr;
@@ -2427,6 +2428,14 @@
         mAndroidOutDevices = new_devices;
 
         for (int i = 0; i < noPalDevices; i++) {
+            /*Skip device set for Handset profile for targets that do not support Handset profile for VoIP call*/
+            if (noHandsetSupport && (mPalOutDevice[i].id == PAL_DEVICE_OUT_SPEAKER &&
+                streamAttributes_.type == PAL_STREAM_VOIP_RX) &&
+                (mPalOutDeviceIds[i] == PAL_DEVICE_OUT_SPEAKER ||
+                mPalOutDeviceIds[i] == PAL_DEVICE_OUT_HANDSET)) {
+                skipDeviceSet = true;
+                AHAL_DBG("Skip pal_stream_set_device as the stream is already on speaker");
+            }
             mPalOutDevice[i].id = mPalOutDeviceIds[i];
             mPalOutDevice[i].config.sample_rate = mPalOutDevice[0].config.sample_rate;
             mPalOutDevice[i].config.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
@@ -2481,6 +2490,13 @@
                        "hifi-filter_custom_key",
                        sizeof(mPalOutDevice[i].custom_config.custom_key));
             }
+
+            /*For targets that do not support Handset profile for VoIP call, set the speaker profile for VoIP call*/
+            if (noHandsetSupport && mPalOutDevice[i].id == PAL_DEVICE_OUT_HANDSET &&
+                streamAttributes_.type == PAL_STREAM_VOIP_RX && !skipDeviceSet) {
+                mPalOutDevice[i].id = PAL_DEVICE_OUT_SPEAKER;
+                AHAL_DBG("set PAL_DEVICE_OUT_SPEAKER instead of Handset_speaker for VoIP_RX ");
+            }
         }
 
         std::shared_ptr<AudioDevice> adevice = AudioDevice::GetInstance();
@@ -2509,7 +2525,8 @@
             }
         }
 
-        if (pal_stream_handle_) {
+        /*Skip device set for Handset profile for targets that do not support Handset profile for VoIP call*/
+        if (pal_stream_handle_ && !skipDeviceSet)  {
             ret = pal_stream_set_device(pal_stream_handle_, noPalDevices, mPalOutDevice);
             if (!ret) {
                 for (const auto &dev : mAndroidOutDevices)
@@ -3064,6 +3081,12 @@
         }
     }
 
+    /*For targets that do not support Handset profile for VoIP call, set the speaker profile for VoIP call*/
+    if(noHandsetSupport && mPalOutDevice->id == PAL_DEVICE_OUT_HANDSET && streamAttributes_.type == PAL_STREAM_VOIP_RX) {
+        mPalOutDevice->id = PAL_DEVICE_OUT_SPEAKER;
+        AHAL_DBG("set PAL_DEVICE_OUT_SPEAKER instead of Handset_speaker for VoIP_RX");
+    }
+
     ret = pal_stream_open(&streamAttributes_,
                           mAndroidOutDevices.size(),
                           mPalOutDevice,
@@ -3812,6 +3835,7 @@
           address(%s)", handle, config->format, config->sample_rate, config->channel_mask,
           mAndroidOutDevices.size(), flags, address);
 
+    noHandsetSupport = property_get_bool("vendor.audio.feature.handset.profile.disable", false);
     //TODO: check if USB device is connected or not
     if (AudioExtn::audio_devices_cmp(mAndroidOutDevices, audio_is_usb_out_device)){
         // get capability from device of USB
@@ -4361,6 +4385,7 @@
 int StreamInPrimary::RouteStream(const std::set<audio_devices_t>& new_devices, bool force_device_switch) {
     bool is_empty, is_input;
     int ret = 0, noPalDevices = 0;
+    bool skipDeviceSet = false;
     pal_device_id_t * deviceId = nullptr;
     struct pal_device* deviceIdConfigs = nullptr;
     pal_param_device_capability_t *device_cap_query = nullptr;
@@ -4441,6 +4466,14 @@
         }
 
         for (int i = 0; i < noPalDevices; i++) {
+            /*Skip device set for targets that do not support Handset profile for VoIP call*/
+            if (noHandsetSupport && (mPalInDevice[i].id == PAL_DEVICE_IN_SPEAKER_MIC &&
+                streamAttributes_.type == PAL_STREAM_VOIP_TX) &&
+                (mPalInDeviceIds[i] == PAL_DEVICE_IN_SPEAKER_MIC ||
+                mPalInDeviceIds[i] == PAL_DEVICE_IN_HANDSET_MIC)) {
+                skipDeviceSet = true;
+                AHAL_DBG("Skip pal_stream_set_device as the stream is already on speaker");
+            }
             mPalInDevice[i].id = mPalInDeviceIds[i];
             if (((mPalInDeviceIds[i] == PAL_DEVICE_IN_USB_DEVICE) ||
                (mPalInDeviceIds[i] == PAL_DEVICE_IN_USB_HEADSET)) && device_cap_query) {
@@ -4484,6 +4517,13 @@
                 ((get_hdr_mode() == AUDIO_RECORD_SPF_HDR) &&
                 (source_ == AUDIO_SOURCE_CAMCORDER || source_ == AUDIO_SOURCE_MIC)))
                 setup_hdr_usecase(&mPalInDevice[i]);
+
+            /*For targets that do not support Handset profile for VoIP call, set speaker profile for VoIP call*/
+            if (noHandsetSupport && mPalInDevice[i].id == PAL_DEVICE_IN_HANDSET_MIC &&
+                streamAttributes_.type == PAL_STREAM_VOIP_TX && !skipDeviceSet) {
+                mPalInDevice[i].id = PAL_DEVICE_IN_SPEAKER_MIC;
+                AHAL_DBG("set PAL_DEVICE_IN_SPEAKER_MIC instead of Handset_mic for VoIP_TX");
+            }
         }
 
         mAndroidInDevices = new_devices;
@@ -4504,7 +4544,7 @@
             }
         }
 
-        if (pal_stream_handle_)
+        if (pal_stream_handle_ && !skipDeviceSet)
             ret = pal_stream_set_device(pal_stream_handle_, noPalDevices, mPalInDevice);
     }
 
@@ -4755,6 +4795,11 @@
         }
     }
 
+    /* For targets that do no support Handset profile for VoIP call so set the speaker profile for VoIP call*/
+    if (noHandsetSupport && mPalInDevice->id == PAL_DEVICE_IN_HANDSET_MIC &&  streamAttributes_.type == PAL_STREAM_VOIP_TX) {
+        mPalInDevice->id = PAL_DEVICE_IN_SPEAKER_MIC;
+        AHAL_DBG("set PAL_DEVICE_IN_SPEAKER_MIC instead of Handset_mic for VoIP_TX");
+    }
     ret = pal_stream_open(&streamAttributes_,
                          mAndroidInDevices.size(),
                          mPalInDevice,
@@ -5180,6 +5225,7 @@
         AHAL_ERR("stream_ new allocation failed");
         goto error;
     }
+    noHandsetSupport = property_get_bool("vendor.audio.feature.handset.profile.disable", false);
 
     if (AudioExtn::audio_devices_cmp(mAndroidInDevices, audio_is_usb_in_device)) {
         // get capability from device of USB
diff --git a/hal/AudioStream.h b/hal/AudioStream.h
index 969224d..e65ce28 100644
--- a/hal/AudioStream.h
+++ b/hal/AudioStream.h
@@ -598,6 +598,7 @@
     uint32_t convertBufSize;
     uint32_t fragments_ = 0;
     uint32_t fragment_size_ = 0;
+    bool noHandsetSupport = false;
     pal_snd_dec_t palSndDec;
     struct pal_compr_gapless_mdata gaplessMeta = {0, 0};
     uint32_t msample_rate;
@@ -674,6 +675,7 @@
     uint32_t fragments_ = 0;
     uint32_t fragment_size_ = 0;
     int FillHalFnPtrs();
+    bool noHandsetSupport = false;
     std::shared_ptr<audio_stream_in>    stream_;
     audio_source_t                      source_;
     friend class AudioDevice;