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 | abb0478 | 2023-05-02 13:56:01 -0700 | [diff] [blame^] | 71 | } |
| 72 | |
| 73 | status_t Engine::loadFromXmlConfigWithFallback(const std::string& xmlFilePath) |
| 74 | { |
| 75 | status_t loadResult = loadAudioPolicyEngineConfig(xmlFilePath); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 76 | if (loadResult < 0) { |
| 77 | ALOGE("Policy Engine configuration is invalid."); |
| 78 | } |
Mikhail Naganov | abb0478 | 2023-05-02 13:56:01 -0700 | [diff] [blame^] | 79 | return loadResult; |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 80 | } |
| 81 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 82 | status_t Engine::initCheck() |
| 83 | { |
François Gaffie | ab1837a | 2019-10-15 10:48:50 +0200 | [diff] [blame] | 84 | std::string error; |
| 85 | if (mPolicyParameterMgr == nullptr || mPolicyParameterMgr->start(error) != NO_ERROR) { |
| 86 | 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] | 87 | return NO_INIT; |
| 88 | } |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 89 | return EngineBase::initCheck(); |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 90 | } |
| 91 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 92 | template <typename Key> |
| 93 | Element<Key> *Engine::getFromCollection(const Key &key) const |
| 94 | { |
| 95 | const Collection<Key> collection = getCollection<Key>(); |
| 96 | return collection.get(key); |
| 97 | } |
| 98 | |
| 99 | template <typename Key> |
| 100 | status_t Engine::add(const std::string &name, const Key &key) |
| 101 | { |
| 102 | Collection<Key> &collection = getCollection<Key>(); |
| 103 | return collection.add(name, key); |
| 104 | } |
| 105 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 106 | template <typename Property, typename Key> |
| 107 | Property Engine::getPropertyForKey(Key key) const |
| 108 | { |
| 109 | Element<Key> *element = getFromCollection<Key>(key); |
| 110 | if (element == NULL) { |
| 111 | ALOGE("%s: Element not found within collection", __FUNCTION__); |
| 112 | return static_cast<Property>(0); |
| 113 | } |
| 114 | return element->template get<Property>(); |
| 115 | } |
| 116 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 117 | bool Engine::setVolumeProfileForStream(const audio_stream_type_t &stream, |
| 118 | const audio_stream_type_t &profile) |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 119 | { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 120 | if (setPropertyForKey<audio_stream_type_t, audio_stream_type_t>(stream, profile)) { |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 121 | switchVolumeCurve(profile, stream); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 122 | return true; |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 127 | template <typename Property, typename Key> |
| 128 | bool Engine::setPropertyForKey(const Property &property, const Key &key) |
| 129 | { |
| 130 | Element<Key> *element = getFromCollection<Key>(key); |
| 131 | if (element == NULL) { |
| 132 | ALOGE("%s: Element not found within collection", __FUNCTION__); |
Francois Gaffie | 15811d3 | 2020-01-14 17:26:02 +0100 | [diff] [blame] | 133 | return false; |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 134 | } |
| 135 | return element->template set<Property>(property) == NO_ERROR; |
| 136 | } |
| 137 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 138 | status_t Engine::setPhoneState(audio_mode_t mode) |
| 139 | { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 140 | status_t status = mPolicyParameterMgr->setPhoneState(mode); |
| 141 | if (status != NO_ERROR) { |
| 142 | return status; |
| 143 | } |
| 144 | return EngineBase::setPhoneState(mode); |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | audio_mode_t Engine::getPhoneState() const |
| 148 | { |
| 149 | return mPolicyParameterMgr->getPhoneState(); |
| 150 | } |
| 151 | |
| 152 | status_t Engine::setForceUse(audio_policy_force_use_t usage, |
| 153 | audio_policy_forced_cfg_t config) |
| 154 | { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 155 | status_t status = mPolicyParameterMgr->setForceUse(usage, config); |
| 156 | if (status != NO_ERROR) { |
| 157 | return status; |
| 158 | } |
| 159 | return EngineBase::setForceUse(usage, config); |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | audio_policy_forced_cfg_t Engine::getForceUse(audio_policy_force_use_t usage) const |
| 163 | { |
| 164 | return mPolicyParameterMgr->getForceUse(usage); |
| 165 | } |
| 166 | |
François Gaffie | ab1837a | 2019-10-15 10:48:50 +0200 | [diff] [blame] | 167 | status_t Engine::setDeviceConnectionState(const sp<DeviceDescriptor> device, |
François Gaffie | 3305c11 | 2018-02-22 10:56:49 +0100 | [diff] [blame] | 168 | audio_policy_dev_state_t state) |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 169 | { |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 170 | mPolicyParameterMgr->setDeviceConnectionState(device->type(), device->address(), state); |
François Gaffie | ab1837a | 2019-10-15 10:48:50 +0200 | [diff] [blame] | 171 | if (audio_is_output_device(device->type())) { |
François Gaffie | a3e696d | 2015-12-18 09:38:43 +0100 | [diff] [blame] | 172 | return mPolicyParameterMgr->setAvailableOutputDevices( |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 173 | getApmObserver()->getAvailableOutputDevices().types()); |
François Gaffie | ab1837a | 2019-10-15 10:48:50 +0200 | [diff] [blame] | 174 | } else if (audio_is_input_device(device->type())) { |
François Gaffie | a3e696d | 2015-12-18 09:38:43 +0100 | [diff] [blame] | 175 | return mPolicyParameterMgr->setAvailableInputDevices( |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 176 | getApmObserver()->getAvailableInputDevices().types()); |
François Gaffie | a3e696d | 2015-12-18 09:38:43 +0100 | [diff] [blame] | 177 | } |
François Gaffie | ab1837a | 2019-10-15 10:48:50 +0200 | [diff] [blame] | 178 | return EngineBase::setDeviceConnectionState(device, state); |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Mikhail Naganov | abb0478 | 2023-05-02 13:56:01 -0700 | [diff] [blame^] | 181 | status_t Engine::loadAudioPolicyEngineConfig(const std::string& xmlFilePath) |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 182 | { |
Mikhail Naganov | abb0478 | 2023-05-02 13:56:01 -0700 | [diff] [blame^] | 183 | auto result = EngineBase::loadAudioPolicyEngineConfig(xmlFilePath); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 184 | |
François Gaffie | 7188f1a | 2018-11-02 14:35:42 +0100 | [diff] [blame] | 185 | // Custom XML Parsing |
| 186 | auto loadCriteria= [this](const auto& configCriteria, const auto& configCriterionTypes) { |
| 187 | for (auto& criterion : configCriteria) { |
| 188 | engineConfig::CriterionType criterionType; |
| 189 | for (auto &configCriterionType : configCriterionTypes) { |
| 190 | if (configCriterionType.name == criterion.typeName) { |
| 191 | criterionType = configCriterionType; |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | ALOG_ASSERT(not criterionType.name.empty(), "Invalid criterion type for %s", |
| 196 | criterion.name.c_str()); |
| 197 | mPolicyParameterMgr->addCriterion(criterion.name, criterionType.isInclusive, |
| 198 | criterionType.valuePairs, |
| 199 | criterion.defaultLiteralValue); |
| 200 | } |
| 201 | }; |
| 202 | |
| 203 | loadCriteria(result.parsedConfig->criteria, result.parsedConfig->criterionTypes); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 204 | return result.nbSkippedElement == 0? NO_ERROR : BAD_VALUE; |
| 205 | } |
| 206 | |
| 207 | DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t ps) const |
| 208 | { |
| 209 | const auto productStrategies = getProductStrategies(); |
| 210 | if (productStrategies.find(ps) == productStrategies.end()) { |
| 211 | ALOGE("%s: Trying to get device on invalid strategy %d", __FUNCTION__, ps); |
| 212 | return {}; |
| 213 | } |
Eric Laurent | af37777 | 2019-03-29 14:50:21 -0700 | [diff] [blame] | 214 | const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices(); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 215 | const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs(); |
jiabin | 12dc6b0 | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 216 | DeviceTypeSet availableOutputDevicesTypes = availableOutputDevices.types(); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 217 | |
| 218 | /** This is the only case handled programmatically because the PFW is unable to know the |
| 219 | * activity of streams. |
| 220 | * |
| 221 | * -While media is playing on a remote device, use the the sonification behavior. |
| 222 | * Note that we test this usecase before testing if media is playing because |
| 223 | * the isStreamActive() method only informs about the activity of a stream, not |
| 224 | * if it's for local playback. Note also that we use the same delay between both tests |
| 225 | * |
| 226 | * -When media is not playing anymore, fall back on the sonification behavior |
| 227 | */ |
jiabin | 12dc6b0 | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 228 | DeviceTypeSet deviceTypes; |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 229 | if (ps == getProductStrategyForStream(AUDIO_STREAM_NOTIFICATION) && |
| 230 | !is_state_in_call(getPhoneState()) && |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 231 | !outputs.isActiveRemotely(toVolumeSource(AUDIO_STREAM_MUSIC), |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 232 | SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY) && |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 233 | outputs.isActive(toVolumeSource(AUDIO_STREAM_MUSIC), |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 234 | SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 235 | product_strategy_t strategyForMedia = |
| 236 | getProductStrategyForStream(AUDIO_STREAM_MUSIC); |
jiabin | 12dc6b0 | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 237 | deviceTypes = productStrategies.getDeviceTypesForProductStrategy(strategyForMedia); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 238 | } else if (ps == getProductStrategyForStream(AUDIO_STREAM_ACCESSIBILITY) && |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 239 | (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) || |
| 240 | outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM)))) { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 241 | // do not route accessibility prompts to a digital output currently configured with a |
| 242 | // compressed format as they would likely not be mixed and dropped. |
| 243 | // Device For Sonification conf file has HDMI, SPDIF and HDMI ARC unreacheable. |
| 244 | product_strategy_t strategyNotification = getProductStrategyForStream(AUDIO_STREAM_RING); |
jiabin | 12dc6b0 | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 245 | deviceTypes = productStrategies.getDeviceTypesForProductStrategy(strategyNotification); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 246 | } else { |
jiabin | 12dc6b0 | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 247 | deviceTypes = productStrategies.getDeviceTypesForProductStrategy(ps); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 248 | } |
jiabin | 12dc6b0 | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 249 | if (deviceTypes.empty() || |
| 250 | Intersection(deviceTypes, availableOutputDevicesTypes).empty()) { |
François Gaffie | c60c369 | 2019-08-09 15:41:24 +0200 | [diff] [blame] | 251 | auto defaultDevice = getApmObserver()->getDefaultOutputDevice(); |
| 252 | ALOG_ASSERT(defaultDevice != nullptr, "no valid default device defined"); |
| 253 | return DeviceVector(defaultDevice); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 254 | } |
jiabin | 12dc6b0 | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 255 | if (/*device_distinguishes_on_address(*deviceTypes.begin())*/ isSingleDeviceType( |
| 256 | deviceTypes, AUDIO_DEVICE_OUT_BUS)) { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 257 | // We do expect only one device for these types of devices |
| 258 | // Criterion device address garantee this one is available |
| 259 | // If this criterion is not wished, need to ensure this device is available |
| 260 | const String8 address(productStrategies.getDeviceAddressForProductStrategy(ps).c_str()); |
jiabin | 12dc6b0 | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 261 | ALOGV("%s:device %s %s %d", |
| 262 | __FUNCTION__, dumpDeviceTypes(deviceTypes).c_str(), address.c_str(), ps); |
| 263 | auto busDevice = availableOutputDevices.getDevice( |
| 264 | *deviceTypes.begin(), address, AUDIO_FORMAT_DEFAULT); |
François Gaffie | c60c369 | 2019-08-09 15:41:24 +0200 | [diff] [blame] | 265 | if (busDevice == nullptr) { |
jiabin | 12dc6b0 | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 266 | ALOGE("%s:unavailable device %s %s, fallback on default", __func__, |
| 267 | dumpDeviceTypes(deviceTypes).c_str(), address.c_str()); |
François Gaffie | c60c369 | 2019-08-09 15:41:24 +0200 | [diff] [blame] | 268 | auto defaultDevice = getApmObserver()->getDefaultOutputDevice(); |
| 269 | ALOG_ASSERT(defaultDevice != nullptr, "Default Output Device NOT available"); |
| 270 | return DeviceVector(defaultDevice); |
| 271 | } |
jiabin | 12dc6b0 | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 272 | return DeviceVector(busDevice); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 273 | } |
jiabin | 12dc6b0 | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 274 | ALOGV("%s:device %s %d", __FUNCTION__, dumpDeviceTypes(deviceTypes).c_str(), ps); |
| 275 | return availableOutputDevices.getDevicesFromTypes(deviceTypes); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes, |
| 279 | const sp<DeviceDescriptor> &preferredDevice, |
| 280 | bool fromCache) const |
| 281 | { |
| 282 | // First check for explict routing device |
| 283 | if (preferredDevice != nullptr) { |
| 284 | ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str()); |
| 285 | return DeviceVector(preferredDevice); |
| 286 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 287 | product_strategy_t strategy = getProductStrategyForAttributes(attributes); |
Eric Laurent | af37777 | 2019-03-29 14:50:21 -0700 | [diff] [blame] | 288 | const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 289 | const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs(); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 290 | // |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 291 | // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to |
| 292 | // be by APM? |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 293 | // |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 294 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 295 | // case the last active client route is used |
| 296 | sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices); |
| 297 | if (device != nullptr) { |
| 298 | return DeviceVector(device); |
| 299 | } |
Francois Gaffie | 6f52ff9 | 2020-08-25 08:53:53 +0200 | [diff] [blame] | 300 | return fromCache? getCachedDevices(strategy) : getDevicesForProductStrategy(strategy); |
| 301 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 302 | |
Francois Gaffie | 6f52ff9 | 2020-08-25 08:53:53 +0200 | [diff] [blame] | 303 | DeviceVector Engine::getCachedDevices(product_strategy_t ps) const |
| 304 | { |
| 305 | return mDevicesForStrategies.find(ps) != mDevicesForStrategies.end() ? |
| 306 | mDevicesForStrategies.at(ps) : DeviceVector{}; |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const |
| 310 | { |
| 311 | auto attributes = EngineBase::getAttributesForStreamType(stream); |
| 312 | return getOutputDevicesForAttributes(attributes, nullptr, fromCache); |
| 313 | } |
| 314 | |
| 315 | sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr, |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 316 | uid_t uid, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 317 | sp<AudioPolicyMix> *mix) const |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 318 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 319 | const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection(); |
Eric Laurent | af37777 | 2019-03-29 14:50:21 -0700 | [diff] [blame] | 320 | const auto availableInputDevices = getApmObserver()->getAvailableInputDevices(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 321 | const auto &inputs = getApmObserver()->getInputs(); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 322 | std::string address; |
| 323 | // |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 324 | // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered |
| 325 | // first as it used to be by APM? |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 326 | // |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 327 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 328 | // case the last active client route is used |
| 329 | sp<DeviceDescriptor> device = |
| 330 | findPreferredDevice(inputs, attr.source, availableInputDevices); |
| 331 | if (device != nullptr) { |
| 332 | return device; |
| 333 | } |
| 334 | |
Jan Sebechlebsky | a7ed527 | 2022-09-15 17:57:42 +0200 | [diff] [blame] | 335 | device = policyMixes.getDeviceAndMixForInputSource(attr, |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 336 | availableInputDevices, |
| 337 | uid, |
| 338 | mix); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 339 | if (device != nullptr) { |
| 340 | return device; |
| 341 | } |
| 342 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 343 | audio_devices_t deviceType = getPropertyForKey<audio_devices_t, audio_source_t>(attr.source); |
| 344 | |
| 345 | if (audio_is_remote_submix_device(deviceType)) { |
| 346 | address = "0"; |
| 347 | std::size_t pos; |
| 348 | std::string tags { attr.tags }; |
| 349 | if ((pos = tags.find("addr=")) != std::string::npos) { |
| 350 | address = tags.substr(pos + std::strlen("addr=")); |
| 351 | } |
| 352 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 353 | return availableInputDevices.getDevice(deviceType, String8(address.c_str()), AUDIO_FORMAT_DEFAULT); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 354 | } |
| 355 | |
François Gaffie | f1e9508 | 2018-11-02 13:53:31 +0100 | [diff] [blame] | 356 | void Engine::setDeviceAddressForProductStrategy(product_strategy_t strategy, |
| 357 | const std::string &address) |
| 358 | { |
| 359 | if (getProductStrategies().find(strategy) == getProductStrategies().end()) { |
| 360 | ALOGE("%s: Trying to set address %s on invalid strategy %d", __FUNCTION__, address.c_str(), |
| 361 | strategy); |
| 362 | return; |
| 363 | } |
| 364 | getProductStrategies().at(strategy)->setDeviceAddress(address); |
| 365 | } |
| 366 | |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 367 | bool Engine::setDeviceTypesForProductStrategy(product_strategy_t strategy, uint64_t devices) |
François Gaffie | f1e9508 | 2018-11-02 13:53:31 +0100 | [diff] [blame] | 368 | { |
| 369 | if (getProductStrategies().find(strategy) == getProductStrategies().end()) { |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 370 | 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] | 371 | return false; |
| 372 | } |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 373 | // Here device matches the criterion value, need to rebuitd android device types; |
| 374 | DeviceTypeSet types = |
| 375 | mPolicyParameterMgr->convertDeviceCriterionValueToDeviceTypes(devices, true /*isOut*/); |
| 376 | getProductStrategies().at(strategy)->setDeviceTypes(types); |
François Gaffie | f1e9508 | 2018-11-02 13:53:31 +0100 | [diff] [blame] | 377 | return true; |
| 378 | } |
| 379 | |
Francois Gaffie | a12de21 | 2021-10-22 10:54:33 +0200 | [diff] [blame] | 380 | bool Engine::setDeviceForInputSource(const audio_source_t &inputSource, uint64_t device) |
| 381 | { |
| 382 | DeviceTypeSet types = mPolicyParameterMgr->convertDeviceCriterionValueToDeviceTypes( |
| 383 | device, false /*isOut*/); |
| 384 | ALOG_ASSERT(types.size() <= 1, "one input device expected at most"); |
| 385 | audio_devices_t deviceType = types.empty() ? AUDIO_DEVICE_IN_DEFAULT : *types.begin(); |
| 386 | return setPropertyForKey<audio_devices_t, audio_source_t>(deviceType, inputSource); |
| 387 | } |
| 388 | |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 389 | template <> |
Mikhail Naganov | e13c679 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 390 | EngineInterface *Engine::queryInterface() |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 391 | { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 392 | return this; |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | template <> |
| 396 | AudioPolicyPluginInterface *Engine::queryInterface() |
| 397 | { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 398 | return this; |
François Gaffie | 20f06f9 | 2015-03-24 09:01:14 +0100 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | } // namespace audio_policy |
| 402 | } // namespace android |