hal: Increase ffv process thread priority

Set ffv process thread priority to ANDROID_PRIORITY_AUDIO
and set affinity for ffv reader thread to achieve better performance.

CRs-Fixed: 2405913
Change-Id: I04743f705cc78ed5f9378a6a566ec4c485f5dcb0
diff --git a/hal/Android.mk b/hal/Android.mk
index 89efe54..cfec83b 100644
--- a/hal/Android.mk
+++ b/hal/Android.mk
@@ -494,6 +494,7 @@
     LOCAL_SHARED_LIBRARIES += vendor.qti.hardware.audiohalext@1.0
 endif
 
+LOCAL_CFLAGS += -D_GNU_SOURCE
 LOCAL_CFLAGS += -Wall -Werror
 
 LOCAL_COPY_HEADERS_TO   := mm-audio
diff --git a/hal/audio_extn/audio_extn.c b/hal/audio_extn/audio_extn.c
index 077ec99..f440362 100644
--- a/hal/audio_extn/audio_extn.c
+++ b/hal/audio_extn/audio_extn.c
@@ -46,6 +46,7 @@
 #include <cutils/properties.h>
 #include <log/log.h>
 #include <unistd.h>
+
 #include "audio_hw.h"
 #include "audio_extn.h"
 #include "voice_extn.h"
@@ -409,6 +410,25 @@
 }
 #endif
 
+/* Affine AHAL thread to CPU core */
+void audio_extn_set_cpu_affinity()
+{
+    cpu_set_t cpuset;
+    struct sched_param sched_param;
+    int policy = SCHED_FIFO, rc = 0;
+
+    ALOGV("%s: Set CPU affinity for read thread", __func__);
+    CPU_ZERO(&cpuset);
+    if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
+        ALOGE("%s: CPU Affinity allocation failed for Capture thread",
+               __func__);
+
+    sched_param.sched_priority = sched_get_priority_min(policy);
+    rc = sched_setscheduler(0, policy, &sched_param);
+    if (rc != 0)
+         ALOGE("%s: Failed to set realtime priority", __func__);
+}
+
 // START: VBAT =============================================================
 void vbat_feature_init(bool is_feature_enabled)
 {
diff --git a/hal/audio_extn/audio_extn.h b/hal/audio_extn/audio_extn.h
index 079d33a..863916b 100644
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -1209,4 +1209,6 @@
 
 bool audio_extn_is_custom_stereo_enabled();
 void audio_extn_send_dual_mono_mixing_coefficients(struct stream_out *out);
+
+void audio_extn_set_cpu_affinity();
 #endif /* AUDIO_EXTN_H */
diff --git a/hal/audio_extn/ffv.c b/hal/audio_extn/ffv.c
old mode 100755
new mode 100644
index a57f68c..a5b15cc
--- a/hal/audio_extn/ffv.c
+++ b/hal/audio_extn/ffv.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2017-2019, The Linux Foundation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -45,6 +45,8 @@
 #include <cutils/log.h>
 #include <pthread.h>
 #include <sys/resource.h>
+#include <unistd.h>
+#include <system/thread_defs.h>
 
 #include "audio_hw.h"
 #include "audio_extn.h"
@@ -599,6 +601,7 @@
     int param_size = 0;
     FfvStatusType status_type;
     int ret = 0;
+    ffv_quadrx_use_dwnmix_param_t quad_downmix;
 
     ALOGV("%s: entry", __func__);
     /* notify library to reset AEC during each start */
@@ -620,6 +623,20 @@
         return -ENOMEM;
     }
 
+    if (in_snd_device == SND_DEVICE_IN_EC_REF_LOOPBACK_QUAD) {
+        quad_downmix.quadrx_dwnmix_enable = true;
+        ALOGD("%s: set param for 4 ch ec, handle %p", __func__, ffvmod.handle);
+        status_type = ffv_set_param_fn(ffvmod.handle,
+            (char *)&quad_downmix,
+            FFV_QUADRX_USE_DWNMIX_PARAM,
+            sizeof(ffv_quadrx_use_dwnmix_param_t));
+        if (status_type) {
+            ALOGE("%s: ERROR. ffv_set_param_fn for quad channel ec ref %d",
+                __func__, status_type);
+            return -EINVAL;
+        }
+    }
+
     pthread_mutex_lock(&ffvmod.init_lock);
     uc_info_tx->id = USECASE_AUDIO_EC_REF_LOOPBACK;
     uc_info_tx->type = PCM_CAPTURE;
@@ -775,6 +792,8 @@
                 return status;
             }
         }
+        audio_extn_set_cpu_affinity();
+        setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
         ffvmod.capture_started = true;
     }