hal: Handle A2dp suspend scenario

Avoid writing data to A2dp output in case of non-offload and
mute offload streams if A2dp is suspended.
Restore device and unmute streams if muted as part of suspend.

CRs-Fixed: 2066578
Change-Id: I3a222f128659de602ba1f6bed4d4e3cdcc7a379a
diff --git a/hal/audio_extn/a2dp.c b/hal/audio_extn/a2dp.c
index 5239a30..b96a147 100644
--- a/hal/audio_extn/a2dp.c
+++ b/hal/audio_extn/a2dp.c
@@ -765,6 +765,8 @@
 {
      int ret, val;
      char value[32]={0};
+     struct audio_usecase *uc_info;
+     struct listnode *node;
 
      if(a2dp.is_a2dp_offload_supported == false) {
         ALOGV("no supported encoders identified,ignoring a2dp setparam");
@@ -801,6 +803,14 @@
              if ((!strncmp(value,"true",sizeof(value)))) {
                 ALOGD("Setting a2dp to suspend state");
                 a2dp.a2dp_suspended = true;
+                list_for_each(node, &a2dp.adev->usecase_list) {
+                    uc_info = node_to_item(node, struct audio_usecase, list);
+                    if (uc_info->type == PCM_PLAYBACK) {
+                        pthread_mutex_unlock(&a2dp.adev->lock);
+                        check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
+                        pthread_mutex_lock(&a2dp.adev->lock);
+                    }
+                }
                 reset_a2dp_enc_config_params();
                 if(a2dp.audio_suspend_stream)
                    a2dp.audio_suspend_stream();
@@ -832,12 +842,13 @@
                         }
                     }
                 }
-                // restore A2DP device for active usecases
                 list_for_each(node, &a2dp.adev->usecase_list) {
                     uc_info = node_to_item(node, struct audio_usecase, list);
-                    if ((uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
-                            (uc_info->out_snd_device != SND_DEVICE_OUT_BT_A2DP))
-                        select_devices(a2dp.adev, uc_info->id);
+                    if (uc_info->type == PCM_PLAYBACK) {
+                        pthread_mutex_unlock(&a2dp.adev->lock);
+                        check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
+                        pthread_mutex_lock(&a2dp.adev->lock);
+                    }
                 }
             }
         }
@@ -874,6 +885,9 @@
 {
     bool ret = false;
 
+    if (a2dp.a2dp_suspended)
+        return ret;
+
     if ((a2dp.bt_state != A2DP_STATE_DISCONNECTED) &&
         (a2dp.is_a2dp_offload_supported) &&
         (a2dp.audio_check_a2dp_ready))
@@ -881,6 +895,11 @@
     return ret;
 }
 
+bool audio_extn_a2dp_is_suspended()
+{
+    return a2dp.a2dp_suspended;
+}
+
 void audio_extn_a2dp_init (void *adev)
 {
   a2dp.adev = (struct audio_device*)adev;