hal: Add support to close streams on power policy callback

* Close existing streams during power policy disable callback

* Stop new streams from starting if current policy is in disabled state

Signed-off-by: Shubhasini Sugumaran <quic_c_shubsu@quicinc.com>
Change-Id: I4960e336f03086d1322708e155a4a4f02098b3fb
diff --git a/hal/audio_extn/PowerPolicyClient.cpp b/hal/audio_extn/PowerPolicyClient.cpp
index e3a9e22..62dcf39 100644
--- a/hal/audio_extn/PowerPolicyClient.cpp
+++ b/hal/audio_extn/PowerPolicyClient.cpp
@@ -51,7 +51,11 @@
 
 }  // namespace
 
-PowerPolicyClient::PowerPolicyClient() {
+PowerPolicyClient::PowerPolicyClient(power_policy_init_config_t init_config) {
+
+    fp_in_set_power_policy = init_config.fp_in_set_power_policy;
+    fp_out_set_power_policy = init_config.fp_out_set_power_policy;
+
     plugin_handle = dlopen(LIB_AUDIO_HAL_PLUGIN, RTLD_NOW);
     if (plugin_handle == NULL) {
         LOG(ERROR) << "Failed to open plugin library";
@@ -89,12 +93,16 @@
     if (hasComponent(powerPolicy.enabledComponents, kAudioComponent)) {
         LOG(ERROR) << "Power policy: Audio component is enabled";
         disable = 0;
+
+        fp_out_set_power_policy(!disable);
         if (hal_plugin_send_msg != NULL)
             hal_plugin_send_msg(AUDIO_HAL_PLUGIN_MSG_SILENT_MODE,
                                 &disable, sizeof(disable));
     } else if (hasComponent(powerPolicy.disabledComponents, kAudioComponent)) {
         LOG(ERROR) << "Power policy: Audio component is disabled";
         disable = 1;
+
+        fp_out_set_power_policy(!disable);
         if (hal_plugin_send_msg != NULL)
             hal_plugin_send_msg(AUDIO_HAL_PLUGIN_MSG_SILENT_MODE,
                                 &disable, sizeof(disable));
@@ -103,11 +111,15 @@
     if (hasComponent(powerPolicy.enabledComponents, kMicComponent)) {
         LOG(ERROR) << "Power policy: Microphone component is enabled";
         disable = 0;
+
+        fp_in_set_power_policy(!disable);
         if (hal_plugin_send_msg != NULL)
             hal_plugin_send_msg(AUDIO_HAL_PLUGIN_MSG_MIC_STATE,
                                 &disable, sizeof(disable));
     } else if (hasComponent(powerPolicy.disabledComponents, kMicComponent)) {
         disable = 1;
+
+        fp_in_set_power_policy(!disable);
         if (hal_plugin_send_msg != NULL)
             hal_plugin_send_msg(AUDIO_HAL_PLUGIN_MSG_MIC_STATE,
                                 &disable, sizeof(disable));
diff --git a/hal/audio_extn/PowerPolicyClient.h b/hal/audio_extn/PowerPolicyClient.h
index da0b434..c5308e3 100644
--- a/hal/audio_extn/PowerPolicyClient.h
+++ b/hal/audio_extn/PowerPolicyClient.h
@@ -32,11 +32,19 @@
 #include "audio_hal_plugin.h"
 
 typedef int32_t (*hal_plugin_send_msg_t) (audio_hal_plugin_msg_type_t, void*, uint32_t);
+typedef void (*fp_in_set_power_policy_t) (uint8_t);
+typedef void (*fp_out_set_power_policy_t) (uint8_t);
+
+typedef struct power_policy_init_config {
+    fp_in_set_power_policy_t                      fp_in_set_power_policy;
+    fp_out_set_power_policy_t                     fp_out_set_power_policy;
+} power_policy_init_config_t;
+
 
 class PowerPolicyClient
     : public ::android::frameworks::automotive::powerpolicy::PowerPolicyClientBase {
   public:
-    explicit PowerPolicyClient();
+    explicit PowerPolicyClient(power_policy_init_config init_config);
     ~PowerPolicyClient();
 
     void onInitFailed();
@@ -48,6 +56,8 @@
   private:
         void* plugin_handle;
         hal_plugin_send_msg_t hal_plugin_send_msg;
+        fp_out_set_power_policy_t fp_out_set_power_policy;
+        fp_in_set_power_policy_t fp_in_set_power_policy;
 };
 
 #endif  // QTI_AUDIO_POWERPOLICYCLIENT_H_
diff --git a/hal/audio_extn/audio_extn.c b/hal/audio_extn/audio_extn.c
old mode 100755
new mode 100644
index 2848649..b094cbd
--- a/hal/audio_extn/audio_extn.c
+++ b/hal/audio_extn/audio_extn.c
@@ -6526,10 +6526,13 @@
         goto exit;
     }
     ALOGD("%s: Launching Power Policy Client", __func__);
-    launch_power_policy();
+    power_policy_init_config_t init_config;
+    init_config.fp_in_set_power_policy = in_set_power_policy;
+    init_config.fp_out_set_power_policy = out_set_power_policy;
+    launch_power_policy(init_config);
 
 exit:
-    pthread_exit(NULL);
+    return NULL;
 }
 
 static int power_policy_feature_init(bool is_feature_enabled)
diff --git a/hal/audio_extn/audio_extn.h b/hal/audio_extn/audio_extn.h
old mode 100755
new mode 100644
index 4344bad..f2038d8
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -1428,6 +1428,17 @@
 } synth_init_config_t;
 // END: SYNTH_HAL FEATURE ==================================================
 
+// START: POWER_POLICY FEATURE ==================================================
+
+typedef void (*fp_in_set_power_policy_t) (uint8_t);
+typedef void (*fp_out_set_power_policy_t) (uint8_t);
+
+typedef struct power_policy_init_config {
+    fp_in_set_power_policy_t                      fp_in_set_power_policy;
+    fp_out_set_power_policy_t                     fp_out_set_power_policy;
+} power_policy_init_config_t;
+// END: POWER_POLICY FEATURE ==================================================
+
 bool audio_extn_edid_is_supported_sr(edid_audio_info* info, int sr);
 bool audio_extn_edid_is_supported_bps(edid_audio_info* info, int bps);
 int audio_extn_edid_get_highest_supported_sr(edid_audio_info* info);
diff --git a/hal/audio_extn/power_policy_launcher.cpp b/hal/audio_extn/power_policy_launcher.cpp
index a85f30c..ac846cd 100644
--- a/hal/audio_extn/power_policy_launcher.cpp
+++ b/hal/audio_extn/power_policy_launcher.cpp
@@ -34,11 +34,11 @@
 
 extern "C" {
 
-    int launchPowerPolicyClient() {
+    int launchPowerPolicyClient(power_policy_init_config_t init_config) {
         ALOGD("%s: power policy launcher called", __func__);
         ABinderProcess_setThreadPoolMaxThreadCount(0);
         std::shared_ptr<PowerPolicyClient> powerPolicyClient =
-                ::ndk::SharedRefBase::make<PowerPolicyClient>();
+                ::ndk::SharedRefBase::make<PowerPolicyClient>(init_config);
         ALOGD("%s:Instantiating power policy client from launcher", __func__);
         powerPolicyClient->init();
         ALOGD("%s: Power Policy class inited, joining threadpool", __func__);