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/services/audiopolicy/managerdefault/AudioPolicyManager.cpp b/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
index 587fd0e..3dbb177 100644
--- a/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
+++ b/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
@@ -115,14 +115,13 @@
}
void AudioPolicyManager::broadcastDeviceConnectionState(const sp<DeviceDescriptor> &device,
- audio_policy_dev_state_t state)
+ media::DeviceConnectedState state)
{
audio_port_v7 devicePort;
device->toAudioPort(&devicePort);
- if (status_t status = mpClientInterface->setDeviceConnectedState(
- &devicePort, state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
+ if (status_t status = mpClientInterface->setDeviceConnectedState(&devicePort, state);
status != OK) {
- ALOGE("Error %d while setting connected state for device %s", status,
+ ALOGE("Error %d while setting connected state for device %s", state,
device->getDeviceTypeAddr().toString(false).c_str());
}
}
@@ -205,14 +204,14 @@
// Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic
// parameters on newly connected devices (instead of opening the outputs...)
- broadcastDeviceConnectionState(device, state);
+ broadcastDeviceConnectionState(device, media::DeviceConnectedState::CONNECTED);
if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) {
mAvailableOutputDevices.remove(device);
mHwModules.cleanUpForDevice(device);
- broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE);
+ broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED);
return INVALID_OPERATION;
}
@@ -234,8 +233,9 @@
ALOGV("%s() disconnecting output device %s", __func__, device->toString().c_str());
- // Send Disconnect to HALs
- broadcastDeviceConnectionState(device, state);
+ // Notify the HAL to prepare to disconnect device
+ broadcastDeviceConnectionState(
+ device, media::DeviceConnectedState::PREPARE_TO_DISCONNECT);
// remove device from available output devices
mAvailableOutputDevices.remove(device);
@@ -244,6 +244,9 @@
checkOutputsForDevice(device, state, outputs);
+ // Send Disconnect to HALs
+ broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED);
+
// Reset active device codec
device->setEncodedFormat(AUDIO_FORMAT_DEFAULT);
@@ -377,12 +380,12 @@
// Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic
// parameters on newly connected devices (instead of opening the inputs...)
- broadcastDeviceConnectionState(device, state);
+ broadcastDeviceConnectionState(device, media::DeviceConnectedState::CONNECTED);
if (checkInputsForDevice(device, state) != NO_ERROR) {
mAvailableInputDevices.remove(device);
- broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE);
+ broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED);
mHwModules.cleanUpForDevice(device);
@@ -400,13 +403,17 @@
ALOGV("%s() disconnecting input device %s", __func__, device->toString().c_str());
- // Set Disconnect to HALs
- broadcastDeviceConnectionState(device, state);
+ // Notify the HAL to prepare to disconnect device
+ broadcastDeviceConnectionState(
+ device, media::DeviceConnectedState::PREPARE_TO_DISCONNECT);
mAvailableInputDevices.remove(device);
checkInputsForDevice(device, state);
+ // Set Disconnect to HALs
+ broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED);
+
// remove device from mReportedFormatsMap cache
mReportedFormatsMap.erase(device);
} break;