Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 17 | #define LOG_TAG "APM_AudioPolicyManager" |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 18 | |
| 19 | // Need to keep the log statements even in production builds |
| 20 | // to enable VERBOSE logging dynamically. |
| 21 | // You can enable VERBOSE logging as follows: |
| 22 | // adb shell setprop log.tag.APM_AudioPolicyManager V |
| 23 | #define LOG_NDEBUG 0 |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 24 | |
| 25 | //#define VERY_VERBOSE_LOGGING |
| 26 | #ifdef VERY_VERBOSE_LOGGING |
| 27 | #define ALOGVV ALOGV |
| 28 | #else |
| 29 | #define ALOGVV(a...) do { } while(0) |
| 30 | #endif |
| 31 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 32 | #include <algorithm> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 33 | #include <inttypes.h> |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 34 | #include <map> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 35 | #include <math.h> |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 36 | #include <set> |
Atneya Nair | 0f0a803 | 2022-12-12 16:20:12 -0800 | [diff] [blame] | 37 | #include <type_traits> |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 38 | #include <unordered_set> |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 39 | #include <vector> |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 40 | |
| 41 | #include <Serializer.h> |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 42 | #include <android/media/audio/common/AudioPort.h> |
Glenn Kasten | 76a1344 | 2020-07-01 12:10:59 -0700 | [diff] [blame] | 43 | #include <cutils/bitops.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 44 | #include <cutils/properties.h> |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 45 | #include <media/AudioParameter.h> |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 46 | #include <policy.h> |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 47 | #include <private/android_filesystem_config.h> |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 48 | #include <system/audio.h> |
Mikhail Naganov | 27cf37c | 2020-04-14 14:47:01 -0700 | [diff] [blame] | 49 | #include <system/audio_config.h> |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 50 | #include <system/audio_effects/effect_hapticgenerator.h> |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 51 | #include <utils/Log.h> |
| 52 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 53 | #include "AudioPolicyManager.h" |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 54 | #include "TypeConverter.h" |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 55 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 56 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 57 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 58 | using android::media::audio::common::AudioDevice; |
| 59 | using android::media::audio::common::AudioDeviceAddress; |
| 60 | using android::media::audio::common::AudioPortDeviceExt; |
| 61 | using android::media::audio::common::AudioPortExt; |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 62 | using content::AttributionSourceState; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 63 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 64 | //FIXME: workaround for truncated touch sounds |
| 65 | // to be removed when the problem is handled by system UI |
| 66 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 67 | |
| 68 | // Largest difference in dB on earpiece in call between the voice volume and another |
| 69 | // media / notification / system volume. |
| 70 | constexpr float IN_CALL_EARPIECE_HEADROOM_DB = 3.f; |
| 71 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 72 | template <typename T> |
| 73 | bool operator== (const SortedVector<T> &left, const SortedVector<T> &right) |
| 74 | { |
| 75 | if (left.size() != right.size()) { |
| 76 | return false; |
| 77 | } |
| 78 | for (size_t index = 0; index < right.size(); index++) { |
| 79 | if (left[index] != right[index]) { |
| 80 | return false; |
| 81 | } |
| 82 | } |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | template <typename T> |
| 87 | bool operator!= (const SortedVector<T> &left, const SortedVector<T> &right) |
| 88 | { |
| 89 | return !(left == right); |
| 90 | } |
| 91 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 92 | // ---------------------------------------------------------------------------- |
| 93 | // AudioPolicyInterface implementation |
| 94 | // ---------------------------------------------------------------------------- |
| 95 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 96 | status_t AudioPolicyManager::setDeviceConnectionState(audio_policy_dev_state_t state, |
| 97 | const android::media::audio::common::AudioPort& port, audio_format_t encodedFormat) { |
| 98 | status_t status = setDeviceConnectionStateInt(state, port, encodedFormat); |
jiabin | a7b4379 | 2018-02-15 16:04:46 -0800 | [diff] [blame] | 99 | nextAudioPortGeneration(); |
| 100 | return status; |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 103 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
| 104 | audio_policy_dev_state_t state, |
| 105 | const char* device_address, |
| 106 | const char* device_name, |
| 107 | audio_format_t encodedFormat) { |
Atneya Nair | 638a6e4 | 2022-12-18 16:45:15 -0800 | [diff] [blame] | 108 | media::AudioPortFw aidlPort; |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 109 | if (status_t status = deviceToAudioPort(device, device_address, device_name, &aidlPort); |
| 110 | status == OK) { |
| 111 | return setDeviceConnectionState(state, aidlPort.hal, encodedFormat); |
| 112 | } else { |
| 113 | ALOGE("Failed to convert to AudioPort Parcelable: %s", statusToString(status).c_str()); |
| 114 | return status; |
| 115 | } |
| 116 | } |
| 117 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 118 | void AudioPolicyManager::broadcastDeviceConnectionState(const sp<DeviceDescriptor> &device, |
| 119 | audio_policy_dev_state_t state) |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 120 | { |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 121 | audio_port_v7 devicePort; |
| 122 | device->toAudioPort(&devicePort); |
| 123 | if (status_t status = mpClientInterface->setDeviceConnectedState( |
| 124 | &devicePort, state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 125 | status != OK) { |
| 126 | ALOGE("Error %d while setting connected state for device %s", status, |
| 127 | device->getDeviceTypeAddr().toString(false).c_str()); |
| 128 | } |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 131 | status_t AudioPolicyManager::setDeviceConnectionStateInt( |
| 132 | audio_policy_dev_state_t state, const android::media::audio::common::AudioPort& port, |
| 133 | audio_format_t encodedFormat) { |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 134 | if (port.ext.getTag() != AudioPortExt::device) { |
| 135 | return BAD_VALUE; |
| 136 | } |
| 137 | audio_devices_t device_type; |
| 138 | std::string device_address; |
| 139 | if (status_t status = aidl2legacy_AudioDevice_audio_device( |
| 140 | port.ext.get<AudioPortExt::device>().device, &device_type, &device_address); |
| 141 | status != OK) { |
| 142 | return status; |
| 143 | }; |
| 144 | const char* device_name = port.name.c_str(); |
| 145 | // connect/disconnect only 1 device at a time |
| 146 | if (!audio_is_output_device(device_type) && !audio_is_input_device(device_type)) |
| 147 | return BAD_VALUE; |
| 148 | |
| 149 | sp<DeviceDescriptor> device = mHwModules.getDeviceDescriptor( |
| 150 | device_type, device_address.c_str(), device_name, encodedFormat, |
| 151 | state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Mikhail Naganov | ddc5f31 | 2022-06-11 00:47:52 +0000 | [diff] [blame] | 152 | if (device == nullptr) { |
| 153 | return INVALID_OPERATION; |
| 154 | } |
| 155 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 156 | device->setExtraAudioDescriptors(port.extraAudioDescriptors); |
| 157 | } |
| 158 | return setDeviceConnectionStateInt(device, state); |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 159 | } |
| 160 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 161 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t deviceType, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 162 | audio_policy_dev_state_t state, |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 163 | const char* device_address, |
| 164 | const char* device_name, |
| 165 | audio_format_t encodedFormat) { |
Atneya Nair | 638a6e4 | 2022-12-18 16:45:15 -0800 | [diff] [blame] | 166 | media::AudioPortFw aidlPort; |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 167 | if (status_t status = deviceToAudioPort(deviceType, device_address, device_name, &aidlPort); |
| 168 | status == OK) { |
| 169 | return setDeviceConnectionStateInt(state, aidlPort.hal, encodedFormat); |
| 170 | } else { |
| 171 | ALOGE("Failed to convert to AudioPort Parcelable: %s", statusToString(status).c_str()); |
| 172 | return status; |
| 173 | } |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 174 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 175 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 176 | status_t AudioPolicyManager::setDeviceConnectionStateInt(const sp<DeviceDescriptor> &device, |
| 177 | audio_policy_dev_state_t state) |
| 178 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 179 | // handle output devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 180 | if (audio_is_output_device(device->type())) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 181 | SortedVector <audio_io_handle_t> outputs; |
| 182 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 183 | ssize_t index = mAvailableOutputDevices.indexOf(device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 184 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 185 | // save a copy of the opened output descriptors before any output is opened or closed |
| 186 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 187 | mPreviousOutputs = mOutputs; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 188 | |
| 189 | bool wasLeUnicastActive = isLeUnicastActive(); |
| 190 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 191 | switch (state) |
| 192 | { |
| 193 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 194 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 195 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 196 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 197 | return INVALID_OPERATION; |
| 198 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 199 | ALOGV("%s() connecting device %s format %x", |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 200 | __func__, device->toString().c_str(), device->getEncodedFormat()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 201 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 202 | // register new device as available |
Francois Gaffie | 993f390 | 2019-04-10 15:39:27 +0200 | [diff] [blame] | 203 | if (mAvailableOutputDevices.add(device) < 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 204 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 205 | } |
| 206 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 207 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 208 | // parameters on newly connected devices (instead of opening the outputs...) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 209 | broadcastDeviceConnectionState(device, state); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 210 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 211 | if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) { |
| 212 | mAvailableOutputDevices.remove(device); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 213 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 214 | mHwModules.cleanUpForDevice(device); |
| 215 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 216 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 217 | return INVALID_OPERATION; |
| 218 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 219 | |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 220 | // Populate encapsulation information when a output device is connected. |
| 221 | device->setEncapsulationInfoFromHal(mpClientInterface); |
| 222 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 223 | // outputs should never be empty here |
| 224 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 225 | "checkOutputsForDevice() returned no outputs but status OK"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 226 | ALOGV("%s() checkOutputsForDevice() returned %zu outputs", __func__, outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 227 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 228 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 229 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 230 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 231 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 232 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 233 | return INVALID_OPERATION; |
| 234 | } |
| 235 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 236 | ALOGV("%s() disconnecting output device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 237 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 238 | // Send Disconnect to HALs |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 239 | broadcastDeviceConnectionState(device, state); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 240 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 241 | // remove device from available output devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 242 | mAvailableOutputDevices.remove(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 243 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 244 | mOutputs.clearSessionRoutesForDevice(device); |
| 245 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 246 | checkOutputsForDevice(device, state, outputs); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 247 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 248 | // Reset active device codec |
| 249 | device->setEncodedFormat(AUDIO_FORMAT_DEFAULT); |
| 250 | |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 251 | // remove device from mReportedFormatsMap cache |
| 252 | mReportedFormatsMap.erase(device); |
| 253 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 254 | // remove preferred mixer configurations |
| 255 | mPreferredMixerAttrInfos.erase(device->getId()); |
| 256 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 257 | } break; |
| 258 | |
| 259 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 260 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 261 | return BAD_VALUE; |
| 262 | } |
| 263 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 264 | // Propagate device availability to Engine |
| 265 | setEngineDeviceConnectionState(device, state); |
| 266 | |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 267 | // No need to evaluate playback routing when connecting a remote submix |
| 268 | // output device used by a dynamic policy of type recorder as no |
| 269 | // playback use case is affected. |
| 270 | bool doCheckForDeviceAndOutputChanges = true; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 271 | if (device->type() == AUDIO_DEVICE_OUT_REMOTE_SUBMIX && device->address() != "0") { |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 272 | for (audio_io_handle_t output : outputs) { |
| 273 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 274 | sp<AudioPolicyMix> policyMix = desc->mPolicyMix.promote(); |
| 275 | if (policyMix != nullptr |
| 276 | && policyMix->mMixType == MIX_TYPE_RECORDERS |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 277 | && device->address() == policyMix->mDeviceAddress.string()) { |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 278 | doCheckForDeviceAndOutputChanges = false; |
| 279 | break; |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | auto checkCloseOutputs = [&]() { |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 285 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 286 | if (!outputs.isEmpty()) { |
| 287 | for (audio_io_handle_t output : outputs) { |
| 288 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 289 | // close unused outputs after device disconnection or direct outputs that have |
| 290 | // been opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 291 | // "outputs" vector never contains duplicated outputs |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 292 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) |
| 293 | || (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 294 | (desc->mDirectOpenCount == 0)) |
| 295 | || (((desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) && |
| 296 | !isOutputOnlyAvailableRouteToSomeDevice(desc))) { |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 297 | clearAudioSourcesForOutput(output); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 298 | closeOutput(output); |
| 299 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 300 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 301 | // check A2DP again after closing A2DP output to reset mA2dpSuspended if needed |
| 302 | return true; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 303 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 304 | return false; |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 305 | }; |
| 306 | |
| 307 | if (doCheckForDeviceAndOutputChanges) { |
| 308 | checkForDeviceAndOutputChanges(checkCloseOutputs); |
| 309 | } else { |
| 310 | checkCloseOutputs(); |
| 311 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 312 | (void)updateCallRouting(false /*fromCache*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 313 | const DeviceVector msdOutDevices = getMsdAudioOutDevices(); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 314 | const DeviceVector activeMediaDevices = |
| 315 | mEngine->getActiveMediaDevices(mAvailableOutputDevices); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 316 | std::map<audio_io_handle_t, DeviceVector> outputsToReopenWithDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 317 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 318 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jaideep Sharma | 89b5b85 | 2020-11-23 16:41:33 +0530 | [diff] [blame] | 319 | if (desc->isActive() && ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || |
| 320 | (desc != mPrimaryOutput))) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 321 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 322 | // do not force device change on duplicated output because if device is 0, it will |
| 323 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 324 | // a valid device selection on those outputs. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 325 | bool force = (msdOutDevices.isEmpty() || msdOutDevices != desc->devices()) |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 326 | && !desc->isDuplicated() |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 327 | && (!device_distinguishes_on_address(device->type()) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 328 | // always force when disconnecting (a non-duplicated device) |
| 329 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 330 | if (desc->mUsePreferredMixerAttributes && newDevices != desc->devices()) { |
| 331 | // If the device is using preferred mixer attributes, the output need to reopen |
| 332 | // with default configuration when the new selected devices are different from |
| 333 | // current routing devices |
| 334 | outputsToReopenWithDevices.emplace(mOutputs.keyAt(i), newDevices); |
| 335 | continue; |
| 336 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 337 | setOutputDevices(desc, newDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 338 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 339 | if (!desc->isDuplicated() && desc->mProfile->hasDynamicAudioProfile() && |
jiabin | 498d215 | 2021-04-02 00:26:46 +0000 | [diff] [blame] | 340 | !activeMediaDevices.empty() && desc->devices() != activeMediaDevices && |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 341 | desc->supportsDevicesForPlayback(activeMediaDevices)) { |
| 342 | // Reopen the output to query the dynamic profiles when there is not active |
| 343 | // clients or all active clients will be rerouted. Otherwise, set the flag |
| 344 | // `mPendingReopenToQueryProfiles` in the SwOutputDescriptor so that the output |
| 345 | // can be reopened to query dynamic profiles when all clients are inactive. |
| 346 | if (areAllActiveTracksRerouted(desc)) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 347 | outputsToReopenWithDevices.emplace(mOutputs.keyAt(i), activeMediaDevices); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 348 | } else { |
| 349 | desc->mPendingReopenToQueryProfiles = true; |
| 350 | } |
| 351 | } |
| 352 | if (!desc->supportsDevicesForPlayback(activeMediaDevices)) { |
| 353 | // Clear the flag that previously set for re-querying profiles. |
| 354 | desc->mPendingReopenToQueryProfiles = false; |
| 355 | } |
| 356 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 357 | reopenOutputsWithDevices(outputsToReopenWithDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 358 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 359 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 360 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 363 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, 0); |
| 364 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 365 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 366 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 367 | } // end if is output device |
| 368 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 369 | // handle input devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 370 | if (audio_is_input_device(device->type())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 371 | ssize_t index = mAvailableInputDevices.indexOf(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 372 | switch (state) |
| 373 | { |
| 374 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 375 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 376 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 377 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 378 | return INVALID_OPERATION; |
| 379 | } |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 380 | |
| 381 | if (mAvailableInputDevices.add(device) < 0) { |
| 382 | return NO_MEMORY; |
| 383 | } |
| 384 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 385 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 386 | // parameters on newly connected devices (instead of opening the inputs...) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 387 | broadcastDeviceConnectionState(device, state); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 388 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 389 | if (checkInputsForDevice(device, state) != NO_ERROR) { |
| 390 | mAvailableInputDevices.remove(device); |
| 391 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 392 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 393 | |
| 394 | mHwModules.cleanUpForDevice(device); |
| 395 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 396 | return INVALID_OPERATION; |
| 397 | } |
| 398 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 399 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 400 | |
| 401 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 402 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 403 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 404 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 405 | return INVALID_OPERATION; |
| 406 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 407 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 408 | ALOGV("%s() disconnecting input device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 409 | |
| 410 | // Set Disconnect to HALs |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 411 | broadcastDeviceConnectionState(device, state); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 412 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 413 | mAvailableInputDevices.remove(device); |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 414 | |
| 415 | checkInputsForDevice(device, state); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 416 | |
| 417 | // remove device from mReportedFormatsMap cache |
| 418 | mReportedFormatsMap.erase(device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 419 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 420 | |
| 421 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 422 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 423 | return BAD_VALUE; |
| 424 | } |
| 425 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 426 | // Propagate device availability to Engine |
| 427 | setEngineDeviceConnectionState(device, state); |
| 428 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 429 | checkCloseInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 430 | // As the input device list can impact the output device selection, update |
| 431 | // getDeviceForStrategy() cache |
| 432 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 433 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 434 | (void)updateCallRouting(false /*fromCache*/); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 435 | // Reconnect Audio Source |
| 436 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { |
| 437 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); |
| 438 | checkAudioSourceForAttributes(attributes); |
| 439 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 440 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 441 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 444 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 445 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 446 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 447 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 448 | ALOGW("%s() invalid device: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 449 | return BAD_VALUE; |
| 450 | } |
| 451 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 452 | status_t AudioPolicyManager::deviceToAudioPort(audio_devices_t device, const char* device_address, |
| 453 | const char* device_name, |
Atneya Nair | 638a6e4 | 2022-12-18 16:45:15 -0800 | [diff] [blame] | 454 | media::AudioPortFw* aidlPort) { |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 455 | DeviceDescriptorBase devDescr(device, device_address); |
| 456 | devDescr.setName(device_name); |
| 457 | return devDescr.writeToParcelable(aidlPort); |
| 458 | } |
| 459 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 460 | void AudioPolicyManager::setEngineDeviceConnectionState(const sp<DeviceDescriptor> device, |
| 461 | audio_policy_dev_state_t state) { |
| 462 | |
| 463 | // the Engine does not have to know about remote submix devices used by dynamic audio policies |
| 464 | if (audio_is_remote_submix_device(device->type()) && device->address() != "0") { |
| 465 | return; |
| 466 | } |
| 467 | mEngine->setDeviceConnectionState(device, state); |
| 468 | } |
| 469 | |
| 470 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 471 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 472 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 473 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 474 | sp<DeviceDescriptor> devDesc = |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 475 | mHwModules.getDeviceDescriptor(device, device_address, "", AUDIO_FORMAT_DEFAULT, |
| 476 | false /* allowToCreate */, |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 477 | (strlen(device_address) != 0)/*matchAddress*/); |
| 478 | |
| 479 | if (devDesc == 0) { |
François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 480 | ALOGV("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 481 | device, device_address); |
| 482 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 483 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 484 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 485 | DeviceVector *deviceVector; |
| 486 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 487 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 488 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 489 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 490 | deviceVector = &mAvailableInputDevices; |
| 491 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 492 | ALOGW("%s() invalid device type %08x", __func__, device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 493 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 494 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 495 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 496 | return (deviceVector->getDevice( |
| 497 | device, String8(device_address), AUDIO_FORMAT_DEFAULT) != 0) ? |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 498 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 501 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 502 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 503 | const char *device_name, |
| 504 | audio_format_t encodedFormat) |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 505 | { |
| 506 | status_t status; |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 507 | String8 reply; |
| 508 | AudioParameter param; |
| 509 | int isReconfigA2dpSupported = 0; |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 510 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 511 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s encodedFormat: 0x%X", |
| 512 | device, device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 513 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 514 | // connect/disconnect only 1 device at a time |
| 515 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 516 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 517 | // Check if the device is currently connected |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 518 | DeviceVector deviceList = mAvailableOutputDevices.getDevicesFromType(device); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 519 | if (deviceList.empty()) { |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 520 | // Nothing to do: device is not connected |
| 521 | return NO_ERROR; |
| 522 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 523 | sp<DeviceDescriptor> devDesc = deviceList.itemAt(0); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 524 | |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 525 | // For offloaded A2DP, Hw modules may have the capability to |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 526 | // configure codecs. |
| 527 | // Handle two specific cases by sending a set parameter to |
| 528 | // configure A2DP codecs. No need to toggle device state. |
| 529 | // Case 1: A2DP active device switches from primary to primary |
| 530 | // module |
| 531 | // Case 2: A2DP device config changes on primary module. |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 532 | if (audio_is_a2dp_out_device(device) && hasPrimaryOutput()) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 533 | sp<HwModule> module = mHwModules.getModuleForDeviceType(device, encodedFormat); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 534 | audio_module_handle_t primaryHandle = mPrimaryOutput->getModuleHandle(); |
| 535 | if (availablePrimaryOutputDevices().contains(devDesc) && |
| 536 | (module != 0 && module->getHandle() == primaryHandle)) { |
| 537 | reply = mpClientInterface->getParameters( |
| 538 | AUDIO_IO_HANDLE_NONE, |
| 539 | String8(AudioParameter::keyReconfigA2dpSupported)); |
| 540 | AudioParameter repliedParameters(reply); |
| 541 | repliedParameters.getInt( |
| 542 | String8(AudioParameter::keyReconfigA2dpSupported), isReconfigA2dpSupported); |
| 543 | if (isReconfigA2dpSupported) { |
| 544 | const String8 key(AudioParameter::keyReconfigA2dp); |
| 545 | param.add(key, String8("true")); |
| 546 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 547 | devDesc->setEncodedFormat(encodedFormat); |
| 548 | return NO_ERROR; |
| 549 | } |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 550 | } |
| 551 | } |
cnx421 | bd2dcc4 | 2020-07-11 14:58:44 +0800 | [diff] [blame] | 552 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); |
| 553 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 554 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 555 | // mute media strategies and delay device switch by the largest |
| 556 | // This avoid sending the music tail into the earpiece or headset. |
| 557 | setStrategyMute(musicStrategy, true, desc); |
| 558 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, |
| 559 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 560 | nullptr, true /*fromCache*/).types()); |
| 561 | } |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 562 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 563 | // This will force reading again the device configuration |
| 564 | status = setDeviceConnectionState(device, |
| 565 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 566 | device_address, device_name, |
| 567 | devDesc->getEncodedFormat()); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 568 | if (status != NO_ERROR) { |
| 569 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 570 | status); |
| 571 | return status; |
| 572 | } |
| 573 | |
| 574 | status = setDeviceConnectionState(device, |
| 575 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 576 | device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 577 | if (status != NO_ERROR) { |
| 578 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 579 | status); |
| 580 | return status; |
| 581 | } |
| 582 | |
| 583 | return NO_ERROR; |
| 584 | } |
| 585 | |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 586 | status_t AudioPolicyManager::getHwOffloadFormatsSupportedForBluetoothMedia( |
| 587 | audio_devices_t device, std::vector<audio_format_t> *formats) |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 588 | { |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 589 | ALOGV("getHwOffloadFormatsSupportedForBluetoothMedia()"); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 590 | status_t status = NO_ERROR; |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 591 | std::unordered_set<audio_format_t> formatSet; |
| 592 | sp<HwModule> primaryModule = |
| 593 | mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY); |
Aniket Kumar Lata | fedb598 | 2019-07-03 11:20:36 -0700 | [diff] [blame] | 594 | if (primaryModule == nullptr) { |
| 595 | ALOGE("%s() unable to get primary module", __func__); |
| 596 | return NO_INIT; |
| 597 | } |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 598 | |
| 599 | DeviceTypeSet audioDeviceSet; |
| 600 | |
| 601 | switch(device) { |
| 602 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: |
| 603 | audioDeviceSet = getAudioDeviceOutAllA2dpSet(); |
| 604 | break; |
| 605 | case AUDIO_DEVICE_OUT_BLE_HEADSET: |
Patty Huang | 36028df | 2022-07-06 00:14:12 +0800 | [diff] [blame] | 606 | audioDeviceSet = getAudioDeviceOutLeAudioUnicastSet(); |
| 607 | break; |
| 608 | case AUDIO_DEVICE_OUT_BLE_BROADCAST: |
| 609 | audioDeviceSet = getAudioDeviceOutLeAudioBroadcastSet(); |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 610 | break; |
| 611 | default: |
| 612 | ALOGE("%s() device type 0x%08x not supported", __func__, device); |
| 613 | return BAD_VALUE; |
| 614 | } |
| 615 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 616 | DeviceVector declaredDevices = primaryModule->getDeclaredDevices().getDevicesFromTypes( |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 617 | audioDeviceSet); |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 618 | for (const auto& device : declaredDevices) { |
| 619 | formatSet.insert(device->encodedFormats().begin(), device->encodedFormats().end()); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 620 | } |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 621 | formats->assign(formatSet.begin(), formatSet.end()); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 622 | return status; |
| 623 | } |
| 624 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 625 | DeviceVector AudioPolicyManager::selectBestRxSinkDevicesForCall(bool fromCache) |
| 626 | { |
| 627 | DeviceVector rxSinkdevices{}; |
| 628 | rxSinkdevices = mEngine->getOutputDevicesForAttributes( |
| 629 | attributes_initializer(AUDIO_USAGE_VOICE_COMMUNICATION), nullptr, fromCache); |
| 630 | if (!rxSinkdevices.isEmpty() && mAvailableOutputDevices.contains(rxSinkdevices.itemAt(0))) { |
| 631 | auto rxSinkDevice = rxSinkdevices.itemAt(0); |
| 632 | auto telephonyRxModule = mHwModules.getModuleForDeviceType( |
| 633 | AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); |
| 634 | // retrieve Rx Source device descriptor |
| 635 | sp<DeviceDescriptor> rxSourceDevice = mAvailableInputDevices.getDevice( |
| 636 | AUDIO_DEVICE_IN_TELEPHONY_RX, String8(), AUDIO_FORMAT_DEFAULT); |
| 637 | |
| 638 | // RX Telephony and Rx sink devices are declared by Primary Audio HAL |
| 639 | if (isPrimaryModule(telephonyRxModule) && (telephonyRxModule->getHalVersionMajor() >= 3) && |
| 640 | telephonyRxModule->supportsPatch(rxSourceDevice, rxSinkDevice)) { |
| 641 | ALOGW("%s() device %s using HW Bridge", __func__, rxSinkDevice->toString().c_str()); |
| 642 | return DeviceVector(rxSinkDevice); |
| 643 | } |
| 644 | } |
| 645 | // Note that despite the fact that getNewOutputDevices() is called on the primary output, |
| 646 | // the device returned is not necessarily reachable via this output |
| 647 | // (filter later by setOutputDevices()) |
| 648 | return getNewOutputDevices(mPrimaryOutput, fromCache); |
| 649 | } |
| 650 | |
| 651 | status_t AudioPolicyManager::updateCallRouting(bool fromCache, uint32_t delayMs, uint32_t *waitMs) |
| 652 | { |
| 653 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
| 654 | DeviceVector rxDevices = selectBestRxSinkDevicesForCall(fromCache); |
| 655 | return updateCallRoutingInternal(rxDevices, delayMs, waitMs); |
| 656 | } |
| 657 | return INVALID_OPERATION; |
| 658 | } |
| 659 | |
| 660 | status_t AudioPolicyManager::updateCallRoutingInternal( |
| 661 | const DeviceVector &rxDevices, uint32_t delayMs, uint32_t *waitMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 662 | { |
| 663 | bool createTxPatch = false; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 664 | bool createRxPatch = false; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 665 | uint32_t muteWaitMs = 0; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 666 | if(!hasPrimaryOutput() || |
| 667 | mPrimaryOutput->devices().onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_STUB)) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 668 | return INVALID_OPERATION; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 669 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 670 | ALOG_ASSERT(!rxDevices.isEmpty(), "%s() no selected output device", __func__); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 671 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 672 | audio_attributes_t attr = { .source = AUDIO_SOURCE_VOICE_COMMUNICATION }; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 673 | auto txSourceDevice = mEngine->getInputDeviceForAttributes(attr); |
Eric Laurent | cedd5b5 | 2023-03-22 00:03:31 +0000 | [diff] [blame] | 674 | if (txSourceDevice == nullptr) { |
| 675 | ALOGE("%s() selected input device not available", __func__); |
| 676 | return INVALID_OPERATION; |
| 677 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 678 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 679 | ALOGV("%s device rxDevice %s txDevice %s", __func__, |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 680 | rxDevices.itemAt(0)->toString().c_str(), txSourceDevice->toString().c_str()); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 681 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 682 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 683 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 684 | |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 685 | auto telephonyRxModule = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 686 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 687 | auto telephonyTxModule = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 688 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_OUT_TELEPHONY_TX, AUDIO_FORMAT_DEFAULT); |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 689 | // retrieve Rx Source and Tx Sink device descriptors |
| 690 | sp<DeviceDescriptor> rxSourceDevice = |
| 691 | mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX, |
| 692 | String8(), |
| 693 | AUDIO_FORMAT_DEFAULT); |
| 694 | sp<DeviceDescriptor> txSinkDevice = |
| 695 | mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, |
| 696 | String8(), |
| 697 | AUDIO_FORMAT_DEFAULT); |
| 698 | |
| 699 | // RX and TX Telephony device are declared by Primary Audio HAL |
| 700 | if (isPrimaryModule(telephonyRxModule) && isPrimaryModule(telephonyTxModule) && |
| 701 | (telephonyRxModule->getHalVersionMajor() >= 3)) { |
| 702 | if (rxSourceDevice == 0 || txSinkDevice == 0) { |
| 703 | // RX / TX Telephony device(s) is(are) not currently available |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 704 | ALOGE("%s() no telephony Tx and/or RX device", __func__); |
| 705 | return INVALID_OPERATION; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 706 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 707 | // createAudioPatchInternal now supports both HW / SW bridging |
| 708 | createRxPatch = true; |
| 709 | createTxPatch = true; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 710 | } else { |
| 711 | // If the RX device is on the primary HW module, then use legacy routing method for |
| 712 | // voice calls via setOutputDevice() on primary output. |
| 713 | // Otherwise, create two audio patches for TX and RX path. |
| 714 | createRxPatch = !(availablePrimaryOutputDevices().contains(rxDevices.itemAt(0))) && |
| 715 | (rxSourceDevice != 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 716 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 717 | // of it due to legacy implementation. If not, create a patch. |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 718 | createTxPatch = !(availablePrimaryModuleInputDevices().contains(txSourceDevice)) && |
| 719 | (txSinkDevice != 0); |
| 720 | } |
| 721 | // Use legacy routing method for voice calls via setOutputDevice() on primary output. |
| 722 | // Otherwise, create two audio patches for TX and RX path. |
| 723 | if (!createRxPatch) { |
| 724 | muteWaitMs = setOutputDevices(mPrimaryOutput, rxDevices, true, delayMs); |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 725 | } else { // create RX path audio patch |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 726 | connectTelephonyRxAudioSource(); |
juyuchen | 2224c5a | 2019-01-21 12:00:58 +0800 | [diff] [blame] | 727 | // If the TX device is on the primary HW module but RX device is |
| 728 | // on other HW module, SinkMetaData of telephony input should handle it |
| 729 | // assuming the device uses audio HAL V5.0 and above |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 730 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 731 | if (createTxPatch) { // create TX path audio patch |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 732 | // terminate active capture if on the same HW module as the call TX source device |
| 733 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 734 | // call TX device but this information is not in the audio patch and logic here must be |
| 735 | // symmetric to the one in startInput() |
| 736 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
| 737 | if (activeDesc->hasSameHwModuleAs(txSourceDevice)) { |
| 738 | closeActiveClients(activeDesc); |
| 739 | } |
| 740 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 741 | connectTelephonyTxAudioSource(txSourceDevice, txSinkDevice, delayMs); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 742 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 743 | if (waitMs != nullptr) { |
| 744 | *waitMs = muteWaitMs; |
| 745 | } |
| 746 | return NO_ERROR; |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 747 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 748 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 749 | bool AudioPolicyManager::isDeviceOfModule( |
| 750 | const sp<DeviceDescriptor>& devDesc, const char *moduleId) const { |
| 751 | sp<HwModule> module = mHwModules.getModuleFromName(moduleId); |
| 752 | if (module != 0) { |
| 753 | return mAvailableOutputDevices.getDevicesFromHwModule(module->getHandle()) |
| 754 | .indexOf(devDesc) != NAME_NOT_FOUND |
| 755 | || mAvailableInputDevices.getDevicesFromHwModule(module->getHandle()) |
| 756 | .indexOf(devDesc) != NAME_NOT_FOUND; |
| 757 | } |
| 758 | return false; |
| 759 | } |
| 760 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 761 | void AudioPolicyManager::connectTelephonyRxAudioSource() |
| 762 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 763 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 764 | const struct audio_port_config source = { |
| 765 | .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE, |
| 766 | .ext.device.type = AUDIO_DEVICE_IN_TELEPHONY_RX, .ext.device.address = "" |
| 767 | }; |
| 768 | const auto aa = mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 769 | mCallRxSourceClient = startAudioSourceInternal(&source, &aa, 0/*uid*/); |
| 770 | ALOGE_IF(mCallRxSourceClient == nullptr, |
| 771 | "%s failed to start Telephony Rx AudioSource", __func__); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 772 | } |
| 773 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 774 | void AudioPolicyManager::disconnectTelephonyAudioSource(sp<SourceClientDescriptor> &clientDesc) |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 775 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 776 | if (clientDesc == nullptr) { |
| 777 | return; |
| 778 | } |
| 779 | ALOGW_IF(stopAudioSource(clientDesc->portId()) != NO_ERROR, |
| 780 | "%s error stopping audio source", __func__); |
| 781 | clientDesc.clear(); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | void AudioPolicyManager::connectTelephonyTxAudioSource( |
| 785 | const sp<DeviceDescriptor> &srcDevice, const sp<DeviceDescriptor> &sinkDevice, |
| 786 | uint32_t delayMs) |
| 787 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 788 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 789 | if (srcDevice == nullptr || sinkDevice == nullptr) { |
| 790 | ALOGW("%s could not create patch, invalid sink and/or source device(s)", __func__); |
| 791 | return; |
| 792 | } |
| 793 | PatchBuilder patchBuilder; |
| 794 | patchBuilder.addSource(srcDevice).addSink(sinkDevice); |
| 795 | ALOGV("%s between source %s and sink %s", __func__, |
| 796 | srcDevice->toString().c_str(), sinkDevice->toString().c_str()); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 797 | auto callTxSourceClientPortId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 798 | const auto aa = mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL); |
| 799 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 800 | struct audio_port_config source = {}; |
| 801 | srcDevice->toAudioPortConfig(&source); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 802 | mCallTxSourceClient = new InternalSourceClientDescriptor( |
| 803 | callTxSourceClientPortId, mUidCached, aa, source, srcDevice, sinkDevice, |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 804 | mCommunnicationStrategy, toVolumeSource(aa)); |
| 805 | audio_patch_handle_t patchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 806 | status_t status = connectAudioSourceToSink( |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 807 | mCallTxSourceClient, sinkDevice, patchBuilder.patch(), patchHandle, mUidCached, |
| 808 | delayMs); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 809 | ALOGE_IF(status != NO_ERROR, "%s() error %d creating TX audio patch", __func__, status); |
| 810 | if (status == NO_ERROR) { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 811 | mAudioSources.add(callTxSourceClientPortId, mCallTxSourceClient); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 812 | } |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 813 | } |
| 814 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 815 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 816 | { |
| 817 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 818 | // store previous phone state for management of sonification strategy below |
| 819 | int oldState = mEngine->getPhoneState(); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 820 | bool wasLeUnicastActive = isLeUnicastActive(); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 821 | |
| 822 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 823 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 824 | return; |
| 825 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 826 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 827 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 828 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 829 | // force reevaluating accessibility routing when call stops |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 830 | invalidateStreams({AUDIO_STREAM_ACCESSIBILITY}); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 831 | } |
| 832 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 833 | /** |
| 834 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 835 | * routing command. |
| 836 | */ |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 837 | bool force = ((isStateInCall(oldState) != isStateInCall(state)) |
| 838 | || (isStateInCall(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 839 | |
| 840 | // check for device and output changes triggered by new phone state |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 841 | checkForDeviceAndOutputChanges(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 842 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 843 | int delayMs = 0; |
| 844 | if (isStateInCall(state)) { |
| 845 | nsecs_t sysTime = systemTime(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 846 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); |
| 847 | auto sonificationStrategy = streamToStrategy(AUDIO_STREAM_ALARM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 848 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 849 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 850 | // mute media and sonification strategies and delay device switch by the largest |
| 851 | // latency of any output where either strategy is active. |
| 852 | // This avoid sending the ring tone or music tail into the earpiece or headset. |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 853 | if ((desc->isStrategyActive(musicStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, sysTime) || |
| 854 | desc->isStrategyActive(sonificationStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, |
| 855 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 856 | (delayMs < (int)desc->latency()*2)) { |
| 857 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 858 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 859 | setStrategyMute(musicStrategy, true, desc); |
| 860 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, |
| 861 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 862 | nullptr, true /*fromCache*/).types()); |
| 863 | setStrategyMute(sonificationStrategy, true, desc); |
| 864 | setStrategyMute(sonificationStrategy, false, desc, MUTE_TIME_MS, |
| 865 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_ALARM), |
| 866 | nullptr, true /*fromCache*/).types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 870 | if (hasPrimaryOutput()) { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 871 | if (state == AUDIO_MODE_IN_CALL) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 872 | (void)updateCallRouting(false /*fromCache*/, delayMs); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 873 | } else { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 874 | DeviceVector rxDevices = getNewOutputDevices(mPrimaryOutput, false /*fromCache*/); |
| 875 | // force routing command to audio hardware when ending call |
| 876 | // even if no device change is needed |
| 877 | if (isStateInCall(oldState) && rxDevices.isEmpty()) { |
| 878 | rxDevices = mPrimaryOutput->devices(); |
| 879 | } |
| 880 | if (oldState == AUDIO_MODE_IN_CALL) { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 881 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 882 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 883 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 884 | setOutputDevices(mPrimaryOutput, rxDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 885 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 886 | } |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 887 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 888 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 889 | // reevaluate routing on all outputs in case tracks have been started during the call |
| 890 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 891 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 892 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 893 | if (state != AUDIO_MODE_IN_CALL || (desc != mPrimaryOutput && !isTelephonyRxOrTx(desc))) { |
| 894 | bool forceRouting = !newDevices.isEmpty(); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 895 | if (desc->mUsePreferredMixerAttributes && newDevices != desc->devices()) { |
| 896 | // If the device is using preferred mixer attributes, the output need to reopen |
| 897 | // with default configuration when the new selected devices are different from |
| 898 | // current routing devices. |
| 899 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices); |
| 900 | continue; |
| 901 | } |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 902 | setOutputDevices(desc, newDevices, forceRouting, 0 /*delayMs*/, nullptr, |
| 903 | true /*requiresMuteCheck*/, !forceRouting /*requiresVolumeCheck*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 904 | } |
| 905 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 906 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 907 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 908 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, delayMs); |
| 909 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 910 | if (isStateInCall(state)) { |
| 911 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 912 | // force reevaluating accessibility routing when call starts |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 913 | invalidateStreams({AUDIO_STREAM_ACCESSIBILITY}); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 917 | mLimitRingtoneVolume = (state == AUDIO_MODE_RINGTONE && |
| 918 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 921 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 922 | return mEngine->getPhoneState(); |
| 923 | } |
| 924 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 925 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 926 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 927 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 928 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 929 | if (config == mEngine->getForceUse(usage)) { |
| 930 | return; |
| 931 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 932 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 933 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 934 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 935 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 936 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 937 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 938 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 939 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 940 | |
| 941 | // check for device and output changes triggered by new force usage |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 942 | checkForDeviceAndOutputChanges(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 943 | |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 944 | // force client reconnection to reevaluate flag AUDIO_FLAG_AUDIBILITY_ENFORCED |
| 945 | if (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 946 | invalidateStreams({AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE}); |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 949 | //FIXME: workaround for truncated touch sounds |
| 950 | // to be removed when the problem is handled by system UI |
| 951 | uint32_t delayMs = 0; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 952 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 953 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 954 | } |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 955 | |
| 956 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 957 | updateInputRouting(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 960 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 961 | { |
| 962 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 963 | } |
| 964 | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 965 | // Find an MSD output profile compatible with the parameters passed. |
| 966 | // When "directOnly" is set, restrict search to profiles for direct outputs. |
| 967 | sp<IOProfile> AudioPolicyManager::getMsdProfileForOutput( |
| 968 | const DeviceVector& devices, |
| 969 | uint32_t samplingRate, |
| 970 | audio_format_t format, |
| 971 | audio_channel_mask_t channelMask, |
| 972 | audio_output_flags_t flags, |
| 973 | bool directOnly) |
| 974 | { |
| 975 | flags = getRelevantFlags(flags, directOnly); |
| 976 | |
| 977 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 978 | if (msdModule != nullptr) { |
| 979 | // for the msd module check if there are patches to the output devices |
| 980 | if (msdHasPatchesToAllDevices(devices.toTypeAddrVector())) { |
| 981 | HwModuleCollection modules; |
| 982 | modules.add(msdModule); |
| 983 | return searchCompatibleProfileHwModules( |
| 984 | modules, getMsdAudioOutDevices(), samplingRate, format, channelMask, |
| 985 | flags, directOnly); |
| 986 | } |
| 987 | } |
| 988 | return nullptr; |
| 989 | } |
| 990 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 991 | // Find an output profile compatible with the parameters passed. When "directOnly" is set, restrict |
| 992 | // search to profiles for direct outputs. |
| 993 | sp<IOProfile> AudioPolicyManager::getProfileForOutput( |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 994 | const DeviceVector& devices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 995 | uint32_t samplingRate, |
| 996 | audio_format_t format, |
| 997 | audio_channel_mask_t channelMask, |
| 998 | audio_output_flags_t flags, |
| 999 | bool directOnly) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1000 | { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1001 | flags = getRelevantFlags(flags, directOnly); |
| 1002 | |
| 1003 | return searchCompatibleProfileHwModules( |
| 1004 | mHwModules, devices, samplingRate, format, channelMask, flags, directOnly); |
| 1005 | } |
| 1006 | |
| 1007 | audio_output_flags_t AudioPolicyManager::getRelevantFlags ( |
| 1008 | audio_output_flags_t flags, bool directOnly) { |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1009 | if (directOnly) { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1010 | // only retain flags that will drive the direct output profile selection |
| 1011 | // if explicitly requested |
| 1012 | static const uint32_t kRelevantFlags = |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1013 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1014 | AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ); |
| 1015 | flags = (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1016 | } |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1017 | return flags; |
| 1018 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1019 | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1020 | sp<IOProfile> AudioPolicyManager::searchCompatibleProfileHwModules ( |
| 1021 | const HwModuleCollection& hwModules, |
| 1022 | const DeviceVector& devices, |
| 1023 | uint32_t samplingRate, |
| 1024 | audio_format_t format, |
| 1025 | audio_channel_mask_t channelMask, |
| 1026 | audio_output_flags_t flags, |
| 1027 | bool directOnly) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1028 | sp<IOProfile> profile; |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1029 | for (const auto& hwModule : hwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 1030 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1031 | if (!curProfile->isCompatibleProfile(devices, |
| 1032 | samplingRate, NULL /*updatedSamplingRate*/, |
| 1033 | format, NULL /*updatedFormat*/, |
| 1034 | channelMask, NULL /*updatedChannelMask*/, |
| 1035 | flags)) { |
| 1036 | continue; |
| 1037 | } |
| 1038 | // reject profiles not corresponding to a device currently available |
| 1039 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
| 1040 | continue; |
| 1041 | } |
| 1042 | // reject profiles if connected device does not support codec |
| 1043 | if (!curProfile->devicesSupportEncodedFormats(devices.types())) { |
| 1044 | continue; |
| 1045 | } |
| 1046 | if (!directOnly) { |
| 1047 | return curProfile; |
| 1048 | } |
| 1049 | |
| 1050 | // when searching for direct outputs, if several profiles are compatible, give priority |
| 1051 | // to one with offload capability |
Patty Huang | 36028df | 2022-07-06 00:14:12 +0800 | [diff] [blame] | 1052 | if (profile != 0 && |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1053 | ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1054 | continue; |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1055 | } |
| 1056 | profile = curProfile; |
| 1057 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1058 | break; |
| 1059 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1060 | } |
| 1061 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1062 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1065 | sp<IOProfile> AudioPolicyManager::getSpatializerOutputProfile( |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 1066 | const audio_config_t *config __unused, const AudioDeviceTypeAddrVector &devices) const |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1067 | { |
| 1068 | for (const auto& hwModule : mHwModules) { |
| 1069 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 1070 | if (curProfile->getFlags() != AUDIO_OUTPUT_FLAG_SPATIALIZER) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1071 | continue; |
| 1072 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1073 | if (!devices.empty()) { |
Eric Laurent | 0c8f7cc | 2022-06-24 14:32:36 +0200 | [diff] [blame] | 1074 | // reject profiles not corresponding to a device currently available |
| 1075 | DeviceVector supportedDevices = curProfile->getSupportedDevices(); |
| 1076 | if (!mAvailableOutputDevices.containsAtLeastOne(supportedDevices)) { |
| 1077 | continue; |
| 1078 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1079 | if (supportedDevices.getDevicesFromDeviceTypeAddrVec(devices).size() |
| 1080 | != devices.size()) { |
| 1081 | continue; |
| 1082 | } |
| 1083 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1084 | ALOGV("%s found profile %s", __func__, curProfile->getName().c_str()); |
| 1085 | return curProfile; |
| 1086 | } |
| 1087 | } |
| 1088 | return nullptr; |
| 1089 | } |
| 1090 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1091 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1092 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1093 | DeviceVector devices = mEngine->getOutputDevicesForStream(stream, false /*fromCache*/); |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1094 | |
| 1095 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). |
| 1096 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, |
| 1097 | // format, flags, etc. This may result in some discrepancy for functions that utilize |
| 1098 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() |
| 1099 | // and AudioSystem::getOutputSamplingRate(). |
| 1100 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1101 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1102 | const audio_io_handle_t output = selectOutput(outputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1103 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1104 | ALOGV("getOutput() stream %d selected devices %s, output %d", stream, |
| 1105 | devices.toString().c_str(), output); |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1106 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1109 | status_t AudioPolicyManager::getAudioAttributes(audio_attributes_t *dstAttr, |
| 1110 | const audio_attributes_t *srcAttr, |
| 1111 | audio_stream_type_t srcStream) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1112 | { |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1113 | if (srcAttr != NULL) { |
| 1114 | if (!isValidAttributes(srcAttr)) { |
| 1115 | ALOGE("%s invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 1116 | __func__, |
| 1117 | srcAttr->usage, srcAttr->content_type, srcAttr->flags, |
| 1118 | srcAttr->tags); |
| 1119 | return BAD_VALUE; |
| 1120 | } |
| 1121 | *dstAttr = *srcAttr; |
| 1122 | } else { |
| 1123 | if (srcStream < AUDIO_STREAM_MIN || srcStream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 1124 | ALOGE("%s: invalid stream type", __func__); |
| 1125 | return BAD_VALUE; |
| 1126 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1127 | *dstAttr = mEngine->getAttributesForStreamType(srcStream); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1128 | } |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 1129 | |
| 1130 | // Only honor audibility enforced when required. The client will be |
| 1131 | // forced to reconnect if the forced usage changes. |
| 1132 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1133 | dstAttr->flags = static_cast<audio_flags_mask_t>( |
| 1134 | dstAttr->flags & ~AUDIO_FLAG_AUDIBILITY_ENFORCED); |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1137 | return NO_ERROR; |
| 1138 | } |
| 1139 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1140 | status_t AudioPolicyManager::getOutputForAttrInt( |
| 1141 | audio_attributes_t *resultAttr, |
| 1142 | audio_io_handle_t *output, |
| 1143 | audio_session_t session, |
| 1144 | const audio_attributes_t *attr, |
| 1145 | audio_stream_type_t *stream, |
| 1146 | uid_t uid, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1147 | audio_config_t *config, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1148 | audio_output_flags_t *flags, |
| 1149 | audio_port_handle_t *selectedDeviceId, |
| 1150 | bool *isRequestedDeviceForExclusiveUse, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1151 | std::vector<sp<AudioPolicyMix>> *secondaryMixes, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1152 | output_type_t *outputType, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1153 | bool *isSpatialized, |
| 1154 | bool *isBitPerfect) |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1155 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1156 | DeviceVector outputDevices; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1157 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1158 | DeviceVector msdDevices = getMsdAudioOutDevices(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1159 | const sp<DeviceDescriptor> requestedDevice = |
| 1160 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 1161 | |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1162 | *outputType = API_OUTPUT_INVALID; |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1163 | *isSpatialized = false; |
| 1164 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1165 | status_t status = getAudioAttributes(resultAttr, attr, *stream); |
| 1166 | if (status != NO_ERROR) { |
| 1167 | return status; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1168 | } |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1169 | if (auto it = mAllowedCapturePolicies.find(uid); it != end(mAllowedCapturePolicies)) { |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1170 | resultAttr->flags = static_cast<audio_flags_mask_t>(resultAttr->flags | it->second); |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1171 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1172 | *stream = mEngine->getStreamTypeForAttributes(*resultAttr); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1173 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1174 | ALOGV("%s() attributes=%s stream=%s session %d selectedDeviceId %d", __func__, |
| 1175 | toString(*resultAttr).c_str(), toString(*stream).c_str(), session, requestedPortId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1176 | |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 1177 | bool usePrimaryOutputFromPolicyMixes = false; |
| 1178 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1179 | // The primary output is the explicit routing (eg. setPreferredDevice) if specified, |
| 1180 | // otherwise, fallback to the dynamic policies, if none match, query the engine. |
| 1181 | // Secondary outputs are always found by dynamic policies as the engine do not support them |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1182 | sp<AudioPolicyMix> primaryMix; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1183 | const audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
| 1184 | .channel_mask = config->channel_mask, |
| 1185 | .format = config->format, |
| 1186 | }; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1187 | status = mPolicyMixes.getOutputForAttr(*resultAttr, clientConfig, uid, session, *flags, |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 1188 | mAvailableOutputDevices, requestedDevice, primaryMix, |
| 1189 | secondaryMixes, usePrimaryOutputFromPolicyMixes); |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1190 | if (status != OK) { |
| 1191 | return status; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1192 | } |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1193 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1194 | // FIXME: in case of RENDER policy, the output capabilities should be checked |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1195 | if ((secondaryMixes != nullptr && !secondaryMixes->empty()) |
| 1196 | && !audio_is_linear_pcm(config->format)) { |
| 1197 | ALOGD("%s: rejecting request as secondary mixes only support pcm", __func__); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1198 | return BAD_VALUE; |
| 1199 | } |
| 1200 | if (usePrimaryOutputFromPolicyMixes) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1201 | sp<DeviceDescriptor> deviceDesc = |
| 1202 | mAvailableOutputDevices.getDevice(primaryMix->mDeviceType, |
| 1203 | primaryMix->mDeviceAddress, |
| 1204 | AUDIO_FORMAT_DEFAULT); |
| 1205 | sp<SwAudioOutputDescriptor> policyDesc = primaryMix->getOutput(); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1206 | bool tryDirectForFlags = policyDesc == nullptr || |
| 1207 | (policyDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT); |
| 1208 | // if a direct output can be opened to deliver the track's multi-channel content to the |
| 1209 | // output rather than being downmixed by the primary output, then use this direct |
| 1210 | // output by by-passing the primary mix if possible, otherwise fall-through to primary |
| 1211 | // mix. |
| 1212 | bool tryDirectForChannelMask = policyDesc != nullptr |
| 1213 | && (audio_channel_count_from_out_mask(policyDesc->getConfig().channel_mask) < |
| 1214 | audio_channel_count_from_out_mask(config->channel_mask)); |
| 1215 | if (deviceDesc != nullptr && (tryDirectForFlags || tryDirectForChannelMask)) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1216 | audio_io_handle_t newOutput; |
| 1217 | status = openDirectOutput( |
| 1218 | *stream, session, config, |
| 1219 | (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT), |
| 1220 | DeviceVector(deviceDesc), &newOutput); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1221 | if (status == NO_ERROR) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1222 | policyDesc = mOutputs.valueFor(newOutput); |
| 1223 | primaryMix->setOutput(policyDesc); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1224 | } else if (tryDirectForFlags) { |
| 1225 | policyDesc = nullptr; |
| 1226 | } // otherwise use primary if available. |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1227 | } |
| 1228 | if (policyDesc != nullptr) { |
| 1229 | policyDesc->mPolicyMix = primaryMix; |
| 1230 | *output = policyDesc->mIoHandle; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1231 | *selectedDeviceId = deviceDesc != 0 ? deviceDesc->getId() : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1232 | |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1233 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 1234 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 1235 | *outputType = API_OUT_MIX_PLAYBACK; |
| 1236 | } else { |
| 1237 | *outputType = API_OUTPUT_LEGACY; |
| 1238 | } |
| 1239 | return NO_ERROR; |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1240 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1241 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1242 | // Virtual sources must always be dynamicaly or explicitly routed |
| 1243 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 1244 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 1245 | return BAD_VALUE; |
| 1246 | } |
| 1247 | // explicit routing managed by getDeviceForStrategy in APM is now handled by engine |
| 1248 | // in order to let the choice of the order to future vendor engine |
| 1249 | outputDevices = mEngine->getOutputDevicesForAttributes(*resultAttr, requestedDevice, false); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 1250 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1251 | if ((resultAttr->flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1252 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1255 | // Set incall music only if device was explicitly set, and fallback to the device which is |
| 1256 | // chosen by the engine if not. |
| 1257 | // FIXME: provide a more generic approach which is not device specific and move this back |
| 1258 | // to getOutputForDevice. |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1259 | // TODO: Remove check of AUDIO_STREAM_MUSIC once migration is completed on the app side. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 1260 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX) && |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1261 | (*stream == AUDIO_STREAM_MUSIC || resultAttr->usage == AUDIO_USAGE_VOICE_COMMUNICATION) && |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1262 | audio_is_linear_pcm(config->format) && |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 1263 | isCallAudioAccessible()) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1264 | if (requestedPortId != AUDIO_PORT_HANDLE_NONE) { |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1265 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1266 | *isRequestedDeviceForExclusiveUse = true; |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1270 | ALOGV("%s() device %s, sampling rate %d, format %#x, channel mask %#x, flags %#x stream %s", |
| 1271 | __func__, outputDevices.toString().c_str(), config->sample_rate, config->format, |
| 1272 | config->channel_mask, *flags, toString(*stream).c_str()); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1273 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1274 | *output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1275 | if (!msdDevices.isEmpty()) { |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1276 | *output = getOutputForDevices(msdDevices, session, resultAttr, config, flags, isSpatialized); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1277 | if (*output != AUDIO_IO_HANDLE_NONE && setMsdOutputPatches(&outputDevices) == NO_ERROR) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1278 | ALOGV("%s() Using MSD devices %s instead of devices %s", |
| 1279 | __func__, msdDevices.toString().c_str(), outputDevices.toString().c_str()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1280 | } else { |
| 1281 | *output = AUDIO_IO_HANDLE_NONE; |
| 1282 | } |
| 1283 | } |
| 1284 | if (*output == AUDIO_IO_HANDLE_NONE) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1285 | sp<PreferredMixerAttributesInfo> info = nullptr; |
| 1286 | if (outputDevices.size() == 1) { |
| 1287 | info = getPreferredMixerAttributesInfo( |
| 1288 | outputDevices.itemAt(0)->getId(), |
| 1289 | mEngine->getProductStrategyForAttributes(*resultAttr)); |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 1290 | // Only use preferred mixer if the uid matches or the preferred mixer is bit-perfect |
| 1291 | // and it is currently active. |
| 1292 | if (info != nullptr && info->getUid() != uid && |
| 1293 | ((info->getFlags() & AUDIO_OUTPUT_FLAG_BIT_PERFECT) == AUDIO_OUTPUT_FLAG_NONE || |
| 1294 | info->getActiveClientCount() == 0)) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1295 | info = nullptr; |
| 1296 | } |
| 1297 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1298 | *output = getOutputForDevices(outputDevices, session, resultAttr, config, |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1299 | flags, isSpatialized, info, resultAttr->flags & AUDIO_FLAG_MUTE_HAPTIC); |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 1300 | // The client will be active if the client is currently preferred mixer owner and the |
| 1301 | // requested configuration matches the preferred mixer configuration. |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1302 | *isBitPerfect = (info != nullptr |
| 1303 | && (info->getFlags() & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != AUDIO_OUTPUT_FLAG_NONE |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 1304 | && info->getUid() == uid |
| 1305 | && *output != AUDIO_IO_HANDLE_NONE |
| 1306 | // When bit-perfect output is selected for the preferred mixer attributes owner, |
| 1307 | // only need to consider the config matches. |
| 1308 | && mOutputs.valueFor(*output)->isConfigurationMatched( |
| 1309 | clientConfig, AUDIO_OUTPUT_FLAG_NONE)); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1310 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1311 | if (*output == AUDIO_IO_HANDLE_NONE) { |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1312 | AudioProfileVector profiles; |
| 1313 | status_t ret = getProfilesForDevices(outputDevices, profiles, *flags, false /*isInput*/); |
| 1314 | if (ret == NO_ERROR && !profiles.empty()) { |
| 1315 | config->channel_mask = profiles[0]->getChannels().empty() ? config->channel_mask |
| 1316 | : *profiles[0]->getChannels().begin(); |
| 1317 | config->sample_rate = profiles[0]->getSampleRates().empty() ? config->sample_rate |
| 1318 | : *profiles[0]->getSampleRates().begin(); |
| 1319 | config->format = profiles[0]->getFormat(); |
| 1320 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1321 | return INVALID_OPERATION; |
| 1322 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1323 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1324 | *selectedDeviceId = getFirstDeviceId(outputDevices); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1325 | for (auto &outputDevice : outputDevices) { |
| 1326 | if (outputDevice->getId() == getConfig().getDefaultOutputDevice()->getId()) { |
| 1327 | *selectedDeviceId = outputDevice->getId(); |
| 1328 | break; |
| 1329 | } |
| 1330 | } |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1331 | |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1332 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX)) { |
| 1333 | *outputType = API_OUTPUT_TELEPHONY_TX; |
| 1334 | } else { |
| 1335 | *outputType = API_OUTPUT_LEGACY; |
| 1336 | } |
| 1337 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1338 | ALOGV("%s returns output %d selectedDeviceId %d", __func__, *output, *selectedDeviceId); |
| 1339 | |
| 1340 | return NO_ERROR; |
| 1341 | } |
| 1342 | |
| 1343 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 1344 | audio_io_handle_t *output, |
| 1345 | audio_session_t session, |
| 1346 | audio_stream_type_t *stream, |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1347 | const AttributionSourceState& attributionSource, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1348 | audio_config_t *config, |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1349 | audio_output_flags_t *flags, |
| 1350 | audio_port_handle_t *selectedDeviceId, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1351 | audio_port_handle_t *portId, |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1352 | std::vector<audio_io_handle_t> *secondaryOutputs, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1353 | output_type_t *outputType, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1354 | bool *isSpatialized, |
| 1355 | bool *isBitPerfect) |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1356 | { |
| 1357 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 1358 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 1359 | return INVALID_OPERATION; |
| 1360 | } |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1361 | const uid_t uid = VALUE_OR_RETURN_STATUS( |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1362 | aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1363 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1364 | audio_attributes_t resultAttr; |
François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1365 | bool isRequestedDeviceForExclusiveUse = false; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1366 | std::vector<sp<AudioPolicyMix>> secondaryMixes; |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1367 | const sp<DeviceDescriptor> requestedDevice = |
| 1368 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 1369 | |
| 1370 | // Prevent from storing invalid requested device id in clients |
| 1371 | const audio_port_handle_t sanitizedRequestedPortId = |
| 1372 | requestedDevice != nullptr ? requestedPortId : AUDIO_PORT_HANDLE_NONE; |
| 1373 | *selectedDeviceId = sanitizedRequestedPortId; |
| 1374 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1375 | status_t status = getOutputForAttrInt(&resultAttr, output, session, attr, stream, uid, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1376 | config, flags, selectedDeviceId, &isRequestedDeviceForExclusiveUse, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1377 | secondaryOutputs != nullptr ? &secondaryMixes : nullptr, outputType, isSpatialized, |
| 1378 | isBitPerfect); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1379 | if (status != NO_ERROR) { |
| 1380 | return status; |
| 1381 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1382 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryOutputDescs; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1383 | if (secondaryOutputs != nullptr) { |
| 1384 | for (auto &secondaryMix : secondaryMixes) { |
| 1385 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); |
| 1386 | if (outputDesc != nullptr && |
| 1387 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { |
| 1388 | secondaryOutputs->push_back(outputDesc->mIoHandle); |
| 1389 | weakSecondaryOutputDescs.push_back(outputDesc); |
| 1390 | } |
| 1391 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1392 | } |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1393 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1394 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1395 | .channel_mask = config->channel_mask, |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1396 | .format = config->format, |
Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1397 | }; |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 1398 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1399 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1400 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1401 | sp<TrackClientDescriptor> clientDesc = |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1402 | new TrackClientDescriptor(*portId, uid, session, resultAttr, clientConfig, |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1403 | sanitizedRequestedPortId, *stream, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1404 | mEngine->getProductStrategyForAttributes(resultAttr), |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 1405 | toVolumeSource(resultAttr), |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1406 | *flags, isRequestedDeviceForExclusiveUse, |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1407 | std::move(weakSecondaryOutputDescs), |
| 1408 | outputDesc->mPolicyMix); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1409 | outputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1410 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1411 | ALOGV("%s() returns output %d requestedPortId %d selectedDeviceId %d for port ID %d", __func__, |
| 1412 | *output, requestedPortId, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1413 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1414 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1417 | status_t AudioPolicyManager::openDirectOutput(audio_stream_type_t stream, |
| 1418 | audio_session_t session, |
| 1419 | const audio_config_t *config, |
| 1420 | audio_output_flags_t flags, |
| 1421 | const DeviceVector &devices, |
| 1422 | audio_io_handle_t *output) { |
| 1423 | |
| 1424 | *output = AUDIO_IO_HANDLE_NONE; |
| 1425 | |
| 1426 | // skip direct output selection if the request can obviously be attached to a mixed output |
| 1427 | // and not explicitly requested |
| 1428 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 1429 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 1430 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
| 1431 | return NAME_NOT_FOUND; |
| 1432 | } |
| 1433 | |
| 1434 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 1435 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 1436 | // when audioflinger detects there is an active non offloadable effect. |
| 1437 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 1438 | // This may prevent offloading in rare situations where effects are left active by apps |
| 1439 | // in the background. |
| 1440 | sp<IOProfile> profile; |
| 1441 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
| 1442 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
| 1443 | profile = getProfileForOutput( |
| 1444 | devices, config->sample_rate, config->format, config->channel_mask, |
| 1445 | flags, true /* directOnly */); |
| 1446 | } |
| 1447 | |
| 1448 | if (profile == nullptr) { |
| 1449 | return NAME_NOT_FOUND; |
| 1450 | } |
| 1451 | |
| 1452 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 1453 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1454 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 1455 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 1456 | // reuse direct output if currently open by the same client |
| 1457 | // and configured with same parameters |
| 1458 | if ((config->sample_rate == desc->getSamplingRate()) && |
| 1459 | (config->format == desc->getFormat()) && |
| 1460 | (config->channel_mask == desc->getChannelMask()) && |
| 1461 | (session == desc->mDirectClientSession)) { |
| 1462 | desc->mDirectOpenCount++; |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 1463 | ALOGV("%s reusing direct output %d for session %d", __func__, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1464 | mOutputs.keyAt(i), session); |
| 1465 | *output = mOutputs.keyAt(i); |
| 1466 | return NO_ERROR; |
| 1467 | } |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | if (!profile->canOpenNewIo()) { |
| 1472 | return NAME_NOT_FOUND; |
| 1473 | } |
| 1474 | |
| 1475 | sp<SwAudioOutputDescriptor> outputDesc = |
| 1476 | new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 1477 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1478 | // An MSD patch may be using the only output stream that can service this request. Release |
| 1479 | // all MSD patches to prioritize this request over any active output on MSD. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1480 | releaseMsdOutputPatches(devices); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1481 | |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 1482 | status_t status = |
| 1483 | outputDesc->open(config, nullptr /* mixerConfig */, devices, stream, flags, output); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1484 | |
| 1485 | // only accept an output with the requested parameters |
| 1486 | if (status != NO_ERROR || |
| 1487 | (config->sample_rate != 0 && config->sample_rate != outputDesc->getSamplingRate()) || |
| 1488 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->getFormat()) || |
| 1489 | (config->channel_mask != 0 && config->channel_mask != outputDesc->getChannelMask())) { |
| 1490 | ALOGV("%s failed opening direct output: output %d sample rate %d %d," |
| 1491 | "format %d %d, channel mask %04x %04x", __func__, *output, config->sample_rate, |
| 1492 | outputDesc->getSamplingRate(), config->format, outputDesc->getFormat(), |
| 1493 | config->channel_mask, outputDesc->getChannelMask()); |
| 1494 | if (*output != AUDIO_IO_HANDLE_NONE) { |
| 1495 | outputDesc->close(); |
| 1496 | } |
| 1497 | // fall back to mixer output if possible when the direct output could not be open |
| 1498 | if (audio_is_linear_pcm(config->format) && |
| 1499 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
| 1500 | return NAME_NOT_FOUND; |
| 1501 | } |
| 1502 | *output = AUDIO_IO_HANDLE_NONE; |
| 1503 | return BAD_VALUE; |
| 1504 | } |
| 1505 | outputDesc->mDirectOpenCount = 1; |
| 1506 | outputDesc->mDirectClientSession = session; |
| 1507 | |
| 1508 | addOutput(*output, outputDesc); |
| 1509 | mPreviousOutputs = mOutputs; |
| 1510 | ALOGV("%s returns new direct output %d", __func__, *output); |
| 1511 | mpClientInterface->onAudioPortListUpdate(); |
| 1512 | return NO_ERROR; |
| 1513 | } |
| 1514 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1515 | audio_io_handle_t AudioPolicyManager::getOutputForDevices( |
| 1516 | const DeviceVector &devices, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1517 | audio_session_t session, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1518 | const audio_attributes_t *attr, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1519 | const audio_config_t *config, |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1520 | audio_output_flags_t *flags, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1521 | bool *isSpatialized, |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1522 | sp<PreferredMixerAttributesInfo> prefMixerConfigInfo, |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1523 | bool forceMutingHaptic) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1524 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1525 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1526 | |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1527 | // Discard haptic channel mask when forcing muting haptic channels. |
| 1528 | audio_channel_mask_t channelMask = forceMutingHaptic |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1529 | ? static_cast<audio_channel_mask_t>(config->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL) |
| 1530 | : config->channel_mask; |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1531 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1532 | // open a direct output if required by specified parameters |
| 1533 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 1534 | // and all common behaviors are driven by checking only the direct flag |
| 1535 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1536 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1537 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1538 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1539 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 1540 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1541 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1542 | |
| 1543 | audio_stream_type_t stream = mEngine->getStreamTypeForAttributes(*attr); |
| 1544 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1545 | // only allow deep buffering for music stream type |
| 1546 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1547 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1548 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1549 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1550 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 1551 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1552 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1553 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1554 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1555 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1556 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1557 | audio_is_linear_pcm(config->format) && |
| 1558 | (*flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) == 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1559 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1560 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1561 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1562 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1563 | |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 1564 | // Attach the Ultrasound flag for the AUDIO_CONTENT_TYPE_ULTRASOUND |
| 1565 | if (attr->content_type == AUDIO_CONTENT_TYPE_ULTRASOUND) { |
| 1566 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_ULTRASOUND); |
| 1567 | } |
| 1568 | |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1569 | *isSpatialized = false; |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1570 | if (mSpatializerOutput != nullptr |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 1571 | && canBeSpatializedInt(attr, config, devices.toTypeAddrVector())) { |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1572 | *isSpatialized = true; |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1573 | return mSpatializerOutput->mIoHandle; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1574 | } |
| 1575 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1576 | audio_config_t directConfig = *config; |
| 1577 | directConfig.channel_mask = channelMask; |
| 1578 | status_t status = openDirectOutput(stream, session, &directConfig, *flags, devices, &output); |
| 1579 | if (status != NAME_NOT_FOUND) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1580 | return output; |
| 1581 | } |
| 1582 | |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1583 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1584 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1585 | if ((*flags & (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ)) != 0) { |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1586 | return AUDIO_IO_HANDLE_NONE; |
| 1587 | } |
Pierre Couillaud | e73496b | 2023-03-06 16:19:05 +0000 | [diff] [blame] | 1588 | // A request for Tuner cannot fallback to a mixed output |
| 1589 | if ((directConfig.offload_info.content_id || directConfig.offload_info.sync_id)) { |
| 1590 | return AUDIO_IO_HANDLE_NONE; |
| 1591 | } |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1592 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1593 | // ignoring channel mask due to downmix capability in mixer |
| 1594 | |
| 1595 | // open a non direct output |
| 1596 | |
| 1597 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1598 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1599 | // get which output is suitable for the specified stream. The actual |
| 1600 | // routing change will happen when startOutput() will be called |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1601 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1602 | if (prefMixerConfigInfo != nullptr) { |
| 1603 | for (audio_io_handle_t outputHandle : outputs) { |
| 1604 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputHandle); |
| 1605 | if (outputDesc->mProfile == prefMixerConfigInfo->getProfile()) { |
| 1606 | output = outputHandle; |
| 1607 | break; |
| 1608 | } |
| 1609 | } |
| 1610 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 1611 | // No output open with the preferred profile. Open a new one. |
| 1612 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 1613 | config.channel_mask = prefMixerConfigInfo->getConfigBase().channel_mask; |
| 1614 | config.sample_rate = prefMixerConfigInfo->getConfigBase().sample_rate; |
| 1615 | config.format = prefMixerConfigInfo->getConfigBase().format; |
| 1616 | sp<SwAudioOutputDescriptor> preferredOutput = openOutputWithProfileAndDevice( |
| 1617 | prefMixerConfigInfo->getProfile(), devices, nullptr /*mixerConfig*/, |
| 1618 | &config, prefMixerConfigInfo->getFlags()); |
| 1619 | if (preferredOutput == nullptr) { |
| 1620 | ALOGE("%s failed to open output with preferred mixer config", __func__); |
| 1621 | } else { |
| 1622 | output = preferredOutput->mIoHandle; |
| 1623 | } |
| 1624 | } |
| 1625 | } else { |
| 1626 | // at this stage we should ignore the DIRECT flag as no direct output could be |
| 1627 | // found earlier |
| 1628 | *flags = (audio_output_flags_t) (*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1629 | output = selectOutput( |
| 1630 | outputs, *flags, config->format, channelMask, config->sample_rate, session); |
| 1631 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1632 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1633 | ALOGW_IF((output == 0), "getOutputForDevices() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1634 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1635 | stream, config->sample_rate, config->format, channelMask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1636 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1637 | return output; |
| 1638 | } |
| 1639 | |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame] | 1640 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1641 | auto msdInDevices = mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1642 | mAvailableInputDevices); |
| 1643 | return msdInDevices.isEmpty()? nullptr : msdInDevices.itemAt(0); |
| 1644 | } |
| 1645 | |
| 1646 | DeviceVector AudioPolicyManager::getMsdAudioOutDevices() const { |
| 1647 | return mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1648 | mAvailableOutputDevices); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1649 | } |
| 1650 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1651 | const AudioPatchCollection AudioPolicyManager::getMsdOutputPatches() const { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1652 | AudioPatchCollection msdPatches; |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1653 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1654 | if (msdModule != 0) { |
| 1655 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { |
| 1656 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); |
| 1657 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { |
| 1658 | const struct audio_port_config *source = &patch->mPatch.sources[j]; |
| 1659 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 1660 | source->ext.device.hw_module == msdModule->getHandle()) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1661 | msdPatches.addAudioPatch(patch->getHandle(), patch); |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1662 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | } |
| 1666 | return msdPatches; |
| 1667 | } |
| 1668 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 1669 | bool AudioPolicyManager::isMsdPatch(const audio_patch_handle_t &handle) const { |
| 1670 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 1671 | if (index < 0) { |
| 1672 | return false; |
| 1673 | } |
| 1674 | const sp<AudioPatch> patch = mAudioPatches.valueAt(index); |
| 1675 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1676 | if (msdModule == nullptr) { |
| 1677 | return false; |
| 1678 | } |
| 1679 | const struct audio_port_config *sink = &patch->mPatch.sinks[0]; |
| 1680 | if (getMsdAudioOutDevices().contains(mAvailableOutputDevices.getDeviceFromId(sink->id))) { |
| 1681 | return true; |
| 1682 | } |
| 1683 | index = getMsdOutputPatches().indexOfKey(handle); |
| 1684 | if (index < 0) { |
| 1685 | return false; |
| 1686 | } |
| 1687 | return true; |
| 1688 | } |
| 1689 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1690 | status_t AudioPolicyManager::getMsdProfiles(bool hwAvSync, |
| 1691 | const InputProfileCollection &inputProfiles, |
| 1692 | const OutputProfileCollection &outputProfiles, |
| 1693 | const sp<DeviceDescriptor> &sourceDevice, |
| 1694 | const sp<DeviceDescriptor> &sinkDevice, |
| 1695 | AudioProfileVector& sourceProfiles, |
| 1696 | AudioProfileVector& sinkProfiles) const { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1697 | if (inputProfiles.isEmpty()) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1698 | ALOGE("%s() no input profiles for source module", __func__); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1699 | return NO_INIT; |
| 1700 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1701 | if (outputProfiles.isEmpty()) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1702 | ALOGE("%s() no output profiles for sink module", __func__); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1703 | return NO_INIT; |
| 1704 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1705 | for (const auto &inProfile : inputProfiles) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1706 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0) && |
| 1707 | inProfile->supportsDevice(sourceDevice)) { |
| 1708 | appendAudioProfiles(sourceProfiles, inProfile->getAudioProfiles()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1709 | } |
| 1710 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1711 | for (const auto &outProfile : outputProfiles) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1712 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) && |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1713 | outProfile->supportsDevice(sinkDevice)) { |
| 1714 | appendAudioProfiles(sinkProfiles, outProfile->getAudioProfiles()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1715 | } |
| 1716 | } |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1717 | return NO_ERROR; |
| 1718 | } |
| 1719 | |
| 1720 | status_t AudioPolicyManager::getBestMsdConfig(bool hwAvSync, |
| 1721 | const AudioProfileVector &sourceProfiles, const AudioProfileVector &sinkProfiles, |
| 1722 | audio_port_config *sourceConfig, audio_port_config *sinkConfig) const |
| 1723 | { |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 1724 | // Compressed formats for MSD module, ordered from most preferred to least preferred. |
| 1725 | static const std::vector<audio_format_t> formatsOrder = {{ |
| 1726 | AUDIO_FORMAT_IEC60958, AUDIO_FORMAT_MAT_2_1, AUDIO_FORMAT_MAT_2_0, AUDIO_FORMAT_E_AC3, |
| 1727 | AUDIO_FORMAT_AC3, AUDIO_FORMAT_PCM_16_BIT }}; |
| 1728 | static const std::vector<audio_channel_mask_t> channelMasksOrder = [](){ |
| 1729 | // Channel position masks for MSD module, 3D > 2D > 1D ordering (most preferred to least |
| 1730 | // preferred). |
| 1731 | std::vector<audio_channel_mask_t> masks = {{ |
| 1732 | AUDIO_CHANNEL_OUT_3POINT1POINT2, AUDIO_CHANNEL_OUT_3POINT0POINT2, |
| 1733 | AUDIO_CHANNEL_OUT_2POINT1POINT2, AUDIO_CHANNEL_OUT_2POINT0POINT2, |
| 1734 | AUDIO_CHANNEL_OUT_5POINT1, AUDIO_CHANNEL_OUT_STEREO }}; |
| 1735 | // insert index masks (higher counts most preferred) as preferred over position masks |
| 1736 | for (int i = 1; i <= AUDIO_CHANNEL_COUNT_MAX; i++) { |
| 1737 | masks.insert( |
| 1738 | masks.begin(), audio_channel_mask_for_index_assignment_from_count(i)); |
| 1739 | } |
| 1740 | return masks; |
| 1741 | }(); |
| 1742 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1743 | struct audio_config_base bestSinkConfig; |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 1744 | status_t result = findBestMatchingOutputConfig(sourceProfiles, sinkProfiles, formatsOrder, |
| 1745 | channelMasksOrder, true /*preferHigherSamplingRates*/, bestSinkConfig); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1746 | if (result != NO_ERROR) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1747 | ALOGD("%s() no matching config found for sink, hwAvSync: %d", |
| 1748 | __func__, hwAvSync); |
Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1749 | return result; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1750 | } |
| 1751 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1752 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; |
| 1753 | sinkConfig->format = bestSinkConfig.format; |
| 1754 | // For encoded streams force direct flag to prevent downstream mixing. |
| 1755 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1756 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); |
Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1757 | if (audio_is_iec61937_compatible(sinkConfig->format)) { |
| 1758 | // For formats compatible with IEC61937 encapsulation, assume that |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1759 | // the input is IEC61937 framed (for proportional buffer sizing). |
Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1760 | // Add the AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO flag so downstream HAL can distinguish between |
| 1761 | // raw and IEC61937 framed streams. |
| 1762 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1763 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO); |
| 1764 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1765 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1766 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 1767 | sourceConfig->channel_mask = |
| 1768 | audio_channel_mask_get_representation(bestSinkConfig.channel_mask) |
| 1769 | == AUDIO_CHANNEL_REPRESENTATION_INDEX ? |
| 1770 | bestSinkConfig.channel_mask : audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1771 | sourceConfig->format = bestSinkConfig.format; |
| 1772 | // Copy input stream directly without any processing (e.g. resampling). |
| 1773 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1774 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); |
| 1775 | if (hwAvSync) { |
| 1776 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1777 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 1778 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1779 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); |
| 1780 | } |
| 1781 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | |
| 1782 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; |
| 1783 | sinkConfig->config_mask |= config_mask; |
| 1784 | sourceConfig->config_mask |= config_mask; |
| 1785 | return NO_ERROR; |
| 1786 | } |
| 1787 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1788 | PatchBuilder AudioPolicyManager::buildMsdPatch(bool msdIsSource, |
| 1789 | const sp<DeviceDescriptor> &device) const |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1790 | { |
| 1791 | PatchBuilder patchBuilder; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1792 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1793 | ALOG_ASSERT(msdModule != nullptr, "MSD module not available"); |
| 1794 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(device, AUDIO_FORMAT_DEFAULT); |
| 1795 | if (deviceModule == nullptr) { |
| 1796 | ALOGE("%s() unable to get module for %s", __func__, device->toString().c_str()); |
| 1797 | return patchBuilder; |
| 1798 | } |
| 1799 | const InputProfileCollection inputProfiles = msdIsSource ? |
| 1800 | msdModule->getInputProfiles() : deviceModule->getInputProfiles(); |
| 1801 | const OutputProfileCollection outputProfiles = msdIsSource ? |
| 1802 | deviceModule->getOutputProfiles() : msdModule->getOutputProfiles(); |
| 1803 | |
| 1804 | const sp<DeviceDescriptor> sourceDevice = msdIsSource ? getMsdAudioInDevice() : device; |
| 1805 | const sp<DeviceDescriptor> sinkDevice = msdIsSource ? |
| 1806 | device : getMsdAudioOutDevices().itemAt(0); |
| 1807 | patchBuilder.addSource(sourceDevice).addSink(sinkDevice); |
| 1808 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1809 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; |
| 1810 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1811 | AudioProfileVector sourceProfiles; |
| 1812 | AudioProfileVector sinkProfiles; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1813 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. |
| 1814 | // For now, we just forcefully try with HwAvSync first. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1815 | for (auto hwAvSync : { true, false }) { |
| 1816 | if (getMsdProfiles(hwAvSync, inputProfiles, outputProfiles, sourceDevice, sinkDevice, |
| 1817 | sourceProfiles, sinkProfiles) != NO_ERROR) { |
| 1818 | continue; |
| 1819 | } |
| 1820 | if (getBestMsdConfig(hwAvSync, sourceProfiles, sinkProfiles, &sourceConfig, |
| 1821 | &sinkConfig) == NO_ERROR) { |
| 1822 | // Found a matching config. Re-create PatchBuilder with this config. |
| 1823 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); |
| 1824 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1825 | } |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1826 | ALOGV("%s() no matching config found. Fall through to default PCM patch" |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1827 | " supporting PCM format conversion.", __func__); |
| 1828 | return patchBuilder; |
| 1829 | } |
| 1830 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1831 | status_t AudioPolicyManager::setMsdOutputPatches(const DeviceVector *outputDevices) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1832 | DeviceVector devices; |
| 1833 | if (outputDevices != nullptr && outputDevices->size() > 0) { |
| 1834 | devices.add(*outputDevices); |
| 1835 | } else { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1836 | // Use media strategy for unspecified output device. This should only |
| 1837 | // occur on checkForDeviceAndOutputChanges(). Device connection events may |
| 1838 | // therefore invalidate explicit routing requests. |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1839 | devices = mEngine->getOutputDevicesForAttributes( |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1840 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1841 | LOG_ALWAYS_FATAL_IF(devices.isEmpty(), "no output device to set MSD patch"); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1842 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1843 | std::vector<PatchBuilder> patchesToCreate; |
| 1844 | for (auto i = 0u; i < devices.size(); ++i) { |
| 1845 | ALOGV("%s() for device %s", __func__, devices[i]->toString().c_str()); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1846 | patchesToCreate.push_back(buildMsdPatch(true /*msdIsSource*/, devices[i])); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1847 | } |
| 1848 | // Retain only the MSD patches associated with outputDevices request. |
| 1849 | // Tear down the others, and create new ones as needed. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1850 | AudioPatchCollection patchesToRemove = getMsdOutputPatches(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1851 | for (auto it = patchesToCreate.begin(); it != patchesToCreate.end(); ) { |
| 1852 | auto retainedPatch = false; |
| 1853 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { |
| 1854 | if (audio_patches_are_equal(it->patch(), &patchesToRemove[i]->mPatch)) { |
| 1855 | patchesToRemove.removeItemsAt(i); |
| 1856 | retainedPatch = true; |
| 1857 | break; |
| 1858 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1859 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1860 | if (retainedPatch) { |
| 1861 | it = patchesToCreate.erase(it); |
| 1862 | continue; |
| 1863 | } |
| 1864 | ++it; |
| 1865 | } |
| 1866 | if (patchesToCreate.size() == 0 && patchesToRemove.size() == 0) { |
| 1867 | return NO_ERROR; |
| 1868 | } |
| 1869 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { |
| 1870 | auto ¤tPatch = patchesToRemove.valueAt(i); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1871 | releaseAudioPatch(currentPatch->getHandle(), mUidCached); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1872 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1873 | status_t status = NO_ERROR; |
| 1874 | for (const auto &p : patchesToCreate) { |
| 1875 | auto currStatus = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, |
| 1876 | p.patch(), 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); |
| 1877 | char message[256]; |
| 1878 | snprintf(message, sizeof(message), "%s() %s: creating MSD patch from device:IN_BUS to " |
| 1879 | "device:%#x (format:%#x channels:%#x samplerate:%d)", __func__, |
| 1880 | currStatus == NO_ERROR ? "Success" : "Error", |
| 1881 | p.patch()->sinks[0].ext.device.type, p.patch()->sources[0].format, |
| 1882 | p.patch()->sources[0].channel_mask, p.patch()->sources[0].sample_rate); |
| 1883 | if (currStatus == NO_ERROR) { |
| 1884 | ALOGD("%s", message); |
| 1885 | } else { |
| 1886 | ALOGE("%s", message); |
| 1887 | if (status == NO_ERROR) { |
| 1888 | status = currStatus; |
| 1889 | } |
| 1890 | } |
| 1891 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1892 | return status; |
| 1893 | } |
| 1894 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1895 | void AudioPolicyManager::releaseMsdOutputPatches(const DeviceVector& devices) { |
| 1896 | AudioPatchCollection msdPatches = getMsdOutputPatches(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1897 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1898 | const auto& patch = msdPatches[i]; |
| 1899 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1900 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1901 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && devices.getDevice(sink->ext.device.type, |
| 1902 | String8(sink->ext.device.address), AUDIO_FORMAT_DEFAULT) != nullptr) { |
| 1903 | releaseAudioPatch(patch->getHandle(), mUidCached); |
| 1904 | break; |
| 1905 | } |
| 1906 | } |
| 1907 | } |
| 1908 | } |
| 1909 | |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 1910 | bool AudioPolicyManager::msdHasPatchesToAllDevices(const AudioDeviceTypeAddrVector& devices) { |
| 1911 | DeviceVector devicesToCheck = mOutputDevicesAll.getDevicesFromDeviceTypeAddrVec(devices); |
| 1912 | AudioPatchCollection msdPatches = getMsdOutputPatches(); |
| 1913 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1914 | const auto& patch = msdPatches[i]; |
| 1915 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1916 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1917 | if (sink->type == AUDIO_PORT_TYPE_DEVICE) { |
| 1918 | const auto& foundDevice = devicesToCheck.getDevice( |
| 1919 | sink->ext.device.type, String8(sink->ext.device.address), AUDIO_FORMAT_DEFAULT); |
| 1920 | if (foundDevice != nullptr) { |
| 1921 | devicesToCheck.remove(foundDevice); |
| 1922 | if (devicesToCheck.isEmpty()) { |
| 1923 | return true; |
| 1924 | } |
| 1925 | } |
| 1926 | } |
| 1927 | } |
| 1928 | } |
| 1929 | return false; |
| 1930 | } |
| 1931 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1932 | audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs, |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1933 | audio_output_flags_t flags, |
| 1934 | audio_format_t format, |
| 1935 | audio_channel_mask_t channelMask, |
| 1936 | uint32_t samplingRate, |
| 1937 | audio_session_t sessionId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1938 | { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1939 | LOG_ALWAYS_FATAL_IF(!(format == AUDIO_FORMAT_INVALID || audio_is_linear_pcm(format)), |
| 1940 | "%s called with format %#x", __func__, format); |
| 1941 | |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1942 | // Return the output that haptic-generating attached to when 1) session id is specified, |
| 1943 | // 2) haptic-generating effect exists for given session id and 3) the output that |
| 1944 | // haptic-generating effect attached to is in given outputs. |
| 1945 | if (sessionId != AUDIO_SESSION_NONE) { |
| 1946 | audio_io_handle_t hapticGeneratingOutput = mEffects.getIoForSession( |
| 1947 | sessionId, FX_IID_HAPTICGENERATOR); |
| 1948 | if (outputs.indexOf(hapticGeneratingOutput) >= 0) { |
| 1949 | return hapticGeneratingOutput; |
| 1950 | } |
| 1951 | } |
| 1952 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1953 | // Flags disqualifying an output: the match must happen before calling selectOutput() |
| 1954 | static const audio_output_flags_t kExcludedFlags = (audio_output_flags_t) |
| 1955 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1956 | |
| 1957 | // Flags expressing a functional request: must be honored in priority over |
| 1958 | // other criteria |
| 1959 | static const audio_output_flags_t kFunctionalFlags = (audio_output_flags_t) |
| 1960 | (AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_INCALL_MUSIC | |
Eric Laurent | e28c66d | 2022-01-21 13:40:41 +0100 | [diff] [blame] | 1961 | AUDIO_OUTPUT_FLAG_TTS | AUDIO_OUTPUT_FLAG_DIRECT_PCM | AUDIO_OUTPUT_FLAG_ULTRASOUND | |
| 1962 | AUDIO_OUTPUT_FLAG_SPATIALIZER); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1963 | // Flags expressing a performance request: have lower priority than serving |
| 1964 | // requested sampling rate or channel mask |
| 1965 | static const audio_output_flags_t kPerformanceFlags = (audio_output_flags_t) |
| 1966 | (AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_DEEP_BUFFER | |
| 1967 | AUDIO_OUTPUT_FLAG_RAW | AUDIO_OUTPUT_FLAG_SYNC); |
| 1968 | |
| 1969 | const audio_output_flags_t functionalFlags = |
| 1970 | (audio_output_flags_t)(flags & kFunctionalFlags); |
| 1971 | const audio_output_flags_t performanceFlags = |
| 1972 | (audio_output_flags_t)(flags & kPerformanceFlags); |
| 1973 | |
| 1974 | audio_io_handle_t bestOutput = (outputs.size() == 0) ? AUDIO_IO_HANDLE_NONE : outputs[0]; |
| 1975 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1976 | // select one output among several that provide a path to a particular device or set of |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1977 | // devices (the list was previously build by getOutputsForDevices()). |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1978 | // The priority is as follows: |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1979 | // 1: the output supporting haptic playback when requesting haptic playback |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1980 | // 2: the output with the highest number of requested functional flags |
Carter Hsu | 199f189 | 2021-10-15 15:47:29 +0800 | [diff] [blame] | 1981 | // with tiebreak preferring the minimum number of extra functional flags |
| 1982 | // (see b/200293124, the incorrect selection of AUDIO_OUTPUT_FLAG_VOIP_RX). |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1983 | // 3: the output supporting the exact channel mask |
| 1984 | // 4: the output with a higher channel count than requested |
jiabin | b12a6da | 2022-06-03 20:48:18 +0000 | [diff] [blame] | 1985 | // 5: the output with the highest sampling rate if the requested sample rate is |
| 1986 | // greater than default sampling rate |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1987 | // 6: the output with the highest number of requested performance flags |
| 1988 | // 7: the output with the bit depth the closest to the requested one |
| 1989 | // 8: the primary output |
| 1990 | // 9: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1991 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1992 | // matching criteria values in priority order for best matching output so far |
| 1993 | std::vector<uint32_t> bestMatchCriteria(8, 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1994 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1995 | const uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); |
| 1996 | const uint32_t hapticChannelCount = audio_channel_count_from_out_mask( |
| 1997 | channelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
Eric Laurent | e78b676 | 2018-12-19 16:29:01 -0800 | [diff] [blame] | 1998 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1999 | for (audio_io_handle_t output : outputs) { |
| 2000 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2001 | // matching criteria values in priority order for current output |
| 2002 | std::vector<uint32_t> currentMatchCriteria(8, 0); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 2003 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2004 | if (outputDesc->isDuplicated()) { |
| 2005 | continue; |
| 2006 | } |
| 2007 | if ((kExcludedFlags & outputDesc->mFlags) != 0) { |
| 2008 | continue; |
| 2009 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2010 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2011 | // If haptic channel is specified, use the haptic output if present. |
| 2012 | // When using haptic output, same audio format and sample rate are required. |
| 2013 | const uint32_t outputHapticChannelCount = audio_channel_count_from_out_mask( |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2014 | outputDesc->getChannelMask() & AUDIO_CHANNEL_HAPTIC_ALL); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2015 | if ((hapticChannelCount == 0) != (outputHapticChannelCount == 0)) { |
| 2016 | continue; |
| 2017 | } |
| 2018 | if (outputHapticChannelCount >= hapticChannelCount |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2019 | && format == outputDesc->getFormat() |
| 2020 | && samplingRate == outputDesc->getSamplingRate()) { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2021 | currentMatchCriteria[0] = outputHapticChannelCount; |
| 2022 | } |
| 2023 | |
| 2024 | // functional flags match |
Carter Hsu | 199f189 | 2021-10-15 15:47:29 +0800 | [diff] [blame] | 2025 | const int matchingFunctionalFlags = |
| 2026 | __builtin_popcount(outputDesc->mFlags & functionalFlags); |
| 2027 | const int totalFunctionalFlags = |
| 2028 | __builtin_popcount(outputDesc->mFlags & kFunctionalFlags); |
| 2029 | // Prefer matching functional flags, but subtract unnecessary functional flags. |
| 2030 | currentMatchCriteria[1] = 100 * (matchingFunctionalFlags + 1) - totalFunctionalFlags; |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2031 | |
| 2032 | // channel mask and channel count match |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2033 | uint32_t outputChannelCount = audio_channel_count_from_out_mask( |
| 2034 | outputDesc->getChannelMask()); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2035 | if (channelMask != AUDIO_CHANNEL_NONE && channelCount > 2 && |
| 2036 | channelCount <= outputChannelCount) { |
| 2037 | if ((audio_channel_mask_get_representation(channelMask) == |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2038 | audio_channel_mask_get_representation(outputDesc->getChannelMask())) && |
| 2039 | ((channelMask & outputDesc->getChannelMask()) == channelMask)) { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2040 | currentMatchCriteria[2] = outputChannelCount; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2041 | } |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2042 | currentMatchCriteria[3] = outputChannelCount; |
| 2043 | } |
| 2044 | |
| 2045 | // sampling rate match |
jiabin | b12a6da | 2022-06-03 20:48:18 +0000 | [diff] [blame] | 2046 | if (samplingRate > SAMPLE_RATE_HZ_DEFAULT) { |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2047 | currentMatchCriteria[4] = outputDesc->getSamplingRate(); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | // performance flags match |
| 2051 | currentMatchCriteria[5] = popcount(outputDesc->mFlags & performanceFlags); |
| 2052 | |
| 2053 | // format match |
| 2054 | if (format != AUDIO_FORMAT_INVALID) { |
| 2055 | currentMatchCriteria[6] = |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 2056 | PolicyAudioPort::kFormatDistanceMax - |
| 2057 | PolicyAudioPort::formatDistance(format, outputDesc->getFormat()); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | // primary output match |
| 2061 | currentMatchCriteria[7] = outputDesc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY; |
| 2062 | |
| 2063 | // compare match criteria by priority then value |
| 2064 | if (std::lexicographical_compare(bestMatchCriteria.begin(), bestMatchCriteria.end(), |
| 2065 | currentMatchCriteria.begin(), currentMatchCriteria.end())) { |
| 2066 | bestMatchCriteria = currentMatchCriteria; |
| 2067 | bestOutput = output; |
| 2068 | |
| 2069 | std::stringstream result; |
| 2070 | std::copy(bestMatchCriteria.begin(), bestMatchCriteria.end(), |
| 2071 | std::ostream_iterator<int>(result, " ")); |
| 2072 | ALOGV("%s new bestOutput %d criteria %s", |
| 2073 | __func__, bestOutput, result.str().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2074 | } |
| 2075 | } |
| 2076 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2077 | return bestOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2078 | } |
| 2079 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2080 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2081 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2082 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2083 | |
| 2084 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2085 | if (outputDesc == 0) { |
| 2086 | ALOGW("startOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2087 | return BAD_VALUE; |
| 2088 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2089 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2090 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2091 | ALOGV("startOutput() output %d, stream %d, session %d", |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2092 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2093 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2094 | status_t status = outputDesc->start(); |
| 2095 | if (status != NO_ERROR) { |
| 2096 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2097 | } |
| 2098 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2099 | uint32_t delayMs; |
| 2100 | status = startSource(outputDesc, client, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2101 | |
| 2102 | if (status != NO_ERROR) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2103 | outputDesc->stop(); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2104 | if (status == DEAD_OBJECT) { |
| 2105 | sp<SwAudioOutputDescriptor> desc = |
| 2106 | reopenOutput(outputDesc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 2107 | if (desc == nullptr) { |
| 2108 | // This is not common, it may indicate something wrong with the HAL. |
| 2109 | ALOGE("%s unable to open output with default config", __func__); |
| 2110 | return status; |
| 2111 | } |
| 2112 | desc->mUsePreferredMixerAttributes = true; |
| 2113 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2114 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2115 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2116 | |
| 2117 | // If the client is the first one active on preferred mixer parameters, reopen the output |
| 2118 | // if the current mixer parameters doesn't match the preferred one. |
| 2119 | if (outputDesc->devices().size() == 1) { |
| 2120 | sp<PreferredMixerAttributesInfo> info = getPreferredMixerAttributesInfo( |
| 2121 | outputDesc->devices()[0]->getId(), client->strategy()); |
| 2122 | if (info != nullptr && info->getUid() == client->uid()) { |
| 2123 | if (info->getActiveClientCount() == 0 && !outputDesc->isConfigurationMatched( |
| 2124 | info->getConfigBase(), info->getFlags())) { |
| 2125 | stopSource(outputDesc, client); |
| 2126 | outputDesc->stop(); |
| 2127 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 2128 | config.channel_mask = info->getConfigBase().channel_mask; |
| 2129 | config.sample_rate = info->getConfigBase().sample_rate; |
| 2130 | config.format = info->getConfigBase().format; |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2131 | sp<SwAudioOutputDescriptor> desc = |
| 2132 | reopenOutput(outputDesc, &config, info->getFlags(), __func__); |
| 2133 | if (desc == nullptr) { |
| 2134 | return BAD_VALUE; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2135 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2136 | desc->mUsePreferredMixerAttributes = true; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2137 | // Intentionally return error to let the client side resending request for |
| 2138 | // creating and starting. |
| 2139 | return DEAD_OBJECT; |
| 2140 | } |
| 2141 | info->increaseActiveClient(); |
| 2142 | } |
| 2143 | } |
| 2144 | |
Jean-Michel Trivi | b1f6e64 | 2023-02-07 20:49:04 +0000 | [diff] [blame] | 2145 | if (client->hasPreferredDevice()) { |
| 2146 | // playback activity with preferred device impacts routing occurred, inform upper layers |
| 2147 | mpClientInterface->onRoutingUpdated(); |
| 2148 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2149 | if (delayMs != 0) { |
| 2150 | usleep(delayMs * 1000); |
| 2151 | } |
| 2152 | |
| 2153 | return status; |
| 2154 | } |
| 2155 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2156 | bool AudioPolicyManager::isLeUnicastActive() const { |
| 2157 | if (isInCall()) { |
| 2158 | return true; |
| 2159 | } |
| 2160 | return isAnyDeviceTypeActive(getAudioDeviceOutLeAudioUnicastSet()); |
| 2161 | } |
| 2162 | |
| 2163 | bool AudioPolicyManager::isAnyDeviceTypeActive(const DeviceTypeSet& deviceTypes) const { |
| 2164 | if (mAvailableOutputDevices.getDevicesFromTypes(deviceTypes).isEmpty()) { |
| 2165 | return false; |
| 2166 | } |
| 2167 | bool active = mOutputs.isAnyDeviceTypeActive(deviceTypes); |
| 2168 | ALOGV("%s active %d", __func__, active); |
| 2169 | return active; |
| 2170 | } |
| 2171 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2172 | status_t AudioPolicyManager::startSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 2173 | const sp<TrackClientDescriptor>& client, |
| 2174 | uint32_t *delayMs) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2175 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2176 | // cannot start playback of STREAM_TTS if any other output is being used |
| 2177 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2178 | |
| 2179 | *delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2180 | audio_stream_type_t stream = client->stream(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2181 | auto clientVolSrc = client->volumeSource(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2182 | auto clientStrategy = client->strategy(); |
| 2183 | auto clientAttr = client->attributes(); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2184 | if (stream == AUDIO_STREAM_TTS) { |
| 2185 | ALOGV("\t found BEACON stream"); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2186 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive( |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 2187 | toVolumeSource(AUDIO_STREAM_TTS, false) /*sourceToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2188 | return INVALID_OPERATION; |
| 2189 | } else { |
| 2190 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 2191 | } |
| 2192 | } else { |
| 2193 | // some playback other than beacon starts |
| 2194 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 2195 | } |
| 2196 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2197 | // force device change if the output is inactive and no audio patch is already present. |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2198 | // check active before incrementing usage count |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 2199 | bool force = !outputDesc->isActive() && !outputDesc->isRouted(); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2200 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2201 | DeviceVector devices; |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2202 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2203 | const char *address = NULL; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2204 | if (policyMix != nullptr) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2205 | audio_devices_t newDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2206 | address = policyMix->mDeviceAddress.string(); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2207 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2208 | newDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2209 | } else { |
| 2210 | newDeviceType = policyMix->mDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2211 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2212 | sp device = mAvailableOutputDevices.getDevice(newDeviceType, String8(address), |
| 2213 | AUDIO_FORMAT_DEFAULT); |
| 2214 | ALOG_ASSERT(device, "%s: no device found t=%u, a=%s", __func__, newDeviceType, address); |
| 2215 | devices.add(device); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2218 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 2219 | // It covers a common case when there is no materially active audio |
| 2220 | // and muting would result in unnecessary delay and dropped audio. |
| 2221 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 2222 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2223 | bool wasLeUnicastActive = isLeUnicastActive(); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2224 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2225 | // increment usage count for this stream on the requested output: |
| 2226 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 2227 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 2228 | outputDesc->setClientActive(client, true); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2229 | |
| 2230 | if (client->hasPreferredDevice(true)) { |
Eric Laurent | 72af801 | 2023-03-15 17:36:22 +0100 | [diff] [blame] | 2231 | if (outputDesc->sameExclusivePreferredDevicesCount() > 0) { |
François Gaffie | f96e543 | 2019-04-09 17:13:56 +0200 | [diff] [blame] | 2232 | // Preferred device may be exclusive, use only if no other active clients on this output |
| 2233 | devices = DeviceVector( |
| 2234 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId())); |
| 2235 | } else { |
| 2236 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
| 2237 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2238 | if (devices != outputDesc->devices()) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2239 | checkStrategyRoute(clientStrategy, outputDesc->mIoHandle); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2240 | } |
| 2241 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2242 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2243 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2244 | selectOutputForMusicEffects(); |
| 2245 | } |
| 2246 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2247 | if (outputDesc->getActivityCount(clientVolSrc) == 1 || !devices.isEmpty()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2248 | // starting an output being rerouted? |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2249 | if (devices.isEmpty()) { |
| 2250 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2251 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2252 | bool shouldWait = |
| 2253 | (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM)) || |
| 2254 | followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_NOTIFICATION)) || |
| 2255 | (beaconMuteLatency > 0)); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2256 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2257 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2258 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2259 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2260 | // An output has a shared device if |
| 2261 | // - managed by the same hw module |
| 2262 | // - supports the currently selected device |
| 2263 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2264 | && (!desc->filterSupportedDevices(devices).isEmpty()); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2265 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2266 | // force a device change if any other output is: |
| 2267 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 2268 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2269 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2270 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2271 | // In this case, the audio HAL must receive the new device selection so that it can |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2272 | // change the device currently selected by the other output. |
| 2273 | if (sharedDevice && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2274 | desc->devices() != devices && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2275 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2276 | force = true; |
| 2277 | } |
| 2278 | // wait for audio on other active outputs to be presented when starting |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2279 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 2280 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2281 | const uint32_t latencyMs = desc->latency(); |
| 2282 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 2283 | |
| 2284 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 2285 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2286 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2287 | |
| 2288 | // Require mute check if another output is on a shared device |
| 2289 | // and currently active to have proper drain and avoid pops. |
| 2290 | // Note restoring AudioTracks onto this output needs to invoke |
| 2291 | // a volume ramp if there is no mute. |
| 2292 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2293 | } |
| 2294 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2295 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2296 | if (outputDesc->mUsePreferredMixerAttributes && devices != outputDesc->devices()) { |
| 2297 | // If the output is open with preferred mixer attributes, but the routed device is |
| 2298 | // changed when calling this function, returning DEAD_OBJECT to indicate routing |
| 2299 | // changed. |
| 2300 | return DEAD_OBJECT; |
| 2301 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2302 | const uint32_t muteWaitMs = |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2303 | setOutputDevices(outputDesc, devices, force, 0, nullptr, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2304 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2305 | // apply volume rules for current stream and device if necessary |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2306 | auto &curves = getVolumeCurves(client->attributes()); |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2307 | if (NO_ERROR != checkAndSetVolume(curves, client->volumeSource(), |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2308 | curves.getVolumeIndex(outputDesc->devices().types()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2309 | outputDesc, |
Francois Gaffie | d11442b | 2020-04-27 11:51:09 +0200 | [diff] [blame] | 2310 | outputDesc->devices().types(), 0 /*delay*/, |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2311 | outputDesc->useHwGain() /*force*/)) { |
| 2312 | // request AudioService to reinitialize the volume curves asynchronously |
| 2313 | ALOGE("checkAndSetVolume failed, requesting volume range init"); |
| 2314 | mpClientInterface->onVolumeRangeInitRequest(); |
| 2315 | }; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2316 | |
| 2317 | // update the outputs if starting an output with a stream that can affect notification |
| 2318 | // routing |
| 2319 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2320 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 2321 | // force reevaluating accessibility routing when ringtone or alarm starts |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2322 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM))) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 2323 | invalidateStreams({AUDIO_STREAM_ACCESSIBILITY}); |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 2324 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2325 | |
| 2326 | if (waitMs > muteWaitMs) { |
| 2327 | *delayMs = waitMs - muteWaitMs; |
| 2328 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2329 | |
| 2330 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 2331 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 2332 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 2333 | // change occurs after the MixerThread starts and causes a stream volume |
| 2334 | // glitch. |
| 2335 | // |
| 2336 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2337 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2338 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2339 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2340 | mEngine->getForceUse( |
| 2341 | AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2342 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), true, outputDesc); |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2343 | } |
| 2344 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2345 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 2346 | // of type MIX_TYPE_RECORDERS |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2347 | if (isSingleDeviceType(devices.types(), &audio_is_remote_submix_device) && |
| 2348 | policyMix != NULL && policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2349 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2350 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2351 | address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2352 | "remote-submix", |
| 2353 | AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2354 | } |
| 2355 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2356 | checkLeBroadcastRoutes(wasLeUnicastActive, outputDesc, *delayMs); |
| 2357 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2358 | return NO_ERROR; |
| 2359 | } |
| 2360 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2361 | void AudioPolicyManager::checkLeBroadcastRoutes(bool wasUnicastActive, |
| 2362 | sp<SwAudioOutputDescriptor> ignoredOutput, uint32_t delayMs) { |
| 2363 | bool isUnicastActive = isLeUnicastActive(); |
| 2364 | |
| 2365 | if (wasUnicastActive != isUnicastActive) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2366 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2367 | //reroute all outputs routed to LE broadcast if LE unicast activy changed on any output |
| 2368 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2369 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2370 | if (desc != ignoredOutput && desc->isActive() |
| 2371 | && ((isUnicastActive && |
| 2372 | !desc->devices(). |
| 2373 | getDevicesFromType(AUDIO_DEVICE_OUT_BLE_BROADCAST).isEmpty()) |
| 2374 | || (wasUnicastActive && |
| 2375 | !desc->devices().getDevicesFromTypes( |
| 2376 | getAudioDeviceOutLeAudioUnicastSet()).isEmpty()))) { |
| 2377 | DeviceVector newDevices = getNewOutputDevices(desc, false /*fromCache*/); |
| 2378 | bool force = desc->devices() != newDevices; |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2379 | if (desc->mUsePreferredMixerAttributes && force) { |
| 2380 | // If the device is using preferred mixer attributes, the output need to reopen |
| 2381 | // with default configuration when the new selected devices are different from |
| 2382 | // current routing devices. |
| 2383 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices); |
| 2384 | continue; |
| 2385 | } |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2386 | setOutputDevices(desc, newDevices, force, delayMs); |
| 2387 | // re-apply device specific volume if not done by setOutputDevice() |
| 2388 | if (!force) { |
| 2389 | applyStreamVolumes(desc, newDevices.types(), delayMs); |
| 2390 | } |
| 2391 | } |
| 2392 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2393 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2394 | } |
| 2395 | } |
| 2396 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2397 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2398 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2399 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2400 | |
| 2401 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2402 | if (outputDesc == 0) { |
| 2403 | ALOGW("stopOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2404 | return BAD_VALUE; |
| 2405 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2406 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2407 | |
Jean-Michel Trivi | b1f6e64 | 2023-02-07 20:49:04 +0000 | [diff] [blame] | 2408 | if (client->hasPreferredDevice(true)) { |
| 2409 | // playback activity with preferred device impacts routing occurred, inform upper layers |
| 2410 | mpClientInterface->onRoutingUpdated(); |
| 2411 | } |
| 2412 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2413 | ALOGV("stopOutput() output %d, stream %d, session %d", |
| 2414 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2415 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2416 | status_t status = stopSource(outputDesc, client); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2417 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2418 | if (status == NO_ERROR ) { |
| 2419 | outputDesc->stop(); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2420 | } else { |
| 2421 | return status; |
| 2422 | } |
| 2423 | |
| 2424 | if (outputDesc->devices().size() == 1) { |
| 2425 | sp<PreferredMixerAttributesInfo> info = getPreferredMixerAttributesInfo( |
| 2426 | outputDesc->devices()[0]->getId(), client->strategy()); |
| 2427 | if (info != nullptr && info->getUid() == client->uid()) { |
| 2428 | info->decreaseActiveClient(); |
| 2429 | if (info->getActiveClientCount() == 0) { |
| 2430 | reopenOutput(outputDesc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 2431 | } |
| 2432 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2433 | } |
| 2434 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2435 | } |
| 2436 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2437 | status_t AudioPolicyManager::stopSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 2438 | const sp<TrackClientDescriptor>& client) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2439 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2440 | // always handle stream stop, check which stream type is stopping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2441 | audio_stream_type_t stream = client->stream(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2442 | auto clientVolSrc = client->volumeSource(); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2443 | bool wasLeUnicastActive = isLeUnicastActive(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2444 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2445 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 2446 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2447 | if (outputDesc->getActivityCount(clientVolSrc) > 0) { |
| 2448 | if (outputDesc->getActivityCount(clientVolSrc) == 1) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2449 | // Automatically disable the remote submix input when output is stopped on a |
| 2450 | // re routing mix of type MIX_TYPE_RECORDERS |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2451 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2452 | if (isSingleDeviceType( |
| 2453 | outputDesc->devices().types(), &audio_is_remote_submix_device) && |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2454 | policyMix != nullptr && |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2455 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2456 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2457 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2458 | policyMix->mDeviceAddress, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2459 | "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2460 | } |
| 2461 | } |
| 2462 | bool forceDeviceUpdate = false; |
Eric Laurent | 72af801 | 2023-03-15 17:36:22 +0100 | [diff] [blame] | 2463 | if (client->hasPreferredDevice(true) && |
| 2464 | outputDesc->sameExclusivePreferredDevicesCount() < 2) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2465 | checkStrategyRoute(client->strategy(), AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2466 | forceDeviceUpdate = true; |
| 2467 | } |
| 2468 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2469 | // decrement usage count of this stream on the output |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 2470 | outputDesc->setClientActive(client, false); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 2471 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2472 | // store time at which the stream was stopped - see isStreamActive() |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2473 | if (outputDesc->getActivityCount(clientVolSrc) == 0 || forceDeviceUpdate) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2474 | outputDesc->setStopTime(client, systemTime()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2475 | DeviceVector newDevices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 2476 | |
| 2477 | // If the routing does not change, if an output is routed on a device using HwGain |
| 2478 | // (aka setAudioPortConfig) and there are still active clients following different |
| 2479 | // volume group(s), force reapply volume |
| 2480 | bool requiresVolumeCheck = outputDesc->getActivityCount(clientVolSrc) == 0 && |
| 2481 | outputDesc->useHwGain() && outputDesc->isAnyActive(VOLUME_SOURCE_NONE); |
| 2482 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2483 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 2484 | // the track stop() command is received and at that time the audio track buffer can |
| 2485 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 2486 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 2487 | // audio path (audio DSP, CODEC ...) |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 2488 | setOutputDevices(outputDesc, newDevices, false, outputDesc->latency()*2, |
| 2489 | nullptr, true /*requiresMuteCheck*/, requiresVolumeCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2490 | |
| 2491 | // force restoring the device selection on other active outputs if it differs from the |
| 2492 | // one being selected for this output |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2493 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2494 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2495 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2496 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2497 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2498 | desc->isActive() && |
| 2499 | outputDesc->sharesHwModuleWith(desc) && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2500 | (newDevices != desc->devices())) { |
| 2501 | DeviceVector newDevices2 = getNewOutputDevices(desc, false /*fromCache*/); |
| 2502 | bool force = desc->devices() != newDevices2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 2503 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2504 | if (desc->mUsePreferredMixerAttributes && force) { |
| 2505 | // If the device is using preferred mixer attributes, the output need to |
| 2506 | // reopen with default configuration when the new selected devices are |
| 2507 | // different from current routing devices. |
| 2508 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices2); |
| 2509 | continue; |
| 2510 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2511 | setOutputDevices(desc, newDevices2, force, delayMs); |
| 2512 | |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2513 | // re-apply device specific volume if not done by setOutputDevice() |
| 2514 | if (!force) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2515 | applyStreamVolumes(desc, newDevices2.types(), delayMs); |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2516 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2517 | } |
| 2518 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2519 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2520 | // update the outputs if stopping one with a stream that can affect notification routing |
| 2521 | handleNotificationRoutingForStream(stream); |
| 2522 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2523 | |
| 2524 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 2525 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 2526 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), false, outputDesc); |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2527 | } |
| 2528 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2529 | if (followsSameRouting(client->attributes(), attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2530 | selectOutputForMusicEffects(); |
| 2531 | } |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2532 | |
| 2533 | checkLeBroadcastRoutes(wasLeUnicastActive, outputDesc, outputDesc->latency()*2); |
| 2534 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2535 | return NO_ERROR; |
| 2536 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2537 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2538 | return INVALID_OPERATION; |
| 2539 | } |
| 2540 | } |
| 2541 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2542 | bool AudioPolicyManager::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2543 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2544 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2545 | |
| 2546 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2547 | if (outputDesc == 0) { |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2548 | // If an output descriptor is closed due to a device routing change, |
| 2549 | // then there are race conditions with releaseOutput from tracks |
| 2550 | // that may be destroyed (with no PlaybackThread) or a PlaybackThread |
| 2551 | // destroyed shortly thereafter. |
| 2552 | // |
| 2553 | // Here we just log a warning, instead of a fatal error. |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2554 | ALOGW("releaseOutput() no output for client %d", portId); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2555 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2556 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2557 | |
| 2558 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2559 | |
Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2560 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
| 2561 | if (outputDesc->isClientActive(client)) { |
| 2562 | ALOGW("releaseOutput() inactivates portId %d in good faith", portId); |
| 2563 | stopOutput(portId); |
| 2564 | } |
| 2565 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2566 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 2567 | if (outputDesc->mDirectOpenCount <= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2568 | ALOGW("releaseOutput() invalid open count %d for output %d", |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2569 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2570 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2571 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2572 | if (--outputDesc->mDirectOpenCount == 0) { |
| 2573 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2574 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2575 | } |
| 2576 | } |
Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2577 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2578 | outputDesc->removeClient(portId); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2579 | if (outputDesc->mPendingReopenToQueryProfiles && outputDesc->getClientCount() == 0) { |
| 2580 | // The output is pending reopened to query dynamic profiles and |
| 2581 | // there is no active clients |
| 2582 | closeOutput(outputDesc->mIoHandle); |
| 2583 | sp<SwAudioOutputDescriptor> newOutputDesc = openOutputWithProfileAndDevice( |
| 2584 | outputDesc->mProfile, mEngine->getActiveMediaDevices(mAvailableOutputDevices)); |
| 2585 | if (newOutputDesc == nullptr) { |
| 2586 | ALOGE("%s failed to open output", __func__); |
| 2587 | } |
| 2588 | return true; |
| 2589 | } |
| 2590 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2591 | } |
| 2592 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2593 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 2594 | audio_io_handle_t *input, |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2595 | audio_unique_id_t riid, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2596 | audio_session_t session, |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2597 | const AttributionSourceState& attributionSource, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2598 | audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2599 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2600 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2601 | input_type_t *inputType, |
| 2602 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2603 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2604 | ALOGV("%s() source %d, sampling rate %d, format %#x, channel mask %#x, session %d, " |
Eric Laurent | 2f2c198 | 2021-06-02 14:03:11 +0200 | [diff] [blame] | 2605 | "flags %#x attributes=%s requested device ID %d", |
| 2606 | __func__, attr->source, config->sample_rate, config->format, config->channel_mask, |
| 2607 | session, flags, toString(*attr).c_str(), *selectedDeviceId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2608 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2609 | status_t status = NO_ERROR; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2610 | audio_attributes_t attributes = *attr; |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2611 | sp<AudioPolicyMix> policyMix; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2612 | sp<DeviceDescriptor> device; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2613 | sp<AudioInputDescriptor> inputDesc; |
| 2614 | sp<RecordClientDescriptor> clientDesc; |
| 2615 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2616 | uid_t uid = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2617 | bool isSoundTrigger; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2618 | |
| 2619 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 2620 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 2621 | return INVALID_OPERATION; |
| 2622 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2623 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2624 | if (attr->source == AUDIO_SOURCE_DEFAULT) { |
| 2625 | attributes.source = AUDIO_SOURCE_MIC; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2626 | } |
| 2627 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2628 | // Explicit routing? |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 2629 | sp<DeviceDescriptor> explicitRoutingDevice = |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2630 | mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2631 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2632 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 2633 | // possible |
| 2634 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 2635 | *input != AUDIO_IO_HANDLE_NONE) { |
| 2636 | ssize_t index = mInputs.indexOfKey(*input); |
| 2637 | if (index < 0) { |
| 2638 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 2639 | status = BAD_VALUE; |
| 2640 | goto error; |
| 2641 | } |
| 2642 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2643 | RecordClientVector clients = inputDesc->getClientsForSession(session); |
| 2644 | if (clients.size() == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2645 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 2646 | status = BAD_VALUE; |
| 2647 | goto error; |
| 2648 | } |
| 2649 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 2650 | // The second call is for the first active client and sets the UID. Any further call |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 2651 | // corresponds to a new client and is only permitted from the same UID. |
| 2652 | // If the first UID is silenced, allow a new UID connection and replace with new UID |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2653 | if (clients.size() > 1) { |
| 2654 | for (const auto& client : clients) { |
| 2655 | // The client map is ordered by key values (portId) and portIds are allocated |
| 2656 | // incrementaly. So the first client in this list is the one opened by audio flinger |
| 2657 | // when the mmap stream is created and should be ignored as it does not correspond |
| 2658 | // to an actual client |
| 2659 | if (client == *clients.cbegin()) { |
| 2660 | continue; |
| 2661 | } |
| 2662 | if (uid != client->uid() && !client->isSilenced()) { |
| 2663 | ALOGW("getInputForAttr() bad uid %d for client %d uid %d", |
| 2664 | uid, client->portId(), client->uid()); |
| 2665 | status = INVALID_OPERATION; |
| 2666 | goto error; |
| 2667 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 2668 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2669 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2670 | *inputType = API_INPUT_LEGACY; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2671 | device = inputDesc->getDevice(); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2672 | |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 2673 | ALOGV("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2674 | goto exit; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2675 | } |
| 2676 | |
| 2677 | *input = AUDIO_IO_HANDLE_NONE; |
| 2678 | *inputType = API_INPUT_INVALID; |
| 2679 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2680 | if (attributes.source == AUDIO_SOURCE_REMOTE_SUBMIX && |
Jan Sebechlebsky | bc56bcd | 2022-09-26 13:15:19 +0200 | [diff] [blame] | 2681 | extractAddressFromAudioAttributes(attributes).has_value()) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2682 | status = mPolicyMixes.getInputMixForAttr(attributes, &policyMix); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2683 | if (status != NO_ERROR) { |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2684 | ALOGW("%s could not find input mix for attr %s", |
| 2685 | __func__, toString(attributes).c_str()); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2686 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2687 | } |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2688 | device = mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2689 | String8(attr->tags + strlen("addr=")), |
| 2690 | AUDIO_FORMAT_DEFAULT); |
| 2691 | if (device == nullptr) { |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 2692 | ALOGW("%s could not find in Remote Submix device for source %d, tags %s", |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2693 | __func__, attributes.source, attributes.tags); |
| 2694 | status = BAD_VALUE; |
| 2695 | goto error; |
| 2696 | } |
| 2697 | |
Kevin Rocard | 25f9b05 | 2019-02-27 15:08:54 -0800 | [diff] [blame] | 2698 | if (is_mix_loopback_render(policyMix->mRouteFlags)) { |
| 2699 | *inputType = API_INPUT_MIX_PUBLIC_CAPTURE_PLAYBACK; |
| 2700 | } else { |
| 2701 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 2702 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2703 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2704 | if (explicitRoutingDevice != nullptr) { |
| 2705 | device = explicitRoutingDevice; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2706 | } else { |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 2707 | // Prevent from storing invalid requested device id in clients |
| 2708 | requestedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 2709 | device = mEngine->getInputDeviceForAttributes(attributes, uid, session, &policyMix); |
yuanjiahsu | 4069d5d | 2021-04-19 07:54:27 +0800 | [diff] [blame] | 2710 | ALOGV_IF(device != nullptr, "%s found device type is 0x%X", |
| 2711 | __FUNCTION__, device->type()); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2712 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2713 | if (device == nullptr) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2714 | ALOGW("getInputForAttr() could not find device for source %d", attributes.source); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2715 | status = BAD_VALUE; |
| 2716 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2717 | } |
Alden DSouza | b7d2078 | 2021-02-08 08:51:42 -0800 | [diff] [blame] | 2718 | if (device->type() == AUDIO_DEVICE_IN_ECHO_REFERENCE) { |
| 2719 | *inputType = API_INPUT_MIX_CAPTURE; |
| 2720 | } else if (policyMix) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2721 | ALOG_ASSERT(policyMix->mMixType == MIX_TYPE_RECORDERS, "Invalid Mix Type"); |
| 2722 | // there is an external policy, but this input is attached to a mix of recorders, |
| 2723 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 2724 | // know about it and is therefore considered "legacy" |
| 2725 | *inputType = API_INPUT_LEGACY; |
| 2726 | } else if (audio_is_remote_submix_device(device->type())) { |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2727 | *inputType = API_INPUT_MIX_CAPTURE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2728 | } else if (device->type() == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 2729 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2730 | } else { |
| 2731 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2732 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 2733 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2734 | } |
| 2735 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2736 | *input = getInputForDevice(device, session, attributes, config, flags, policyMix); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2737 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2738 | status = INVALID_OPERATION; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2739 | AudioProfileVector profiles; |
| 2740 | status_t ret = getProfilesForDevices( |
| 2741 | DeviceVector(device), profiles, flags, true /*isInput*/); |
| 2742 | if (ret == NO_ERROR && !profiles.empty()) { |
| 2743 | config->channel_mask = profiles[0]->getChannels().empty() ? config->channel_mask |
| 2744 | : *profiles[0]->getChannels().begin(); |
| 2745 | config->sample_rate = profiles[0]->getSampleRates().empty() ? config->sample_rate |
| 2746 | : *profiles[0]->getSampleRates().begin(); |
| 2747 | config->format = profiles[0]->getFormat(); |
| 2748 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2749 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2750 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2751 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2752 | exit: |
| 2753 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2754 | *selectedDeviceId = mAvailableInputDevices.contains(device) ? |
| 2755 | device->getId() : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2756 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2757 | isSoundTrigger = attributes.source == AUDIO_SOURCE_HOTWORD && |
Carter Hsu | d0cce2e | 2019-05-03 17:36:28 +0800 | [diff] [blame] | 2758 | mSoundTriggerSessions.indexOfKey(session) >= 0; |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 2759 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2760 | |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2761 | clientDesc = new RecordClientDescriptor(*portId, riid, uid, session, attributes, *config, |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2762 | requestedDeviceId, attributes.source, flags, |
| 2763 | isSoundTrigger); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2764 | inputDesc = mInputs.valueFor(*input); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2765 | inputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2766 | |
| 2767 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", |
| 2768 | *input, *inputType, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2769 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2770 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2771 | |
| 2772 | error: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2773 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2777 | audio_io_handle_t AudioPolicyManager::getInputForDevice(const sp<DeviceDescriptor> &device, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2778 | audio_session_t session, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2779 | const audio_attributes_t &attributes, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2780 | audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2781 | audio_input_flags_t flags, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2782 | const sp<AudioPolicyMix> &policyMix) |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2783 | { |
| 2784 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2785 | audio_source_t halInputSource = attributes.source; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2786 | bool isSoundTrigger = false; |
| 2787 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2788 | if (attributes.source == AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2789 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 2790 | if (index >= 0) { |
| 2791 | input = mSoundTriggerSessions.valueFor(session); |
| 2792 | isSoundTrigger = true; |
| 2793 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 2794 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 2795 | } else { |
| 2796 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2797 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2798 | } else if (attributes.source == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2799 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 2800 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2801 | } |
| 2802 | |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 2803 | if (attributes.source == AUDIO_SOURCE_ULTRASOUND) { |
| 2804 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_ULTRASOUND); |
| 2805 | } |
| 2806 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2807 | // sampling rate and flags may be updated by getInputProfile |
| 2808 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 2809 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 2810 | audio_format_t profileFormat = config->format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2811 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2812 | audio_input_flags_t profileFlags = flags; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 2813 | // find a compatible input profile (not necessarily identical in parameters) |
| 2814 | sp<IOProfile> profile = getInputProfile( |
| 2815 | device, profileSamplingRate, profileFormat, profileChannelMask, profileFlags); |
| 2816 | if (profile == nullptr) { |
| 2817 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2818 | } |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 2819 | |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2820 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2821 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2822 | if (samplingRate == 0) { |
| 2823 | samplingRate = profileSamplingRate; |
| 2824 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2825 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 2826 | if (profile->getModuleHandle() == 0) { |
| 2827 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2828 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2829 | } |
| 2830 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2831 | // Reuse an already opened input if a client with the same session ID already exists |
| 2832 | // on that input |
| 2833 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2834 | sp <AudioInputDescriptor> desc = mInputs.valueAt(i); |
| 2835 | if (desc->mProfile != profile) { |
| 2836 | continue; |
| 2837 | } |
| 2838 | RecordClientVector clients = desc->clientsList(); |
| 2839 | for (const auto &client : clients) { |
| 2840 | if (session == client->session()) { |
| 2841 | return desc->mIoHandle; |
| 2842 | } |
| 2843 | } |
| 2844 | } |
| 2845 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2846 | if (!profile->canOpenNewIo()) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2847 | for (size_t i = 0; i < mInputs.size(); ) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 2848 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2849 | if (desc->mProfile != profile) { |
Carter Hsu | 9a645a9 | 2019-05-15 12:15:32 +0800 | [diff] [blame] | 2850 | i++; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2851 | continue; |
| 2852 | } |
| 2853 | // if sound trigger, reuse input if used by other sound trigger on same session |
| 2854 | // else |
| 2855 | // reuse input if active client app is not in IDLE state |
| 2856 | // |
| 2857 | RecordClientVector clients = desc->clientsList(); |
| 2858 | bool doClose = false; |
| 2859 | for (const auto& client : clients) { |
| 2860 | if (isSoundTrigger != client->isSoundTrigger()) { |
| 2861 | continue; |
| 2862 | } |
| 2863 | if (client->isSoundTrigger()) { |
| 2864 | if (session == client->session()) { |
| 2865 | return desc->mIoHandle; |
| 2866 | } |
| 2867 | continue; |
| 2868 | } |
| 2869 | if (client->active() && client->appState() != APP_STATE_IDLE) { |
| 2870 | return desc->mIoHandle; |
| 2871 | } |
| 2872 | doClose = true; |
| 2873 | } |
| 2874 | if (doClose) { |
| 2875 | closeInput(desc->mIoHandle); |
| 2876 | } else { |
| 2877 | i++; |
| 2878 | } |
| 2879 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2880 | } |
| 2881 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2882 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2883 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2884 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 2885 | lConfig.sample_rate = profileSamplingRate; |
| 2886 | lConfig.channel_mask = profileChannelMask; |
| 2887 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 2888 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2889 | status_t status = inputDesc->open(&lConfig, device, halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2890 | |
| 2891 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2892 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2893 | (profileSamplingRate != lConfig.sample_rate) || |
| 2894 | !audio_formats_match(profileFormat, lConfig.format) || |
| 2895 | (profileChannelMask != lConfig.channel_mask)) { |
| 2896 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 2897 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2898 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2899 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2900 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2901 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2902 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2903 | } |
| 2904 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2905 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2906 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2907 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2908 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2909 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2910 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2911 | } |
| 2912 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2913 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId) |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 2914 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2915 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2916 | |
| 2917 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2918 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2919 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 2920 | return DEAD_OBJECT; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2921 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2922 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2923 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2924 | if (client->active()) { |
| 2925 | ALOGW("%s input %d client %d already started", __FUNCTION__, input, client->portId()); |
| 2926 | return INVALID_OPERATION; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2927 | } |
| 2928 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2929 | audio_session_t session = client->session(); |
| 2930 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2931 | ALOGV("%s input:%d, session:%d)", __FUNCTION__, input, session); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2932 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2933 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2934 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2935 | status_t status = inputDesc->start(); |
| 2936 | if (status != NO_ERROR) { |
| 2937 | return status; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2938 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2939 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2940 | // increment activity count before calling getNewInputDevice() below as only active sessions |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2941 | // are considered for device selection |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2942 | inputDesc->setClientActive(client, true); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2943 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2944 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2945 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2946 | sp<DeviceDescriptor> device = getNewInputDevice(inputDesc); |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2947 | if (device != nullptr) { |
| 2948 | status = setInputDevice(input, device, true /* force */); |
| 2949 | } else { |
| 2950 | ALOGW("%s no new input device can be found for descriptor %d", |
| 2951 | __FUNCTION__, inputDesc->getId()); |
| 2952 | status = BAD_VALUE; |
| 2953 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2954 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2955 | if (status == NO_ERROR && inputDesc->activeCount() == 1) { |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2956 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2957 | // if input maps to a dynamic policy with an activity listener, notify of state change |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2958 | if ((policyMix != nullptr) |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2959 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2960 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2961 | MIX_STATE_MIXING); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2962 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2963 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2964 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 2965 | if (primaryInputDevices.contains(device) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2966 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 2967 | mpClientInterface->setSoundTriggerCaptureState(true); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2968 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2969 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2970 | // automatically enable the remote submix output when input is started if not |
| 2971 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2972 | // For remote submix (a virtual device), we open only one input per capture request. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2973 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2974 | String8 address = String8(""); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2975 | if (policyMix == nullptr) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2976 | address = String8("0"); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2977 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2978 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2979 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2980 | if (address != "") { |
| 2981 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2982 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2983 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2984 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2985 | } |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2986 | } else if (status != NO_ERROR) { |
| 2987 | // Restore client activity state. |
| 2988 | inputDesc->setClientActive(client, false); |
| 2989 | inputDesc->stop(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2990 | } |
| 2991 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2992 | ALOGV("%s input %d source = %d status = %d exit", |
| 2993 | __FUNCTION__, input, client->source(), status); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2994 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2995 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2996 | } |
| 2997 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2998 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2999 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3000 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 3001 | |
| 3002 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 3003 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3004 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3005 | return BAD_VALUE; |
| 3006 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3007 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3008 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3009 | if (!client->active()) { |
| 3010 | ALOGW("%s input %d client %d already stopped", __FUNCTION__, input, client->portId()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3011 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3012 | } |
Carter Hsu | e6139d5 | 2021-07-08 10:30:20 +0800 | [diff] [blame] | 3013 | auto old_source = inputDesc->source(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3014 | inputDesc->setClientActive(client, false); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 3015 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3016 | inputDesc->stop(); |
| 3017 | if (inputDesc->isActive()) { |
Carter Hsu | e6139d5 | 2021-07-08 10:30:20 +0800 | [diff] [blame] | 3018 | auto current_source = inputDesc->source(); |
| 3019 | setInputDevice(input, getNewInputDevice(inputDesc), |
| 3020 | old_source != current_source /* force */); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3021 | } else { |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3022 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3023 | // if input maps to a dynamic policy with an activity listener, notify of state change |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 3024 | if ((policyMix != nullptr) |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3025 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 3026 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3027 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 3028 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3029 | |
| 3030 | // automatically disable the remote submix output when input is stopped if not |
| 3031 | // used by a policy mix of type MIX_TYPE_RECORDERS |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3032 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3033 | String8 address = String8(""); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 3034 | if (policyMix == nullptr) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3035 | address = String8("0"); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3036 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 3037 | address = policyMix->mDeviceAddress; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3038 | } |
| 3039 | if (address != "") { |
| 3040 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 3041 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 3042 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3043 | } |
| 3044 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3045 | resetInputDevice(input); |
| 3046 | |
| 3047 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 3048 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3049 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 3050 | if (primaryInputDevices.contains(inputDesc->getDevice()) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3051 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 3052 | mpClientInterface->setSoundTriggerCaptureState(false); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3053 | } |
| 3054 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3055 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3056 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3057 | } |
| 3058 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3059 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3060 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3061 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 3062 | |
| 3063 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 3064 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3065 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3066 | return; |
| 3067 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3068 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3069 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 3070 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3071 | ALOGV("%s %d", __FUNCTION__, input); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 3072 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3073 | inputDesc->removeClient(portId); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3074 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3075 | if (inputDesc->getClientCount() > 0) { |
| 3076 | ALOGV("%s(%d) %zu clients remaining", __func__, portId, inputDesc->getClientCount()); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3077 | return; |
| 3078 | } |
| 3079 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3080 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3081 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3082 | ALOGV("%s exit", __FUNCTION__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3083 | } |
| 3084 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3085 | void AudioPolicyManager::closeActiveClients(const sp<AudioInputDescriptor>& input) |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3086 | { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3087 | RecordClientVector clients = input->clientsList(true); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3088 | |
| 3089 | for (const auto& client : clients) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3090 | closeClient(client->portId()); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3091 | } |
| 3092 | } |
| 3093 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3094 | void AudioPolicyManager::closeClient(audio_port_handle_t portId) |
| 3095 | { |
| 3096 | stopInput(portId); |
| 3097 | releaseInput(portId); |
| 3098 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3099 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3100 | void AudioPolicyManager::checkCloseInputs() { |
| 3101 | // After connecting or disconnecting an input device, close input if: |
| 3102 | // - it has no client (was just opened to check profile) OR |
| 3103 | // - none of its supported devices are connected anymore OR |
| 3104 | // - one of its clients cannot be routed to one of its supported |
| 3105 | // devices anymore. Otherwise update device selection |
| 3106 | std::vector<audio_io_handle_t> inputsToClose; |
| 3107 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3108 | const sp<AudioInputDescriptor> input = mInputs.valueAt(i); |
| 3109 | if (input->clientsList().size() == 0 |
Eric Laurent | 85732f4 | 2020-03-19 11:31:10 -0700 | [diff] [blame] | 3110 | || !mAvailableInputDevices.containsAtLeastOne(input->supportedDevices())) { |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3111 | inputsToClose.push_back(mInputs.keyAt(i)); |
| 3112 | } else { |
| 3113 | bool close = false; |
| 3114 | for (const auto& client : input->clientsList()) { |
| 3115 | sp<DeviceDescriptor> device = |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 3116 | mEngine->getInputDeviceForAttributes(client->attributes(), client->uid(), |
| 3117 | client->session()); |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3118 | if (!input->supportedDevices().contains(device)) { |
| 3119 | close = true; |
| 3120 | break; |
| 3121 | } |
| 3122 | } |
| 3123 | if (close) { |
| 3124 | inputsToClose.push_back(mInputs.keyAt(i)); |
| 3125 | } else { |
| 3126 | setInputDevice(input->mIoHandle, getNewInputDevice(input)); |
| 3127 | } |
| 3128 | } |
| 3129 | } |
| 3130 | |
| 3131 | for (const audio_io_handle_t handle : inputsToClose) { |
| 3132 | ALOGV("%s closing input %d", __func__, handle); |
| 3133 | closeInput(handle); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3134 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3135 | } |
| 3136 | |
François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 3137 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, int indexMin, int indexMax) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3138 | { |
| 3139 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 3140 | if (indexMin < 0 || indexMax < 0) { |
| 3141 | ALOGE("%s for stream %d: invalid min %d or max %d", __func__, stream , indexMin, indexMax); |
| 3142 | return; |
| 3143 | } |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 3144 | getVolumeCurves(stream).initVolume(indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3145 | |
| 3146 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3147 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 3148 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3149 | continue; |
| 3150 | } |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 3151 | getVolumeCurves((audio_stream_type_t)curStream).initVolume(indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3152 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3153 | } |
| 3154 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3155 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3156 | int index, |
| 3157 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3158 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3159 | auto attributes = mEngine->getAttributesForStreamType(stream); |
Eric Laurent | 242a9f8 | 2020-03-23 15:57:04 -0700 | [diff] [blame] | 3160 | if (attributes == AUDIO_ATTRIBUTES_INITIALIZER) { |
| 3161 | ALOGW("%s: no group for stream %s, bailing out", __func__, toString(stream).c_str()); |
| 3162 | return NO_ERROR; |
| 3163 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3164 | ALOGV("%s: stream %s attributes=%s", __func__, |
| 3165 | toString(stream).c_str(), toString(attributes).c_str()); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3166 | return setVolumeIndexForAttributes(attributes, index, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3167 | } |
| 3168 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3169 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3170 | int *index, |
| 3171 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3172 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3173 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this |
| 3174 | // stream by the engine. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3175 | DeviceTypeSet deviceTypes = {device}; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 3176 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3177 | deviceTypes = mEngine->getOutputDevicesForStream( |
| 3178 | stream, true /*fromCache*/).types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3179 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3180 | return getVolumeIndex(getVolumeCurves(stream), *index, deviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3181 | } |
| 3182 | |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3183 | status_t AudioPolicyManager::setVolumeIndexForAttributes(const audio_attributes_t &attributes, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3184 | int index, |
| 3185 | audio_devices_t device) |
| 3186 | { |
| 3187 | // Get Volume group matching the Audio Attributes |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3188 | auto group = mEngine->getVolumeGroupForAttributes(attributes); |
| 3189 | if (group == VOLUME_GROUP_NONE) { |
| 3190 | ALOGD("%s: no group matching with %s", __FUNCTION__, toString(attributes).c_str()); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3191 | return BAD_VALUE; |
| 3192 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3193 | ALOGV("%s: group %d matching with %s", __FUNCTION__, group, toString(attributes).c_str()); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3194 | status_t status = NO_ERROR; |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3195 | IVolumeCurves &curves = getVolumeCurves(attributes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3196 | VolumeSource vs = toVolumeSource(group); |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3197 | // AUDIO_STREAM_BLUETOOTH_SCO is only used for volume control so we remap |
| 3198 | // to AUDIO_STREAM_VOICE_CALL to match with relevant playback activity |
| 3199 | VolumeSource activityVs = (vs == toVolumeSource(AUDIO_STREAM_BLUETOOTH_SCO, false)) ? |
| 3200 | toVolumeSource(AUDIO_STREAM_VOICE_CALL, false) : vs; |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3201 | product_strategy_t strategy = mEngine->getProductStrategyForAttributes(attributes); |
| 3202 | |
| 3203 | status = setVolumeCurveIndex(index, device, curves); |
| 3204 | if (status != NO_ERROR) { |
| 3205 | ALOGE("%s failed to set curve index for group %d device 0x%X", __func__, group, device); |
| 3206 | return status; |
| 3207 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3208 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3209 | DeviceTypeSet curSrcDevices; |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3210 | auto curCurvAttrs = curves.getAttributes(); |
| 3211 | if (!curCurvAttrs.empty() && curCurvAttrs.front() != defaultAttr) { |
| 3212 | auto attr = curCurvAttrs.front(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3213 | curSrcDevices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false).types(); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3214 | } else if (!curves.getStreamTypes().empty()) { |
| 3215 | auto stream = curves.getStreamTypes().front(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3216 | curSrcDevices = mEngine->getOutputDevicesForStream(stream, false).types(); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3217 | } else { |
| 3218 | ALOGE("%s: Invalid src %d: no valid attributes nor stream",__func__, vs); |
| 3219 | return BAD_VALUE; |
| 3220 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3221 | audio_devices_t curSrcDevice = Volume::getDeviceForVolume(curSrcDevices); |
| 3222 | resetDeviceTypes(curSrcDevices, curSrcDevice); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3223 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3224 | // update volume on all outputs and streams matching the following: |
| 3225 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 3226 | // - The device (or devices) selected by the engine for this stream includes |
| 3227 | // the requested device |
| 3228 | // - For non default requested device, currently selected device on the output is either the |
| 3229 | // requested device or one of the devices selected by the engine for this stream |
| 3230 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 3231 | // no specific device volume value exists for currently selected device. |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3232 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3233 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3234 | DeviceTypeSet curDevices = desc->devices().types(); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3235 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3236 | if (curDevices.erase(AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { |
| 3237 | curDevices.insert(AUDIO_DEVICE_OUT_SPEAKER); |
Robert Lee | 5e66e79 | 2019-04-03 18:37:15 +0800 | [diff] [blame] | 3238 | } |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3239 | |
| 3240 | if (!(desc->isActive(activityVs) || isInCallOrScreening())) { |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3241 | continue; |
| 3242 | } |
| 3243 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME && |
| 3244 | curDevices.find(device) == curDevices.end()) { |
| 3245 | continue; |
| 3246 | } |
| 3247 | bool applyVolume = false; |
| 3248 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
| 3249 | curSrcDevices.insert(device); |
| 3250 | applyVolume = (curSrcDevices.find( |
| 3251 | Volume::getDeviceForVolume(curDevices)) != curSrcDevices.end()); |
| 3252 | } else { |
| 3253 | applyVolume = !curves.hasVolumeIndexForDevice(curSrcDevice); |
| 3254 | } |
| 3255 | if (!applyVolume) { |
| 3256 | continue; // next output |
| 3257 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3258 | // Inter / intra volume group priority management: Loop on strategies arranged by priority |
| 3259 | // If a higher priority strategy is active, and the output is routed to a device with a |
| 3260 | // HW Gain management, do not change the volume |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3261 | if (desc->useHwGain()) { |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3262 | applyVolume = false; |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 3263 | // If the volume source is active with higher priority source, ensure at least Sw Muted |
| 3264 | desc->setSwMute((index == 0), vs, curves.getStreamTypes(), curDevices, 0 /*delayMs*/); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3265 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { |
| 3266 | auto activeClients = desc->clientsList(true /*activeOnly*/, productStrategy, |
| 3267 | false /*preferredDevice*/); |
| 3268 | if (activeClients.empty()) { |
| 3269 | continue; |
| 3270 | } |
| 3271 | bool isPreempted = false; |
| 3272 | bool isHigherPriority = productStrategy < strategy; |
| 3273 | for (const auto &client : activeClients) { |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3274 | if (isHigherPriority && (client->volumeSource() != activityVs)) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3275 | ALOGV("%s: Strategy=%d (\nrequester:\n" |
| 3276 | " group %d, volumeGroup=%d attributes=%s)\n" |
| 3277 | " higher priority source active:\n" |
| 3278 | " volumeGroup=%d attributes=%s) \n" |
| 3279 | " on output %zu, bailing out", __func__, productStrategy, |
| 3280 | group, group, toString(attributes).c_str(), |
| 3281 | client->volumeSource(), toString(client->attributes()).c_str(), i); |
| 3282 | applyVolume = false; |
| 3283 | isPreempted = true; |
| 3284 | break; |
| 3285 | } |
| 3286 | // However, continue for loop to ensure no higher prio clients running on output |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3287 | if (client->volumeSource() == activityVs) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3288 | applyVolume = true; |
| 3289 | } |
| 3290 | } |
| 3291 | if (isPreempted || applyVolume) { |
| 3292 | break; |
| 3293 | } |
| 3294 | } |
| 3295 | if (!applyVolume) { |
| 3296 | continue; // next output |
| 3297 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3298 | } |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3299 | //FIXME: workaround for truncated touch sounds |
| 3300 | // delayed volume change for system stream to be removed when the problem is |
| 3301 | // handled by system UI |
| 3302 | status_t volStatus = checkAndSetVolume( |
| 3303 | curves, vs, index, desc, curDevices, |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3304 | ((vs == toVolumeSource(AUDIO_STREAM_SYSTEM, false))? |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3305 | TOUCH_SOUND_FIXED_DELAY_MS : 0)); |
| 3306 | if (volStatus != NO_ERROR) { |
| 3307 | status = volStatus; |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3308 | } |
| 3309 | } |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3310 | mpClientInterface->onAudioVolumeGroupChanged(group, 0 /*flags*/); |
| 3311 | return status; |
| 3312 | } |
| 3313 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3314 | status_t AudioPolicyManager::setVolumeCurveIndex(int index, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3315 | audio_devices_t device, |
| 3316 | IVolumeCurves &volumeCurves) |
| 3317 | { |
| 3318 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 3319 | // app that has MODIFY_PHONE_STATE permission. |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3320 | bool hasVoice = hasVoiceStream(volumeCurves.getStreamTypes()); |
| 3321 | if (((index < volumeCurves.getVolumeIndexMin()) && !(hasVoice && index == 0)) || |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3322 | (index > volumeCurves.getVolumeIndexMax())) { |
| 3323 | ALOGD("%s: wrong index %d min=%d max=%d", __FUNCTION__, index, |
| 3324 | volumeCurves.getVolumeIndexMin(), volumeCurves.getVolumeIndexMax()); |
| 3325 | return BAD_VALUE; |
| 3326 | } |
| 3327 | if (!audio_is_output_device(device)) { |
| 3328 | return BAD_VALUE; |
| 3329 | } |
| 3330 | |
| 3331 | // Force max volume if stream cannot be muted |
| 3332 | if (!volumeCurves.canBeMuted()) index = volumeCurves.getVolumeIndexMax(); |
| 3333 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3334 | ALOGV("%s device %08x, index %d", __FUNCTION__ , device, index); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3335 | volumeCurves.addCurrentVolumeIndex(device, index); |
| 3336 | return NO_ERROR; |
| 3337 | } |
| 3338 | |
| 3339 | status_t AudioPolicyManager::getVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3340 | int &index, |
| 3341 | audio_devices_t device) |
| 3342 | { |
| 3343 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this |
| 3344 | // stream by the engine. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3345 | DeviceTypeSet deviceTypes = {device}; |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3346 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Mikhail Naganov | bb990f2 | 2022-06-15 00:46:43 +0000 | [diff] [blame] | 3347 | deviceTypes = mEngine->getOutputDevicesForAttributes( |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3348 | attr, nullptr, true /*fromCache*/).types(); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3349 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3350 | return getVolumeIndex(getVolumeCurves(attr), index, deviceTypes); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | status_t AudioPolicyManager::getVolumeIndex(const IVolumeCurves &curves, |
| 3354 | int &index, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3355 | const DeviceTypeSet& deviceTypes) const |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3356 | { |
Mikhail Naganov | bb990f2 | 2022-06-15 00:46:43 +0000 | [diff] [blame] | 3357 | if (!isSingleDeviceType(deviceTypes, audio_is_output_device)) { |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3358 | return BAD_VALUE; |
| 3359 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3360 | index = curves.getVolumeIndex(deviceTypes); |
| 3361 | ALOGV("%s: device %s index %d", __FUNCTION__, dumpDeviceTypes(deviceTypes).c_str(), index); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3362 | return NO_ERROR; |
| 3363 | } |
| 3364 | |
| 3365 | status_t AudioPolicyManager::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3366 | int &index) |
| 3367 | { |
| 3368 | index = getVolumeCurves(attr).getVolumeIndexMin(); |
| 3369 | return NO_ERROR; |
| 3370 | } |
| 3371 | |
| 3372 | status_t AudioPolicyManager::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3373 | int &index) |
| 3374 | { |
| 3375 | index = getVolumeCurves(attr).getVolumeIndexMax(); |
| 3376 | return NO_ERROR; |
| 3377 | } |
| 3378 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3379 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3380 | { |
| 3381 | // select one output among several suitable for global effects. |
| 3382 | // The priority is as follows: |
| 3383 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 3384 | // AudioFlinger will invalidate the track and the offloaded output |
| 3385 | // will be closed causing the effect to be moved to a PCM output. |
| 3386 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3387 | // 3: The primary output |
| 3388 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3389 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3390 | DeviceVector devices = mEngine->getOutputDevicesForAttributes( |
| 3391 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3392 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3393 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3394 | if (outputs.size() == 0) { |
| 3395 | return AUDIO_IO_HANDLE_NONE; |
| 3396 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3397 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3398 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3399 | bool activeOnly = true; |
| 3400 | |
| 3401 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 3402 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 3403 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 3404 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 3405 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3406 | for (audio_io_handle_t output : outputs) { |
| 3407 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 3408 | if (activeOnly && !desc->isActive(toVolumeSource(AUDIO_STREAM_MUSIC))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3409 | continue; |
| 3410 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3411 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 3412 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3413 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3414 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3415 | } |
| 3416 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3417 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3418 | } |
| 3419 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3420 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3421 | } |
| 3422 | } |
| 3423 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 3424 | output = outputOffloaded; |
| 3425 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 3426 | output = outputDeepBuffer; |
| 3427 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 3428 | output = outputPrimary; |
| 3429 | } else { |
| 3430 | output = outputs[0]; |
| 3431 | } |
| 3432 | activeOnly = false; |
| 3433 | } |
| 3434 | |
| 3435 | if (output != mMusicEffectOutput) { |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3436 | mEffects.moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3437 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 3438 | mMusicEffectOutput = output; |
| 3439 | } |
| 3440 | |
| 3441 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3442 | return output; |
| 3443 | } |
| 3444 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3445 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 3446 | { |
| 3447 | return selectOutputForMusicEffects(); |
| 3448 | } |
| 3449 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3450 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3451 | audio_io_handle_t io, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 3452 | product_strategy_t strategy, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3453 | int session, |
| 3454 | int id) |
| 3455 | { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3456 | if (session != AUDIO_SESSION_DEVICE) { |
| 3457 | ssize_t index = mOutputs.indexOfKey(io); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3458 | if (index < 0) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3459 | index = mInputs.indexOfKey(io); |
| 3460 | if (index < 0) { |
| 3461 | ALOGW("registerEffect() unknown io %d", io); |
| 3462 | return INVALID_OPERATION; |
| 3463 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3464 | } |
| 3465 | } |
Eric Laurent | bf8f69f | 2022-03-25 17:48:38 +0100 | [diff] [blame] | 3466 | bool isMusicEffect = (session != AUDIO_SESSION_OUTPUT_STAGE) |
| 3467 | && ((strategy == streamToStrategy(AUDIO_STREAM_MUSIC) |
| 3468 | || strategy == PRODUCT_STRATEGY_NONE)); |
| 3469 | return mEffects.registerEffect(desc, io, session, id, isMusicEffect); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3470 | } |
| 3471 | |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 3472 | status_t AudioPolicyManager::unregisterEffect(int id) |
| 3473 | { |
| 3474 | if (mEffects.getEffect(id) == nullptr) { |
| 3475 | return INVALID_OPERATION; |
| 3476 | } |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 3477 | if (mEffects.isEffectEnabled(id)) { |
| 3478 | ALOGW("%s effect %d enabled", __FUNCTION__, id); |
| 3479 | setEffectEnabled(id, false); |
| 3480 | } |
| 3481 | return mEffects.unregisterEffect(id); |
| 3482 | } |
| 3483 | |
| 3484 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) |
| 3485 | { |
| 3486 | sp<EffectDescriptor> effect = mEffects.getEffect(id); |
| 3487 | if (effect == nullptr) { |
| 3488 | return INVALID_OPERATION; |
| 3489 | } |
| 3490 | |
| 3491 | status_t status = mEffects.setEffectEnabled(id, enabled); |
| 3492 | if (status == NO_ERROR) { |
| 3493 | mInputs.trackEffectEnabled(effect, enabled); |
| 3494 | } |
| 3495 | return status; |
| 3496 | } |
| 3497 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3498 | |
| 3499 | status_t AudioPolicyManager::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) |
| 3500 | { |
| 3501 | mEffects.moveEffects(ids, io); |
| 3502 | return NO_ERROR; |
| 3503 | } |
| 3504 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3505 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 3506 | { |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3507 | auto vs = toVolumeSource(stream, false); |
| 3508 | return vs != VOLUME_SOURCE_NONE ? mOutputs.isActive(vs, inPastMs) : false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3509 | } |
| 3510 | |
| 3511 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 3512 | { |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3513 | auto vs = toVolumeSource(stream, false); |
| 3514 | return vs != VOLUME_SOURCE_NONE ? mOutputs.isActiveRemotely(vs, inPastMs) : false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3515 | } |
| 3516 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3517 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3518 | { |
| 3519 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3520 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3521 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3522 | return true; |
| 3523 | } |
| 3524 | } |
| 3525 | return false; |
| 3526 | } |
| 3527 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3528 | // Register a list of custom mixes with their attributes and format. |
| 3529 | // When a mix is registered, corresponding input and output profiles are |
| 3530 | // added to the remote submix hw module. The profile contains only the |
| 3531 | // parameters (sampling rate, format...) specified by the mix. |
| 3532 | // The corresponding input remote submix device is also connected. |
| 3533 | // |
| 3534 | // When a remote submix device is connected, the address is checked to select the |
| 3535 | // appropriate profile and the corresponding input or output stream is opened. |
| 3536 | // |
| 3537 | // When capture starts, getInputForAttr() will: |
| 3538 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 3539 | // - 2 if none found, getDeviceForInputSource() will: |
| 3540 | // - 2.1 look for a mix matching the attributes source |
| 3541 | // - 2.2 if none found, default to device selection by policy rules |
| 3542 | // At this time, the corresponding output remote submix device is also connected |
| 3543 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 3544 | // after AudioTracks are invalidated |
| 3545 | // |
| 3546 | // When playback starts, getOutputForAttr() will: |
| 3547 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 3548 | // - 2 if none found, look for a mix matching the attributes usage |
| 3549 | // - 3 if none found, default to device and output selection by policy rules. |
| 3550 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3551 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3552 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3553 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 3554 | status_t res = NO_ERROR; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3555 | bool checkOutputs = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3556 | sp<HwModule> rSubmixModule; |
| 3557 | // examine each mix's route type |
| 3558 | for (size_t i = 0; i < mixes.size(); i++) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3559 | AudioMix mix = mixes[i]; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3560 | // Only capture of playback is allowed in LOOP_BACK & RENDER mode |
| 3561 | if (is_mix_loopback_render(mix.mRouteFlags) && mix.mMixType != MIX_TYPE_PLAYERS) { |
| 3562 | ALOGE("Unsupported Policy Mix %zu of %zu: " |
| 3563 | "Only capture of playback is allowed in LOOP_BACK & RENDER mode", |
| 3564 | i, mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3565 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3566 | break; |
| 3567 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3568 | // LOOP_BACK and LOOP_BACK | RENDER have the same remote submix backend and are handled |
| 3569 | // in the same way. |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3570 | if ((mix.mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3571 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK %d", i, mixes.size(), |
| 3572 | mix.mRouteFlags); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3573 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3574 | rSubmixModule = mHwModules.getModuleFromName( |
| 3575 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 3576 | if (rSubmixModule == 0) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3577 | ALOGE("Unable to find audio module for submix, aborting mix %zu registration", |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3578 | i); |
| 3579 | res = INVALID_OPERATION; |
| 3580 | break; |
| 3581 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3582 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3583 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3584 | String8 address = mix.mDeviceAddress; |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3585 | audio_devices_t deviceTypeToMakeAvailable; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3586 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3587 | mix.mDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3588 | deviceTypeToMakeAvailable = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 3589 | } else { |
| 3590 | mix.mDeviceType = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 3591 | deviceTypeToMakeAvailable = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3592 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3593 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3594 | if (mPolicyMixes.registerMix(mix, 0 /*output desc*/) != NO_ERROR) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3595 | ALOGE("Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3596 | res = INVALID_OPERATION; |
| 3597 | break; |
| 3598 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3599 | audio_config_t outputConfig = mix.mFormat; |
| 3600 | audio_config_t inputConfig = mix.mFormat; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3601 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL |
| 3602 | // in stereo and let audio flinger do the channel conversion if needed. |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3603 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 3604 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3605 | rSubmixModule->addOutputProfile(address.c_str(), &outputConfig, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3606 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3607 | rSubmixModule->addInputProfile(address.c_str(), &inputConfig, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3608 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3609 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3610 | if ((res = setDeviceConnectionStateInt(deviceTypeToMakeAvailable, |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 3611 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3612 | address.string(), "remote-submix", AUDIO_FORMAT_DEFAULT)) != NO_ERROR) { |
| 3613 | ALOGE("Failed to set remote submix device available, type %u, address %s", |
| 3614 | mix.mDeviceType, address.string()); |
| 3615 | break; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3616 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3617 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 3618 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3619 | audio_devices_t type = mix.mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3620 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3621 | i, mixes.size(), type, address.string()); |
| 3622 | |
| 3623 | sp<DeviceDescriptor> device = mHwModules.getDeviceDescriptor( |
| 3624 | mix.mDeviceType, mix.mDeviceAddress, |
| 3625 | String8(), AUDIO_FORMAT_DEFAULT); |
| 3626 | if (device == nullptr) { |
| 3627 | res = INVALID_OPERATION; |
| 3628 | break; |
| 3629 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3630 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3631 | bool foundOutput = false; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3632 | // First try to find an already opened output supporting the device |
| 3633 | for (size_t j = 0 ; j < mOutputs.size() && !foundOutput && res == NO_ERROR; j++) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3634 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3635 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3636 | if (!desc->isDuplicated() && desc->supportedDevices().contains(device)) { |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3637 | if (mPolicyMixes.registerMix(mix, desc) != NO_ERROR) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3638 | ALOGE("Could not register mix RENDER, dev=0x%X addr=%s", type, |
| 3639 | address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3640 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3641 | } else { |
| 3642 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3643 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3644 | } |
| 3645 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3646 | // If no output found, try to find a direct output profile supporting the device |
| 3647 | for (size_t i = 0; i < mHwModules.size() && !foundOutput && res == NO_ERROR; i++) { |
| 3648 | sp<HwModule> module = mHwModules[i]; |
| 3649 | for (size_t j = 0; |
| 3650 | j < module->getOutputProfiles().size() && !foundOutput && res == NO_ERROR; |
| 3651 | j++) { |
| 3652 | sp<IOProfile> profile = module->getOutputProfiles()[j]; |
| 3653 | if (profile->isDirectOutput() && profile->supportsDevice(device)) { |
| 3654 | if (mPolicyMixes.registerMix(mix, nullptr) != NO_ERROR) { |
| 3655 | ALOGE("Could not register mix RENDER, dev=0x%X addr=%s", type, |
| 3656 | address.string()); |
| 3657 | res = INVALID_OPERATION; |
| 3658 | } else { |
| 3659 | foundOutput = true; |
| 3660 | } |
| 3661 | } |
| 3662 | } |
| 3663 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3664 | if (res != NO_ERROR) { |
| 3665 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3666 | i, type, address.string()); |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3667 | res = INVALID_OPERATION; |
| 3668 | break; |
| 3669 | } else if (!foundOutput) { |
| 3670 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3671 | i, type, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3672 | res = INVALID_OPERATION; |
| 3673 | break; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3674 | } else { |
| 3675 | checkOutputs = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3676 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3677 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3678 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3679 | if (res != NO_ERROR) { |
| 3680 | unregisterPolicyMixes(mixes); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3681 | } else if (checkOutputs) { |
| 3682 | checkForDeviceAndOutputChanges(); |
| 3683 | updateCallAndOutputRouting(); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3684 | } |
| 3685 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3686 | } |
| 3687 | |
| 3688 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 3689 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3690 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3691 | status_t res = NO_ERROR; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3692 | bool checkOutputs = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3693 | sp<HwModule> rSubmixModule; |
| 3694 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3695 | for (const auto& mix : mixes) { |
| 3696 | if ((mix.mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3697 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3698 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3699 | rSubmixModule = mHwModules.getModuleFromName( |
| 3700 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 3701 | if (rSubmixModule == 0) { |
| 3702 | res = INVALID_OPERATION; |
| 3703 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3704 | } |
| 3705 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3706 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3707 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3708 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3709 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3710 | res = INVALID_OPERATION; |
| 3711 | continue; |
| 3712 | } |
| 3713 | |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 3714 | for (auto device : {AUDIO_DEVICE_IN_REMOTE_SUBMIX, AUDIO_DEVICE_OUT_REMOTE_SUBMIX}) { |
| 3715 | if (getDeviceConnectionState(device, address.string()) == |
| 3716 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 3717 | res = setDeviceConnectionStateInt(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3718 | address.string(), "remote-submix", |
| 3719 | AUDIO_FORMAT_DEFAULT); |
| 3720 | if (res != OK) { |
| 3721 | ALOGE("Error making RemoteSubmix device unavailable for mix " |
| 3722 | "with type %d, address %s", device, address.string()); |
| 3723 | } |
| 3724 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3725 | } |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3726 | rSubmixModule->removeOutputProfile(address.c_str()); |
| 3727 | rSubmixModule->removeInputProfile(address.c_str()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3728 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3729 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3730 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3731 | res = INVALID_OPERATION; |
| 3732 | continue; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3733 | } else { |
| 3734 | checkOutputs = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3735 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3736 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3737 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3738 | if (res == NO_ERROR && checkOutputs) { |
| 3739 | checkForDeviceAndOutputChanges(); |
| 3740 | updateCallAndOutputRouting(); |
| 3741 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3742 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3743 | } |
| 3744 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3745 | void AudioPolicyManager::dumpManualSurroundFormats(String8 *dst) const |
| 3746 | { |
| 3747 | size_t i = 0; |
| 3748 | constexpr size_t audioFormatPrefixLen = sizeof("AUDIO_FORMAT_"); |
| 3749 | for (const auto& fmt : mManualSurroundFormats) { |
| 3750 | if (i++ != 0) dst->append(", "); |
| 3751 | std::string sfmt; |
| 3752 | FormatConverter::toString(fmt, sfmt); |
| 3753 | dst->append(sfmt.size() >= audioFormatPrefixLen ? |
| 3754 | sfmt.c_str() + audioFormatPrefixLen - 1 : sfmt.c_str()); |
| 3755 | } |
| 3756 | } |
| 3757 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3758 | // Returns true if all devices types match the predicate and are supported by one HW module |
| 3759 | bool AudioPolicyManager::areAllDevicesSupported( |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3760 | const AudioDeviceTypeAddrVector& devices, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3761 | std::function<bool(audio_devices_t)> predicate, |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 3762 | const char *context, |
| 3763 | bool matchAddress) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3764 | for (size_t i = 0; i < devices.size(); i++) { |
| 3765 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor( |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3766 | devices[i].mType, devices[i].getAddress(), String8(), |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 3767 | AUDIO_FORMAT_DEFAULT, false /*allowToCreate*/, matchAddress); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3768 | if (devDesc == nullptr || (predicate != nullptr && !predicate(devices[i].mType))) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3769 | ALOGE("%s: device type %#x address %s not supported or not match predicate", |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3770 | context, devices[i].mType, devices[i].getAddress()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3771 | return false; |
| 3772 | } |
| 3773 | } |
| 3774 | return true; |
| 3775 | } |
| 3776 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3777 | status_t AudioPolicyManager::setUidDeviceAffinities(uid_t uid, |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3778 | const AudioDeviceTypeAddrVector& devices) { |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3779 | ALOGV("%s() uid=%d num devices %zu", __FUNCTION__, uid, devices.size()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3780 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
| 3781 | return BAD_VALUE; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3782 | } |
| 3783 | status_t res = mPolicyMixes.setUidDeviceAffinities(uid, devices); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3784 | if (res != NO_ERROR) { |
| 3785 | ALOGE("%s() Could not set all device affinities for uid = %d", __FUNCTION__, uid); |
| 3786 | return res; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3787 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3788 | |
| 3789 | checkForDeviceAndOutputChanges(); |
| 3790 | updateCallAndOutputRouting(); |
| 3791 | |
| 3792 | return NO_ERROR; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3793 | } |
| 3794 | |
| 3795 | status_t AudioPolicyManager::removeUidDeviceAffinities(uid_t uid) { |
| 3796 | ALOGV("%s() uid=%d", __FUNCTION__, uid); |
Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 3797 | status_t res = mPolicyMixes.removeUidDeviceAffinities(uid); |
| 3798 | if (res != NO_ERROR) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3799 | ALOGE("%s() Could not remove all device affinities for uid = %d", |
Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 3800 | __FUNCTION__, uid); |
| 3801 | return INVALID_OPERATION; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3802 | } |
| 3803 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3804 | checkForDeviceAndOutputChanges(); |
| 3805 | updateCallAndOutputRouting(); |
| 3806 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3807 | return res; |
| 3808 | } |
| 3809 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3810 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3811 | status_t AudioPolicyManager::setDevicesRoleForStrategy(product_strategy_t strategy, |
| 3812 | device_role_t role, |
| 3813 | const AudioDeviceTypeAddrVector &devices) { |
| 3814 | ALOGV("%s() strategy=%d role=%d %s", __func__, strategy, role, |
| 3815 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3816 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3817 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3818 | return BAD_VALUE; |
| 3819 | } |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3820 | status_t status = mEngine->setDevicesRoleForStrategy(strategy, role, devices); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3821 | if (status != NO_ERROR) { |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3822 | ALOGW("Engine could not set preferred devices %s for strategy %d role %d", |
| 3823 | dumpAudioDeviceTypeAddrVector(devices).c_str(), strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3824 | return status; |
| 3825 | } |
| 3826 | |
| 3827 | checkForDeviceAndOutputChanges(); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3828 | |
| 3829 | bool forceVolumeReeval = false; |
| 3830 | // FIXME: workaround for truncated touch sounds |
| 3831 | // to be removed when the problem is handled by system UI |
| 3832 | uint32_t delayMs = 0; |
| 3833 | if (strategy == mCommunnicationStrategy) { |
| 3834 | forceVolumeReeval = true; |
| 3835 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 3836 | updateInputRouting(); |
| 3837 | } |
| 3838 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3839 | |
| 3840 | return NO_ERROR; |
| 3841 | } |
| 3842 | |
| 3843 | void AudioPolicyManager::updateCallAndOutputRouting(bool forceVolumeReeval, uint32_t delayMs) |
| 3844 | { |
| 3845 | uint32_t waitMs = 0; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 3846 | bool wasLeUnicastActive = isLeUnicastActive(); |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 3847 | if (updateCallRouting(true /*fromCache*/, delayMs, &waitMs) == NO_ERROR) { |
Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 3848 | // Only apply special touch sound delay once |
| 3849 | delayMs = 0; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3850 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 3851 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3852 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3853 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 3854 | DeviceVector newDevices = getNewOutputDevices(outputDesc, true /*fromCache*/); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 3855 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || |
| 3856 | (outputDesc != mPrimaryOutput && !isTelephonyRxOrTx(outputDesc))) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3857 | // As done in setDeviceConnectionState, we could also fix default device issue by |
| 3858 | // preventing the force re-routing in case of default dev that distinguishes on address. |
| 3859 | // Let's give back to engine full device choice decision however. |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 3860 | bool forceRouting = !newDevices.isEmpty(); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 3861 | if (outputDesc->mUsePreferredMixerAttributes && newDevices != outputDesc->devices()) { |
| 3862 | // If the device is using preferred mixer attributes, the output need to reopen |
| 3863 | // with default configuration when the new selected devices are different from |
| 3864 | // current routing devices. |
| 3865 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices); |
| 3866 | continue; |
| 3867 | } |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 3868 | waitMs = setOutputDevices(outputDesc, newDevices, forceRouting, delayMs, nullptr, |
| 3869 | true /*requiresMuteCheck*/, |
| 3870 | !forceRouting /*requiresVolumeCheck*/); |
Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 3871 | // Only apply special touch sound delay once |
| 3872 | delayMs = 0; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3873 | } |
| 3874 | if (forceVolumeReeval && !newDevices.isEmpty()) { |
| 3875 | applyStreamVolumes(outputDesc, newDevices.types(), waitMs, true); |
| 3876 | } |
| 3877 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 3878 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 3879 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3880 | } |
| 3881 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3882 | void AudioPolicyManager::updateInputRouting() { |
| 3883 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Jaideep Sharma | 408349a | 2020-11-27 14:47:17 +0530 | [diff] [blame] | 3884 | // Skip for hotword recording as the input device switch |
| 3885 | // is handled within sound trigger HAL |
| 3886 | if (activeDesc->isSoundTrigger() && activeDesc->source() == AUDIO_SOURCE_HOTWORD) { |
| 3887 | continue; |
| 3888 | } |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3889 | auto newDevice = getNewInputDevice(activeDesc); |
| 3890 | // Force new input selection if the new device can not be reached via current input |
| 3891 | if (activeDesc->mProfile->getSupportedDevices().contains(newDevice)) { |
| 3892 | setInputDevice(activeDesc->mIoHandle, newDevice); |
| 3893 | } else { |
| 3894 | closeInput(activeDesc->mIoHandle); |
| 3895 | } |
| 3896 | } |
| 3897 | } |
| 3898 | |
Paul Wang | 5d7cdb5 | 2022-11-22 09:45:06 +0000 | [diff] [blame] | 3899 | status_t |
| 3900 | AudioPolicyManager::removeDevicesRoleForStrategy(product_strategy_t strategy, |
| 3901 | device_role_t role, |
| 3902 | const AudioDeviceTypeAddrVector &devices) { |
| 3903 | ALOGV("%s() strategy=%d role=%d %s", __func__, strategy, role, |
| 3904 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3905 | |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 3906 | if (!areAllDevicesSupported( |
| 3907 | devices, audio_is_output_device, __func__, /*matchAddress*/false)) { |
Paul Wang | 5d7cdb5 | 2022-11-22 09:45:06 +0000 | [diff] [blame] | 3908 | return BAD_VALUE; |
| 3909 | } |
| 3910 | status_t status = mEngine->removeDevicesRoleForStrategy(strategy, role, devices); |
| 3911 | if (status != NO_ERROR) { |
| 3912 | ALOGW("Engine could not remove devices %s for strategy %d role %d", |
| 3913 | dumpAudioDeviceTypeAddrVector(devices).c_str(), strategy, role); |
| 3914 | return status; |
| 3915 | } |
| 3916 | |
| 3917 | checkForDeviceAndOutputChanges(); |
| 3918 | |
| 3919 | bool forceVolumeReeval = false; |
| 3920 | // TODO(b/263479999): workaround for truncated touch sounds |
| 3921 | // to be removed when the problem is handled by system UI |
| 3922 | uint32_t delayMs = 0; |
| 3923 | if (strategy == mCommunnicationStrategy) { |
| 3924 | forceVolumeReeval = true; |
| 3925 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 3926 | updateInputRouting(); |
| 3927 | } |
| 3928 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
| 3929 | |
| 3930 | return NO_ERROR; |
| 3931 | } |
| 3932 | |
| 3933 | status_t AudioPolicyManager::clearDevicesRoleForStrategy(product_strategy_t strategy, |
| 3934 | device_role_t role) |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3935 | { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3936 | ALOGV("%s() strategy=%d role=%d", __func__, strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3937 | |
Paul Wang | 5d7cdb5 | 2022-11-22 09:45:06 +0000 | [diff] [blame] | 3938 | status_t status = mEngine->clearDevicesRoleForStrategy(strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3939 | if (status != NO_ERROR) { |
Eric Laurent | 9ae44f3 | 2022-12-14 16:17:02 +0100 | [diff] [blame] | 3940 | ALOGW_IF(status != NAME_NOT_FOUND, |
| 3941 | "Engine could not remove device role for strategy %d status %d", |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3942 | strategy, status); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3943 | return status; |
| 3944 | } |
| 3945 | |
| 3946 | checkForDeviceAndOutputChanges(); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3947 | |
| 3948 | bool forceVolumeReeval = false; |
| 3949 | // FIXME: workaround for truncated touch sounds |
| 3950 | // to be removed when the problem is handled by system UI |
| 3951 | uint32_t delayMs = 0; |
| 3952 | if (strategy == mCommunnicationStrategy) { |
| 3953 | forceVolumeReeval = true; |
| 3954 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 3955 | updateInputRouting(); |
| 3956 | } |
| 3957 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3958 | |
| 3959 | return NO_ERROR; |
| 3960 | } |
| 3961 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3962 | status_t AudioPolicyManager::getDevicesForRoleAndStrategy(product_strategy_t strategy, |
| 3963 | device_role_t role, |
| 3964 | AudioDeviceTypeAddrVector &devices) { |
| 3965 | return mEngine->getDevicesForRoleAndStrategy(strategy, role, devices); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3966 | } |
| 3967 | |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3968 | status_t AudioPolicyManager::setDevicesRoleForCapturePreset( |
| 3969 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { |
| 3970 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, |
| 3971 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3972 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3973 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3974 | return BAD_VALUE; |
| 3975 | } |
| 3976 | status_t status = mEngine->setDevicesRoleForCapturePreset(audioSource, role, devices); |
| 3977 | ALOGW_IF(status != NO_ERROR, |
| 3978 | "Engine could not set preferred devices %s for audio source %d role %d", |
| 3979 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); |
| 3980 | |
| 3981 | return status; |
| 3982 | } |
| 3983 | |
| 3984 | status_t AudioPolicyManager::addDevicesRoleForCapturePreset( |
| 3985 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { |
| 3986 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, |
| 3987 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3988 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3989 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3990 | return BAD_VALUE; |
| 3991 | } |
| 3992 | status_t status = mEngine->addDevicesRoleForCapturePreset(audioSource, role, devices); |
| 3993 | ALOGW_IF(status != NO_ERROR, |
| 3994 | "Engine could not add preferred devices %s for audio source %d role %d", |
| 3995 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); |
| 3996 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3997 | updateInputRouting(); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3998 | return status; |
| 3999 | } |
| 4000 | |
| 4001 | status_t AudioPolicyManager::removeDevicesRoleForCapturePreset( |
| 4002 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) |
| 4003 | { |
| 4004 | ALOGV("%s() audioSource=%d role=%d devices=%s", __func__, audioSource, role, |
| 4005 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 4006 | |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 4007 | if (!areAllDevicesSupported( |
| 4008 | devices, audio_call_is_input_device, __func__, /*matchAddress*/false)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4009 | return BAD_VALUE; |
| 4010 | } |
| 4011 | |
| 4012 | status_t status = mEngine->removeDevicesRoleForCapturePreset( |
| 4013 | audioSource, role, devices); |
Eric Laurent | 9ae44f3 | 2022-12-14 16:17:02 +0100 | [diff] [blame] | 4014 | ALOGW_IF(status != NO_ERROR && status != NAME_NOT_FOUND, |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4015 | "Engine could not remove devices role (%d) for capture preset %d", role, audioSource); |
Eric Laurent | 9ae44f3 | 2022-12-14 16:17:02 +0100 | [diff] [blame] | 4016 | if (status == NO_ERROR) { |
| 4017 | updateInputRouting(); |
| 4018 | } |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4019 | return status; |
| 4020 | } |
| 4021 | |
| 4022 | status_t AudioPolicyManager::clearDevicesRoleForCapturePreset(audio_source_t audioSource, |
| 4023 | device_role_t role) { |
| 4024 | ALOGV("%s() audioSource=%d role=%d", __func__, audioSource, role); |
| 4025 | |
| 4026 | status_t status = mEngine->clearDevicesRoleForCapturePreset(audioSource, role); |
Eric Laurent | 9ae44f3 | 2022-12-14 16:17:02 +0100 | [diff] [blame] | 4027 | ALOGW_IF(status != NO_ERROR && status != NAME_NOT_FOUND, |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4028 | "Engine could not clear devices role (%d) for capture preset %d", role, audioSource); |
Eric Laurent | 9ae44f3 | 2022-12-14 16:17:02 +0100 | [diff] [blame] | 4029 | if (status == NO_ERROR) { |
| 4030 | updateInputRouting(); |
| 4031 | } |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4032 | return status; |
| 4033 | } |
| 4034 | |
| 4035 | status_t AudioPolicyManager::getDevicesForRoleAndCapturePreset( |
| 4036 | audio_source_t audioSource, device_role_t role, AudioDeviceTypeAddrVector &devices) { |
| 4037 | return mEngine->getDevicesForRoleAndCapturePreset(audioSource, role, devices); |
| 4038 | } |
| 4039 | |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4040 | status_t AudioPolicyManager::setUserIdDeviceAffinities(int userId, |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 4041 | const AudioDeviceTypeAddrVector& devices) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 4042 | ALOGV("%s() userId=%d num devices %zu", __func__, userId, devices.size()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4043 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
| 4044 | return BAD_VALUE; |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4045 | } |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4046 | status_t status = mPolicyMixes.setUserIdDeviceAffinities(userId, devices); |
| 4047 | if (status != NO_ERROR) { |
| 4048 | ALOGE("%s() could not set device affinity for userId %d", |
| 4049 | __FUNCTION__, userId); |
| 4050 | return status; |
| 4051 | } |
| 4052 | |
| 4053 | // reevaluate outputs for all devices |
| 4054 | checkForDeviceAndOutputChanges(); |
| 4055 | updateCallAndOutputRouting(); |
| 4056 | |
| 4057 | return NO_ERROR; |
| 4058 | } |
| 4059 | |
| 4060 | status_t AudioPolicyManager::removeUserIdDeviceAffinities(int userId) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 4061 | ALOGV("%s() userId=%d", __FUNCTION__, userId); |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4062 | status_t status = mPolicyMixes.removeUserIdDeviceAffinities(userId); |
| 4063 | if (status != NO_ERROR) { |
| 4064 | ALOGE("%s() Could not remove all device affinities fo userId = %d", |
| 4065 | __FUNCTION__, userId); |
| 4066 | return status; |
| 4067 | } |
| 4068 | |
| 4069 | // reevaluate outputs for all devices |
| 4070 | checkForDeviceAndOutputChanges(); |
| 4071 | updateCallAndOutputRouting(); |
| 4072 | |
| 4073 | return NO_ERROR; |
| 4074 | } |
| 4075 | |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4076 | void AudioPolicyManager::dump(String8 *dst) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4077 | { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4078 | dst->appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
Mikhail Naganov | 0dbe87b | 2021-12-01 02:03:31 +0000 | [diff] [blame] | 4079 | dst->appendFormat(" Primary Output I/O handle: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4080 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 4081 | std::string stateLiteral; |
| 4082 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4083 | dst->appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 4084 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 4085 | "communications", "media", "record", "dock", "system", |
| 4086 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 4087 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 4088 | i < AUDIO_POLICY_FORCE_USE_CNT; i = (audio_policy_force_use_t)((int)i + 1)) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4089 | audio_policy_forced_cfg_t forceUseValue = mEngine->getForceUse(i); |
| 4090 | dst->appendFormat(" Force use for %s: %d", forceUses[i], forceUseValue); |
| 4091 | if (i == AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND && |
| 4092 | forceUseValue == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 4093 | dst->append(" (MANUAL: "); |
| 4094 | dumpManualSurroundFormats(dst); |
| 4095 | dst->append(")"); |
| 4096 | } |
| 4097 | dst->append("\n"); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 4098 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4099 | dst->appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 4100 | dst->appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
Mikhail Naganov | b3bcb4f | 2022-02-23 23:46:56 +0000 | [diff] [blame] | 4101 | dst->appendFormat(" Communication Strategy id: %d\n", mCommunnicationStrategy); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4102 | dst->appendFormat(" Config source: %s\n", mConfig.getSource().c_str()); // getConfig not const |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4103 | |
Mikhail Naganov | 0f413b2 | 2021-12-02 05:29:27 +0000 | [diff] [blame] | 4104 | dst->append("\n"); |
| 4105 | mAvailableOutputDevices.dump(dst, String8("Available output"), 1); |
| 4106 | dst->append("\n"); |
| 4107 | mAvailableInputDevices.dump(dst, String8("Available input"), 1); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4108 | mHwModulesAll.dump(dst); |
| 4109 | mOutputs.dump(dst); |
| 4110 | mInputs.dump(dst); |
Mikhail Naganov | 0f413b2 | 2021-12-02 05:29:27 +0000 | [diff] [blame] | 4111 | mEffects.dump(dst, 1); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4112 | mAudioPatches.dump(dst); |
| 4113 | mPolicyMixes.dump(dst); |
| 4114 | mAudioSources.dump(dst); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4115 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 4116 | dst->appendFormat(" AllowedCapturePolicies:\n"); |
| 4117 | for (auto& policy : mAllowedCapturePolicies) { |
| 4118 | dst->appendFormat(" - uid=%d flag_mask=%#x\n", policy.first, policy.second); |
| 4119 | } |
| 4120 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4121 | dst->appendFormat(" Preferred mixer audio configuration:\n"); |
| 4122 | for (const auto it : mPreferredMixerAttrInfos) { |
| 4123 | dst->appendFormat(" - device port id: %d\n", it.first); |
| 4124 | for (const auto preferredMixerInfoIt : it.second) { |
| 4125 | dst->appendFormat(" - strategy: %d; ", preferredMixerInfoIt.first); |
| 4126 | preferredMixerInfoIt.second->dump(dst); |
| 4127 | } |
| 4128 | } |
| 4129 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4130 | dst->appendFormat("\nPolicy Engine dump:\n"); |
| 4131 | mEngine->dump(dst); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4132 | } |
| 4133 | |
| 4134 | status_t AudioPolicyManager::dump(int fd) |
| 4135 | { |
| 4136 | String8 result; |
| 4137 | dump(&result); |
Andy Hung | bb54e20 | 2018-10-05 11:42:02 -0700 | [diff] [blame] | 4138 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4139 | return NO_ERROR; |
| 4140 | } |
| 4141 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 4142 | status_t AudioPolicyManager::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) |
| 4143 | { |
| 4144 | mAllowedCapturePolicies[uid] = capturePolicy; |
| 4145 | return NO_ERROR; |
| 4146 | } |
| 4147 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4148 | // This function checks for the parameters which can be offloaded. |
| 4149 | // This can be enhanced depending on the capability of the DSP and policy |
| 4150 | // of the system. |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4151 | audio_offload_mode_t AudioPolicyManager::getOffloadSupport(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4152 | { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4153 | ALOGV("%s: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4154 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4155 | __func__, offloadInfo.sample_rate, offloadInfo.channel_mask, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4156 | offloadInfo.format, |
| 4157 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 4158 | offloadInfo.has_video); |
| 4159 | |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4160 | if (!isOffloadPossible(offloadInfo)) { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4161 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4162 | } |
| 4163 | |
| 4164 | // See if there is a profile to support this. |
| 4165 | // AUDIO_DEVICE_NONE |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4166 | sp<IOProfile> profile = getProfileForOutput(DeviceVector() /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4167 | offloadInfo.sample_rate, |
| 4168 | offloadInfo.format, |
| 4169 | offloadInfo.channel_mask, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4170 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, |
| 4171 | true /* directOnly */); |
Eric Laurent | 9436544 | 2021-01-08 18:36:05 +0100 | [diff] [blame] | 4172 | ALOGV("%s: profile %sfound%s", __func__, profile != nullptr ? "" : "NOT ", |
| 4173 | (profile != nullptr && (profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) |
| 4174 | ? ", supports gapless" : ""); |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4175 | if (profile == nullptr) { |
| 4176 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
| 4177 | } |
| 4178 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) { |
| 4179 | return AUDIO_OFFLOAD_GAPLESS_SUPPORTED; |
| 4180 | } |
| 4181 | return AUDIO_OFFLOAD_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4182 | } |
| 4183 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4184 | bool AudioPolicyManager::isDirectOutputSupported(const audio_config_base_t& config, |
| 4185 | const audio_attributes_t& attributes) { |
| 4186 | audio_output_flags_t output_flags = AUDIO_OUTPUT_FLAG_NONE; |
François Gaffie | 58d4be5 | 2018-11-06 15:30:12 +0100 | [diff] [blame] | 4187 | audio_flags_to_audio_output_flags(attributes.flags, &output_flags); |
Dorin Drimus | 9901eb0 | 2022-01-14 11:36:51 +0000 | [diff] [blame] | 4188 | DeviceVector outputDevices = mEngine->getOutputDevicesForAttributes(attributes); |
| 4189 | sp<IOProfile> profile = getProfileForOutput(outputDevices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4190 | config.sample_rate, |
| 4191 | config.format, |
| 4192 | config.channel_mask, |
| 4193 | output_flags, |
| 4194 | true /* directOnly */); |
| 4195 | ALOGV("%s() profile %sfound with name: %s, " |
| 4196 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 4197 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 4198 | (profile != 0 ? profile->getTagName().c_str() : "null"), |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4199 | config.sample_rate, config.format, config.channel_mask, output_flags); |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 4200 | |
| 4201 | // also try the MSD module if compatible profile not found |
| 4202 | if (profile == nullptr) { |
| 4203 | profile = getMsdProfileForOutput(outputDevices, |
| 4204 | config.sample_rate, |
| 4205 | config.format, |
| 4206 | config.channel_mask, |
| 4207 | output_flags, |
| 4208 | true /* directOnly */); |
| 4209 | ALOGV("%s() MSD profile %sfound with name: %s, " |
| 4210 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 4211 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
| 4212 | (profile != 0 ? profile->getTagName().c_str() : "null"), |
| 4213 | config.sample_rate, config.format, config.channel_mask, output_flags); |
| 4214 | } |
| 4215 | return (profile != nullptr); |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4216 | } |
| 4217 | |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4218 | bool AudioPolicyManager::isOffloadPossible(const audio_offload_info_t &offloadInfo, |
| 4219 | bool durationIgnored) { |
| 4220 | if (mMasterMono) { |
| 4221 | return false; // no offloading if mono is set. |
| 4222 | } |
| 4223 | |
| 4224 | // Check if offload has been disabled |
| 4225 | if (property_get_bool("audio.offload.disable", false /* default_value */)) { |
| 4226 | ALOGV("%s: offload disabled by audio.offload.disable", __func__); |
| 4227 | return false; |
| 4228 | } |
| 4229 | |
| 4230 | // Check if stream type is music, then only allow offload as of now. |
| 4231 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 4232 | { |
| 4233 | ALOGV("%s: stream_type != MUSIC, returning false", __func__); |
| 4234 | return false; |
| 4235 | } |
| 4236 | |
| 4237 | //TODO: enable audio offloading with video when ready |
| 4238 | const bool allowOffloadWithVideo = |
| 4239 | property_get_bool("audio.offload.video", false /* default_value */); |
| 4240 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
| 4241 | ALOGV("%s: has_video == true, returning false", __func__); |
| 4242 | return false; |
| 4243 | } |
| 4244 | |
| 4245 | //If duration is less than minimum value defined in property, return false |
| 4246 | const int min_duration_secs = property_get_int32( |
| 4247 | "audio.offload.min.duration.secs", -1 /* default_value */); |
| 4248 | if (!durationIgnored) { |
| 4249 | if (min_duration_secs >= 0) { |
| 4250 | if (offloadInfo.duration_us < min_duration_secs * 1000000LL) { |
| 4251 | ALOGV("%s: Offload denied by duration < audio.offload.min.duration.secs(=%d)", |
| 4252 | __func__, min_duration_secs); |
| 4253 | return false; |
| 4254 | } |
| 4255 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 4256 | ALOGV("%s: Offload denied by duration < default min(=%u)", |
| 4257 | __func__, OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 4258 | return false; |
| 4259 | } |
| 4260 | } |
| 4261 | |
| 4262 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 4263 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 4264 | // detects there is an active non offloadable effect. |
| 4265 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 4266 | // This may prevent offloading in rare situations where effects are left active by apps |
| 4267 | // in the background. |
| 4268 | if (mEffects.isNonOffloadableEffectEnabled()) { |
| 4269 | return false; |
| 4270 | } |
| 4271 | |
| 4272 | return true; |
| 4273 | } |
| 4274 | |
| 4275 | audio_direct_mode_t AudioPolicyManager::getDirectPlaybackSupport(const audio_attributes_t *attr, |
| 4276 | const audio_config_t *config) { |
| 4277 | audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER; |
| 4278 | offloadInfo.format = config->format; |
| 4279 | offloadInfo.sample_rate = config->sample_rate; |
| 4280 | offloadInfo.channel_mask = config->channel_mask; |
| 4281 | offloadInfo.stream_type = mEngine->getStreamTypeForAttributes(*attr); |
| 4282 | offloadInfo.has_video = false; |
| 4283 | offloadInfo.is_streaming = false; |
| 4284 | const bool offloadPossible = isOffloadPossible(offloadInfo, true /*durationIgnored*/); |
| 4285 | |
| 4286 | audio_direct_mode_t directMode = AUDIO_DIRECT_NOT_SUPPORTED; |
| 4287 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 4288 | audio_flags_to_audio_output_flags(attr->flags, &flags); |
| 4289 | // only retain flags that will drive compressed offload or passthrough |
| 4290 | uint32_t relevantFlags = AUDIO_OUTPUT_FLAG_HW_AV_SYNC; |
| 4291 | if (offloadPossible) { |
| 4292 | relevantFlags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD; |
| 4293 | } |
| 4294 | flags = (audio_output_flags_t)((flags & relevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 4295 | |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4296 | DeviceVector engineOutputDevices = mEngine->getOutputDevicesForAttributes(*attr); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4297 | for (const auto& hwModule : mHwModules) { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4298 | DeviceVector outputDevices = engineOutputDevices; |
| 4299 | // the MSD module checks for different conditions and output devices |
| 4300 | if (strcmp(hwModule->getName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0) { |
| 4301 | if (!msdHasPatchesToAllDevices(engineOutputDevices.toTypeAddrVector())) { |
| 4302 | continue; |
| 4303 | } |
| 4304 | outputDevices = getMsdAudioOutDevices(); |
| 4305 | } |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4306 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
jiabin | c8f7dfc | 2022-01-06 18:42:08 +0000 | [diff] [blame] | 4307 | if (!curProfile->isCompatibleProfile(outputDevices, |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4308 | config->sample_rate, nullptr /*updatedSamplingRate*/, |
| 4309 | config->format, nullptr /*updatedFormat*/, |
| 4310 | config->channel_mask, nullptr /*updatedChannelMask*/, |
| 4311 | flags)) { |
| 4312 | continue; |
| 4313 | } |
| 4314 | // reject profiles not corresponding to a device currently available |
| 4315 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
| 4316 | continue; |
| 4317 | } |
| 4318 | if ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) |
| 4319 | != AUDIO_OUTPUT_FLAG_NONE) { |
jiabin | c6132d6 | 2022-01-01 07:36:31 +0000 | [diff] [blame] | 4320 | if ((directMode & AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED) |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4321 | != AUDIO_DIRECT_NOT_SUPPORTED) { |
| 4322 | // Already reports offload gapless supported. No need to report offload support. |
| 4323 | continue; |
| 4324 | } |
| 4325 | if ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) |
| 4326 | != AUDIO_OUTPUT_FLAG_NONE) { |
| 4327 | // If offload gapless is reported, no need to report offload support. |
| 4328 | directMode = (audio_direct_mode_t) ((directMode & |
| 4329 | ~AUDIO_DIRECT_OFFLOAD_SUPPORTED) | |
| 4330 | AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED); |
| 4331 | } else { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4332 | directMode = (audio_direct_mode_t)(directMode | AUDIO_DIRECT_OFFLOAD_SUPPORTED); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4333 | } |
| 4334 | } else { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4335 | directMode = (audio_direct_mode_t) (directMode | AUDIO_DIRECT_BITSTREAM_SUPPORTED); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4336 | } |
| 4337 | } |
| 4338 | } |
| 4339 | return directMode; |
| 4340 | } |
| 4341 | |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4342 | status_t AudioPolicyManager::getDirectProfilesForAttributes(const audio_attributes_t* attr, |
| 4343 | AudioProfileVector& audioProfilesVector) { |
Dorin Drimus | 1711263 | 2022-09-23 15:28:51 +0000 | [diff] [blame] | 4344 | if (mEffects.isNonOffloadableEffectEnabled()) { |
| 4345 | return OK; |
| 4346 | } |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 4347 | DeviceVector devices; |
| 4348 | status_t status = getDevicesForAttributes(*attr, devices, false /* forVolume */); |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4349 | if (status != OK) { |
| 4350 | return status; |
| 4351 | } |
| 4352 | ALOGV("%s: found %zu output devices for attributes.", __func__, devices.size()); |
| 4353 | if (devices.empty()) { |
| 4354 | return OK; // no output devices for the attributes |
| 4355 | } |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 4356 | return getProfilesForDevices(devices, audioProfilesVector, |
| 4357 | AUDIO_OUTPUT_FLAG_DIRECT /*flags*/, false /*isInput*/); |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4358 | } |
| 4359 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4360 | status_t AudioPolicyManager::getSupportedMixerAttributes( |
| 4361 | audio_port_handle_t portId, std::vector<audio_mixer_attributes_t> &mixerAttrs) { |
| 4362 | ALOGV("%s, portId=%d", __func__, portId); |
| 4363 | sp<DeviceDescriptor> deviceDescriptor = mAvailableOutputDevices.getDeviceFromId(portId); |
| 4364 | if (deviceDescriptor == nullptr) { |
| 4365 | ALOGE("%s the requested device is currently unavailable", __func__); |
| 4366 | return BAD_VALUE; |
| 4367 | } |
jiabin | 96daffc | 2023-05-11 17:51:55 +0000 | [diff] [blame^] | 4368 | if (!audio_is_usb_out_device(deviceDescriptor->type())) { |
| 4369 | ALOGE("%s the requested device(type=%#x) is not usb device", __func__, |
| 4370 | deviceDescriptor->type()); |
| 4371 | return BAD_VALUE; |
| 4372 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4373 | for (const auto& hwModule : mHwModules) { |
| 4374 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
| 4375 | if (curProfile->supportsDevice(deviceDescriptor)) { |
| 4376 | curProfile->toSupportedMixerAttributes(&mixerAttrs); |
| 4377 | } |
| 4378 | } |
| 4379 | } |
| 4380 | return NO_ERROR; |
| 4381 | } |
| 4382 | |
| 4383 | status_t AudioPolicyManager::setPreferredMixerAttributes( |
| 4384 | const audio_attributes_t *attr, |
| 4385 | audio_port_handle_t portId, |
| 4386 | uid_t uid, |
| 4387 | const audio_mixer_attributes_t *mixerAttributes) { |
| 4388 | ALOGV("%s, attr=%s, mixerAttributes={format=%#x, channelMask=%#x, samplingRate=%u, " |
| 4389 | "mixerBehavior=%d}, uid=%d, portId=%u", |
| 4390 | __func__, toString(*attr).c_str(), mixerAttributes->config.format, |
| 4391 | mixerAttributes->config.channel_mask, mixerAttributes->config.sample_rate, |
| 4392 | mixerAttributes->mixer_behavior, uid, portId); |
| 4393 | if (attr->usage != AUDIO_USAGE_MEDIA) { |
| 4394 | ALOGE("%s failed, only media is allowed, the given usage is %d", __func__, attr->usage); |
| 4395 | return BAD_VALUE; |
| 4396 | } |
| 4397 | sp<DeviceDescriptor> deviceDescriptor = mAvailableOutputDevices.getDeviceFromId(portId); |
| 4398 | if (deviceDescriptor == nullptr) { |
| 4399 | ALOGE("%s the requested device is currently unavailable", __func__); |
| 4400 | return BAD_VALUE; |
| 4401 | } |
| 4402 | if (!audio_is_usb_out_device(deviceDescriptor->type())) { |
| 4403 | ALOGE("%s(%d), type=%d, is not a usb output device", |
| 4404 | __func__, portId, deviceDescriptor->type()); |
| 4405 | return BAD_VALUE; |
| 4406 | } |
| 4407 | |
| 4408 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 4409 | audio_flags_to_audio_output_flags(attr->flags, &flags); |
| 4410 | flags = (audio_output_flags_t) (flags | |
| 4411 | audio_output_flags_from_mixer_behavior(mixerAttributes->mixer_behavior)); |
| 4412 | sp<IOProfile> profile = nullptr; |
| 4413 | DeviceVector devices(deviceDescriptor); |
| 4414 | for (const auto& hwModule : mHwModules) { |
| 4415 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
| 4416 | if (curProfile->hasDynamicAudioProfile() |
| 4417 | && curProfile->isCompatibleProfile(devices, |
| 4418 | mixerAttributes->config.sample_rate, |
| 4419 | nullptr /*updatedSamplingRate*/, |
| 4420 | mixerAttributes->config.format, |
| 4421 | nullptr /*updatedFormat*/, |
| 4422 | mixerAttributes->config.channel_mask, |
| 4423 | nullptr /*updatedChannelMask*/, |
| 4424 | flags, |
| 4425 | false /*exactMatchRequiredForInputFlags*/)) { |
| 4426 | profile = curProfile; |
| 4427 | break; |
| 4428 | } |
| 4429 | } |
| 4430 | } |
| 4431 | if (profile == nullptr) { |
| 4432 | ALOGE("%s, there is no compatible profile found", __func__); |
| 4433 | return BAD_VALUE; |
| 4434 | } |
| 4435 | |
| 4436 | sp<PreferredMixerAttributesInfo> mixerAttrInfo = |
| 4437 | sp<PreferredMixerAttributesInfo>::make( |
| 4438 | uid, portId, profile, flags, *mixerAttributes); |
| 4439 | const product_strategy_t strategy = mEngine->getProductStrategyForAttributes(*attr); |
| 4440 | mPreferredMixerAttrInfos[portId][strategy] = mixerAttrInfo; |
| 4441 | |
| 4442 | // If 1) there is any client from the preferred mixer configuration owner that is currently |
| 4443 | // active and matches the strategy and 2) current output is on the preferred device and the |
| 4444 | // mixer configuration doesn't match the preferred one, reopen output with preferred mixer |
| 4445 | // configuration. |
| 4446 | std::vector<audio_io_handle_t> outputsToReopen; |
| 4447 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4448 | const auto output = mOutputs.valueAt(i); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 4449 | if (output->mProfile == profile && output->devices().onlyContainsDevice(deviceDescriptor)) { |
| 4450 | if (output->isConfigurationMatched(mixerAttributes->config, flags)) { |
| 4451 | output->mUsePreferredMixerAttributes = true; |
| 4452 | } else { |
| 4453 | for (const auto &client: output->getActiveClients()) { |
| 4454 | if (client->uid() == uid && client->strategy() == strategy) { |
| 4455 | client->setIsInvalid(); |
| 4456 | outputsToReopen.push_back(output->mIoHandle); |
| 4457 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4458 | } |
| 4459 | } |
| 4460 | } |
| 4461 | } |
| 4462 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4463 | config.sample_rate = mixerAttributes->config.sample_rate; |
| 4464 | config.channel_mask = mixerAttributes->config.channel_mask; |
| 4465 | config.format = mixerAttributes->config.format; |
| 4466 | for (const auto output : outputsToReopen) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 4467 | sp<SwAudioOutputDescriptor> desc = |
| 4468 | reopenOutput(mOutputs.valueFor(output), &config, flags, __func__); |
| 4469 | if (desc == nullptr) { |
| 4470 | ALOGE("%s, failed to reopen output with preferred mixer attributes", __func__); |
| 4471 | continue; |
| 4472 | } |
| 4473 | desc->mUsePreferredMixerAttributes = true; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
| 4476 | return NO_ERROR; |
| 4477 | } |
| 4478 | |
| 4479 | sp<PreferredMixerAttributesInfo> AudioPolicyManager::getPreferredMixerAttributesInfo( |
| 4480 | audio_port_handle_t devicePortId, product_strategy_t strategy) { |
| 4481 | auto it = mPreferredMixerAttrInfos.find(devicePortId); |
| 4482 | if (it == mPreferredMixerAttrInfos.end()) { |
| 4483 | return nullptr; |
| 4484 | } |
| 4485 | auto mixerAttrInfoIt = it->second.find(strategy); |
| 4486 | if (mixerAttrInfoIt == it->second.end()) { |
| 4487 | return nullptr; |
| 4488 | } |
| 4489 | return mixerAttrInfoIt->second; |
| 4490 | } |
| 4491 | |
| 4492 | status_t AudioPolicyManager::getPreferredMixerAttributes( |
| 4493 | const audio_attributes_t *attr, |
| 4494 | audio_port_handle_t portId, |
| 4495 | audio_mixer_attributes_t* mixerAttributes) { |
| 4496 | sp<PreferredMixerAttributesInfo> info = getPreferredMixerAttributesInfo( |
| 4497 | portId, mEngine->getProductStrategyForAttributes(*attr)); |
| 4498 | if (info == nullptr) { |
| 4499 | return NAME_NOT_FOUND; |
| 4500 | } |
| 4501 | *mixerAttributes = info->getMixerAttributes(); |
| 4502 | return NO_ERROR; |
| 4503 | } |
| 4504 | |
| 4505 | status_t AudioPolicyManager::clearPreferredMixerAttributes(const audio_attributes_t *attr, |
| 4506 | audio_port_handle_t portId, |
| 4507 | uid_t uid) { |
| 4508 | const product_strategy_t strategy = mEngine->getProductStrategyForAttributes(*attr); |
| 4509 | const auto preferredMixerAttrInfo = getPreferredMixerAttributesInfo(portId, strategy); |
| 4510 | if (preferredMixerAttrInfo == nullptr) { |
| 4511 | return NAME_NOT_FOUND; |
| 4512 | } |
| 4513 | if (preferredMixerAttrInfo->getUid() != uid) { |
| 4514 | ALOGE("%s, requested uid=%d, owned uid=%d", |
| 4515 | __func__, uid, preferredMixerAttrInfo->getUid()); |
| 4516 | return PERMISSION_DENIED; |
| 4517 | } |
| 4518 | mPreferredMixerAttrInfos[portId].erase(strategy); |
| 4519 | if (mPreferredMixerAttrInfos[portId].empty()) { |
| 4520 | mPreferredMixerAttrInfos.erase(portId); |
| 4521 | } |
| 4522 | |
| 4523 | // Reconfig existing output |
| 4524 | std::vector<audio_io_handle_t> potentialOutputsToReopen; |
| 4525 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4526 | if (mOutputs.valueAt(i)->mProfile == preferredMixerAttrInfo->getProfile()) { |
| 4527 | potentialOutputsToReopen.push_back(mOutputs.keyAt(i)); |
| 4528 | } |
| 4529 | } |
| 4530 | for (const auto output : potentialOutputsToReopen) { |
| 4531 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 4532 | if (desc->isConfigurationMatched(preferredMixerAttrInfo->getConfigBase(), |
| 4533 | preferredMixerAttrInfo->getFlags())) { |
| 4534 | reopenOutput(desc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 4535 | } |
| 4536 | } |
| 4537 | return NO_ERROR; |
| 4538 | } |
| 4539 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4540 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 4541 | audio_port_type_t type, |
| 4542 | unsigned int *num_ports, |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4543 | struct audio_port_v7 *ports, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4544 | unsigned int *generation) |
| 4545 | { |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4546 | if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) || |
| 4547 | generation == nullptr) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4548 | return BAD_VALUE; |
| 4549 | } |
| 4550 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4551 | if (ports == nullptr) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4552 | *num_ports = 0; |
| 4553 | } |
| 4554 | |
| 4555 | size_t portsWritten = 0; |
| 4556 | size_t portsMax = *num_ports; |
| 4557 | *num_ports = 0; |
| 4558 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4559 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 4560 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4561 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4562 | for (const auto& dev : mAvailableOutputDevices) { |
| 4563 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4564 | continue; |
| 4565 | } |
| 4566 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4567 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4568 | } |
| 4569 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4570 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4571 | } |
| 4572 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4573 | for (const auto& dev : mAvailableInputDevices) { |
| 4574 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4575 | continue; |
| 4576 | } |
| 4577 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4578 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4579 | } |
| 4580 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4581 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4582 | } |
| 4583 | } |
| 4584 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 4585 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 4586 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 4587 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 4588 | } |
| 4589 | *num_ports += mInputs.size(); |
| 4590 | } |
| 4591 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4592 | size_t numOutputs = 0; |
| 4593 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4594 | if (!mOutputs[i]->isDuplicated()) { |
| 4595 | numOutputs++; |
| 4596 | if (portsWritten < portsMax) { |
| 4597 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 4598 | } |
| 4599 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4600 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4601 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4602 | } |
| 4603 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4604 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4605 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 4606 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4607 | return NO_ERROR; |
| 4608 | } |
| 4609 | |
Mikhail Naganov | 5edc5ed | 2023-03-23 14:52:15 -0700 | [diff] [blame] | 4610 | status_t AudioPolicyManager::listDeclaredDevicePorts(media::AudioPortRole role, |
| 4611 | std::vector<media::AudioPortFw>* _aidl_return) { |
| 4612 | auto pushPort = [&](const sp<DeviceDescriptor>& dev) -> status_t { |
| 4613 | audio_port_v7 port; |
| 4614 | dev->toAudioPort(&port); |
| 4615 | auto aidlPort = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_v7_AudioPortFw(port)); |
| 4616 | _aidl_return->push_back(std::move(aidlPort)); |
| 4617 | return OK; |
| 4618 | }; |
| 4619 | |
| 4620 | for (const auto& module : mHwModulesAll) { |
| 4621 | for (const auto& dev : module->getDeclaredDevices()) { |
| 4622 | if (role == media::AudioPortRole::NONE || |
| 4623 | ((role == media::AudioPortRole::SOURCE) |
| 4624 | == audio_is_input_device(dev->type()))) { |
| 4625 | RETURN_STATUS_IF_ERROR(pushPort(dev)); |
| 4626 | } |
| 4627 | } |
| 4628 | } |
| 4629 | return OK; |
| 4630 | } |
| 4631 | |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4632 | status_t AudioPolicyManager::getAudioPort(struct audio_port_v7 *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4633 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 4634 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 4635 | return BAD_VALUE; |
| 4636 | } |
| 4637 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 4638 | if (dev != 0) { |
| 4639 | dev->toAudioPort(port); |
| 4640 | return NO_ERROR; |
| 4641 | } |
| 4642 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 4643 | if (dev != 0) { |
| 4644 | dev->toAudioPort(port); |
| 4645 | return NO_ERROR; |
| 4646 | } |
| 4647 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 4648 | if (out != 0) { |
| 4649 | out->toAudioPort(port); |
| 4650 | return NO_ERROR; |
| 4651 | } |
| 4652 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 4653 | if (in != 0) { |
| 4654 | in->toAudioPort(port); |
| 4655 | return NO_ERROR; |
| 4656 | } |
| 4657 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4658 | } |
| 4659 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4660 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 4661 | audio_patch_handle_t *handle, |
| 4662 | uid_t uid) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4663 | { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4664 | ALOGV("%s", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4665 | if (handle == NULL || patch == NULL) { |
| 4666 | return BAD_VALUE; |
| 4667 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4668 | ALOGV("%s num sources %d num sinks %d", __func__, patch->num_sources, patch->num_sinks); |
Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 4669 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4670 | return BAD_VALUE; |
| 4671 | } |
| 4672 | // only one source per audio patch supported for now |
| 4673 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4674 | return INVALID_OPERATION; |
| 4675 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4676 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4677 | return INVALID_OPERATION; |
| 4678 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4679 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4680 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 4681 | return INVALID_OPERATION; |
| 4682 | } |
| 4683 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4684 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4685 | sp<DeviceDescriptor> srcDevice = mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 4686 | sp<DeviceDescriptor> sinkDevice = mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id); |
| 4687 | if (srcDevice == nullptr || sinkDevice == nullptr) { |
| 4688 | ALOGW("%s could not create patch, invalid sink and/or source device(s)", __func__); |
| 4689 | return BAD_VALUE; |
| 4690 | } |
| 4691 | ALOGV("%s between source %s and sink %s", __func__, |
| 4692 | srcDevice->toString().c_str(), sinkDevice->toString().c_str()); |
| 4693 | audio_port_handle_t portId = PolicyAudioPort::getNextUniqueId(); |
| 4694 | // Default attributes, default volume priority, not to infer with non raw audio patches. |
| 4695 | audio_attributes_t attributes = attributes_initializer(AUDIO_USAGE_MEDIA); |
| 4696 | const struct audio_port_config *source = &patch->sources[0]; |
| 4697 | sp<SourceClientDescriptor> sourceDesc = |
| 4698 | new InternalSourceClientDescriptor( |
| 4699 | portId, uid, attributes, *source, srcDevice, sinkDevice, |
| 4700 | mEngine->getProductStrategyForAttributes(attributes), toVolumeSource(attributes)); |
| 4701 | |
| 4702 | status_t status = |
| 4703 | connectAudioSourceToSink(sourceDesc, sinkDevice, patch, *handle, uid, 0 /* delayMs */); |
| 4704 | |
| 4705 | if (status != NO_ERROR) { |
| 4706 | return INVALID_OPERATION; |
| 4707 | } |
| 4708 | mAudioSources.add(portId, sourceDesc); |
| 4709 | return NO_ERROR; |
| 4710 | } |
| 4711 | |
| 4712 | status_t AudioPolicyManager::connectAudioSourceToSink( |
| 4713 | const sp<SourceClientDescriptor>& sourceDesc, const sp<DeviceDescriptor> &sinkDevice, |
| 4714 | const struct audio_patch *patch, |
| 4715 | audio_patch_handle_t &handle, |
| 4716 | uid_t uid, uint32_t delayMs) |
| 4717 | { |
| 4718 | status_t status = createAudioPatchInternal(patch, &handle, uid, delayMs, sourceDesc); |
| 4719 | if (status != NO_ERROR || mAudioPatches.indexOfKey(handle) < 0) { |
| 4720 | ALOGW("%s patch panel could not connect device patch, error %d", __func__, status); |
| 4721 | return INVALID_OPERATION; |
| 4722 | } |
| 4723 | sourceDesc->connect(handle, sinkDevice); |
| 4724 | if (isMsdPatch(handle)) { |
| 4725 | return NO_ERROR; |
| 4726 | } |
| 4727 | // SW Bridge? (@todo: HW bridge, keep track of HwOutput for device selection "reconsideration") |
| 4728 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); |
| 4729 | ALOG_ASSERT(swOutput != nullptr, "%s: a swOutput shall always be associated", __func__); |
| 4730 | if (swOutput->getClient(sourceDesc->portId()) != nullptr) { |
| 4731 | ALOGW("%s source portId has already been attached to outputDesc", __func__); |
| 4732 | goto FailurePatchAdded; |
| 4733 | } |
| 4734 | status = swOutput->start(); |
| 4735 | if (status != NO_ERROR) { |
| 4736 | goto FailureSourceAdded; |
| 4737 | } |
| 4738 | swOutput->addClient(sourceDesc); |
| 4739 | status = startSource(swOutput, sourceDesc, &delayMs); |
| 4740 | if (status != NO_ERROR) { |
| 4741 | ALOGW("%s failed to start source, error %d", __FUNCTION__, status); |
| 4742 | goto FailureSourceActive; |
| 4743 | } |
| 4744 | if (delayMs != 0) { |
| 4745 | usleep(delayMs * 1000); |
| 4746 | } |
| 4747 | return NO_ERROR; |
| 4748 | |
| 4749 | FailureSourceActive: |
| 4750 | swOutput->stop(); |
| 4751 | releaseOutput(sourceDesc->portId()); |
| 4752 | FailureSourceAdded: |
| 4753 | sourceDesc->setSwOutput(nullptr); |
| 4754 | FailurePatchAdded: |
| 4755 | releaseAudioPatchInternal(handle); |
| 4756 | return INVALID_OPERATION; |
| 4757 | } |
| 4758 | |
| 4759 | status_t AudioPolicyManager::createAudioPatchInternal(const struct audio_patch *patch, |
| 4760 | audio_patch_handle_t *handle, |
| 4761 | uid_t uid, uint32_t delayMs, |
| 4762 | const sp<SourceClientDescriptor>& sourceDesc) |
| 4763 | { |
| 4764 | ALOGV("%s num sources %d num sinks %d", __func__, patch->num_sources, patch->num_sinks); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4765 | sp<AudioPatch> patchDesc; |
| 4766 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 4767 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4768 | ALOGV("%s source id %d role %d type %d", __func__, patch->sources[0].id, |
| 4769 | patch->sources[0].role, |
| 4770 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4771 | #if LOG_NDEBUG == 0 |
| 4772 | for (size_t i = 0; i < patch->num_sinks; i++) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4773 | ALOGV("%s sink %zu: id %d role %d type %d", __func__ ,i, patch->sinks[i].id, |
| 4774 | patch->sinks[i].role, |
| 4775 | patch->sinks[i].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4776 | } |
| 4777 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4778 | |
| 4779 | if (index >= 0) { |
| 4780 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4781 | ALOGV("%s mUidCached %d patchDesc->mUid %d uid %d", |
| 4782 | __func__, mUidCached, patchDesc->getUid(), uid); |
| 4783 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4784 | return INVALID_OPERATION; |
| 4785 | } |
| 4786 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4787 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4788 | } |
| 4789 | |
| 4790 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4791 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4792 | if (outputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4793 | ALOGV("%s output not found for id %d", __func__, patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4794 | return BAD_VALUE; |
| 4795 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4796 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 4797 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4798 | if (patchDesc != 0) { |
| 4799 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4800 | ALOGV("%s source id differs for patch current id %d new id %d", |
| 4801 | __func__, patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4802 | return BAD_VALUE; |
| 4803 | } |
| 4804 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4805 | DeviceVector devices; |
| 4806 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4807 | // Only support mix to devices connection |
| 4808 | // TODO add support for mix to mix connection |
| 4809 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4810 | ALOGV("%s source mix but sink is not a device", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4811 | return INVALID_OPERATION; |
| 4812 | } |
| 4813 | sp<DeviceDescriptor> devDesc = |
| 4814 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 4815 | if (devDesc == 0) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4816 | ALOGV("%s out device not found for id %d", __func__, patch->sinks[i].id); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4817 | return BAD_VALUE; |
| 4818 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4819 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4820 | if (!outputDesc->mProfile->isCompatibleProfile(DeviceVector(devDesc), |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4821 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4822 | NULL, // updatedSamplingRate |
| 4823 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4824 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4825 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4826 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4827 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4828 | ALOGV("%s profile not supported for device %08x", __func__, devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4829 | return INVALID_OPERATION; |
| 4830 | } |
| 4831 | devices.add(devDesc); |
| 4832 | } |
| 4833 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4834 | return INVALID_OPERATION; |
| 4835 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4836 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4837 | // TODO: reconfigure output format and channels here |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4838 | ALOGV("%s setting device %s on output %d", |
| 4839 | __func__, dumpDeviceTypes(devices.types()).c_str(), outputDesc->mIoHandle); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4840 | setOutputDevices(outputDesc, devices, true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4841 | index = mAudioPatches.indexOfKey(*handle); |
| 4842 | if (index >= 0) { |
| 4843 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4844 | ALOGW("%s setOutputDevice() did not reuse the patch provided", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4845 | } |
| 4846 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4847 | patchDesc->setUid(uid); |
| 4848 | ALOGV("%s success", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4849 | } else { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4850 | ALOGW("%s setOutputDevice() failed to create a patch", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4851 | return INVALID_OPERATION; |
| 4852 | } |
| 4853 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 4854 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 4855 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4856 | // only one sink supported when connecting an input device to a mix |
| 4857 | if (patch->num_sinks > 1) { |
| 4858 | return INVALID_OPERATION; |
| 4859 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4860 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4861 | if (inputDesc == NULL) { |
| 4862 | return BAD_VALUE; |
| 4863 | } |
| 4864 | if (patchDesc != 0) { |
| 4865 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 4866 | return BAD_VALUE; |
| 4867 | } |
| 4868 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4869 | sp<DeviceDescriptor> device = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4870 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4871 | if (device == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4872 | return BAD_VALUE; |
| 4873 | } |
| 4874 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4875 | if (!inputDesc->mProfile->isCompatibleProfile(DeviceVector(device), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4876 | patch->sinks[0].sample_rate, |
| 4877 | NULL, /*updatedSampleRate*/ |
| 4878 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4879 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4880 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4881 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4882 | // FIXME for the parameter type, |
| 4883 | // and the NONE |
| 4884 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 4885 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4886 | return INVALID_OPERATION; |
| 4887 | } |
| 4888 | // TODO: reconfigure output format and channels here |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4889 | ALOGV("%s setting device %s on output %d", __func__, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4890 | device->toString().c_str(), inputDesc->mIoHandle); |
| 4891 | setInputDevice(inputDesc->mIoHandle, device, true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4892 | index = mAudioPatches.indexOfKey(*handle); |
| 4893 | if (index >= 0) { |
| 4894 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4895 | ALOGW("%s setInputDevice() did not reuse the patch provided", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4896 | } |
| 4897 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4898 | patchDesc->setUid(uid); |
| 4899 | ALOGV("%s success", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4900 | } else { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4901 | ALOGW("%s setInputDevice() failed to create a patch", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4902 | return INVALID_OPERATION; |
| 4903 | } |
| 4904 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 4905 | // device to device connection |
| 4906 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4907 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4908 | return BAD_VALUE; |
| 4909 | } |
| 4910 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4911 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4912 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4913 | if (srcDevice == 0) { |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 4914 | return BAD_VALUE; |
| 4915 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4916 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4917 | //update source and sink with our own data as the data passed in the patch may |
| 4918 | // be incomplete. |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4919 | PatchBuilder patchBuilder; |
| 4920 | audio_port_config sourcePortConfig = {}; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 4921 | |
| 4922 | // if first sink is to MSD, establish single MSD patch |
| 4923 | if (getMsdAudioOutDevices().contains( |
| 4924 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id))) { |
| 4925 | ALOGV("%s patching to MSD", __FUNCTION__); |
| 4926 | patchBuilder = buildMsdPatch(false /*msdIsSource*/, srcDevice); |
| 4927 | goto installPatch; |
| 4928 | } |
| 4929 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4930 | srcDevice->toAudioPortConfig(&sourcePortConfig, &patch->sources[0]); |
| 4931 | patchBuilder.addSource(sourcePortConfig); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4932 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4933 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4934 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4935 | ALOGV("%s source device but one sink is not a device", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4936 | return INVALID_OPERATION; |
| 4937 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4938 | sp<DeviceDescriptor> sinkDevice = |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4939 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4940 | if (sinkDevice == 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4941 | return BAD_VALUE; |
| 4942 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4943 | audio_port_config sinkPortConfig = {}; |
| 4944 | sinkDevice->toAudioPortConfig(&sinkPortConfig, &patch->sinks[i]); |
| 4945 | patchBuilder.addSink(sinkPortConfig); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4946 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4947 | // Whatever Sw or Hw bridge, we do attach an SwOutput to an Audio Source for |
| 4948 | // volume management purpose (tracking activity) |
| 4949 | // In case of Hw bridge, it is a Work Around. The mixPort used is the one declared |
| 4950 | // in config XML to reach the sink so that is can be declared as available. |
| 4951 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 4952 | sp<SwAudioOutputDescriptor> outputDesc; |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4953 | if (!sourceDesc->isInternal()) { |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4954 | // take care of dynamic routing for SwOutput selection, |
| 4955 | audio_attributes_t attributes = sourceDesc->attributes(); |
| 4956 | audio_stream_type_t stream = sourceDesc->stream(); |
| 4957 | audio_attributes_t resultAttr; |
| 4958 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4959 | config.sample_rate = sourceDesc->config().sample_rate; |
François Gaffie | 2a24db4 | 2022-04-04 16:45:02 +0200 | [diff] [blame] | 4960 | audio_channel_mask_t sourceMask = sourceDesc->config().channel_mask; |
| 4961 | config.channel_mask = |
| 4962 | (audio_channel_mask_get_representation(sourceMask) |
| 4963 | == AUDIO_CHANNEL_REPRESENTATION_INDEX) ? sourceMask |
| 4964 | : audio_channel_mask_in_to_out(sourceMask); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4965 | config.format = sourceDesc->config().format; |
| 4966 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 4967 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 4968 | bool isRequestedDeviceForExclusiveUse = false; |
| 4969 | output_type_t outputType; |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 4970 | bool isSpatialized; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 4971 | bool isBitPerfect; |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4972 | getOutputForAttrInt(&resultAttr, &output, AUDIO_SESSION_NONE, &attributes, |
| 4973 | &stream, sourceDesc->uid(), &config, &flags, |
| 4974 | &selectedDeviceId, &isRequestedDeviceForExclusiveUse, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 4975 | nullptr, &outputType, &isSpatialized, &isBitPerfect); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4976 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 4977 | ALOGV("%s no output for device %s", |
| 4978 | __FUNCTION__, sinkDevice->toString().c_str()); |
| 4979 | return INVALID_OPERATION; |
| 4980 | } |
| 4981 | outputDesc = mOutputs.valueFor(output); |
| 4982 | if (outputDesc->isDuplicated()) { |
| 4983 | ALOGE("%s output is duplicated", __func__); |
| 4984 | return INVALID_OPERATION; |
| 4985 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 4986 | bool closeOutput = outputDesc->mDirectOpenCount != 0; |
| 4987 | sourceDesc->setSwOutput(outputDesc, closeOutput); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4988 | } else { |
| 4989 | // Same for "raw patches" aka created from createAudioPatch API |
| 4990 | SortedVector<audio_io_handle_t> outputs = |
| 4991 | getOutputsForDevices(DeviceVector(sinkDevice), mOutputs); |
| 4992 | // if the sink device is reachable via an opened output stream, request to |
| 4993 | // go via this output stream by adding a second source to the patch |
| 4994 | // description |
| 4995 | output = selectOutput(outputs); |
| 4996 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 4997 | ALOGE("%s no output available for internal patch sink", __func__); |
| 4998 | return INVALID_OPERATION; |
| 4999 | } |
| 5000 | outputDesc = mOutputs.valueFor(output); |
| 5001 | if (outputDesc->isDuplicated()) { |
| 5002 | ALOGV("%s output for device %s is duplicated", |
| 5003 | __func__, sinkDevice->toString().c_str()); |
| 5004 | return INVALID_OPERATION; |
| 5005 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5006 | sourceDesc->setSwOutput(outputDesc, /* closeOutput= */ false); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5007 | } |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 5008 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 5009 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 5010 | // - audio HAL version is < 3.0 |
Francois Gaffie | 99896da | 2018-04-09 11:05:33 +0200 | [diff] [blame] | 5011 | // - audio HAL version is >= 3.0 but no route has been declared between devices |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5012 | // - called from startAudioSource (aka sourceDesc is not internal) and source device |
| 5013 | // does not have a gain controller |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5014 | if (!srcDevice->hasSameHwModuleAs(sinkDevice) || |
| 5015 | (srcDevice->getModuleVersionMajor() < 3) || |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5016 | !srcDevice->getModule()->supportsPatch(srcDevice, sinkDevice) || |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5017 | (!sourceDesc->isInternal() && |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5018 | srcDevice->getAudioPort()->getGains().size() == 0)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 5019 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5020 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5021 | return INVALID_OPERATION; |
| 5022 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5023 | sourceDesc->setUseSwBridge(); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5024 | if (outputDesc != nullptr) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5025 | audio_port_config srcMixPortConfig = {}; |
Ytai Ben-Tsvi | c9d2a91 | 2021-11-22 16:43:09 -0800 | [diff] [blame] | 5026 | outputDesc->toAudioPortConfig(&srcMixPortConfig, nullptr); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5027 | // for volume control, we may need a valid stream |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 5028 | srcMixPortConfig.ext.mix.usecase.stream = |
| 5029 | (!sourceDesc->isInternal() || isCallTxAudioSource(sourceDesc)) ? |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5030 | mEngine->getStreamTypeForAttributes(sourceDesc->attributes()) : |
| 5031 | AUDIO_STREAM_PATCH; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5032 | patchBuilder.addSource(srcMixPortConfig); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5033 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5034 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5035 | } |
| 5036 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 5037 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 5038 | installPatch: |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5039 | status_t status = installPatch( |
| 5040 | __func__, index, handle, patchBuilder.patch(), delayMs, uid, &patchDesc); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5041 | if (status != NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5042 | ALOGW("%s patch panel could not connect device patch, error %d", __func__, status); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5043 | return INVALID_OPERATION; |
| 5044 | } |
| 5045 | } else { |
| 5046 | return BAD_VALUE; |
| 5047 | } |
| 5048 | } else { |
| 5049 | return BAD_VALUE; |
| 5050 | } |
| 5051 | return NO_ERROR; |
| 5052 | } |
| 5053 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5054 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, uid_t uid) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5055 | { |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5056 | ALOGV("%s patch %d", __func__, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5057 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 5058 | |
| 5059 | if (index < 0) { |
| 5060 | return BAD_VALUE; |
| 5061 | } |
| 5062 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5063 | ALOGV("%s() mUidCached %d patchDesc->mUid %d uid %d", |
| 5064 | __func__, mUidCached, patchDesc->getUid(), uid); |
| 5065 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5066 | return INVALID_OPERATION; |
| 5067 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5068 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 5069 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 5070 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5071 | if (sourceDesc != nullptr && sourceDesc->getPatchHandle() == handle) { |
| 5072 | portId = sourceDesc->portId(); |
| 5073 | break; |
| 5074 | } |
| 5075 | } |
| 5076 | return portId != AUDIO_PORT_HANDLE_NONE ? |
| 5077 | stopAudioSource(portId) : releaseAudioPatchInternal(handle); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5078 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5079 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5080 | status_t AudioPolicyManager::releaseAudioPatchInternal(audio_patch_handle_t handle, |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5081 | uint32_t delayMs, |
| 5082 | const sp<SourceClientDescriptor>& sourceDesc) |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5083 | { |
| 5084 | ALOGV("%s patch %d", __func__, handle); |
| 5085 | if (mAudioPatches.indexOfKey(handle) < 0) { |
| 5086 | ALOGE("%s: no patch found with handle=%d", __func__, handle); |
| 5087 | return BAD_VALUE; |
| 5088 | } |
| 5089 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5090 | struct audio_patch *patch = &patchDesc->mPatch; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5091 | patchDesc->setUid(mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5092 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5093 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5094 | if (outputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5095 | ALOGV("%s output not found for id %d", __func__, patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5096 | return BAD_VALUE; |
| 5097 | } |
| 5098 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5099 | setOutputDevices(outputDesc, |
| 5100 | getNewOutputDevices(outputDesc, true /*fromCache*/), |
| 5101 | true, |
| 5102 | 0, |
| 5103 | NULL); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5104 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 5105 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5106 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5107 | if (inputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5108 | ALOGV("%s input not found for id %d", __func__, patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5109 | return BAD_VALUE; |
| 5110 | } |
| 5111 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 5112 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5113 | true, |
| 5114 | NULL); |
| 5115 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5116 | status_t status = |
| 5117 | mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); |
| 5118 | ALOGV("%s patch panel returned %d patchHandle %d", |
| 5119 | __func__, status, patchDesc->getAfHandle()); |
| 5120 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5121 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5122 | mpClientInterface->onAudioPatchListUpdate(); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5123 | // SW or HW Bridge |
| 5124 | sp<SwAudioOutputDescriptor> outputDesc = nullptr; |
| 5125 | audio_patch_handle_t patchHandle = AUDIO_PATCH_HANDLE_NONE; |
Francois Gaffie | 7feb854 | 2020-04-06 17:39:47 +0200 | [diff] [blame] | 5126 | if (patch->num_sources > 1 && patch->sources[1].type == AUDIO_PORT_TYPE_MIX) { |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5127 | outputDesc = mOutputs.getOutputFromId(patch->sources[1].id); |
| 5128 | } else if (patch->num_sources == 1 && sourceDesc != nullptr) { |
| 5129 | outputDesc = sourceDesc->swOutput().promote(); |
| 5130 | } |
| 5131 | if (outputDesc == nullptr) { |
| 5132 | ALOGW("%s no output for id %d", __func__, patch->sources[0].id); |
| 5133 | // releaseOutput has already called closeOutput in case of direct output |
| 5134 | return NO_ERROR; |
| 5135 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5136 | patchHandle = outputDesc->getPatchHandle(); |
| 5137 | // When a Sw bridge is released, the mixer used by this bridge will release its |
| 5138 | // patch at AudioFlinger side. Hence, the mixer audio patch must be recreated |
| 5139 | // Reuse patch handle to force audio flinger removing initial mixer patch removal |
| 5140 | // updating hal patch handle (prevent leaks). |
| 5141 | // While using a HwBridge, force reconsidering device only if not reusing an existing |
| 5142 | // output and no more activity on output (will force to close). |
| 5143 | bool force = sourceDesc->useSwBridge() || |
| 5144 | (sourceDesc->canCloseOutput() && !outputDesc->isActive()); |
| 5145 | // APM pattern is to have always outputs opened / patch realized for reachable devices. |
| 5146 | // Update device may result to NONE (empty), coupled with force, it releases the patch. |
| 5147 | // Reconsider device only for cases: |
| 5148 | // 1 / Active Output |
| 5149 | // 2 / Inactive Output previously hosting HwBridge |
| 5150 | // 3 / Inactive Output previously hosting SwBridge that can be closed. |
| 5151 | bool updateDevice = outputDesc->isActive() || !sourceDesc->useSwBridge() || |
| 5152 | sourceDesc->canCloseOutput(); |
| 5153 | setOutputDevices(outputDesc, |
| 5154 | updateDevice ? getNewOutputDevices(outputDesc, true /*fromCache*/) : |
| 5155 | outputDesc->devices(), |
| 5156 | force, |
| 5157 | 0, |
| 5158 | patchHandle == AUDIO_PATCH_HANDLE_NONE ? nullptr : &patchHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5159 | } else { |
| 5160 | return BAD_VALUE; |
| 5161 | } |
| 5162 | } else { |
| 5163 | return BAD_VALUE; |
| 5164 | } |
| 5165 | return NO_ERROR; |
| 5166 | } |
| 5167 | |
| 5168 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 5169 | struct audio_patch *patches, |
| 5170 | unsigned int *generation) |
| 5171 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5172 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5173 | return BAD_VALUE; |
| 5174 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5175 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5176 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5177 | } |
| 5178 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5179 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5180 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5181 | ALOGV("setAudioPortConfig()"); |
| 5182 | |
| 5183 | if (config == NULL) { |
| 5184 | return BAD_VALUE; |
| 5185 | } |
| 5186 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 5187 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5188 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 5189 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5190 | } |
| 5191 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5192 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5193 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 5194 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5195 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5196 | if (outputDesc == NULL) { |
| 5197 | return BAD_VALUE; |
| 5198 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5199 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 5200 | "setAudioPortConfig() called on duplicated output %d", |
| 5201 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5202 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5203 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5204 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5205 | if (inputDesc == NULL) { |
| 5206 | return BAD_VALUE; |
| 5207 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5208 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5209 | } else { |
| 5210 | return BAD_VALUE; |
| 5211 | } |
| 5212 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 5213 | sp<DeviceDescriptor> deviceDesc; |
| 5214 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 5215 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 5216 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 5217 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 5218 | } else { |
| 5219 | return BAD_VALUE; |
| 5220 | } |
| 5221 | if (deviceDesc == NULL) { |
| 5222 | return BAD_VALUE; |
| 5223 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5224 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5225 | } else { |
| 5226 | return BAD_VALUE; |
| 5227 | } |
| 5228 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 5229 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5230 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 5231 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 5232 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5233 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 5234 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5235 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5236 | if (status != NO_ERROR) { |
| 5237 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5238 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5239 | |
| 5240 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5241 | } |
| 5242 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5243 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 5244 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5245 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5246 | clearAudioPatches(uid); |
| 5247 | clearSessionRoutes(uid); |
| 5248 | } |
| 5249 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5250 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 5251 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 5252 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5253 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5254 | if (patchDesc->getUid() == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 5255 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5256 | } |
| 5257 | } |
| 5258 | } |
| 5259 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5260 | void AudioPolicyManager::checkStrategyRoute(product_strategy_t ps, audio_io_handle_t ouptutToSkip) |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5261 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5262 | // Take the first attributes following the product strategy as it is used to retrieve the routed |
| 5263 | // device. All attributes wihin a strategy follows the same "routing strategy" |
| 5264 | auto attributes = mEngine->getAllAttributesForProductStrategy(ps).front(); |
| 5265 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(attributes, nullptr, false); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5266 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5267 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5268 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 5269 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 5270 | continue; |
| 5271 | } |
| 5272 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5273 | if (!outputDesc->isStrategyActive(ps)) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5274 | continue; |
| 5275 | } |
| 5276 | // If the default device for this strategy is on another output mix, |
| 5277 | // invalidate all tracks in this strategy to force re connection. |
| 5278 | // Otherwise select new device on the output mix. |
| 5279 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 5280 | invalidateStreams(mEngine->getStreamTypesForProductStrategy(ps)); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5281 | } else { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5282 | DeviceVector newDevices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
| 5283 | if (outputDesc->mUsePreferredMixerAttributes && outputDesc->devices() != newDevices) { |
| 5284 | // If the device is using preferred mixer attributes, the output need to reopen |
| 5285 | // with default configuration when the new selected devices are different from |
| 5286 | // current routing devices. |
| 5287 | outputsToReopen.emplace(mOutputs.keyAt(j), newDevices); |
| 5288 | continue; |
| 5289 | } |
| 5290 | setOutputDevices(outputDesc, newDevices, false); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5291 | } |
| 5292 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5293 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5294 | } |
| 5295 | |
| 5296 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 5297 | { |
| 5298 | // remove output routes associated with this uid |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5299 | std::vector<product_strategy_t> affectedStrategies; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5300 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5301 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 5302 | for (const auto& client : outputDesc->getClientIterable()) { |
| 5303 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 5304 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5305 | auto clientStrategy = client->strategy(); |
| 5306 | if (std::find(begin(affectedStrategies), end(affectedStrategies), clientStrategy) != |
| 5307 | end(affectedStrategies)) { |
| 5308 | continue; |
| 5309 | } |
| 5310 | affectedStrategies.push_back(client->strategy()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5311 | } |
| 5312 | } |
| 5313 | } |
| 5314 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5315 | for (const auto& strategy : affectedStrategies) { |
| 5316 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5317 | } |
| 5318 | |
| 5319 | // remove input routes associated with this uid |
| 5320 | SortedVector<audio_source_t> affectedSources; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5321 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5322 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 5323 | for (const auto& client : inputDesc->getClientIterable()) { |
| 5324 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 5325 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 5326 | affectedSources.add(client->source()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5327 | } |
| 5328 | } |
| 5329 | } |
| 5330 | // reroute inputs if necessary |
| 5331 | SortedVector<audio_io_handle_t> inputsToClose; |
| 5332 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5333 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 5334 | if (affectedSources.indexOf(inputDesc->source()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5335 | inputsToClose.add(inputDesc->mIoHandle); |
| 5336 | } |
| 5337 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5338 | for (const auto& input : inputsToClose) { |
| 5339 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5340 | } |
| 5341 | } |
| 5342 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5343 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 5344 | { |
| 5345 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5346 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5347 | if (sourceDesc->uid() == uid) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5348 | stopAudioSource(mAudioSources.keyAt(i)); |
| 5349 | } |
| 5350 | } |
| 5351 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5352 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5353 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 5354 | audio_io_handle_t *ioHandle, |
| 5355 | audio_devices_t *device) |
| 5356 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 5357 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 5358 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5359 | audio_attributes_t attr = { .source = AUDIO_SOURCE_HOTWORD }; |
Eric Laurent | cedd5b5 | 2023-03-22 00:03:31 +0000 | [diff] [blame] | 5360 | sp<DeviceDescriptor> deviceDesc = mEngine->getInputDeviceForAttributes(attr); |
| 5361 | if (deviceDesc == nullptr) { |
| 5362 | return INVALID_OPERATION; |
| 5363 | } |
| 5364 | *device = deviceDesc->type(); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5365 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 5366 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5367 | } |
| 5368 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5369 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5370 | const audio_attributes_t *attributes, |
| 5371 | audio_port_handle_t *portId, |
| 5372 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5373 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5374 | ALOGV("%s", __FUNCTION__); |
| 5375 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 5376 | |
| 5377 | if (source == NULL || attributes == NULL || portId == NULL) { |
| 5378 | ALOGW("%s invalid argument: source %p attributes %p handle %p", |
| 5379 | __FUNCTION__, source, attributes, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5380 | return BAD_VALUE; |
| 5381 | } |
| 5382 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5383 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 5384 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5385 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", |
| 5386 | __FUNCTION__, source->role, source->type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5387 | return INVALID_OPERATION; |
| 5388 | } |
| 5389 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5390 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5391 | mAvailableInputDevices.getDevice(source->ext.device.type, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5392 | String8(source->ext.device.address), |
| 5393 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5394 | if (srcDevice == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5395 | ALOGW("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5396 | return BAD_VALUE; |
| 5397 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5398 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 5399 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5400 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 5401 | sp<SourceClientDescriptor> sourceDesc = |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5402 | new SourceClientDescriptor(*portId, uid, *attributes, *source, srcDevice, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 5403 | mEngine->getStreamTypeForAttributes(*attributes), |
| 5404 | mEngine->getProductStrategyForAttributes(*attributes), |
| 5405 | toVolumeSource(*attributes)); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5406 | |
| 5407 | status_t status = connectAudioSource(sourceDesc); |
| 5408 | if (status == NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5409 | mAudioSources.add(*portId, sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5410 | } |
| 5411 | return status; |
| 5412 | } |
| 5413 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 5414 | sp<SourceClientDescriptor> AudioPolicyManager::startAudioSourceInternal( |
| 5415 | const struct audio_port_config *source, const audio_attributes_t *attributes, uid_t uid) |
| 5416 | { |
| 5417 | ALOGV("%s", __FUNCTION__); |
| 5418 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 5419 | |
| 5420 | status_t status = startAudioSource(source, attributes, &portId, uid); |
| 5421 | ALOGE_IF(status != OK, "%s: failed to start audio source (%d)", __func__, status); |
| 5422 | return mAudioSources.valueFor(portId); |
| 5423 | } |
| 5424 | |
| 5425 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5426 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5427 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5428 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5429 | |
| 5430 | // make sure we only have one patch per source. |
| 5431 | disconnectAudioSource(sourceDesc); |
| 5432 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5433 | audio_attributes_t attributes = sourceDesc->attributes(); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5434 | // May the device (dynamic) have been disconnected/reconnected, id has changed. |
| 5435 | sp<DeviceDescriptor> srcDevice = mAvailableInputDevices.getDevice( |
| 5436 | sourceDesc->srcDevice()->type(), |
| 5437 | String8(sourceDesc->srcDevice()->address().c_str()), |
| 5438 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5439 | DeviceVector sinkDevices = |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 5440 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false /*fromCache*/); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5441 | ALOG_ASSERT(!sinkDevices.isEmpty(), "connectAudioSource(): no device found for attributes"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5442 | sp<DeviceDescriptor> sinkDevice = sinkDevices.itemAt(0); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5443 | if (!mAvailableOutputDevices.contains(sinkDevice)) { |
| 5444 | ALOGE("%s Device %s not available", __func__, sinkDevice->toString().c_str()); |
| 5445 | return INVALID_OPERATION; |
| 5446 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5447 | PatchBuilder patchBuilder; |
| 5448 | patchBuilder.addSink(sinkDevice).addSource(srcDevice); |
| 5449 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5450 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5451 | return connectAudioSourceToSink( |
| 5452 | sourceDesc, sinkDevice, patchBuilder.patch(), handle, mUidCached, 0 /*delayMs*/); |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5453 | } |
| 5454 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5455 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5456 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5457 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); |
| 5458 | ALOGV("%s port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5459 | if (sourceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5460 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5461 | return BAD_VALUE; |
| 5462 | } |
| 5463 | status_t status = disconnectAudioSource(sourceDesc); |
| 5464 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5465 | mAudioSources.removeItem(portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5466 | return status; |
| 5467 | } |
| 5468 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5469 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 5470 | { |
| 5471 | if (mMasterMono == mono) { |
| 5472 | return NO_ERROR; |
| 5473 | } |
| 5474 | mMasterMono = mono; |
| 5475 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 5476 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 5477 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 5478 | // |
| 5479 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 5480 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 5481 | // play back offloaded. |
| 5482 | if (mMasterMono) { |
| 5483 | Vector<audio_io_handle_t> offloaded; |
| 5484 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 5485 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5486 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 5487 | offloaded.push(desc->mIoHandle); |
| 5488 | } |
| 5489 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5490 | for (const auto& handle : offloaded) { |
| 5491 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5492 | } |
| 5493 | } |
| 5494 | // update master mono for all remaining outputs |
| 5495 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 5496 | updateMono(mOutputs.keyAt(i)); |
| 5497 | } |
| 5498 | return NO_ERROR; |
| 5499 | } |
| 5500 | |
| 5501 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 5502 | { |
| 5503 | *mono = mMasterMono; |
| 5504 | return NO_ERROR; |
| 5505 | } |
| 5506 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 5507 | float AudioPolicyManager::getStreamVolumeDB( |
| 5508 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 5509 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5510 | return computeVolume(getVolumeCurves(stream), toVolumeSource(stream), index, {device}); |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 5511 | } |
| 5512 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5513 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 5514 | audio_format_t *surroundFormats, |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5515 | bool *surroundFormatsEnabled) |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5516 | { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5517 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && |
| 5518 | (surroundFormats == nullptr || surroundFormatsEnabled == nullptr))) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5519 | return BAD_VALUE; |
| 5520 | } |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5521 | ALOGV("%s() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p", |
| 5522 | __func__, *numSurroundFormats, surroundFormats, surroundFormatsEnabled); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5523 | |
| 5524 | size_t formatsWritten = 0; |
| 5525 | size_t formatsMax = *numSurroundFormats; |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 5526 | |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5527 | *numSurroundFormats = mConfig.getSurroundFormats().size(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5528 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5529 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5530 | for (const auto& format: mConfig.getSurroundFormats()) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5531 | if (formatsWritten < formatsMax) { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5532 | surroundFormats[formatsWritten] = format.first; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5533 | bool formatEnabled = true; |
| 5534 | switch (forceUse) { |
| 5535 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL: |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5536 | formatEnabled = mManualSurroundFormats.count(format.first) != 0; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5537 | break; |
| 5538 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER: |
| 5539 | formatEnabled = false; |
| 5540 | break; |
| 5541 | default: // AUTO or ALWAYS => true |
| 5542 | break; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5543 | } |
| 5544 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 5545 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5546 | } |
| 5547 | return NO_ERROR; |
| 5548 | } |
| 5549 | |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5550 | status_t AudioPolicyManager::getReportedSurroundFormats(unsigned int *numSurroundFormats, |
| 5551 | audio_format_t *surroundFormats) { |
| 5552 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) { |
| 5553 | return BAD_VALUE; |
| 5554 | } |
| 5555 | ALOGV("%s() numSurroundFormats %d surroundFormats %p", |
| 5556 | __func__, *numSurroundFormats, surroundFormats); |
| 5557 | |
| 5558 | size_t formatsWritten = 0; |
| 5559 | size_t formatsMax = *numSurroundFormats; |
| 5560 | std::unordered_set<audio_format_t> formats; // Uses primary surround formats only |
| 5561 | |
| 5562 | // Return formats from all device profiles that have already been resolved by |
| 5563 | // checkOutputsForDevice(). |
| 5564 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 5565 | sp<DeviceDescriptor> device = mAvailableOutputDevices[i]; |
| 5566 | audio_devices_t deviceType = device->type(); |
| 5567 | // Enabling/disabling formats are applied to only HDMI devices. So, this function |
| 5568 | // returns formats reported by HDMI devices. |
| 5569 | if (deviceType != AUDIO_DEVICE_OUT_HDMI) { |
| 5570 | continue; |
| 5571 | } |
| 5572 | // Formats reported by sink devices |
| 5573 | std::unordered_set<audio_format_t> formatset; |
| 5574 | if (auto it = mReportedFormatsMap.find(device); it != mReportedFormatsMap.end()) { |
| 5575 | formatset.insert(it->second.begin(), it->second.end()); |
| 5576 | } |
| 5577 | |
| 5578 | // Formats hard-coded in the in policy configuration file (if any). |
| 5579 | FormatVector encodedFormats = device->encodedFormats(); |
| 5580 | formatset.insert(encodedFormats.begin(), encodedFormats.end()); |
| 5581 | // Filter the formats which are supported by the vendor hardware. |
| 5582 | for (auto it = formatset.begin(); it != formatset.end(); ++it) { |
| 5583 | if (mConfig.getSurroundFormats().count(*it) != 0) { |
| 5584 | formats.insert(*it); |
| 5585 | } else { |
| 5586 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 5587 | if (pair.second.count(*it) != 0) { |
| 5588 | formats.insert(pair.first); |
| 5589 | break; |
| 5590 | } |
| 5591 | } |
| 5592 | } |
| 5593 | } |
| 5594 | } |
| 5595 | *numSurroundFormats = formats.size(); |
| 5596 | for (const auto& format: formats) { |
| 5597 | if (formatsWritten < formatsMax) { |
| 5598 | surroundFormats[formatsWritten++] = format; |
| 5599 | } |
| 5600 | } |
| 5601 | return NO_ERROR; |
| 5602 | } |
| 5603 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5604 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 5605 | { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5606 | ALOGV("%s() format 0x%X enabled %d", __func__, audioFormat, enabled); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 5607 | const auto& formatIter = mConfig.getSurroundFormats().find(audioFormat); |
| 5608 | if (formatIter == mConfig.getSurroundFormats().end()) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5609 | ALOGW("%s() format 0x%X is not a known surround format", __func__, audioFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5610 | return BAD_VALUE; |
| 5611 | } |
| 5612 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5613 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND) != |
| 5614 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5615 | ALOGW("%s() not in manual mode for surround sound format selection", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5616 | return INVALID_OPERATION; |
| 5617 | } |
| 5618 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5619 | if ((mManualSurroundFormats.count(audioFormat) != 0) == enabled) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5620 | return NO_ERROR; |
| 5621 | } |
| 5622 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5623 | std::unordered_set<audio_format_t> surroundFormatsBackup(mManualSurroundFormats); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5624 | if (enabled) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5625 | mManualSurroundFormats.insert(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 5626 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5627 | mManualSurroundFormats.insert(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5628 | } |
| 5629 | } else { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5630 | mManualSurroundFormats.erase(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 5631 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5632 | mManualSurroundFormats.erase(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5633 | } |
| 5634 | } |
| 5635 | |
| 5636 | sp<SwAudioOutputDescriptor> outputDesc; |
| 5637 | bool profileUpdated = false; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5638 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromType( |
| 5639 | AUDIO_DEVICE_OUT_HDMI); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5640 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 5641 | // Simulate reconnection to update enabled surround sound formats. |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5642 | String8 address = String8(hdmiOutputDevices[i]->address().c_str()); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 5643 | std::string name = hdmiOutputDevices[i]->getName(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5644 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 5645 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 5646 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5647 | name.c_str(), |
| 5648 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5649 | if (status != NO_ERROR) { |
| 5650 | continue; |
| 5651 | } |
| 5652 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 5653 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 5654 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5655 | name.c_str(), |
| 5656 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5657 | profileUpdated |= (status == NO_ERROR); |
| 5658 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5659 | // FIXME: Why doing this for input HDMI devices if we don't augment their reported formats? |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5660 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromType( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5661 | AUDIO_DEVICE_IN_HDMI); |
| 5662 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 5663 | // Simulate reconnection to update enabled surround sound formats. |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5664 | String8 address = String8(hdmiInputDevices[i]->address().c_str()); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 5665 | std::string name = hdmiInputDevices[i]->getName(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5666 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 5667 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 5668 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5669 | name.c_str(), |
| 5670 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5671 | if (status != NO_ERROR) { |
| 5672 | continue; |
| 5673 | } |
| 5674 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 5675 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 5676 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5677 | name.c_str(), |
| 5678 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5679 | profileUpdated |= (status == NO_ERROR); |
| 5680 | } |
| 5681 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5682 | if (!profileUpdated) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5683 | ALOGW("%s() no audio profiles updated, undoing surround formats change", __func__); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5684 | mManualSurroundFormats = std::move(surroundFormatsBackup); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5685 | } |
| 5686 | |
| 5687 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 5688 | } |
| 5689 | |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5690 | void AudioPolicyManager::setAppState(audio_port_handle_t portId, app_state_t state) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 5691 | { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5692 | ALOGV("%s(portId:%d, state:%d)", __func__, portId, state); |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 5693 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5694 | mInputs.valueAt(i)->setAppState(portId, state); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 5695 | } |
| 5696 | } |
| 5697 | |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 5698 | bool AudioPolicyManager::isHapticPlaybackSupported() |
| 5699 | { |
| 5700 | for (const auto& hwModule : mHwModules) { |
| 5701 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 5702 | for (const auto &outProfile : outputProfiles) { |
| 5703 | struct audio_port audioPort; |
| 5704 | outProfile->toAudioPort(&audioPort); |
| 5705 | for (size_t i = 0; i < audioPort.num_channel_masks; i++) { |
| 5706 | if (audioPort.channel_masks[i] & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 5707 | return true; |
| 5708 | } |
| 5709 | } |
| 5710 | } |
| 5711 | } |
| 5712 | return false; |
| 5713 | } |
| 5714 | |
Carter Hsu | 325a8eb | 2022-01-19 19:56:51 +0800 | [diff] [blame] | 5715 | bool AudioPolicyManager::isUltrasoundSupported() |
| 5716 | { |
| 5717 | bool hasUltrasoundOutput = false; |
| 5718 | bool hasUltrasoundInput = false; |
| 5719 | for (const auto& hwModule : mHwModules) { |
| 5720 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 5721 | if (!hasUltrasoundOutput) { |
| 5722 | for (const auto &outProfile : outputProfiles) { |
| 5723 | if (outProfile->getFlags() & AUDIO_OUTPUT_FLAG_ULTRASOUND) { |
| 5724 | hasUltrasoundOutput = true; |
| 5725 | break; |
| 5726 | } |
| 5727 | } |
| 5728 | } |
| 5729 | |
| 5730 | const InputProfileCollection &inputProfiles = hwModule->getInputProfiles(); |
| 5731 | if (!hasUltrasoundInput) { |
| 5732 | for (const auto &inputProfile : inputProfiles) { |
| 5733 | if (inputProfile->getFlags() & AUDIO_INPUT_FLAG_ULTRASOUND) { |
| 5734 | hasUltrasoundInput = true; |
| 5735 | break; |
| 5736 | } |
| 5737 | } |
| 5738 | } |
| 5739 | |
| 5740 | if (hasUltrasoundOutput && hasUltrasoundInput) |
| 5741 | return true; |
| 5742 | } |
| 5743 | return false; |
| 5744 | } |
| 5745 | |
Atneya Nair | 698f5ef | 2022-12-15 16:15:09 -0800 | [diff] [blame] | 5746 | bool AudioPolicyManager::isHotwordStreamSupported(bool lookbackAudio) |
| 5747 | { |
| 5748 | const auto mask = AUDIO_INPUT_FLAG_HOTWORD_TAP | |
| 5749 | (lookbackAudio ? AUDIO_INPUT_FLAG_HW_LOOKBACK : 0); |
| 5750 | for (const auto& hwModule : mHwModules) { |
| 5751 | const InputProfileCollection &inputProfiles = hwModule->getInputProfiles(); |
| 5752 | for (const auto &inputProfile : inputProfiles) { |
| 5753 | if ((inputProfile->getFlags() & mask) == mask) { |
| 5754 | return true; |
| 5755 | } |
| 5756 | } |
| 5757 | } |
| 5758 | return false; |
| 5759 | } |
| 5760 | |
Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 5761 | bool AudioPolicyManager::isCallScreenModeSupported() |
| 5762 | { |
| 5763 | return getConfig().isCallScreenModeSupported(); |
| 5764 | } |
| 5765 | |
| 5766 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5767 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5768 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5769 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5770 | if (!sourceDesc->isConnected()) { |
| 5771 | ALOGV("%s port Id %d already disconnected", __FUNCTION__, sourceDesc->portId()); |
| 5772 | return NO_ERROR; |
| 5773 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5774 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); |
| 5775 | if (swOutput != 0) { |
| 5776 | status_t status = stopSource(swOutput, sourceDesc); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 5777 | if (status == NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5778 | swOutput->stop(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 5779 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5780 | if (releaseOutput(sourceDesc->portId())) { |
| 5781 | // The output descriptor is reopened to query dynamic profiles. In that case, there is |
| 5782 | // no need to release audio patch here but just return NO_ERROR. |
| 5783 | return NO_ERROR; |
| 5784 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5785 | } else { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5786 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5787 | if (hwOutputDesc != 0) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5788 | // close Hwoutput and remove from mHwOutputs |
| 5789 | } else { |
| 5790 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 5791 | } |
| 5792 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5793 | status_t status = releaseAudioPatchInternal(sourceDesc->getPatchHandle(), 0, sourceDesc); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5794 | sourceDesc->disconnect(); |
| 5795 | return status; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5796 | } |
| 5797 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5798 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForAttributesOnOutput( |
| 5799 | audio_io_handle_t output, const audio_attributes_t &attr) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5800 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5801 | sp<SourceClientDescriptor> source; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5802 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5803 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5804 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5805 | if (followsSameRouting(attr, sourceDesc->attributes()) && |
| 5806 | outputDesc != 0 && outputDesc->mIoHandle == output) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5807 | source = sourceDesc; |
| 5808 | break; |
| 5809 | } |
| 5810 | } |
| 5811 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5812 | } |
| 5813 | |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5814 | bool AudioPolicyManager::canBeSpatializedInt(const audio_attributes_t *attr, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5815 | const audio_config_t *config, |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5816 | const AudioDeviceTypeAddrVector &devices) const |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5817 | { |
| 5818 | // The caller can have the audio attributes criteria ignored by either passing a null ptr or |
| 5819 | // the AUDIO_ATTRIBUTES_INITIALIZER value. |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5820 | // If attributes are specified, current policy is to only allow spatialization for media |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5821 | // and game usages. |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5822 | if (attr != nullptr && *attr != AUDIO_ATTRIBUTES_INITIALIZER) { |
| 5823 | if (attr->usage != AUDIO_USAGE_MEDIA && attr->usage != AUDIO_USAGE_GAME) { |
| 5824 | return false; |
| 5825 | } |
| 5826 | if ((attr->flags & (AUDIO_FLAG_CONTENT_SPATIALIZED | AUDIO_FLAG_NEVER_SPATIALIZE)) != 0) { |
| 5827 | return false; |
| 5828 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5829 | } |
| 5830 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5831 | sp<IOProfile> profile = |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5832 | getSpatializerOutputProfile(config, devices); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5833 | if (profile == nullptr) { |
| 5834 | return false; |
| 5835 | } |
| 5836 | |
| 5837 | // The caller can have the audio config criteria ignored by either passing a null ptr or |
| 5838 | // the AUDIO_CONFIG_INITIALIZER value. |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5839 | // If an audio config is specified, current policy is to only allow spatialization for |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5840 | // some positional channel masks. |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5841 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5842 | if (config != nullptr && *config != AUDIO_CONFIG_INITIALIZER) { |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5843 | if (!audio_is_channel_mask_spatialized(config->channel_mask)) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5844 | return false; |
| 5845 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5846 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5847 | return true; |
| 5848 | } |
| 5849 | |
| 5850 | void AudioPolicyManager::checkVirtualizerClientRoutes() { |
| 5851 | std::set<audio_stream_type_t> streamsToInvalidate; |
| 5852 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5853 | const sp<SwAudioOutputDescriptor>& desc = mOutputs[i]; |
| 5854 | for (const sp<TrackClientDescriptor>& client : desc->getClientIterable()) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5855 | audio_attributes_t attr = client->attributes(); |
| 5856 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false); |
| 5857 | AudioDeviceTypeAddrVector devicesTypeAddress = devices.toTypeAddrVector(); |
| 5858 | audio_config_base_t clientConfig = client->config(); |
| 5859 | audio_config_t config = audio_config_initializer(&clientConfig); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5860 | if (desc != mSpatializerOutput |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5861 | && canBeSpatializedInt(&attr, &config, devicesTypeAddress)) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5862 | streamsToInvalidate.insert(client->stream()); |
| 5863 | } |
| 5864 | } |
| 5865 | } |
| 5866 | |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 5867 | invalidateStreams(StreamTypeVector(streamsToInvalidate.begin(), streamsToInvalidate.end())); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5868 | } |
| 5869 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5870 | |
| 5871 | bool AudioPolicyManager::isOutputOnlyAvailableRouteToSomeDevice( |
| 5872 | const sp<SwAudioOutputDescriptor>& outputDesc) { |
| 5873 | if (outputDesc->isDuplicated()) { |
| 5874 | return false; |
| 5875 | } |
| 5876 | DeviceVector devices = outputDesc->supportedDevices(); |
| 5877 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5878 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5879 | if (desc == outputDesc || desc->isDuplicated()) { |
| 5880 | continue; |
| 5881 | } |
| 5882 | DeviceVector sharedDevices = desc->filterSupportedDevices(devices); |
| 5883 | if (!sharedDevices.isEmpty() |
| 5884 | && (desc->devicesSupportEncodedFormats(sharedDevices.types()) |
| 5885 | == outputDesc->devicesSupportEncodedFormats(sharedDevices.types()))) { |
| 5886 | return false; |
| 5887 | } |
| 5888 | } |
| 5889 | return true; |
| 5890 | } |
| 5891 | |
| 5892 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5893 | status_t AudioPolicyManager::getSpatializerOutput(const audio_config_base_t *mixerConfig, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5894 | const audio_attributes_t *attr, |
| 5895 | audio_io_handle_t *output) { |
| 5896 | *output = AUDIO_IO_HANDLE_NONE; |
| 5897 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5898 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(*attr, nullptr, false); |
| 5899 | AudioDeviceTypeAddrVector devicesTypeAddress = devices.toTypeAddrVector(); |
| 5900 | audio_config_t *configPtr = nullptr; |
| 5901 | audio_config_t config; |
| 5902 | if (mixerConfig != nullptr) { |
| 5903 | config = audio_config_initializer(mixerConfig); |
| 5904 | configPtr = &config; |
| 5905 | } |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5906 | if (!canBeSpatializedInt(attr, configPtr, devicesTypeAddress)) { |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5907 | ALOGV("%s provided attributes or mixer config cannot be spatialized", __func__); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5908 | return BAD_VALUE; |
| 5909 | } |
| 5910 | |
| 5911 | sp<IOProfile> profile = |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5912 | getSpatializerOutputProfile(configPtr, devicesTypeAddress); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5913 | if (profile == nullptr) { |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5914 | ALOGV("%s no suitable output profile for provided attributes or mixer config", __func__); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5915 | return BAD_VALUE; |
| 5916 | } |
| 5917 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5918 | std::vector<sp<SwAudioOutputDescriptor>> spatializerOutputs; |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5919 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5920 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5921 | if (!desc->isDuplicated() |
| 5922 | && (desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) { |
| 5923 | spatializerOutputs.push_back(desc); |
| 5924 | ALOGV("%s adding opened spatializer Output %d", __func__, desc->mIoHandle); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5925 | } |
| 5926 | } |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5927 | mSpatializerOutput.clear(); |
| 5928 | bool outputsChanged = false; |
| 5929 | for (const auto& desc : spatializerOutputs) { |
| 5930 | if (desc->mProfile == profile |
| 5931 | && (configPtr == nullptr |
| 5932 | || configPtr->channel_mask == desc->mMixerChannelMask)) { |
| 5933 | mSpatializerOutput = desc; |
| 5934 | ALOGV("%s reusing current spatializer output %d", __func__, desc->mIoHandle); |
| 5935 | } else { |
| 5936 | ALOGV("%s closing spatializerOutput output %d to match channel mask %#x" |
| 5937 | " and devices %s", __func__, desc->mIoHandle, |
| 5938 | configPtr != nullptr ? configPtr->channel_mask : 0, |
| 5939 | devices.toString().c_str()); |
| 5940 | closeOutput(desc->mIoHandle); |
| 5941 | outputsChanged = true; |
| 5942 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5943 | } |
| 5944 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5945 | if (mSpatializerOutput == nullptr) { |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5946 | sp<SwAudioOutputDescriptor> desc = |
| 5947 | openOutputWithProfileAndDevice(profile, devices, mixerConfig); |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5948 | if (desc != nullptr) { |
| 5949 | mSpatializerOutput = desc; |
| 5950 | outputsChanged = true; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5951 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5952 | } |
| 5953 | |
| 5954 | checkVirtualizerClientRoutes(); |
| 5955 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5956 | if (outputsChanged) { |
| 5957 | mPreviousOutputs = mOutputs; |
| 5958 | mpClientInterface->onAudioPortListUpdate(); |
| 5959 | } |
| 5960 | |
| 5961 | if (mSpatializerOutput == nullptr) { |
| 5962 | ALOGV("%s could not open spatializer output with requested config", __func__); |
| 5963 | return BAD_VALUE; |
| 5964 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5965 | *output = mSpatializerOutput->mIoHandle; |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5966 | ALOGV("%s returning new spatializer output %d", __func__, *output); |
| 5967 | return OK; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5968 | } |
| 5969 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5970 | status_t AudioPolicyManager::releaseSpatializerOutput(audio_io_handle_t output) { |
| 5971 | if (mSpatializerOutput == nullptr) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5972 | return INVALID_OPERATION; |
| 5973 | } |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5974 | if (mSpatializerOutput->mIoHandle != output) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5975 | return BAD_VALUE; |
| 5976 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5977 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5978 | if (!isOutputOnlyAvailableRouteToSomeDevice(mSpatializerOutput)) { |
| 5979 | ALOGV("%s closing spatializer output %d", __func__, mSpatializerOutput->mIoHandle); |
| 5980 | closeOutput(mSpatializerOutput->mIoHandle); |
| 5981 | //from now on mSpatializerOutput is null |
| 5982 | checkVirtualizerClientRoutes(); |
| 5983 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5984 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5985 | return NO_ERROR; |
| 5986 | } |
| 5987 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5988 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5989 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5990 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5991 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 5992 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 5993 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5994 | } |
| 5995 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 5996 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 5997 | if (std::string audioPolicyXmlConfigFile = audio_get_audio_policy_config_file(); |
| 5998 | !audioPolicyXmlConfigFile.empty()) { |
| 5999 | status_t ret = deserializeAudioPolicyFile(audioPolicyXmlConfigFile.c_str(), &config); |
| 6000 | if (ret == NO_ERROR) { |
| 6001 | config.setSource(audioPolicyXmlConfigFile); |
Cheney Ni | 6851adb | 2018-11-01 06:30:37 +0800 | [diff] [blame] | 6002 | } |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 6003 | return ret; |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 6004 | } |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 6005 | return BAD_VALUE; |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 6006 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 6007 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6008 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, |
| 6009 | bool /*forTesting*/) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6010 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 6011 | mUidCached(AID_AUDIOSERVER), // no need to call getuid(), there's only one of us running. |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6012 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6013 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6014 | mA2dpSuspended(false), |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6015 | mConfig(mHwModulesAll, mOutputDevicesAll, mInputDevicesAll, mDefaultOutputDevice), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6016 | mAudioPortGeneration(1), |
| 6017 | mBeaconMuteRefCount(0), |
| 6018 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 6019 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 6020 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6021 | mMasterMono(false), |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 6022 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6023 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6024 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 6025 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6026 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 6027 | : AudioPolicyManager(clientInterface, false /*forTesting*/) |
| 6028 | { |
| 6029 | loadConfig(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6030 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 6031 | |
Kevin Rocard | dfa1e8a | 2018-10-26 16:42:07 -0700 | [diff] [blame] | 6032 | void AudioPolicyManager::loadConfig() { |
| 6033 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 6034 | ALOGE("could not load audio policy configuration file, setting defaults"); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6035 | getConfig().setDefault(); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 6036 | } |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6037 | } |
| 6038 | |
| 6039 | status_t AudioPolicyManager::initialize() { |
Mikhail Naganov | 4783555 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 6040 | { |
| 6041 | auto engLib = EngineLibrary::load( |
| 6042 | "libaudiopolicyengine" + getConfig().getEngineLibraryNameSuffix() + ".so"); |
| 6043 | if (!engLib) { |
| 6044 | ALOGE("%s: Failed to load the engine library", __FUNCTION__); |
| 6045 | return NO_INIT; |
| 6046 | } |
| 6047 | mEngine = engLib->createEngine(); |
| 6048 | if (mEngine == nullptr) { |
| 6049 | ALOGE("%s: Failed to instantiate the APM engine", __FUNCTION__); |
| 6050 | return NO_INIT; |
| 6051 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6052 | } |
| 6053 | mEngine->setObserver(this); |
| 6054 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6055 | if (status != NO_ERROR) { |
| 6056 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 6057 | return status; |
| 6058 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6059 | |
Mikhail Naganov | e93ae28 | 2023-02-02 15:28:26 -0800 | [diff] [blame] | 6060 | // If microphones address is empty, set it according to device type |
| 6061 | for (size_t i = 0; i < mInputDevicesAll.size(); i++) { |
| 6062 | if (mInputDevicesAll[i]->address().empty()) { |
| 6063 | if (mInputDevicesAll[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 6064 | mInputDevicesAll[i]->setAddress(AUDIO_BOTTOM_MICROPHONE_ADDRESS); |
| 6065 | } else if (mInputDevicesAll[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { |
| 6066 | mInputDevicesAll[i]->setAddress(AUDIO_BACK_MICROPHONE_ADDRESS); |
| 6067 | } |
| 6068 | } |
| 6069 | } |
| 6070 | |
jiabin | 2923018 | 2023-04-04 21:02:36 +0000 | [diff] [blame] | 6071 | // The actual device selection cache will be updated when calling `updateDevicesAndOutputs` |
| 6072 | // at the end of this function. |
| 6073 | mEngine->initializeDeviceSelectionCache(); |
Eric Laurent | 1d69c87 | 2021-01-11 18:53:01 +0100 | [diff] [blame] | 6074 | mCommunnicationStrategy = mEngine->getProductStrategyForAttributes( |
| 6075 | mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL)); |
| 6076 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6077 | // after parsing the config, mOutputDevicesAll and mInputDevicesAll contain all known devices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6078 | // open all output streams needed to access attached devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6079 | onNewAudioModulesAvailableInt(nullptr /*newDevices*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6080 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6081 | // make sure default device is reachable |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6082 | if (mDefaultOutputDevice == 0 || !mAvailableOutputDevices.contains(mDefaultOutputDevice)) { |
| 6083 | ALOGE_IF(mDefaultOutputDevice != 0, "Default device %s is unreachable", |
| 6084 | mDefaultOutputDevice->toString().c_str()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6085 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6086 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 6087 | ALOGW_IF(mPrimaryOutput == nullptr, "The policy configuration does not declare a primary output"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6088 | |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 6089 | // Silence ALOGV statements |
| 6090 | property_set("log.tag." LOG_TAG, "D"); |
| 6091 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6092 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6093 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6094 | } |
| 6095 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6096 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6097 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6098 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6099 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6100 | } |
| 6101 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6102 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6103 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6104 | mAvailableOutputDevices.clear(); |
| 6105 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6106 | mOutputs.clear(); |
| 6107 | mInputs.clear(); |
| 6108 | mHwModules.clear(); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 6109 | mHwModulesAll.clear(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6110 | mManualSurroundFormats.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6111 | } |
| 6112 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6113 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6114 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 6115 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6116 | } |
| 6117 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6118 | // --- |
| 6119 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6120 | void AudioPolicyManager::onNewAudioModulesAvailable() |
| 6121 | { |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6122 | DeviceVector newDevices; |
| 6123 | onNewAudioModulesAvailableInt(&newDevices); |
| 6124 | if (!newDevices.empty()) { |
| 6125 | nextAudioPortGeneration(); |
| 6126 | mpClientInterface->onAudioPortListUpdate(); |
| 6127 | } |
| 6128 | } |
| 6129 | |
| 6130 | void AudioPolicyManager::onNewAudioModulesAvailableInt(DeviceVector *newDevices) |
| 6131 | { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6132 | for (const auto& hwModule : mHwModulesAll) { |
| 6133 | if (std::find(mHwModules.begin(), mHwModules.end(), hwModule) != mHwModules.end()) { |
| 6134 | continue; |
| 6135 | } |
| 6136 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); |
| 6137 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
| 6138 | ALOGW("could not open HW module %s", hwModule->getName()); |
| 6139 | continue; |
| 6140 | } |
| 6141 | mHwModules.push_back(hwModule); |
Dean Wheatley | 12a8713 | 2021-04-16 10:08:49 +1000 | [diff] [blame] | 6142 | // open all output streams needed to access attached devices. |
| 6143 | // direct outputs are closed immediately after checking the availability of attached devices |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6144 | // This also validates mAvailableOutputDevices list |
| 6145 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
| 6146 | if (!outProfile->canOpenNewIo()) { |
| 6147 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 6148 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 6149 | continue; |
| 6150 | } |
| 6151 | if (!outProfile->hasSupportedDevices()) { |
| 6152 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
| 6153 | continue; |
| 6154 | } |
Carter Hsu | 1a3364a | 2022-01-21 15:32:56 +0800 | [diff] [blame] | 6155 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0 || |
| 6156 | (outProfile->getFlags() & AUDIO_OUTPUT_FLAG_ULTRASOUND) != 0) { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6157 | mTtsOutputAvailable = true; |
| 6158 | } |
| 6159 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6160 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
| 6161 | DeviceVector availProfileDevices = supportedDevices.filter(mOutputDevicesAll); |
| 6162 | sp<DeviceDescriptor> supportedDevice = 0; |
| 6163 | if (supportedDevices.contains(mDefaultOutputDevice)) { |
| 6164 | supportedDevice = mDefaultOutputDevice; |
| 6165 | } else { |
| 6166 | // choose first device present in profile's SupportedDevices also part of |
| 6167 | // mAvailableOutputDevices. |
| 6168 | if (availProfileDevices.isEmpty()) { |
| 6169 | continue; |
| 6170 | } |
| 6171 | supportedDevice = availProfileDevices.itemAt(0); |
| 6172 | } |
| 6173 | if (!mOutputDevicesAll.contains(supportedDevice)) { |
| 6174 | continue; |
| 6175 | } |
| 6176 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 6177 | mpClientInterface); |
| 6178 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 6179 | status_t status = outputDesc->open(nullptr /* halConfig */, nullptr /* mixerConfig */, |
| 6180 | DeviceVector(supportedDevice), |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6181 | AUDIO_STREAM_DEFAULT, |
| 6182 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 6183 | if (status != NO_ERROR) { |
| 6184 | ALOGW("Cannot open output stream for devices %s on hw module %s", |
| 6185 | supportedDevice->toString().c_str(), hwModule->getName()); |
| 6186 | continue; |
| 6187 | } |
| 6188 | for (const auto &device : availProfileDevices) { |
| 6189 | // give a valid ID to an attached device once confirmed it is reachable |
| 6190 | if (!device->isAttached()) { |
| 6191 | device->attach(hwModule); |
| 6192 | mAvailableOutputDevices.add(device); |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 6193 | device->setEncapsulationInfoFromHal(mpClientInterface); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6194 | if (newDevices) newDevices->add(device); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6195 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 6196 | } |
| 6197 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 6198 | if (mPrimaryOutput == nullptr && |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6199 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 6200 | mPrimaryOutput = outputDesc; |
| 6201 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6202 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6203 | outputDesc->close(); |
| 6204 | } else { |
| 6205 | addOutput(output, outputDesc); |
| 6206 | setOutputDevices(outputDesc, |
| 6207 | DeviceVector(supportedDevice), |
| 6208 | true, |
| 6209 | 0, |
| 6210 | NULL); |
| 6211 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6212 | } |
| 6213 | // open input streams needed to access attached devices to validate |
| 6214 | // mAvailableInputDevices list |
| 6215 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
| 6216 | if (!inProfile->canOpenNewIo()) { |
| 6217 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 6218 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 6219 | continue; |
| 6220 | } |
| 6221 | if (!inProfile->hasSupportedDevices()) { |
| 6222 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
| 6223 | continue; |
| 6224 | } |
| 6225 | // chose first device present in profile's SupportedDevices also part of |
| 6226 | // available input devices |
| 6227 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
| 6228 | DeviceVector availProfileDevices = supportedDevices.filter(mInputDevicesAll); |
| 6229 | if (availProfileDevices.isEmpty()) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 6230 | ALOGV("%s: Input device list is empty! for profile %s", |
| 6231 | __func__, inProfile->getTagName().c_str()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6232 | continue; |
| 6233 | } |
| 6234 | sp<AudioInputDescriptor> inputDesc = |
| 6235 | new AudioInputDescriptor(inProfile, mpClientInterface); |
| 6236 | |
| 6237 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 6238 | status_t status = inputDesc->open(nullptr, |
| 6239 | availProfileDevices.itemAt(0), |
| 6240 | AUDIO_SOURCE_MIC, |
| 6241 | AUDIO_INPUT_FLAG_NONE, |
| 6242 | &input); |
| 6243 | if (status != NO_ERROR) { |
| 6244 | ALOGW("Cannot open input stream for device %s on hw module %s", |
| 6245 | availProfileDevices.toString().c_str(), |
| 6246 | hwModule->getName()); |
| 6247 | continue; |
| 6248 | } |
| 6249 | for (const auto &device : availProfileDevices) { |
| 6250 | // give a valid ID to an attached device once confirmed it is reachable |
| 6251 | if (!device->isAttached()) { |
| 6252 | device->attach(hwModule); |
| 6253 | device->importAudioPortAndPickAudioProfile(inProfile, true); |
| 6254 | mAvailableInputDevices.add(device); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6255 | if (newDevices) newDevices->add(device); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6256 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 6257 | } |
| 6258 | } |
| 6259 | inputDesc->close(); |
| 6260 | } |
| 6261 | } |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6262 | |
| 6263 | // Check if spatializer outputs can be closed until used. |
| 6264 | // mOutputs vector never contains duplicated outputs at this point. |
| 6265 | std::vector<audio_io_handle_t> outputsClosed; |
| 6266 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6267 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 6268 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0 |
| 6269 | && !isOutputOnlyAvailableRouteToSomeDevice(desc)) { |
| 6270 | outputsClosed.push_back(desc->mIoHandle); |
| 6271 | desc->close(); |
| 6272 | } |
| 6273 | } |
| 6274 | for (auto output : outputsClosed) { |
| 6275 | removeOutput(output); |
| 6276 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6277 | } |
| 6278 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 6279 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 6280 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6281 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6282 | mOutputs.add(output, outputDesc); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6283 | applyStreamVolumes(outputDesc, DeviceTypeSet(), 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 6284 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6285 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6286 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6287 | } |
| 6288 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6289 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 6290 | { |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 6291 | if (mPrimaryOutput != 0 && mPrimaryOutput == mOutputs.valueFor(output)) { |
| 6292 | ALOGV("%s: removing primary output", __func__); |
| 6293 | mPrimaryOutput = nullptr; |
| 6294 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6295 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6296 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6297 | } |
| 6298 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 6299 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 6300 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6301 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6302 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6303 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6304 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6305 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6306 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6307 | audio_policy_dev_state_t state, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6308 | SortedVector<audio_io_handle_t>& outputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6309 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6310 | audio_devices_t deviceType = device->type(); |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 6311 | const String8 &address = String8(device->address().c_str()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6312 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6313 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6314 | if (audio_device_is_digital(deviceType)) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6315 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6316 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6317 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6318 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 6319 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 6320 | // first call getAudioPort to get the supported attributes from the HAL |
| 6321 | struct audio_port_v7 port = {}; |
| 6322 | device->toAudioPort(&port); |
| 6323 | status_t status = mpClientInterface->getAudioPort(&port); |
| 6324 | if (status == NO_ERROR) { |
| 6325 | device->importAudioPort(port); |
| 6326 | } |
| 6327 | |
| 6328 | // then list already open outputs that can be routed to this device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6329 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6330 | desc = mOutputs.valueAt(i); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6331 | if (!desc->isDuplicated() && desc->supportsDevice(device) |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6332 | && desc->devicesSupportEncodedFormats({deviceType})) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6333 | ALOGV("checkOutputsForDevice(): adding opened output %d on device %s", |
| 6334 | mOutputs.keyAt(i), device->toString().c_str()); |
| 6335 | outputs.add(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6336 | } |
| 6337 | } |
| 6338 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6339 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6340 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6341 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 6342 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6343 | if (profile->supportsDevice(device)) { |
| 6344 | profiles.add(profile); |
| 6345 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 6346 | j, hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6347 | } |
| 6348 | } |
| 6349 | } |
| 6350 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 6351 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6352 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6353 | if (profiles.isEmpty() && outputs.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6354 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6355 | return BAD_VALUE; |
| 6356 | } |
| 6357 | |
| 6358 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 6359 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 6360 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6361 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6362 | |
| 6363 | // nothing to do if one output is already opened for this profile |
| 6364 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6365 | for (j = 0; j < outputs.size(); j++) { |
| 6366 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6367 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 6368 | // matching profile: save the sample rates, format and channel masks supported |
| 6369 | // by the profile in our device descriptor |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6370 | if (audio_device_is_digital(deviceType)) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6371 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6372 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6373 | break; |
| 6374 | } |
| 6375 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6376 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6377 | continue; |
| 6378 | } |
| 6379 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 6380 | if (!profile->canOpenNewIo()) { |
| 6381 | ALOGW("Max Output number %u already opened for this profile %s", |
| 6382 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 6383 | continue; |
| 6384 | } |
| 6385 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 6386 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 6387 | deviceType, address.string(), profile.get(), profile->getName().c_str()); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6388 | desc = openOutputWithProfileAndDevice(profile, DeviceVector(device)); |
| 6389 | audio_io_handle_t output = desc == nullptr ? AUDIO_IO_HANDLE_NONE : desc->mIoHandle; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6390 | if (output == AUDIO_IO_HANDLE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6391 | ALOGW("checkOutputsForDevice() could not open output for device %x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6392 | profiles.removeAt(profile_index); |
| 6393 | profile_index--; |
| 6394 | } else { |
| 6395 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6396 | // Load digital format info only for digital devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6397 | if (audio_device_is_digital(deviceType)) { |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6398 | // TODO: when getAudioPort is ready, it may not be needed to import the audio |
| 6399 | // port but just pick audio profile |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6400 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6401 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 6402 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6403 | if (device_distinguishes_on_address(deviceType)) { |
| 6404 | ALOGV("checkOutputsForDevice(): setOutputDevices %s", |
| 6405 | device->toString().c_str()); |
| 6406 | setOutputDevices(desc, DeviceVector(device), true/*force*/, 0/*delay*/, |
| 6407 | NULL/*patch handle*/); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6408 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6409 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 6410 | } |
| 6411 | } |
| 6412 | |
| 6413 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6414 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6415 | return BAD_VALUE; |
| 6416 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6417 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6418 | // check if one opened output is not needed any more after disconnecting one device |
| 6419 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6420 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6421 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6422 | // exact match on device |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6423 | if (device_distinguishes_on_address(deviceType) && desc->supportsDevice(device) |
Francois Gaffie | c7d4c22 | 2021-12-02 11:12:52 +0100 | [diff] [blame] | 6424 | && desc->containsSingleDeviceSupportingEncodedFormats(device)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6425 | outputs.add(mOutputs.keyAt(i)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6426 | } else if (!mAvailableOutputDevices.containsAtLeastOne(desc->supportedDevices())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6427 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 6428 | mOutputs.keyAt(i)); |
| 6429 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6430 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6431 | } |
| 6432 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6433 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6434 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6435 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 6436 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6437 | if (!profile->supportsDevice(device)) { |
| 6438 | continue; |
| 6439 | } |
| 6440 | ALOGV("checkOutputsForDevice(): " |
| 6441 | "clearing direct output profile %zu on module %s", |
| 6442 | j, hwModule->getName()); |
| 6443 | profile->clearAudioProfiles(); |
| 6444 | if (!profile->hasDynamicAudioProfile()) { |
| 6445 | continue; |
| 6446 | } |
| 6447 | // When a device is disconnected, if there is an IOProfile that contains dynamic |
| 6448 | // profiles and supports the disconnected device, call getAudioPort to repopulate |
| 6449 | // the capabilities of the devices that is supported by the IOProfile. |
| 6450 | for (const auto& supportedDevice : profile->getSupportedDevices()) { |
| 6451 | if (supportedDevice == device || |
| 6452 | !mAvailableOutputDevices.contains(supportedDevice)) { |
| 6453 | continue; |
| 6454 | } |
| 6455 | struct audio_port_v7 port; |
| 6456 | supportedDevice->toAudioPort(&port); |
| 6457 | status_t status = mpClientInterface->getAudioPort(&port); |
| 6458 | if (status == NO_ERROR) { |
| 6459 | supportedDevice->importAudioPort(port); |
| 6460 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6461 | } |
| 6462 | } |
| 6463 | } |
| 6464 | } |
| 6465 | return NO_ERROR; |
| 6466 | } |
| 6467 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6468 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& device, |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6469 | audio_policy_dev_state_t state) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6470 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6471 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6472 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6473 | if (audio_device_is_digital(device->type())) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6474 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6475 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6476 | } |
| 6477 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6478 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
jiabin | bf5f426 | 2023-04-12 21:48:34 +0000 | [diff] [blame] | 6479 | // first call getAudioPort to get the supported attributes from the HAL |
| 6480 | struct audio_port_v7 port = {}; |
| 6481 | device->toAudioPort(&port); |
| 6482 | status_t status = mpClientInterface->getAudioPort(&port); |
| 6483 | if (status == NO_ERROR) { |
| 6484 | device->importAudioPort(port); |
| 6485 | } |
| 6486 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6487 | // look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6488 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6489 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6490 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6491 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6492 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6493 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6494 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6495 | if (profile->supportsDevice(device)) { |
| 6496 | profiles.add(profile); |
| 6497 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 6498 | profile_index, hwModule->getName()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6499 | } |
| 6500 | } |
| 6501 | } |
| 6502 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6503 | if (profiles.isEmpty()) { |
| 6504 | ALOGW("%s: No input profile available for device %s", |
| 6505 | __func__, device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6506 | return BAD_VALUE; |
| 6507 | } |
| 6508 | |
| 6509 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 6510 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 6511 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 6512 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6513 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 6514 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6515 | // nothing to do if one input is already opened for this profile |
| 6516 | size_t input_index; |
| 6517 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 6518 | desc = mInputs.valueAt(input_index); |
| 6519 | if (desc->mProfile == profile) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6520 | if (audio_device_is_digital(device->type())) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6521 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6522 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6523 | break; |
| 6524 | } |
| 6525 | } |
| 6526 | if (input_index != mInputs.size()) { |
| 6527 | continue; |
| 6528 | } |
| 6529 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 6530 | if (!profile->canOpenNewIo()) { |
| 6531 | ALOGW("Max Input number %u already opened for this profile %s", |
| 6532 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 6533 | continue; |
| 6534 | } |
| 6535 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6536 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6537 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
jiabin | bf5f426 | 2023-04-12 21:48:34 +0000 | [diff] [blame] | 6538 | status = desc->open(nullptr, device, AUDIO_SOURCE_MIC, AUDIO_INPUT_FLAG_NONE, &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6539 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6540 | if (status == NO_ERROR) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 6541 | const String8& address = String8(device->address().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6542 | if (!address.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6543 | char *param = audio_device_address_to_parameter(device->type(), address); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6544 | mpClientInterface->setParameters(input, String8(param)); |
| 6545 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6546 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6547 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6548 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6549 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6550 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6551 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6552 | } |
| 6553 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6554 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6555 | addInput(input, desc); |
| 6556 | } |
| 6557 | } // endif input != 0 |
| 6558 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6559 | if (input == AUDIO_IO_HANDLE_NONE) { |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 6560 | ALOGW("%s could not open input for device %s", __func__, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6561 | device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6562 | profiles.removeAt(profile_index); |
| 6563 | profile_index--; |
| 6564 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6565 | if (audio_device_is_digital(device->type())) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6566 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6567 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6568 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 6569 | } |
| 6570 | } // end scan profiles |
| 6571 | |
| 6572 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6573 | ALOGW("%s: No input available for device %s", __func__, device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6574 | return BAD_VALUE; |
| 6575 | } |
| 6576 | } else { |
| 6577 | // Disconnect |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6578 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 6579 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6580 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6581 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6582 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6583 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6584 | if (profile->supportsDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 6585 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 6586 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6587 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6588 | } |
| 6589 | } |
| 6590 | } |
| 6591 | } // end disconnect |
| 6592 | |
| 6593 | return NO_ERROR; |
| 6594 | } |
| 6595 | |
| 6596 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6597 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6598 | { |
| 6599 | ALOGV("closeOutput(%d)", output); |
| 6600 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6601 | sp<SwAudioOutputDescriptor> closingOutput = mOutputs.valueFor(output); |
| 6602 | if (closingOutput == NULL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6603 | ALOGW("closeOutput() unknown output %d", output); |
| 6604 | return; |
| 6605 | } |
Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 6606 | const bool closingOutputWasActive = closingOutput->isActive(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6607 | mPolicyMixes.closeOutput(closingOutput); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6608 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6609 | // look for duplicated outputs connected to the output being removed. |
| 6610 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6611 | sp<SwAudioOutputDescriptor> dupOutput = mOutputs.valueAt(i); |
| 6612 | if (dupOutput->isDuplicated() && |
| 6613 | (dupOutput->mOutput1 == closingOutput || dupOutput->mOutput2 == closingOutput)) { |
| 6614 | sp<SwAudioOutputDescriptor> remainingOutput = |
| 6615 | dupOutput->mOutput1 == closingOutput ? dupOutput->mOutput2 : dupOutput->mOutput1; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6616 | // As all active tracks on duplicated output will be deleted, |
| 6617 | // and as they were also referenced on the other output, the reference |
| 6618 | // count for their stream type must be adjusted accordingly on |
| 6619 | // the other output. |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6620 | const bool wasActive = remainingOutput->isActive(); |
| 6621 | // Note: no-op on the closing output where all clients has already been set inactive |
| 6622 | dupOutput->setAllClientsInactive(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 6623 | // stop() will be a no op if the output is still active but is needed in case all |
| 6624 | // active streams refcounts where cleared above |
| 6625 | if (wasActive) { |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6626 | remainingOutput->stop(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 6627 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6628 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 6629 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 6630 | |
| 6631 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6632 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6633 | } |
| 6634 | } |
| 6635 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6636 | nextAudioPortGeneration(); |
| 6637 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6638 | ssize_t index = mAudioPatches.indexOfKey(closingOutput->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6639 | if (index >= 0) { |
| 6640 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6641 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 6642 | patchDesc->getAfHandle(), 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6643 | mAudioPatches.removeItemsAt(index); |
| 6644 | mpClientInterface->onAudioPatchListUpdate(); |
| 6645 | } |
| 6646 | |
Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 6647 | if (closingOutputWasActive) { |
| 6648 | closingOutput->stop(); |
| 6649 | } |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6650 | closingOutput->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6651 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6652 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6653 | mPreviousOutputs = mOutputs; |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 6654 | if (closingOutput == mSpatializerOutput) { |
| 6655 | mSpatializerOutput.clear(); |
| 6656 | } |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6657 | |
| 6658 | // MSD patches may have been released to support a non-MSD direct output. Reset MSD patch if |
| 6659 | // no direct outputs are open. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6660 | if (!getMsdAudioOutDevices().isEmpty()) { |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6661 | bool directOutputOpen = false; |
| 6662 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6663 | if (mOutputs[i]->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 6664 | directOutputOpen = true; |
| 6665 | break; |
| 6666 | } |
| 6667 | } |
| 6668 | if (!directOutputOpen) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 6669 | ALOGV("no direct outputs open, reset MSD patches"); |
| 6670 | // TODO: The MSD patches to be established here may differ to current MSD patches due to |
| 6671 | // how output devices for patching are resolved. Avoid by caching and reusing the |
| 6672 | // arguments to mEngine->getOutputDevicesForAttributes() when resolving which output |
| 6673 | // devices to patch to. This may be complicated by the fact that devices may become |
| 6674 | // unavailable. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 6675 | setMsdOutputPatches(); |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6676 | } |
| 6677 | } |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6678 | } |
| 6679 | |
| 6680 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 6681 | { |
| 6682 | ALOGV("closeInput(%d)", input); |
| 6683 | |
| 6684 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 6685 | if (inputDesc == NULL) { |
| 6686 | ALOGW("closeInput() unknown input %d", input); |
| 6687 | return; |
| 6688 | } |
| 6689 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6690 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6691 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6692 | sp<DeviceDescriptor> device = inputDesc->getDevice(); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 6693 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6694 | if (index >= 0) { |
| 6695 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6696 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 6697 | patchDesc->getAfHandle(), 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6698 | mAudioPatches.removeItemsAt(index); |
| 6699 | mpClientInterface->onAudioPatchListUpdate(); |
| 6700 | } |
| 6701 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6702 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6703 | mInputs.removeItem(input); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6704 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6705 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 6706 | if (primaryInputDevices.contains(device) && |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6707 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 6708 | mpClientInterface->setSoundTriggerCaptureState(false); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6709 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6710 | } |
| 6711 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6712 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevices( |
| 6713 | const DeviceVector &devices, |
| 6714 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6715 | { |
| 6716 | SortedVector<audio_io_handle_t> outputs; |
| 6717 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6718 | ALOGVV("%s() devices %s", __func__, devices.toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6719 | for (size_t i = 0; i < openOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6720 | ALOGVV("output %zu isDuplicated=%d device=%s", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 6721 | i, openOutputs.valueAt(i)->isDuplicated(), |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6722 | openOutputs.valueAt(i)->supportedDevices().toString().c_str()); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6723 | if (openOutputs.valueAt(i)->supportsAllDevices(devices) |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6724 | && openOutputs.valueAt(i)->devicesSupportEncodedFormats(devices.types())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6725 | ALOGVV("%s() found output %d", __func__, openOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6726 | outputs.add(openOutputs.keyAt(i)); |
| 6727 | } |
| 6728 | } |
| 6729 | return outputs; |
| 6730 | } |
| 6731 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6732 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 6733 | { |
| 6734 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 6735 | // output is suspended before any tracks are moved to it |
| 6736 | checkA2dpSuspend(); |
| 6737 | checkOutputForAllStrategies(); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6738 | checkSecondaryOutputs(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 6739 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6740 | updateDevicesAndOutputs(); |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 6741 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 6742 | // TODO: The MSD patches to be established here may differ to current MSD patches due to how |
| 6743 | // output devices for patching are resolved. Nevertheless, AudioTracks affected by device |
| 6744 | // configuration changes will ultimately be rerouted correctly. We can still avoid |
| 6745 | // unnecessary rerouting by caching and reusing the arguments to |
| 6746 | // mEngine->getOutputDevicesForAttributes() when resolving which output devices to patch to. |
| 6747 | // This may be complicated by the fact that devices may become unavailable. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 6748 | setMsdOutputPatches(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 6749 | } |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 6750 | // an event that changed routing likely occurred, inform upper layers |
| 6751 | mpClientInterface->onRoutingUpdated(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6752 | } |
| 6753 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6754 | bool AudioPolicyManager::followsSameRouting(const audio_attributes_t &lAttr, |
| 6755 | const audio_attributes_t &rAttr) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6756 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6757 | return mEngine->getProductStrategyForAttributes(lAttr) == |
| 6758 | mEngine->getProductStrategyForAttributes(rAttr); |
| 6759 | } |
| 6760 | |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6761 | void AudioPolicyManager::checkAudioSourceForAttributes(const audio_attributes_t &attr) |
| 6762 | { |
| 6763 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 6764 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 6765 | if (sourceDesc != nullptr && followsSameRouting(attr, sourceDesc->attributes()) |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 6766 | && sourceDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6767 | && !isCallRxAudioSource(sourceDesc) && !sourceDesc->isInternal()) { |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6768 | connectAudioSource(sourceDesc); |
| 6769 | } |
| 6770 | } |
| 6771 | } |
| 6772 | |
| 6773 | void AudioPolicyManager::clearAudioSourcesForOutput(audio_io_handle_t output) |
| 6774 | { |
| 6775 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 6776 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 6777 | if (sourceDesc != nullptr && sourceDesc->swOutput().promote() != nullptr |
| 6778 | && sourceDesc->swOutput().promote()->mIoHandle == output) { |
| 6779 | disconnectAudioSource(sourceDesc); |
| 6780 | } |
| 6781 | } |
| 6782 | } |
| 6783 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6784 | void AudioPolicyManager::checkOutputForAttributes(const audio_attributes_t &attr) |
| 6785 | { |
| 6786 | auto psId = mEngine->getProductStrategyForAttributes(attr); |
| 6787 | |
| 6788 | DeviceVector oldDevices = mEngine->getOutputDevicesForAttributes(attr, 0, true /*fromCache*/); |
| 6789 | DeviceVector newDevices = mEngine->getOutputDevicesForAttributes(attr, 0, false /*fromCache*/); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 6790 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6791 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevices(oldDevices, mPreviousOutputs); |
| 6792 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevices(newDevices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6793 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6794 | uint32_t maxLatency = 0; |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 6795 | bool unneededUsePrimaryOutputFromPolicyMixes = false; |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6796 | std::vector<sp<SwAudioOutputDescriptor>> invalidatedOutputs; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6797 | // take into account dynamic audio policies related changes: if a client is now associated |
| 6798 | // to a different policy mix than at creation time, invalidate corresponding stream |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6799 | for (size_t i = 0; i < mPreviousOutputs.size(); i++) { |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6800 | const sp<SwAudioOutputDescriptor>& desc = mPreviousOutputs.valueAt(i); |
| 6801 | if (desc->isDuplicated()) { |
| 6802 | continue; |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 6803 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6804 | for (const sp<TrackClientDescriptor>& client : desc->getClientIterable()) { |
| 6805 | if (mEngine->getProductStrategyForAttributes(client->attributes()) != psId) { |
| 6806 | continue; |
| 6807 | } |
| 6808 | sp<AudioPolicyMix> primaryMix; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 6809 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->config(), |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 6810 | client->uid(), client->session(), client->flags(), mAvailableOutputDevices, |
| 6811 | nullptr /* requestedDevice */, primaryMix, nullptr /* secondaryMixes */, |
| 6812 | unneededUsePrimaryOutputFromPolicyMixes); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6813 | if (status != OK) { |
| 6814 | continue; |
| 6815 | } |
yucliu | f4de36d | 2020-09-14 14:57:56 -0700 | [diff] [blame] | 6816 | if (client->getPrimaryMix() != primaryMix || client->hasLostPrimaryMix()) { |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6817 | if (desc->isStrategyActive(psId) && maxLatency < desc->latency()) { |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6818 | maxLatency = desc->latency(); |
| 6819 | } |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6820 | invalidatedOutputs.push_back(desc); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6821 | } |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 6822 | } |
| 6823 | } |
| 6824 | |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6825 | if (srcOutputs != dstOutputs || !invalidatedOutputs.empty()) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6826 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 6827 | // audio from invalidated tracks will be rendered when unmuting |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6828 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 6829 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6830 | if (desc == nullptr) continue; |
| 6831 | |
| 6832 | if (desc->isStrategyActive(psId) && maxLatency < desc->latency()) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6833 | maxLatency = desc->latency(); |
| 6834 | } |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6835 | |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6836 | bool invalidate = false; |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6837 | for (auto client : desc->clientsList(false /*activeOnly*/)) { |
Eric Laurent | 78aade8 | 2019-09-13 18:55:08 -0700 | [diff] [blame] | 6838 | if (desc->isDuplicated() || !desc->mProfile->isDirectOutput()) { |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6839 | // a client on a non direct outputs has necessarily a linear PCM format |
| 6840 | // so we can call selectOutput() safely |
| 6841 | const audio_io_handle_t newOutput = selectOutput(dstOutputs, |
| 6842 | client->flags(), |
| 6843 | client->config().format, |
| 6844 | client->config().channel_mask, |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 6845 | client->config().sample_rate, |
| 6846 | client->session()); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6847 | if (newOutput != srcOut) { |
| 6848 | invalidate = true; |
| 6849 | break; |
| 6850 | } |
| 6851 | } else { |
| 6852 | sp<IOProfile> profile = getProfileForOutput(newDevices, |
| 6853 | client->config().sample_rate, |
| 6854 | client->config().format, |
| 6855 | client->config().channel_mask, |
| 6856 | client->flags(), |
| 6857 | true /* directOnly */); |
| 6858 | if (profile != desc->mProfile) { |
| 6859 | invalidate = true; |
| 6860 | break; |
| 6861 | } |
| 6862 | } |
| 6863 | } |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6864 | // mute strategy while moving tracks from one output to another |
| 6865 | if (invalidate) { |
| 6866 | invalidatedOutputs.push_back(desc); |
| 6867 | if (desc->isStrategyActive(psId)) { |
| 6868 | setStrategyMute(psId, true, desc); |
| 6869 | setStrategyMute(psId, false, desc, maxLatency * LATENCY_MUTE_FACTOR, |
| 6870 | newDevices.types()); |
| 6871 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6872 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6873 | sp<SourceClientDescriptor> source = getSourceForAttributesOnOutput(srcOut, attr); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6874 | if (source != nullptr && !isCallRxAudioSource(source) && !source->isInternal()) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6875 | connectAudioSource(source); |
| 6876 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6877 | } |
| 6878 | |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6879 | ALOGV_IF(!(srcOutputs.isEmpty() || dstOutputs.isEmpty()), |
| 6880 | "%s: strategy %d, moving from output %s to output %s", __func__, psId, |
| 6881 | std::to_string(srcOutputs[0]).c_str(), |
| 6882 | std::to_string(dstOutputs[0]).c_str()); |
| 6883 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6884 | // Move effects associated to this stream from previous output to new output |
| 6885 | if (followsSameRouting(attr, attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6886 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6887 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6888 | // Move tracks associated to this stream (and linked) from previous output to new output |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6889 | if (!invalidatedOutputs.empty()) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 6890 | invalidateStreams(mEngine->getStreamTypesForProductStrategy(psId)); |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6891 | for (sp<SwAudioOutputDescriptor> desc : invalidatedOutputs) { |
jiabin | 4925685 | 2022-03-09 11:21:35 -0800 | [diff] [blame] | 6892 | desc->setTracksInvalidatedStatusByStrategy(psId); |
| 6893 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6894 | } |
| 6895 | } |
| 6896 | } |
| 6897 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6898 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6899 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6900 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { |
| 6901 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); |
| 6902 | checkOutputForAttributes(attributes); |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6903 | checkAudioSourceForAttributes(attributes); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6904 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6905 | } |
| 6906 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6907 | void AudioPolicyManager::checkSecondaryOutputs() { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 6908 | PortHandleVector clientsToInvalidate; |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6909 | TrackSecondaryOutputsMap trackSecondaryOutputs; |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 6910 | bool unneededUsePrimaryOutputFromPolicyMixes = false; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6911 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6912 | const sp<SwAudioOutputDescriptor>& outputDescriptor = mOutputs[i]; |
| 6913 | for (const sp<TrackClientDescriptor>& client : outputDescriptor->getClientIterable()) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6914 | sp<AudioPolicyMix> primaryMix; |
| 6915 | std::vector<sp<AudioPolicyMix>> secondaryMixes; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 6916 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->config(), |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 6917 | client->uid(), client->session(), client->flags(), mAvailableOutputDevices, |
| 6918 | nullptr /* requestedDevice */, primaryMix, &secondaryMixes, |
| 6919 | unneededUsePrimaryOutputFromPolicyMixes); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6920 | std::vector<sp<SwAudioOutputDescriptor>> secondaryDescs; |
| 6921 | for (auto &secondaryMix : secondaryMixes) { |
| 6922 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); |
| 6923 | if (outputDesc != nullptr && |
| 6924 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { |
| 6925 | secondaryDescs.push_back(outputDesc); |
| 6926 | } |
| 6927 | } |
| 6928 | |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 6929 | if (status != OK && |
| 6930 | (client->flags() & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == AUDIO_OUTPUT_FLAG_NONE) { |
| 6931 | // When it failed to query secondary output, only invalidate the client that is not |
| 6932 | // MMAP. The reason is that MMAP stream will not support secondary output. |
| 6933 | clientsToInvalidate.push_back(client->portId()); |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6934 | } else if (!std::equal( |
| 6935 | client->getSecondaryOutputs().begin(), |
| 6936 | client->getSecondaryOutputs().end(), |
| 6937 | secondaryDescs.begin(), secondaryDescs.end())) { |
jiabin | a528106 | 2021-11-23 00:10:23 +0000 | [diff] [blame] | 6938 | if (!audio_is_linear_pcm(client->config().format)) { |
| 6939 | // If the format is not PCM, the tracks should be invalidated to get correct |
| 6940 | // behavior when the secondary output is changed. |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 6941 | clientsToInvalidate.push_back(client->portId()); |
jiabin | a528106 | 2021-11-23 00:10:23 +0000 | [diff] [blame] | 6942 | } else { |
| 6943 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryDescs; |
| 6944 | std::vector<audio_io_handle_t> secondaryOutputIds; |
| 6945 | for (const auto &secondaryDesc: secondaryDescs) { |
| 6946 | secondaryOutputIds.push_back(secondaryDesc->mIoHandle); |
| 6947 | weakSecondaryDescs.push_back(secondaryDesc); |
| 6948 | } |
| 6949 | trackSecondaryOutputs.emplace(client->portId(), secondaryOutputIds); |
| 6950 | client->setSecondaryOutputs(std::move(weakSecondaryDescs)); |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6951 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6952 | } |
| 6953 | } |
| 6954 | } |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6955 | if (!trackSecondaryOutputs.empty()) { |
| 6956 | mpClientInterface->updateSecondaryOutputs(trackSecondaryOutputs); |
| 6957 | } |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 6958 | if (!clientsToInvalidate.empty()) { |
| 6959 | ALOGD("%s Invalidate clients due to fail getting output for attr", __func__); |
| 6960 | mpClientInterface->invalidateTracks(clientsToInvalidate); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6961 | } |
| 6962 | } |
| 6963 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6964 | bool AudioPolicyManager::isScoRequestedForComm() const { |
| 6965 | AudioDeviceTypeAddrVector devices; |
| 6966 | mEngine->getDevicesForRoleAndStrategy(mCommunnicationStrategy, DEVICE_ROLE_PREFERRED, devices); |
| 6967 | for (const auto &device : devices) { |
| 6968 | if (audio_is_bluetooth_out_sco_device(device.mType)) { |
| 6969 | return true; |
| 6970 | } |
| 6971 | } |
| 6972 | return false; |
| 6973 | } |
| 6974 | |
Eric Laurent | 1a8b45f | 2022-04-13 16:01:47 +0200 | [diff] [blame] | 6975 | bool AudioPolicyManager::isHearingAidUsedForComm() const { |
| 6976 | DeviceVector devices = mEngine->getOutputDevicesForStream(AUDIO_STREAM_VOICE_CALL, |
| 6977 | true /*fromCache*/); |
| 6978 | for (const auto &device : devices) { |
| 6979 | if (device->type() == AUDIO_DEVICE_OUT_HEARING_AID) { |
| 6980 | return true; |
| 6981 | } |
| 6982 | } |
| 6983 | return false; |
| 6984 | } |
| 6985 | |
| 6986 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6987 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6988 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6989 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 6990 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6991 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6992 | return; |
| 6993 | } |
| 6994 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6995 | bool isScoConnected = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6996 | (mAvailableInputDevices.types().count(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) != 0 || |
| 6997 | !Intersection(mAvailableOutputDevices.types(), getAudioDeviceOutAllScoSet()).empty()); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6998 | bool isScoRequested = isScoRequestedForComm(); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6999 | |
| 7000 | // if suspended, restore A2DP output if: |
| 7001 | // ((SCO device is NOT connected) || |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7002 | // ((SCO is not requested) && |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7003 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7004 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7005 | // if not suspended, suspend A2DP output if: |
| 7006 | // (SCO device is connected) && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7007 | // ((SCO is requested) || |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7008 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7009 | // |
| 7010 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7011 | if (!isScoConnected || |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7012 | (!isScoRequested && |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7013 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7014 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7015 | |
| 7016 | mpClientInterface->restoreOutput(a2dpOutput); |
| 7017 | mA2dpSuspended = false; |
| 7018 | } |
| 7019 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7020 | if (isScoConnected && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7021 | (isScoRequested || |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7022 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7023 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7024 | |
| 7025 | mpClientInterface->suspendOutput(a2dpOutput); |
| 7026 | mA2dpSuspended = true; |
| 7027 | } |
| 7028 | } |
| 7029 | } |
| 7030 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7031 | DeviceVector AudioPolicyManager::getNewOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 7032 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7033 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7034 | DeviceVector devices; |
| 7035 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 7036 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7037 | if (index >= 0) { |
| 7038 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7039 | if (patchDesc->getUid() != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7040 | ALOGV("%s device %s forced by patch %d", __func__, |
| 7041 | outputDesc->devices().toString().c_str(), outputDesc->getPatchHandle()); |
| 7042 | return outputDesc->devices(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7043 | } |
| 7044 | } |
| 7045 | |
Dean Wheatley | 514b431 | 2020-06-17 21:45:00 +1000 | [diff] [blame] | 7046 | // Do not retrieve engine device for outputs through MSD |
| 7047 | // TODO: support explicit routing requests by resetting MSD patch to engine device. |
| 7048 | if (outputDesc->devices() == getMsdAudioOutDevices()) { |
| 7049 | return outputDesc->devices(); |
| 7050 | } |
| 7051 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 7052 | // Honor explicit routing requests only if no client using default routing is active on this |
| 7053 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 7054 | bool active; // unused |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7055 | sp<DeviceDescriptor> device = |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7056 | findPreferredDevice(outputDesc, PRODUCT_STRATEGY_NONE, active, mAvailableOutputDevices); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7057 | if (device != nullptr) { |
| 7058 | return DeviceVector(device); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 7059 | } |
| 7060 | |
François Gaffie | a807ef9 | 2018-11-05 10:44:33 +0100 | [diff] [blame] | 7061 | // Legacy Engine cannot take care of bus devices and mix, so we need to handle the conflict |
| 7062 | // of setForceUse / Default Bus device here |
| 7063 | device = mPolicyMixes.getDeviceAndMixForOutput(outputDesc, mAvailableOutputDevices); |
| 7064 | if (device != nullptr) { |
| 7065 | return DeviceVector(device); |
| 7066 | } |
| 7067 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7068 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { |
| 7069 | StreamTypeVector streams = mEngine->getStreamTypesForProductStrategy(productStrategy); |
| 7070 | auto attr = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7071 | auto hasStreamActive = [&](auto stream) { |
| 7072 | return hasStream(streams, stream) && isStreamActive(stream, 0); |
| 7073 | }; |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 7074 | |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7075 | auto doGetOutputDevicesForVoice = [&]() { |
| 7076 | return hasVoiceStream(streams) && (outputDesc == mPrimaryOutput || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7077 | outputDesc->isActive(toVolumeSource(AUDIO_STREAM_VOICE_CALL, false))) && |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7078 | (isInCall() || |
Henrik Backlund | 07c654a | 2021-10-14 15:57:10 +0200 | [diff] [blame] | 7079 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc)) && |
| 7080 | !isStreamActive(AUDIO_STREAM_ENFORCED_AUDIBLE, 0); |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7081 | }; |
| 7082 | |
| 7083 | // With low-latency playing on speaker, music on WFD, when the first low-latency |
| 7084 | // output is stopped, getNewOutputDevices checks for a product strategy |
| 7085 | // from the list, as STRATEGY_SONIFICATION comes prior to STRATEGY_MEDIA. |
Carter Hsu | cc58a6b | 2021-07-20 08:44:50 +0000 | [diff] [blame] | 7086 | // If an ALARM or ENFORCED_AUDIBLE stream is supported by the product strategy, |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7087 | // devices are returned for STRATEGY_SONIFICATION without checking whether the |
| 7088 | // stream is associated to the output descriptor. |
| 7089 | if (doGetOutputDevicesForVoice() || outputDesc->isStrategyActive(productStrategy) || |
| 7090 | ((hasStreamActive(AUDIO_STREAM_ALARM) || |
| 7091 | hasStreamActive(AUDIO_STREAM_ENFORCED_AUDIBLE)) && |
| 7092 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc))) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7093 | // Retrieval of devices for voice DL is done on primary output profile, cannot |
| 7094 | // check the route (would force modifying configuration file for this profile) |
| 7095 | devices = mEngine->getOutputDevicesForAttributes(attr, nullptr, fromCache); |
| 7096 | break; |
| 7097 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7098 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7099 | ALOGV("%s selected devices %s", __func__, devices.toString().c_str()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7100 | return devices; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7101 | } |
| 7102 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7103 | sp<DeviceDescriptor> AudioPolicyManager::getNewInputDevice( |
| 7104 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7105 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7106 | sp<DeviceDescriptor> device; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7107 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 7108 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7109 | if (index >= 0) { |
| 7110 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7111 | if (patchDesc->getUid() != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7112 | ALOGV("getNewInputDevice() device %s forced by patch %d", |
| 7113 | inputDesc->getDevice()->toString().c_str(), inputDesc->getPatchHandle()); |
| 7114 | return inputDesc->getDevice(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7115 | } |
| 7116 | } |
| 7117 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 7118 | // Honor explicit routing requests only if no client using default routing is active on this |
| 7119 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 7120 | bool active; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7121 | device = findPreferredDevice(inputDesc, AUDIO_SOURCE_DEFAULT, active, mAvailableInputDevices); |
| 7122 | if (device != nullptr) { |
| 7123 | return device; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 7124 | } |
| 7125 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 7126 | // If we are not in call and no client is active on this input, this methods returns |
Andy Hung | f024a9e | 2019-01-30 16:01:02 -0800 | [diff] [blame] | 7127 | // a null sp<>, causing the patch on the input stream to be released. |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7128 | audio_attributes_t attributes; |
| 7129 | uid_t uid; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7130 | audio_session_t session; |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7131 | sp<RecordClientDescriptor> topClient = inputDesc->getHighestPriorityClient(); |
| 7132 | if (topClient != nullptr) { |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 7133 | attributes = topClient->attributes(); |
| 7134 | uid = topClient->uid(); |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7135 | session = topClient->session(); |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7136 | } else { |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 7137 | attributes = { .source = AUDIO_SOURCE_DEFAULT }; |
| 7138 | uid = 0; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7139 | session = AUDIO_SESSION_NONE; |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7140 | } |
| 7141 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7142 | if (attributes.source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 7143 | attributes.source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 7144 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7145 | if (attributes.source != AUDIO_SOURCE_DEFAULT) { |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7146 | device = mEngine->getInputDeviceForAttributes(attributes, uid, session); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 7147 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7148 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7149 | return device; |
| 7150 | } |
| 7151 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 7152 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 7153 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 7154 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 7155 | } |
| 7156 | |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 7157 | status_t AudioPolicyManager::getDevicesForAttributes( |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7158 | const audio_attributes_t &attr, AudioDeviceTypeAddrVector *devices, bool forVolume) { |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 7159 | if (devices == nullptr) { |
| 7160 | return BAD_VALUE; |
| 7161 | } |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7162 | |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7163 | DeviceVector curDevices; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 7164 | if (status_t status = getDevicesForAttributes(attr, curDevices, forVolume); status != OK) { |
| 7165 | return status; |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7166 | } |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 7167 | for (const auto& device : curDevices) { |
| 7168 | devices->push_back(device->getDeviceTypeAddr()); |
| 7169 | } |
| 7170 | return NO_ERROR; |
| 7171 | } |
| 7172 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7173 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7174 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 7175 | case AUDIO_STREAM_MUSIC: |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7176 | checkOutputForAttributes(attributes_initializer(AUDIO_USAGE_NOTIFICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7177 | updateDevicesAndOutputs(); |
| 7178 | break; |
| 7179 | default: |
| 7180 | break; |
| 7181 | } |
| 7182 | } |
| 7183 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7184 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 7185 | |
| 7186 | // skip beacon mute management if a dedicated TTS output is available |
| 7187 | if (mTtsOutputAvailable) { |
| 7188 | return 0; |
| 7189 | } |
| 7190 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7191 | switch(event) { |
| 7192 | case STARTING_OUTPUT: |
| 7193 | mBeaconMuteRefCount++; |
| 7194 | break; |
| 7195 | case STOPPING_OUTPUT: |
| 7196 | if (mBeaconMuteRefCount > 0) { |
| 7197 | mBeaconMuteRefCount--; |
| 7198 | } |
| 7199 | break; |
| 7200 | case STARTING_BEACON: |
| 7201 | mBeaconPlayingRefCount++; |
| 7202 | break; |
| 7203 | case STOPPING_BEACON: |
| 7204 | if (mBeaconPlayingRefCount > 0) { |
| 7205 | mBeaconPlayingRefCount--; |
| 7206 | } |
| 7207 | break; |
| 7208 | } |
| 7209 | |
| 7210 | if (mBeaconMuteRefCount > 0) { |
| 7211 | // any playback causes beacon to be muted |
| 7212 | return setBeaconMute(true); |
| 7213 | } else { |
| 7214 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 7215 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 7216 | } |
| 7217 | } |
| 7218 | |
| 7219 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 7220 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 7221 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 7222 | // keep track of muted state to avoid repeating mute/unmute operations |
| 7223 | if (mBeaconMuted != mute) { |
| 7224 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 7225 | ALOGV("\t muting %d", mute); |
| 7226 | uint32_t maxLatency = 0; |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7227 | auto ttsVolumeSource = toVolumeSource(AUDIO_STREAM_TTS, false); |
| 7228 | if (ttsVolumeSource == VOLUME_SOURCE_NONE) { |
| 7229 | ALOGV("\t no tts volume source available"); |
| 7230 | return 0; |
| 7231 | } |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7232 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7233 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7234 | setVolumeSourceMute(ttsVolumeSource, mute/*on*/, desc, 0 /*delay*/, DeviceTypeSet()); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7235 | const uint32_t latency = desc->latency() * 2; |
Eric Laurent | cdb2b35 | 2020-07-23 10:57:02 -0700 | [diff] [blame] | 7236 | if (desc->isActive(latency * 2) && latency > maxLatency) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7237 | maxLatency = latency; |
| 7238 | } |
| 7239 | } |
| 7240 | mBeaconMuted = mute; |
| 7241 | return maxLatency; |
| 7242 | } |
| 7243 | return 0; |
| 7244 | } |
| 7245 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7246 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7247 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7248 | mEngine->updateDeviceSelectionCache(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7249 | mPreviousOutputs = mOutputs; |
| 7250 | } |
| 7251 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 7252 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7253 | const DeviceVector &prevDevices, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7254 | uint32_t delayMs) |
| 7255 | { |
| 7256 | // mute/unmute strategies using an incompatible device combination |
| 7257 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 7258 | // if unmuting, unmute only after the specified delay |
| 7259 | if (outputDesc->isDuplicated()) { |
| 7260 | return 0; |
| 7261 | } |
| 7262 | |
| 7263 | uint32_t muteWaitMs = 0; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7264 | DeviceVector devices = outputDesc->devices(); |
| 7265 | bool shouldMute = outputDesc->isActive() && (devices.size() >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7266 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7267 | auto productStrategies = mEngine->getOrderedProductStrategies(); |
| 7268 | for (const auto &productStrategy : productStrategies) { |
| 7269 | auto attributes = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); |
| 7270 | DeviceVector curDevices = |
| 7271 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false/*fromCache*/); |
| 7272 | curDevices = curDevices.filter(outputDesc->supportedDevices()); |
| 7273 | bool mute = shouldMute && curDevices.containsAtLeastOne(devices) && curDevices != devices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7274 | bool doMute = false; |
| 7275 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7276 | if (mute && !outputDesc->isStrategyMutedByDevice(productStrategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7277 | doMute = true; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7278 | outputDesc->setStrategyMutedByDevice(productStrategy, true); |
| 7279 | } else if (!mute && outputDesc->isStrategyMutedByDevice(productStrategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7280 | doMute = true; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7281 | outputDesc->setStrategyMutedByDevice(productStrategy, false); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7282 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7283 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7284 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7285 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7286 | // skip output if it does not share any device with current output |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7287 | if (!desc->supportedDevices().containsAtLeastOne(outputDesc->supportedDevices())) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7288 | continue; |
| 7289 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7290 | ALOGVV("%s() %s (curDevice %s)", __func__, |
| 7291 | mute ? "muting" : "unmuting", curDevices.toString().c_str()); |
| 7292 | setStrategyMute(productStrategy, mute, desc, mute ? 0 : delayMs); |
| 7293 | if (desc->isStrategyActive(productStrategy)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7294 | if (mute) { |
| 7295 | // FIXME: should not need to double latency if volume could be applied |
| 7296 | // immediately by the audioflinger mixer. We must account for the delay |
| 7297 | // between now and the next time the audioflinger thread for this output |
| 7298 | // will process a buffer (which corresponds to one buffer size, |
| 7299 | // usually 1/2 or 1/4 of the latency). |
| 7300 | if (muteWaitMs < desc->latency() * 2) { |
| 7301 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7302 | } |
| 7303 | } |
| 7304 | } |
| 7305 | } |
| 7306 | } |
| 7307 | } |
| 7308 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7309 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 7310 | // different per device volumes |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7311 | if (outputDesc->isActive() && (devices != prevDevices)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 7312 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
Jasmine Cha | f6074fe | 2021-08-17 13:44:31 +0800 | [diff] [blame] | 7313 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 7314 | if (muteWaitMs < tempMuteWaitMs) { |
| 7315 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7316 | } |
Jasmine Cha | f6074fe | 2021-08-17 13:44:31 +0800 | [diff] [blame] | 7317 | |
| 7318 | // If recommended duration is defined, replace temporary mute duration to avoid |
| 7319 | // truncated notifications at beginning, which depends on duration of changing path in HAL. |
| 7320 | // Otherwise, temporary mute duration is conservatively set to 4 times the reported latency. |
| 7321 | uint32_t tempRecommendedMuteDuration = outputDesc->getRecommendedMuteDurationMs(); |
| 7322 | uint32_t tempMuteDurationMs = tempRecommendedMuteDuration > 0 ? |
| 7323 | tempRecommendedMuteDuration : outputDesc->latency() * 4; |
| 7324 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7325 | for (const auto &activeVs : outputDesc->getActiveVolumeSources()) { |
| 7326 | // make sure that we do not start the temporary mute period too early in case of |
| 7327 | // delayed device change |
| 7328 | setVolumeSourceMute(activeVs, true, outputDesc, delayMs); |
| 7329 | setVolumeSourceMute(activeVs, false, outputDesc, delayMs + tempMuteDurationMs, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7330 | devices.types()); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7331 | } |
| 7332 | } |
| 7333 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7334 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 7335 | if (muteWaitMs > delayMs) { |
| 7336 | muteWaitMs -= delayMs; |
| 7337 | usleep(muteWaitMs * 1000); |
| 7338 | return muteWaitMs; |
| 7339 | } |
| 7340 | return 0; |
| 7341 | } |
| 7342 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7343 | uint32_t AudioPolicyManager::setOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 7344 | const DeviceVector &devices, |
| 7345 | bool force, |
| 7346 | int delayMs, |
| 7347 | audio_patch_handle_t *patchHandle, |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7348 | bool requiresMuteCheck, bool requiresVolumeCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7349 | { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 7350 | // TODO(b/262404095): Consider if the output need to be reopened. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7351 | ALOGV("%s device %s delayMs %d", __func__, devices.toString().c_str(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7352 | uint32_t muteWaitMs; |
| 7353 | |
| 7354 | if (outputDesc->isDuplicated()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7355 | muteWaitMs = setOutputDevices(outputDesc->subOutput1(), devices, force, delayMs, |
| 7356 | nullptr /* patchHandle */, requiresMuteCheck); |
| 7357 | muteWaitMs += setOutputDevices(outputDesc->subOutput2(), devices, force, delayMs, |
| 7358 | nullptr /* patchHandle */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7359 | return muteWaitMs; |
| 7360 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7361 | |
| 7362 | // filter devices according to output selected |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7363 | DeviceVector filteredDevices = outputDesc->filterSupportedDevices(devices); |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 7364 | DeviceVector prevDevices = outputDesc->devices(); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7365 | DeviceVector availPrevDevices = mAvailableOutputDevices.filter(prevDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7366 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7367 | ALOGV("setOutputDevices() prevDevice %s", prevDevices.toString().c_str()); |
| 7368 | |
| 7369 | if (!filteredDevices.isEmpty()) { |
| 7370 | outputDesc->setDevices(filteredDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7371 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 7372 | |
| 7373 | // if the outputs are not materially active, there is no need to mute. |
| 7374 | if (requiresMuteCheck) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7375 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevices, delayMs); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 7376 | } else { |
| 7377 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 7378 | muteWaitMs = 0; |
| 7379 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7380 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 7381 | bool outputRouted = outputDesc->isRouted(); |
| 7382 | |
Eric Laurent | 79ea958 | 2020-06-11 18:49:24 -0700 | [diff] [blame] | 7383 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 7384 | // output profile or if new device is not supported AND previous device(s) is(are) still |
| 7385 | // available (otherwise reset device must be done on the output) |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7386 | if (!devices.isEmpty() && filteredDevices.isEmpty() && !availPrevDevices.empty()) { |
Eric Laurent | 79ea958 | 2020-06-11 18:49:24 -0700 | [diff] [blame] | 7387 | ALOGV("%s: unsupported device %s for output", __func__, devices.toString().c_str()); |
| 7388 | // restore previous device after evaluating strategy mute state |
| 7389 | outputDesc->setDevices(prevDevices); |
| 7390 | return muteWaitMs; |
| 7391 | } |
| 7392 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7393 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 7394 | // the requested device is AUDIO_DEVICE_NONE |
| 7395 | // OR the requested device is the same as current device |
| 7396 | // AND force is not specified |
| 7397 | // AND the output is connected by a valid audio patch. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7398 | // Doing this check here allows the caller to call setOutputDevices() without conditions |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 7399 | if ((filteredDevices.isEmpty() || filteredDevices == prevDevices) && !force && outputRouted) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7400 | ALOGV("%s setting same device %s or null device, force=%d, patch handle=%d", __func__, |
| 7401 | filteredDevices.toString().c_str(), force, outputDesc->getPatchHandle()); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7402 | if (requiresVolumeCheck && !filteredDevices.isEmpty()) { |
| 7403 | ALOGV("%s setting same device on routed output, force apply volumes", __func__); |
| 7404 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs, true /*force*/); |
| 7405 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7406 | return muteWaitMs; |
| 7407 | } |
| 7408 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7409 | ALOGV("%s changing device to %s", __func__, filteredDevices.toString().c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7410 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7411 | // do the routing |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7412 | if (filteredDevices.isEmpty() || mAvailableOutputDevices.filter(filteredDevices).empty()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7413 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7414 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7415 | PatchBuilder patchBuilder; |
| 7416 | patchBuilder.addSource(outputDesc); |
| 7417 | ALOG_ASSERT(filteredDevices.size() <= AUDIO_PATCH_PORTS_MAX, "Too many sink ports"); |
| 7418 | for (const auto &filteredDevice : filteredDevices) { |
| 7419 | patchBuilder.addSink(filteredDevice); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 7420 | } |
| 7421 | |
Jasmine Cha | 7f82d1a | 2020-03-16 13:21:47 +0800 | [diff] [blame] | 7422 | // Add half reported latency to delayMs when muteWaitMs is null in order |
| 7423 | // to avoid disordered sequence of muting volume and changing devices. |
| 7424 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), |
| 7425 | muteWaitMs == 0 ? (delayMs + (outputDesc->latency() / 2)) : delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7426 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7427 | |
| 7428 | // update stream volumes according to new device |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7429 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7430 | |
| 7431 | return muteWaitMs; |
| 7432 | } |
| 7433 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7434 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7435 | int delayMs, |
| 7436 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7437 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7438 | ssize_t index; |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 7439 | if (patchHandle == nullptr && !outputDesc->isRouted()) { |
| 7440 | return INVALID_OPERATION; |
| 7441 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7442 | if (patchHandle) { |
| 7443 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 7444 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 7445 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7446 | } |
| 7447 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7448 | return INVALID_OPERATION; |
| 7449 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7450 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7451 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7452 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 7453 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7454 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7455 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 7456 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7457 | return status; |
| 7458 | } |
| 7459 | |
| 7460 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7461 | const sp<DeviceDescriptor> &device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7462 | bool force, |
| 7463 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7464 | { |
| 7465 | status_t status = NO_ERROR; |
| 7466 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7467 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7468 | if ((device != nullptr) && ((device != inputDesc->getDevice()) || force)) { |
| 7469 | inputDesc->setDevice(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7470 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7471 | if (mAvailableInputDevices.contains(device)) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7472 | PatchBuilder patchBuilder; |
| 7473 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 7474 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 7475 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7476 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 7477 | auto result = usecase; |
| 7478 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 7479 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 7480 | } |
| 7481 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7482 | //only one input device for now |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7483 | addSource(device); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7484 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7485 | } |
| 7486 | } |
| 7487 | return status; |
| 7488 | } |
| 7489 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7490 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 7491 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7492 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7493 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7494 | ssize_t index; |
| 7495 | if (patchHandle) { |
| 7496 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 7497 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 7498 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7499 | } |
| 7500 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7501 | return INVALID_OPERATION; |
| 7502 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7503 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7504 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7505 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 7506 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7507 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7508 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 7509 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7510 | return status; |
| 7511 | } |
| 7512 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7513 | sp<IOProfile> AudioPolicyManager::getInputProfile(const sp<DeviceDescriptor> &device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7514 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 7515 | audio_format_t& format, |
| 7516 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7517 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7518 | { |
| 7519 | // Choose an input profile based on the requested capture parameters: select the first available |
| 7520 | // profile supporting all requested parameters. |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7521 | // The flags can be ignored if it doesn't contain a much match flag. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 7522 | // |
| 7523 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 7524 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7525 | |
Atneya Nair | 0f0a803 | 2022-12-12 16:20:12 -0800 | [diff] [blame] | 7526 | using underlying_input_flag_t = std::underlying_type_t<audio_input_flags_t>; |
| 7527 | const underlying_input_flag_t mustMatchFlag = AUDIO_INPUT_FLAG_MMAP_NOIRQ | |
| 7528 | AUDIO_INPUT_FLAG_HOTWORD_TAP | AUDIO_INPUT_FLAG_HW_LOOKBACK; |
| 7529 | |
| 7530 | const underlying_input_flag_t oriFlags = flags; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 7531 | |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7532 | for (;;) { |
| 7533 | sp<IOProfile> firstInexact = nullptr; |
| 7534 | uint32_t updatedSamplingRate = 0; |
| 7535 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 7536 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
| 7537 | for (const auto& hwModule : mHwModules) { |
| 7538 | for (const auto& profile : hwModule->getInputProfiles()) { |
| 7539 | // profile->log(); |
| 7540 | //updatedFormat = format; |
| 7541 | if (profile->isCompatibleProfile(DeviceVector(device), samplingRate, |
| 7542 | &samplingRate /*updatedSamplingRate*/, |
| 7543 | format, |
| 7544 | &format, /*updatedFormat*/ |
| 7545 | channelMask, |
| 7546 | &channelMask /*updatedChannelMask*/, |
| 7547 | // FIXME ugly cast |
| 7548 | (audio_output_flags_t) flags, |
| 7549 | true /*exactMatchRequiredForInputFlags*/)) { |
| 7550 | return profile; |
| 7551 | } |
| 7552 | if (firstInexact == nullptr && profile->isCompatibleProfile(DeviceVector(device), |
| 7553 | samplingRate, |
| 7554 | &updatedSamplingRate, |
| 7555 | format, |
| 7556 | &updatedFormat, |
| 7557 | channelMask, |
| 7558 | &updatedChannelMask, |
| 7559 | // FIXME ugly cast |
| 7560 | (audio_output_flags_t) flags, |
| 7561 | false /*exactMatchRequiredForInputFlags*/)) { |
| 7562 | firstInexact = profile; |
| 7563 | } |
| 7564 | } |
| 7565 | } |
| 7566 | |
| 7567 | if (firstInexact != nullptr) { |
| 7568 | samplingRate = updatedSamplingRate; |
| 7569 | format = updatedFormat; |
| 7570 | channelMask = updatedChannelMask; |
| 7571 | return firstInexact; |
| 7572 | } else if (flags & AUDIO_INPUT_FLAG_RAW) { |
| 7573 | flags = (audio_input_flags_t) (flags & ~AUDIO_INPUT_FLAG_RAW); // retry |
| 7574 | } else if ((flags & mustMatchFlag) == AUDIO_INPUT_FLAG_NONE && |
| 7575 | flags != AUDIO_INPUT_FLAG_NONE && audio_is_linear_pcm(format)) { |
| 7576 | flags = AUDIO_INPUT_FLAG_NONE; |
| 7577 | } else { // fail |
| 7578 | ALOGW("%s could not find profile for device %s, sampling rate %u, format %#x, " |
| 7579 | "channel mask 0x%X, flags %#x", __func__, device->toString().c_str(), |
| 7580 | samplingRate, format, channelMask, oriFlags); |
| 7581 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7582 | } |
| 7583 | } |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7584 | |
| 7585 | return nullptr; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7586 | } |
| 7587 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7588 | float AudioPolicyManager::computeVolume(IVolumeCurves &curves, |
| 7589 | VolumeSource volumeSource, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 7590 | int index, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7591 | const DeviceTypeSet& deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7592 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7593 | float volumeDb = curves.volIndexToDb(Volume::getDeviceCategory(deviceTypes), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 7594 | |
| 7595 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 7596 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 7597 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 7598 | // the ringtone volume |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7599 | const auto callVolumeSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL, false); |
| 7600 | const auto ringVolumeSrc = toVolumeSource(AUDIO_STREAM_RING, false); |
| 7601 | const auto musicVolumeSrc = toVolumeSource(AUDIO_STREAM_MUSIC, false); |
| 7602 | const auto alarmVolumeSrc = toVolumeSource(AUDIO_STREAM_ALARM, false); |
| 7603 | const auto a11yVolumeSrc = toVolumeSource(AUDIO_STREAM_ACCESSIBILITY, false); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7604 | |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7605 | if (volumeSource == a11yVolumeSrc |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7606 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) && |
| 7607 | mOutputs.isActive(ringVolumeSrc, 0)) { |
| 7608 | auto &ringCurves = getVolumeCurves(AUDIO_STREAM_RING); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7609 | const float ringVolumeDb = computeVolume(ringCurves, ringVolumeSrc, index, deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7610 | return ringVolumeDb - 4 > volumeDb ? ringVolumeDb - 4 : volumeDb; |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 7611 | } |
| 7612 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 7613 | // in-call: always cap volume by voice volume + some low headroom |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7614 | if ((volumeSource != callVolumeSrc && (isInCall() || |
| 7615 | mOutputs.isActiveLocally(callVolumeSrc))) && |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7616 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM, false) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7617 | volumeSource == ringVolumeSrc || volumeSource == musicVolumeSrc || |
| 7618 | volumeSource == alarmVolumeSrc || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7619 | volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION, false) || |
| 7620 | volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false) || |
| 7621 | volumeSource == toVolumeSource(AUDIO_STREAM_DTMF, false) || |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7622 | volumeSource == a11yVolumeSrc)) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7623 | auto &voiceCurves = getVolumeCurves(callVolumeSrc); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7624 | int voiceVolumeIndex = voiceCurves.getVolumeIndex(deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7625 | const float maxVoiceVolDb = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7626 | computeVolume(voiceCurves, callVolumeSrc, voiceVolumeIndex, deviceTypes) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 7627 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 7628 | // FIXME: Workaround for call screening applications until a proper audio mode is defined |
| 7629 | // to support this scenario : Exempt the RING stream from the audio cap if the audio was |
| 7630 | // programmatically muted. |
| 7631 | // VOICE_CALL stream has minVolumeIndex > 0 : Users cannot set the volume of voice calls to |
| 7632 | // 0. We don't want to cap volume when the system has programmatically muted the voice call |
| 7633 | // stream. See setVolumeCurveIndex() for more information. |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7634 | bool exemptFromCapping = |
| 7635 | ((volumeSource == ringVolumeSrc) || (volumeSource == a11yVolumeSrc)) |
| 7636 | && (voiceVolumeIndex == 0); |
Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 7637 | ALOGV_IF(exemptFromCapping, "%s volume source %d at vol=%f not capped", __func__, |
| 7638 | volumeSource, volumeDb); |
| 7639 | if ((volumeDb > maxVoiceVolDb) && !exemptFromCapping) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7640 | ALOGV("%s volume source %d at vol=%f overriden by volume group %d at vol=%f", __func__, |
| 7641 | volumeSource, volumeDb, callVolumeSrc, maxVoiceVolDb); |
| 7642 | volumeDb = maxVoiceVolDb; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 7643 | } |
| 7644 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7645 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 7646 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 7647 | // - always attenuate notifications volume by 6dB |
| 7648 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 7649 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7650 | // - if music is playing, always limit the volume to current music volume, |
| 7651 | // with a minimum threshold at -36dB so that notification is always perceived. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7652 | if (!Intersection(deviceTypes, |
| 7653 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, |
| 7654 | AUDIO_DEVICE_OUT_WIRED_HEADSET, AUDIO_DEVICE_OUT_WIRED_HEADPHONE, |
Eric Laurent | c42df45 | 2020-08-07 10:51:53 -0700 | [diff] [blame] | 7655 | AUDIO_DEVICE_OUT_USB_HEADSET, AUDIO_DEVICE_OUT_HEARING_AID, |
| 7656 | AUDIO_DEVICE_OUT_BLE_HEADSET}).empty() && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7657 | ((volumeSource == alarmVolumeSrc || |
| 7658 | volumeSource == ringVolumeSrc) || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7659 | (volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION, false)) || |
| 7660 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM, false)) || |
| 7661 | ((volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false)) && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7662 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
| 7663 | curves.canBeMuted()) { |
| 7664 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7665 | // when the phone is ringing we must consider that music could have been paused just before |
| 7666 | // by the music application and behave as if music was active if the last music track was |
| 7667 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 7668 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7669 | mLimitRingtoneVolume) { |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7670 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7671 | DeviceTypeSet musicDevice = |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7672 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 7673 | nullptr, true /*fromCache*/).types(); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7674 | auto &musicCurves = getVolumeCurves(AUDIO_STREAM_MUSIC); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7675 | float musicVolDb = computeVolume(musicCurves, |
| 7676 | musicVolumeSrc, |
| 7677 | musicCurves.getVolumeIndex(musicDevice), |
| 7678 | musicDevice); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7679 | float minVolDb = (musicVolDb > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 7680 | musicVolDb : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
| 7681 | if (volumeDb > minVolDb) { |
| 7682 | volumeDb = minVolDb; |
| 7683 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDb, musicVolDb); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7684 | } |
Eric Laurent | 7b6385c | 2021-05-12 17:55:36 +0200 | [diff] [blame] | 7685 | if (Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER |
| 7686 | && !Intersection(deviceTypes, {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, |
| 7687 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES}).empty()) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 7688 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 7689 | // intended to be played |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7690 | if ((volumeDb > -96.0f) && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7691 | (musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDb)) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7692 | ALOGV("%s increasing volume for volume source=%d device=%s from %f to %f", |
| 7693 | __func__, volumeSource, dumpDeviceTypes(deviceTypes).c_str(), volumeDb, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7694 | musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 7695 | volumeDb = musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 7696 | } |
| 7697 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7698 | } else if ((Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7699 | (!(volumeSource == alarmVolumeSrc || volumeSource == ringVolumeSrc))) { |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7700 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7701 | } |
| 7702 | } |
| 7703 | |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7704 | return volumeDb; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7705 | } |
| 7706 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7707 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7708 | VolumeSource fromVolumeSource, |
| 7709 | VolumeSource toVolumeSource) |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7710 | { |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7711 | if (fromVolumeSource == toVolumeSource) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7712 | return srcIndex; |
| 7713 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7714 | auto &srcCurves = getVolumeCurves(fromVolumeSource); |
| 7715 | auto &dstCurves = getVolumeCurves(toVolumeSource); |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7716 | float minSrc = (float)srcCurves.getVolumeIndexMin(); |
| 7717 | float maxSrc = (float)srcCurves.getVolumeIndexMax(); |
| 7718 | float minDst = (float)dstCurves.getVolumeIndexMin(); |
| 7719 | float maxDst = (float)dstCurves.getVolumeIndexMax(); |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7720 | |
Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 7721 | // preserve mute request or correct range |
| 7722 | if (srcIndex < minSrc) { |
| 7723 | if (srcIndex == 0) { |
| 7724 | return 0; |
| 7725 | } |
| 7726 | srcIndex = minSrc; |
| 7727 | } else if (srcIndex > maxSrc) { |
| 7728 | srcIndex = maxSrc; |
| 7729 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7730 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 7731 | } |
| 7732 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7733 | status_t AudioPolicyManager::checkAndSetVolume(IVolumeCurves &curves, |
| 7734 | VolumeSource volumeSource, |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7735 | int index, |
| 7736 | const sp<AudioOutputDescriptor>& outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7737 | DeviceTypeSet deviceTypes, |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7738 | int delayMs, |
| 7739 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7740 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7741 | // do not change actual attributes volume if the attributes is muted |
| 7742 | if (outputDesc->isMuted(volumeSource)) { |
| 7743 | ALOGVV("%s: volume source %d muted count %d active=%d", __func__, volumeSource, |
| 7744 | outputDesc->getMuteCount(volumeSource), outputDesc->isActive(volumeSource)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7745 | return NO_ERROR; |
| 7746 | } |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7747 | VolumeSource callVolSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL, false); |
| 7748 | VolumeSource btScoVolSrc = toVolumeSource(AUDIO_STREAM_BLUETOOTH_SCO, false); |
| 7749 | bool isVoiceVolSrc = (volumeSource != VOLUME_SOURCE_NONE) && (callVolSrc == volumeSource); |
| 7750 | bool isBtScoVolSrc = (volumeSource != VOLUME_SOURCE_NONE) && (btScoVolSrc == volumeSource); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7751 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7752 | bool isScoRequested = isScoRequestedForComm(); |
Eric Laurent | 1a8b45f | 2022-04-13 16:01:47 +0200 | [diff] [blame] | 7753 | bool isHAUsed = isHearingAidUsedForComm(); |
| 7754 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7755 | // do not change in call volume if bluetooth is connected and vice versa |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7756 | // if sco and call follow same curves, bypass forceUseForComm |
| 7757 | if ((callVolSrc != btScoVolSrc) && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7758 | ((isVoiceVolSrc && isScoRequested) || |
Beibei | f660a51 | 2023-02-28 17:00:34 +0800 | [diff] [blame] | 7759 | (isBtScoVolSrc && !(isScoRequested || isHAUsed))) && |
| 7760 | !isSingleDeviceType(deviceTypes, AUDIO_DEVICE_OUT_TELEPHONY_TX)) { |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7761 | ALOGV("%s cannot set volume group %d volume when is%srequested for comm", __func__, |
Eric Laurent | 1a8b45f | 2022-04-13 16:01:47 +0200 | [diff] [blame] | 7762 | volumeSource, isScoRequested ? " " : " not "); |
Eric Laurent | 571ef96 | 2020-07-24 11:43:48 -0700 | [diff] [blame] | 7763 | // Do not return an error here as AudioService will always set both voice call |
| 7764 | // and bluetooth SCO volumes due to stream aliasing. |
| 7765 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7766 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7767 | if (deviceTypes.empty()) { |
| 7768 | deviceTypes = outputDesc->devices().types(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7769 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7770 | |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 7771 | if (curves.getVolumeIndexMin() < 0 || curves.getVolumeIndexMax() < 0) { |
| 7772 | ALOGE("invalid volume index range"); |
| 7773 | return BAD_VALUE; |
| 7774 | } |
| 7775 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7776 | float volumeDb = computeVolume(curves, volumeSource, index, deviceTypes); |
| 7777 | if (outputDesc->isFixedVolume(deviceTypes) || |
Eric Laurent | 9698a4c | 2020-10-12 17:10:23 -0700 | [diff] [blame] | 7778 | // Force VoIP volume to max for bluetooth SCO device except if muted |
| 7779 | (index != 0 && (isVoiceVolSrc || isBtScoVolSrc) && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7780 | isSingleDeviceType(deviceTypes, audio_is_bluetooth_out_sco_device))) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 7781 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7782 | } |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 7783 | const bool muted = (index == 0) && (volumeDb != 0.0f); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7784 | outputDesc->setVolume( |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 7785 | volumeDb, muted, volumeSource, curves.getStreamTypes(), deviceTypes, delayMs, force); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7786 | |
Eric Laurent | e8f2c0f | 2021-08-17 11:17:19 +0200 | [diff] [blame] | 7787 | if (outputDesc == mPrimaryOutput && (isVoiceVolSrc || isBtScoVolSrc)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7788 | float voiceVolume; |
Eric Laurent | fad001d | 2019-06-11 19:17:57 -0700 | [diff] [blame] | 7789 | // Force voice volume to max or mute for Bluetooth SCO as other attenuations are managed by the headset |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7790 | if (isVoiceVolSrc) { |
| 7791 | voiceVolume = (float)index/(float)curves.getVolumeIndexMax(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7792 | } else { |
Eric Laurent | fad001d | 2019-06-11 19:17:57 -0700 | [diff] [blame] | 7793 | voiceVolume = index == 0 ? 0.0 : 1.0; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7794 | } |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 7795 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7796 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 7797 | mLastVoiceVolume = voiceVolume; |
| 7798 | } |
| 7799 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7800 | return NO_ERROR; |
| 7801 | } |
| 7802 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7803 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7804 | const DeviceTypeSet& deviceTypes, |
| 7805 | int delayMs, |
| 7806 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7807 | { |
jiabin | cd51052 | 2020-01-22 09:40:55 -0800 | [diff] [blame] | 7808 | ALOGVV("applyStreamVolumes() for device %s", dumpDeviceTypes(deviceTypes).c_str()); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7809 | for (const auto &volumeGroup : mEngine->getVolumeGroups()) { |
| 7810 | auto &curves = getVolumeCurves(toVolumeSource(volumeGroup)); |
| 7811 | checkAndSetVolume(curves, toVolumeSource(volumeGroup), |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7812 | curves.getVolumeIndex(deviceTypes), |
| 7813 | outputDesc, deviceTypes, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7814 | } |
| 7815 | } |
| 7816 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7817 | void AudioPolicyManager::setStrategyMute(product_strategy_t strategy, |
| 7818 | bool on, |
| 7819 | const sp<AudioOutputDescriptor>& outputDesc, |
| 7820 | int delayMs, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7821 | DeviceTypeSet deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7822 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7823 | std::vector<VolumeSource> sourcesToMute; |
| 7824 | for (auto attributes: mEngine->getAllAttributesForProductStrategy(strategy)) { |
| 7825 | ALOGVV("%s() attributes %s, mute %d, output ID %d", __func__, |
| 7826 | toString(attributes).c_str(), on, outputDesc->getId()); |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7827 | VolumeSource source = toVolumeSource(attributes, false); |
| 7828 | if ((source != VOLUME_SOURCE_NONE) && |
| 7829 | (std::find(begin(sourcesToMute), end(sourcesToMute), source) |
| 7830 | == end(sourcesToMute))) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7831 | sourcesToMute.push_back(source); |
| 7832 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7833 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7834 | for (auto source : sourcesToMute) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7835 | setVolumeSourceMute(source, on, outputDesc, delayMs, deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7836 | } |
| 7837 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7838 | } |
| 7839 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7840 | void AudioPolicyManager::setVolumeSourceMute(VolumeSource volumeSource, |
| 7841 | bool on, |
| 7842 | const sp<AudioOutputDescriptor>& outputDesc, |
| 7843 | int delayMs, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7844 | DeviceTypeSet deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7845 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7846 | if (deviceTypes.empty()) { |
| 7847 | deviceTypes = outputDesc->devices().types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7848 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7849 | auto &curves = getVolumeCurves(volumeSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7850 | if (on) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7851 | if (!outputDesc->isMuted(volumeSource)) { |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7852 | if (curves.canBeMuted() && |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7853 | (volumeSource != toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7854 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == |
| 7855 | AUDIO_POLICY_FORCE_NONE))) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7856 | checkAndSetVolume(curves, volumeSource, 0, outputDesc, deviceTypes, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7857 | } |
| 7858 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7859 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not |
| 7860 | // ignored |
| 7861 | outputDesc->incMuteCount(volumeSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7862 | } else { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7863 | if (!outputDesc->isMuted(volumeSource)) { |
| 7864 | ALOGV("%s unmuting non muted attributes!", __func__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7865 | return; |
| 7866 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7867 | if (outputDesc->decMuteCount(volumeSource) == 0) { |
| 7868 | checkAndSetVolume(curves, volumeSource, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7869 | curves.getVolumeIndex(deviceTypes), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7870 | outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7871 | deviceTypes, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7872 | delayMs); |
| 7873 | } |
| 7874 | } |
| 7875 | } |
| 7876 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7877 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 7878 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7879 | // has flags that map to a stream type? |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 7880 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 7881 | return true; |
| 7882 | } |
| 7883 | |
| 7884 | // has known usage? |
| 7885 | switch (paa->usage) { |
| 7886 | case AUDIO_USAGE_UNKNOWN: |
| 7887 | case AUDIO_USAGE_MEDIA: |
| 7888 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 7889 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 7890 | case AUDIO_USAGE_ALARM: |
| 7891 | case AUDIO_USAGE_NOTIFICATION: |
| 7892 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 7893 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 7894 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 7895 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 7896 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 7897 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 7898 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 7899 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 7900 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7901 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 7902 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | 21777f8 | 2019-12-06 18:12:06 -0800 | [diff] [blame] | 7903 | case AUDIO_USAGE_CALL_ASSISTANT: |
Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 7904 | case AUDIO_USAGE_EMERGENCY: |
| 7905 | case AUDIO_USAGE_SAFETY: |
| 7906 | case AUDIO_USAGE_VEHICLE_STATUS: |
| 7907 | case AUDIO_USAGE_ANNOUNCEMENT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 7908 | break; |
| 7909 | default: |
| 7910 | return false; |
| 7911 | } |
| 7912 | return true; |
| 7913 | } |
| 7914 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7915 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 7916 | { |
| 7917 | return mEngine->getForceUse(usage); |
| 7918 | } |
| 7919 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 7920 | bool AudioPolicyManager::isInCall() const { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7921 | return isStateInCall(mEngine->getPhoneState()); |
| 7922 | } |
| 7923 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 7924 | bool AudioPolicyManager::isStateInCall(int state) const { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7925 | return is_state_in_call(state); |
| 7926 | } |
| 7927 | |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 7928 | bool AudioPolicyManager::isCallAudioAccessible() const { |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 7929 | audio_mode_t mode = mEngine->getPhoneState(); |
| 7930 | return (mode == AUDIO_MODE_IN_CALL) |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 7931 | || (mode == AUDIO_MODE_CALL_SCREEN) |
| 7932 | || (mode == AUDIO_MODE_CALL_REDIRECT); |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 7933 | } |
| 7934 | |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 7935 | bool AudioPolicyManager::isInCallOrScreening() const { |
| 7936 | audio_mode_t mode = mEngine->getPhoneState(); |
| 7937 | return isStateInCall(mode) || mode == AUDIO_MODE_CALL_SCREEN; |
| 7938 | } |
| 7939 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7940 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 7941 | { |
| 7942 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 7943 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7944 | if (sourceDesc->isConnected() && (sourceDesc->srcDevice()->equals(deviceDesc) || |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 7945 | sourceDesc->sinkDevice()->equals(deviceDesc)) |
| 7946 | && !isCallRxAudioSource(sourceDesc)) { |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7947 | disconnectAudioSource(sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7948 | } |
| 7949 | } |
| 7950 | |
| 7951 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 7952 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 7953 | bool release = false; |
| 7954 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 7955 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 7956 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 7957 | source->ext.device.type == deviceDesc->type()) { |
| 7958 | release = true; |
| 7959 | } |
| 7960 | } |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7961 | const char *address = deviceDesc->address().c_str(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7962 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 7963 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 7964 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7965 | sink->ext.device.type == deviceDesc->type() && |
| 7966 | (strnlen(address, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0 |
| 7967 | || strncmp(sink->ext.device.address, address, |
| 7968 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7969 | release = true; |
| 7970 | } |
| 7971 | } |
| 7972 | if (release) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7973 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->getHandle()); |
| 7974 | releaseAudioPatch(patchDesc->getHandle(), patchDesc->getUid()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7975 | } |
| 7976 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7977 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 7978 | mInputs.clearSessionRoutesForDevice(deviceDesc); |
| 7979 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7980 | mHwModules.cleanUpForDevice(deviceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7981 | } |
| 7982 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7983 | void AudioPolicyManager::modifySurroundFormats( |
| 7984 | const sp<DeviceDescriptor>& devDesc, FormatVector *formatsPtr) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7985 | std::unordered_set<audio_format_t> enforcedSurround( |
| 7986 | devDesc->encodedFormats().begin(), devDesc->encodedFormats().end()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7987 | std::unordered_set<audio_format_t> allSurround; // A flat set of all known surround formats |
| 7988 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 7989 | allSurround.insert(pair.first); |
| 7990 | for (const auto& subformat : pair.second) allSurround.insert(subformat); |
| 7991 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7992 | |
| 7993 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 7994 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7995 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7996 | // This is the resulting set of formats depending on the surround mode: |
| 7997 | // 'all surround' = allSurround |
| 7998 | // 'enforced surround' = enforcedSurround [may include IEC69137 which isn't raw surround fmt] |
| 7999 | // 'non-surround' = not in 'all surround' and not in 'enforced surround' |
| 8000 | // 'manual surround' = mManualSurroundFormats |
| 8001 | // AUTO: formats v 'enforced surround' |
| 8002 | // ALWAYS: formats v 'all surround' v 'enforced surround' |
| 8003 | // NEVER: formats ^ 'non-surround' |
| 8004 | // MANUAL: formats ^ ('non-surround' v 'manual surround' v (IEC69137 ^ 'enforced surround')) |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8005 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8006 | std::unordered_set<audio_format_t> formatSet; |
| 8007 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL |
| 8008 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8009 | // formatSet is (formats ^ 'non-surround') |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8010 | for (auto formatIter = formatsPtr->begin(); formatIter != formatsPtr->end(); ++formatIter) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8011 | if (allSurround.count(*formatIter) == 0 && enforcedSurround.count(*formatIter) == 0) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8012 | formatSet.insert(*formatIter); |
| 8013 | } |
| 8014 | } |
| 8015 | } else { |
| 8016 | formatSet.insert(formatsPtr->begin(), formatsPtr->end()); |
| 8017 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8018 | formatsPtr->clear(); // Re-filled from the formatSet at the end. |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8019 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 8020 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8021 | formatSet.insert(mManualSurroundFormats.begin(), mManualSurroundFormats.end()); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8022 | // Enable IEC61937 when in MANUAL mode if it's enforced for this device. |
| 8023 | if (enforcedSurround.count(AUDIO_FORMAT_IEC61937) != 0) { |
| 8024 | formatSet.insert(AUDIO_FORMAT_IEC61937); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8025 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8026 | } else if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { // AUTO or ALWAYS |
| 8027 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 8028 | formatSet.insert(allSurround.begin(), allSurround.end()); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 8029 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8030 | formatSet.insert(enforcedSurround.begin(), enforcedSurround.end()); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8031 | } |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8032 | for (const auto& format : formatSet) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8033 | formatsPtr->push_back(format); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8034 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8035 | } |
| 8036 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8037 | void AudioPolicyManager::modifySurroundChannelMasks(ChannelMaskSet *channelMasksPtr) { |
| 8038 | ChannelMaskSet &channelMasks = *channelMasksPtr; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8039 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 8040 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 8041 | |
| 8042 | // If NEVER, then remove support for channelMasks > stereo. |
| 8043 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8044 | for (auto it = channelMasks.begin(); it != channelMasks.end();) { |
| 8045 | audio_channel_mask_t channelMask = *it; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8046 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 8047 | ALOGV("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8048 | it = channelMasks.erase(it); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8049 | } else { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8050 | ++it; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8051 | } |
| 8052 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 8053 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 8054 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 8055 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8056 | bool supports5dot1 = false; |
| 8057 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 8058 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8059 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 8060 | supports5dot1 = true; |
| 8061 | break; |
| 8062 | } |
| 8063 | } |
| 8064 | // If not then add 5.1 support. |
| 8065 | if (!supports5dot1) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8066 | channelMasks.insert(AUDIO_CHANNEL_OUT_5POINT1); |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 8067 | ALOGV("%s: force MANUAL or ALWAYS, so adding channelMask for 5.1 surround", __func__); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8068 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8069 | } |
| 8070 | } |
| 8071 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8072 | void AudioPolicyManager::updateAudioProfiles(const sp<DeviceDescriptor>& devDesc, |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 8073 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8074 | AudioProfileVector &profiles) |
| 8075 | { |
| 8076 | String8 reply; |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8077 | audio_devices_t device = devDesc->type(); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8078 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8079 | // Format MUST be checked first to update the list of AudioProfile |
| 8080 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 8081 | reply = mpClientInterface->getParameters( |
| 8082 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 8083 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 8084 | AudioParameter repliedParameters(reply); |
jiabin | f26596b | 2023-04-12 18:56:39 +0000 | [diff] [blame] | 8085 | FormatVector formats; |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 8086 | if (repliedParameters.get( |
jiabin | f26596b | 2023-04-12 18:56:39 +0000 | [diff] [blame] | 8087 | String8(AudioParameter::keyStreamSupportedFormats), reply) == NO_ERROR) { |
| 8088 | formats = formatsFromString(reply.string()); |
| 8089 | } else if (devDesc->hasValidAudioProfile()) { |
| 8090 | ALOGD("%s: using the device profiles", __func__); |
| 8091 | formats = devDesc->getAudioProfiles().getSupportedFormats(); |
| 8092 | } else { |
| 8093 | ALOGE("%s: failed to retrieve format, bailing out", __func__); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8094 | return; |
| 8095 | } |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 8096 | mReportedFormatsMap[devDesc] = formats; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8097 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 8098 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8099 | modifySurroundFormats(devDesc, &formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 8100 | } |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 8101 | addProfilesForFormats(profiles, formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8102 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8103 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 8104 | for (audio_format_t format : profiles.getSupportedFormats()) { |
jiabin | f26596b | 2023-04-12 18:56:39 +0000 | [diff] [blame] | 8105 | std::optional<ChannelMaskSet> channelMasks; |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8106 | SampleRateSet samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8107 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 8108 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8109 | |
| 8110 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 8111 | reply = mpClientInterface->getParameters( |
| 8112 | ioHandle, |
| 8113 | requestedParameters.toString() + ";" + |
| 8114 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8115 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 8116 | AudioParameter repliedParameters(reply); |
| 8117 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 8118 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 8119 | samplingRates = samplingRatesFromString(reply.string()); |
jiabin | f26596b | 2023-04-12 18:56:39 +0000 | [diff] [blame] | 8120 | } else { |
| 8121 | samplingRates = devDesc->getAudioProfiles().getSampleRatesFor(format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8122 | } |
| 8123 | } |
| 8124 | if (profiles.hasDynamicChannelsFor(format)) { |
| 8125 | reply = mpClientInterface->getParameters(ioHandle, |
| 8126 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 8127 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8128 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 8129 | AudioParameter repliedParameters(reply); |
| 8130 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 8131 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 8132 | channelMasks = channelMasksFromString(reply.string()); |
jiabin | f26596b | 2023-04-12 18:56:39 +0000 | [diff] [blame] | 8133 | } else { |
| 8134 | channelMasks = devDesc->getAudioProfiles().getChannelMasksFor(format); |
| 8135 | } |
| 8136 | if (channelMasks.has_value() && (device == AUDIO_DEVICE_OUT_HDMI |
| 8137 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD))) { |
| 8138 | modifySurroundChannelMasks(&channelMasks.value()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8139 | } |
| 8140 | } |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 8141 | addDynamicAudioProfileAndSort( |
jiabin | f26596b | 2023-04-12 18:56:39 +0000 | [diff] [blame] | 8142 | profiles, new AudioProfile( |
| 8143 | format, channelMasks.value_or(ChannelMaskSet()), samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8144 | } |
| 8145 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8146 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8147 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 8148 | audio_patch_handle_t *patchHandle, |
| 8149 | AudioIODescriptorInterface *ioDescriptor, |
| 8150 | const struct audio_patch *patch, |
| 8151 | int delayMs) |
| 8152 | { |
| 8153 | ssize_t index = mAudioPatches.indexOfKey( |
| 8154 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 8155 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 8156 | sp<AudioPatch> patchDesc; |
| 8157 | status_t status = installPatch( |
| 8158 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 8159 | if (status == NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8160 | ioDescriptor->setPatchHandle(patchDesc->getHandle()); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8161 | } |
| 8162 | return status; |
| 8163 | } |
| 8164 | |
| 8165 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 8166 | ssize_t index, |
| 8167 | audio_patch_handle_t *patchHandle, |
| 8168 | const struct audio_patch *patch, |
| 8169 | int delayMs, |
| 8170 | uid_t uid, |
| 8171 | sp<AudioPatch> *patchDescPtr) |
| 8172 | { |
| 8173 | sp<AudioPatch> patchDesc; |
| 8174 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 8175 | if (index >= 0) { |
| 8176 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8177 | afPatchHandle = patchDesc->getAfHandle(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8178 | } |
| 8179 | |
| 8180 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 8181 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 8182 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 8183 | if (status == NO_ERROR) { |
| 8184 | if (index < 0) { |
| 8185 | patchDesc = new AudioPatch(patch, uid); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8186 | addAudioPatch(patchDesc->getHandle(), patchDesc); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8187 | } else { |
| 8188 | patchDesc->mPatch = *patch; |
| 8189 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8190 | patchDesc->setAfHandle(afPatchHandle); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8191 | if (patchHandle) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8192 | *patchHandle = patchDesc->getHandle(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8193 | } |
| 8194 | nextAudioPortGeneration(); |
| 8195 | mpClientInterface->onAudioPatchListUpdate(); |
| 8196 | } |
| 8197 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 8198 | return status; |
| 8199 | } |
| 8200 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8201 | bool AudioPolicyManager::areAllActiveTracksRerouted(const sp<SwAudioOutputDescriptor>& output) |
| 8202 | { |
| 8203 | const TrackClientVector activeClients = output->getActiveClients(); |
| 8204 | if (activeClients.empty()) { |
| 8205 | return true; |
| 8206 | } |
| 8207 | ssize_t index = mAudioPatches.indexOfKey(output->getPatchHandle()); |
| 8208 | if (index < 0) { |
| 8209 | ALOGE("%s, no audio patch found while there are active clients on output %d", |
| 8210 | __func__, output->getId()); |
| 8211 | return false; |
| 8212 | } |
| 8213 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 8214 | DeviceVector routedDevices; |
| 8215 | for (int i = 0; i < patchDesc->mPatch.num_sinks; ++i) { |
| 8216 | sp<DeviceDescriptor> device = mAvailableOutputDevices.getDeviceFromId( |
| 8217 | patchDesc->mPatch.sinks[i].id); |
| 8218 | if (device == nullptr) { |
| 8219 | ALOGE("%s, no audio device found with id(%d)", |
| 8220 | __func__, patchDesc->mPatch.sinks[i].id); |
| 8221 | return false; |
| 8222 | } |
| 8223 | routedDevices.add(device); |
| 8224 | } |
| 8225 | for (const auto& client : activeClients) { |
jiabin | 4925685 | 2022-03-09 11:21:35 -0800 | [diff] [blame] | 8226 | if (client->isInvalid()) { |
| 8227 | // No need to take care about invalidated clients. |
| 8228 | continue; |
| 8229 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8230 | sp<DeviceDescriptor> preferredDevice = |
| 8231 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId()); |
| 8232 | if (mEngine->getOutputDevicesForAttributes( |
| 8233 | client->attributes(), preferredDevice, false) == routedDevices) { |
| 8234 | return false; |
| 8235 | } |
| 8236 | } |
| 8237 | return true; |
| 8238 | } |
| 8239 | |
| 8240 | sp<SwAudioOutputDescriptor> AudioPolicyManager::openOutputWithProfileAndDevice( |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 8241 | const sp<IOProfile>& profile, const DeviceVector& devices, |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8242 | const audio_config_base_t *mixerConfig, const audio_config_t *halConfig, |
| 8243 | audio_output_flags_t flags) |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8244 | { |
| 8245 | for (const auto& device : devices) { |
| 8246 | // TODO: This should be checking if the profile supports the device combo. |
| 8247 | if (!profile->supportsDevice(device)) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8248 | ALOGE("%s profile(%s) doesn't support device %#x", __func__, profile->getName().c_str(), |
| 8249 | device->type()); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8250 | return nullptr; |
| 8251 | } |
| 8252 | } |
| 8253 | sp<SwAudioOutputDescriptor> desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 8254 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8255 | status_t status = desc->open(halConfig, mixerConfig, devices, |
| 8256 | AUDIO_STREAM_DEFAULT, flags, &output); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8257 | if (status != NO_ERROR) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8258 | ALOGE("%s failed to open output %d", __func__, status); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8259 | return nullptr; |
| 8260 | } |
| 8261 | |
| 8262 | // Here is where the out_set_parameters() for card & device gets called |
| 8263 | sp<DeviceDescriptor> device = devices.getDeviceForOpening(); |
| 8264 | const audio_devices_t deviceType = device->type(); |
| 8265 | const String8 &address = String8(device->address().c_str()); |
| 8266 | if (!address.isEmpty()) { |
| 8267 | char *param = audio_device_address_to_parameter(deviceType, address.c_str()); |
| 8268 | mpClientInterface->setParameters(output, String8(param)); |
| 8269 | free(param); |
| 8270 | } |
| 8271 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
| 8272 | if (!profile->hasValidAudioProfile()) { |
| 8273 | ALOGW("%s() missing param", __func__); |
| 8274 | desc->close(); |
| 8275 | return nullptr; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8276 | } else if (profile->hasDynamicAudioProfile() && halConfig == nullptr) { |
| 8277 | // Reopen the output with the best audio profile picked by APM when the profile supports |
| 8278 | // dynamic audio profile and the hal config is not specified. |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8279 | desc->close(); |
| 8280 | output = AUDIO_IO_HANDLE_NONE; |
| 8281 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 8282 | profile->pickAudioProfile( |
| 8283 | config.sample_rate, config.channel_mask, config.format); |
| 8284 | config.offload_info.sample_rate = config.sample_rate; |
| 8285 | config.offload_info.channel_mask = config.channel_mask; |
| 8286 | config.offload_info.format = config.format; |
| 8287 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8288 | status = desc->open(&config, mixerConfig, devices, AUDIO_STREAM_DEFAULT, flags, &output); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8289 | if (status != NO_ERROR) { |
| 8290 | return nullptr; |
| 8291 | } |
| 8292 | } |
| 8293 | |
| 8294 | addOutput(output, desc); |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 8295 | |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 8296 | sp<DeviceDescriptor> speaker = mAvailableOutputDevices.getDevice( |
| 8297 | AUDIO_DEVICE_OUT_SPEAKER, String8(""), AUDIO_FORMAT_DEFAULT); |
| 8298 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8299 | if (audio_is_remote_submix_device(deviceType) && address != "0") { |
| 8300 | sp<AudioPolicyMix> policyMix; |
| 8301 | if (mPolicyMixes.getAudioPolicyMix(deviceType, address, policyMix) == NO_ERROR) { |
| 8302 | policyMix->setOutput(desc); |
| 8303 | desc->mPolicyMix = policyMix; |
| 8304 | } else { |
| 8305 | ALOGW("checkOutputsForDevice() cannot find policy for address %s", |
| 8306 | address.string()); |
| 8307 | } |
| 8308 | |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 8309 | } else if (hasPrimaryOutput() && speaker != nullptr |
| 8310 | && mPrimaryOutput->supportsDevice(speaker) && !desc->supportsDevice(speaker) |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 8311 | && ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) { |
| 8312 | // no duplicated output for: |
| 8313 | // - direct outputs |
| 8314 | // - outputs used by dynamic policy mixes |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 8315 | // - outputs that supports SPEAKER while the primary output does not. |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8316 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
| 8317 | |
| 8318 | //TODO: configure audio effect output stage here |
| 8319 | |
| 8320 | // open a duplicating output thread for the new output and the primary output |
| 8321 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 8322 | new SwAudioOutputDescriptor(nullptr, mpClientInterface); |
| 8323 | status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, &duplicatedOutput); |
| 8324 | if (status == NO_ERROR) { |
| 8325 | // add duplicated output descriptor |
| 8326 | addOutput(duplicatedOutput, dupOutputDesc); |
| 8327 | } else { |
| 8328 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
| 8329 | mPrimaryOutput->mIoHandle, output); |
| 8330 | desc->close(); |
| 8331 | removeOutput(output); |
| 8332 | nextAudioPortGeneration(); |
| 8333 | return nullptr; |
| 8334 | } |
| 8335 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 8336 | if (mPrimaryOutput == nullptr && profile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 8337 | ALOGV("%s(): re-assigning mPrimaryOutput", __func__); |
| 8338 | mPrimaryOutput = desc; |
| 8339 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8340 | return desc; |
| 8341 | } |
| 8342 | |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 8343 | status_t AudioPolicyManager::getDevicesForAttributes( |
| 8344 | const audio_attributes_t &attr, DeviceVector &devices, bool forVolume) { |
| 8345 | // Devices are determined in the following precedence: |
| 8346 | // |
| 8347 | // 1) Devices associated with a dynamic policy matching the attributes. This is often |
| 8348 | // a remote submix from MIX_ROUTE_FLAG_LOOP_BACK. |
| 8349 | // |
| 8350 | // If no such dynamic policy then |
| 8351 | // 2) Devices containing an active client using setPreferredDevice |
| 8352 | // with same strategy as the attributes. |
| 8353 | // (from the default Engine::getOutputDevicesForAttributes() implementation). |
| 8354 | // |
| 8355 | // If no corresponding active client with setPreferredDevice then |
| 8356 | // 3) Devices associated with the strategy determined by the attributes |
| 8357 | // (from the default Engine::getOutputDevicesForAttributes() implementation). |
| 8358 | // |
| 8359 | // See related getOutputForAttrInt(). |
| 8360 | |
| 8361 | // check dynamic policies but only for primary descriptors (secondary not used for audible |
| 8362 | // audio routing, only used for duplication for playback capture) |
| 8363 | sp<AudioPolicyMix> policyMix; |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 8364 | bool unneededUsePrimaryOutputFromPolicyMixes = false; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 8365 | status_t status = mPolicyMixes.getOutputForAttr(attr, AUDIO_CONFIG_BASE_INITIALIZER, |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 8366 | 0 /*uid unknown here*/, AUDIO_SESSION_NONE, AUDIO_OUTPUT_FLAG_NONE, |
| 8367 | mAvailableOutputDevices, nullptr /* requestedDevice */, policyMix, |
| 8368 | nullptr /* secondaryMixes */, unneededUsePrimaryOutputFromPolicyMixes); |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 8369 | if (status != OK) { |
| 8370 | return status; |
| 8371 | } |
| 8372 | |
| 8373 | if (policyMix != nullptr && policyMix->getOutput() != nullptr && |
| 8374 | // For volume control, skip LOOPBACK mixes which use AUDIO_DEVICE_OUT_REMOTE_SUBMIX |
| 8375 | // as they are unaffected by device/stream volume |
| 8376 | // (per SwAudioOutputDescriptor::isFixedVolume()). |
| 8377 | (!forVolume || policyMix->mDeviceType != AUDIO_DEVICE_OUT_REMOTE_SUBMIX) |
| 8378 | ) { |
| 8379 | sp<DeviceDescriptor> deviceDesc = mAvailableOutputDevices.getDevice( |
| 8380 | policyMix->mDeviceType, policyMix->mDeviceAddress, AUDIO_FORMAT_DEFAULT); |
| 8381 | devices.add(deviceDesc); |
| 8382 | } else { |
| 8383 | // The default Engine::getOutputDevicesForAttributes() uses findPreferredDevice() |
| 8384 | // which selects setPreferredDevice if active. This means forVolume call |
| 8385 | // will take an active setPreferredDevice, if such exists. |
| 8386 | |
| 8387 | devices = mEngine->getOutputDevicesForAttributes( |
| 8388 | attr, nullptr /* preferredDevice */, false /* fromCache */); |
| 8389 | } |
| 8390 | |
| 8391 | if (forVolume) { |
| 8392 | // We alias the device AUDIO_DEVICE_OUT_SPEAKER_SAFE to AUDIO_DEVICE_OUT_SPEAKER |
| 8393 | // for single volume control in AudioService (such relationship should exist if |
| 8394 | // SPEAKER_SAFE is present). |
| 8395 | // |
| 8396 | // (This is unrelated to a different device grouping as Volume::getDeviceCategory) |
| 8397 | DeviceVector speakerSafeDevices = |
| 8398 | devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER_SAFE); |
| 8399 | if (!speakerSafeDevices.isEmpty()) { |
| 8400 | devices.merge(mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER)); |
| 8401 | devices.remove(speakerSafeDevices); |
| 8402 | } |
| 8403 | } |
| 8404 | |
| 8405 | return NO_ERROR; |
| 8406 | } |
| 8407 | |
| 8408 | status_t AudioPolicyManager::getProfilesForDevices(const DeviceVector& devices, |
| 8409 | AudioProfileVector& audioProfiles, |
| 8410 | uint32_t flags, |
| 8411 | bool isInput) { |
| 8412 | for (const auto& hwModule : mHwModules) { |
| 8413 | // the MSD module checks for different conditions |
| 8414 | if (strcmp(hwModule->getName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0) { |
| 8415 | continue; |
| 8416 | } |
| 8417 | IOProfileCollection ioProfiles = isInput ? hwModule->getInputProfiles() |
| 8418 | : hwModule->getOutputProfiles(); |
| 8419 | for (const auto& profile : ioProfiles) { |
| 8420 | if (!profile->areAllDevicesSupported(devices) || |
| 8421 | !profile->isCompatibleProfileForFlags( |
| 8422 | flags, false /*exactMatchRequiredForInputFlags*/)) { |
| 8423 | continue; |
| 8424 | } |
| 8425 | audioProfiles.addAllValidProfiles(profile->asAudioPort()->getAudioProfiles()); |
| 8426 | } |
| 8427 | } |
| 8428 | |
| 8429 | if (!isInput) { |
| 8430 | // add the direct profiles from MSD if present and has audio patches to all the output(s) |
| 8431 | const auto &msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 8432 | if (msdModule != nullptr) { |
| 8433 | if (msdHasPatchesToAllDevices(devices.toTypeAddrVector())) { |
| 8434 | ALOGV("%s: MSD audio patches set to all output devices.", __func__); |
| 8435 | for (const auto &profile: msdModule->getOutputProfiles()) { |
| 8436 | if (!profile->asAudioPort()->isDirectOutput()) { |
| 8437 | continue; |
| 8438 | } |
| 8439 | audioProfiles.addAllValidProfiles(profile->asAudioPort()->getAudioProfiles()); |
| 8440 | } |
| 8441 | } else { |
| 8442 | ALOGV("%s: MSD audio patches NOT set to all output devices.", __func__); |
| 8443 | } |
| 8444 | } |
| 8445 | } |
| 8446 | |
| 8447 | return NO_ERROR; |
| 8448 | } |
| 8449 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 8450 | sp<SwAudioOutputDescriptor> AudioPolicyManager::reopenOutput(sp<SwAudioOutputDescriptor> outputDesc, |
| 8451 | const audio_config_t *config, |
| 8452 | audio_output_flags_t flags, |
| 8453 | const char* caller) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8454 | closeOutput(outputDesc->mIoHandle); |
| 8455 | sp<SwAudioOutputDescriptor> preferredOutput = openOutputWithProfileAndDevice( |
| 8456 | outputDesc->mProfile, outputDesc->devices(), nullptr /*mixerConfig*/, config, flags); |
| 8457 | if (preferredOutput == nullptr) { |
| 8458 | ALOGE("%s failed to reopen output device=%d, caller=%s", |
| 8459 | __func__, outputDesc->devices()[0]->getId(), caller); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8460 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 8461 | return preferredOutput; |
| 8462 | } |
| 8463 | |
| 8464 | void AudioPolicyManager::reopenOutputsWithDevices( |
| 8465 | const std::map<audio_io_handle_t, DeviceVector> &outputsToReopen) { |
| 8466 | for (const auto& [output, devices] : outputsToReopen) { |
| 8467 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 8468 | closeOutput(output); |
| 8469 | openOutputWithProfileAndDevice(desc->mProfile, devices); |
| 8470 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8471 | } |
| 8472 | |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 8473 | PortHandleVector AudioPolicyManager::getClientsForStream( |
| 8474 | audio_stream_type_t streamType) const { |
| 8475 | PortHandleVector clients; |
| 8476 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 8477 | PortHandleVector clientsForStream = mOutputs.valueAt(i)->getClientsForStream(streamType); |
| 8478 | clients.insert(clients.end(), clientsForStream.begin(), clientsForStream.end()); |
| 8479 | } |
| 8480 | return clients; |
| 8481 | } |
| 8482 | |
| 8483 | void AudioPolicyManager::invalidateStreams(StreamTypeVector streams) const { |
| 8484 | PortHandleVector clients; |
| 8485 | for (auto stream : streams) { |
| 8486 | PortHandleVector clientsForStream = getClientsForStream(stream); |
| 8487 | clients.insert(clients.end(), clientsForStream.begin(), clientsForStream.end()); |
| 8488 | } |
| 8489 | mpClientInterface->invalidateTracks(clients); |
| 8490 | } |
| 8491 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 8492 | } // namespace android |