Enable lower latency audio capture

Bug: 16601366
Change-Id: I128db28937e2fefd4e41a924204442a72d45f1c8
diff --git a/audio/audio_hw.c b/audio/audio_hw.c
index 5df15f4..25ddbaf 100644
--- a/audio/audio_hw.c
+++ b/audio/audio_hw.c
@@ -49,7 +49,8 @@
 #define OUT_SAMPLING_RATE 44100
 
 #define IN_PERIOD_SIZE 1024
-#define IN_PERIOD_COUNT 4
+#define IN_PERIOD_SIZE_LOW_LATENCY 512
+#define IN_PERIOD_COUNT 2
 #define IN_SAMPLING_RATE 44100
 
 #define SCO_PERIOD_SIZE 256
@@ -86,6 +87,16 @@
     .stop_threshold = (IN_PERIOD_SIZE * IN_PERIOD_COUNT),
 };
 
+struct pcm_config pcm_config_in_low_latency = {
+    .channels = 2,
+    .rate = IN_SAMPLING_RATE,
+    .period_size = IN_PERIOD_SIZE_LOW_LATENCY,
+    .period_count = IN_PERIOD_COUNT,
+    .format = PCM_FORMAT_S16_LE,
+    .start_threshold = 1,
+    .stop_threshold = (IN_PERIOD_SIZE_LOW_LATENCY * IN_PERIOD_COUNT),
+};
+
 struct pcm_config pcm_config_sco = {
     .channels = 1,
     .rate = SCO_SAMPLING_RATE,
@@ -135,7 +146,8 @@
 
     pthread_mutex_t lock; /* see note below on mutex acquisition order */
     struct pcm *pcm;
-    struct pcm_config *pcm_config;
+    struct pcm_config *pcm_config;          /* current configuration */
+    struct pcm_config *pcm_config_non_sco;  /* configuration to return after SCO is done */
     bool standby;
 
     unsigned int requested_rate;
@@ -338,7 +350,7 @@
         in->pcm_config = &pcm_config_sco;
     } else {
         device = PCM_DEVICE;
-        in->pcm_config = &pcm_config_in;
+        in->pcm_config = in->pcm_config_non_sco;
     }
 
     /*
@@ -1180,7 +1192,7 @@
                                   audio_devices_t devices,
                                   struct audio_config *config,
                                   struct audio_stream_in **stream_in,
-                                  audio_input_flags_t flags __unused)
+                                  audio_input_flags_t flags)
 {
     struct audio_device *adev = (struct audio_device *)dev;
     struct stream_in *in;
@@ -1217,7 +1229,10 @@
     in->dev = adev;
     in->standby = true;
     in->requested_rate = config->sample_rate;
-    in->pcm_config = &pcm_config_in; /* default PCM config */
+    /* default PCM config */
+    in->pcm_config = (config->sample_rate == IN_SAMPLING_RATE) && (flags & AUDIO_INPUT_FLAG_FAST) ?
+            &pcm_config_in_low_latency : &pcm_config_in;
+    in->pcm_config_non_sco = in->pcm_config;
 
     *stream_in = &in->stream;
     return 0;