blob: ad49b1930178a53101bac89a834b44f9fc8bbf70 [file] [log] [blame]
François Gaffie20f06f92015-03-24 09:01:14 +01001/*
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 Gaffie20f06f92015-03-24 09:01:14 +010028#include "Stream.h"
29#include "InputSource.h"
François Gaffiedc7553f2018-11-02 10:39:57 +010030
31#include <EngineConfig.h>
François Gaffie20f06f92015-03-24 09:01:14 +010032#include <policy.h>
François Gaffiec005e562018-11-06 15:04:49 +010033#include <AudioIODescriptorInterface.h>
François Gaffie20f06f92015-03-24 09:01:14 +010034#include <ParameterManagerWrapper.h>
jiabin12dc6b02019-10-01 09:38:30 -070035#include <media/AudioContainers.h>
François Gaffie20f06f92015-03-24 09:01:14 +010036
François Gaffiec60c3692019-08-09 15:41:24 +020037#include <media/TypeConverter.h>
38
Francois Gaffiea12de212021-10-22 10:54:33 +020039#include <cinttypes>
40
François Gaffie20f06f92015-03-24 09:01:14 +010041using std::string;
42using std::map;
43
François Gaffief19cf792018-05-30 17:22:17 +020044namespace android {
45namespace audio_policy {
46
François Gaffie20f06f92015-03-24 09:01:14 +010047template <>
François Gaffie20f06f92015-03-24 09:01:14 +010048StreamCollection &Engine::getCollection<audio_stream_type_t>()
49{
50 return mStreamCollection;
51}
52template <>
François Gaffie20f06f92015-03-24 09:01:14 +010053InputSourceCollection &Engine::getCollection<audio_source_t>()
54{
55 return mInputSourceCollection;
56}
57
58template <>
François Gaffie20f06f92015-03-24 09:01:14 +010059const StreamCollection &Engine::getCollection<audio_stream_type_t>() const
60{
61 return mStreamCollection;
62}
63template <>
François Gaffie20f06f92015-03-24 09:01:14 +010064const InputSourceCollection &Engine::getCollection<audio_source_t>() const
65{
66 return mInputSourceCollection;
67}
68
François Gaffiedc7553f2018-11-02 10:39:57 +010069Engine::Engine() : mPolicyParameterMgr(new ParameterManagerWrapper())
François Gaffie20f06f92015-03-24 09:01:14 +010070{
Mikhail Naganovf1b6d972023-05-02 13:56:01 -070071}
72
Mikhail Naganov9e459d72023-05-05 17:36:39 -070073status_t Engine::loadFromHalConfigWithFallback(
François Gaffiead3dce92024-03-26 17:20:04 +010074 const media::audio::common::AudioHalEngineConfig& aidlConfig) {
Mikhail Naganov9e459d72023-05-05 17:36:39 -070075
François Gaffiead3dce92024-03-26 17:20:04 +010076 auto capResult = capEngineConfig::convert(aidlConfig);
77 if (capResult.parsedConfig == nullptr) {
78 ALOGE("%s CapEngine Config invalid", __func__);
79 return BAD_VALUE;
François Gaffiedc7553f2018-11-02 10:39:57 +010080 }
François Gaffiead3dce92024-03-26 17:20:04 +010081 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 Gaffieab1837a2019-10-15 10:48:50 +020094 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 Gaffie0f17ab72015-05-13 18:13:00 +020097 return NO_INIT;
98 }
François Gaffiead3dce92024-03-26 17:20:04 +010099 return mPolicyParameterMgr->setConfiguration(capResult);
100}
101
102status_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
113template<typename T>
114status_t Engine::loadWithFallback(const T& configSource) {
Mikhail Naganovbada1f52024-12-03 16:20:10 -0800115 auto result = EngineBase::loadAudioPolicyEngineConfig(configSource, true /*isConfigurable*/);
François Gaffiead3dce92024-03-26 17:20:04 +0100116 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
141status_t Engine::initCheck()
142{
François Gaffiedc7553f2018-11-02 10:39:57 +0100143 return EngineBase::initCheck();
François Gaffie20f06f92015-03-24 09:01:14 +0100144}
145
François Gaffie20f06f92015-03-24 09:01:14 +0100146template <typename Key>
147Element<Key> *Engine::getFromCollection(const Key &key) const
148{
François Gaffied85c1602023-05-05 13:41:49 +0200149 const Collection<Key> &collection = getCollection<Key>();
François Gaffie20f06f92015-03-24 09:01:14 +0100150 return collection.get(key);
151}
152
153template <typename Key>
154status_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 Gaffie20f06f92015-03-24 09:01:14 +0100160template <typename Property, typename Key>
161Property 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 Gaffiedc7553f2018-11-02 10:39:57 +0100171bool Engine::setVolumeProfileForStream(const audio_stream_type_t &stream,
172 const audio_stream_type_t &profile)
François Gaffied1ab2bd2015-12-02 18:20:06 +0100173{
François Gaffiedc7553f2018-11-02 10:39:57 +0100174 if (setPropertyForKey<audio_stream_type_t, audio_stream_type_t>(stream, profile)) {
Eric Laurentf5aa58d2019-02-22 18:20:11 -0800175 switchVolumeCurve(profile, stream);
François Gaffied1ab2bd2015-12-02 18:20:06 +0100176 return true;
177 }
178 return false;
179}
180
François Gaffie20f06f92015-03-24 09:01:14 +0100181template <typename Property, typename Key>
182bool 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 Gaffie15811d32020-01-14 17:26:02 +0100187 return false;
François Gaffie20f06f92015-03-24 09:01:14 +0100188 }
189 return element->template set<Property>(property) == NO_ERROR;
190}
191
François Gaffie20f06f92015-03-24 09:01:14 +0100192status_t Engine::setPhoneState(audio_mode_t mode)
193{
François Gaffiedc7553f2018-11-02 10:39:57 +0100194 status_t status = mPolicyParameterMgr->setPhoneState(mode);
195 if (status != NO_ERROR) {
196 return status;
197 }
198 return EngineBase::setPhoneState(mode);
François Gaffie20f06f92015-03-24 09:01:14 +0100199}
200
201audio_mode_t Engine::getPhoneState() const
202{
203 return mPolicyParameterMgr->getPhoneState();
204}
205
206status_t Engine::setForceUse(audio_policy_force_use_t usage,
207 audio_policy_forced_cfg_t config)
208{
François Gaffiedc7553f2018-11-02 10:39:57 +0100209 status_t status = mPolicyParameterMgr->setForceUse(usage, config);
210 if (status != NO_ERROR) {
211 return status;
212 }
213 return EngineBase::setForceUse(usage, config);
François Gaffie20f06f92015-03-24 09:01:14 +0100214}
215
216audio_policy_forced_cfg_t Engine::getForceUse(audio_policy_force_use_t usage) const
217{
218 return mPolicyParameterMgr->getForceUse(usage);
219}
220
Eric Laurent32d01f32022-12-22 16:16:21 +0100221status_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 Gaffieab1837a2019-10-15 10:48:50 +0200236status_t Engine::setDeviceConnectionState(const sp<DeviceDescriptor> device,
François Gaffie3305c112018-02-22 10:56:49 +0100237 audio_policy_dev_state_t state)
François Gaffie20f06f92015-03-24 09:01:14 +0100238{
Francois Gaffiea12de212021-10-22 10:54:33 +0200239 mPolicyParameterMgr->setDeviceConnectionState(device->type(), device->address(), state);
François Gaffieab1837a2019-10-15 10:48:50 +0200240 if (audio_is_output_device(device->type())) {
François Gaffiea3e696d2015-12-18 09:38:43 +0100241 return mPolicyParameterMgr->setAvailableOutputDevices(
Francois Gaffiea12de212021-10-22 10:54:33 +0200242 getApmObserver()->getAvailableOutputDevices().types());
François Gaffieab1837a2019-10-15 10:48:50 +0200243 } else if (audio_is_input_device(device->type())) {
François Gaffiea3e696d2015-12-18 09:38:43 +0100244 return mPolicyParameterMgr->setAvailableInputDevices(
Francois Gaffiea12de212021-10-22 10:54:33 +0200245 getApmObserver()->getAvailableInputDevices().types());
François Gaffiea3e696d2015-12-18 09:38:43 +0100246 }
François Gaffieab1837a2019-10-15 10:48:50 +0200247 return EngineBase::setDeviceConnectionState(device, state);
François Gaffie20f06f92015-03-24 09:01:14 +0100248}
249
Eric Laurent32d01f32022-12-22 16:16:21 +0100250status_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
278status_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
304status_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
323void 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
330status_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 Gaffiedc7553f2018-11-02 10:39:57 +0100349DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t ps) const
350{
Eric Laurent32d01f32022-12-22 16:16:21 +0100351 DeviceVector selectedDevices = {};
352 DeviceVector disabledDevices = {};
François Gaffiedc7553f2018-11-02 10:39:57 +0100353 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 Laurent32d01f32022-12-22 16:16:21 +0100356 return selectedDevices;
François Gaffiedc7553f2018-11-02 10:39:57 +0100357 }
Eric Laurent32d01f32022-12-22 16:16:21 +0100358 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100359 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabin12dc6b02019-10-01 09:38:30 -0700360 DeviceTypeSet availableOutputDevicesTypes = availableOutputDevices.types();
François Gaffiedc7553f2018-11-02 10:39:57 +0100361
Eric Laurent32d01f32022-12-22 16:16:21 +0100362 // 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 Gaffiedc7553f2018-11-02 10:39:57 +0100370 /** 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 */
jiabin12dc6b02019-10-01 09:38:30 -0700380 DeviceTypeSet deviceTypes;
Eric Laurent32d01f32022-12-22 16:16:21 +0100381 product_strategy_t psOrFallback = ps;
François Gaffiedc7553f2018-11-02 10:39:57 +0100382 if (ps == getProductStrategyForStream(AUDIO_STREAM_NOTIFICATION) &&
383 !is_state_in_call(getPhoneState()) &&
Eric Laurent83d17c22019-04-02 17:10:01 -0700384 !outputs.isActiveRemotely(toVolumeSource(AUDIO_STREAM_MUSIC),
François Gaffie1c878552018-11-22 16:53:21 +0100385 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY) &&
Eric Laurent83d17c22019-04-02 17:10:01 -0700386 outputs.isActive(toVolumeSource(AUDIO_STREAM_MUSIC),
François Gaffie1c878552018-11-22 16:53:21 +0100387 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
Eric Laurent32d01f32022-12-22 16:16:21 +0100388 psOrFallback = getProductStrategyForStream(AUDIO_STREAM_MUSIC);
François Gaffiedc7553f2018-11-02 10:39:57 +0100389 } else if (ps == getProductStrategyForStream(AUDIO_STREAM_ACCESSIBILITY) &&
Eric Laurent83d17c22019-04-02 17:10:01 -0700390 (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
391 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM)))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100392 // 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 Laurent32d01f32022-12-22 16:16:21 +0100395 psOrFallback = getProductStrategyForStream(AUDIO_STREAM_RING);
François Gaffiedc7553f2018-11-02 10:39:57 +0100396 }
Eric Laurent32d01f32022-12-22 16:16:21 +0100397 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
jiabin12dc6b02019-10-01 09:38:30 -0700403 if (deviceTypes.empty() ||
404 Intersection(deviceTypes, availableOutputDevicesTypes).empty()) {
François Gaffiec60c3692019-08-09 15:41:24 +0200405 auto defaultDevice = getApmObserver()->getDefaultOutputDevice();
406 ALOG_ASSERT(defaultDevice != nullptr, "no valid default device defined");
Eric Laurent32d01f32022-12-22 16:16:21 +0100407 selectedDevices = DeviceVector(defaultDevice);
408 } else if (/*device_distinguishes_on_address(*deviceTypes.begin())*/ isSingleDeviceType(
jiabin12dc6b02019-10-01 09:38:30 -0700409 deviceTypes, AUDIO_DEVICE_OUT_BUS)) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100410 // 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());
jiabin12dc6b02019-10-01 09:38:30 -0700414 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 Gaffiec60c3692019-08-09 15:41:24 +0200418 if (busDevice == nullptr) {
jiabin12dc6b02019-10-01 09:38:30 -0700419 ALOGE("%s:unavailable device %s %s, fallback on default", __func__,
420 dumpDeviceTypes(deviceTypes).c_str(), address.c_str());
François Gaffiec60c3692019-08-09 15:41:24 +0200421 auto defaultDevice = getApmObserver()->getDefaultOutputDevice();
422 ALOG_ASSERT(defaultDevice != nullptr, "Default Output Device NOT available");
Eric Laurent32d01f32022-12-22 16:16:21 +0100423 selectedDevices = DeviceVector(defaultDevice);
424 } else {
425 selectedDevices = DeviceVector(busDevice);
François Gaffiec60c3692019-08-09 15:41:24 +0200426 }
Eric Laurent32d01f32022-12-22 16:16:21 +0100427 } else {
428 ALOGV("%s:device %s %d", __FUNCTION__, dumpDeviceTypes(deviceTypes).c_str(), ps);
429 selectedDevices = availableOutputDevices.getDevicesFromTypes(deviceTypes);
François Gaffiedc7553f2018-11-02 10:39:57 +0100430 }
Eric Laurent32d01f32022-12-22 16:16:21 +0100431 return selectedDevices;
François Gaffiedc7553f2018-11-02 10:39:57 +0100432}
433
434DeviceVector 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 Gaffiec005e562018-11-06 15:04:49 +0100443 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700444 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100445 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100446 //
François Gaffiec005e562018-11-06 15:04:49 +0100447 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
448 // be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100449 //
François Gaffiec005e562018-11-06 15:04:49 +0100450 // 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 Gaffie6f52ff92020-08-25 08:53:53 +0200456 return fromCache? getCachedDevices(strategy) : getDevicesForProductStrategy(strategy);
457}
François Gaffiec005e562018-11-06 15:04:49 +0100458
Francois Gaffie6f52ff92020-08-25 08:53:53 +0200459DeviceVector Engine::getCachedDevices(product_strategy_t ps) const
460{
461 return mDevicesForStrategies.find(ps) != mDevicesForStrategies.end() ?
462 mDevicesForStrategies.at(ps) : DeviceVector{};
François Gaffiedc7553f2018-11-02 10:39:57 +0100463}
464
465DeviceVector 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
471sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800472 uid_t uid,
Jan Sebechlebsky1a80c062022-08-09 15:21:18 +0200473 audio_session_t session,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800474 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100475{
François Gaffiec005e562018-11-06 15:04:49 +0100476 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700477 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100478 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100479 std::string address;
480 //
François Gaffiec005e562018-11-06 15:04:49 +0100481 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
482 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100483 //
François Gaffiec005e562018-11-06 15:04:49 +0100484 // 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 Sebechlebsky28ed9452022-09-15 17:57:42 +0200492 device = policyMixes.getDeviceAndMixForInputSource(attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800493 availableInputDevices,
494 uid,
Jan Sebechlebsky1a80c062022-08-09 15:21:18 +0200495 session,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800496 mix);
François Gaffiec005e562018-11-06 15:04:49 +0100497 if (device != nullptr) {
498 return device;
499 }
500
François Gaffiedc7553f2018-11-02 10:39:57 +0100501 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 Gaffiec005e562018-11-06 15:04:49 +0100511 return availableInputDevices.getDevice(deviceType, String8(address.c_str()), AUDIO_FORMAT_DEFAULT);
François Gaffiedc7553f2018-11-02 10:39:57 +0100512}
513
François Gaffief1e95082018-11-02 13:53:31 +0100514void 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 Gaffiea12de212021-10-22 10:54:33 +0200525bool Engine::setDeviceTypesForProductStrategy(product_strategy_t strategy, uint64_t devices)
François Gaffief1e95082018-11-02 13:53:31 +0100526{
527 if (getProductStrategies().find(strategy) == getProductStrategies().end()) {
Francois Gaffiea12de212021-10-22 10:54:33 +0200528 ALOGE("%s: set device %" PRId64 " on invalid strategy %d", __FUNCTION__, devices, strategy);
François Gaffief1e95082018-11-02 13:53:31 +0100529 return false;
530 }
Francois Gaffiea12de212021-10-22 10:54:33 +0200531 // 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 Gaffief1e95082018-11-02 13:53:31 +0100535 return true;
536}
537
Francois Gaffiea12de212021-10-22 10:54:33 +0200538bool 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 Gaffie20f06f92015-03-24 09:01:14 +0100547template <>
Mikhail Naganove13c6792019-05-14 10:32:51 -0700548EngineInterface *Engine::queryInterface()
François Gaffie20f06f92015-03-24 09:01:14 +0100549{
François Gaffiedc7553f2018-11-02 10:39:57 +0100550 return this;
François Gaffie20f06f92015-03-24 09:01:14 +0100551}
552
553template <>
554AudioPolicyPluginInterface *Engine::queryInterface()
555{
François Gaffiedc7553f2018-11-02 10:39:57 +0100556 return this;
François Gaffie20f06f92015-03-24 09:01:14 +0100557}
558
559} // namespace audio_policy
560} // namespace android