audio: Fix handling of external devices disconnection
A mix port can be patched to multiple connected device ports. Thus, when
disconnecting an external device and removing the connected port, the
profiles of the mix port can only be cleared iff there are no more
connected device ports patched to it, and it did not have profiles prior to
connection of the first device.
Enhanced VTS tests to catch this problem in the HAL implementations. Also,
ensure that audio ports and audio routes do not change after the test
finishes. This ensures that tests can't affect each other.
Bug: 298175108
Test: atest audiosystem_tests
Test: atest VtsHalAudioCoreTargetTest
Change-Id: Ia666b874958fb260513fc2b8cd20a823953ec679
diff --git a/audio/aidl/default/Module.cpp b/audio/aidl/default/Module.cpp
index b7761bf..3117134 100644
--- a/audio/aidl/default/Module.cpp
+++ b/audio/aidl/default/Module.cpp
@@ -517,7 +517,7 @@
connectedPort.id = ++getConfig().nextPortId;
auto [connectedPortsIt, _] =
- mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::vector<int32_t>()));
+ mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::set<int32_t>()));
LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
<< "connected port ID " << connectedPort.id;
ports.push_back(connectedPort);
@@ -550,9 +550,21 @@
// of all profiles from all routable dynamic device ports would be more involved.
for (const auto mixPortId : routablePortIds) {
auto portsIt = findById<AudioPort>(ports, mixPortId);
- if (portsIt != ports.end() && portsIt->profiles.empty()) {
- portsIt->profiles = connectedPort.profiles;
- connectedPortsIt->second.push_back(portsIt->id);
+ if (portsIt != ports.end()) {
+ if (portsIt->profiles.empty()) {
+ portsIt->profiles = connectedPort.profiles;
+ connectedPortsIt->second.insert(portsIt->id);
+ } else {
+ // Check if profiles are non empty because they were populated by
+ // a previous connection. Otherwise, it means that they are not empty because
+ // the mix port has static profiles.
+ for (const auto cp : mConnectedDevicePorts) {
+ if (cp.second.count(portsIt->id) > 0) {
+ connectedPortsIt->second.insert(portsIt->id);
+ break;
+ }
+ }
+ }
}
}
*_aidl_return = std::move(connectedPort);
@@ -607,13 +619,20 @@
}
}
- for (const auto mixPortId : connectedPortsIt->second) {
+ // Clear profiles for mix ports that are not connected to any other ports.
+ std::set<int32_t> mixPortsToClear = std::move(connectedPortsIt->second);
+ mConnectedDevicePorts.erase(connectedPortsIt);
+ for (const auto& connectedPort : mConnectedDevicePorts) {
+ for (int32_t mixPortId : connectedPort.second) {
+ mixPortsToClear.erase(mixPortId);
+ }
+ }
+ for (int32_t mixPortId : mixPortsToClear) {
auto mixPortIt = findById<AudioPort>(ports, mixPortId);
if (mixPortIt != ports.end()) {
mixPortIt->profiles = {};
}
}
- mConnectedDevicePorts.erase(connectedPortsIt);
return ndk::ScopedAStatus::ok();
}
diff --git a/audio/aidl/default/include/core-impl/Module.h b/audio/aidl/default/include/core-impl/Module.h
index fb3eef2..bfdab51 100644
--- a/audio/aidl/default/include/core-impl/Module.h
+++ b/audio/aidl/default/include/core-impl/Module.h
@@ -142,7 +142,7 @@
// ids of device ports created at runtime via 'connectExternalDevice'.
// Also stores a list of ids of mix ports with dynamic profiles that were populated from
// the connected port. This list can be empty, thus an int->int multimap can't be used.
- using ConnectedDevicePorts = std::map<int32_t, std::vector<int32_t>>;
+ using ConnectedDevicePorts = std::map<int32_t, std::set<int32_t>>;
// Maps port ids and port config ids to patch ids.
// Multimap because both ports and configs can be used by multiple patches.
using Patches = std::multimap<int32_t, int32_t>;