libaudiohal@aidl: update dynamic mix port when disconnecting device.

When an external device is disconnected, the framework should query the
HAL to update the cached mix port so that the cached mix ports are up to
date in libaudiohal. That prevents stream opening failure after
disconecting external devices.

Bug: 251215866
Test: connect multiple different devices
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:4def684ea164870c775bd4bc6e688bf669d242d2)
Merged-In: Ibc6c29f9a31c0cb5257e8348aba6822086564947
Change-Id: Ibc6c29f9a31c0cb5257e8348aba6822086564947
diff --git a/media/libaudiohal/impl/Hal2AidlMapper.cpp b/media/libaudiohal/impl/Hal2AidlMapper.cpp
index 63ace8c..e4e0909 100644
--- a/media/libaudiohal/impl/Hal2AidlMapper.cpp
+++ b/media/libaudiohal/impl/Hal2AidlMapper.cpp
@@ -247,6 +247,7 @@
         ALOGD("%s: disconnected port replacement: %s", __func__, port.toString().c_str());
         mDisconnectedPortReplacement = std::pair<int32_t, AudioPort>();
     }
+    updateDynamicMixPorts();
 }
 
 status_t Hal2AidlMapper::findOrCreatePatch(
@@ -1002,6 +1003,9 @@
     if (status == OK) {
         auto portIt = mPorts.find(portId);
         if (portIt != mPorts.end()) {
+            if (port->ext.getTag() == AudioPortExt::Tag::mix && portIt->second != *port) {
+                mDynamicMixPortIds.insert(portId);
+            }
             portIt->second = *port;
         } else {
             ALOGW("%s, port(%d) returned successfully from the HAL but not it is not cached",
@@ -1050,4 +1054,15 @@
     return OK;
 }
 
+void Hal2AidlMapper::updateDynamicMixPorts() {
+    for (int32_t portId : mDynamicMixPortIds) {
+        if (auto it = mPorts.find(portId); it != mPorts.end()) {
+            updateAudioPort(portId, &it->second);
+        } else {
+            // This must not happen
+            ALOGE("%s, cannot find port for id=%d", __func__, portId);
+        }
+    }
+}
+
 } // namespace android