audio: Fix realtime stream routing behavior

Update adm of pending routing event before handling
routing in audio_hw.

Test: Loopback App
Bug: 31770169
Bug: 32053844
CRs-Fixed: 1061281
Change-Id: Iea6f28a27c9c86406edad558b533ed4a5d9aff43
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 6ca763e..2039924 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -345,12 +345,6 @@
 {
     struct audio_device *adev = out->dev;
 
-    if (out->routing_change) {
-        out->routing_change = false;
-        if (adev->adm_on_routing_change)
-            adev->adm_on_routing_change(adev->adm_data, out->handle);
-    }
-
     if (adev->adm_request_focus_v2) {
         adev->adm_request_focus_v2(adev->adm_data, out->handle, ns);
     } else if (adev->adm_request_focus) {
@@ -362,12 +356,6 @@
 {
     struct audio_device *adev = in->dev;
 
-    if (in->routing_change) {
-        in->routing_change = false;
-        if (adev->adm_on_routing_change)
-            adev->adm_on_routing_change(adev->adm_data, in->capture_handle);
-    }
-
     if (adev->adm_request_focus_v2) {
         adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns);
     } else if (adev->adm_request_focus) {
@@ -2039,7 +2027,11 @@
             if (!out->standby) {
                 if (!same_dev) {
                     ALOGV("update routing change");
-                    out->routing_change = true;
+                    // inform adm before actual routing to prevent glitches.
+                    if (adev->adm_on_routing_change) {
+                        adev->adm_on_routing_change(adev->adm_data,
+                                                    out->handle);
+                    }
                 }
                 select_devices(adev, out->usecase);
                 audio_extn_tfa_98xx_update();
@@ -2791,7 +2783,11 @@
             /* If recording is in progress, change the tx device to new device */
             if (!in->standby) {
                 ALOGV("update input routing change");
-                in->routing_change = true;
+                // inform adm before actual routing to prevent glitches.
+                if (adev->adm_on_routing_change) {
+                    adev->adm_on_routing_change(adev->adm_data,
+                                                in->capture_handle);
+                }
                 select_devices(adev, in->usecase);
             }
         }
diff --git a/hal/audio_hw.h b/hal/audio_hw.h
index 03f1587..ca068e2 100644
--- a/hal/audio_hw.h
+++ b/hal/audio_hw.h
@@ -204,7 +204,6 @@
     int send_new_metadata;
     bool realtime;
     int af_period_multiplier;
-    bool routing_change;
     struct audio_device *dev;
     card_status_t card_status;
 
@@ -234,7 +233,6 @@
     bool is_st_session_active;
     bool realtime;
     int af_period_multiplier;
-    bool routing_change;
     struct audio_device *dev;
     audio_format_t format;
     card_status_t card_status;