hal: select speaker mic for voice communication

- Current implementation selects handset mic for voice
  communication in speaker mode
- Check if the primary output (rx path) is active on
  speaker and select speaker mic during input device
  selection
- If the tx path is started first, ensure that tx device
  is updated while starting the rx path.

Bug: 8325112
Change-Id: I1c556c0c9c92e599c8a1f68575b26ecdad155e7e
Signed-off-by: Iliyan Malchev <malchev@google.com>
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index c5acd17..44e7f0d 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -764,14 +764,27 @@
         if (usecase->type == PCM_PLAYBACK) {
             usecase->devices = usecase->stream.out->devices;
             in_snd_device = SND_DEVICE_NONE;
-            if (out_snd_device == SND_DEVICE_NONE)
+            if (out_snd_device == SND_DEVICE_NONE) {
                 out_snd_device = get_output_snd_device(adev,
                                             usecase->stream.out->devices);
+                if (usecase->stream.out == adev->primary_output &&
+                        adev->active_input &&
+                        adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
+                    select_devices(adev, adev->active_input->usecase);
+                }
+            }
         } else if (usecase->type == PCM_CAPTURE) {
             usecase->devices = usecase->stream.in->device;
             out_snd_device = SND_DEVICE_NONE;
-            if (in_snd_device == SND_DEVICE_NONE)
-                in_snd_device = get_input_snd_device(adev, SND_DEVICE_NONE);
+            if (in_snd_device == SND_DEVICE_NONE) {
+                if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
+                        adev->primary_output && !adev->primary_output->standby) {
+                    in_snd_device = get_input_snd_device(adev,
+                                        adev->primary_output->devices);
+                } else {
+                    in_snd_device = get_input_snd_device(adev, AUDIO_DEVICE_NONE);
+                }
+            }
         }
     }
 
@@ -1402,13 +1415,14 @@
 
     pthread_mutex_lock(&out->lock);
     if (out->standby) {
+        out->standby = false;
         pthread_mutex_lock(&adev->lock);
         ret = start_output_stream(out);
         pthread_mutex_unlock(&adev->lock);
         if (ret != 0) {
+            out->standby = true;
             goto exit;
         }
-        out->standby = false;
     }
 
     if (out->pcm) {