hal: Add support for two concurrent record sessions

Add support to fallback to compress record path for second
record session during concurrent recording.

CRs-Fixed: 1088820
Change-Id: I4ade655eb4b6b80a7cc831b82aec07534fc7c30c
diff --git a/hal/audio_extn/compress_in.c b/hal/audio_extn/compress_in.c
index c032480..403ee8b 100644
--- a/hal/audio_extn/compress_in.c
+++ b/hal/audio_extn/compress_in.c
@@ -84,7 +84,8 @@
 
 bool audio_extn_cin_applicable_stream(struct stream_in *in)
 {
-    if (in->flags & AUDIO_INPUT_FLAG_TIMESTAMP)
+    if ((in->flags & AUDIO_INPUT_FLAG_TIMESTAMP) ||
+        in->usecase == USECASE_AUDIO_RECORD_COMPRESS2)
         return true;
 
     return false;
@@ -256,7 +257,8 @@
     int ret = 0, buffer_size = 0, meta_size = sizeof(struct snd_codec_metadata);
     cin_private_data_t *cin_data = NULL;
 
-    if (!COMPRESSED_TIMESTAMP_FLAG) {
+    if (!COMPRESSED_TIMESTAMP_FLAG &&
+        (in->flags & AUDIO_INPUT_FLAG_TIMESTAMP)) {
         ALOGE("%s: timestamp mode not supported!", __func__);
         return -EINVAL;
     }
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index d275e3d..e6dfb66 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -4672,7 +4672,6 @@
         }
     }
 
-    /* Update config params with the requested sample rate and channels */
     in->usecase = USECASE_AUDIO_RECORD;
     if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
             (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
@@ -4683,6 +4682,24 @@
         in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags);
     }
 
+    pthread_mutex_lock(&adev->lock);
+    if (in->usecase == USECASE_AUDIO_RECORD) {
+        if (!(adev->pcm_record_uc_state) &&
+            ((flags & AUDIO_INPUT_FLAG_TIMESTAMP) == 0)) {
+            ALOGV("%s: pcm record usecase", __func__);
+            adev->pcm_record_uc_state = 1;
+        } else {
+            /*
+             * Assign default compress record use case, actual use case
+             * assignment will happen later.
+             */
+             in->usecase = USECASE_AUDIO_RECORD_COMPRESS2;
+             ALOGV("%s: compress record usecase", __func__);
+        }
+    }
+    pthread_mutex_unlock(&adev->lock);
+
+    /* Update config params with the requested sample rate and channels */
     if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
         if (adev->mode != AUDIO_MODE_IN_CALL) {
             ret = -EINVAL;
@@ -4796,6 +4813,13 @@
     if (audio_extn_compr_cap_enabled() &&
             audio_extn_compr_cap_format_supported(in->config.format))
         audio_extn_compr_cap_deinit();
+
+    if (in->usecase == USECASE_AUDIO_RECORD) {
+        pthread_mutex_lock(&adev->lock);
+        adev->pcm_record_uc_state = 0;
+        pthread_mutex_unlock(&adev->lock);
+    }
+
     if (audio_extn_cin_attached_usecase(in->usecase))
         audio_extn_cin_close_input_stream(in);
 
@@ -4927,6 +4951,7 @@
     list_init(&adev->usecase_list);
     adev->cur_wfd_channels = 2;
     adev->offload_usecases_state = 0;
+    adev->pcm_record_uc_state = 0;
     adev->is_channel_status_set = false;
     adev->perf_lock_opts[0] = 0x101;
     adev->perf_lock_opts[1] = 0x20E;
diff --git a/hal/audio_hw.h b/hal/audio_hw.h
index 38ac547..f837952 100644
--- a/hal/audio_hw.h
+++ b/hal/audio_hw.h
@@ -385,6 +385,7 @@
     bool is_channel_status_set;
     void *platform;
     unsigned int offload_usecases_state;
+    unsigned int pcm_record_uc_state;
     void *visualizer_lib;
     int (*visualizer_start_output)(audio_io_handle_t, int);
     int (*visualizer_stop_output)(audio_io_handle_t, int);