Add prepareToDisconnectExternalDevice.
When an external device is disconnected, some HALs rely on the early
disconnect indication to immediately abort any active read/write
operations on drivers to avoid problems with HW interfaces. In that
case, APM will broadcast the device disconnection before the streams are
closed. However, in AOSP AIDL HAL, the device may not be disconnected
when there are active configurations. In that case, a new way to notify
the HAL to prepare to disconnect external device is required.
Bug: 277955540
Test: atest audiopolicy_tests
Test: connect/disconnect USB device
Change-Id: Ie5868ac5efedb3dbdd8b4c2e3225c0c4085806a1
Merged-In: Ie5868ac5efedb3dbdd8b4c2e3225c0c4085806a1
diff --git a/media/libaudiohal/impl/DeviceHalAidl.cpp b/media/libaudiohal/impl/DeviceHalAidl.cpp
index 1390c16..e452a35 100644
--- a/media/libaudiohal/impl/DeviceHalAidl.cpp
+++ b/media/libaudiohal/impl/DeviceHalAidl.cpp
@@ -907,12 +907,33 @@
return statusTFromBinderStatus(mModule->supportsVariableLatency(supports));
}
+
+status_t DeviceHalAidl::prepareToDisconnectExternalDevice(const struct audio_port_v7* port) {
+ // There is not AIDL API defined for `prepareToDisconnectExternalDevice`.
+ // Call `setConnectedState` instead.
+ // TODO(b/279824103): call prepareToDisconnectExternalDevice when it is added.
+ const status_t status = setConnectedState(port, false /*connected*/);
+ if (status == NO_ERROR) {
+ mDeviceDisconnectionNotified.insert(port->id);
+ }
+ return status;
+}
+
status_t DeviceHalAidl::setConnectedState(const struct audio_port_v7 *port, bool connected) {
TIME_CHECK();
if (!mModule) return NO_INIT;
if (port == nullptr) {
return BAD_VALUE;
}
+ if (!connected && mDeviceDisconnectionNotified.erase(port->id) > 0) {
+ // For device disconnection, APM will first call `prepareToDisconnectExternalDevice`
+ // and then call `setConnectedState`. However, there is no API for
+ // `prepareToDisconnectExternalDevice` yet. In that case, `setConnectedState` will be
+ // called when calling `prepareToDisconnectExternalDevice`. Do not call to the HAL if
+ // previous call is successful. Also remove the cache here to avoid a large cache after
+ // a long run.
+ return NO_ERROR;
+ }
bool isInput = VALUE_OR_RETURN_STATUS(::aidl::android::portDirection(port->role, port->type)) ==
::aidl::android::AudioPortDirection::INPUT;
AudioPort aidlPort = VALUE_OR_RETURN_STATUS(