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