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, |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 119 | media::DeviceConnectedState 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); |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 123 | if (status_t status = mpClientInterface->setDeviceConnectedState(&devicePort, state); |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 124 | status != OK) { |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 125 | ALOGE("Error %d while setting connected state for device %s", state, |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 126 | device->getDeviceTypeAddr().toString(false).c_str()); |
| 127 | } |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 130 | status_t AudioPolicyManager::setDeviceConnectionStateInt( |
| 131 | audio_policy_dev_state_t state, const android::media::audio::common::AudioPort& port, |
| 132 | audio_format_t encodedFormat) { |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 133 | if (port.ext.getTag() != AudioPortExt::device) { |
| 134 | return BAD_VALUE; |
| 135 | } |
| 136 | audio_devices_t device_type; |
| 137 | std::string device_address; |
| 138 | if (status_t status = aidl2legacy_AudioDevice_audio_device( |
| 139 | port.ext.get<AudioPortExt::device>().device, &device_type, &device_address); |
| 140 | status != OK) { |
| 141 | return status; |
| 142 | }; |
| 143 | const char* device_name = port.name.c_str(); |
| 144 | // connect/disconnect only 1 device at a time |
| 145 | if (!audio_is_output_device(device_type) && !audio_is_input_device(device_type)) |
| 146 | return BAD_VALUE; |
| 147 | |
| 148 | sp<DeviceDescriptor> device = mHwModules.getDeviceDescriptor( |
| 149 | device_type, device_address.c_str(), device_name, encodedFormat, |
| 150 | state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Mikhail Naganov | ddc5f31 | 2022-06-11 00:47:52 +0000 | [diff] [blame] | 151 | if (device == nullptr) { |
| 152 | return INVALID_OPERATION; |
| 153 | } |
| 154 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 155 | device->setExtraAudioDescriptors(port.extraAudioDescriptors); |
| 156 | } |
| 157 | return setDeviceConnectionStateInt(device, state); |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 158 | } |
| 159 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 160 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t deviceType, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 161 | audio_policy_dev_state_t state, |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 162 | const char* device_address, |
| 163 | const char* device_name, |
| 164 | audio_format_t encodedFormat) { |
Atneya Nair | 638a6e4 | 2022-12-18 16:45:15 -0800 | [diff] [blame] | 165 | media::AudioPortFw aidlPort; |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 166 | if (status_t status = deviceToAudioPort(deviceType, device_address, device_name, &aidlPort); |
| 167 | status == OK) { |
| 168 | return setDeviceConnectionStateInt(state, aidlPort.hal, encodedFormat); |
| 169 | } else { |
| 170 | ALOGE("Failed to convert to AudioPort Parcelable: %s", statusToString(status).c_str()); |
| 171 | return status; |
| 172 | } |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 173 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 174 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 175 | status_t AudioPolicyManager::setDeviceConnectionStateInt(const sp<DeviceDescriptor> &device, |
| 176 | audio_policy_dev_state_t state) |
| 177 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 178 | // handle output devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 179 | if (audio_is_output_device(device->type())) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 180 | SortedVector <audio_io_handle_t> outputs; |
| 181 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 182 | ssize_t index = mAvailableOutputDevices.indexOf(device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 183 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 184 | // save a copy of the opened output descriptors before any output is opened or closed |
| 185 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 186 | mPreviousOutputs = mOutputs; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 187 | |
| 188 | bool wasLeUnicastActive = isLeUnicastActive(); |
| 189 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 190 | switch (state) |
| 191 | { |
| 192 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 193 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 194 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 195 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 196 | return INVALID_OPERATION; |
| 197 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 198 | ALOGV("%s() connecting device %s format %x", |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 199 | __func__, device->toString().c_str(), device->getEncodedFormat()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 200 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 201 | // register new device as available |
Francois Gaffie | 993f390 | 2019-04-10 15:39:27 +0200 | [diff] [blame] | 202 | if (mAvailableOutputDevices.add(device) < 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 203 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 204 | } |
| 205 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 206 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 207 | // parameters on newly connected devices (instead of opening the outputs...) |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 208 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::CONNECTED); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 209 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 210 | if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) { |
| 211 | mAvailableOutputDevices.remove(device); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 212 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 213 | mHwModules.cleanUpForDevice(device); |
| 214 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 215 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 216 | return INVALID_OPERATION; |
| 217 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 218 | |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 219 | // Populate encapsulation information when a output device is connected. |
| 220 | device->setEncapsulationInfoFromHal(mpClientInterface); |
| 221 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 222 | // outputs should never be empty here |
| 223 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 224 | "checkOutputsForDevice() returned no outputs but status OK"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 225 | ALOGV("%s() checkOutputsForDevice() returned %zu outputs", __func__, outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 226 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 227 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 228 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 229 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 230 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 231 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 232 | return INVALID_OPERATION; |
| 233 | } |
| 234 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 235 | ALOGV("%s() disconnecting output device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 236 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 237 | // Notify the HAL to prepare to disconnect device |
| 238 | broadcastDeviceConnectionState( |
| 239 | device, media::DeviceConnectedState::PREPARE_TO_DISCONNECT); |
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 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 248 | // Send Disconnect to HALs |
| 249 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED); |
| 250 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 251 | // Reset active device codec |
| 252 | device->setEncodedFormat(AUDIO_FORMAT_DEFAULT); |
| 253 | |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 254 | // remove device from mReportedFormatsMap cache |
| 255 | mReportedFormatsMap.erase(device); |
| 256 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 257 | // remove preferred mixer configurations |
| 258 | mPreferredMixerAttrInfos.erase(device->getId()); |
| 259 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 260 | } break; |
| 261 | |
| 262 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 263 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 264 | return BAD_VALUE; |
| 265 | } |
| 266 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 267 | // Propagate device availability to Engine |
| 268 | setEngineDeviceConnectionState(device, state); |
| 269 | |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 270 | // No need to evaluate playback routing when connecting a remote submix |
| 271 | // output device used by a dynamic policy of type recorder as no |
| 272 | // playback use case is affected. |
| 273 | bool doCheckForDeviceAndOutputChanges = true; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 274 | if (device->type() == AUDIO_DEVICE_OUT_REMOTE_SUBMIX && device->address() != "0") { |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 275 | for (audio_io_handle_t output : outputs) { |
| 276 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 277 | sp<AudioPolicyMix> policyMix = desc->mPolicyMix.promote(); |
| 278 | if (policyMix != nullptr |
| 279 | && policyMix->mMixType == MIX_TYPE_RECORDERS |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 280 | && device->address() == policyMix->mDeviceAddress.string()) { |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 281 | doCheckForDeviceAndOutputChanges = false; |
| 282 | break; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | auto checkCloseOutputs = [&]() { |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 288 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 289 | if (!outputs.isEmpty()) { |
| 290 | for (audio_io_handle_t output : outputs) { |
| 291 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 292 | // close unused outputs after device disconnection or direct outputs that have |
| 293 | // been opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 294 | // "outputs" vector never contains duplicated outputs |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 295 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) |
| 296 | || (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 297 | (desc->mDirectOpenCount == 0)) |
| 298 | || (((desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) && |
| 299 | !isOutputOnlyAvailableRouteToSomeDevice(desc))) { |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 300 | clearAudioSourcesForOutput(output); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 301 | closeOutput(output); |
| 302 | } |
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 | // check A2DP again after closing A2DP output to reset mA2dpSuspended if needed |
| 305 | return true; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 306 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 307 | return false; |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | if (doCheckForDeviceAndOutputChanges) { |
| 311 | checkForDeviceAndOutputChanges(checkCloseOutputs); |
| 312 | } else { |
| 313 | checkCloseOutputs(); |
| 314 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 315 | (void)updateCallRouting(false /*fromCache*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 316 | const DeviceVector msdOutDevices = getMsdAudioOutDevices(); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 317 | const DeviceVector activeMediaDevices = |
| 318 | mEngine->getActiveMediaDevices(mAvailableOutputDevices); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 319 | std::map<audio_io_handle_t, DeviceVector> outputsToReopenWithDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 320 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 321 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jaideep Sharma | 89b5b85 | 2020-11-23 16:41:33 +0530 | [diff] [blame] | 322 | if (desc->isActive() && ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || |
| 323 | (desc != mPrimaryOutput))) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 324 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 325 | // do not force device change on duplicated output because if device is 0, it will |
| 326 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 327 | // a valid device selection on those outputs. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 328 | bool force = (msdOutDevices.isEmpty() || msdOutDevices != desc->devices()) |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 329 | && !desc->isDuplicated() |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 330 | && (!device_distinguishes_on_address(device->type()) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 331 | // always force when disconnecting (a non-duplicated device) |
| 332 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 333 | if (desc->mUsePreferredMixerAttributes && newDevices != desc->devices()) { |
| 334 | // If the device is using preferred mixer attributes, the output need to reopen |
| 335 | // with default configuration when the new selected devices are different from |
| 336 | // current routing devices |
| 337 | outputsToReopenWithDevices.emplace(mOutputs.keyAt(i), newDevices); |
| 338 | continue; |
| 339 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 340 | setOutputDevices(desc, newDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 341 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 342 | if (!desc->isDuplicated() && desc->mProfile->hasDynamicAudioProfile() && |
jiabin | 498d215 | 2021-04-02 00:26:46 +0000 | [diff] [blame] | 343 | !activeMediaDevices.empty() && desc->devices() != activeMediaDevices && |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 344 | desc->supportsDevicesForPlayback(activeMediaDevices)) { |
| 345 | // Reopen the output to query the dynamic profiles when there is not active |
| 346 | // clients or all active clients will be rerouted. Otherwise, set the flag |
| 347 | // `mPendingReopenToQueryProfiles` in the SwOutputDescriptor so that the output |
| 348 | // can be reopened to query dynamic profiles when all clients are inactive. |
| 349 | if (areAllActiveTracksRerouted(desc)) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 350 | outputsToReopenWithDevices.emplace(mOutputs.keyAt(i), activeMediaDevices); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 351 | } else { |
| 352 | desc->mPendingReopenToQueryProfiles = true; |
| 353 | } |
| 354 | } |
| 355 | if (!desc->supportsDevicesForPlayback(activeMediaDevices)) { |
| 356 | // Clear the flag that previously set for re-querying profiles. |
| 357 | desc->mPendingReopenToQueryProfiles = false; |
| 358 | } |
| 359 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 360 | reopenOutputsWithDevices(outputsToReopenWithDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 361 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 362 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 363 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 366 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, 0); |
| 367 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 368 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 369 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 370 | } // end if is output device |
| 371 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 372 | // handle input devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 373 | if (audio_is_input_device(device->type())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 374 | ssize_t index = mAvailableInputDevices.indexOf(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 375 | switch (state) |
| 376 | { |
| 377 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 378 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 379 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 380 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 381 | return INVALID_OPERATION; |
| 382 | } |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 383 | |
| 384 | if (mAvailableInputDevices.add(device) < 0) { |
| 385 | return NO_MEMORY; |
| 386 | } |
| 387 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 388 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 389 | // parameters on newly connected devices (instead of opening the inputs...) |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 390 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::CONNECTED); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 391 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 392 | if (checkInputsForDevice(device, state) != NO_ERROR) { |
| 393 | mAvailableInputDevices.remove(device); |
| 394 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 395 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 396 | |
| 397 | mHwModules.cleanUpForDevice(device); |
| 398 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 399 | return INVALID_OPERATION; |
| 400 | } |
| 401 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 402 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 403 | |
| 404 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 405 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 406 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 407 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 408 | return INVALID_OPERATION; |
| 409 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 410 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 411 | ALOGV("%s() disconnecting input device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 412 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 413 | // Notify the HAL to prepare to disconnect device |
| 414 | broadcastDeviceConnectionState( |
| 415 | device, media::DeviceConnectedState::PREPARE_TO_DISCONNECT); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 416 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 417 | mAvailableInputDevices.remove(device); |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 418 | |
| 419 | checkInputsForDevice(device, state); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 420 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 421 | // Set Disconnect to HALs |
| 422 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED); |
| 423 | |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 424 | // remove device from mReportedFormatsMap cache |
| 425 | mReportedFormatsMap.erase(device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 426 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 427 | |
| 428 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 429 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 430 | return BAD_VALUE; |
| 431 | } |
| 432 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 433 | // Propagate device availability to Engine |
| 434 | setEngineDeviceConnectionState(device, state); |
| 435 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 436 | checkCloseInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 437 | // As the input device list can impact the output device selection, update |
| 438 | // getDeviceForStrategy() cache |
| 439 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 440 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 441 | (void)updateCallRouting(false /*fromCache*/); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 442 | // Reconnect Audio Source |
| 443 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { |
| 444 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); |
| 445 | checkAudioSourceForAttributes(attributes); |
| 446 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 447 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 448 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 451 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 452 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 453 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 454 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 455 | ALOGW("%s() invalid device: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 456 | return BAD_VALUE; |
| 457 | } |
| 458 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 459 | status_t AudioPolicyManager::deviceToAudioPort(audio_devices_t device, const char* device_address, |
| 460 | const char* device_name, |
Atneya Nair | 638a6e4 | 2022-12-18 16:45:15 -0800 | [diff] [blame] | 461 | media::AudioPortFw* aidlPort) { |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 462 | DeviceDescriptorBase devDescr(device, device_address); |
| 463 | devDescr.setName(device_name); |
| 464 | return devDescr.writeToParcelable(aidlPort); |
| 465 | } |
| 466 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 467 | void AudioPolicyManager::setEngineDeviceConnectionState(const sp<DeviceDescriptor> device, |
| 468 | audio_policy_dev_state_t state) { |
| 469 | |
| 470 | // the Engine does not have to know about remote submix devices used by dynamic audio policies |
| 471 | if (audio_is_remote_submix_device(device->type()) && device->address() != "0") { |
| 472 | return; |
| 473 | } |
| 474 | mEngine->setDeviceConnectionState(device, state); |
| 475 | } |
| 476 | |
| 477 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 478 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 479 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 480 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 481 | sp<DeviceDescriptor> devDesc = |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 482 | mHwModules.getDeviceDescriptor(device, device_address, "", AUDIO_FORMAT_DEFAULT, |
| 483 | false /* allowToCreate */, |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 484 | (strlen(device_address) != 0)/*matchAddress*/); |
| 485 | |
| 486 | if (devDesc == 0) { |
François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 487 | ALOGV("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 488 | device, device_address); |
| 489 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 490 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 491 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 492 | DeviceVector *deviceVector; |
| 493 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 494 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 495 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 496 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 497 | deviceVector = &mAvailableInputDevices; |
| 498 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 499 | ALOGW("%s() invalid device type %08x", __func__, device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 500 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 501 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 502 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 503 | return (deviceVector->getDevice( |
| 504 | device, String8(device_address), AUDIO_FORMAT_DEFAULT) != 0) ? |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 505 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 508 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 509 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 510 | const char *device_name, |
| 511 | audio_format_t encodedFormat) |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 512 | { |
| 513 | status_t status; |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 514 | String8 reply; |
| 515 | AudioParameter param; |
| 516 | int isReconfigA2dpSupported = 0; |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 517 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 518 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s encodedFormat: 0x%X", |
| 519 | device, device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 520 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 521 | // connect/disconnect only 1 device at a time |
| 522 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 523 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 524 | // Check if the device is currently connected |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 525 | DeviceVector deviceList = mAvailableOutputDevices.getDevicesFromType(device); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 526 | if (deviceList.empty()) { |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 527 | // Nothing to do: device is not connected |
| 528 | return NO_ERROR; |
| 529 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 530 | sp<DeviceDescriptor> devDesc = deviceList.itemAt(0); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 531 | |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 532 | // For offloaded A2DP, Hw modules may have the capability to |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 533 | // configure codecs. |
| 534 | // Handle two specific cases by sending a set parameter to |
| 535 | // configure A2DP codecs. No need to toggle device state. |
| 536 | // Case 1: A2DP active device switches from primary to primary |
| 537 | // module |
| 538 | // Case 2: A2DP device config changes on primary module. |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 539 | if (audio_is_a2dp_out_device(device) && hasPrimaryOutput()) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 540 | sp<HwModule> module = mHwModules.getModuleForDeviceType(device, encodedFormat); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 541 | audio_module_handle_t primaryHandle = mPrimaryOutput->getModuleHandle(); |
| 542 | if (availablePrimaryOutputDevices().contains(devDesc) && |
| 543 | (module != 0 && module->getHandle() == primaryHandle)) { |
| 544 | reply = mpClientInterface->getParameters( |
| 545 | AUDIO_IO_HANDLE_NONE, |
| 546 | String8(AudioParameter::keyReconfigA2dpSupported)); |
| 547 | AudioParameter repliedParameters(reply); |
| 548 | repliedParameters.getInt( |
| 549 | String8(AudioParameter::keyReconfigA2dpSupported), isReconfigA2dpSupported); |
| 550 | if (isReconfigA2dpSupported) { |
| 551 | const String8 key(AudioParameter::keyReconfigA2dp); |
| 552 | param.add(key, String8("true")); |
| 553 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 554 | devDesc->setEncodedFormat(encodedFormat); |
| 555 | return NO_ERROR; |
| 556 | } |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 557 | } |
| 558 | } |
cnx421 | bd2dcc4 | 2020-07-11 14:58:44 +0800 | [diff] [blame] | 559 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); |
| 560 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 561 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 562 | // mute media strategies and delay device switch by the largest |
| 563 | // This avoid sending the music tail into the earpiece or headset. |
| 564 | setStrategyMute(musicStrategy, true, desc); |
| 565 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, |
| 566 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 567 | nullptr, true /*fromCache*/).types()); |
| 568 | } |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 569 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 570 | // This will force reading again the device configuration |
| 571 | status = setDeviceConnectionState(device, |
| 572 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 573 | device_address, device_name, |
| 574 | devDesc->getEncodedFormat()); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 575 | if (status != NO_ERROR) { |
| 576 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 577 | status); |
| 578 | return status; |
| 579 | } |
| 580 | |
| 581 | status = setDeviceConnectionState(device, |
| 582 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 583 | device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 584 | if (status != NO_ERROR) { |
| 585 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 586 | status); |
| 587 | return status; |
| 588 | } |
| 589 | |
| 590 | return NO_ERROR; |
| 591 | } |
| 592 | |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 593 | status_t AudioPolicyManager::getHwOffloadFormatsSupportedForBluetoothMedia( |
| 594 | audio_devices_t device, std::vector<audio_format_t> *formats) |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 595 | { |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 596 | ALOGV("getHwOffloadFormatsSupportedForBluetoothMedia()"); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 597 | status_t status = NO_ERROR; |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 598 | std::unordered_set<audio_format_t> formatSet; |
| 599 | sp<HwModule> primaryModule = |
| 600 | mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY); |
Aniket Kumar Lata | fedb598 | 2019-07-03 11:20:36 -0700 | [diff] [blame] | 601 | if (primaryModule == nullptr) { |
| 602 | ALOGE("%s() unable to get primary module", __func__); |
| 603 | return NO_INIT; |
| 604 | } |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 605 | |
| 606 | DeviceTypeSet audioDeviceSet; |
| 607 | |
| 608 | switch(device) { |
| 609 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: |
| 610 | audioDeviceSet = getAudioDeviceOutAllA2dpSet(); |
| 611 | break; |
| 612 | case AUDIO_DEVICE_OUT_BLE_HEADSET: |
Patty Huang | 36028df | 2022-07-06 00:14:12 +0800 | [diff] [blame] | 613 | audioDeviceSet = getAudioDeviceOutLeAudioUnicastSet(); |
| 614 | break; |
| 615 | case AUDIO_DEVICE_OUT_BLE_BROADCAST: |
| 616 | audioDeviceSet = getAudioDeviceOutLeAudioBroadcastSet(); |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 617 | break; |
| 618 | default: |
| 619 | ALOGE("%s() device type 0x%08x not supported", __func__, device); |
| 620 | return BAD_VALUE; |
| 621 | } |
| 622 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 623 | DeviceVector declaredDevices = primaryModule->getDeclaredDevices().getDevicesFromTypes( |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 624 | audioDeviceSet); |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 625 | for (const auto& device : declaredDevices) { |
| 626 | formatSet.insert(device->encodedFormats().begin(), device->encodedFormats().end()); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 627 | } |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 628 | formats->assign(formatSet.begin(), formatSet.end()); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 629 | return status; |
| 630 | } |
| 631 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 632 | DeviceVector AudioPolicyManager::selectBestRxSinkDevicesForCall(bool fromCache) |
| 633 | { |
| 634 | DeviceVector rxSinkdevices{}; |
| 635 | rxSinkdevices = mEngine->getOutputDevicesForAttributes( |
| 636 | attributes_initializer(AUDIO_USAGE_VOICE_COMMUNICATION), nullptr, fromCache); |
| 637 | if (!rxSinkdevices.isEmpty() && mAvailableOutputDevices.contains(rxSinkdevices.itemAt(0))) { |
| 638 | auto rxSinkDevice = rxSinkdevices.itemAt(0); |
| 639 | auto telephonyRxModule = mHwModules.getModuleForDeviceType( |
| 640 | AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); |
| 641 | // retrieve Rx Source device descriptor |
| 642 | sp<DeviceDescriptor> rxSourceDevice = mAvailableInputDevices.getDevice( |
| 643 | AUDIO_DEVICE_IN_TELEPHONY_RX, String8(), AUDIO_FORMAT_DEFAULT); |
| 644 | |
| 645 | // RX Telephony and Rx sink devices are declared by Primary Audio HAL |
| 646 | if (isPrimaryModule(telephonyRxModule) && (telephonyRxModule->getHalVersionMajor() >= 3) && |
| 647 | telephonyRxModule->supportsPatch(rxSourceDevice, rxSinkDevice)) { |
| 648 | ALOGW("%s() device %s using HW Bridge", __func__, rxSinkDevice->toString().c_str()); |
| 649 | return DeviceVector(rxSinkDevice); |
| 650 | } |
| 651 | } |
| 652 | // Note that despite the fact that getNewOutputDevices() is called on the primary output, |
| 653 | // the device returned is not necessarily reachable via this output |
| 654 | // (filter later by setOutputDevices()) |
| 655 | return getNewOutputDevices(mPrimaryOutput, fromCache); |
| 656 | } |
| 657 | |
| 658 | status_t AudioPolicyManager::updateCallRouting(bool fromCache, uint32_t delayMs, uint32_t *waitMs) |
| 659 | { |
| 660 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
| 661 | DeviceVector rxDevices = selectBestRxSinkDevicesForCall(fromCache); |
| 662 | return updateCallRoutingInternal(rxDevices, delayMs, waitMs); |
| 663 | } |
| 664 | return INVALID_OPERATION; |
| 665 | } |
| 666 | |
| 667 | status_t AudioPolicyManager::updateCallRoutingInternal( |
| 668 | const DeviceVector &rxDevices, uint32_t delayMs, uint32_t *waitMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 669 | { |
| 670 | bool createTxPatch = false; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 671 | bool createRxPatch = false; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 672 | uint32_t muteWaitMs = 0; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 673 | if(!hasPrimaryOutput() || |
| 674 | mPrimaryOutput->devices().onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_STUB)) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 675 | return INVALID_OPERATION; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 676 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 677 | ALOG_ASSERT(!rxDevices.isEmpty(), "%s() no selected output device", __func__); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 678 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 679 | audio_attributes_t attr = { .source = AUDIO_SOURCE_VOICE_COMMUNICATION }; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 680 | auto txSourceDevice = mEngine->getInputDeviceForAttributes(attr); |
Eric Laurent | cedd5b5 | 2023-03-22 00:03:31 +0000 | [diff] [blame] | 681 | if (txSourceDevice == nullptr) { |
| 682 | ALOGE("%s() selected input device not available", __func__); |
| 683 | return INVALID_OPERATION; |
| 684 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 685 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 686 | ALOGV("%s device rxDevice %s txDevice %s", __func__, |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 687 | rxDevices.itemAt(0)->toString().c_str(), txSourceDevice->toString().c_str()); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 688 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 689 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 690 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 691 | |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 692 | auto telephonyRxModule = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 693 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 694 | auto telephonyTxModule = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 695 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_OUT_TELEPHONY_TX, AUDIO_FORMAT_DEFAULT); |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 696 | // retrieve Rx Source and Tx Sink device descriptors |
| 697 | sp<DeviceDescriptor> rxSourceDevice = |
| 698 | mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX, |
| 699 | String8(), |
| 700 | AUDIO_FORMAT_DEFAULT); |
| 701 | sp<DeviceDescriptor> txSinkDevice = |
| 702 | mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, |
| 703 | String8(), |
| 704 | AUDIO_FORMAT_DEFAULT); |
| 705 | |
| 706 | // RX and TX Telephony device are declared by Primary Audio HAL |
| 707 | if (isPrimaryModule(telephonyRxModule) && isPrimaryModule(telephonyTxModule) && |
| 708 | (telephonyRxModule->getHalVersionMajor() >= 3)) { |
| 709 | if (rxSourceDevice == 0 || txSinkDevice == 0) { |
| 710 | // RX / TX Telephony device(s) is(are) not currently available |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 711 | ALOGE("%s() no telephony Tx and/or RX device", __func__); |
| 712 | return INVALID_OPERATION; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 713 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 714 | // createAudioPatchInternal now supports both HW / SW bridging |
| 715 | createRxPatch = true; |
| 716 | createTxPatch = true; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 717 | } else { |
| 718 | // If the RX device is on the primary HW module, then use legacy routing method for |
| 719 | // voice calls via setOutputDevice() on primary output. |
| 720 | // Otherwise, create two audio patches for TX and RX path. |
| 721 | createRxPatch = !(availablePrimaryOutputDevices().contains(rxDevices.itemAt(0))) && |
| 722 | (rxSourceDevice != 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 723 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 724 | // of it due to legacy implementation. If not, create a patch. |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 725 | createTxPatch = !(availablePrimaryModuleInputDevices().contains(txSourceDevice)) && |
| 726 | (txSinkDevice != 0); |
| 727 | } |
| 728 | // Use legacy routing method for voice calls via setOutputDevice() on primary output. |
| 729 | // Otherwise, create two audio patches for TX and RX path. |
| 730 | if (!createRxPatch) { |
| 731 | muteWaitMs = setOutputDevices(mPrimaryOutput, rxDevices, true, delayMs); |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 732 | } else { // create RX path audio patch |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 733 | connectTelephonyRxAudioSource(); |
juyuchen | 2224c5a | 2019-01-21 12:00:58 +0800 | [diff] [blame] | 734 | // If the TX device is on the primary HW module but RX device is |
| 735 | // on other HW module, SinkMetaData of telephony input should handle it |
| 736 | // assuming the device uses audio HAL V5.0 and above |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 737 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 738 | if (createTxPatch) { // create TX path audio patch |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 739 | // terminate active capture if on the same HW module as the call TX source device |
| 740 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 741 | // call TX device but this information is not in the audio patch and logic here must be |
| 742 | // symmetric to the one in startInput() |
| 743 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
| 744 | if (activeDesc->hasSameHwModuleAs(txSourceDevice)) { |
| 745 | closeActiveClients(activeDesc); |
| 746 | } |
| 747 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 748 | connectTelephonyTxAudioSource(txSourceDevice, txSinkDevice, delayMs); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 749 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 750 | if (waitMs != nullptr) { |
| 751 | *waitMs = muteWaitMs; |
| 752 | } |
| 753 | return NO_ERROR; |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 754 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 755 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 756 | bool AudioPolicyManager::isDeviceOfModule( |
| 757 | const sp<DeviceDescriptor>& devDesc, const char *moduleId) const { |
| 758 | sp<HwModule> module = mHwModules.getModuleFromName(moduleId); |
| 759 | if (module != 0) { |
| 760 | return mAvailableOutputDevices.getDevicesFromHwModule(module->getHandle()) |
| 761 | .indexOf(devDesc) != NAME_NOT_FOUND |
| 762 | || mAvailableInputDevices.getDevicesFromHwModule(module->getHandle()) |
| 763 | .indexOf(devDesc) != NAME_NOT_FOUND; |
| 764 | } |
| 765 | return false; |
| 766 | } |
| 767 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 768 | void AudioPolicyManager::connectTelephonyRxAudioSource() |
| 769 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 770 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 771 | const struct audio_port_config source = { |
| 772 | .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE, |
| 773 | .ext.device.type = AUDIO_DEVICE_IN_TELEPHONY_RX, .ext.device.address = "" |
| 774 | }; |
| 775 | const auto aa = mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 776 | mCallRxSourceClient = startAudioSourceInternal(&source, &aa, 0/*uid*/); |
| 777 | ALOGE_IF(mCallRxSourceClient == nullptr, |
| 778 | "%s failed to start Telephony Rx AudioSource", __func__); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 779 | } |
| 780 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 781 | void AudioPolicyManager::disconnectTelephonyAudioSource(sp<SourceClientDescriptor> &clientDesc) |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 782 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 783 | if (clientDesc == nullptr) { |
| 784 | return; |
| 785 | } |
| 786 | ALOGW_IF(stopAudioSource(clientDesc->portId()) != NO_ERROR, |
| 787 | "%s error stopping audio source", __func__); |
| 788 | clientDesc.clear(); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | void AudioPolicyManager::connectTelephonyTxAudioSource( |
| 792 | const sp<DeviceDescriptor> &srcDevice, const sp<DeviceDescriptor> &sinkDevice, |
| 793 | uint32_t delayMs) |
| 794 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 795 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 796 | if (srcDevice == nullptr || sinkDevice == nullptr) { |
| 797 | ALOGW("%s could not create patch, invalid sink and/or source device(s)", __func__); |
| 798 | return; |
| 799 | } |
| 800 | PatchBuilder patchBuilder; |
| 801 | patchBuilder.addSource(srcDevice).addSink(sinkDevice); |
| 802 | ALOGV("%s between source %s and sink %s", __func__, |
| 803 | srcDevice->toString().c_str(), sinkDevice->toString().c_str()); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 804 | auto callTxSourceClientPortId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 805 | const auto aa = mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL); |
| 806 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 807 | struct audio_port_config source = {}; |
| 808 | srcDevice->toAudioPortConfig(&source); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 809 | mCallTxSourceClient = new InternalSourceClientDescriptor( |
| 810 | callTxSourceClientPortId, mUidCached, aa, source, srcDevice, sinkDevice, |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 811 | mCommunnicationStrategy, toVolumeSource(aa)); |
| 812 | audio_patch_handle_t patchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 813 | status_t status = connectAudioSourceToSink( |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 814 | mCallTxSourceClient, sinkDevice, patchBuilder.patch(), patchHandle, mUidCached, |
| 815 | delayMs); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 816 | ALOGE_IF(status != NO_ERROR, "%s() error %d creating TX audio patch", __func__, status); |
| 817 | if (status == NO_ERROR) { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 818 | mAudioSources.add(callTxSourceClientPortId, mCallTxSourceClient); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 819 | } |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 820 | } |
| 821 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 822 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 823 | { |
| 824 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 825 | // store previous phone state for management of sonification strategy below |
| 826 | int oldState = mEngine->getPhoneState(); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 827 | bool wasLeUnicastActive = isLeUnicastActive(); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 828 | |
| 829 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 830 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 831 | return; |
| 832 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 833 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 834 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 835 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 836 | // force reevaluating accessibility routing when call stops |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 837 | invalidateStreams({AUDIO_STREAM_ACCESSIBILITY}); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 838 | } |
| 839 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 840 | /** |
| 841 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 842 | * routing command. |
| 843 | */ |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 844 | bool force = ((isStateInCall(oldState) != isStateInCall(state)) |
| 845 | || (isStateInCall(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 846 | |
| 847 | // check for device and output changes triggered by new phone state |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 848 | checkForDeviceAndOutputChanges(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 849 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 850 | int delayMs = 0; |
| 851 | if (isStateInCall(state)) { |
| 852 | nsecs_t sysTime = systemTime(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 853 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); |
| 854 | auto sonificationStrategy = streamToStrategy(AUDIO_STREAM_ALARM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 855 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 856 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 857 | // mute media and sonification strategies and delay device switch by the largest |
| 858 | // latency of any output where either strategy is active. |
| 859 | // 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] | 860 | if ((desc->isStrategyActive(musicStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, sysTime) || |
| 861 | desc->isStrategyActive(sonificationStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, |
| 862 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 863 | (delayMs < (int)desc->latency()*2)) { |
| 864 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 865 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 866 | setStrategyMute(musicStrategy, true, desc); |
| 867 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, |
| 868 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 869 | nullptr, true /*fromCache*/).types()); |
| 870 | setStrategyMute(sonificationStrategy, true, desc); |
| 871 | setStrategyMute(sonificationStrategy, false, desc, MUTE_TIME_MS, |
| 872 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_ALARM), |
| 873 | nullptr, true /*fromCache*/).types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 877 | if (hasPrimaryOutput()) { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 878 | if (state == AUDIO_MODE_IN_CALL) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 879 | (void)updateCallRouting(false /*fromCache*/, delayMs); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 880 | } else { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 881 | DeviceVector rxDevices = getNewOutputDevices(mPrimaryOutput, false /*fromCache*/); |
| 882 | // force routing command to audio hardware when ending call |
| 883 | // even if no device change is needed |
| 884 | if (isStateInCall(oldState) && rxDevices.isEmpty()) { |
| 885 | rxDevices = mPrimaryOutput->devices(); |
| 886 | } |
| 887 | if (oldState == AUDIO_MODE_IN_CALL) { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 888 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 889 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 890 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 891 | setOutputDevices(mPrimaryOutput, rxDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 892 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 893 | } |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 894 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 895 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 896 | // reevaluate routing on all outputs in case tracks have been started during the call |
| 897 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 898 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 899 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 900 | if (state != AUDIO_MODE_IN_CALL || (desc != mPrimaryOutput && !isTelephonyRxOrTx(desc))) { |
| 901 | bool forceRouting = !newDevices.isEmpty(); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 902 | if (desc->mUsePreferredMixerAttributes && newDevices != desc->devices()) { |
| 903 | // If the device is using preferred mixer attributes, the output need to reopen |
| 904 | // with default configuration when the new selected devices are different from |
| 905 | // current routing devices. |
| 906 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices); |
| 907 | continue; |
| 908 | } |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 909 | setOutputDevices(desc, newDevices, forceRouting, 0 /*delayMs*/, nullptr, |
| 910 | true /*requiresMuteCheck*/, !forceRouting /*requiresVolumeCheck*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 911 | } |
| 912 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 913 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 914 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 915 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, delayMs); |
| 916 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 917 | if (isStateInCall(state)) { |
| 918 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 919 | // force reevaluating accessibility routing when call starts |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 920 | invalidateStreams({AUDIO_STREAM_ACCESSIBILITY}); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | // 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] | 924 | mLimitRingtoneVolume = (state == AUDIO_MODE_RINGTONE && |
| 925 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 928 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 929 | return mEngine->getPhoneState(); |
| 930 | } |
| 931 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 932 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 933 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 934 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 935 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 936 | if (config == mEngine->getForceUse(usage)) { |
| 937 | return; |
| 938 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 939 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 940 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 941 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 942 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 943 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 944 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 945 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 946 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 947 | |
| 948 | // check for device and output changes triggered by new force usage |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 949 | checkForDeviceAndOutputChanges(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 950 | |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 951 | // force client reconnection to reevaluate flag AUDIO_FLAG_AUDIBILITY_ENFORCED |
| 952 | if (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 953 | invalidateStreams({AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE}); |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 956 | //FIXME: workaround for truncated touch sounds |
| 957 | // to be removed when the problem is handled by system UI |
| 958 | uint32_t delayMs = 0; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 959 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 960 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 961 | } |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 962 | |
| 963 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 964 | updateInputRouting(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 967 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 968 | { |
| 969 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 970 | } |
| 971 | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 972 | // Find an MSD output profile compatible with the parameters passed. |
| 973 | // When "directOnly" is set, restrict search to profiles for direct outputs. |
| 974 | sp<IOProfile> AudioPolicyManager::getMsdProfileForOutput( |
| 975 | const DeviceVector& devices, |
| 976 | uint32_t samplingRate, |
| 977 | audio_format_t format, |
| 978 | audio_channel_mask_t channelMask, |
| 979 | audio_output_flags_t flags, |
| 980 | bool directOnly) |
| 981 | { |
| 982 | flags = getRelevantFlags(flags, directOnly); |
| 983 | |
| 984 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 985 | if (msdModule != nullptr) { |
| 986 | // for the msd module check if there are patches to the output devices |
| 987 | if (msdHasPatchesToAllDevices(devices.toTypeAddrVector())) { |
| 988 | HwModuleCollection modules; |
| 989 | modules.add(msdModule); |
| 990 | return searchCompatibleProfileHwModules( |
| 991 | modules, getMsdAudioOutDevices(), samplingRate, format, channelMask, |
| 992 | flags, directOnly); |
| 993 | } |
| 994 | } |
| 995 | return nullptr; |
| 996 | } |
| 997 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 998 | // Find an output profile compatible with the parameters passed. When "directOnly" is set, restrict |
| 999 | // search to profiles for direct outputs. |
| 1000 | sp<IOProfile> AudioPolicyManager::getProfileForOutput( |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1001 | const DeviceVector& devices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1002 | uint32_t samplingRate, |
| 1003 | audio_format_t format, |
| 1004 | audio_channel_mask_t channelMask, |
| 1005 | audio_output_flags_t flags, |
| 1006 | bool directOnly) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1007 | { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1008 | flags = getRelevantFlags(flags, directOnly); |
| 1009 | |
| 1010 | return searchCompatibleProfileHwModules( |
| 1011 | mHwModules, devices, samplingRate, format, channelMask, flags, directOnly); |
| 1012 | } |
| 1013 | |
| 1014 | audio_output_flags_t AudioPolicyManager::getRelevantFlags ( |
| 1015 | audio_output_flags_t flags, bool directOnly) { |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1016 | if (directOnly) { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1017 | // only retain flags that will drive the direct output profile selection |
| 1018 | // if explicitly requested |
| 1019 | static const uint32_t kRelevantFlags = |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1020 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1021 | AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ); |
| 1022 | flags = (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1023 | } |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1024 | return flags; |
| 1025 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1026 | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1027 | sp<IOProfile> AudioPolicyManager::searchCompatibleProfileHwModules ( |
| 1028 | const HwModuleCollection& hwModules, |
| 1029 | const DeviceVector& devices, |
| 1030 | uint32_t samplingRate, |
| 1031 | audio_format_t format, |
| 1032 | audio_channel_mask_t channelMask, |
| 1033 | audio_output_flags_t flags, |
| 1034 | bool directOnly) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1035 | sp<IOProfile> profile; |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1036 | for (const auto& hwModule : hwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 1037 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1038 | if (!curProfile->isCompatibleProfile(devices, |
| 1039 | samplingRate, NULL /*updatedSamplingRate*/, |
| 1040 | format, NULL /*updatedFormat*/, |
| 1041 | channelMask, NULL /*updatedChannelMask*/, |
| 1042 | flags)) { |
| 1043 | continue; |
| 1044 | } |
| 1045 | // reject profiles not corresponding to a device currently available |
| 1046 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
| 1047 | continue; |
| 1048 | } |
| 1049 | // reject profiles if connected device does not support codec |
| 1050 | if (!curProfile->devicesSupportEncodedFormats(devices.types())) { |
| 1051 | continue; |
| 1052 | } |
| 1053 | if (!directOnly) { |
| 1054 | return curProfile; |
| 1055 | } |
| 1056 | |
| 1057 | // when searching for direct outputs, if several profiles are compatible, give priority |
| 1058 | // to one with offload capability |
Patty Huang | 36028df | 2022-07-06 00:14:12 +0800 | [diff] [blame] | 1059 | if (profile != 0 && |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1060 | ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1061 | continue; |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1062 | } |
| 1063 | profile = curProfile; |
| 1064 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1065 | break; |
| 1066 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1067 | } |
| 1068 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1069 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1072 | sp<IOProfile> AudioPolicyManager::getSpatializerOutputProfile( |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 1073 | const audio_config_t *config __unused, const AudioDeviceTypeAddrVector &devices) const |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1074 | { |
| 1075 | for (const auto& hwModule : mHwModules) { |
| 1076 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 1077 | if (curProfile->getFlags() != AUDIO_OUTPUT_FLAG_SPATIALIZER) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1078 | continue; |
| 1079 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1080 | if (!devices.empty()) { |
Eric Laurent | 0c8f7cc | 2022-06-24 14:32:36 +0200 | [diff] [blame] | 1081 | // reject profiles not corresponding to a device currently available |
| 1082 | DeviceVector supportedDevices = curProfile->getSupportedDevices(); |
| 1083 | if (!mAvailableOutputDevices.containsAtLeastOne(supportedDevices)) { |
| 1084 | continue; |
| 1085 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1086 | if (supportedDevices.getDevicesFromDeviceTypeAddrVec(devices).size() |
| 1087 | != devices.size()) { |
| 1088 | continue; |
| 1089 | } |
| 1090 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1091 | ALOGV("%s found profile %s", __func__, curProfile->getName().c_str()); |
| 1092 | return curProfile; |
| 1093 | } |
| 1094 | } |
| 1095 | return nullptr; |
| 1096 | } |
| 1097 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1098 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1099 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1100 | DeviceVector devices = mEngine->getOutputDevicesForStream(stream, false /*fromCache*/); |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1101 | |
| 1102 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). |
| 1103 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, |
| 1104 | // format, flags, etc. This may result in some discrepancy for functions that utilize |
| 1105 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() |
| 1106 | // and AudioSystem::getOutputSamplingRate(). |
| 1107 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1108 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1109 | const audio_io_handle_t output = selectOutput(outputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1110 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1111 | ALOGV("getOutput() stream %d selected devices %s, output %d", stream, |
| 1112 | devices.toString().c_str(), output); |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1113 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1116 | status_t AudioPolicyManager::getAudioAttributes(audio_attributes_t *dstAttr, |
| 1117 | const audio_attributes_t *srcAttr, |
| 1118 | audio_stream_type_t srcStream) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1119 | { |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1120 | if (srcAttr != NULL) { |
| 1121 | if (!isValidAttributes(srcAttr)) { |
| 1122 | ALOGE("%s invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 1123 | __func__, |
| 1124 | srcAttr->usage, srcAttr->content_type, srcAttr->flags, |
| 1125 | srcAttr->tags); |
| 1126 | return BAD_VALUE; |
| 1127 | } |
| 1128 | *dstAttr = *srcAttr; |
| 1129 | } else { |
| 1130 | if (srcStream < AUDIO_STREAM_MIN || srcStream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 1131 | ALOGE("%s: invalid stream type", __func__); |
| 1132 | return BAD_VALUE; |
| 1133 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1134 | *dstAttr = mEngine->getAttributesForStreamType(srcStream); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1135 | } |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 1136 | |
| 1137 | // Only honor audibility enforced when required. The client will be |
| 1138 | // forced to reconnect if the forced usage changes. |
| 1139 | 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] | 1140 | dstAttr->flags = static_cast<audio_flags_mask_t>( |
| 1141 | dstAttr->flags & ~AUDIO_FLAG_AUDIBILITY_ENFORCED); |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1144 | return NO_ERROR; |
| 1145 | } |
| 1146 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1147 | status_t AudioPolicyManager::getOutputForAttrInt( |
| 1148 | audio_attributes_t *resultAttr, |
| 1149 | audio_io_handle_t *output, |
| 1150 | audio_session_t session, |
| 1151 | const audio_attributes_t *attr, |
| 1152 | audio_stream_type_t *stream, |
| 1153 | uid_t uid, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1154 | audio_config_t *config, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1155 | audio_output_flags_t *flags, |
| 1156 | audio_port_handle_t *selectedDeviceId, |
| 1157 | bool *isRequestedDeviceForExclusiveUse, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1158 | std::vector<sp<AudioPolicyMix>> *secondaryMixes, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1159 | output_type_t *outputType, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1160 | bool *isSpatialized, |
| 1161 | bool *isBitPerfect) |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1162 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1163 | DeviceVector outputDevices; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1164 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1165 | DeviceVector msdDevices = getMsdAudioOutDevices(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1166 | const sp<DeviceDescriptor> requestedDevice = |
| 1167 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 1168 | |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1169 | *outputType = API_OUTPUT_INVALID; |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1170 | *isSpatialized = false; |
| 1171 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1172 | status_t status = getAudioAttributes(resultAttr, attr, *stream); |
| 1173 | if (status != NO_ERROR) { |
| 1174 | return status; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1175 | } |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1176 | if (auto it = mAllowedCapturePolicies.find(uid); it != end(mAllowedCapturePolicies)) { |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1177 | resultAttr->flags = static_cast<audio_flags_mask_t>(resultAttr->flags | it->second); |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1178 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1179 | *stream = mEngine->getStreamTypeForAttributes(*resultAttr); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1180 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1181 | ALOGV("%s() attributes=%s stream=%s session %d selectedDeviceId %d", __func__, |
| 1182 | toString(*resultAttr).c_str(), toString(*stream).c_str(), session, requestedPortId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1183 | |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 1184 | bool usePrimaryOutputFromPolicyMixes = false; |
| 1185 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1186 | // The primary output is the explicit routing (eg. setPreferredDevice) if specified, |
| 1187 | // otherwise, fallback to the dynamic policies, if none match, query the engine. |
| 1188 | // 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] | 1189 | sp<AudioPolicyMix> primaryMix; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1190 | const audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
| 1191 | .channel_mask = config->channel_mask, |
| 1192 | .format = config->format, |
| 1193 | }; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1194 | status = mPolicyMixes.getOutputForAttr(*resultAttr, clientConfig, uid, session, *flags, |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 1195 | mAvailableOutputDevices, requestedDevice, primaryMix, |
| 1196 | secondaryMixes, usePrimaryOutputFromPolicyMixes); |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1197 | if (status != OK) { |
| 1198 | return status; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1199 | } |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1200 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1201 | // FIXME: in case of RENDER policy, the output capabilities should be checked |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1202 | if ((secondaryMixes != nullptr && !secondaryMixes->empty()) |
| 1203 | && !audio_is_linear_pcm(config->format)) { |
| 1204 | ALOGD("%s: rejecting request as secondary mixes only support pcm", __func__); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1205 | return BAD_VALUE; |
| 1206 | } |
| 1207 | if (usePrimaryOutputFromPolicyMixes) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1208 | sp<DeviceDescriptor> deviceDesc = |
| 1209 | mAvailableOutputDevices.getDevice(primaryMix->mDeviceType, |
| 1210 | primaryMix->mDeviceAddress, |
| 1211 | AUDIO_FORMAT_DEFAULT); |
| 1212 | sp<SwAudioOutputDescriptor> policyDesc = primaryMix->getOutput(); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1213 | bool tryDirectForFlags = policyDesc == nullptr || |
| 1214 | (policyDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT); |
| 1215 | // if a direct output can be opened to deliver the track's multi-channel content to the |
| 1216 | // output rather than being downmixed by the primary output, then use this direct |
| 1217 | // output by by-passing the primary mix if possible, otherwise fall-through to primary |
| 1218 | // mix. |
| 1219 | bool tryDirectForChannelMask = policyDesc != nullptr |
| 1220 | && (audio_channel_count_from_out_mask(policyDesc->getConfig().channel_mask) < |
| 1221 | audio_channel_count_from_out_mask(config->channel_mask)); |
| 1222 | if (deviceDesc != nullptr && (tryDirectForFlags || tryDirectForChannelMask)) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1223 | audio_io_handle_t newOutput; |
| 1224 | status = openDirectOutput( |
| 1225 | *stream, session, config, |
| 1226 | (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT), |
| 1227 | DeviceVector(deviceDesc), &newOutput); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1228 | if (status == NO_ERROR) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1229 | policyDesc = mOutputs.valueFor(newOutput); |
| 1230 | primaryMix->setOutput(policyDesc); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1231 | } else if (tryDirectForFlags) { |
| 1232 | policyDesc = nullptr; |
| 1233 | } // otherwise use primary if available. |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1234 | } |
| 1235 | if (policyDesc != nullptr) { |
| 1236 | policyDesc->mPolicyMix = primaryMix; |
| 1237 | *output = policyDesc->mIoHandle; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1238 | *selectedDeviceId = deviceDesc != 0 ? deviceDesc->getId() : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1239 | |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1240 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 1241 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 1242 | *outputType = API_OUT_MIX_PLAYBACK; |
| 1243 | } else { |
| 1244 | *outputType = API_OUTPUT_LEGACY; |
| 1245 | } |
| 1246 | return NO_ERROR; |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1247 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1248 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1249 | // Virtual sources must always be dynamicaly or explicitly routed |
| 1250 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 1251 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 1252 | return BAD_VALUE; |
| 1253 | } |
| 1254 | // explicit routing managed by getDeviceForStrategy in APM is now handled by engine |
| 1255 | // in order to let the choice of the order to future vendor engine |
| 1256 | outputDevices = mEngine->getOutputDevicesForAttributes(*resultAttr, requestedDevice, false); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 1257 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1258 | if ((resultAttr->flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1259 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1262 | // Set incall music only if device was explicitly set, and fallback to the device which is |
| 1263 | // chosen by the engine if not. |
| 1264 | // FIXME: provide a more generic approach which is not device specific and move this back |
| 1265 | // to getOutputForDevice. |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1266 | // 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] | 1267 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX) && |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1268 | (*stream == AUDIO_STREAM_MUSIC || resultAttr->usage == AUDIO_USAGE_VOICE_COMMUNICATION) && |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1269 | audio_is_linear_pcm(config->format) && |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 1270 | isCallAudioAccessible()) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1271 | if (requestedPortId != AUDIO_PORT_HANDLE_NONE) { |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1272 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1273 | *isRequestedDeviceForExclusiveUse = true; |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1274 | } |
| 1275 | } |
| 1276 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1277 | ALOGV("%s() device %s, sampling rate %d, format %#x, channel mask %#x, flags %#x stream %s", |
| 1278 | __func__, outputDevices.toString().c_str(), config->sample_rate, config->format, |
| 1279 | config->channel_mask, *flags, toString(*stream).c_str()); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1280 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1281 | *output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1282 | if (!msdDevices.isEmpty()) { |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1283 | *output = getOutputForDevices(msdDevices, session, resultAttr, config, flags, isSpatialized); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1284 | if (*output != AUDIO_IO_HANDLE_NONE && setMsdOutputPatches(&outputDevices) == NO_ERROR) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1285 | ALOGV("%s() Using MSD devices %s instead of devices %s", |
| 1286 | __func__, msdDevices.toString().c_str(), outputDevices.toString().c_str()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1287 | } else { |
| 1288 | *output = AUDIO_IO_HANDLE_NONE; |
| 1289 | } |
| 1290 | } |
| 1291 | if (*output == AUDIO_IO_HANDLE_NONE) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1292 | sp<PreferredMixerAttributesInfo> info = nullptr; |
| 1293 | if (outputDevices.size() == 1) { |
| 1294 | info = getPreferredMixerAttributesInfo( |
| 1295 | outputDevices.itemAt(0)->getId(), |
jiabin | d9a58d3 | 2023-06-01 17:57:30 +0000 | [diff] [blame] | 1296 | mEngine->getProductStrategyForAttributes(*resultAttr), |
| 1297 | true /*activeBitPerfectPreferred*/); |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 1298 | // Only use preferred mixer if the uid matches or the preferred mixer is bit-perfect |
| 1299 | // and it is currently active. |
| 1300 | if (info != nullptr && info->getUid() != uid && |
| 1301 | ((info->getFlags() & AUDIO_OUTPUT_FLAG_BIT_PERFECT) == AUDIO_OUTPUT_FLAG_NONE || |
| 1302 | info->getActiveClientCount() == 0)) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1303 | info = nullptr; |
| 1304 | } |
| 1305 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1306 | *output = getOutputForDevices(outputDevices, session, resultAttr, config, |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1307 | flags, isSpatialized, info, resultAttr->flags & AUDIO_FLAG_MUTE_HAPTIC); |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 1308 | // The client will be active if the client is currently preferred mixer owner and the |
| 1309 | // requested configuration matches the preferred mixer configuration. |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1310 | *isBitPerfect = (info != nullptr |
| 1311 | && (info->getFlags() & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != AUDIO_OUTPUT_FLAG_NONE |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 1312 | && info->getUid() == uid |
| 1313 | && *output != AUDIO_IO_HANDLE_NONE |
| 1314 | // When bit-perfect output is selected for the preferred mixer attributes owner, |
| 1315 | // only need to consider the config matches. |
| 1316 | && mOutputs.valueFor(*output)->isConfigurationMatched( |
| 1317 | clientConfig, AUDIO_OUTPUT_FLAG_NONE)); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1318 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1319 | if (*output == AUDIO_IO_HANDLE_NONE) { |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1320 | AudioProfileVector profiles; |
| 1321 | status_t ret = getProfilesForDevices(outputDevices, profiles, *flags, false /*isInput*/); |
| 1322 | if (ret == NO_ERROR && !profiles.empty()) { |
| 1323 | config->channel_mask = profiles[0]->getChannels().empty() ? config->channel_mask |
| 1324 | : *profiles[0]->getChannels().begin(); |
| 1325 | config->sample_rate = profiles[0]->getSampleRates().empty() ? config->sample_rate |
| 1326 | : *profiles[0]->getSampleRates().begin(); |
| 1327 | config->format = profiles[0]->getFormat(); |
| 1328 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1329 | return INVALID_OPERATION; |
| 1330 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1331 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1332 | *selectedDeviceId = getFirstDeviceId(outputDevices); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1333 | for (auto &outputDevice : outputDevices) { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 1334 | if (outputDevice->getId() == mConfig->getDefaultOutputDevice()->getId()) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1335 | *selectedDeviceId = outputDevice->getId(); |
| 1336 | break; |
| 1337 | } |
| 1338 | } |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1339 | |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1340 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX)) { |
| 1341 | *outputType = API_OUTPUT_TELEPHONY_TX; |
| 1342 | } else { |
| 1343 | *outputType = API_OUTPUT_LEGACY; |
| 1344 | } |
| 1345 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1346 | ALOGV("%s returns output %d selectedDeviceId %d", __func__, *output, *selectedDeviceId); |
| 1347 | |
| 1348 | return NO_ERROR; |
| 1349 | } |
| 1350 | |
| 1351 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 1352 | audio_io_handle_t *output, |
| 1353 | audio_session_t session, |
| 1354 | audio_stream_type_t *stream, |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1355 | const AttributionSourceState& attributionSource, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1356 | audio_config_t *config, |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1357 | audio_output_flags_t *flags, |
| 1358 | audio_port_handle_t *selectedDeviceId, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1359 | audio_port_handle_t *portId, |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1360 | std::vector<audio_io_handle_t> *secondaryOutputs, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1361 | output_type_t *outputType, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1362 | bool *isSpatialized, |
| 1363 | bool *isBitPerfect) |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1364 | { |
| 1365 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 1366 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 1367 | return INVALID_OPERATION; |
| 1368 | } |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1369 | const uid_t uid = VALUE_OR_RETURN_STATUS( |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1370 | aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1371 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1372 | audio_attributes_t resultAttr; |
François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1373 | bool isRequestedDeviceForExclusiveUse = false; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1374 | std::vector<sp<AudioPolicyMix>> secondaryMixes; |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1375 | const sp<DeviceDescriptor> requestedDevice = |
| 1376 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 1377 | |
| 1378 | // Prevent from storing invalid requested device id in clients |
| 1379 | const audio_port_handle_t sanitizedRequestedPortId = |
| 1380 | requestedDevice != nullptr ? requestedPortId : AUDIO_PORT_HANDLE_NONE; |
| 1381 | *selectedDeviceId = sanitizedRequestedPortId; |
| 1382 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1383 | status_t status = getOutputForAttrInt(&resultAttr, output, session, attr, stream, uid, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1384 | config, flags, selectedDeviceId, &isRequestedDeviceForExclusiveUse, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1385 | secondaryOutputs != nullptr ? &secondaryMixes : nullptr, outputType, isSpatialized, |
| 1386 | isBitPerfect); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1387 | if (status != NO_ERROR) { |
| 1388 | return status; |
| 1389 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1390 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryOutputDescs; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1391 | if (secondaryOutputs != nullptr) { |
| 1392 | for (auto &secondaryMix : secondaryMixes) { |
| 1393 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); |
| 1394 | if (outputDesc != nullptr && |
| 1395 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { |
| 1396 | secondaryOutputs->push_back(outputDesc->mIoHandle); |
| 1397 | weakSecondaryOutputDescs.push_back(outputDesc); |
| 1398 | } |
| 1399 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1400 | } |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1401 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1402 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1403 | .channel_mask = config->channel_mask, |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1404 | .format = config->format, |
Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1405 | }; |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 1406 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1407 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1408 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1409 | sp<TrackClientDescriptor> clientDesc = |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1410 | new TrackClientDescriptor(*portId, uid, session, resultAttr, clientConfig, |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1411 | sanitizedRequestedPortId, *stream, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1412 | mEngine->getProductStrategyForAttributes(resultAttr), |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 1413 | toVolumeSource(resultAttr), |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1414 | *flags, isRequestedDeviceForExclusiveUse, |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1415 | std::move(weakSecondaryOutputDescs), |
| 1416 | outputDesc->mPolicyMix); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1417 | outputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1418 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1419 | ALOGV("%s() returns output %d requestedPortId %d selectedDeviceId %d for port ID %d", __func__, |
| 1420 | *output, requestedPortId, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1421 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1422 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1425 | status_t AudioPolicyManager::openDirectOutput(audio_stream_type_t stream, |
| 1426 | audio_session_t session, |
| 1427 | const audio_config_t *config, |
| 1428 | audio_output_flags_t flags, |
| 1429 | const DeviceVector &devices, |
| 1430 | audio_io_handle_t *output) { |
| 1431 | |
| 1432 | *output = AUDIO_IO_HANDLE_NONE; |
| 1433 | |
| 1434 | // skip direct output selection if the request can obviously be attached to a mixed output |
| 1435 | // and not explicitly requested |
| 1436 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 1437 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 1438 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
| 1439 | return NAME_NOT_FOUND; |
| 1440 | } |
| 1441 | |
| 1442 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 1443 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 1444 | // when audioflinger detects there is an active non offloadable effect. |
| 1445 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 1446 | // This may prevent offloading in rare situations where effects are left active by apps |
| 1447 | // in the background. |
| 1448 | sp<IOProfile> profile; |
| 1449 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
| 1450 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
| 1451 | profile = getProfileForOutput( |
| 1452 | devices, config->sample_rate, config->format, config->channel_mask, |
| 1453 | flags, true /* directOnly */); |
| 1454 | } |
| 1455 | |
| 1456 | if (profile == nullptr) { |
| 1457 | return NAME_NOT_FOUND; |
| 1458 | } |
| 1459 | |
| 1460 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 1461 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1462 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 1463 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 1464 | // reuse direct output if currently open by the same client |
| 1465 | // and configured with same parameters |
| 1466 | if ((config->sample_rate == desc->getSamplingRate()) && |
| 1467 | (config->format == desc->getFormat()) && |
| 1468 | (config->channel_mask == desc->getChannelMask()) && |
| 1469 | (session == desc->mDirectClientSession)) { |
| 1470 | desc->mDirectOpenCount++; |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 1471 | ALOGV("%s reusing direct output %d for session %d", __func__, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1472 | mOutputs.keyAt(i), session); |
| 1473 | *output = mOutputs.keyAt(i); |
| 1474 | return NO_ERROR; |
| 1475 | } |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | if (!profile->canOpenNewIo()) { |
| 1480 | return NAME_NOT_FOUND; |
| 1481 | } |
| 1482 | |
| 1483 | sp<SwAudioOutputDescriptor> outputDesc = |
| 1484 | new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 1485 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1486 | // An MSD patch may be using the only output stream that can service this request. Release |
| 1487 | // 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] | 1488 | releaseMsdOutputPatches(devices); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1489 | |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 1490 | status_t status = |
| 1491 | outputDesc->open(config, nullptr /* mixerConfig */, devices, stream, flags, output); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1492 | |
| 1493 | // only accept an output with the requested parameters |
| 1494 | if (status != NO_ERROR || |
| 1495 | (config->sample_rate != 0 && config->sample_rate != outputDesc->getSamplingRate()) || |
| 1496 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->getFormat()) || |
| 1497 | (config->channel_mask != 0 && config->channel_mask != outputDesc->getChannelMask())) { |
| 1498 | ALOGV("%s failed opening direct output: output %d sample rate %d %d," |
| 1499 | "format %d %d, channel mask %04x %04x", __func__, *output, config->sample_rate, |
| 1500 | outputDesc->getSamplingRate(), config->format, outputDesc->getFormat(), |
| 1501 | config->channel_mask, outputDesc->getChannelMask()); |
| 1502 | if (*output != AUDIO_IO_HANDLE_NONE) { |
| 1503 | outputDesc->close(); |
| 1504 | } |
| 1505 | // fall back to mixer output if possible when the direct output could not be open |
| 1506 | if (audio_is_linear_pcm(config->format) && |
| 1507 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
| 1508 | return NAME_NOT_FOUND; |
| 1509 | } |
| 1510 | *output = AUDIO_IO_HANDLE_NONE; |
| 1511 | return BAD_VALUE; |
| 1512 | } |
| 1513 | outputDesc->mDirectOpenCount = 1; |
| 1514 | outputDesc->mDirectClientSession = session; |
| 1515 | |
| 1516 | addOutput(*output, outputDesc); |
| 1517 | mPreviousOutputs = mOutputs; |
| 1518 | ALOGV("%s returns new direct output %d", __func__, *output); |
| 1519 | mpClientInterface->onAudioPortListUpdate(); |
| 1520 | return NO_ERROR; |
| 1521 | } |
| 1522 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1523 | audio_io_handle_t AudioPolicyManager::getOutputForDevices( |
| 1524 | const DeviceVector &devices, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1525 | audio_session_t session, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1526 | const audio_attributes_t *attr, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1527 | const audio_config_t *config, |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1528 | audio_output_flags_t *flags, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1529 | bool *isSpatialized, |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1530 | sp<PreferredMixerAttributesInfo> prefMixerConfigInfo, |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1531 | bool forceMutingHaptic) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1532 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1533 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1534 | |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1535 | // Discard haptic channel mask when forcing muting haptic channels. |
| 1536 | audio_channel_mask_t channelMask = forceMutingHaptic |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1537 | ? static_cast<audio_channel_mask_t>(config->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL) |
| 1538 | : config->channel_mask; |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1539 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1540 | // open a direct output if required by specified parameters |
| 1541 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 1542 | // and all common behaviors are driven by checking only the direct flag |
| 1543 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1544 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1545 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1546 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1547 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 1548 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1549 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1550 | |
| 1551 | audio_stream_type_t stream = mEngine->getStreamTypeForAttributes(*attr); |
| 1552 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1553 | // only allow deep buffering for music stream type |
| 1554 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1555 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1556 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1557 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1558 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 1559 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1560 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1561 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1562 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1563 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1564 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1565 | audio_is_linear_pcm(config->format) && |
| 1566 | (*flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) == 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1567 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1568 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1569 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1570 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1571 | |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 1572 | // Attach the Ultrasound flag for the AUDIO_CONTENT_TYPE_ULTRASOUND |
| 1573 | if (attr->content_type == AUDIO_CONTENT_TYPE_ULTRASOUND) { |
| 1574 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_ULTRASOUND); |
| 1575 | } |
| 1576 | |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1577 | *isSpatialized = false; |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1578 | if (mSpatializerOutput != nullptr |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 1579 | && canBeSpatializedInt(attr, config, devices.toTypeAddrVector())) { |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1580 | *isSpatialized = true; |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1581 | return mSpatializerOutput->mIoHandle; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1582 | } |
| 1583 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1584 | audio_config_t directConfig = *config; |
| 1585 | directConfig.channel_mask = channelMask; |
| 1586 | status_t status = openDirectOutput(stream, session, &directConfig, *flags, devices, &output); |
| 1587 | if (status != NAME_NOT_FOUND) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1588 | return output; |
| 1589 | } |
| 1590 | |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1591 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1592 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1593 | 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] | 1594 | return AUDIO_IO_HANDLE_NONE; |
| 1595 | } |
Pierre Couillaud | e73496b | 2023-03-06 16:19:05 +0000 | [diff] [blame] | 1596 | // A request for Tuner cannot fallback to a mixed output |
| 1597 | if ((directConfig.offload_info.content_id || directConfig.offload_info.sync_id)) { |
| 1598 | return AUDIO_IO_HANDLE_NONE; |
| 1599 | } |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1600 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1601 | // ignoring channel mask due to downmix capability in mixer |
| 1602 | |
| 1603 | // open a non direct output |
| 1604 | |
| 1605 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1606 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1607 | // get which output is suitable for the specified stream. The actual |
| 1608 | // routing change will happen when startOutput() will be called |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1609 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1610 | if (prefMixerConfigInfo != nullptr) { |
| 1611 | for (audio_io_handle_t outputHandle : outputs) { |
| 1612 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputHandle); |
| 1613 | if (outputDesc->mProfile == prefMixerConfigInfo->getProfile()) { |
| 1614 | output = outputHandle; |
| 1615 | break; |
| 1616 | } |
| 1617 | } |
| 1618 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 1619 | // No output open with the preferred profile. Open a new one. |
| 1620 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 1621 | config.channel_mask = prefMixerConfigInfo->getConfigBase().channel_mask; |
| 1622 | config.sample_rate = prefMixerConfigInfo->getConfigBase().sample_rate; |
| 1623 | config.format = prefMixerConfigInfo->getConfigBase().format; |
| 1624 | sp<SwAudioOutputDescriptor> preferredOutput = openOutputWithProfileAndDevice( |
| 1625 | prefMixerConfigInfo->getProfile(), devices, nullptr /*mixerConfig*/, |
| 1626 | &config, prefMixerConfigInfo->getFlags()); |
| 1627 | if (preferredOutput == nullptr) { |
| 1628 | ALOGE("%s failed to open output with preferred mixer config", __func__); |
| 1629 | } else { |
| 1630 | output = preferredOutput->mIoHandle; |
| 1631 | } |
| 1632 | } |
| 1633 | } else { |
| 1634 | // at this stage we should ignore the DIRECT flag as no direct output could be |
| 1635 | // found earlier |
| 1636 | *flags = (audio_output_flags_t) (*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1637 | output = selectOutput( |
| 1638 | outputs, *flags, config->format, channelMask, config->sample_rate, session); |
| 1639 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1640 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1641 | ALOGW_IF((output == 0), "getOutputForDevices() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1642 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1643 | stream, config->sample_rate, config->format, channelMask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1644 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1645 | return output; |
| 1646 | } |
| 1647 | |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame] | 1648 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1649 | auto msdInDevices = mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1650 | mAvailableInputDevices); |
| 1651 | return msdInDevices.isEmpty()? nullptr : msdInDevices.itemAt(0); |
| 1652 | } |
| 1653 | |
| 1654 | DeviceVector AudioPolicyManager::getMsdAudioOutDevices() const { |
| 1655 | return mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1656 | mAvailableOutputDevices); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1657 | } |
| 1658 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1659 | const AudioPatchCollection AudioPolicyManager::getMsdOutputPatches() const { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1660 | AudioPatchCollection msdPatches; |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1661 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1662 | if (msdModule != 0) { |
| 1663 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { |
| 1664 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); |
| 1665 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { |
| 1666 | const struct audio_port_config *source = &patch->mPatch.sources[j]; |
| 1667 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 1668 | source->ext.device.hw_module == msdModule->getHandle()) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1669 | msdPatches.addAudioPatch(patch->getHandle(), patch); |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1670 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1671 | } |
| 1672 | } |
| 1673 | } |
| 1674 | return msdPatches; |
| 1675 | } |
| 1676 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 1677 | bool AudioPolicyManager::isMsdPatch(const audio_patch_handle_t &handle) const { |
| 1678 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 1679 | if (index < 0) { |
| 1680 | return false; |
| 1681 | } |
| 1682 | const sp<AudioPatch> patch = mAudioPatches.valueAt(index); |
| 1683 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1684 | if (msdModule == nullptr) { |
| 1685 | return false; |
| 1686 | } |
| 1687 | const struct audio_port_config *sink = &patch->mPatch.sinks[0]; |
| 1688 | if (getMsdAudioOutDevices().contains(mAvailableOutputDevices.getDeviceFromId(sink->id))) { |
| 1689 | return true; |
| 1690 | } |
| 1691 | index = getMsdOutputPatches().indexOfKey(handle); |
| 1692 | if (index < 0) { |
| 1693 | return false; |
| 1694 | } |
| 1695 | return true; |
| 1696 | } |
| 1697 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1698 | status_t AudioPolicyManager::getMsdProfiles(bool hwAvSync, |
| 1699 | const InputProfileCollection &inputProfiles, |
| 1700 | const OutputProfileCollection &outputProfiles, |
| 1701 | const sp<DeviceDescriptor> &sourceDevice, |
| 1702 | const sp<DeviceDescriptor> &sinkDevice, |
| 1703 | AudioProfileVector& sourceProfiles, |
| 1704 | AudioProfileVector& sinkProfiles) const { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1705 | if (inputProfiles.isEmpty()) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1706 | ALOGE("%s() no input profiles for source module", __func__); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1707 | return NO_INIT; |
| 1708 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1709 | if (outputProfiles.isEmpty()) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1710 | ALOGE("%s() no output profiles for sink module", __func__); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1711 | return NO_INIT; |
| 1712 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1713 | for (const auto &inProfile : inputProfiles) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1714 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0) && |
| 1715 | inProfile->supportsDevice(sourceDevice)) { |
| 1716 | appendAudioProfiles(sourceProfiles, inProfile->getAudioProfiles()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1717 | } |
| 1718 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1719 | for (const auto &outProfile : outputProfiles) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1720 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) && |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1721 | outProfile->supportsDevice(sinkDevice)) { |
| 1722 | appendAudioProfiles(sinkProfiles, outProfile->getAudioProfiles()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1723 | } |
| 1724 | } |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1725 | return NO_ERROR; |
| 1726 | } |
| 1727 | |
| 1728 | status_t AudioPolicyManager::getBestMsdConfig(bool hwAvSync, |
| 1729 | const AudioProfileVector &sourceProfiles, const AudioProfileVector &sinkProfiles, |
| 1730 | audio_port_config *sourceConfig, audio_port_config *sinkConfig) const |
| 1731 | { |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 1732 | // Compressed formats for MSD module, ordered from most preferred to least preferred. |
| 1733 | static const std::vector<audio_format_t> formatsOrder = {{ |
| 1734 | AUDIO_FORMAT_IEC60958, AUDIO_FORMAT_MAT_2_1, AUDIO_FORMAT_MAT_2_0, AUDIO_FORMAT_E_AC3, |
| 1735 | AUDIO_FORMAT_AC3, AUDIO_FORMAT_PCM_16_BIT }}; |
| 1736 | static const std::vector<audio_channel_mask_t> channelMasksOrder = [](){ |
| 1737 | // Channel position masks for MSD module, 3D > 2D > 1D ordering (most preferred to least |
| 1738 | // preferred). |
| 1739 | std::vector<audio_channel_mask_t> masks = {{ |
| 1740 | AUDIO_CHANNEL_OUT_3POINT1POINT2, AUDIO_CHANNEL_OUT_3POINT0POINT2, |
| 1741 | AUDIO_CHANNEL_OUT_2POINT1POINT2, AUDIO_CHANNEL_OUT_2POINT0POINT2, |
| 1742 | AUDIO_CHANNEL_OUT_5POINT1, AUDIO_CHANNEL_OUT_STEREO }}; |
| 1743 | // insert index masks (higher counts most preferred) as preferred over position masks |
| 1744 | for (int i = 1; i <= AUDIO_CHANNEL_COUNT_MAX; i++) { |
| 1745 | masks.insert( |
| 1746 | masks.begin(), audio_channel_mask_for_index_assignment_from_count(i)); |
| 1747 | } |
| 1748 | return masks; |
| 1749 | }(); |
| 1750 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1751 | struct audio_config_base bestSinkConfig; |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 1752 | status_t result = findBestMatchingOutputConfig(sourceProfiles, sinkProfiles, formatsOrder, |
| 1753 | channelMasksOrder, true /*preferHigherSamplingRates*/, bestSinkConfig); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1754 | if (result != NO_ERROR) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1755 | ALOGD("%s() no matching config found for sink, hwAvSync: %d", |
| 1756 | __func__, hwAvSync); |
Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1757 | return result; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1758 | } |
| 1759 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1760 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; |
| 1761 | sinkConfig->format = bestSinkConfig.format; |
| 1762 | // For encoded streams force direct flag to prevent downstream mixing. |
| 1763 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1764 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); |
Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1765 | if (audio_is_iec61937_compatible(sinkConfig->format)) { |
| 1766 | // For formats compatible with IEC61937 encapsulation, assume that |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1767 | // the input is IEC61937 framed (for proportional buffer sizing). |
Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1768 | // Add the AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO flag so downstream HAL can distinguish between |
| 1769 | // raw and IEC61937 framed streams. |
| 1770 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1771 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO); |
| 1772 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1773 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1774 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 1775 | sourceConfig->channel_mask = |
| 1776 | audio_channel_mask_get_representation(bestSinkConfig.channel_mask) |
| 1777 | == AUDIO_CHANNEL_REPRESENTATION_INDEX ? |
| 1778 | bestSinkConfig.channel_mask : audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1779 | sourceConfig->format = bestSinkConfig.format; |
| 1780 | // Copy input stream directly without any processing (e.g. resampling). |
| 1781 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1782 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); |
| 1783 | if (hwAvSync) { |
| 1784 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1785 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 1786 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1787 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); |
| 1788 | } |
| 1789 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | |
| 1790 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; |
| 1791 | sinkConfig->config_mask |= config_mask; |
| 1792 | sourceConfig->config_mask |= config_mask; |
| 1793 | return NO_ERROR; |
| 1794 | } |
| 1795 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1796 | PatchBuilder AudioPolicyManager::buildMsdPatch(bool msdIsSource, |
| 1797 | const sp<DeviceDescriptor> &device) const |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1798 | { |
| 1799 | PatchBuilder patchBuilder; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1800 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1801 | ALOG_ASSERT(msdModule != nullptr, "MSD module not available"); |
| 1802 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(device, AUDIO_FORMAT_DEFAULT); |
| 1803 | if (deviceModule == nullptr) { |
| 1804 | ALOGE("%s() unable to get module for %s", __func__, device->toString().c_str()); |
| 1805 | return patchBuilder; |
| 1806 | } |
| 1807 | const InputProfileCollection inputProfiles = msdIsSource ? |
| 1808 | msdModule->getInputProfiles() : deviceModule->getInputProfiles(); |
| 1809 | const OutputProfileCollection outputProfiles = msdIsSource ? |
| 1810 | deviceModule->getOutputProfiles() : msdModule->getOutputProfiles(); |
| 1811 | |
| 1812 | const sp<DeviceDescriptor> sourceDevice = msdIsSource ? getMsdAudioInDevice() : device; |
| 1813 | const sp<DeviceDescriptor> sinkDevice = msdIsSource ? |
| 1814 | device : getMsdAudioOutDevices().itemAt(0); |
| 1815 | patchBuilder.addSource(sourceDevice).addSink(sinkDevice); |
| 1816 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1817 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; |
| 1818 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1819 | AudioProfileVector sourceProfiles; |
| 1820 | AudioProfileVector sinkProfiles; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1821 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. |
| 1822 | // For now, we just forcefully try with HwAvSync first. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1823 | for (auto hwAvSync : { true, false }) { |
| 1824 | if (getMsdProfiles(hwAvSync, inputProfiles, outputProfiles, sourceDevice, sinkDevice, |
| 1825 | sourceProfiles, sinkProfiles) != NO_ERROR) { |
| 1826 | continue; |
| 1827 | } |
| 1828 | if (getBestMsdConfig(hwAvSync, sourceProfiles, sinkProfiles, &sourceConfig, |
| 1829 | &sinkConfig) == NO_ERROR) { |
| 1830 | // Found a matching config. Re-create PatchBuilder with this config. |
| 1831 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); |
| 1832 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1833 | } |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1834 | ALOGV("%s() no matching config found. Fall through to default PCM patch" |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1835 | " supporting PCM format conversion.", __func__); |
| 1836 | return patchBuilder; |
| 1837 | } |
| 1838 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1839 | status_t AudioPolicyManager::setMsdOutputPatches(const DeviceVector *outputDevices) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1840 | DeviceVector devices; |
| 1841 | if (outputDevices != nullptr && outputDevices->size() > 0) { |
| 1842 | devices.add(*outputDevices); |
| 1843 | } else { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1844 | // Use media strategy for unspecified output device. This should only |
| 1845 | // occur on checkForDeviceAndOutputChanges(). Device connection events may |
| 1846 | // therefore invalidate explicit routing requests. |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1847 | devices = mEngine->getOutputDevicesForAttributes( |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1848 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1849 | 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] | 1850 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1851 | std::vector<PatchBuilder> patchesToCreate; |
| 1852 | for (auto i = 0u; i < devices.size(); ++i) { |
| 1853 | ALOGV("%s() for device %s", __func__, devices[i]->toString().c_str()); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1854 | patchesToCreate.push_back(buildMsdPatch(true /*msdIsSource*/, devices[i])); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1855 | } |
| 1856 | // Retain only the MSD patches associated with outputDevices request. |
| 1857 | // Tear down the others, and create new ones as needed. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1858 | AudioPatchCollection patchesToRemove = getMsdOutputPatches(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1859 | for (auto it = patchesToCreate.begin(); it != patchesToCreate.end(); ) { |
| 1860 | auto retainedPatch = false; |
| 1861 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { |
| 1862 | if (audio_patches_are_equal(it->patch(), &patchesToRemove[i]->mPatch)) { |
| 1863 | patchesToRemove.removeItemsAt(i); |
| 1864 | retainedPatch = true; |
| 1865 | break; |
| 1866 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1867 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1868 | if (retainedPatch) { |
| 1869 | it = patchesToCreate.erase(it); |
| 1870 | continue; |
| 1871 | } |
| 1872 | ++it; |
| 1873 | } |
| 1874 | if (patchesToCreate.size() == 0 && patchesToRemove.size() == 0) { |
| 1875 | return NO_ERROR; |
| 1876 | } |
| 1877 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { |
| 1878 | auto ¤tPatch = patchesToRemove.valueAt(i); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1879 | releaseAudioPatch(currentPatch->getHandle(), mUidCached); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1880 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1881 | status_t status = NO_ERROR; |
| 1882 | for (const auto &p : patchesToCreate) { |
| 1883 | auto currStatus = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, |
| 1884 | p.patch(), 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); |
| 1885 | char message[256]; |
| 1886 | snprintf(message, sizeof(message), "%s() %s: creating MSD patch from device:IN_BUS to " |
| 1887 | "device:%#x (format:%#x channels:%#x samplerate:%d)", __func__, |
| 1888 | currStatus == NO_ERROR ? "Success" : "Error", |
| 1889 | p.patch()->sinks[0].ext.device.type, p.patch()->sources[0].format, |
| 1890 | p.patch()->sources[0].channel_mask, p.patch()->sources[0].sample_rate); |
| 1891 | if (currStatus == NO_ERROR) { |
| 1892 | ALOGD("%s", message); |
| 1893 | } else { |
| 1894 | ALOGE("%s", message); |
| 1895 | if (status == NO_ERROR) { |
| 1896 | status = currStatus; |
| 1897 | } |
| 1898 | } |
| 1899 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1900 | return status; |
| 1901 | } |
| 1902 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1903 | void AudioPolicyManager::releaseMsdOutputPatches(const DeviceVector& devices) { |
| 1904 | AudioPatchCollection msdPatches = getMsdOutputPatches(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1905 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1906 | const auto& patch = msdPatches[i]; |
| 1907 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1908 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1909 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && devices.getDevice(sink->ext.device.type, |
| 1910 | String8(sink->ext.device.address), AUDIO_FORMAT_DEFAULT) != nullptr) { |
| 1911 | releaseAudioPatch(patch->getHandle(), mUidCached); |
| 1912 | break; |
| 1913 | } |
| 1914 | } |
| 1915 | } |
| 1916 | } |
| 1917 | |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 1918 | bool AudioPolicyManager::msdHasPatchesToAllDevices(const AudioDeviceTypeAddrVector& devices) { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 1919 | DeviceVector devicesToCheck = |
| 1920 | mConfig->getOutputDevices().getDevicesFromDeviceTypeAddrVec(devices); |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 1921 | AudioPatchCollection msdPatches = getMsdOutputPatches(); |
| 1922 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1923 | const auto& patch = msdPatches[i]; |
| 1924 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1925 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1926 | if (sink->type == AUDIO_PORT_TYPE_DEVICE) { |
| 1927 | const auto& foundDevice = devicesToCheck.getDevice( |
| 1928 | sink->ext.device.type, String8(sink->ext.device.address), AUDIO_FORMAT_DEFAULT); |
| 1929 | if (foundDevice != nullptr) { |
| 1930 | devicesToCheck.remove(foundDevice); |
| 1931 | if (devicesToCheck.isEmpty()) { |
| 1932 | return true; |
| 1933 | } |
| 1934 | } |
| 1935 | } |
| 1936 | } |
| 1937 | } |
| 1938 | return false; |
| 1939 | } |
| 1940 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1941 | audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs, |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1942 | audio_output_flags_t flags, |
| 1943 | audio_format_t format, |
| 1944 | audio_channel_mask_t channelMask, |
| 1945 | uint32_t samplingRate, |
| 1946 | audio_session_t sessionId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1947 | { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1948 | LOG_ALWAYS_FATAL_IF(!(format == AUDIO_FORMAT_INVALID || audio_is_linear_pcm(format)), |
| 1949 | "%s called with format %#x", __func__, format); |
| 1950 | |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1951 | // Return the output that haptic-generating attached to when 1) session id is specified, |
| 1952 | // 2) haptic-generating effect exists for given session id and 3) the output that |
| 1953 | // haptic-generating effect attached to is in given outputs. |
| 1954 | if (sessionId != AUDIO_SESSION_NONE) { |
| 1955 | audio_io_handle_t hapticGeneratingOutput = mEffects.getIoForSession( |
| 1956 | sessionId, FX_IID_HAPTICGENERATOR); |
| 1957 | if (outputs.indexOf(hapticGeneratingOutput) >= 0) { |
| 1958 | return hapticGeneratingOutput; |
| 1959 | } |
| 1960 | } |
| 1961 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1962 | // Flags disqualifying an output: the match must happen before calling selectOutput() |
| 1963 | static const audio_output_flags_t kExcludedFlags = (audio_output_flags_t) |
| 1964 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1965 | |
| 1966 | // Flags expressing a functional request: must be honored in priority over |
| 1967 | // other criteria |
| 1968 | static const audio_output_flags_t kFunctionalFlags = (audio_output_flags_t) |
| 1969 | (AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_INCALL_MUSIC | |
Eric Laurent | e28c66d | 2022-01-21 13:40:41 +0100 | [diff] [blame] | 1970 | AUDIO_OUTPUT_FLAG_TTS | AUDIO_OUTPUT_FLAG_DIRECT_PCM | AUDIO_OUTPUT_FLAG_ULTRASOUND | |
| 1971 | AUDIO_OUTPUT_FLAG_SPATIALIZER); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1972 | // Flags expressing a performance request: have lower priority than serving |
| 1973 | // requested sampling rate or channel mask |
| 1974 | static const audio_output_flags_t kPerformanceFlags = (audio_output_flags_t) |
| 1975 | (AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_DEEP_BUFFER | |
| 1976 | AUDIO_OUTPUT_FLAG_RAW | AUDIO_OUTPUT_FLAG_SYNC); |
| 1977 | |
| 1978 | const audio_output_flags_t functionalFlags = |
| 1979 | (audio_output_flags_t)(flags & kFunctionalFlags); |
| 1980 | const audio_output_flags_t performanceFlags = |
| 1981 | (audio_output_flags_t)(flags & kPerformanceFlags); |
| 1982 | |
| 1983 | audio_io_handle_t bestOutput = (outputs.size() == 0) ? AUDIO_IO_HANDLE_NONE : outputs[0]; |
| 1984 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1985 | // 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] | 1986 | // devices (the list was previously build by getOutputsForDevices()). |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1987 | // The priority is as follows: |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1988 | // 1: the output supporting haptic playback when requesting haptic playback |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1989 | // 2: the output with the highest number of requested functional flags |
Carter Hsu | 199f189 | 2021-10-15 15:47:29 +0800 | [diff] [blame] | 1990 | // with tiebreak preferring the minimum number of extra functional flags |
| 1991 | // (see b/200293124, the incorrect selection of AUDIO_OUTPUT_FLAG_VOIP_RX). |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1992 | // 3: the output supporting the exact channel mask |
| 1993 | // 4: the output with a higher channel count than requested |
jiabin | b12a6da | 2022-06-03 20:48:18 +0000 | [diff] [blame] | 1994 | // 5: the output with the highest sampling rate if the requested sample rate is |
| 1995 | // greater than default sampling rate |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1996 | // 6: the output with the highest number of requested performance flags |
| 1997 | // 7: the output with the bit depth the closest to the requested one |
| 1998 | // 8: the primary output |
| 1999 | // 9: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2000 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2001 | // matching criteria values in priority order for best matching output so far |
| 2002 | std::vector<uint32_t> bestMatchCriteria(8, 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2003 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2004 | const uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); |
| 2005 | const uint32_t hapticChannelCount = audio_channel_count_from_out_mask( |
| 2006 | channelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
Eric Laurent | e78b676 | 2018-12-19 16:29:01 -0800 | [diff] [blame] | 2007 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2008 | for (audio_io_handle_t output : outputs) { |
| 2009 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2010 | // matching criteria values in priority order for current output |
| 2011 | std::vector<uint32_t> currentMatchCriteria(8, 0); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 2012 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2013 | if (outputDesc->isDuplicated()) { |
| 2014 | continue; |
| 2015 | } |
| 2016 | if ((kExcludedFlags & outputDesc->mFlags) != 0) { |
| 2017 | continue; |
| 2018 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2019 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2020 | // If haptic channel is specified, use the haptic output if present. |
| 2021 | // When using haptic output, same audio format and sample rate are required. |
| 2022 | const uint32_t outputHapticChannelCount = audio_channel_count_from_out_mask( |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2023 | outputDesc->getChannelMask() & AUDIO_CHANNEL_HAPTIC_ALL); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2024 | if ((hapticChannelCount == 0) != (outputHapticChannelCount == 0)) { |
| 2025 | continue; |
| 2026 | } |
| 2027 | if (outputHapticChannelCount >= hapticChannelCount |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2028 | && format == outputDesc->getFormat() |
| 2029 | && samplingRate == outputDesc->getSamplingRate()) { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2030 | currentMatchCriteria[0] = outputHapticChannelCount; |
| 2031 | } |
| 2032 | |
| 2033 | // functional flags match |
Carter Hsu | 199f189 | 2021-10-15 15:47:29 +0800 | [diff] [blame] | 2034 | const int matchingFunctionalFlags = |
| 2035 | __builtin_popcount(outputDesc->mFlags & functionalFlags); |
| 2036 | const int totalFunctionalFlags = |
| 2037 | __builtin_popcount(outputDesc->mFlags & kFunctionalFlags); |
| 2038 | // Prefer matching functional flags, but subtract unnecessary functional flags. |
| 2039 | currentMatchCriteria[1] = 100 * (matchingFunctionalFlags + 1) - totalFunctionalFlags; |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2040 | |
| 2041 | // channel mask and channel count match |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2042 | uint32_t outputChannelCount = audio_channel_count_from_out_mask( |
| 2043 | outputDesc->getChannelMask()); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2044 | if (channelMask != AUDIO_CHANNEL_NONE && channelCount > 2 && |
| 2045 | channelCount <= outputChannelCount) { |
| 2046 | if ((audio_channel_mask_get_representation(channelMask) == |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2047 | audio_channel_mask_get_representation(outputDesc->getChannelMask())) && |
| 2048 | ((channelMask & outputDesc->getChannelMask()) == channelMask)) { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2049 | currentMatchCriteria[2] = outputChannelCount; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2050 | } |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2051 | currentMatchCriteria[3] = outputChannelCount; |
| 2052 | } |
| 2053 | |
| 2054 | // sampling rate match |
jiabin | b12a6da | 2022-06-03 20:48:18 +0000 | [diff] [blame] | 2055 | if (samplingRate > SAMPLE_RATE_HZ_DEFAULT) { |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2056 | currentMatchCriteria[4] = outputDesc->getSamplingRate(); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | // performance flags match |
| 2060 | currentMatchCriteria[5] = popcount(outputDesc->mFlags & performanceFlags); |
| 2061 | |
| 2062 | // format match |
| 2063 | if (format != AUDIO_FORMAT_INVALID) { |
| 2064 | currentMatchCriteria[6] = |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 2065 | PolicyAudioPort::kFormatDistanceMax - |
| 2066 | PolicyAudioPort::formatDistance(format, outputDesc->getFormat()); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | // primary output match |
| 2070 | currentMatchCriteria[7] = outputDesc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY; |
| 2071 | |
| 2072 | // compare match criteria by priority then value |
| 2073 | if (std::lexicographical_compare(bestMatchCriteria.begin(), bestMatchCriteria.end(), |
| 2074 | currentMatchCriteria.begin(), currentMatchCriteria.end())) { |
| 2075 | bestMatchCriteria = currentMatchCriteria; |
| 2076 | bestOutput = output; |
| 2077 | |
| 2078 | std::stringstream result; |
| 2079 | std::copy(bestMatchCriteria.begin(), bestMatchCriteria.end(), |
| 2080 | std::ostream_iterator<int>(result, " ")); |
| 2081 | ALOGV("%s new bestOutput %d criteria %s", |
| 2082 | __func__, bestOutput, result.str().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2083 | } |
| 2084 | } |
| 2085 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2086 | return bestOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2087 | } |
| 2088 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2089 | status_t AudioPolicyManager::startOutput(audio_port_handle_t 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("%s portId %d", __FUNCTION__, portId); |
| 2092 | |
| 2093 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2094 | if (outputDesc == 0) { |
| 2095 | ALOGW("startOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2096 | return BAD_VALUE; |
| 2097 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2098 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2099 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2100 | ALOGV("startOutput() output %d, stream %d, session %d", |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2101 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2102 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2103 | status_t status = outputDesc->start(); |
| 2104 | if (status != NO_ERROR) { |
| 2105 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2106 | } |
| 2107 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2108 | uint32_t delayMs; |
| 2109 | status = startSource(outputDesc, client, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2110 | |
| 2111 | if (status != NO_ERROR) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2112 | outputDesc->stop(); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2113 | if (status == DEAD_OBJECT) { |
| 2114 | sp<SwAudioOutputDescriptor> desc = |
| 2115 | reopenOutput(outputDesc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 2116 | if (desc == nullptr) { |
| 2117 | // This is not common, it may indicate something wrong with the HAL. |
| 2118 | ALOGE("%s unable to open output with default config", __func__); |
| 2119 | return status; |
| 2120 | } |
| 2121 | desc->mUsePreferredMixerAttributes = true; |
| 2122 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2123 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2124 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2125 | |
| 2126 | // If the client is the first one active on preferred mixer parameters, reopen the output |
| 2127 | // if the current mixer parameters doesn't match the preferred one. |
| 2128 | if (outputDesc->devices().size() == 1) { |
| 2129 | sp<PreferredMixerAttributesInfo> info = getPreferredMixerAttributesInfo( |
| 2130 | outputDesc->devices()[0]->getId(), client->strategy()); |
| 2131 | if (info != nullptr && info->getUid() == client->uid()) { |
| 2132 | if (info->getActiveClientCount() == 0 && !outputDesc->isConfigurationMatched( |
| 2133 | info->getConfigBase(), info->getFlags())) { |
| 2134 | stopSource(outputDesc, client); |
| 2135 | outputDesc->stop(); |
| 2136 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 2137 | config.channel_mask = info->getConfigBase().channel_mask; |
| 2138 | config.sample_rate = info->getConfigBase().sample_rate; |
| 2139 | config.format = info->getConfigBase().format; |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2140 | sp<SwAudioOutputDescriptor> desc = |
| 2141 | reopenOutput(outputDesc, &config, info->getFlags(), __func__); |
| 2142 | if (desc == nullptr) { |
| 2143 | return BAD_VALUE; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2144 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2145 | desc->mUsePreferredMixerAttributes = true; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2146 | // Intentionally return error to let the client side resending request for |
| 2147 | // creating and starting. |
| 2148 | return DEAD_OBJECT; |
| 2149 | } |
| 2150 | info->increaseActiveClient(); |
| 2151 | } |
| 2152 | } |
| 2153 | |
Jean-Michel Trivi | b1f6e64 | 2023-02-07 20:49:04 +0000 | [diff] [blame] | 2154 | if (client->hasPreferredDevice()) { |
| 2155 | // playback activity with preferred device impacts routing occurred, inform upper layers |
| 2156 | mpClientInterface->onRoutingUpdated(); |
| 2157 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2158 | if (delayMs != 0) { |
| 2159 | usleep(delayMs * 1000); |
| 2160 | } |
| 2161 | |
| 2162 | return status; |
| 2163 | } |
| 2164 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2165 | bool AudioPolicyManager::isLeUnicastActive() const { |
| 2166 | if (isInCall()) { |
| 2167 | return true; |
| 2168 | } |
| 2169 | return isAnyDeviceTypeActive(getAudioDeviceOutLeAudioUnicastSet()); |
| 2170 | } |
| 2171 | |
| 2172 | bool AudioPolicyManager::isAnyDeviceTypeActive(const DeviceTypeSet& deviceTypes) const { |
| 2173 | if (mAvailableOutputDevices.getDevicesFromTypes(deviceTypes).isEmpty()) { |
| 2174 | return false; |
| 2175 | } |
| 2176 | bool active = mOutputs.isAnyDeviceTypeActive(deviceTypes); |
| 2177 | ALOGV("%s active %d", __func__, active); |
| 2178 | return active; |
| 2179 | } |
| 2180 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2181 | status_t AudioPolicyManager::startSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 2182 | const sp<TrackClientDescriptor>& client, |
| 2183 | uint32_t *delayMs) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2184 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2185 | // cannot start playback of STREAM_TTS if any other output is being used |
| 2186 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2187 | |
| 2188 | *delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2189 | audio_stream_type_t stream = client->stream(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2190 | auto clientVolSrc = client->volumeSource(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2191 | auto clientStrategy = client->strategy(); |
| 2192 | auto clientAttr = client->attributes(); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2193 | if (stream == AUDIO_STREAM_TTS) { |
| 2194 | ALOGV("\t found BEACON stream"); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2195 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive( |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 2196 | toVolumeSource(AUDIO_STREAM_TTS, false) /*sourceToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2197 | return INVALID_OPERATION; |
| 2198 | } else { |
| 2199 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 2200 | } |
| 2201 | } else { |
| 2202 | // some playback other than beacon starts |
| 2203 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 2204 | } |
| 2205 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2206 | // 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] | 2207 | // check active before incrementing usage count |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 2208 | bool force = !outputDesc->isActive() && !outputDesc->isRouted(); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2209 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2210 | DeviceVector devices; |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2211 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2212 | const char *address = NULL; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2213 | if (policyMix != nullptr) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2214 | audio_devices_t newDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2215 | address = policyMix->mDeviceAddress.string(); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2216 | 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] | 2217 | newDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2218 | } else { |
| 2219 | newDeviceType = policyMix->mDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2220 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2221 | sp device = mAvailableOutputDevices.getDevice(newDeviceType, String8(address), |
| 2222 | AUDIO_FORMAT_DEFAULT); |
| 2223 | ALOG_ASSERT(device, "%s: no device found t=%u, a=%s", __func__, newDeviceType, address); |
| 2224 | devices.add(device); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2225 | } |
| 2226 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2227 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 2228 | // It covers a common case when there is no materially active audio |
| 2229 | // and muting would result in unnecessary delay and dropped audio. |
| 2230 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 2231 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2232 | bool wasLeUnicastActive = isLeUnicastActive(); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2233 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2234 | // increment usage count for this stream on the requested output: |
| 2235 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 2236 | // 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] | 2237 | outputDesc->setClientActive(client, true); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2238 | |
| 2239 | if (client->hasPreferredDevice(true)) { |
Eric Laurent | 72af801 | 2023-03-15 17:36:22 +0100 | [diff] [blame] | 2240 | if (outputDesc->sameExclusivePreferredDevicesCount() > 0) { |
François Gaffie | f96e543 | 2019-04-09 17:13:56 +0200 | [diff] [blame] | 2241 | // Preferred device may be exclusive, use only if no other active clients on this output |
| 2242 | devices = DeviceVector( |
| 2243 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId())); |
| 2244 | } else { |
| 2245 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
| 2246 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2247 | if (devices != outputDesc->devices()) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2248 | checkStrategyRoute(clientStrategy, outputDesc->mIoHandle); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2249 | } |
| 2250 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2251 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2252 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2253 | selectOutputForMusicEffects(); |
| 2254 | } |
| 2255 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2256 | if (outputDesc->getActivityCount(clientVolSrc) == 1 || !devices.isEmpty()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2257 | // starting an output being rerouted? |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2258 | if (devices.isEmpty()) { |
| 2259 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2260 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2261 | bool shouldWait = |
| 2262 | (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM)) || |
| 2263 | followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_NOTIFICATION)) || |
| 2264 | (beaconMuteLatency > 0)); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2265 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2266 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2267 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2268 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2269 | // An output has a shared device if |
| 2270 | // - managed by the same hw module |
| 2271 | // - supports the currently selected device |
| 2272 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2273 | && (!desc->filterSupportedDevices(devices).isEmpty()); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2274 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2275 | // force a device change if any other output is: |
| 2276 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 2277 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2278 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2279 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2280 | // 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] | 2281 | // change the device currently selected by the other output. |
| 2282 | if (sharedDevice && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2283 | desc->devices() != devices && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2284 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2285 | force = true; |
| 2286 | } |
| 2287 | // 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] | 2288 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 2289 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2290 | const uint32_t latencyMs = desc->latency(); |
| 2291 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 2292 | |
| 2293 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 2294 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2295 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2296 | |
| 2297 | // Require mute check if another output is on a shared device |
| 2298 | // and currently active to have proper drain and avoid pops. |
| 2299 | // Note restoring AudioTracks onto this output needs to invoke |
| 2300 | // a volume ramp if there is no mute. |
| 2301 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2302 | } |
| 2303 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2304 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2305 | if (outputDesc->mUsePreferredMixerAttributes && devices != outputDesc->devices()) { |
| 2306 | // If the output is open with preferred mixer attributes, but the routed device is |
| 2307 | // changed when calling this function, returning DEAD_OBJECT to indicate routing |
| 2308 | // changed. |
| 2309 | return DEAD_OBJECT; |
| 2310 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2311 | const uint32_t muteWaitMs = |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2312 | setOutputDevices(outputDesc, devices, force, 0, nullptr, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2313 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2314 | // apply volume rules for current stream and device if necessary |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2315 | auto &curves = getVolumeCurves(client->attributes()); |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2316 | if (NO_ERROR != checkAndSetVolume(curves, client->volumeSource(), |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2317 | curves.getVolumeIndex(outputDesc->devices().types()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2318 | outputDesc, |
Francois Gaffie | d11442b | 2020-04-27 11:51:09 +0200 | [diff] [blame] | 2319 | outputDesc->devices().types(), 0 /*delay*/, |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2320 | outputDesc->useHwGain() /*force*/)) { |
| 2321 | // request AudioService to reinitialize the volume curves asynchronously |
| 2322 | ALOGE("checkAndSetVolume failed, requesting volume range init"); |
| 2323 | mpClientInterface->onVolumeRangeInitRequest(); |
| 2324 | }; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2325 | |
| 2326 | // update the outputs if starting an output with a stream that can affect notification |
| 2327 | // routing |
| 2328 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2329 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 2330 | // force reevaluating accessibility routing when ringtone or alarm starts |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2331 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM))) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 2332 | invalidateStreams({AUDIO_STREAM_ACCESSIBILITY}); |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 2333 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2334 | |
| 2335 | if (waitMs > muteWaitMs) { |
| 2336 | *delayMs = waitMs - muteWaitMs; |
| 2337 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2338 | |
| 2339 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 2340 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 2341 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 2342 | // change occurs after the MixerThread starts and causes a stream volume |
| 2343 | // glitch. |
| 2344 | // |
| 2345 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2346 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2347 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2348 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2349 | mEngine->getForceUse( |
| 2350 | AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2351 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), true, outputDesc); |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2352 | } |
| 2353 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2354 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 2355 | // of type MIX_TYPE_RECORDERS |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2356 | if (isSingleDeviceType(devices.types(), &audio_is_remote_submix_device) && |
| 2357 | policyMix != NULL && policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2358 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2359 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2360 | address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2361 | "remote-submix", |
| 2362 | AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2363 | } |
| 2364 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2365 | checkLeBroadcastRoutes(wasLeUnicastActive, outputDesc, *delayMs); |
| 2366 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2367 | return NO_ERROR; |
| 2368 | } |
| 2369 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2370 | void AudioPolicyManager::checkLeBroadcastRoutes(bool wasUnicastActive, |
| 2371 | sp<SwAudioOutputDescriptor> ignoredOutput, uint32_t delayMs) { |
| 2372 | bool isUnicastActive = isLeUnicastActive(); |
| 2373 | |
| 2374 | if (wasUnicastActive != isUnicastActive) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2375 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2376 | //reroute all outputs routed to LE broadcast if LE unicast activy changed on any output |
| 2377 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2378 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2379 | if (desc != ignoredOutput && desc->isActive() |
| 2380 | && ((isUnicastActive && |
| 2381 | !desc->devices(). |
| 2382 | getDevicesFromType(AUDIO_DEVICE_OUT_BLE_BROADCAST).isEmpty()) |
| 2383 | || (wasUnicastActive && |
| 2384 | !desc->devices().getDevicesFromTypes( |
| 2385 | getAudioDeviceOutLeAudioUnicastSet()).isEmpty()))) { |
| 2386 | DeviceVector newDevices = getNewOutputDevices(desc, false /*fromCache*/); |
| 2387 | bool force = desc->devices() != newDevices; |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2388 | if (desc->mUsePreferredMixerAttributes && force) { |
| 2389 | // If the device is using preferred mixer attributes, the output need to reopen |
| 2390 | // with default configuration when the new selected devices are different from |
| 2391 | // current routing devices. |
| 2392 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices); |
| 2393 | continue; |
| 2394 | } |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2395 | setOutputDevices(desc, newDevices, force, delayMs); |
| 2396 | // re-apply device specific volume if not done by setOutputDevice() |
| 2397 | if (!force) { |
| 2398 | applyStreamVolumes(desc, newDevices.types(), delayMs); |
| 2399 | } |
| 2400 | } |
| 2401 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2402 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2403 | } |
| 2404 | } |
| 2405 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2406 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2407 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2408 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2409 | |
| 2410 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2411 | if (outputDesc == 0) { |
| 2412 | ALOGW("stopOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2413 | return BAD_VALUE; |
| 2414 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2415 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2416 | |
Jean-Michel Trivi | b1f6e64 | 2023-02-07 20:49:04 +0000 | [diff] [blame] | 2417 | if (client->hasPreferredDevice(true)) { |
| 2418 | // playback activity with preferred device impacts routing occurred, inform upper layers |
| 2419 | mpClientInterface->onRoutingUpdated(); |
| 2420 | } |
| 2421 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2422 | ALOGV("stopOutput() output %d, stream %d, session %d", |
| 2423 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2424 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2425 | status_t status = stopSource(outputDesc, client); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2426 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2427 | if (status == NO_ERROR ) { |
| 2428 | outputDesc->stop(); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2429 | } else { |
| 2430 | return status; |
| 2431 | } |
| 2432 | |
| 2433 | if (outputDesc->devices().size() == 1) { |
| 2434 | sp<PreferredMixerAttributesInfo> info = getPreferredMixerAttributesInfo( |
| 2435 | outputDesc->devices()[0]->getId(), client->strategy()); |
| 2436 | if (info != nullptr && info->getUid() == client->uid()) { |
| 2437 | info->decreaseActiveClient(); |
| 2438 | if (info->getActiveClientCount() == 0) { |
| 2439 | reopenOutput(outputDesc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 2440 | } |
| 2441 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2442 | } |
| 2443 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2444 | } |
| 2445 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2446 | status_t AudioPolicyManager::stopSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 2447 | const sp<TrackClientDescriptor>& client) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2448 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2449 | // always handle stream stop, check which stream type is stopping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2450 | audio_stream_type_t stream = client->stream(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2451 | auto clientVolSrc = client->volumeSource(); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2452 | bool wasLeUnicastActive = isLeUnicastActive(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2453 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2454 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 2455 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2456 | if (outputDesc->getActivityCount(clientVolSrc) > 0) { |
| 2457 | if (outputDesc->getActivityCount(clientVolSrc) == 1) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2458 | // Automatically disable the remote submix input when output is stopped on a |
| 2459 | // re routing mix of type MIX_TYPE_RECORDERS |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2460 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2461 | if (isSingleDeviceType( |
| 2462 | outputDesc->devices().types(), &audio_is_remote_submix_device) && |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2463 | policyMix != nullptr && |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2464 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2465 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2466 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2467 | policyMix->mDeviceAddress, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2468 | "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2469 | } |
| 2470 | } |
| 2471 | bool forceDeviceUpdate = false; |
Eric Laurent | 72af801 | 2023-03-15 17:36:22 +0100 | [diff] [blame] | 2472 | if (client->hasPreferredDevice(true) && |
| 2473 | outputDesc->sameExclusivePreferredDevicesCount() < 2) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2474 | checkStrategyRoute(client->strategy(), AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2475 | forceDeviceUpdate = true; |
| 2476 | } |
| 2477 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2478 | // decrement usage count of this stream on the output |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 2479 | outputDesc->setClientActive(client, false); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 2480 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2481 | // store time at which the stream was stopped - see isStreamActive() |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2482 | if (outputDesc->getActivityCount(clientVolSrc) == 0 || forceDeviceUpdate) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2483 | outputDesc->setStopTime(client, systemTime()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2484 | DeviceVector newDevices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 2485 | |
| 2486 | // If the routing does not change, if an output is routed on a device using HwGain |
| 2487 | // (aka setAudioPortConfig) and there are still active clients following different |
| 2488 | // volume group(s), force reapply volume |
| 2489 | bool requiresVolumeCheck = outputDesc->getActivityCount(clientVolSrc) == 0 && |
| 2490 | outputDesc->useHwGain() && outputDesc->isAnyActive(VOLUME_SOURCE_NONE); |
| 2491 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2492 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 2493 | // the track stop() command is received and at that time the audio track buffer can |
| 2494 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 2495 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 2496 | // audio path (audio DSP, CODEC ...) |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 2497 | setOutputDevices(outputDesc, newDevices, false, outputDesc->latency()*2, |
| 2498 | nullptr, true /*requiresMuteCheck*/, requiresVolumeCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2499 | |
| 2500 | // force restoring the device selection on other active outputs if it differs from the |
| 2501 | // one being selected for this output |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2502 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2503 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2504 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2505 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2506 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2507 | desc->isActive() && |
| 2508 | outputDesc->sharesHwModuleWith(desc) && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2509 | (newDevices != desc->devices())) { |
| 2510 | DeviceVector newDevices2 = getNewOutputDevices(desc, false /*fromCache*/); |
| 2511 | bool force = desc->devices() != newDevices2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 2512 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2513 | if (desc->mUsePreferredMixerAttributes && force) { |
| 2514 | // If the device is using preferred mixer attributes, the output need to |
| 2515 | // reopen with default configuration when the new selected devices are |
| 2516 | // different from current routing devices. |
| 2517 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices2); |
| 2518 | continue; |
| 2519 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2520 | setOutputDevices(desc, newDevices2, force, delayMs); |
| 2521 | |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2522 | // re-apply device specific volume if not done by setOutputDevice() |
| 2523 | if (!force) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2524 | applyStreamVolumes(desc, newDevices2.types(), delayMs); |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2525 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2526 | } |
| 2527 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2528 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2529 | // update the outputs if stopping one with a stream that can affect notification routing |
| 2530 | handleNotificationRoutingForStream(stream); |
| 2531 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2532 | |
| 2533 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 2534 | 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] | 2535 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), false, outputDesc); |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2536 | } |
| 2537 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2538 | if (followsSameRouting(client->attributes(), attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2539 | selectOutputForMusicEffects(); |
| 2540 | } |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2541 | |
| 2542 | checkLeBroadcastRoutes(wasLeUnicastActive, outputDesc, outputDesc->latency()*2); |
| 2543 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2544 | return NO_ERROR; |
| 2545 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2546 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2547 | return INVALID_OPERATION; |
| 2548 | } |
| 2549 | } |
| 2550 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2551 | bool AudioPolicyManager::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2552 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2553 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2554 | |
| 2555 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2556 | if (outputDesc == 0) { |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2557 | // If an output descriptor is closed due to a device routing change, |
| 2558 | // then there are race conditions with releaseOutput from tracks |
| 2559 | // that may be destroyed (with no PlaybackThread) or a PlaybackThread |
| 2560 | // destroyed shortly thereafter. |
| 2561 | // |
| 2562 | // Here we just log a warning, instead of a fatal error. |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2563 | ALOGW("releaseOutput() no output for client %d", portId); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2564 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2565 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2566 | |
| 2567 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2568 | |
Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2569 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
| 2570 | if (outputDesc->isClientActive(client)) { |
| 2571 | ALOGW("releaseOutput() inactivates portId %d in good faith", portId); |
| 2572 | stopOutput(portId); |
| 2573 | } |
| 2574 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2575 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 2576 | if (outputDesc->mDirectOpenCount <= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2577 | ALOGW("releaseOutput() invalid open count %d for output %d", |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2578 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2579 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2580 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2581 | if (--outputDesc->mDirectOpenCount == 0) { |
| 2582 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2583 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2584 | } |
| 2585 | } |
Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2586 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2587 | outputDesc->removeClient(portId); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2588 | if (outputDesc->mPendingReopenToQueryProfiles && outputDesc->getClientCount() == 0) { |
| 2589 | // The output is pending reopened to query dynamic profiles and |
| 2590 | // there is no active clients |
| 2591 | closeOutput(outputDesc->mIoHandle); |
| 2592 | sp<SwAudioOutputDescriptor> newOutputDesc = openOutputWithProfileAndDevice( |
| 2593 | outputDesc->mProfile, mEngine->getActiveMediaDevices(mAvailableOutputDevices)); |
| 2594 | if (newOutputDesc == nullptr) { |
| 2595 | ALOGE("%s failed to open output", __func__); |
| 2596 | } |
| 2597 | return true; |
| 2598 | } |
| 2599 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2600 | } |
| 2601 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2602 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 2603 | audio_io_handle_t *input, |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2604 | audio_unique_id_t riid, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2605 | audio_session_t session, |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2606 | const AttributionSourceState& attributionSource, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2607 | audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2608 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2609 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2610 | input_type_t *inputType, |
| 2611 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2612 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2613 | 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] | 2614 | "flags %#x attributes=%s requested device ID %d", |
| 2615 | __func__, attr->source, config->sample_rate, config->format, config->channel_mask, |
| 2616 | session, flags, toString(*attr).c_str(), *selectedDeviceId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2617 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2618 | status_t status = NO_ERROR; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2619 | audio_attributes_t attributes = *attr; |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2620 | sp<AudioPolicyMix> policyMix; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2621 | sp<DeviceDescriptor> device; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2622 | sp<AudioInputDescriptor> inputDesc; |
| 2623 | sp<RecordClientDescriptor> clientDesc; |
| 2624 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2625 | 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] | 2626 | bool isSoundTrigger; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2627 | |
| 2628 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 2629 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 2630 | return INVALID_OPERATION; |
| 2631 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2632 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2633 | if (attr->source == AUDIO_SOURCE_DEFAULT) { |
| 2634 | attributes.source = AUDIO_SOURCE_MIC; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2635 | } |
| 2636 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2637 | // Explicit routing? |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 2638 | sp<DeviceDescriptor> explicitRoutingDevice = |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2639 | mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2640 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2641 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 2642 | // possible |
| 2643 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 2644 | *input != AUDIO_IO_HANDLE_NONE) { |
| 2645 | ssize_t index = mInputs.indexOfKey(*input); |
| 2646 | if (index < 0) { |
| 2647 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 2648 | status = BAD_VALUE; |
| 2649 | goto error; |
| 2650 | } |
| 2651 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2652 | RecordClientVector clients = inputDesc->getClientsForSession(session); |
| 2653 | if (clients.size() == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2654 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 2655 | status = BAD_VALUE; |
| 2656 | goto error; |
| 2657 | } |
| 2658 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 2659 | // 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] | 2660 | // corresponds to a new client and is only permitted from the same UID. |
| 2661 | // 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] | 2662 | if (clients.size() > 1) { |
| 2663 | for (const auto& client : clients) { |
| 2664 | // The client map is ordered by key values (portId) and portIds are allocated |
| 2665 | // incrementaly. So the first client in this list is the one opened by audio flinger |
| 2666 | // when the mmap stream is created and should be ignored as it does not correspond |
| 2667 | // to an actual client |
| 2668 | if (client == *clients.cbegin()) { |
| 2669 | continue; |
| 2670 | } |
| 2671 | if (uid != client->uid() && !client->isSilenced()) { |
| 2672 | ALOGW("getInputForAttr() bad uid %d for client %d uid %d", |
| 2673 | uid, client->portId(), client->uid()); |
| 2674 | status = INVALID_OPERATION; |
| 2675 | goto error; |
| 2676 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 2677 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2678 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2679 | *inputType = API_INPUT_LEGACY; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2680 | device = inputDesc->getDevice(); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2681 | |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 2682 | ALOGV("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2683 | goto exit; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | *input = AUDIO_IO_HANDLE_NONE; |
| 2687 | *inputType = API_INPUT_INVALID; |
| 2688 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2689 | if (attributes.source == AUDIO_SOURCE_REMOTE_SUBMIX && |
Jan Sebechlebsky | bc56bcd | 2022-09-26 13:15:19 +0200 | [diff] [blame] | 2690 | extractAddressFromAudioAttributes(attributes).has_value()) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2691 | status = mPolicyMixes.getInputMixForAttr(attributes, &policyMix); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2692 | if (status != NO_ERROR) { |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2693 | ALOGW("%s could not find input mix for attr %s", |
| 2694 | __func__, toString(attributes).c_str()); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2695 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2696 | } |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2697 | device = mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2698 | String8(attr->tags + strlen("addr=")), |
| 2699 | AUDIO_FORMAT_DEFAULT); |
| 2700 | if (device == nullptr) { |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 2701 | 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] | 2702 | __func__, attributes.source, attributes.tags); |
| 2703 | status = BAD_VALUE; |
| 2704 | goto error; |
| 2705 | } |
| 2706 | |
Kevin Rocard | 25f9b05 | 2019-02-27 15:08:54 -0800 | [diff] [blame] | 2707 | if (is_mix_loopback_render(policyMix->mRouteFlags)) { |
| 2708 | *inputType = API_INPUT_MIX_PUBLIC_CAPTURE_PLAYBACK; |
| 2709 | } else { |
| 2710 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 2711 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2712 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2713 | if (explicitRoutingDevice != nullptr) { |
| 2714 | device = explicitRoutingDevice; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2715 | } else { |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 2716 | // Prevent from storing invalid requested device id in clients |
| 2717 | requestedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 2718 | device = mEngine->getInputDeviceForAttributes(attributes, uid, session, &policyMix); |
yuanjiahsu | 4069d5d | 2021-04-19 07:54:27 +0800 | [diff] [blame] | 2719 | ALOGV_IF(device != nullptr, "%s found device type is 0x%X", |
| 2720 | __FUNCTION__, device->type()); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2721 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2722 | if (device == nullptr) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2723 | ALOGW("getInputForAttr() could not find device for source %d", attributes.source); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2724 | status = BAD_VALUE; |
| 2725 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2726 | } |
Alden DSouza | b7d2078 | 2021-02-08 08:51:42 -0800 | [diff] [blame] | 2727 | if (device->type() == AUDIO_DEVICE_IN_ECHO_REFERENCE) { |
| 2728 | *inputType = API_INPUT_MIX_CAPTURE; |
| 2729 | } else if (policyMix) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2730 | ALOG_ASSERT(policyMix->mMixType == MIX_TYPE_RECORDERS, "Invalid Mix Type"); |
| 2731 | // there is an external policy, but this input is attached to a mix of recorders, |
| 2732 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 2733 | // know about it and is therefore considered "legacy" |
| 2734 | *inputType = API_INPUT_LEGACY; |
| 2735 | } else if (audio_is_remote_submix_device(device->type())) { |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2736 | *inputType = API_INPUT_MIX_CAPTURE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2737 | } else if (device->type() == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 2738 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2739 | } else { |
| 2740 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2741 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 2742 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2743 | } |
| 2744 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2745 | *input = getInputForDevice(device, session, attributes, config, flags, policyMix); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2746 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2747 | status = INVALID_OPERATION; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2748 | AudioProfileVector profiles; |
| 2749 | status_t ret = getProfilesForDevices( |
| 2750 | DeviceVector(device), profiles, flags, true /*isInput*/); |
| 2751 | if (ret == NO_ERROR && !profiles.empty()) { |
| 2752 | config->channel_mask = profiles[0]->getChannels().empty() ? config->channel_mask |
| 2753 | : *profiles[0]->getChannels().begin(); |
| 2754 | config->sample_rate = profiles[0]->getSampleRates().empty() ? config->sample_rate |
| 2755 | : *profiles[0]->getSampleRates().begin(); |
| 2756 | config->format = profiles[0]->getFormat(); |
| 2757 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2758 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2759 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2760 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2761 | exit: |
| 2762 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2763 | *selectedDeviceId = mAvailableInputDevices.contains(device) ? |
| 2764 | device->getId() : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2765 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2766 | isSoundTrigger = attributes.source == AUDIO_SOURCE_HOTWORD && |
Carter Hsu | d0cce2e | 2019-05-03 17:36:28 +0800 | [diff] [blame] | 2767 | mSoundTriggerSessions.indexOfKey(session) >= 0; |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 2768 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2769 | |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2770 | clientDesc = new RecordClientDescriptor(*portId, riid, uid, session, attributes, *config, |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2771 | requestedDeviceId, attributes.source, flags, |
| 2772 | isSoundTrigger); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2773 | inputDesc = mInputs.valueFor(*input); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2774 | inputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2775 | |
| 2776 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", |
| 2777 | *input, *inputType, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2778 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2779 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2780 | |
| 2781 | error: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2782 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2783 | } |
| 2784 | |
| 2785 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2786 | audio_io_handle_t AudioPolicyManager::getInputForDevice(const sp<DeviceDescriptor> &device, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2787 | audio_session_t session, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2788 | const audio_attributes_t &attributes, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2789 | audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2790 | audio_input_flags_t flags, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2791 | const sp<AudioPolicyMix> &policyMix) |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2792 | { |
| 2793 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2794 | audio_source_t halInputSource = attributes.source; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2795 | bool isSoundTrigger = false; |
| 2796 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2797 | if (attributes.source == AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2798 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 2799 | if (index >= 0) { |
| 2800 | input = mSoundTriggerSessions.valueFor(session); |
| 2801 | isSoundTrigger = true; |
| 2802 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 2803 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 2804 | } else { |
| 2805 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2806 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2807 | } else if (attributes.source == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2808 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 2809 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2810 | } |
| 2811 | |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 2812 | if (attributes.source == AUDIO_SOURCE_ULTRASOUND) { |
| 2813 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_ULTRASOUND); |
| 2814 | } |
| 2815 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2816 | // sampling rate and flags may be updated by getInputProfile |
| 2817 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 2818 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 2819 | audio_format_t profileFormat = config->format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2820 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2821 | audio_input_flags_t profileFlags = flags; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 2822 | // find a compatible input profile (not necessarily identical in parameters) |
| 2823 | sp<IOProfile> profile = getInputProfile( |
| 2824 | device, profileSamplingRate, profileFormat, profileChannelMask, profileFlags); |
| 2825 | if (profile == nullptr) { |
| 2826 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2827 | } |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 2828 | |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2829 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2830 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2831 | if (samplingRate == 0) { |
| 2832 | samplingRate = profileSamplingRate; |
| 2833 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2834 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 2835 | if (profile->getModuleHandle() == 0) { |
| 2836 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2837 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2838 | } |
| 2839 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2840 | // Reuse an already opened input if a client with the same session ID already exists |
| 2841 | // on that input |
| 2842 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2843 | sp <AudioInputDescriptor> desc = mInputs.valueAt(i); |
| 2844 | if (desc->mProfile != profile) { |
| 2845 | continue; |
| 2846 | } |
| 2847 | RecordClientVector clients = desc->clientsList(); |
| 2848 | for (const auto &client : clients) { |
| 2849 | if (session == client->session()) { |
| 2850 | return desc->mIoHandle; |
| 2851 | } |
| 2852 | } |
| 2853 | } |
| 2854 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2855 | if (!profile->canOpenNewIo()) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2856 | for (size_t i = 0; i < mInputs.size(); ) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 2857 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2858 | if (desc->mProfile != profile) { |
Carter Hsu | 9a645a9 | 2019-05-15 12:15:32 +0800 | [diff] [blame] | 2859 | i++; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2860 | continue; |
| 2861 | } |
| 2862 | // if sound trigger, reuse input if used by other sound trigger on same session |
| 2863 | // else |
| 2864 | // reuse input if active client app is not in IDLE state |
| 2865 | // |
| 2866 | RecordClientVector clients = desc->clientsList(); |
| 2867 | bool doClose = false; |
| 2868 | for (const auto& client : clients) { |
| 2869 | if (isSoundTrigger != client->isSoundTrigger()) { |
| 2870 | continue; |
| 2871 | } |
| 2872 | if (client->isSoundTrigger()) { |
| 2873 | if (session == client->session()) { |
| 2874 | return desc->mIoHandle; |
| 2875 | } |
| 2876 | continue; |
| 2877 | } |
| 2878 | if (client->active() && client->appState() != APP_STATE_IDLE) { |
| 2879 | return desc->mIoHandle; |
| 2880 | } |
| 2881 | doClose = true; |
| 2882 | } |
| 2883 | if (doClose) { |
| 2884 | closeInput(desc->mIoHandle); |
| 2885 | } else { |
| 2886 | i++; |
| 2887 | } |
| 2888 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2889 | } |
| 2890 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2891 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2892 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2893 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 2894 | lConfig.sample_rate = profileSamplingRate; |
| 2895 | lConfig.channel_mask = profileChannelMask; |
| 2896 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 2897 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2898 | status_t status = inputDesc->open(&lConfig, device, halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2899 | |
| 2900 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2901 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2902 | (profileSamplingRate != lConfig.sample_rate) || |
| 2903 | !audio_formats_match(profileFormat, lConfig.format) || |
| 2904 | (profileChannelMask != lConfig.channel_mask)) { |
| 2905 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 2906 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2907 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2908 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2909 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2910 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2911 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2912 | } |
| 2913 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2914 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2915 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2916 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2917 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2918 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2919 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2920 | } |
| 2921 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2922 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId) |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 2923 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2924 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2925 | |
| 2926 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2927 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2928 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 2929 | return DEAD_OBJECT; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2930 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2931 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2932 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2933 | if (client->active()) { |
| 2934 | ALOGW("%s input %d client %d already started", __FUNCTION__, input, client->portId()); |
| 2935 | return INVALID_OPERATION; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2936 | } |
| 2937 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2938 | audio_session_t session = client->session(); |
| 2939 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2940 | ALOGV("%s input:%d, session:%d)", __FUNCTION__, input, session); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2941 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2942 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2943 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2944 | status_t status = inputDesc->start(); |
| 2945 | if (status != NO_ERROR) { |
| 2946 | return status; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2947 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2948 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2949 | // increment activity count before calling getNewInputDevice() below as only active sessions |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2950 | // are considered for device selection |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2951 | inputDesc->setClientActive(client, true); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2952 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2953 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2954 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2955 | sp<DeviceDescriptor> device = getNewInputDevice(inputDesc); |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2956 | if (device != nullptr) { |
| 2957 | status = setInputDevice(input, device, true /* force */); |
| 2958 | } else { |
| 2959 | ALOGW("%s no new input device can be found for descriptor %d", |
| 2960 | __FUNCTION__, inputDesc->getId()); |
| 2961 | status = BAD_VALUE; |
| 2962 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2963 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2964 | if (status == NO_ERROR && inputDesc->activeCount() == 1) { |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2965 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2966 | // 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] | 2967 | if ((policyMix != nullptr) |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2968 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2969 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2970 | MIX_STATE_MIXING); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2971 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2972 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2973 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 2974 | if (primaryInputDevices.contains(device) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2975 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 2976 | mpClientInterface->setSoundTriggerCaptureState(true); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2977 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2978 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2979 | // automatically enable the remote submix output when input is started if not |
| 2980 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2981 | // 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] | 2982 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2983 | String8 address = String8(""); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2984 | if (policyMix == nullptr) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2985 | address = String8("0"); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2986 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2987 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2988 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2989 | if (address != "") { |
| 2990 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2991 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2992 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2993 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2994 | } |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2995 | } else if (status != NO_ERROR) { |
| 2996 | // Restore client activity state. |
| 2997 | inputDesc->setClientActive(client, false); |
| 2998 | inputDesc->stop(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2999 | } |
| 3000 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 3001 | ALOGV("%s input %d source = %d status = %d exit", |
| 3002 | __FUNCTION__, input, client->source(), status); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3003 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 3004 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3005 | } |
| 3006 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3007 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3008 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3009 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 3010 | |
| 3011 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 3012 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3013 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3014 | return BAD_VALUE; |
| 3015 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3016 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3017 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3018 | if (!client->active()) { |
| 3019 | ALOGW("%s input %d client %d already stopped", __FUNCTION__, input, client->portId()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3020 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3021 | } |
Carter Hsu | e6139d5 | 2021-07-08 10:30:20 +0800 | [diff] [blame] | 3022 | auto old_source = inputDesc->source(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3023 | inputDesc->setClientActive(client, false); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 3024 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3025 | inputDesc->stop(); |
| 3026 | if (inputDesc->isActive()) { |
Carter Hsu | e6139d5 | 2021-07-08 10:30:20 +0800 | [diff] [blame] | 3027 | auto current_source = inputDesc->source(); |
| 3028 | setInputDevice(input, getNewInputDevice(inputDesc), |
| 3029 | old_source != current_source /* force */); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3030 | } else { |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3031 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3032 | // 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] | 3033 | if ((policyMix != nullptr) |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3034 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 3035 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3036 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 3037 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3038 | |
| 3039 | // automatically disable the remote submix output when input is stopped if not |
| 3040 | // used by a policy mix of type MIX_TYPE_RECORDERS |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3041 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3042 | String8 address = String8(""); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 3043 | if (policyMix == nullptr) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3044 | address = String8("0"); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3045 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 3046 | address = policyMix->mDeviceAddress; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3047 | } |
| 3048 | if (address != "") { |
| 3049 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 3050 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 3051 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3052 | } |
| 3053 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3054 | resetInputDevice(input); |
| 3055 | |
| 3056 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 3057 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3058 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 3059 | if (primaryInputDevices.contains(inputDesc->getDevice()) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3060 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 3061 | mpClientInterface->setSoundTriggerCaptureState(false); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3062 | } |
| 3063 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3064 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3065 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3066 | } |
| 3067 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3068 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3069 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3070 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 3071 | |
| 3072 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 3073 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3074 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3075 | return; |
| 3076 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3077 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3078 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 3079 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3080 | ALOGV("%s %d", __FUNCTION__, input); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 3081 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3082 | inputDesc->removeClient(portId); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3083 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3084 | if (inputDesc->getClientCount() > 0) { |
| 3085 | ALOGV("%s(%d) %zu clients remaining", __func__, portId, inputDesc->getClientCount()); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3086 | return; |
| 3087 | } |
| 3088 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3089 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3090 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3091 | ALOGV("%s exit", __FUNCTION__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3092 | } |
| 3093 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3094 | void AudioPolicyManager::closeActiveClients(const sp<AudioInputDescriptor>& input) |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3095 | { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3096 | RecordClientVector clients = input->clientsList(true); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3097 | |
| 3098 | for (const auto& client : clients) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3099 | closeClient(client->portId()); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3100 | } |
| 3101 | } |
| 3102 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3103 | void AudioPolicyManager::closeClient(audio_port_handle_t portId) |
| 3104 | { |
| 3105 | stopInput(portId); |
| 3106 | releaseInput(portId); |
| 3107 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3108 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3109 | void AudioPolicyManager::checkCloseInputs() { |
| 3110 | // After connecting or disconnecting an input device, close input if: |
| 3111 | // - it has no client (was just opened to check profile) OR |
| 3112 | // - none of its supported devices are connected anymore OR |
| 3113 | // - one of its clients cannot be routed to one of its supported |
| 3114 | // devices anymore. Otherwise update device selection |
| 3115 | std::vector<audio_io_handle_t> inputsToClose; |
| 3116 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3117 | const sp<AudioInputDescriptor> input = mInputs.valueAt(i); |
| 3118 | if (input->clientsList().size() == 0 |
Eric Laurent | 85732f4 | 2020-03-19 11:31:10 -0700 | [diff] [blame] | 3119 | || !mAvailableInputDevices.containsAtLeastOne(input->supportedDevices())) { |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3120 | inputsToClose.push_back(mInputs.keyAt(i)); |
| 3121 | } else { |
| 3122 | bool close = false; |
| 3123 | for (const auto& client : input->clientsList()) { |
| 3124 | sp<DeviceDescriptor> device = |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 3125 | mEngine->getInputDeviceForAttributes(client->attributes(), client->uid(), |
| 3126 | client->session()); |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3127 | if (!input->supportedDevices().contains(device)) { |
| 3128 | close = true; |
| 3129 | break; |
| 3130 | } |
| 3131 | } |
| 3132 | if (close) { |
| 3133 | inputsToClose.push_back(mInputs.keyAt(i)); |
| 3134 | } else { |
| 3135 | setInputDevice(input->mIoHandle, getNewInputDevice(input)); |
| 3136 | } |
| 3137 | } |
| 3138 | } |
| 3139 | |
| 3140 | for (const audio_io_handle_t handle : inputsToClose) { |
| 3141 | ALOGV("%s closing input %d", __func__, handle); |
| 3142 | closeInput(handle); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3143 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3144 | } |
| 3145 | |
François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 3146 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, int indexMin, int indexMax) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3147 | { |
| 3148 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 3149 | if (indexMin < 0 || indexMax < 0) { |
| 3150 | ALOGE("%s for stream %d: invalid min %d or max %d", __func__, stream , indexMin, indexMax); |
| 3151 | return; |
| 3152 | } |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 3153 | getVolumeCurves(stream).initVolume(indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3154 | |
| 3155 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3156 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 3157 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3158 | continue; |
| 3159 | } |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 3160 | getVolumeCurves((audio_stream_type_t)curStream).initVolume(indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3161 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3162 | } |
| 3163 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3164 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3165 | int index, |
| 3166 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3167 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3168 | auto attributes = mEngine->getAttributesForStreamType(stream); |
Eric Laurent | 242a9f8 | 2020-03-23 15:57:04 -0700 | [diff] [blame] | 3169 | if (attributes == AUDIO_ATTRIBUTES_INITIALIZER) { |
| 3170 | ALOGW("%s: no group for stream %s, bailing out", __func__, toString(stream).c_str()); |
| 3171 | return NO_ERROR; |
| 3172 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3173 | ALOGV("%s: stream %s attributes=%s", __func__, |
| 3174 | toString(stream).c_str(), toString(attributes).c_str()); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3175 | return setVolumeIndexForAttributes(attributes, index, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3176 | } |
| 3177 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3178 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3179 | int *index, |
| 3180 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3181 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3182 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this |
| 3183 | // stream by the engine. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3184 | DeviceTypeSet deviceTypes = {device}; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 3185 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3186 | deviceTypes = mEngine->getOutputDevicesForStream( |
| 3187 | stream, true /*fromCache*/).types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3188 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3189 | return getVolumeIndex(getVolumeCurves(stream), *index, deviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3190 | } |
| 3191 | |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3192 | status_t AudioPolicyManager::setVolumeIndexForAttributes(const audio_attributes_t &attributes, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3193 | int index, |
| 3194 | audio_devices_t device) |
| 3195 | { |
| 3196 | // Get Volume group matching the Audio Attributes |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3197 | auto group = mEngine->getVolumeGroupForAttributes(attributes); |
| 3198 | if (group == VOLUME_GROUP_NONE) { |
| 3199 | 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] | 3200 | return BAD_VALUE; |
| 3201 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3202 | 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] | 3203 | status_t status = NO_ERROR; |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3204 | IVolumeCurves &curves = getVolumeCurves(attributes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3205 | VolumeSource vs = toVolumeSource(group); |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3206 | // AUDIO_STREAM_BLUETOOTH_SCO is only used for volume control so we remap |
| 3207 | // to AUDIO_STREAM_VOICE_CALL to match with relevant playback activity |
| 3208 | VolumeSource activityVs = (vs == toVolumeSource(AUDIO_STREAM_BLUETOOTH_SCO, false)) ? |
| 3209 | toVolumeSource(AUDIO_STREAM_VOICE_CALL, false) : vs; |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3210 | product_strategy_t strategy = mEngine->getProductStrategyForAttributes(attributes); |
| 3211 | |
| 3212 | status = setVolumeCurveIndex(index, device, curves); |
| 3213 | if (status != NO_ERROR) { |
| 3214 | ALOGE("%s failed to set curve index for group %d device 0x%X", __func__, group, device); |
| 3215 | return status; |
| 3216 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3217 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3218 | DeviceTypeSet curSrcDevices; |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3219 | auto curCurvAttrs = curves.getAttributes(); |
| 3220 | if (!curCurvAttrs.empty() && curCurvAttrs.front() != defaultAttr) { |
| 3221 | auto attr = curCurvAttrs.front(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3222 | curSrcDevices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false).types(); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3223 | } else if (!curves.getStreamTypes().empty()) { |
| 3224 | auto stream = curves.getStreamTypes().front(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3225 | curSrcDevices = mEngine->getOutputDevicesForStream(stream, false).types(); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3226 | } else { |
| 3227 | ALOGE("%s: Invalid src %d: no valid attributes nor stream",__func__, vs); |
| 3228 | return BAD_VALUE; |
| 3229 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3230 | audio_devices_t curSrcDevice = Volume::getDeviceForVolume(curSrcDevices); |
| 3231 | resetDeviceTypes(curSrcDevices, curSrcDevice); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3232 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3233 | // update volume on all outputs and streams matching the following: |
| 3234 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 3235 | // - The device (or devices) selected by the engine for this stream includes |
| 3236 | // the requested device |
| 3237 | // - For non default requested device, currently selected device on the output is either the |
| 3238 | // requested device or one of the devices selected by the engine for this stream |
| 3239 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 3240 | // no specific device volume value exists for currently selected device. |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3241 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3242 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3243 | DeviceTypeSet curDevices = desc->devices().types(); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3244 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3245 | if (curDevices.erase(AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { |
| 3246 | curDevices.insert(AUDIO_DEVICE_OUT_SPEAKER); |
Robert Lee | 5e66e79 | 2019-04-03 18:37:15 +0800 | [diff] [blame] | 3247 | } |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3248 | |
| 3249 | if (!(desc->isActive(activityVs) || isInCallOrScreening())) { |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3250 | continue; |
| 3251 | } |
| 3252 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME && |
| 3253 | curDevices.find(device) == curDevices.end()) { |
| 3254 | continue; |
| 3255 | } |
| 3256 | bool applyVolume = false; |
| 3257 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
| 3258 | curSrcDevices.insert(device); |
| 3259 | applyVolume = (curSrcDevices.find( |
| 3260 | Volume::getDeviceForVolume(curDevices)) != curSrcDevices.end()); |
| 3261 | } else { |
| 3262 | applyVolume = !curves.hasVolumeIndexForDevice(curSrcDevice); |
| 3263 | } |
| 3264 | if (!applyVolume) { |
| 3265 | continue; // next output |
| 3266 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3267 | // Inter / intra volume group priority management: Loop on strategies arranged by priority |
| 3268 | // If a higher priority strategy is active, and the output is routed to a device with a |
| 3269 | // HW Gain management, do not change the volume |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3270 | if (desc->useHwGain()) { |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3271 | applyVolume = false; |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 3272 | // If the volume source is active with higher priority source, ensure at least Sw Muted |
| 3273 | desc->setSwMute((index == 0), vs, curves.getStreamTypes(), curDevices, 0 /*delayMs*/); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3274 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { |
| 3275 | auto activeClients = desc->clientsList(true /*activeOnly*/, productStrategy, |
| 3276 | false /*preferredDevice*/); |
| 3277 | if (activeClients.empty()) { |
| 3278 | continue; |
| 3279 | } |
| 3280 | bool isPreempted = false; |
| 3281 | bool isHigherPriority = productStrategy < strategy; |
| 3282 | for (const auto &client : activeClients) { |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3283 | if (isHigherPriority && (client->volumeSource() != activityVs)) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3284 | ALOGV("%s: Strategy=%d (\nrequester:\n" |
| 3285 | " group %d, volumeGroup=%d attributes=%s)\n" |
| 3286 | " higher priority source active:\n" |
| 3287 | " volumeGroup=%d attributes=%s) \n" |
| 3288 | " on output %zu, bailing out", __func__, productStrategy, |
| 3289 | group, group, toString(attributes).c_str(), |
| 3290 | client->volumeSource(), toString(client->attributes()).c_str(), i); |
| 3291 | applyVolume = false; |
| 3292 | isPreempted = true; |
| 3293 | break; |
| 3294 | } |
| 3295 | // 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] | 3296 | if (client->volumeSource() == activityVs) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3297 | applyVolume = true; |
| 3298 | } |
| 3299 | } |
| 3300 | if (isPreempted || applyVolume) { |
| 3301 | break; |
| 3302 | } |
| 3303 | } |
| 3304 | if (!applyVolume) { |
| 3305 | continue; // next output |
| 3306 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3307 | } |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3308 | //FIXME: workaround for truncated touch sounds |
| 3309 | // delayed volume change for system stream to be removed when the problem is |
| 3310 | // handled by system UI |
| 3311 | status_t volStatus = checkAndSetVolume( |
| 3312 | curves, vs, index, desc, curDevices, |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3313 | ((vs == toVolumeSource(AUDIO_STREAM_SYSTEM, false))? |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3314 | TOUCH_SOUND_FIXED_DELAY_MS : 0)); |
| 3315 | if (volStatus != NO_ERROR) { |
| 3316 | status = volStatus; |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3317 | } |
| 3318 | } |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3319 | mpClientInterface->onAudioVolumeGroupChanged(group, 0 /*flags*/); |
| 3320 | return status; |
| 3321 | } |
| 3322 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3323 | status_t AudioPolicyManager::setVolumeCurveIndex(int index, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3324 | audio_devices_t device, |
| 3325 | IVolumeCurves &volumeCurves) |
| 3326 | { |
| 3327 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 3328 | // app that has MODIFY_PHONE_STATE permission. |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3329 | bool hasVoice = hasVoiceStream(volumeCurves.getStreamTypes()); |
| 3330 | if (((index < volumeCurves.getVolumeIndexMin()) && !(hasVoice && index == 0)) || |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3331 | (index > volumeCurves.getVolumeIndexMax())) { |
| 3332 | ALOGD("%s: wrong index %d min=%d max=%d", __FUNCTION__, index, |
| 3333 | volumeCurves.getVolumeIndexMin(), volumeCurves.getVolumeIndexMax()); |
| 3334 | return BAD_VALUE; |
| 3335 | } |
| 3336 | if (!audio_is_output_device(device)) { |
| 3337 | return BAD_VALUE; |
| 3338 | } |
| 3339 | |
| 3340 | // Force max volume if stream cannot be muted |
| 3341 | if (!volumeCurves.canBeMuted()) index = volumeCurves.getVolumeIndexMax(); |
| 3342 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3343 | ALOGV("%s device %08x, index %d", __FUNCTION__ , device, index); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3344 | volumeCurves.addCurrentVolumeIndex(device, index); |
| 3345 | return NO_ERROR; |
| 3346 | } |
| 3347 | |
| 3348 | status_t AudioPolicyManager::getVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3349 | int &index, |
| 3350 | audio_devices_t device) |
| 3351 | { |
| 3352 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this |
| 3353 | // stream by the engine. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3354 | DeviceTypeSet deviceTypes = {device}; |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3355 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Mikhail Naganov | bb990f2 | 2022-06-15 00:46:43 +0000 | [diff] [blame] | 3356 | deviceTypes = mEngine->getOutputDevicesForAttributes( |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3357 | attr, nullptr, true /*fromCache*/).types(); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3358 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3359 | return getVolumeIndex(getVolumeCurves(attr), index, deviceTypes); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3360 | } |
| 3361 | |
| 3362 | status_t AudioPolicyManager::getVolumeIndex(const IVolumeCurves &curves, |
| 3363 | int &index, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3364 | const DeviceTypeSet& deviceTypes) const |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3365 | { |
Mikhail Naganov | bb990f2 | 2022-06-15 00:46:43 +0000 | [diff] [blame] | 3366 | if (!isSingleDeviceType(deviceTypes, audio_is_output_device)) { |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3367 | return BAD_VALUE; |
| 3368 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3369 | index = curves.getVolumeIndex(deviceTypes); |
| 3370 | 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] | 3371 | return NO_ERROR; |
| 3372 | } |
| 3373 | |
| 3374 | status_t AudioPolicyManager::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3375 | int &index) |
| 3376 | { |
| 3377 | index = getVolumeCurves(attr).getVolumeIndexMin(); |
| 3378 | return NO_ERROR; |
| 3379 | } |
| 3380 | |
| 3381 | status_t AudioPolicyManager::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3382 | int &index) |
| 3383 | { |
| 3384 | index = getVolumeCurves(attr).getVolumeIndexMax(); |
| 3385 | return NO_ERROR; |
| 3386 | } |
| 3387 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3388 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3389 | { |
| 3390 | // select one output among several suitable for global effects. |
| 3391 | // The priority is as follows: |
| 3392 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 3393 | // AudioFlinger will invalidate the track and the offloaded output |
| 3394 | // will be closed causing the effect to be moved to a PCM output. |
| 3395 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3396 | // 3: The primary output |
| 3397 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3398 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3399 | DeviceVector devices = mEngine->getOutputDevicesForAttributes( |
| 3400 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3401 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3402 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3403 | if (outputs.size() == 0) { |
| 3404 | return AUDIO_IO_HANDLE_NONE; |
| 3405 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3406 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3407 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3408 | bool activeOnly = true; |
| 3409 | |
| 3410 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 3411 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 3412 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 3413 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 3414 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3415 | for (audio_io_handle_t output : outputs) { |
| 3416 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 3417 | if (activeOnly && !desc->isActive(toVolumeSource(AUDIO_STREAM_MUSIC))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3418 | continue; |
| 3419 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3420 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 3421 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3422 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3423 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3424 | } |
| 3425 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3426 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3427 | } |
| 3428 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3429 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3430 | } |
| 3431 | } |
| 3432 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 3433 | output = outputOffloaded; |
| 3434 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 3435 | output = outputDeepBuffer; |
| 3436 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 3437 | output = outputPrimary; |
| 3438 | } else { |
| 3439 | output = outputs[0]; |
| 3440 | } |
| 3441 | activeOnly = false; |
| 3442 | } |
| 3443 | |
| 3444 | if (output != mMusicEffectOutput) { |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3445 | mEffects.moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3446 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 3447 | mMusicEffectOutput = output; |
| 3448 | } |
| 3449 | |
| 3450 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3451 | return output; |
| 3452 | } |
| 3453 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3454 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 3455 | { |
| 3456 | return selectOutputForMusicEffects(); |
| 3457 | } |
| 3458 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3459 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3460 | audio_io_handle_t io, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 3461 | product_strategy_t strategy, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3462 | int session, |
| 3463 | int id) |
| 3464 | { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3465 | if (session != AUDIO_SESSION_DEVICE) { |
| 3466 | ssize_t index = mOutputs.indexOfKey(io); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3467 | if (index < 0) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3468 | index = mInputs.indexOfKey(io); |
| 3469 | if (index < 0) { |
| 3470 | ALOGW("registerEffect() unknown io %d", io); |
| 3471 | return INVALID_OPERATION; |
| 3472 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3473 | } |
| 3474 | } |
Eric Laurent | bf8f69f | 2022-03-25 17:48:38 +0100 | [diff] [blame] | 3475 | bool isMusicEffect = (session != AUDIO_SESSION_OUTPUT_STAGE) |
| 3476 | && ((strategy == streamToStrategy(AUDIO_STREAM_MUSIC) |
| 3477 | || strategy == PRODUCT_STRATEGY_NONE)); |
| 3478 | return mEffects.registerEffect(desc, io, session, id, isMusicEffect); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3479 | } |
| 3480 | |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 3481 | status_t AudioPolicyManager::unregisterEffect(int id) |
| 3482 | { |
| 3483 | if (mEffects.getEffect(id) == nullptr) { |
| 3484 | return INVALID_OPERATION; |
| 3485 | } |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 3486 | if (mEffects.isEffectEnabled(id)) { |
| 3487 | ALOGW("%s effect %d enabled", __FUNCTION__, id); |
| 3488 | setEffectEnabled(id, false); |
| 3489 | } |
| 3490 | return mEffects.unregisterEffect(id); |
| 3491 | } |
| 3492 | |
| 3493 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) |
| 3494 | { |
| 3495 | sp<EffectDescriptor> effect = mEffects.getEffect(id); |
| 3496 | if (effect == nullptr) { |
| 3497 | return INVALID_OPERATION; |
| 3498 | } |
| 3499 | |
| 3500 | status_t status = mEffects.setEffectEnabled(id, enabled); |
| 3501 | if (status == NO_ERROR) { |
| 3502 | mInputs.trackEffectEnabled(effect, enabled); |
| 3503 | } |
| 3504 | return status; |
| 3505 | } |
| 3506 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3507 | |
| 3508 | status_t AudioPolicyManager::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) |
| 3509 | { |
| 3510 | mEffects.moveEffects(ids, io); |
| 3511 | return NO_ERROR; |
| 3512 | } |
| 3513 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3514 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 3515 | { |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3516 | auto vs = toVolumeSource(stream, false); |
| 3517 | return vs != VOLUME_SOURCE_NONE ? mOutputs.isActive(vs, inPastMs) : false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 3521 | { |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3522 | auto vs = toVolumeSource(stream, false); |
| 3523 | return vs != VOLUME_SOURCE_NONE ? mOutputs.isActiveRemotely(vs, inPastMs) : false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3524 | } |
| 3525 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3526 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3527 | { |
| 3528 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3529 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3530 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3531 | return true; |
| 3532 | } |
| 3533 | } |
| 3534 | return false; |
| 3535 | } |
| 3536 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3537 | // Register a list of custom mixes with their attributes and format. |
| 3538 | // When a mix is registered, corresponding input and output profiles are |
| 3539 | // added to the remote submix hw module. The profile contains only the |
| 3540 | // parameters (sampling rate, format...) specified by the mix. |
| 3541 | // The corresponding input remote submix device is also connected. |
| 3542 | // |
| 3543 | // When a remote submix device is connected, the address is checked to select the |
| 3544 | // appropriate profile and the corresponding input or output stream is opened. |
| 3545 | // |
| 3546 | // When capture starts, getInputForAttr() will: |
| 3547 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 3548 | // - 2 if none found, getDeviceForInputSource() will: |
| 3549 | // - 2.1 look for a mix matching the attributes source |
| 3550 | // - 2.2 if none found, default to device selection by policy rules |
| 3551 | // At this time, the corresponding output remote submix device is also connected |
| 3552 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 3553 | // after AudioTracks are invalidated |
| 3554 | // |
| 3555 | // When playback starts, getOutputForAttr() will: |
| 3556 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 3557 | // - 2 if none found, look for a mix matching the attributes usage |
| 3558 | // - 3 if none found, default to device and output selection by policy rules. |
| 3559 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3560 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3561 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3562 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 3563 | status_t res = NO_ERROR; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3564 | bool checkOutputs = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3565 | sp<HwModule> rSubmixModule; |
| 3566 | // examine each mix's route type |
| 3567 | for (size_t i = 0; i < mixes.size(); i++) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3568 | AudioMix mix = mixes[i]; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3569 | // Only capture of playback is allowed in LOOP_BACK & RENDER mode |
| 3570 | if (is_mix_loopback_render(mix.mRouteFlags) && mix.mMixType != MIX_TYPE_PLAYERS) { |
| 3571 | ALOGE("Unsupported Policy Mix %zu of %zu: " |
| 3572 | "Only capture of playback is allowed in LOOP_BACK & RENDER mode", |
| 3573 | i, mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3574 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3575 | break; |
| 3576 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3577 | // LOOP_BACK and LOOP_BACK | RENDER have the same remote submix backend and are handled |
| 3578 | // in the same way. |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3579 | 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] | 3580 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK %d", i, mixes.size(), |
| 3581 | mix.mRouteFlags); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3582 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3583 | rSubmixModule = mHwModules.getModuleFromName( |
| 3584 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 3585 | if (rSubmixModule == 0) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3586 | ALOGE("Unable to find audio module for submix, aborting mix %zu registration", |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3587 | i); |
| 3588 | res = INVALID_OPERATION; |
| 3589 | break; |
| 3590 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3591 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3592 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3593 | String8 address = mix.mDeviceAddress; |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3594 | audio_devices_t deviceTypeToMakeAvailable; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3595 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3596 | mix.mDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3597 | deviceTypeToMakeAvailable = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 3598 | } else { |
| 3599 | mix.mDeviceType = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 3600 | deviceTypeToMakeAvailable = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3601 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3602 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3603 | if (mPolicyMixes.registerMix(mix, 0 /*output desc*/) != NO_ERROR) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3604 | ALOGE("Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3605 | res = INVALID_OPERATION; |
| 3606 | break; |
| 3607 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3608 | audio_config_t outputConfig = mix.mFormat; |
| 3609 | audio_config_t inputConfig = mix.mFormat; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3610 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL |
| 3611 | // 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] | 3612 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 3613 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3614 | rSubmixModule->addOutputProfile(address.c_str(), &outputConfig, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3615 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3616 | rSubmixModule->addInputProfile(address.c_str(), &inputConfig, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3617 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3618 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3619 | if ((res = setDeviceConnectionStateInt(deviceTypeToMakeAvailable, |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 3620 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3621 | address.string(), "remote-submix", AUDIO_FORMAT_DEFAULT)) != NO_ERROR) { |
| 3622 | ALOGE("Failed to set remote submix device available, type %u, address %s", |
| 3623 | mix.mDeviceType, address.string()); |
| 3624 | break; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3625 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3626 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 3627 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3628 | audio_devices_t type = mix.mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3629 | 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] | 3630 | i, mixes.size(), type, address.string()); |
| 3631 | |
| 3632 | sp<DeviceDescriptor> device = mHwModules.getDeviceDescriptor( |
| 3633 | mix.mDeviceType, mix.mDeviceAddress, |
| 3634 | String8(), AUDIO_FORMAT_DEFAULT); |
| 3635 | if (device == nullptr) { |
| 3636 | res = INVALID_OPERATION; |
| 3637 | break; |
| 3638 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3639 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3640 | bool foundOutput = false; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3641 | // First try to find an already opened output supporting the device |
| 3642 | 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] | 3643 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3644 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3645 | if (!desc->isDuplicated() && desc->supportedDevices().contains(device)) { |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3646 | if (mPolicyMixes.registerMix(mix, desc) != NO_ERROR) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3647 | ALOGE("Could not register mix RENDER, dev=0x%X addr=%s", type, |
| 3648 | address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3649 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3650 | } else { |
| 3651 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3652 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3653 | } |
| 3654 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3655 | // If no output found, try to find a direct output profile supporting the device |
| 3656 | for (size_t i = 0; i < mHwModules.size() && !foundOutput && res == NO_ERROR; i++) { |
| 3657 | sp<HwModule> module = mHwModules[i]; |
| 3658 | for (size_t j = 0; |
| 3659 | j < module->getOutputProfiles().size() && !foundOutput && res == NO_ERROR; |
| 3660 | j++) { |
| 3661 | sp<IOProfile> profile = module->getOutputProfiles()[j]; |
| 3662 | if (profile->isDirectOutput() && profile->supportsDevice(device)) { |
| 3663 | if (mPolicyMixes.registerMix(mix, nullptr) != NO_ERROR) { |
| 3664 | ALOGE("Could not register mix RENDER, dev=0x%X addr=%s", type, |
| 3665 | address.string()); |
| 3666 | res = INVALID_OPERATION; |
| 3667 | } else { |
| 3668 | foundOutput = true; |
| 3669 | } |
| 3670 | } |
| 3671 | } |
| 3672 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3673 | if (res != NO_ERROR) { |
| 3674 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3675 | i, type, address.string()); |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3676 | res = INVALID_OPERATION; |
| 3677 | break; |
| 3678 | } else if (!foundOutput) { |
| 3679 | 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] | 3680 | i, type, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3681 | res = INVALID_OPERATION; |
| 3682 | break; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3683 | } else { |
| 3684 | checkOutputs = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3685 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3686 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3687 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3688 | if (res != NO_ERROR) { |
| 3689 | unregisterPolicyMixes(mixes); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3690 | } else if (checkOutputs) { |
| 3691 | checkForDeviceAndOutputChanges(); |
| 3692 | updateCallAndOutputRouting(); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3693 | } |
| 3694 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 3698 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3699 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3700 | status_t res = NO_ERROR; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3701 | bool checkOutputs = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3702 | sp<HwModule> rSubmixModule; |
| 3703 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3704 | for (const auto& mix : mixes) { |
| 3705 | 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] | 3706 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3707 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3708 | rSubmixModule = mHwModules.getModuleFromName( |
| 3709 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 3710 | if (rSubmixModule == 0) { |
| 3711 | res = INVALID_OPERATION; |
| 3712 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3713 | } |
| 3714 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3715 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3716 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3717 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3718 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3719 | res = INVALID_OPERATION; |
| 3720 | continue; |
| 3721 | } |
| 3722 | |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 3723 | for (auto device : {AUDIO_DEVICE_IN_REMOTE_SUBMIX, AUDIO_DEVICE_OUT_REMOTE_SUBMIX}) { |
| 3724 | if (getDeviceConnectionState(device, address.string()) == |
| 3725 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 3726 | res = setDeviceConnectionStateInt(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3727 | address.string(), "remote-submix", |
| 3728 | AUDIO_FORMAT_DEFAULT); |
| 3729 | if (res != OK) { |
| 3730 | ALOGE("Error making RemoteSubmix device unavailable for mix " |
| 3731 | "with type %d, address %s", device, address.string()); |
| 3732 | } |
| 3733 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3734 | } |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3735 | rSubmixModule->removeOutputProfile(address.c_str()); |
| 3736 | rSubmixModule->removeInputProfile(address.c_str()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3737 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3738 | } 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] | 3739 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3740 | res = INVALID_OPERATION; |
| 3741 | continue; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3742 | } else { |
| 3743 | checkOutputs = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3744 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3745 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3746 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3747 | if (res == NO_ERROR && checkOutputs) { |
| 3748 | checkForDeviceAndOutputChanges(); |
| 3749 | updateCallAndOutputRouting(); |
| 3750 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3751 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3752 | } |
| 3753 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3754 | void AudioPolicyManager::dumpManualSurroundFormats(String8 *dst) const |
| 3755 | { |
| 3756 | size_t i = 0; |
| 3757 | constexpr size_t audioFormatPrefixLen = sizeof("AUDIO_FORMAT_"); |
| 3758 | for (const auto& fmt : mManualSurroundFormats) { |
| 3759 | if (i++ != 0) dst->append(", "); |
| 3760 | std::string sfmt; |
| 3761 | FormatConverter::toString(fmt, sfmt); |
| 3762 | dst->append(sfmt.size() >= audioFormatPrefixLen ? |
| 3763 | sfmt.c_str() + audioFormatPrefixLen - 1 : sfmt.c_str()); |
| 3764 | } |
| 3765 | } |
| 3766 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3767 | // Returns true if all devices types match the predicate and are supported by one HW module |
| 3768 | bool AudioPolicyManager::areAllDevicesSupported( |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3769 | const AudioDeviceTypeAddrVector& devices, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3770 | std::function<bool(audio_devices_t)> predicate, |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 3771 | const char *context, |
| 3772 | bool matchAddress) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3773 | for (size_t i = 0; i < devices.size(); i++) { |
| 3774 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor( |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3775 | devices[i].mType, devices[i].getAddress(), String8(), |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 3776 | AUDIO_FORMAT_DEFAULT, false /*allowToCreate*/, matchAddress); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3777 | if (devDesc == nullptr || (predicate != nullptr && !predicate(devices[i].mType))) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3778 | ALOGE("%s: device type %#x address %s not supported or not match predicate", |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3779 | context, devices[i].mType, devices[i].getAddress()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3780 | return false; |
| 3781 | } |
| 3782 | } |
| 3783 | return true; |
| 3784 | } |
| 3785 | |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame^] | 3786 | void AudioPolicyManager::changeOutputDevicesMuteState( |
| 3787 | const AudioDeviceTypeAddrVector& devices) { |
| 3788 | ALOGVV("%s() num devices %zu", __func__, devices.size()); |
| 3789 | |
| 3790 | std::vector<sp<SwAudioOutputDescriptor>> outputs = |
| 3791 | getSoftwareOutputsForDevices(devices); |
| 3792 | |
| 3793 | for (size_t i = 0; i < outputs.size(); i++) { |
| 3794 | sp<SwAudioOutputDescriptor> outputDesc = outputs[i]; |
| 3795 | DeviceVector prevDevices = outputDesc->devices(); |
| 3796 | checkDeviceMuteStrategies(outputDesc, prevDevices, 0 /* delayMs */); |
| 3797 | } |
| 3798 | } |
| 3799 | |
| 3800 | std::vector<sp<SwAudioOutputDescriptor>> AudioPolicyManager::getSoftwareOutputsForDevices( |
| 3801 | const AudioDeviceTypeAddrVector& devices) const |
| 3802 | { |
| 3803 | std::vector<sp<SwAudioOutputDescriptor>> outputs; |
| 3804 | DeviceVector deviceDescriptors; |
| 3805 | for (size_t j = 0; j < devices.size(); j++) { |
| 3806 | sp<DeviceDescriptor> desc = mHwModules.getDeviceDescriptor( |
| 3807 | devices[j].mType, devices[j].getAddress(), String8(), AUDIO_FORMAT_DEFAULT); |
| 3808 | if (desc == nullptr || !audio_is_output_device(devices[j].mType)) { |
| 3809 | ALOGE("%s: device type %#x address %s not supported or not an output device", |
| 3810 | __func__, devices[j].mType, devices[j].getAddress()); |
| 3811 | continue; |
| 3812 | } |
| 3813 | deviceDescriptors.add(desc); |
| 3814 | } |
| 3815 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3816 | if (!mOutputs.valueAt(i)->supportsAtLeastOne(deviceDescriptors)) { |
| 3817 | continue; |
| 3818 | } |
| 3819 | outputs.push_back(mOutputs.valueAt(i)); |
| 3820 | } |
| 3821 | return outputs; |
| 3822 | } |
| 3823 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3824 | status_t AudioPolicyManager::setUidDeviceAffinities(uid_t uid, |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3825 | const AudioDeviceTypeAddrVector& devices) { |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3826 | ALOGV("%s() uid=%d num devices %zu", __FUNCTION__, uid, devices.size()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3827 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
| 3828 | return BAD_VALUE; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3829 | } |
| 3830 | status_t res = mPolicyMixes.setUidDeviceAffinities(uid, devices); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3831 | if (res != NO_ERROR) { |
| 3832 | ALOGE("%s() Could not set all device affinities for uid = %d", __FUNCTION__, uid); |
| 3833 | return res; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3834 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3835 | |
| 3836 | checkForDeviceAndOutputChanges(); |
| 3837 | updateCallAndOutputRouting(); |
| 3838 | |
| 3839 | return NO_ERROR; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3840 | } |
| 3841 | |
| 3842 | status_t AudioPolicyManager::removeUidDeviceAffinities(uid_t uid) { |
| 3843 | ALOGV("%s() uid=%d", __FUNCTION__, uid); |
Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 3844 | status_t res = mPolicyMixes.removeUidDeviceAffinities(uid); |
| 3845 | if (res != NO_ERROR) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3846 | ALOGE("%s() Could not remove all device affinities for uid = %d", |
Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 3847 | __FUNCTION__, uid); |
| 3848 | return INVALID_OPERATION; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3849 | } |
| 3850 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3851 | checkForDeviceAndOutputChanges(); |
| 3852 | updateCallAndOutputRouting(); |
| 3853 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3854 | return res; |
| 3855 | } |
| 3856 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3857 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3858 | status_t AudioPolicyManager::setDevicesRoleForStrategy(product_strategy_t strategy, |
| 3859 | device_role_t role, |
| 3860 | const AudioDeviceTypeAddrVector &devices) { |
| 3861 | ALOGV("%s() strategy=%d role=%d %s", __func__, strategy, role, |
| 3862 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3863 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3864 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3865 | return BAD_VALUE; |
| 3866 | } |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3867 | status_t status = mEngine->setDevicesRoleForStrategy(strategy, role, devices); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3868 | if (status != NO_ERROR) { |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3869 | ALOGW("Engine could not set preferred devices %s for strategy %d role %d", |
| 3870 | dumpAudioDeviceTypeAddrVector(devices).c_str(), strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3871 | return status; |
| 3872 | } |
| 3873 | |
| 3874 | checkForDeviceAndOutputChanges(); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3875 | |
| 3876 | bool forceVolumeReeval = false; |
| 3877 | // FIXME: workaround for truncated touch sounds |
| 3878 | // to be removed when the problem is handled by system UI |
| 3879 | uint32_t delayMs = 0; |
| 3880 | if (strategy == mCommunnicationStrategy) { |
| 3881 | forceVolumeReeval = true; |
| 3882 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 3883 | updateInputRouting(); |
| 3884 | } |
| 3885 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3886 | |
| 3887 | return NO_ERROR; |
| 3888 | } |
| 3889 | |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame^] | 3890 | void AudioPolicyManager::updateCallAndOutputRouting(bool forceVolumeReeval, uint32_t delayMs, |
| 3891 | bool skipDelays) |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3892 | { |
| 3893 | uint32_t waitMs = 0; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 3894 | bool wasLeUnicastActive = isLeUnicastActive(); |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 3895 | if (updateCallRouting(true /*fromCache*/, delayMs, &waitMs) == NO_ERROR) { |
Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 3896 | // Only apply special touch sound delay once |
| 3897 | delayMs = 0; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3898 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 3899 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3900 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3901 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 3902 | DeviceVector newDevices = getNewOutputDevices(outputDesc, true /*fromCache*/); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 3903 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || |
| 3904 | (outputDesc != mPrimaryOutput && !isTelephonyRxOrTx(outputDesc))) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3905 | // As done in setDeviceConnectionState, we could also fix default device issue by |
| 3906 | // preventing the force re-routing in case of default dev that distinguishes on address. |
| 3907 | // Let's give back to engine full device choice decision however. |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 3908 | bool forceRouting = !newDevices.isEmpty(); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 3909 | if (outputDesc->mUsePreferredMixerAttributes && newDevices != outputDesc->devices()) { |
| 3910 | // If the device is using preferred mixer attributes, the output need to reopen |
| 3911 | // with default configuration when the new selected devices are different from |
| 3912 | // current routing devices. |
| 3913 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices); |
| 3914 | continue; |
| 3915 | } |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 3916 | waitMs = setOutputDevices(outputDesc, newDevices, forceRouting, delayMs, nullptr, |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame^] | 3917 | !skipDelays /*requiresMuteCheck*/, |
| 3918 | !forceRouting /*requiresVolumeCheck*/, skipDelays); |
Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 3919 | // Only apply special touch sound delay once |
| 3920 | delayMs = 0; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3921 | } |
| 3922 | if (forceVolumeReeval && !newDevices.isEmpty()) { |
| 3923 | applyStreamVolumes(outputDesc, newDevices.types(), waitMs, true); |
| 3924 | } |
| 3925 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 3926 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 3927 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3928 | } |
| 3929 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3930 | void AudioPolicyManager::updateInputRouting() { |
| 3931 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Jaideep Sharma | 408349a | 2020-11-27 14:47:17 +0530 | [diff] [blame] | 3932 | // Skip for hotword recording as the input device switch |
| 3933 | // is handled within sound trigger HAL |
| 3934 | if (activeDesc->isSoundTrigger() && activeDesc->source() == AUDIO_SOURCE_HOTWORD) { |
| 3935 | continue; |
| 3936 | } |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3937 | auto newDevice = getNewInputDevice(activeDesc); |
| 3938 | // Force new input selection if the new device can not be reached via current input |
| 3939 | if (activeDesc->mProfile->getSupportedDevices().contains(newDevice)) { |
| 3940 | setInputDevice(activeDesc->mIoHandle, newDevice); |
| 3941 | } else { |
| 3942 | closeInput(activeDesc->mIoHandle); |
| 3943 | } |
| 3944 | } |
| 3945 | } |
| 3946 | |
Paul Wang | 5d7cdb5 | 2022-11-22 09:45:06 +0000 | [diff] [blame] | 3947 | status_t |
| 3948 | AudioPolicyManager::removeDevicesRoleForStrategy(product_strategy_t strategy, |
| 3949 | device_role_t role, |
| 3950 | const AudioDeviceTypeAddrVector &devices) { |
| 3951 | ALOGV("%s() strategy=%d role=%d %s", __func__, strategy, role, |
| 3952 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3953 | |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 3954 | if (!areAllDevicesSupported( |
| 3955 | devices, audio_is_output_device, __func__, /*matchAddress*/false)) { |
Paul Wang | 5d7cdb5 | 2022-11-22 09:45:06 +0000 | [diff] [blame] | 3956 | return BAD_VALUE; |
| 3957 | } |
| 3958 | status_t status = mEngine->removeDevicesRoleForStrategy(strategy, role, devices); |
| 3959 | if (status != NO_ERROR) { |
| 3960 | ALOGW("Engine could not remove devices %s for strategy %d role %d", |
| 3961 | dumpAudioDeviceTypeAddrVector(devices).c_str(), strategy, role); |
| 3962 | return status; |
| 3963 | } |
| 3964 | |
| 3965 | checkForDeviceAndOutputChanges(); |
| 3966 | |
| 3967 | bool forceVolumeReeval = false; |
| 3968 | // TODO(b/263479999): workaround for truncated touch sounds |
| 3969 | // to be removed when the problem is handled by system UI |
| 3970 | uint32_t delayMs = 0; |
| 3971 | if (strategy == mCommunnicationStrategy) { |
| 3972 | forceVolumeReeval = true; |
| 3973 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 3974 | updateInputRouting(); |
| 3975 | } |
| 3976 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
| 3977 | |
| 3978 | return NO_ERROR; |
| 3979 | } |
| 3980 | |
| 3981 | status_t AudioPolicyManager::clearDevicesRoleForStrategy(product_strategy_t strategy, |
| 3982 | device_role_t role) |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3983 | { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3984 | ALOGV("%s() strategy=%d role=%d", __func__, strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3985 | |
Paul Wang | 5d7cdb5 | 2022-11-22 09:45:06 +0000 | [diff] [blame] | 3986 | status_t status = mEngine->clearDevicesRoleForStrategy(strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3987 | if (status != NO_ERROR) { |
Eric Laurent | 9ae44f3 | 2022-12-14 16:17:02 +0100 | [diff] [blame] | 3988 | ALOGW_IF(status != NAME_NOT_FOUND, |
| 3989 | "Engine could not remove device role for strategy %d status %d", |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3990 | strategy, status); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3991 | return status; |
| 3992 | } |
| 3993 | |
| 3994 | checkForDeviceAndOutputChanges(); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3995 | |
| 3996 | bool forceVolumeReeval = false; |
| 3997 | // FIXME: workaround for truncated touch sounds |
| 3998 | // to be removed when the problem is handled by system UI |
| 3999 | uint32_t delayMs = 0; |
| 4000 | if (strategy == mCommunnicationStrategy) { |
| 4001 | forceVolumeReeval = true; |
| 4002 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 4003 | updateInputRouting(); |
| 4004 | } |
| 4005 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4006 | |
| 4007 | return NO_ERROR; |
| 4008 | } |
| 4009 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 4010 | status_t AudioPolicyManager::getDevicesForRoleAndStrategy(product_strategy_t strategy, |
| 4011 | device_role_t role, |
| 4012 | AudioDeviceTypeAddrVector &devices) { |
| 4013 | return mEngine->getDevicesForRoleAndStrategy(strategy, role, devices); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4014 | } |
| 4015 | |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4016 | status_t AudioPolicyManager::setDevicesRoleForCapturePreset( |
| 4017 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { |
| 4018 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, |
| 4019 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 4020 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 4021 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4022 | return BAD_VALUE; |
| 4023 | } |
| 4024 | status_t status = mEngine->setDevicesRoleForCapturePreset(audioSource, role, devices); |
| 4025 | ALOGW_IF(status != NO_ERROR, |
| 4026 | "Engine could not set preferred devices %s for audio source %d role %d", |
| 4027 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); |
| 4028 | |
| 4029 | return status; |
| 4030 | } |
| 4031 | |
| 4032 | status_t AudioPolicyManager::addDevicesRoleForCapturePreset( |
| 4033 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { |
| 4034 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, |
| 4035 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 4036 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 4037 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4038 | return BAD_VALUE; |
| 4039 | } |
| 4040 | status_t status = mEngine->addDevicesRoleForCapturePreset(audioSource, role, devices); |
| 4041 | ALOGW_IF(status != NO_ERROR, |
| 4042 | "Engine could not add preferred devices %s for audio source %d role %d", |
| 4043 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); |
| 4044 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4045 | updateInputRouting(); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4046 | return status; |
| 4047 | } |
| 4048 | |
| 4049 | status_t AudioPolicyManager::removeDevicesRoleForCapturePreset( |
| 4050 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) |
| 4051 | { |
| 4052 | ALOGV("%s() audioSource=%d role=%d devices=%s", __func__, audioSource, role, |
| 4053 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 4054 | |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 4055 | if (!areAllDevicesSupported( |
| 4056 | devices, audio_call_is_input_device, __func__, /*matchAddress*/false)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4057 | return BAD_VALUE; |
| 4058 | } |
| 4059 | |
| 4060 | status_t status = mEngine->removeDevicesRoleForCapturePreset( |
| 4061 | audioSource, role, devices); |
Eric Laurent | 9ae44f3 | 2022-12-14 16:17:02 +0100 | [diff] [blame] | 4062 | ALOGW_IF(status != NO_ERROR && status != NAME_NOT_FOUND, |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4063 | "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] | 4064 | if (status == NO_ERROR) { |
| 4065 | updateInputRouting(); |
| 4066 | } |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4067 | return status; |
| 4068 | } |
| 4069 | |
| 4070 | status_t AudioPolicyManager::clearDevicesRoleForCapturePreset(audio_source_t audioSource, |
| 4071 | device_role_t role) { |
| 4072 | ALOGV("%s() audioSource=%d role=%d", __func__, audioSource, role); |
| 4073 | |
| 4074 | status_t status = mEngine->clearDevicesRoleForCapturePreset(audioSource, role); |
Eric Laurent | 9ae44f3 | 2022-12-14 16:17:02 +0100 | [diff] [blame] | 4075 | ALOGW_IF(status != NO_ERROR && status != NAME_NOT_FOUND, |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4076 | "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] | 4077 | if (status == NO_ERROR) { |
| 4078 | updateInputRouting(); |
| 4079 | } |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4080 | return status; |
| 4081 | } |
| 4082 | |
| 4083 | status_t AudioPolicyManager::getDevicesForRoleAndCapturePreset( |
| 4084 | audio_source_t audioSource, device_role_t role, AudioDeviceTypeAddrVector &devices) { |
| 4085 | return mEngine->getDevicesForRoleAndCapturePreset(audioSource, role, devices); |
| 4086 | } |
| 4087 | |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4088 | status_t AudioPolicyManager::setUserIdDeviceAffinities(int userId, |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 4089 | const AudioDeviceTypeAddrVector& devices) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 4090 | ALOGV("%s() userId=%d num devices %zu", __func__, userId, devices.size()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4091 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
| 4092 | return BAD_VALUE; |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4093 | } |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4094 | status_t status = mPolicyMixes.setUserIdDeviceAffinities(userId, devices); |
| 4095 | if (status != NO_ERROR) { |
| 4096 | ALOGE("%s() could not set device affinity for userId %d", |
| 4097 | __FUNCTION__, userId); |
| 4098 | return status; |
| 4099 | } |
| 4100 | |
| 4101 | // reevaluate outputs for all devices |
| 4102 | checkForDeviceAndOutputChanges(); |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame^] | 4103 | changeOutputDevicesMuteState(devices); |
| 4104 | updateCallAndOutputRouting(false /* forceVolumeReeval */, 0 /* delayMs */, |
| 4105 | true /* skipDelays */); |
| 4106 | changeOutputDevicesMuteState(devices); |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4107 | |
| 4108 | return NO_ERROR; |
| 4109 | } |
| 4110 | |
| 4111 | status_t AudioPolicyManager::removeUserIdDeviceAffinities(int userId) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 4112 | ALOGV("%s() userId=%d", __FUNCTION__, userId); |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame^] | 4113 | AudioDeviceTypeAddrVector devices; |
| 4114 | mPolicyMixes.getDevicesForUserId(userId, devices); |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4115 | status_t status = mPolicyMixes.removeUserIdDeviceAffinities(userId); |
| 4116 | if (status != NO_ERROR) { |
| 4117 | ALOGE("%s() Could not remove all device affinities fo userId = %d", |
| 4118 | __FUNCTION__, userId); |
| 4119 | return status; |
| 4120 | } |
| 4121 | |
| 4122 | // reevaluate outputs for all devices |
| 4123 | checkForDeviceAndOutputChanges(); |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame^] | 4124 | changeOutputDevicesMuteState(devices); |
| 4125 | updateCallAndOutputRouting(false /* forceVolumeReeval */, 0 /* delayMs */, |
| 4126 | true /* skipDelays */); |
| 4127 | changeOutputDevicesMuteState(devices); |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4128 | |
| 4129 | return NO_ERROR; |
| 4130 | } |
| 4131 | |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4132 | void AudioPolicyManager::dump(String8 *dst) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4133 | { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4134 | dst->appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
Mikhail Naganov | 0dbe87b | 2021-12-01 02:03:31 +0000 | [diff] [blame] | 4135 | dst->appendFormat(" Primary Output I/O handle: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4136 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 4137 | std::string stateLiteral; |
| 4138 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4139 | dst->appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 4140 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 4141 | "communications", "media", "record", "dock", "system", |
| 4142 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 4143 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 4144 | 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] | 4145 | audio_policy_forced_cfg_t forceUseValue = mEngine->getForceUse(i); |
| 4146 | dst->appendFormat(" Force use for %s: %d", forceUses[i], forceUseValue); |
| 4147 | if (i == AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND && |
| 4148 | forceUseValue == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 4149 | dst->append(" (MANUAL: "); |
| 4150 | dumpManualSurroundFormats(dst); |
| 4151 | dst->append(")"); |
| 4152 | } |
| 4153 | dst->append("\n"); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 4154 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4155 | dst->appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 4156 | dst->appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
Mikhail Naganov | b3bcb4f | 2022-02-23 23:46:56 +0000 | [diff] [blame] | 4157 | dst->appendFormat(" Communication Strategy id: %d\n", mCommunnicationStrategy); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 4158 | dst->appendFormat(" Config source: %s\n", mConfig->getSource().c_str()); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4159 | |
Mikhail Naganov | 0f413b2 | 2021-12-02 05:29:27 +0000 | [diff] [blame] | 4160 | dst->append("\n"); |
| 4161 | mAvailableOutputDevices.dump(dst, String8("Available output"), 1); |
| 4162 | dst->append("\n"); |
| 4163 | mAvailableInputDevices.dump(dst, String8("Available input"), 1); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 4164 | mHwModules.dump(dst); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4165 | mOutputs.dump(dst); |
| 4166 | mInputs.dump(dst); |
Mikhail Naganov | 0f413b2 | 2021-12-02 05:29:27 +0000 | [diff] [blame] | 4167 | mEffects.dump(dst, 1); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4168 | mAudioPatches.dump(dst); |
| 4169 | mPolicyMixes.dump(dst); |
| 4170 | mAudioSources.dump(dst); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4171 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 4172 | dst->appendFormat(" AllowedCapturePolicies:\n"); |
| 4173 | for (auto& policy : mAllowedCapturePolicies) { |
| 4174 | dst->appendFormat(" - uid=%d flag_mask=%#x\n", policy.first, policy.second); |
| 4175 | } |
| 4176 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4177 | dst->appendFormat(" Preferred mixer audio configuration:\n"); |
| 4178 | for (const auto it : mPreferredMixerAttrInfos) { |
| 4179 | dst->appendFormat(" - device port id: %d\n", it.first); |
| 4180 | for (const auto preferredMixerInfoIt : it.second) { |
| 4181 | dst->appendFormat(" - strategy: %d; ", preferredMixerInfoIt.first); |
| 4182 | preferredMixerInfoIt.second->dump(dst); |
| 4183 | } |
| 4184 | } |
| 4185 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4186 | dst->appendFormat("\nPolicy Engine dump:\n"); |
| 4187 | mEngine->dump(dst); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4188 | } |
| 4189 | |
| 4190 | status_t AudioPolicyManager::dump(int fd) |
| 4191 | { |
| 4192 | String8 result; |
| 4193 | dump(&result); |
Andy Hung | bb54e20 | 2018-10-05 11:42:02 -0700 | [diff] [blame] | 4194 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4195 | return NO_ERROR; |
| 4196 | } |
| 4197 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 4198 | status_t AudioPolicyManager::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) |
| 4199 | { |
| 4200 | mAllowedCapturePolicies[uid] = capturePolicy; |
| 4201 | return NO_ERROR; |
| 4202 | } |
| 4203 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4204 | // This function checks for the parameters which can be offloaded. |
| 4205 | // This can be enhanced depending on the capability of the DSP and policy |
| 4206 | // of the system. |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4207 | audio_offload_mode_t AudioPolicyManager::getOffloadSupport(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4208 | { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4209 | ALOGV("%s: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4210 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4211 | __func__, offloadInfo.sample_rate, offloadInfo.channel_mask, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4212 | offloadInfo.format, |
| 4213 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 4214 | offloadInfo.has_video); |
| 4215 | |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4216 | if (!isOffloadPossible(offloadInfo)) { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4217 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4218 | } |
| 4219 | |
| 4220 | // See if there is a profile to support this. |
| 4221 | // AUDIO_DEVICE_NONE |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4222 | sp<IOProfile> profile = getProfileForOutput(DeviceVector() /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4223 | offloadInfo.sample_rate, |
| 4224 | offloadInfo.format, |
| 4225 | offloadInfo.channel_mask, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4226 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, |
| 4227 | true /* directOnly */); |
Eric Laurent | 9436544 | 2021-01-08 18:36:05 +0100 | [diff] [blame] | 4228 | ALOGV("%s: profile %sfound%s", __func__, profile != nullptr ? "" : "NOT ", |
| 4229 | (profile != nullptr && (profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) |
| 4230 | ? ", supports gapless" : ""); |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4231 | if (profile == nullptr) { |
| 4232 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
| 4233 | } |
| 4234 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) { |
| 4235 | return AUDIO_OFFLOAD_GAPLESS_SUPPORTED; |
| 4236 | } |
| 4237 | return AUDIO_OFFLOAD_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4238 | } |
| 4239 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4240 | bool AudioPolicyManager::isDirectOutputSupported(const audio_config_base_t& config, |
| 4241 | const audio_attributes_t& attributes) { |
| 4242 | audio_output_flags_t output_flags = AUDIO_OUTPUT_FLAG_NONE; |
François Gaffie | 58d4be5 | 2018-11-06 15:30:12 +0100 | [diff] [blame] | 4243 | audio_flags_to_audio_output_flags(attributes.flags, &output_flags); |
Dorin Drimus | 9901eb0 | 2022-01-14 11:36:51 +0000 | [diff] [blame] | 4244 | DeviceVector outputDevices = mEngine->getOutputDevicesForAttributes(attributes); |
| 4245 | sp<IOProfile> profile = getProfileForOutput(outputDevices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4246 | config.sample_rate, |
| 4247 | config.format, |
| 4248 | config.channel_mask, |
| 4249 | output_flags, |
| 4250 | true /* directOnly */); |
| 4251 | ALOGV("%s() profile %sfound with name: %s, " |
| 4252 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 4253 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 4254 | (profile != 0 ? profile->getTagName().c_str() : "null"), |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4255 | config.sample_rate, config.format, config.channel_mask, output_flags); |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 4256 | |
| 4257 | // also try the MSD module if compatible profile not found |
| 4258 | if (profile == nullptr) { |
| 4259 | profile = getMsdProfileForOutput(outputDevices, |
| 4260 | config.sample_rate, |
| 4261 | config.format, |
| 4262 | config.channel_mask, |
| 4263 | output_flags, |
| 4264 | true /* directOnly */); |
| 4265 | ALOGV("%s() MSD profile %sfound with name: %s, " |
| 4266 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 4267 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
| 4268 | (profile != 0 ? profile->getTagName().c_str() : "null"), |
| 4269 | config.sample_rate, config.format, config.channel_mask, output_flags); |
| 4270 | } |
| 4271 | return (profile != nullptr); |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4272 | } |
| 4273 | |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4274 | bool AudioPolicyManager::isOffloadPossible(const audio_offload_info_t &offloadInfo, |
| 4275 | bool durationIgnored) { |
| 4276 | if (mMasterMono) { |
| 4277 | return false; // no offloading if mono is set. |
| 4278 | } |
| 4279 | |
| 4280 | // Check if offload has been disabled |
| 4281 | if (property_get_bool("audio.offload.disable", false /* default_value */)) { |
| 4282 | ALOGV("%s: offload disabled by audio.offload.disable", __func__); |
| 4283 | return false; |
| 4284 | } |
| 4285 | |
| 4286 | // Check if stream type is music, then only allow offload as of now. |
| 4287 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 4288 | { |
| 4289 | ALOGV("%s: stream_type != MUSIC, returning false", __func__); |
| 4290 | return false; |
| 4291 | } |
| 4292 | |
| 4293 | //TODO: enable audio offloading with video when ready |
| 4294 | const bool allowOffloadWithVideo = |
| 4295 | property_get_bool("audio.offload.video", false /* default_value */); |
| 4296 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
| 4297 | ALOGV("%s: has_video == true, returning false", __func__); |
| 4298 | return false; |
| 4299 | } |
| 4300 | |
| 4301 | //If duration is less than minimum value defined in property, return false |
| 4302 | const int min_duration_secs = property_get_int32( |
| 4303 | "audio.offload.min.duration.secs", -1 /* default_value */); |
| 4304 | if (!durationIgnored) { |
| 4305 | if (min_duration_secs >= 0) { |
| 4306 | if (offloadInfo.duration_us < min_duration_secs * 1000000LL) { |
| 4307 | ALOGV("%s: Offload denied by duration < audio.offload.min.duration.secs(=%d)", |
| 4308 | __func__, min_duration_secs); |
| 4309 | return false; |
| 4310 | } |
| 4311 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 4312 | ALOGV("%s: Offload denied by duration < default min(=%u)", |
| 4313 | __func__, OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 4314 | return false; |
| 4315 | } |
| 4316 | } |
| 4317 | |
| 4318 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 4319 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 4320 | // detects there is an active non offloadable effect. |
| 4321 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 4322 | // This may prevent offloading in rare situations where effects are left active by apps |
| 4323 | // in the background. |
| 4324 | if (mEffects.isNonOffloadableEffectEnabled()) { |
| 4325 | return false; |
| 4326 | } |
| 4327 | |
| 4328 | return true; |
| 4329 | } |
| 4330 | |
| 4331 | audio_direct_mode_t AudioPolicyManager::getDirectPlaybackSupport(const audio_attributes_t *attr, |
| 4332 | const audio_config_t *config) { |
| 4333 | audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER; |
| 4334 | offloadInfo.format = config->format; |
| 4335 | offloadInfo.sample_rate = config->sample_rate; |
| 4336 | offloadInfo.channel_mask = config->channel_mask; |
| 4337 | offloadInfo.stream_type = mEngine->getStreamTypeForAttributes(*attr); |
| 4338 | offloadInfo.has_video = false; |
| 4339 | offloadInfo.is_streaming = false; |
| 4340 | const bool offloadPossible = isOffloadPossible(offloadInfo, true /*durationIgnored*/); |
| 4341 | |
| 4342 | audio_direct_mode_t directMode = AUDIO_DIRECT_NOT_SUPPORTED; |
| 4343 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 4344 | audio_flags_to_audio_output_flags(attr->flags, &flags); |
| 4345 | // only retain flags that will drive compressed offload or passthrough |
| 4346 | uint32_t relevantFlags = AUDIO_OUTPUT_FLAG_HW_AV_SYNC; |
| 4347 | if (offloadPossible) { |
| 4348 | relevantFlags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD; |
| 4349 | } |
| 4350 | flags = (audio_output_flags_t)((flags & relevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 4351 | |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4352 | DeviceVector engineOutputDevices = mEngine->getOutputDevicesForAttributes(*attr); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4353 | for (const auto& hwModule : mHwModules) { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4354 | DeviceVector outputDevices = engineOutputDevices; |
| 4355 | // the MSD module checks for different conditions and output devices |
| 4356 | if (strcmp(hwModule->getName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0) { |
| 4357 | if (!msdHasPatchesToAllDevices(engineOutputDevices.toTypeAddrVector())) { |
| 4358 | continue; |
| 4359 | } |
| 4360 | outputDevices = getMsdAudioOutDevices(); |
| 4361 | } |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4362 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
jiabin | c8f7dfc | 2022-01-06 18:42:08 +0000 | [diff] [blame] | 4363 | if (!curProfile->isCompatibleProfile(outputDevices, |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4364 | config->sample_rate, nullptr /*updatedSamplingRate*/, |
| 4365 | config->format, nullptr /*updatedFormat*/, |
| 4366 | config->channel_mask, nullptr /*updatedChannelMask*/, |
| 4367 | flags)) { |
| 4368 | continue; |
| 4369 | } |
| 4370 | // reject profiles not corresponding to a device currently available |
| 4371 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
| 4372 | continue; |
| 4373 | } |
| 4374 | if ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) |
| 4375 | != AUDIO_OUTPUT_FLAG_NONE) { |
jiabin | c6132d6 | 2022-01-01 07:36:31 +0000 | [diff] [blame] | 4376 | if ((directMode & AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED) |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4377 | != AUDIO_DIRECT_NOT_SUPPORTED) { |
| 4378 | // Already reports offload gapless supported. No need to report offload support. |
| 4379 | continue; |
| 4380 | } |
| 4381 | if ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) |
| 4382 | != AUDIO_OUTPUT_FLAG_NONE) { |
| 4383 | // If offload gapless is reported, no need to report offload support. |
| 4384 | directMode = (audio_direct_mode_t) ((directMode & |
| 4385 | ~AUDIO_DIRECT_OFFLOAD_SUPPORTED) | |
| 4386 | AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED); |
| 4387 | } else { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4388 | directMode = (audio_direct_mode_t)(directMode | AUDIO_DIRECT_OFFLOAD_SUPPORTED); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4389 | } |
| 4390 | } else { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4391 | directMode = (audio_direct_mode_t) (directMode | AUDIO_DIRECT_BITSTREAM_SUPPORTED); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4392 | } |
| 4393 | } |
| 4394 | } |
| 4395 | return directMode; |
| 4396 | } |
| 4397 | |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4398 | status_t AudioPolicyManager::getDirectProfilesForAttributes(const audio_attributes_t* attr, |
| 4399 | AudioProfileVector& audioProfilesVector) { |
Dorin Drimus | 1711263 | 2022-09-23 15:28:51 +0000 | [diff] [blame] | 4400 | if (mEffects.isNonOffloadableEffectEnabled()) { |
| 4401 | return OK; |
| 4402 | } |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 4403 | DeviceVector devices; |
| 4404 | status_t status = getDevicesForAttributes(*attr, devices, false /* forVolume */); |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4405 | if (status != OK) { |
| 4406 | return status; |
| 4407 | } |
| 4408 | ALOGV("%s: found %zu output devices for attributes.", __func__, devices.size()); |
| 4409 | if (devices.empty()) { |
| 4410 | return OK; // no output devices for the attributes |
| 4411 | } |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 4412 | return getProfilesForDevices(devices, audioProfilesVector, |
| 4413 | AUDIO_OUTPUT_FLAG_DIRECT /*flags*/, false /*isInput*/); |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4414 | } |
| 4415 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4416 | status_t AudioPolicyManager::getSupportedMixerAttributes( |
| 4417 | audio_port_handle_t portId, std::vector<audio_mixer_attributes_t> &mixerAttrs) { |
| 4418 | ALOGV("%s, portId=%d", __func__, portId); |
| 4419 | sp<DeviceDescriptor> deviceDescriptor = mAvailableOutputDevices.getDeviceFromId(portId); |
| 4420 | if (deviceDescriptor == nullptr) { |
| 4421 | ALOGE("%s the requested device is currently unavailable", __func__); |
| 4422 | return BAD_VALUE; |
| 4423 | } |
jiabin | 96daffc | 2023-05-11 17:51:55 +0000 | [diff] [blame] | 4424 | if (!audio_is_usb_out_device(deviceDescriptor->type())) { |
| 4425 | ALOGE("%s the requested device(type=%#x) is not usb device", __func__, |
| 4426 | deviceDescriptor->type()); |
| 4427 | return BAD_VALUE; |
| 4428 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4429 | for (const auto& hwModule : mHwModules) { |
| 4430 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
| 4431 | if (curProfile->supportsDevice(deviceDescriptor)) { |
| 4432 | curProfile->toSupportedMixerAttributes(&mixerAttrs); |
| 4433 | } |
| 4434 | } |
| 4435 | } |
| 4436 | return NO_ERROR; |
| 4437 | } |
| 4438 | |
| 4439 | status_t AudioPolicyManager::setPreferredMixerAttributes( |
| 4440 | const audio_attributes_t *attr, |
| 4441 | audio_port_handle_t portId, |
| 4442 | uid_t uid, |
| 4443 | const audio_mixer_attributes_t *mixerAttributes) { |
| 4444 | ALOGV("%s, attr=%s, mixerAttributes={format=%#x, channelMask=%#x, samplingRate=%u, " |
| 4445 | "mixerBehavior=%d}, uid=%d, portId=%u", |
| 4446 | __func__, toString(*attr).c_str(), mixerAttributes->config.format, |
| 4447 | mixerAttributes->config.channel_mask, mixerAttributes->config.sample_rate, |
| 4448 | mixerAttributes->mixer_behavior, uid, portId); |
| 4449 | if (attr->usage != AUDIO_USAGE_MEDIA) { |
| 4450 | ALOGE("%s failed, only media is allowed, the given usage is %d", __func__, attr->usage); |
| 4451 | return BAD_VALUE; |
| 4452 | } |
| 4453 | sp<DeviceDescriptor> deviceDescriptor = mAvailableOutputDevices.getDeviceFromId(portId); |
| 4454 | if (deviceDescriptor == nullptr) { |
| 4455 | ALOGE("%s the requested device is currently unavailable", __func__); |
| 4456 | return BAD_VALUE; |
| 4457 | } |
| 4458 | if (!audio_is_usb_out_device(deviceDescriptor->type())) { |
| 4459 | ALOGE("%s(%d), type=%d, is not a usb output device", |
| 4460 | __func__, portId, deviceDescriptor->type()); |
| 4461 | return BAD_VALUE; |
| 4462 | } |
| 4463 | |
| 4464 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 4465 | audio_flags_to_audio_output_flags(attr->flags, &flags); |
| 4466 | flags = (audio_output_flags_t) (flags | |
| 4467 | audio_output_flags_from_mixer_behavior(mixerAttributes->mixer_behavior)); |
| 4468 | sp<IOProfile> profile = nullptr; |
| 4469 | DeviceVector devices(deviceDescriptor); |
| 4470 | for (const auto& hwModule : mHwModules) { |
| 4471 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
| 4472 | if (curProfile->hasDynamicAudioProfile() |
| 4473 | && curProfile->isCompatibleProfile(devices, |
| 4474 | mixerAttributes->config.sample_rate, |
| 4475 | nullptr /*updatedSamplingRate*/, |
| 4476 | mixerAttributes->config.format, |
| 4477 | nullptr /*updatedFormat*/, |
| 4478 | mixerAttributes->config.channel_mask, |
| 4479 | nullptr /*updatedChannelMask*/, |
| 4480 | flags, |
| 4481 | false /*exactMatchRequiredForInputFlags*/)) { |
| 4482 | profile = curProfile; |
| 4483 | break; |
| 4484 | } |
| 4485 | } |
| 4486 | } |
| 4487 | if (profile == nullptr) { |
| 4488 | ALOGE("%s, there is no compatible profile found", __func__); |
| 4489 | return BAD_VALUE; |
| 4490 | } |
| 4491 | |
| 4492 | sp<PreferredMixerAttributesInfo> mixerAttrInfo = |
| 4493 | sp<PreferredMixerAttributesInfo>::make( |
| 4494 | uid, portId, profile, flags, *mixerAttributes); |
| 4495 | const product_strategy_t strategy = mEngine->getProductStrategyForAttributes(*attr); |
| 4496 | mPreferredMixerAttrInfos[portId][strategy] = mixerAttrInfo; |
| 4497 | |
| 4498 | // If 1) there is any client from the preferred mixer configuration owner that is currently |
| 4499 | // active and matches the strategy and 2) current output is on the preferred device and the |
| 4500 | // mixer configuration doesn't match the preferred one, reopen output with preferred mixer |
| 4501 | // configuration. |
| 4502 | std::vector<audio_io_handle_t> outputsToReopen; |
| 4503 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4504 | const auto output = mOutputs.valueAt(i); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 4505 | if (output->mProfile == profile && output->devices().onlyContainsDevice(deviceDescriptor)) { |
| 4506 | if (output->isConfigurationMatched(mixerAttributes->config, flags)) { |
| 4507 | output->mUsePreferredMixerAttributes = true; |
| 4508 | } else { |
| 4509 | for (const auto &client: output->getActiveClients()) { |
| 4510 | if (client->uid() == uid && client->strategy() == strategy) { |
| 4511 | client->setIsInvalid(); |
| 4512 | outputsToReopen.push_back(output->mIoHandle); |
| 4513 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4514 | } |
| 4515 | } |
| 4516 | } |
| 4517 | } |
| 4518 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4519 | config.sample_rate = mixerAttributes->config.sample_rate; |
| 4520 | config.channel_mask = mixerAttributes->config.channel_mask; |
| 4521 | config.format = mixerAttributes->config.format; |
| 4522 | for (const auto output : outputsToReopen) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 4523 | sp<SwAudioOutputDescriptor> desc = |
| 4524 | reopenOutput(mOutputs.valueFor(output), &config, flags, __func__); |
| 4525 | if (desc == nullptr) { |
| 4526 | ALOGE("%s, failed to reopen output with preferred mixer attributes", __func__); |
| 4527 | continue; |
| 4528 | } |
| 4529 | desc->mUsePreferredMixerAttributes = true; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4530 | } |
| 4531 | |
| 4532 | return NO_ERROR; |
| 4533 | } |
| 4534 | |
| 4535 | sp<PreferredMixerAttributesInfo> AudioPolicyManager::getPreferredMixerAttributesInfo( |
jiabin | d9a58d3 | 2023-06-01 17:57:30 +0000 | [diff] [blame] | 4536 | audio_port_handle_t devicePortId, |
| 4537 | product_strategy_t strategy, |
| 4538 | bool activeBitPerfectPreferred) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4539 | auto it = mPreferredMixerAttrInfos.find(devicePortId); |
| 4540 | if (it == mPreferredMixerAttrInfos.end()) { |
| 4541 | return nullptr; |
| 4542 | } |
jiabin | d9a58d3 | 2023-06-01 17:57:30 +0000 | [diff] [blame] | 4543 | if (activeBitPerfectPreferred) { |
| 4544 | for (auto [strategy, info] : it->second) { |
| 4545 | if ((info->getFlags() & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != AUDIO_OUTPUT_FLAG_NONE |
| 4546 | && info->getActiveClientCount() != 0) { |
| 4547 | return info; |
| 4548 | } |
| 4549 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4550 | } |
jiabin | d9a58d3 | 2023-06-01 17:57:30 +0000 | [diff] [blame] | 4551 | auto strategyMatchedMixerAttrInfoIt = it->second.find(strategy); |
| 4552 | return strategyMatchedMixerAttrInfoIt == it->second.end() |
| 4553 | ? nullptr : strategyMatchedMixerAttrInfoIt->second; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4554 | } |
| 4555 | |
| 4556 | status_t AudioPolicyManager::getPreferredMixerAttributes( |
| 4557 | const audio_attributes_t *attr, |
| 4558 | audio_port_handle_t portId, |
| 4559 | audio_mixer_attributes_t* mixerAttributes) { |
| 4560 | sp<PreferredMixerAttributesInfo> info = getPreferredMixerAttributesInfo( |
| 4561 | portId, mEngine->getProductStrategyForAttributes(*attr)); |
| 4562 | if (info == nullptr) { |
| 4563 | return NAME_NOT_FOUND; |
| 4564 | } |
| 4565 | *mixerAttributes = info->getMixerAttributes(); |
| 4566 | return NO_ERROR; |
| 4567 | } |
| 4568 | |
| 4569 | status_t AudioPolicyManager::clearPreferredMixerAttributes(const audio_attributes_t *attr, |
| 4570 | audio_port_handle_t portId, |
| 4571 | uid_t uid) { |
| 4572 | const product_strategy_t strategy = mEngine->getProductStrategyForAttributes(*attr); |
| 4573 | const auto preferredMixerAttrInfo = getPreferredMixerAttributesInfo(portId, strategy); |
| 4574 | if (preferredMixerAttrInfo == nullptr) { |
| 4575 | return NAME_NOT_FOUND; |
| 4576 | } |
| 4577 | if (preferredMixerAttrInfo->getUid() != uid) { |
| 4578 | ALOGE("%s, requested uid=%d, owned uid=%d", |
| 4579 | __func__, uid, preferredMixerAttrInfo->getUid()); |
| 4580 | return PERMISSION_DENIED; |
| 4581 | } |
| 4582 | mPreferredMixerAttrInfos[portId].erase(strategy); |
| 4583 | if (mPreferredMixerAttrInfos[portId].empty()) { |
| 4584 | mPreferredMixerAttrInfos.erase(portId); |
| 4585 | } |
| 4586 | |
| 4587 | // Reconfig existing output |
| 4588 | std::vector<audio_io_handle_t> potentialOutputsToReopen; |
| 4589 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4590 | if (mOutputs.valueAt(i)->mProfile == preferredMixerAttrInfo->getProfile()) { |
| 4591 | potentialOutputsToReopen.push_back(mOutputs.keyAt(i)); |
| 4592 | } |
| 4593 | } |
| 4594 | for (const auto output : potentialOutputsToReopen) { |
| 4595 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 4596 | if (desc->isConfigurationMatched(preferredMixerAttrInfo->getConfigBase(), |
| 4597 | preferredMixerAttrInfo->getFlags())) { |
| 4598 | reopenOutput(desc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 4599 | } |
| 4600 | } |
| 4601 | return NO_ERROR; |
| 4602 | } |
| 4603 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4604 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 4605 | audio_port_type_t type, |
| 4606 | unsigned int *num_ports, |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4607 | struct audio_port_v7 *ports, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4608 | unsigned int *generation) |
| 4609 | { |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4610 | if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) || |
| 4611 | generation == nullptr) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4612 | return BAD_VALUE; |
| 4613 | } |
| 4614 | 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] | 4615 | if (ports == nullptr) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4616 | *num_ports = 0; |
| 4617 | } |
| 4618 | |
| 4619 | size_t portsWritten = 0; |
| 4620 | size_t portsMax = *num_ports; |
| 4621 | *num_ports = 0; |
| 4622 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4623 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 4624 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4625 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4626 | for (const auto& dev : mAvailableOutputDevices) { |
| 4627 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4628 | continue; |
| 4629 | } |
| 4630 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4631 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4632 | } |
| 4633 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4634 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4635 | } |
| 4636 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4637 | for (const auto& dev : mAvailableInputDevices) { |
| 4638 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4639 | continue; |
| 4640 | } |
| 4641 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4642 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4643 | } |
| 4644 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4645 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4646 | } |
| 4647 | } |
| 4648 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 4649 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 4650 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 4651 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 4652 | } |
| 4653 | *num_ports += mInputs.size(); |
| 4654 | } |
| 4655 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4656 | size_t numOutputs = 0; |
| 4657 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4658 | if (!mOutputs[i]->isDuplicated()) { |
| 4659 | numOutputs++; |
| 4660 | if (portsWritten < portsMax) { |
| 4661 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 4662 | } |
| 4663 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4664 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4665 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4666 | } |
| 4667 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4668 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4669 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 4670 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4671 | return NO_ERROR; |
| 4672 | } |
| 4673 | |
Mikhail Naganov | 5edc5ed | 2023-03-23 14:52:15 -0700 | [diff] [blame] | 4674 | status_t AudioPolicyManager::listDeclaredDevicePorts(media::AudioPortRole role, |
| 4675 | std::vector<media::AudioPortFw>* _aidl_return) { |
| 4676 | auto pushPort = [&](const sp<DeviceDescriptor>& dev) -> status_t { |
| 4677 | audio_port_v7 port; |
| 4678 | dev->toAudioPort(&port); |
| 4679 | auto aidlPort = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_v7_AudioPortFw(port)); |
| 4680 | _aidl_return->push_back(std::move(aidlPort)); |
| 4681 | return OK; |
| 4682 | }; |
| 4683 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 4684 | for (const auto& module : mHwModules) { |
Mikhail Naganov | 5edc5ed | 2023-03-23 14:52:15 -0700 | [diff] [blame] | 4685 | for (const auto& dev : module->getDeclaredDevices()) { |
| 4686 | if (role == media::AudioPortRole::NONE || |
| 4687 | ((role == media::AudioPortRole::SOURCE) |
| 4688 | == audio_is_input_device(dev->type()))) { |
| 4689 | RETURN_STATUS_IF_ERROR(pushPort(dev)); |
| 4690 | } |
| 4691 | } |
| 4692 | } |
| 4693 | return OK; |
| 4694 | } |
| 4695 | |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4696 | status_t AudioPolicyManager::getAudioPort(struct audio_port_v7 *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4697 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 4698 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 4699 | return BAD_VALUE; |
| 4700 | } |
| 4701 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 4702 | if (dev != 0) { |
| 4703 | dev->toAudioPort(port); |
| 4704 | return NO_ERROR; |
| 4705 | } |
| 4706 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 4707 | if (dev != 0) { |
| 4708 | dev->toAudioPort(port); |
| 4709 | return NO_ERROR; |
| 4710 | } |
| 4711 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 4712 | if (out != 0) { |
| 4713 | out->toAudioPort(port); |
| 4714 | return NO_ERROR; |
| 4715 | } |
| 4716 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 4717 | if (in != 0) { |
| 4718 | in->toAudioPort(port); |
| 4719 | return NO_ERROR; |
| 4720 | } |
| 4721 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4722 | } |
| 4723 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4724 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 4725 | audio_patch_handle_t *handle, |
| 4726 | uid_t uid) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4727 | { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4728 | ALOGV("%s", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4729 | if (handle == NULL || patch == NULL) { |
| 4730 | return BAD_VALUE; |
| 4731 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4732 | 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] | 4733 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4734 | return BAD_VALUE; |
| 4735 | } |
| 4736 | // only one source per audio patch supported for now |
| 4737 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4738 | return INVALID_OPERATION; |
| 4739 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4740 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4741 | return INVALID_OPERATION; |
| 4742 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4743 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4744 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 4745 | return INVALID_OPERATION; |
| 4746 | } |
| 4747 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4748 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4749 | sp<DeviceDescriptor> srcDevice = mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 4750 | sp<DeviceDescriptor> sinkDevice = mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id); |
| 4751 | if (srcDevice == nullptr || sinkDevice == nullptr) { |
| 4752 | ALOGW("%s could not create patch, invalid sink and/or source device(s)", __func__); |
| 4753 | return BAD_VALUE; |
| 4754 | } |
| 4755 | ALOGV("%s between source %s and sink %s", __func__, |
| 4756 | srcDevice->toString().c_str(), sinkDevice->toString().c_str()); |
| 4757 | audio_port_handle_t portId = PolicyAudioPort::getNextUniqueId(); |
| 4758 | // Default attributes, default volume priority, not to infer with non raw audio patches. |
| 4759 | audio_attributes_t attributes = attributes_initializer(AUDIO_USAGE_MEDIA); |
| 4760 | const struct audio_port_config *source = &patch->sources[0]; |
| 4761 | sp<SourceClientDescriptor> sourceDesc = |
| 4762 | new InternalSourceClientDescriptor( |
| 4763 | portId, uid, attributes, *source, srcDevice, sinkDevice, |
| 4764 | mEngine->getProductStrategyForAttributes(attributes), toVolumeSource(attributes)); |
| 4765 | |
| 4766 | status_t status = |
| 4767 | connectAudioSourceToSink(sourceDesc, sinkDevice, patch, *handle, uid, 0 /* delayMs */); |
| 4768 | |
| 4769 | if (status != NO_ERROR) { |
| 4770 | return INVALID_OPERATION; |
| 4771 | } |
| 4772 | mAudioSources.add(portId, sourceDesc); |
| 4773 | return NO_ERROR; |
| 4774 | } |
| 4775 | |
| 4776 | status_t AudioPolicyManager::connectAudioSourceToSink( |
| 4777 | const sp<SourceClientDescriptor>& sourceDesc, const sp<DeviceDescriptor> &sinkDevice, |
| 4778 | const struct audio_patch *patch, |
| 4779 | audio_patch_handle_t &handle, |
| 4780 | uid_t uid, uint32_t delayMs) |
| 4781 | { |
| 4782 | status_t status = createAudioPatchInternal(patch, &handle, uid, delayMs, sourceDesc); |
| 4783 | if (status != NO_ERROR || mAudioPatches.indexOfKey(handle) < 0) { |
| 4784 | ALOGW("%s patch panel could not connect device patch, error %d", __func__, status); |
| 4785 | return INVALID_OPERATION; |
| 4786 | } |
| 4787 | sourceDesc->connect(handle, sinkDevice); |
| 4788 | if (isMsdPatch(handle)) { |
| 4789 | return NO_ERROR; |
| 4790 | } |
| 4791 | // SW Bridge? (@todo: HW bridge, keep track of HwOutput for device selection "reconsideration") |
| 4792 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); |
| 4793 | ALOG_ASSERT(swOutput != nullptr, "%s: a swOutput shall always be associated", __func__); |
| 4794 | if (swOutput->getClient(sourceDesc->portId()) != nullptr) { |
| 4795 | ALOGW("%s source portId has already been attached to outputDesc", __func__); |
| 4796 | goto FailurePatchAdded; |
| 4797 | } |
| 4798 | status = swOutput->start(); |
| 4799 | if (status != NO_ERROR) { |
| 4800 | goto FailureSourceAdded; |
| 4801 | } |
| 4802 | swOutput->addClient(sourceDesc); |
| 4803 | status = startSource(swOutput, sourceDesc, &delayMs); |
| 4804 | if (status != NO_ERROR) { |
| 4805 | ALOGW("%s failed to start source, error %d", __FUNCTION__, status); |
| 4806 | goto FailureSourceActive; |
| 4807 | } |
| 4808 | if (delayMs != 0) { |
| 4809 | usleep(delayMs * 1000); |
| 4810 | } |
| 4811 | return NO_ERROR; |
| 4812 | |
| 4813 | FailureSourceActive: |
| 4814 | swOutput->stop(); |
| 4815 | releaseOutput(sourceDesc->portId()); |
| 4816 | FailureSourceAdded: |
| 4817 | sourceDesc->setSwOutput(nullptr); |
| 4818 | FailurePatchAdded: |
| 4819 | releaseAudioPatchInternal(handle); |
| 4820 | return INVALID_OPERATION; |
| 4821 | } |
| 4822 | |
| 4823 | status_t AudioPolicyManager::createAudioPatchInternal(const struct audio_patch *patch, |
| 4824 | audio_patch_handle_t *handle, |
| 4825 | uid_t uid, uint32_t delayMs, |
| 4826 | const sp<SourceClientDescriptor>& sourceDesc) |
| 4827 | { |
| 4828 | 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] | 4829 | sp<AudioPatch> patchDesc; |
| 4830 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 4831 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4832 | ALOGV("%s source id %d role %d type %d", __func__, patch->sources[0].id, |
| 4833 | patch->sources[0].role, |
| 4834 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4835 | #if LOG_NDEBUG == 0 |
| 4836 | for (size_t i = 0; i < patch->num_sinks; i++) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4837 | ALOGV("%s sink %zu: id %d role %d type %d", __func__ ,i, patch->sinks[i].id, |
| 4838 | patch->sinks[i].role, |
| 4839 | patch->sinks[i].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4840 | } |
| 4841 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4842 | |
| 4843 | if (index >= 0) { |
| 4844 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4845 | ALOGV("%s mUidCached %d patchDesc->mUid %d uid %d", |
| 4846 | __func__, mUidCached, patchDesc->getUid(), uid); |
| 4847 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4848 | return INVALID_OPERATION; |
| 4849 | } |
| 4850 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4851 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4852 | } |
| 4853 | |
| 4854 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4855 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4856 | if (outputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4857 | 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] | 4858 | return BAD_VALUE; |
| 4859 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4860 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 4861 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4862 | if (patchDesc != 0) { |
| 4863 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4864 | ALOGV("%s source id differs for patch current id %d new id %d", |
| 4865 | __func__, patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4866 | return BAD_VALUE; |
| 4867 | } |
| 4868 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4869 | DeviceVector devices; |
| 4870 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4871 | // Only support mix to devices connection |
| 4872 | // TODO add support for mix to mix connection |
| 4873 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4874 | ALOGV("%s source mix but sink is not a device", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4875 | return INVALID_OPERATION; |
| 4876 | } |
| 4877 | sp<DeviceDescriptor> devDesc = |
| 4878 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 4879 | if (devDesc == 0) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4880 | 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] | 4881 | return BAD_VALUE; |
| 4882 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4883 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4884 | if (!outputDesc->mProfile->isCompatibleProfile(DeviceVector(devDesc), |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4885 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4886 | NULL, // updatedSamplingRate |
| 4887 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4888 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4889 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4890 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4891 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4892 | ALOGV("%s profile not supported for device %08x", __func__, devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4893 | return INVALID_OPERATION; |
| 4894 | } |
| 4895 | devices.add(devDesc); |
| 4896 | } |
| 4897 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4898 | return INVALID_OPERATION; |
| 4899 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4900 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4901 | // TODO: reconfigure output format and channels here |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4902 | ALOGV("%s setting device %s on output %d", |
| 4903 | __func__, dumpDeviceTypes(devices.types()).c_str(), outputDesc->mIoHandle); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4904 | setOutputDevices(outputDesc, devices, true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4905 | index = mAudioPatches.indexOfKey(*handle); |
| 4906 | if (index >= 0) { |
| 4907 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4908 | ALOGW("%s setOutputDevice() did not reuse the patch provided", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4909 | } |
| 4910 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4911 | patchDesc->setUid(uid); |
| 4912 | ALOGV("%s success", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4913 | } else { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4914 | ALOGW("%s setOutputDevice() failed to create a patch", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4915 | return INVALID_OPERATION; |
| 4916 | } |
| 4917 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 4918 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 4919 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4920 | // only one sink supported when connecting an input device to a mix |
| 4921 | if (patch->num_sinks > 1) { |
| 4922 | return INVALID_OPERATION; |
| 4923 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4924 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4925 | if (inputDesc == NULL) { |
| 4926 | return BAD_VALUE; |
| 4927 | } |
| 4928 | if (patchDesc != 0) { |
| 4929 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 4930 | return BAD_VALUE; |
| 4931 | } |
| 4932 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4933 | sp<DeviceDescriptor> device = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4934 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4935 | if (device == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4936 | return BAD_VALUE; |
| 4937 | } |
| 4938 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4939 | if (!inputDesc->mProfile->isCompatibleProfile(DeviceVector(device), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4940 | patch->sinks[0].sample_rate, |
| 4941 | NULL, /*updatedSampleRate*/ |
| 4942 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4943 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4944 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4945 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4946 | // FIXME for the parameter type, |
| 4947 | // and the NONE |
| 4948 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 4949 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4950 | return INVALID_OPERATION; |
| 4951 | } |
| 4952 | // TODO: reconfigure output format and channels here |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4953 | ALOGV("%s setting device %s on output %d", __func__, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4954 | device->toString().c_str(), inputDesc->mIoHandle); |
| 4955 | setInputDevice(inputDesc->mIoHandle, device, true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4956 | index = mAudioPatches.indexOfKey(*handle); |
| 4957 | if (index >= 0) { |
| 4958 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4959 | ALOGW("%s setInputDevice() did not reuse the patch provided", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4960 | } |
| 4961 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4962 | patchDesc->setUid(uid); |
| 4963 | ALOGV("%s success", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4964 | } else { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4965 | ALOGW("%s setInputDevice() failed to create a patch", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4966 | return INVALID_OPERATION; |
| 4967 | } |
| 4968 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 4969 | // device to device connection |
| 4970 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4971 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4972 | return BAD_VALUE; |
| 4973 | } |
| 4974 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4975 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4976 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4977 | if (srcDevice == 0) { |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 4978 | return BAD_VALUE; |
| 4979 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4980 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4981 | //update source and sink with our own data as the data passed in the patch may |
| 4982 | // be incomplete. |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4983 | PatchBuilder patchBuilder; |
| 4984 | audio_port_config sourcePortConfig = {}; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 4985 | |
| 4986 | // if first sink is to MSD, establish single MSD patch |
| 4987 | if (getMsdAudioOutDevices().contains( |
| 4988 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id))) { |
| 4989 | ALOGV("%s patching to MSD", __FUNCTION__); |
| 4990 | patchBuilder = buildMsdPatch(false /*msdIsSource*/, srcDevice); |
| 4991 | goto installPatch; |
| 4992 | } |
| 4993 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4994 | srcDevice->toAudioPortConfig(&sourcePortConfig, &patch->sources[0]); |
| 4995 | patchBuilder.addSource(sourcePortConfig); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4996 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4997 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4998 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4999 | ALOGV("%s source device but one sink is not a device", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5000 | return INVALID_OPERATION; |
| 5001 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5002 | sp<DeviceDescriptor> sinkDevice = |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5003 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5004 | if (sinkDevice == 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5005 | return BAD_VALUE; |
| 5006 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5007 | audio_port_config sinkPortConfig = {}; |
| 5008 | sinkDevice->toAudioPortConfig(&sinkPortConfig, &patch->sinks[i]); |
| 5009 | patchBuilder.addSink(sinkPortConfig); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5010 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5011 | // Whatever Sw or Hw bridge, we do attach an SwOutput to an Audio Source for |
| 5012 | // volume management purpose (tracking activity) |
| 5013 | // In case of Hw bridge, it is a Work Around. The mixPort used is the one declared |
| 5014 | // in config XML to reach the sink so that is can be declared as available. |
| 5015 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 5016 | sp<SwAudioOutputDescriptor> outputDesc; |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5017 | if (!sourceDesc->isInternal()) { |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5018 | // take care of dynamic routing for SwOutput selection, |
| 5019 | audio_attributes_t attributes = sourceDesc->attributes(); |
| 5020 | audio_stream_type_t stream = sourceDesc->stream(); |
| 5021 | audio_attributes_t resultAttr; |
| 5022 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 5023 | config.sample_rate = sourceDesc->config().sample_rate; |
François Gaffie | 2a24db4 | 2022-04-04 16:45:02 +0200 | [diff] [blame] | 5024 | audio_channel_mask_t sourceMask = sourceDesc->config().channel_mask; |
| 5025 | config.channel_mask = |
| 5026 | (audio_channel_mask_get_representation(sourceMask) |
| 5027 | == AUDIO_CHANNEL_REPRESENTATION_INDEX) ? sourceMask |
| 5028 | : audio_channel_mask_in_to_out(sourceMask); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5029 | config.format = sourceDesc->config().format; |
| 5030 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 5031 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 5032 | bool isRequestedDeviceForExclusiveUse = false; |
| 5033 | output_type_t outputType; |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 5034 | bool isSpatialized; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 5035 | bool isBitPerfect; |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5036 | getOutputForAttrInt(&resultAttr, &output, AUDIO_SESSION_NONE, &attributes, |
| 5037 | &stream, sourceDesc->uid(), &config, &flags, |
| 5038 | &selectedDeviceId, &isRequestedDeviceForExclusiveUse, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 5039 | nullptr, &outputType, &isSpatialized, &isBitPerfect); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5040 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 5041 | ALOGV("%s no output for device %s", |
| 5042 | __FUNCTION__, sinkDevice->toString().c_str()); |
| 5043 | return INVALID_OPERATION; |
| 5044 | } |
| 5045 | outputDesc = mOutputs.valueFor(output); |
| 5046 | if (outputDesc->isDuplicated()) { |
| 5047 | ALOGE("%s output is duplicated", __func__); |
| 5048 | return INVALID_OPERATION; |
| 5049 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5050 | bool closeOutput = outputDesc->mDirectOpenCount != 0; |
| 5051 | sourceDesc->setSwOutput(outputDesc, closeOutput); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5052 | } else { |
| 5053 | // Same for "raw patches" aka created from createAudioPatch API |
| 5054 | SortedVector<audio_io_handle_t> outputs = |
| 5055 | getOutputsForDevices(DeviceVector(sinkDevice), mOutputs); |
| 5056 | // if the sink device is reachable via an opened output stream, request to |
| 5057 | // go via this output stream by adding a second source to the patch |
| 5058 | // description |
| 5059 | output = selectOutput(outputs); |
| 5060 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 5061 | ALOGE("%s no output available for internal patch sink", __func__); |
| 5062 | return INVALID_OPERATION; |
| 5063 | } |
| 5064 | outputDesc = mOutputs.valueFor(output); |
| 5065 | if (outputDesc->isDuplicated()) { |
| 5066 | ALOGV("%s output for device %s is duplicated", |
| 5067 | __func__, sinkDevice->toString().c_str()); |
| 5068 | return INVALID_OPERATION; |
| 5069 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5070 | sourceDesc->setSwOutput(outputDesc, /* closeOutput= */ false); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5071 | } |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 5072 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 5073 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 5074 | // - audio HAL version is < 3.0 |
Francois Gaffie | 99896da | 2018-04-09 11:05:33 +0200 | [diff] [blame] | 5075 | // - 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] | 5076 | // - called from startAudioSource (aka sourceDesc is not internal) and source device |
| 5077 | // does not have a gain controller |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5078 | if (!srcDevice->hasSameHwModuleAs(sinkDevice) || |
| 5079 | (srcDevice->getModuleVersionMajor() < 3) || |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5080 | !srcDevice->getModule()->supportsPatch(srcDevice, sinkDevice) || |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5081 | (!sourceDesc->isInternal() && |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5082 | srcDevice->getAudioPort()->getGains().size() == 0)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 5083 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5084 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5085 | return INVALID_OPERATION; |
| 5086 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5087 | sourceDesc->setUseSwBridge(); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5088 | if (outputDesc != nullptr) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5089 | audio_port_config srcMixPortConfig = {}; |
Ytai Ben-Tsvi | c9d2a91 | 2021-11-22 16:43:09 -0800 | [diff] [blame] | 5090 | outputDesc->toAudioPortConfig(&srcMixPortConfig, nullptr); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5091 | // for volume control, we may need a valid stream |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 5092 | srcMixPortConfig.ext.mix.usecase.stream = |
| 5093 | (!sourceDesc->isInternal() || isCallTxAudioSource(sourceDesc)) ? |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5094 | mEngine->getStreamTypeForAttributes(sourceDesc->attributes()) : |
| 5095 | AUDIO_STREAM_PATCH; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5096 | patchBuilder.addSource(srcMixPortConfig); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5097 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5098 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5099 | } |
| 5100 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 5101 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 5102 | installPatch: |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5103 | status_t status = installPatch( |
| 5104 | __func__, index, handle, patchBuilder.patch(), delayMs, uid, &patchDesc); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5105 | if (status != NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5106 | 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] | 5107 | return INVALID_OPERATION; |
| 5108 | } |
| 5109 | } else { |
| 5110 | return BAD_VALUE; |
| 5111 | } |
| 5112 | } else { |
| 5113 | return BAD_VALUE; |
| 5114 | } |
| 5115 | return NO_ERROR; |
| 5116 | } |
| 5117 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5118 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, uid_t uid) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5119 | { |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5120 | ALOGV("%s patch %d", __func__, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5121 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 5122 | |
| 5123 | if (index < 0) { |
| 5124 | return BAD_VALUE; |
| 5125 | } |
| 5126 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5127 | ALOGV("%s() mUidCached %d patchDesc->mUid %d uid %d", |
| 5128 | __func__, mUidCached, patchDesc->getUid(), uid); |
| 5129 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5130 | return INVALID_OPERATION; |
| 5131 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5132 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 5133 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 5134 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5135 | if (sourceDesc != nullptr && sourceDesc->getPatchHandle() == handle) { |
| 5136 | portId = sourceDesc->portId(); |
| 5137 | break; |
| 5138 | } |
| 5139 | } |
| 5140 | return portId != AUDIO_PORT_HANDLE_NONE ? |
| 5141 | stopAudioSource(portId) : releaseAudioPatchInternal(handle); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5142 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5143 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5144 | status_t AudioPolicyManager::releaseAudioPatchInternal(audio_patch_handle_t handle, |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5145 | uint32_t delayMs, |
| 5146 | const sp<SourceClientDescriptor>& sourceDesc) |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5147 | { |
| 5148 | ALOGV("%s patch %d", __func__, handle); |
| 5149 | if (mAudioPatches.indexOfKey(handle) < 0) { |
| 5150 | ALOGE("%s: no patch found with handle=%d", __func__, handle); |
| 5151 | return BAD_VALUE; |
| 5152 | } |
| 5153 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5154 | struct audio_patch *patch = &patchDesc->mPatch; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5155 | patchDesc->setUid(mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5156 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5157 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5158 | if (outputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5159 | 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] | 5160 | return BAD_VALUE; |
| 5161 | } |
| 5162 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5163 | setOutputDevices(outputDesc, |
| 5164 | getNewOutputDevices(outputDesc, true /*fromCache*/), |
| 5165 | true, |
| 5166 | 0, |
| 5167 | NULL); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5168 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 5169 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5170 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5171 | if (inputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5172 | 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] | 5173 | return BAD_VALUE; |
| 5174 | } |
| 5175 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 5176 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5177 | true, |
| 5178 | NULL); |
| 5179 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5180 | status_t status = |
| 5181 | mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); |
| 5182 | ALOGV("%s patch panel returned %d patchHandle %d", |
| 5183 | __func__, status, patchDesc->getAfHandle()); |
| 5184 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5185 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5186 | mpClientInterface->onAudioPatchListUpdate(); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5187 | // SW or HW Bridge |
| 5188 | sp<SwAudioOutputDescriptor> outputDesc = nullptr; |
| 5189 | audio_patch_handle_t patchHandle = AUDIO_PATCH_HANDLE_NONE; |
Francois Gaffie | 7feb854 | 2020-04-06 17:39:47 +0200 | [diff] [blame] | 5190 | 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] | 5191 | outputDesc = mOutputs.getOutputFromId(patch->sources[1].id); |
| 5192 | } else if (patch->num_sources == 1 && sourceDesc != nullptr) { |
| 5193 | outputDesc = sourceDesc->swOutput().promote(); |
| 5194 | } |
| 5195 | if (outputDesc == nullptr) { |
| 5196 | ALOGW("%s no output for id %d", __func__, patch->sources[0].id); |
| 5197 | // releaseOutput has already called closeOutput in case of direct output |
| 5198 | return NO_ERROR; |
| 5199 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5200 | patchHandle = outputDesc->getPatchHandle(); |
| 5201 | // When a Sw bridge is released, the mixer used by this bridge will release its |
| 5202 | // patch at AudioFlinger side. Hence, the mixer audio patch must be recreated |
| 5203 | // Reuse patch handle to force audio flinger removing initial mixer patch removal |
| 5204 | // updating hal patch handle (prevent leaks). |
| 5205 | // While using a HwBridge, force reconsidering device only if not reusing an existing |
| 5206 | // output and no more activity on output (will force to close). |
| 5207 | bool force = sourceDesc->useSwBridge() || |
| 5208 | (sourceDesc->canCloseOutput() && !outputDesc->isActive()); |
| 5209 | // APM pattern is to have always outputs opened / patch realized for reachable devices. |
| 5210 | // Update device may result to NONE (empty), coupled with force, it releases the patch. |
| 5211 | // Reconsider device only for cases: |
| 5212 | // 1 / Active Output |
| 5213 | // 2 / Inactive Output previously hosting HwBridge |
| 5214 | // 3 / Inactive Output previously hosting SwBridge that can be closed. |
| 5215 | bool updateDevice = outputDesc->isActive() || !sourceDesc->useSwBridge() || |
| 5216 | sourceDesc->canCloseOutput(); |
| 5217 | setOutputDevices(outputDesc, |
| 5218 | updateDevice ? getNewOutputDevices(outputDesc, true /*fromCache*/) : |
| 5219 | outputDesc->devices(), |
| 5220 | force, |
| 5221 | 0, |
| 5222 | patchHandle == AUDIO_PATCH_HANDLE_NONE ? nullptr : &patchHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5223 | } else { |
| 5224 | return BAD_VALUE; |
| 5225 | } |
| 5226 | } else { |
| 5227 | return BAD_VALUE; |
| 5228 | } |
| 5229 | return NO_ERROR; |
| 5230 | } |
| 5231 | |
| 5232 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 5233 | struct audio_patch *patches, |
| 5234 | unsigned int *generation) |
| 5235 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5236 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5237 | return BAD_VALUE; |
| 5238 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5239 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5240 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5241 | } |
| 5242 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5243 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5244 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5245 | ALOGV("setAudioPortConfig()"); |
| 5246 | |
| 5247 | if (config == NULL) { |
| 5248 | return BAD_VALUE; |
| 5249 | } |
| 5250 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 5251 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5252 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 5253 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5254 | } |
| 5255 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5256 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5257 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 5258 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5259 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5260 | if (outputDesc == NULL) { |
| 5261 | return BAD_VALUE; |
| 5262 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5263 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 5264 | "setAudioPortConfig() called on duplicated output %d", |
| 5265 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5266 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5267 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5268 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5269 | if (inputDesc == NULL) { |
| 5270 | return BAD_VALUE; |
| 5271 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5272 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5273 | } else { |
| 5274 | return BAD_VALUE; |
| 5275 | } |
| 5276 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 5277 | sp<DeviceDescriptor> deviceDesc; |
| 5278 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 5279 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 5280 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 5281 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 5282 | } else { |
| 5283 | return BAD_VALUE; |
| 5284 | } |
| 5285 | if (deviceDesc == NULL) { |
| 5286 | return BAD_VALUE; |
| 5287 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5288 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5289 | } else { |
| 5290 | return BAD_VALUE; |
| 5291 | } |
| 5292 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 5293 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5294 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 5295 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 5296 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5297 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 5298 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5299 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5300 | if (status != NO_ERROR) { |
| 5301 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5302 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5303 | |
| 5304 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5305 | } |
| 5306 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5307 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 5308 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5309 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5310 | clearAudioPatches(uid); |
| 5311 | clearSessionRoutes(uid); |
| 5312 | } |
| 5313 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5314 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 5315 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 5316 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5317 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5318 | if (patchDesc->getUid() == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 5319 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5320 | } |
| 5321 | } |
| 5322 | } |
| 5323 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5324 | void AudioPolicyManager::checkStrategyRoute(product_strategy_t ps, audio_io_handle_t ouptutToSkip) |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5325 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5326 | // Take the first attributes following the product strategy as it is used to retrieve the routed |
| 5327 | // device. All attributes wihin a strategy follows the same "routing strategy" |
| 5328 | auto attributes = mEngine->getAllAttributesForProductStrategy(ps).front(); |
| 5329 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(attributes, nullptr, false); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5330 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5331 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5332 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 5333 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 5334 | continue; |
| 5335 | } |
| 5336 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5337 | if (!outputDesc->isStrategyActive(ps)) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5338 | continue; |
| 5339 | } |
| 5340 | // If the default device for this strategy is on another output mix, |
| 5341 | // invalidate all tracks in this strategy to force re connection. |
| 5342 | // Otherwise select new device on the output mix. |
| 5343 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 5344 | invalidateStreams(mEngine->getStreamTypesForProductStrategy(ps)); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5345 | } else { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5346 | DeviceVector newDevices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
| 5347 | if (outputDesc->mUsePreferredMixerAttributes && outputDesc->devices() != newDevices) { |
| 5348 | // If the device is using preferred mixer attributes, the output need to reopen |
| 5349 | // with default configuration when the new selected devices are different from |
| 5350 | // current routing devices. |
| 5351 | outputsToReopen.emplace(mOutputs.keyAt(j), newDevices); |
| 5352 | continue; |
| 5353 | } |
| 5354 | setOutputDevices(outputDesc, newDevices, false); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5355 | } |
| 5356 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5357 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5358 | } |
| 5359 | |
| 5360 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 5361 | { |
| 5362 | // remove output routes associated with this uid |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5363 | std::vector<product_strategy_t> affectedStrategies; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5364 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5365 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 5366 | for (const auto& client : outputDesc->getClientIterable()) { |
| 5367 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 5368 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5369 | auto clientStrategy = client->strategy(); |
| 5370 | if (std::find(begin(affectedStrategies), end(affectedStrategies), clientStrategy) != |
| 5371 | end(affectedStrategies)) { |
| 5372 | continue; |
| 5373 | } |
| 5374 | affectedStrategies.push_back(client->strategy()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5375 | } |
| 5376 | } |
| 5377 | } |
| 5378 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5379 | for (const auto& strategy : affectedStrategies) { |
| 5380 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5381 | } |
| 5382 | |
| 5383 | // remove input routes associated with this uid |
| 5384 | SortedVector<audio_source_t> affectedSources; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5385 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5386 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 5387 | for (const auto& client : inputDesc->getClientIterable()) { |
| 5388 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 5389 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 5390 | affectedSources.add(client->source()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5391 | } |
| 5392 | } |
| 5393 | } |
| 5394 | // reroute inputs if necessary |
| 5395 | SortedVector<audio_io_handle_t> inputsToClose; |
| 5396 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5397 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 5398 | if (affectedSources.indexOf(inputDesc->source()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5399 | inputsToClose.add(inputDesc->mIoHandle); |
| 5400 | } |
| 5401 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5402 | for (const auto& input : inputsToClose) { |
| 5403 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5404 | } |
| 5405 | } |
| 5406 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5407 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 5408 | { |
| 5409 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5410 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5411 | if (sourceDesc->uid() == uid) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5412 | stopAudioSource(mAudioSources.keyAt(i)); |
| 5413 | } |
| 5414 | } |
| 5415 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5416 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5417 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 5418 | audio_io_handle_t *ioHandle, |
| 5419 | audio_devices_t *device) |
| 5420 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 5421 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 5422 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5423 | audio_attributes_t attr = { .source = AUDIO_SOURCE_HOTWORD }; |
Eric Laurent | cedd5b5 | 2023-03-22 00:03:31 +0000 | [diff] [blame] | 5424 | sp<DeviceDescriptor> deviceDesc = mEngine->getInputDeviceForAttributes(attr); |
| 5425 | if (deviceDesc == nullptr) { |
| 5426 | return INVALID_OPERATION; |
| 5427 | } |
| 5428 | *device = deviceDesc->type(); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5429 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 5430 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5431 | } |
| 5432 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5433 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5434 | const audio_attributes_t *attributes, |
| 5435 | audio_port_handle_t *portId, |
| 5436 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5437 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5438 | ALOGV("%s", __FUNCTION__); |
| 5439 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 5440 | |
| 5441 | if (source == NULL || attributes == NULL || portId == NULL) { |
| 5442 | ALOGW("%s invalid argument: source %p attributes %p handle %p", |
| 5443 | __FUNCTION__, source, attributes, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5444 | return BAD_VALUE; |
| 5445 | } |
| 5446 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5447 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 5448 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5449 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", |
| 5450 | __FUNCTION__, source->role, source->type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5451 | return INVALID_OPERATION; |
| 5452 | } |
| 5453 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5454 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5455 | mAvailableInputDevices.getDevice(source->ext.device.type, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5456 | String8(source->ext.device.address), |
| 5457 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5458 | if (srcDevice == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5459 | 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] | 5460 | return BAD_VALUE; |
| 5461 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5462 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 5463 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5464 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 5465 | sp<SourceClientDescriptor> sourceDesc = |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5466 | new SourceClientDescriptor(*portId, uid, *attributes, *source, srcDevice, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 5467 | mEngine->getStreamTypeForAttributes(*attributes), |
| 5468 | mEngine->getProductStrategyForAttributes(*attributes), |
| 5469 | toVolumeSource(*attributes)); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5470 | |
| 5471 | status_t status = connectAudioSource(sourceDesc); |
| 5472 | if (status == NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5473 | mAudioSources.add(*portId, sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5474 | } |
| 5475 | return status; |
| 5476 | } |
| 5477 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 5478 | sp<SourceClientDescriptor> AudioPolicyManager::startAudioSourceInternal( |
| 5479 | const struct audio_port_config *source, const audio_attributes_t *attributes, uid_t uid) |
| 5480 | { |
| 5481 | ALOGV("%s", __FUNCTION__); |
| 5482 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 5483 | |
| 5484 | status_t status = startAudioSource(source, attributes, &portId, uid); |
| 5485 | ALOGE_IF(status != OK, "%s: failed to start audio source (%d)", __func__, status); |
| 5486 | return mAudioSources.valueFor(portId); |
| 5487 | } |
| 5488 | |
| 5489 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5490 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5491 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5492 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5493 | |
| 5494 | // make sure we only have one patch per source. |
| 5495 | disconnectAudioSource(sourceDesc); |
| 5496 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5497 | audio_attributes_t attributes = sourceDesc->attributes(); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5498 | // May the device (dynamic) have been disconnected/reconnected, id has changed. |
| 5499 | sp<DeviceDescriptor> srcDevice = mAvailableInputDevices.getDevice( |
| 5500 | sourceDesc->srcDevice()->type(), |
| 5501 | String8(sourceDesc->srcDevice()->address().c_str()), |
| 5502 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5503 | DeviceVector sinkDevices = |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 5504 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false /*fromCache*/); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5505 | ALOG_ASSERT(!sinkDevices.isEmpty(), "connectAudioSource(): no device found for attributes"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5506 | sp<DeviceDescriptor> sinkDevice = sinkDevices.itemAt(0); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5507 | if (!mAvailableOutputDevices.contains(sinkDevice)) { |
| 5508 | ALOGE("%s Device %s not available", __func__, sinkDevice->toString().c_str()); |
| 5509 | return INVALID_OPERATION; |
| 5510 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5511 | PatchBuilder patchBuilder; |
| 5512 | patchBuilder.addSink(sinkDevice).addSource(srcDevice); |
| 5513 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5514 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5515 | return connectAudioSourceToSink( |
| 5516 | sourceDesc, sinkDevice, patchBuilder.patch(), handle, mUidCached, 0 /*delayMs*/); |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5517 | } |
| 5518 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5519 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5520 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5521 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); |
| 5522 | ALOGV("%s port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5523 | if (sourceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5524 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5525 | return BAD_VALUE; |
| 5526 | } |
| 5527 | status_t status = disconnectAudioSource(sourceDesc); |
| 5528 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5529 | mAudioSources.removeItem(portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5530 | return status; |
| 5531 | } |
| 5532 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5533 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 5534 | { |
| 5535 | if (mMasterMono == mono) { |
| 5536 | return NO_ERROR; |
| 5537 | } |
| 5538 | mMasterMono = mono; |
| 5539 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 5540 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 5541 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 5542 | // |
| 5543 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 5544 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 5545 | // play back offloaded. |
| 5546 | if (mMasterMono) { |
| 5547 | Vector<audio_io_handle_t> offloaded; |
| 5548 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 5549 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5550 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 5551 | offloaded.push(desc->mIoHandle); |
| 5552 | } |
| 5553 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5554 | for (const auto& handle : offloaded) { |
| 5555 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5556 | } |
| 5557 | } |
| 5558 | // update master mono for all remaining outputs |
| 5559 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 5560 | updateMono(mOutputs.keyAt(i)); |
| 5561 | } |
| 5562 | return NO_ERROR; |
| 5563 | } |
| 5564 | |
| 5565 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 5566 | { |
| 5567 | *mono = mMasterMono; |
| 5568 | return NO_ERROR; |
| 5569 | } |
| 5570 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 5571 | float AudioPolicyManager::getStreamVolumeDB( |
| 5572 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 5573 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5574 | return computeVolume(getVolumeCurves(stream), toVolumeSource(stream), index, {device}); |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 5575 | } |
| 5576 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5577 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 5578 | audio_format_t *surroundFormats, |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5579 | bool *surroundFormatsEnabled) |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5580 | { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5581 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && |
| 5582 | (surroundFormats == nullptr || surroundFormatsEnabled == nullptr))) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5583 | return BAD_VALUE; |
| 5584 | } |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5585 | ALOGV("%s() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p", |
| 5586 | __func__, *numSurroundFormats, surroundFormats, surroundFormatsEnabled); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5587 | |
| 5588 | size_t formatsWritten = 0; |
| 5589 | size_t formatsMax = *numSurroundFormats; |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 5590 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5591 | *numSurroundFormats = mConfig->getSurroundFormats().size(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5592 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5593 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5594 | for (const auto& format: mConfig->getSurroundFormats()) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5595 | if (formatsWritten < formatsMax) { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5596 | surroundFormats[formatsWritten] = format.first; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5597 | bool formatEnabled = true; |
| 5598 | switch (forceUse) { |
| 5599 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL: |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5600 | formatEnabled = mManualSurroundFormats.count(format.first) != 0; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5601 | break; |
| 5602 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER: |
| 5603 | formatEnabled = false; |
| 5604 | break; |
| 5605 | default: // AUTO or ALWAYS => true |
| 5606 | break; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5607 | } |
| 5608 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 5609 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5610 | } |
| 5611 | return NO_ERROR; |
| 5612 | } |
| 5613 | |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5614 | status_t AudioPolicyManager::getReportedSurroundFormats(unsigned int *numSurroundFormats, |
| 5615 | audio_format_t *surroundFormats) { |
| 5616 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) { |
| 5617 | return BAD_VALUE; |
| 5618 | } |
| 5619 | ALOGV("%s() numSurroundFormats %d surroundFormats %p", |
| 5620 | __func__, *numSurroundFormats, surroundFormats); |
| 5621 | |
| 5622 | size_t formatsWritten = 0; |
| 5623 | size_t formatsMax = *numSurroundFormats; |
| 5624 | std::unordered_set<audio_format_t> formats; // Uses primary surround formats only |
| 5625 | |
| 5626 | // Return formats from all device profiles that have already been resolved by |
| 5627 | // checkOutputsForDevice(). |
| 5628 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 5629 | sp<DeviceDescriptor> device = mAvailableOutputDevices[i]; |
| 5630 | audio_devices_t deviceType = device->type(); |
| 5631 | // Enabling/disabling formats are applied to only HDMI devices. So, this function |
| 5632 | // returns formats reported by HDMI devices. |
| 5633 | if (deviceType != AUDIO_DEVICE_OUT_HDMI) { |
| 5634 | continue; |
| 5635 | } |
| 5636 | // Formats reported by sink devices |
| 5637 | std::unordered_set<audio_format_t> formatset; |
| 5638 | if (auto it = mReportedFormatsMap.find(device); it != mReportedFormatsMap.end()) { |
| 5639 | formatset.insert(it->second.begin(), it->second.end()); |
| 5640 | } |
| 5641 | |
| 5642 | // Formats hard-coded in the in policy configuration file (if any). |
| 5643 | FormatVector encodedFormats = device->encodedFormats(); |
| 5644 | formatset.insert(encodedFormats.begin(), encodedFormats.end()); |
| 5645 | // Filter the formats which are supported by the vendor hardware. |
| 5646 | for (auto it = formatset.begin(); it != formatset.end(); ++it) { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5647 | if (mConfig->getSurroundFormats().count(*it) != 0) { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5648 | formats.insert(*it); |
| 5649 | } else { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5650 | for (const auto& pair : mConfig->getSurroundFormats()) { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5651 | if (pair.second.count(*it) != 0) { |
| 5652 | formats.insert(pair.first); |
| 5653 | break; |
| 5654 | } |
| 5655 | } |
| 5656 | } |
| 5657 | } |
| 5658 | } |
| 5659 | *numSurroundFormats = formats.size(); |
| 5660 | for (const auto& format: formats) { |
| 5661 | if (formatsWritten < formatsMax) { |
| 5662 | surroundFormats[formatsWritten++] = format; |
| 5663 | } |
| 5664 | } |
| 5665 | return NO_ERROR; |
| 5666 | } |
| 5667 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5668 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 5669 | { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5670 | ALOGV("%s() format 0x%X enabled %d", __func__, audioFormat, enabled); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5671 | const auto& formatIter = mConfig->getSurroundFormats().find(audioFormat); |
| 5672 | if (formatIter == mConfig->getSurroundFormats().end()) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5673 | ALOGW("%s() format 0x%X is not a known surround format", __func__, audioFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5674 | return BAD_VALUE; |
| 5675 | } |
| 5676 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5677 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND) != |
| 5678 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5679 | ALOGW("%s() not in manual mode for surround sound format selection", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5680 | return INVALID_OPERATION; |
| 5681 | } |
| 5682 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5683 | if ((mManualSurroundFormats.count(audioFormat) != 0) == enabled) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5684 | return NO_ERROR; |
| 5685 | } |
| 5686 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5687 | std::unordered_set<audio_format_t> surroundFormatsBackup(mManualSurroundFormats); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5688 | if (enabled) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5689 | mManualSurroundFormats.insert(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 5690 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5691 | mManualSurroundFormats.insert(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5692 | } |
| 5693 | } else { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5694 | mManualSurroundFormats.erase(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 5695 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5696 | mManualSurroundFormats.erase(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5697 | } |
| 5698 | } |
| 5699 | |
| 5700 | sp<SwAudioOutputDescriptor> outputDesc; |
| 5701 | bool profileUpdated = false; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5702 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromType( |
| 5703 | AUDIO_DEVICE_OUT_HDMI); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5704 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 5705 | // Simulate reconnection to update enabled surround sound formats. |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5706 | String8 address = String8(hdmiOutputDevices[i]->address().c_str()); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 5707 | std::string name = hdmiOutputDevices[i]->getName(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5708 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 5709 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 5710 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5711 | name.c_str(), |
| 5712 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5713 | if (status != NO_ERROR) { |
| 5714 | continue; |
| 5715 | } |
| 5716 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 5717 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 5718 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5719 | name.c_str(), |
| 5720 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5721 | profileUpdated |= (status == NO_ERROR); |
| 5722 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5723 | // 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] | 5724 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromType( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5725 | AUDIO_DEVICE_IN_HDMI); |
| 5726 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 5727 | // Simulate reconnection to update enabled surround sound formats. |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5728 | String8 address = String8(hdmiInputDevices[i]->address().c_str()); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 5729 | std::string name = hdmiInputDevices[i]->getName(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5730 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 5731 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 5732 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5733 | name.c_str(), |
| 5734 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5735 | if (status != NO_ERROR) { |
| 5736 | continue; |
| 5737 | } |
| 5738 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 5739 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 5740 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5741 | name.c_str(), |
| 5742 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5743 | profileUpdated |= (status == NO_ERROR); |
| 5744 | } |
| 5745 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5746 | if (!profileUpdated) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5747 | ALOGW("%s() no audio profiles updated, undoing surround formats change", __func__); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5748 | mManualSurroundFormats = std::move(surroundFormatsBackup); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5749 | } |
| 5750 | |
| 5751 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 5752 | } |
| 5753 | |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5754 | void AudioPolicyManager::setAppState(audio_port_handle_t portId, app_state_t state) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 5755 | { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5756 | ALOGV("%s(portId:%d, state:%d)", __func__, portId, state); |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 5757 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5758 | mInputs.valueAt(i)->setAppState(portId, state); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 5759 | } |
| 5760 | } |
| 5761 | |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 5762 | bool AudioPolicyManager::isHapticPlaybackSupported() |
| 5763 | { |
| 5764 | for (const auto& hwModule : mHwModules) { |
| 5765 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 5766 | for (const auto &outProfile : outputProfiles) { |
| 5767 | struct audio_port audioPort; |
| 5768 | outProfile->toAudioPort(&audioPort); |
| 5769 | for (size_t i = 0; i < audioPort.num_channel_masks; i++) { |
| 5770 | if (audioPort.channel_masks[i] & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 5771 | return true; |
| 5772 | } |
| 5773 | } |
| 5774 | } |
| 5775 | } |
| 5776 | return false; |
| 5777 | } |
| 5778 | |
Carter Hsu | 325a8eb | 2022-01-19 19:56:51 +0800 | [diff] [blame] | 5779 | bool AudioPolicyManager::isUltrasoundSupported() |
| 5780 | { |
| 5781 | bool hasUltrasoundOutput = false; |
| 5782 | bool hasUltrasoundInput = false; |
| 5783 | for (const auto& hwModule : mHwModules) { |
| 5784 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 5785 | if (!hasUltrasoundOutput) { |
| 5786 | for (const auto &outProfile : outputProfiles) { |
| 5787 | if (outProfile->getFlags() & AUDIO_OUTPUT_FLAG_ULTRASOUND) { |
| 5788 | hasUltrasoundOutput = true; |
| 5789 | break; |
| 5790 | } |
| 5791 | } |
| 5792 | } |
| 5793 | |
| 5794 | const InputProfileCollection &inputProfiles = hwModule->getInputProfiles(); |
| 5795 | if (!hasUltrasoundInput) { |
| 5796 | for (const auto &inputProfile : inputProfiles) { |
| 5797 | if (inputProfile->getFlags() & AUDIO_INPUT_FLAG_ULTRASOUND) { |
| 5798 | hasUltrasoundInput = true; |
| 5799 | break; |
| 5800 | } |
| 5801 | } |
| 5802 | } |
| 5803 | |
| 5804 | if (hasUltrasoundOutput && hasUltrasoundInput) |
| 5805 | return true; |
| 5806 | } |
| 5807 | return false; |
| 5808 | } |
| 5809 | |
Atneya Nair | 698f5ef | 2022-12-15 16:15:09 -0800 | [diff] [blame] | 5810 | bool AudioPolicyManager::isHotwordStreamSupported(bool lookbackAudio) |
| 5811 | { |
| 5812 | const auto mask = AUDIO_INPUT_FLAG_HOTWORD_TAP | |
| 5813 | (lookbackAudio ? AUDIO_INPUT_FLAG_HW_LOOKBACK : 0); |
| 5814 | for (const auto& hwModule : mHwModules) { |
| 5815 | const InputProfileCollection &inputProfiles = hwModule->getInputProfiles(); |
| 5816 | for (const auto &inputProfile : inputProfiles) { |
| 5817 | if ((inputProfile->getFlags() & mask) == mask) { |
| 5818 | return true; |
| 5819 | } |
| 5820 | } |
| 5821 | } |
| 5822 | return false; |
| 5823 | } |
| 5824 | |
Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 5825 | bool AudioPolicyManager::isCallScreenModeSupported() |
| 5826 | { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5827 | return mConfig->isCallScreenModeSupported(); |
Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 5828 | } |
| 5829 | |
| 5830 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5831 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5832 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5833 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5834 | if (!sourceDesc->isConnected()) { |
| 5835 | ALOGV("%s port Id %d already disconnected", __FUNCTION__, sourceDesc->portId()); |
| 5836 | return NO_ERROR; |
| 5837 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5838 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); |
| 5839 | if (swOutput != 0) { |
| 5840 | status_t status = stopSource(swOutput, sourceDesc); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 5841 | if (status == NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5842 | swOutput->stop(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 5843 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5844 | if (releaseOutput(sourceDesc->portId())) { |
| 5845 | // The output descriptor is reopened to query dynamic profiles. In that case, there is |
| 5846 | // no need to release audio patch here but just return NO_ERROR. |
| 5847 | return NO_ERROR; |
| 5848 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5849 | } else { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5850 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5851 | if (hwOutputDesc != 0) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5852 | // close Hwoutput and remove from mHwOutputs |
| 5853 | } else { |
| 5854 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 5855 | } |
| 5856 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5857 | status_t status = releaseAudioPatchInternal(sourceDesc->getPatchHandle(), 0, sourceDesc); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5858 | sourceDesc->disconnect(); |
| 5859 | return status; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5860 | } |
| 5861 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5862 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForAttributesOnOutput( |
| 5863 | audio_io_handle_t output, const audio_attributes_t &attr) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5864 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5865 | sp<SourceClientDescriptor> source; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5866 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5867 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5868 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5869 | if (followsSameRouting(attr, sourceDesc->attributes()) && |
| 5870 | outputDesc != 0 && outputDesc->mIoHandle == output) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5871 | source = sourceDesc; |
| 5872 | break; |
| 5873 | } |
| 5874 | } |
| 5875 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5876 | } |
| 5877 | |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5878 | bool AudioPolicyManager::canBeSpatializedInt(const audio_attributes_t *attr, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5879 | const audio_config_t *config, |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5880 | const AudioDeviceTypeAddrVector &devices) const |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5881 | { |
| 5882 | // The caller can have the audio attributes criteria ignored by either passing a null ptr or |
| 5883 | // the AUDIO_ATTRIBUTES_INITIALIZER value. |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5884 | // 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] | 5885 | // and game usages. |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5886 | if (attr != nullptr && *attr != AUDIO_ATTRIBUTES_INITIALIZER) { |
| 5887 | if (attr->usage != AUDIO_USAGE_MEDIA && attr->usage != AUDIO_USAGE_GAME) { |
| 5888 | return false; |
| 5889 | } |
| 5890 | if ((attr->flags & (AUDIO_FLAG_CONTENT_SPATIALIZED | AUDIO_FLAG_NEVER_SPATIALIZE)) != 0) { |
| 5891 | return false; |
| 5892 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5893 | } |
| 5894 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5895 | sp<IOProfile> profile = |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5896 | getSpatializerOutputProfile(config, devices); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5897 | if (profile == nullptr) { |
| 5898 | return false; |
| 5899 | } |
| 5900 | |
| 5901 | // The caller can have the audio config criteria ignored by either passing a null ptr or |
| 5902 | // the AUDIO_CONFIG_INITIALIZER value. |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5903 | // 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] | 5904 | // some positional channel masks. |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5905 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5906 | if (config != nullptr && *config != AUDIO_CONFIG_INITIALIZER) { |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5907 | if (!audio_is_channel_mask_spatialized(config->channel_mask)) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5908 | return false; |
| 5909 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5910 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5911 | return true; |
| 5912 | } |
| 5913 | |
| 5914 | void AudioPolicyManager::checkVirtualizerClientRoutes() { |
| 5915 | std::set<audio_stream_type_t> streamsToInvalidate; |
| 5916 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5917 | const sp<SwAudioOutputDescriptor>& desc = mOutputs[i]; |
| 5918 | for (const sp<TrackClientDescriptor>& client : desc->getClientIterable()) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5919 | audio_attributes_t attr = client->attributes(); |
| 5920 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false); |
| 5921 | AudioDeviceTypeAddrVector devicesTypeAddress = devices.toTypeAddrVector(); |
| 5922 | audio_config_base_t clientConfig = client->config(); |
| 5923 | audio_config_t config = audio_config_initializer(&clientConfig); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5924 | if (desc != mSpatializerOutput |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5925 | && canBeSpatializedInt(&attr, &config, devicesTypeAddress)) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5926 | streamsToInvalidate.insert(client->stream()); |
| 5927 | } |
| 5928 | } |
| 5929 | } |
| 5930 | |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 5931 | invalidateStreams(StreamTypeVector(streamsToInvalidate.begin(), streamsToInvalidate.end())); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5932 | } |
| 5933 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5934 | |
| 5935 | bool AudioPolicyManager::isOutputOnlyAvailableRouteToSomeDevice( |
| 5936 | const sp<SwAudioOutputDescriptor>& outputDesc) { |
| 5937 | if (outputDesc->isDuplicated()) { |
| 5938 | return false; |
| 5939 | } |
| 5940 | DeviceVector devices = outputDesc->supportedDevices(); |
| 5941 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5942 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5943 | if (desc == outputDesc || desc->isDuplicated()) { |
| 5944 | continue; |
| 5945 | } |
| 5946 | DeviceVector sharedDevices = desc->filterSupportedDevices(devices); |
| 5947 | if (!sharedDevices.isEmpty() |
| 5948 | && (desc->devicesSupportEncodedFormats(sharedDevices.types()) |
| 5949 | == outputDesc->devicesSupportEncodedFormats(sharedDevices.types()))) { |
| 5950 | return false; |
| 5951 | } |
| 5952 | } |
| 5953 | return true; |
| 5954 | } |
| 5955 | |
| 5956 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5957 | status_t AudioPolicyManager::getSpatializerOutput(const audio_config_base_t *mixerConfig, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5958 | const audio_attributes_t *attr, |
| 5959 | audio_io_handle_t *output) { |
| 5960 | *output = AUDIO_IO_HANDLE_NONE; |
| 5961 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5962 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(*attr, nullptr, false); |
| 5963 | AudioDeviceTypeAddrVector devicesTypeAddress = devices.toTypeAddrVector(); |
| 5964 | audio_config_t *configPtr = nullptr; |
| 5965 | audio_config_t config; |
| 5966 | if (mixerConfig != nullptr) { |
| 5967 | config = audio_config_initializer(mixerConfig); |
| 5968 | configPtr = &config; |
| 5969 | } |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5970 | if (!canBeSpatializedInt(attr, configPtr, devicesTypeAddress)) { |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5971 | ALOGV("%s provided attributes or mixer config cannot be spatialized", __func__); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5972 | return BAD_VALUE; |
| 5973 | } |
| 5974 | |
| 5975 | sp<IOProfile> profile = |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5976 | getSpatializerOutputProfile(configPtr, devicesTypeAddress); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5977 | if (profile == nullptr) { |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5978 | 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] | 5979 | return BAD_VALUE; |
| 5980 | } |
| 5981 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5982 | std::vector<sp<SwAudioOutputDescriptor>> spatializerOutputs; |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5983 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5984 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5985 | if (!desc->isDuplicated() |
| 5986 | && (desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) { |
| 5987 | spatializerOutputs.push_back(desc); |
| 5988 | ALOGV("%s adding opened spatializer Output %d", __func__, desc->mIoHandle); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5989 | } |
| 5990 | } |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5991 | mSpatializerOutput.clear(); |
| 5992 | bool outputsChanged = false; |
| 5993 | for (const auto& desc : spatializerOutputs) { |
| 5994 | if (desc->mProfile == profile |
| 5995 | && (configPtr == nullptr |
| 5996 | || configPtr->channel_mask == desc->mMixerChannelMask)) { |
| 5997 | mSpatializerOutput = desc; |
| 5998 | ALOGV("%s reusing current spatializer output %d", __func__, desc->mIoHandle); |
| 5999 | } else { |
| 6000 | ALOGV("%s closing spatializerOutput output %d to match channel mask %#x" |
| 6001 | " and devices %s", __func__, desc->mIoHandle, |
| 6002 | configPtr != nullptr ? configPtr->channel_mask : 0, |
| 6003 | devices.toString().c_str()); |
| 6004 | closeOutput(desc->mIoHandle); |
| 6005 | outputsChanged = true; |
| 6006 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6007 | } |
| 6008 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6009 | if (mSpatializerOutput == nullptr) { |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 6010 | sp<SwAudioOutputDescriptor> desc = |
| 6011 | openOutputWithProfileAndDevice(profile, devices, mixerConfig); |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6012 | if (desc != nullptr) { |
| 6013 | mSpatializerOutput = desc; |
| 6014 | outputsChanged = true; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6015 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6016 | } |
| 6017 | |
| 6018 | checkVirtualizerClientRoutes(); |
| 6019 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6020 | if (outputsChanged) { |
| 6021 | mPreviousOutputs = mOutputs; |
| 6022 | mpClientInterface->onAudioPortListUpdate(); |
| 6023 | } |
| 6024 | |
| 6025 | if (mSpatializerOutput == nullptr) { |
| 6026 | ALOGV("%s could not open spatializer output with requested config", __func__); |
| 6027 | return BAD_VALUE; |
| 6028 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6029 | *output = mSpatializerOutput->mIoHandle; |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6030 | ALOGV("%s returning new spatializer output %d", __func__, *output); |
| 6031 | return OK; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6032 | } |
| 6033 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 6034 | status_t AudioPolicyManager::releaseSpatializerOutput(audio_io_handle_t output) { |
| 6035 | if (mSpatializerOutput == nullptr) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6036 | return INVALID_OPERATION; |
| 6037 | } |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 6038 | if (mSpatializerOutput->mIoHandle != output) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6039 | return BAD_VALUE; |
| 6040 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6041 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6042 | if (!isOutputOnlyAvailableRouteToSomeDevice(mSpatializerOutput)) { |
| 6043 | ALOGV("%s closing spatializer output %d", __func__, mSpatializerOutput->mIoHandle); |
| 6044 | closeOutput(mSpatializerOutput->mIoHandle); |
| 6045 | //from now on mSpatializerOutput is null |
| 6046 | checkVirtualizerClientRoutes(); |
| 6047 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6048 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6049 | return NO_ERROR; |
| 6050 | } |
| 6051 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6052 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6053 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6054 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6055 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 6056 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 6057 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6058 | } |
| 6059 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6060 | AudioPolicyManager::AudioPolicyManager(const sp<const AudioPolicyConfig>& config, |
Mikhail Naganov | f1b6d97 | 2023-05-02 13:56:01 -0700 | [diff] [blame] | 6061 | EngineInstance&& engine, |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6062 | AudioPolicyClientInterface *clientInterface) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6063 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 6064 | mUidCached(AID_AUDIOSERVER), // no need to call getuid(), there's only one of us running. |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6065 | mConfig(config), |
Mikhail Naganov | f1b6d97 | 2023-05-02 13:56:01 -0700 | [diff] [blame] | 6066 | mEngine(std::move(engine)), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6067 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6068 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6069 | mA2dpSuspended(false), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6070 | mAudioPortGeneration(1), |
| 6071 | mBeaconMuteRefCount(0), |
| 6072 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 6073 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 6074 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6075 | mMasterMono(false), |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 6076 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6077 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6078 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 6079 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6080 | status_t AudioPolicyManager::initialize() { |
Mikhail Naganov | f1b6d97 | 2023-05-02 13:56:01 -0700 | [diff] [blame] | 6081 | if (mEngine == nullptr) { |
| 6082 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6083 | } |
| 6084 | mEngine->setObserver(this); |
| 6085 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6086 | if (status != NO_ERROR) { |
| 6087 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 6088 | return status; |
| 6089 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6090 | |
jiabin | 2923018 | 2023-04-04 21:02:36 +0000 | [diff] [blame] | 6091 | // The actual device selection cache will be updated when calling `updateDevicesAndOutputs` |
| 6092 | // at the end of this function. |
| 6093 | mEngine->initializeDeviceSelectionCache(); |
Eric Laurent | 1d69c87 | 2021-01-11 18:53:01 +0100 | [diff] [blame] | 6094 | mCommunnicationStrategy = mEngine->getProductStrategyForAttributes( |
| 6095 | mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL)); |
| 6096 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6097 | // after parsing the config, mConfig contain all known devices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6098 | // open all output streams needed to access attached devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6099 | onNewAudioModulesAvailableInt(nullptr /*newDevices*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6100 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6101 | // make sure default device is reachable |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6102 | if (const auto defaultOutputDevice = mConfig->getDefaultOutputDevice(); |
| 6103 | defaultOutputDevice == nullptr || |
| 6104 | !mAvailableOutputDevices.contains(defaultOutputDevice)) { |
| 6105 | ALOGE_IF(defaultOutputDevice != nullptr, "Default device %s is unreachable", |
| 6106 | defaultOutputDevice->toString().c_str()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6107 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6108 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 6109 | 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] | 6110 | |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 6111 | // Silence ALOGV statements |
| 6112 | property_set("log.tag." LOG_TAG, "D"); |
| 6113 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6114 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6115 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6116 | } |
| 6117 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6118 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6119 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6120 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6121 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6122 | } |
| 6123 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6124 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6125 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6126 | mAvailableOutputDevices.clear(); |
| 6127 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6128 | mOutputs.clear(); |
| 6129 | mInputs.clear(); |
| 6130 | mHwModules.clear(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6131 | mManualSurroundFormats.clear(); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6132 | mConfig.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6133 | } |
| 6134 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6135 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6136 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 6137 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6138 | } |
| 6139 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6140 | // --- |
| 6141 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6142 | void AudioPolicyManager::onNewAudioModulesAvailable() |
| 6143 | { |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6144 | DeviceVector newDevices; |
| 6145 | onNewAudioModulesAvailableInt(&newDevices); |
| 6146 | if (!newDevices.empty()) { |
| 6147 | nextAudioPortGeneration(); |
| 6148 | mpClientInterface->onAudioPortListUpdate(); |
| 6149 | } |
| 6150 | } |
| 6151 | |
| 6152 | void AudioPolicyManager::onNewAudioModulesAvailableInt(DeviceVector *newDevices) |
| 6153 | { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6154 | for (const auto& hwModule : mConfig->getHwModules()) { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6155 | if (std::find(mHwModules.begin(), mHwModules.end(), hwModule) != mHwModules.end()) { |
| 6156 | continue; |
| 6157 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6158 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
Mikhail Naganov | ffd9771 | 2023-05-03 17:45:36 -0700 | [diff] [blame] | 6159 | if (audio_module_handle_t handle = mpClientInterface->loadHwModule(hwModule->getName()); |
| 6160 | handle != AUDIO_MODULE_HANDLE_NONE) { |
| 6161 | hwModule->setHandle(handle); |
| 6162 | } else { |
| 6163 | ALOGW("could not load HW module %s", hwModule->getName()); |
| 6164 | continue; |
| 6165 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6166 | } |
| 6167 | mHwModules.push_back(hwModule); |
Dean Wheatley | 12a8713 | 2021-04-16 10:08:49 +1000 | [diff] [blame] | 6168 | // open all output streams needed to access attached devices. |
| 6169 | // direct outputs are closed immediately after checking the availability of attached devices |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6170 | // This also validates mAvailableOutputDevices list |
| 6171 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
| 6172 | if (!outProfile->canOpenNewIo()) { |
| 6173 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 6174 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 6175 | continue; |
| 6176 | } |
| 6177 | if (!outProfile->hasSupportedDevices()) { |
| 6178 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
| 6179 | continue; |
| 6180 | } |
Carter Hsu | 1a3364a | 2022-01-21 15:32:56 +0800 | [diff] [blame] | 6181 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0 || |
| 6182 | (outProfile->getFlags() & AUDIO_OUTPUT_FLAG_ULTRASOUND) != 0) { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6183 | mTtsOutputAvailable = true; |
| 6184 | } |
| 6185 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6186 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6187 | DeviceVector availProfileDevices = supportedDevices.filter(mConfig->getOutputDevices()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6188 | sp<DeviceDescriptor> supportedDevice = 0; |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6189 | if (supportedDevices.contains(mConfig->getDefaultOutputDevice())) { |
| 6190 | supportedDevice = mConfig->getDefaultOutputDevice(); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6191 | } else { |
| 6192 | // choose first device present in profile's SupportedDevices also part of |
| 6193 | // mAvailableOutputDevices. |
| 6194 | if (availProfileDevices.isEmpty()) { |
| 6195 | continue; |
| 6196 | } |
| 6197 | supportedDevice = availProfileDevices.itemAt(0); |
| 6198 | } |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6199 | if (!mConfig->getOutputDevices().contains(supportedDevice)) { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6200 | continue; |
| 6201 | } |
| 6202 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 6203 | mpClientInterface); |
| 6204 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 6205 | status_t status = outputDesc->open(nullptr /* halConfig */, nullptr /* mixerConfig */, |
| 6206 | DeviceVector(supportedDevice), |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6207 | AUDIO_STREAM_DEFAULT, |
| 6208 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 6209 | if (status != NO_ERROR) { |
| 6210 | ALOGW("Cannot open output stream for devices %s on hw module %s", |
| 6211 | supportedDevice->toString().c_str(), hwModule->getName()); |
| 6212 | continue; |
| 6213 | } |
| 6214 | for (const auto &device : availProfileDevices) { |
| 6215 | // give a valid ID to an attached device once confirmed it is reachable |
| 6216 | if (!device->isAttached()) { |
| 6217 | device->attach(hwModule); |
| 6218 | mAvailableOutputDevices.add(device); |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 6219 | device->setEncapsulationInfoFromHal(mpClientInterface); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6220 | if (newDevices) newDevices->add(device); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6221 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 6222 | } |
| 6223 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 6224 | if (mPrimaryOutput == nullptr && |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6225 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 6226 | mPrimaryOutput = outputDesc; |
| 6227 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6228 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6229 | outputDesc->close(); |
| 6230 | } else { |
| 6231 | addOutput(output, outputDesc); |
| 6232 | setOutputDevices(outputDesc, |
| 6233 | DeviceVector(supportedDevice), |
| 6234 | true, |
| 6235 | 0, |
| 6236 | NULL); |
| 6237 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6238 | } |
| 6239 | // open input streams needed to access attached devices to validate |
| 6240 | // mAvailableInputDevices list |
| 6241 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
| 6242 | if (!inProfile->canOpenNewIo()) { |
| 6243 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 6244 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 6245 | continue; |
| 6246 | } |
| 6247 | if (!inProfile->hasSupportedDevices()) { |
| 6248 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
| 6249 | continue; |
| 6250 | } |
| 6251 | // chose first device present in profile's SupportedDevices also part of |
| 6252 | // available input devices |
| 6253 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6254 | DeviceVector availProfileDevices = supportedDevices.filter(mConfig->getInputDevices()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6255 | if (availProfileDevices.isEmpty()) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 6256 | ALOGV("%s: Input device list is empty! for profile %s", |
| 6257 | __func__, inProfile->getTagName().c_str()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6258 | continue; |
| 6259 | } |
| 6260 | sp<AudioInputDescriptor> inputDesc = |
| 6261 | new AudioInputDescriptor(inProfile, mpClientInterface); |
| 6262 | |
| 6263 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 6264 | status_t status = inputDesc->open(nullptr, |
| 6265 | availProfileDevices.itemAt(0), |
| 6266 | AUDIO_SOURCE_MIC, |
| 6267 | AUDIO_INPUT_FLAG_NONE, |
| 6268 | &input); |
| 6269 | if (status != NO_ERROR) { |
| 6270 | ALOGW("Cannot open input stream for device %s on hw module %s", |
| 6271 | availProfileDevices.toString().c_str(), |
| 6272 | hwModule->getName()); |
| 6273 | continue; |
| 6274 | } |
| 6275 | for (const auto &device : availProfileDevices) { |
| 6276 | // give a valid ID to an attached device once confirmed it is reachable |
| 6277 | if (!device->isAttached()) { |
| 6278 | device->attach(hwModule); |
| 6279 | device->importAudioPortAndPickAudioProfile(inProfile, true); |
| 6280 | mAvailableInputDevices.add(device); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6281 | if (newDevices) newDevices->add(device); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6282 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 6283 | } |
| 6284 | } |
| 6285 | inputDesc->close(); |
| 6286 | } |
| 6287 | } |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6288 | |
| 6289 | // Check if spatializer outputs can be closed until used. |
| 6290 | // mOutputs vector never contains duplicated outputs at this point. |
| 6291 | std::vector<audio_io_handle_t> outputsClosed; |
| 6292 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6293 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 6294 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0 |
| 6295 | && !isOutputOnlyAvailableRouteToSomeDevice(desc)) { |
| 6296 | outputsClosed.push_back(desc->mIoHandle); |
| 6297 | desc->close(); |
| 6298 | } |
| 6299 | } |
| 6300 | for (auto output : outputsClosed) { |
| 6301 | removeOutput(output); |
| 6302 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6303 | } |
| 6304 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 6305 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 6306 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6307 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6308 | mOutputs.add(output, outputDesc); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6309 | applyStreamVolumes(outputDesc, DeviceTypeSet(), 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 6310 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6311 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6312 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6313 | } |
| 6314 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6315 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 6316 | { |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 6317 | if (mPrimaryOutput != 0 && mPrimaryOutput == mOutputs.valueFor(output)) { |
| 6318 | ALOGV("%s: removing primary output", __func__); |
| 6319 | mPrimaryOutput = nullptr; |
| 6320 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6321 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6322 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6323 | } |
| 6324 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 6325 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 6326 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6327 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6328 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6329 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6330 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6331 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6332 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6333 | audio_policy_dev_state_t state, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6334 | SortedVector<audio_io_handle_t>& outputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6335 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6336 | audio_devices_t deviceType = device->type(); |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 6337 | const String8 &address = String8(device->address().c_str()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6338 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6339 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6340 | if (audio_device_is_digital(deviceType)) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6341 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6342 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6343 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6344 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 6345 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 6346 | // first call getAudioPort to get the supported attributes from the HAL |
| 6347 | struct audio_port_v7 port = {}; |
| 6348 | device->toAudioPort(&port); |
| 6349 | status_t status = mpClientInterface->getAudioPort(&port); |
| 6350 | if (status == NO_ERROR) { |
| 6351 | device->importAudioPort(port); |
| 6352 | } |
| 6353 | |
| 6354 | // then list already open outputs that can be routed to this device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6355 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6356 | desc = mOutputs.valueAt(i); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6357 | if (!desc->isDuplicated() && desc->supportsDevice(device) |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6358 | && desc->devicesSupportEncodedFormats({deviceType})) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6359 | ALOGV("checkOutputsForDevice(): adding opened output %d on device %s", |
| 6360 | mOutputs.keyAt(i), device->toString().c_str()); |
| 6361 | outputs.add(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6362 | } |
| 6363 | } |
| 6364 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6365 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6366 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6367 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 6368 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6369 | if (profile->supportsDevice(device)) { |
| 6370 | profiles.add(profile); |
| 6371 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 6372 | j, hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6373 | } |
| 6374 | } |
| 6375 | } |
| 6376 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 6377 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6378 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6379 | if (profiles.isEmpty() && outputs.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6380 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6381 | return BAD_VALUE; |
| 6382 | } |
| 6383 | |
| 6384 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 6385 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 6386 | 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] | 6387 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6388 | |
| 6389 | // nothing to do if one output is already opened for this profile |
| 6390 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6391 | for (j = 0; j < outputs.size(); j++) { |
| 6392 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6393 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 6394 | // matching profile: save the sample rates, format and channel masks supported |
| 6395 | // by the profile in our device descriptor |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6396 | if (audio_device_is_digital(deviceType)) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6397 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6398 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6399 | break; |
| 6400 | } |
| 6401 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6402 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6403 | continue; |
| 6404 | } |
| 6405 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 6406 | if (!profile->canOpenNewIo()) { |
| 6407 | ALOGW("Max Output number %u already opened for this profile %s", |
| 6408 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 6409 | continue; |
| 6410 | } |
| 6411 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 6412 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 6413 | deviceType, address.string(), profile.get(), profile->getName().c_str()); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6414 | desc = openOutputWithProfileAndDevice(profile, DeviceVector(device)); |
| 6415 | 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] | 6416 | if (output == AUDIO_IO_HANDLE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6417 | ALOGW("checkOutputsForDevice() could not open output for device %x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6418 | profiles.removeAt(profile_index); |
| 6419 | profile_index--; |
| 6420 | } else { |
| 6421 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6422 | // Load digital format info only for digital devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6423 | if (audio_device_is_digital(deviceType)) { |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6424 | // TODO: when getAudioPort is ready, it may not be needed to import the audio |
| 6425 | // port but just pick audio profile |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6426 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6427 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 6428 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6429 | if (device_distinguishes_on_address(deviceType)) { |
| 6430 | ALOGV("checkOutputsForDevice(): setOutputDevices %s", |
| 6431 | device->toString().c_str()); |
| 6432 | setOutputDevices(desc, DeviceVector(device), true/*force*/, 0/*delay*/, |
| 6433 | NULL/*patch handle*/); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6434 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6435 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 6436 | } |
| 6437 | } |
| 6438 | |
| 6439 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6440 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6441 | return BAD_VALUE; |
| 6442 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6443 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6444 | // check if one opened output is not needed any more after disconnecting one device |
| 6445 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6446 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6447 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6448 | // exact match on device |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6449 | if (device_distinguishes_on_address(deviceType) && desc->supportsDevice(device) |
Francois Gaffie | c7d4c22 | 2021-12-02 11:12:52 +0100 | [diff] [blame] | 6450 | && desc->containsSingleDeviceSupportingEncodedFormats(device)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6451 | outputs.add(mOutputs.keyAt(i)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6452 | } else if (!mAvailableOutputDevices.containsAtLeastOne(desc->supportedDevices())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6453 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 6454 | mOutputs.keyAt(i)); |
| 6455 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6456 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6457 | } |
| 6458 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6459 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6460 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6461 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 6462 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6463 | if (!profile->supportsDevice(device)) { |
| 6464 | continue; |
| 6465 | } |
| 6466 | ALOGV("checkOutputsForDevice(): " |
| 6467 | "clearing direct output profile %zu on module %s", |
| 6468 | j, hwModule->getName()); |
| 6469 | profile->clearAudioProfiles(); |
| 6470 | if (!profile->hasDynamicAudioProfile()) { |
| 6471 | continue; |
| 6472 | } |
| 6473 | // When a device is disconnected, if there is an IOProfile that contains dynamic |
| 6474 | // profiles and supports the disconnected device, call getAudioPort to repopulate |
| 6475 | // the capabilities of the devices that is supported by the IOProfile. |
| 6476 | for (const auto& supportedDevice : profile->getSupportedDevices()) { |
| 6477 | if (supportedDevice == device || |
| 6478 | !mAvailableOutputDevices.contains(supportedDevice)) { |
| 6479 | continue; |
| 6480 | } |
| 6481 | struct audio_port_v7 port; |
| 6482 | supportedDevice->toAudioPort(&port); |
| 6483 | status_t status = mpClientInterface->getAudioPort(&port); |
| 6484 | if (status == NO_ERROR) { |
| 6485 | supportedDevice->importAudioPort(port); |
| 6486 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6487 | } |
| 6488 | } |
| 6489 | } |
| 6490 | } |
| 6491 | return NO_ERROR; |
| 6492 | } |
| 6493 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6494 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& device, |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6495 | audio_policy_dev_state_t state) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6496 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6497 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6498 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6499 | if (audio_device_is_digital(device->type())) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6500 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6501 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6502 | } |
| 6503 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6504 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
jiabin | bf5f426 | 2023-04-12 21:48:34 +0000 | [diff] [blame] | 6505 | // first call getAudioPort to get the supported attributes from the HAL |
| 6506 | struct audio_port_v7 port = {}; |
| 6507 | device->toAudioPort(&port); |
| 6508 | status_t status = mpClientInterface->getAudioPort(&port); |
| 6509 | if (status == NO_ERROR) { |
| 6510 | device->importAudioPort(port); |
| 6511 | } |
| 6512 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6513 | // look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6514 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6515 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6516 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6517 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6518 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6519 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6520 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6521 | if (profile->supportsDevice(device)) { |
| 6522 | profiles.add(profile); |
| 6523 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 6524 | profile_index, hwModule->getName()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6525 | } |
| 6526 | } |
| 6527 | } |
| 6528 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6529 | if (profiles.isEmpty()) { |
| 6530 | ALOGW("%s: No input profile available for device %s", |
| 6531 | __func__, device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6532 | return BAD_VALUE; |
| 6533 | } |
| 6534 | |
| 6535 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 6536 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 6537 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 6538 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6539 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 6540 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6541 | // nothing to do if one input is already opened for this profile |
| 6542 | size_t input_index; |
| 6543 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 6544 | desc = mInputs.valueAt(input_index); |
| 6545 | if (desc->mProfile == profile) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6546 | if (audio_device_is_digital(device->type())) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6547 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6548 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6549 | break; |
| 6550 | } |
| 6551 | } |
| 6552 | if (input_index != mInputs.size()) { |
| 6553 | continue; |
| 6554 | } |
| 6555 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 6556 | if (!profile->canOpenNewIo()) { |
| 6557 | ALOGW("Max Input number %u already opened for this profile %s", |
| 6558 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 6559 | continue; |
| 6560 | } |
| 6561 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6562 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6563 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
jiabin | bf5f426 | 2023-04-12 21:48:34 +0000 | [diff] [blame] | 6564 | 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] | 6565 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6566 | if (status == NO_ERROR) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 6567 | const String8& address = String8(device->address().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6568 | if (!address.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6569 | char *param = audio_device_address_to_parameter(device->type(), address); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6570 | mpClientInterface->setParameters(input, String8(param)); |
| 6571 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6572 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6573 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6574 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6575 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6576 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6577 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6578 | } |
| 6579 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6580 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6581 | addInput(input, desc); |
| 6582 | } |
| 6583 | } // endif input != 0 |
| 6584 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6585 | if (input == AUDIO_IO_HANDLE_NONE) { |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 6586 | ALOGW("%s could not open input for device %s", __func__, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6587 | device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6588 | profiles.removeAt(profile_index); |
| 6589 | profile_index--; |
| 6590 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6591 | if (audio_device_is_digital(device->type())) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6592 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6593 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6594 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 6595 | } |
| 6596 | } // end scan profiles |
| 6597 | |
| 6598 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6599 | 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] | 6600 | return BAD_VALUE; |
| 6601 | } |
| 6602 | } else { |
| 6603 | // Disconnect |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6604 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 6605 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6606 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6607 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6608 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6609 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6610 | if (profile->supportsDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 6611 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 6612 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6613 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6614 | } |
| 6615 | } |
| 6616 | } |
| 6617 | } // end disconnect |
| 6618 | |
| 6619 | return NO_ERROR; |
| 6620 | } |
| 6621 | |
| 6622 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6623 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6624 | { |
| 6625 | ALOGV("closeOutput(%d)", output); |
| 6626 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6627 | sp<SwAudioOutputDescriptor> closingOutput = mOutputs.valueFor(output); |
| 6628 | if (closingOutput == NULL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6629 | ALOGW("closeOutput() unknown output %d", output); |
| 6630 | return; |
| 6631 | } |
Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 6632 | const bool closingOutputWasActive = closingOutput->isActive(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6633 | mPolicyMixes.closeOutput(closingOutput); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6634 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6635 | // look for duplicated outputs connected to the output being removed. |
| 6636 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6637 | sp<SwAudioOutputDescriptor> dupOutput = mOutputs.valueAt(i); |
| 6638 | if (dupOutput->isDuplicated() && |
| 6639 | (dupOutput->mOutput1 == closingOutput || dupOutput->mOutput2 == closingOutput)) { |
| 6640 | sp<SwAudioOutputDescriptor> remainingOutput = |
| 6641 | dupOutput->mOutput1 == closingOutput ? dupOutput->mOutput2 : dupOutput->mOutput1; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6642 | // As all active tracks on duplicated output will be deleted, |
| 6643 | // and as they were also referenced on the other output, the reference |
| 6644 | // count for their stream type must be adjusted accordingly on |
| 6645 | // the other output. |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6646 | const bool wasActive = remainingOutput->isActive(); |
| 6647 | // Note: no-op on the closing output where all clients has already been set inactive |
| 6648 | dupOutput->setAllClientsInactive(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 6649 | // stop() will be a no op if the output is still active but is needed in case all |
| 6650 | // active streams refcounts where cleared above |
| 6651 | if (wasActive) { |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6652 | remainingOutput->stop(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 6653 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6654 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 6655 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 6656 | |
| 6657 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6658 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6659 | } |
| 6660 | } |
| 6661 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6662 | nextAudioPortGeneration(); |
| 6663 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6664 | ssize_t index = mAudioPatches.indexOfKey(closingOutput->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6665 | if (index >= 0) { |
| 6666 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6667 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 6668 | patchDesc->getAfHandle(), 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6669 | mAudioPatches.removeItemsAt(index); |
| 6670 | mpClientInterface->onAudioPatchListUpdate(); |
| 6671 | } |
| 6672 | |
Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 6673 | if (closingOutputWasActive) { |
| 6674 | closingOutput->stop(); |
| 6675 | } |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6676 | closingOutput->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6677 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6678 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6679 | mPreviousOutputs = mOutputs; |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 6680 | if (closingOutput == mSpatializerOutput) { |
| 6681 | mSpatializerOutput.clear(); |
| 6682 | } |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6683 | |
| 6684 | // MSD patches may have been released to support a non-MSD direct output. Reset MSD patch if |
| 6685 | // no direct outputs are open. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6686 | if (!getMsdAudioOutDevices().isEmpty()) { |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6687 | bool directOutputOpen = false; |
| 6688 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6689 | if (mOutputs[i]->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 6690 | directOutputOpen = true; |
| 6691 | break; |
| 6692 | } |
| 6693 | } |
| 6694 | if (!directOutputOpen) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 6695 | ALOGV("no direct outputs open, reset MSD patches"); |
| 6696 | // TODO: The MSD patches to be established here may differ to current MSD patches due to |
| 6697 | // how output devices for patching are resolved. Avoid by caching and reusing the |
| 6698 | // arguments to mEngine->getOutputDevicesForAttributes() when resolving which output |
| 6699 | // devices to patch to. This may be complicated by the fact that devices may become |
| 6700 | // unavailable. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 6701 | setMsdOutputPatches(); |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6702 | } |
| 6703 | } |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6704 | } |
| 6705 | |
| 6706 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 6707 | { |
| 6708 | ALOGV("closeInput(%d)", input); |
| 6709 | |
| 6710 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 6711 | if (inputDesc == NULL) { |
| 6712 | ALOGW("closeInput() unknown input %d", input); |
| 6713 | return; |
| 6714 | } |
| 6715 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6716 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6717 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6718 | sp<DeviceDescriptor> device = inputDesc->getDevice(); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 6719 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6720 | if (index >= 0) { |
| 6721 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6722 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 6723 | patchDesc->getAfHandle(), 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6724 | mAudioPatches.removeItemsAt(index); |
| 6725 | mpClientInterface->onAudioPatchListUpdate(); |
| 6726 | } |
| 6727 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6728 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6729 | mInputs.removeItem(input); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6730 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6731 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 6732 | if (primaryInputDevices.contains(device) && |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6733 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 6734 | mpClientInterface->setSoundTriggerCaptureState(false); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6735 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6736 | } |
| 6737 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6738 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevices( |
| 6739 | const DeviceVector &devices, |
| 6740 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6741 | { |
| 6742 | SortedVector<audio_io_handle_t> outputs; |
| 6743 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6744 | ALOGVV("%s() devices %s", __func__, devices.toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6745 | for (size_t i = 0; i < openOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6746 | ALOGVV("output %zu isDuplicated=%d device=%s", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 6747 | i, openOutputs.valueAt(i)->isDuplicated(), |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6748 | openOutputs.valueAt(i)->supportedDevices().toString().c_str()); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6749 | if (openOutputs.valueAt(i)->supportsAllDevices(devices) |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6750 | && openOutputs.valueAt(i)->devicesSupportEncodedFormats(devices.types())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6751 | ALOGVV("%s() found output %d", __func__, openOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6752 | outputs.add(openOutputs.keyAt(i)); |
| 6753 | } |
| 6754 | } |
| 6755 | return outputs; |
| 6756 | } |
| 6757 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6758 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 6759 | { |
| 6760 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 6761 | // output is suspended before any tracks are moved to it |
| 6762 | checkA2dpSuspend(); |
| 6763 | checkOutputForAllStrategies(); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6764 | checkSecondaryOutputs(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 6765 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6766 | updateDevicesAndOutputs(); |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 6767 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 6768 | // TODO: The MSD patches to be established here may differ to current MSD patches due to how |
| 6769 | // output devices for patching are resolved. Nevertheless, AudioTracks affected by device |
| 6770 | // configuration changes will ultimately be rerouted correctly. We can still avoid |
| 6771 | // unnecessary rerouting by caching and reusing the arguments to |
| 6772 | // mEngine->getOutputDevicesForAttributes() when resolving which output devices to patch to. |
| 6773 | // This may be complicated by the fact that devices may become unavailable. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 6774 | setMsdOutputPatches(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 6775 | } |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 6776 | // an event that changed routing likely occurred, inform upper layers |
| 6777 | mpClientInterface->onRoutingUpdated(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6778 | } |
| 6779 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6780 | bool AudioPolicyManager::followsSameRouting(const audio_attributes_t &lAttr, |
| 6781 | const audio_attributes_t &rAttr) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6782 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6783 | return mEngine->getProductStrategyForAttributes(lAttr) == |
| 6784 | mEngine->getProductStrategyForAttributes(rAttr); |
| 6785 | } |
| 6786 | |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6787 | void AudioPolicyManager::checkAudioSourceForAttributes(const audio_attributes_t &attr) |
| 6788 | { |
| 6789 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 6790 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 6791 | if (sourceDesc != nullptr && followsSameRouting(attr, sourceDesc->attributes()) |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 6792 | && sourceDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6793 | && !isCallRxAudioSource(sourceDesc) && !sourceDesc->isInternal()) { |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6794 | connectAudioSource(sourceDesc); |
| 6795 | } |
| 6796 | } |
| 6797 | } |
| 6798 | |
| 6799 | void AudioPolicyManager::clearAudioSourcesForOutput(audio_io_handle_t output) |
| 6800 | { |
| 6801 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 6802 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 6803 | if (sourceDesc != nullptr && sourceDesc->swOutput().promote() != nullptr |
| 6804 | && sourceDesc->swOutput().promote()->mIoHandle == output) { |
| 6805 | disconnectAudioSource(sourceDesc); |
| 6806 | } |
| 6807 | } |
| 6808 | } |
| 6809 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6810 | void AudioPolicyManager::checkOutputForAttributes(const audio_attributes_t &attr) |
| 6811 | { |
| 6812 | auto psId = mEngine->getProductStrategyForAttributes(attr); |
| 6813 | |
| 6814 | DeviceVector oldDevices = mEngine->getOutputDevicesForAttributes(attr, 0, true /*fromCache*/); |
| 6815 | DeviceVector newDevices = mEngine->getOutputDevicesForAttributes(attr, 0, false /*fromCache*/); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 6816 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6817 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevices(oldDevices, mPreviousOutputs); |
| 6818 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevices(newDevices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6819 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6820 | uint32_t maxLatency = 0; |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 6821 | bool unneededUsePrimaryOutputFromPolicyMixes = false; |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6822 | std::vector<sp<SwAudioOutputDescriptor>> invalidatedOutputs; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6823 | // take into account dynamic audio policies related changes: if a client is now associated |
| 6824 | // to a different policy mix than at creation time, invalidate corresponding stream |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6825 | for (size_t i = 0; i < mPreviousOutputs.size(); i++) { |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6826 | const sp<SwAudioOutputDescriptor>& desc = mPreviousOutputs.valueAt(i); |
| 6827 | if (desc->isDuplicated()) { |
| 6828 | continue; |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 6829 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6830 | for (const sp<TrackClientDescriptor>& client : desc->getClientIterable()) { |
| 6831 | if (mEngine->getProductStrategyForAttributes(client->attributes()) != psId) { |
| 6832 | continue; |
| 6833 | } |
| 6834 | sp<AudioPolicyMix> primaryMix; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 6835 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->config(), |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 6836 | client->uid(), client->session(), client->flags(), mAvailableOutputDevices, |
| 6837 | nullptr /* requestedDevice */, primaryMix, nullptr /* secondaryMixes */, |
| 6838 | unneededUsePrimaryOutputFromPolicyMixes); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6839 | if (status != OK) { |
| 6840 | continue; |
| 6841 | } |
yucliu | f4de36d | 2020-09-14 14:57:56 -0700 | [diff] [blame] | 6842 | if (client->getPrimaryMix() != primaryMix || client->hasLostPrimaryMix()) { |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6843 | if (desc->isStrategyActive(psId) && maxLatency < desc->latency()) { |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6844 | maxLatency = desc->latency(); |
| 6845 | } |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6846 | invalidatedOutputs.push_back(desc); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6847 | } |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 6848 | } |
| 6849 | } |
| 6850 | |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6851 | if (srcOutputs != dstOutputs || !invalidatedOutputs.empty()) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6852 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 6853 | // audio from invalidated tracks will be rendered when unmuting |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6854 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 6855 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6856 | if (desc == nullptr) continue; |
| 6857 | |
| 6858 | if (desc->isStrategyActive(psId) && maxLatency < desc->latency()) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6859 | maxLatency = desc->latency(); |
| 6860 | } |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6861 | |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6862 | bool invalidate = false; |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6863 | for (auto client : desc->clientsList(false /*activeOnly*/)) { |
Eric Laurent | 78aade8 | 2019-09-13 18:55:08 -0700 | [diff] [blame] | 6864 | if (desc->isDuplicated() || !desc->mProfile->isDirectOutput()) { |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6865 | // a client on a non direct outputs has necessarily a linear PCM format |
| 6866 | // so we can call selectOutput() safely |
| 6867 | const audio_io_handle_t newOutput = selectOutput(dstOutputs, |
| 6868 | client->flags(), |
| 6869 | client->config().format, |
| 6870 | client->config().channel_mask, |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 6871 | client->config().sample_rate, |
| 6872 | client->session()); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6873 | if (newOutput != srcOut) { |
| 6874 | invalidate = true; |
| 6875 | break; |
| 6876 | } |
| 6877 | } else { |
| 6878 | sp<IOProfile> profile = getProfileForOutput(newDevices, |
| 6879 | client->config().sample_rate, |
| 6880 | client->config().format, |
| 6881 | client->config().channel_mask, |
| 6882 | client->flags(), |
| 6883 | true /* directOnly */); |
| 6884 | if (profile != desc->mProfile) { |
| 6885 | invalidate = true; |
| 6886 | break; |
| 6887 | } |
| 6888 | } |
| 6889 | } |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6890 | // mute strategy while moving tracks from one output to another |
| 6891 | if (invalidate) { |
| 6892 | invalidatedOutputs.push_back(desc); |
| 6893 | if (desc->isStrategyActive(psId)) { |
| 6894 | setStrategyMute(psId, true, desc); |
| 6895 | setStrategyMute(psId, false, desc, maxLatency * LATENCY_MUTE_FACTOR, |
| 6896 | newDevices.types()); |
| 6897 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6898 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6899 | sp<SourceClientDescriptor> source = getSourceForAttributesOnOutput(srcOut, attr); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6900 | if (source != nullptr && !isCallRxAudioSource(source) && !source->isInternal()) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6901 | connectAudioSource(source); |
| 6902 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6903 | } |
| 6904 | |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6905 | ALOGV_IF(!(srcOutputs.isEmpty() || dstOutputs.isEmpty()), |
| 6906 | "%s: strategy %d, moving from output %s to output %s", __func__, psId, |
| 6907 | std::to_string(srcOutputs[0]).c_str(), |
| 6908 | std::to_string(dstOutputs[0]).c_str()); |
| 6909 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6910 | // Move effects associated to this stream from previous output to new output |
| 6911 | if (followsSameRouting(attr, attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6912 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6913 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6914 | // 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] | 6915 | if (!invalidatedOutputs.empty()) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 6916 | invalidateStreams(mEngine->getStreamTypesForProductStrategy(psId)); |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6917 | for (sp<SwAudioOutputDescriptor> desc : invalidatedOutputs) { |
jiabin | 4925685 | 2022-03-09 11:21:35 -0800 | [diff] [blame] | 6918 | desc->setTracksInvalidatedStatusByStrategy(psId); |
| 6919 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6920 | } |
| 6921 | } |
| 6922 | } |
| 6923 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6924 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6925 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6926 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { |
| 6927 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); |
| 6928 | checkOutputForAttributes(attributes); |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6929 | checkAudioSourceForAttributes(attributes); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6930 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6931 | } |
| 6932 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6933 | void AudioPolicyManager::checkSecondaryOutputs() { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 6934 | PortHandleVector clientsToInvalidate; |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6935 | TrackSecondaryOutputsMap trackSecondaryOutputs; |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 6936 | bool unneededUsePrimaryOutputFromPolicyMixes = false; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6937 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6938 | const sp<SwAudioOutputDescriptor>& outputDescriptor = mOutputs[i]; |
| 6939 | for (const sp<TrackClientDescriptor>& client : outputDescriptor->getClientIterable()) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6940 | sp<AudioPolicyMix> primaryMix; |
| 6941 | std::vector<sp<AudioPolicyMix>> secondaryMixes; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 6942 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->config(), |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 6943 | client->uid(), client->session(), client->flags(), mAvailableOutputDevices, |
| 6944 | nullptr /* requestedDevice */, primaryMix, &secondaryMixes, |
| 6945 | unneededUsePrimaryOutputFromPolicyMixes); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6946 | std::vector<sp<SwAudioOutputDescriptor>> secondaryDescs; |
| 6947 | for (auto &secondaryMix : secondaryMixes) { |
| 6948 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); |
| 6949 | if (outputDesc != nullptr && |
| 6950 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { |
| 6951 | secondaryDescs.push_back(outputDesc); |
| 6952 | } |
| 6953 | } |
| 6954 | |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 6955 | if (status != OK && |
| 6956 | (client->flags() & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == AUDIO_OUTPUT_FLAG_NONE) { |
| 6957 | // When it failed to query secondary output, only invalidate the client that is not |
| 6958 | // MMAP. The reason is that MMAP stream will not support secondary output. |
| 6959 | clientsToInvalidate.push_back(client->portId()); |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6960 | } else if (!std::equal( |
| 6961 | client->getSecondaryOutputs().begin(), |
| 6962 | client->getSecondaryOutputs().end(), |
| 6963 | secondaryDescs.begin(), secondaryDescs.end())) { |
jiabin | a528106 | 2021-11-23 00:10:23 +0000 | [diff] [blame] | 6964 | if (!audio_is_linear_pcm(client->config().format)) { |
| 6965 | // If the format is not PCM, the tracks should be invalidated to get correct |
| 6966 | // behavior when the secondary output is changed. |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 6967 | clientsToInvalidate.push_back(client->portId()); |
jiabin | a528106 | 2021-11-23 00:10:23 +0000 | [diff] [blame] | 6968 | } else { |
| 6969 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryDescs; |
| 6970 | std::vector<audio_io_handle_t> secondaryOutputIds; |
| 6971 | for (const auto &secondaryDesc: secondaryDescs) { |
| 6972 | secondaryOutputIds.push_back(secondaryDesc->mIoHandle); |
| 6973 | weakSecondaryDescs.push_back(secondaryDesc); |
| 6974 | } |
| 6975 | trackSecondaryOutputs.emplace(client->portId(), secondaryOutputIds); |
| 6976 | client->setSecondaryOutputs(std::move(weakSecondaryDescs)); |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6977 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6978 | } |
| 6979 | } |
| 6980 | } |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6981 | if (!trackSecondaryOutputs.empty()) { |
| 6982 | mpClientInterface->updateSecondaryOutputs(trackSecondaryOutputs); |
| 6983 | } |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 6984 | if (!clientsToInvalidate.empty()) { |
| 6985 | ALOGD("%s Invalidate clients due to fail getting output for attr", __func__); |
| 6986 | mpClientInterface->invalidateTracks(clientsToInvalidate); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6987 | } |
| 6988 | } |
| 6989 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6990 | bool AudioPolicyManager::isScoRequestedForComm() const { |
| 6991 | AudioDeviceTypeAddrVector devices; |
| 6992 | mEngine->getDevicesForRoleAndStrategy(mCommunnicationStrategy, DEVICE_ROLE_PREFERRED, devices); |
| 6993 | for (const auto &device : devices) { |
| 6994 | if (audio_is_bluetooth_out_sco_device(device.mType)) { |
| 6995 | return true; |
| 6996 | } |
| 6997 | } |
| 6998 | return false; |
| 6999 | } |
| 7000 | |
Eric Laurent | 1a8b45f | 2022-04-13 16:01:47 +0200 | [diff] [blame] | 7001 | bool AudioPolicyManager::isHearingAidUsedForComm() const { |
| 7002 | DeviceVector devices = mEngine->getOutputDevicesForStream(AUDIO_STREAM_VOICE_CALL, |
| 7003 | true /*fromCache*/); |
| 7004 | for (const auto &device : devices) { |
| 7005 | if (device->type() == AUDIO_DEVICE_OUT_HEARING_AID) { |
| 7006 | return true; |
| 7007 | } |
| 7008 | } |
| 7009 | return false; |
| 7010 | } |
| 7011 | |
| 7012 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7013 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7014 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7015 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 7016 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7017 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7018 | return; |
| 7019 | } |
| 7020 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7021 | bool isScoConnected = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7022 | (mAvailableInputDevices.types().count(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) != 0 || |
| 7023 | !Intersection(mAvailableOutputDevices.types(), getAudioDeviceOutAllScoSet()).empty()); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7024 | bool isScoRequested = isScoRequestedForComm(); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7025 | |
| 7026 | // if suspended, restore A2DP output if: |
| 7027 | // ((SCO device is NOT connected) || |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7028 | // ((SCO is not requested) && |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7029 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7030 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7031 | // if not suspended, suspend A2DP output if: |
| 7032 | // (SCO device is connected) && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7033 | // ((SCO is requested) || |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7034 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7035 | // |
| 7036 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7037 | if (!isScoConnected || |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7038 | (!isScoRequested && |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7039 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7040 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7041 | |
| 7042 | mpClientInterface->restoreOutput(a2dpOutput); |
| 7043 | mA2dpSuspended = false; |
| 7044 | } |
| 7045 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7046 | if (isScoConnected && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7047 | (isScoRequested || |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7048 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7049 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7050 | |
| 7051 | mpClientInterface->suspendOutput(a2dpOutput); |
| 7052 | mA2dpSuspended = true; |
| 7053 | } |
| 7054 | } |
| 7055 | } |
| 7056 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7057 | DeviceVector AudioPolicyManager::getNewOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 7058 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7059 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7060 | DeviceVector devices; |
| 7061 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 7062 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7063 | if (index >= 0) { |
| 7064 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7065 | if (patchDesc->getUid() != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7066 | ALOGV("%s device %s forced by patch %d", __func__, |
| 7067 | outputDesc->devices().toString().c_str(), outputDesc->getPatchHandle()); |
| 7068 | return outputDesc->devices(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7069 | } |
| 7070 | } |
| 7071 | |
Dean Wheatley | 514b431 | 2020-06-17 21:45:00 +1000 | [diff] [blame] | 7072 | // Do not retrieve engine device for outputs through MSD |
| 7073 | // TODO: support explicit routing requests by resetting MSD patch to engine device. |
| 7074 | if (outputDesc->devices() == getMsdAudioOutDevices()) { |
| 7075 | return outputDesc->devices(); |
| 7076 | } |
| 7077 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 7078 | // Honor explicit routing requests only if no client using default routing is active on this |
| 7079 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 7080 | bool active; // unused |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7081 | sp<DeviceDescriptor> device = |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7082 | findPreferredDevice(outputDesc, PRODUCT_STRATEGY_NONE, active, mAvailableOutputDevices); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7083 | if (device != nullptr) { |
| 7084 | return DeviceVector(device); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 7085 | } |
| 7086 | |
François Gaffie | a807ef9 | 2018-11-05 10:44:33 +0100 | [diff] [blame] | 7087 | // Legacy Engine cannot take care of bus devices and mix, so we need to handle the conflict |
| 7088 | // of setForceUse / Default Bus device here |
| 7089 | device = mPolicyMixes.getDeviceAndMixForOutput(outputDesc, mAvailableOutputDevices); |
| 7090 | if (device != nullptr) { |
| 7091 | return DeviceVector(device); |
| 7092 | } |
| 7093 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7094 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { |
| 7095 | StreamTypeVector streams = mEngine->getStreamTypesForProductStrategy(productStrategy); |
| 7096 | auto attr = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7097 | auto hasStreamActive = [&](auto stream) { |
| 7098 | return hasStream(streams, stream) && isStreamActive(stream, 0); |
| 7099 | }; |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 7100 | |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7101 | auto doGetOutputDevicesForVoice = [&]() { |
| 7102 | return hasVoiceStream(streams) && (outputDesc == mPrimaryOutput || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7103 | outputDesc->isActive(toVolumeSource(AUDIO_STREAM_VOICE_CALL, false))) && |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7104 | (isInCall() || |
Henrik Backlund | 07c654a | 2021-10-14 15:57:10 +0200 | [diff] [blame] | 7105 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc)) && |
| 7106 | !isStreamActive(AUDIO_STREAM_ENFORCED_AUDIBLE, 0); |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7107 | }; |
| 7108 | |
| 7109 | // With low-latency playing on speaker, music on WFD, when the first low-latency |
| 7110 | // output is stopped, getNewOutputDevices checks for a product strategy |
| 7111 | // from the list, as STRATEGY_SONIFICATION comes prior to STRATEGY_MEDIA. |
Carter Hsu | cc58a6b | 2021-07-20 08:44:50 +0000 | [diff] [blame] | 7112 | // 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] | 7113 | // devices are returned for STRATEGY_SONIFICATION without checking whether the |
| 7114 | // stream is associated to the output descriptor. |
| 7115 | if (doGetOutputDevicesForVoice() || outputDesc->isStrategyActive(productStrategy) || |
| 7116 | ((hasStreamActive(AUDIO_STREAM_ALARM) || |
| 7117 | hasStreamActive(AUDIO_STREAM_ENFORCED_AUDIBLE)) && |
| 7118 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc))) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7119 | // Retrieval of devices for voice DL is done on primary output profile, cannot |
| 7120 | // check the route (would force modifying configuration file for this profile) |
| 7121 | devices = mEngine->getOutputDevicesForAttributes(attr, nullptr, fromCache); |
| 7122 | break; |
| 7123 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7124 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7125 | ALOGV("%s selected devices %s", __func__, devices.toString().c_str()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7126 | return devices; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7127 | } |
| 7128 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7129 | sp<DeviceDescriptor> AudioPolicyManager::getNewInputDevice( |
| 7130 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7131 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7132 | sp<DeviceDescriptor> device; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7133 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 7134 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7135 | if (index >= 0) { |
| 7136 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7137 | if (patchDesc->getUid() != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7138 | ALOGV("getNewInputDevice() device %s forced by patch %d", |
| 7139 | inputDesc->getDevice()->toString().c_str(), inputDesc->getPatchHandle()); |
| 7140 | return inputDesc->getDevice(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7141 | } |
| 7142 | } |
| 7143 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 7144 | // Honor explicit routing requests only if no client using default routing is active on this |
| 7145 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 7146 | bool active; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7147 | device = findPreferredDevice(inputDesc, AUDIO_SOURCE_DEFAULT, active, mAvailableInputDevices); |
| 7148 | if (device != nullptr) { |
| 7149 | return device; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 7150 | } |
| 7151 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 7152 | // 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] | 7153 | // a null sp<>, causing the patch on the input stream to be released. |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7154 | audio_attributes_t attributes; |
| 7155 | uid_t uid; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7156 | audio_session_t session; |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7157 | sp<RecordClientDescriptor> topClient = inputDesc->getHighestPriorityClient(); |
| 7158 | if (topClient != nullptr) { |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 7159 | attributes = topClient->attributes(); |
| 7160 | uid = topClient->uid(); |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7161 | session = topClient->session(); |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7162 | } else { |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 7163 | attributes = { .source = AUDIO_SOURCE_DEFAULT }; |
| 7164 | uid = 0; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7165 | session = AUDIO_SESSION_NONE; |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7166 | } |
| 7167 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7168 | if (attributes.source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 7169 | attributes.source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 7170 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7171 | if (attributes.source != AUDIO_SOURCE_DEFAULT) { |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7172 | device = mEngine->getInputDeviceForAttributes(attributes, uid, session); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 7173 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7174 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7175 | return device; |
| 7176 | } |
| 7177 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 7178 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 7179 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 7180 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 7181 | } |
| 7182 | |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 7183 | status_t AudioPolicyManager::getDevicesForAttributes( |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7184 | const audio_attributes_t &attr, AudioDeviceTypeAddrVector *devices, bool forVolume) { |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 7185 | if (devices == nullptr) { |
| 7186 | return BAD_VALUE; |
| 7187 | } |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7188 | |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7189 | DeviceVector curDevices; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 7190 | if (status_t status = getDevicesForAttributes(attr, curDevices, forVolume); status != OK) { |
| 7191 | return status; |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7192 | } |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 7193 | for (const auto& device : curDevices) { |
| 7194 | devices->push_back(device->getDeviceTypeAddr()); |
| 7195 | } |
| 7196 | return NO_ERROR; |
| 7197 | } |
| 7198 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7199 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7200 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 7201 | case AUDIO_STREAM_MUSIC: |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7202 | checkOutputForAttributes(attributes_initializer(AUDIO_USAGE_NOTIFICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7203 | updateDevicesAndOutputs(); |
| 7204 | break; |
| 7205 | default: |
| 7206 | break; |
| 7207 | } |
| 7208 | } |
| 7209 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7210 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 7211 | |
| 7212 | // skip beacon mute management if a dedicated TTS output is available |
| 7213 | if (mTtsOutputAvailable) { |
| 7214 | return 0; |
| 7215 | } |
| 7216 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7217 | switch(event) { |
| 7218 | case STARTING_OUTPUT: |
| 7219 | mBeaconMuteRefCount++; |
| 7220 | break; |
| 7221 | case STOPPING_OUTPUT: |
| 7222 | if (mBeaconMuteRefCount > 0) { |
| 7223 | mBeaconMuteRefCount--; |
| 7224 | } |
| 7225 | break; |
| 7226 | case STARTING_BEACON: |
| 7227 | mBeaconPlayingRefCount++; |
| 7228 | break; |
| 7229 | case STOPPING_BEACON: |
| 7230 | if (mBeaconPlayingRefCount > 0) { |
| 7231 | mBeaconPlayingRefCount--; |
| 7232 | } |
| 7233 | break; |
| 7234 | } |
| 7235 | |
| 7236 | if (mBeaconMuteRefCount > 0) { |
| 7237 | // any playback causes beacon to be muted |
| 7238 | return setBeaconMute(true); |
| 7239 | } else { |
| 7240 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 7241 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 7242 | } |
| 7243 | } |
| 7244 | |
| 7245 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 7246 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 7247 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 7248 | // keep track of muted state to avoid repeating mute/unmute operations |
| 7249 | if (mBeaconMuted != mute) { |
| 7250 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 7251 | ALOGV("\t muting %d", mute); |
| 7252 | uint32_t maxLatency = 0; |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7253 | auto ttsVolumeSource = toVolumeSource(AUDIO_STREAM_TTS, false); |
| 7254 | if (ttsVolumeSource == VOLUME_SOURCE_NONE) { |
| 7255 | ALOGV("\t no tts volume source available"); |
| 7256 | return 0; |
| 7257 | } |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7258 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7259 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7260 | setVolumeSourceMute(ttsVolumeSource, mute/*on*/, desc, 0 /*delay*/, DeviceTypeSet()); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7261 | const uint32_t latency = desc->latency() * 2; |
Eric Laurent | cdb2b35 | 2020-07-23 10:57:02 -0700 | [diff] [blame] | 7262 | if (desc->isActive(latency * 2) && latency > maxLatency) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7263 | maxLatency = latency; |
| 7264 | } |
| 7265 | } |
| 7266 | mBeaconMuted = mute; |
| 7267 | return maxLatency; |
| 7268 | } |
| 7269 | return 0; |
| 7270 | } |
| 7271 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7272 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7273 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7274 | mEngine->updateDeviceSelectionCache(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7275 | mPreviousOutputs = mOutputs; |
| 7276 | } |
| 7277 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 7278 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7279 | const DeviceVector &prevDevices, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7280 | uint32_t delayMs) |
| 7281 | { |
| 7282 | // mute/unmute strategies using an incompatible device combination |
| 7283 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 7284 | // if unmuting, unmute only after the specified delay |
| 7285 | if (outputDesc->isDuplicated()) { |
| 7286 | return 0; |
| 7287 | } |
| 7288 | |
| 7289 | uint32_t muteWaitMs = 0; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7290 | DeviceVector devices = outputDesc->devices(); |
| 7291 | bool shouldMute = outputDesc->isActive() && (devices.size() >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7292 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7293 | auto productStrategies = mEngine->getOrderedProductStrategies(); |
| 7294 | for (const auto &productStrategy : productStrategies) { |
| 7295 | auto attributes = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); |
| 7296 | DeviceVector curDevices = |
| 7297 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false/*fromCache*/); |
| 7298 | curDevices = curDevices.filter(outputDesc->supportedDevices()); |
| 7299 | bool mute = shouldMute && curDevices.containsAtLeastOne(devices) && curDevices != devices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7300 | bool doMute = false; |
| 7301 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7302 | if (mute && !outputDesc->isStrategyMutedByDevice(productStrategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7303 | doMute = true; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7304 | outputDesc->setStrategyMutedByDevice(productStrategy, true); |
| 7305 | } else if (!mute && outputDesc->isStrategyMutedByDevice(productStrategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7306 | doMute = true; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7307 | outputDesc->setStrategyMutedByDevice(productStrategy, false); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7308 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7309 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7310 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7311 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7312 | // 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] | 7313 | if (!desc->supportedDevices().containsAtLeastOne(outputDesc->supportedDevices())) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7314 | continue; |
| 7315 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7316 | ALOGVV("%s() %s (curDevice %s)", __func__, |
| 7317 | mute ? "muting" : "unmuting", curDevices.toString().c_str()); |
| 7318 | setStrategyMute(productStrategy, mute, desc, mute ? 0 : delayMs); |
| 7319 | if (desc->isStrategyActive(productStrategy)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7320 | if (mute) { |
| 7321 | // FIXME: should not need to double latency if volume could be applied |
| 7322 | // immediately by the audioflinger mixer. We must account for the delay |
| 7323 | // between now and the next time the audioflinger thread for this output |
| 7324 | // will process a buffer (which corresponds to one buffer size, |
| 7325 | // usually 1/2 or 1/4 of the latency). |
| 7326 | if (muteWaitMs < desc->latency() * 2) { |
| 7327 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7328 | } |
| 7329 | } |
| 7330 | } |
| 7331 | } |
| 7332 | } |
| 7333 | } |
| 7334 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7335 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 7336 | // different per device volumes |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7337 | if (outputDesc->isActive() && (devices != prevDevices)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 7338 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
Jasmine Cha | f6074fe | 2021-08-17 13:44:31 +0800 | [diff] [blame] | 7339 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 7340 | if (muteWaitMs < tempMuteWaitMs) { |
| 7341 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7342 | } |
Jasmine Cha | f6074fe | 2021-08-17 13:44:31 +0800 | [diff] [blame] | 7343 | |
| 7344 | // If recommended duration is defined, replace temporary mute duration to avoid |
| 7345 | // truncated notifications at beginning, which depends on duration of changing path in HAL. |
| 7346 | // Otherwise, temporary mute duration is conservatively set to 4 times the reported latency. |
| 7347 | uint32_t tempRecommendedMuteDuration = outputDesc->getRecommendedMuteDurationMs(); |
| 7348 | uint32_t tempMuteDurationMs = tempRecommendedMuteDuration > 0 ? |
| 7349 | tempRecommendedMuteDuration : outputDesc->latency() * 4; |
| 7350 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7351 | for (const auto &activeVs : outputDesc->getActiveVolumeSources()) { |
| 7352 | // make sure that we do not start the temporary mute period too early in case of |
| 7353 | // delayed device change |
| 7354 | setVolumeSourceMute(activeVs, true, outputDesc, delayMs); |
| 7355 | setVolumeSourceMute(activeVs, false, outputDesc, delayMs + tempMuteDurationMs, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7356 | devices.types()); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7357 | } |
| 7358 | } |
| 7359 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7360 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 7361 | if (muteWaitMs > delayMs) { |
| 7362 | muteWaitMs -= delayMs; |
| 7363 | usleep(muteWaitMs * 1000); |
| 7364 | return muteWaitMs; |
| 7365 | } |
| 7366 | return 0; |
| 7367 | } |
| 7368 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7369 | uint32_t AudioPolicyManager::setOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 7370 | const DeviceVector &devices, |
| 7371 | bool force, |
| 7372 | int delayMs, |
| 7373 | audio_patch_handle_t *patchHandle, |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame^] | 7374 | bool requiresMuteCheck, bool requiresVolumeCheck, |
| 7375 | bool skipMuteDelay) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7376 | { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 7377 | // TODO(b/262404095): Consider if the output need to be reopened. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7378 | ALOGV("%s device %s delayMs %d", __func__, devices.toString().c_str(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7379 | uint32_t muteWaitMs; |
| 7380 | |
| 7381 | if (outputDesc->isDuplicated()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7382 | muteWaitMs = setOutputDevices(outputDesc->subOutput1(), devices, force, delayMs, |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame^] | 7383 | nullptr /* patchHandle */, requiresMuteCheck, skipMuteDelay); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7384 | muteWaitMs += setOutputDevices(outputDesc->subOutput2(), devices, force, delayMs, |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame^] | 7385 | nullptr /* patchHandle */, requiresMuteCheck, skipMuteDelay); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7386 | return muteWaitMs; |
| 7387 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7388 | |
| 7389 | // filter devices according to output selected |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7390 | DeviceVector filteredDevices = outputDesc->filterSupportedDevices(devices); |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 7391 | DeviceVector prevDevices = outputDesc->devices(); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7392 | DeviceVector availPrevDevices = mAvailableOutputDevices.filter(prevDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7393 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7394 | ALOGV("setOutputDevices() prevDevice %s", prevDevices.toString().c_str()); |
| 7395 | |
| 7396 | if (!filteredDevices.isEmpty()) { |
| 7397 | outputDesc->setDevices(filteredDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7398 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 7399 | |
| 7400 | // if the outputs are not materially active, there is no need to mute. |
| 7401 | if (requiresMuteCheck) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7402 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevices, delayMs); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 7403 | } else { |
| 7404 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 7405 | muteWaitMs = 0; |
| 7406 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7407 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 7408 | bool outputRouted = outputDesc->isRouted(); |
| 7409 | |
Eric Laurent | 79ea958 | 2020-06-11 18:49:24 -0700 | [diff] [blame] | 7410 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 7411 | // output profile or if new device is not supported AND previous device(s) is(are) still |
| 7412 | // available (otherwise reset device must be done on the output) |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7413 | if (!devices.isEmpty() && filteredDevices.isEmpty() && !availPrevDevices.empty()) { |
Eric Laurent | 79ea958 | 2020-06-11 18:49:24 -0700 | [diff] [blame] | 7414 | ALOGV("%s: unsupported device %s for output", __func__, devices.toString().c_str()); |
| 7415 | // restore previous device after evaluating strategy mute state |
| 7416 | outputDesc->setDevices(prevDevices); |
| 7417 | return muteWaitMs; |
| 7418 | } |
| 7419 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7420 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 7421 | // the requested device is AUDIO_DEVICE_NONE |
| 7422 | // OR the requested device is the same as current device |
| 7423 | // AND force is not specified |
| 7424 | // AND the output is connected by a valid audio patch. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7425 | // Doing this check here allows the caller to call setOutputDevices() without conditions |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 7426 | if ((filteredDevices.isEmpty() || filteredDevices == prevDevices) && !force && outputRouted) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7427 | ALOGV("%s setting same device %s or null device, force=%d, patch handle=%d", __func__, |
| 7428 | filteredDevices.toString().c_str(), force, outputDesc->getPatchHandle()); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7429 | if (requiresVolumeCheck && !filteredDevices.isEmpty()) { |
| 7430 | ALOGV("%s setting same device on routed output, force apply volumes", __func__); |
| 7431 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs, true /*force*/); |
| 7432 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7433 | return muteWaitMs; |
| 7434 | } |
| 7435 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7436 | ALOGV("%s changing device to %s", __func__, filteredDevices.toString().c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7437 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7438 | // do the routing |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7439 | if (filteredDevices.isEmpty() || mAvailableOutputDevices.filter(filteredDevices).empty()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7440 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7441 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7442 | PatchBuilder patchBuilder; |
| 7443 | patchBuilder.addSource(outputDesc); |
| 7444 | ALOG_ASSERT(filteredDevices.size() <= AUDIO_PATCH_PORTS_MAX, "Too many sink ports"); |
| 7445 | for (const auto &filteredDevice : filteredDevices) { |
| 7446 | patchBuilder.addSink(filteredDevice); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 7447 | } |
| 7448 | |
Jasmine Cha | 7f82d1a | 2020-03-16 13:21:47 +0800 | [diff] [blame] | 7449 | // Add half reported latency to delayMs when muteWaitMs is null in order |
| 7450 | // to avoid disordered sequence of muting volume and changing devices. |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame^] | 7451 | int actualDelayMs = !skipMuteDelay && muteWaitMs == 0 |
| 7452 | ? (delayMs + (outputDesc->latency() / 2)) : delayMs; |
| 7453 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), actualDelayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7454 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7455 | |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame^] | 7456 | // Since the mute is skip, also skip the apply stream volume as that will be applied externally |
| 7457 | if (!skipMuteDelay) { |
| 7458 | // update stream volumes according to new device |
| 7459 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs); |
| 7460 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7461 | |
| 7462 | return muteWaitMs; |
| 7463 | } |
| 7464 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7465 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7466 | int delayMs, |
| 7467 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7468 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7469 | ssize_t index; |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 7470 | if (patchHandle == nullptr && !outputDesc->isRouted()) { |
| 7471 | return INVALID_OPERATION; |
| 7472 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7473 | if (patchHandle) { |
| 7474 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 7475 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 7476 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7477 | } |
| 7478 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7479 | return INVALID_OPERATION; |
| 7480 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7481 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7482 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7483 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 7484 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7485 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7486 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 7487 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7488 | return status; |
| 7489 | } |
| 7490 | |
| 7491 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7492 | const sp<DeviceDescriptor> &device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7493 | bool force, |
| 7494 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7495 | { |
| 7496 | status_t status = NO_ERROR; |
| 7497 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7498 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7499 | if ((device != nullptr) && ((device != inputDesc->getDevice()) || force)) { |
| 7500 | inputDesc->setDevice(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7501 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7502 | if (mAvailableInputDevices.contains(device)) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7503 | PatchBuilder patchBuilder; |
| 7504 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 7505 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 7506 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7507 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 7508 | auto result = usecase; |
| 7509 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 7510 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 7511 | } |
| 7512 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7513 | //only one input device for now |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7514 | addSource(device); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7515 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7516 | } |
| 7517 | } |
| 7518 | return status; |
| 7519 | } |
| 7520 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7521 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 7522 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7523 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7524 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7525 | ssize_t index; |
| 7526 | if (patchHandle) { |
| 7527 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 7528 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 7529 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7530 | } |
| 7531 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7532 | return INVALID_OPERATION; |
| 7533 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7534 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7535 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7536 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 7537 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7538 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7539 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 7540 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7541 | return status; |
| 7542 | } |
| 7543 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7544 | sp<IOProfile> AudioPolicyManager::getInputProfile(const sp<DeviceDescriptor> &device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7545 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 7546 | audio_format_t& format, |
| 7547 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7548 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7549 | { |
| 7550 | // Choose an input profile based on the requested capture parameters: select the first available |
| 7551 | // profile supporting all requested parameters. |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7552 | // 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] | 7553 | // |
| 7554 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 7555 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7556 | |
Atneya Nair | 0f0a803 | 2022-12-12 16:20:12 -0800 | [diff] [blame] | 7557 | using underlying_input_flag_t = std::underlying_type_t<audio_input_flags_t>; |
| 7558 | const underlying_input_flag_t mustMatchFlag = AUDIO_INPUT_FLAG_MMAP_NOIRQ | |
| 7559 | AUDIO_INPUT_FLAG_HOTWORD_TAP | AUDIO_INPUT_FLAG_HW_LOOKBACK; |
| 7560 | |
| 7561 | const underlying_input_flag_t oriFlags = flags; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 7562 | |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7563 | for (;;) { |
| 7564 | sp<IOProfile> firstInexact = nullptr; |
| 7565 | uint32_t updatedSamplingRate = 0; |
| 7566 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 7567 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
| 7568 | for (const auto& hwModule : mHwModules) { |
| 7569 | for (const auto& profile : hwModule->getInputProfiles()) { |
| 7570 | // profile->log(); |
| 7571 | //updatedFormat = format; |
| 7572 | if (profile->isCompatibleProfile(DeviceVector(device), samplingRate, |
| 7573 | &samplingRate /*updatedSamplingRate*/, |
| 7574 | format, |
| 7575 | &format, /*updatedFormat*/ |
| 7576 | channelMask, |
| 7577 | &channelMask /*updatedChannelMask*/, |
| 7578 | // FIXME ugly cast |
| 7579 | (audio_output_flags_t) flags, |
| 7580 | true /*exactMatchRequiredForInputFlags*/)) { |
| 7581 | return profile; |
| 7582 | } |
| 7583 | if (firstInexact == nullptr && profile->isCompatibleProfile(DeviceVector(device), |
| 7584 | samplingRate, |
| 7585 | &updatedSamplingRate, |
| 7586 | format, |
| 7587 | &updatedFormat, |
| 7588 | channelMask, |
| 7589 | &updatedChannelMask, |
| 7590 | // FIXME ugly cast |
| 7591 | (audio_output_flags_t) flags, |
| 7592 | false /*exactMatchRequiredForInputFlags*/)) { |
| 7593 | firstInexact = profile; |
| 7594 | } |
| 7595 | } |
| 7596 | } |
| 7597 | |
| 7598 | if (firstInexact != nullptr) { |
| 7599 | samplingRate = updatedSamplingRate; |
| 7600 | format = updatedFormat; |
| 7601 | channelMask = updatedChannelMask; |
| 7602 | return firstInexact; |
| 7603 | } else if (flags & AUDIO_INPUT_FLAG_RAW) { |
| 7604 | flags = (audio_input_flags_t) (flags & ~AUDIO_INPUT_FLAG_RAW); // retry |
| 7605 | } else if ((flags & mustMatchFlag) == AUDIO_INPUT_FLAG_NONE && |
| 7606 | flags != AUDIO_INPUT_FLAG_NONE && audio_is_linear_pcm(format)) { |
| 7607 | flags = AUDIO_INPUT_FLAG_NONE; |
| 7608 | } else { // fail |
| 7609 | ALOGW("%s could not find profile for device %s, sampling rate %u, format %#x, " |
| 7610 | "channel mask 0x%X, flags %#x", __func__, device->toString().c_str(), |
| 7611 | samplingRate, format, channelMask, oriFlags); |
| 7612 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7613 | } |
| 7614 | } |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7615 | |
| 7616 | return nullptr; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7617 | } |
| 7618 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7619 | float AudioPolicyManager::computeVolume(IVolumeCurves &curves, |
| 7620 | VolumeSource volumeSource, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 7621 | int index, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7622 | const DeviceTypeSet& deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7623 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7624 | float volumeDb = curves.volIndexToDb(Volume::getDeviceCategory(deviceTypes), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 7625 | |
| 7626 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 7627 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 7628 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 7629 | // the ringtone volume |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7630 | const auto callVolumeSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL, false); |
| 7631 | const auto ringVolumeSrc = toVolumeSource(AUDIO_STREAM_RING, false); |
| 7632 | const auto musicVolumeSrc = toVolumeSource(AUDIO_STREAM_MUSIC, false); |
| 7633 | const auto alarmVolumeSrc = toVolumeSource(AUDIO_STREAM_ALARM, false); |
| 7634 | const auto a11yVolumeSrc = toVolumeSource(AUDIO_STREAM_ACCESSIBILITY, false); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7635 | |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7636 | if (volumeSource == a11yVolumeSrc |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7637 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) && |
| 7638 | mOutputs.isActive(ringVolumeSrc, 0)) { |
| 7639 | auto &ringCurves = getVolumeCurves(AUDIO_STREAM_RING); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7640 | const float ringVolumeDb = computeVolume(ringCurves, ringVolumeSrc, index, deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7641 | return ringVolumeDb - 4 > volumeDb ? ringVolumeDb - 4 : volumeDb; |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 7642 | } |
| 7643 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 7644 | // in-call: always cap volume by voice volume + some low headroom |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7645 | if ((volumeSource != callVolumeSrc && (isInCall() || |
| 7646 | mOutputs.isActiveLocally(callVolumeSrc))) && |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7647 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM, false) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7648 | volumeSource == ringVolumeSrc || volumeSource == musicVolumeSrc || |
| 7649 | volumeSource == alarmVolumeSrc || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7650 | volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION, false) || |
| 7651 | volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false) || |
| 7652 | volumeSource == toVolumeSource(AUDIO_STREAM_DTMF, false) || |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7653 | volumeSource == a11yVolumeSrc)) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7654 | auto &voiceCurves = getVolumeCurves(callVolumeSrc); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7655 | int voiceVolumeIndex = voiceCurves.getVolumeIndex(deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7656 | const float maxVoiceVolDb = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7657 | computeVolume(voiceCurves, callVolumeSrc, voiceVolumeIndex, deviceTypes) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 7658 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 7659 | // FIXME: Workaround for call screening applications until a proper audio mode is defined |
| 7660 | // to support this scenario : Exempt the RING stream from the audio cap if the audio was |
| 7661 | // programmatically muted. |
| 7662 | // VOICE_CALL stream has minVolumeIndex > 0 : Users cannot set the volume of voice calls to |
| 7663 | // 0. We don't want to cap volume when the system has programmatically muted the voice call |
| 7664 | // stream. See setVolumeCurveIndex() for more information. |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7665 | bool exemptFromCapping = |
| 7666 | ((volumeSource == ringVolumeSrc) || (volumeSource == a11yVolumeSrc)) |
| 7667 | && (voiceVolumeIndex == 0); |
Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 7668 | ALOGV_IF(exemptFromCapping, "%s volume source %d at vol=%f not capped", __func__, |
| 7669 | volumeSource, volumeDb); |
| 7670 | if ((volumeDb > maxVoiceVolDb) && !exemptFromCapping) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7671 | ALOGV("%s volume source %d at vol=%f overriden by volume group %d at vol=%f", __func__, |
| 7672 | volumeSource, volumeDb, callVolumeSrc, maxVoiceVolDb); |
| 7673 | volumeDb = maxVoiceVolDb; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 7674 | } |
| 7675 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7676 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 7677 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 7678 | // - always attenuate notifications volume by 6dB |
| 7679 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 7680 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7681 | // - if music is playing, always limit the volume to current music volume, |
| 7682 | // with a minimum threshold at -36dB so that notification is always perceived. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7683 | if (!Intersection(deviceTypes, |
| 7684 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, |
| 7685 | AUDIO_DEVICE_OUT_WIRED_HEADSET, AUDIO_DEVICE_OUT_WIRED_HEADPHONE, |
Eric Laurent | c42df45 | 2020-08-07 10:51:53 -0700 | [diff] [blame] | 7686 | AUDIO_DEVICE_OUT_USB_HEADSET, AUDIO_DEVICE_OUT_HEARING_AID, |
| 7687 | AUDIO_DEVICE_OUT_BLE_HEADSET}).empty() && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7688 | ((volumeSource == alarmVolumeSrc || |
| 7689 | volumeSource == ringVolumeSrc) || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7690 | (volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION, false)) || |
| 7691 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM, false)) || |
| 7692 | ((volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false)) && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7693 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
| 7694 | curves.canBeMuted()) { |
| 7695 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7696 | // when the phone is ringing we must consider that music could have been paused just before |
| 7697 | // by the music application and behave as if music was active if the last music track was |
| 7698 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 7699 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7700 | mLimitRingtoneVolume) { |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7701 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7702 | DeviceTypeSet musicDevice = |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7703 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 7704 | nullptr, true /*fromCache*/).types(); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7705 | auto &musicCurves = getVolumeCurves(AUDIO_STREAM_MUSIC); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7706 | float musicVolDb = computeVolume(musicCurves, |
| 7707 | musicVolumeSrc, |
| 7708 | musicCurves.getVolumeIndex(musicDevice), |
| 7709 | musicDevice); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7710 | float minVolDb = (musicVolDb > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 7711 | musicVolDb : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
| 7712 | if (volumeDb > minVolDb) { |
| 7713 | volumeDb = minVolDb; |
| 7714 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDb, musicVolDb); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7715 | } |
Eric Laurent | 7b6385c | 2021-05-12 17:55:36 +0200 | [diff] [blame] | 7716 | if (Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER |
| 7717 | && !Intersection(deviceTypes, {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, |
| 7718 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES}).empty()) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 7719 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 7720 | // intended to be played |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7721 | if ((volumeDb > -96.0f) && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7722 | (musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDb)) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7723 | ALOGV("%s increasing volume for volume source=%d device=%s from %f to %f", |
| 7724 | __func__, volumeSource, dumpDeviceTypes(deviceTypes).c_str(), volumeDb, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7725 | musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 7726 | volumeDb = musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 7727 | } |
| 7728 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7729 | } else if ((Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7730 | (!(volumeSource == alarmVolumeSrc || volumeSource == ringVolumeSrc))) { |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7731 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7732 | } |
| 7733 | } |
| 7734 | |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7735 | return volumeDb; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7736 | } |
| 7737 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7738 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7739 | VolumeSource fromVolumeSource, |
| 7740 | VolumeSource toVolumeSource) |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7741 | { |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7742 | if (fromVolumeSource == toVolumeSource) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7743 | return srcIndex; |
| 7744 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7745 | auto &srcCurves = getVolumeCurves(fromVolumeSource); |
| 7746 | auto &dstCurves = getVolumeCurves(toVolumeSource); |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7747 | float minSrc = (float)srcCurves.getVolumeIndexMin(); |
| 7748 | float maxSrc = (float)srcCurves.getVolumeIndexMax(); |
| 7749 | float minDst = (float)dstCurves.getVolumeIndexMin(); |
| 7750 | float maxDst = (float)dstCurves.getVolumeIndexMax(); |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7751 | |
Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 7752 | // preserve mute request or correct range |
| 7753 | if (srcIndex < minSrc) { |
| 7754 | if (srcIndex == 0) { |
| 7755 | return 0; |
| 7756 | } |
| 7757 | srcIndex = minSrc; |
| 7758 | } else if (srcIndex > maxSrc) { |
| 7759 | srcIndex = maxSrc; |
| 7760 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7761 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 7762 | } |
| 7763 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7764 | status_t AudioPolicyManager::checkAndSetVolume(IVolumeCurves &curves, |
| 7765 | VolumeSource volumeSource, |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7766 | int index, |
| 7767 | const sp<AudioOutputDescriptor>& outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7768 | DeviceTypeSet deviceTypes, |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7769 | int delayMs, |
| 7770 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7771 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7772 | // do not change actual attributes volume if the attributes is muted |
| 7773 | if (outputDesc->isMuted(volumeSource)) { |
| 7774 | ALOGVV("%s: volume source %d muted count %d active=%d", __func__, volumeSource, |
| 7775 | outputDesc->getMuteCount(volumeSource), outputDesc->isActive(volumeSource)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7776 | return NO_ERROR; |
| 7777 | } |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7778 | VolumeSource callVolSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL, false); |
| 7779 | VolumeSource btScoVolSrc = toVolumeSource(AUDIO_STREAM_BLUETOOTH_SCO, false); |
| 7780 | bool isVoiceVolSrc = (volumeSource != VOLUME_SOURCE_NONE) && (callVolSrc == volumeSource); |
| 7781 | bool isBtScoVolSrc = (volumeSource != VOLUME_SOURCE_NONE) && (btScoVolSrc == volumeSource); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7782 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7783 | bool isScoRequested = isScoRequestedForComm(); |
Eric Laurent | 1a8b45f | 2022-04-13 16:01:47 +0200 | [diff] [blame] | 7784 | bool isHAUsed = isHearingAidUsedForComm(); |
| 7785 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7786 | // 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] | 7787 | // if sco and call follow same curves, bypass forceUseForComm |
| 7788 | if ((callVolSrc != btScoVolSrc) && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7789 | ((isVoiceVolSrc && isScoRequested) || |
Beibei | f660a51 | 2023-02-28 17:00:34 +0800 | [diff] [blame] | 7790 | (isBtScoVolSrc && !(isScoRequested || isHAUsed))) && |
| 7791 | !isSingleDeviceType(deviceTypes, AUDIO_DEVICE_OUT_TELEPHONY_TX)) { |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7792 | 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] | 7793 | volumeSource, isScoRequested ? " " : " not "); |
Eric Laurent | 571ef96 | 2020-07-24 11:43:48 -0700 | [diff] [blame] | 7794 | // Do not return an error here as AudioService will always set both voice call |
| 7795 | // and bluetooth SCO volumes due to stream aliasing. |
| 7796 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7797 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7798 | if (deviceTypes.empty()) { |
| 7799 | deviceTypes = outputDesc->devices().types(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7800 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7801 | |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 7802 | if (curves.getVolumeIndexMin() < 0 || curves.getVolumeIndexMax() < 0) { |
| 7803 | ALOGE("invalid volume index range"); |
| 7804 | return BAD_VALUE; |
| 7805 | } |
| 7806 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7807 | float volumeDb = computeVolume(curves, volumeSource, index, deviceTypes); |
| 7808 | if (outputDesc->isFixedVolume(deviceTypes) || |
Eric Laurent | 9698a4c | 2020-10-12 17:10:23 -0700 | [diff] [blame] | 7809 | // Force VoIP volume to max for bluetooth SCO device except if muted |
| 7810 | (index != 0 && (isVoiceVolSrc || isBtScoVolSrc) && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7811 | isSingleDeviceType(deviceTypes, audio_is_bluetooth_out_sco_device))) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 7812 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7813 | } |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 7814 | const bool muted = (index == 0) && (volumeDb != 0.0f); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7815 | outputDesc->setVolume( |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 7816 | volumeDb, muted, volumeSource, curves.getStreamTypes(), deviceTypes, delayMs, force); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7817 | |
Eric Laurent | e8f2c0f | 2021-08-17 11:17:19 +0200 | [diff] [blame] | 7818 | if (outputDesc == mPrimaryOutput && (isVoiceVolSrc || isBtScoVolSrc)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7819 | float voiceVolume; |
Eric Laurent | fad001d | 2019-06-11 19:17:57 -0700 | [diff] [blame] | 7820 | // 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] | 7821 | if (isVoiceVolSrc) { |
| 7822 | voiceVolume = (float)index/(float)curves.getVolumeIndexMax(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7823 | } else { |
Eric Laurent | fad001d | 2019-06-11 19:17:57 -0700 | [diff] [blame] | 7824 | voiceVolume = index == 0 ? 0.0 : 1.0; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7825 | } |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 7826 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7827 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 7828 | mLastVoiceVolume = voiceVolume; |
| 7829 | } |
| 7830 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7831 | return NO_ERROR; |
| 7832 | } |
| 7833 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7834 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7835 | const DeviceTypeSet& deviceTypes, |
| 7836 | int delayMs, |
| 7837 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7838 | { |
jiabin | cd51052 | 2020-01-22 09:40:55 -0800 | [diff] [blame] | 7839 | ALOGVV("applyStreamVolumes() for device %s", dumpDeviceTypes(deviceTypes).c_str()); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7840 | for (const auto &volumeGroup : mEngine->getVolumeGroups()) { |
| 7841 | auto &curves = getVolumeCurves(toVolumeSource(volumeGroup)); |
| 7842 | checkAndSetVolume(curves, toVolumeSource(volumeGroup), |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7843 | curves.getVolumeIndex(deviceTypes), |
| 7844 | outputDesc, deviceTypes, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7845 | } |
| 7846 | } |
| 7847 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7848 | void AudioPolicyManager::setStrategyMute(product_strategy_t strategy, |
| 7849 | bool on, |
| 7850 | const sp<AudioOutputDescriptor>& outputDesc, |
| 7851 | int delayMs, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7852 | DeviceTypeSet deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7853 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7854 | std::vector<VolumeSource> sourcesToMute; |
| 7855 | for (auto attributes: mEngine->getAllAttributesForProductStrategy(strategy)) { |
| 7856 | ALOGVV("%s() attributes %s, mute %d, output ID %d", __func__, |
| 7857 | toString(attributes).c_str(), on, outputDesc->getId()); |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7858 | VolumeSource source = toVolumeSource(attributes, false); |
| 7859 | if ((source != VOLUME_SOURCE_NONE) && |
| 7860 | (std::find(begin(sourcesToMute), end(sourcesToMute), source) |
| 7861 | == end(sourcesToMute))) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7862 | sourcesToMute.push_back(source); |
| 7863 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7864 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7865 | for (auto source : sourcesToMute) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7866 | setVolumeSourceMute(source, on, outputDesc, delayMs, deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7867 | } |
| 7868 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7869 | } |
| 7870 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7871 | void AudioPolicyManager::setVolumeSourceMute(VolumeSource volumeSource, |
| 7872 | bool on, |
| 7873 | const sp<AudioOutputDescriptor>& outputDesc, |
| 7874 | int delayMs, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7875 | DeviceTypeSet deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7876 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7877 | if (deviceTypes.empty()) { |
| 7878 | deviceTypes = outputDesc->devices().types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7879 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7880 | auto &curves = getVolumeCurves(volumeSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7881 | if (on) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7882 | if (!outputDesc->isMuted(volumeSource)) { |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7883 | if (curves.canBeMuted() && |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7884 | (volumeSource != toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7885 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == |
| 7886 | AUDIO_POLICY_FORCE_NONE))) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7887 | checkAndSetVolume(curves, volumeSource, 0, outputDesc, deviceTypes, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7888 | } |
| 7889 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7890 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not |
| 7891 | // ignored |
| 7892 | outputDesc->incMuteCount(volumeSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7893 | } else { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7894 | if (!outputDesc->isMuted(volumeSource)) { |
| 7895 | ALOGV("%s unmuting non muted attributes!", __func__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7896 | return; |
| 7897 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7898 | if (outputDesc->decMuteCount(volumeSource) == 0) { |
| 7899 | checkAndSetVolume(curves, volumeSource, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7900 | curves.getVolumeIndex(deviceTypes), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7901 | outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7902 | deviceTypes, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7903 | delayMs); |
| 7904 | } |
| 7905 | } |
| 7906 | } |
| 7907 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7908 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 7909 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7910 | // has flags that map to a stream type? |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 7911 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 7912 | return true; |
| 7913 | } |
| 7914 | |
| 7915 | // has known usage? |
| 7916 | switch (paa->usage) { |
| 7917 | case AUDIO_USAGE_UNKNOWN: |
| 7918 | case AUDIO_USAGE_MEDIA: |
| 7919 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 7920 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 7921 | case AUDIO_USAGE_ALARM: |
| 7922 | case AUDIO_USAGE_NOTIFICATION: |
| 7923 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 7924 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 7925 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 7926 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 7927 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 7928 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 7929 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 7930 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 7931 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7932 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 7933 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | 21777f8 | 2019-12-06 18:12:06 -0800 | [diff] [blame] | 7934 | case AUDIO_USAGE_CALL_ASSISTANT: |
Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 7935 | case AUDIO_USAGE_EMERGENCY: |
| 7936 | case AUDIO_USAGE_SAFETY: |
| 7937 | case AUDIO_USAGE_VEHICLE_STATUS: |
| 7938 | case AUDIO_USAGE_ANNOUNCEMENT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 7939 | break; |
| 7940 | default: |
| 7941 | return false; |
| 7942 | } |
| 7943 | return true; |
| 7944 | } |
| 7945 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7946 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 7947 | { |
| 7948 | return mEngine->getForceUse(usage); |
| 7949 | } |
| 7950 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 7951 | bool AudioPolicyManager::isInCall() const { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7952 | return isStateInCall(mEngine->getPhoneState()); |
| 7953 | } |
| 7954 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 7955 | bool AudioPolicyManager::isStateInCall(int state) const { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7956 | return is_state_in_call(state); |
| 7957 | } |
| 7958 | |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 7959 | bool AudioPolicyManager::isCallAudioAccessible() const { |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 7960 | audio_mode_t mode = mEngine->getPhoneState(); |
| 7961 | return (mode == AUDIO_MODE_IN_CALL) |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 7962 | || (mode == AUDIO_MODE_CALL_SCREEN) |
| 7963 | || (mode == AUDIO_MODE_CALL_REDIRECT); |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 7964 | } |
| 7965 | |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 7966 | bool AudioPolicyManager::isInCallOrScreening() const { |
| 7967 | audio_mode_t mode = mEngine->getPhoneState(); |
| 7968 | return isStateInCall(mode) || mode == AUDIO_MODE_CALL_SCREEN; |
| 7969 | } |
| 7970 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7971 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 7972 | { |
| 7973 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 7974 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7975 | if (sourceDesc->isConnected() && (sourceDesc->srcDevice()->equals(deviceDesc) || |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 7976 | sourceDesc->sinkDevice()->equals(deviceDesc)) |
| 7977 | && !isCallRxAudioSource(sourceDesc)) { |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7978 | disconnectAudioSource(sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7979 | } |
| 7980 | } |
| 7981 | |
| 7982 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 7983 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 7984 | bool release = false; |
| 7985 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 7986 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 7987 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 7988 | source->ext.device.type == deviceDesc->type()) { |
| 7989 | release = true; |
| 7990 | } |
| 7991 | } |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7992 | const char *address = deviceDesc->address().c_str(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7993 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 7994 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 7995 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7996 | sink->ext.device.type == deviceDesc->type() && |
| 7997 | (strnlen(address, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0 |
| 7998 | || strncmp(sink->ext.device.address, address, |
| 7999 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8000 | release = true; |
| 8001 | } |
| 8002 | } |
| 8003 | if (release) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8004 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->getHandle()); |
| 8005 | releaseAudioPatch(patchDesc->getHandle(), patchDesc->getUid()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8006 | } |
| 8007 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 8008 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 8009 | mInputs.clearSessionRoutesForDevice(deviceDesc); |
| 8010 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 8011 | mHwModules.cleanUpForDevice(deviceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8012 | } |
| 8013 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8014 | void AudioPolicyManager::modifySurroundFormats( |
| 8015 | const sp<DeviceDescriptor>& devDesc, FormatVector *formatsPtr) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8016 | std::unordered_set<audio_format_t> enforcedSurround( |
| 8017 | devDesc->encodedFormats().begin(), devDesc->encodedFormats().end()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8018 | std::unordered_set<audio_format_t> allSurround; // A flat set of all known surround formats |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 8019 | for (const auto& pair : mConfig->getSurroundFormats()) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8020 | allSurround.insert(pair.first); |
| 8021 | for (const auto& subformat : pair.second) allSurround.insert(subformat); |
| 8022 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8023 | |
| 8024 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 8025 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8026 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8027 | // This is the resulting set of formats depending on the surround mode: |
| 8028 | // 'all surround' = allSurround |
| 8029 | // 'enforced surround' = enforcedSurround [may include IEC69137 which isn't raw surround fmt] |
| 8030 | // 'non-surround' = not in 'all surround' and not in 'enforced surround' |
| 8031 | // 'manual surround' = mManualSurroundFormats |
| 8032 | // AUTO: formats v 'enforced surround' |
| 8033 | // ALWAYS: formats v 'all surround' v 'enforced surround' |
| 8034 | // NEVER: formats ^ 'non-surround' |
| 8035 | // MANUAL: formats ^ ('non-surround' v 'manual surround' v (IEC69137 ^ 'enforced surround')) |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8036 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8037 | std::unordered_set<audio_format_t> formatSet; |
| 8038 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL |
| 8039 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8040 | // formatSet is (formats ^ 'non-surround') |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8041 | for (auto formatIter = formatsPtr->begin(); formatIter != formatsPtr->end(); ++formatIter) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8042 | if (allSurround.count(*formatIter) == 0 && enforcedSurround.count(*formatIter) == 0) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8043 | formatSet.insert(*formatIter); |
| 8044 | } |
| 8045 | } |
| 8046 | } else { |
| 8047 | formatSet.insert(formatsPtr->begin(), formatsPtr->end()); |
| 8048 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8049 | formatsPtr->clear(); // Re-filled from the formatSet at the end. |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8050 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 8051 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8052 | formatSet.insert(mManualSurroundFormats.begin(), mManualSurroundFormats.end()); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8053 | // Enable IEC61937 when in MANUAL mode if it's enforced for this device. |
| 8054 | if (enforcedSurround.count(AUDIO_FORMAT_IEC61937) != 0) { |
| 8055 | formatSet.insert(AUDIO_FORMAT_IEC61937); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8056 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8057 | } else if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { // AUTO or ALWAYS |
| 8058 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 8059 | formatSet.insert(allSurround.begin(), allSurround.end()); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 8060 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8061 | formatSet.insert(enforcedSurround.begin(), enforcedSurround.end()); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8062 | } |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8063 | for (const auto& format : formatSet) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8064 | formatsPtr->push_back(format); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8065 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8066 | } |
| 8067 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8068 | void AudioPolicyManager::modifySurroundChannelMasks(ChannelMaskSet *channelMasksPtr) { |
| 8069 | ChannelMaskSet &channelMasks = *channelMasksPtr; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8070 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 8071 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 8072 | |
| 8073 | // If NEVER, then remove support for channelMasks > stereo. |
| 8074 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8075 | for (auto it = channelMasks.begin(); it != channelMasks.end();) { |
| 8076 | audio_channel_mask_t channelMask = *it; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8077 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 8078 | ALOGV("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8079 | it = channelMasks.erase(it); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8080 | } else { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8081 | ++it; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8082 | } |
| 8083 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 8084 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 8085 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 8086 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8087 | bool supports5dot1 = false; |
| 8088 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 8089 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8090 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 8091 | supports5dot1 = true; |
| 8092 | break; |
| 8093 | } |
| 8094 | } |
| 8095 | // If not then add 5.1 support. |
| 8096 | if (!supports5dot1) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8097 | channelMasks.insert(AUDIO_CHANNEL_OUT_5POINT1); |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 8098 | 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] | 8099 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8100 | } |
| 8101 | } |
| 8102 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8103 | void AudioPolicyManager::updateAudioProfiles(const sp<DeviceDescriptor>& devDesc, |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 8104 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8105 | AudioProfileVector &profiles) |
| 8106 | { |
| 8107 | String8 reply; |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8108 | audio_devices_t device = devDesc->type(); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8109 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8110 | // Format MUST be checked first to update the list of AudioProfile |
| 8111 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 8112 | reply = mpClientInterface->getParameters( |
| 8113 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 8114 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 8115 | AudioParameter repliedParameters(reply); |
jiabin | f26596b | 2023-04-12 18:56:39 +0000 | [diff] [blame] | 8116 | FormatVector formats; |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 8117 | if (repliedParameters.get( |
jiabin | f26596b | 2023-04-12 18:56:39 +0000 | [diff] [blame] | 8118 | String8(AudioParameter::keyStreamSupportedFormats), reply) == NO_ERROR) { |
| 8119 | formats = formatsFromString(reply.string()); |
| 8120 | } else if (devDesc->hasValidAudioProfile()) { |
| 8121 | ALOGD("%s: using the device profiles", __func__); |
| 8122 | formats = devDesc->getAudioProfiles().getSupportedFormats(); |
| 8123 | } else { |
| 8124 | ALOGE("%s: failed to retrieve format, bailing out", __func__); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8125 | return; |
| 8126 | } |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 8127 | mReportedFormatsMap[devDesc] = formats; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8128 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 8129 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8130 | modifySurroundFormats(devDesc, &formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 8131 | } |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 8132 | addProfilesForFormats(profiles, formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8133 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8134 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 8135 | for (audio_format_t format : profiles.getSupportedFormats()) { |
jiabin | f26596b | 2023-04-12 18:56:39 +0000 | [diff] [blame] | 8136 | std::optional<ChannelMaskSet> channelMasks; |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8137 | SampleRateSet samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8138 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 8139 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8140 | |
| 8141 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 8142 | reply = mpClientInterface->getParameters( |
| 8143 | ioHandle, |
| 8144 | requestedParameters.toString() + ";" + |
| 8145 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8146 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 8147 | AudioParameter repliedParameters(reply); |
| 8148 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 8149 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 8150 | samplingRates = samplingRatesFromString(reply.string()); |
jiabin | f26596b | 2023-04-12 18:56:39 +0000 | [diff] [blame] | 8151 | } else { |
| 8152 | samplingRates = devDesc->getAudioProfiles().getSampleRatesFor(format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8153 | } |
| 8154 | } |
| 8155 | if (profiles.hasDynamicChannelsFor(format)) { |
| 8156 | reply = mpClientInterface->getParameters(ioHandle, |
| 8157 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 8158 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8159 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 8160 | AudioParameter repliedParameters(reply); |
| 8161 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 8162 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 8163 | channelMasks = channelMasksFromString(reply.string()); |
jiabin | f26596b | 2023-04-12 18:56:39 +0000 | [diff] [blame] | 8164 | } else { |
| 8165 | channelMasks = devDesc->getAudioProfiles().getChannelMasksFor(format); |
| 8166 | } |
| 8167 | if (channelMasks.has_value() && (device == AUDIO_DEVICE_OUT_HDMI |
| 8168 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD))) { |
| 8169 | modifySurroundChannelMasks(&channelMasks.value()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8170 | } |
| 8171 | } |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 8172 | addDynamicAudioProfileAndSort( |
jiabin | f26596b | 2023-04-12 18:56:39 +0000 | [diff] [blame] | 8173 | profiles, new AudioProfile( |
| 8174 | format, channelMasks.value_or(ChannelMaskSet()), samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8175 | } |
| 8176 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8177 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8178 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 8179 | audio_patch_handle_t *patchHandle, |
| 8180 | AudioIODescriptorInterface *ioDescriptor, |
| 8181 | const struct audio_patch *patch, |
| 8182 | int delayMs) |
| 8183 | { |
| 8184 | ssize_t index = mAudioPatches.indexOfKey( |
| 8185 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 8186 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 8187 | sp<AudioPatch> patchDesc; |
| 8188 | status_t status = installPatch( |
| 8189 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 8190 | if (status == NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8191 | ioDescriptor->setPatchHandle(patchDesc->getHandle()); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8192 | } |
| 8193 | return status; |
| 8194 | } |
| 8195 | |
| 8196 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 8197 | ssize_t index, |
| 8198 | audio_patch_handle_t *patchHandle, |
| 8199 | const struct audio_patch *patch, |
| 8200 | int delayMs, |
| 8201 | uid_t uid, |
| 8202 | sp<AudioPatch> *patchDescPtr) |
| 8203 | { |
| 8204 | sp<AudioPatch> patchDesc; |
| 8205 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 8206 | if (index >= 0) { |
| 8207 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8208 | afPatchHandle = patchDesc->getAfHandle(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8209 | } |
| 8210 | |
| 8211 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 8212 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 8213 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 8214 | if (status == NO_ERROR) { |
| 8215 | if (index < 0) { |
| 8216 | patchDesc = new AudioPatch(patch, uid); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8217 | addAudioPatch(patchDesc->getHandle(), patchDesc); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8218 | } else { |
| 8219 | patchDesc->mPatch = *patch; |
| 8220 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8221 | patchDesc->setAfHandle(afPatchHandle); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8222 | if (patchHandle) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8223 | *patchHandle = patchDesc->getHandle(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8224 | } |
| 8225 | nextAudioPortGeneration(); |
| 8226 | mpClientInterface->onAudioPatchListUpdate(); |
| 8227 | } |
| 8228 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 8229 | return status; |
| 8230 | } |
| 8231 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8232 | bool AudioPolicyManager::areAllActiveTracksRerouted(const sp<SwAudioOutputDescriptor>& output) |
| 8233 | { |
| 8234 | const TrackClientVector activeClients = output->getActiveClients(); |
| 8235 | if (activeClients.empty()) { |
| 8236 | return true; |
| 8237 | } |
| 8238 | ssize_t index = mAudioPatches.indexOfKey(output->getPatchHandle()); |
| 8239 | if (index < 0) { |
| 8240 | ALOGE("%s, no audio patch found while there are active clients on output %d", |
| 8241 | __func__, output->getId()); |
| 8242 | return false; |
| 8243 | } |
| 8244 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 8245 | DeviceVector routedDevices; |
| 8246 | for (int i = 0; i < patchDesc->mPatch.num_sinks; ++i) { |
| 8247 | sp<DeviceDescriptor> device = mAvailableOutputDevices.getDeviceFromId( |
| 8248 | patchDesc->mPatch.sinks[i].id); |
| 8249 | if (device == nullptr) { |
| 8250 | ALOGE("%s, no audio device found with id(%d)", |
| 8251 | __func__, patchDesc->mPatch.sinks[i].id); |
| 8252 | return false; |
| 8253 | } |
| 8254 | routedDevices.add(device); |
| 8255 | } |
| 8256 | for (const auto& client : activeClients) { |
jiabin | 4925685 | 2022-03-09 11:21:35 -0800 | [diff] [blame] | 8257 | if (client->isInvalid()) { |
| 8258 | // No need to take care about invalidated clients. |
| 8259 | continue; |
| 8260 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8261 | sp<DeviceDescriptor> preferredDevice = |
| 8262 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId()); |
| 8263 | if (mEngine->getOutputDevicesForAttributes( |
| 8264 | client->attributes(), preferredDevice, false) == routedDevices) { |
| 8265 | return false; |
| 8266 | } |
| 8267 | } |
| 8268 | return true; |
| 8269 | } |
| 8270 | |
| 8271 | sp<SwAudioOutputDescriptor> AudioPolicyManager::openOutputWithProfileAndDevice( |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 8272 | const sp<IOProfile>& profile, const DeviceVector& devices, |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8273 | const audio_config_base_t *mixerConfig, const audio_config_t *halConfig, |
| 8274 | audio_output_flags_t flags) |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8275 | { |
| 8276 | for (const auto& device : devices) { |
| 8277 | // TODO: This should be checking if the profile supports the device combo. |
| 8278 | if (!profile->supportsDevice(device)) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8279 | ALOGE("%s profile(%s) doesn't support device %#x", __func__, profile->getName().c_str(), |
| 8280 | device->type()); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8281 | return nullptr; |
| 8282 | } |
| 8283 | } |
| 8284 | sp<SwAudioOutputDescriptor> desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 8285 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8286 | status_t status = desc->open(halConfig, mixerConfig, devices, |
| 8287 | AUDIO_STREAM_DEFAULT, flags, &output); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8288 | if (status != NO_ERROR) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8289 | ALOGE("%s failed to open output %d", __func__, status); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8290 | return nullptr; |
| 8291 | } |
| 8292 | |
| 8293 | // Here is where the out_set_parameters() for card & device gets called |
| 8294 | sp<DeviceDescriptor> device = devices.getDeviceForOpening(); |
| 8295 | const audio_devices_t deviceType = device->type(); |
| 8296 | const String8 &address = String8(device->address().c_str()); |
| 8297 | if (!address.isEmpty()) { |
| 8298 | char *param = audio_device_address_to_parameter(deviceType, address.c_str()); |
| 8299 | mpClientInterface->setParameters(output, String8(param)); |
| 8300 | free(param); |
| 8301 | } |
| 8302 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
| 8303 | if (!profile->hasValidAudioProfile()) { |
| 8304 | ALOGW("%s() missing param", __func__); |
| 8305 | desc->close(); |
| 8306 | return nullptr; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8307 | } else if (profile->hasDynamicAudioProfile() && halConfig == nullptr) { |
| 8308 | // Reopen the output with the best audio profile picked by APM when the profile supports |
| 8309 | // dynamic audio profile and the hal config is not specified. |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8310 | desc->close(); |
| 8311 | output = AUDIO_IO_HANDLE_NONE; |
| 8312 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 8313 | profile->pickAudioProfile( |
| 8314 | config.sample_rate, config.channel_mask, config.format); |
| 8315 | config.offload_info.sample_rate = config.sample_rate; |
| 8316 | config.offload_info.channel_mask = config.channel_mask; |
| 8317 | config.offload_info.format = config.format; |
| 8318 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8319 | status = desc->open(&config, mixerConfig, devices, AUDIO_STREAM_DEFAULT, flags, &output); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8320 | if (status != NO_ERROR) { |
| 8321 | return nullptr; |
| 8322 | } |
| 8323 | } |
| 8324 | |
| 8325 | addOutput(output, desc); |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 8326 | |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 8327 | sp<DeviceDescriptor> speaker = mAvailableOutputDevices.getDevice( |
| 8328 | AUDIO_DEVICE_OUT_SPEAKER, String8(""), AUDIO_FORMAT_DEFAULT); |
| 8329 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8330 | if (audio_is_remote_submix_device(deviceType) && address != "0") { |
| 8331 | sp<AudioPolicyMix> policyMix; |
| 8332 | if (mPolicyMixes.getAudioPolicyMix(deviceType, address, policyMix) == NO_ERROR) { |
| 8333 | policyMix->setOutput(desc); |
| 8334 | desc->mPolicyMix = policyMix; |
| 8335 | } else { |
| 8336 | ALOGW("checkOutputsForDevice() cannot find policy for address %s", |
| 8337 | address.string()); |
| 8338 | } |
| 8339 | |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 8340 | } else if (hasPrimaryOutput() && speaker != nullptr |
| 8341 | && mPrimaryOutput->supportsDevice(speaker) && !desc->supportsDevice(speaker) |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 8342 | && ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) { |
| 8343 | // no duplicated output for: |
| 8344 | // - direct outputs |
| 8345 | // - outputs used by dynamic policy mixes |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 8346 | // - outputs that supports SPEAKER while the primary output does not. |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8347 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
| 8348 | |
| 8349 | //TODO: configure audio effect output stage here |
| 8350 | |
| 8351 | // open a duplicating output thread for the new output and the primary output |
| 8352 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 8353 | new SwAudioOutputDescriptor(nullptr, mpClientInterface); |
| 8354 | status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, &duplicatedOutput); |
| 8355 | if (status == NO_ERROR) { |
| 8356 | // add duplicated output descriptor |
| 8357 | addOutput(duplicatedOutput, dupOutputDesc); |
| 8358 | } else { |
| 8359 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
| 8360 | mPrimaryOutput->mIoHandle, output); |
| 8361 | desc->close(); |
| 8362 | removeOutput(output); |
| 8363 | nextAudioPortGeneration(); |
| 8364 | return nullptr; |
| 8365 | } |
| 8366 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 8367 | if (mPrimaryOutput == nullptr && profile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 8368 | ALOGV("%s(): re-assigning mPrimaryOutput", __func__); |
| 8369 | mPrimaryOutput = desc; |
| 8370 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8371 | return desc; |
| 8372 | } |
| 8373 | |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 8374 | status_t AudioPolicyManager::getDevicesForAttributes( |
| 8375 | const audio_attributes_t &attr, DeviceVector &devices, bool forVolume) { |
| 8376 | // Devices are determined in the following precedence: |
| 8377 | // |
| 8378 | // 1) Devices associated with a dynamic policy matching the attributes. This is often |
| 8379 | // a remote submix from MIX_ROUTE_FLAG_LOOP_BACK. |
| 8380 | // |
| 8381 | // If no such dynamic policy then |
| 8382 | // 2) Devices containing an active client using setPreferredDevice |
| 8383 | // with same strategy as the attributes. |
| 8384 | // (from the default Engine::getOutputDevicesForAttributes() implementation). |
| 8385 | // |
| 8386 | // If no corresponding active client with setPreferredDevice then |
| 8387 | // 3) Devices associated with the strategy determined by the attributes |
| 8388 | // (from the default Engine::getOutputDevicesForAttributes() implementation). |
| 8389 | // |
| 8390 | // See related getOutputForAttrInt(). |
| 8391 | |
| 8392 | // check dynamic policies but only for primary descriptors (secondary not used for audible |
| 8393 | // audio routing, only used for duplication for playback capture) |
| 8394 | sp<AudioPolicyMix> policyMix; |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 8395 | bool unneededUsePrimaryOutputFromPolicyMixes = false; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 8396 | status_t status = mPolicyMixes.getOutputForAttr(attr, AUDIO_CONFIG_BASE_INITIALIZER, |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 8397 | 0 /*uid unknown here*/, AUDIO_SESSION_NONE, AUDIO_OUTPUT_FLAG_NONE, |
| 8398 | mAvailableOutputDevices, nullptr /* requestedDevice */, policyMix, |
| 8399 | nullptr /* secondaryMixes */, unneededUsePrimaryOutputFromPolicyMixes); |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 8400 | if (status != OK) { |
| 8401 | return status; |
| 8402 | } |
| 8403 | |
| 8404 | if (policyMix != nullptr && policyMix->getOutput() != nullptr && |
| 8405 | // For volume control, skip LOOPBACK mixes which use AUDIO_DEVICE_OUT_REMOTE_SUBMIX |
| 8406 | // as they are unaffected by device/stream volume |
| 8407 | // (per SwAudioOutputDescriptor::isFixedVolume()). |
| 8408 | (!forVolume || policyMix->mDeviceType != AUDIO_DEVICE_OUT_REMOTE_SUBMIX) |
| 8409 | ) { |
| 8410 | sp<DeviceDescriptor> deviceDesc = mAvailableOutputDevices.getDevice( |
| 8411 | policyMix->mDeviceType, policyMix->mDeviceAddress, AUDIO_FORMAT_DEFAULT); |
| 8412 | devices.add(deviceDesc); |
| 8413 | } else { |
| 8414 | // The default Engine::getOutputDevicesForAttributes() uses findPreferredDevice() |
| 8415 | // which selects setPreferredDevice if active. This means forVolume call |
| 8416 | // will take an active setPreferredDevice, if such exists. |
| 8417 | |
| 8418 | devices = mEngine->getOutputDevicesForAttributes( |
| 8419 | attr, nullptr /* preferredDevice */, false /* fromCache */); |
| 8420 | } |
| 8421 | |
| 8422 | if (forVolume) { |
| 8423 | // We alias the device AUDIO_DEVICE_OUT_SPEAKER_SAFE to AUDIO_DEVICE_OUT_SPEAKER |
| 8424 | // for single volume control in AudioService (such relationship should exist if |
| 8425 | // SPEAKER_SAFE is present). |
| 8426 | // |
| 8427 | // (This is unrelated to a different device grouping as Volume::getDeviceCategory) |
| 8428 | DeviceVector speakerSafeDevices = |
| 8429 | devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER_SAFE); |
| 8430 | if (!speakerSafeDevices.isEmpty()) { |
| 8431 | devices.merge(mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER)); |
| 8432 | devices.remove(speakerSafeDevices); |
| 8433 | } |
| 8434 | } |
| 8435 | |
| 8436 | return NO_ERROR; |
| 8437 | } |
| 8438 | |
| 8439 | status_t AudioPolicyManager::getProfilesForDevices(const DeviceVector& devices, |
| 8440 | AudioProfileVector& audioProfiles, |
| 8441 | uint32_t flags, |
| 8442 | bool isInput) { |
| 8443 | for (const auto& hwModule : mHwModules) { |
| 8444 | // the MSD module checks for different conditions |
| 8445 | if (strcmp(hwModule->getName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0) { |
| 8446 | continue; |
| 8447 | } |
| 8448 | IOProfileCollection ioProfiles = isInput ? hwModule->getInputProfiles() |
| 8449 | : hwModule->getOutputProfiles(); |
| 8450 | for (const auto& profile : ioProfiles) { |
| 8451 | if (!profile->areAllDevicesSupported(devices) || |
| 8452 | !profile->isCompatibleProfileForFlags( |
| 8453 | flags, false /*exactMatchRequiredForInputFlags*/)) { |
| 8454 | continue; |
| 8455 | } |
| 8456 | audioProfiles.addAllValidProfiles(profile->asAudioPort()->getAudioProfiles()); |
| 8457 | } |
| 8458 | } |
| 8459 | |
| 8460 | if (!isInput) { |
| 8461 | // add the direct profiles from MSD if present and has audio patches to all the output(s) |
| 8462 | const auto &msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 8463 | if (msdModule != nullptr) { |
| 8464 | if (msdHasPatchesToAllDevices(devices.toTypeAddrVector())) { |
| 8465 | ALOGV("%s: MSD audio patches set to all output devices.", __func__); |
| 8466 | for (const auto &profile: msdModule->getOutputProfiles()) { |
| 8467 | if (!profile->asAudioPort()->isDirectOutput()) { |
| 8468 | continue; |
| 8469 | } |
| 8470 | audioProfiles.addAllValidProfiles(profile->asAudioPort()->getAudioProfiles()); |
| 8471 | } |
| 8472 | } else { |
| 8473 | ALOGV("%s: MSD audio patches NOT set to all output devices.", __func__); |
| 8474 | } |
| 8475 | } |
| 8476 | } |
| 8477 | |
| 8478 | return NO_ERROR; |
| 8479 | } |
| 8480 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 8481 | sp<SwAudioOutputDescriptor> AudioPolicyManager::reopenOutput(sp<SwAudioOutputDescriptor> outputDesc, |
| 8482 | const audio_config_t *config, |
| 8483 | audio_output_flags_t flags, |
| 8484 | const char* caller) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8485 | closeOutput(outputDesc->mIoHandle); |
| 8486 | sp<SwAudioOutputDescriptor> preferredOutput = openOutputWithProfileAndDevice( |
| 8487 | outputDesc->mProfile, outputDesc->devices(), nullptr /*mixerConfig*/, config, flags); |
| 8488 | if (preferredOutput == nullptr) { |
| 8489 | ALOGE("%s failed to reopen output device=%d, caller=%s", |
| 8490 | __func__, outputDesc->devices()[0]->getId(), caller); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8491 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 8492 | return preferredOutput; |
| 8493 | } |
| 8494 | |
| 8495 | void AudioPolicyManager::reopenOutputsWithDevices( |
| 8496 | const std::map<audio_io_handle_t, DeviceVector> &outputsToReopen) { |
| 8497 | for (const auto& [output, devices] : outputsToReopen) { |
| 8498 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 8499 | closeOutput(output); |
| 8500 | openOutputWithProfileAndDevice(desc->mProfile, devices); |
| 8501 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8502 | } |
| 8503 | |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 8504 | PortHandleVector AudioPolicyManager::getClientsForStream( |
| 8505 | audio_stream_type_t streamType) const { |
| 8506 | PortHandleVector clients; |
| 8507 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 8508 | PortHandleVector clientsForStream = mOutputs.valueAt(i)->getClientsForStream(streamType); |
| 8509 | clients.insert(clients.end(), clientsForStream.begin(), clientsForStream.end()); |
| 8510 | } |
| 8511 | return clients; |
| 8512 | } |
| 8513 | |
| 8514 | void AudioPolicyManager::invalidateStreams(StreamTypeVector streams) const { |
| 8515 | PortHandleVector clients; |
| 8516 | for (auto stream : streams) { |
| 8517 | PortHandleVector clientsForStream = getClientsForStream(stream); |
| 8518 | clients.insert(clients.end(), clientsForStream.begin(), clientsForStream.end()); |
| 8519 | } |
| 8520 | mpClientInterface->invalidateTracks(clients); |
| 8521 | } |
| 8522 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 8523 | } // namespace android |