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 | |
Jan Sebechlebsky | 0af8e87 | 2023-08-11 14:45:08 +0200 | [diff] [blame] | 17 | #include "utils/Errors.h" |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 18 | #define LOG_TAG "APM_AudioPolicyManager" |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 19 | |
| 20 | // Need to keep the log statements even in production builds |
Eric Laurent | 7ee1437 | 2024-01-23 11:57:46 +0100 | [diff] [blame] | 21 | // to enable VERBOSE logging dynamically. |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 22 | // You can enable VERBOSE logging as follows: |
| 23 | // adb shell setprop log.tag.APM_AudioPolicyManager V |
| 24 | #define LOG_NDEBUG 0 |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 25 | |
| 26 | //#define VERY_VERBOSE_LOGGING |
| 27 | #ifdef VERY_VERBOSE_LOGGING |
| 28 | #define ALOGVV ALOGV |
| 29 | #else |
| 30 | #define ALOGVV(a...) do { } while(0) |
| 31 | #endif |
| 32 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 33 | #include <algorithm> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 34 | #include <inttypes.h> |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 35 | #include <map> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 36 | #include <math.h> |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 37 | #include <set> |
Atneya Nair | 0f0a803 | 2022-12-12 16:20:12 -0800 | [diff] [blame] | 38 | #include <type_traits> |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 39 | #include <unordered_set> |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 40 | #include <vector> |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 41 | |
| 42 | #include <Serializer.h> |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 43 | #include <android/media/audio/common/AudioPort.h> |
Andy Hung | 481bfe3 | 2023-12-18 14:00:29 -0800 | [diff] [blame] | 44 | #include <com_android_media_audio.h> |
Marvin Ramin | bdefaf0 | 2023-11-01 09:10:32 +0100 | [diff] [blame] | 45 | #include <android_media_audiopolicy.h> |
Atneya Nair | b16666a | 2023-12-11 20:18:33 -0800 | [diff] [blame] | 46 | #include <com_android_media_audioserver.h> |
Glenn Kasten | 76a1344 | 2020-07-01 12:10:59 -0700 | [diff] [blame] | 47 | #include <cutils/bitops.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 48 | #include <cutils/properties.h> |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 49 | #include <media/AudioParameter.h> |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 50 | #include <policy.h> |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 51 | #include <private/android_filesystem_config.h> |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 52 | #include <system/audio.h> |
Mikhail Naganov | 27cf37c | 2020-04-14 14:47:01 -0700 | [diff] [blame] | 53 | #include <system/audio_config.h> |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 54 | #include <system/audio_effects/effect_hapticgenerator.h> |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 55 | #include <utils/Log.h> |
| 56 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 57 | #include "AudioPolicyManager.h" |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 58 | #include "TypeConverter.h" |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 59 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 60 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 61 | |
Marvin Ramin | bdefaf0 | 2023-11-01 09:10:32 +0100 | [diff] [blame] | 62 | |
| 63 | namespace audio_flags = android::media::audiopolicy; |
| 64 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 65 | using android::media::audio::common::AudioDevice; |
| 66 | using android::media::audio::common::AudioDeviceAddress; |
| 67 | using android::media::audio::common::AudioPortDeviceExt; |
| 68 | using android::media::audio::common::AudioPortExt; |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 69 | using content::AttributionSourceState; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 70 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 71 | //FIXME: workaround for truncated touch sounds |
| 72 | // to be removed when the problem is handled by system UI |
| 73 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 74 | |
| 75 | // Largest difference in dB on earpiece in call between the voice volume and another |
| 76 | // media / notification / system volume. |
| 77 | constexpr float IN_CALL_EARPIECE_HEADROOM_DB = 3.f; |
| 78 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 79 | template <typename T> |
| 80 | bool operator== (const SortedVector<T> &left, const SortedVector<T> &right) |
| 81 | { |
| 82 | if (left.size() != right.size()) { |
| 83 | return false; |
| 84 | } |
| 85 | for (size_t index = 0; index < right.size(); index++) { |
| 86 | if (left[index] != right[index]) { |
| 87 | return false; |
| 88 | } |
| 89 | } |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | template <typename T> |
| 94 | bool operator!= (const SortedVector<T> &left, const SortedVector<T> &right) |
| 95 | { |
| 96 | return !(left == right); |
| 97 | } |
| 98 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 99 | // ---------------------------------------------------------------------------- |
| 100 | // AudioPolicyInterface implementation |
| 101 | // ---------------------------------------------------------------------------- |
| 102 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 103 | status_t AudioPolicyManager::setDeviceConnectionState(audio_policy_dev_state_t state, |
| 104 | const android::media::audio::common::AudioPort& port, audio_format_t encodedFormat) { |
| 105 | status_t status = setDeviceConnectionStateInt(state, port, encodedFormat); |
jiabin | a7b4379 | 2018-02-15 16:04:46 -0800 | [diff] [blame] | 106 | nextAudioPortGeneration(); |
| 107 | return status; |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 110 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
| 111 | audio_policy_dev_state_t state, |
| 112 | const char* device_address, |
| 113 | const char* device_name, |
| 114 | audio_format_t encodedFormat) { |
Atneya Nair | 638a6e4 | 2022-12-18 16:45:15 -0800 | [diff] [blame] | 115 | media::AudioPortFw aidlPort; |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 116 | if (status_t status = deviceToAudioPort(device, device_address, device_name, &aidlPort); |
| 117 | status == OK) { |
| 118 | return setDeviceConnectionState(state, aidlPort.hal, encodedFormat); |
| 119 | } else { |
| 120 | ALOGE("Failed to convert to AudioPort Parcelable: %s", statusToString(status).c_str()); |
| 121 | return status; |
| 122 | } |
| 123 | } |
| 124 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 125 | void AudioPolicyManager::broadcastDeviceConnectionState(const sp<DeviceDescriptor> &device, |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 126 | media::DeviceConnectedState state) |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 127 | { |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 128 | audio_port_v7 devicePort; |
| 129 | device->toAudioPort(&devicePort); |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 130 | if (status_t status = mpClientInterface->setDeviceConnectedState(&devicePort, state); |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 131 | status != OK) { |
Andy Hung | f7786a4 | 2024-02-08 21:19:47 -0800 | [diff] [blame] | 132 | ALOGE("Error %d while setting connected state for device %s", |
| 133 | static_cast<int>(state), |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 134 | device->getDeviceTypeAddr().toString(false).c_str()); |
| 135 | } |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 138 | status_t AudioPolicyManager::setDeviceConnectionStateInt( |
| 139 | audio_policy_dev_state_t state, const android::media::audio::common::AudioPort& port, |
| 140 | audio_format_t encodedFormat) { |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 141 | if (port.ext.getTag() != AudioPortExt::device) { |
| 142 | return BAD_VALUE; |
| 143 | } |
| 144 | audio_devices_t device_type; |
| 145 | std::string device_address; |
| 146 | if (status_t status = aidl2legacy_AudioDevice_audio_device( |
| 147 | port.ext.get<AudioPortExt::device>().device, &device_type, &device_address); |
| 148 | status != OK) { |
| 149 | return status; |
| 150 | }; |
| 151 | const char* device_name = port.name.c_str(); |
| 152 | // connect/disconnect only 1 device at a time |
| 153 | if (!audio_is_output_device(device_type) && !audio_is_input_device(device_type)) |
| 154 | return BAD_VALUE; |
| 155 | |
| 156 | sp<DeviceDescriptor> device = mHwModules.getDeviceDescriptor( |
| 157 | device_type, device_address.c_str(), device_name, encodedFormat, |
| 158 | state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Mikhail Naganov | ddc5f31 | 2022-06-11 00:47:52 +0000 | [diff] [blame] | 159 | if (device == nullptr) { |
| 160 | return INVALID_OPERATION; |
| 161 | } |
| 162 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 163 | device->setExtraAudioDescriptors(port.extraAudioDescriptors); |
| 164 | } |
| 165 | return setDeviceConnectionStateInt(device, state); |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 166 | } |
| 167 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 168 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t deviceType, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 169 | audio_policy_dev_state_t state, |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 170 | const char* device_address, |
| 171 | const char* device_name, |
| 172 | audio_format_t encodedFormat) { |
Atneya Nair | 638a6e4 | 2022-12-18 16:45:15 -0800 | [diff] [blame] | 173 | media::AudioPortFw aidlPort; |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 174 | if (status_t status = deviceToAudioPort(deviceType, device_address, device_name, &aidlPort); |
| 175 | status == OK) { |
| 176 | return setDeviceConnectionStateInt(state, aidlPort.hal, encodedFormat); |
| 177 | } else { |
| 178 | ALOGE("Failed to convert to AudioPort Parcelable: %s", statusToString(status).c_str()); |
| 179 | return status; |
| 180 | } |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 181 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 182 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 183 | status_t AudioPolicyManager::setDeviceConnectionStateInt(const sp<DeviceDescriptor> &device, |
| 184 | audio_policy_dev_state_t state) |
| 185 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 186 | // handle output devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 187 | if (audio_is_output_device(device->type())) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 188 | SortedVector <audio_io_handle_t> outputs; |
| 189 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 190 | ssize_t index = mAvailableOutputDevices.indexOf(device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 191 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 192 | // save a copy of the opened output descriptors before any output is opened or closed |
| 193 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 194 | mPreviousOutputs = mOutputs; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 195 | |
| 196 | bool wasLeUnicastActive = isLeUnicastActive(); |
| 197 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 198 | switch (state) |
| 199 | { |
| 200 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 201 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 202 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 203 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 204 | return INVALID_OPERATION; |
| 205 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 206 | ALOGV("%s() connecting device %s format %x", |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 207 | __func__, device->toString().c_str(), device->getEncodedFormat()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 208 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 209 | // register new device as available |
Francois Gaffie | 993f390 | 2019-04-10 15:39:27 +0200 | [diff] [blame] | 210 | if (mAvailableOutputDevices.add(device) < 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 211 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 212 | } |
| 213 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 214 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 215 | // parameters on newly connected devices (instead of opening the outputs...) |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 216 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::CONNECTED); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 217 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 218 | if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) { |
| 219 | mAvailableOutputDevices.remove(device); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 220 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 221 | mHwModules.cleanUpForDevice(device); |
| 222 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 223 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 224 | return INVALID_OPERATION; |
| 225 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 226 | |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 227 | // Populate encapsulation information when a output device is connected. |
| 228 | device->setEncapsulationInfoFromHal(mpClientInterface); |
| 229 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 230 | // outputs should never be empty here |
| 231 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 232 | "checkOutputsForDevice() returned no outputs but status OK"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 233 | ALOGV("%s() checkOutputsForDevice() returned %zu outputs", __func__, outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 234 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 235 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 236 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 237 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 238 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 239 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 240 | return INVALID_OPERATION; |
| 241 | } |
| 242 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 243 | ALOGV("%s() disconnecting output device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 244 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 245 | // Notify the HAL to prepare to disconnect device |
| 246 | broadcastDeviceConnectionState( |
| 247 | device, media::DeviceConnectedState::PREPARE_TO_DISCONNECT); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 248 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 249 | // remove device from available output devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 250 | mAvailableOutputDevices.remove(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 251 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 252 | mOutputs.clearSessionRoutesForDevice(device); |
| 253 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 254 | checkOutputsForDevice(device, state, outputs); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 255 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 256 | // Send Disconnect to HALs |
| 257 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED); |
| 258 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 259 | // Reset active device codec |
| 260 | device->setEncodedFormat(AUDIO_FORMAT_DEFAULT); |
| 261 | |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 262 | // remove device from mReportedFormatsMap cache |
| 263 | mReportedFormatsMap.erase(device); |
| 264 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 265 | // remove preferred mixer configurations |
| 266 | mPreferredMixerAttrInfos.erase(device->getId()); |
| 267 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 268 | } break; |
| 269 | |
| 270 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 271 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 272 | return BAD_VALUE; |
| 273 | } |
| 274 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 275 | // Propagate device availability to Engine |
| 276 | setEngineDeviceConnectionState(device, state); |
| 277 | |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 278 | // No need to evaluate playback routing when connecting a remote submix |
| 279 | // output device used by a dynamic policy of type recorder as no |
| 280 | // playback use case is affected. |
| 281 | bool doCheckForDeviceAndOutputChanges = true; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 282 | if (device->type() == AUDIO_DEVICE_OUT_REMOTE_SUBMIX && device->address() != "0") { |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 283 | for (audio_io_handle_t output : outputs) { |
| 284 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 285 | sp<AudioPolicyMix> policyMix = desc->mPolicyMix.promote(); |
| 286 | if (policyMix != nullptr |
| 287 | && policyMix->mMixType == MIX_TYPE_RECORDERS |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 288 | && device->address() == policyMix->mDeviceAddress.c_str()) { |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 289 | doCheckForDeviceAndOutputChanges = false; |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | auto checkCloseOutputs = [&]() { |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 296 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 297 | if (!outputs.isEmpty()) { |
| 298 | for (audio_io_handle_t output : outputs) { |
| 299 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 300 | // close unused outputs after device disconnection or direct outputs that have |
| 301 | // been opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 302 | // "outputs" vector never contains duplicated outputs |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 303 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) |
| 304 | || (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 305 | (desc->mDirectOpenCount == 0)) |
| 306 | || (((desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) && |
| 307 | !isOutputOnlyAvailableRouteToSomeDevice(desc))) { |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 308 | clearAudioSourcesForOutput(output); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 309 | closeOutput(output); |
| 310 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 311 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 312 | // check A2DP again after closing A2DP output to reset mA2dpSuspended if needed |
| 313 | return true; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 314 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 315 | return false; |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 316 | }; |
| 317 | |
| 318 | if (doCheckForDeviceAndOutputChanges) { |
| 319 | checkForDeviceAndOutputChanges(checkCloseOutputs); |
| 320 | } else { |
| 321 | checkCloseOutputs(); |
| 322 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 323 | (void)updateCallRouting(false /*fromCache*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 324 | const DeviceVector msdOutDevices = getMsdAudioOutDevices(); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 325 | const DeviceVector activeMediaDevices = |
| 326 | mEngine->getActiveMediaDevices(mAvailableOutputDevices); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 327 | std::map<audio_io_handle_t, DeviceVector> outputsToReopenWithDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 328 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 329 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jaideep Sharma | 89b5b85 | 2020-11-23 16:41:33 +0530 | [diff] [blame] | 330 | if (desc->isActive() && ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || |
| 331 | (desc != mPrimaryOutput))) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 332 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 333 | // do not force device change on duplicated output because if device is 0, it will |
| 334 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 335 | // a valid device selection on those outputs. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 336 | bool force = (msdOutDevices.isEmpty() || msdOutDevices != desc->devices()) |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 337 | && !desc->isDuplicated() |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 338 | && (!device_distinguishes_on_address(device->type()) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 339 | // always force when disconnecting (a non-duplicated device) |
| 340 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 341 | if (desc->mUsePreferredMixerAttributes && newDevices != desc->devices()) { |
| 342 | // If the device is using preferred mixer attributes, the output need to reopen |
| 343 | // with default configuration when the new selected devices are different from |
| 344 | // current routing devices |
| 345 | outputsToReopenWithDevices.emplace(mOutputs.keyAt(i), newDevices); |
| 346 | continue; |
| 347 | } |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 348 | setOutputDevices(__func__, desc, newDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 349 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 350 | if (!desc->isDuplicated() && desc->mProfile->hasDynamicAudioProfile() && |
jiabin | 498d215 | 2021-04-02 00:26:46 +0000 | [diff] [blame] | 351 | !activeMediaDevices.empty() && desc->devices() != activeMediaDevices && |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 352 | desc->supportsDevicesForPlayback(activeMediaDevices)) { |
| 353 | // Reopen the output to query the dynamic profiles when there is not active |
| 354 | // clients or all active clients will be rerouted. Otherwise, set the flag |
| 355 | // `mPendingReopenToQueryProfiles` in the SwOutputDescriptor so that the output |
| 356 | // can be reopened to query dynamic profiles when all clients are inactive. |
| 357 | if (areAllActiveTracksRerouted(desc)) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 358 | outputsToReopenWithDevices.emplace(mOutputs.keyAt(i), activeMediaDevices); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 359 | } else { |
| 360 | desc->mPendingReopenToQueryProfiles = true; |
| 361 | } |
| 362 | } |
| 363 | if (!desc->supportsDevicesForPlayback(activeMediaDevices)) { |
| 364 | // Clear the flag that previously set for re-querying profiles. |
| 365 | desc->mPendingReopenToQueryProfiles = false; |
| 366 | } |
| 367 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 368 | reopenOutputsWithDevices(outputsToReopenWithDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 369 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 370 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 371 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 374 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, 0); |
| 375 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 376 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 377 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 378 | } // end if is output device |
| 379 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 380 | // handle input devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 381 | if (audio_is_input_device(device->type())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 382 | ssize_t index = mAvailableInputDevices.indexOf(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 383 | switch (state) |
| 384 | { |
| 385 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 386 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 387 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 388 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 389 | return INVALID_OPERATION; |
| 390 | } |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 391 | |
| 392 | if (mAvailableInputDevices.add(device) < 0) { |
| 393 | return NO_MEMORY; |
| 394 | } |
| 395 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 396 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 397 | // parameters on newly connected devices (instead of opening the inputs...) |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 398 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::CONNECTED); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 399 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 400 | if (checkInputsForDevice(device, state) != NO_ERROR) { |
| 401 | mAvailableInputDevices.remove(device); |
| 402 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 403 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 404 | |
| 405 | mHwModules.cleanUpForDevice(device); |
| 406 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 407 | return INVALID_OPERATION; |
| 408 | } |
| 409 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 410 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 411 | |
| 412 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 413 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 414 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 415 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 416 | return INVALID_OPERATION; |
| 417 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 418 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 419 | ALOGV("%s() disconnecting input device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 420 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 421 | // Notify the HAL to prepare to disconnect device |
| 422 | broadcastDeviceConnectionState( |
| 423 | device, media::DeviceConnectedState::PREPARE_TO_DISCONNECT); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 424 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 425 | mAvailableInputDevices.remove(device); |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 426 | |
| 427 | checkInputsForDevice(device, state); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 428 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 429 | // Set Disconnect to HALs |
| 430 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED); |
| 431 | |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 432 | // remove device from mReportedFormatsMap cache |
| 433 | mReportedFormatsMap.erase(device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 434 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 435 | |
| 436 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 437 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 438 | return BAD_VALUE; |
| 439 | } |
| 440 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 441 | // Propagate device availability to Engine |
| 442 | setEngineDeviceConnectionState(device, state); |
| 443 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 444 | checkCloseInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 445 | // As the input device list can impact the output device selection, update |
| 446 | // getDeviceForStrategy() cache |
| 447 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 448 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 449 | (void)updateCallRouting(false /*fromCache*/); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 450 | // Reconnect Audio Source |
| 451 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { |
| 452 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); |
| 453 | checkAudioSourceForAttributes(attributes); |
| 454 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 455 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 456 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 459 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 460 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 461 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 462 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 463 | ALOGW("%s() invalid device: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 464 | return BAD_VALUE; |
| 465 | } |
| 466 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 467 | status_t AudioPolicyManager::deviceToAudioPort(audio_devices_t device, const char* device_address, |
| 468 | const char* device_name, |
Atneya Nair | 638a6e4 | 2022-12-18 16:45:15 -0800 | [diff] [blame] | 469 | media::AudioPortFw* aidlPort) { |
Andy Hung | 5b9a611 | 2023-08-09 19:56:57 -0700 | [diff] [blame] | 470 | const auto devDescr = sp<DeviceDescriptorBase>::make(device, device_address); |
| 471 | devDescr->setName(device_name); |
| 472 | return devDescr->writeToParcelable(aidlPort); |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 473 | } |
| 474 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 475 | void AudioPolicyManager::setEngineDeviceConnectionState(const sp<DeviceDescriptor> device, |
| 476 | audio_policy_dev_state_t state) { |
| 477 | |
| 478 | // the Engine does not have to know about remote submix devices used by dynamic audio policies |
| 479 | if (audio_is_remote_submix_device(device->type()) && device->address() != "0") { |
| 480 | return; |
| 481 | } |
| 482 | mEngine->setDeviceConnectionState(device, state); |
| 483 | } |
| 484 | |
| 485 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 486 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 487 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 488 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 489 | sp<DeviceDescriptor> devDesc = |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 490 | mHwModules.getDeviceDescriptor(device, device_address, "", AUDIO_FORMAT_DEFAULT, |
| 491 | false /* allowToCreate */, |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 492 | (strlen(device_address) != 0)/*matchAddress*/); |
| 493 | |
| 494 | if (devDesc == 0) { |
François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 495 | ALOGV("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 496 | device, device_address); |
| 497 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 498 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 499 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 500 | DeviceVector *deviceVector; |
| 501 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 502 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 503 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 504 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 505 | deviceVector = &mAvailableInputDevices; |
| 506 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 507 | ALOGW("%s() invalid device type %08x", __func__, device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 508 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 509 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 510 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 511 | return (deviceVector->getDevice( |
| 512 | device, String8(device_address), AUDIO_FORMAT_DEFAULT) != 0) ? |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 513 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 516 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 517 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 518 | const char *device_name, |
| 519 | audio_format_t encodedFormat) |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 520 | { |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 521 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s encodedFormat: 0x%X", |
| 522 | device, device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 523 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 524 | // connect/disconnect only 1 device at a time |
| 525 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 526 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 527 | // Check if the device is currently connected |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 528 | DeviceVector deviceList = mAvailableOutputDevices.getDevicesFromType(device); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 529 | if (deviceList.empty()) { |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 530 | // Nothing to do: device is not connected |
| 531 | return NO_ERROR; |
| 532 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 533 | sp<DeviceDescriptor> devDesc = deviceList.itemAt(0); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 534 | |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 535 | // For offloaded A2DP, Hw modules may have the capability to |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 536 | // configure codecs. |
| 537 | // Handle two specific cases by sending a set parameter to |
| 538 | // configure A2DP codecs. No need to toggle device state. |
| 539 | // Case 1: A2DP active device switches from primary to primary |
| 540 | // module |
| 541 | // Case 2: A2DP device config changes on primary module. |
Eric Laurent | 7e3c083 | 2023-11-30 15:04:50 +0100 | [diff] [blame] | 542 | if (device_has_encoding_capability(device) && hasPrimaryOutput()) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 543 | sp<HwModule> module = mHwModules.getModuleForDeviceType(device, encodedFormat); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 544 | audio_module_handle_t primaryHandle = mPrimaryOutput->getModuleHandle(); |
| 545 | if (availablePrimaryOutputDevices().contains(devDesc) && |
| 546 | (module != 0 && module->getHandle() == primaryHandle)) { |
Eric Laurent | 7e3c083 | 2023-11-30 15:04:50 +0100 | [diff] [blame] | 547 | bool isA2dp = audio_is_a2dp_out_device(device); |
| 548 | const String8 supportKey = isA2dp ? String8(AudioParameter::keyReconfigA2dpSupported) |
| 549 | : String8(AudioParameter::keyReconfigLeSupported); |
| 550 | String8 reply = mpClientInterface->getParameters(AUDIO_IO_HANDLE_NONE, supportKey); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 551 | AudioParameter repliedParameters(reply); |
Eric Laurent | 7e3c083 | 2023-11-30 15:04:50 +0100 | [diff] [blame] | 552 | int isReconfigSupported; |
| 553 | repliedParameters.getInt(supportKey, isReconfigSupported); |
| 554 | if (isReconfigSupported) { |
| 555 | const String8 key = isA2dp ? String8(AudioParameter::keyReconfigA2dp) |
| 556 | : String8(AudioParameter::keyReconfigLe); |
| 557 | AudioParameter param; |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 558 | param.add(key, String8("true")); |
| 559 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 560 | devDesc->setEncodedFormat(encodedFormat); |
| 561 | return NO_ERROR; |
| 562 | } |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 563 | } |
| 564 | } |
cnx421 | bd2dcc4 | 2020-07-11 14:58:44 +0800 | [diff] [blame] | 565 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); |
| 566 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 567 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 568 | // mute media strategies and delay device switch by the largest |
| 569 | // This avoid sending the music tail into the earpiece or headset. |
| 570 | setStrategyMute(musicStrategy, true, desc); |
| 571 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, |
| 572 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 573 | nullptr, true /*fromCache*/).types()); |
| 574 | } |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 575 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 576 | // This will force reading again the device configuration |
Eric Laurent | 7e3c083 | 2023-11-30 15:04:50 +0100 | [diff] [blame] | 577 | status_t status = setDeviceConnectionState(device, |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 578 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 579 | device_address, device_name, |
| 580 | devDesc->getEncodedFormat()); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 581 | if (status != NO_ERROR) { |
| 582 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 583 | status); |
| 584 | return status; |
| 585 | } |
| 586 | |
| 587 | status = setDeviceConnectionState(device, |
| 588 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 589 | device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 590 | if (status != NO_ERROR) { |
| 591 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 592 | status); |
| 593 | return status; |
| 594 | } |
| 595 | |
| 596 | return NO_ERROR; |
| 597 | } |
| 598 | |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 599 | status_t AudioPolicyManager::getHwOffloadFormatsSupportedForBluetoothMedia( |
| 600 | audio_devices_t device, std::vector<audio_format_t> *formats) |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 601 | { |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 602 | ALOGV("getHwOffloadFormatsSupportedForBluetoothMedia()"); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 603 | status_t status = NO_ERROR; |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 604 | std::unordered_set<audio_format_t> formatSet; |
| 605 | sp<HwModule> primaryModule = |
| 606 | mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY); |
Aniket Kumar Lata | fedb598 | 2019-07-03 11:20:36 -0700 | [diff] [blame] | 607 | if (primaryModule == nullptr) { |
| 608 | ALOGE("%s() unable to get primary module", __func__); |
| 609 | return NO_INIT; |
| 610 | } |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 611 | |
| 612 | DeviceTypeSet audioDeviceSet; |
| 613 | |
| 614 | switch(device) { |
| 615 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: |
| 616 | audioDeviceSet = getAudioDeviceOutAllA2dpSet(); |
| 617 | break; |
| 618 | case AUDIO_DEVICE_OUT_BLE_HEADSET: |
Patty Huang | 36028df | 2022-07-06 00:14:12 +0800 | [diff] [blame] | 619 | audioDeviceSet = getAudioDeviceOutLeAudioUnicastSet(); |
| 620 | break; |
| 621 | case AUDIO_DEVICE_OUT_BLE_BROADCAST: |
| 622 | audioDeviceSet = getAudioDeviceOutLeAudioBroadcastSet(); |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 623 | break; |
| 624 | default: |
| 625 | ALOGE("%s() device type 0x%08x not supported", __func__, device); |
| 626 | return BAD_VALUE; |
| 627 | } |
| 628 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 629 | DeviceVector declaredDevices = primaryModule->getDeclaredDevices().getDevicesFromTypes( |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 630 | audioDeviceSet); |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 631 | for (const auto& device : declaredDevices) { |
| 632 | formatSet.insert(device->encodedFormats().begin(), device->encodedFormats().end()); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 633 | } |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 634 | formats->assign(formatSet.begin(), formatSet.end()); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 635 | return status; |
| 636 | } |
| 637 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 638 | DeviceVector AudioPolicyManager::selectBestRxSinkDevicesForCall(bool fromCache) |
| 639 | { |
| 640 | DeviceVector rxSinkdevices{}; |
| 641 | rxSinkdevices = mEngine->getOutputDevicesForAttributes( |
| 642 | attributes_initializer(AUDIO_USAGE_VOICE_COMMUNICATION), nullptr, fromCache); |
| 643 | if (!rxSinkdevices.isEmpty() && mAvailableOutputDevices.contains(rxSinkdevices.itemAt(0))) { |
| 644 | auto rxSinkDevice = rxSinkdevices.itemAt(0); |
| 645 | auto telephonyRxModule = mHwModules.getModuleForDeviceType( |
| 646 | AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); |
| 647 | // retrieve Rx Source device descriptor |
| 648 | sp<DeviceDescriptor> rxSourceDevice = mAvailableInputDevices.getDevice( |
| 649 | AUDIO_DEVICE_IN_TELEPHONY_RX, String8(), AUDIO_FORMAT_DEFAULT); |
| 650 | |
| 651 | // RX Telephony and Rx sink devices are declared by Primary Audio HAL |
| 652 | if (isPrimaryModule(telephonyRxModule) && (telephonyRxModule->getHalVersionMajor() >= 3) && |
| 653 | telephonyRxModule->supportsPatch(rxSourceDevice, rxSinkDevice)) { |
| 654 | ALOGW("%s() device %s using HW Bridge", __func__, rxSinkDevice->toString().c_str()); |
| 655 | return DeviceVector(rxSinkDevice); |
| 656 | } |
| 657 | } |
| 658 | // Note that despite the fact that getNewOutputDevices() is called on the primary output, |
| 659 | // the device returned is not necessarily reachable via this output |
| 660 | // (filter later by setOutputDevices()) |
| 661 | return getNewOutputDevices(mPrimaryOutput, fromCache); |
| 662 | } |
| 663 | |
| 664 | status_t AudioPolicyManager::updateCallRouting(bool fromCache, uint32_t delayMs, uint32_t *waitMs) |
| 665 | { |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 666 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 667 | DeviceVector rxDevices = selectBestRxSinkDevicesForCall(fromCache); |
| 668 | return updateCallRoutingInternal(rxDevices, delayMs, waitMs); |
| 669 | } |
| 670 | return INVALID_OPERATION; |
| 671 | } |
| 672 | |
| 673 | status_t AudioPolicyManager::updateCallRoutingInternal( |
| 674 | const DeviceVector &rxDevices, uint32_t delayMs, uint32_t *waitMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 675 | { |
| 676 | bool createTxPatch = false; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 677 | bool createRxPatch = false; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 678 | uint32_t muteWaitMs = 0; |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 679 | if (hasPrimaryOutput() && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 680 | mPrimaryOutput->devices().onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_STUB)) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 681 | return INVALID_OPERATION; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 682 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 683 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 684 | audio_attributes_t attr = { .source = AUDIO_SOURCE_VOICE_COMMUNICATION }; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 685 | auto txSourceDevice = mEngine->getInputDeviceForAttributes(attr); |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 686 | |
| 687 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 688 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
| 689 | |
| 690 | if (rxDevices.isEmpty()) { |
| 691 | ALOGW("%s() no selected output device", __func__); |
| 692 | return INVALID_OPERATION; |
| 693 | } |
Eric Laurent | cedd5b5 | 2023-03-22 00:03:31 +0000 | [diff] [blame] | 694 | if (txSourceDevice == nullptr) { |
| 695 | ALOGE("%s() selected input device not available", __func__); |
| 696 | return INVALID_OPERATION; |
| 697 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 698 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 699 | ALOGV("%s device rxDevice %s txDevice %s", __func__, |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 700 | rxDevices.itemAt(0)->toString().c_str(), txSourceDevice->toString().c_str()); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 701 | |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 702 | auto telephonyRxModule = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 703 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 704 | auto telephonyTxModule = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 705 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_OUT_TELEPHONY_TX, AUDIO_FORMAT_DEFAULT); |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 706 | // retrieve Rx Source and Tx Sink device descriptors |
| 707 | sp<DeviceDescriptor> rxSourceDevice = |
| 708 | mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX, |
| 709 | String8(), |
| 710 | AUDIO_FORMAT_DEFAULT); |
| 711 | sp<DeviceDescriptor> txSinkDevice = |
| 712 | mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, |
| 713 | String8(), |
| 714 | AUDIO_FORMAT_DEFAULT); |
| 715 | |
| 716 | // RX and TX Telephony device are declared by Primary Audio HAL |
| 717 | if (isPrimaryModule(telephonyRxModule) && isPrimaryModule(telephonyTxModule) && |
| 718 | (telephonyRxModule->getHalVersionMajor() >= 3)) { |
| 719 | if (rxSourceDevice == 0 || txSinkDevice == 0) { |
| 720 | // RX / TX Telephony device(s) is(are) not currently available |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 721 | ALOGE("%s() no telephony Tx and/or RX device", __func__); |
| 722 | return INVALID_OPERATION; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 723 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 724 | // createAudioPatchInternal now supports both HW / SW bridging |
| 725 | createRxPatch = true; |
| 726 | createTxPatch = true; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 727 | } else { |
| 728 | // If the RX device is on the primary HW module, then use legacy routing method for |
| 729 | // voice calls via setOutputDevice() on primary output. |
| 730 | // Otherwise, create two audio patches for TX and RX path. |
| 731 | createRxPatch = !(availablePrimaryOutputDevices().contains(rxDevices.itemAt(0))) && |
| 732 | (rxSourceDevice != 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 733 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 734 | // of it due to legacy implementation. If not, create a patch. |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 735 | createTxPatch = !(availablePrimaryModuleInputDevices().contains(txSourceDevice)) && |
| 736 | (txSinkDevice != 0); |
| 737 | } |
| 738 | // Use legacy routing method for voice calls via setOutputDevice() on primary output. |
| 739 | // Otherwise, create two audio patches for TX and RX path. |
| 740 | if (!createRxPatch) { |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 741 | if (!hasPrimaryOutput()) { |
| 742 | ALOGW("%s() no primary output available", __func__); |
| 743 | return INVALID_OPERATION; |
| 744 | } |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 745 | muteWaitMs = setOutputDevices(__func__, mPrimaryOutput, rxDevices, true, delayMs); |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 746 | } else { // create RX path audio patch |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 747 | connectTelephonyRxAudioSource(); |
juyuchen | 2224c5a | 2019-01-21 12:00:58 +0800 | [diff] [blame] | 748 | // If the TX device is on the primary HW module but RX device is |
| 749 | // on other HW module, SinkMetaData of telephony input should handle it |
| 750 | // assuming the device uses audio HAL V5.0 and above |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 751 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 752 | if (createTxPatch) { // create TX path audio patch |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 753 | // terminate active capture if on the same HW module as the call TX source device |
| 754 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 755 | // call TX device but this information is not in the audio patch and logic here must be |
| 756 | // symmetric to the one in startInput() |
| 757 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
| 758 | if (activeDesc->hasSameHwModuleAs(txSourceDevice)) { |
| 759 | closeActiveClients(activeDesc); |
| 760 | } |
| 761 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 762 | connectTelephonyTxAudioSource(txSourceDevice, txSinkDevice, delayMs); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 763 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 764 | if (waitMs != nullptr) { |
| 765 | *waitMs = muteWaitMs; |
| 766 | } |
| 767 | return NO_ERROR; |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 768 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 769 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 770 | bool AudioPolicyManager::isDeviceOfModule( |
| 771 | const sp<DeviceDescriptor>& devDesc, const char *moduleId) const { |
| 772 | sp<HwModule> module = mHwModules.getModuleFromName(moduleId); |
| 773 | if (module != 0) { |
| 774 | return mAvailableOutputDevices.getDevicesFromHwModule(module->getHandle()) |
| 775 | .indexOf(devDesc) != NAME_NOT_FOUND |
| 776 | || mAvailableInputDevices.getDevicesFromHwModule(module->getHandle()) |
| 777 | .indexOf(devDesc) != NAME_NOT_FOUND; |
| 778 | } |
| 779 | return false; |
| 780 | } |
| 781 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 782 | void AudioPolicyManager::connectTelephonyRxAudioSource() |
| 783 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 784 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 785 | const struct audio_port_config source = { |
| 786 | .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE, |
| 787 | .ext.device.type = AUDIO_DEVICE_IN_TELEPHONY_RX, .ext.device.address = "" |
| 788 | }; |
| 789 | const auto aa = mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL); |
Eric Laurent | 541a200 | 2024-01-15 18:11:42 +0100 | [diff] [blame] | 790 | |
| 791 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 792 | status_t status = startAudioSource(&source, &aa, &portId, 0 /*uid*/, true /*internal*/); |
| 793 | ALOGE_IF(status != OK, "%s: failed to start audio source (%d)", __func__, status); |
| 794 | mCallRxSourceClient = mAudioSources.valueFor(portId); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 795 | ALOGE_IF(mCallRxSourceClient == nullptr, |
| 796 | "%s failed to start Telephony Rx AudioSource", __func__); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 797 | } |
| 798 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 799 | void AudioPolicyManager::disconnectTelephonyAudioSource(sp<SourceClientDescriptor> &clientDesc) |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 800 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 801 | if (clientDesc == nullptr) { |
| 802 | return; |
| 803 | } |
| 804 | ALOGW_IF(stopAudioSource(clientDesc->portId()) != NO_ERROR, |
| 805 | "%s error stopping audio source", __func__); |
| 806 | clientDesc.clear(); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | void AudioPolicyManager::connectTelephonyTxAudioSource( |
| 810 | const sp<DeviceDescriptor> &srcDevice, const sp<DeviceDescriptor> &sinkDevice, |
| 811 | uint32_t delayMs) |
| 812 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 813 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 814 | if (srcDevice == nullptr || sinkDevice == nullptr) { |
| 815 | ALOGW("%s could not create patch, invalid sink and/or source device(s)", __func__); |
| 816 | return; |
| 817 | } |
| 818 | PatchBuilder patchBuilder; |
| 819 | patchBuilder.addSource(srcDevice).addSink(sinkDevice); |
| 820 | ALOGV("%s between source %s and sink %s", __func__, |
| 821 | srcDevice->toString().c_str(), sinkDevice->toString().c_str()); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 822 | auto callTxSourceClientPortId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 823 | const auto aa = mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL); |
| 824 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 825 | struct audio_port_config source = {}; |
| 826 | srcDevice->toAudioPortConfig(&source); |
Eric Laurent | 541a200 | 2024-01-15 18:11:42 +0100 | [diff] [blame] | 827 | mCallTxSourceClient = new SourceClientDescriptor( |
| 828 | callTxSourceClientPortId, mUidCached, aa, source, srcDevice, AUDIO_STREAM_PATCH, |
| 829 | mCommunnicationStrategy, toVolumeSource(aa), true); |
| 830 | mCallTxSourceClient->setPreferredDeviceId(sinkDevice->getId()); |
| 831 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 832 | audio_patch_handle_t patchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 833 | status_t status = connectAudioSourceToSink( |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 834 | mCallTxSourceClient, sinkDevice, patchBuilder.patch(), patchHandle, mUidCached, |
| 835 | delayMs); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 836 | ALOGE_IF(status != NO_ERROR, "%s() error %d creating TX audio patch", __func__, status); |
| 837 | if (status == NO_ERROR) { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 838 | mAudioSources.add(callTxSourceClientPortId, mCallTxSourceClient); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 839 | } |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 840 | } |
| 841 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 842 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 843 | { |
| 844 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 845 | // store previous phone state for management of sonification strategy below |
| 846 | int oldState = mEngine->getPhoneState(); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 847 | bool wasLeUnicastActive = isLeUnicastActive(); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 848 | |
| 849 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 850 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 851 | return; |
| 852 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 853 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 854 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 855 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 856 | // force reevaluating accessibility routing when call stops |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 857 | invalidateStreams({AUDIO_STREAM_ACCESSIBILITY}); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 858 | } |
| 859 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 860 | /** |
| 861 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 862 | * routing command. |
| 863 | */ |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 864 | bool force = ((isStateInCall(oldState) != isStateInCall(state)) |
| 865 | || (isStateInCall(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 866 | |
| 867 | // check for device and output changes triggered by new phone state |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 868 | checkForDeviceAndOutputChanges(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 869 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 870 | int delayMs = 0; |
| 871 | if (isStateInCall(state)) { |
| 872 | nsecs_t sysTime = systemTime(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 873 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); |
| 874 | auto sonificationStrategy = streamToStrategy(AUDIO_STREAM_ALARM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 875 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 876 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 877 | // mute media and sonification strategies and delay device switch by the largest |
| 878 | // latency of any output where either strategy is active. |
| 879 | // 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] | 880 | if ((desc->isStrategyActive(musicStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, sysTime) || |
| 881 | desc->isStrategyActive(sonificationStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, |
| 882 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 883 | (delayMs < (int)desc->latency()*2)) { |
| 884 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 885 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 886 | setStrategyMute(musicStrategy, true, desc); |
| 887 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, |
| 888 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 889 | nullptr, true /*fromCache*/).types()); |
| 890 | setStrategyMute(sonificationStrategy, true, desc); |
| 891 | setStrategyMute(sonificationStrategy, false, desc, MUTE_TIME_MS, |
| 892 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_ALARM), |
| 893 | nullptr, true /*fromCache*/).types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 897 | if (state == AUDIO_MODE_IN_CALL) { |
| 898 | (void)updateCallRouting(false /*fromCache*/, delayMs); |
| 899 | } else { |
| 900 | if (oldState == AUDIO_MODE_IN_CALL) { |
| 901 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 902 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
| 903 | } |
| 904 | if (hasPrimaryOutput()) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 905 | DeviceVector rxDevices = getNewOutputDevices(mPrimaryOutput, false /*fromCache*/); |
| 906 | // force routing command to audio hardware when ending call |
| 907 | // even if no device change is needed |
| 908 | if (isStateInCall(oldState) && rxDevices.isEmpty()) { |
| 909 | rxDevices = mPrimaryOutput->devices(); |
| 910 | } |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 911 | setOutputDevices(__func__, mPrimaryOutput, rxDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 912 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 913 | } |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 914 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 915 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 916 | // reevaluate routing on all outputs in case tracks have been started during the call |
| 917 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 918 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 919 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 920 | if (state != AUDIO_MODE_IN_CALL || (desc != mPrimaryOutput && !isTelephonyRxOrTx(desc))) { |
| 921 | bool forceRouting = !newDevices.isEmpty(); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 922 | if (desc->mUsePreferredMixerAttributes && newDevices != desc->devices()) { |
| 923 | // If the device is using preferred mixer attributes, the output need to reopen |
| 924 | // with default configuration when the new selected devices are different from |
| 925 | // current routing devices. |
| 926 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices); |
| 927 | continue; |
| 928 | } |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 929 | setOutputDevices(__func__, desc, newDevices, forceRouting, 0 /*delayMs*/, nullptr, |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 930 | true /*requiresMuteCheck*/, !forceRouting /*requiresVolumeCheck*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 931 | } |
| 932 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 933 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 934 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 935 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, delayMs); |
| 936 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 937 | if (isStateInCall(state)) { |
| 938 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 939 | // force reevaluating accessibility routing when call starts |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 940 | invalidateStreams({AUDIO_STREAM_ACCESSIBILITY}); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | // 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] | 944 | mLimitRingtoneVolume = (state == AUDIO_MODE_RINGTONE && |
| 945 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 948 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 949 | return mEngine->getPhoneState(); |
| 950 | } |
| 951 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 952 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 953 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 954 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 955 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 956 | if (config == mEngine->getForceUse(usage)) { |
| 957 | return; |
| 958 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 959 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 960 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 961 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 962 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 963 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 964 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 965 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 966 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 967 | |
| 968 | // check for device and output changes triggered by new force usage |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 969 | checkForDeviceAndOutputChanges(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 970 | |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 971 | // force client reconnection to reevaluate flag AUDIO_FLAG_AUDIBILITY_ENFORCED |
| 972 | if (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 973 | invalidateStreams({AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE}); |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 974 | } |
| 975 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 976 | //FIXME: workaround for truncated touch sounds |
| 977 | // to be removed when the problem is handled by system UI |
| 978 | uint32_t delayMs = 0; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 979 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 980 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 981 | } |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 982 | |
| 983 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 984 | updateInputRouting(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 987 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 988 | { |
| 989 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 990 | } |
| 991 | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 992 | // Find an MSD output profile compatible with the parameters passed. |
| 993 | // When "directOnly" is set, restrict search to profiles for direct outputs. |
| 994 | sp<IOProfile> AudioPolicyManager::getMsdProfileForOutput( |
| 995 | const DeviceVector& devices, |
| 996 | uint32_t samplingRate, |
| 997 | audio_format_t format, |
| 998 | audio_channel_mask_t channelMask, |
| 999 | audio_output_flags_t flags, |
| 1000 | bool directOnly) |
| 1001 | { |
| 1002 | flags = getRelevantFlags(flags, directOnly); |
| 1003 | |
| 1004 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1005 | if (msdModule != nullptr) { |
| 1006 | // for the msd module check if there are patches to the output devices |
| 1007 | if (msdHasPatchesToAllDevices(devices.toTypeAddrVector())) { |
| 1008 | HwModuleCollection modules; |
| 1009 | modules.add(msdModule); |
| 1010 | return searchCompatibleProfileHwModules( |
| 1011 | modules, getMsdAudioOutDevices(), samplingRate, format, channelMask, |
| 1012 | flags, directOnly); |
| 1013 | } |
| 1014 | } |
| 1015 | return nullptr; |
| 1016 | } |
| 1017 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1018 | // Find an output profile compatible with the parameters passed. When "directOnly" is set, restrict |
| 1019 | // search to profiles for direct outputs. |
| 1020 | sp<IOProfile> AudioPolicyManager::getProfileForOutput( |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1021 | const DeviceVector& devices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1022 | uint32_t samplingRate, |
| 1023 | audio_format_t format, |
| 1024 | audio_channel_mask_t channelMask, |
| 1025 | audio_output_flags_t flags, |
| 1026 | bool directOnly) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1027 | { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1028 | flags = getRelevantFlags(flags, directOnly); |
| 1029 | |
| 1030 | return searchCompatibleProfileHwModules( |
| 1031 | mHwModules, devices, samplingRate, format, channelMask, flags, directOnly); |
| 1032 | } |
| 1033 | |
| 1034 | audio_output_flags_t AudioPolicyManager::getRelevantFlags ( |
| 1035 | audio_output_flags_t flags, bool directOnly) { |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1036 | if (directOnly) { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1037 | // only retain flags that will drive the direct output profile selection |
| 1038 | // if explicitly requested |
| 1039 | static const uint32_t kRelevantFlags = |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1040 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1041 | AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ); |
| 1042 | flags = (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1043 | } |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1044 | return flags; |
| 1045 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1046 | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1047 | sp<IOProfile> AudioPolicyManager::searchCompatibleProfileHwModules ( |
| 1048 | const HwModuleCollection& hwModules, |
| 1049 | const DeviceVector& devices, |
| 1050 | uint32_t samplingRate, |
| 1051 | audio_format_t format, |
| 1052 | audio_channel_mask_t channelMask, |
| 1053 | audio_output_flags_t flags, |
| 1054 | bool directOnly) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1055 | sp<IOProfile> profile; |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1056 | for (const auto& hwModule : hwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 1057 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
jiabin | 66acc43 | 2024-02-06 00:57:36 +0000 | [diff] [blame] | 1058 | if (curProfile->getCompatibilityScore(devices, |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1059 | samplingRate, NULL /*updatedSamplingRate*/, |
| 1060 | format, NULL /*updatedFormat*/, |
| 1061 | channelMask, NULL /*updatedChannelMask*/, |
jiabin | 66acc43 | 2024-02-06 00:57:36 +0000 | [diff] [blame] | 1062 | flags) == IOProfile::NO_MATCH) { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1063 | continue; |
| 1064 | } |
| 1065 | // reject profiles not corresponding to a device currently available |
| 1066 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
| 1067 | continue; |
| 1068 | } |
| 1069 | // reject profiles if connected device does not support codec |
| 1070 | if (!curProfile->devicesSupportEncodedFormats(devices.types())) { |
| 1071 | continue; |
| 1072 | } |
| 1073 | if (!directOnly) { |
| 1074 | return curProfile; |
| 1075 | } |
| 1076 | |
| 1077 | // when searching for direct outputs, if several profiles are compatible, give priority |
| 1078 | // to one with offload capability |
Patty Huang | 36028df | 2022-07-06 00:14:12 +0800 | [diff] [blame] | 1079 | if (profile != 0 && |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1080 | ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1081 | continue; |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1082 | } |
| 1083 | profile = curProfile; |
| 1084 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1085 | break; |
| 1086 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1087 | } |
| 1088 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1089 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1092 | sp<IOProfile> AudioPolicyManager::getSpatializerOutputProfile( |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 1093 | const audio_config_t *config __unused, const AudioDeviceTypeAddrVector &devices) const |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1094 | { |
| 1095 | for (const auto& hwModule : mHwModules) { |
| 1096 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 1097 | if (curProfile->getFlags() != AUDIO_OUTPUT_FLAG_SPATIALIZER) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1098 | continue; |
| 1099 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1100 | if (!devices.empty()) { |
Eric Laurent | 0c8f7cc | 2022-06-24 14:32:36 +0200 | [diff] [blame] | 1101 | // reject profiles not corresponding to a device currently available |
| 1102 | DeviceVector supportedDevices = curProfile->getSupportedDevices(); |
| 1103 | if (!mAvailableOutputDevices.containsAtLeastOne(supportedDevices)) { |
| 1104 | continue; |
| 1105 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1106 | if (supportedDevices.getDevicesFromDeviceTypeAddrVec(devices).size() |
| 1107 | != devices.size()) { |
| 1108 | continue; |
| 1109 | } |
| 1110 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1111 | ALOGV("%s found profile %s", __func__, curProfile->getName().c_str()); |
| 1112 | return curProfile; |
| 1113 | } |
| 1114 | } |
| 1115 | return nullptr; |
| 1116 | } |
| 1117 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1118 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1119 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1120 | DeviceVector devices = mEngine->getOutputDevicesForStream(stream, false /*fromCache*/); |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1121 | |
| 1122 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). |
| 1123 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, |
| 1124 | // format, flags, etc. This may result in some discrepancy for functions that utilize |
| 1125 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() |
| 1126 | // and AudioSystem::getOutputSamplingRate(). |
| 1127 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1128 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Mingyu Shih | 75563d3 | 2023-05-24 04:47:40 +0800 | [diff] [blame] | 1129 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 1130 | if (stream == AUDIO_STREAM_MUSIC && |
| 1131 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 1132 | flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
| 1133 | } |
| 1134 | const audio_io_handle_t output = selectOutput(outputs, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1135 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1136 | ALOGV("getOutput() stream %d selected devices %s, output %d", stream, |
| 1137 | devices.toString().c_str(), output); |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1138 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1141 | status_t AudioPolicyManager::getAudioAttributes(audio_attributes_t *dstAttr, |
| 1142 | const audio_attributes_t *srcAttr, |
| 1143 | audio_stream_type_t srcStream) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1144 | { |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1145 | if (srcAttr != NULL) { |
| 1146 | if (!isValidAttributes(srcAttr)) { |
| 1147 | ALOGE("%s invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 1148 | __func__, |
| 1149 | srcAttr->usage, srcAttr->content_type, srcAttr->flags, |
| 1150 | srcAttr->tags); |
| 1151 | return BAD_VALUE; |
| 1152 | } |
| 1153 | *dstAttr = *srcAttr; |
| 1154 | } else { |
| 1155 | if (srcStream < AUDIO_STREAM_MIN || srcStream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 1156 | ALOGE("%s: invalid stream type", __func__); |
| 1157 | return BAD_VALUE; |
| 1158 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1159 | *dstAttr = mEngine->getAttributesForStreamType(srcStream); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1160 | } |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 1161 | |
| 1162 | // Only honor audibility enforced when required. The client will be |
| 1163 | // forced to reconnect if the forced usage changes. |
| 1164 | 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] | 1165 | dstAttr->flags = static_cast<audio_flags_mask_t>( |
| 1166 | dstAttr->flags & ~AUDIO_FLAG_AUDIBILITY_ENFORCED); |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1169 | return NO_ERROR; |
| 1170 | } |
| 1171 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1172 | status_t AudioPolicyManager::getOutputForAttrInt( |
| 1173 | audio_attributes_t *resultAttr, |
| 1174 | audio_io_handle_t *output, |
| 1175 | audio_session_t session, |
| 1176 | const audio_attributes_t *attr, |
| 1177 | audio_stream_type_t *stream, |
| 1178 | uid_t uid, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1179 | audio_config_t *config, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1180 | audio_output_flags_t *flags, |
| 1181 | audio_port_handle_t *selectedDeviceId, |
| 1182 | bool *isRequestedDeviceForExclusiveUse, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1183 | std::vector<sp<AudioPolicyMix>> *secondaryMixes, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1184 | output_type_t *outputType, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1185 | bool *isSpatialized, |
| 1186 | bool *isBitPerfect) |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1187 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1188 | DeviceVector outputDevices; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1189 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1190 | DeviceVector msdDevices = getMsdAudioOutDevices(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1191 | const sp<DeviceDescriptor> requestedDevice = |
| 1192 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 1193 | |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1194 | *outputType = API_OUTPUT_INVALID; |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1195 | *isSpatialized = false; |
| 1196 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1197 | status_t status = getAudioAttributes(resultAttr, attr, *stream); |
| 1198 | if (status != NO_ERROR) { |
| 1199 | return status; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1200 | } |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1201 | if (auto it = mAllowedCapturePolicies.find(uid); it != end(mAllowedCapturePolicies)) { |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1202 | resultAttr->flags = static_cast<audio_flags_mask_t>(resultAttr->flags | it->second); |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1203 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1204 | *stream = mEngine->getStreamTypeForAttributes(*resultAttr); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1205 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1206 | ALOGV("%s() attributes=%s stream=%s session %d selectedDeviceId %d", __func__, |
| 1207 | toString(*resultAttr).c_str(), toString(*stream).c_str(), session, requestedPortId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1208 | |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 1209 | bool usePrimaryOutputFromPolicyMixes = false; |
| 1210 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1211 | // The primary output is the explicit routing (eg. setPreferredDevice) if specified, |
| 1212 | // otherwise, fallback to the dynamic policies, if none match, query the engine. |
| 1213 | // 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] | 1214 | sp<AudioPolicyMix> primaryMix; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1215 | const audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
| 1216 | .channel_mask = config->channel_mask, |
| 1217 | .format = config->format, |
| 1218 | }; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1219 | status = mPolicyMixes.getOutputForAttr(*resultAttr, clientConfig, uid, session, *flags, |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 1220 | mAvailableOutputDevices, requestedDevice, primaryMix, |
| 1221 | secondaryMixes, usePrimaryOutputFromPolicyMixes); |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1222 | if (status != OK) { |
| 1223 | return status; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1224 | } |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1225 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1226 | // FIXME: in case of RENDER policy, the output capabilities should be checked |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1227 | if ((secondaryMixes != nullptr && !secondaryMixes->empty()) |
| 1228 | && !audio_is_linear_pcm(config->format)) { |
| 1229 | ALOGD("%s: rejecting request as secondary mixes only support pcm", __func__); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1230 | return BAD_VALUE; |
| 1231 | } |
| 1232 | if (usePrimaryOutputFromPolicyMixes) { |
jiabin | 24ff57a | 2023-11-27 21:06:51 +0000 | [diff] [blame] | 1233 | sp<DeviceDescriptor> policyMixDevice = |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1234 | mAvailableOutputDevices.getDevice(primaryMix->mDeviceType, |
| 1235 | primaryMix->mDeviceAddress, |
| 1236 | AUDIO_FORMAT_DEFAULT); |
| 1237 | sp<SwAudioOutputDescriptor> policyDesc = primaryMix->getOutput(); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1238 | bool tryDirectForFlags = policyDesc == nullptr || |
jiabin | 24ff57a | 2023-11-27 21:06:51 +0000 | [diff] [blame] | 1239 | (policyDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) || |
| 1240 | (*flags & (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ)); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1241 | // if a direct output can be opened to deliver the track's multi-channel content to the |
| 1242 | // output rather than being downmixed by the primary output, then use this direct |
| 1243 | // output by by-passing the primary mix if possible, otherwise fall-through to primary |
| 1244 | // mix. |
| 1245 | bool tryDirectForChannelMask = policyDesc != nullptr |
| 1246 | && (audio_channel_count_from_out_mask(policyDesc->getConfig().channel_mask) < |
| 1247 | audio_channel_count_from_out_mask(config->channel_mask)); |
jiabin | 24ff57a | 2023-11-27 21:06:51 +0000 | [diff] [blame] | 1248 | if (policyMixDevice != nullptr && (tryDirectForFlags || tryDirectForChannelMask)) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1249 | audio_io_handle_t newOutput; |
| 1250 | status = openDirectOutput( |
| 1251 | *stream, session, config, |
| 1252 | (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT), |
jiabin | 24ff57a | 2023-11-27 21:06:51 +0000 | [diff] [blame] | 1253 | DeviceVector(policyMixDevice), &newOutput); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1254 | if (status == NO_ERROR) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1255 | policyDesc = mOutputs.valueFor(newOutput); |
| 1256 | primaryMix->setOutput(policyDesc); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1257 | } else if (tryDirectForFlags) { |
jiabin | 24ff57a | 2023-11-27 21:06:51 +0000 | [diff] [blame] | 1258 | ALOGW("%s, failed open direct, status: %d", __func__, status); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1259 | policyDesc = nullptr; |
| 1260 | } // otherwise use primary if available. |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1261 | } |
| 1262 | if (policyDesc != nullptr) { |
| 1263 | policyDesc->mPolicyMix = primaryMix; |
| 1264 | *output = policyDesc->mIoHandle; |
jiabin | 24ff57a | 2023-11-27 21:06:51 +0000 | [diff] [blame] | 1265 | *selectedDeviceId = policyMixDevice != nullptr ? policyMixDevice->getId() |
| 1266 | : AUDIO_PORT_HANDLE_NONE; |
| 1267 | if ((policyDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1268 | // Remove direct flag as it is not on a direct output. |
| 1269 | *flags = (audio_output_flags_t) (*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1270 | } |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1271 | |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1272 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 1273 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 1274 | *outputType = API_OUT_MIX_PLAYBACK; |
| 1275 | } else { |
| 1276 | *outputType = API_OUTPUT_LEGACY; |
| 1277 | } |
| 1278 | return NO_ERROR; |
jiabin | 24ff57a | 2023-11-27 21:06:51 +0000 | [diff] [blame] | 1279 | } else { |
| 1280 | if (policyMixDevice != nullptr) { |
| 1281 | ALOGE("%s, try to use primary mix but no output found", __func__); |
| 1282 | return INVALID_OPERATION; |
| 1283 | } |
| 1284 | // Fallback to default engine selection as the selected primary mix device is not |
| 1285 | // available. |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1286 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1287 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1288 | // Virtual sources must always be dynamicaly or explicitly routed |
| 1289 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 1290 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 1291 | return BAD_VALUE; |
| 1292 | } |
| 1293 | // explicit routing managed by getDeviceForStrategy in APM is now handled by engine |
| 1294 | // in order to let the choice of the order to future vendor engine |
| 1295 | outputDevices = mEngine->getOutputDevicesForAttributes(*resultAttr, requestedDevice, false); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 1296 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1297 | if ((resultAttr->flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1298 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1301 | // Set incall music only if device was explicitly set, and fallback to the device which is |
| 1302 | // chosen by the engine if not. |
| 1303 | // FIXME: provide a more generic approach which is not device specific and move this back |
| 1304 | // to getOutputForDevice. |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1305 | // 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] | 1306 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX) && |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1307 | (*stream == AUDIO_STREAM_MUSIC || resultAttr->usage == AUDIO_USAGE_VOICE_COMMUNICATION) && |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1308 | audio_is_linear_pcm(config->format) && |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 1309 | isCallAudioAccessible()) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1310 | if (requestedPortId != AUDIO_PORT_HANDLE_NONE) { |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1311 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1312 | *isRequestedDeviceForExclusiveUse = true; |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1313 | } |
| 1314 | } |
| 1315 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1316 | ALOGV("%s() device %s, sampling rate %d, format %#x, channel mask %#x, flags %#x stream %s", |
| 1317 | __func__, outputDevices.toString().c_str(), config->sample_rate, config->format, |
| 1318 | config->channel_mask, *flags, toString(*stream).c_str()); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1319 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1320 | *output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1321 | if (!msdDevices.isEmpty()) { |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1322 | *output = getOutputForDevices(msdDevices, session, resultAttr, config, flags, isSpatialized); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1323 | if (*output != AUDIO_IO_HANDLE_NONE && setMsdOutputPatches(&outputDevices) == NO_ERROR) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1324 | ALOGV("%s() Using MSD devices %s instead of devices %s", |
| 1325 | __func__, msdDevices.toString().c_str(), outputDevices.toString().c_str()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1326 | } else { |
| 1327 | *output = AUDIO_IO_HANDLE_NONE; |
| 1328 | } |
| 1329 | } |
| 1330 | if (*output == AUDIO_IO_HANDLE_NONE) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1331 | sp<PreferredMixerAttributesInfo> info = nullptr; |
| 1332 | if (outputDevices.size() == 1) { |
| 1333 | info = getPreferredMixerAttributesInfo( |
| 1334 | outputDevices.itemAt(0)->getId(), |
jiabin | d9a58d3 | 2023-06-01 17:57:30 +0000 | [diff] [blame] | 1335 | mEngine->getProductStrategyForAttributes(*resultAttr), |
| 1336 | true /*activeBitPerfectPreferred*/); |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 1337 | // Only use preferred mixer if the uid matches or the preferred mixer is bit-perfect |
| 1338 | // and it is currently active. |
| 1339 | if (info != nullptr && info->getUid() != uid && |
| 1340 | ((info->getFlags() & AUDIO_OUTPUT_FLAG_BIT_PERFECT) == AUDIO_OUTPUT_FLAG_NONE || |
| 1341 | info->getActiveClientCount() == 0)) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1342 | info = nullptr; |
| 1343 | } |
| 1344 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1345 | *output = getOutputForDevices(outputDevices, session, resultAttr, config, |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1346 | flags, isSpatialized, info, resultAttr->flags & AUDIO_FLAG_MUTE_HAPTIC); |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 1347 | // The client will be active if the client is currently preferred mixer owner and the |
| 1348 | // requested configuration matches the preferred mixer configuration. |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1349 | *isBitPerfect = (info != nullptr |
| 1350 | && (info->getFlags() & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != AUDIO_OUTPUT_FLAG_NONE |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 1351 | && info->getUid() == uid |
| 1352 | && *output != AUDIO_IO_HANDLE_NONE |
| 1353 | // When bit-perfect output is selected for the preferred mixer attributes owner, |
| 1354 | // only need to consider the config matches. |
| 1355 | && mOutputs.valueFor(*output)->isConfigurationMatched( |
| 1356 | clientConfig, AUDIO_OUTPUT_FLAG_NONE)); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1357 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1358 | if (*output == AUDIO_IO_HANDLE_NONE) { |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1359 | AudioProfileVector profiles; |
| 1360 | status_t ret = getProfilesForDevices(outputDevices, profiles, *flags, false /*isInput*/); |
| 1361 | if (ret == NO_ERROR && !profiles.empty()) { |
Robert Wu | b98ff1b | 2023-06-15 22:53:58 +0000 | [diff] [blame] | 1362 | const auto channels = profiles[0]->getChannels(); |
| 1363 | if (!channels.empty() && (channels.find(config->channel_mask) == channels.end())) { |
| 1364 | config->channel_mask = *channels.begin(); |
| 1365 | } |
| 1366 | const auto sampleRates = profiles[0]->getSampleRates(); |
| 1367 | if (!sampleRates.empty() && |
| 1368 | (sampleRates.find(config->sample_rate) == sampleRates.end())) { |
| 1369 | config->sample_rate = *sampleRates.begin(); |
| 1370 | } |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1371 | config->format = profiles[0]->getFormat(); |
| 1372 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1373 | return INVALID_OPERATION; |
| 1374 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1375 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1376 | *selectedDeviceId = getFirstDeviceId(outputDevices); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1377 | for (auto &outputDevice : outputDevices) { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 1378 | if (outputDevice->getId() == mConfig->getDefaultOutputDevice()->getId()) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1379 | *selectedDeviceId = outputDevice->getId(); |
| 1380 | break; |
| 1381 | } |
| 1382 | } |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1383 | |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1384 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX)) { |
| 1385 | *outputType = API_OUTPUT_TELEPHONY_TX; |
| 1386 | } else { |
| 1387 | *outputType = API_OUTPUT_LEGACY; |
| 1388 | } |
| 1389 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1390 | ALOGV("%s returns output %d selectedDeviceId %d", __func__, *output, *selectedDeviceId); |
| 1391 | |
| 1392 | return NO_ERROR; |
| 1393 | } |
| 1394 | |
| 1395 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 1396 | audio_io_handle_t *output, |
| 1397 | audio_session_t session, |
| 1398 | audio_stream_type_t *stream, |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1399 | const AttributionSourceState& attributionSource, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1400 | audio_config_t *config, |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1401 | audio_output_flags_t *flags, |
| 1402 | audio_port_handle_t *selectedDeviceId, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1403 | audio_port_handle_t *portId, |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1404 | std::vector<audio_io_handle_t> *secondaryOutputs, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1405 | output_type_t *outputType, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1406 | bool *isSpatialized, |
| 1407 | bool *isBitPerfect) |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1408 | { |
| 1409 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 1410 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 1411 | return INVALID_OPERATION; |
| 1412 | } |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1413 | const uid_t uid = VALUE_OR_RETURN_STATUS( |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1414 | aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1415 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1416 | audio_attributes_t resultAttr; |
François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1417 | bool isRequestedDeviceForExclusiveUse = false; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1418 | std::vector<sp<AudioPolicyMix>> secondaryMixes; |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1419 | const sp<DeviceDescriptor> requestedDevice = |
| 1420 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 1421 | |
| 1422 | // Prevent from storing invalid requested device id in clients |
| 1423 | const audio_port_handle_t sanitizedRequestedPortId = |
| 1424 | requestedDevice != nullptr ? requestedPortId : AUDIO_PORT_HANDLE_NONE; |
| 1425 | *selectedDeviceId = sanitizedRequestedPortId; |
| 1426 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1427 | status_t status = getOutputForAttrInt(&resultAttr, output, session, attr, stream, uid, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1428 | config, flags, selectedDeviceId, &isRequestedDeviceForExclusiveUse, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1429 | secondaryOutputs != nullptr ? &secondaryMixes : nullptr, outputType, isSpatialized, |
| 1430 | isBitPerfect); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1431 | if (status != NO_ERROR) { |
| 1432 | return status; |
| 1433 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1434 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryOutputDescs; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1435 | if (secondaryOutputs != nullptr) { |
| 1436 | for (auto &secondaryMix : secondaryMixes) { |
| 1437 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); |
| 1438 | if (outputDesc != nullptr && |
| 1439 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { |
| 1440 | secondaryOutputs->push_back(outputDesc->mIoHandle); |
| 1441 | weakSecondaryOutputDescs.push_back(outputDesc); |
| 1442 | } |
| 1443 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1444 | } |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1445 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1446 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1447 | .channel_mask = config->channel_mask, |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1448 | .format = config->format, |
Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1449 | }; |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 1450 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1451 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1452 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1453 | sp<TrackClientDescriptor> clientDesc = |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1454 | new TrackClientDescriptor(*portId, uid, session, resultAttr, clientConfig, |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1455 | sanitizedRequestedPortId, *stream, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1456 | mEngine->getProductStrategyForAttributes(resultAttr), |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 1457 | toVolumeSource(resultAttr), |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1458 | *flags, isRequestedDeviceForExclusiveUse, |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1459 | std::move(weakSecondaryOutputDescs), |
| 1460 | outputDesc->mPolicyMix); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1461 | outputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1462 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1463 | ALOGV("%s() returns output %d requestedPortId %d selectedDeviceId %d for port ID %d", __func__, |
| 1464 | *output, requestedPortId, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1465 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1466 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1467 | } |
| 1468 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1469 | status_t AudioPolicyManager::openDirectOutput(audio_stream_type_t stream, |
| 1470 | audio_session_t session, |
| 1471 | const audio_config_t *config, |
| 1472 | audio_output_flags_t flags, |
| 1473 | const DeviceVector &devices, |
| 1474 | audio_io_handle_t *output) { |
| 1475 | |
| 1476 | *output = AUDIO_IO_HANDLE_NONE; |
| 1477 | |
| 1478 | // skip direct output selection if the request can obviously be attached to a mixed output |
| 1479 | // and not explicitly requested |
| 1480 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 1481 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 1482 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
| 1483 | return NAME_NOT_FOUND; |
| 1484 | } |
| 1485 | |
| 1486 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 1487 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 1488 | // when audioflinger detects there is an active non offloadable effect. |
| 1489 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 1490 | // This may prevent offloading in rare situations where effects are left active by apps |
| 1491 | // in the background. |
| 1492 | sp<IOProfile> profile; |
| 1493 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
| 1494 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
| 1495 | profile = getProfileForOutput( |
| 1496 | devices, config->sample_rate, config->format, config->channel_mask, |
| 1497 | flags, true /* directOnly */); |
| 1498 | } |
| 1499 | |
| 1500 | if (profile == nullptr) { |
| 1501 | return NAME_NOT_FOUND; |
| 1502 | } |
| 1503 | |
| 1504 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 1505 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1506 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 1507 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 1508 | // reuse direct output if currently open by the same client |
| 1509 | // and configured with same parameters |
| 1510 | if ((config->sample_rate == desc->getSamplingRate()) && |
| 1511 | (config->format == desc->getFormat()) && |
| 1512 | (config->channel_mask == desc->getChannelMask()) && |
| 1513 | (session == desc->mDirectClientSession)) { |
| 1514 | desc->mDirectOpenCount++; |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 1515 | ALOGV("%s reusing direct output %d for session %d", __func__, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1516 | mOutputs.keyAt(i), session); |
| 1517 | *output = mOutputs.keyAt(i); |
| 1518 | return NO_ERROR; |
| 1519 | } |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | if (!profile->canOpenNewIo()) { |
Atneya Nair | b16666a | 2023-12-11 20:18:33 -0800 | [diff] [blame] | 1524 | if (!com::android::media::audioserver::direct_track_reprioritization()) { |
| 1525 | return NAME_NOT_FOUND; |
| 1526 | } else if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) != 0) { |
| 1527 | // MMAP gracefully handles lack of an exclusive track resource by mixing |
| 1528 | // above the audio framework. For AAudio to know that the limit is reached, |
| 1529 | // return an error. |
| 1530 | return NAME_NOT_FOUND; |
| 1531 | } else { |
| 1532 | // Close outputs on this profile, if available, to free resources for this request |
| 1533 | for (int i = 0; i < mOutputs.size() && !profile->canOpenNewIo(); i++) { |
| 1534 | const auto desc = mOutputs.valueAt(i); |
| 1535 | if (desc->mProfile == profile) { |
| 1536 | closeOutput(desc->mIoHandle); |
| 1537 | } |
| 1538 | } |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | // Unable to close streams to find free resources for this request |
| 1543 | if (!profile->canOpenNewIo()) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1544 | return NAME_NOT_FOUND; |
| 1545 | } |
| 1546 | |
Atneya Nair | b16666a | 2023-12-11 20:18:33 -0800 | [diff] [blame] | 1547 | auto outputDesc = sp<SwAudioOutputDescriptor>::make(profile, mpClientInterface); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1548 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1549 | // An MSD patch may be using the only output stream that can service this request. Release |
| 1550 | // 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] | 1551 | releaseMsdOutputPatches(devices); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1552 | |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 1553 | status_t status = |
| 1554 | outputDesc->open(config, nullptr /* mixerConfig */, devices, stream, flags, output); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1555 | |
| 1556 | // only accept an output with the requested parameters |
| 1557 | if (status != NO_ERROR || |
| 1558 | (config->sample_rate != 0 && config->sample_rate != outputDesc->getSamplingRate()) || |
| 1559 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->getFormat()) || |
| 1560 | (config->channel_mask != 0 && config->channel_mask != outputDesc->getChannelMask())) { |
| 1561 | ALOGV("%s failed opening direct output: output %d sample rate %d %d," |
| 1562 | "format %d %d, channel mask %04x %04x", __func__, *output, config->sample_rate, |
| 1563 | outputDesc->getSamplingRate(), config->format, outputDesc->getFormat(), |
| 1564 | config->channel_mask, outputDesc->getChannelMask()); |
| 1565 | if (*output != AUDIO_IO_HANDLE_NONE) { |
| 1566 | outputDesc->close(); |
| 1567 | } |
| 1568 | // fall back to mixer output if possible when the direct output could not be open |
| 1569 | if (audio_is_linear_pcm(config->format) && |
| 1570 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
| 1571 | return NAME_NOT_FOUND; |
| 1572 | } |
| 1573 | *output = AUDIO_IO_HANDLE_NONE; |
| 1574 | return BAD_VALUE; |
| 1575 | } |
| 1576 | outputDesc->mDirectOpenCount = 1; |
| 1577 | outputDesc->mDirectClientSession = session; |
| 1578 | |
| 1579 | addOutput(*output, outputDesc); |
| 1580 | mPreviousOutputs = mOutputs; |
| 1581 | ALOGV("%s returns new direct output %d", __func__, *output); |
| 1582 | mpClientInterface->onAudioPortListUpdate(); |
| 1583 | return NO_ERROR; |
| 1584 | } |
| 1585 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1586 | audio_io_handle_t AudioPolicyManager::getOutputForDevices( |
| 1587 | const DeviceVector &devices, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1588 | audio_session_t session, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1589 | const audio_attributes_t *attr, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1590 | const audio_config_t *config, |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1591 | audio_output_flags_t *flags, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1592 | bool *isSpatialized, |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1593 | sp<PreferredMixerAttributesInfo> prefMixerConfigInfo, |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1594 | bool forceMutingHaptic) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1595 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1596 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1597 | |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1598 | // Discard haptic channel mask when forcing muting haptic channels. |
| 1599 | audio_channel_mask_t channelMask = forceMutingHaptic |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1600 | ? static_cast<audio_channel_mask_t>(config->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL) |
| 1601 | : config->channel_mask; |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1602 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1603 | // open a direct output if required by specified parameters |
| 1604 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 1605 | // and all common behaviors are driven by checking only the direct flag |
| 1606 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1607 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1608 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1609 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1610 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 1611 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1612 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1613 | |
| 1614 | audio_stream_type_t stream = mEngine->getStreamTypeForAttributes(*attr); |
| 1615 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1616 | // only allow deep buffering for music stream type |
| 1617 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1618 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1619 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1620 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1621 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 1622 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1623 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1624 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1625 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1626 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1627 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1628 | audio_is_linear_pcm(config->format) && |
| 1629 | (*flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) == 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1630 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1631 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1632 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1633 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1634 | |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 1635 | // Attach the Ultrasound flag for the AUDIO_CONTENT_TYPE_ULTRASOUND |
| 1636 | if (attr->content_type == AUDIO_CONTENT_TYPE_ULTRASOUND) { |
| 1637 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_ULTRASOUND); |
| 1638 | } |
| 1639 | |
Eric Laurent | f9230d5 | 2024-01-26 18:49:09 +0100 | [diff] [blame] | 1640 | // Use the spatializer output if the content can be spatialized, no preferred mixer |
| 1641 | // was specified and offload or direct playback is not explicitly requested. |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1642 | *isSpatialized = false; |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1643 | if (mSpatializerOutput != nullptr |
jiabin | 2462ce8 | 2024-01-12 20:37:59 +0000 | [diff] [blame] | 1644 | && canBeSpatializedInt(attr, config, devices.toTypeAddrVector()) |
Eric Laurent | f9230d5 | 2024-01-26 18:49:09 +0100 | [diff] [blame] | 1645 | && prefMixerConfigInfo == nullptr |
| 1646 | && ((*flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT)) == 0)) { |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1647 | *isSpatialized = true; |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1648 | return mSpatializerOutput->mIoHandle; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1649 | } |
| 1650 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1651 | audio_config_t directConfig = *config; |
| 1652 | directConfig.channel_mask = channelMask; |
| 1653 | status_t status = openDirectOutput(stream, session, &directConfig, *flags, devices, &output); |
| 1654 | if (status != NAME_NOT_FOUND) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1655 | return output; |
| 1656 | } |
| 1657 | |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1658 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1659 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1660 | 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] | 1661 | return AUDIO_IO_HANDLE_NONE; |
| 1662 | } |
Pierre Couillaud | e73496b | 2023-03-06 16:19:05 +0000 | [diff] [blame] | 1663 | // A request for Tuner cannot fallback to a mixed output |
| 1664 | if ((directConfig.offload_info.content_id || directConfig.offload_info.sync_id)) { |
| 1665 | return AUDIO_IO_HANDLE_NONE; |
| 1666 | } |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1667 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1668 | // ignoring channel mask due to downmix capability in mixer |
| 1669 | |
| 1670 | // open a non direct output |
| 1671 | |
| 1672 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1673 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1674 | // get which output is suitable for the specified stream. The actual |
| 1675 | // routing change will happen when startOutput() will be called |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1676 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1677 | if (prefMixerConfigInfo != nullptr) { |
| 1678 | for (audio_io_handle_t outputHandle : outputs) { |
| 1679 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputHandle); |
| 1680 | if (outputDesc->mProfile == prefMixerConfigInfo->getProfile()) { |
| 1681 | output = outputHandle; |
| 1682 | break; |
| 1683 | } |
| 1684 | } |
| 1685 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 1686 | // No output open with the preferred profile. Open a new one. |
| 1687 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 1688 | config.channel_mask = prefMixerConfigInfo->getConfigBase().channel_mask; |
| 1689 | config.sample_rate = prefMixerConfigInfo->getConfigBase().sample_rate; |
| 1690 | config.format = prefMixerConfigInfo->getConfigBase().format; |
| 1691 | sp<SwAudioOutputDescriptor> preferredOutput = openOutputWithProfileAndDevice( |
| 1692 | prefMixerConfigInfo->getProfile(), devices, nullptr /*mixerConfig*/, |
| 1693 | &config, prefMixerConfigInfo->getFlags()); |
| 1694 | if (preferredOutput == nullptr) { |
| 1695 | ALOGE("%s failed to open output with preferred mixer config", __func__); |
| 1696 | } else { |
| 1697 | output = preferredOutput->mIoHandle; |
| 1698 | } |
| 1699 | } |
| 1700 | } else { |
| 1701 | // at this stage we should ignore the DIRECT flag as no direct output could be |
| 1702 | // found earlier |
| 1703 | *flags = (audio_output_flags_t) (*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1704 | output = selectOutput( |
| 1705 | outputs, *flags, config->format, channelMask, config->sample_rate, session); |
| 1706 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1707 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1708 | ALOGW_IF((output == 0), "getOutputForDevices() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1709 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1710 | stream, config->sample_rate, config->format, channelMask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1711 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1712 | return output; |
| 1713 | } |
| 1714 | |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame] | 1715 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1716 | auto msdInDevices = mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1717 | mAvailableInputDevices); |
| 1718 | return msdInDevices.isEmpty()? nullptr : msdInDevices.itemAt(0); |
| 1719 | } |
| 1720 | |
| 1721 | DeviceVector AudioPolicyManager::getMsdAudioOutDevices() const { |
| 1722 | return mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1723 | mAvailableOutputDevices); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1724 | } |
| 1725 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1726 | const AudioPatchCollection AudioPolicyManager::getMsdOutputPatches() const { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1727 | AudioPatchCollection msdPatches; |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1728 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1729 | if (msdModule != 0) { |
| 1730 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { |
| 1731 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); |
| 1732 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { |
| 1733 | const struct audio_port_config *source = &patch->mPatch.sources[j]; |
| 1734 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 1735 | source->ext.device.hw_module == msdModule->getHandle()) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1736 | msdPatches.addAudioPatch(patch->getHandle(), patch); |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1737 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1738 | } |
| 1739 | } |
| 1740 | } |
| 1741 | return msdPatches; |
| 1742 | } |
| 1743 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 1744 | bool AudioPolicyManager::isMsdPatch(const audio_patch_handle_t &handle) const { |
| 1745 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 1746 | if (index < 0) { |
| 1747 | return false; |
| 1748 | } |
| 1749 | const sp<AudioPatch> patch = mAudioPatches.valueAt(index); |
| 1750 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1751 | if (msdModule == nullptr) { |
| 1752 | return false; |
| 1753 | } |
| 1754 | const struct audio_port_config *sink = &patch->mPatch.sinks[0]; |
| 1755 | if (getMsdAudioOutDevices().contains(mAvailableOutputDevices.getDeviceFromId(sink->id))) { |
| 1756 | return true; |
| 1757 | } |
| 1758 | index = getMsdOutputPatches().indexOfKey(handle); |
| 1759 | if (index < 0) { |
| 1760 | return false; |
| 1761 | } |
| 1762 | return true; |
| 1763 | } |
| 1764 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1765 | status_t AudioPolicyManager::getMsdProfiles(bool hwAvSync, |
| 1766 | const InputProfileCollection &inputProfiles, |
| 1767 | const OutputProfileCollection &outputProfiles, |
| 1768 | const sp<DeviceDescriptor> &sourceDevice, |
| 1769 | const sp<DeviceDescriptor> &sinkDevice, |
| 1770 | AudioProfileVector& sourceProfiles, |
| 1771 | AudioProfileVector& sinkProfiles) const { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1772 | if (inputProfiles.isEmpty()) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1773 | ALOGE("%s() no input profiles for source module", __func__); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1774 | return NO_INIT; |
| 1775 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1776 | if (outputProfiles.isEmpty()) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1777 | ALOGE("%s() no output profiles for sink module", __func__); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1778 | return NO_INIT; |
| 1779 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1780 | for (const auto &inProfile : inputProfiles) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1781 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0) && |
| 1782 | inProfile->supportsDevice(sourceDevice)) { |
| 1783 | appendAudioProfiles(sourceProfiles, inProfile->getAudioProfiles()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1784 | } |
| 1785 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1786 | for (const auto &outProfile : outputProfiles) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1787 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) && |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1788 | outProfile->supportsDevice(sinkDevice)) { |
| 1789 | appendAudioProfiles(sinkProfiles, outProfile->getAudioProfiles()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1790 | } |
| 1791 | } |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1792 | return NO_ERROR; |
| 1793 | } |
| 1794 | |
| 1795 | status_t AudioPolicyManager::getBestMsdConfig(bool hwAvSync, |
| 1796 | const AudioProfileVector &sourceProfiles, const AudioProfileVector &sinkProfiles, |
| 1797 | audio_port_config *sourceConfig, audio_port_config *sinkConfig) const |
| 1798 | { |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 1799 | // Compressed formats for MSD module, ordered from most preferred to least preferred. |
| 1800 | static const std::vector<audio_format_t> formatsOrder = {{ |
| 1801 | AUDIO_FORMAT_IEC60958, AUDIO_FORMAT_MAT_2_1, AUDIO_FORMAT_MAT_2_0, AUDIO_FORMAT_E_AC3, |
Dean Wheatley | 0f27c60 | 2023-08-23 13:57:21 +1000 | [diff] [blame] | 1802 | AUDIO_FORMAT_AC3, AUDIO_FORMAT_PCM_FLOAT, AUDIO_FORMAT_PCM_32_BIT, |
| 1803 | AUDIO_FORMAT_PCM_8_24_BIT, AUDIO_FORMAT_PCM_24_BIT_PACKED, AUDIO_FORMAT_PCM_16_BIT }}; |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 1804 | static const std::vector<audio_channel_mask_t> channelMasksOrder = [](){ |
| 1805 | // Channel position masks for MSD module, 3D > 2D > 1D ordering (most preferred to least |
| 1806 | // preferred). |
| 1807 | std::vector<audio_channel_mask_t> masks = {{ |
| 1808 | AUDIO_CHANNEL_OUT_3POINT1POINT2, AUDIO_CHANNEL_OUT_3POINT0POINT2, |
| 1809 | AUDIO_CHANNEL_OUT_2POINT1POINT2, AUDIO_CHANNEL_OUT_2POINT0POINT2, |
| 1810 | AUDIO_CHANNEL_OUT_5POINT1, AUDIO_CHANNEL_OUT_STEREO }}; |
| 1811 | // insert index masks (higher counts most preferred) as preferred over position masks |
| 1812 | for (int i = 1; i <= AUDIO_CHANNEL_COUNT_MAX; i++) { |
| 1813 | masks.insert( |
| 1814 | masks.begin(), audio_channel_mask_for_index_assignment_from_count(i)); |
| 1815 | } |
| 1816 | return masks; |
| 1817 | }(); |
| 1818 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1819 | struct audio_config_base bestSinkConfig; |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 1820 | status_t result = findBestMatchingOutputConfig(sourceProfiles, sinkProfiles, formatsOrder, |
| 1821 | channelMasksOrder, true /*preferHigherSamplingRates*/, bestSinkConfig); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1822 | if (result != NO_ERROR) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1823 | ALOGD("%s() no matching config found for sink, hwAvSync: %d", |
| 1824 | __func__, hwAvSync); |
Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1825 | return result; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1826 | } |
| 1827 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1828 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; |
| 1829 | sinkConfig->format = bestSinkConfig.format; |
| 1830 | // For encoded streams force direct flag to prevent downstream mixing. |
| 1831 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1832 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); |
Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1833 | if (audio_is_iec61937_compatible(sinkConfig->format)) { |
| 1834 | // For formats compatible with IEC61937 encapsulation, assume that |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1835 | // the input is IEC61937 framed (for proportional buffer sizing). |
Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1836 | // Add the AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO flag so downstream HAL can distinguish between |
| 1837 | // raw and IEC61937 framed streams. |
| 1838 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1839 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO); |
| 1840 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1841 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1842 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 1843 | sourceConfig->channel_mask = |
| 1844 | audio_channel_mask_get_representation(bestSinkConfig.channel_mask) |
| 1845 | == AUDIO_CHANNEL_REPRESENTATION_INDEX ? |
| 1846 | bestSinkConfig.channel_mask : audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1847 | sourceConfig->format = bestSinkConfig.format; |
| 1848 | // Copy input stream directly without any processing (e.g. resampling). |
| 1849 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1850 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); |
| 1851 | if (hwAvSync) { |
| 1852 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1853 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 1854 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1855 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); |
| 1856 | } |
| 1857 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | |
| 1858 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; |
| 1859 | sinkConfig->config_mask |= config_mask; |
| 1860 | sourceConfig->config_mask |= config_mask; |
| 1861 | return NO_ERROR; |
| 1862 | } |
| 1863 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1864 | PatchBuilder AudioPolicyManager::buildMsdPatch(bool msdIsSource, |
| 1865 | const sp<DeviceDescriptor> &device) const |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1866 | { |
| 1867 | PatchBuilder patchBuilder; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1868 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1869 | ALOG_ASSERT(msdModule != nullptr, "MSD module not available"); |
| 1870 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(device, AUDIO_FORMAT_DEFAULT); |
| 1871 | if (deviceModule == nullptr) { |
| 1872 | ALOGE("%s() unable to get module for %s", __func__, device->toString().c_str()); |
| 1873 | return patchBuilder; |
| 1874 | } |
| 1875 | const InputProfileCollection inputProfiles = msdIsSource ? |
| 1876 | msdModule->getInputProfiles() : deviceModule->getInputProfiles(); |
| 1877 | const OutputProfileCollection outputProfiles = msdIsSource ? |
| 1878 | deviceModule->getOutputProfiles() : msdModule->getOutputProfiles(); |
| 1879 | |
| 1880 | const sp<DeviceDescriptor> sourceDevice = msdIsSource ? getMsdAudioInDevice() : device; |
| 1881 | const sp<DeviceDescriptor> sinkDevice = msdIsSource ? |
| 1882 | device : getMsdAudioOutDevices().itemAt(0); |
| 1883 | patchBuilder.addSource(sourceDevice).addSink(sinkDevice); |
| 1884 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1885 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; |
| 1886 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1887 | AudioProfileVector sourceProfiles; |
| 1888 | AudioProfileVector sinkProfiles; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1889 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. |
| 1890 | // For now, we just forcefully try with HwAvSync first. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1891 | for (auto hwAvSync : { true, false }) { |
| 1892 | if (getMsdProfiles(hwAvSync, inputProfiles, outputProfiles, sourceDevice, sinkDevice, |
| 1893 | sourceProfiles, sinkProfiles) != NO_ERROR) { |
| 1894 | continue; |
| 1895 | } |
| 1896 | if (getBestMsdConfig(hwAvSync, sourceProfiles, sinkProfiles, &sourceConfig, |
| 1897 | &sinkConfig) == NO_ERROR) { |
| 1898 | // Found a matching config. Re-create PatchBuilder with this config. |
| 1899 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); |
| 1900 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1901 | } |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1902 | ALOGV("%s() no matching config found. Fall through to default PCM patch" |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1903 | " supporting PCM format conversion.", __func__); |
| 1904 | return patchBuilder; |
| 1905 | } |
| 1906 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1907 | status_t AudioPolicyManager::setMsdOutputPatches(const DeviceVector *outputDevices) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1908 | DeviceVector devices; |
| 1909 | if (outputDevices != nullptr && outputDevices->size() > 0) { |
| 1910 | devices.add(*outputDevices); |
| 1911 | } else { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1912 | // Use media strategy for unspecified output device. This should only |
| 1913 | // occur on checkForDeviceAndOutputChanges(). Device connection events may |
| 1914 | // therefore invalidate explicit routing requests. |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1915 | devices = mEngine->getOutputDevicesForAttributes( |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1916 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1917 | 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] | 1918 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1919 | std::vector<PatchBuilder> patchesToCreate; |
| 1920 | for (auto i = 0u; i < devices.size(); ++i) { |
| 1921 | ALOGV("%s() for device %s", __func__, devices[i]->toString().c_str()); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1922 | patchesToCreate.push_back(buildMsdPatch(true /*msdIsSource*/, devices[i])); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1923 | } |
| 1924 | // Retain only the MSD patches associated with outputDevices request. |
| 1925 | // Tear down the others, and create new ones as needed. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1926 | AudioPatchCollection patchesToRemove = getMsdOutputPatches(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1927 | for (auto it = patchesToCreate.begin(); it != patchesToCreate.end(); ) { |
| 1928 | auto retainedPatch = false; |
| 1929 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { |
| 1930 | if (audio_patches_are_equal(it->patch(), &patchesToRemove[i]->mPatch)) { |
| 1931 | patchesToRemove.removeItemsAt(i); |
| 1932 | retainedPatch = true; |
| 1933 | break; |
| 1934 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1935 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1936 | if (retainedPatch) { |
| 1937 | it = patchesToCreate.erase(it); |
| 1938 | continue; |
| 1939 | } |
| 1940 | ++it; |
| 1941 | } |
| 1942 | if (patchesToCreate.size() == 0 && patchesToRemove.size() == 0) { |
| 1943 | return NO_ERROR; |
| 1944 | } |
| 1945 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { |
| 1946 | auto ¤tPatch = patchesToRemove.valueAt(i); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1947 | releaseAudioPatch(currentPatch->getHandle(), mUidCached); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1948 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1949 | status_t status = NO_ERROR; |
| 1950 | for (const auto &p : patchesToCreate) { |
| 1951 | auto currStatus = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, |
| 1952 | p.patch(), 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); |
| 1953 | char message[256]; |
| 1954 | snprintf(message, sizeof(message), "%s() %s: creating MSD patch from device:IN_BUS to " |
| 1955 | "device:%#x (format:%#x channels:%#x samplerate:%d)", __func__, |
| 1956 | currStatus == NO_ERROR ? "Success" : "Error", |
| 1957 | p.patch()->sinks[0].ext.device.type, p.patch()->sources[0].format, |
| 1958 | p.patch()->sources[0].channel_mask, p.patch()->sources[0].sample_rate); |
| 1959 | if (currStatus == NO_ERROR) { |
| 1960 | ALOGD("%s", message); |
| 1961 | } else { |
| 1962 | ALOGE("%s", message); |
| 1963 | if (status == NO_ERROR) { |
| 1964 | status = currStatus; |
| 1965 | } |
| 1966 | } |
| 1967 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1968 | return status; |
| 1969 | } |
| 1970 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1971 | void AudioPolicyManager::releaseMsdOutputPatches(const DeviceVector& devices) { |
| 1972 | AudioPatchCollection msdPatches = getMsdOutputPatches(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1973 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1974 | const auto& patch = msdPatches[i]; |
| 1975 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1976 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1977 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && devices.getDevice(sink->ext.device.type, |
| 1978 | String8(sink->ext.device.address), AUDIO_FORMAT_DEFAULT) != nullptr) { |
| 1979 | releaseAudioPatch(patch->getHandle(), mUidCached); |
| 1980 | break; |
| 1981 | } |
| 1982 | } |
| 1983 | } |
| 1984 | } |
| 1985 | |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 1986 | bool AudioPolicyManager::msdHasPatchesToAllDevices(const AudioDeviceTypeAddrVector& devices) { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 1987 | DeviceVector devicesToCheck = |
| 1988 | mConfig->getOutputDevices().getDevicesFromDeviceTypeAddrVec(devices); |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 1989 | AudioPatchCollection msdPatches = getMsdOutputPatches(); |
| 1990 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1991 | const auto& patch = msdPatches[i]; |
| 1992 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1993 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1994 | if (sink->type == AUDIO_PORT_TYPE_DEVICE) { |
| 1995 | const auto& foundDevice = devicesToCheck.getDevice( |
| 1996 | sink->ext.device.type, String8(sink->ext.device.address), AUDIO_FORMAT_DEFAULT); |
| 1997 | if (foundDevice != nullptr) { |
| 1998 | devicesToCheck.remove(foundDevice); |
| 1999 | if (devicesToCheck.isEmpty()) { |
| 2000 | return true; |
| 2001 | } |
| 2002 | } |
| 2003 | } |
| 2004 | } |
| 2005 | } |
| 2006 | return false; |
| 2007 | } |
| 2008 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2009 | audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs, |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 2010 | audio_output_flags_t flags, |
| 2011 | audio_format_t format, |
| 2012 | audio_channel_mask_t channelMask, |
| 2013 | uint32_t samplingRate, |
| 2014 | audio_session_t sessionId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2015 | { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2016 | LOG_ALWAYS_FATAL_IF(!(format == AUDIO_FORMAT_INVALID || audio_is_linear_pcm(format)), |
| 2017 | "%s called with format %#x", __func__, format); |
| 2018 | |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 2019 | // Return the output that haptic-generating attached to when 1) session id is specified, |
| 2020 | // 2) haptic-generating effect exists for given session id and 3) the output that |
| 2021 | // haptic-generating effect attached to is in given outputs. |
| 2022 | if (sessionId != AUDIO_SESSION_NONE) { |
| 2023 | audio_io_handle_t hapticGeneratingOutput = mEffects.getIoForSession( |
| 2024 | sessionId, FX_IID_HAPTICGENERATOR); |
| 2025 | if (outputs.indexOf(hapticGeneratingOutput) >= 0) { |
| 2026 | return hapticGeneratingOutput; |
| 2027 | } |
| 2028 | } |
| 2029 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2030 | // Flags disqualifying an output: the match must happen before calling selectOutput() |
| 2031 | static const audio_output_flags_t kExcludedFlags = (audio_output_flags_t) |
| 2032 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT); |
| 2033 | |
| 2034 | // Flags expressing a functional request: must be honored in priority over |
| 2035 | // other criteria |
| 2036 | static const audio_output_flags_t kFunctionalFlags = (audio_output_flags_t) |
| 2037 | (AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_INCALL_MUSIC | |
Eric Laurent | e28c66d | 2022-01-21 13:40:41 +0100 | [diff] [blame] | 2038 | AUDIO_OUTPUT_FLAG_TTS | AUDIO_OUTPUT_FLAG_DIRECT_PCM | AUDIO_OUTPUT_FLAG_ULTRASOUND | |
| 2039 | AUDIO_OUTPUT_FLAG_SPATIALIZER); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2040 | // Flags expressing a performance request: have lower priority than serving |
| 2041 | // requested sampling rate or channel mask |
| 2042 | static const audio_output_flags_t kPerformanceFlags = (audio_output_flags_t) |
| 2043 | (AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_DEEP_BUFFER | |
| 2044 | AUDIO_OUTPUT_FLAG_RAW | AUDIO_OUTPUT_FLAG_SYNC); |
| 2045 | |
| 2046 | const audio_output_flags_t functionalFlags = |
| 2047 | (audio_output_flags_t)(flags & kFunctionalFlags); |
| 2048 | const audio_output_flags_t performanceFlags = |
| 2049 | (audio_output_flags_t)(flags & kPerformanceFlags); |
| 2050 | |
| 2051 | audio_io_handle_t bestOutput = (outputs.size() == 0) ? AUDIO_IO_HANDLE_NONE : outputs[0]; |
| 2052 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2053 | // 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] | 2054 | // devices (the list was previously build by getOutputsForDevices()). |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2055 | // The priority is as follows: |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 2056 | // 1: the output supporting haptic playback when requesting haptic playback |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2057 | // 2: the output with the highest number of requested functional flags |
Carter Hsu | 199f189 | 2021-10-15 15:47:29 +0800 | [diff] [blame] | 2058 | // with tiebreak preferring the minimum number of extra functional flags |
| 2059 | // (see b/200293124, the incorrect selection of AUDIO_OUTPUT_FLAG_VOIP_RX). |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2060 | // 3: the output supporting the exact channel mask |
| 2061 | // 4: the output with a higher channel count than requested |
jiabin | b12a6da | 2022-06-03 20:48:18 +0000 | [diff] [blame] | 2062 | // 5: the output with the highest sampling rate if the requested sample rate is |
| 2063 | // greater than default sampling rate |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2064 | // 6: the output with the highest number of requested performance flags |
| 2065 | // 7: the output with the bit depth the closest to the requested one |
| 2066 | // 8: the primary output |
| 2067 | // 9: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2068 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2069 | // matching criteria values in priority order for best matching output so far |
| 2070 | std::vector<uint32_t> bestMatchCriteria(8, 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2071 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2072 | const uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); |
| 2073 | const uint32_t hapticChannelCount = audio_channel_count_from_out_mask( |
| 2074 | channelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
Eric Laurent | e78b676 | 2018-12-19 16:29:01 -0800 | [diff] [blame] | 2075 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2076 | for (audio_io_handle_t output : outputs) { |
| 2077 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2078 | // matching criteria values in priority order for current output |
| 2079 | std::vector<uint32_t> currentMatchCriteria(8, 0); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 2080 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2081 | if (outputDesc->isDuplicated()) { |
| 2082 | continue; |
| 2083 | } |
| 2084 | if ((kExcludedFlags & outputDesc->mFlags) != 0) { |
| 2085 | continue; |
| 2086 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2087 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2088 | // If haptic channel is specified, use the haptic output if present. |
| 2089 | // When using haptic output, same audio format and sample rate are required. |
| 2090 | const uint32_t outputHapticChannelCount = audio_channel_count_from_out_mask( |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2091 | outputDesc->getChannelMask() & AUDIO_CHANNEL_HAPTIC_ALL); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2092 | if ((hapticChannelCount == 0) != (outputHapticChannelCount == 0)) { |
| 2093 | continue; |
| 2094 | } |
| 2095 | if (outputHapticChannelCount >= hapticChannelCount |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2096 | && format == outputDesc->getFormat() |
| 2097 | && samplingRate == outputDesc->getSamplingRate()) { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2098 | currentMatchCriteria[0] = outputHapticChannelCount; |
| 2099 | } |
| 2100 | |
| 2101 | // functional flags match |
Carter Hsu | 199f189 | 2021-10-15 15:47:29 +0800 | [diff] [blame] | 2102 | const int matchingFunctionalFlags = |
| 2103 | __builtin_popcount(outputDesc->mFlags & functionalFlags); |
| 2104 | const int totalFunctionalFlags = |
| 2105 | __builtin_popcount(outputDesc->mFlags & kFunctionalFlags); |
| 2106 | // Prefer matching functional flags, but subtract unnecessary functional flags. |
| 2107 | currentMatchCriteria[1] = 100 * (matchingFunctionalFlags + 1) - totalFunctionalFlags; |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2108 | |
| 2109 | // channel mask and channel count match |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2110 | uint32_t outputChannelCount = audio_channel_count_from_out_mask( |
| 2111 | outputDesc->getChannelMask()); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2112 | if (channelMask != AUDIO_CHANNEL_NONE && channelCount > 2 && |
| 2113 | channelCount <= outputChannelCount) { |
| 2114 | if ((audio_channel_mask_get_representation(channelMask) == |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2115 | audio_channel_mask_get_representation(outputDesc->getChannelMask())) && |
| 2116 | ((channelMask & outputDesc->getChannelMask()) == channelMask)) { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2117 | currentMatchCriteria[2] = outputChannelCount; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2118 | } |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2119 | currentMatchCriteria[3] = outputChannelCount; |
| 2120 | } |
| 2121 | |
| 2122 | // sampling rate match |
jiabin | b12a6da | 2022-06-03 20:48:18 +0000 | [diff] [blame] | 2123 | if (samplingRate > SAMPLE_RATE_HZ_DEFAULT) { |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2124 | currentMatchCriteria[4] = outputDesc->getSamplingRate(); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | // performance flags match |
| 2128 | currentMatchCriteria[5] = popcount(outputDesc->mFlags & performanceFlags); |
| 2129 | |
| 2130 | // format match |
| 2131 | if (format != AUDIO_FORMAT_INVALID) { |
| 2132 | currentMatchCriteria[6] = |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 2133 | PolicyAudioPort::kFormatDistanceMax - |
| 2134 | PolicyAudioPort::formatDistance(format, outputDesc->getFormat()); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | // primary output match |
| 2138 | currentMatchCriteria[7] = outputDesc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY; |
| 2139 | |
| 2140 | // compare match criteria by priority then value |
| 2141 | if (std::lexicographical_compare(bestMatchCriteria.begin(), bestMatchCriteria.end(), |
| 2142 | currentMatchCriteria.begin(), currentMatchCriteria.end())) { |
| 2143 | bestMatchCriteria = currentMatchCriteria; |
| 2144 | bestOutput = output; |
| 2145 | |
| 2146 | std::stringstream result; |
| 2147 | std::copy(bestMatchCriteria.begin(), bestMatchCriteria.end(), |
| 2148 | std::ostream_iterator<int>(result, " ")); |
| 2149 | ALOGV("%s new bestOutput %d criteria %s", |
| 2150 | __func__, bestOutput, result.str().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2151 | } |
| 2152 | } |
| 2153 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2154 | return bestOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2155 | } |
| 2156 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2157 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2158 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2159 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2160 | |
| 2161 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2162 | if (outputDesc == 0) { |
| 2163 | ALOGW("startOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2164 | return BAD_VALUE; |
| 2165 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2166 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2167 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2168 | ALOGV("startOutput() output %d, stream %d, session %d", |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2169 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2170 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2171 | status_t status = outputDesc->start(); |
| 2172 | if (status != NO_ERROR) { |
| 2173 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2174 | } |
| 2175 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2176 | uint32_t delayMs; |
| 2177 | status = startSource(outputDesc, client, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2178 | |
| 2179 | if (status != NO_ERROR) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2180 | outputDesc->stop(); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2181 | if (status == DEAD_OBJECT) { |
| 2182 | sp<SwAudioOutputDescriptor> desc = |
| 2183 | reopenOutput(outputDesc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 2184 | if (desc == nullptr) { |
| 2185 | // This is not common, it may indicate something wrong with the HAL. |
| 2186 | ALOGE("%s unable to open output with default config", __func__); |
| 2187 | return status; |
| 2188 | } |
| 2189 | desc->mUsePreferredMixerAttributes = true; |
| 2190 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2191 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2192 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2193 | |
| 2194 | // If the client is the first one active on preferred mixer parameters, reopen the output |
| 2195 | // if the current mixer parameters doesn't match the preferred one. |
| 2196 | if (outputDesc->devices().size() == 1) { |
| 2197 | sp<PreferredMixerAttributesInfo> info = getPreferredMixerAttributesInfo( |
| 2198 | outputDesc->devices()[0]->getId(), client->strategy()); |
| 2199 | if (info != nullptr && info->getUid() == client->uid()) { |
| 2200 | if (info->getActiveClientCount() == 0 && !outputDesc->isConfigurationMatched( |
| 2201 | info->getConfigBase(), info->getFlags())) { |
| 2202 | stopSource(outputDesc, client); |
| 2203 | outputDesc->stop(); |
| 2204 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 2205 | config.channel_mask = info->getConfigBase().channel_mask; |
| 2206 | config.sample_rate = info->getConfigBase().sample_rate; |
| 2207 | config.format = info->getConfigBase().format; |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2208 | sp<SwAudioOutputDescriptor> desc = |
| 2209 | reopenOutput(outputDesc, &config, info->getFlags(), __func__); |
| 2210 | if (desc == nullptr) { |
| 2211 | return BAD_VALUE; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2212 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2213 | desc->mUsePreferredMixerAttributes = true; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2214 | // Intentionally return error to let the client side resending request for |
| 2215 | // creating and starting. |
| 2216 | return DEAD_OBJECT; |
| 2217 | } |
| 2218 | info->increaseActiveClient(); |
jiabin | e3d1f55 | 2023-06-14 17:42:17 +0000 | [diff] [blame] | 2219 | if (info->getActiveClientCount() == 1 && |
| 2220 | (info->getFlags() & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != AUDIO_OUTPUT_FLAG_NONE) { |
| 2221 | // If it is first bit-perfect client, reroute all clients that will be routed to |
| 2222 | // the bit-perfect sink so that it is guaranteed only bit-perfect stream is active. |
| 2223 | PortHandleVector clientsToInvalidate; |
| 2224 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2225 | if (mOutputs[i] == outputDesc || |
jiabin | 98c519c | 2023-07-05 17:34:21 +0000 | [diff] [blame] | 2226 | mOutputs[i]->devices().filter(outputDesc->devices()).isEmpty()) { |
jiabin | e3d1f55 | 2023-06-14 17:42:17 +0000 | [diff] [blame] | 2227 | continue; |
| 2228 | } |
| 2229 | for (const auto& c : mOutputs[i]->getClientIterable()) { |
| 2230 | clientsToInvalidate.push_back(c->portId()); |
| 2231 | } |
| 2232 | } |
| 2233 | if (!clientsToInvalidate.empty()) { |
| 2234 | ALOGD("%s Invalidate clients due to first bit-perfect client started", |
| 2235 | __func__); |
| 2236 | mpClientInterface->invalidateTracks(clientsToInvalidate); |
| 2237 | } |
| 2238 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2239 | } |
| 2240 | } |
| 2241 | |
Jean-Michel Trivi | b1f6e64 | 2023-02-07 20:49:04 +0000 | [diff] [blame] | 2242 | if (client->hasPreferredDevice()) { |
| 2243 | // playback activity with preferred device impacts routing occurred, inform upper layers |
| 2244 | mpClientInterface->onRoutingUpdated(); |
| 2245 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2246 | if (delayMs != 0) { |
| 2247 | usleep(delayMs * 1000); |
| 2248 | } |
| 2249 | |
| 2250 | return status; |
| 2251 | } |
| 2252 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2253 | bool AudioPolicyManager::isLeUnicastActive() const { |
| 2254 | if (isInCall()) { |
| 2255 | return true; |
| 2256 | } |
| 2257 | return isAnyDeviceTypeActive(getAudioDeviceOutLeAudioUnicastSet()); |
| 2258 | } |
| 2259 | |
| 2260 | bool AudioPolicyManager::isAnyDeviceTypeActive(const DeviceTypeSet& deviceTypes) const { |
| 2261 | if (mAvailableOutputDevices.getDevicesFromTypes(deviceTypes).isEmpty()) { |
| 2262 | return false; |
| 2263 | } |
| 2264 | bool active = mOutputs.isAnyDeviceTypeActive(deviceTypes); |
| 2265 | ALOGV("%s active %d", __func__, active); |
| 2266 | return active; |
| 2267 | } |
| 2268 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2269 | status_t AudioPolicyManager::startSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 2270 | const sp<TrackClientDescriptor>& client, |
| 2271 | uint32_t *delayMs) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2272 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2273 | // cannot start playback of STREAM_TTS if any other output is being used |
| 2274 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2275 | |
| 2276 | *delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2277 | audio_stream_type_t stream = client->stream(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2278 | auto clientVolSrc = client->volumeSource(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2279 | auto clientStrategy = client->strategy(); |
| 2280 | auto clientAttr = client->attributes(); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2281 | if (stream == AUDIO_STREAM_TTS) { |
| 2282 | ALOGV("\t found BEACON stream"); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2283 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive( |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 2284 | toVolumeSource(AUDIO_STREAM_TTS, false) /*sourceToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2285 | return INVALID_OPERATION; |
| 2286 | } else { |
| 2287 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 2288 | } |
| 2289 | } else { |
| 2290 | // some playback other than beacon starts |
| 2291 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 2292 | } |
| 2293 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2294 | // 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] | 2295 | // check active before incrementing usage count |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 2296 | bool force = !outputDesc->isActive() && !outputDesc->isRouted(); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2297 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2298 | DeviceVector devices; |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2299 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2300 | const char *address = NULL; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2301 | if (policyMix != nullptr) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2302 | audio_devices_t newDeviceType; |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2303 | address = policyMix->mDeviceAddress.c_str(); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2304 | 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] | 2305 | newDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2306 | } else { |
| 2307 | newDeviceType = policyMix->mDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2308 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2309 | sp device = mAvailableOutputDevices.getDevice(newDeviceType, String8(address), |
| 2310 | AUDIO_FORMAT_DEFAULT); |
| 2311 | ALOG_ASSERT(device, "%s: no device found t=%u, a=%s", __func__, newDeviceType, address); |
| 2312 | devices.add(device); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2313 | } |
| 2314 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2315 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 2316 | // It covers a common case when there is no materially active audio |
| 2317 | // and muting would result in unnecessary delay and dropped audio. |
| 2318 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 2319 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2320 | bool wasLeUnicastActive = isLeUnicastActive(); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2321 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2322 | // increment usage count for this stream on the requested output: |
| 2323 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 2324 | // 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] | 2325 | outputDesc->setClientActive(client, true); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2326 | |
| 2327 | if (client->hasPreferredDevice(true)) { |
Eric Laurent | 72af801 | 2023-03-15 17:36:22 +0100 | [diff] [blame] | 2328 | if (outputDesc->sameExclusivePreferredDevicesCount() > 0) { |
François Gaffie | f96e543 | 2019-04-09 17:13:56 +0200 | [diff] [blame] | 2329 | // Preferred device may be exclusive, use only if no other active clients on this output |
| 2330 | devices = DeviceVector( |
| 2331 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId())); |
| 2332 | } else { |
| 2333 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
| 2334 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2335 | if (devices != outputDesc->devices()) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2336 | checkStrategyRoute(clientStrategy, outputDesc->mIoHandle); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2337 | } |
| 2338 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2339 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2340 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2341 | selectOutputForMusicEffects(); |
| 2342 | } |
| 2343 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2344 | if (outputDesc->getActivityCount(clientVolSrc) == 1 || !devices.isEmpty()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2345 | // starting an output being rerouted? |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2346 | if (devices.isEmpty()) { |
| 2347 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2348 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2349 | bool shouldWait = |
| 2350 | (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM)) || |
| 2351 | followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_NOTIFICATION)) || |
| 2352 | (beaconMuteLatency > 0)); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2353 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2354 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2355 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2356 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2357 | // An output has a shared device if |
| 2358 | // - managed by the same hw module |
| 2359 | // - supports the currently selected device |
| 2360 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2361 | && (!desc->filterSupportedDevices(devices).isEmpty()); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2362 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2363 | // force a device change if any other output is: |
| 2364 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 2365 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2366 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2367 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2368 | // 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] | 2369 | // change the device currently selected by the other output. |
| 2370 | if (sharedDevice && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2371 | desc->devices() != devices && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2372 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2373 | force = true; |
| 2374 | } |
| 2375 | // 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] | 2376 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 2377 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2378 | const uint32_t latencyMs = desc->latency(); |
| 2379 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 2380 | |
| 2381 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 2382 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2383 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2384 | |
| 2385 | // Require mute check if another output is on a shared device |
| 2386 | // and currently active to have proper drain and avoid pops. |
| 2387 | // Note restoring AudioTracks onto this output needs to invoke |
| 2388 | // a volume ramp if there is no mute. |
| 2389 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2390 | } |
| 2391 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2392 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2393 | if (outputDesc->mUsePreferredMixerAttributes && devices != outputDesc->devices()) { |
| 2394 | // If the output is open with preferred mixer attributes, but the routed device is |
| 2395 | // changed when calling this function, returning DEAD_OBJECT to indicate routing |
| 2396 | // changed. |
| 2397 | return DEAD_OBJECT; |
| 2398 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2399 | const uint32_t muteWaitMs = |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 2400 | setOutputDevices(__func__, outputDesc, devices, force, 0, nullptr, |
| 2401 | requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2402 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2403 | // apply volume rules for current stream and device if necessary |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2404 | auto &curves = getVolumeCurves(client->attributes()); |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2405 | if (NO_ERROR != checkAndSetVolume(curves, client->volumeSource(), |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2406 | curves.getVolumeIndex(outputDesc->devices().types()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2407 | outputDesc, |
Francois Gaffie | d11442b | 2020-04-27 11:51:09 +0200 | [diff] [blame] | 2408 | outputDesc->devices().types(), 0 /*delay*/, |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2409 | outputDesc->useHwGain() /*force*/)) { |
| 2410 | // request AudioService to reinitialize the volume curves asynchronously |
| 2411 | ALOGE("checkAndSetVolume failed, requesting volume range init"); |
| 2412 | mpClientInterface->onVolumeRangeInitRequest(); |
| 2413 | }; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2414 | |
| 2415 | // update the outputs if starting an output with a stream that can affect notification |
| 2416 | // routing |
| 2417 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2418 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 2419 | // force reevaluating accessibility routing when ringtone or alarm starts |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2420 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM))) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 2421 | invalidateStreams({AUDIO_STREAM_ACCESSIBILITY}); |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 2422 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2423 | |
| 2424 | if (waitMs > muteWaitMs) { |
| 2425 | *delayMs = waitMs - muteWaitMs; |
| 2426 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2427 | |
| 2428 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 2429 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 2430 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 2431 | // change occurs after the MixerThread starts and causes a stream volume |
| 2432 | // glitch. |
| 2433 | // |
| 2434 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2435 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2436 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2437 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2438 | mEngine->getForceUse( |
| 2439 | AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2440 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), true, outputDesc); |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2441 | } |
| 2442 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2443 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 2444 | // of type MIX_TYPE_RECORDERS |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2445 | if (isSingleDeviceType(devices.types(), &audio_is_remote_submix_device) && |
| 2446 | policyMix != NULL && policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2447 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2448 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2449 | address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2450 | "remote-submix", |
| 2451 | AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2452 | } |
| 2453 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2454 | checkLeBroadcastRoutes(wasLeUnicastActive, outputDesc, *delayMs); |
| 2455 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2456 | return NO_ERROR; |
| 2457 | } |
| 2458 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2459 | void AudioPolicyManager::checkLeBroadcastRoutes(bool wasUnicastActive, |
| 2460 | sp<SwAudioOutputDescriptor> ignoredOutput, uint32_t delayMs) { |
| 2461 | bool isUnicastActive = isLeUnicastActive(); |
| 2462 | |
| 2463 | if (wasUnicastActive != isUnicastActive) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2464 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2465 | //reroute all outputs routed to LE broadcast if LE unicast activy changed on any output |
| 2466 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2467 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2468 | if (desc != ignoredOutput && desc->isActive() |
| 2469 | && ((isUnicastActive && |
| 2470 | !desc->devices(). |
| 2471 | getDevicesFromType(AUDIO_DEVICE_OUT_BLE_BROADCAST).isEmpty()) |
| 2472 | || (wasUnicastActive && |
| 2473 | !desc->devices().getDevicesFromTypes( |
| 2474 | getAudioDeviceOutLeAudioUnicastSet()).isEmpty()))) { |
| 2475 | DeviceVector newDevices = getNewOutputDevices(desc, false /*fromCache*/); |
| 2476 | bool force = desc->devices() != newDevices; |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2477 | if (desc->mUsePreferredMixerAttributes && force) { |
| 2478 | // If the device is using preferred mixer attributes, the output need to reopen |
| 2479 | // with default configuration when the new selected devices are different from |
| 2480 | // current routing devices. |
| 2481 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices); |
| 2482 | continue; |
| 2483 | } |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 2484 | setOutputDevices(__func__, desc, newDevices, force, delayMs); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2485 | // re-apply device specific volume if not done by setOutputDevice() |
| 2486 | if (!force) { |
| 2487 | applyStreamVolumes(desc, newDevices.types(), delayMs); |
| 2488 | } |
| 2489 | } |
| 2490 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2491 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2492 | } |
| 2493 | } |
| 2494 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2495 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2496 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2497 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2498 | |
| 2499 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2500 | if (outputDesc == 0) { |
| 2501 | ALOGW("stopOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2502 | return BAD_VALUE; |
| 2503 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2504 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2505 | |
Jean-Michel Trivi | b1f6e64 | 2023-02-07 20:49:04 +0000 | [diff] [blame] | 2506 | if (client->hasPreferredDevice(true)) { |
| 2507 | // playback activity with preferred device impacts routing occurred, inform upper layers |
| 2508 | mpClientInterface->onRoutingUpdated(); |
| 2509 | } |
| 2510 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2511 | ALOGV("stopOutput() output %d, stream %d, session %d", |
| 2512 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2513 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2514 | status_t status = stopSource(outputDesc, client); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2515 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2516 | if (status == NO_ERROR ) { |
| 2517 | outputDesc->stop(); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2518 | } else { |
| 2519 | return status; |
| 2520 | } |
| 2521 | |
| 2522 | if (outputDesc->devices().size() == 1) { |
| 2523 | sp<PreferredMixerAttributesInfo> info = getPreferredMixerAttributesInfo( |
| 2524 | outputDesc->devices()[0]->getId(), client->strategy()); |
| 2525 | if (info != nullptr && info->getUid() == client->uid()) { |
| 2526 | info->decreaseActiveClient(); |
| 2527 | if (info->getActiveClientCount() == 0) { |
| 2528 | reopenOutput(outputDesc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 2529 | } |
| 2530 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2531 | } |
| 2532 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2533 | } |
| 2534 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2535 | status_t AudioPolicyManager::stopSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 2536 | const sp<TrackClientDescriptor>& client) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2537 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2538 | // always handle stream stop, check which stream type is stopping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2539 | audio_stream_type_t stream = client->stream(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2540 | auto clientVolSrc = client->volumeSource(); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2541 | bool wasLeUnicastActive = isLeUnicastActive(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2542 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2543 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 2544 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2545 | if (outputDesc->getActivityCount(clientVolSrc) > 0) { |
| 2546 | if (outputDesc->getActivityCount(clientVolSrc) == 1) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2547 | // Automatically disable the remote submix input when output is stopped on a |
| 2548 | // re routing mix of type MIX_TYPE_RECORDERS |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2549 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2550 | if (isSingleDeviceType( |
| 2551 | outputDesc->devices().types(), &audio_is_remote_submix_device) && |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2552 | policyMix != nullptr && |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2553 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2554 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2555 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2556 | policyMix->mDeviceAddress, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2557 | "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2558 | } |
| 2559 | } |
| 2560 | bool forceDeviceUpdate = false; |
Eric Laurent | 72af801 | 2023-03-15 17:36:22 +0100 | [diff] [blame] | 2561 | if (client->hasPreferredDevice(true) && |
| 2562 | outputDesc->sameExclusivePreferredDevicesCount() < 2) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2563 | checkStrategyRoute(client->strategy(), AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2564 | forceDeviceUpdate = true; |
| 2565 | } |
| 2566 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2567 | // decrement usage count of this stream on the output |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 2568 | outputDesc->setClientActive(client, false); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 2569 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2570 | // store time at which the stream was stopped - see isStreamActive() |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2571 | if (outputDesc->getActivityCount(clientVolSrc) == 0 || forceDeviceUpdate) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2572 | outputDesc->setStopTime(client, systemTime()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2573 | DeviceVector newDevices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 2574 | |
| 2575 | // If the routing does not change, if an output is routed on a device using HwGain |
| 2576 | // (aka setAudioPortConfig) and there are still active clients following different |
| 2577 | // volume group(s), force reapply volume |
| 2578 | bool requiresVolumeCheck = outputDesc->getActivityCount(clientVolSrc) == 0 && |
| 2579 | outputDesc->useHwGain() && outputDesc->isAnyActive(VOLUME_SOURCE_NONE); |
| 2580 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2581 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 2582 | // the track stop() command is received and at that time the audio track buffer can |
| 2583 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 2584 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 2585 | // audio path (audio DSP, CODEC ...) |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 2586 | setOutputDevices(__func__, outputDesc, newDevices, false, outputDesc->latency()*2, |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 2587 | nullptr, true /*requiresMuteCheck*/, requiresVolumeCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2588 | |
| 2589 | // force restoring the device selection on other active outputs if it differs from the |
| 2590 | // one being selected for this output |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2591 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2592 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2593 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2594 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2595 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2596 | desc->isActive() && |
| 2597 | outputDesc->sharesHwModuleWith(desc) && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2598 | (newDevices != desc->devices())) { |
| 2599 | DeviceVector newDevices2 = getNewOutputDevices(desc, false /*fromCache*/); |
| 2600 | bool force = desc->devices() != newDevices2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 2601 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2602 | if (desc->mUsePreferredMixerAttributes && force) { |
| 2603 | // If the device is using preferred mixer attributes, the output need to |
| 2604 | // reopen with default configuration when the new selected devices are |
| 2605 | // different from current routing devices. |
| 2606 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices2); |
| 2607 | continue; |
| 2608 | } |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 2609 | setOutputDevices(__func__, desc, newDevices2, force, delayMs); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2610 | |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2611 | // re-apply device specific volume if not done by setOutputDevice() |
| 2612 | if (!force) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2613 | applyStreamVolumes(desc, newDevices2.types(), delayMs); |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2614 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2615 | } |
| 2616 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2617 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2618 | // update the outputs if stopping one with a stream that can affect notification routing |
| 2619 | handleNotificationRoutingForStream(stream); |
| 2620 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2621 | |
| 2622 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 2623 | 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] | 2624 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), false, outputDesc); |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2625 | } |
| 2626 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2627 | if (followsSameRouting(client->attributes(), attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2628 | selectOutputForMusicEffects(); |
| 2629 | } |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2630 | |
| 2631 | checkLeBroadcastRoutes(wasLeUnicastActive, outputDesc, outputDesc->latency()*2); |
| 2632 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2633 | return NO_ERROR; |
| 2634 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2635 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2636 | return INVALID_OPERATION; |
| 2637 | } |
| 2638 | } |
| 2639 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2640 | bool AudioPolicyManager::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2641 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2642 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2643 | |
| 2644 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2645 | if (outputDesc == 0) { |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2646 | // If an output descriptor is closed due to a device routing change, |
| 2647 | // then there are race conditions with releaseOutput from tracks |
| 2648 | // that may be destroyed (with no PlaybackThread) or a PlaybackThread |
| 2649 | // destroyed shortly thereafter. |
| 2650 | // |
| 2651 | // Here we just log a warning, instead of a fatal error. |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2652 | ALOGW("releaseOutput() no output for client %d", portId); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2653 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2654 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2655 | |
| 2656 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2657 | |
Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2658 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
| 2659 | if (outputDesc->isClientActive(client)) { |
| 2660 | ALOGW("releaseOutput() inactivates portId %d in good faith", portId); |
| 2661 | stopOutput(portId); |
| 2662 | } |
| 2663 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2664 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 2665 | if (outputDesc->mDirectOpenCount <= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2666 | ALOGW("releaseOutput() invalid open count %d for output %d", |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2667 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2668 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2669 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2670 | if (--outputDesc->mDirectOpenCount == 0) { |
| 2671 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2672 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2673 | } |
| 2674 | } |
Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2675 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2676 | outputDesc->removeClient(portId); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2677 | if (outputDesc->mPendingReopenToQueryProfiles && outputDesc->getClientCount() == 0) { |
| 2678 | // The output is pending reopened to query dynamic profiles and |
| 2679 | // there is no active clients |
| 2680 | closeOutput(outputDesc->mIoHandle); |
| 2681 | sp<SwAudioOutputDescriptor> newOutputDesc = openOutputWithProfileAndDevice( |
| 2682 | outputDesc->mProfile, mEngine->getActiveMediaDevices(mAvailableOutputDevices)); |
| 2683 | if (newOutputDesc == nullptr) { |
| 2684 | ALOGE("%s failed to open output", __func__); |
| 2685 | } |
| 2686 | return true; |
| 2687 | } |
| 2688 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2689 | } |
| 2690 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2691 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 2692 | audio_io_handle_t *input, |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2693 | audio_unique_id_t riid, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2694 | audio_session_t session, |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2695 | const AttributionSourceState& attributionSource, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2696 | audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2697 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2698 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2699 | input_type_t *inputType, |
| 2700 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2701 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2702 | 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] | 2703 | "flags %#x attributes=%s requested device ID %d", |
| 2704 | __func__, attr->source, config->sample_rate, config->format, config->channel_mask, |
| 2705 | session, flags, toString(*attr).c_str(), *selectedDeviceId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2706 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2707 | status_t status = NO_ERROR; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2708 | audio_attributes_t attributes = *attr; |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2709 | sp<AudioPolicyMix> policyMix; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2710 | sp<DeviceDescriptor> device; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2711 | sp<AudioInputDescriptor> inputDesc; |
François Gaffie | 1b4753e | 2023-02-06 10:36:33 +0100 | [diff] [blame] | 2712 | sp<AudioInputDescriptor> previousInputDesc; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2713 | sp<RecordClientDescriptor> clientDesc; |
| 2714 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2715 | 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] | 2716 | bool isSoundTrigger; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2717 | |
| 2718 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 2719 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 2720 | return INVALID_OPERATION; |
| 2721 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2722 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2723 | if (attr->source == AUDIO_SOURCE_DEFAULT) { |
| 2724 | attributes.source = AUDIO_SOURCE_MIC; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2725 | } |
| 2726 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2727 | // Explicit routing? |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 2728 | sp<DeviceDescriptor> explicitRoutingDevice = |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2729 | mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2730 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2731 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 2732 | // possible |
| 2733 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 2734 | *input != AUDIO_IO_HANDLE_NONE) { |
| 2735 | ssize_t index = mInputs.indexOfKey(*input); |
| 2736 | if (index < 0) { |
| 2737 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 2738 | status = BAD_VALUE; |
| 2739 | goto error; |
| 2740 | } |
| 2741 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2742 | RecordClientVector clients = inputDesc->getClientsForSession(session); |
| 2743 | if (clients.size() == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2744 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 2745 | status = BAD_VALUE; |
| 2746 | goto error; |
| 2747 | } |
| 2748 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 2749 | // 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] | 2750 | // corresponds to a new client and is only permitted from the same UID. |
| 2751 | // 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] | 2752 | if (clients.size() > 1) { |
| 2753 | for (const auto& client : clients) { |
| 2754 | // The client map is ordered by key values (portId) and portIds are allocated |
| 2755 | // incrementaly. So the first client in this list is the one opened by audio flinger |
| 2756 | // when the mmap stream is created and should be ignored as it does not correspond |
| 2757 | // to an actual client |
| 2758 | if (client == *clients.cbegin()) { |
| 2759 | continue; |
| 2760 | } |
| 2761 | if (uid != client->uid() && !client->isSilenced()) { |
| 2762 | ALOGW("getInputForAttr() bad uid %d for client %d uid %d", |
| 2763 | uid, client->portId(), client->uid()); |
| 2764 | status = INVALID_OPERATION; |
| 2765 | goto error; |
| 2766 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 2767 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2768 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2769 | *inputType = API_INPUT_LEGACY; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2770 | device = inputDesc->getDevice(); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2771 | |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 2772 | ALOGV("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2773 | goto exit; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | *input = AUDIO_IO_HANDLE_NONE; |
| 2777 | *inputType = API_INPUT_INVALID; |
| 2778 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2779 | if (attributes.source == AUDIO_SOURCE_REMOTE_SUBMIX && |
Jan Sebechlebsky | bc56bcd | 2022-09-26 13:15:19 +0200 | [diff] [blame] | 2780 | extractAddressFromAudioAttributes(attributes).has_value()) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2781 | status = mPolicyMixes.getInputMixForAttr(attributes, &policyMix); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2782 | if (status != NO_ERROR) { |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2783 | ALOGW("%s could not find input mix for attr %s", |
| 2784 | __func__, toString(attributes).c_str()); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2785 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2786 | } |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2787 | device = mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2788 | String8(attr->tags + strlen("addr=")), |
| 2789 | AUDIO_FORMAT_DEFAULT); |
| 2790 | if (device == nullptr) { |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 2791 | 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] | 2792 | __func__, attributes.source, attributes.tags); |
| 2793 | status = BAD_VALUE; |
| 2794 | goto error; |
| 2795 | } |
| 2796 | |
Kevin Rocard | 25f9b05 | 2019-02-27 15:08:54 -0800 | [diff] [blame] | 2797 | if (is_mix_loopback_render(policyMix->mRouteFlags)) { |
| 2798 | *inputType = API_INPUT_MIX_PUBLIC_CAPTURE_PLAYBACK; |
| 2799 | } else { |
| 2800 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 2801 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2802 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2803 | if (explicitRoutingDevice != nullptr) { |
| 2804 | device = explicitRoutingDevice; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2805 | } else { |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 2806 | // Prevent from storing invalid requested device id in clients |
| 2807 | requestedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 2808 | device = mEngine->getInputDeviceForAttributes(attributes, uid, session, &policyMix); |
yuanjiahsu | 4069d5d | 2021-04-19 07:54:27 +0800 | [diff] [blame] | 2809 | ALOGV_IF(device != nullptr, "%s found device type is 0x%X", |
| 2810 | __FUNCTION__, device->type()); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2811 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2812 | if (device == nullptr) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2813 | ALOGW("getInputForAttr() could not find device for source %d", attributes.source); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2814 | status = BAD_VALUE; |
| 2815 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2816 | } |
Alden DSouza | b7d2078 | 2021-02-08 08:51:42 -0800 | [diff] [blame] | 2817 | if (device->type() == AUDIO_DEVICE_IN_ECHO_REFERENCE) { |
| 2818 | *inputType = API_INPUT_MIX_CAPTURE; |
| 2819 | } else if (policyMix) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2820 | ALOG_ASSERT(policyMix->mMixType == MIX_TYPE_RECORDERS, "Invalid Mix Type"); |
| 2821 | // there is an external policy, but this input is attached to a mix of recorders, |
| 2822 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 2823 | // know about it and is therefore considered "legacy" |
| 2824 | *inputType = API_INPUT_LEGACY; |
| 2825 | } else if (audio_is_remote_submix_device(device->type())) { |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2826 | *inputType = API_INPUT_MIX_CAPTURE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2827 | } else if (device->type() == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 2828 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2829 | } else { |
| 2830 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2831 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 2832 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2833 | } |
| 2834 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2835 | *input = getInputForDevice(device, session, attributes, config, flags, policyMix); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2836 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2837 | status = INVALID_OPERATION; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2838 | AudioProfileVector profiles; |
| 2839 | status_t ret = getProfilesForDevices( |
| 2840 | DeviceVector(device), profiles, flags, true /*isInput*/); |
| 2841 | if (ret == NO_ERROR && !profiles.empty()) { |
Robert Wu | b98ff1b | 2023-06-15 22:53:58 +0000 | [diff] [blame] | 2842 | const auto channels = profiles[0]->getChannels(); |
| 2843 | if (!channels.empty() && (channels.find(config->channel_mask) == channels.end())) { |
| 2844 | config->channel_mask = *channels.begin(); |
| 2845 | } |
| 2846 | const auto sampleRates = profiles[0]->getSampleRates(); |
| 2847 | if (!sampleRates.empty() && |
| 2848 | (sampleRates.find(config->sample_rate) == sampleRates.end())) { |
| 2849 | config->sample_rate = *sampleRates.begin(); |
| 2850 | } |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2851 | config->format = profiles[0]->getFormat(); |
| 2852 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2853 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2854 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2855 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2856 | exit: |
| 2857 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2858 | *selectedDeviceId = mAvailableInputDevices.contains(device) ? |
| 2859 | device->getId() : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2860 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2861 | isSoundTrigger = attributes.source == AUDIO_SOURCE_HOTWORD && |
Carter Hsu | d0cce2e | 2019-05-03 17:36:28 +0800 | [diff] [blame] | 2862 | mSoundTriggerSessions.indexOfKey(session) >= 0; |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 2863 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2864 | |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2865 | clientDesc = new RecordClientDescriptor(*portId, riid, uid, session, attributes, *config, |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2866 | requestedDeviceId, attributes.source, flags, |
| 2867 | isSoundTrigger); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2868 | inputDesc = mInputs.valueFor(*input); |
François Gaffie | 1b4753e | 2023-02-06 10:36:33 +0100 | [diff] [blame] | 2869 | // Move (if found) effect for the client session to its input |
| 2870 | mEffects.moveEffectsForIo(session, *input, &mInputs, mpClientInterface); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2871 | inputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2872 | |
| 2873 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", |
| 2874 | *input, *inputType, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2875 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2876 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2877 | |
| 2878 | error: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2879 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2880 | } |
| 2881 | |
| 2882 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2883 | audio_io_handle_t AudioPolicyManager::getInputForDevice(const sp<DeviceDescriptor> &device, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2884 | audio_session_t session, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2885 | const audio_attributes_t &attributes, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2886 | audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2887 | audio_input_flags_t flags, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2888 | const sp<AudioPolicyMix> &policyMix) |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2889 | { |
| 2890 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2891 | audio_source_t halInputSource = attributes.source; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2892 | bool isSoundTrigger = false; |
| 2893 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2894 | if (attributes.source == AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2895 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 2896 | if (index >= 0) { |
| 2897 | input = mSoundTriggerSessions.valueFor(session); |
| 2898 | isSoundTrigger = true; |
| 2899 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 2900 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 2901 | } else { |
| 2902 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2903 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2904 | } else if (attributes.source == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2905 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 2906 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2907 | } |
| 2908 | |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 2909 | if (attributes.source == AUDIO_SOURCE_ULTRASOUND) { |
| 2910 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_ULTRASOUND); |
| 2911 | } |
| 2912 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2913 | // sampling rate and flags may be updated by getInputProfile |
| 2914 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 2915 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 2916 | audio_format_t profileFormat = config->format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2917 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2918 | audio_input_flags_t profileFlags = flags; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 2919 | // find a compatible input profile (not necessarily identical in parameters) |
| 2920 | sp<IOProfile> profile = getInputProfile( |
| 2921 | device, profileSamplingRate, profileFormat, profileChannelMask, profileFlags); |
| 2922 | if (profile == nullptr) { |
| 2923 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2924 | } |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 2925 | |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2926 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2927 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2928 | if (samplingRate == 0) { |
| 2929 | samplingRate = profileSamplingRate; |
| 2930 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2931 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 2932 | if (profile->getModuleHandle() == 0) { |
| 2933 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2934 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2935 | } |
| 2936 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2937 | // Reuse an already opened input if a client with the same session ID already exists |
| 2938 | // on that input |
| 2939 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2940 | sp <AudioInputDescriptor> desc = mInputs.valueAt(i); |
| 2941 | if (desc->mProfile != profile) { |
| 2942 | continue; |
| 2943 | } |
| 2944 | RecordClientVector clients = desc->clientsList(); |
| 2945 | for (const auto &client : clients) { |
| 2946 | if (session == client->session()) { |
| 2947 | return desc->mIoHandle; |
| 2948 | } |
| 2949 | } |
| 2950 | } |
| 2951 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2952 | if (!profile->canOpenNewIo()) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2953 | for (size_t i = 0; i < mInputs.size(); ) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 2954 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2955 | if (desc->mProfile != profile) { |
Carter Hsu | 9a645a9 | 2019-05-15 12:15:32 +0800 | [diff] [blame] | 2956 | i++; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2957 | continue; |
| 2958 | } |
| 2959 | // if sound trigger, reuse input if used by other sound trigger on same session |
| 2960 | // else |
| 2961 | // reuse input if active client app is not in IDLE state |
| 2962 | // |
| 2963 | RecordClientVector clients = desc->clientsList(); |
| 2964 | bool doClose = false; |
| 2965 | for (const auto& client : clients) { |
| 2966 | if (isSoundTrigger != client->isSoundTrigger()) { |
| 2967 | continue; |
| 2968 | } |
| 2969 | if (client->isSoundTrigger()) { |
| 2970 | if (session == client->session()) { |
| 2971 | return desc->mIoHandle; |
| 2972 | } |
| 2973 | continue; |
| 2974 | } |
| 2975 | if (client->active() && client->appState() != APP_STATE_IDLE) { |
| 2976 | return desc->mIoHandle; |
| 2977 | } |
| 2978 | doClose = true; |
| 2979 | } |
| 2980 | if (doClose) { |
| 2981 | closeInput(desc->mIoHandle); |
| 2982 | } else { |
| 2983 | i++; |
| 2984 | } |
| 2985 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2986 | } |
| 2987 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2988 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2989 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2990 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 2991 | lConfig.sample_rate = profileSamplingRate; |
| 2992 | lConfig.channel_mask = profileChannelMask; |
| 2993 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 2994 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2995 | status_t status = inputDesc->open(&lConfig, device, halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2996 | |
| 2997 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2998 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2999 | (profileSamplingRate != lConfig.sample_rate) || |
| 3000 | !audio_formats_match(profileFormat, lConfig.format) || |
| 3001 | (profileChannelMask != lConfig.channel_mask)) { |
| 3002 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 3003 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3004 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3005 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3006 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3007 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3008 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3009 | } |
| 3010 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3011 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3012 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3013 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3014 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 3015 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3016 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3017 | } |
| 3018 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3019 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId) |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 3020 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3021 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 3022 | |
| 3023 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 3024 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3025 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 3026 | return DEAD_OBJECT; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3027 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3028 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3029 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3030 | if (client->active()) { |
| 3031 | ALOGW("%s input %d client %d already started", __FUNCTION__, input, client->portId()); |
| 3032 | return INVALID_OPERATION; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 3033 | } |
| 3034 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3035 | audio_session_t session = client->session(); |
| 3036 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3037 | ALOGV("%s input:%d, session:%d)", __FUNCTION__, input, session); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3038 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3039 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 3040 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3041 | status_t status = inputDesc->start(); |
| 3042 | if (status != NO_ERROR) { |
| 3043 | return status; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 3044 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3045 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 3046 | // increment activity count before calling getNewInputDevice() below as only active sessions |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 3047 | // are considered for device selection |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3048 | inputDesc->setClientActive(client, true); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 3049 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3050 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 3051 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3052 | sp<DeviceDescriptor> device = getNewInputDevice(inputDesc); |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 3053 | if (device != nullptr) { |
| 3054 | status = setInputDevice(input, device, true /* force */); |
| 3055 | } else { |
| 3056 | ALOGW("%s no new input device can be found for descriptor %d", |
| 3057 | __FUNCTION__, inputDesc->getId()); |
| 3058 | status = BAD_VALUE; |
| 3059 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3060 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 3061 | if (status == NO_ERROR && inputDesc->activeCount() == 1) { |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3062 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3063 | // 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] | 3064 | if ((policyMix != nullptr) |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3065 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 3066 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3067 | MIX_STATE_MIXING); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3068 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3069 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3070 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 3071 | if (primaryInputDevices.contains(device) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3072 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 3073 | mpClientInterface->setSoundTriggerCaptureState(true); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3074 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 3075 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3076 | // automatically enable the remote submix output when input is started if not |
| 3077 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 3078 | // 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] | 3079 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3080 | String8 address = String8(""); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 3081 | if (policyMix == nullptr) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3082 | address = String8("0"); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3083 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 3084 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 3085 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3086 | if (address != "") { |
| 3087 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 3088 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 3089 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3090 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 3091 | } |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 3092 | } else if (status != NO_ERROR) { |
| 3093 | // Restore client activity state. |
| 3094 | inputDesc->setClientActive(client, false); |
| 3095 | inputDesc->stop(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3096 | } |
| 3097 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 3098 | ALOGV("%s input %d source = %d status = %d exit", |
| 3099 | __FUNCTION__, input, client->source(), status); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3100 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 3101 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3102 | } |
| 3103 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3104 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3105 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3106 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 3107 | |
| 3108 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 3109 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3110 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3111 | return BAD_VALUE; |
| 3112 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3113 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3114 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3115 | if (!client->active()) { |
| 3116 | ALOGW("%s input %d client %d already stopped", __FUNCTION__, input, client->portId()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3117 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3118 | } |
Carter Hsu | e6139d5 | 2021-07-08 10:30:20 +0800 | [diff] [blame] | 3119 | auto old_source = inputDesc->source(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3120 | inputDesc->setClientActive(client, false); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 3121 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3122 | inputDesc->stop(); |
| 3123 | if (inputDesc->isActive()) { |
Carter Hsu | e6139d5 | 2021-07-08 10:30:20 +0800 | [diff] [blame] | 3124 | auto current_source = inputDesc->source(); |
| 3125 | setInputDevice(input, getNewInputDevice(inputDesc), |
| 3126 | old_source != current_source /* force */); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3127 | } else { |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3128 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3129 | // 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] | 3130 | if ((policyMix != nullptr) |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3131 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 3132 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3133 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 3134 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3135 | |
| 3136 | // automatically disable the remote submix output when input is stopped if not |
| 3137 | // used by a policy mix of type MIX_TYPE_RECORDERS |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3138 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3139 | String8 address = String8(""); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 3140 | if (policyMix == nullptr) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3141 | address = String8("0"); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3142 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 3143 | address = policyMix->mDeviceAddress; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3144 | } |
| 3145 | if (address != "") { |
| 3146 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 3147 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 3148 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3149 | } |
| 3150 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3151 | resetInputDevice(input); |
| 3152 | |
| 3153 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 3154 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3155 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 3156 | if (primaryInputDevices.contains(inputDesc->getDevice()) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3157 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 3158 | mpClientInterface->setSoundTriggerCaptureState(false); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3159 | } |
| 3160 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3161 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3162 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3163 | } |
| 3164 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3165 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3166 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3167 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 3168 | |
| 3169 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 3170 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3171 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3172 | return; |
| 3173 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3174 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3175 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 3176 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3177 | ALOGV("%s %d", __FUNCTION__, input); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 3178 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3179 | inputDesc->removeClient(portId); |
François Gaffie | 1b4753e | 2023-02-06 10:36:33 +0100 | [diff] [blame] | 3180 | mEffects.putOrphanEffects(client->session(), input, &mInputs, mpClientInterface); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3181 | if (inputDesc->getClientCount() > 0) { |
| 3182 | ALOGV("%s(%d) %zu clients remaining", __func__, portId, inputDesc->getClientCount()); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3183 | return; |
| 3184 | } |
| 3185 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3186 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3187 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3188 | ALOGV("%s exit", __FUNCTION__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3189 | } |
| 3190 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3191 | void AudioPolicyManager::closeActiveClients(const sp<AudioInputDescriptor>& input) |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3192 | { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3193 | RecordClientVector clients = input->clientsList(true); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3194 | |
| 3195 | for (const auto& client : clients) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3196 | closeClient(client->portId()); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3197 | } |
| 3198 | } |
| 3199 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3200 | void AudioPolicyManager::closeClient(audio_port_handle_t portId) |
| 3201 | { |
| 3202 | stopInput(portId); |
| 3203 | releaseInput(portId); |
| 3204 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3205 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3206 | void AudioPolicyManager::checkCloseInputs() { |
| 3207 | // After connecting or disconnecting an input device, close input if: |
| 3208 | // - it has no client (was just opened to check profile) OR |
| 3209 | // - none of its supported devices are connected anymore OR |
| 3210 | // - one of its clients cannot be routed to one of its supported |
| 3211 | // devices anymore. Otherwise update device selection |
| 3212 | std::vector<audio_io_handle_t> inputsToClose; |
| 3213 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3214 | const sp<AudioInputDescriptor> input = mInputs.valueAt(i); |
| 3215 | if (input->clientsList().size() == 0 |
Eric Laurent | 85732f4 | 2020-03-19 11:31:10 -0700 | [diff] [blame] | 3216 | || !mAvailableInputDevices.containsAtLeastOne(input->supportedDevices())) { |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3217 | inputsToClose.push_back(mInputs.keyAt(i)); |
| 3218 | } else { |
| 3219 | bool close = false; |
| 3220 | for (const auto& client : input->clientsList()) { |
| 3221 | sp<DeviceDescriptor> device = |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 3222 | mEngine->getInputDeviceForAttributes(client->attributes(), client->uid(), |
| 3223 | client->session()); |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3224 | if (!input->supportedDevices().contains(device)) { |
| 3225 | close = true; |
| 3226 | break; |
| 3227 | } |
| 3228 | } |
| 3229 | if (close) { |
| 3230 | inputsToClose.push_back(mInputs.keyAt(i)); |
| 3231 | } else { |
| 3232 | setInputDevice(input->mIoHandle, getNewInputDevice(input)); |
| 3233 | } |
| 3234 | } |
| 3235 | } |
| 3236 | |
| 3237 | for (const audio_io_handle_t handle : inputsToClose) { |
| 3238 | ALOGV("%s closing input %d", __func__, handle); |
| 3239 | closeInput(handle); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3240 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3241 | } |
| 3242 | |
François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 3243 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, int indexMin, int indexMax) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3244 | { |
| 3245 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 3246 | if (indexMin < 0 || indexMax < 0) { |
| 3247 | ALOGE("%s for stream %d: invalid min %d or max %d", __func__, stream , indexMin, indexMax); |
| 3248 | return; |
| 3249 | } |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 3250 | getVolumeCurves(stream).initVolume(indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3251 | |
| 3252 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3253 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 3254 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3255 | continue; |
| 3256 | } |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 3257 | getVolumeCurves((audio_stream_type_t)curStream).initVolume(indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3258 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3259 | } |
| 3260 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3261 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3262 | int index, |
| 3263 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3264 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3265 | auto attributes = mEngine->getAttributesForStreamType(stream); |
Eric Laurent | 242a9f8 | 2020-03-23 15:57:04 -0700 | [diff] [blame] | 3266 | if (attributes == AUDIO_ATTRIBUTES_INITIALIZER) { |
| 3267 | ALOGW("%s: no group for stream %s, bailing out", __func__, toString(stream).c_str()); |
| 3268 | return NO_ERROR; |
| 3269 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3270 | ALOGV("%s: stream %s attributes=%s", __func__, |
| 3271 | toString(stream).c_str(), toString(attributes).c_str()); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3272 | return setVolumeIndexForAttributes(attributes, index, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3273 | } |
| 3274 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3275 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3276 | int *index, |
| 3277 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3278 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3279 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this |
| 3280 | // stream by the engine. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3281 | DeviceTypeSet deviceTypes = {device}; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 3282 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3283 | deviceTypes = mEngine->getOutputDevicesForStream( |
| 3284 | stream, true /*fromCache*/).types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3285 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3286 | return getVolumeIndex(getVolumeCurves(stream), *index, deviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3287 | } |
| 3288 | |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3289 | status_t AudioPolicyManager::setVolumeIndexForAttributes(const audio_attributes_t &attributes, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3290 | int index, |
| 3291 | audio_devices_t device) |
| 3292 | { |
| 3293 | // Get Volume group matching the Audio Attributes |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3294 | auto group = mEngine->getVolumeGroupForAttributes(attributes); |
| 3295 | if (group == VOLUME_GROUP_NONE) { |
| 3296 | 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] | 3297 | return BAD_VALUE; |
| 3298 | } |
Eric Laurent | ae6e88c | 2024-01-10 14:42:57 +0100 | [diff] [blame] | 3299 | ALOGV("%s: group %d matching with %s index %d", |
| 3300 | __FUNCTION__, group, toString(attributes).c_str(), index); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3301 | status_t status = NO_ERROR; |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3302 | IVolumeCurves &curves = getVolumeCurves(attributes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3303 | VolumeSource vs = toVolumeSource(group); |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3304 | // AUDIO_STREAM_BLUETOOTH_SCO is only used for volume control so we remap |
| 3305 | // to AUDIO_STREAM_VOICE_CALL to match with relevant playback activity |
| 3306 | VolumeSource activityVs = (vs == toVolumeSource(AUDIO_STREAM_BLUETOOTH_SCO, false)) ? |
| 3307 | toVolumeSource(AUDIO_STREAM_VOICE_CALL, false) : vs; |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3308 | product_strategy_t strategy = mEngine->getProductStrategyForAttributes(attributes); |
| 3309 | |
| 3310 | status = setVolumeCurveIndex(index, device, curves); |
| 3311 | if (status != NO_ERROR) { |
| 3312 | ALOGE("%s failed to set curve index for group %d device 0x%X", __func__, group, device); |
| 3313 | return status; |
| 3314 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3315 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3316 | DeviceTypeSet curSrcDevices; |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3317 | auto curCurvAttrs = curves.getAttributes(); |
| 3318 | if (!curCurvAttrs.empty() && curCurvAttrs.front() != defaultAttr) { |
| 3319 | auto attr = curCurvAttrs.front(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3320 | curSrcDevices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false).types(); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3321 | } else if (!curves.getStreamTypes().empty()) { |
| 3322 | auto stream = curves.getStreamTypes().front(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3323 | curSrcDevices = mEngine->getOutputDevicesForStream(stream, false).types(); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3324 | } else { |
| 3325 | ALOGE("%s: Invalid src %d: no valid attributes nor stream",__func__, vs); |
| 3326 | return BAD_VALUE; |
| 3327 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3328 | audio_devices_t curSrcDevice = Volume::getDeviceForVolume(curSrcDevices); |
| 3329 | resetDeviceTypes(curSrcDevices, curSrcDevice); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3330 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3331 | // update volume on all outputs and streams matching the following: |
| 3332 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 3333 | // - The device (or devices) selected by the engine for this stream includes |
| 3334 | // the requested device |
| 3335 | // - For non default requested device, currently selected device on the output is either the |
| 3336 | // requested device or one of the devices selected by the engine for this stream |
| 3337 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 3338 | // no specific device volume value exists for currently selected device. |
Henrik Backlund | 18373a3 | 2024-01-24 10:26:42 +0100 | [diff] [blame] | 3339 | // - Only apply the volume if the requested device is the desired device for volume control. |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3340 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3341 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3342 | DeviceTypeSet curDevices = desc->devices().types(); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3343 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3344 | if (curDevices.erase(AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { |
| 3345 | curDevices.insert(AUDIO_DEVICE_OUT_SPEAKER); |
Robert Lee | 5e66e79 | 2019-04-03 18:37:15 +0800 | [diff] [blame] | 3346 | } |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3347 | |
| 3348 | if (!(desc->isActive(activityVs) || isInCallOrScreening())) { |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3349 | continue; |
| 3350 | } |
| 3351 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME && |
| 3352 | curDevices.find(device) == curDevices.end()) { |
| 3353 | continue; |
| 3354 | } |
| 3355 | bool applyVolume = false; |
| 3356 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
| 3357 | curSrcDevices.insert(device); |
| 3358 | applyVolume = (curSrcDevices.find( |
Henrik Backlund | 18373a3 | 2024-01-24 10:26:42 +0100 | [diff] [blame] | 3359 | Volume::getDeviceForVolume(curDevices)) != curSrcDevices.end()) |
| 3360 | && Volume::getDeviceForVolume(curSrcDevices) == device; |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3361 | } else { |
| 3362 | applyVolume = !curves.hasVolumeIndexForDevice(curSrcDevice); |
| 3363 | } |
| 3364 | if (!applyVolume) { |
| 3365 | continue; // next output |
| 3366 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3367 | // Inter / intra volume group priority management: Loop on strategies arranged by priority |
| 3368 | // If a higher priority strategy is active, and the output is routed to a device with a |
| 3369 | // HW Gain management, do not change the volume |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3370 | if (desc->useHwGain()) { |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3371 | applyVolume = false; |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 3372 | // If the volume source is active with higher priority source, ensure at least Sw Muted |
| 3373 | desc->setSwMute((index == 0), vs, curves.getStreamTypes(), curDevices, 0 /*delayMs*/); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3374 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { |
| 3375 | auto activeClients = desc->clientsList(true /*activeOnly*/, productStrategy, |
| 3376 | false /*preferredDevice*/); |
| 3377 | if (activeClients.empty()) { |
| 3378 | continue; |
| 3379 | } |
| 3380 | bool isPreempted = false; |
| 3381 | bool isHigherPriority = productStrategy < strategy; |
| 3382 | for (const auto &client : activeClients) { |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3383 | if (isHigherPriority && (client->volumeSource() != activityVs)) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3384 | ALOGV("%s: Strategy=%d (\nrequester:\n" |
| 3385 | " group %d, volumeGroup=%d attributes=%s)\n" |
| 3386 | " higher priority source active:\n" |
| 3387 | " volumeGroup=%d attributes=%s) \n" |
| 3388 | " on output %zu, bailing out", __func__, productStrategy, |
| 3389 | group, group, toString(attributes).c_str(), |
| 3390 | client->volumeSource(), toString(client->attributes()).c_str(), i); |
| 3391 | applyVolume = false; |
| 3392 | isPreempted = true; |
| 3393 | break; |
| 3394 | } |
| 3395 | // 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] | 3396 | if (client->volumeSource() == activityVs) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3397 | applyVolume = true; |
| 3398 | } |
| 3399 | } |
| 3400 | if (isPreempted || applyVolume) { |
| 3401 | break; |
| 3402 | } |
| 3403 | } |
| 3404 | if (!applyVolume) { |
| 3405 | continue; // next output |
| 3406 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3407 | } |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3408 | //FIXME: workaround for truncated touch sounds |
| 3409 | // delayed volume change for system stream to be removed when the problem is |
| 3410 | // handled by system UI |
| 3411 | status_t volStatus = checkAndSetVolume( |
| 3412 | curves, vs, index, desc, curDevices, |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3413 | ((vs == toVolumeSource(AUDIO_STREAM_SYSTEM, false))? |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3414 | TOUCH_SOUND_FIXED_DELAY_MS : 0)); |
| 3415 | if (volStatus != NO_ERROR) { |
| 3416 | status = volStatus; |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3417 | } |
| 3418 | } |
Eric Laurent | ae6e88c | 2024-01-10 14:42:57 +0100 | [diff] [blame] | 3419 | |
| 3420 | // update voice volume if the an active call route exists |
| 3421 | if (mCallRxSourceClient != nullptr && mCallRxSourceClient->isConnected() |
| 3422 | && (curSrcDevices.find( |
| 3423 | Volume::getDeviceForVolume({mCallRxSourceClient->sinkDevice()->type()})) |
| 3424 | != curSrcDevices.end())) { |
| 3425 | bool isVoiceVolSrc; |
| 3426 | bool isBtScoVolSrc; |
| 3427 | if (isVolumeConsistentForCalls(vs, {mCallRxSourceClient->sinkDevice()->type()}, |
| 3428 | isVoiceVolSrc, isBtScoVolSrc, __func__) |
| 3429 | && (isVoiceVolSrc || isBtScoVolSrc)) { |
| 3430 | setVoiceVolume(index, curves, isVoiceVolSrc, 0); |
| 3431 | } |
| 3432 | } |
| 3433 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3434 | mpClientInterface->onAudioVolumeGroupChanged(group, 0 /*flags*/); |
| 3435 | return status; |
| 3436 | } |
| 3437 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3438 | status_t AudioPolicyManager::setVolumeCurveIndex(int index, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3439 | audio_devices_t device, |
| 3440 | IVolumeCurves &volumeCurves) |
| 3441 | { |
| 3442 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 3443 | // app that has MODIFY_PHONE_STATE permission. |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3444 | bool hasVoice = hasVoiceStream(volumeCurves.getStreamTypes()); |
| 3445 | if (((index < volumeCurves.getVolumeIndexMin()) && !(hasVoice && index == 0)) || |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3446 | (index > volumeCurves.getVolumeIndexMax())) { |
| 3447 | ALOGD("%s: wrong index %d min=%d max=%d", __FUNCTION__, index, |
| 3448 | volumeCurves.getVolumeIndexMin(), volumeCurves.getVolumeIndexMax()); |
| 3449 | return BAD_VALUE; |
| 3450 | } |
| 3451 | if (!audio_is_output_device(device)) { |
| 3452 | return BAD_VALUE; |
| 3453 | } |
| 3454 | |
| 3455 | // Force max volume if stream cannot be muted |
| 3456 | if (!volumeCurves.canBeMuted()) index = volumeCurves.getVolumeIndexMax(); |
| 3457 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3458 | ALOGV("%s device %08x, index %d", __FUNCTION__ , device, index); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3459 | volumeCurves.addCurrentVolumeIndex(device, index); |
| 3460 | return NO_ERROR; |
| 3461 | } |
| 3462 | |
| 3463 | status_t AudioPolicyManager::getVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3464 | int &index, |
| 3465 | audio_devices_t device) |
| 3466 | { |
| 3467 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this |
| 3468 | // stream by the engine. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3469 | DeviceTypeSet deviceTypes = {device}; |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3470 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Mikhail Naganov | bb990f2 | 2022-06-15 00:46:43 +0000 | [diff] [blame] | 3471 | deviceTypes = mEngine->getOutputDevicesForAttributes( |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3472 | attr, nullptr, true /*fromCache*/).types(); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3473 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3474 | return getVolumeIndex(getVolumeCurves(attr), index, deviceTypes); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3475 | } |
| 3476 | |
| 3477 | status_t AudioPolicyManager::getVolumeIndex(const IVolumeCurves &curves, |
| 3478 | int &index, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3479 | const DeviceTypeSet& deviceTypes) const |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3480 | { |
Mikhail Naganov | bb990f2 | 2022-06-15 00:46:43 +0000 | [diff] [blame] | 3481 | if (!isSingleDeviceType(deviceTypes, audio_is_output_device)) { |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3482 | return BAD_VALUE; |
| 3483 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3484 | index = curves.getVolumeIndex(deviceTypes); |
| 3485 | 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] | 3486 | return NO_ERROR; |
| 3487 | } |
| 3488 | |
| 3489 | status_t AudioPolicyManager::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3490 | int &index) |
| 3491 | { |
| 3492 | index = getVolumeCurves(attr).getVolumeIndexMin(); |
| 3493 | return NO_ERROR; |
| 3494 | } |
| 3495 | |
| 3496 | status_t AudioPolicyManager::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3497 | int &index) |
| 3498 | { |
| 3499 | index = getVolumeCurves(attr).getVolumeIndexMax(); |
| 3500 | return NO_ERROR; |
| 3501 | } |
| 3502 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3503 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3504 | { |
| 3505 | // select one output among several suitable for global effects. |
| 3506 | // The priority is as follows: |
| 3507 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 3508 | // AudioFlinger will invalidate the track and the offloaded output |
| 3509 | // will be closed causing the effect to be moved to a PCM output. |
| 3510 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3511 | // 3: The primary output |
| 3512 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3513 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3514 | DeviceVector devices = mEngine->getOutputDevicesForAttributes( |
| 3515 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3516 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3517 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3518 | if (outputs.size() == 0) { |
| 3519 | return AUDIO_IO_HANDLE_NONE; |
| 3520 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3521 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3522 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3523 | bool activeOnly = true; |
| 3524 | |
| 3525 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 3526 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 3527 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 3528 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 3529 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3530 | for (audio_io_handle_t output : outputs) { |
| 3531 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 3532 | if (activeOnly && !desc->isActive(toVolumeSource(AUDIO_STREAM_MUSIC))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3533 | continue; |
| 3534 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3535 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 3536 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3537 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3538 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3539 | } |
| 3540 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3541 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3542 | } |
| 3543 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3544 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3545 | } |
| 3546 | } |
| 3547 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 3548 | output = outputOffloaded; |
| 3549 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 3550 | output = outputDeepBuffer; |
| 3551 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 3552 | output = outputPrimary; |
| 3553 | } else { |
| 3554 | output = outputs[0]; |
| 3555 | } |
| 3556 | activeOnly = false; |
| 3557 | } |
| 3558 | |
| 3559 | if (output != mMusicEffectOutput) { |
François Gaffie | 1b4753e | 2023-02-06 10:36:33 +0100 | [diff] [blame] | 3560 | mEffects.moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output, |
| 3561 | mpClientInterface); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3562 | mMusicEffectOutput = output; |
| 3563 | } |
| 3564 | |
| 3565 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3566 | return output; |
| 3567 | } |
| 3568 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3569 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 3570 | { |
| 3571 | return selectOutputForMusicEffects(); |
| 3572 | } |
| 3573 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3574 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3575 | audio_io_handle_t io, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 3576 | product_strategy_t strategy, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3577 | int session, |
| 3578 | int id) |
| 3579 | { |
Priyanka Advani | 0d2e51a | 2024-03-18 21:35:46 +0000 | [diff] [blame^] | 3580 | if (session != AUDIO_SESSION_DEVICE) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3581 | ssize_t index = mOutputs.indexOfKey(io); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3582 | if (index < 0) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3583 | index = mInputs.indexOfKey(io); |
| 3584 | if (index < 0) { |
| 3585 | ALOGW("registerEffect() unknown io %d", io); |
| 3586 | return INVALID_OPERATION; |
| 3587 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3588 | } |
| 3589 | } |
Eric Laurent | bf8f69f | 2022-03-25 17:48:38 +0100 | [diff] [blame] | 3590 | bool isMusicEffect = (session != AUDIO_SESSION_OUTPUT_STAGE) |
| 3591 | && ((strategy == streamToStrategy(AUDIO_STREAM_MUSIC) |
| 3592 | || strategy == PRODUCT_STRATEGY_NONE)); |
| 3593 | return mEffects.registerEffect(desc, io, session, id, isMusicEffect); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3594 | } |
| 3595 | |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 3596 | status_t AudioPolicyManager::unregisterEffect(int id) |
| 3597 | { |
| 3598 | if (mEffects.getEffect(id) == nullptr) { |
| 3599 | return INVALID_OPERATION; |
| 3600 | } |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 3601 | if (mEffects.isEffectEnabled(id)) { |
| 3602 | ALOGW("%s effect %d enabled", __FUNCTION__, id); |
| 3603 | setEffectEnabled(id, false); |
| 3604 | } |
| 3605 | return mEffects.unregisterEffect(id); |
| 3606 | } |
| 3607 | |
| 3608 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) |
| 3609 | { |
| 3610 | sp<EffectDescriptor> effect = mEffects.getEffect(id); |
| 3611 | if (effect == nullptr) { |
| 3612 | return INVALID_OPERATION; |
| 3613 | } |
| 3614 | |
| 3615 | status_t status = mEffects.setEffectEnabled(id, enabled); |
| 3616 | if (status == NO_ERROR) { |
| 3617 | mInputs.trackEffectEnabled(effect, enabled); |
| 3618 | } |
| 3619 | return status; |
| 3620 | } |
| 3621 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3622 | |
| 3623 | status_t AudioPolicyManager::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) |
| 3624 | { |
| 3625 | mEffects.moveEffects(ids, io); |
| 3626 | return NO_ERROR; |
| 3627 | } |
| 3628 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3629 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 3630 | { |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3631 | auto vs = toVolumeSource(stream, false); |
| 3632 | return vs != VOLUME_SOURCE_NONE ? mOutputs.isActive(vs, inPastMs) : false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3633 | } |
| 3634 | |
| 3635 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 3636 | { |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3637 | auto vs = toVolumeSource(stream, false); |
| 3638 | return vs != VOLUME_SOURCE_NONE ? mOutputs.isActiveRemotely(vs, inPastMs) : false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3639 | } |
| 3640 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3641 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3642 | { |
| 3643 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3644 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3645 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3646 | return true; |
| 3647 | } |
| 3648 | } |
| 3649 | return false; |
| 3650 | } |
| 3651 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3652 | // Register a list of custom mixes with their attributes and format. |
| 3653 | // When a mix is registered, corresponding input and output profiles are |
| 3654 | // added to the remote submix hw module. The profile contains only the |
| 3655 | // parameters (sampling rate, format...) specified by the mix. |
| 3656 | // The corresponding input remote submix device is also connected. |
| 3657 | // |
| 3658 | // When a remote submix device is connected, the address is checked to select the |
| 3659 | // appropriate profile and the corresponding input or output stream is opened. |
| 3660 | // |
| 3661 | // When capture starts, getInputForAttr() will: |
| 3662 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 3663 | // - 2 if none found, getDeviceForInputSource() will: |
| 3664 | // - 2.1 look for a mix matching the attributes source |
| 3665 | // - 2.2 if none found, default to device selection by policy rules |
| 3666 | // At this time, the corresponding output remote submix device is also connected |
| 3667 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 3668 | // after AudioTracks are invalidated |
| 3669 | // |
| 3670 | // When playback starts, getOutputForAttr() will: |
| 3671 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 3672 | // - 2 if none found, look for a mix matching the attributes usage |
| 3673 | // - 3 if none found, default to device and output selection by policy rules. |
| 3674 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3675 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3676 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3677 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 3678 | status_t res = NO_ERROR; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3679 | bool checkOutputs = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3680 | sp<HwModule> rSubmixModule; |
Marvin Ramin | abd9b89 | 2023-11-17 16:36:27 +0100 | [diff] [blame] | 3681 | Vector<AudioMix> registeredMixes; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3682 | // examine each mix's route type |
| 3683 | for (size_t i = 0; i < mixes.size(); i++) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3684 | AudioMix mix = mixes[i]; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3685 | // Only capture of playback is allowed in LOOP_BACK & RENDER mode |
| 3686 | if (is_mix_loopback_render(mix.mRouteFlags) && mix.mMixType != MIX_TYPE_PLAYERS) { |
| 3687 | ALOGE("Unsupported Policy Mix %zu of %zu: " |
| 3688 | "Only capture of playback is allowed in LOOP_BACK & RENDER mode", |
| 3689 | i, mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3690 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3691 | break; |
| 3692 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3693 | // LOOP_BACK and LOOP_BACK | RENDER have the same remote submix backend and are handled |
| 3694 | // in the same way. |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3695 | 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] | 3696 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK %d", i, mixes.size(), |
| 3697 | mix.mRouteFlags); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3698 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3699 | rSubmixModule = mHwModules.getModuleFromName( |
| 3700 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 3701 | if (rSubmixModule == 0) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3702 | ALOGE("Unable to find audio module for submix, aborting mix %zu registration", |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3703 | i); |
| 3704 | res = INVALID_OPERATION; |
| 3705 | break; |
| 3706 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3707 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3708 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3709 | String8 address = mix.mDeviceAddress; |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3710 | audio_devices_t deviceTypeToMakeAvailable; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3711 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3712 | mix.mDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3713 | deviceTypeToMakeAvailable = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 3714 | } else { |
| 3715 | mix.mDeviceType = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 3716 | deviceTypeToMakeAvailable = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3717 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3718 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3719 | if (mPolicyMixes.registerMix(mix, 0 /*output desc*/) != NO_ERROR) { |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 3720 | ALOGE("Error registering mix %zu for address %s", i, address.c_str()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3721 | res = INVALID_OPERATION; |
| 3722 | break; |
| 3723 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3724 | audio_config_t outputConfig = mix.mFormat; |
| 3725 | audio_config_t inputConfig = mix.mFormat; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3726 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL |
| 3727 | // 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] | 3728 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 3729 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3730 | rSubmixModule->addOutputProfile(address.c_str(), &outputConfig, |
Dean Wheatley | 8055186 | 2023-11-17 01:32:22 +1100 | [diff] [blame] | 3731 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address, |
| 3732 | audio_is_linear_pcm(outputConfig.format) |
| 3733 | ? AUDIO_OUTPUT_FLAG_NONE : AUDIO_OUTPUT_FLAG_DIRECT); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3734 | rSubmixModule->addInputProfile(address.c_str(), &inputConfig, |
Dean Wheatley | 8055186 | 2023-11-17 01:32:22 +1100 | [diff] [blame] | 3735 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address, |
| 3736 | audio_is_linear_pcm(inputConfig.format) |
| 3737 | ? AUDIO_INPUT_FLAG_NONE : AUDIO_INPUT_FLAG_DIRECT); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3738 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3739 | if ((res = setDeviceConnectionStateInt(deviceTypeToMakeAvailable, |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 3740 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 3741 | address.c_str(), "remote-submix", AUDIO_FORMAT_DEFAULT)) != NO_ERROR) { |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 3742 | ALOGE("Failed to set remote submix device available, type %u, address %s", |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 3743 | mix.mDeviceType, address.c_str()); |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 3744 | break; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3745 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3746 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 3747 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3748 | audio_devices_t type = mix.mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3749 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 3750 | i, mixes.size(), type, address.c_str()); |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3751 | |
| 3752 | sp<DeviceDescriptor> device = mHwModules.getDeviceDescriptor( |
| 3753 | mix.mDeviceType, mix.mDeviceAddress, |
| 3754 | String8(), AUDIO_FORMAT_DEFAULT); |
| 3755 | if (device == nullptr) { |
| 3756 | res = INVALID_OPERATION; |
| 3757 | break; |
| 3758 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3759 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3760 | bool foundOutput = false; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3761 | // First try to find an already opened output supporting the device |
| 3762 | 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] | 3763 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3764 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3765 | if (!desc->isDuplicated() && desc->supportedDevices().contains(device)) { |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3766 | if (mPolicyMixes.registerMix(mix, desc) != NO_ERROR) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3767 | ALOGE("Could not register mix RENDER, dev=0x%X addr=%s", type, |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 3768 | address.c_str()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3769 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3770 | } else { |
| 3771 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3772 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3773 | } |
| 3774 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3775 | // If no output found, try to find a direct output profile supporting the device |
| 3776 | for (size_t i = 0; i < mHwModules.size() && !foundOutput && res == NO_ERROR; i++) { |
| 3777 | sp<HwModule> module = mHwModules[i]; |
| 3778 | for (size_t j = 0; |
| 3779 | j < module->getOutputProfiles().size() && !foundOutput && res == NO_ERROR; |
| 3780 | j++) { |
| 3781 | sp<IOProfile> profile = module->getOutputProfiles()[j]; |
| 3782 | if (profile->isDirectOutput() && profile->supportsDevice(device)) { |
| 3783 | if (mPolicyMixes.registerMix(mix, nullptr) != NO_ERROR) { |
| 3784 | ALOGE("Could not register mix RENDER, dev=0x%X addr=%s", type, |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 3785 | address.c_str()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3786 | res = INVALID_OPERATION; |
| 3787 | } else { |
| 3788 | foundOutput = true; |
| 3789 | } |
| 3790 | } |
| 3791 | } |
| 3792 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3793 | if (res != NO_ERROR) { |
| 3794 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 3795 | i, type, address.c_str()); |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3796 | res = INVALID_OPERATION; |
| 3797 | break; |
| 3798 | } else if (!foundOutput) { |
| 3799 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 3800 | i, type, address.c_str()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3801 | res = INVALID_OPERATION; |
| 3802 | break; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3803 | } else { |
| 3804 | checkOutputs = true; |
Marvin Ramin | abd9b89 | 2023-11-17 16:36:27 +0100 | [diff] [blame] | 3805 | registeredMixes.add(mix); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3806 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3807 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3808 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3809 | if (res != NO_ERROR) { |
Marvin Ramin | abd9b89 | 2023-11-17 16:36:27 +0100 | [diff] [blame] | 3810 | if (audio_flags::audio_mix_ownership()) { |
| 3811 | // Only unregister mixes that were actually registered to not accidentally unregister |
| 3812 | // mixes that already existed previously. |
| 3813 | unregisterPolicyMixes(registeredMixes); |
| 3814 | registeredMixes.clear(); |
| 3815 | } else { |
| 3816 | unregisterPolicyMixes(mixes); |
| 3817 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3818 | } else if (checkOutputs) { |
| 3819 | checkForDeviceAndOutputChanges(); |
| 3820 | updateCallAndOutputRouting(); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3821 | } |
| 3822 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3823 | } |
| 3824 | |
| 3825 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 3826 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3827 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3828 | status_t res = NO_ERROR; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3829 | bool checkOutputs = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3830 | sp<HwModule> rSubmixModule; |
| 3831 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3832 | for (const auto& mix : mixes) { |
| 3833 | 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] | 3834 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3835 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3836 | rSubmixModule = mHwModules.getModuleFromName( |
| 3837 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 3838 | if (rSubmixModule == 0) { |
| 3839 | res = INVALID_OPERATION; |
| 3840 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3841 | } |
| 3842 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3843 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3844 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3845 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3846 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3847 | res = INVALID_OPERATION; |
| 3848 | continue; |
| 3849 | } |
| 3850 | |
Marvin Ramin | 0783e20 | 2024-03-05 12:45:50 +0100 | [diff] [blame] | 3851 | for (auto device: {AUDIO_DEVICE_IN_REMOTE_SUBMIX, AUDIO_DEVICE_OUT_REMOTE_SUBMIX}) { |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 3852 | if (getDeviceConnectionState(device, address.c_str()) == |
Marvin Ramin | 0783e20 | 2024-03-05 12:45:50 +0100 | [diff] [blame] | 3853 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 3854 | status_t currentRes = |
| 3855 | setDeviceConnectionStateInt(device, |
| 3856 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3857 | address.c_str(), |
| 3858 | "remote-submix", |
| 3859 | AUDIO_FORMAT_DEFAULT); |
| 3860 | if (!audio_flags::audio_mix_ownership()) { |
| 3861 | res = currentRes; |
| 3862 | } |
| 3863 | if (currentRes != OK) { |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 3864 | ALOGE("Error making RemoteSubmix device unavailable for mix " |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 3865 | "with type %d, address %s", device, address.c_str()); |
Marvin Ramin | 0783e20 | 2024-03-05 12:45:50 +0100 | [diff] [blame] | 3866 | res = INVALID_OPERATION; |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 3867 | } |
| 3868 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3869 | } |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3870 | rSubmixModule->removeOutputProfile(address.c_str()); |
| 3871 | rSubmixModule->removeInputProfile(address.c_str()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3872 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3873 | } 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] | 3874 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3875 | res = INVALID_OPERATION; |
| 3876 | continue; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3877 | } else { |
| 3878 | checkOutputs = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3879 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3880 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3881 | } |
Marvin Ramin | 0783e20 | 2024-03-05 12:45:50 +0100 | [diff] [blame] | 3882 | |
| 3883 | if (res == NO_ERROR && checkOutputs) { |
| 3884 | checkForDeviceAndOutputChanges(); |
| 3885 | updateCallAndOutputRouting(); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3886 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3887 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3888 | } |
| 3889 | |
Marvin Ramin | bdefaf0 | 2023-11-01 09:10:32 +0100 | [diff] [blame] | 3890 | status_t AudioPolicyManager::getRegisteredPolicyMixes(std::vector<AudioMix>& _aidl_return) { |
| 3891 | if (!audio_flags::audio_mix_test_api()) { |
| 3892 | return INVALID_OPERATION; |
| 3893 | } |
| 3894 | |
| 3895 | _aidl_return.clear(); |
| 3896 | _aidl_return.reserve(mPolicyMixes.size()); |
| 3897 | for (const auto &policyMix: mPolicyMixes) { |
| 3898 | _aidl_return.emplace_back(policyMix->mCriteria, policyMix->mMixType, |
| 3899 | policyMix->mFormat, policyMix->mRouteFlags, policyMix->mDeviceAddress, |
| 3900 | policyMix->mCbFlags); |
| 3901 | _aidl_return.back().mDeviceType = policyMix->mDeviceType; |
Marvin Ramin | abd9b89 | 2023-11-17 16:36:27 +0100 | [diff] [blame] | 3902 | _aidl_return.back().mToken = policyMix->mToken; |
Marvin Ramin | bdefaf0 | 2023-11-01 09:10:32 +0100 | [diff] [blame] | 3903 | } |
| 3904 | |
Vlad Popa | a5d73f3 | 2024-03-08 16:05:38 -0800 | [diff] [blame] | 3905 | ALOGVV("%s() returning %zu registered mixes", __func__, _aidl_return.size()); |
Marvin Ramin | bdefaf0 | 2023-11-01 09:10:32 +0100 | [diff] [blame] | 3906 | return OK; |
| 3907 | } |
| 3908 | |
Jan Sebechlebsky | 0af8e87 | 2023-08-11 14:45:08 +0200 | [diff] [blame] | 3909 | status_t AudioPolicyManager::updatePolicyMix( |
| 3910 | const AudioMix& mix, |
| 3911 | const std::vector<AudioMixMatchCriterion>& updatedCriteria) { |
| 3912 | status_t res = mPolicyMixes.updateMix(mix, updatedCriteria); |
| 3913 | if (res == NO_ERROR) { |
| 3914 | checkForDeviceAndOutputChanges(); |
| 3915 | updateCallAndOutputRouting(); |
| 3916 | } |
| 3917 | return res; |
| 3918 | } |
| 3919 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3920 | void AudioPolicyManager::dumpManualSurroundFormats(String8 *dst) const |
| 3921 | { |
| 3922 | size_t i = 0; |
| 3923 | constexpr size_t audioFormatPrefixLen = sizeof("AUDIO_FORMAT_"); |
| 3924 | for (const auto& fmt : mManualSurroundFormats) { |
| 3925 | if (i++ != 0) dst->append(", "); |
| 3926 | std::string sfmt; |
| 3927 | FormatConverter::toString(fmt, sfmt); |
| 3928 | dst->append(sfmt.size() >= audioFormatPrefixLen ? |
| 3929 | sfmt.c_str() + audioFormatPrefixLen - 1 : sfmt.c_str()); |
| 3930 | } |
| 3931 | } |
| 3932 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3933 | // Returns true if all devices types match the predicate and are supported by one HW module |
| 3934 | bool AudioPolicyManager::areAllDevicesSupported( |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3935 | const AudioDeviceTypeAddrVector& devices, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3936 | std::function<bool(audio_devices_t)> predicate, |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 3937 | const char *context, |
| 3938 | bool matchAddress) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3939 | for (size_t i = 0; i < devices.size(); i++) { |
| 3940 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor( |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3941 | devices[i].mType, devices[i].getAddress(), String8(), |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 3942 | AUDIO_FORMAT_DEFAULT, false /*allowToCreate*/, matchAddress); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3943 | if (devDesc == nullptr || (predicate != nullptr && !predicate(devices[i].mType))) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3944 | ALOGE("%s: device type %#x address %s not supported or not match predicate", |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3945 | context, devices[i].mType, devices[i].getAddress()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3946 | return false; |
| 3947 | } |
| 3948 | } |
| 3949 | return true; |
| 3950 | } |
| 3951 | |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 3952 | void AudioPolicyManager::changeOutputDevicesMuteState( |
| 3953 | const AudioDeviceTypeAddrVector& devices) { |
| 3954 | ALOGVV("%s() num devices %zu", __func__, devices.size()); |
| 3955 | |
| 3956 | std::vector<sp<SwAudioOutputDescriptor>> outputs = |
| 3957 | getSoftwareOutputsForDevices(devices); |
| 3958 | |
| 3959 | for (size_t i = 0; i < outputs.size(); i++) { |
| 3960 | sp<SwAudioOutputDescriptor> outputDesc = outputs[i]; |
| 3961 | DeviceVector prevDevices = outputDesc->devices(); |
| 3962 | checkDeviceMuteStrategies(outputDesc, prevDevices, 0 /* delayMs */); |
| 3963 | } |
| 3964 | } |
| 3965 | |
| 3966 | std::vector<sp<SwAudioOutputDescriptor>> AudioPolicyManager::getSoftwareOutputsForDevices( |
| 3967 | const AudioDeviceTypeAddrVector& devices) const |
| 3968 | { |
| 3969 | std::vector<sp<SwAudioOutputDescriptor>> outputs; |
| 3970 | DeviceVector deviceDescriptors; |
| 3971 | for (size_t j = 0; j < devices.size(); j++) { |
| 3972 | sp<DeviceDescriptor> desc = mHwModules.getDeviceDescriptor( |
| 3973 | devices[j].mType, devices[j].getAddress(), String8(), AUDIO_FORMAT_DEFAULT); |
| 3974 | if (desc == nullptr || !audio_is_output_device(devices[j].mType)) { |
| 3975 | ALOGE("%s: device type %#x address %s not supported or not an output device", |
| 3976 | __func__, devices[j].mType, devices[j].getAddress()); |
| 3977 | continue; |
| 3978 | } |
| 3979 | deviceDescriptors.add(desc); |
| 3980 | } |
| 3981 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3982 | if (!mOutputs.valueAt(i)->supportsAtLeastOne(deviceDescriptors)) { |
| 3983 | continue; |
| 3984 | } |
| 3985 | outputs.push_back(mOutputs.valueAt(i)); |
| 3986 | } |
| 3987 | return outputs; |
| 3988 | } |
| 3989 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3990 | status_t AudioPolicyManager::setUidDeviceAffinities(uid_t uid, |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3991 | const AudioDeviceTypeAddrVector& devices) { |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3992 | ALOGV("%s() uid=%d num devices %zu", __FUNCTION__, uid, devices.size()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3993 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
| 3994 | return BAD_VALUE; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3995 | } |
| 3996 | status_t res = mPolicyMixes.setUidDeviceAffinities(uid, devices); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3997 | if (res != NO_ERROR) { |
| 3998 | ALOGE("%s() Could not set all device affinities for uid = %d", __FUNCTION__, uid); |
| 3999 | return res; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 4000 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4001 | |
| 4002 | checkForDeviceAndOutputChanges(); |
| 4003 | updateCallAndOutputRouting(); |
| 4004 | |
| 4005 | return NO_ERROR; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 4006 | } |
| 4007 | |
| 4008 | status_t AudioPolicyManager::removeUidDeviceAffinities(uid_t uid) { |
| 4009 | ALOGV("%s() uid=%d", __FUNCTION__, uid); |
Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 4010 | status_t res = mPolicyMixes.removeUidDeviceAffinities(uid); |
| 4011 | if (res != NO_ERROR) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4012 | ALOGE("%s() Could not remove all device affinities for uid = %d", |
Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 4013 | __FUNCTION__, uid); |
| 4014 | return INVALID_OPERATION; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 4015 | } |
| 4016 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4017 | checkForDeviceAndOutputChanges(); |
| 4018 | updateCallAndOutputRouting(); |
| 4019 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 4020 | return res; |
| 4021 | } |
| 4022 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4023 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 4024 | status_t AudioPolicyManager::setDevicesRoleForStrategy(product_strategy_t strategy, |
| 4025 | device_role_t role, |
| 4026 | const AudioDeviceTypeAddrVector &devices) { |
| 4027 | ALOGV("%s() strategy=%d role=%d %s", __func__, strategy, role, |
| 4028 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4029 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4030 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4031 | return BAD_VALUE; |
| 4032 | } |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 4033 | status_t status = mEngine->setDevicesRoleForStrategy(strategy, role, devices); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4034 | if (status != NO_ERROR) { |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 4035 | ALOGW("Engine could not set preferred devices %s for strategy %d role %d", |
| 4036 | dumpAudioDeviceTypeAddrVector(devices).c_str(), strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4037 | return status; |
| 4038 | } |
| 4039 | |
| 4040 | checkForDeviceAndOutputChanges(); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4041 | |
| 4042 | bool forceVolumeReeval = false; |
| 4043 | // FIXME: workaround for truncated touch sounds |
| 4044 | // to be removed when the problem is handled by system UI |
| 4045 | uint32_t delayMs = 0; |
| 4046 | if (strategy == mCommunnicationStrategy) { |
| 4047 | forceVolumeReeval = true; |
| 4048 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 4049 | updateInputRouting(); |
| 4050 | } |
| 4051 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4052 | |
| 4053 | return NO_ERROR; |
| 4054 | } |
| 4055 | |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 4056 | void AudioPolicyManager::updateCallAndOutputRouting(bool forceVolumeReeval, uint32_t delayMs, |
| 4057 | bool skipDelays) |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4058 | { |
| 4059 | uint32_t waitMs = 0; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 4060 | bool wasLeUnicastActive = isLeUnicastActive(); |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 4061 | if (updateCallRouting(true /*fromCache*/, delayMs, &waitMs) == NO_ERROR) { |
Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 4062 | // Only apply special touch sound delay once |
| 4063 | delayMs = 0; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4064 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 4065 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4066 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4067 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 4068 | DeviceVector newDevices = getNewOutputDevices(outputDesc, true /*fromCache*/); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 4069 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || |
| 4070 | (outputDesc != mPrimaryOutput && !isTelephonyRxOrTx(outputDesc))) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4071 | // As done in setDeviceConnectionState, we could also fix default device issue by |
| 4072 | // preventing the force re-routing in case of default dev that distinguishes on address. |
| 4073 | // Let's give back to engine full device choice decision however. |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 4074 | bool forceRouting = !newDevices.isEmpty(); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 4075 | if (outputDesc->mUsePreferredMixerAttributes && newDevices != outputDesc->devices()) { |
| 4076 | // If the device is using preferred mixer attributes, the output need to reopen |
| 4077 | // with default configuration when the new selected devices are different from |
| 4078 | // current routing devices. |
| 4079 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices); |
| 4080 | continue; |
| 4081 | } |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 4082 | |
| 4083 | waitMs = setOutputDevices(__func__, outputDesc, newDevices, forceRouting, delayMs, |
| 4084 | nullptr, !skipDelays /*requiresMuteCheck*/, |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 4085 | !forceRouting /*requiresVolumeCheck*/, skipDelays); |
Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 4086 | // Only apply special touch sound delay once |
| 4087 | delayMs = 0; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4088 | } |
| 4089 | if (forceVolumeReeval && !newDevices.isEmpty()) { |
| 4090 | applyStreamVolumes(outputDesc, newDevices.types(), waitMs, true); |
| 4091 | } |
| 4092 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 4093 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 4094 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4095 | } |
| 4096 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4097 | void AudioPolicyManager::updateInputRouting() { |
| 4098 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Jaideep Sharma | 408349a | 2020-11-27 14:47:17 +0530 | [diff] [blame] | 4099 | // Skip for hotword recording as the input device switch |
| 4100 | // is handled within sound trigger HAL |
| 4101 | if (activeDesc->isSoundTrigger() && activeDesc->source() == AUDIO_SOURCE_HOTWORD) { |
| 4102 | continue; |
| 4103 | } |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4104 | auto newDevice = getNewInputDevice(activeDesc); |
| 4105 | // Force new input selection if the new device can not be reached via current input |
| 4106 | if (activeDesc->mProfile->getSupportedDevices().contains(newDevice)) { |
| 4107 | setInputDevice(activeDesc->mIoHandle, newDevice); |
| 4108 | } else { |
| 4109 | closeInput(activeDesc->mIoHandle); |
| 4110 | } |
| 4111 | } |
| 4112 | } |
| 4113 | |
Paul Wang | 5d7cdb5 | 2022-11-22 09:45:06 +0000 | [diff] [blame] | 4114 | status_t |
| 4115 | AudioPolicyManager::removeDevicesRoleForStrategy(product_strategy_t strategy, |
| 4116 | device_role_t role, |
| 4117 | const AudioDeviceTypeAddrVector &devices) { |
| 4118 | ALOGV("%s() strategy=%d role=%d %s", __func__, strategy, role, |
| 4119 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 4120 | |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 4121 | if (!areAllDevicesSupported( |
| 4122 | devices, audio_is_output_device, __func__, /*matchAddress*/false)) { |
Paul Wang | 5d7cdb5 | 2022-11-22 09:45:06 +0000 | [diff] [blame] | 4123 | return BAD_VALUE; |
| 4124 | } |
| 4125 | status_t status = mEngine->removeDevicesRoleForStrategy(strategy, role, devices); |
| 4126 | if (status != NO_ERROR) { |
| 4127 | ALOGW("Engine could not remove devices %s for strategy %d role %d", |
| 4128 | dumpAudioDeviceTypeAddrVector(devices).c_str(), strategy, role); |
| 4129 | return status; |
| 4130 | } |
| 4131 | |
| 4132 | checkForDeviceAndOutputChanges(); |
| 4133 | |
| 4134 | bool forceVolumeReeval = false; |
| 4135 | // TODO(b/263479999): workaround for truncated touch sounds |
| 4136 | // to be removed when the problem is handled by system UI |
| 4137 | uint32_t delayMs = 0; |
| 4138 | if (strategy == mCommunnicationStrategy) { |
| 4139 | forceVolumeReeval = true; |
| 4140 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 4141 | updateInputRouting(); |
| 4142 | } |
| 4143 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
| 4144 | |
| 4145 | return NO_ERROR; |
| 4146 | } |
| 4147 | |
| 4148 | status_t AudioPolicyManager::clearDevicesRoleForStrategy(product_strategy_t strategy, |
| 4149 | device_role_t role) |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4150 | { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 4151 | ALOGV("%s() strategy=%d role=%d", __func__, strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4152 | |
Paul Wang | 5d7cdb5 | 2022-11-22 09:45:06 +0000 | [diff] [blame] | 4153 | status_t status = mEngine->clearDevicesRoleForStrategy(strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4154 | if (status != NO_ERROR) { |
Eric Laurent | 9ae44f3 | 2022-12-14 16:17:02 +0100 | [diff] [blame] | 4155 | ALOGW_IF(status != NAME_NOT_FOUND, |
| 4156 | "Engine could not remove device role for strategy %d status %d", |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4157 | strategy, status); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4158 | return status; |
| 4159 | } |
| 4160 | |
| 4161 | checkForDeviceAndOutputChanges(); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4162 | |
| 4163 | bool forceVolumeReeval = false; |
| 4164 | // FIXME: workaround for truncated touch sounds |
| 4165 | // to be removed when the problem is handled by system UI |
| 4166 | uint32_t delayMs = 0; |
| 4167 | if (strategy == mCommunnicationStrategy) { |
| 4168 | forceVolumeReeval = true; |
| 4169 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 4170 | updateInputRouting(); |
| 4171 | } |
| 4172 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4173 | |
| 4174 | return NO_ERROR; |
| 4175 | } |
| 4176 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 4177 | status_t AudioPolicyManager::getDevicesForRoleAndStrategy(product_strategy_t strategy, |
| 4178 | device_role_t role, |
| 4179 | AudioDeviceTypeAddrVector &devices) { |
| 4180 | return mEngine->getDevicesForRoleAndStrategy(strategy, role, devices); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4181 | } |
| 4182 | |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4183 | status_t AudioPolicyManager::setDevicesRoleForCapturePreset( |
| 4184 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { |
| 4185 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, |
| 4186 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 4187 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 4188 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4189 | return BAD_VALUE; |
| 4190 | } |
| 4191 | status_t status = mEngine->setDevicesRoleForCapturePreset(audioSource, role, devices); |
| 4192 | ALOGW_IF(status != NO_ERROR, |
| 4193 | "Engine could not set preferred devices %s for audio source %d role %d", |
| 4194 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); |
| 4195 | |
| 4196 | return status; |
| 4197 | } |
| 4198 | |
| 4199 | status_t AudioPolicyManager::addDevicesRoleForCapturePreset( |
| 4200 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { |
| 4201 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, |
| 4202 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 4203 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 4204 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4205 | return BAD_VALUE; |
| 4206 | } |
| 4207 | status_t status = mEngine->addDevicesRoleForCapturePreset(audioSource, role, devices); |
| 4208 | ALOGW_IF(status != NO_ERROR, |
| 4209 | "Engine could not add preferred devices %s for audio source %d role %d", |
| 4210 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); |
| 4211 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4212 | updateInputRouting(); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4213 | return status; |
| 4214 | } |
| 4215 | |
| 4216 | status_t AudioPolicyManager::removeDevicesRoleForCapturePreset( |
| 4217 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) |
| 4218 | { |
| 4219 | ALOGV("%s() audioSource=%d role=%d devices=%s", __func__, audioSource, role, |
| 4220 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 4221 | |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 4222 | if (!areAllDevicesSupported( |
| 4223 | devices, audio_call_is_input_device, __func__, /*matchAddress*/false)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4224 | return BAD_VALUE; |
| 4225 | } |
| 4226 | |
| 4227 | status_t status = mEngine->removeDevicesRoleForCapturePreset( |
| 4228 | audioSource, role, devices); |
Eric Laurent | 9ae44f3 | 2022-12-14 16:17:02 +0100 | [diff] [blame] | 4229 | ALOGW_IF(status != NO_ERROR && status != NAME_NOT_FOUND, |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4230 | "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] | 4231 | if (status == NO_ERROR) { |
| 4232 | updateInputRouting(); |
| 4233 | } |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4234 | return status; |
| 4235 | } |
| 4236 | |
| 4237 | status_t AudioPolicyManager::clearDevicesRoleForCapturePreset(audio_source_t audioSource, |
| 4238 | device_role_t role) { |
| 4239 | ALOGV("%s() audioSource=%d role=%d", __func__, audioSource, role); |
| 4240 | |
| 4241 | status_t status = mEngine->clearDevicesRoleForCapturePreset(audioSource, role); |
Eric Laurent | 9ae44f3 | 2022-12-14 16:17:02 +0100 | [diff] [blame] | 4242 | ALOGW_IF(status != NO_ERROR && status != NAME_NOT_FOUND, |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4243 | "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] | 4244 | if (status == NO_ERROR) { |
| 4245 | updateInputRouting(); |
| 4246 | } |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4247 | return status; |
| 4248 | } |
| 4249 | |
| 4250 | status_t AudioPolicyManager::getDevicesForRoleAndCapturePreset( |
| 4251 | audio_source_t audioSource, device_role_t role, AudioDeviceTypeAddrVector &devices) { |
| 4252 | return mEngine->getDevicesForRoleAndCapturePreset(audioSource, role, devices); |
| 4253 | } |
| 4254 | |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4255 | status_t AudioPolicyManager::setUserIdDeviceAffinities(int userId, |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 4256 | const AudioDeviceTypeAddrVector& devices) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 4257 | ALOGV("%s() userId=%d num devices %zu", __func__, userId, devices.size()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4258 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
| 4259 | return BAD_VALUE; |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4260 | } |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4261 | status_t status = mPolicyMixes.setUserIdDeviceAffinities(userId, devices); |
| 4262 | if (status != NO_ERROR) { |
| 4263 | ALOGE("%s() could not set device affinity for userId %d", |
| 4264 | __FUNCTION__, userId); |
| 4265 | return status; |
| 4266 | } |
| 4267 | |
| 4268 | // reevaluate outputs for all devices |
| 4269 | checkForDeviceAndOutputChanges(); |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 4270 | changeOutputDevicesMuteState(devices); |
| 4271 | updateCallAndOutputRouting(false /* forceVolumeReeval */, 0 /* delayMs */, |
| 4272 | true /* skipDelays */); |
| 4273 | changeOutputDevicesMuteState(devices); |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4274 | |
| 4275 | return NO_ERROR; |
| 4276 | } |
| 4277 | |
| 4278 | status_t AudioPolicyManager::removeUserIdDeviceAffinities(int userId) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 4279 | ALOGV("%s() userId=%d", __FUNCTION__, userId); |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 4280 | AudioDeviceTypeAddrVector devices; |
| 4281 | mPolicyMixes.getDevicesForUserId(userId, devices); |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4282 | status_t status = mPolicyMixes.removeUserIdDeviceAffinities(userId); |
| 4283 | if (status != NO_ERROR) { |
| 4284 | ALOGE("%s() Could not remove all device affinities fo userId = %d", |
| 4285 | __FUNCTION__, userId); |
| 4286 | return status; |
| 4287 | } |
| 4288 | |
| 4289 | // reevaluate outputs for all devices |
| 4290 | checkForDeviceAndOutputChanges(); |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 4291 | changeOutputDevicesMuteState(devices); |
| 4292 | updateCallAndOutputRouting(false /* forceVolumeReeval */, 0 /* delayMs */, |
| 4293 | true /* skipDelays */); |
| 4294 | changeOutputDevicesMuteState(devices); |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4295 | |
| 4296 | return NO_ERROR; |
| 4297 | } |
| 4298 | |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4299 | void AudioPolicyManager::dump(String8 *dst) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4300 | { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4301 | dst->appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
Mikhail Naganov | 0dbe87b | 2021-12-01 02:03:31 +0000 | [diff] [blame] | 4302 | dst->appendFormat(" Primary Output I/O handle: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4303 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 4304 | std::string stateLiteral; |
| 4305 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4306 | dst->appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 4307 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 4308 | "communications", "media", "record", "dock", "system", |
| 4309 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 4310 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 4311 | 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] | 4312 | audio_policy_forced_cfg_t forceUseValue = mEngine->getForceUse(i); |
| 4313 | dst->appendFormat(" Force use for %s: %d", forceUses[i], forceUseValue); |
| 4314 | if (i == AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND && |
| 4315 | forceUseValue == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 4316 | dst->append(" (MANUAL: "); |
| 4317 | dumpManualSurroundFormats(dst); |
| 4318 | dst->append(")"); |
| 4319 | } |
| 4320 | dst->append("\n"); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 4321 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4322 | dst->appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 4323 | dst->appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
Mikhail Naganov | b3bcb4f | 2022-02-23 23:46:56 +0000 | [diff] [blame] | 4324 | dst->appendFormat(" Communication Strategy id: %d\n", mCommunnicationStrategy); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 4325 | dst->appendFormat(" Config source: %s\n", mConfig->getSource().c_str()); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4326 | |
Mikhail Naganov | 0f413b2 | 2021-12-02 05:29:27 +0000 | [diff] [blame] | 4327 | dst->append("\n"); |
| 4328 | mAvailableOutputDevices.dump(dst, String8("Available output"), 1); |
| 4329 | dst->append("\n"); |
| 4330 | mAvailableInputDevices.dump(dst, String8("Available input"), 1); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 4331 | mHwModules.dump(dst); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4332 | mOutputs.dump(dst); |
| 4333 | mInputs.dump(dst); |
Mikhail Naganov | 0f413b2 | 2021-12-02 05:29:27 +0000 | [diff] [blame] | 4334 | mEffects.dump(dst, 1); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4335 | mAudioPatches.dump(dst); |
| 4336 | mPolicyMixes.dump(dst); |
| 4337 | mAudioSources.dump(dst); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4338 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 4339 | dst->appendFormat(" AllowedCapturePolicies:\n"); |
| 4340 | for (auto& policy : mAllowedCapturePolicies) { |
| 4341 | dst->appendFormat(" - uid=%d flag_mask=%#x\n", policy.first, policy.second); |
| 4342 | } |
| 4343 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4344 | dst->appendFormat(" Preferred mixer audio configuration:\n"); |
| 4345 | for (const auto it : mPreferredMixerAttrInfos) { |
| 4346 | dst->appendFormat(" - device port id: %d\n", it.first); |
| 4347 | for (const auto preferredMixerInfoIt : it.second) { |
| 4348 | dst->appendFormat(" - strategy: %d; ", preferredMixerInfoIt.first); |
| 4349 | preferredMixerInfoIt.second->dump(dst); |
| 4350 | } |
| 4351 | } |
| 4352 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4353 | dst->appendFormat("\nPolicy Engine dump:\n"); |
| 4354 | mEngine->dump(dst); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4355 | } |
| 4356 | |
| 4357 | status_t AudioPolicyManager::dump(int fd) |
| 4358 | { |
| 4359 | String8 result; |
| 4360 | dump(&result); |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 4361 | write(fd, result.c_str(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4362 | return NO_ERROR; |
| 4363 | } |
| 4364 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 4365 | status_t AudioPolicyManager::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) |
| 4366 | { |
| 4367 | mAllowedCapturePolicies[uid] = capturePolicy; |
| 4368 | return NO_ERROR; |
| 4369 | } |
| 4370 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4371 | // This function checks for the parameters which can be offloaded. |
| 4372 | // This can be enhanced depending on the capability of the DSP and policy |
| 4373 | // of the system. |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4374 | audio_offload_mode_t AudioPolicyManager::getOffloadSupport(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4375 | { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4376 | ALOGV("%s: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4377 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4378 | __func__, offloadInfo.sample_rate, offloadInfo.channel_mask, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4379 | offloadInfo.format, |
| 4380 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 4381 | offloadInfo.has_video); |
| 4382 | |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4383 | if (!isOffloadPossible(offloadInfo)) { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4384 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4385 | } |
| 4386 | |
| 4387 | // See if there is a profile to support this. |
| 4388 | // AUDIO_DEVICE_NONE |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4389 | sp<IOProfile> profile = getProfileForOutput(DeviceVector() /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4390 | offloadInfo.sample_rate, |
| 4391 | offloadInfo.format, |
| 4392 | offloadInfo.channel_mask, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4393 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, |
| 4394 | true /* directOnly */); |
Eric Laurent | 9436544 | 2021-01-08 18:36:05 +0100 | [diff] [blame] | 4395 | ALOGV("%s: profile %sfound%s", __func__, profile != nullptr ? "" : "NOT ", |
| 4396 | (profile != nullptr && (profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) |
| 4397 | ? ", supports gapless" : ""); |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4398 | if (profile == nullptr) { |
| 4399 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
| 4400 | } |
| 4401 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) { |
| 4402 | return AUDIO_OFFLOAD_GAPLESS_SUPPORTED; |
| 4403 | } |
| 4404 | return AUDIO_OFFLOAD_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4405 | } |
| 4406 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4407 | bool AudioPolicyManager::isDirectOutputSupported(const audio_config_base_t& config, |
| 4408 | const audio_attributes_t& attributes) { |
| 4409 | audio_output_flags_t output_flags = AUDIO_OUTPUT_FLAG_NONE; |
François Gaffie | 58d4be5 | 2018-11-06 15:30:12 +0100 | [diff] [blame] | 4410 | audio_flags_to_audio_output_flags(attributes.flags, &output_flags); |
Dorin Drimus | 9901eb0 | 2022-01-14 11:36:51 +0000 | [diff] [blame] | 4411 | DeviceVector outputDevices = mEngine->getOutputDevicesForAttributes(attributes); |
| 4412 | sp<IOProfile> profile = getProfileForOutput(outputDevices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4413 | config.sample_rate, |
| 4414 | config.format, |
| 4415 | config.channel_mask, |
| 4416 | output_flags, |
| 4417 | true /* directOnly */); |
| 4418 | ALOGV("%s() profile %sfound with name: %s, " |
| 4419 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 4420 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 4421 | (profile != 0 ? profile->getTagName().c_str() : "null"), |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4422 | config.sample_rate, config.format, config.channel_mask, output_flags); |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 4423 | |
| 4424 | // also try the MSD module if compatible profile not found |
| 4425 | if (profile == nullptr) { |
| 4426 | profile = getMsdProfileForOutput(outputDevices, |
| 4427 | config.sample_rate, |
| 4428 | config.format, |
| 4429 | config.channel_mask, |
| 4430 | output_flags, |
| 4431 | true /* directOnly */); |
| 4432 | ALOGV("%s() MSD profile %sfound with name: %s, " |
| 4433 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 4434 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
| 4435 | (profile != 0 ? profile->getTagName().c_str() : "null"), |
| 4436 | config.sample_rate, config.format, config.channel_mask, output_flags); |
| 4437 | } |
| 4438 | return (profile != nullptr); |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4439 | } |
| 4440 | |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4441 | bool AudioPolicyManager::isOffloadPossible(const audio_offload_info_t &offloadInfo, |
| 4442 | bool durationIgnored) { |
| 4443 | if (mMasterMono) { |
| 4444 | return false; // no offloading if mono is set. |
| 4445 | } |
| 4446 | |
| 4447 | // Check if offload has been disabled |
| 4448 | if (property_get_bool("audio.offload.disable", false /* default_value */)) { |
| 4449 | ALOGV("%s: offload disabled by audio.offload.disable", __func__); |
| 4450 | return false; |
| 4451 | } |
| 4452 | |
| 4453 | // Check if stream type is music, then only allow offload as of now. |
| 4454 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 4455 | { |
| 4456 | ALOGV("%s: stream_type != MUSIC, returning false", __func__); |
| 4457 | return false; |
| 4458 | } |
| 4459 | |
| 4460 | //TODO: enable audio offloading with video when ready |
| 4461 | const bool allowOffloadWithVideo = |
| 4462 | property_get_bool("audio.offload.video", false /* default_value */); |
| 4463 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
| 4464 | ALOGV("%s: has_video == true, returning false", __func__); |
| 4465 | return false; |
| 4466 | } |
| 4467 | |
| 4468 | //If duration is less than minimum value defined in property, return false |
| 4469 | const int min_duration_secs = property_get_int32( |
| 4470 | "audio.offload.min.duration.secs", -1 /* default_value */); |
| 4471 | if (!durationIgnored) { |
| 4472 | if (min_duration_secs >= 0) { |
| 4473 | if (offloadInfo.duration_us < min_duration_secs * 1000000LL) { |
| 4474 | ALOGV("%s: Offload denied by duration < audio.offload.min.duration.secs(=%d)", |
| 4475 | __func__, min_duration_secs); |
| 4476 | return false; |
| 4477 | } |
| 4478 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 4479 | ALOGV("%s: Offload denied by duration < default min(=%u)", |
| 4480 | __func__, OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 4481 | return false; |
| 4482 | } |
| 4483 | } |
| 4484 | |
| 4485 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 4486 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 4487 | // detects there is an active non offloadable effect. |
| 4488 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 4489 | // This may prevent offloading in rare situations where effects are left active by apps |
| 4490 | // in the background. |
| 4491 | if (mEffects.isNonOffloadableEffectEnabled()) { |
| 4492 | return false; |
| 4493 | } |
| 4494 | |
| 4495 | return true; |
| 4496 | } |
| 4497 | |
| 4498 | audio_direct_mode_t AudioPolicyManager::getDirectPlaybackSupport(const audio_attributes_t *attr, |
| 4499 | const audio_config_t *config) { |
| 4500 | audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER; |
| 4501 | offloadInfo.format = config->format; |
| 4502 | offloadInfo.sample_rate = config->sample_rate; |
| 4503 | offloadInfo.channel_mask = config->channel_mask; |
| 4504 | offloadInfo.stream_type = mEngine->getStreamTypeForAttributes(*attr); |
| 4505 | offloadInfo.has_video = false; |
| 4506 | offloadInfo.is_streaming = false; |
| 4507 | const bool offloadPossible = isOffloadPossible(offloadInfo, true /*durationIgnored*/); |
| 4508 | |
| 4509 | audio_direct_mode_t directMode = AUDIO_DIRECT_NOT_SUPPORTED; |
| 4510 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 4511 | audio_flags_to_audio_output_flags(attr->flags, &flags); |
| 4512 | // only retain flags that will drive compressed offload or passthrough |
| 4513 | uint32_t relevantFlags = AUDIO_OUTPUT_FLAG_HW_AV_SYNC; |
| 4514 | if (offloadPossible) { |
| 4515 | relevantFlags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD; |
| 4516 | } |
| 4517 | flags = (audio_output_flags_t)((flags & relevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 4518 | |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4519 | DeviceVector engineOutputDevices = mEngine->getOutputDevicesForAttributes(*attr); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4520 | for (const auto& hwModule : mHwModules) { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4521 | DeviceVector outputDevices = engineOutputDevices; |
| 4522 | // the MSD module checks for different conditions and output devices |
| 4523 | if (strcmp(hwModule->getName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0) { |
| 4524 | if (!msdHasPatchesToAllDevices(engineOutputDevices.toTypeAddrVector())) { |
| 4525 | continue; |
| 4526 | } |
| 4527 | outputDevices = getMsdAudioOutDevices(); |
| 4528 | } |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4529 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
jiabin | 66acc43 | 2024-02-06 00:57:36 +0000 | [diff] [blame] | 4530 | if (curProfile->getCompatibilityScore(outputDevices, |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4531 | config->sample_rate, nullptr /*updatedSamplingRate*/, |
| 4532 | config->format, nullptr /*updatedFormat*/, |
| 4533 | config->channel_mask, nullptr /*updatedChannelMask*/, |
jiabin | 66acc43 | 2024-02-06 00:57:36 +0000 | [diff] [blame] | 4534 | flags) == IOProfile::NO_MATCH) { |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4535 | continue; |
| 4536 | } |
| 4537 | // reject profiles not corresponding to a device currently available |
| 4538 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
| 4539 | continue; |
| 4540 | } |
Yinchu Chen | 9f66758 | 2023-10-10 09:44:54 +0000 | [diff] [blame] | 4541 | if (offloadPossible && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) |
| 4542 | != AUDIO_OUTPUT_FLAG_NONE)) { |
jiabin | c6132d6 | 2022-01-01 07:36:31 +0000 | [diff] [blame] | 4543 | if ((directMode & AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED) |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4544 | != AUDIO_DIRECT_NOT_SUPPORTED) { |
| 4545 | // Already reports offload gapless supported. No need to report offload support. |
| 4546 | continue; |
| 4547 | } |
| 4548 | if ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) |
| 4549 | != AUDIO_OUTPUT_FLAG_NONE) { |
| 4550 | // If offload gapless is reported, no need to report offload support. |
| 4551 | directMode = (audio_direct_mode_t) ((directMode & |
| 4552 | ~AUDIO_DIRECT_OFFLOAD_SUPPORTED) | |
| 4553 | AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED); |
| 4554 | } else { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4555 | directMode = (audio_direct_mode_t)(directMode | AUDIO_DIRECT_OFFLOAD_SUPPORTED); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4556 | } |
| 4557 | } else { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4558 | directMode = (audio_direct_mode_t) (directMode | AUDIO_DIRECT_BITSTREAM_SUPPORTED); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4559 | } |
| 4560 | } |
| 4561 | } |
| 4562 | return directMode; |
| 4563 | } |
| 4564 | |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4565 | status_t AudioPolicyManager::getDirectProfilesForAttributes(const audio_attributes_t* attr, |
| 4566 | AudioProfileVector& audioProfilesVector) { |
Dorin Drimus | 1711263 | 2022-09-23 15:28:51 +0000 | [diff] [blame] | 4567 | if (mEffects.isNonOffloadableEffectEnabled()) { |
| 4568 | return OK; |
| 4569 | } |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 4570 | DeviceVector devices; |
| 4571 | status_t status = getDevicesForAttributes(*attr, devices, false /* forVolume */); |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4572 | if (status != OK) { |
| 4573 | return status; |
| 4574 | } |
| 4575 | ALOGV("%s: found %zu output devices for attributes.", __func__, devices.size()); |
| 4576 | if (devices.empty()) { |
| 4577 | return OK; // no output devices for the attributes |
| 4578 | } |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 4579 | return getProfilesForDevices(devices, audioProfilesVector, |
| 4580 | AUDIO_OUTPUT_FLAG_DIRECT /*flags*/, false /*isInput*/); |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4581 | } |
| 4582 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4583 | status_t AudioPolicyManager::getSupportedMixerAttributes( |
| 4584 | audio_port_handle_t portId, std::vector<audio_mixer_attributes_t> &mixerAttrs) { |
| 4585 | ALOGV("%s, portId=%d", __func__, portId); |
| 4586 | sp<DeviceDescriptor> deviceDescriptor = mAvailableOutputDevices.getDeviceFromId(portId); |
| 4587 | if (deviceDescriptor == nullptr) { |
| 4588 | ALOGE("%s the requested device is currently unavailable", __func__); |
| 4589 | return BAD_VALUE; |
| 4590 | } |
jiabin | 96daffc | 2023-05-11 17:51:55 +0000 | [diff] [blame] | 4591 | if (!audio_is_usb_out_device(deviceDescriptor->type())) { |
| 4592 | ALOGE("%s the requested device(type=%#x) is not usb device", __func__, |
| 4593 | deviceDescriptor->type()); |
| 4594 | return BAD_VALUE; |
| 4595 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4596 | for (const auto& hwModule : mHwModules) { |
| 4597 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
| 4598 | if (curProfile->supportsDevice(deviceDescriptor)) { |
| 4599 | curProfile->toSupportedMixerAttributes(&mixerAttrs); |
| 4600 | } |
| 4601 | } |
| 4602 | } |
| 4603 | return NO_ERROR; |
| 4604 | } |
| 4605 | |
| 4606 | status_t AudioPolicyManager::setPreferredMixerAttributes( |
| 4607 | const audio_attributes_t *attr, |
| 4608 | audio_port_handle_t portId, |
| 4609 | uid_t uid, |
| 4610 | const audio_mixer_attributes_t *mixerAttributes) { |
| 4611 | ALOGV("%s, attr=%s, mixerAttributes={format=%#x, channelMask=%#x, samplingRate=%u, " |
| 4612 | "mixerBehavior=%d}, uid=%d, portId=%u", |
| 4613 | __func__, toString(*attr).c_str(), mixerAttributes->config.format, |
| 4614 | mixerAttributes->config.channel_mask, mixerAttributes->config.sample_rate, |
| 4615 | mixerAttributes->mixer_behavior, uid, portId); |
| 4616 | if (attr->usage != AUDIO_USAGE_MEDIA) { |
| 4617 | ALOGE("%s failed, only media is allowed, the given usage is %d", __func__, attr->usage); |
| 4618 | return BAD_VALUE; |
| 4619 | } |
| 4620 | sp<DeviceDescriptor> deviceDescriptor = mAvailableOutputDevices.getDeviceFromId(portId); |
| 4621 | if (deviceDescriptor == nullptr) { |
| 4622 | ALOGE("%s the requested device is currently unavailable", __func__); |
| 4623 | return BAD_VALUE; |
| 4624 | } |
| 4625 | if (!audio_is_usb_out_device(deviceDescriptor->type())) { |
| 4626 | ALOGE("%s(%d), type=%d, is not a usb output device", |
| 4627 | __func__, portId, deviceDescriptor->type()); |
| 4628 | return BAD_VALUE; |
| 4629 | } |
| 4630 | |
| 4631 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 4632 | audio_flags_to_audio_output_flags(attr->flags, &flags); |
| 4633 | flags = (audio_output_flags_t) (flags | |
| 4634 | audio_output_flags_from_mixer_behavior(mixerAttributes->mixer_behavior)); |
| 4635 | sp<IOProfile> profile = nullptr; |
| 4636 | DeviceVector devices(deviceDescriptor); |
| 4637 | for (const auto& hwModule : mHwModules) { |
| 4638 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
| 4639 | if (curProfile->hasDynamicAudioProfile() |
jiabin | 66acc43 | 2024-02-06 00:57:36 +0000 | [diff] [blame] | 4640 | && curProfile->getCompatibilityScore( |
| 4641 | devices, |
| 4642 | mixerAttributes->config.sample_rate, |
| 4643 | nullptr /*updatedSamplingRate*/, |
| 4644 | mixerAttributes->config.format, |
| 4645 | nullptr /*updatedFormat*/, |
| 4646 | mixerAttributes->config.channel_mask, |
| 4647 | nullptr /*updatedChannelMask*/, |
| 4648 | flags, |
| 4649 | false /*exactMatchRequiredForInputFlags*/) |
| 4650 | != IOProfile::NO_MATCH) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4651 | profile = curProfile; |
| 4652 | break; |
| 4653 | } |
| 4654 | } |
| 4655 | } |
| 4656 | if (profile == nullptr) { |
| 4657 | ALOGE("%s, there is no compatible profile found", __func__); |
| 4658 | return BAD_VALUE; |
| 4659 | } |
| 4660 | |
| 4661 | sp<PreferredMixerAttributesInfo> mixerAttrInfo = |
| 4662 | sp<PreferredMixerAttributesInfo>::make( |
| 4663 | uid, portId, profile, flags, *mixerAttributes); |
| 4664 | const product_strategy_t strategy = mEngine->getProductStrategyForAttributes(*attr); |
| 4665 | mPreferredMixerAttrInfos[portId][strategy] = mixerAttrInfo; |
| 4666 | |
| 4667 | // If 1) there is any client from the preferred mixer configuration owner that is currently |
| 4668 | // active and matches the strategy and 2) current output is on the preferred device and the |
| 4669 | // mixer configuration doesn't match the preferred one, reopen output with preferred mixer |
| 4670 | // configuration. |
| 4671 | std::vector<audio_io_handle_t> outputsToReopen; |
| 4672 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4673 | const auto output = mOutputs.valueAt(i); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 4674 | if (output->mProfile == profile && output->devices().onlyContainsDevice(deviceDescriptor)) { |
| 4675 | if (output->isConfigurationMatched(mixerAttributes->config, flags)) { |
| 4676 | output->mUsePreferredMixerAttributes = true; |
| 4677 | } else { |
| 4678 | for (const auto &client: output->getActiveClients()) { |
| 4679 | if (client->uid() == uid && client->strategy() == strategy) { |
| 4680 | client->setIsInvalid(); |
| 4681 | outputsToReopen.push_back(output->mIoHandle); |
| 4682 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4683 | } |
| 4684 | } |
| 4685 | } |
| 4686 | } |
| 4687 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4688 | config.sample_rate = mixerAttributes->config.sample_rate; |
| 4689 | config.channel_mask = mixerAttributes->config.channel_mask; |
| 4690 | config.format = mixerAttributes->config.format; |
| 4691 | for (const auto output : outputsToReopen) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 4692 | sp<SwAudioOutputDescriptor> desc = |
| 4693 | reopenOutput(mOutputs.valueFor(output), &config, flags, __func__); |
| 4694 | if (desc == nullptr) { |
| 4695 | ALOGE("%s, failed to reopen output with preferred mixer attributes", __func__); |
| 4696 | continue; |
| 4697 | } |
| 4698 | desc->mUsePreferredMixerAttributes = true; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4699 | } |
| 4700 | |
| 4701 | return NO_ERROR; |
| 4702 | } |
| 4703 | |
| 4704 | sp<PreferredMixerAttributesInfo> AudioPolicyManager::getPreferredMixerAttributesInfo( |
jiabin | d9a58d3 | 2023-06-01 17:57:30 +0000 | [diff] [blame] | 4705 | audio_port_handle_t devicePortId, |
| 4706 | product_strategy_t strategy, |
| 4707 | bool activeBitPerfectPreferred) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4708 | auto it = mPreferredMixerAttrInfos.find(devicePortId); |
| 4709 | if (it == mPreferredMixerAttrInfos.end()) { |
| 4710 | return nullptr; |
| 4711 | } |
jiabin | d9a58d3 | 2023-06-01 17:57:30 +0000 | [diff] [blame] | 4712 | if (activeBitPerfectPreferred) { |
| 4713 | for (auto [strategy, info] : it->second) { |
| 4714 | if ((info->getFlags() & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != AUDIO_OUTPUT_FLAG_NONE |
| 4715 | && info->getActiveClientCount() != 0) { |
| 4716 | return info; |
| 4717 | } |
| 4718 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4719 | } |
jiabin | d9a58d3 | 2023-06-01 17:57:30 +0000 | [diff] [blame] | 4720 | auto strategyMatchedMixerAttrInfoIt = it->second.find(strategy); |
| 4721 | return strategyMatchedMixerAttrInfoIt == it->second.end() |
| 4722 | ? nullptr : strategyMatchedMixerAttrInfoIt->second; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4723 | } |
| 4724 | |
| 4725 | status_t AudioPolicyManager::getPreferredMixerAttributes( |
| 4726 | const audio_attributes_t *attr, |
| 4727 | audio_port_handle_t portId, |
| 4728 | audio_mixer_attributes_t* mixerAttributes) { |
| 4729 | sp<PreferredMixerAttributesInfo> info = getPreferredMixerAttributesInfo( |
| 4730 | portId, mEngine->getProductStrategyForAttributes(*attr)); |
| 4731 | if (info == nullptr) { |
| 4732 | return NAME_NOT_FOUND; |
| 4733 | } |
| 4734 | *mixerAttributes = info->getMixerAttributes(); |
| 4735 | return NO_ERROR; |
| 4736 | } |
| 4737 | |
| 4738 | status_t AudioPolicyManager::clearPreferredMixerAttributes(const audio_attributes_t *attr, |
| 4739 | audio_port_handle_t portId, |
| 4740 | uid_t uid) { |
| 4741 | const product_strategy_t strategy = mEngine->getProductStrategyForAttributes(*attr); |
| 4742 | const auto preferredMixerAttrInfo = getPreferredMixerAttributesInfo(portId, strategy); |
| 4743 | if (preferredMixerAttrInfo == nullptr) { |
| 4744 | return NAME_NOT_FOUND; |
| 4745 | } |
| 4746 | if (preferredMixerAttrInfo->getUid() != uid) { |
| 4747 | ALOGE("%s, requested uid=%d, owned uid=%d", |
| 4748 | __func__, uid, preferredMixerAttrInfo->getUid()); |
| 4749 | return PERMISSION_DENIED; |
| 4750 | } |
| 4751 | mPreferredMixerAttrInfos[portId].erase(strategy); |
| 4752 | if (mPreferredMixerAttrInfos[portId].empty()) { |
| 4753 | mPreferredMixerAttrInfos.erase(portId); |
| 4754 | } |
| 4755 | |
| 4756 | // Reconfig existing output |
| 4757 | std::vector<audio_io_handle_t> potentialOutputsToReopen; |
| 4758 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4759 | if (mOutputs.valueAt(i)->mProfile == preferredMixerAttrInfo->getProfile()) { |
| 4760 | potentialOutputsToReopen.push_back(mOutputs.keyAt(i)); |
| 4761 | } |
| 4762 | } |
| 4763 | for (const auto output : potentialOutputsToReopen) { |
| 4764 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 4765 | if (desc->isConfigurationMatched(preferredMixerAttrInfo->getConfigBase(), |
| 4766 | preferredMixerAttrInfo->getFlags())) { |
| 4767 | reopenOutput(desc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 4768 | } |
| 4769 | } |
| 4770 | return NO_ERROR; |
| 4771 | } |
| 4772 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4773 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 4774 | audio_port_type_t type, |
| 4775 | unsigned int *num_ports, |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4776 | struct audio_port_v7 *ports, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4777 | unsigned int *generation) |
| 4778 | { |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4779 | if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) || |
| 4780 | generation == nullptr) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4781 | return BAD_VALUE; |
| 4782 | } |
| 4783 | 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] | 4784 | if (ports == nullptr) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4785 | *num_ports = 0; |
| 4786 | } |
| 4787 | |
| 4788 | size_t portsWritten = 0; |
| 4789 | size_t portsMax = *num_ports; |
| 4790 | *num_ports = 0; |
| 4791 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4792 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 4793 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4794 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4795 | for (const auto& dev : mAvailableOutputDevices) { |
| 4796 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4797 | continue; |
| 4798 | } |
| 4799 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4800 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4801 | } |
| 4802 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4803 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4804 | } |
| 4805 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4806 | for (const auto& dev : mAvailableInputDevices) { |
| 4807 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4808 | continue; |
| 4809 | } |
| 4810 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4811 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4812 | } |
| 4813 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4814 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4815 | } |
| 4816 | } |
| 4817 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 4818 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 4819 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 4820 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 4821 | } |
| 4822 | *num_ports += mInputs.size(); |
| 4823 | } |
| 4824 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4825 | size_t numOutputs = 0; |
| 4826 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4827 | if (!mOutputs[i]->isDuplicated()) { |
| 4828 | numOutputs++; |
| 4829 | if (portsWritten < portsMax) { |
| 4830 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 4831 | } |
| 4832 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4833 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4834 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4835 | } |
| 4836 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4837 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4838 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 4839 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4840 | return NO_ERROR; |
| 4841 | } |
| 4842 | |
Mikhail Naganov | 5edc5ed | 2023-03-23 14:52:15 -0700 | [diff] [blame] | 4843 | status_t AudioPolicyManager::listDeclaredDevicePorts(media::AudioPortRole role, |
| 4844 | std::vector<media::AudioPortFw>* _aidl_return) { |
| 4845 | auto pushPort = [&](const sp<DeviceDescriptor>& dev) -> status_t { |
| 4846 | audio_port_v7 port; |
| 4847 | dev->toAudioPort(&port); |
| 4848 | auto aidlPort = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_v7_AudioPortFw(port)); |
| 4849 | _aidl_return->push_back(std::move(aidlPort)); |
| 4850 | return OK; |
| 4851 | }; |
| 4852 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 4853 | for (const auto& module : mHwModules) { |
Mikhail Naganov | 5edc5ed | 2023-03-23 14:52:15 -0700 | [diff] [blame] | 4854 | for (const auto& dev : module->getDeclaredDevices()) { |
| 4855 | if (role == media::AudioPortRole::NONE || |
| 4856 | ((role == media::AudioPortRole::SOURCE) |
| 4857 | == audio_is_input_device(dev->type()))) { |
| 4858 | RETURN_STATUS_IF_ERROR(pushPort(dev)); |
| 4859 | } |
| 4860 | } |
| 4861 | } |
| 4862 | return OK; |
| 4863 | } |
| 4864 | |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4865 | status_t AudioPolicyManager::getAudioPort(struct audio_port_v7 *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4866 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 4867 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 4868 | return BAD_VALUE; |
| 4869 | } |
| 4870 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 4871 | if (dev != 0) { |
| 4872 | dev->toAudioPort(port); |
| 4873 | return NO_ERROR; |
| 4874 | } |
| 4875 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 4876 | if (dev != 0) { |
| 4877 | dev->toAudioPort(port); |
| 4878 | return NO_ERROR; |
| 4879 | } |
| 4880 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 4881 | if (out != 0) { |
| 4882 | out->toAudioPort(port); |
| 4883 | return NO_ERROR; |
| 4884 | } |
| 4885 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 4886 | if (in != 0) { |
| 4887 | in->toAudioPort(port); |
| 4888 | return NO_ERROR; |
| 4889 | } |
| 4890 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4891 | } |
| 4892 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4893 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 4894 | audio_patch_handle_t *handle, |
| 4895 | uid_t uid) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4896 | { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4897 | ALOGV("%s", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4898 | if (handle == NULL || patch == NULL) { |
| 4899 | return BAD_VALUE; |
| 4900 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4901 | 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] | 4902 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4903 | return BAD_VALUE; |
| 4904 | } |
| 4905 | // only one source per audio patch supported for now |
| 4906 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4907 | return INVALID_OPERATION; |
| 4908 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4909 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4910 | return INVALID_OPERATION; |
| 4911 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4912 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4913 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 4914 | return INVALID_OPERATION; |
| 4915 | } |
| 4916 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4917 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4918 | sp<DeviceDescriptor> srcDevice = mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 4919 | sp<DeviceDescriptor> sinkDevice = mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id); |
| 4920 | if (srcDevice == nullptr || sinkDevice == nullptr) { |
| 4921 | ALOGW("%s could not create patch, invalid sink and/or source device(s)", __func__); |
| 4922 | return BAD_VALUE; |
| 4923 | } |
| 4924 | ALOGV("%s between source %s and sink %s", __func__, |
| 4925 | srcDevice->toString().c_str(), sinkDevice->toString().c_str()); |
| 4926 | audio_port_handle_t portId = PolicyAudioPort::getNextUniqueId(); |
| 4927 | // Default attributes, default volume priority, not to infer with non raw audio patches. |
| 4928 | audio_attributes_t attributes = attributes_initializer(AUDIO_USAGE_MEDIA); |
| 4929 | const struct audio_port_config *source = &patch->sources[0]; |
| 4930 | sp<SourceClientDescriptor> sourceDesc = |
Eric Laurent | 541a200 | 2024-01-15 18:11:42 +0100 | [diff] [blame] | 4931 | new SourceClientDescriptor( |
| 4932 | portId, uid, attributes, *source, srcDevice, AUDIO_STREAM_PATCH, |
| 4933 | mEngine->getProductStrategyForAttributes(attributes), toVolumeSource(attributes), |
| 4934 | true); |
| 4935 | sourceDesc->setPreferredDeviceId(sinkDevice->getId()); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4936 | |
| 4937 | status_t status = |
| 4938 | connectAudioSourceToSink(sourceDesc, sinkDevice, patch, *handle, uid, 0 /* delayMs */); |
| 4939 | |
| 4940 | if (status != NO_ERROR) { |
| 4941 | return INVALID_OPERATION; |
| 4942 | } |
| 4943 | mAudioSources.add(portId, sourceDesc); |
| 4944 | return NO_ERROR; |
| 4945 | } |
| 4946 | |
| 4947 | status_t AudioPolicyManager::connectAudioSourceToSink( |
| 4948 | const sp<SourceClientDescriptor>& sourceDesc, const sp<DeviceDescriptor> &sinkDevice, |
| 4949 | const struct audio_patch *patch, |
| 4950 | audio_patch_handle_t &handle, |
| 4951 | uid_t uid, uint32_t delayMs) |
| 4952 | { |
| 4953 | status_t status = createAudioPatchInternal(patch, &handle, uid, delayMs, sourceDesc); |
| 4954 | if (status != NO_ERROR || mAudioPatches.indexOfKey(handle) < 0) { |
| 4955 | ALOGW("%s patch panel could not connect device patch, error %d", __func__, status); |
| 4956 | return INVALID_OPERATION; |
| 4957 | } |
| 4958 | sourceDesc->connect(handle, sinkDevice); |
| 4959 | if (isMsdPatch(handle)) { |
| 4960 | return NO_ERROR; |
| 4961 | } |
| 4962 | // SW Bridge? (@todo: HW bridge, keep track of HwOutput for device selection "reconsideration") |
| 4963 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); |
| 4964 | ALOG_ASSERT(swOutput != nullptr, "%s: a swOutput shall always be associated", __func__); |
| 4965 | if (swOutput->getClient(sourceDesc->portId()) != nullptr) { |
| 4966 | ALOGW("%s source portId has already been attached to outputDesc", __func__); |
| 4967 | goto FailurePatchAdded; |
| 4968 | } |
| 4969 | status = swOutput->start(); |
| 4970 | if (status != NO_ERROR) { |
| 4971 | goto FailureSourceAdded; |
| 4972 | } |
| 4973 | swOutput->addClient(sourceDesc); |
| 4974 | status = startSource(swOutput, sourceDesc, &delayMs); |
| 4975 | if (status != NO_ERROR) { |
| 4976 | ALOGW("%s failed to start source, error %d", __FUNCTION__, status); |
| 4977 | goto FailureSourceActive; |
| 4978 | } |
| 4979 | if (delayMs != 0) { |
| 4980 | usleep(delayMs * 1000); |
| 4981 | } |
| 4982 | return NO_ERROR; |
| 4983 | |
| 4984 | FailureSourceActive: |
| 4985 | swOutput->stop(); |
| 4986 | releaseOutput(sourceDesc->portId()); |
| 4987 | FailureSourceAdded: |
| 4988 | sourceDesc->setSwOutput(nullptr); |
| 4989 | FailurePatchAdded: |
| 4990 | releaseAudioPatchInternal(handle); |
| 4991 | return INVALID_OPERATION; |
| 4992 | } |
| 4993 | |
| 4994 | status_t AudioPolicyManager::createAudioPatchInternal(const struct audio_patch *patch, |
| 4995 | audio_patch_handle_t *handle, |
| 4996 | uid_t uid, uint32_t delayMs, |
| 4997 | const sp<SourceClientDescriptor>& sourceDesc) |
| 4998 | { |
| 4999 | 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] | 5000 | sp<AudioPatch> patchDesc; |
| 5001 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 5002 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5003 | ALOGV("%s source id %d role %d type %d", __func__, patch->sources[0].id, |
| 5004 | patch->sources[0].role, |
| 5005 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5006 | #if LOG_NDEBUG == 0 |
| 5007 | for (size_t i = 0; i < patch->num_sinks; i++) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5008 | ALOGV("%s sink %zu: id %d role %d type %d", __func__ ,i, patch->sinks[i].id, |
| 5009 | patch->sinks[i].role, |
| 5010 | patch->sinks[i].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5011 | } |
| 5012 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5013 | |
| 5014 | if (index >= 0) { |
| 5015 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5016 | ALOGV("%s mUidCached %d patchDesc->mUid %d uid %d", |
| 5017 | __func__, mUidCached, patchDesc->getUid(), uid); |
| 5018 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5019 | return INVALID_OPERATION; |
| 5020 | } |
| 5021 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5022 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5023 | } |
| 5024 | |
| 5025 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5026 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5027 | if (outputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5028 | 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] | 5029 | return BAD_VALUE; |
| 5030 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5031 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 5032 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5033 | if (patchDesc != 0) { |
| 5034 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5035 | ALOGV("%s source id differs for patch current id %d new id %d", |
| 5036 | __func__, patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5037 | return BAD_VALUE; |
| 5038 | } |
| 5039 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5040 | DeviceVector devices; |
| 5041 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 5042 | // Only support mix to devices connection |
| 5043 | // TODO add support for mix to mix connection |
| 5044 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5045 | ALOGV("%s source mix but sink is not a device", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5046 | return INVALID_OPERATION; |
| 5047 | } |
| 5048 | sp<DeviceDescriptor> devDesc = |
| 5049 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 5050 | if (devDesc == 0) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5051 | 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] | 5052 | return BAD_VALUE; |
| 5053 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5054 | |
jiabin | 66acc43 | 2024-02-06 00:57:36 +0000 | [diff] [blame] | 5055 | if (outputDesc->mProfile->getCompatibilityScore( |
| 5056 | DeviceVector(devDesc), |
| 5057 | patch->sources[0].sample_rate, |
| 5058 | nullptr, // updatedSamplingRate |
| 5059 | patch->sources[0].format, |
| 5060 | nullptr, // updatedFormat |
| 5061 | patch->sources[0].channel_mask, |
| 5062 | nullptr, // updatedChannelMask |
| 5063 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/) == IOProfile::NO_MATCH) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5064 | ALOGV("%s profile not supported for device %08x", __func__, devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5065 | return INVALID_OPERATION; |
| 5066 | } |
| 5067 | devices.add(devDesc); |
| 5068 | } |
| 5069 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5070 | return INVALID_OPERATION; |
| 5071 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5072 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5073 | // TODO: reconfigure output format and channels here |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5074 | ALOGV("%s setting device %s on output %d", |
| 5075 | __func__, dumpDeviceTypes(devices.types()).c_str(), outputDesc->mIoHandle); |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 5076 | setOutputDevices(__func__, outputDesc, devices, true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5077 | index = mAudioPatches.indexOfKey(*handle); |
| 5078 | if (index >= 0) { |
| 5079 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5080 | ALOGW("%s setOutputDevice() did not reuse the patch provided", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5081 | } |
| 5082 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5083 | patchDesc->setUid(uid); |
| 5084 | ALOGV("%s success", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5085 | } else { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5086 | ALOGW("%s setOutputDevice() failed to create a patch", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5087 | return INVALID_OPERATION; |
| 5088 | } |
| 5089 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 5090 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 5091 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5092 | // only one sink supported when connecting an input device to a mix |
| 5093 | if (patch->num_sinks > 1) { |
| 5094 | return INVALID_OPERATION; |
| 5095 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5096 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5097 | if (inputDesc == NULL) { |
| 5098 | return BAD_VALUE; |
| 5099 | } |
| 5100 | if (patchDesc != 0) { |
| 5101 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 5102 | return BAD_VALUE; |
| 5103 | } |
| 5104 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5105 | sp<DeviceDescriptor> device = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5106 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5107 | if (device == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5108 | return BAD_VALUE; |
| 5109 | } |
| 5110 | |
jiabin | 66acc43 | 2024-02-06 00:57:36 +0000 | [diff] [blame] | 5111 | if (inputDesc->mProfile->getCompatibilityScore( |
| 5112 | DeviceVector(device), |
| 5113 | patch->sinks[0].sample_rate, |
| 5114 | nullptr, /*updatedSampleRate*/ |
| 5115 | patch->sinks[0].format, |
| 5116 | nullptr, /*updatedFormat*/ |
| 5117 | patch->sinks[0].channel_mask, |
| 5118 | nullptr, /*updatedChannelMask*/ |
| 5119 | // FIXME for the parameter type, |
| 5120 | // and the NONE |
| 5121 | (audio_output_flags_t) |
| 5122 | AUDIO_INPUT_FLAG_NONE) == IOProfile::NO_MATCH) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5123 | return INVALID_OPERATION; |
| 5124 | } |
| 5125 | // TODO: reconfigure output format and channels here |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5126 | ALOGV("%s setting device %s on output %d", __func__, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5127 | device->toString().c_str(), inputDesc->mIoHandle); |
| 5128 | setInputDevice(inputDesc->mIoHandle, device, true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5129 | index = mAudioPatches.indexOfKey(*handle); |
| 5130 | if (index >= 0) { |
| 5131 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5132 | ALOGW("%s setInputDevice() did not reuse the patch provided", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5133 | } |
| 5134 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5135 | patchDesc->setUid(uid); |
| 5136 | ALOGV("%s success", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5137 | } else { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5138 | ALOGW("%s setInputDevice() failed to create a patch", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5139 | return INVALID_OPERATION; |
| 5140 | } |
| 5141 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 5142 | // device to device connection |
| 5143 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5144 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5145 | return BAD_VALUE; |
| 5146 | } |
| 5147 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5148 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5149 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5150 | if (srcDevice == 0) { |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 5151 | return BAD_VALUE; |
| 5152 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5153 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5154 | //update source and sink with our own data as the data passed in the patch may |
| 5155 | // be incomplete. |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5156 | PatchBuilder patchBuilder; |
| 5157 | audio_port_config sourcePortConfig = {}; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 5158 | |
| 5159 | // if first sink is to MSD, establish single MSD patch |
| 5160 | if (getMsdAudioOutDevices().contains( |
| 5161 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id))) { |
| 5162 | ALOGV("%s patching to MSD", __FUNCTION__); |
| 5163 | patchBuilder = buildMsdPatch(false /*msdIsSource*/, srcDevice); |
| 5164 | goto installPatch; |
| 5165 | } |
| 5166 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5167 | srcDevice->toAudioPortConfig(&sourcePortConfig, &patch->sources[0]); |
| 5168 | patchBuilder.addSource(sourcePortConfig); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5169 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5170 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 5171 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5172 | ALOGV("%s source device but one sink is not a device", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5173 | return INVALID_OPERATION; |
| 5174 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5175 | sp<DeviceDescriptor> sinkDevice = |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5176 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5177 | if (sinkDevice == 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5178 | return BAD_VALUE; |
| 5179 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5180 | audio_port_config sinkPortConfig = {}; |
| 5181 | sinkDevice->toAudioPortConfig(&sinkPortConfig, &patch->sinks[i]); |
| 5182 | patchBuilder.addSink(sinkPortConfig); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5183 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5184 | // Whatever Sw or Hw bridge, we do attach an SwOutput to an Audio Source for |
| 5185 | // volume management purpose (tracking activity) |
| 5186 | // In case of Hw bridge, it is a Work Around. The mixPort used is the one declared |
| 5187 | // in config XML to reach the sink so that is can be declared as available. |
| 5188 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 5189 | sp<SwAudioOutputDescriptor> outputDesc; |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5190 | if (!sourceDesc->isInternal()) { |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5191 | // take care of dynamic routing for SwOutput selection, |
| 5192 | audio_attributes_t attributes = sourceDesc->attributes(); |
| 5193 | audio_stream_type_t stream = sourceDesc->stream(); |
| 5194 | audio_attributes_t resultAttr; |
| 5195 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 5196 | config.sample_rate = sourceDesc->config().sample_rate; |
François Gaffie | 2a24db4 | 2022-04-04 16:45:02 +0200 | [diff] [blame] | 5197 | audio_channel_mask_t sourceMask = sourceDesc->config().channel_mask; |
| 5198 | config.channel_mask = |
| 5199 | (audio_channel_mask_get_representation(sourceMask) |
| 5200 | == AUDIO_CHANNEL_REPRESENTATION_INDEX) ? sourceMask |
| 5201 | : audio_channel_mask_in_to_out(sourceMask); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5202 | config.format = sourceDesc->config().format; |
| 5203 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 5204 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 5205 | bool isRequestedDeviceForExclusiveUse = false; |
| 5206 | output_type_t outputType; |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 5207 | bool isSpatialized; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 5208 | bool isBitPerfect; |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5209 | getOutputForAttrInt(&resultAttr, &output, AUDIO_SESSION_NONE, &attributes, |
| 5210 | &stream, sourceDesc->uid(), &config, &flags, |
| 5211 | &selectedDeviceId, &isRequestedDeviceForExclusiveUse, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 5212 | nullptr, &outputType, &isSpatialized, &isBitPerfect); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5213 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 5214 | ALOGV("%s no output for device %s", |
| 5215 | __FUNCTION__, sinkDevice->toString().c_str()); |
| 5216 | return INVALID_OPERATION; |
| 5217 | } |
| 5218 | outputDesc = mOutputs.valueFor(output); |
| 5219 | if (outputDesc->isDuplicated()) { |
| 5220 | ALOGE("%s output is duplicated", __func__); |
| 5221 | return INVALID_OPERATION; |
| 5222 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5223 | bool closeOutput = outputDesc->mDirectOpenCount != 0; |
| 5224 | sourceDesc->setSwOutput(outputDesc, closeOutput); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5225 | } else { |
| 5226 | // Same for "raw patches" aka created from createAudioPatch API |
| 5227 | SortedVector<audio_io_handle_t> outputs = |
| 5228 | getOutputsForDevices(DeviceVector(sinkDevice), mOutputs); |
| 5229 | // if the sink device is reachable via an opened output stream, request to |
| 5230 | // go via this output stream by adding a second source to the patch |
| 5231 | // description |
| 5232 | output = selectOutput(outputs); |
| 5233 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 5234 | ALOGE("%s no output available for internal patch sink", __func__); |
| 5235 | return INVALID_OPERATION; |
| 5236 | } |
| 5237 | outputDesc = mOutputs.valueFor(output); |
| 5238 | if (outputDesc->isDuplicated()) { |
| 5239 | ALOGV("%s output for device %s is duplicated", |
| 5240 | __func__, sinkDevice->toString().c_str()); |
| 5241 | return INVALID_OPERATION; |
| 5242 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5243 | sourceDesc->setSwOutput(outputDesc, /* closeOutput= */ false); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5244 | } |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 5245 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 5246 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 5247 | // - audio HAL version is < 3.0 |
Francois Gaffie | 99896da | 2018-04-09 11:05:33 +0200 | [diff] [blame] | 5248 | // - 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] | 5249 | // - called from startAudioSource (aka sourceDesc is not internal) and source device |
| 5250 | // does not have a gain controller |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5251 | if (!srcDevice->hasSameHwModuleAs(sinkDevice) || |
| 5252 | (srcDevice->getModuleVersionMajor() < 3) || |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5253 | !srcDevice->getModule()->supportsPatch(srcDevice, sinkDevice) || |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5254 | (!sourceDesc->isInternal() && |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5255 | srcDevice->getAudioPort()->getGains().size() == 0)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 5256 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5257 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5258 | return INVALID_OPERATION; |
| 5259 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5260 | sourceDesc->setUseSwBridge(); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5261 | if (outputDesc != nullptr) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5262 | audio_port_config srcMixPortConfig = {}; |
Ytai Ben-Tsvi | c9d2a91 | 2021-11-22 16:43:09 -0800 | [diff] [blame] | 5263 | outputDesc->toAudioPortConfig(&srcMixPortConfig, nullptr); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5264 | // for volume control, we may need a valid stream |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 5265 | srcMixPortConfig.ext.mix.usecase.stream = |
| 5266 | (!sourceDesc->isInternal() || isCallTxAudioSource(sourceDesc)) ? |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5267 | mEngine->getStreamTypeForAttributes(sourceDesc->attributes()) : |
| 5268 | AUDIO_STREAM_PATCH; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5269 | patchBuilder.addSource(srcMixPortConfig); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5270 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5271 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5272 | } |
| 5273 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 5274 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 5275 | installPatch: |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5276 | status_t status = installPatch( |
| 5277 | __func__, index, handle, patchBuilder.patch(), delayMs, uid, &patchDesc); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5278 | if (status != NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5279 | 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] | 5280 | return INVALID_OPERATION; |
| 5281 | } |
| 5282 | } else { |
| 5283 | return BAD_VALUE; |
| 5284 | } |
| 5285 | } else { |
| 5286 | return BAD_VALUE; |
| 5287 | } |
| 5288 | return NO_ERROR; |
| 5289 | } |
| 5290 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5291 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, uid_t uid) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5292 | { |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5293 | ALOGV("%s patch %d", __func__, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5294 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 5295 | |
| 5296 | if (index < 0) { |
| 5297 | return BAD_VALUE; |
| 5298 | } |
| 5299 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5300 | ALOGV("%s() mUidCached %d patchDesc->mUid %d uid %d", |
| 5301 | __func__, mUidCached, patchDesc->getUid(), uid); |
| 5302 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5303 | return INVALID_OPERATION; |
| 5304 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5305 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 5306 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 5307 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5308 | if (sourceDesc != nullptr && sourceDesc->getPatchHandle() == handle) { |
| 5309 | portId = sourceDesc->portId(); |
| 5310 | break; |
| 5311 | } |
| 5312 | } |
| 5313 | return portId != AUDIO_PORT_HANDLE_NONE ? |
| 5314 | stopAudioSource(portId) : releaseAudioPatchInternal(handle); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5315 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5316 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5317 | status_t AudioPolicyManager::releaseAudioPatchInternal(audio_patch_handle_t handle, |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5318 | uint32_t delayMs, |
| 5319 | const sp<SourceClientDescriptor>& sourceDesc) |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5320 | { |
| 5321 | ALOGV("%s patch %d", __func__, handle); |
| 5322 | if (mAudioPatches.indexOfKey(handle) < 0) { |
| 5323 | ALOGE("%s: no patch found with handle=%d", __func__, handle); |
| 5324 | return BAD_VALUE; |
| 5325 | } |
| 5326 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5327 | struct audio_patch *patch = &patchDesc->mPatch; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5328 | patchDesc->setUid(mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5329 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5330 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5331 | if (outputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5332 | 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] | 5333 | return BAD_VALUE; |
| 5334 | } |
| 5335 | |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 5336 | setOutputDevices(__func__, outputDesc, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5337 | getNewOutputDevices(outputDesc, true /*fromCache*/), |
| 5338 | true, |
| 5339 | 0, |
| 5340 | NULL); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5341 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 5342 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5343 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5344 | if (inputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5345 | 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] | 5346 | return BAD_VALUE; |
| 5347 | } |
| 5348 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 5349 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5350 | true, |
| 5351 | NULL); |
| 5352 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5353 | status_t status = |
| 5354 | mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); |
| 5355 | ALOGV("%s patch panel returned %d patchHandle %d", |
| 5356 | __func__, status, patchDesc->getAfHandle()); |
| 5357 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5358 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5359 | mpClientInterface->onAudioPatchListUpdate(); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5360 | // SW or HW Bridge |
| 5361 | sp<SwAudioOutputDescriptor> outputDesc = nullptr; |
| 5362 | audio_patch_handle_t patchHandle = AUDIO_PATCH_HANDLE_NONE; |
Francois Gaffie | 7feb854 | 2020-04-06 17:39:47 +0200 | [diff] [blame] | 5363 | 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] | 5364 | outputDesc = mOutputs.getOutputFromId(patch->sources[1].id); |
| 5365 | } else if (patch->num_sources == 1 && sourceDesc != nullptr) { |
| 5366 | outputDesc = sourceDesc->swOutput().promote(); |
| 5367 | } |
| 5368 | if (outputDesc == nullptr) { |
| 5369 | ALOGW("%s no output for id %d", __func__, patch->sources[0].id); |
| 5370 | // releaseOutput has already called closeOutput in case of direct output |
| 5371 | return NO_ERROR; |
| 5372 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5373 | patchHandle = outputDesc->getPatchHandle(); |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5374 | // While using a HwBridge, force reconsidering device only if not reusing an existing |
| 5375 | // output and no more activity on output (will force to close). |
François Gaffie | 150fcc6 | 2023-09-15 11:02:39 +0200 | [diff] [blame] | 5376 | const bool force = sourceDesc->canCloseOutput() && !outputDesc->isActive(); |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5377 | // APM pattern is to have always outputs opened / patch realized for reachable devices. |
| 5378 | // Update device may result to NONE (empty), coupled with force, it releases the patch. |
| 5379 | // Reconsider device only for cases: |
| 5380 | // 1 / Active Output |
| 5381 | // 2 / Inactive Output previously hosting HwBridge |
| 5382 | // 3 / Inactive Output previously hosting SwBridge that can be closed. |
| 5383 | bool updateDevice = outputDesc->isActive() || !sourceDesc->useSwBridge() || |
| 5384 | sourceDesc->canCloseOutput(); |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 5385 | setOutputDevices(__func__, outputDesc, |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5386 | updateDevice ? getNewOutputDevices(outputDesc, true /*fromCache*/) : |
| 5387 | outputDesc->devices(), |
| 5388 | force, |
| 5389 | 0, |
| 5390 | patchHandle == AUDIO_PATCH_HANDLE_NONE ? nullptr : &patchHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5391 | } else { |
| 5392 | return BAD_VALUE; |
| 5393 | } |
| 5394 | } else { |
| 5395 | return BAD_VALUE; |
| 5396 | } |
| 5397 | return NO_ERROR; |
| 5398 | } |
| 5399 | |
| 5400 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 5401 | struct audio_patch *patches, |
| 5402 | unsigned int *generation) |
| 5403 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5404 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5405 | return BAD_VALUE; |
| 5406 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5407 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5408 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5409 | } |
| 5410 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5411 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5412 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5413 | ALOGV("setAudioPortConfig()"); |
| 5414 | |
| 5415 | if (config == NULL) { |
| 5416 | return BAD_VALUE; |
| 5417 | } |
| 5418 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 5419 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5420 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 5421 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5422 | } |
| 5423 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5424 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5425 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 5426 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5427 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5428 | if (outputDesc == NULL) { |
| 5429 | return BAD_VALUE; |
| 5430 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5431 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 5432 | "setAudioPortConfig() called on duplicated output %d", |
| 5433 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5434 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5435 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5436 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5437 | if (inputDesc == NULL) { |
| 5438 | return BAD_VALUE; |
| 5439 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5440 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5441 | } else { |
| 5442 | return BAD_VALUE; |
| 5443 | } |
| 5444 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 5445 | sp<DeviceDescriptor> deviceDesc; |
| 5446 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 5447 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 5448 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 5449 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 5450 | } else { |
| 5451 | return BAD_VALUE; |
| 5452 | } |
| 5453 | if (deviceDesc == NULL) { |
| 5454 | return BAD_VALUE; |
| 5455 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5456 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5457 | } else { |
| 5458 | return BAD_VALUE; |
| 5459 | } |
| 5460 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 5461 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5462 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 5463 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 5464 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5465 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 5466 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5467 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5468 | if (status != NO_ERROR) { |
| 5469 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5470 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5471 | |
| 5472 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5473 | } |
| 5474 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5475 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 5476 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5477 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5478 | clearAudioPatches(uid); |
| 5479 | clearSessionRoutes(uid); |
| 5480 | } |
| 5481 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5482 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 5483 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 5484 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5485 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5486 | if (patchDesc->getUid() == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 5487 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5488 | } |
| 5489 | } |
| 5490 | } |
| 5491 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5492 | void AudioPolicyManager::checkStrategyRoute(product_strategy_t ps, audio_io_handle_t ouptutToSkip) |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5493 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5494 | // Take the first attributes following the product strategy as it is used to retrieve the routed |
| 5495 | // device. All attributes wihin a strategy follows the same "routing strategy" |
| 5496 | auto attributes = mEngine->getAllAttributesForProductStrategy(ps).front(); |
| 5497 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(attributes, nullptr, false); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5498 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5499 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5500 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 5501 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 5502 | continue; |
| 5503 | } |
| 5504 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5505 | if (!outputDesc->isStrategyActive(ps)) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5506 | continue; |
| 5507 | } |
| 5508 | // If the default device for this strategy is on another output mix, |
| 5509 | // invalidate all tracks in this strategy to force re connection. |
| 5510 | // Otherwise select new device on the output mix. |
| 5511 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 5512 | invalidateStreams(mEngine->getStreamTypesForProductStrategy(ps)); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5513 | } else { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5514 | DeviceVector newDevices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
| 5515 | if (outputDesc->mUsePreferredMixerAttributes && outputDesc->devices() != newDevices) { |
| 5516 | // If the device is using preferred mixer attributes, the output need to reopen |
| 5517 | // with default configuration when the new selected devices are different from |
| 5518 | // current routing devices. |
| 5519 | outputsToReopen.emplace(mOutputs.keyAt(j), newDevices); |
| 5520 | continue; |
| 5521 | } |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 5522 | setOutputDevices(__func__, outputDesc, newDevices, false); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5523 | } |
| 5524 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5525 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5526 | } |
| 5527 | |
| 5528 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 5529 | { |
| 5530 | // remove output routes associated with this uid |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5531 | std::vector<product_strategy_t> affectedStrategies; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5532 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5533 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 5534 | for (const auto& client : outputDesc->getClientIterable()) { |
| 5535 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 5536 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5537 | auto clientStrategy = client->strategy(); |
| 5538 | if (std::find(begin(affectedStrategies), end(affectedStrategies), clientStrategy) != |
| 5539 | end(affectedStrategies)) { |
| 5540 | continue; |
| 5541 | } |
| 5542 | affectedStrategies.push_back(client->strategy()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5543 | } |
| 5544 | } |
| 5545 | } |
| 5546 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5547 | for (const auto& strategy : affectedStrategies) { |
| 5548 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5549 | } |
| 5550 | |
| 5551 | // remove input routes associated with this uid |
| 5552 | SortedVector<audio_source_t> affectedSources; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5553 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5554 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 5555 | for (const auto& client : inputDesc->getClientIterable()) { |
| 5556 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 5557 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 5558 | affectedSources.add(client->source()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5559 | } |
| 5560 | } |
| 5561 | } |
| 5562 | // reroute inputs if necessary |
| 5563 | SortedVector<audio_io_handle_t> inputsToClose; |
| 5564 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5565 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 5566 | if (affectedSources.indexOf(inputDesc->source()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5567 | inputsToClose.add(inputDesc->mIoHandle); |
| 5568 | } |
| 5569 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5570 | for (const auto& input : inputsToClose) { |
| 5571 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5572 | } |
| 5573 | } |
| 5574 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5575 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 5576 | { |
| 5577 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5578 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5579 | if (sourceDesc->uid() == uid) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5580 | stopAudioSource(mAudioSources.keyAt(i)); |
| 5581 | } |
| 5582 | } |
| 5583 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5584 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5585 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 5586 | audio_io_handle_t *ioHandle, |
| 5587 | audio_devices_t *device) |
| 5588 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 5589 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 5590 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5591 | audio_attributes_t attr = { .source = AUDIO_SOURCE_HOTWORD }; |
Eric Laurent | cedd5b5 | 2023-03-22 00:03:31 +0000 | [diff] [blame] | 5592 | sp<DeviceDescriptor> deviceDesc = mEngine->getInputDeviceForAttributes(attr); |
| 5593 | if (deviceDesc == nullptr) { |
| 5594 | return INVALID_OPERATION; |
| 5595 | } |
| 5596 | *device = deviceDesc->type(); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5597 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 5598 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5599 | } |
| 5600 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5601 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5602 | const audio_attributes_t *attributes, |
| 5603 | audio_port_handle_t *portId, |
Eric Laurent | 541a200 | 2024-01-15 18:11:42 +0100 | [diff] [blame] | 5604 | uid_t uid, bool internal) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5605 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5606 | ALOGV("%s", __FUNCTION__); |
| 5607 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 5608 | |
| 5609 | if (source == NULL || attributes == NULL || portId == NULL) { |
| 5610 | ALOGW("%s invalid argument: source %p attributes %p handle %p", |
| 5611 | __FUNCTION__, source, attributes, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5612 | return BAD_VALUE; |
| 5613 | } |
| 5614 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5615 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 5616 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5617 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", |
| 5618 | __FUNCTION__, source->role, source->type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5619 | return INVALID_OPERATION; |
| 5620 | } |
| 5621 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5622 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5623 | mAvailableInputDevices.getDevice(source->ext.device.type, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5624 | String8(source->ext.device.address), |
| 5625 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5626 | if (srcDevice == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5627 | 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] | 5628 | return BAD_VALUE; |
| 5629 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5630 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 5631 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5632 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 5633 | sp<SourceClientDescriptor> sourceDesc = |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5634 | new SourceClientDescriptor(*portId, uid, *attributes, *source, srcDevice, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 5635 | mEngine->getStreamTypeForAttributes(*attributes), |
| 5636 | mEngine->getProductStrategyForAttributes(*attributes), |
Eric Laurent | 541a200 | 2024-01-15 18:11:42 +0100 | [diff] [blame] | 5637 | toVolumeSource(*attributes), internal); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5638 | |
| 5639 | status_t status = connectAudioSource(sourceDesc); |
| 5640 | if (status == NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5641 | mAudioSources.add(*portId, sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5642 | } |
| 5643 | return status; |
| 5644 | } |
| 5645 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5646 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5647 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5648 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5649 | |
| 5650 | // make sure we only have one patch per source. |
| 5651 | disconnectAudioSource(sourceDesc); |
| 5652 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5653 | audio_attributes_t attributes = sourceDesc->attributes(); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5654 | // May the device (dynamic) have been disconnected/reconnected, id has changed. |
| 5655 | sp<DeviceDescriptor> srcDevice = mAvailableInputDevices.getDevice( |
| 5656 | sourceDesc->srcDevice()->type(), |
| 5657 | String8(sourceDesc->srcDevice()->address().c_str()), |
| 5658 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5659 | DeviceVector sinkDevices = |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 5660 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false /*fromCache*/); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5661 | ALOG_ASSERT(!sinkDevices.isEmpty(), "connectAudioSource(): no device found for attributes"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5662 | sp<DeviceDescriptor> sinkDevice = sinkDevices.itemAt(0); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5663 | if (!mAvailableOutputDevices.contains(sinkDevice)) { |
| 5664 | ALOGE("%s Device %s not available", __func__, sinkDevice->toString().c_str()); |
| 5665 | return INVALID_OPERATION; |
| 5666 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5667 | PatchBuilder patchBuilder; |
| 5668 | patchBuilder.addSink(sinkDevice).addSource(srcDevice); |
| 5669 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5670 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5671 | return connectAudioSourceToSink( |
| 5672 | sourceDesc, sinkDevice, patchBuilder.patch(), handle, mUidCached, 0 /*delayMs*/); |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5673 | } |
| 5674 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5675 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5676 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5677 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); |
| 5678 | ALOGV("%s port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5679 | if (sourceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5680 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5681 | return BAD_VALUE; |
| 5682 | } |
| 5683 | status_t status = disconnectAudioSource(sourceDesc); |
| 5684 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5685 | mAudioSources.removeItem(portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5686 | return status; |
| 5687 | } |
| 5688 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5689 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 5690 | { |
| 5691 | if (mMasterMono == mono) { |
| 5692 | return NO_ERROR; |
| 5693 | } |
| 5694 | mMasterMono = mono; |
| 5695 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 5696 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 5697 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 5698 | // |
| 5699 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 5700 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 5701 | // play back offloaded. |
| 5702 | if (mMasterMono) { |
| 5703 | Vector<audio_io_handle_t> offloaded; |
| 5704 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 5705 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5706 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 5707 | offloaded.push(desc->mIoHandle); |
| 5708 | } |
| 5709 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5710 | for (const auto& handle : offloaded) { |
| 5711 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5712 | } |
| 5713 | } |
| 5714 | // update master mono for all remaining outputs |
| 5715 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 5716 | updateMono(mOutputs.keyAt(i)); |
| 5717 | } |
| 5718 | return NO_ERROR; |
| 5719 | } |
| 5720 | |
| 5721 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 5722 | { |
| 5723 | *mono = mMasterMono; |
| 5724 | return NO_ERROR; |
| 5725 | } |
| 5726 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 5727 | float AudioPolicyManager::getStreamVolumeDB( |
| 5728 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 5729 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5730 | return computeVolume(getVolumeCurves(stream), toVolumeSource(stream), index, {device}); |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 5731 | } |
| 5732 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5733 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 5734 | audio_format_t *surroundFormats, |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5735 | bool *surroundFormatsEnabled) |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5736 | { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5737 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && |
| 5738 | (surroundFormats == nullptr || surroundFormatsEnabled == nullptr))) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5739 | return BAD_VALUE; |
| 5740 | } |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5741 | ALOGV("%s() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p", |
| 5742 | __func__, *numSurroundFormats, surroundFormats, surroundFormatsEnabled); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5743 | |
| 5744 | size_t formatsWritten = 0; |
| 5745 | size_t formatsMax = *numSurroundFormats; |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 5746 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5747 | *numSurroundFormats = mConfig->getSurroundFormats().size(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5748 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5749 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5750 | for (const auto& format: mConfig->getSurroundFormats()) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5751 | if (formatsWritten < formatsMax) { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5752 | surroundFormats[formatsWritten] = format.first; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5753 | bool formatEnabled = true; |
| 5754 | switch (forceUse) { |
| 5755 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL: |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5756 | formatEnabled = mManualSurroundFormats.count(format.first) != 0; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5757 | break; |
| 5758 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER: |
| 5759 | formatEnabled = false; |
| 5760 | break; |
| 5761 | default: // AUTO or ALWAYS => true |
| 5762 | break; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5763 | } |
| 5764 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 5765 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5766 | } |
| 5767 | return NO_ERROR; |
| 5768 | } |
| 5769 | |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5770 | status_t AudioPolicyManager::getReportedSurroundFormats(unsigned int *numSurroundFormats, |
| 5771 | audio_format_t *surroundFormats) { |
| 5772 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) { |
| 5773 | return BAD_VALUE; |
| 5774 | } |
| 5775 | ALOGV("%s() numSurroundFormats %d surroundFormats %p", |
| 5776 | __func__, *numSurroundFormats, surroundFormats); |
| 5777 | |
| 5778 | size_t formatsWritten = 0; |
| 5779 | size_t formatsMax = *numSurroundFormats; |
| 5780 | std::unordered_set<audio_format_t> formats; // Uses primary surround formats only |
| 5781 | |
| 5782 | // Return formats from all device profiles that have already been resolved by |
| 5783 | // checkOutputsForDevice(). |
| 5784 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 5785 | sp<DeviceDescriptor> device = mAvailableOutputDevices[i]; |
| 5786 | audio_devices_t deviceType = device->type(); |
| 5787 | // Enabling/disabling formats are applied to only HDMI devices. So, this function |
| 5788 | // returns formats reported by HDMI devices. |
| 5789 | if (deviceType != AUDIO_DEVICE_OUT_HDMI) { |
| 5790 | continue; |
| 5791 | } |
| 5792 | // Formats reported by sink devices |
| 5793 | std::unordered_set<audio_format_t> formatset; |
| 5794 | if (auto it = mReportedFormatsMap.find(device); it != mReportedFormatsMap.end()) { |
| 5795 | formatset.insert(it->second.begin(), it->second.end()); |
| 5796 | } |
| 5797 | |
| 5798 | // Formats hard-coded in the in policy configuration file (if any). |
| 5799 | FormatVector encodedFormats = device->encodedFormats(); |
| 5800 | formatset.insert(encodedFormats.begin(), encodedFormats.end()); |
| 5801 | // Filter the formats which are supported by the vendor hardware. |
| 5802 | for (auto it = formatset.begin(); it != formatset.end(); ++it) { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5803 | if (mConfig->getSurroundFormats().count(*it) != 0) { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5804 | formats.insert(*it); |
| 5805 | } else { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5806 | for (const auto& pair : mConfig->getSurroundFormats()) { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5807 | if (pair.second.count(*it) != 0) { |
| 5808 | formats.insert(pair.first); |
| 5809 | break; |
| 5810 | } |
| 5811 | } |
| 5812 | } |
| 5813 | } |
| 5814 | } |
| 5815 | *numSurroundFormats = formats.size(); |
| 5816 | for (const auto& format: formats) { |
| 5817 | if (formatsWritten < formatsMax) { |
| 5818 | surroundFormats[formatsWritten++] = format; |
| 5819 | } |
| 5820 | } |
| 5821 | return NO_ERROR; |
| 5822 | } |
| 5823 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5824 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 5825 | { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5826 | ALOGV("%s() format 0x%X enabled %d", __func__, audioFormat, enabled); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5827 | const auto& formatIter = mConfig->getSurroundFormats().find(audioFormat); |
| 5828 | if (formatIter == mConfig->getSurroundFormats().end()) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5829 | ALOGW("%s() format 0x%X is not a known surround format", __func__, audioFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5830 | return BAD_VALUE; |
| 5831 | } |
| 5832 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5833 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND) != |
| 5834 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5835 | ALOGW("%s() not in manual mode for surround sound format selection", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5836 | return INVALID_OPERATION; |
| 5837 | } |
| 5838 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5839 | if ((mManualSurroundFormats.count(audioFormat) != 0) == enabled) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5840 | return NO_ERROR; |
| 5841 | } |
| 5842 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5843 | std::unordered_set<audio_format_t> surroundFormatsBackup(mManualSurroundFormats); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5844 | if (enabled) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5845 | mManualSurroundFormats.insert(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 5846 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5847 | mManualSurroundFormats.insert(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5848 | } |
| 5849 | } else { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5850 | mManualSurroundFormats.erase(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 5851 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5852 | mManualSurroundFormats.erase(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5853 | } |
| 5854 | } |
| 5855 | |
| 5856 | sp<SwAudioOutputDescriptor> outputDesc; |
| 5857 | bool profileUpdated = false; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5858 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromType( |
| 5859 | AUDIO_DEVICE_OUT_HDMI); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5860 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 5861 | // Simulate reconnection to update enabled surround sound formats. |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5862 | String8 address = String8(hdmiOutputDevices[i]->address().c_str()); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 5863 | std::string name = hdmiOutputDevices[i]->getName(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5864 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 5865 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 5866 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5867 | name.c_str(), |
| 5868 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5869 | if (status != NO_ERROR) { |
| 5870 | continue; |
| 5871 | } |
| 5872 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 5873 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 5874 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5875 | name.c_str(), |
| 5876 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5877 | profileUpdated |= (status == NO_ERROR); |
| 5878 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5879 | // 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] | 5880 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromType( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5881 | AUDIO_DEVICE_IN_HDMI); |
| 5882 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 5883 | // Simulate reconnection to update enabled surround sound formats. |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5884 | String8 address = String8(hdmiInputDevices[i]->address().c_str()); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 5885 | std::string name = hdmiInputDevices[i]->getName(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5886 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 5887 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 5888 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5889 | name.c_str(), |
| 5890 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5891 | if (status != NO_ERROR) { |
| 5892 | continue; |
| 5893 | } |
| 5894 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 5895 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 5896 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5897 | name.c_str(), |
| 5898 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5899 | profileUpdated |= (status == NO_ERROR); |
| 5900 | } |
| 5901 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5902 | if (!profileUpdated) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5903 | ALOGW("%s() no audio profiles updated, undoing surround formats change", __func__); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5904 | mManualSurroundFormats = std::move(surroundFormatsBackup); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5905 | } |
| 5906 | |
| 5907 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 5908 | } |
| 5909 | |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5910 | void AudioPolicyManager::setAppState(audio_port_handle_t portId, app_state_t state) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 5911 | { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5912 | ALOGV("%s(portId:%d, state:%d)", __func__, portId, state); |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 5913 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5914 | mInputs.valueAt(i)->setAppState(portId, state); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 5915 | } |
| 5916 | } |
| 5917 | |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 5918 | bool AudioPolicyManager::isHapticPlaybackSupported() |
| 5919 | { |
| 5920 | for (const auto& hwModule : mHwModules) { |
| 5921 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 5922 | for (const auto &outProfile : outputProfiles) { |
| 5923 | struct audio_port audioPort; |
| 5924 | outProfile->toAudioPort(&audioPort); |
| 5925 | for (size_t i = 0; i < audioPort.num_channel_masks; i++) { |
| 5926 | if (audioPort.channel_masks[i] & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 5927 | return true; |
| 5928 | } |
| 5929 | } |
| 5930 | } |
| 5931 | } |
| 5932 | return false; |
| 5933 | } |
| 5934 | |
Carter Hsu | 325a8eb | 2022-01-19 19:56:51 +0800 | [diff] [blame] | 5935 | bool AudioPolicyManager::isUltrasoundSupported() |
| 5936 | { |
| 5937 | bool hasUltrasoundOutput = false; |
| 5938 | bool hasUltrasoundInput = false; |
| 5939 | for (const auto& hwModule : mHwModules) { |
| 5940 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 5941 | if (!hasUltrasoundOutput) { |
| 5942 | for (const auto &outProfile : outputProfiles) { |
| 5943 | if (outProfile->getFlags() & AUDIO_OUTPUT_FLAG_ULTRASOUND) { |
| 5944 | hasUltrasoundOutput = true; |
| 5945 | break; |
| 5946 | } |
| 5947 | } |
| 5948 | } |
| 5949 | |
| 5950 | const InputProfileCollection &inputProfiles = hwModule->getInputProfiles(); |
| 5951 | if (!hasUltrasoundInput) { |
| 5952 | for (const auto &inputProfile : inputProfiles) { |
| 5953 | if (inputProfile->getFlags() & AUDIO_INPUT_FLAG_ULTRASOUND) { |
| 5954 | hasUltrasoundInput = true; |
| 5955 | break; |
| 5956 | } |
| 5957 | } |
| 5958 | } |
| 5959 | |
| 5960 | if (hasUltrasoundOutput && hasUltrasoundInput) |
| 5961 | return true; |
| 5962 | } |
| 5963 | return false; |
| 5964 | } |
| 5965 | |
Atneya Nair | 698f5ef | 2022-12-15 16:15:09 -0800 | [diff] [blame] | 5966 | bool AudioPolicyManager::isHotwordStreamSupported(bool lookbackAudio) |
| 5967 | { |
| 5968 | const auto mask = AUDIO_INPUT_FLAG_HOTWORD_TAP | |
| 5969 | (lookbackAudio ? AUDIO_INPUT_FLAG_HW_LOOKBACK : 0); |
| 5970 | for (const auto& hwModule : mHwModules) { |
| 5971 | const InputProfileCollection &inputProfiles = hwModule->getInputProfiles(); |
| 5972 | for (const auto &inputProfile : inputProfiles) { |
| 5973 | if ((inputProfile->getFlags() & mask) == mask) { |
| 5974 | return true; |
| 5975 | } |
| 5976 | } |
| 5977 | } |
| 5978 | return false; |
| 5979 | } |
| 5980 | |
Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 5981 | bool AudioPolicyManager::isCallScreenModeSupported() |
| 5982 | { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5983 | return mConfig->isCallScreenModeSupported(); |
Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 5984 | } |
| 5985 | |
| 5986 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5987 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5988 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5989 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5990 | if (!sourceDesc->isConnected()) { |
| 5991 | ALOGV("%s port Id %d already disconnected", __FUNCTION__, sourceDesc->portId()); |
| 5992 | return NO_ERROR; |
| 5993 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5994 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); |
| 5995 | if (swOutput != 0) { |
| 5996 | status_t status = stopSource(swOutput, sourceDesc); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 5997 | if (status == NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5998 | swOutput->stop(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 5999 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6000 | if (releaseOutput(sourceDesc->portId())) { |
| 6001 | // The output descriptor is reopened to query dynamic profiles. In that case, there is |
| 6002 | // no need to release audio patch here but just return NO_ERROR. |
| 6003 | return NO_ERROR; |
| 6004 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6005 | } else { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6006 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6007 | if (hwOutputDesc != 0) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6008 | // close Hwoutput and remove from mHwOutputs |
| 6009 | } else { |
| 6010 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 6011 | } |
| 6012 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6013 | status_t status = releaseAudioPatchInternal(sourceDesc->getPatchHandle(), 0, sourceDesc); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 6014 | sourceDesc->disconnect(); |
| 6015 | return status; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6016 | } |
| 6017 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6018 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForAttributesOnOutput( |
| 6019 | audio_io_handle_t output, const audio_attributes_t &attr) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6020 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6021 | sp<SourceClientDescriptor> source; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6022 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6023 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6024 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6025 | if (followsSameRouting(attr, sourceDesc->attributes()) && |
| 6026 | outputDesc != 0 && outputDesc->mIoHandle == output) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6027 | source = sourceDesc; |
| 6028 | break; |
| 6029 | } |
| 6030 | } |
| 6031 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 6032 | } |
| 6033 | |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 6034 | bool AudioPolicyManager::canBeSpatializedInt(const audio_attributes_t *attr, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6035 | const audio_config_t *config, |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 6036 | const AudioDeviceTypeAddrVector &devices) const |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6037 | { |
| 6038 | // The caller can have the audio attributes criteria ignored by either passing a null ptr or |
| 6039 | // the AUDIO_ATTRIBUTES_INITIALIZER value. |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 6040 | // 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] | 6041 | // and game usages. |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6042 | if (attr != nullptr && *attr != AUDIO_ATTRIBUTES_INITIALIZER) { |
| 6043 | if (attr->usage != AUDIO_USAGE_MEDIA && attr->usage != AUDIO_USAGE_GAME) { |
| 6044 | return false; |
| 6045 | } |
| 6046 | if ((attr->flags & (AUDIO_FLAG_CONTENT_SPATIALIZED | AUDIO_FLAG_NEVER_SPATIALIZE)) != 0) { |
| 6047 | return false; |
| 6048 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6049 | } |
| 6050 | |
Eric Laurent | d332bc8 | 2023-08-04 11:45:23 +0200 | [diff] [blame] | 6051 | // The caller can have the audio config criteria ignored by either passing a null ptr or |
| 6052 | // the AUDIO_CONFIG_INITIALIZER value. |
| 6053 | // If an audio config is specified, current policy is to only allow spatialization for |
Eric Laurent | f9230d5 | 2024-01-26 18:49:09 +0100 | [diff] [blame] | 6054 | // some positional channel masks and PCM format and for stereo if low latency performance |
| 6055 | // mode is not requested. |
Eric Laurent | d332bc8 | 2023-08-04 11:45:23 +0200 | [diff] [blame] | 6056 | |
| 6057 | if (config != nullptr && *config != AUDIO_CONFIG_INITIALIZER) { |
Nikhil Bhanu | 8f4ea77 | 2024-01-31 17:15:52 -0800 | [diff] [blame] | 6058 | static const bool stereo_spatialization_enabled = |
| 6059 | property_get_bool("ro.audio.stereo_spatialization_enabled", false); |
Andy Hung | 481bfe3 | 2023-12-18 14:00:29 -0800 | [diff] [blame] | 6060 | const bool channel_mask_spatialized = |
Nikhil Bhanu | 8f4ea77 | 2024-01-31 17:15:52 -0800 | [diff] [blame] | 6061 | (stereo_spatialization_enabled && com_android_media_audio_stereo_spatialization()) |
Andy Hung | 481bfe3 | 2023-12-18 14:00:29 -0800 | [diff] [blame] | 6062 | ? audio_channel_mask_contains_stereo(config->channel_mask) |
| 6063 | : audio_is_channel_mask_spatialized(config->channel_mask); |
| 6064 | if (!channel_mask_spatialized) { |
Eric Laurent | d332bc8 | 2023-08-04 11:45:23 +0200 | [diff] [blame] | 6065 | return false; |
| 6066 | } |
| 6067 | if (!audio_is_linear_pcm(config->format)) { |
| 6068 | return false; |
| 6069 | } |
Eric Laurent | f9230d5 | 2024-01-26 18:49:09 +0100 | [diff] [blame] | 6070 | if (config->channel_mask == AUDIO_CHANNEL_OUT_STEREO |
| 6071 | && ((attr->flags & AUDIO_FLAG_LOW_LATENCY) != 0)) { |
| 6072 | return false; |
| 6073 | } |
Eric Laurent | d332bc8 | 2023-08-04 11:45:23 +0200 | [diff] [blame] | 6074 | } |
| 6075 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6076 | sp<IOProfile> profile = |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6077 | getSpatializerOutputProfile(config, devices); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6078 | if (profile == nullptr) { |
| 6079 | return false; |
| 6080 | } |
| 6081 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6082 | return true; |
| 6083 | } |
| 6084 | |
| 6085 | void AudioPolicyManager::checkVirtualizerClientRoutes() { |
| 6086 | std::set<audio_stream_type_t> streamsToInvalidate; |
| 6087 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6088 | const sp<SwAudioOutputDescriptor>& desc = mOutputs[i]; |
| 6089 | for (const sp<TrackClientDescriptor>& client : desc->getClientIterable()) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6090 | audio_attributes_t attr = client->attributes(); |
| 6091 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false); |
| 6092 | AudioDeviceTypeAddrVector devicesTypeAddress = devices.toTypeAddrVector(); |
| 6093 | audio_config_base_t clientConfig = client->config(); |
| 6094 | audio_config_t config = audio_config_initializer(&clientConfig); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6095 | if (desc != mSpatializerOutput |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 6096 | && canBeSpatializedInt(&attr, &config, devicesTypeAddress)) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6097 | streamsToInvalidate.insert(client->stream()); |
| 6098 | } |
| 6099 | } |
| 6100 | } |
| 6101 | |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 6102 | invalidateStreams(StreamTypeVector(streamsToInvalidate.begin(), streamsToInvalidate.end())); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6103 | } |
| 6104 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6105 | |
| 6106 | bool AudioPolicyManager::isOutputOnlyAvailableRouteToSomeDevice( |
| 6107 | const sp<SwAudioOutputDescriptor>& outputDesc) { |
| 6108 | if (outputDesc->isDuplicated()) { |
| 6109 | return false; |
| 6110 | } |
| 6111 | DeviceVector devices = outputDesc->supportedDevices(); |
| 6112 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6113 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 6114 | if (desc == outputDesc || desc->isDuplicated()) { |
| 6115 | continue; |
| 6116 | } |
| 6117 | DeviceVector sharedDevices = desc->filterSupportedDevices(devices); |
| 6118 | if (!sharedDevices.isEmpty() |
| 6119 | && (desc->devicesSupportEncodedFormats(sharedDevices.types()) |
| 6120 | == outputDesc->devicesSupportEncodedFormats(sharedDevices.types()))) { |
| 6121 | return false; |
| 6122 | } |
| 6123 | } |
| 6124 | return true; |
| 6125 | } |
| 6126 | |
| 6127 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 6128 | status_t AudioPolicyManager::getSpatializerOutput(const audio_config_base_t *mixerConfig, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6129 | const audio_attributes_t *attr, |
| 6130 | audio_io_handle_t *output) { |
| 6131 | *output = AUDIO_IO_HANDLE_NONE; |
| 6132 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6133 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(*attr, nullptr, false); |
| 6134 | AudioDeviceTypeAddrVector devicesTypeAddress = devices.toTypeAddrVector(); |
| 6135 | audio_config_t *configPtr = nullptr; |
| 6136 | audio_config_t config; |
| 6137 | if (mixerConfig != nullptr) { |
| 6138 | config = audio_config_initializer(mixerConfig); |
| 6139 | configPtr = &config; |
| 6140 | } |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 6141 | if (!canBeSpatializedInt(attr, configPtr, devicesTypeAddress)) { |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6142 | ALOGV("%s provided attributes or mixer config cannot be spatialized", __func__); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6143 | return BAD_VALUE; |
| 6144 | } |
| 6145 | |
| 6146 | sp<IOProfile> profile = |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6147 | getSpatializerOutputProfile(configPtr, devicesTypeAddress); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6148 | if (profile == nullptr) { |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6149 | 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] | 6150 | return BAD_VALUE; |
| 6151 | } |
| 6152 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6153 | std::vector<sp<SwAudioOutputDescriptor>> spatializerOutputs; |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6154 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6155 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6156 | if (!desc->isDuplicated() |
| 6157 | && (desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) { |
| 6158 | spatializerOutputs.push_back(desc); |
| 6159 | ALOGV("%s adding opened spatializer Output %d", __func__, desc->mIoHandle); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6160 | } |
| 6161 | } |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6162 | mSpatializerOutput.clear(); |
| 6163 | bool outputsChanged = false; |
| 6164 | for (const auto& desc : spatializerOutputs) { |
| 6165 | if (desc->mProfile == profile |
| 6166 | && (configPtr == nullptr |
| 6167 | || configPtr->channel_mask == desc->mMixerChannelMask)) { |
| 6168 | mSpatializerOutput = desc; |
| 6169 | ALOGV("%s reusing current spatializer output %d", __func__, desc->mIoHandle); |
| 6170 | } else { |
| 6171 | ALOGV("%s closing spatializerOutput output %d to match channel mask %#x" |
| 6172 | " and devices %s", __func__, desc->mIoHandle, |
| 6173 | configPtr != nullptr ? configPtr->channel_mask : 0, |
| 6174 | devices.toString().c_str()); |
| 6175 | closeOutput(desc->mIoHandle); |
| 6176 | outputsChanged = true; |
| 6177 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6178 | } |
| 6179 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6180 | if (mSpatializerOutput == nullptr) { |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 6181 | sp<SwAudioOutputDescriptor> desc = |
| 6182 | openOutputWithProfileAndDevice(profile, devices, mixerConfig); |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6183 | if (desc != nullptr) { |
| 6184 | mSpatializerOutput = desc; |
| 6185 | outputsChanged = true; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6186 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6187 | } |
| 6188 | |
| 6189 | checkVirtualizerClientRoutes(); |
| 6190 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6191 | if (outputsChanged) { |
| 6192 | mPreviousOutputs = mOutputs; |
| 6193 | mpClientInterface->onAudioPortListUpdate(); |
| 6194 | } |
| 6195 | |
| 6196 | if (mSpatializerOutput == nullptr) { |
| 6197 | ALOGV("%s could not open spatializer output with requested config", __func__); |
| 6198 | return BAD_VALUE; |
| 6199 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6200 | *output = mSpatializerOutput->mIoHandle; |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6201 | ALOGV("%s returning new spatializer output %d", __func__, *output); |
| 6202 | return OK; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6203 | } |
| 6204 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 6205 | status_t AudioPolicyManager::releaseSpatializerOutput(audio_io_handle_t output) { |
| 6206 | if (mSpatializerOutput == nullptr) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6207 | return INVALID_OPERATION; |
| 6208 | } |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 6209 | if (mSpatializerOutput->mIoHandle != output) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6210 | return BAD_VALUE; |
| 6211 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6212 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6213 | if (!isOutputOnlyAvailableRouteToSomeDevice(mSpatializerOutput)) { |
| 6214 | ALOGV("%s closing spatializer output %d", __func__, mSpatializerOutput->mIoHandle); |
| 6215 | closeOutput(mSpatializerOutput->mIoHandle); |
| 6216 | //from now on mSpatializerOutput is null |
| 6217 | checkVirtualizerClientRoutes(); |
| 6218 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6219 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6220 | return NO_ERROR; |
| 6221 | } |
| 6222 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6223 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6224 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6225 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6226 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 6227 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 6228 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6229 | } |
| 6230 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6231 | AudioPolicyManager::AudioPolicyManager(const sp<const AudioPolicyConfig>& config, |
Mikhail Naganov | f1b6d97 | 2023-05-02 13:56:01 -0700 | [diff] [blame] | 6232 | EngineInstance&& engine, |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6233 | AudioPolicyClientInterface *clientInterface) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6234 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 6235 | 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] | 6236 | mConfig(config), |
Mikhail Naganov | f1b6d97 | 2023-05-02 13:56:01 -0700 | [diff] [blame] | 6237 | mEngine(std::move(engine)), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6238 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6239 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6240 | mA2dpSuspended(false), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6241 | mAudioPortGeneration(1), |
| 6242 | mBeaconMuteRefCount(0), |
| 6243 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 6244 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 6245 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6246 | mMasterMono(false), |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 6247 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6248 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6249 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 6250 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6251 | status_t AudioPolicyManager::initialize() { |
Mikhail Naganov | f1b6d97 | 2023-05-02 13:56:01 -0700 | [diff] [blame] | 6252 | if (mEngine == nullptr) { |
| 6253 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6254 | } |
| 6255 | mEngine->setObserver(this); |
| 6256 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6257 | if (status != NO_ERROR) { |
| 6258 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 6259 | return status; |
| 6260 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6261 | |
jiabin | 2923018 | 2023-04-04 21:02:36 +0000 | [diff] [blame] | 6262 | // The actual device selection cache will be updated when calling `updateDevicesAndOutputs` |
| 6263 | // at the end of this function. |
| 6264 | mEngine->initializeDeviceSelectionCache(); |
Eric Laurent | 1d69c87 | 2021-01-11 18:53:01 +0100 | [diff] [blame] | 6265 | mCommunnicationStrategy = mEngine->getProductStrategyForAttributes( |
| 6266 | mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL)); |
| 6267 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6268 | // after parsing the config, mConfig contain all known devices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6269 | // open all output streams needed to access attached devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6270 | onNewAudioModulesAvailableInt(nullptr /*newDevices*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6271 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6272 | // make sure default device is reachable |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6273 | if (const auto defaultOutputDevice = mConfig->getDefaultOutputDevice(); |
| 6274 | defaultOutputDevice == nullptr || |
| 6275 | !mAvailableOutputDevices.contains(defaultOutputDevice)) { |
| 6276 | ALOGE_IF(defaultOutputDevice != nullptr, "Default device %s is unreachable", |
| 6277 | defaultOutputDevice->toString().c_str()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6278 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6279 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 6280 | 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] | 6281 | |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 6282 | // Silence ALOGV statements |
| 6283 | property_set("log.tag." LOG_TAG, "D"); |
| 6284 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6285 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6286 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6287 | } |
| 6288 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6289 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6290 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6291 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6292 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6293 | } |
| 6294 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6295 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6296 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6297 | mAvailableOutputDevices.clear(); |
| 6298 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6299 | mOutputs.clear(); |
| 6300 | mInputs.clear(); |
| 6301 | mHwModules.clear(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6302 | mManualSurroundFormats.clear(); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6303 | mConfig.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6304 | } |
| 6305 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6306 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6307 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 6308 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6309 | } |
| 6310 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6311 | // --- |
| 6312 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6313 | void AudioPolicyManager::onNewAudioModulesAvailable() |
| 6314 | { |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6315 | DeviceVector newDevices; |
| 6316 | onNewAudioModulesAvailableInt(&newDevices); |
| 6317 | if (!newDevices.empty()) { |
| 6318 | nextAudioPortGeneration(); |
| 6319 | mpClientInterface->onAudioPortListUpdate(); |
| 6320 | } |
| 6321 | } |
| 6322 | |
| 6323 | void AudioPolicyManager::onNewAudioModulesAvailableInt(DeviceVector *newDevices) |
| 6324 | { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6325 | for (const auto& hwModule : mConfig->getHwModules()) { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6326 | if (std::find(mHwModules.begin(), mHwModules.end(), hwModule) != mHwModules.end()) { |
| 6327 | continue; |
| 6328 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6329 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
Mikhail Naganov | ffd9771 | 2023-05-03 17:45:36 -0700 | [diff] [blame] | 6330 | if (audio_module_handle_t handle = mpClientInterface->loadHwModule(hwModule->getName()); |
| 6331 | handle != AUDIO_MODULE_HANDLE_NONE) { |
| 6332 | hwModule->setHandle(handle); |
| 6333 | } else { |
| 6334 | ALOGW("could not load HW module %s", hwModule->getName()); |
| 6335 | continue; |
| 6336 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6337 | } |
| 6338 | mHwModules.push_back(hwModule); |
Dean Wheatley | 12a8713 | 2021-04-16 10:08:49 +1000 | [diff] [blame] | 6339 | // open all output streams needed to access attached devices. |
| 6340 | // direct outputs are closed immediately after checking the availability of attached devices |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6341 | // This also validates mAvailableOutputDevices list |
| 6342 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
| 6343 | if (!outProfile->canOpenNewIo()) { |
| 6344 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 6345 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 6346 | continue; |
| 6347 | } |
| 6348 | if (!outProfile->hasSupportedDevices()) { |
| 6349 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
| 6350 | continue; |
| 6351 | } |
Carter Hsu | 1a3364a | 2022-01-21 15:32:56 +0800 | [diff] [blame] | 6352 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0 || |
| 6353 | (outProfile->getFlags() & AUDIO_OUTPUT_FLAG_ULTRASOUND) != 0) { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6354 | mTtsOutputAvailable = true; |
| 6355 | } |
| 6356 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6357 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6358 | DeviceVector availProfileDevices = supportedDevices.filter(mConfig->getOutputDevices()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6359 | sp<DeviceDescriptor> supportedDevice = 0; |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6360 | if (supportedDevices.contains(mConfig->getDefaultOutputDevice())) { |
| 6361 | supportedDevice = mConfig->getDefaultOutputDevice(); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6362 | } else { |
| 6363 | // choose first device present in profile's SupportedDevices also part of |
| 6364 | // mAvailableOutputDevices. |
| 6365 | if (availProfileDevices.isEmpty()) { |
| 6366 | continue; |
| 6367 | } |
| 6368 | supportedDevice = availProfileDevices.itemAt(0); |
| 6369 | } |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6370 | if (!mConfig->getOutputDevices().contains(supportedDevice)) { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6371 | continue; |
| 6372 | } |
| 6373 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 6374 | mpClientInterface); |
| 6375 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 6376 | status_t status = outputDesc->open(nullptr /* halConfig */, nullptr /* mixerConfig */, |
| 6377 | DeviceVector(supportedDevice), |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6378 | AUDIO_STREAM_DEFAULT, |
| 6379 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 6380 | if (status != NO_ERROR) { |
| 6381 | ALOGW("Cannot open output stream for devices %s on hw module %s", |
| 6382 | supportedDevice->toString().c_str(), hwModule->getName()); |
| 6383 | continue; |
| 6384 | } |
| 6385 | for (const auto &device : availProfileDevices) { |
| 6386 | // give a valid ID to an attached device once confirmed it is reachable |
| 6387 | if (!device->isAttached()) { |
| 6388 | device->attach(hwModule); |
| 6389 | mAvailableOutputDevices.add(device); |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 6390 | device->setEncapsulationInfoFromHal(mpClientInterface); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6391 | if (newDevices) newDevices->add(device); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6392 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 6393 | } |
| 6394 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 6395 | if (mPrimaryOutput == nullptr && |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6396 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 6397 | mPrimaryOutput = outputDesc; |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 6398 | mPrimaryModuleHandle = mPrimaryOutput->getModuleHandle(); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6399 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6400 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6401 | outputDesc->close(); |
| 6402 | } else { |
| 6403 | addOutput(output, outputDesc); |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 6404 | setOutputDevices(__func__, outputDesc, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6405 | DeviceVector(supportedDevice), |
| 6406 | true, |
| 6407 | 0, |
| 6408 | NULL); |
| 6409 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6410 | } |
| 6411 | // open input streams needed to access attached devices to validate |
| 6412 | // mAvailableInputDevices list |
| 6413 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
| 6414 | if (!inProfile->canOpenNewIo()) { |
| 6415 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 6416 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 6417 | continue; |
| 6418 | } |
| 6419 | if (!inProfile->hasSupportedDevices()) { |
| 6420 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
| 6421 | continue; |
| 6422 | } |
| 6423 | // chose first device present in profile's SupportedDevices also part of |
| 6424 | // available input devices |
| 6425 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6426 | DeviceVector availProfileDevices = supportedDevices.filter(mConfig->getInputDevices()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6427 | if (availProfileDevices.isEmpty()) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 6428 | ALOGV("%s: Input device list is empty! for profile %s", |
| 6429 | __func__, inProfile->getTagName().c_str()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6430 | continue; |
| 6431 | } |
| 6432 | sp<AudioInputDescriptor> inputDesc = |
| 6433 | new AudioInputDescriptor(inProfile, mpClientInterface); |
| 6434 | |
| 6435 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 6436 | status_t status = inputDesc->open(nullptr, |
| 6437 | availProfileDevices.itemAt(0), |
| 6438 | AUDIO_SOURCE_MIC, |
| 6439 | AUDIO_INPUT_FLAG_NONE, |
| 6440 | &input); |
| 6441 | if (status != NO_ERROR) { |
| 6442 | ALOGW("Cannot open input stream for device %s on hw module %s", |
| 6443 | availProfileDevices.toString().c_str(), |
| 6444 | hwModule->getName()); |
| 6445 | continue; |
| 6446 | } |
| 6447 | for (const auto &device : availProfileDevices) { |
| 6448 | // give a valid ID to an attached device once confirmed it is reachable |
| 6449 | if (!device->isAttached()) { |
| 6450 | device->attach(hwModule); |
| 6451 | device->importAudioPortAndPickAudioProfile(inProfile, true); |
| 6452 | mAvailableInputDevices.add(device); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6453 | if (newDevices) newDevices->add(device); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6454 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 6455 | } |
| 6456 | } |
| 6457 | inputDesc->close(); |
| 6458 | } |
| 6459 | } |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6460 | |
| 6461 | // Check if spatializer outputs can be closed until used. |
| 6462 | // mOutputs vector never contains duplicated outputs at this point. |
| 6463 | std::vector<audio_io_handle_t> outputsClosed; |
| 6464 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6465 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 6466 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0 |
| 6467 | && !isOutputOnlyAvailableRouteToSomeDevice(desc)) { |
| 6468 | outputsClosed.push_back(desc->mIoHandle); |
| 6469 | desc->close(); |
| 6470 | } |
| 6471 | } |
| 6472 | for (auto output : outputsClosed) { |
| 6473 | removeOutput(output); |
| 6474 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6475 | } |
| 6476 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 6477 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 6478 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6479 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6480 | mOutputs.add(output, outputDesc); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6481 | applyStreamVolumes(outputDesc, DeviceTypeSet(), 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 6482 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6483 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6484 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6485 | } |
| 6486 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6487 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 6488 | { |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 6489 | if (mPrimaryOutput != 0 && mPrimaryOutput == mOutputs.valueFor(output)) { |
| 6490 | ALOGV("%s: removing primary output", __func__); |
| 6491 | mPrimaryOutput = nullptr; |
| 6492 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6493 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6494 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6495 | } |
| 6496 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 6497 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 6498 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6499 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6500 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6501 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6502 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6503 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6504 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6505 | audio_policy_dev_state_t state, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6506 | SortedVector<audio_io_handle_t>& outputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6507 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6508 | audio_devices_t deviceType = device->type(); |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 6509 | const String8 &address = String8(device->address().c_str()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6510 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6511 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6512 | if (audio_device_is_digital(deviceType)) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6513 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6514 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6515 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6516 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 6517 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 6518 | // first call getAudioPort to get the supported attributes from the HAL |
| 6519 | struct audio_port_v7 port = {}; |
| 6520 | device->toAudioPort(&port); |
| 6521 | status_t status = mpClientInterface->getAudioPort(&port); |
| 6522 | if (status == NO_ERROR) { |
| 6523 | device->importAudioPort(port); |
| 6524 | } |
| 6525 | |
| 6526 | // then list already open outputs that can be routed to this device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6527 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6528 | desc = mOutputs.valueAt(i); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6529 | if (!desc->isDuplicated() && desc->supportsDevice(device) |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6530 | && desc->devicesSupportEncodedFormats({deviceType})) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6531 | ALOGV("checkOutputsForDevice(): adding opened output %d on device %s", |
| 6532 | mOutputs.keyAt(i), device->toString().c_str()); |
| 6533 | outputs.add(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6534 | } |
| 6535 | } |
| 6536 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6537 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6538 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6539 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 6540 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6541 | if (profile->supportsDevice(device)) { |
| 6542 | profiles.add(profile); |
| 6543 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 6544 | j, hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6545 | } |
| 6546 | } |
| 6547 | } |
| 6548 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 6549 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6550 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6551 | if (profiles.isEmpty() && outputs.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6552 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6553 | return BAD_VALUE; |
| 6554 | } |
| 6555 | |
| 6556 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 6557 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 6558 | 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] | 6559 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6560 | |
| 6561 | // nothing to do if one output is already opened for this profile |
| 6562 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6563 | for (j = 0; j < outputs.size(); j++) { |
| 6564 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6565 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 6566 | // matching profile: save the sample rates, format and channel masks supported |
| 6567 | // by the profile in our device descriptor |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6568 | if (audio_device_is_digital(deviceType)) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6569 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6570 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6571 | break; |
| 6572 | } |
| 6573 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6574 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6575 | continue; |
| 6576 | } |
| 6577 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 6578 | if (!profile->canOpenNewIo()) { |
| 6579 | ALOGW("Max Output number %u already opened for this profile %s", |
| 6580 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 6581 | continue; |
| 6582 | } |
| 6583 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 6584 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 6585 | deviceType, address.c_str(), profile.get(), profile->getName().c_str()); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6586 | desc = openOutputWithProfileAndDevice(profile, DeviceVector(device)); |
| 6587 | 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] | 6588 | if (output == AUDIO_IO_HANDLE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6589 | ALOGW("checkOutputsForDevice() could not open output for device %x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6590 | profiles.removeAt(profile_index); |
| 6591 | profile_index--; |
| 6592 | } else { |
| 6593 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6594 | // Load digital format info only for digital devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6595 | if (audio_device_is_digital(deviceType)) { |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6596 | // TODO: when getAudioPort is ready, it may not be needed to import the audio |
| 6597 | // port but just pick audio profile |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6598 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6599 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 6600 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6601 | if (device_distinguishes_on_address(deviceType)) { |
| 6602 | ALOGV("checkOutputsForDevice(): setOutputDevices %s", |
| 6603 | device->toString().c_str()); |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 6604 | setOutputDevices(__func__, desc, DeviceVector(device), true/*force*/, |
| 6605 | 0/*delay*/, NULL/*patch handle*/); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6606 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6607 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 6608 | } |
| 6609 | } |
| 6610 | |
| 6611 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6612 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6613 | return BAD_VALUE; |
| 6614 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6615 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6616 | // check if one opened output is not needed any more after disconnecting one device |
| 6617 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6618 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6619 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6620 | // exact match on device |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6621 | if (device_distinguishes_on_address(deviceType) && desc->supportsDevice(device) |
Francois Gaffie | c7d4c22 | 2021-12-02 11:12:52 +0100 | [diff] [blame] | 6622 | && desc->containsSingleDeviceSupportingEncodedFormats(device)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6623 | outputs.add(mOutputs.keyAt(i)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6624 | } else if (!mAvailableOutputDevices.containsAtLeastOne(desc->supportedDevices())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6625 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 6626 | mOutputs.keyAt(i)); |
| 6627 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6628 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6629 | } |
| 6630 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6631 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6632 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6633 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 6634 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6635 | if (!profile->supportsDevice(device)) { |
| 6636 | continue; |
| 6637 | } |
| 6638 | ALOGV("checkOutputsForDevice(): " |
| 6639 | "clearing direct output profile %zu on module %s", |
| 6640 | j, hwModule->getName()); |
| 6641 | profile->clearAudioProfiles(); |
| 6642 | if (!profile->hasDynamicAudioProfile()) { |
| 6643 | continue; |
| 6644 | } |
| 6645 | // When a device is disconnected, if there is an IOProfile that contains dynamic |
| 6646 | // profiles and supports the disconnected device, call getAudioPort to repopulate |
| 6647 | // the capabilities of the devices that is supported by the IOProfile. |
| 6648 | for (const auto& supportedDevice : profile->getSupportedDevices()) { |
| 6649 | if (supportedDevice == device || |
| 6650 | !mAvailableOutputDevices.contains(supportedDevice)) { |
| 6651 | continue; |
| 6652 | } |
| 6653 | struct audio_port_v7 port; |
| 6654 | supportedDevice->toAudioPort(&port); |
| 6655 | status_t status = mpClientInterface->getAudioPort(&port); |
| 6656 | if (status == NO_ERROR) { |
| 6657 | supportedDevice->importAudioPort(port); |
| 6658 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6659 | } |
| 6660 | } |
| 6661 | } |
| 6662 | } |
| 6663 | return NO_ERROR; |
| 6664 | } |
| 6665 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6666 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& device, |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6667 | audio_policy_dev_state_t state) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6668 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6669 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6670 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6671 | if (audio_device_is_digital(device->type())) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6672 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6673 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6674 | } |
| 6675 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6676 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
jiabin | bf5f426 | 2023-04-12 21:48:34 +0000 | [diff] [blame] | 6677 | // first call getAudioPort to get the supported attributes from the HAL |
| 6678 | struct audio_port_v7 port = {}; |
| 6679 | device->toAudioPort(&port); |
| 6680 | status_t status = mpClientInterface->getAudioPort(&port); |
| 6681 | if (status == NO_ERROR) { |
| 6682 | device->importAudioPort(port); |
| 6683 | } |
| 6684 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6685 | // look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6686 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6687 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6688 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6689 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6690 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6691 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6692 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6693 | if (profile->supportsDevice(device)) { |
| 6694 | profiles.add(profile); |
| 6695 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 6696 | profile_index, hwModule->getName()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6697 | } |
| 6698 | } |
| 6699 | } |
| 6700 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6701 | if (profiles.isEmpty()) { |
| 6702 | ALOGW("%s: No input profile available for device %s", |
| 6703 | __func__, device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6704 | return BAD_VALUE; |
| 6705 | } |
| 6706 | |
| 6707 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 6708 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 6709 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 6710 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6711 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 6712 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6713 | // nothing to do if one input is already opened for this profile |
| 6714 | size_t input_index; |
| 6715 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 6716 | desc = mInputs.valueAt(input_index); |
| 6717 | if (desc->mProfile == profile) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6718 | if (audio_device_is_digital(device->type())) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6719 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6720 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6721 | break; |
| 6722 | } |
| 6723 | } |
| 6724 | if (input_index != mInputs.size()) { |
| 6725 | continue; |
| 6726 | } |
| 6727 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 6728 | if (!profile->canOpenNewIo()) { |
| 6729 | ALOGW("Max Input number %u already opened for this profile %s", |
| 6730 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 6731 | continue; |
| 6732 | } |
| 6733 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6734 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6735 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
jiabin | bf5f426 | 2023-04-12 21:48:34 +0000 | [diff] [blame] | 6736 | 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] | 6737 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6738 | if (status == NO_ERROR) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 6739 | const String8& address = String8(device->address().c_str()); |
Tomasz Wasilczyk | fd9ffd1 | 2023-08-14 17:56:22 +0000 | [diff] [blame] | 6740 | if (!address.empty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6741 | char *param = audio_device_address_to_parameter(device->type(), address); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6742 | mpClientInterface->setParameters(input, String8(param)); |
| 6743 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6744 | } |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 6745 | updateAudioProfiles(device, input, profile); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6746 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6747 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6748 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6749 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6750 | } |
| 6751 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6752 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6753 | addInput(input, desc); |
| 6754 | } |
| 6755 | } // endif input != 0 |
| 6756 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6757 | if (input == AUDIO_IO_HANDLE_NONE) { |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 6758 | ALOGW("%s could not open input for device %s", __func__, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6759 | device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6760 | profiles.removeAt(profile_index); |
| 6761 | profile_index--; |
| 6762 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6763 | if (audio_device_is_digital(device->type())) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6764 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6765 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6766 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 6767 | } |
| 6768 | } // end scan profiles |
| 6769 | |
| 6770 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6771 | 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] | 6772 | return BAD_VALUE; |
| 6773 | } |
| 6774 | } else { |
| 6775 | // Disconnect |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6776 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 6777 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6778 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6779 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6780 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6781 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6782 | if (profile->supportsDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 6783 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 6784 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6785 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6786 | } |
| 6787 | } |
| 6788 | } |
| 6789 | } // end disconnect |
| 6790 | |
| 6791 | return NO_ERROR; |
| 6792 | } |
| 6793 | |
| 6794 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6795 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6796 | { |
| 6797 | ALOGV("closeOutput(%d)", output); |
| 6798 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6799 | sp<SwAudioOutputDescriptor> closingOutput = mOutputs.valueFor(output); |
| 6800 | if (closingOutput == NULL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6801 | ALOGW("closeOutput() unknown output %d", output); |
| 6802 | return; |
| 6803 | } |
Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 6804 | const bool closingOutputWasActive = closingOutput->isActive(); |
jiabin | 24ff57a | 2023-11-27 21:06:51 +0000 | [diff] [blame] | 6805 | mPolicyMixes.closeOutput(closingOutput, mOutputs); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6806 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6807 | // look for duplicated outputs connected to the output being removed. |
| 6808 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6809 | sp<SwAudioOutputDescriptor> dupOutput = mOutputs.valueAt(i); |
| 6810 | if (dupOutput->isDuplicated() && |
| 6811 | (dupOutput->mOutput1 == closingOutput || dupOutput->mOutput2 == closingOutput)) { |
| 6812 | sp<SwAudioOutputDescriptor> remainingOutput = |
| 6813 | dupOutput->mOutput1 == closingOutput ? dupOutput->mOutput2 : dupOutput->mOutput1; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6814 | // As all active tracks on duplicated output will be deleted, |
| 6815 | // and as they were also referenced on the other output, the reference |
| 6816 | // count for their stream type must be adjusted accordingly on |
| 6817 | // the other output. |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6818 | const bool wasActive = remainingOutput->isActive(); |
| 6819 | // Note: no-op on the closing output where all clients has already been set inactive |
| 6820 | dupOutput->setAllClientsInactive(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 6821 | // stop() will be a no op if the output is still active but is needed in case all |
| 6822 | // active streams refcounts where cleared above |
| 6823 | if (wasActive) { |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6824 | remainingOutput->stop(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 6825 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6826 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 6827 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 6828 | |
| 6829 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6830 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6831 | } |
| 6832 | } |
| 6833 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6834 | nextAudioPortGeneration(); |
| 6835 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6836 | ssize_t index = mAudioPatches.indexOfKey(closingOutput->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6837 | if (index >= 0) { |
| 6838 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6839 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 6840 | patchDesc->getAfHandle(), 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6841 | mAudioPatches.removeItemsAt(index); |
| 6842 | mpClientInterface->onAudioPatchListUpdate(); |
| 6843 | } |
| 6844 | |
Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 6845 | if (closingOutputWasActive) { |
| 6846 | closingOutput->stop(); |
| 6847 | } |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6848 | closingOutput->close(); |
jiabin | 14b50cc | 2023-12-13 19:01:52 +0000 | [diff] [blame] | 6849 | if ((closingOutput->getFlags().output & AUDIO_OUTPUT_FLAG_BIT_PERFECT) |
| 6850 | == AUDIO_OUTPUT_FLAG_BIT_PERFECT) { |
| 6851 | for (const auto device : closingOutput->devices()) { |
| 6852 | device->setPreferredConfig(nullptr); |
| 6853 | } |
| 6854 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6855 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6856 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6857 | mPreviousOutputs = mOutputs; |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 6858 | if (closingOutput == mSpatializerOutput) { |
| 6859 | mSpatializerOutput.clear(); |
| 6860 | } |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6861 | |
| 6862 | // MSD patches may have been released to support a non-MSD direct output. Reset MSD patch if |
| 6863 | // no direct outputs are open. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6864 | if (!getMsdAudioOutDevices().isEmpty()) { |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6865 | bool directOutputOpen = false; |
| 6866 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6867 | if (mOutputs[i]->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 6868 | directOutputOpen = true; |
| 6869 | break; |
| 6870 | } |
| 6871 | } |
| 6872 | if (!directOutputOpen) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 6873 | ALOGV("no direct outputs open, reset MSD patches"); |
| 6874 | // TODO: The MSD patches to be established here may differ to current MSD patches due to |
| 6875 | // how output devices for patching are resolved. Avoid by caching and reusing the |
| 6876 | // arguments to mEngine->getOutputDevicesForAttributes() when resolving which output |
| 6877 | // devices to patch to. This may be complicated by the fact that devices may become |
| 6878 | // unavailable. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 6879 | setMsdOutputPatches(); |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6880 | } |
| 6881 | } |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6882 | } |
| 6883 | |
| 6884 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 6885 | { |
| 6886 | ALOGV("closeInput(%d)", input); |
| 6887 | |
| 6888 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 6889 | if (inputDesc == NULL) { |
| 6890 | ALOGW("closeInput() unknown input %d", input); |
| 6891 | return; |
| 6892 | } |
| 6893 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6894 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6895 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6896 | sp<DeviceDescriptor> device = inputDesc->getDevice(); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 6897 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6898 | if (index >= 0) { |
| 6899 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6900 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 6901 | patchDesc->getAfHandle(), 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6902 | mAudioPatches.removeItemsAt(index); |
| 6903 | mpClientInterface->onAudioPatchListUpdate(); |
| 6904 | } |
| 6905 | |
François Gaffie | 6ebbce0 | 2023-07-19 13:27:53 +0200 | [diff] [blame] | 6906 | mEffects.putOrphanEffectsForIo(input); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6907 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6908 | mInputs.removeItem(input); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6909 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6910 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 6911 | if (primaryInputDevices.contains(device) && |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6912 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 6913 | mpClientInterface->setSoundTriggerCaptureState(false); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6914 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6915 | } |
| 6916 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6917 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevices( |
| 6918 | const DeviceVector &devices, |
| 6919 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6920 | { |
| 6921 | SortedVector<audio_io_handle_t> outputs; |
| 6922 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6923 | ALOGVV("%s() devices %s", __func__, devices.toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6924 | for (size_t i = 0; i < openOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6925 | ALOGVV("output %zu isDuplicated=%d device=%s", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 6926 | i, openOutputs.valueAt(i)->isDuplicated(), |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6927 | openOutputs.valueAt(i)->supportedDevices().toString().c_str()); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6928 | if (openOutputs.valueAt(i)->supportsAllDevices(devices) |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6929 | && openOutputs.valueAt(i)->devicesSupportEncodedFormats(devices.types())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6930 | ALOGVV("%s() found output %d", __func__, openOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6931 | outputs.add(openOutputs.keyAt(i)); |
| 6932 | } |
| 6933 | } |
| 6934 | return outputs; |
| 6935 | } |
| 6936 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6937 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 6938 | { |
| 6939 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 6940 | // output is suspended before any tracks are moved to it |
| 6941 | checkA2dpSuspend(); |
| 6942 | checkOutputForAllStrategies(); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6943 | checkSecondaryOutputs(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 6944 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6945 | updateDevicesAndOutputs(); |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 6946 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 6947 | // TODO: The MSD patches to be established here may differ to current MSD patches due to how |
| 6948 | // output devices for patching are resolved. Nevertheless, AudioTracks affected by device |
| 6949 | // configuration changes will ultimately be rerouted correctly. We can still avoid |
| 6950 | // unnecessary rerouting by caching and reusing the arguments to |
| 6951 | // mEngine->getOutputDevicesForAttributes() when resolving which output devices to patch to. |
| 6952 | // This may be complicated by the fact that devices may become unavailable. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 6953 | setMsdOutputPatches(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 6954 | } |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 6955 | // an event that changed routing likely occurred, inform upper layers |
| 6956 | mpClientInterface->onRoutingUpdated(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6957 | } |
| 6958 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6959 | bool AudioPolicyManager::followsSameRouting(const audio_attributes_t &lAttr, |
| 6960 | const audio_attributes_t &rAttr) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6961 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6962 | return mEngine->getProductStrategyForAttributes(lAttr) == |
| 6963 | mEngine->getProductStrategyForAttributes(rAttr); |
| 6964 | } |
| 6965 | |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6966 | void AudioPolicyManager::checkAudioSourceForAttributes(const audio_attributes_t &attr) |
| 6967 | { |
| 6968 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 6969 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 6970 | if (sourceDesc != nullptr && followsSameRouting(attr, sourceDesc->attributes()) |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 6971 | && sourceDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6972 | && !isCallRxAudioSource(sourceDesc) && !sourceDesc->isInternal()) { |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6973 | connectAudioSource(sourceDesc); |
| 6974 | } |
| 6975 | } |
| 6976 | } |
| 6977 | |
| 6978 | void AudioPolicyManager::clearAudioSourcesForOutput(audio_io_handle_t output) |
| 6979 | { |
| 6980 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 6981 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 6982 | if (sourceDesc != nullptr && sourceDesc->swOutput().promote() != nullptr |
| 6983 | && sourceDesc->swOutput().promote()->mIoHandle == output) { |
| 6984 | disconnectAudioSource(sourceDesc); |
| 6985 | } |
| 6986 | } |
| 6987 | } |
| 6988 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6989 | void AudioPolicyManager::checkOutputForAttributes(const audio_attributes_t &attr) |
| 6990 | { |
| 6991 | auto psId = mEngine->getProductStrategyForAttributes(attr); |
| 6992 | |
| 6993 | DeviceVector oldDevices = mEngine->getOutputDevicesForAttributes(attr, 0, true /*fromCache*/); |
| 6994 | DeviceVector newDevices = mEngine->getOutputDevicesForAttributes(attr, 0, false /*fromCache*/); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 6995 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6996 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevices(oldDevices, mPreviousOutputs); |
| 6997 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevices(newDevices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6998 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6999 | uint32_t maxLatency = 0; |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 7000 | bool unneededUsePrimaryOutputFromPolicyMixes = false; |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 7001 | std::vector<sp<SwAudioOutputDescriptor>> invalidatedOutputs; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 7002 | // take into account dynamic audio policies related changes: if a client is now associated |
| 7003 | // to a different policy mix than at creation time, invalidate corresponding stream |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 7004 | for (size_t i = 0; i < mPreviousOutputs.size(); i++) { |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 7005 | const sp<SwAudioOutputDescriptor>& desc = mPreviousOutputs.valueAt(i); |
| 7006 | if (desc->isDuplicated()) { |
| 7007 | continue; |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 7008 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 7009 | for (const sp<TrackClientDescriptor>& client : desc->getClientIterable()) { |
| 7010 | if (mEngine->getProductStrategyForAttributes(client->attributes()) != psId) { |
| 7011 | continue; |
| 7012 | } |
| 7013 | sp<AudioPolicyMix> primaryMix; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 7014 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->config(), |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 7015 | client->uid(), client->session(), client->flags(), mAvailableOutputDevices, |
| 7016 | nullptr /* requestedDevice */, primaryMix, nullptr /* secondaryMixes */, |
| 7017 | unneededUsePrimaryOutputFromPolicyMixes); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 7018 | if (status != OK) { |
| 7019 | continue; |
| 7020 | } |
yucliu | f4de36d | 2020-09-14 14:57:56 -0700 | [diff] [blame] | 7021 | if (client->getPrimaryMix() != primaryMix || client->hasLostPrimaryMix()) { |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 7022 | if (desc->isStrategyActive(psId) && maxLatency < desc->latency()) { |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 7023 | maxLatency = desc->latency(); |
| 7024 | } |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 7025 | invalidatedOutputs.push_back(desc); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 7026 | } |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 7027 | } |
| 7028 | } |
| 7029 | |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 7030 | if (srcOutputs != dstOutputs || !invalidatedOutputs.empty()) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 7031 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 7032 | // audio from invalidated tracks will be rendered when unmuting |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 7033 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 7034 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 7035 | if (desc == nullptr) continue; |
| 7036 | |
| 7037 | if (desc->isStrategyActive(psId) && maxLatency < desc->latency()) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 7038 | maxLatency = desc->latency(); |
| 7039 | } |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 7040 | |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 7041 | bool invalidate = false; |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 7042 | for (auto client : desc->clientsList(false /*activeOnly*/)) { |
Eric Laurent | 78aade8 | 2019-09-13 18:55:08 -0700 | [diff] [blame] | 7043 | if (desc->isDuplicated() || !desc->mProfile->isDirectOutput()) { |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 7044 | // a client on a non direct outputs has necessarily a linear PCM format |
| 7045 | // so we can call selectOutput() safely |
| 7046 | const audio_io_handle_t newOutput = selectOutput(dstOutputs, |
| 7047 | client->flags(), |
| 7048 | client->config().format, |
| 7049 | client->config().channel_mask, |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 7050 | client->config().sample_rate, |
| 7051 | client->session()); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 7052 | if (newOutput != srcOut) { |
| 7053 | invalidate = true; |
| 7054 | break; |
| 7055 | } |
| 7056 | } else { |
| 7057 | sp<IOProfile> profile = getProfileForOutput(newDevices, |
| 7058 | client->config().sample_rate, |
| 7059 | client->config().format, |
| 7060 | client->config().channel_mask, |
| 7061 | client->flags(), |
| 7062 | true /* directOnly */); |
| 7063 | if (profile != desc->mProfile) { |
| 7064 | invalidate = true; |
| 7065 | break; |
| 7066 | } |
| 7067 | } |
| 7068 | } |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 7069 | // mute strategy while moving tracks from one output to another |
| 7070 | if (invalidate) { |
| 7071 | invalidatedOutputs.push_back(desc); |
| 7072 | if (desc->isStrategyActive(psId)) { |
| 7073 | setStrategyMute(psId, true, desc); |
| 7074 | setStrategyMute(psId, false, desc, maxLatency * LATENCY_MUTE_FACTOR, |
| 7075 | newDevices.types()); |
| 7076 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7077 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7078 | sp<SourceClientDescriptor> source = getSourceForAttributesOnOutput(srcOut, attr); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 7079 | if (source != nullptr && !isCallRxAudioSource(source) && !source->isInternal()) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7080 | connectAudioSource(source); |
| 7081 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7082 | } |
| 7083 | |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 7084 | ALOGV_IF(!(srcOutputs.isEmpty() || dstOutputs.isEmpty()), |
| 7085 | "%s: strategy %d, moving from output %s to output %s", __func__, psId, |
| 7086 | std::to_string(srcOutputs[0]).c_str(), |
| 7087 | std::to_string(dstOutputs[0]).c_str()); |
| 7088 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7089 | // Move effects associated to this stream from previous output to new output |
| 7090 | if (followsSameRouting(attr, attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 7091 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7092 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7093 | // 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] | 7094 | if (!invalidatedOutputs.empty()) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 7095 | invalidateStreams(mEngine->getStreamTypesForProductStrategy(psId)); |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 7096 | for (sp<SwAudioOutputDescriptor> desc : invalidatedOutputs) { |
jiabin | 4925685 | 2022-03-09 11:21:35 -0800 | [diff] [blame] | 7097 | desc->setTracksInvalidatedStatusByStrategy(psId); |
| 7098 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7099 | } |
| 7100 | } |
| 7101 | } |
| 7102 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7103 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7104 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7105 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { |
| 7106 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); |
| 7107 | checkOutputForAttributes(attributes); |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 7108 | checkAudioSourceForAttributes(attributes); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7109 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7110 | } |
| 7111 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 7112 | void AudioPolicyManager::checkSecondaryOutputs() { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 7113 | PortHandleVector clientsToInvalidate; |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 7114 | TrackSecondaryOutputsMap trackSecondaryOutputs; |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 7115 | bool unneededUsePrimaryOutputFromPolicyMixes = false; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 7116 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 7117 | const sp<SwAudioOutputDescriptor>& outputDescriptor = mOutputs[i]; |
| 7118 | for (const sp<TrackClientDescriptor>& client : outputDescriptor->getClientIterable()) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 7119 | sp<AudioPolicyMix> primaryMix; |
| 7120 | std::vector<sp<AudioPolicyMix>> secondaryMixes; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 7121 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->config(), |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 7122 | client->uid(), client->session(), client->flags(), mAvailableOutputDevices, |
| 7123 | nullptr /* requestedDevice */, primaryMix, &secondaryMixes, |
| 7124 | unneededUsePrimaryOutputFromPolicyMixes); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 7125 | std::vector<sp<SwAudioOutputDescriptor>> secondaryDescs; |
| 7126 | for (auto &secondaryMix : secondaryMixes) { |
| 7127 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); |
| 7128 | if (outputDesc != nullptr && |
| 7129 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { |
| 7130 | secondaryDescs.push_back(outputDesc); |
| 7131 | } |
| 7132 | } |
| 7133 | |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 7134 | if (status != OK && |
| 7135 | (client->flags() & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == AUDIO_OUTPUT_FLAG_NONE) { |
| 7136 | // When it failed to query secondary output, only invalidate the client that is not |
| 7137 | // MMAP. The reason is that MMAP stream will not support secondary output. |
| 7138 | clientsToInvalidate.push_back(client->portId()); |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 7139 | } else if (!std::equal( |
| 7140 | client->getSecondaryOutputs().begin(), |
| 7141 | client->getSecondaryOutputs().end(), |
| 7142 | secondaryDescs.begin(), secondaryDescs.end())) { |
jiabin | a528106 | 2021-11-23 00:10:23 +0000 | [diff] [blame] | 7143 | if (!audio_is_linear_pcm(client->config().format)) { |
| 7144 | // If the format is not PCM, the tracks should be invalidated to get correct |
| 7145 | // behavior when the secondary output is changed. |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 7146 | clientsToInvalidate.push_back(client->portId()); |
jiabin | a528106 | 2021-11-23 00:10:23 +0000 | [diff] [blame] | 7147 | } else { |
| 7148 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryDescs; |
| 7149 | std::vector<audio_io_handle_t> secondaryOutputIds; |
| 7150 | for (const auto &secondaryDesc: secondaryDescs) { |
| 7151 | secondaryOutputIds.push_back(secondaryDesc->mIoHandle); |
| 7152 | weakSecondaryDescs.push_back(secondaryDesc); |
| 7153 | } |
| 7154 | trackSecondaryOutputs.emplace(client->portId(), secondaryOutputIds); |
| 7155 | client->setSecondaryOutputs(std::move(weakSecondaryDescs)); |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 7156 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 7157 | } |
| 7158 | } |
| 7159 | } |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 7160 | if (!trackSecondaryOutputs.empty()) { |
| 7161 | mpClientInterface->updateSecondaryOutputs(trackSecondaryOutputs); |
| 7162 | } |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 7163 | if (!clientsToInvalidate.empty()) { |
| 7164 | ALOGD("%s Invalidate clients due to fail getting output for attr", __func__); |
| 7165 | mpClientInterface->invalidateTracks(clientsToInvalidate); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 7166 | } |
| 7167 | } |
| 7168 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7169 | bool AudioPolicyManager::isScoRequestedForComm() const { |
| 7170 | AudioDeviceTypeAddrVector devices; |
| 7171 | mEngine->getDevicesForRoleAndStrategy(mCommunnicationStrategy, DEVICE_ROLE_PREFERRED, devices); |
| 7172 | for (const auto &device : devices) { |
| 7173 | if (audio_is_bluetooth_out_sco_device(device.mType)) { |
| 7174 | return true; |
| 7175 | } |
| 7176 | } |
| 7177 | return false; |
| 7178 | } |
| 7179 | |
Eric Laurent | 1a8b45f | 2022-04-13 16:01:47 +0200 | [diff] [blame] | 7180 | bool AudioPolicyManager::isHearingAidUsedForComm() const { |
| 7181 | DeviceVector devices = mEngine->getOutputDevicesForStream(AUDIO_STREAM_VOICE_CALL, |
| 7182 | true /*fromCache*/); |
| 7183 | for (const auto &device : devices) { |
| 7184 | if (device->type() == AUDIO_DEVICE_OUT_HEARING_AID) { |
| 7185 | return true; |
| 7186 | } |
| 7187 | } |
| 7188 | return false; |
| 7189 | } |
| 7190 | |
| 7191 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7192 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7193 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7194 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 7195 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7196 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7197 | return; |
| 7198 | } |
| 7199 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7200 | bool isScoConnected = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7201 | (mAvailableInputDevices.types().count(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) != 0 || |
| 7202 | !Intersection(mAvailableOutputDevices.types(), getAudioDeviceOutAllScoSet()).empty()); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7203 | bool isScoRequested = isScoRequestedForComm(); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7204 | |
| 7205 | // if suspended, restore A2DP output if: |
| 7206 | // ((SCO device is NOT connected) || |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7207 | // ((SCO is not requested) && |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7208 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7209 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7210 | // if not suspended, suspend A2DP output if: |
| 7211 | // (SCO device is connected) && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7212 | // ((SCO is requested) || |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7213 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7214 | // |
| 7215 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7216 | if (!isScoConnected || |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7217 | (!isScoRequested && |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7218 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7219 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7220 | |
| 7221 | mpClientInterface->restoreOutput(a2dpOutput); |
| 7222 | mA2dpSuspended = false; |
| 7223 | } |
| 7224 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7225 | if (isScoConnected && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7226 | (isScoRequested || |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7227 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7228 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7229 | |
| 7230 | mpClientInterface->suspendOutput(a2dpOutput); |
| 7231 | mA2dpSuspended = true; |
| 7232 | } |
| 7233 | } |
| 7234 | } |
| 7235 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7236 | DeviceVector AudioPolicyManager::getNewOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 7237 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7238 | { |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 7239 | if (outputDesc == nullptr) { |
| 7240 | return DeviceVector{}; |
| 7241 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7242 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 7243 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7244 | if (index >= 0) { |
| 7245 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7246 | if (patchDesc->getUid() != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7247 | ALOGV("%s device %s forced by patch %d", __func__, |
| 7248 | outputDesc->devices().toString().c_str(), outputDesc->getPatchHandle()); |
| 7249 | return outputDesc->devices(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7250 | } |
| 7251 | } |
| 7252 | |
Dean Wheatley | 514b431 | 2020-06-17 21:45:00 +1000 | [diff] [blame] | 7253 | // Do not retrieve engine device for outputs through MSD |
| 7254 | // TODO: support explicit routing requests by resetting MSD patch to engine device. |
| 7255 | if (outputDesc->devices() == getMsdAudioOutDevices()) { |
| 7256 | return outputDesc->devices(); |
| 7257 | } |
| 7258 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 7259 | // Honor explicit routing requests only if no client using default routing is active on this |
| 7260 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 7261 | bool active; // unused |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7262 | sp<DeviceDescriptor> device = |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7263 | findPreferredDevice(outputDesc, PRODUCT_STRATEGY_NONE, active, mAvailableOutputDevices); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7264 | if (device != nullptr) { |
| 7265 | return DeviceVector(device); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 7266 | } |
| 7267 | |
François Gaffie | a807ef9 | 2018-11-05 10:44:33 +0100 | [diff] [blame] | 7268 | // Legacy Engine cannot take care of bus devices and mix, so we need to handle the conflict |
| 7269 | // of setForceUse / Default Bus device here |
| 7270 | device = mPolicyMixes.getDeviceAndMixForOutput(outputDesc, mAvailableOutputDevices); |
| 7271 | if (device != nullptr) { |
| 7272 | return DeviceVector(device); |
| 7273 | } |
| 7274 | |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 7275 | DeviceVector devices; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7276 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { |
| 7277 | StreamTypeVector streams = mEngine->getStreamTypesForProductStrategy(productStrategy); |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7278 | auto hasStreamActive = [&](auto stream) { |
| 7279 | return hasStream(streams, stream) && isStreamActive(stream, 0); |
| 7280 | }; |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 7281 | |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7282 | auto doGetOutputDevicesForVoice = [&]() { |
| 7283 | return hasVoiceStream(streams) && (outputDesc == mPrimaryOutput || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7284 | outputDesc->isActive(toVolumeSource(AUDIO_STREAM_VOICE_CALL, false))) && |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7285 | (isInCall() || |
Henrik Backlund | 07c654a | 2021-10-14 15:57:10 +0200 | [diff] [blame] | 7286 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc)) && |
| 7287 | !isStreamActive(AUDIO_STREAM_ENFORCED_AUDIBLE, 0); |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7288 | }; |
| 7289 | |
| 7290 | // With low-latency playing on speaker, music on WFD, when the first low-latency |
| 7291 | // output is stopped, getNewOutputDevices checks for a product strategy |
| 7292 | // from the list, as STRATEGY_SONIFICATION comes prior to STRATEGY_MEDIA. |
Carter Hsu | cc58a6b | 2021-07-20 08:44:50 +0000 | [diff] [blame] | 7293 | // 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] | 7294 | // devices are returned for STRATEGY_SONIFICATION without checking whether the |
| 7295 | // stream is associated to the output descriptor. |
| 7296 | if (doGetOutputDevicesForVoice() || outputDesc->isStrategyActive(productStrategy) || |
| 7297 | ((hasStreamActive(AUDIO_STREAM_ALARM) || |
| 7298 | hasStreamActive(AUDIO_STREAM_ENFORCED_AUDIBLE)) && |
| 7299 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc))) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7300 | // Retrieval of devices for voice DL is done on primary output profile, cannot |
| 7301 | // check the route (would force modifying configuration file for this profile) |
jiangyao | 9478094 | 2024-03-05 10:43:14 +0800 | [diff] [blame] | 7302 | auto attr = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7303 | devices = mEngine->getOutputDevicesForAttributes(attr, nullptr, fromCache); |
| 7304 | break; |
| 7305 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7306 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7307 | ALOGV("%s selected devices %s", __func__, devices.toString().c_str()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7308 | return devices; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7309 | } |
| 7310 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7311 | sp<DeviceDescriptor> AudioPolicyManager::getNewInputDevice( |
| 7312 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7313 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7314 | sp<DeviceDescriptor> device; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7315 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 7316 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7317 | if (index >= 0) { |
| 7318 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7319 | if (patchDesc->getUid() != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7320 | ALOGV("getNewInputDevice() device %s forced by patch %d", |
| 7321 | inputDesc->getDevice()->toString().c_str(), inputDesc->getPatchHandle()); |
| 7322 | return inputDesc->getDevice(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7323 | } |
| 7324 | } |
| 7325 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 7326 | // Honor explicit routing requests only if no client using default routing is active on this |
| 7327 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 7328 | bool active; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7329 | device = findPreferredDevice(inputDesc, AUDIO_SOURCE_DEFAULT, active, mAvailableInputDevices); |
| 7330 | if (device != nullptr) { |
| 7331 | return device; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 7332 | } |
| 7333 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 7334 | // 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] | 7335 | // a null sp<>, causing the patch on the input stream to be released. |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7336 | audio_attributes_t attributes; |
| 7337 | uid_t uid; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7338 | audio_session_t session; |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7339 | sp<RecordClientDescriptor> topClient = inputDesc->getHighestPriorityClient(); |
| 7340 | if (topClient != nullptr) { |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 7341 | attributes = topClient->attributes(); |
| 7342 | uid = topClient->uid(); |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7343 | session = topClient->session(); |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7344 | } else { |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 7345 | attributes = { .source = AUDIO_SOURCE_DEFAULT }; |
| 7346 | uid = 0; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7347 | session = AUDIO_SESSION_NONE; |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7348 | } |
| 7349 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7350 | if (attributes.source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 7351 | attributes.source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 7352 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7353 | if (attributes.source != AUDIO_SOURCE_DEFAULT) { |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7354 | device = mEngine->getInputDeviceForAttributes(attributes, uid, session); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 7355 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7356 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7357 | return device; |
| 7358 | } |
| 7359 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 7360 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 7361 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 7362 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 7363 | } |
| 7364 | |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 7365 | status_t AudioPolicyManager::getDevicesForAttributes( |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7366 | const audio_attributes_t &attr, AudioDeviceTypeAddrVector *devices, bool forVolume) { |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 7367 | if (devices == nullptr) { |
| 7368 | return BAD_VALUE; |
| 7369 | } |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7370 | |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7371 | DeviceVector curDevices; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 7372 | if (status_t status = getDevicesForAttributes(attr, curDevices, forVolume); status != OK) { |
| 7373 | return status; |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7374 | } |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 7375 | for (const auto& device : curDevices) { |
| 7376 | devices->push_back(device->getDeviceTypeAddr()); |
| 7377 | } |
| 7378 | return NO_ERROR; |
| 7379 | } |
| 7380 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7381 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7382 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 7383 | case AUDIO_STREAM_MUSIC: |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7384 | checkOutputForAttributes(attributes_initializer(AUDIO_USAGE_NOTIFICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7385 | updateDevicesAndOutputs(); |
| 7386 | break; |
| 7387 | default: |
| 7388 | break; |
| 7389 | } |
| 7390 | } |
| 7391 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7392 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 7393 | |
| 7394 | // skip beacon mute management if a dedicated TTS output is available |
| 7395 | if (mTtsOutputAvailable) { |
| 7396 | return 0; |
| 7397 | } |
| 7398 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7399 | switch(event) { |
| 7400 | case STARTING_OUTPUT: |
| 7401 | mBeaconMuteRefCount++; |
| 7402 | break; |
| 7403 | case STOPPING_OUTPUT: |
| 7404 | if (mBeaconMuteRefCount > 0) { |
| 7405 | mBeaconMuteRefCount--; |
| 7406 | } |
| 7407 | break; |
| 7408 | case STARTING_BEACON: |
| 7409 | mBeaconPlayingRefCount++; |
| 7410 | break; |
| 7411 | case STOPPING_BEACON: |
| 7412 | if (mBeaconPlayingRefCount > 0) { |
| 7413 | mBeaconPlayingRefCount--; |
| 7414 | } |
| 7415 | break; |
| 7416 | } |
| 7417 | |
| 7418 | if (mBeaconMuteRefCount > 0) { |
| 7419 | // any playback causes beacon to be muted |
| 7420 | return setBeaconMute(true); |
| 7421 | } else { |
| 7422 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 7423 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 7424 | } |
| 7425 | } |
| 7426 | |
| 7427 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 7428 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 7429 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 7430 | // keep track of muted state to avoid repeating mute/unmute operations |
| 7431 | if (mBeaconMuted != mute) { |
| 7432 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 7433 | ALOGV("\t muting %d", mute); |
| 7434 | uint32_t maxLatency = 0; |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7435 | auto ttsVolumeSource = toVolumeSource(AUDIO_STREAM_TTS, false); |
| 7436 | if (ttsVolumeSource == VOLUME_SOURCE_NONE) { |
| 7437 | ALOGV("\t no tts volume source available"); |
| 7438 | return 0; |
| 7439 | } |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7440 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7441 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7442 | setVolumeSourceMute(ttsVolumeSource, mute/*on*/, desc, 0 /*delay*/, DeviceTypeSet()); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7443 | const uint32_t latency = desc->latency() * 2; |
Eric Laurent | cdb2b35 | 2020-07-23 10:57:02 -0700 | [diff] [blame] | 7444 | if (desc->isActive(latency * 2) && latency > maxLatency) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7445 | maxLatency = latency; |
| 7446 | } |
| 7447 | } |
| 7448 | mBeaconMuted = mute; |
| 7449 | return maxLatency; |
| 7450 | } |
| 7451 | return 0; |
| 7452 | } |
| 7453 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7454 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7455 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7456 | mEngine->updateDeviceSelectionCache(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7457 | mPreviousOutputs = mOutputs; |
| 7458 | } |
| 7459 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 7460 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7461 | const DeviceVector &prevDevices, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7462 | uint32_t delayMs) |
| 7463 | { |
| 7464 | // mute/unmute strategies using an incompatible device combination |
| 7465 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 7466 | // if unmuting, unmute only after the specified delay |
| 7467 | if (outputDesc->isDuplicated()) { |
| 7468 | return 0; |
| 7469 | } |
| 7470 | |
| 7471 | uint32_t muteWaitMs = 0; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7472 | DeviceVector devices = outputDesc->devices(); |
| 7473 | bool shouldMute = outputDesc->isActive() && (devices.size() >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7474 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7475 | auto productStrategies = mEngine->getOrderedProductStrategies(); |
| 7476 | for (const auto &productStrategy : productStrategies) { |
| 7477 | auto attributes = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); |
| 7478 | DeviceVector curDevices = |
| 7479 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false/*fromCache*/); |
| 7480 | curDevices = curDevices.filter(outputDesc->supportedDevices()); |
| 7481 | bool mute = shouldMute && curDevices.containsAtLeastOne(devices) && curDevices != devices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7482 | bool doMute = false; |
| 7483 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7484 | if (mute && !outputDesc->isStrategyMutedByDevice(productStrategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7485 | doMute = true; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7486 | outputDesc->setStrategyMutedByDevice(productStrategy, true); |
| 7487 | } else if (!mute && outputDesc->isStrategyMutedByDevice(productStrategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7488 | doMute = true; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7489 | outputDesc->setStrategyMutedByDevice(productStrategy, false); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7490 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7491 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7492 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7493 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7494 | // 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] | 7495 | if (!desc->supportedDevices().containsAtLeastOne(outputDesc->supportedDevices())) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7496 | continue; |
| 7497 | } |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 7498 | ALOGVV("%s() output %s %s (curDevice %s)", __func__, desc->info().c_str(), |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7499 | mute ? "muting" : "unmuting", curDevices.toString().c_str()); |
| 7500 | setStrategyMute(productStrategy, mute, desc, mute ? 0 : delayMs); |
| 7501 | if (desc->isStrategyActive(productStrategy)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7502 | if (mute) { |
| 7503 | // FIXME: should not need to double latency if volume could be applied |
| 7504 | // immediately by the audioflinger mixer. We must account for the delay |
| 7505 | // between now and the next time the audioflinger thread for this output |
| 7506 | // will process a buffer (which corresponds to one buffer size, |
| 7507 | // usually 1/2 or 1/4 of the latency). |
| 7508 | if (muteWaitMs < desc->latency() * 2) { |
| 7509 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7510 | } |
| 7511 | } |
| 7512 | } |
| 7513 | } |
| 7514 | } |
| 7515 | } |
| 7516 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7517 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 7518 | // different per device volumes |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7519 | if (outputDesc->isActive() && (devices != prevDevices)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 7520 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
Jasmine Cha | f6074fe | 2021-08-17 13:44:31 +0800 | [diff] [blame] | 7521 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 7522 | if (muteWaitMs < tempMuteWaitMs) { |
| 7523 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7524 | } |
Jasmine Cha | f6074fe | 2021-08-17 13:44:31 +0800 | [diff] [blame] | 7525 | |
| 7526 | // If recommended duration is defined, replace temporary mute duration to avoid |
| 7527 | // truncated notifications at beginning, which depends on duration of changing path in HAL. |
| 7528 | // Otherwise, temporary mute duration is conservatively set to 4 times the reported latency. |
| 7529 | uint32_t tempRecommendedMuteDuration = outputDesc->getRecommendedMuteDurationMs(); |
| 7530 | uint32_t tempMuteDurationMs = tempRecommendedMuteDuration > 0 ? |
| 7531 | tempRecommendedMuteDuration : outputDesc->latency() * 4; |
| 7532 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7533 | for (const auto &activeVs : outputDesc->getActiveVolumeSources()) { |
| 7534 | // make sure that we do not start the temporary mute period too early in case of |
| 7535 | // delayed device change |
| 7536 | setVolumeSourceMute(activeVs, true, outputDesc, delayMs); |
| 7537 | setVolumeSourceMute(activeVs, false, outputDesc, delayMs + tempMuteDurationMs, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7538 | devices.types()); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7539 | } |
| 7540 | } |
| 7541 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7542 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 7543 | if (muteWaitMs > delayMs) { |
| 7544 | muteWaitMs -= delayMs; |
| 7545 | usleep(muteWaitMs * 1000); |
| 7546 | return muteWaitMs; |
| 7547 | } |
| 7548 | return 0; |
| 7549 | } |
| 7550 | |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 7551 | uint32_t AudioPolicyManager::setOutputDevices(const char *caller, |
| 7552 | const sp<SwAudioOutputDescriptor>& outputDesc, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7553 | const DeviceVector &devices, |
| 7554 | bool force, |
| 7555 | int delayMs, |
| 7556 | audio_patch_handle_t *patchHandle, |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 7557 | bool requiresMuteCheck, bool requiresVolumeCheck, |
| 7558 | bool skipMuteDelay) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7559 | { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 7560 | // TODO(b/262404095): Consider if the output need to be reopened. |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 7561 | std::string logPrefix = std::string("caller ") + caller + outputDesc->info(); |
| 7562 | ALOGV("%s %s device %s delayMs %d", __func__, logPrefix.c_str(), |
| 7563 | devices.toString().c_str(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7564 | uint32_t muteWaitMs; |
| 7565 | |
| 7566 | if (outputDesc->isDuplicated()) { |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 7567 | muteWaitMs = setOutputDevices(__func__, outputDesc->subOutput1(), devices, force, delayMs, |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 7568 | nullptr /* patchHandle */, requiresMuteCheck, skipMuteDelay); |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 7569 | muteWaitMs += setOutputDevices(__func__, outputDesc->subOutput2(), devices, force, delayMs, |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 7570 | nullptr /* patchHandle */, requiresMuteCheck, skipMuteDelay); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7571 | return muteWaitMs; |
| 7572 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7573 | |
| 7574 | // filter devices according to output selected |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7575 | DeviceVector filteredDevices = outputDesc->filterSupportedDevices(devices); |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 7576 | DeviceVector prevDevices = outputDesc->devices(); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7577 | DeviceVector availPrevDevices = mAvailableOutputDevices.filter(prevDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7578 | |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 7579 | ALOGV("%s %s prevDevice %s", __func__, logPrefix.c_str(), |
| 7580 | prevDevices.toString().c_str()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7581 | |
| 7582 | if (!filteredDevices.isEmpty()) { |
| 7583 | outputDesc->setDevices(filteredDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7584 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 7585 | |
| 7586 | // if the outputs are not materially active, there is no need to mute. |
| 7587 | if (requiresMuteCheck) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7588 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevices, delayMs); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 7589 | } else { |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 7590 | ALOGV("%s: %s suppressing checkDeviceMuteStrategies", __func__, |
| 7591 | logPrefix.c_str()); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 7592 | muteWaitMs = 0; |
| 7593 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7594 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 7595 | bool outputRouted = outputDesc->isRouted(); |
| 7596 | |
Eric Laurent | 79ea958 | 2020-06-11 18:49:24 -0700 | [diff] [blame] | 7597 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 7598 | // output profile or if new device is not supported AND previous device(s) is(are) still |
| 7599 | // available (otherwise reset device must be done on the output) |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7600 | if (!devices.isEmpty() && filteredDevices.isEmpty() && !availPrevDevices.empty()) { |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 7601 | ALOGV("%s: %s unsupported device %s for output", __func__, logPrefix.c_str(), |
| 7602 | devices.toString().c_str()); |
Eric Laurent | 79ea958 | 2020-06-11 18:49:24 -0700 | [diff] [blame] | 7603 | // restore previous device after evaluating strategy mute state |
| 7604 | outputDesc->setDevices(prevDevices); |
| 7605 | return muteWaitMs; |
| 7606 | } |
| 7607 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7608 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 7609 | // the requested device is AUDIO_DEVICE_NONE |
| 7610 | // OR the requested device is the same as current device |
| 7611 | // AND force is not specified |
| 7612 | // AND the output is connected by a valid audio patch. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7613 | // Doing this check here allows the caller to call setOutputDevices() without conditions |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 7614 | if ((filteredDevices.isEmpty() || filteredDevices == prevDevices) && !force && outputRouted) { |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 7615 | ALOGV("%s %s setting same device %s or null device, force=%d, patch handle=%d", |
| 7616 | __func__, logPrefix.c_str(), filteredDevices.toString().c_str(), force, |
| 7617 | outputDesc->getPatchHandle()); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7618 | if (requiresVolumeCheck && !filteredDevices.isEmpty()) { |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 7619 | ALOGV("%s %s setting same device on routed output, force apply volumes", |
| 7620 | __func__, logPrefix.c_str()); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7621 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs, true /*force*/); |
| 7622 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7623 | return muteWaitMs; |
| 7624 | } |
| 7625 | |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 7626 | ALOGV("%s %s changing device to %s", __func__, logPrefix.c_str(), |
| 7627 | filteredDevices.toString().c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7628 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7629 | // do the routing |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7630 | if (filteredDevices.isEmpty() || mAvailableOutputDevices.filter(filteredDevices).empty()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7631 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7632 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7633 | PatchBuilder patchBuilder; |
| 7634 | patchBuilder.addSource(outputDesc); |
| 7635 | ALOG_ASSERT(filteredDevices.size() <= AUDIO_PATCH_PORTS_MAX, "Too many sink ports"); |
| 7636 | for (const auto &filteredDevice : filteredDevices) { |
| 7637 | patchBuilder.addSink(filteredDevice); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 7638 | } |
| 7639 | |
Jasmine Cha | 7f82d1a | 2020-03-16 13:21:47 +0800 | [diff] [blame] | 7640 | // Add half reported latency to delayMs when muteWaitMs is null in order |
| 7641 | // to avoid disordered sequence of muting volume and changing devices. |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 7642 | int actualDelayMs = !skipMuteDelay && muteWaitMs == 0 |
| 7643 | ? (delayMs + (outputDesc->latency() / 2)) : delayMs; |
| 7644 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), actualDelayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7645 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7646 | |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 7647 | // Since the mute is skip, also skip the apply stream volume as that will be applied externally |
| 7648 | if (!skipMuteDelay) { |
| 7649 | // update stream volumes according to new device |
| 7650 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs); |
| 7651 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7652 | |
| 7653 | return muteWaitMs; |
| 7654 | } |
| 7655 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7656 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7657 | int delayMs, |
| 7658 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7659 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7660 | ssize_t index; |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 7661 | if (patchHandle == nullptr && !outputDesc->isRouted()) { |
| 7662 | return INVALID_OPERATION; |
| 7663 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7664 | if (patchHandle) { |
| 7665 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 7666 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 7667 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7668 | } |
| 7669 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7670 | return INVALID_OPERATION; |
| 7671 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7672 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7673 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7674 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 7675 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7676 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7677 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 7678 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7679 | return status; |
| 7680 | } |
| 7681 | |
| 7682 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7683 | const sp<DeviceDescriptor> &device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7684 | bool force, |
| 7685 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7686 | { |
| 7687 | status_t status = NO_ERROR; |
| 7688 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7689 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7690 | if ((device != nullptr) && ((device != inputDesc->getDevice()) || force)) { |
| 7691 | inputDesc->setDevice(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7692 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7693 | if (mAvailableInputDevices.contains(device)) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7694 | PatchBuilder patchBuilder; |
| 7695 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 7696 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 7697 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7698 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 7699 | auto result = usecase; |
| 7700 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 7701 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 7702 | } |
| 7703 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7704 | //only one input device for now |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7705 | addSource(device); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7706 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7707 | } |
| 7708 | } |
| 7709 | return status; |
| 7710 | } |
| 7711 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7712 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 7713 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7714 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7715 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7716 | ssize_t index; |
| 7717 | if (patchHandle) { |
| 7718 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 7719 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 7720 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7721 | } |
| 7722 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7723 | return INVALID_OPERATION; |
| 7724 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7725 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7726 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7727 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 7728 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7729 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7730 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 7731 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7732 | return status; |
| 7733 | } |
| 7734 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7735 | sp<IOProfile> AudioPolicyManager::getInputProfile(const sp<DeviceDescriptor> &device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7736 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 7737 | audio_format_t& format, |
| 7738 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7739 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7740 | { |
| 7741 | // Choose an input profile based on the requested capture parameters: select the first available |
| 7742 | // profile supporting all requested parameters. |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7743 | // The flags can be ignored if it doesn't contain a much match flag. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7744 | |
Atneya Nair | 0f0a803 | 2022-12-12 16:20:12 -0800 | [diff] [blame] | 7745 | using underlying_input_flag_t = std::underlying_type_t<audio_input_flags_t>; |
| 7746 | const underlying_input_flag_t mustMatchFlag = AUDIO_INPUT_FLAG_MMAP_NOIRQ | |
| 7747 | AUDIO_INPUT_FLAG_HOTWORD_TAP | AUDIO_INPUT_FLAG_HW_LOOKBACK; |
| 7748 | |
| 7749 | const underlying_input_flag_t oriFlags = flags; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 7750 | |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7751 | for (;;) { |
| 7752 | sp<IOProfile> firstInexact = nullptr; |
| 7753 | uint32_t updatedSamplingRate = 0; |
| 7754 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 7755 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
| 7756 | for (const auto& hwModule : mHwModules) { |
| 7757 | for (const auto& profile : hwModule->getInputProfiles()) { |
| 7758 | // profile->log(); |
| 7759 | //updatedFormat = format; |
jiabin | 66acc43 | 2024-02-06 00:57:36 +0000 | [diff] [blame] | 7760 | if (profile->getCompatibilityScore( |
| 7761 | DeviceVector(device), |
| 7762 | samplingRate, |
| 7763 | &updatedSamplingRate, |
| 7764 | format, |
| 7765 | &updatedFormat, |
| 7766 | channelMask, |
| 7767 | &updatedChannelMask, |
| 7768 | // FIXME ugly cast |
| 7769 | (audio_output_flags_t) flags, |
| 7770 | true /*exactMatchRequiredForInputFlags*/) == IOProfile::EXACT_MATCH) { |
| 7771 | samplingRate = updatedSamplingRate; |
| 7772 | format = updatedFormat; |
| 7773 | channelMask = updatedChannelMask; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7774 | return profile; |
| 7775 | } |
jiabin | 66acc43 | 2024-02-06 00:57:36 +0000 | [diff] [blame] | 7776 | if (firstInexact == nullptr |
| 7777 | && profile->getCompatibilityScore( |
| 7778 | DeviceVector(device), |
| 7779 | samplingRate, |
| 7780 | &updatedSamplingRate, |
| 7781 | format, |
| 7782 | &updatedFormat, |
| 7783 | channelMask, |
| 7784 | &updatedChannelMask, |
| 7785 | // FIXME ugly cast |
| 7786 | (audio_output_flags_t) flags, |
| 7787 | false /*exactMatchRequiredForInputFlags*/) |
| 7788 | != IOProfile::NO_MATCH) { |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7789 | firstInexact = profile; |
| 7790 | } |
| 7791 | } |
| 7792 | } |
| 7793 | |
| 7794 | if (firstInexact != nullptr) { |
| 7795 | samplingRate = updatedSamplingRate; |
| 7796 | format = updatedFormat; |
| 7797 | channelMask = updatedChannelMask; |
| 7798 | return firstInexact; |
| 7799 | } else if (flags & AUDIO_INPUT_FLAG_RAW) { |
| 7800 | flags = (audio_input_flags_t) (flags & ~AUDIO_INPUT_FLAG_RAW); // retry |
| 7801 | } else if ((flags & mustMatchFlag) == AUDIO_INPUT_FLAG_NONE && |
| 7802 | flags != AUDIO_INPUT_FLAG_NONE && audio_is_linear_pcm(format)) { |
| 7803 | flags = AUDIO_INPUT_FLAG_NONE; |
| 7804 | } else { // fail |
| 7805 | ALOGW("%s could not find profile for device %s, sampling rate %u, format %#x, " |
| 7806 | "channel mask 0x%X, flags %#x", __func__, device->toString().c_str(), |
| 7807 | samplingRate, format, channelMask, oriFlags); |
| 7808 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7809 | } |
| 7810 | } |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7811 | |
| 7812 | return nullptr; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7813 | } |
| 7814 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7815 | float AudioPolicyManager::computeVolume(IVolumeCurves &curves, |
| 7816 | VolumeSource volumeSource, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 7817 | int index, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7818 | const DeviceTypeSet& deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7819 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7820 | float volumeDb = curves.volIndexToDb(Volume::getDeviceCategory(deviceTypes), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 7821 | |
| 7822 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 7823 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 7824 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 7825 | // the ringtone volume |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7826 | const auto callVolumeSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL, false); |
| 7827 | const auto ringVolumeSrc = toVolumeSource(AUDIO_STREAM_RING, false); |
| 7828 | const auto musicVolumeSrc = toVolumeSource(AUDIO_STREAM_MUSIC, false); |
| 7829 | const auto alarmVolumeSrc = toVolumeSource(AUDIO_STREAM_ALARM, false); |
| 7830 | const auto a11yVolumeSrc = toVolumeSource(AUDIO_STREAM_ACCESSIBILITY, false); |
Oscar Azucena | 437ded5 | 2023-08-30 18:45:18 -0700 | [diff] [blame] | 7831 | // Verify that the current volume source is not the ringer volume to prevent recursively |
| 7832 | // calling to compute volume. This could happen in cases where a11y and ringer sounds belong |
| 7833 | // to the same volume group. |
| 7834 | if (volumeSource != ringVolumeSrc && volumeSource == a11yVolumeSrc |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7835 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) && |
| 7836 | mOutputs.isActive(ringVolumeSrc, 0)) { |
| 7837 | auto &ringCurves = getVolumeCurves(AUDIO_STREAM_RING); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7838 | const float ringVolumeDb = computeVolume(ringCurves, ringVolumeSrc, index, deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7839 | return ringVolumeDb - 4 > volumeDb ? ringVolumeDb - 4 : volumeDb; |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 7840 | } |
| 7841 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 7842 | // in-call: always cap volume by voice volume + some low headroom |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7843 | if ((volumeSource != callVolumeSrc && (isInCall() || |
| 7844 | mOutputs.isActiveLocally(callVolumeSrc))) && |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7845 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM, false) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7846 | volumeSource == ringVolumeSrc || volumeSource == musicVolumeSrc || |
| 7847 | volumeSource == alarmVolumeSrc || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7848 | volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION, false) || |
| 7849 | volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false) || |
| 7850 | volumeSource == toVolumeSource(AUDIO_STREAM_DTMF, false) || |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7851 | volumeSource == a11yVolumeSrc)) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7852 | auto &voiceCurves = getVolumeCurves(callVolumeSrc); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7853 | int voiceVolumeIndex = voiceCurves.getVolumeIndex(deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7854 | const float maxVoiceVolDb = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7855 | computeVolume(voiceCurves, callVolumeSrc, voiceVolumeIndex, deviceTypes) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 7856 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 7857 | // FIXME: Workaround for call screening applications until a proper audio mode is defined |
| 7858 | // to support this scenario : Exempt the RING stream from the audio cap if the audio was |
| 7859 | // programmatically muted. |
| 7860 | // VOICE_CALL stream has minVolumeIndex > 0 : Users cannot set the volume of voice calls to |
| 7861 | // 0. We don't want to cap volume when the system has programmatically muted the voice call |
| 7862 | // stream. See setVolumeCurveIndex() for more information. |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7863 | bool exemptFromCapping = |
| 7864 | ((volumeSource == ringVolumeSrc) || (volumeSource == a11yVolumeSrc)) |
| 7865 | && (voiceVolumeIndex == 0); |
Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 7866 | ALOGV_IF(exemptFromCapping, "%s volume source %d at vol=%f not capped", __func__, |
| 7867 | volumeSource, volumeDb); |
| 7868 | if ((volumeDb > maxVoiceVolDb) && !exemptFromCapping) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7869 | ALOGV("%s volume source %d at vol=%f overriden by volume group %d at vol=%f", __func__, |
| 7870 | volumeSource, volumeDb, callVolumeSrc, maxVoiceVolDb); |
| 7871 | volumeDb = maxVoiceVolDb; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 7872 | } |
| 7873 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7874 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 7875 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 7876 | // - always attenuate notifications volume by 6dB |
| 7877 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 7878 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7879 | // - if music is playing, always limit the volume to current music volume, |
| 7880 | // with a minimum threshold at -36dB so that notification is always perceived. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7881 | if (!Intersection(deviceTypes, |
| 7882 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, |
| 7883 | AUDIO_DEVICE_OUT_WIRED_HEADSET, AUDIO_DEVICE_OUT_WIRED_HEADPHONE, |
Eric Laurent | c42df45 | 2020-08-07 10:51:53 -0700 | [diff] [blame] | 7884 | AUDIO_DEVICE_OUT_USB_HEADSET, AUDIO_DEVICE_OUT_HEARING_AID, |
| 7885 | AUDIO_DEVICE_OUT_BLE_HEADSET}).empty() && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7886 | ((volumeSource == alarmVolumeSrc || |
| 7887 | volumeSource == ringVolumeSrc) || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7888 | (volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION, false)) || |
| 7889 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM, false)) || |
| 7890 | ((volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false)) && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7891 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
| 7892 | curves.canBeMuted()) { |
| 7893 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7894 | // when the phone is ringing we must consider that music could have been paused just before |
| 7895 | // by the music application and behave as if music was active if the last music track was |
| 7896 | // just stopped |
Oscar Azucena | 437ded5 | 2023-08-30 18:45:18 -0700 | [diff] [blame] | 7897 | // Verify that the current volume source is not the music volume to prevent recursively |
| 7898 | // calling to compute volume. This could happen in cases where music and |
| 7899 | // (alarm, ring, notification, system, etc.) sounds belong to the same volume group. |
| 7900 | if (volumeSource != musicVolumeSrc && |
| 7901 | (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) |
| 7902 | || mLimitRingtoneVolume)) { |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7903 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7904 | DeviceTypeSet musicDevice = |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7905 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 7906 | nullptr, true /*fromCache*/).types(); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7907 | auto &musicCurves = getVolumeCurves(AUDIO_STREAM_MUSIC); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7908 | float musicVolDb = computeVolume(musicCurves, |
| 7909 | musicVolumeSrc, |
| 7910 | musicCurves.getVolumeIndex(musicDevice), |
| 7911 | musicDevice); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7912 | float minVolDb = (musicVolDb > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 7913 | musicVolDb : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
| 7914 | if (volumeDb > minVolDb) { |
| 7915 | volumeDb = minVolDb; |
| 7916 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDb, musicVolDb); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7917 | } |
Eric Laurent | 7b6385c | 2021-05-12 17:55:36 +0200 | [diff] [blame] | 7918 | if (Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER |
| 7919 | && !Intersection(deviceTypes, {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, |
| 7920 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES}).empty()) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 7921 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 7922 | // intended to be played |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7923 | if ((volumeDb > -96.0f) && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7924 | (musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDb)) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7925 | ALOGV("%s increasing volume for volume source=%d device=%s from %f to %f", |
| 7926 | __func__, volumeSource, dumpDeviceTypes(deviceTypes).c_str(), volumeDb, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7927 | musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 7928 | volumeDb = musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 7929 | } |
| 7930 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7931 | } else if ((Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7932 | (!(volumeSource == alarmVolumeSrc || volumeSource == ringVolumeSrc))) { |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7933 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7934 | } |
| 7935 | } |
| 7936 | |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7937 | return volumeDb; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7938 | } |
| 7939 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7940 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7941 | VolumeSource fromVolumeSource, |
| 7942 | VolumeSource toVolumeSource) |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7943 | { |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7944 | if (fromVolumeSource == toVolumeSource) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7945 | return srcIndex; |
| 7946 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7947 | auto &srcCurves = getVolumeCurves(fromVolumeSource); |
| 7948 | auto &dstCurves = getVolumeCurves(toVolumeSource); |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7949 | float minSrc = (float)srcCurves.getVolumeIndexMin(); |
| 7950 | float maxSrc = (float)srcCurves.getVolumeIndexMax(); |
| 7951 | float minDst = (float)dstCurves.getVolumeIndexMin(); |
| 7952 | float maxDst = (float)dstCurves.getVolumeIndexMax(); |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7953 | |
Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 7954 | // preserve mute request or correct range |
| 7955 | if (srcIndex < minSrc) { |
| 7956 | if (srcIndex == 0) { |
| 7957 | return 0; |
| 7958 | } |
| 7959 | srcIndex = minSrc; |
| 7960 | } else if (srcIndex > maxSrc) { |
| 7961 | srcIndex = maxSrc; |
| 7962 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7963 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 7964 | } |
| 7965 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7966 | status_t AudioPolicyManager::checkAndSetVolume(IVolumeCurves &curves, |
| 7967 | VolumeSource volumeSource, |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7968 | int index, |
| 7969 | const sp<AudioOutputDescriptor>& outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7970 | DeviceTypeSet deviceTypes, |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7971 | int delayMs, |
| 7972 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7973 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7974 | // do not change actual attributes volume if the attributes is muted |
| 7975 | if (outputDesc->isMuted(volumeSource)) { |
| 7976 | ALOGVV("%s: volume source %d muted count %d active=%d", __func__, volumeSource, |
| 7977 | outputDesc->getMuteCount(volumeSource), outputDesc->isActive(volumeSource)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7978 | return NO_ERROR; |
| 7979 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7980 | |
Eric Laurent | ae6e88c | 2024-01-10 14:42:57 +0100 | [diff] [blame] | 7981 | bool isVoiceVolSrc; |
| 7982 | bool isBtScoVolSrc; |
| 7983 | if (!isVolumeConsistentForCalls( |
| 7984 | volumeSource, deviceTypes, isVoiceVolSrc, isBtScoVolSrc, __func__)) { |
Eric Laurent | 571ef96 | 2020-07-24 11:43:48 -0700 | [diff] [blame] | 7985 | // Do not return an error here as AudioService will always set both voice call |
Eric Laurent | ae6e88c | 2024-01-10 14:42:57 +0100 | [diff] [blame] | 7986 | // and Bluetooth SCO volumes due to stream aliasing. |
Eric Laurent | 571ef96 | 2020-07-24 11:43:48 -0700 | [diff] [blame] | 7987 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7988 | } |
Eric Laurent | ae6e88c | 2024-01-10 14:42:57 +0100 | [diff] [blame] | 7989 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7990 | if (deviceTypes.empty()) { |
| 7991 | deviceTypes = outputDesc->devices().types(); |
chenxin20 | 80986da | 2023-07-17 11:45:21 +0800 | [diff] [blame] | 7992 | index = curves.getVolumeIndex(deviceTypes); |
| 7993 | ALOGD("%s if deviceTypes is change from none to device %s, need get index %d", |
| 7994 | __func__, dumpDeviceTypes(deviceTypes).c_str(), index); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7995 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7996 | |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 7997 | if (curves.getVolumeIndexMin() < 0 || curves.getVolumeIndexMax() < 0) { |
| 7998 | ALOGE("invalid volume index range"); |
| 7999 | return BAD_VALUE; |
| 8000 | } |
| 8001 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8002 | float volumeDb = computeVolume(curves, volumeSource, index, deviceTypes); |
| 8003 | if (outputDesc->isFixedVolume(deviceTypes) || |
Eric Laurent | 9698a4c | 2020-10-12 17:10:23 -0700 | [diff] [blame] | 8004 | // Force VoIP volume to max for bluetooth SCO device except if muted |
| 8005 | (index != 0 && (isVoiceVolSrc || isBtScoVolSrc) && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8006 | isSingleDeviceType(deviceTypes, audio_is_bluetooth_out_sco_device))) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 8007 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 8008 | } |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 8009 | const bool muted = (index == 0) && (volumeDb != 0.0f); |
Eric Laurent | 31a428a | 2023-08-11 12:16:28 +0200 | [diff] [blame] | 8010 | outputDesc->setVolume(volumeDb, muted, volumeSource, curves.getStreamTypes(), |
| 8011 | deviceTypes, delayMs, force, isVoiceVolSrc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 8012 | |
Eric Laurent | e8f2c0f | 2021-08-17 11:17:19 +0200 | [diff] [blame] | 8013 | if (outputDesc == mPrimaryOutput && (isVoiceVolSrc || isBtScoVolSrc)) { |
Eric Laurent | ae6e88c | 2024-01-10 14:42:57 +0100 | [diff] [blame] | 8014 | setVoiceVolume(index, curves, isVoiceVolSrc, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8015 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8016 | return NO_ERROR; |
| 8017 | } |
| 8018 | |
Eric Laurent | ae6e88c | 2024-01-10 14:42:57 +0100 | [diff] [blame] | 8019 | void AudioPolicyManager::setVoiceVolume( |
| 8020 | int index, IVolumeCurves &curves, bool isVoiceVolSrc, int delayMs) { |
| 8021 | float voiceVolume; |
| 8022 | // Force voice volume to max or mute for Bluetooth SCO as other attenuations are managed |
| 8023 | // by the headset |
| 8024 | if (isVoiceVolSrc) { |
| 8025 | voiceVolume = (float)index/(float)curves.getVolumeIndexMax(); |
| 8026 | } else { |
| 8027 | voiceVolume = index == 0 ? 0.0 : 1.0; |
| 8028 | } |
| 8029 | if (voiceVolume != mLastVoiceVolume) { |
| 8030 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 8031 | mLastVoiceVolume = voiceVolume; |
| 8032 | } |
| 8033 | } |
| 8034 | |
| 8035 | bool AudioPolicyManager::isVolumeConsistentForCalls(VolumeSource volumeSource, |
| 8036 | const DeviceTypeSet& deviceTypes, |
| 8037 | bool& isVoiceVolSrc, |
| 8038 | bool& isBtScoVolSrc, |
| 8039 | const char* caller) { |
| 8040 | const VolumeSource callVolSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL, false); |
| 8041 | const VolumeSource btScoVolSrc = toVolumeSource(AUDIO_STREAM_BLUETOOTH_SCO, false); |
| 8042 | const bool isScoRequested = isScoRequestedForComm(); |
| 8043 | const bool isHAUsed = isHearingAidUsedForComm(); |
| 8044 | |
| 8045 | isVoiceVolSrc = (volumeSource != VOLUME_SOURCE_NONE) && (callVolSrc == volumeSource); |
| 8046 | isBtScoVolSrc = (volumeSource != VOLUME_SOURCE_NONE) && (btScoVolSrc == volumeSource); |
| 8047 | |
| 8048 | if ((callVolSrc != btScoVolSrc) && |
| 8049 | ((isVoiceVolSrc && isScoRequested) || |
| 8050 | (isBtScoVolSrc && !(isScoRequested || isHAUsed))) && |
| 8051 | !isSingleDeviceType(deviceTypes, AUDIO_DEVICE_OUT_TELEPHONY_TX)) { |
| 8052 | ALOGV("%s cannot set volume group %d volume when is%srequested for comm", caller, |
| 8053 | volumeSource, isScoRequested ? " " : " not "); |
| 8054 | return false; |
| 8055 | } |
| 8056 | return true; |
| 8057 | } |
| 8058 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 8059 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8060 | const DeviceTypeSet& deviceTypes, |
| 8061 | int delayMs, |
| 8062 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8063 | { |
jiabin | cd51052 | 2020-01-22 09:40:55 -0800 | [diff] [blame] | 8064 | ALOGVV("applyStreamVolumes() for device %s", dumpDeviceTypes(deviceTypes).c_str()); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8065 | for (const auto &volumeGroup : mEngine->getVolumeGroups()) { |
| 8066 | auto &curves = getVolumeCurves(toVolumeSource(volumeGroup)); |
| 8067 | checkAndSetVolume(curves, toVolumeSource(volumeGroup), |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8068 | curves.getVolumeIndex(deviceTypes), |
| 8069 | outputDesc, deviceTypes, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8070 | } |
| 8071 | } |
| 8072 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 8073 | void AudioPolicyManager::setStrategyMute(product_strategy_t strategy, |
| 8074 | bool on, |
| 8075 | const sp<AudioOutputDescriptor>& outputDesc, |
| 8076 | int delayMs, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8077 | DeviceTypeSet deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8078 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8079 | std::vector<VolumeSource> sourcesToMute; |
| 8080 | for (auto attributes: mEngine->getAllAttributesForProductStrategy(strategy)) { |
| 8081 | ALOGVV("%s() attributes %s, mute %d, output ID %d", __func__, |
| 8082 | toString(attributes).c_str(), on, outputDesc->getId()); |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 8083 | VolumeSource source = toVolumeSource(attributes, false); |
| 8084 | if ((source != VOLUME_SOURCE_NONE) && |
| 8085 | (std::find(begin(sourcesToMute), end(sourcesToMute), source) |
| 8086 | == end(sourcesToMute))) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8087 | sourcesToMute.push_back(source); |
| 8088 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8089 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8090 | for (auto source : sourcesToMute) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8091 | setVolumeSourceMute(source, on, outputDesc, delayMs, deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8092 | } |
| 8093 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8094 | } |
| 8095 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8096 | void AudioPolicyManager::setVolumeSourceMute(VolumeSource volumeSource, |
| 8097 | bool on, |
| 8098 | const sp<AudioOutputDescriptor>& outputDesc, |
| 8099 | int delayMs, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8100 | DeviceTypeSet deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8101 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8102 | if (deviceTypes.empty()) { |
| 8103 | deviceTypes = outputDesc->devices().types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8104 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8105 | auto &curves = getVolumeCurves(volumeSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8106 | if (on) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8107 | if (!outputDesc->isMuted(volumeSource)) { |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 8108 | if (curves.canBeMuted() && |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 8109 | (volumeSource != toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8110 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == |
| 8111 | AUDIO_POLICY_FORCE_NONE))) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8112 | checkAndSetVolume(curves, volumeSource, 0, outputDesc, deviceTypes, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8113 | } |
| 8114 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8115 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not |
| 8116 | // ignored |
| 8117 | outputDesc->incMuteCount(volumeSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8118 | } else { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8119 | if (!outputDesc->isMuted(volumeSource)) { |
| 8120 | ALOGV("%s unmuting non muted attributes!", __func__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8121 | return; |
| 8122 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8123 | if (outputDesc->decMuteCount(volumeSource) == 0) { |
| 8124 | checkAndSetVolume(curves, volumeSource, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8125 | curves.getVolumeIndex(deviceTypes), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 8126 | outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8127 | deviceTypes, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8128 | delayMs); |
| 8129 | } |
| 8130 | } |
| 8131 | } |
| 8132 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 8133 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 8134 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 8135 | // has flags that map to a stream type? |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 8136 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 8137 | return true; |
| 8138 | } |
| 8139 | |
| 8140 | // has known usage? |
| 8141 | switch (paa->usage) { |
| 8142 | case AUDIO_USAGE_UNKNOWN: |
| 8143 | case AUDIO_USAGE_MEDIA: |
| 8144 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 8145 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 8146 | case AUDIO_USAGE_ALARM: |
| 8147 | case AUDIO_USAGE_NOTIFICATION: |
| 8148 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 8149 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 8150 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 8151 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 8152 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 8153 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 8154 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 8155 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 8156 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 8157 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 8158 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | 21777f8 | 2019-12-06 18:12:06 -0800 | [diff] [blame] | 8159 | case AUDIO_USAGE_CALL_ASSISTANT: |
Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 8160 | case AUDIO_USAGE_EMERGENCY: |
| 8161 | case AUDIO_USAGE_SAFETY: |
| 8162 | case AUDIO_USAGE_VEHICLE_STATUS: |
| 8163 | case AUDIO_USAGE_ANNOUNCEMENT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 8164 | break; |
| 8165 | default: |
| 8166 | return false; |
| 8167 | } |
| 8168 | return true; |
| 8169 | } |
| 8170 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 8171 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 8172 | { |
| 8173 | return mEngine->getForceUse(usage); |
| 8174 | } |
| 8175 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 8176 | bool AudioPolicyManager::isInCall() const { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 8177 | return isStateInCall(mEngine->getPhoneState()); |
| 8178 | } |
| 8179 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 8180 | bool AudioPolicyManager::isStateInCall(int state) const { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 8181 | return is_state_in_call(state); |
| 8182 | } |
| 8183 | |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 8184 | bool AudioPolicyManager::isCallAudioAccessible() const { |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 8185 | audio_mode_t mode = mEngine->getPhoneState(); |
| 8186 | return (mode == AUDIO_MODE_IN_CALL) |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 8187 | || (mode == AUDIO_MODE_CALL_SCREEN) |
| 8188 | || (mode == AUDIO_MODE_CALL_REDIRECT); |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 8189 | } |
| 8190 | |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 8191 | bool AudioPolicyManager::isInCallOrScreening() const { |
| 8192 | audio_mode_t mode = mEngine->getPhoneState(); |
| 8193 | return isStateInCall(mode) || mode == AUDIO_MODE_CALL_SCREEN; |
| 8194 | } |
| 8195 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8196 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 8197 | { |
| 8198 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 8199 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 8200 | if (sourceDesc->isConnected() && (sourceDesc->srcDevice()->equals(deviceDesc) || |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 8201 | sourceDesc->sinkDevice()->equals(deviceDesc)) |
| 8202 | && !isCallRxAudioSource(sourceDesc)) { |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 8203 | disconnectAudioSource(sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8204 | } |
| 8205 | } |
| 8206 | |
| 8207 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 8208 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 8209 | bool release = false; |
| 8210 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 8211 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 8212 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 8213 | source->ext.device.type == deviceDesc->type()) { |
| 8214 | release = true; |
| 8215 | } |
| 8216 | } |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 8217 | const char *address = deviceDesc->address().c_str(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8218 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 8219 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 8220 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 8221 | sink->ext.device.type == deviceDesc->type() && |
| 8222 | (strnlen(address, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0 |
| 8223 | || strncmp(sink->ext.device.address, address, |
| 8224 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8225 | release = true; |
| 8226 | } |
| 8227 | } |
| 8228 | if (release) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8229 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->getHandle()); |
| 8230 | releaseAudioPatch(patchDesc->getHandle(), patchDesc->getUid()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8231 | } |
| 8232 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 8233 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 8234 | mInputs.clearSessionRoutesForDevice(deviceDesc); |
| 8235 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 8236 | mHwModules.cleanUpForDevice(deviceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8237 | } |
| 8238 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8239 | void AudioPolicyManager::modifySurroundFormats( |
| 8240 | const sp<DeviceDescriptor>& devDesc, FormatVector *formatsPtr) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8241 | std::unordered_set<audio_format_t> enforcedSurround( |
| 8242 | devDesc->encodedFormats().begin(), devDesc->encodedFormats().end()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8243 | 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] | 8244 | for (const auto& pair : mConfig->getSurroundFormats()) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8245 | allSurround.insert(pair.first); |
| 8246 | for (const auto& subformat : pair.second) allSurround.insert(subformat); |
| 8247 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8248 | |
| 8249 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 8250 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8251 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8252 | // This is the resulting set of formats depending on the surround mode: |
| 8253 | // 'all surround' = allSurround |
| 8254 | // 'enforced surround' = enforcedSurround [may include IEC69137 which isn't raw surround fmt] |
| 8255 | // 'non-surround' = not in 'all surround' and not in 'enforced surround' |
| 8256 | // 'manual surround' = mManualSurroundFormats |
| 8257 | // AUTO: formats v 'enforced surround' |
| 8258 | // ALWAYS: formats v 'all surround' v 'enforced surround' |
| 8259 | // NEVER: formats ^ 'non-surround' |
| 8260 | // MANUAL: formats ^ ('non-surround' v 'manual surround' v (IEC69137 ^ 'enforced surround')) |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8261 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8262 | std::unordered_set<audio_format_t> formatSet; |
| 8263 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL |
| 8264 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8265 | // formatSet is (formats ^ 'non-surround') |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8266 | for (auto formatIter = formatsPtr->begin(); formatIter != formatsPtr->end(); ++formatIter) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8267 | if (allSurround.count(*formatIter) == 0 && enforcedSurround.count(*formatIter) == 0) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8268 | formatSet.insert(*formatIter); |
| 8269 | } |
| 8270 | } |
| 8271 | } else { |
| 8272 | formatSet.insert(formatsPtr->begin(), formatsPtr->end()); |
| 8273 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8274 | formatsPtr->clear(); // Re-filled from the formatSet at the end. |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8275 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 8276 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8277 | formatSet.insert(mManualSurroundFormats.begin(), mManualSurroundFormats.end()); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8278 | // Enable IEC61937 when in MANUAL mode if it's enforced for this device. |
| 8279 | if (enforcedSurround.count(AUDIO_FORMAT_IEC61937) != 0) { |
| 8280 | formatSet.insert(AUDIO_FORMAT_IEC61937); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8281 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8282 | } else if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { // AUTO or ALWAYS |
| 8283 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 8284 | formatSet.insert(allSurround.begin(), allSurround.end()); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 8285 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8286 | formatSet.insert(enforcedSurround.begin(), enforcedSurround.end()); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8287 | } |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8288 | for (const auto& format : formatSet) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8289 | formatsPtr->push_back(format); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8290 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8291 | } |
| 8292 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8293 | void AudioPolicyManager::modifySurroundChannelMasks(ChannelMaskSet *channelMasksPtr) { |
| 8294 | ChannelMaskSet &channelMasks = *channelMasksPtr; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8295 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 8296 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 8297 | |
| 8298 | // If NEVER, then remove support for channelMasks > stereo. |
| 8299 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8300 | for (auto it = channelMasks.begin(); it != channelMasks.end();) { |
| 8301 | audio_channel_mask_t channelMask = *it; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8302 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 8303 | ALOGV("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8304 | it = channelMasks.erase(it); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8305 | } else { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8306 | ++it; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8307 | } |
| 8308 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 8309 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 8310 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 8311 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8312 | bool supports5dot1 = false; |
| 8313 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 8314 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8315 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 8316 | supports5dot1 = true; |
| 8317 | break; |
| 8318 | } |
| 8319 | } |
| 8320 | // If not then add 5.1 support. |
| 8321 | if (!supports5dot1) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8322 | channelMasks.insert(AUDIO_CHANNEL_OUT_5POINT1); |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 8323 | 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] | 8324 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8325 | } |
| 8326 | } |
| 8327 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8328 | void AudioPolicyManager::updateAudioProfiles(const sp<DeviceDescriptor>& devDesc, |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 8329 | audio_io_handle_t ioHandle, |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 8330 | const sp<IOProfile>& profile) { |
| 8331 | if (!profile->hasDynamicAudioProfile()) { |
| 8332 | return; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8333 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8334 | |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 8335 | audio_port_v7 devicePort; |
| 8336 | devDesc->toAudioPort(&devicePort); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8337 | |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 8338 | audio_port_v7 mixPort; |
| 8339 | profile->toAudioPort(&mixPort); |
| 8340 | mixPort.ext.mix.handle = ioHandle; |
| 8341 | |
| 8342 | status_t status = mpClientInterface->getAudioMixPort(&devicePort, &mixPort); |
| 8343 | if (status != NO_ERROR) { |
| 8344 | ALOGE("%s failed to query the attributes of the mix port", __func__); |
| 8345 | return; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8346 | } |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 8347 | |
| 8348 | std::set<audio_format_t> supportedFormats; |
| 8349 | for (size_t i = 0; i < mixPort.num_audio_profiles; ++i) { |
| 8350 | supportedFormats.insert(mixPort.audio_profiles[i].format); |
| 8351 | } |
| 8352 | FormatVector formats(supportedFormats.begin(), supportedFormats.end()); |
| 8353 | mReportedFormatsMap[devDesc] = formats; |
| 8354 | |
| 8355 | if (devDesc->type() == AUDIO_DEVICE_OUT_HDMI || |
| 8356 | isDeviceOfModule(devDesc,AUDIO_HARDWARE_MODULE_ID_MSD)) { |
| 8357 | modifySurroundFormats(devDesc, &formats); |
| 8358 | size_t modifiedNumProfiles = 0; |
| 8359 | for (size_t i = 0; i < mixPort.num_audio_profiles; ++i) { |
| 8360 | if (std::find(formats.begin(), formats.end(), mixPort.audio_profiles[i].format) == |
| 8361 | formats.end()) { |
| 8362 | // Skip the format that is not present after modifying surround formats. |
| 8363 | continue; |
| 8364 | } |
| 8365 | memcpy(&mixPort.audio_profiles[modifiedNumProfiles], &mixPort.audio_profiles[i], |
| 8366 | sizeof(struct audio_profile)); |
| 8367 | ChannelMaskSet channels(mixPort.audio_profiles[modifiedNumProfiles].channel_masks, |
| 8368 | mixPort.audio_profiles[modifiedNumProfiles].channel_masks + |
| 8369 | mixPort.audio_profiles[modifiedNumProfiles].num_channel_masks); |
| 8370 | modifySurroundChannelMasks(&channels); |
| 8371 | std::copy(channels.begin(), channels.end(), |
| 8372 | std::begin(mixPort.audio_profiles[modifiedNumProfiles].channel_masks)); |
| 8373 | mixPort.audio_profiles[modifiedNumProfiles++].num_channel_masks = channels.size(); |
| 8374 | } |
| 8375 | mixPort.num_audio_profiles = modifiedNumProfiles; |
| 8376 | } |
| 8377 | profile->importAudioPort(mixPort); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8378 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8379 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8380 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 8381 | audio_patch_handle_t *patchHandle, |
| 8382 | AudioIODescriptorInterface *ioDescriptor, |
| 8383 | const struct audio_patch *patch, |
| 8384 | int delayMs) |
| 8385 | { |
| 8386 | ssize_t index = mAudioPatches.indexOfKey( |
| 8387 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 8388 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 8389 | sp<AudioPatch> patchDesc; |
| 8390 | status_t status = installPatch( |
| 8391 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 8392 | if (status == NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8393 | ioDescriptor->setPatchHandle(patchDesc->getHandle()); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8394 | } |
| 8395 | return status; |
| 8396 | } |
| 8397 | |
| 8398 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 8399 | ssize_t index, |
| 8400 | audio_patch_handle_t *patchHandle, |
| 8401 | const struct audio_patch *patch, |
| 8402 | int delayMs, |
| 8403 | uid_t uid, |
| 8404 | sp<AudioPatch> *patchDescPtr) |
| 8405 | { |
| 8406 | sp<AudioPatch> patchDesc; |
| 8407 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 8408 | if (index >= 0) { |
| 8409 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8410 | afPatchHandle = patchDesc->getAfHandle(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8411 | } |
| 8412 | |
| 8413 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 8414 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 8415 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 8416 | if (status == NO_ERROR) { |
| 8417 | if (index < 0) { |
| 8418 | patchDesc = new AudioPatch(patch, uid); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8419 | addAudioPatch(patchDesc->getHandle(), patchDesc); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8420 | } else { |
| 8421 | patchDesc->mPatch = *patch; |
| 8422 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8423 | patchDesc->setAfHandle(afPatchHandle); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8424 | if (patchHandle) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8425 | *patchHandle = patchDesc->getHandle(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8426 | } |
| 8427 | nextAudioPortGeneration(); |
| 8428 | mpClientInterface->onAudioPatchListUpdate(); |
| 8429 | } |
| 8430 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 8431 | return status; |
| 8432 | } |
| 8433 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8434 | bool AudioPolicyManager::areAllActiveTracksRerouted(const sp<SwAudioOutputDescriptor>& output) |
| 8435 | { |
| 8436 | const TrackClientVector activeClients = output->getActiveClients(); |
| 8437 | if (activeClients.empty()) { |
| 8438 | return true; |
| 8439 | } |
| 8440 | ssize_t index = mAudioPatches.indexOfKey(output->getPatchHandle()); |
| 8441 | if (index < 0) { |
| 8442 | ALOGE("%s, no audio patch found while there are active clients on output %d", |
| 8443 | __func__, output->getId()); |
| 8444 | return false; |
| 8445 | } |
| 8446 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 8447 | DeviceVector routedDevices; |
| 8448 | for (int i = 0; i < patchDesc->mPatch.num_sinks; ++i) { |
| 8449 | sp<DeviceDescriptor> device = mAvailableOutputDevices.getDeviceFromId( |
| 8450 | patchDesc->mPatch.sinks[i].id); |
| 8451 | if (device == nullptr) { |
| 8452 | ALOGE("%s, no audio device found with id(%d)", |
| 8453 | __func__, patchDesc->mPatch.sinks[i].id); |
| 8454 | return false; |
| 8455 | } |
| 8456 | routedDevices.add(device); |
| 8457 | } |
| 8458 | for (const auto& client : activeClients) { |
jiabin | 4925685 | 2022-03-09 11:21:35 -0800 | [diff] [blame] | 8459 | if (client->isInvalid()) { |
| 8460 | // No need to take care about invalidated clients. |
| 8461 | continue; |
| 8462 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8463 | sp<DeviceDescriptor> preferredDevice = |
| 8464 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId()); |
| 8465 | if (mEngine->getOutputDevicesForAttributes( |
| 8466 | client->attributes(), preferredDevice, false) == routedDevices) { |
| 8467 | return false; |
| 8468 | } |
| 8469 | } |
| 8470 | return true; |
| 8471 | } |
| 8472 | |
| 8473 | sp<SwAudioOutputDescriptor> AudioPolicyManager::openOutputWithProfileAndDevice( |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 8474 | const sp<IOProfile>& profile, const DeviceVector& devices, |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8475 | const audio_config_base_t *mixerConfig, const audio_config_t *halConfig, |
| 8476 | audio_output_flags_t flags) |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8477 | { |
| 8478 | for (const auto& device : devices) { |
| 8479 | // TODO: This should be checking if the profile supports the device combo. |
| 8480 | if (!profile->supportsDevice(device)) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8481 | ALOGE("%s profile(%s) doesn't support device %#x", __func__, profile->getName().c_str(), |
| 8482 | device->type()); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8483 | return nullptr; |
| 8484 | } |
| 8485 | } |
| 8486 | sp<SwAudioOutputDescriptor> desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 8487 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8488 | status_t status = desc->open(halConfig, mixerConfig, devices, |
| 8489 | AUDIO_STREAM_DEFAULT, flags, &output); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8490 | if (status != NO_ERROR) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8491 | ALOGE("%s failed to open output %d", __func__, status); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8492 | return nullptr; |
| 8493 | } |
jiabin | 14b50cc | 2023-12-13 19:01:52 +0000 | [diff] [blame] | 8494 | if ((flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) == AUDIO_OUTPUT_FLAG_BIT_PERFECT) { |
| 8495 | auto portConfig = desc->getConfig(); |
| 8496 | for (const auto& device : devices) { |
| 8497 | device->setPreferredConfig(&portConfig); |
| 8498 | } |
| 8499 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8500 | |
| 8501 | // Here is where the out_set_parameters() for card & device gets called |
| 8502 | sp<DeviceDescriptor> device = devices.getDeviceForOpening(); |
| 8503 | const audio_devices_t deviceType = device->type(); |
| 8504 | const String8 &address = String8(device->address().c_str()); |
Tomasz Wasilczyk | fd9ffd1 | 2023-08-14 17:56:22 +0000 | [diff] [blame] | 8505 | if (!address.empty()) { |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8506 | char *param = audio_device_address_to_parameter(deviceType, address.c_str()); |
| 8507 | mpClientInterface->setParameters(output, String8(param)); |
| 8508 | free(param); |
| 8509 | } |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 8510 | updateAudioProfiles(device, output, profile); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8511 | if (!profile->hasValidAudioProfile()) { |
| 8512 | ALOGW("%s() missing param", __func__); |
| 8513 | desc->close(); |
| 8514 | return nullptr; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8515 | } else if (profile->hasDynamicAudioProfile() && halConfig == nullptr) { |
| 8516 | // Reopen the output with the best audio profile picked by APM when the profile supports |
| 8517 | // dynamic audio profile and the hal config is not specified. |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8518 | desc->close(); |
| 8519 | output = AUDIO_IO_HANDLE_NONE; |
| 8520 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 8521 | profile->pickAudioProfile( |
| 8522 | config.sample_rate, config.channel_mask, config.format); |
| 8523 | config.offload_info.sample_rate = config.sample_rate; |
| 8524 | config.offload_info.channel_mask = config.channel_mask; |
| 8525 | config.offload_info.format = config.format; |
| 8526 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8527 | status = desc->open(&config, mixerConfig, devices, AUDIO_STREAM_DEFAULT, flags, &output); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8528 | if (status != NO_ERROR) { |
| 8529 | return nullptr; |
| 8530 | } |
| 8531 | } |
| 8532 | |
| 8533 | addOutput(output, desc); |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 8534 | |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 8535 | sp<DeviceDescriptor> speaker = mAvailableOutputDevices.getDevice( |
| 8536 | AUDIO_DEVICE_OUT_SPEAKER, String8(""), AUDIO_FORMAT_DEFAULT); |
| 8537 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8538 | if (audio_is_remote_submix_device(deviceType) && address != "0") { |
| 8539 | sp<AudioPolicyMix> policyMix; |
| 8540 | if (mPolicyMixes.getAudioPolicyMix(deviceType, address, policyMix) == NO_ERROR) { |
| 8541 | policyMix->setOutput(desc); |
| 8542 | desc->mPolicyMix = policyMix; |
| 8543 | } else { |
| 8544 | ALOGW("checkOutputsForDevice() cannot find policy for address %s", |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 8545 | address.c_str()); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8546 | } |
| 8547 | |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 8548 | } else if (hasPrimaryOutput() && speaker != nullptr |
| 8549 | && mPrimaryOutput->supportsDevice(speaker) && !desc->supportsDevice(speaker) |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 8550 | && ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) { |
| 8551 | // no duplicated output for: |
| 8552 | // - direct outputs |
| 8553 | // - outputs used by dynamic policy mixes |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 8554 | // - outputs that supports SPEAKER while the primary output does not. |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8555 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
| 8556 | |
| 8557 | //TODO: configure audio effect output stage here |
| 8558 | |
| 8559 | // open a duplicating output thread for the new output and the primary output |
| 8560 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 8561 | new SwAudioOutputDescriptor(nullptr, mpClientInterface); |
| 8562 | status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, &duplicatedOutput); |
| 8563 | if (status == NO_ERROR) { |
| 8564 | // add duplicated output descriptor |
| 8565 | addOutput(duplicatedOutput, dupOutputDesc); |
| 8566 | } else { |
| 8567 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
| 8568 | mPrimaryOutput->mIoHandle, output); |
| 8569 | desc->close(); |
| 8570 | removeOutput(output); |
| 8571 | nextAudioPortGeneration(); |
| 8572 | return nullptr; |
| 8573 | } |
| 8574 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 8575 | if (mPrimaryOutput == nullptr && profile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 8576 | ALOGV("%s(): re-assigning mPrimaryOutput", __func__); |
| 8577 | mPrimaryOutput = desc; |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 8578 | mPrimaryModuleHandle = mPrimaryOutput->getModuleHandle(); |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 8579 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8580 | return desc; |
| 8581 | } |
| 8582 | |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 8583 | status_t AudioPolicyManager::getDevicesForAttributes( |
| 8584 | const audio_attributes_t &attr, DeviceVector &devices, bool forVolume) { |
| 8585 | // Devices are determined in the following precedence: |
| 8586 | // |
| 8587 | // 1) Devices associated with a dynamic policy matching the attributes. This is often |
| 8588 | // a remote submix from MIX_ROUTE_FLAG_LOOP_BACK. |
| 8589 | // |
| 8590 | // If no such dynamic policy then |
| 8591 | // 2) Devices containing an active client using setPreferredDevice |
| 8592 | // with same strategy as the attributes. |
| 8593 | // (from the default Engine::getOutputDevicesForAttributes() implementation). |
| 8594 | // |
| 8595 | // If no corresponding active client with setPreferredDevice then |
| 8596 | // 3) Devices associated with the strategy determined by the attributes |
| 8597 | // (from the default Engine::getOutputDevicesForAttributes() implementation). |
| 8598 | // |
| 8599 | // See related getOutputForAttrInt(). |
| 8600 | |
| 8601 | // check dynamic policies but only for primary descriptors (secondary not used for audible |
| 8602 | // audio routing, only used for duplication for playback capture) |
| 8603 | sp<AudioPolicyMix> policyMix; |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 8604 | bool unneededUsePrimaryOutputFromPolicyMixes = false; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 8605 | status_t status = mPolicyMixes.getOutputForAttr(attr, AUDIO_CONFIG_BASE_INITIALIZER, |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 8606 | 0 /*uid unknown here*/, AUDIO_SESSION_NONE, AUDIO_OUTPUT_FLAG_NONE, |
| 8607 | mAvailableOutputDevices, nullptr /* requestedDevice */, policyMix, |
| 8608 | nullptr /* secondaryMixes */, unneededUsePrimaryOutputFromPolicyMixes); |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 8609 | if (status != OK) { |
| 8610 | return status; |
| 8611 | } |
| 8612 | |
| 8613 | if (policyMix != nullptr && policyMix->getOutput() != nullptr && |
| 8614 | // For volume control, skip LOOPBACK mixes which use AUDIO_DEVICE_OUT_REMOTE_SUBMIX |
| 8615 | // as they are unaffected by device/stream volume |
| 8616 | // (per SwAudioOutputDescriptor::isFixedVolume()). |
| 8617 | (!forVolume || policyMix->mDeviceType != AUDIO_DEVICE_OUT_REMOTE_SUBMIX) |
| 8618 | ) { |
| 8619 | sp<DeviceDescriptor> deviceDesc = mAvailableOutputDevices.getDevice( |
| 8620 | policyMix->mDeviceType, policyMix->mDeviceAddress, AUDIO_FORMAT_DEFAULT); |
| 8621 | devices.add(deviceDesc); |
| 8622 | } else { |
| 8623 | // The default Engine::getOutputDevicesForAttributes() uses findPreferredDevice() |
| 8624 | // which selects setPreferredDevice if active. This means forVolume call |
| 8625 | // will take an active setPreferredDevice, if such exists. |
| 8626 | |
| 8627 | devices = mEngine->getOutputDevicesForAttributes( |
| 8628 | attr, nullptr /* preferredDevice */, false /* fromCache */); |
| 8629 | } |
| 8630 | |
| 8631 | if (forVolume) { |
| 8632 | // We alias the device AUDIO_DEVICE_OUT_SPEAKER_SAFE to AUDIO_DEVICE_OUT_SPEAKER |
| 8633 | // for single volume control in AudioService (such relationship should exist if |
| 8634 | // SPEAKER_SAFE is present). |
| 8635 | // |
| 8636 | // (This is unrelated to a different device grouping as Volume::getDeviceCategory) |
| 8637 | DeviceVector speakerSafeDevices = |
| 8638 | devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER_SAFE); |
| 8639 | if (!speakerSafeDevices.isEmpty()) { |
| 8640 | devices.merge(mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER)); |
| 8641 | devices.remove(speakerSafeDevices); |
| 8642 | } |
| 8643 | } |
| 8644 | |
| 8645 | return NO_ERROR; |
| 8646 | } |
| 8647 | |
| 8648 | status_t AudioPolicyManager::getProfilesForDevices(const DeviceVector& devices, |
| 8649 | AudioProfileVector& audioProfiles, |
| 8650 | uint32_t flags, |
| 8651 | bool isInput) { |
| 8652 | for (const auto& hwModule : mHwModules) { |
| 8653 | // the MSD module checks for different conditions |
| 8654 | if (strcmp(hwModule->getName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0) { |
| 8655 | continue; |
| 8656 | } |
| 8657 | IOProfileCollection ioProfiles = isInput ? hwModule->getInputProfiles() |
| 8658 | : hwModule->getOutputProfiles(); |
| 8659 | for (const auto& profile : ioProfiles) { |
| 8660 | if (!profile->areAllDevicesSupported(devices) || |
| 8661 | !profile->isCompatibleProfileForFlags( |
| 8662 | flags, false /*exactMatchRequiredForInputFlags*/)) { |
| 8663 | continue; |
| 8664 | } |
| 8665 | audioProfiles.addAllValidProfiles(profile->asAudioPort()->getAudioProfiles()); |
| 8666 | } |
| 8667 | } |
| 8668 | |
| 8669 | if (!isInput) { |
| 8670 | // add the direct profiles from MSD if present and has audio patches to all the output(s) |
| 8671 | const auto &msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 8672 | if (msdModule != nullptr) { |
| 8673 | if (msdHasPatchesToAllDevices(devices.toTypeAddrVector())) { |
| 8674 | ALOGV("%s: MSD audio patches set to all output devices.", __func__); |
| 8675 | for (const auto &profile: msdModule->getOutputProfiles()) { |
| 8676 | if (!profile->asAudioPort()->isDirectOutput()) { |
| 8677 | continue; |
| 8678 | } |
| 8679 | audioProfiles.addAllValidProfiles(profile->asAudioPort()->getAudioProfiles()); |
| 8680 | } |
| 8681 | } else { |
| 8682 | ALOGV("%s: MSD audio patches NOT set to all output devices.", __func__); |
| 8683 | } |
| 8684 | } |
| 8685 | } |
| 8686 | |
| 8687 | return NO_ERROR; |
| 8688 | } |
| 8689 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 8690 | sp<SwAudioOutputDescriptor> AudioPolicyManager::reopenOutput(sp<SwAudioOutputDescriptor> outputDesc, |
| 8691 | const audio_config_t *config, |
| 8692 | audio_output_flags_t flags, |
| 8693 | const char* caller) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8694 | closeOutput(outputDesc->mIoHandle); |
| 8695 | sp<SwAudioOutputDescriptor> preferredOutput = openOutputWithProfileAndDevice( |
| 8696 | outputDesc->mProfile, outputDesc->devices(), nullptr /*mixerConfig*/, config, flags); |
| 8697 | if (preferredOutput == nullptr) { |
| 8698 | ALOGE("%s failed to reopen output device=%d, caller=%s", |
| 8699 | __func__, outputDesc->devices()[0]->getId(), caller); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8700 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 8701 | return preferredOutput; |
| 8702 | } |
| 8703 | |
| 8704 | void AudioPolicyManager::reopenOutputsWithDevices( |
| 8705 | const std::map<audio_io_handle_t, DeviceVector> &outputsToReopen) { |
| 8706 | for (const auto& [output, devices] : outputsToReopen) { |
| 8707 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 8708 | closeOutput(output); |
| 8709 | openOutputWithProfileAndDevice(desc->mProfile, devices); |
| 8710 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8711 | } |
| 8712 | |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 8713 | PortHandleVector AudioPolicyManager::getClientsForStream( |
| 8714 | audio_stream_type_t streamType) const { |
| 8715 | PortHandleVector clients; |
| 8716 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 8717 | PortHandleVector clientsForStream = mOutputs.valueAt(i)->getClientsForStream(streamType); |
| 8718 | clients.insert(clients.end(), clientsForStream.begin(), clientsForStream.end()); |
| 8719 | } |
| 8720 | return clients; |
| 8721 | } |
| 8722 | |
| 8723 | void AudioPolicyManager::invalidateStreams(StreamTypeVector streams) const { |
| 8724 | PortHandleVector clients; |
| 8725 | for (auto stream : streams) { |
| 8726 | PortHandleVector clientsForStream = getClientsForStream(stream); |
| 8727 | clients.insert(clients.end(), clientsForStream.begin(), clientsForStream.end()); |
| 8728 | } |
| 8729 | mpClientInterface->invalidateTracks(clients); |
| 8730 | } |
| 8731 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 8732 | } // namespace android |