François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "APM::AudioPolicyEngine" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | //#define VERY_VERBOSE_LOGGING |
| 21 | #ifdef VERY_VERBOSE_LOGGING |
| 22 | #define ALOGVV ALOGV |
| 23 | #else |
| 24 | #define ALOGVV(a...) do { } while(0) |
| 25 | #endif |
| 26 | |
| 27 | #include "Engine.h" |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 28 | #include "Stream.h" |
| 29 | #include "InputSource.h" |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 30 | |
| 31 | #include <EngineConfig.h> |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 32 | #include <policy.h> |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 33 | #include <AudioIODescriptorInterface.h> |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 34 | #include <ParameterManagerWrapper.h> |
jiabin | 12dc6b0 | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 35 | #include <media/AudioContainers.h> |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 36 | |
François Gaffie | c60c369 | 2019-08-09 15:41:24 +0200 | [diff] [blame] | 37 | #include <media/TypeConverter.h> |
| 38 | |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 39 | #include <cinttypes> |
| 40 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 41 | using std::string; |
| 42 | using std::map; |
| 43 | |
François Gaffie | f19cf79 | 2018-05-30 17:22:17 +0200 | [diff] [blame] | 44 | namespace android { |
| 45 | namespace audio_policy { |
| 46 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 47 | template <> |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 48 | StreamCollection &Engine::getCollection<audio_stream_type_t>() |
| 49 | { |
| 50 | return mStreamCollection; |
| 51 | } |
| 52 | template <> |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 53 | InputSourceCollection &Engine::getCollection<audio_source_t>() |
| 54 | { |
| 55 | return mInputSourceCollection; |
| 56 | } |
| 57 | |
| 58 | template <> |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 59 | const StreamCollection &Engine::getCollection<audio_stream_type_t>() const |
| 60 | { |
| 61 | return mStreamCollection; |
| 62 | } |
| 63 | template <> |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 64 | const InputSourceCollection &Engine::getCollection<audio_source_t>() const |
| 65 | { |
| 66 | return mInputSourceCollection; |
| 67 | } |
| 68 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 69 | Engine::Engine() : mPolicyParameterMgr(new ParameterManagerWrapper()) |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 70 | { |
Mikhail Naganov | f1b6d97 | 2023-05-02 13:56:01 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Mikhail Naganov | 9e459d7 | 2023-05-05 17:36:39 -0700 | [diff] [blame] | 73 | status_t Engine::loadFromHalConfigWithFallback( |
François Gaffie | ad3dce9 | 2024-03-26 17:20:04 +0100 | [diff] [blame^] | 74 | const media::audio::common::AudioHalEngineConfig& aidlConfig) { |
Mikhail Naganov | 9e459d7 | 2023-05-05 17:36:39 -0700 | [diff] [blame] | 75 | |
François Gaffie | ad3dce9 | 2024-03-26 17:20:04 +0100 | [diff] [blame^] | 76 | auto capResult = capEngineConfig::convert(aidlConfig); |
| 77 | if (capResult.parsedConfig == nullptr) { |
| 78 | ALOGE("%s CapEngine Config invalid", __func__); |
| 79 | return BAD_VALUE; |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 80 | } |
François Gaffie | ad3dce9 | 2024-03-26 17:20:04 +0100 | [diff] [blame^] | 81 | status_t ret = loadWithFallback(aidlConfig); |
| 82 | if (ret != NO_ERROR) { |
| 83 | return ret; |
| 84 | } |
| 85 | auto loadCriteria= [this](const auto& capCriteria) { |
| 86 | for (auto& capCriterion : capCriteria) { |
| 87 | mPolicyParameterMgr->addCriterion(capCriterion.criterion.name, |
| 88 | capCriterion.criterionType.isInclusive, |
| 89 | capCriterion.criterionType.valuePairs, |
| 90 | capCriterion.criterion.defaultLiteralValue); |
| 91 | } |
| 92 | }; |
| 93 | loadCriteria(capResult.parsedConfig->capCriteria); |
François Gaffie | ab1837a | 2019-10-15 10:48:50 +0200 | [diff] [blame] | 94 | std::string error; |
| 95 | if (mPolicyParameterMgr == nullptr || mPolicyParameterMgr->start(error) != NO_ERROR) { |
| 96 | ALOGE("%s: could not start Policy PFW: %s", __FUNCTION__, error.c_str()); |
François Gaffie | 0f17ab7 | 2015-05-13 18:13:00 +0200 | [diff] [blame] | 97 | return NO_INIT; |
| 98 | } |
François Gaffie | ad3dce9 | 2024-03-26 17:20:04 +0100 | [diff] [blame^] | 99 | return mPolicyParameterMgr->setConfiguration(capResult); |
| 100 | } |
| 101 | |
| 102 | status_t Engine::loadFromXmlConfigWithFallback(const std::string& xmlFilePath) |
| 103 | { |
| 104 | status_t status = loadWithFallback(xmlFilePath); |
| 105 | std::string error; |
| 106 | if (mPolicyParameterMgr == nullptr || mPolicyParameterMgr->start(error) != NO_ERROR) { |
| 107 | ALOGE("%s: could not start Policy PFW: %s", __FUNCTION__, error.c_str()); |
| 108 | return NO_INIT; |
| 109 | } |
| 110 | return status; |
| 111 | } |
| 112 | |
| 113 | template<typename T> |
| 114 | status_t Engine::loadWithFallback(const T& configSource) { |
| 115 | auto result = EngineBase::loadAudioPolicyEngineConfig(configSource); |
| 116 | ALOGE_IF(result.nbSkippedElement != 0, |
| 117 | "Policy Engine configuration is partially invalid, skipped %zu elements", |
| 118 | result.nbSkippedElement); |
| 119 | |
| 120 | auto loadCriteria= [this](const auto& configCriteria, const auto& configCriterionTypes) { |
| 121 | for (auto& criterion : configCriteria) { |
| 122 | engineConfig::CriterionType criterionType; |
| 123 | for (auto &configCriterionType : configCriterionTypes) { |
| 124 | if (configCriterionType.name == criterion.typeName) { |
| 125 | criterionType = configCriterionType; |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | ALOG_ASSERT(not criterionType.name.empty(), "Invalid criterion type for %s", |
| 130 | criterion.name.c_str()); |
| 131 | mPolicyParameterMgr->addCriterion(criterion.name, criterionType.isInclusive, |
| 132 | criterionType.valuePairs, |
| 133 | criterion.defaultLiteralValue); |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | loadCriteria(result.parsedConfig->criteria, result.parsedConfig->criterionTypes); |
| 138 | return result.nbSkippedElement == 0? NO_ERROR : BAD_VALUE; |
| 139 | } |
| 140 | |
| 141 | status_t Engine::initCheck() |
| 142 | { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 143 | return EngineBase::initCheck(); |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 144 | } |
| 145 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 146 | template <typename Key> |
| 147 | Element<Key> *Engine::getFromCollection(const Key &key) const |
| 148 | { |
François Gaffie | d85c160 | 2023-05-05 13:41:49 +0200 | [diff] [blame] | 149 | const Collection<Key> &collection = getCollection<Key>(); |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 150 | return collection.get(key); |
| 151 | } |
| 152 | |
| 153 | template <typename Key> |
| 154 | status_t Engine::add(const std::string &name, const Key &key) |
| 155 | { |
| 156 | Collection<Key> &collection = getCollection<Key>(); |
| 157 | return collection.add(name, key); |
| 158 | } |
| 159 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 160 | template <typename Property, typename Key> |
| 161 | Property Engine::getPropertyForKey(Key key) const |
| 162 | { |
| 163 | Element<Key> *element = getFromCollection<Key>(key); |
| 164 | if (element == NULL) { |
| 165 | ALOGE("%s: Element not found within collection", __FUNCTION__); |
| 166 | return static_cast<Property>(0); |
| 167 | } |
| 168 | return element->template get<Property>(); |
| 169 | } |
| 170 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 171 | bool Engine::setVolumeProfileForStream(const audio_stream_type_t &stream, |
| 172 | const audio_stream_type_t &profile) |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 173 | { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 174 | if (setPropertyForKey<audio_stream_type_t, audio_stream_type_t>(stream, profile)) { |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 175 | switchVolumeCurve(profile, stream); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 176 | return true; |
| 177 | } |
| 178 | return false; |
| 179 | } |
| 180 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 181 | template <typename Property, typename Key> |
| 182 | bool Engine::setPropertyForKey(const Property &property, const Key &key) |
| 183 | { |
| 184 | Element<Key> *element = getFromCollection<Key>(key); |
| 185 | if (element == NULL) { |
| 186 | ALOGE("%s: Element not found within collection", __FUNCTION__); |
Francois Gaffie | 15811d3 | 2020-01-14 17:26:02 +0100 | [diff] [blame] | 187 | return false; |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 188 | } |
| 189 | return element->template set<Property>(property) == NO_ERROR; |
| 190 | } |
| 191 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 192 | status_t Engine::setPhoneState(audio_mode_t mode) |
| 193 | { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 194 | status_t status = mPolicyParameterMgr->setPhoneState(mode); |
| 195 | if (status != NO_ERROR) { |
| 196 | return status; |
| 197 | } |
| 198 | return EngineBase::setPhoneState(mode); |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | audio_mode_t Engine::getPhoneState() const |
| 202 | { |
| 203 | return mPolicyParameterMgr->getPhoneState(); |
| 204 | } |
| 205 | |
| 206 | status_t Engine::setForceUse(audio_policy_force_use_t usage, |
| 207 | audio_policy_forced_cfg_t config) |
| 208 | { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 209 | status_t status = mPolicyParameterMgr->setForceUse(usage, config); |
| 210 | if (status != NO_ERROR) { |
| 211 | return status; |
| 212 | } |
| 213 | return EngineBase::setForceUse(usage, config); |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | audio_policy_forced_cfg_t Engine::getForceUse(audio_policy_force_use_t usage) const |
| 217 | { |
| 218 | return mPolicyParameterMgr->getForceUse(usage); |
| 219 | } |
| 220 | |
Eric Laurent | 32d01f3 | 2022-12-22 16:16:21 +0100 | [diff] [blame] | 221 | status_t Engine::setOutputDevicesConnectionState(const DeviceVector &devices, |
| 222 | audio_policy_dev_state_t state) |
| 223 | { |
| 224 | for (const auto &device : devices) { |
| 225 | mPolicyParameterMgr->setDeviceConnectionState(device->type(), device->address(), state); |
| 226 | } |
| 227 | DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices(); |
| 228 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 229 | availableOutputDevices.remove(devices); |
| 230 | } else { |
| 231 | availableOutputDevices.add(devices); |
| 232 | } |
| 233 | return mPolicyParameterMgr->setAvailableOutputDevices(availableOutputDevices.types()); |
| 234 | } |
| 235 | |
François Gaffie | ab1837a | 2019-10-15 10:48:50 +0200 | [diff] [blame] | 236 | status_t Engine::setDeviceConnectionState(const sp<DeviceDescriptor> device, |
François Gaffie | 3305c11 | 2018-02-22 10:56:49 +0100 | [diff] [blame] | 237 | audio_policy_dev_state_t state) |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 238 | { |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 239 | mPolicyParameterMgr->setDeviceConnectionState(device->type(), device->address(), state); |
François Gaffie | ab1837a | 2019-10-15 10:48:50 +0200 | [diff] [blame] | 240 | if (audio_is_output_device(device->type())) { |
François Gaffie | a3e696d | 2015-12-18 09:38:43 +0100 | [diff] [blame] | 241 | return mPolicyParameterMgr->setAvailableOutputDevices( |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 242 | getApmObserver()->getAvailableOutputDevices().types()); |
François Gaffie | ab1837a | 2019-10-15 10:48:50 +0200 | [diff] [blame] | 243 | } else if (audio_is_input_device(device->type())) { |
François Gaffie | a3e696d | 2015-12-18 09:38:43 +0100 | [diff] [blame] | 244 | return mPolicyParameterMgr->setAvailableInputDevices( |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 245 | getApmObserver()->getAvailableInputDevices().types()); |
François Gaffie | a3e696d | 2015-12-18 09:38:43 +0100 | [diff] [blame] | 246 | } |
François Gaffie | ab1837a | 2019-10-15 10:48:50 +0200 | [diff] [blame] | 247 | return EngineBase::setDeviceConnectionState(device, state); |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Eric Laurent | 32d01f3 | 2022-12-22 16:16:21 +0100 | [diff] [blame] | 250 | status_t Engine::setDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role, |
| 251 | const AudioDeviceTypeAddrVector &devices) |
| 252 | { |
| 253 | DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices(); |
| 254 | DeviceVector prevDisabledDevices = |
| 255 | getDisabledDevicesForProductStrategy(availableOutputDevices, strategy); |
| 256 | status_t status = EngineBase::setDevicesRoleForStrategy(strategy, role, devices); |
| 257 | if (status != NO_ERROR) { |
| 258 | return status; |
| 259 | } |
| 260 | DeviceVector newDisabledDevices = |
| 261 | getDisabledDevicesForProductStrategy(availableOutputDevices, strategy); |
| 262 | if (role == DEVICE_ROLE_PREFERRED) { |
| 263 | DeviceVector reenabledDevices = prevDisabledDevices; |
| 264 | reenabledDevices.remove(newDisabledDevices); |
| 265 | if (reenabledDevices.empty()) { |
| 266 | ALOGD("%s DEVICE_ROLE_PREFERRED empty renabled devices", __func__); |
| 267 | return status; |
| 268 | } |
| 269 | // some devices were moved from disabled to preferred, need to force a resync for these |
| 270 | enableDevicesForStrategy(strategy, prevDisabledDevices); |
| 271 | } |
| 272 | if (newDisabledDevices.empty()) { |
| 273 | return status; |
| 274 | } |
| 275 | return disableDevicesForStrategy(strategy, newDisabledDevices); |
| 276 | } |
| 277 | |
| 278 | status_t Engine::removeDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role, |
| 279 | const AudioDeviceTypeAddrVector &devices) |
| 280 | { |
| 281 | const auto productStrategies = getProductStrategies(); |
| 282 | if (productStrategies.find(strategy) == end(productStrategies)) { |
| 283 | ALOGE("%s invalid %d", __func__, strategy); |
| 284 | return BAD_VALUE; |
| 285 | } |
| 286 | DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices(); |
| 287 | DeviceVector prevDisabledDevices = |
| 288 | getDisabledDevicesForProductStrategy(availableOutputDevices, strategy); |
| 289 | status_t status = EngineBase::removeDevicesRoleForStrategy(strategy, role, devices); |
| 290 | if (status != NO_ERROR || role == DEVICE_ROLE_PREFERRED) { |
| 291 | return status; |
| 292 | } |
| 293 | // Removing ROLE_DISABLED for given devices, need to force a resync for these |
| 294 | enableDevicesForStrategy(strategy, prevDisabledDevices); |
| 295 | |
| 296 | DeviceVector remainingDisabledDevices = getDisabledDevicesForProductStrategy( |
| 297 | availableOutputDevices, strategy); |
| 298 | if (remainingDisabledDevices.empty()) { |
| 299 | return status; |
| 300 | } |
| 301 | return disableDevicesForStrategy(strategy, remainingDisabledDevices); |
| 302 | } |
| 303 | |
| 304 | status_t Engine::clearDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role) |
| 305 | { |
| 306 | const auto productStrategies = getProductStrategies(); |
| 307 | if (productStrategies.find(strategy) == end(productStrategies)) { |
| 308 | ALOGE("%s invalid %d", __func__, strategy); |
| 309 | return BAD_VALUE; |
| 310 | } |
| 311 | DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices(); |
| 312 | DeviceVector prevDisabledDevices = |
| 313 | getDisabledDevicesForProductStrategy(availableOutputDevices, strategy); |
| 314 | status_t status = EngineBase::clearDevicesRoleForStrategy(strategy, role); |
| 315 | if (status != NO_ERROR || role == DEVICE_ROLE_PREFERRED || prevDisabledDevices.empty()) { |
| 316 | return status; |
| 317 | } |
| 318 | // Disabled devices were removed, need to force a resync for these |
| 319 | enableDevicesForStrategy(strategy, prevDisabledDevices); |
| 320 | return NO_ERROR; |
| 321 | } |
| 322 | |
| 323 | void Engine::enableDevicesForStrategy(product_strategy_t strategy __unused, |
| 324 | const DeviceVector &devicesToEnable) { |
| 325 | // devices were (re)enabled, need to force a resync for these |
| 326 | setOutputDevicesConnectionState(devicesToEnable, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); |
| 327 | setOutputDevicesConnectionState(devicesToEnable, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 328 | } |
| 329 | |
| 330 | status_t Engine::disableDevicesForStrategy(product_strategy_t strategy, |
| 331 | const DeviceVector &devicesToDisable) { |
| 332 | // Filter out disabled devices for this strategy. |
| 333 | // However, to update the output device decision, availability criterion shall be updated, |
| 334 | // which may impact other strategies. So, as a WA, reconsider now and later to prevent from |
| 335 | // altering decision for other strategies; |
| 336 | setOutputDevicesConnectionState(devicesToDisable, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); |
| 337 | |
| 338 | DeviceTypeSet deviceTypes = getProductStrategies().getDeviceTypesForProductStrategy(strategy); |
| 339 | const std::string address(getProductStrategies().getDeviceAddressForProductStrategy(strategy)); |
| 340 | |
| 341 | setOutputDevicesConnectionState(devicesToDisable, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 342 | |
| 343 | // Force reapply devices for given strategy |
| 344 | getProductStrategies().at(strategy)->setDeviceTypes(deviceTypes); |
| 345 | setDeviceAddressForProductStrategy(strategy, address); |
| 346 | return NO_ERROR; |
| 347 | } |
| 348 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 349 | DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t ps) const |
| 350 | { |
Eric Laurent | 32d01f3 | 2022-12-22 16:16:21 +0100 | [diff] [blame] | 351 | DeviceVector selectedDevices = {}; |
| 352 | DeviceVector disabledDevices = {}; |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 353 | const auto productStrategies = getProductStrategies(); |
| 354 | if (productStrategies.find(ps) == productStrategies.end()) { |
| 355 | ALOGE("%s: Trying to get device on invalid strategy %d", __FUNCTION__, ps); |
Eric Laurent | 32d01f3 | 2022-12-22 16:16:21 +0100 | [diff] [blame] | 356 | return selectedDevices; |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 357 | } |
Eric Laurent | 32d01f3 | 2022-12-22 16:16:21 +0100 | [diff] [blame] | 358 | DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices(); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 359 | const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs(); |
jiabin | 12dc6b0 | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 360 | DeviceTypeSet availableOutputDevicesTypes = availableOutputDevices.types(); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 361 | |
Eric Laurent | 32d01f3 | 2022-12-22 16:16:21 +0100 | [diff] [blame] | 362 | // check if this strategy has a preferred device that is available, |
| 363 | // if yes, give priority to it. |
| 364 | DeviceVector preferredAvailableDevVec = |
| 365 | getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, ps); |
| 366 | if (!preferredAvailableDevVec.isEmpty()) { |
| 367 | return preferredAvailableDevVec; |
| 368 | } |
| 369 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 370 | /** This is the only case handled programmatically because the PFW is unable to know the |
| 371 | * activity of streams. |
| 372 | * |
| 373 | * -While media is playing on a remote device, use the the sonification behavior. |
| 374 | * Note that we test this usecase before testing if media is playing because |
| 375 | * the isStreamActive() method only informs about the activity of a stream, not |
| 376 | * if it's for local playback. Note also that we use the same delay between both tests |
| 377 | * |
| 378 | * -When media is not playing anymore, fall back on the sonification behavior |
| 379 | */ |
jiabin | 12dc6b0 | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 380 | DeviceTypeSet deviceTypes; |
Eric Laurent | 32d01f3 | 2022-12-22 16:16:21 +0100 | [diff] [blame] | 381 | product_strategy_t psOrFallback = ps; |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 382 | if (ps == getProductStrategyForStream(AUDIO_STREAM_NOTIFICATION) && |
| 383 | !is_state_in_call(getPhoneState()) && |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 384 | !outputs.isActiveRemotely(toVolumeSource(AUDIO_STREAM_MUSIC), |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 385 | SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY) && |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 386 | outputs.isActive(toVolumeSource(AUDIO_STREAM_MUSIC), |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 387 | SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { |
Eric Laurent | 32d01f3 | 2022-12-22 16:16:21 +0100 | [diff] [blame] | 388 | psOrFallback = getProductStrategyForStream(AUDIO_STREAM_MUSIC); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 389 | } else if (ps == getProductStrategyForStream(AUDIO_STREAM_ACCESSIBILITY) && |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 390 | (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) || |
| 391 | outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM)))) { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 392 | // do not route accessibility prompts to a digital output currently configured with a |
| 393 | // compressed format as they would likely not be mixed and dropped. |
| 394 | // Device For Sonification conf file has HDMI, SPDIF and HDMI ARC unreacheable. |
Eric Laurent | 32d01f3 | 2022-12-22 16:16:21 +0100 | [diff] [blame] | 395 | psOrFallback = getProductStrategyForStream(AUDIO_STREAM_RING); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 396 | } |
Eric Laurent | 32d01f3 | 2022-12-22 16:16:21 +0100 | [diff] [blame] | 397 | disabledDevices = getDisabledDevicesForProductStrategy(availableOutputDevices, psOrFallback); |
| 398 | deviceTypes = productStrategies.getDeviceTypesForProductStrategy(psOrFallback); |
| 399 | // In case a fallback is decided on other strategy, prevent from selecting this device if |
| 400 | // disabled for current strategy. |
| 401 | availableOutputDevices.remove(disabledDevices); |
| 402 | |
jiabin | 12dc6b0 | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 403 | if (deviceTypes.empty() || |
| 404 | Intersection(deviceTypes, availableOutputDevicesTypes).empty()) { |
François Gaffie | c60c369 | 2019-08-09 15:41:24 +0200 | [diff] [blame] | 405 | auto defaultDevice = getApmObserver()->getDefaultOutputDevice(); |
| 406 | ALOG_ASSERT(defaultDevice != nullptr, "no valid default device defined"); |
Eric Laurent | 32d01f3 | 2022-12-22 16:16:21 +0100 | [diff] [blame] | 407 | selectedDevices = DeviceVector(defaultDevice); |
| 408 | } else if (/*device_distinguishes_on_address(*deviceTypes.begin())*/ isSingleDeviceType( |
jiabin | 12dc6b0 | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 409 | deviceTypes, AUDIO_DEVICE_OUT_BUS)) { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 410 | // We do expect only one device for these types of devices |
| 411 | // Criterion device address garantee this one is available |
| 412 | // If this criterion is not wished, need to ensure this device is available |
| 413 | const String8 address(productStrategies.getDeviceAddressForProductStrategy(ps).c_str()); |
jiabin | 12dc6b0 | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 414 | ALOGV("%s:device %s %s %d", |
| 415 | __FUNCTION__, dumpDeviceTypes(deviceTypes).c_str(), address.c_str(), ps); |
| 416 | auto busDevice = availableOutputDevices.getDevice( |
| 417 | *deviceTypes.begin(), address, AUDIO_FORMAT_DEFAULT); |
François Gaffie | c60c369 | 2019-08-09 15:41:24 +0200 | [diff] [blame] | 418 | if (busDevice == nullptr) { |
jiabin | 12dc6b0 | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 419 | ALOGE("%s:unavailable device %s %s, fallback on default", __func__, |
| 420 | dumpDeviceTypes(deviceTypes).c_str(), address.c_str()); |
François Gaffie | c60c369 | 2019-08-09 15:41:24 +0200 | [diff] [blame] | 421 | auto defaultDevice = getApmObserver()->getDefaultOutputDevice(); |
| 422 | ALOG_ASSERT(defaultDevice != nullptr, "Default Output Device NOT available"); |
Eric Laurent | 32d01f3 | 2022-12-22 16:16:21 +0100 | [diff] [blame] | 423 | selectedDevices = DeviceVector(defaultDevice); |
| 424 | } else { |
| 425 | selectedDevices = DeviceVector(busDevice); |
François Gaffie | c60c369 | 2019-08-09 15:41:24 +0200 | [diff] [blame] | 426 | } |
Eric Laurent | 32d01f3 | 2022-12-22 16:16:21 +0100 | [diff] [blame] | 427 | } else { |
| 428 | ALOGV("%s:device %s %d", __FUNCTION__, dumpDeviceTypes(deviceTypes).c_str(), ps); |
| 429 | selectedDevices = availableOutputDevices.getDevicesFromTypes(deviceTypes); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 430 | } |
Eric Laurent | 32d01f3 | 2022-12-22 16:16:21 +0100 | [diff] [blame] | 431 | return selectedDevices; |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes, |
| 435 | const sp<DeviceDescriptor> &preferredDevice, |
| 436 | bool fromCache) const |
| 437 | { |
| 438 | // First check for explict routing device |
| 439 | if (preferredDevice != nullptr) { |
| 440 | ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str()); |
| 441 | return DeviceVector(preferredDevice); |
| 442 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 443 | product_strategy_t strategy = getProductStrategyForAttributes(attributes); |
Eric Laurent | af37777 | 2019-03-29 14:50:21 -0700 | [diff] [blame] | 444 | const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 445 | const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs(); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 446 | // |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 447 | // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to |
| 448 | // be by APM? |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 449 | // |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 450 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 451 | // case the last active client route is used |
| 452 | sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices); |
| 453 | if (device != nullptr) { |
| 454 | return DeviceVector(device); |
| 455 | } |
Francois Gaffie | 6f52ff9 | 2020-08-25 08:53:53 +0200 | [diff] [blame] | 456 | return fromCache? getCachedDevices(strategy) : getDevicesForProductStrategy(strategy); |
| 457 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 458 | |
Francois Gaffie | 6f52ff9 | 2020-08-25 08:53:53 +0200 | [diff] [blame] | 459 | DeviceVector Engine::getCachedDevices(product_strategy_t ps) const |
| 460 | { |
| 461 | return mDevicesForStrategies.find(ps) != mDevicesForStrategies.end() ? |
| 462 | mDevicesForStrategies.at(ps) : DeviceVector{}; |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const |
| 466 | { |
| 467 | auto attributes = EngineBase::getAttributesForStreamType(stream); |
| 468 | return getOutputDevicesForAttributes(attributes, nullptr, fromCache); |
| 469 | } |
| 470 | |
| 471 | sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr, |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 472 | uid_t uid, |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 473 | audio_session_t session, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 474 | sp<AudioPolicyMix> *mix) const |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 475 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 476 | const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection(); |
Eric Laurent | af37777 | 2019-03-29 14:50:21 -0700 | [diff] [blame] | 477 | const auto availableInputDevices = getApmObserver()->getAvailableInputDevices(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 478 | const auto &inputs = getApmObserver()->getInputs(); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 479 | std::string address; |
| 480 | // |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 481 | // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered |
| 482 | // first as it used to be by APM? |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 483 | // |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 484 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 485 | // case the last active client route is used |
| 486 | sp<DeviceDescriptor> device = |
| 487 | findPreferredDevice(inputs, attr.source, availableInputDevices); |
| 488 | if (device != nullptr) { |
| 489 | return device; |
| 490 | } |
| 491 | |
Jan Sebechlebsky | 28ed945 | 2022-09-15 17:57:42 +0200 | [diff] [blame] | 492 | device = policyMixes.getDeviceAndMixForInputSource(attr, |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 493 | availableInputDevices, |
| 494 | uid, |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 495 | session, |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 496 | mix); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 497 | if (device != nullptr) { |
| 498 | return device; |
| 499 | } |
| 500 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 501 | audio_devices_t deviceType = getPropertyForKey<audio_devices_t, audio_source_t>(attr.source); |
| 502 | |
| 503 | if (audio_is_remote_submix_device(deviceType)) { |
| 504 | address = "0"; |
| 505 | std::size_t pos; |
| 506 | std::string tags { attr.tags }; |
| 507 | if ((pos = tags.find("addr=")) != std::string::npos) { |
| 508 | address = tags.substr(pos + std::strlen("addr=")); |
| 509 | } |
| 510 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 511 | return availableInputDevices.getDevice(deviceType, String8(address.c_str()), AUDIO_FORMAT_DEFAULT); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 512 | } |
| 513 | |
François Gaffie | f1e9508 | 2018-11-02 13:53:31 +0100 | [diff] [blame] | 514 | void Engine::setDeviceAddressForProductStrategy(product_strategy_t strategy, |
| 515 | const std::string &address) |
| 516 | { |
| 517 | if (getProductStrategies().find(strategy) == getProductStrategies().end()) { |
| 518 | ALOGE("%s: Trying to set address %s on invalid strategy %d", __FUNCTION__, address.c_str(), |
| 519 | strategy); |
| 520 | return; |
| 521 | } |
| 522 | getProductStrategies().at(strategy)->setDeviceAddress(address); |
| 523 | } |
| 524 | |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 525 | bool Engine::setDeviceTypesForProductStrategy(product_strategy_t strategy, uint64_t devices) |
François Gaffie | f1e9508 | 2018-11-02 13:53:31 +0100 | [diff] [blame] | 526 | { |
| 527 | if (getProductStrategies().find(strategy) == getProductStrategies().end()) { |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 528 | ALOGE("%s: set device %" PRId64 " on invalid strategy %d", __FUNCTION__, devices, strategy); |
François Gaffie | f1e9508 | 2018-11-02 13:53:31 +0100 | [diff] [blame] | 529 | return false; |
| 530 | } |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 531 | // Here device matches the criterion value, need to rebuitd android device types; |
| 532 | DeviceTypeSet types = |
| 533 | mPolicyParameterMgr->convertDeviceCriterionValueToDeviceTypes(devices, true /*isOut*/); |
| 534 | getProductStrategies().at(strategy)->setDeviceTypes(types); |
François Gaffie | f1e9508 | 2018-11-02 13:53:31 +0100 | [diff] [blame] | 535 | return true; |
| 536 | } |
| 537 | |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 538 | bool Engine::setDeviceForInputSource(const audio_source_t &inputSource, uint64_t device) |
| 539 | { |
| 540 | DeviceTypeSet types = mPolicyParameterMgr->convertDeviceCriterionValueToDeviceTypes( |
| 541 | device, false /*isOut*/); |
| 542 | ALOG_ASSERT(types.size() <= 1, "one input device expected at most"); |
| 543 | audio_devices_t deviceType = types.empty() ? AUDIO_DEVICE_IN_DEFAULT : *types.begin(); |
| 544 | return setPropertyForKey<audio_devices_t, audio_source_t>(deviceType, inputSource); |
| 545 | } |
| 546 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 547 | template <> |
Mikhail Naganov | e13c679 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 548 | EngineInterface *Engine::queryInterface() |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 549 | { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 550 | return this; |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | template <> |
| 554 | AudioPolicyPluginInterface *Engine::queryInterface() |
| 555 | { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 556 | return this; |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | } // namespace audio_policy |
| 560 | } // namespace android |