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> |
jiabin | 7ed22ff | 2024-10-04 20:51:21 +0000 | [diff] [blame^] | 43 | #include <android/media/audio/common/AudioMMapPolicy.h> |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 44 | #include <android/media/audio/common/AudioPort.h> |
Andy Hung | 481bfe3 | 2023-12-18 14:00:29 -0800 | [diff] [blame] | 45 | #include <com_android_media_audio.h> |
Marvin Ramin | bdefaf0 | 2023-11-01 09:10:32 +0100 | [diff] [blame] | 46 | #include <android_media_audiopolicy.h> |
Atneya Nair | b16666a | 2023-12-11 20:18:33 -0800 | [diff] [blame] | 47 | #include <com_android_media_audioserver.h> |
Glenn Kasten | 76a1344 | 2020-07-01 12:10:59 -0700 | [diff] [blame] | 48 | #include <cutils/bitops.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" |
Shunkai Yao | 2dcd60c | 2024-08-27 21:08:53 +0000 | [diff] [blame] | 58 | #include "SpatializerHelper.h" |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 59 | #include "TypeConverter.h" |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 60 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 61 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 62 | |
Marvin Ramin | bdefaf0 | 2023-11-01 09:10:32 +0100 | [diff] [blame] | 63 | |
| 64 | namespace audio_flags = android::media::audiopolicy; |
| 65 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 66 | using android::media::audio::common::AudioDevice; |
| 67 | using android::media::audio::common::AudioDeviceAddress; |
jiabin | 7ed22ff | 2024-10-04 20:51:21 +0000 | [diff] [blame^] | 68 | using android::media::audio::common::AudioDeviceDescription; |
| 69 | using android::media::audio::common::AudioMMapPolicy; |
| 70 | using android::media::audio::common::AudioMMapPolicyInfo; |
| 71 | using android::media::audio::common::AudioMMapPolicyType; |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 72 | using android::media::audio::common::AudioPortDeviceExt; |
| 73 | using android::media::audio::common::AudioPortExt; |
Eric Laurent | b2fb410 | 2024-06-21 12:25:26 +0000 | [diff] [blame] | 74 | using com::android::media::audioserver::fix_call_audio_patch; |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 75 | using content::AttributionSourceState; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 76 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 77 | //FIXME: workaround for truncated touch sounds |
| 78 | // to be removed when the problem is handled by system UI |
| 79 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 80 | |
| 81 | // Largest difference in dB on earpiece in call between the voice volume and another |
| 82 | // media / notification / system volume. |
| 83 | constexpr float IN_CALL_EARPIECE_HEADROOM_DB = 3.f; |
| 84 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 85 | template <typename T> |
| 86 | bool operator== (const SortedVector<T> &left, const SortedVector<T> &right) |
| 87 | { |
| 88 | if (left.size() != right.size()) { |
| 89 | return false; |
| 90 | } |
| 91 | for (size_t index = 0; index < right.size(); index++) { |
| 92 | if (left[index] != right[index]) { |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | template <typename T> |
| 100 | bool operator!= (const SortedVector<T> &left, const SortedVector<T> &right) |
| 101 | { |
| 102 | return !(left == right); |
| 103 | } |
| 104 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 105 | // ---------------------------------------------------------------------------- |
| 106 | // AudioPolicyInterface implementation |
| 107 | // ---------------------------------------------------------------------------- |
| 108 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 109 | status_t AudioPolicyManager::setDeviceConnectionState(audio_policy_dev_state_t state, |
| 110 | const android::media::audio::common::AudioPort& port, audio_format_t encodedFormat) { |
| 111 | status_t status = setDeviceConnectionStateInt(state, port, encodedFormat); |
jiabin | a7b4379 | 2018-02-15 16:04:46 -0800 | [diff] [blame] | 112 | nextAudioPortGeneration(); |
| 113 | return status; |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 116 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
| 117 | audio_policy_dev_state_t state, |
| 118 | const char* device_address, |
| 119 | const char* device_name, |
| 120 | audio_format_t encodedFormat) { |
Atneya Nair | 638a6e4 | 2022-12-18 16:45:15 -0800 | [diff] [blame] | 121 | media::AudioPortFw aidlPort; |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 122 | if (status_t status = deviceToAudioPort(device, device_address, device_name, &aidlPort); |
| 123 | status == OK) { |
| 124 | return setDeviceConnectionState(state, aidlPort.hal, encodedFormat); |
| 125 | } else { |
| 126 | ALOGE("Failed to convert to AudioPort Parcelable: %s", statusToString(status).c_str()); |
| 127 | return status; |
| 128 | } |
| 129 | } |
| 130 | |
Ping Tsai | 2a5a524 | 2024-08-16 13:39:10 +0800 | [diff] [blame] | 131 | status_t AudioPolicyManager::broadcastDeviceConnectionState(const sp<DeviceDescriptor> &device, |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 132 | media::DeviceConnectedState state) |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 133 | { |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 134 | audio_port_v7 devicePort; |
| 135 | device->toAudioPort(&devicePort); |
Ping Tsai | 2a5a524 | 2024-08-16 13:39:10 +0800 | [diff] [blame] | 136 | status_t status = mpClientInterface->setDeviceConnectedState(&devicePort, state); |
| 137 | ALOGE_IF(status != OK, "Error %d while setting connected state %d for device %s", status, |
| 138 | static_cast<int>(state), device->getDeviceTypeAddr().toString(false).c_str()); |
| 139 | |
| 140 | return status; |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 143 | status_t AudioPolicyManager::setDeviceConnectionStateInt( |
| 144 | audio_policy_dev_state_t state, const android::media::audio::common::AudioPort& port, |
| 145 | audio_format_t encodedFormat) { |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 146 | if (port.ext.getTag() != AudioPortExt::device) { |
| 147 | return BAD_VALUE; |
| 148 | } |
| 149 | audio_devices_t device_type; |
| 150 | std::string device_address; |
| 151 | if (status_t status = aidl2legacy_AudioDevice_audio_device( |
| 152 | port.ext.get<AudioPortExt::device>().device, &device_type, &device_address); |
| 153 | status != OK) { |
| 154 | return status; |
| 155 | }; |
| 156 | const char* device_name = port.name.c_str(); |
| 157 | // connect/disconnect only 1 device at a time |
| 158 | if (!audio_is_output_device(device_type) && !audio_is_input_device(device_type)) |
| 159 | return BAD_VALUE; |
| 160 | |
| 161 | sp<DeviceDescriptor> device = mHwModules.getDeviceDescriptor( |
| 162 | device_type, device_address.c_str(), device_name, encodedFormat, |
| 163 | state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Mikhail Naganov | ddc5f31 | 2022-06-11 00:47:52 +0000 | [diff] [blame] | 164 | if (device == nullptr) { |
| 165 | return INVALID_OPERATION; |
| 166 | } |
| 167 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 168 | device->setExtraAudioDescriptors(port.extraAudioDescriptors); |
| 169 | } |
| 170 | return setDeviceConnectionStateInt(device, state); |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 171 | } |
| 172 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 173 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t deviceType, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 174 | audio_policy_dev_state_t state, |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 175 | const char* device_address, |
| 176 | const char* device_name, |
| 177 | audio_format_t encodedFormat) { |
Atneya Nair | 638a6e4 | 2022-12-18 16:45:15 -0800 | [diff] [blame] | 178 | media::AudioPortFw aidlPort; |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 179 | if (status_t status = deviceToAudioPort(deviceType, device_address, device_name, &aidlPort); |
| 180 | status == OK) { |
| 181 | return setDeviceConnectionStateInt(state, aidlPort.hal, encodedFormat); |
| 182 | } else { |
| 183 | ALOGE("Failed to convert to AudioPort Parcelable: %s", statusToString(status).c_str()); |
| 184 | return status; |
| 185 | } |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 186 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 187 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 188 | status_t AudioPolicyManager::setDeviceConnectionStateInt(const sp<DeviceDescriptor> &device, |
| 189 | audio_policy_dev_state_t state) |
| 190 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 191 | // handle output devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 192 | if (audio_is_output_device(device->type())) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 193 | SortedVector <audio_io_handle_t> outputs; |
| 194 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 195 | ssize_t index = mAvailableOutputDevices.indexOf(device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 196 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 197 | // save a copy of the opened output descriptors before any output is opened or closed |
| 198 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 199 | mPreviousOutputs = mOutputs; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 200 | |
| 201 | bool wasLeUnicastActive = isLeUnicastActive(); |
| 202 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 203 | switch (state) |
| 204 | { |
| 205 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 206 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 207 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 208 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 209 | return INVALID_OPERATION; |
| 210 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 211 | ALOGV("%s() connecting device %s format %x", |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 212 | __func__, device->toString().c_str(), device->getEncodedFormat()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 213 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 214 | // register new device as available |
Francois Gaffie | 993f390 | 2019-04-10 15:39:27 +0200 | [diff] [blame] | 215 | if (mAvailableOutputDevices.add(device) < 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 216 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 217 | } |
| 218 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 219 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 220 | // parameters on newly connected devices (instead of opening the outputs...) |
Ping Tsai | 2a5a524 | 2024-08-16 13:39:10 +0800 | [diff] [blame] | 221 | if (broadcastDeviceConnectionState( |
| 222 | device, media::DeviceConnectedState::CONNECTED) != NO_ERROR) { |
| 223 | mAvailableOutputDevices.remove(device); |
| 224 | mHwModules.cleanUpForDevice(device); |
| 225 | ALOGE("%s() device %s format %x connection failed", __func__, |
| 226 | device->toString().c_str(), device->getEncodedFormat()); |
| 227 | return INVALID_OPERATION; |
| 228 | } |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 229 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 230 | if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) { |
| 231 | mAvailableOutputDevices.remove(device); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 232 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 233 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED); |
Mikhail Naganov | f88c2f3 | 2024-04-16 15:01:13 -0700 | [diff] [blame] | 234 | |
| 235 | mHwModules.cleanUpForDevice(device); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 236 | return INVALID_OPERATION; |
| 237 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 238 | |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 239 | // Populate encapsulation information when a output device is connected. |
| 240 | device->setEncapsulationInfoFromHal(mpClientInterface); |
| 241 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 242 | // outputs should never be empty here |
| 243 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 244 | "checkOutputsForDevice() returned no outputs but status OK"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 245 | ALOGV("%s() checkOutputsForDevice() returned %zu outputs", __func__, outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 246 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 247 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 248 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 249 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 250 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 251 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 252 | return INVALID_OPERATION; |
| 253 | } |
| 254 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 255 | ALOGV("%s() disconnecting output device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 256 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 257 | // Notify the HAL to prepare to disconnect device |
| 258 | broadcastDeviceConnectionState( |
| 259 | device, media::DeviceConnectedState::PREPARE_TO_DISCONNECT); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 260 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 261 | // remove device from available output devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 262 | mAvailableOutputDevices.remove(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 263 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 264 | mOutputs.clearSessionRoutesForDevice(device); |
| 265 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 266 | checkOutputsForDevice(device, state, outputs); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 267 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 268 | // Send Disconnect to HALs |
| 269 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED); |
| 270 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 271 | // Reset active device codec |
| 272 | device->setEncodedFormat(AUDIO_FORMAT_DEFAULT); |
| 273 | |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 274 | // remove device from mReportedFormatsMap cache |
| 275 | mReportedFormatsMap.erase(device); |
| 276 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 277 | // remove preferred mixer configurations |
| 278 | mPreferredMixerAttrInfos.erase(device->getId()); |
| 279 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 280 | } break; |
| 281 | |
| 282 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 283 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 284 | return BAD_VALUE; |
| 285 | } |
| 286 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 287 | // Propagate device availability to Engine |
| 288 | setEngineDeviceConnectionState(device, state); |
| 289 | |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 290 | // No need to evaluate playback routing when connecting a remote submix |
| 291 | // output device used by a dynamic policy of type recorder as no |
| 292 | // playback use case is affected. |
| 293 | bool doCheckForDeviceAndOutputChanges = true; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 294 | if (device->type() == AUDIO_DEVICE_OUT_REMOTE_SUBMIX && device->address() != "0") { |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 295 | for (audio_io_handle_t output : outputs) { |
| 296 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 297 | sp<AudioPolicyMix> policyMix = desc->mPolicyMix.promote(); |
| 298 | if (policyMix != nullptr |
| 299 | && policyMix->mMixType == MIX_TYPE_RECORDERS |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 300 | && device->address() == policyMix->mDeviceAddress.c_str()) { |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 301 | doCheckForDeviceAndOutputChanges = false; |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | auto checkCloseOutputs = [&]() { |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 308 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 309 | if (!outputs.isEmpty()) { |
| 310 | for (audio_io_handle_t output : outputs) { |
| 311 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 312 | // close unused outputs after device disconnection or direct outputs that have |
| 313 | // been opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 314 | // "outputs" vector never contains duplicated outputs |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 315 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) |
| 316 | || (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 317 | (desc->mDirectOpenCount == 0)) |
| 318 | || (((desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) && |
| 319 | !isOutputOnlyAvailableRouteToSomeDevice(desc))) { |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 320 | clearAudioSourcesForOutput(output); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 321 | closeOutput(output); |
| 322 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 323 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 324 | // check A2DP again after closing A2DP output to reset mA2dpSuspended if needed |
| 325 | return true; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 326 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 327 | return false; |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 328 | }; |
| 329 | |
| 330 | if (doCheckForDeviceAndOutputChanges) { |
| 331 | checkForDeviceAndOutputChanges(checkCloseOutputs); |
| 332 | } else { |
| 333 | checkCloseOutputs(); |
| 334 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 335 | (void)updateCallRouting(false /*fromCache*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 336 | const DeviceVector msdOutDevices = getMsdAudioOutDevices(); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 337 | const DeviceVector activeMediaDevices = |
| 338 | mEngine->getActiveMediaDevices(mAvailableOutputDevices); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 339 | std::map<audio_io_handle_t, DeviceVector> outputsToReopenWithDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 340 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 341 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jaideep Sharma | 89b5b85 | 2020-11-23 16:41:33 +0530 | [diff] [blame] | 342 | if (desc->isActive() && ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || |
| 343 | (desc != mPrimaryOutput))) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 344 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 345 | // do not force device change on duplicated output because if device is 0, it will |
| 346 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 347 | // a valid device selection on those outputs. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 348 | bool force = (msdOutDevices.isEmpty() || msdOutDevices != desc->devices()) |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 349 | && !desc->isDuplicated() |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 350 | && (!device_distinguishes_on_address(device->type()) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 351 | // always force when disconnecting (a non-duplicated device) |
| 352 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 353 | if (desc->mPreferredAttrInfo != nullptr && newDevices != desc->devices()) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 354 | // If the device is using preferred mixer attributes, the output need to reopen |
| 355 | // with default configuration when the new selected devices are different from |
| 356 | // current routing devices |
| 357 | outputsToReopenWithDevices.emplace(mOutputs.keyAt(i), newDevices); |
| 358 | continue; |
| 359 | } |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 360 | setOutputDevices(__func__, desc, newDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 361 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 362 | if (!desc->isDuplicated() && desc->mProfile->hasDynamicAudioProfile() && |
jiabin | 498d215 | 2021-04-02 00:26:46 +0000 | [diff] [blame] | 363 | !activeMediaDevices.empty() && desc->devices() != activeMediaDevices && |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 364 | desc->supportsDevicesForPlayback(activeMediaDevices)) { |
| 365 | // Reopen the output to query the dynamic profiles when there is not active |
| 366 | // clients or all active clients will be rerouted. Otherwise, set the flag |
| 367 | // `mPendingReopenToQueryProfiles` in the SwOutputDescriptor so that the output |
| 368 | // can be reopened to query dynamic profiles when all clients are inactive. |
| 369 | if (areAllActiveTracksRerouted(desc)) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 370 | outputsToReopenWithDevices.emplace(mOutputs.keyAt(i), activeMediaDevices); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 371 | } else { |
| 372 | desc->mPendingReopenToQueryProfiles = true; |
| 373 | } |
| 374 | } |
| 375 | if (!desc->supportsDevicesForPlayback(activeMediaDevices)) { |
| 376 | // Clear the flag that previously set for re-querying profiles. |
| 377 | desc->mPendingReopenToQueryProfiles = false; |
| 378 | } |
| 379 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 380 | reopenOutputsWithDevices(outputsToReopenWithDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 381 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 382 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 383 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 386 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, 0); |
| 387 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 388 | mpClientInterface->onAudioPortListUpdate(); |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 389 | ALOGV("%s() completed for device: %s", __func__, device->toString().c_str()); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 390 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 391 | } // end if is output device |
| 392 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 393 | // handle input devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 394 | if (audio_is_input_device(device->type())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 395 | ssize_t index = mAvailableInputDevices.indexOf(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 396 | switch (state) |
| 397 | { |
| 398 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 399 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 400 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 401 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 402 | return INVALID_OPERATION; |
| 403 | } |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 404 | |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 405 | ALOGV("%s() connecting device %s", __func__, device->toString().c_str()); |
| 406 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 407 | if (mAvailableInputDevices.add(device) < 0) { |
| 408 | return NO_MEMORY; |
| 409 | } |
| 410 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 411 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 412 | // parameters on newly connected devices (instead of opening the inputs...) |
Ping Tsai | 2a5a524 | 2024-08-16 13:39:10 +0800 | [diff] [blame] | 413 | if (broadcastDeviceConnectionState( |
| 414 | device, media::DeviceConnectedState::CONNECTED) != NO_ERROR) { |
| 415 | mAvailableInputDevices.remove(device); |
| 416 | mHwModules.cleanUpForDevice(device); |
| 417 | ALOGE("%s() device %s format %x connection failed", __func__, |
| 418 | device->toString().c_str(), device->getEncodedFormat()); |
| 419 | return INVALID_OPERATION; |
| 420 | } |
Mikhail Naganov | c66ffc1 | 2024-05-30 16:56:25 -0700 | [diff] [blame] | 421 | // Propagate device availability to Engine |
| 422 | setEngineDeviceConnectionState(device, state); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 423 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 424 | if (checkInputsForDevice(device, state) != NO_ERROR) { |
Mikhail Naganov | c66ffc1 | 2024-05-30 16:56:25 -0700 | [diff] [blame] | 425 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); |
| 426 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 427 | mAvailableInputDevices.remove(device); |
| 428 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 429 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 430 | |
| 431 | mHwModules.cleanUpForDevice(device); |
| 432 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 433 | return INVALID_OPERATION; |
| 434 | } |
| 435 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 436 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 437 | |
| 438 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 439 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 440 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 441 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 442 | return INVALID_OPERATION; |
| 443 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 444 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 445 | ALOGV("%s() disconnecting input device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 446 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 447 | // Notify the HAL to prepare to disconnect device |
| 448 | broadcastDeviceConnectionState( |
| 449 | device, media::DeviceConnectedState::PREPARE_TO_DISCONNECT); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 450 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 451 | mAvailableInputDevices.remove(device); |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 452 | |
| 453 | checkInputsForDevice(device, state); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 454 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 455 | // Set Disconnect to HALs |
| 456 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED); |
| 457 | |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 458 | // remove device from mReportedFormatsMap cache |
| 459 | mReportedFormatsMap.erase(device); |
Mikhail Naganov | c66ffc1 | 2024-05-30 16:56:25 -0700 | [diff] [blame] | 460 | |
| 461 | // Propagate device availability to Engine |
| 462 | setEngineDeviceConnectionState(device, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 463 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 464 | |
| 465 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 466 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 467 | return BAD_VALUE; |
| 468 | } |
| 469 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 470 | checkCloseInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 471 | // As the input device list can impact the output device selection, update |
| 472 | // getDeviceForStrategy() cache |
| 473 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 474 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 475 | (void)updateCallRouting(false /*fromCache*/); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 476 | // Reconnect Audio Source |
| 477 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { |
| 478 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); |
| 479 | checkAudioSourceForAttributes(attributes); |
| 480 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 481 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 482 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 485 | mpClientInterface->onAudioPortListUpdate(); |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 486 | ALOGV("%s() completed for device: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 487 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 488 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 489 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 490 | ALOGW("%s() invalid device: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 491 | return BAD_VALUE; |
| 492 | } |
| 493 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 494 | status_t AudioPolicyManager::deviceToAudioPort(audio_devices_t device, const char* device_address, |
| 495 | const char* device_name, |
Atneya Nair | 638a6e4 | 2022-12-18 16:45:15 -0800 | [diff] [blame] | 496 | media::AudioPortFw* aidlPort) { |
Andy Hung | 5b9a611 | 2023-08-09 19:56:57 -0700 | [diff] [blame] | 497 | const auto devDescr = sp<DeviceDescriptorBase>::make(device, device_address); |
| 498 | devDescr->setName(device_name); |
| 499 | return devDescr->writeToParcelable(aidlPort); |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 500 | } |
| 501 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 502 | void AudioPolicyManager::setEngineDeviceConnectionState(const sp<DeviceDescriptor> device, |
| 503 | audio_policy_dev_state_t state) { |
| 504 | |
| 505 | // the Engine does not have to know about remote submix devices used by dynamic audio policies |
| 506 | if (audio_is_remote_submix_device(device->type()) && device->address() != "0") { |
| 507 | return; |
| 508 | } |
| 509 | mEngine->setDeviceConnectionState(device, state); |
| 510 | } |
| 511 | |
| 512 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 513 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 514 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 515 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 516 | sp<DeviceDescriptor> devDesc = |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 517 | mHwModules.getDeviceDescriptor(device, device_address, "", AUDIO_FORMAT_DEFAULT, |
| 518 | false /* allowToCreate */, |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 519 | (strlen(device_address) != 0)/*matchAddress*/); |
| 520 | |
| 521 | if (devDesc == 0) { |
François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 522 | ALOGV("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 523 | device, device_address); |
| 524 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 525 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 526 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 527 | DeviceVector *deviceVector; |
| 528 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 529 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 530 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 531 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 532 | deviceVector = &mAvailableInputDevices; |
| 533 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 534 | ALOGW("%s() invalid device type %08x", __func__, device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 535 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 536 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 537 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 538 | return (deviceVector->getDevice( |
| 539 | device, String8(device_address), AUDIO_FORMAT_DEFAULT) != 0) ? |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 540 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 541 | } |
| 542 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 543 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 544 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 545 | const char *device_name, |
| 546 | audio_format_t encodedFormat) |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 547 | { |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 548 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s encodedFormat: 0x%X", |
| 549 | device, device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 550 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 551 | // connect/disconnect only 1 device at a time |
| 552 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 553 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 554 | // Check if the device is currently connected |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 555 | DeviceVector deviceList = mAvailableOutputDevices.getDevicesFromType(device); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 556 | if (deviceList.empty()) { |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 557 | // Nothing to do: device is not connected |
| 558 | return NO_ERROR; |
| 559 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 560 | sp<DeviceDescriptor> devDesc = deviceList.itemAt(0); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 561 | |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 562 | // For offloaded A2DP, Hw modules may have the capability to |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 563 | // configure codecs. |
| 564 | // Handle two specific cases by sending a set parameter to |
| 565 | // configure A2DP codecs. No need to toggle device state. |
| 566 | // Case 1: A2DP active device switches from primary to primary |
| 567 | // module |
| 568 | // Case 2: A2DP device config changes on primary module. |
Eric Laurent | 7e3c083 | 2023-11-30 15:04:50 +0100 | [diff] [blame] | 569 | if (device_has_encoding_capability(device) && hasPrimaryOutput()) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 570 | sp<HwModule> module = mHwModules.getModuleForDeviceType(device, encodedFormat); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 571 | audio_module_handle_t primaryHandle = mPrimaryOutput->getModuleHandle(); |
| 572 | if (availablePrimaryOutputDevices().contains(devDesc) && |
| 573 | (module != 0 && module->getHandle() == primaryHandle)) { |
Eric Laurent | 7e3c083 | 2023-11-30 15:04:50 +0100 | [diff] [blame] | 574 | bool isA2dp = audio_is_a2dp_out_device(device); |
| 575 | const String8 supportKey = isA2dp ? String8(AudioParameter::keyReconfigA2dpSupported) |
| 576 | : String8(AudioParameter::keyReconfigLeSupported); |
| 577 | String8 reply = mpClientInterface->getParameters(AUDIO_IO_HANDLE_NONE, supportKey); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 578 | AudioParameter repliedParameters(reply); |
Eric Laurent | 7e3c083 | 2023-11-30 15:04:50 +0100 | [diff] [blame] | 579 | int isReconfigSupported; |
| 580 | repliedParameters.getInt(supportKey, isReconfigSupported); |
| 581 | if (isReconfigSupported) { |
| 582 | const String8 key = isA2dp ? String8(AudioParameter::keyReconfigA2dp) |
| 583 | : String8(AudioParameter::keyReconfigLe); |
| 584 | AudioParameter param; |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 585 | param.add(key, String8("true")); |
| 586 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 587 | devDesc->setEncodedFormat(encodedFormat); |
| 588 | return NO_ERROR; |
| 589 | } |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
cnx421 | bd2dcc4 | 2020-07-11 14:58:44 +0800 | [diff] [blame] | 592 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | cd0f24f | 2024-05-16 13:11:39 +0000 | [diff] [blame] | 593 | uint32_t muteWaitMs = 0; |
cnx421 | bd2dcc4 | 2020-07-11 14:58:44 +0800 | [diff] [blame] | 594 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 595 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | cd0f24f | 2024-05-16 13:11:39 +0000 | [diff] [blame] | 596 | // mute media strategies to avoid sending the music tail into |
| 597 | // the earpiece or headset. |
| 598 | if (desc->isStrategyActive(musicStrategy)) { |
| 599 | uint32_t tempRecommendedMuteDuration = desc->getRecommendedMuteDurationMs(); |
| 600 | uint32_t tempMuteDurationMs = tempRecommendedMuteDuration > 0 ? |
| 601 | tempRecommendedMuteDuration : desc->latency() * 4; |
| 602 | if (muteWaitMs < tempMuteDurationMs) { |
| 603 | muteWaitMs = tempMuteDurationMs; |
| 604 | } |
| 605 | } |
cnx421 | bd2dcc4 | 2020-07-11 14:58:44 +0800 | [diff] [blame] | 606 | setStrategyMute(musicStrategy, true, desc); |
| 607 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, |
| 608 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 609 | nullptr, true /*fromCache*/).types()); |
| 610 | } |
Eric Laurent | cd0f24f | 2024-05-16 13:11:39 +0000 | [diff] [blame] | 611 | // Wait for the muted audio to propagate down the audio path see checkDeviceMuteStrategies(). |
| 612 | // We assume that MUTE_TIME_MS is way larger than muteWaitMs so that unmuting still |
| 613 | // happens after the actual device switch. |
| 614 | if (muteWaitMs > 0) { |
| 615 | ALOGW_IF(MUTE_TIME_MS < muteWaitMs * 2, "%s excessive mute wait %d", __func__, muteWaitMs); |
| 616 | usleep(muteWaitMs * 1000); |
| 617 | } |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 618 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 619 | // This will force reading again the device configuration |
Eric Laurent | 7e3c083 | 2023-11-30 15:04:50 +0100 | [diff] [blame] | 620 | status_t status = setDeviceConnectionState(device, |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 621 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 622 | device_address, device_name, |
| 623 | devDesc->getEncodedFormat()); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 624 | if (status != NO_ERROR) { |
| 625 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 626 | status); |
| 627 | return status; |
| 628 | } |
| 629 | |
| 630 | status = setDeviceConnectionState(device, |
| 631 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 632 | device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 633 | if (status != NO_ERROR) { |
| 634 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 635 | status); |
| 636 | return status; |
| 637 | } |
| 638 | |
| 639 | return NO_ERROR; |
| 640 | } |
| 641 | |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 642 | status_t AudioPolicyManager::getHwOffloadFormatsSupportedForBluetoothMedia( |
| 643 | audio_devices_t device, std::vector<audio_format_t> *formats) |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 644 | { |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 645 | ALOGV("getHwOffloadFormatsSupportedForBluetoothMedia()"); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 646 | status_t status = NO_ERROR; |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 647 | std::unordered_set<audio_format_t> formatSet; |
| 648 | sp<HwModule> primaryModule = |
| 649 | mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY); |
Aniket Kumar Lata | fedb598 | 2019-07-03 11:20:36 -0700 | [diff] [blame] | 650 | if (primaryModule == nullptr) { |
| 651 | ALOGE("%s() unable to get primary module", __func__); |
| 652 | return NO_INIT; |
| 653 | } |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 654 | |
| 655 | DeviceTypeSet audioDeviceSet; |
| 656 | |
| 657 | switch(device) { |
| 658 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: |
| 659 | audioDeviceSet = getAudioDeviceOutAllA2dpSet(); |
| 660 | break; |
| 661 | case AUDIO_DEVICE_OUT_BLE_HEADSET: |
Patty Huang | 36028df | 2022-07-06 00:14:12 +0800 | [diff] [blame] | 662 | audioDeviceSet = getAudioDeviceOutLeAudioUnicastSet(); |
| 663 | break; |
| 664 | case AUDIO_DEVICE_OUT_BLE_BROADCAST: |
| 665 | audioDeviceSet = getAudioDeviceOutLeAudioBroadcastSet(); |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 666 | break; |
| 667 | default: |
| 668 | ALOGE("%s() device type 0x%08x not supported", __func__, device); |
| 669 | return BAD_VALUE; |
| 670 | } |
| 671 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 672 | DeviceVector declaredDevices = primaryModule->getDeclaredDevices().getDevicesFromTypes( |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 673 | audioDeviceSet); |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 674 | for (const auto& device : declaredDevices) { |
| 675 | formatSet.insert(device->encodedFormats().begin(), device->encodedFormats().end()); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 676 | } |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 677 | formats->assign(formatSet.begin(), formatSet.end()); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 678 | return status; |
| 679 | } |
| 680 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 681 | DeviceVector AudioPolicyManager::selectBestRxSinkDevicesForCall(bool fromCache) |
| 682 | { |
| 683 | DeviceVector rxSinkdevices{}; |
| 684 | rxSinkdevices = mEngine->getOutputDevicesForAttributes( |
| 685 | attributes_initializer(AUDIO_USAGE_VOICE_COMMUNICATION), nullptr, fromCache); |
| 686 | if (!rxSinkdevices.isEmpty() && mAvailableOutputDevices.contains(rxSinkdevices.itemAt(0))) { |
| 687 | auto rxSinkDevice = rxSinkdevices.itemAt(0); |
| 688 | auto telephonyRxModule = mHwModules.getModuleForDeviceType( |
| 689 | AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); |
| 690 | // retrieve Rx Source device descriptor |
| 691 | sp<DeviceDescriptor> rxSourceDevice = mAvailableInputDevices.getDevice( |
| 692 | AUDIO_DEVICE_IN_TELEPHONY_RX, String8(), AUDIO_FORMAT_DEFAULT); |
| 693 | |
| 694 | // RX Telephony and Rx sink devices are declared by Primary Audio HAL |
| 695 | if (isPrimaryModule(telephonyRxModule) && (telephonyRxModule->getHalVersionMajor() >= 3) && |
| 696 | telephonyRxModule->supportsPatch(rxSourceDevice, rxSinkDevice)) { |
| 697 | ALOGW("%s() device %s using HW Bridge", __func__, rxSinkDevice->toString().c_str()); |
| 698 | return DeviceVector(rxSinkDevice); |
| 699 | } |
| 700 | } |
| 701 | // Note that despite the fact that getNewOutputDevices() is called on the primary output, |
| 702 | // the device returned is not necessarily reachable via this output |
| 703 | // (filter later by setOutputDevices()) |
| 704 | return getNewOutputDevices(mPrimaryOutput, fromCache); |
| 705 | } |
| 706 | |
| 707 | status_t AudioPolicyManager::updateCallRouting(bool fromCache, uint32_t delayMs, uint32_t *waitMs) |
| 708 | { |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 709 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 710 | DeviceVector rxDevices = selectBestRxSinkDevicesForCall(fromCache); |
| 711 | return updateCallRoutingInternal(rxDevices, delayMs, waitMs); |
| 712 | } |
| 713 | return INVALID_OPERATION; |
| 714 | } |
| 715 | |
| 716 | status_t AudioPolicyManager::updateCallRoutingInternal( |
| 717 | const DeviceVector &rxDevices, uint32_t delayMs, uint32_t *waitMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 718 | { |
| 719 | bool createTxPatch = false; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 720 | bool createRxPatch = false; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 721 | uint32_t muteWaitMs = 0; |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 722 | if (hasPrimaryOutput() && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 723 | mPrimaryOutput->devices().onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_STUB)) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 724 | return INVALID_OPERATION; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 725 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 726 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 727 | audio_attributes_t attr = { .source = AUDIO_SOURCE_VOICE_COMMUNICATION }; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 728 | auto txSourceDevice = mEngine->getInputDeviceForAttributes(attr); |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 729 | |
Eric Laurent | b2fb410 | 2024-06-21 12:25:26 +0000 | [diff] [blame] | 730 | if (!fix_call_audio_patch()) { |
| 731 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 732 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
| 733 | } |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 734 | |
| 735 | if (rxDevices.isEmpty()) { |
| 736 | ALOGW("%s() no selected output device", __func__); |
| 737 | return INVALID_OPERATION; |
| 738 | } |
Eric Laurent | cedd5b5 | 2023-03-22 00:03:31 +0000 | [diff] [blame] | 739 | if (txSourceDevice == nullptr) { |
| 740 | ALOGE("%s() selected input device not available", __func__); |
| 741 | return INVALID_OPERATION; |
| 742 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 743 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 744 | ALOGV("%s device rxDevice %s txDevice %s", __func__, |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 745 | rxDevices.itemAt(0)->toString().c_str(), txSourceDevice->toString().c_str()); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 746 | |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 747 | auto telephonyRxModule = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 748 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 749 | auto telephonyTxModule = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 750 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_OUT_TELEPHONY_TX, AUDIO_FORMAT_DEFAULT); |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 751 | // retrieve Rx Source and Tx Sink device descriptors |
| 752 | sp<DeviceDescriptor> rxSourceDevice = |
| 753 | mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX, |
| 754 | String8(), |
| 755 | AUDIO_FORMAT_DEFAULT); |
| 756 | sp<DeviceDescriptor> txSinkDevice = |
| 757 | mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, |
| 758 | String8(), |
| 759 | AUDIO_FORMAT_DEFAULT); |
| 760 | |
| 761 | // RX and TX Telephony device are declared by Primary Audio HAL |
| 762 | if (isPrimaryModule(telephonyRxModule) && isPrimaryModule(telephonyTxModule) && |
| 763 | (telephonyRxModule->getHalVersionMajor() >= 3)) { |
| 764 | if (rxSourceDevice == 0 || txSinkDevice == 0) { |
| 765 | // RX / TX Telephony device(s) is(are) not currently available |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 766 | ALOGE("%s() no telephony Tx and/or RX device", __func__); |
| 767 | return INVALID_OPERATION; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 768 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 769 | // createAudioPatchInternal now supports both HW / SW bridging |
| 770 | createRxPatch = true; |
| 771 | createTxPatch = true; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 772 | } else { |
| 773 | // If the RX device is on the primary HW module, then use legacy routing method for |
| 774 | // voice calls via setOutputDevice() on primary output. |
| 775 | // Otherwise, create two audio patches for TX and RX path. |
| 776 | createRxPatch = !(availablePrimaryOutputDevices().contains(rxDevices.itemAt(0))) && |
| 777 | (rxSourceDevice != 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 778 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 779 | // of it due to legacy implementation. If not, create a patch. |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 780 | createTxPatch = !(availablePrimaryModuleInputDevices().contains(txSourceDevice)) && |
| 781 | (txSinkDevice != 0); |
| 782 | } |
| 783 | // Use legacy routing method for voice calls via setOutputDevice() on primary output. |
| 784 | // Otherwise, create two audio patches for TX and RX path. |
| 785 | if (!createRxPatch) { |
Eric Laurent | b2fb410 | 2024-06-21 12:25:26 +0000 | [diff] [blame] | 786 | if (fix_call_audio_patch()) { |
| 787 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 788 | } |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 789 | if (!hasPrimaryOutput()) { |
| 790 | ALOGW("%s() no primary output available", __func__); |
| 791 | return INVALID_OPERATION; |
| 792 | } |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 793 | muteWaitMs = setOutputDevices(__func__, mPrimaryOutput, rxDevices, true, delayMs); |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 794 | } else { // create RX path audio patch |
David Li | f85c5e3 | 2024-07-01 13:14:10 +0000 | [diff] [blame] | 795 | connectTelephonyRxAudioSource(delayMs); |
juyuchen | 2224c5a | 2019-01-21 12:00:58 +0800 | [diff] [blame] | 796 | // If the TX device is on the primary HW module but RX device is |
| 797 | // on other HW module, SinkMetaData of telephony input should handle it |
| 798 | // assuming the device uses audio HAL V5.0 and above |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 799 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 800 | if (createTxPatch) { // create TX path audio patch |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 801 | // terminate active capture if on the same HW module as the call TX source device |
| 802 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 803 | // call TX device but this information is not in the audio patch and logic here must be |
| 804 | // symmetric to the one in startInput() |
| 805 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
| 806 | if (activeDesc->hasSameHwModuleAs(txSourceDevice)) { |
| 807 | closeActiveClients(activeDesc); |
| 808 | } |
| 809 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 810 | connectTelephonyTxAudioSource(txSourceDevice, txSinkDevice, delayMs); |
Eric Laurent | b2fb410 | 2024-06-21 12:25:26 +0000 | [diff] [blame] | 811 | } else if (fix_call_audio_patch()) { |
| 812 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 813 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 814 | if (waitMs != nullptr) { |
| 815 | *waitMs = muteWaitMs; |
| 816 | } |
| 817 | return NO_ERROR; |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 818 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 819 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 820 | bool AudioPolicyManager::isDeviceOfModule( |
| 821 | const sp<DeviceDescriptor>& devDesc, const char *moduleId) const { |
| 822 | sp<HwModule> module = mHwModules.getModuleFromName(moduleId); |
| 823 | if (module != 0) { |
| 824 | return mAvailableOutputDevices.getDevicesFromHwModule(module->getHandle()) |
| 825 | .indexOf(devDesc) != NAME_NOT_FOUND |
| 826 | || mAvailableInputDevices.getDevicesFromHwModule(module->getHandle()) |
| 827 | .indexOf(devDesc) != NAME_NOT_FOUND; |
| 828 | } |
| 829 | return false; |
| 830 | } |
| 831 | |
David Li | f85c5e3 | 2024-07-01 13:14:10 +0000 | [diff] [blame] | 832 | void AudioPolicyManager::connectTelephonyRxAudioSource(uint32_t delayMs) |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 833 | { |
Eric Laurent | b2fb410 | 2024-06-21 12:25:26 +0000 | [diff] [blame] | 834 | const auto aa = mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL); |
| 835 | |
| 836 | if (fix_call_audio_patch()) { |
| 837 | if (mCallRxSourceClient != nullptr) { |
| 838 | DeviceVector rxDevices = |
| 839 | mEngine->getOutputDevicesForAttributes(aa, nullptr, false /*fromCache*/); |
| 840 | ALOG_ASSERT(!rxDevices.isEmpty() || !mCallRxSourceClient->isConnected(), |
| 841 | "connectTelephonyRxAudioSource(): no device found for call RX source"); |
| 842 | sp<DeviceDescriptor> rxDevice = rxDevices.itemAt(0); |
| 843 | if (mCallRxSourceClient->isConnected() |
| 844 | && mCallRxSourceClient->sinkDevice()->equals(rxDevice)) { |
| 845 | return; |
| 846 | } |
| 847 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 848 | } |
| 849 | } else { |
| 850 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 851 | } |
| 852 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 853 | const struct audio_port_config source = { |
| 854 | .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE, |
| 855 | .ext.device.type = AUDIO_DEVICE_IN_TELEPHONY_RX, .ext.device.address = "" |
| 856 | }; |
Eric Laurent | 541a200 | 2024-01-15 18:11:42 +0100 | [diff] [blame] | 857 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | b2fb410 | 2024-06-21 12:25:26 +0000 | [diff] [blame] | 858 | |
Eric Laurent | ccbd787 | 2024-06-20 12:34:15 +0000 | [diff] [blame] | 859 | status_t status = startAudioSourceInternal(&source, &aa, &portId, 0 /*uid*/, |
David Li | f85c5e3 | 2024-07-01 13:14:10 +0000 | [diff] [blame] | 860 | true /*internal*/, true /*isCallRx*/, delayMs); |
Eric Laurent | 541a200 | 2024-01-15 18:11:42 +0100 | [diff] [blame] | 861 | ALOGE_IF(status != OK, "%s: failed to start audio source (%d)", __func__, status); |
| 862 | mCallRxSourceClient = mAudioSources.valueFor(portId); |
Eric Laurent | b2fb410 | 2024-06-21 12:25:26 +0000 | [diff] [blame] | 863 | ALOGV("%s portdID %d between source %s and sink %s", __func__, portId, |
| 864 | mCallRxSourceClient->srcDevice()->toString().c_str(), |
| 865 | mCallRxSourceClient->sinkDevice()->toString().c_str()); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 866 | ALOGE_IF(mCallRxSourceClient == nullptr, |
| 867 | "%s failed to start Telephony Rx AudioSource", __func__); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 868 | } |
| 869 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 870 | void AudioPolicyManager::disconnectTelephonyAudioSource(sp<SourceClientDescriptor> &clientDesc) |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 871 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 872 | if (clientDesc == nullptr) { |
| 873 | return; |
| 874 | } |
| 875 | ALOGW_IF(stopAudioSource(clientDesc->portId()) != NO_ERROR, |
| 876 | "%s error stopping audio source", __func__); |
| 877 | clientDesc.clear(); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | void AudioPolicyManager::connectTelephonyTxAudioSource( |
| 881 | const sp<DeviceDescriptor> &srcDevice, const sp<DeviceDescriptor> &sinkDevice, |
| 882 | uint32_t delayMs) |
| 883 | { |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 884 | if (srcDevice == nullptr || sinkDevice == nullptr) { |
| 885 | ALOGW("%s could not create patch, invalid sink and/or source device(s)", __func__); |
| 886 | return; |
| 887 | } |
Eric Laurent | b2fb410 | 2024-06-21 12:25:26 +0000 | [diff] [blame] | 888 | |
| 889 | if (fix_call_audio_patch()) { |
| 890 | if (mCallTxSourceClient != nullptr) { |
| 891 | if (mCallTxSourceClient->isConnected() |
| 892 | && mCallTxSourceClient->srcDevice()->equals(srcDevice)) { |
| 893 | return; |
| 894 | } |
| 895 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
| 896 | } |
| 897 | } else { |
| 898 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
| 899 | } |
| 900 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 901 | PatchBuilder patchBuilder; |
| 902 | patchBuilder.addSource(srcDevice).addSink(sinkDevice); |
Eric Laurent | b2fb410 | 2024-06-21 12:25:26 +0000 | [diff] [blame] | 903 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 904 | auto callTxSourceClientPortId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 905 | const auto aa = mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL); |
| 906 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 907 | struct audio_port_config source = {}; |
| 908 | srcDevice->toAudioPortConfig(&source); |
Eric Laurent | 541a200 | 2024-01-15 18:11:42 +0100 | [diff] [blame] | 909 | mCallTxSourceClient = new SourceClientDescriptor( |
| 910 | callTxSourceClientPortId, mUidCached, aa, source, srcDevice, AUDIO_STREAM_PATCH, |
Eric Laurent | ccbd787 | 2024-06-20 12:34:15 +0000 | [diff] [blame] | 911 | mCommunnicationStrategy, toVolumeSource(aa), true, |
| 912 | false /*isCallRx*/, true /*isCallTx*/); |
Eric Laurent | 541a200 | 2024-01-15 18:11:42 +0100 | [diff] [blame] | 913 | mCallTxSourceClient->setPreferredDeviceId(sinkDevice->getId()); |
| 914 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 915 | audio_patch_handle_t patchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 916 | status_t status = connectAudioSourceToSink( |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 917 | mCallTxSourceClient, sinkDevice, patchBuilder.patch(), patchHandle, mUidCached, |
| 918 | delayMs); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 919 | ALOGE_IF(status != NO_ERROR, "%s() error %d creating TX audio patch", __func__, status); |
Eric Laurent | b2fb410 | 2024-06-21 12:25:26 +0000 | [diff] [blame] | 920 | ALOGV("%s portdID %d between source %s and sink %s", __func__, callTxSourceClientPortId, |
| 921 | srcDevice->toString().c_str(), sinkDevice->toString().c_str()); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 922 | if (status == NO_ERROR) { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 923 | mAudioSources.add(callTxSourceClientPortId, mCallTxSourceClient); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 924 | } |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 925 | } |
| 926 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 927 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 928 | { |
| 929 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 930 | // store previous phone state for management of sonification strategy below |
| 931 | int oldState = mEngine->getPhoneState(); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 932 | bool wasLeUnicastActive = isLeUnicastActive(); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 933 | |
| 934 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 935 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 936 | return; |
| 937 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 938 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 939 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 940 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 941 | // force reevaluating accessibility routing when call stops |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 942 | invalidateStreams({AUDIO_STREAM_ACCESSIBILITY}); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 943 | } |
| 944 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 945 | /** |
| 946 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 947 | * routing command. |
| 948 | */ |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 949 | bool force = ((isStateInCall(oldState) != isStateInCall(state)) |
| 950 | || (isStateInCall(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 951 | |
| 952 | // check for device and output changes triggered by new phone state |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 953 | checkForDeviceAndOutputChanges(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 954 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 955 | int delayMs = 0; |
| 956 | if (isStateInCall(state)) { |
| 957 | nsecs_t sysTime = systemTime(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 958 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); |
| 959 | auto sonificationStrategy = streamToStrategy(AUDIO_STREAM_ALARM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 960 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 961 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 962 | // mute media and sonification strategies and delay device switch by the largest |
| 963 | // latency of any output where either strategy is active. |
| 964 | // 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] | 965 | if ((desc->isStrategyActive(musicStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, sysTime) || |
| 966 | desc->isStrategyActive(sonificationStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, |
| 967 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 968 | (delayMs < (int)desc->latency()*2)) { |
| 969 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 970 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 971 | setStrategyMute(musicStrategy, true, desc); |
| 972 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, |
| 973 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 974 | nullptr, true /*fromCache*/).types()); |
| 975 | setStrategyMute(sonificationStrategy, true, desc); |
| 976 | setStrategyMute(sonificationStrategy, false, desc, MUTE_TIME_MS, |
| 977 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_ALARM), |
| 978 | nullptr, true /*fromCache*/).types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 982 | if (state == AUDIO_MODE_IN_CALL) { |
| 983 | (void)updateCallRouting(false /*fromCache*/, delayMs); |
| 984 | } else { |
| 985 | if (oldState == AUDIO_MODE_IN_CALL) { |
| 986 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 987 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
| 988 | } |
| 989 | if (hasPrimaryOutput()) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 990 | DeviceVector rxDevices = getNewOutputDevices(mPrimaryOutput, false /*fromCache*/); |
| 991 | // force routing command to audio hardware when ending call |
| 992 | // even if no device change is needed |
| 993 | if (isStateInCall(oldState) && rxDevices.isEmpty()) { |
| 994 | rxDevices = mPrimaryOutput->devices(); |
| 995 | } |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 996 | setOutputDevices(__func__, mPrimaryOutput, rxDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 997 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 998 | } |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 999 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 1000 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 1001 | // reevaluate routing on all outputs in case tracks have been started during the call |
| 1002 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1003 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1004 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 1005 | if (state != AUDIO_MODE_NORMAL && oldState == AUDIO_MODE_NORMAL |
| 1006 | && desc->mPreferredAttrInfo != nullptr) { |
| 1007 | // If the output is using preferred mixer attributes and the audio mode is not normal, |
| 1008 | // the output need to reopen with default configuration. |
| 1009 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices); |
| 1010 | continue; |
| 1011 | } |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 1012 | if (state != AUDIO_MODE_IN_CALL || (desc != mPrimaryOutput && !isTelephonyRxOrTx(desc))) { |
| 1013 | bool forceRouting = !newDevices.isEmpty(); |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 1014 | setOutputDevices(__func__, desc, newDevices, forceRouting, 0 /*delayMs*/, nullptr, |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 1015 | true /*requiresMuteCheck*/, !forceRouting /*requiresVolumeCheck*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 1016 | } |
| 1017 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 1018 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 1019 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 1020 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, delayMs); |
| 1021 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1022 | if (isStateInCall(state)) { |
| 1023 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 1024 | // force reevaluating accessibility routing when call starts |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 1025 | invalidateStreams({AUDIO_STREAM_ACCESSIBILITY}); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | // 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] | 1029 | mLimitRingtoneVolume = (state == AUDIO_MODE_RINGTONE && |
| 1030 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 1033 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 1034 | return mEngine->getPhoneState(); |
| 1035 | } |
| 1036 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1037 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1038 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1039 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 1040 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 1041 | if (config == mEngine->getForceUse(usage)) { |
| 1042 | return; |
| 1043 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1044 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 1045 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 1046 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 1047 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1048 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 1049 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 1050 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 1051 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1052 | |
| 1053 | // check for device and output changes triggered by new force usage |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 1054 | checkForDeviceAndOutputChanges(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 1055 | |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 1056 | // force client reconnection to reevaluate flag AUDIO_FLAG_AUDIBILITY_ENFORCED |
| 1057 | if (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 1058 | invalidateStreams({AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE}); |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1061 | //FIXME: workaround for truncated touch sounds |
| 1062 | // to be removed when the problem is handled by system UI |
| 1063 | uint32_t delayMs = 0; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1064 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 1065 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 1066 | } |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 1067 | |
| 1068 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 1069 | updateInputRouting(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1072 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1073 | { |
| 1074 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 1075 | } |
| 1076 | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1077 | // Find an MSD output profile compatible with the parameters passed. |
| 1078 | // When "directOnly" is set, restrict search to profiles for direct outputs. |
| 1079 | sp<IOProfile> AudioPolicyManager::getMsdProfileForOutput( |
| 1080 | const DeviceVector& devices, |
| 1081 | uint32_t samplingRate, |
| 1082 | audio_format_t format, |
| 1083 | audio_channel_mask_t channelMask, |
| 1084 | audio_output_flags_t flags, |
| 1085 | bool directOnly) |
| 1086 | { |
| 1087 | flags = getRelevantFlags(flags, directOnly); |
| 1088 | |
| 1089 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1090 | if (msdModule != nullptr) { |
| 1091 | // for the msd module check if there are patches to the output devices |
| 1092 | if (msdHasPatchesToAllDevices(devices.toTypeAddrVector())) { |
| 1093 | HwModuleCollection modules; |
| 1094 | modules.add(msdModule); |
| 1095 | return searchCompatibleProfileHwModules( |
| 1096 | modules, getMsdAudioOutDevices(), samplingRate, format, channelMask, |
| 1097 | flags, directOnly); |
| 1098 | } |
| 1099 | } |
| 1100 | return nullptr; |
| 1101 | } |
| 1102 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1103 | // Find an output profile compatible with the parameters passed. When "directOnly" is set, restrict |
| 1104 | // search to profiles for direct outputs. |
| 1105 | sp<IOProfile> AudioPolicyManager::getProfileForOutput( |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1106 | const DeviceVector& devices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1107 | uint32_t samplingRate, |
| 1108 | audio_format_t format, |
| 1109 | audio_channel_mask_t channelMask, |
| 1110 | audio_output_flags_t flags, |
| 1111 | bool directOnly) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1112 | { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1113 | flags = getRelevantFlags(flags, directOnly); |
| 1114 | |
| 1115 | return searchCompatibleProfileHwModules( |
| 1116 | mHwModules, devices, samplingRate, format, channelMask, flags, directOnly); |
| 1117 | } |
| 1118 | |
| 1119 | audio_output_flags_t AudioPolicyManager::getRelevantFlags ( |
| 1120 | audio_output_flags_t flags, bool directOnly) { |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1121 | if (directOnly) { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1122 | // only retain flags that will drive the direct output profile selection |
| 1123 | // if explicitly requested |
| 1124 | static const uint32_t kRelevantFlags = |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1125 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1126 | AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ); |
| 1127 | flags = (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1128 | } |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1129 | return flags; |
| 1130 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1131 | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1132 | sp<IOProfile> AudioPolicyManager::searchCompatibleProfileHwModules ( |
| 1133 | const HwModuleCollection& hwModules, |
| 1134 | const DeviceVector& devices, |
| 1135 | uint32_t samplingRate, |
| 1136 | audio_format_t format, |
| 1137 | audio_channel_mask_t channelMask, |
| 1138 | audio_output_flags_t flags, |
| 1139 | bool directOnly) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1140 | sp<IOProfile> profile; |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1141 | for (const auto& hwModule : hwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 1142 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
jiabin | 66acc43 | 2024-02-06 00:57:36 +0000 | [diff] [blame] | 1143 | if (curProfile->getCompatibilityScore(devices, |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1144 | samplingRate, NULL /*updatedSamplingRate*/, |
| 1145 | format, NULL /*updatedFormat*/, |
| 1146 | channelMask, NULL /*updatedChannelMask*/, |
jiabin | 66acc43 | 2024-02-06 00:57:36 +0000 | [diff] [blame] | 1147 | flags) == IOProfile::NO_MATCH) { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1148 | continue; |
| 1149 | } |
| 1150 | // reject profiles not corresponding to a device currently available |
| 1151 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
| 1152 | continue; |
| 1153 | } |
| 1154 | // reject profiles if connected device does not support codec |
| 1155 | if (!curProfile->devicesSupportEncodedFormats(devices.types())) { |
| 1156 | continue; |
| 1157 | } |
| 1158 | if (!directOnly) { |
| 1159 | return curProfile; |
| 1160 | } |
| 1161 | |
| 1162 | // when searching for direct outputs, if several profiles are compatible, give priority |
| 1163 | // to one with offload capability |
Patty Huang | 36028df | 2022-07-06 00:14:12 +0800 | [diff] [blame] | 1164 | if (profile != 0 && |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1165 | ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1166 | continue; |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1167 | } |
| 1168 | profile = curProfile; |
| 1169 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1170 | break; |
| 1171 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1172 | } |
| 1173 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1174 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1177 | sp<IOProfile> AudioPolicyManager::getSpatializerOutputProfile( |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 1178 | const audio_config_t *config __unused, const AudioDeviceTypeAddrVector &devices) const |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1179 | { |
| 1180 | for (const auto& hwModule : mHwModules) { |
| 1181 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 1182 | if (curProfile->getFlags() != AUDIO_OUTPUT_FLAG_SPATIALIZER) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1183 | continue; |
| 1184 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1185 | if (!devices.empty()) { |
Eric Laurent | 0c8f7cc | 2022-06-24 14:32:36 +0200 | [diff] [blame] | 1186 | // reject profiles not corresponding to a device currently available |
| 1187 | DeviceVector supportedDevices = curProfile->getSupportedDevices(); |
| 1188 | if (!mAvailableOutputDevices.containsAtLeastOne(supportedDevices)) { |
| 1189 | continue; |
| 1190 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1191 | if (supportedDevices.getDevicesFromDeviceTypeAddrVec(devices).size() |
| 1192 | != devices.size()) { |
| 1193 | continue; |
| 1194 | } |
| 1195 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1196 | ALOGV("%s found profile %s", __func__, curProfile->getName().c_str()); |
| 1197 | return curProfile; |
| 1198 | } |
| 1199 | } |
| 1200 | return nullptr; |
| 1201 | } |
| 1202 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1203 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1204 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1205 | DeviceVector devices = mEngine->getOutputDevicesForStream(stream, false /*fromCache*/); |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1206 | |
| 1207 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). |
| 1208 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, |
| 1209 | // format, flags, etc. This may result in some discrepancy for functions that utilize |
| 1210 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() |
| 1211 | // and AudioSystem::getOutputSamplingRate(). |
| 1212 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1213 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Mingyu Shih | 75563d3 | 2023-05-24 04:47:40 +0800 | [diff] [blame] | 1214 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
Mikhail Naganov | 806170e | 2024-09-05 17:26:50 -0700 | [diff] [blame] | 1215 | if (stream == AUDIO_STREAM_MUSIC && mConfig->useDeepBufferForMedia()) { |
Mingyu Shih | 75563d3 | 2023-05-24 04:47:40 +0800 | [diff] [blame] | 1216 | flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
| 1217 | } |
| 1218 | const audio_io_handle_t output = selectOutput(outputs, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1219 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1220 | ALOGV("getOutput() stream %d selected devices %s, output %d", stream, |
| 1221 | devices.toString().c_str(), output); |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1222 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1225 | status_t AudioPolicyManager::getAudioAttributes(audio_attributes_t *dstAttr, |
| 1226 | const audio_attributes_t *srcAttr, |
| 1227 | audio_stream_type_t srcStream) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1228 | { |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1229 | if (srcAttr != NULL) { |
| 1230 | if (!isValidAttributes(srcAttr)) { |
| 1231 | ALOGE("%s invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 1232 | __func__, |
| 1233 | srcAttr->usage, srcAttr->content_type, srcAttr->flags, |
| 1234 | srcAttr->tags); |
| 1235 | return BAD_VALUE; |
| 1236 | } |
| 1237 | *dstAttr = *srcAttr; |
| 1238 | } else { |
| 1239 | if (srcStream < AUDIO_STREAM_MIN || srcStream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 1240 | ALOGE("%s: invalid stream type", __func__); |
| 1241 | return BAD_VALUE; |
| 1242 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1243 | *dstAttr = mEngine->getAttributesForStreamType(srcStream); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1244 | } |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 1245 | |
| 1246 | // Only honor audibility enforced when required. The client will be |
| 1247 | // forced to reconnect if the forced usage changes. |
| 1248 | 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] | 1249 | dstAttr->flags = static_cast<audio_flags_mask_t>( |
| 1250 | dstAttr->flags & ~AUDIO_FLAG_AUDIBILITY_ENFORCED); |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1253 | return NO_ERROR; |
| 1254 | } |
| 1255 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1256 | status_t AudioPolicyManager::getOutputForAttrInt( |
| 1257 | audio_attributes_t *resultAttr, |
| 1258 | audio_io_handle_t *output, |
| 1259 | audio_session_t session, |
| 1260 | const audio_attributes_t *attr, |
| 1261 | audio_stream_type_t *stream, |
| 1262 | uid_t uid, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1263 | audio_config_t *config, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1264 | audio_output_flags_t *flags, |
| 1265 | audio_port_handle_t *selectedDeviceId, |
| 1266 | bool *isRequestedDeviceForExclusiveUse, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1267 | std::vector<sp<AudioPolicyMix>> *secondaryMixes, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1268 | output_type_t *outputType, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1269 | bool *isSpatialized, |
| 1270 | bool *isBitPerfect) |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1271 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1272 | DeviceVector outputDevices; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1273 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1274 | DeviceVector msdDevices = getMsdAudioOutDevices(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1275 | const sp<DeviceDescriptor> requestedDevice = |
| 1276 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 1277 | |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1278 | *outputType = API_OUTPUT_INVALID; |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1279 | *isSpatialized = false; |
| 1280 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1281 | status_t status = getAudioAttributes(resultAttr, attr, *stream); |
| 1282 | if (status != NO_ERROR) { |
| 1283 | return status; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1284 | } |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1285 | if (auto it = mAllowedCapturePolicies.find(uid); it != end(mAllowedCapturePolicies)) { |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1286 | resultAttr->flags = static_cast<audio_flags_mask_t>(resultAttr->flags | it->second); |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1287 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1288 | *stream = mEngine->getStreamTypeForAttributes(*resultAttr); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1289 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1290 | ALOGV("%s() attributes=%s stream=%s session %d selectedDeviceId %d", __func__, |
| 1291 | toString(*resultAttr).c_str(), toString(*stream).c_str(), session, requestedPortId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1292 | |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 1293 | bool usePrimaryOutputFromPolicyMixes = false; |
| 1294 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1295 | // The primary output is the explicit routing (eg. setPreferredDevice) if specified, |
| 1296 | // otherwise, fallback to the dynamic policies, if none match, query the engine. |
| 1297 | // 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] | 1298 | sp<AudioPolicyMix> primaryMix; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1299 | const audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
| 1300 | .channel_mask = config->channel_mask, |
| 1301 | .format = config->format, |
| 1302 | }; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1303 | status = mPolicyMixes.getOutputForAttr(*resultAttr, clientConfig, uid, session, *flags, |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 1304 | mAvailableOutputDevices, requestedDevice, primaryMix, |
| 1305 | secondaryMixes, usePrimaryOutputFromPolicyMixes); |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1306 | if (status != OK) { |
| 1307 | return status; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1308 | } |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1309 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1310 | // FIXME: in case of RENDER policy, the output capabilities should be checked |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1311 | if ((secondaryMixes != nullptr && !secondaryMixes->empty()) |
Andy Hung | db27c44 | 2024-08-14 11:37:57 -0700 | [diff] [blame] | 1312 | && (!audio_is_linear_pcm(config->format) || |
| 1313 | *flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) { |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1314 | ALOGD("%s: rejecting request as secondary mixes only support pcm", __func__); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1315 | return BAD_VALUE; |
| 1316 | } |
| 1317 | if (usePrimaryOutputFromPolicyMixes) { |
jiabin | 24ff57a | 2023-11-27 21:06:51 +0000 | [diff] [blame] | 1318 | sp<DeviceDescriptor> policyMixDevice = |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1319 | mAvailableOutputDevices.getDevice(primaryMix->mDeviceType, |
| 1320 | primaryMix->mDeviceAddress, |
| 1321 | AUDIO_FORMAT_DEFAULT); |
| 1322 | sp<SwAudioOutputDescriptor> policyDesc = primaryMix->getOutput(); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1323 | bool tryDirectForFlags = policyDesc == nullptr || |
jiabin | 24ff57a | 2023-11-27 21:06:51 +0000 | [diff] [blame] | 1324 | (policyDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) || |
| 1325 | (*flags & (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ)); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1326 | // if a direct output can be opened to deliver the track's multi-channel content to the |
| 1327 | // output rather than being downmixed by the primary output, then use this direct |
| 1328 | // output by by-passing the primary mix if possible, otherwise fall-through to primary |
| 1329 | // mix. |
| 1330 | bool tryDirectForChannelMask = policyDesc != nullptr |
| 1331 | && (audio_channel_count_from_out_mask(policyDesc->getConfig().channel_mask) < |
| 1332 | audio_channel_count_from_out_mask(config->channel_mask)); |
jiabin | 24ff57a | 2023-11-27 21:06:51 +0000 | [diff] [blame] | 1333 | if (policyMixDevice != nullptr && (tryDirectForFlags || tryDirectForChannelMask)) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1334 | audio_io_handle_t newOutput; |
| 1335 | status = openDirectOutput( |
| 1336 | *stream, session, config, |
| 1337 | (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT), |
Haofan Wang | f6e304f | 2024-07-09 23:06:58 -0700 | [diff] [blame] | 1338 | DeviceVector(policyMixDevice), &newOutput, *resultAttr); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1339 | if (status == NO_ERROR) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1340 | policyDesc = mOutputs.valueFor(newOutput); |
| 1341 | primaryMix->setOutput(policyDesc); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1342 | } else if (tryDirectForFlags) { |
jiabin | 24ff57a | 2023-11-27 21:06:51 +0000 | [diff] [blame] | 1343 | ALOGW("%s, failed open direct, status: %d", __func__, status); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1344 | policyDesc = nullptr; |
| 1345 | } // otherwise use primary if available. |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1346 | } |
| 1347 | if (policyDesc != nullptr) { |
| 1348 | policyDesc->mPolicyMix = primaryMix; |
| 1349 | *output = policyDesc->mIoHandle; |
jiabin | 24ff57a | 2023-11-27 21:06:51 +0000 | [diff] [blame] | 1350 | *selectedDeviceId = policyMixDevice != nullptr ? policyMixDevice->getId() |
| 1351 | : AUDIO_PORT_HANDLE_NONE; |
| 1352 | if ((policyDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1353 | // Remove direct flag as it is not on a direct output. |
| 1354 | *flags = (audio_output_flags_t) (*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1355 | } |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1356 | |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1357 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 1358 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 1359 | *outputType = API_OUT_MIX_PLAYBACK; |
| 1360 | } else { |
| 1361 | *outputType = API_OUTPUT_LEGACY; |
| 1362 | } |
| 1363 | return NO_ERROR; |
jiabin | 24ff57a | 2023-11-27 21:06:51 +0000 | [diff] [blame] | 1364 | } else { |
| 1365 | if (policyMixDevice != nullptr) { |
| 1366 | ALOGE("%s, try to use primary mix but no output found", __func__); |
| 1367 | return INVALID_OPERATION; |
| 1368 | } |
| 1369 | // Fallback to default engine selection as the selected primary mix device is not |
| 1370 | // available. |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1371 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1372 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1373 | // Virtual sources must always be dynamicaly or explicitly routed |
| 1374 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 1375 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 1376 | return BAD_VALUE; |
| 1377 | } |
| 1378 | // explicit routing managed by getDeviceForStrategy in APM is now handled by engine |
| 1379 | // in order to let the choice of the order to future vendor engine |
| 1380 | outputDevices = mEngine->getOutputDevicesForAttributes(*resultAttr, requestedDevice, false); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 1381 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1382 | if ((resultAttr->flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1383 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1386 | // Set incall music only if device was explicitly set, and fallback to the device which is |
| 1387 | // chosen by the engine if not. |
| 1388 | // FIXME: provide a more generic approach which is not device specific and move this back |
| 1389 | // to getOutputForDevice. |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1390 | // 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] | 1391 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX) && |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1392 | (*stream == AUDIO_STREAM_MUSIC || resultAttr->usage == AUDIO_USAGE_VOICE_COMMUNICATION) && |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1393 | audio_is_linear_pcm(config->format) && |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 1394 | isCallAudioAccessible()) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1395 | if (requestedPortId != AUDIO_PORT_HANDLE_NONE) { |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1396 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1397 | *isRequestedDeviceForExclusiveUse = true; |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1398 | } |
| 1399 | } |
| 1400 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1401 | ALOGV("%s() device %s, sampling rate %d, format %#x, channel mask %#x, flags %#x stream %s", |
| 1402 | __func__, outputDevices.toString().c_str(), config->sample_rate, config->format, |
| 1403 | config->channel_mask, *flags, toString(*stream).c_str()); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1404 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1405 | *output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1406 | if (!msdDevices.isEmpty()) { |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1407 | *output = getOutputForDevices(msdDevices, session, resultAttr, config, flags, isSpatialized); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1408 | if (*output != AUDIO_IO_HANDLE_NONE && setMsdOutputPatches(&outputDevices) == NO_ERROR) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1409 | ALOGV("%s() Using MSD devices %s instead of devices %s", |
| 1410 | __func__, msdDevices.toString().c_str(), outputDevices.toString().c_str()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1411 | } else { |
| 1412 | *output = AUDIO_IO_HANDLE_NONE; |
| 1413 | } |
| 1414 | } |
| 1415 | if (*output == AUDIO_IO_HANDLE_NONE) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1416 | sp<PreferredMixerAttributesInfo> info = nullptr; |
| 1417 | if (outputDevices.size() == 1) { |
| 1418 | info = getPreferredMixerAttributesInfo( |
| 1419 | outputDevices.itemAt(0)->getId(), |
jiabin | d9a58d3 | 2023-06-01 17:57:30 +0000 | [diff] [blame] | 1420 | mEngine->getProductStrategyForAttributes(*resultAttr), |
| 1421 | true /*activeBitPerfectPreferred*/); |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 1422 | // Only use preferred mixer if the uid matches or the preferred mixer is bit-perfect |
| 1423 | // and it is currently active. |
| 1424 | if (info != nullptr && info->getUid() != uid && |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 1425 | (!info->isBitPerfect() || info->getActiveClientCount() == 0)) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1426 | info = nullptr; |
| 1427 | } |
jiabin | 8a09667 | 2024-09-18 18:20:24 +0000 | [diff] [blame] | 1428 | |
| 1429 | if (info != nullptr && info->isBitPerfect() && |
| 1430 | (*flags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 1431 | AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ)) != 0) { |
| 1432 | // Reject direct request if a preferred mixer config in use is bit-perfect. |
| 1433 | ALOGD("%s reject direct request as bit-perfect mixer attributes is active", |
| 1434 | __func__); |
| 1435 | return BAD_VALUE; |
| 1436 | } |
| 1437 | |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 1438 | if (com::android::media::audioserver:: |
| 1439 | fix_concurrent_playback_behavior_with_bit_perfect_client()) { |
| 1440 | if (info != nullptr && info->getUid() == uid && |
| 1441 | info->configMatches(*config) && |
| 1442 | (mEngine->getPhoneState() != AUDIO_MODE_NORMAL || |
| 1443 | std::any_of(gHighPriorityUseCases.begin(), gHighPriorityUseCases.end(), |
| 1444 | [this, &outputDevices](audio_usage_t usage) { |
| 1445 | return mOutputs.isUsageActiveOnDevice( |
| 1446 | usage, outputDevices[0]); }))) { |
| 1447 | // Bit-perfect request is not allowed when the phone mode is not normal or |
| 1448 | // there is any higher priority user case active. |
| 1449 | return INVALID_OPERATION; |
| 1450 | } |
| 1451 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1452 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1453 | *output = getOutputForDevices(outputDevices, session, resultAttr, config, |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1454 | flags, isSpatialized, info, resultAttr->flags & AUDIO_FLAG_MUTE_HAPTIC); |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 1455 | // The client will be active if the client is currently preferred mixer owner and the |
| 1456 | // requested configuration matches the preferred mixer configuration. |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1457 | *isBitPerfect = (info != nullptr |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 1458 | && info->isBitPerfect() |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 1459 | && info->getUid() == uid |
| 1460 | && *output != AUDIO_IO_HANDLE_NONE |
| 1461 | // When bit-perfect output is selected for the preferred mixer attributes owner, |
| 1462 | // only need to consider the config matches. |
| 1463 | && mOutputs.valueFor(*output)->isConfigurationMatched( |
| 1464 | clientConfig, AUDIO_OUTPUT_FLAG_NONE)); |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 1465 | |
| 1466 | if (*isBitPerfect) { |
| 1467 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_BIT_PERFECT); |
| 1468 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1469 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1470 | if (*output == AUDIO_IO_HANDLE_NONE) { |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1471 | AudioProfileVector profiles; |
| 1472 | status_t ret = getProfilesForDevices(outputDevices, profiles, *flags, false /*isInput*/); |
| 1473 | if (ret == NO_ERROR && !profiles.empty()) { |
Robert Wu | b98ff1b | 2023-06-15 22:53:58 +0000 | [diff] [blame] | 1474 | const auto channels = profiles[0]->getChannels(); |
| 1475 | if (!channels.empty() && (channels.find(config->channel_mask) == channels.end())) { |
| 1476 | config->channel_mask = *channels.begin(); |
| 1477 | } |
| 1478 | const auto sampleRates = profiles[0]->getSampleRates(); |
| 1479 | if (!sampleRates.empty() && |
| 1480 | (sampleRates.find(config->sample_rate) == sampleRates.end())) { |
| 1481 | config->sample_rate = *sampleRates.begin(); |
| 1482 | } |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1483 | config->format = profiles[0]->getFormat(); |
| 1484 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1485 | return INVALID_OPERATION; |
| 1486 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1487 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1488 | *selectedDeviceId = getFirstDeviceId(outputDevices); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1489 | for (auto &outputDevice : outputDevices) { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 1490 | if (outputDevice->getId() == mConfig->getDefaultOutputDevice()->getId()) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1491 | *selectedDeviceId = outputDevice->getId(); |
| 1492 | break; |
| 1493 | } |
| 1494 | } |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1495 | |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1496 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX)) { |
| 1497 | *outputType = API_OUTPUT_TELEPHONY_TX; |
| 1498 | } else { |
| 1499 | *outputType = API_OUTPUT_LEGACY; |
| 1500 | } |
| 1501 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1502 | ALOGV("%s returns output %d selectedDeviceId %d", __func__, *output, *selectedDeviceId); |
| 1503 | |
| 1504 | return NO_ERROR; |
| 1505 | } |
| 1506 | |
| 1507 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 1508 | audio_io_handle_t *output, |
| 1509 | audio_session_t session, |
| 1510 | audio_stream_type_t *stream, |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1511 | const AttributionSourceState& attributionSource, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1512 | audio_config_t *config, |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1513 | audio_output_flags_t *flags, |
| 1514 | audio_port_handle_t *selectedDeviceId, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1515 | audio_port_handle_t *portId, |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1516 | std::vector<audio_io_handle_t> *secondaryOutputs, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1517 | output_type_t *outputType, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1518 | bool *isSpatialized, |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 1519 | bool *isBitPerfect, |
| 1520 | float *volume) |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1521 | { |
| 1522 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 1523 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 1524 | return INVALID_OPERATION; |
| 1525 | } |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1526 | const uid_t uid = VALUE_OR_RETURN_STATUS( |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1527 | aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1528 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1529 | audio_attributes_t resultAttr; |
François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1530 | bool isRequestedDeviceForExclusiveUse = false; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1531 | std::vector<sp<AudioPolicyMix>> secondaryMixes; |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1532 | const sp<DeviceDescriptor> requestedDevice = |
| 1533 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 1534 | |
| 1535 | // Prevent from storing invalid requested device id in clients |
| 1536 | const audio_port_handle_t sanitizedRequestedPortId = |
| 1537 | requestedDevice != nullptr ? requestedPortId : AUDIO_PORT_HANDLE_NONE; |
| 1538 | *selectedDeviceId = sanitizedRequestedPortId; |
| 1539 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1540 | status_t status = getOutputForAttrInt(&resultAttr, output, session, attr, stream, uid, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1541 | config, flags, selectedDeviceId, &isRequestedDeviceForExclusiveUse, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1542 | secondaryOutputs != nullptr ? &secondaryMixes : nullptr, outputType, isSpatialized, |
| 1543 | isBitPerfect); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1544 | if (status != NO_ERROR) { |
| 1545 | return status; |
| 1546 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1547 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryOutputDescs; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1548 | if (secondaryOutputs != nullptr) { |
| 1549 | for (auto &secondaryMix : secondaryMixes) { |
| 1550 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); |
| 1551 | if (outputDesc != nullptr && |
| 1552 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { |
| 1553 | secondaryOutputs->push_back(outputDesc->mIoHandle); |
| 1554 | weakSecondaryOutputDescs.push_back(outputDesc); |
| 1555 | } |
| 1556 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1557 | } |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1558 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1559 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1560 | .channel_mask = config->channel_mask, |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1561 | .format = config->format, |
Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1562 | }; |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 1563 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1564 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1565 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1566 | sp<TrackClientDescriptor> clientDesc = |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1567 | new TrackClientDescriptor(*portId, uid, session, resultAttr, clientConfig, |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1568 | sanitizedRequestedPortId, *stream, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1569 | mEngine->getProductStrategyForAttributes(resultAttr), |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 1570 | toVolumeSource(resultAttr), |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1571 | *flags, isRequestedDeviceForExclusiveUse, |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1572 | std::move(weakSecondaryOutputDescs), |
| 1573 | outputDesc->mPolicyMix); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1574 | outputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1575 | |
Andy Hung | 6b137d1 | 2024-08-27 22:35:17 +0000 | [diff] [blame] | 1576 | *volume = Volume::DbToAmpl(outputDesc->getCurVolume(toVolumeSource(resultAttr))); |
| 1577 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1578 | ALOGV("%s() returns output %d requestedPortId %d selectedDeviceId %d for port ID %d", __func__, |
| 1579 | *output, requestedPortId, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1580 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1581 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1582 | } |
| 1583 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1584 | status_t AudioPolicyManager::openDirectOutput(audio_stream_type_t stream, |
| 1585 | audio_session_t session, |
| 1586 | const audio_config_t *config, |
| 1587 | audio_output_flags_t flags, |
| 1588 | const DeviceVector &devices, |
Haofan Wang | f6e304f | 2024-07-09 23:06:58 -0700 | [diff] [blame] | 1589 | audio_io_handle_t *output, |
| 1590 | audio_attributes_t attributes) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1591 | |
| 1592 | *output = AUDIO_IO_HANDLE_NONE; |
| 1593 | |
| 1594 | // skip direct output selection if the request can obviously be attached to a mixed output |
| 1595 | // and not explicitly requested |
| 1596 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 1597 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 1598 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
| 1599 | return NAME_NOT_FOUND; |
| 1600 | } |
| 1601 | |
Mikhail Naganov | 806170e | 2024-09-05 17:26:50 -0700 | [diff] [blame] | 1602 | // Reject flag combinations that do not make sense. Note that the requested flags might not |
| 1603 | // have the 'DIRECT' flag set, however once a direct-capable profile is found, it will |
| 1604 | // combine the requested flags with its own flags, yielding an unsupported combination. |
| 1605 | if ((flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
| 1606 | return NAME_NOT_FOUND; |
| 1607 | } |
| 1608 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1609 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 1610 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 1611 | // when audioflinger detects there is an active non offloadable effect. |
| 1612 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 1613 | // This may prevent offloading in rare situations where effects are left active by apps |
| 1614 | // in the background. |
| 1615 | sp<IOProfile> profile; |
| 1616 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
| 1617 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
| 1618 | profile = getProfileForOutput( |
| 1619 | devices, config->sample_rate, config->format, config->channel_mask, |
| 1620 | flags, true /* directOnly */); |
| 1621 | } |
| 1622 | |
| 1623 | if (profile == nullptr) { |
| 1624 | return NAME_NOT_FOUND; |
| 1625 | } |
| 1626 | |
| 1627 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 1628 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1629 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 1630 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 1631 | // reuse direct output if currently open by the same client |
| 1632 | // and configured with same parameters |
| 1633 | if ((config->sample_rate == desc->getSamplingRate()) && |
| 1634 | (config->format == desc->getFormat()) && |
| 1635 | (config->channel_mask == desc->getChannelMask()) && |
| 1636 | (session == desc->mDirectClientSession)) { |
| 1637 | desc->mDirectOpenCount++; |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 1638 | ALOGI("%s reusing direct output %d for session %d", __func__, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1639 | mOutputs.keyAt(i), session); |
| 1640 | *output = mOutputs.keyAt(i); |
| 1641 | return NO_ERROR; |
| 1642 | } |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | if (!profile->canOpenNewIo()) { |
Atneya Nair | b16666a | 2023-12-11 20:18:33 -0800 | [diff] [blame] | 1647 | if (!com::android::media::audioserver::direct_track_reprioritization()) { |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 1648 | ALOGW("%s profile %s can't open new output maxOpenCount reached", __func__, |
| 1649 | profile->getName().c_str()); |
Atneya Nair | b16666a | 2023-12-11 20:18:33 -0800 | [diff] [blame] | 1650 | return NAME_NOT_FOUND; |
| 1651 | } else if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) != 0) { |
| 1652 | // MMAP gracefully handles lack of an exclusive track resource by mixing |
| 1653 | // above the audio framework. For AAudio to know that the limit is reached, |
| 1654 | // return an error. |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 1655 | ALOGW("%s profile %s can't open new mmap output maxOpenCount reached", __func__, |
| 1656 | profile->getName().c_str()); |
Atneya Nair | b16666a | 2023-12-11 20:18:33 -0800 | [diff] [blame] | 1657 | return NAME_NOT_FOUND; |
| 1658 | } else { |
| 1659 | // Close outputs on this profile, if available, to free resources for this request |
| 1660 | for (int i = 0; i < mOutputs.size() && !profile->canOpenNewIo(); i++) { |
| 1661 | const auto desc = mOutputs.valueAt(i); |
| 1662 | if (desc->mProfile == profile) { |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 1663 | ALOGV("%s closeOutput %d to prioritize session %d on profile %s", __func__, |
| 1664 | desc->mIoHandle, session, profile->getName().c_str()); |
Atneya Nair | b16666a | 2023-12-11 20:18:33 -0800 | [diff] [blame] | 1665 | closeOutput(desc->mIoHandle); |
| 1666 | } |
| 1667 | } |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | // Unable to close streams to find free resources for this request |
| 1672 | if (!profile->canOpenNewIo()) { |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 1673 | ALOGW("%s profile %s can't open new output maxOpenCount reached", __func__, |
| 1674 | profile->getName().c_str()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1675 | return NAME_NOT_FOUND; |
| 1676 | } |
| 1677 | |
Atneya Nair | b16666a | 2023-12-11 20:18:33 -0800 | [diff] [blame] | 1678 | auto outputDesc = sp<SwAudioOutputDescriptor>::make(profile, mpClientInterface); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1679 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1680 | // An MSD patch may be using the only output stream that can service this request. Release |
| 1681 | // 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] | 1682 | releaseMsdOutputPatches(devices); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1683 | |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 1684 | status_t status = |
Dean Wheatley | dfb67b8 | 2024-01-23 09:36:29 +1100 | [diff] [blame] | 1685 | outputDesc->open(config, nullptr /* mixerConfig */, devices, stream, &flags, output, |
Haofan Wang | f6e304f | 2024-07-09 23:06:58 -0700 | [diff] [blame] | 1686 | attributes); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1687 | |
Dean Wheatley | d27bbb9 | 2024-01-19 15:54:35 +1100 | [diff] [blame] | 1688 | // only accept an output with the requested parameters, unless the format can be IEC61937 |
| 1689 | // encapsulated and opened by AudioFlinger as wrapped IEC61937. |
| 1690 | const bool ignoreRequestedParametersCheck = audio_is_iec61937_compatible(config->format) |
| 1691 | && (flags & AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO) |
| 1692 | && audio_has_proportional_frames(outputDesc->getFormat()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1693 | if (status != NO_ERROR || |
Dean Wheatley | d27bbb9 | 2024-01-19 15:54:35 +1100 | [diff] [blame] | 1694 | (!ignoreRequestedParametersCheck && |
| 1695 | ((config->sample_rate != 0 && config->sample_rate != outputDesc->getSamplingRate()) || |
| 1696 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->getFormat()) || |
| 1697 | (config->channel_mask != 0 && config->channel_mask != outputDesc->getChannelMask())))) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1698 | ALOGV("%s failed opening direct output: output %d sample rate %d %d," |
| 1699 | "format %d %d, channel mask %04x %04x", __func__, *output, config->sample_rate, |
| 1700 | outputDesc->getSamplingRate(), config->format, outputDesc->getFormat(), |
| 1701 | config->channel_mask, outputDesc->getChannelMask()); |
| 1702 | if (*output != AUDIO_IO_HANDLE_NONE) { |
| 1703 | outputDesc->close(); |
| 1704 | } |
| 1705 | // fall back to mixer output if possible when the direct output could not be open |
| 1706 | if (audio_is_linear_pcm(config->format) && |
| 1707 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
| 1708 | return NAME_NOT_FOUND; |
| 1709 | } |
| 1710 | *output = AUDIO_IO_HANDLE_NONE; |
| 1711 | return BAD_VALUE; |
| 1712 | } |
| 1713 | outputDesc->mDirectOpenCount = 1; |
| 1714 | outputDesc->mDirectClientSession = session; |
| 1715 | |
| 1716 | addOutput(*output, outputDesc); |
Mikhail Naganov | ccd149c | 2024-09-26 14:16:13 -0700 | [diff] [blame] | 1717 | // The version check is essentially to avoid making this call in the case of the HIDL HAL. |
| 1718 | if (auto hwModule = mHwModules.getModuleFromHandle(mPrimaryModuleHandle); hwModule && |
| 1719 | hwModule->getHalVersionMajor() >= 3) { |
| 1720 | setOutputDevices(__func__, outputDesc, devices, true, 0, NULL); |
| 1721 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1722 | mPreviousOutputs = mOutputs; |
| 1723 | ALOGV("%s returns new direct output %d", __func__, *output); |
| 1724 | mpClientInterface->onAudioPortListUpdate(); |
| 1725 | return NO_ERROR; |
| 1726 | } |
| 1727 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1728 | audio_io_handle_t AudioPolicyManager::getOutputForDevices( |
| 1729 | const DeviceVector &devices, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1730 | audio_session_t session, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1731 | const audio_attributes_t *attr, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1732 | const audio_config_t *config, |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1733 | audio_output_flags_t *flags, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1734 | bool *isSpatialized, |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1735 | sp<PreferredMixerAttributesInfo> prefMixerConfigInfo, |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1736 | bool forceMutingHaptic) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1737 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1738 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1739 | |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1740 | // Discard haptic channel mask when forcing muting haptic channels. |
| 1741 | audio_channel_mask_t channelMask = forceMutingHaptic |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1742 | ? static_cast<audio_channel_mask_t>(config->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL) |
| 1743 | : config->channel_mask; |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1744 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1745 | // open a direct output if required by specified parameters |
| 1746 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 1747 | // and all common behaviors are driven by checking only the direct flag |
| 1748 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1749 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1750 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1751 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1752 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 1753 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1754 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1755 | |
| 1756 | audio_stream_type_t stream = mEngine->getStreamTypeForAttributes(*attr); |
| 1757 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1758 | // only allow deep buffering for music stream type |
| 1759 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1760 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1761 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Mikhail Naganov | 806170e | 2024-09-05 17:26:50 -0700 | [diff] [blame] | 1762 | *flags == AUDIO_OUTPUT_FLAG_NONE && mConfig->useDeepBufferForMedia()) { |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1763 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1764 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1765 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1766 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1767 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1768 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1769 | audio_is_linear_pcm(config->format) && |
| 1770 | (*flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) == 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1771 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1772 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1773 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1774 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1775 | |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 1776 | // Attach the Ultrasound flag for the AUDIO_CONTENT_TYPE_ULTRASOUND |
| 1777 | if (attr->content_type == AUDIO_CONTENT_TYPE_ULTRASOUND) { |
| 1778 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_ULTRASOUND); |
| 1779 | } |
| 1780 | |
Eric Laurent | f9230d5 | 2024-01-26 18:49:09 +0100 | [diff] [blame] | 1781 | // Use the spatializer output if the content can be spatialized, no preferred mixer |
Shunkai Yao | 4c3af93 | 2024-04-26 04:12:21 +0000 | [diff] [blame] | 1782 | // was specified and offload or direct playback is not explicitly requested, and there is no |
| 1783 | // haptic channel included in playback |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1784 | *isSpatialized = false; |
Shunkai Yao | 4c3af93 | 2024-04-26 04:12:21 +0000 | [diff] [blame] | 1785 | if (mSpatializerOutput != nullptr && |
| 1786 | canBeSpatializedInt(attr, config, devices.toTypeAddrVector()) && |
| 1787 | prefMixerConfigInfo == nullptr && |
| 1788 | ((*flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT)) == 0) && |
| 1789 | checkHapticCompatibilityOnSpatializerOutput(config, session)) { |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1790 | *isSpatialized = true; |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1791 | return mSpatializerOutput->mIoHandle; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1792 | } |
| 1793 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1794 | audio_config_t directConfig = *config; |
| 1795 | directConfig.channel_mask = channelMask; |
Haofan Wang | f6e304f | 2024-07-09 23:06:58 -0700 | [diff] [blame] | 1796 | |
| 1797 | status_t status = openDirectOutput(stream, session, &directConfig, *flags, devices, &output, |
| 1798 | *attr); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1799 | if (status != NAME_NOT_FOUND) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1800 | return output; |
| 1801 | } |
| 1802 | |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1803 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1804 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1805 | 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] | 1806 | return AUDIO_IO_HANDLE_NONE; |
| 1807 | } |
Pierre Couillaud | e73496b | 2023-03-06 16:19:05 +0000 | [diff] [blame] | 1808 | // A request for Tuner cannot fallback to a mixed output |
| 1809 | if ((directConfig.offload_info.content_id || directConfig.offload_info.sync_id)) { |
| 1810 | return AUDIO_IO_HANDLE_NONE; |
| 1811 | } |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1812 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1813 | // ignoring channel mask due to downmix capability in mixer |
| 1814 | |
| 1815 | // open a non direct output |
| 1816 | |
| 1817 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1818 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1819 | // get which output is suitable for the specified stream. The actual |
| 1820 | // routing change will happen when startOutput() will be called |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1821 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1822 | if (prefMixerConfigInfo != nullptr) { |
| 1823 | for (audio_io_handle_t outputHandle : outputs) { |
| 1824 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputHandle); |
| 1825 | if (outputDesc->mProfile == prefMixerConfigInfo->getProfile()) { |
| 1826 | output = outputHandle; |
| 1827 | break; |
| 1828 | } |
| 1829 | } |
| 1830 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 1831 | // No output open with the preferred profile. Open a new one. |
| 1832 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 1833 | config.channel_mask = prefMixerConfigInfo->getConfigBase().channel_mask; |
| 1834 | config.sample_rate = prefMixerConfigInfo->getConfigBase().sample_rate; |
| 1835 | config.format = prefMixerConfigInfo->getConfigBase().format; |
| 1836 | sp<SwAudioOutputDescriptor> preferredOutput = openOutputWithProfileAndDevice( |
| 1837 | prefMixerConfigInfo->getProfile(), devices, nullptr /*mixerConfig*/, |
| 1838 | &config, prefMixerConfigInfo->getFlags()); |
| 1839 | if (preferredOutput == nullptr) { |
| 1840 | ALOGE("%s failed to open output with preferred mixer config", __func__); |
| 1841 | } else { |
| 1842 | output = preferredOutput->mIoHandle; |
| 1843 | } |
| 1844 | } |
| 1845 | } else { |
| 1846 | // at this stage we should ignore the DIRECT flag as no direct output could be |
| 1847 | // found earlier |
| 1848 | *flags = (audio_output_flags_t) (*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 1849 | if (com::android::media::audioserver:: |
| 1850 | fix_concurrent_playback_behavior_with_bit_perfect_client()) { |
| 1851 | // If the preferred mixer attributes is null, do not select the bit-perfect output |
| 1852 | // unless the bit-perfect output is the only output. |
| 1853 | // The bit-perfect output can exist while the passed in preferred mixer attributes |
| 1854 | // info is null when it is a high priority client. The high priority clients are |
| 1855 | // ringtone or alarm, which is not a bit-perfect use case. |
| 1856 | size_t i = 0; |
| 1857 | while (i < outputs.size() && outputs.size() > 1) { |
| 1858 | auto desc = mOutputs.valueFor(outputs[i]); |
| 1859 | // The output descriptor must not be null here. |
| 1860 | if (desc->isBitPerfect()) { |
| 1861 | outputs.removeItemsAt(i); |
| 1862 | } else { |
| 1863 | i += 1; |
| 1864 | } |
| 1865 | } |
| 1866 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1867 | output = selectOutput( |
| 1868 | outputs, *flags, config->format, channelMask, config->sample_rate, session); |
| 1869 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1870 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1871 | ALOGW_IF((output == 0), "getOutputForDevices() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1872 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1873 | stream, config->sample_rate, config->format, channelMask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1874 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1875 | return output; |
| 1876 | } |
| 1877 | |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame] | 1878 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1879 | auto msdInDevices = mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1880 | mAvailableInputDevices); |
| 1881 | return msdInDevices.isEmpty()? nullptr : msdInDevices.itemAt(0); |
| 1882 | } |
| 1883 | |
| 1884 | DeviceVector AudioPolicyManager::getMsdAudioOutDevices() const { |
| 1885 | return mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1886 | mAvailableOutputDevices); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1887 | } |
| 1888 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1889 | const AudioPatchCollection AudioPolicyManager::getMsdOutputPatches() const { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1890 | AudioPatchCollection msdPatches; |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1891 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1892 | if (msdModule != 0) { |
| 1893 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { |
| 1894 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); |
| 1895 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { |
| 1896 | const struct audio_port_config *source = &patch->mPatch.sources[j]; |
| 1897 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 1898 | source->ext.device.hw_module == msdModule->getHandle()) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1899 | msdPatches.addAudioPatch(patch->getHandle(), patch); |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1900 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1901 | } |
| 1902 | } |
| 1903 | } |
| 1904 | return msdPatches; |
| 1905 | } |
| 1906 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 1907 | bool AudioPolicyManager::isMsdPatch(const audio_patch_handle_t &handle) const { |
| 1908 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 1909 | if (index < 0) { |
| 1910 | return false; |
| 1911 | } |
| 1912 | const sp<AudioPatch> patch = mAudioPatches.valueAt(index); |
| 1913 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1914 | if (msdModule == nullptr) { |
| 1915 | return false; |
| 1916 | } |
| 1917 | const struct audio_port_config *sink = &patch->mPatch.sinks[0]; |
| 1918 | if (getMsdAudioOutDevices().contains(mAvailableOutputDevices.getDeviceFromId(sink->id))) { |
| 1919 | return true; |
| 1920 | } |
| 1921 | index = getMsdOutputPatches().indexOfKey(handle); |
| 1922 | if (index < 0) { |
| 1923 | return false; |
| 1924 | } |
| 1925 | return true; |
| 1926 | } |
| 1927 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1928 | status_t AudioPolicyManager::getMsdProfiles(bool hwAvSync, |
| 1929 | const InputProfileCollection &inputProfiles, |
| 1930 | const OutputProfileCollection &outputProfiles, |
| 1931 | const sp<DeviceDescriptor> &sourceDevice, |
| 1932 | const sp<DeviceDescriptor> &sinkDevice, |
| 1933 | AudioProfileVector& sourceProfiles, |
| 1934 | AudioProfileVector& sinkProfiles) const { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1935 | if (inputProfiles.isEmpty()) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1936 | ALOGE("%s() no input profiles for source module", __func__); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1937 | return NO_INIT; |
| 1938 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1939 | if (outputProfiles.isEmpty()) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1940 | ALOGE("%s() no output profiles for sink module", __func__); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1941 | return NO_INIT; |
| 1942 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1943 | for (const auto &inProfile : inputProfiles) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1944 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0) && |
| 1945 | inProfile->supportsDevice(sourceDevice)) { |
| 1946 | appendAudioProfiles(sourceProfiles, inProfile->getAudioProfiles()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1947 | } |
| 1948 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1949 | for (const auto &outProfile : outputProfiles) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1950 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) && |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1951 | outProfile->supportsDevice(sinkDevice)) { |
| 1952 | appendAudioProfiles(sinkProfiles, outProfile->getAudioProfiles()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1953 | } |
| 1954 | } |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1955 | return NO_ERROR; |
| 1956 | } |
| 1957 | |
| 1958 | status_t AudioPolicyManager::getBestMsdConfig(bool hwAvSync, |
| 1959 | const AudioProfileVector &sourceProfiles, const AudioProfileVector &sinkProfiles, |
| 1960 | audio_port_config *sourceConfig, audio_port_config *sinkConfig) const |
| 1961 | { |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 1962 | // Compressed formats for MSD module, ordered from most preferred to least preferred. |
| 1963 | static const std::vector<audio_format_t> formatsOrder = {{ |
| 1964 | 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] | 1965 | AUDIO_FORMAT_AC3, AUDIO_FORMAT_PCM_FLOAT, AUDIO_FORMAT_PCM_32_BIT, |
| 1966 | 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] | 1967 | static const std::vector<audio_channel_mask_t> channelMasksOrder = [](){ |
| 1968 | // Channel position masks for MSD module, 3D > 2D > 1D ordering (most preferred to least |
| 1969 | // preferred). |
| 1970 | std::vector<audio_channel_mask_t> masks = {{ |
| 1971 | AUDIO_CHANNEL_OUT_3POINT1POINT2, AUDIO_CHANNEL_OUT_3POINT0POINT2, |
| 1972 | AUDIO_CHANNEL_OUT_2POINT1POINT2, AUDIO_CHANNEL_OUT_2POINT0POINT2, |
| 1973 | AUDIO_CHANNEL_OUT_5POINT1, AUDIO_CHANNEL_OUT_STEREO }}; |
| 1974 | // insert index masks (higher counts most preferred) as preferred over position masks |
| 1975 | for (int i = 1; i <= AUDIO_CHANNEL_COUNT_MAX; i++) { |
| 1976 | masks.insert( |
| 1977 | masks.begin(), audio_channel_mask_for_index_assignment_from_count(i)); |
| 1978 | } |
| 1979 | return masks; |
| 1980 | }(); |
| 1981 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1982 | struct audio_config_base bestSinkConfig; |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 1983 | status_t result = findBestMatchingOutputConfig(sourceProfiles, sinkProfiles, formatsOrder, |
| 1984 | channelMasksOrder, true /*preferHigherSamplingRates*/, bestSinkConfig); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1985 | if (result != NO_ERROR) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1986 | ALOGD("%s() no matching config found for sink, hwAvSync: %d", |
| 1987 | __func__, hwAvSync); |
Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1988 | return result; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1989 | } |
| 1990 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1991 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; |
| 1992 | sinkConfig->format = bestSinkConfig.format; |
| 1993 | // For encoded streams force direct flag to prevent downstream mixing. |
| 1994 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1995 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); |
Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1996 | if (audio_is_iec61937_compatible(sinkConfig->format)) { |
| 1997 | // For formats compatible with IEC61937 encapsulation, assume that |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1998 | // the input is IEC61937 framed (for proportional buffer sizing). |
Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1999 | // Add the AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO flag so downstream HAL can distinguish between |
| 2000 | // raw and IEC61937 framed streams. |
| 2001 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 2002 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO); |
| 2003 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 2004 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; |
| 2005 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 2006 | sourceConfig->channel_mask = |
| 2007 | audio_channel_mask_get_representation(bestSinkConfig.channel_mask) |
| 2008 | == AUDIO_CHANNEL_REPRESENTATION_INDEX ? |
| 2009 | bestSinkConfig.channel_mask : audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 2010 | sourceConfig->format = bestSinkConfig.format; |
| 2011 | // Copy input stream directly without any processing (e.g. resampling). |
| 2012 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 2013 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); |
| 2014 | if (hwAvSync) { |
| 2015 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 2016 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 2017 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 2018 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); |
| 2019 | } |
| 2020 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | |
| 2021 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; |
| 2022 | sinkConfig->config_mask |= config_mask; |
| 2023 | sourceConfig->config_mask |= config_mask; |
| 2024 | return NO_ERROR; |
| 2025 | } |
| 2026 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 2027 | PatchBuilder AudioPolicyManager::buildMsdPatch(bool msdIsSource, |
| 2028 | const sp<DeviceDescriptor> &device) const |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 2029 | { |
| 2030 | PatchBuilder patchBuilder; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 2031 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 2032 | ALOG_ASSERT(msdModule != nullptr, "MSD module not available"); |
| 2033 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(device, AUDIO_FORMAT_DEFAULT); |
| 2034 | if (deviceModule == nullptr) { |
| 2035 | ALOGE("%s() unable to get module for %s", __func__, device->toString().c_str()); |
| 2036 | return patchBuilder; |
| 2037 | } |
| 2038 | const InputProfileCollection inputProfiles = msdIsSource ? |
| 2039 | msdModule->getInputProfiles() : deviceModule->getInputProfiles(); |
| 2040 | const OutputProfileCollection outputProfiles = msdIsSource ? |
| 2041 | deviceModule->getOutputProfiles() : msdModule->getOutputProfiles(); |
| 2042 | |
| 2043 | const sp<DeviceDescriptor> sourceDevice = msdIsSource ? getMsdAudioInDevice() : device; |
| 2044 | const sp<DeviceDescriptor> sinkDevice = msdIsSource ? |
| 2045 | device : getMsdAudioOutDevices().itemAt(0); |
| 2046 | patchBuilder.addSource(sourceDevice).addSink(sinkDevice); |
| 2047 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 2048 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; |
| 2049 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 2050 | AudioProfileVector sourceProfiles; |
| 2051 | AudioProfileVector sinkProfiles; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 2052 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. |
| 2053 | // For now, we just forcefully try with HwAvSync first. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 2054 | for (auto hwAvSync : { true, false }) { |
| 2055 | if (getMsdProfiles(hwAvSync, inputProfiles, outputProfiles, sourceDevice, sinkDevice, |
| 2056 | sourceProfiles, sinkProfiles) != NO_ERROR) { |
| 2057 | continue; |
| 2058 | } |
| 2059 | if (getBestMsdConfig(hwAvSync, sourceProfiles, sinkProfiles, &sourceConfig, |
| 2060 | &sinkConfig) == NO_ERROR) { |
| 2061 | // Found a matching config. Re-create PatchBuilder with this config. |
| 2062 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); |
| 2063 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 2064 | } |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 2065 | ALOGV("%s() no matching config found. Fall through to default PCM patch" |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 2066 | " supporting PCM format conversion.", __func__); |
| 2067 | return patchBuilder; |
| 2068 | } |
| 2069 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 2070 | status_t AudioPolicyManager::setMsdOutputPatches(const DeviceVector *outputDevices) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 2071 | DeviceVector devices; |
| 2072 | if (outputDevices != nullptr && outputDevices->size() > 0) { |
| 2073 | devices.add(*outputDevices); |
| 2074 | } else { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 2075 | // Use media strategy for unspecified output device. This should only |
| 2076 | // occur on checkForDeviceAndOutputChanges(). Device connection events may |
| 2077 | // therefore invalidate explicit routing requests. |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 2078 | devices = mEngine->getOutputDevicesForAttributes( |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2079 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 2080 | 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] | 2081 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 2082 | std::vector<PatchBuilder> patchesToCreate; |
| 2083 | for (auto i = 0u; i < devices.size(); ++i) { |
| 2084 | ALOGV("%s() for device %s", __func__, devices[i]->toString().c_str()); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 2085 | patchesToCreate.push_back(buildMsdPatch(true /*msdIsSource*/, devices[i])); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 2086 | } |
| 2087 | // Retain only the MSD patches associated with outputDevices request. |
| 2088 | // Tear down the others, and create new ones as needed. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 2089 | AudioPatchCollection patchesToRemove = getMsdOutputPatches(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 2090 | for (auto it = patchesToCreate.begin(); it != patchesToCreate.end(); ) { |
| 2091 | auto retainedPatch = false; |
| 2092 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { |
| 2093 | if (audio_patches_are_equal(it->patch(), &patchesToRemove[i]->mPatch)) { |
| 2094 | patchesToRemove.removeItemsAt(i); |
| 2095 | retainedPatch = true; |
| 2096 | break; |
| 2097 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 2098 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 2099 | if (retainedPatch) { |
| 2100 | it = patchesToCreate.erase(it); |
| 2101 | continue; |
| 2102 | } |
| 2103 | ++it; |
| 2104 | } |
| 2105 | if (patchesToCreate.size() == 0 && patchesToRemove.size() == 0) { |
| 2106 | return NO_ERROR; |
| 2107 | } |
| 2108 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { |
| 2109 | auto ¤tPatch = patchesToRemove.valueAt(i); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 2110 | releaseAudioPatch(currentPatch->getHandle(), mUidCached); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 2111 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 2112 | status_t status = NO_ERROR; |
| 2113 | for (const auto &p : patchesToCreate) { |
| 2114 | auto currStatus = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, |
| 2115 | p.patch(), 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); |
| 2116 | char message[256]; |
| 2117 | snprintf(message, sizeof(message), "%s() %s: creating MSD patch from device:IN_BUS to " |
| 2118 | "device:%#x (format:%#x channels:%#x samplerate:%d)", __func__, |
| 2119 | currStatus == NO_ERROR ? "Success" : "Error", |
| 2120 | p.patch()->sinks[0].ext.device.type, p.patch()->sources[0].format, |
| 2121 | p.patch()->sources[0].channel_mask, p.patch()->sources[0].sample_rate); |
| 2122 | if (currStatus == NO_ERROR) { |
| 2123 | ALOGD("%s", message); |
| 2124 | } else { |
| 2125 | ALOGE("%s", message); |
| 2126 | if (status == NO_ERROR) { |
| 2127 | status = currStatus; |
| 2128 | } |
| 2129 | } |
| 2130 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 2131 | return status; |
| 2132 | } |
| 2133 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 2134 | void AudioPolicyManager::releaseMsdOutputPatches(const DeviceVector& devices) { |
| 2135 | AudioPatchCollection msdPatches = getMsdOutputPatches(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 2136 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 2137 | const auto& patch = msdPatches[i]; |
| 2138 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 2139 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 2140 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && devices.getDevice(sink->ext.device.type, |
| 2141 | String8(sink->ext.device.address), AUDIO_FORMAT_DEFAULT) != nullptr) { |
| 2142 | releaseAudioPatch(patch->getHandle(), mUidCached); |
| 2143 | break; |
| 2144 | } |
| 2145 | } |
| 2146 | } |
| 2147 | } |
| 2148 | |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 2149 | bool AudioPolicyManager::msdHasPatchesToAllDevices(const AudioDeviceTypeAddrVector& devices) { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 2150 | DeviceVector devicesToCheck = |
| 2151 | mConfig->getOutputDevices().getDevicesFromDeviceTypeAddrVec(devices); |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 2152 | AudioPatchCollection msdPatches = getMsdOutputPatches(); |
| 2153 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 2154 | const auto& patch = msdPatches[i]; |
| 2155 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 2156 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 2157 | if (sink->type == AUDIO_PORT_TYPE_DEVICE) { |
| 2158 | const auto& foundDevice = devicesToCheck.getDevice( |
| 2159 | sink->ext.device.type, String8(sink->ext.device.address), AUDIO_FORMAT_DEFAULT); |
| 2160 | if (foundDevice != nullptr) { |
| 2161 | devicesToCheck.remove(foundDevice); |
| 2162 | if (devicesToCheck.isEmpty()) { |
| 2163 | return true; |
| 2164 | } |
| 2165 | } |
| 2166 | } |
| 2167 | } |
| 2168 | } |
| 2169 | return false; |
| 2170 | } |
| 2171 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2172 | audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs, |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 2173 | audio_output_flags_t flags, |
| 2174 | audio_format_t format, |
| 2175 | audio_channel_mask_t channelMask, |
| 2176 | uint32_t samplingRate, |
| 2177 | audio_session_t sessionId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2178 | { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2179 | LOG_ALWAYS_FATAL_IF(!(format == AUDIO_FORMAT_INVALID || audio_is_linear_pcm(format)), |
| 2180 | "%s called with format %#x", __func__, format); |
| 2181 | |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 2182 | // Return the output that haptic-generating attached to when 1) session id is specified, |
| 2183 | // 2) haptic-generating effect exists for given session id and 3) the output that |
| 2184 | // haptic-generating effect attached to is in given outputs. |
| 2185 | if (sessionId != AUDIO_SESSION_NONE) { |
| 2186 | audio_io_handle_t hapticGeneratingOutput = mEffects.getIoForSession( |
| 2187 | sessionId, FX_IID_HAPTICGENERATOR); |
| 2188 | if (outputs.indexOf(hapticGeneratingOutput) >= 0) { |
| 2189 | return hapticGeneratingOutput; |
| 2190 | } |
| 2191 | } |
| 2192 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2193 | // Flags disqualifying an output: the match must happen before calling selectOutput() |
| 2194 | static const audio_output_flags_t kExcludedFlags = (audio_output_flags_t) |
| 2195 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT); |
| 2196 | |
| 2197 | // Flags expressing a functional request: must be honored in priority over |
| 2198 | // other criteria |
| 2199 | static const audio_output_flags_t kFunctionalFlags = (audio_output_flags_t) |
| 2200 | (AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_INCALL_MUSIC | |
Eric Laurent | e28c66d | 2022-01-21 13:40:41 +0100 | [diff] [blame] | 2201 | AUDIO_OUTPUT_FLAG_TTS | AUDIO_OUTPUT_FLAG_DIRECT_PCM | AUDIO_OUTPUT_FLAG_ULTRASOUND | |
| 2202 | AUDIO_OUTPUT_FLAG_SPATIALIZER); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2203 | // Flags expressing a performance request: have lower priority than serving |
| 2204 | // requested sampling rate or channel mask |
| 2205 | static const audio_output_flags_t kPerformanceFlags = (audio_output_flags_t) |
| 2206 | (AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_DEEP_BUFFER | |
| 2207 | AUDIO_OUTPUT_FLAG_RAW | AUDIO_OUTPUT_FLAG_SYNC); |
| 2208 | |
| 2209 | const audio_output_flags_t functionalFlags = |
| 2210 | (audio_output_flags_t)(flags & kFunctionalFlags); |
| 2211 | const audio_output_flags_t performanceFlags = |
| 2212 | (audio_output_flags_t)(flags & kPerformanceFlags); |
| 2213 | |
| 2214 | audio_io_handle_t bestOutput = (outputs.size() == 0) ? AUDIO_IO_HANDLE_NONE : outputs[0]; |
| 2215 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2216 | // 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] | 2217 | // devices (the list was previously build by getOutputsForDevices()). |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2218 | // The priority is as follows: |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 2219 | // 1: the output supporting haptic playback when requesting haptic playback |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2220 | // 2: the output with the highest number of requested functional flags |
Carter Hsu | 199f189 | 2021-10-15 15:47:29 +0800 | [diff] [blame] | 2221 | // with tiebreak preferring the minimum number of extra functional flags |
| 2222 | // (see b/200293124, the incorrect selection of AUDIO_OUTPUT_FLAG_VOIP_RX). |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2223 | // 3: the output supporting the exact channel mask |
| 2224 | // 4: the output with a higher channel count than requested |
jiabin | b12a6da | 2022-06-03 20:48:18 +0000 | [diff] [blame] | 2225 | // 5: the output with the highest sampling rate if the requested sample rate is |
| 2226 | // greater than default sampling rate |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2227 | // 6: the output with the highest number of requested performance flags |
| 2228 | // 7: the output with the bit depth the closest to the requested one |
| 2229 | // 8: the primary output |
| 2230 | // 9: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2231 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2232 | // matching criteria values in priority order for best matching output so far |
| 2233 | std::vector<uint32_t> bestMatchCriteria(8, 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2234 | |
Shunkai Yao | cb21feb | 2024-07-17 00:34:54 +0000 | [diff] [blame] | 2235 | const bool hasOrphanHaptic = mEffects.hasOrphansForSession(sessionId, FX_IID_HAPTICGENERATOR); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2236 | const uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); |
| 2237 | const uint32_t hapticChannelCount = audio_channel_count_from_out_mask( |
| 2238 | channelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
Eric Laurent | e78b676 | 2018-12-19 16:29:01 -0800 | [diff] [blame] | 2239 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2240 | for (audio_io_handle_t output : outputs) { |
| 2241 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2242 | // matching criteria values in priority order for current output |
| 2243 | std::vector<uint32_t> currentMatchCriteria(8, 0); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 2244 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2245 | if (outputDesc->isDuplicated()) { |
| 2246 | continue; |
| 2247 | } |
| 2248 | if ((kExcludedFlags & outputDesc->mFlags) != 0) { |
| 2249 | continue; |
| 2250 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2251 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2252 | // If haptic channel is specified, use the haptic output if present. |
| 2253 | // When using haptic output, same audio format and sample rate are required. |
| 2254 | const uint32_t outputHapticChannelCount = audio_channel_count_from_out_mask( |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2255 | outputDesc->getChannelMask() & AUDIO_CHANNEL_HAPTIC_ALL); |
Shunkai Yao | 28f14bd | 2024-04-05 22:50:56 +0000 | [diff] [blame] | 2256 | // skip if haptic channel specified but output does not support it, or output support haptic |
| 2257 | // but there is no haptic channel requested AND no orphan haptic effect exist |
| 2258 | if ((hapticChannelCount != 0 && outputHapticChannelCount == 0) || |
| 2259 | (hapticChannelCount == 0 && outputHapticChannelCount != 0 && !hasOrphanHaptic)) { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2260 | continue; |
| 2261 | } |
Shunkai Yao | 28f14bd | 2024-04-05 22:50:56 +0000 | [diff] [blame] | 2262 | // In the case of audio-coupled-haptic playback, there is no format conversion and |
| 2263 | // resampling in the framework, same format/channel/sampleRate for client and the output |
| 2264 | // thread is required. In the case of HapticGenerator effect, do not require format |
| 2265 | // matching. |
| 2266 | if ((outputHapticChannelCount >= hapticChannelCount && format == outputDesc->getFormat() && |
| 2267 | samplingRate == outputDesc->getSamplingRate()) || |
Shunkai Yao | 4c3af93 | 2024-04-26 04:12:21 +0000 | [diff] [blame] | 2268 | (outputHapticChannelCount != 0 && hasOrphanHaptic)) { |
Shunkai Yao | 28f14bd | 2024-04-05 22:50:56 +0000 | [diff] [blame] | 2269 | currentMatchCriteria[0] = outputHapticChannelCount; |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | // functional flags match |
Carter Hsu | 199f189 | 2021-10-15 15:47:29 +0800 | [diff] [blame] | 2273 | const int matchingFunctionalFlags = |
| 2274 | __builtin_popcount(outputDesc->mFlags & functionalFlags); |
| 2275 | const int totalFunctionalFlags = |
| 2276 | __builtin_popcount(outputDesc->mFlags & kFunctionalFlags); |
| 2277 | // Prefer matching functional flags, but subtract unnecessary functional flags. |
| 2278 | currentMatchCriteria[1] = 100 * (matchingFunctionalFlags + 1) - totalFunctionalFlags; |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2279 | |
| 2280 | // channel mask and channel count match |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2281 | uint32_t outputChannelCount = audio_channel_count_from_out_mask( |
| 2282 | outputDesc->getChannelMask()); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2283 | if (channelMask != AUDIO_CHANNEL_NONE && channelCount > 2 && |
| 2284 | channelCount <= outputChannelCount) { |
| 2285 | if ((audio_channel_mask_get_representation(channelMask) == |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2286 | audio_channel_mask_get_representation(outputDesc->getChannelMask())) && |
| 2287 | ((channelMask & outputDesc->getChannelMask()) == channelMask)) { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2288 | currentMatchCriteria[2] = outputChannelCount; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2289 | } |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2290 | currentMatchCriteria[3] = outputChannelCount; |
| 2291 | } |
| 2292 | |
| 2293 | // sampling rate match |
jiabin | b12a6da | 2022-06-03 20:48:18 +0000 | [diff] [blame] | 2294 | if (samplingRate > SAMPLE_RATE_HZ_DEFAULT) { |
Richard Folke Tullberg | 67c12fe | 2024-02-21 12:00:06 +0100 | [diff] [blame] | 2295 | int diff; // avoid unsigned integer overflow. |
| 2296 | __builtin_sub_overflow(outputDesc->getSamplingRate(), samplingRate, &diff); |
| 2297 | |
| 2298 | // prefer the closest output sampling rate greater than or equal to target |
| 2299 | // if none exists, prefer the closest output sampling rate less than target. |
| 2300 | // |
| 2301 | // criteria is offset to make non-negative. |
| 2302 | currentMatchCriteria[4] = diff >= 0 ? -diff + 200'000'000 : diff + 100'000'000; |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | // performance flags match |
| 2306 | currentMatchCriteria[5] = popcount(outputDesc->mFlags & performanceFlags); |
| 2307 | |
| 2308 | // format match |
| 2309 | if (format != AUDIO_FORMAT_INVALID) { |
| 2310 | currentMatchCriteria[6] = |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 2311 | PolicyAudioPort::kFormatDistanceMax - |
| 2312 | PolicyAudioPort::formatDistance(format, outputDesc->getFormat()); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | // primary output match |
| 2316 | currentMatchCriteria[7] = outputDesc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY; |
| 2317 | |
| 2318 | // compare match criteria by priority then value |
| 2319 | if (std::lexicographical_compare(bestMatchCriteria.begin(), bestMatchCriteria.end(), |
| 2320 | currentMatchCriteria.begin(), currentMatchCriteria.end())) { |
| 2321 | bestMatchCriteria = currentMatchCriteria; |
| 2322 | bestOutput = output; |
| 2323 | |
| 2324 | std::stringstream result; |
| 2325 | std::copy(bestMatchCriteria.begin(), bestMatchCriteria.end(), |
| 2326 | std::ostream_iterator<int>(result, " ")); |
| 2327 | ALOGV("%s new bestOutput %d criteria %s", |
| 2328 | __func__, bestOutput, result.str().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2329 | } |
| 2330 | } |
| 2331 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2332 | return bestOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2333 | } |
| 2334 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2335 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2336 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2337 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2338 | |
| 2339 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2340 | if (outputDesc == 0) { |
| 2341 | ALOGW("startOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2342 | return BAD_VALUE; |
| 2343 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2344 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2345 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2346 | ALOGV("startOutput() output %d, stream %d, session %d", |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2347 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2348 | |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 2349 | if (com::android::media::audioserver::fix_concurrent_playback_behavior_with_bit_perfect_client() |
| 2350 | && gHighPriorityUseCases.count(client->attributes().usage) != 0 |
| 2351 | && outputDesc->isBitPerfect()) { |
| 2352 | // Usually, APM selects bit-perfect output for high priority use cases only when |
| 2353 | // bit-perfect output is the only output that can be routed to the selected device. |
| 2354 | // However, here is no need to play high priority use cases such as ringtone and alarm |
| 2355 | // on the bit-perfect path. Reopen the output and return DEAD_OBJECT so that the client |
| 2356 | // can attach to new output. |
| 2357 | ALOGD("%s: reopen bit-perfect output as high priority use case(%d) is starting", |
| 2358 | __func__, client->stream()); |
| 2359 | reopenOutput(outputDesc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 2360 | return DEAD_OBJECT; |
| 2361 | } |
| 2362 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2363 | status_t status = outputDesc->start(); |
| 2364 | if (status != NO_ERROR) { |
| 2365 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2366 | } |
| 2367 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2368 | uint32_t delayMs; |
| 2369 | status = startSource(outputDesc, client, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2370 | |
| 2371 | if (status != NO_ERROR) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2372 | outputDesc->stop(); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2373 | if (status == DEAD_OBJECT) { |
| 2374 | sp<SwAudioOutputDescriptor> desc = |
| 2375 | reopenOutput(outputDesc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 2376 | if (desc == nullptr) { |
| 2377 | // This is not common, it may indicate something wrong with the HAL. |
| 2378 | ALOGE("%s unable to open output with default config", __func__); |
| 2379 | return status; |
| 2380 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2381 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2382 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2383 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2384 | |
| 2385 | // If the client is the first one active on preferred mixer parameters, reopen the output |
| 2386 | // if the current mixer parameters doesn't match the preferred one. |
| 2387 | if (outputDesc->devices().size() == 1) { |
| 2388 | sp<PreferredMixerAttributesInfo> info = getPreferredMixerAttributesInfo( |
| 2389 | outputDesc->devices()[0]->getId(), client->strategy()); |
| 2390 | if (info != nullptr && info->getUid() == client->uid()) { |
| 2391 | if (info->getActiveClientCount() == 0 && !outputDesc->isConfigurationMatched( |
| 2392 | info->getConfigBase(), info->getFlags())) { |
| 2393 | stopSource(outputDesc, client); |
| 2394 | outputDesc->stop(); |
| 2395 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 2396 | config.channel_mask = info->getConfigBase().channel_mask; |
| 2397 | config.sample_rate = info->getConfigBase().sample_rate; |
| 2398 | config.format = info->getConfigBase().format; |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2399 | sp<SwAudioOutputDescriptor> desc = |
| 2400 | reopenOutput(outputDesc, &config, info->getFlags(), __func__); |
| 2401 | if (desc == nullptr) { |
| 2402 | return BAD_VALUE; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2403 | } |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 2404 | desc->mPreferredAttrInfo = info; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2405 | // Intentionally return error to let the client side resending request for |
| 2406 | // creating and starting. |
| 2407 | return DEAD_OBJECT; |
| 2408 | } |
| 2409 | info->increaseActiveClient(); |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 2410 | if (info->getActiveClientCount() == 1 && info->isBitPerfect()) { |
jiabin | e3d1f55 | 2023-06-14 17:42:17 +0000 | [diff] [blame] | 2411 | // If it is first bit-perfect client, reroute all clients that will be routed to |
| 2412 | // the bit-perfect sink so that it is guaranteed only bit-perfect stream is active. |
| 2413 | PortHandleVector clientsToInvalidate; |
jiabin | e6b8749 | 2024-09-18 23:36:14 +0000 | [diff] [blame] | 2414 | std::vector<sp<SwAudioOutputDescriptor>> outputsToResetDevice; |
jiabin | e3d1f55 | 2023-06-14 17:42:17 +0000 | [diff] [blame] | 2415 | for (size_t i = 0; i < mOutputs.size(); i++) { |
jiabin | fedb92e | 2024-09-16 21:36:30 +0000 | [diff] [blame] | 2416 | if (mOutputs[i] == outputDesc || (!mOutputs[i]->devices().isEmpty() && |
| 2417 | mOutputs[i]->devices().filter(outputDesc->devices()).isEmpty())) { |
jiabin | e3d1f55 | 2023-06-14 17:42:17 +0000 | [diff] [blame] | 2418 | continue; |
| 2419 | } |
jiabin | e6b8749 | 2024-09-18 23:36:14 +0000 | [diff] [blame] | 2420 | if (mOutputs[i]->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
| 2421 | outputsToResetDevice.push_back(mOutputs[i]); |
| 2422 | } |
jiabin | e3d1f55 | 2023-06-14 17:42:17 +0000 | [diff] [blame] | 2423 | for (const auto& c : mOutputs[i]->getClientIterable()) { |
| 2424 | clientsToInvalidate.push_back(c->portId()); |
| 2425 | } |
| 2426 | } |
| 2427 | if (!clientsToInvalidate.empty()) { |
| 2428 | ALOGD("%s Invalidate clients due to first bit-perfect client started", |
| 2429 | __func__); |
| 2430 | mpClientInterface->invalidateTracks(clientsToInvalidate); |
| 2431 | } |
jiabin | e6b8749 | 2024-09-18 23:36:14 +0000 | [diff] [blame] | 2432 | for (const auto& output : outputsToResetDevice) { |
| 2433 | resetOutputDevice(output, 0 /*delayMs*/, nullptr /*patchHandle*/); |
| 2434 | } |
jiabin | e3d1f55 | 2023-06-14 17:42:17 +0000 | [diff] [blame] | 2435 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2436 | } |
| 2437 | } |
| 2438 | |
Jean-Michel Trivi | b1f6e64 | 2023-02-07 20:49:04 +0000 | [diff] [blame] | 2439 | if (client->hasPreferredDevice()) { |
| 2440 | // playback activity with preferred device impacts routing occurred, inform upper layers |
| 2441 | mpClientInterface->onRoutingUpdated(); |
| 2442 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2443 | if (delayMs != 0) { |
| 2444 | usleep(delayMs * 1000); |
| 2445 | } |
| 2446 | |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 2447 | if (status == NO_ERROR && |
| 2448 | outputDesc->mPreferredAttrInfo != nullptr && |
| 2449 | outputDesc->isBitPerfect() && |
| 2450 | com::android::media::audioserver:: |
| 2451 | fix_concurrent_playback_behavior_with_bit_perfect_client()) { |
| 2452 | // A new client is started on bit-perfect output, update all clients internal mute. |
| 2453 | updateClientsInternalMute(outputDesc); |
| 2454 | } |
| 2455 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2456 | return status; |
| 2457 | } |
| 2458 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2459 | bool AudioPolicyManager::isLeUnicastActive() const { |
| 2460 | if (isInCall()) { |
| 2461 | return true; |
| 2462 | } |
| 2463 | return isAnyDeviceTypeActive(getAudioDeviceOutLeAudioUnicastSet()); |
| 2464 | } |
| 2465 | |
| 2466 | bool AudioPolicyManager::isAnyDeviceTypeActive(const DeviceTypeSet& deviceTypes) const { |
| 2467 | if (mAvailableOutputDevices.getDevicesFromTypes(deviceTypes).isEmpty()) { |
| 2468 | return false; |
| 2469 | } |
| 2470 | bool active = mOutputs.isAnyDeviceTypeActive(deviceTypes); |
| 2471 | ALOGV("%s active %d", __func__, active); |
| 2472 | return active; |
| 2473 | } |
| 2474 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2475 | status_t AudioPolicyManager::startSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 2476 | const sp<TrackClientDescriptor>& client, |
| 2477 | uint32_t *delayMs) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2478 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2479 | // cannot start playback of STREAM_TTS if any other output is being used |
| 2480 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2481 | |
| 2482 | *delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2483 | audio_stream_type_t stream = client->stream(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2484 | auto clientVolSrc = client->volumeSource(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2485 | auto clientStrategy = client->strategy(); |
| 2486 | auto clientAttr = client->attributes(); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2487 | if (stream == AUDIO_STREAM_TTS) { |
| 2488 | ALOGV("\t found BEACON stream"); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2489 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive( |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 2490 | toVolumeSource(AUDIO_STREAM_TTS, false) /*sourceToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2491 | return INVALID_OPERATION; |
| 2492 | } else { |
| 2493 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 2494 | } |
| 2495 | } else { |
| 2496 | // some playback other than beacon starts |
| 2497 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 2498 | } |
| 2499 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2500 | // 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] | 2501 | // check active before incrementing usage count |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 2502 | bool force = !outputDesc->isActive() && !outputDesc->isRouted(); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2503 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2504 | DeviceVector devices; |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2505 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2506 | const char *address = NULL; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2507 | if (policyMix != nullptr) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2508 | audio_devices_t newDeviceType; |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2509 | address = policyMix->mDeviceAddress.c_str(); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2510 | 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] | 2511 | newDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2512 | } else { |
| 2513 | newDeviceType = policyMix->mDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2514 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2515 | sp device = mAvailableOutputDevices.getDevice(newDeviceType, String8(address), |
| 2516 | AUDIO_FORMAT_DEFAULT); |
| 2517 | ALOG_ASSERT(device, "%s: no device found t=%u, a=%s", __func__, newDeviceType, address); |
| 2518 | devices.add(device); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2519 | } |
| 2520 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2521 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 2522 | // It covers a common case when there is no materially active audio |
| 2523 | // and muting would result in unnecessary delay and dropped audio. |
| 2524 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 2525 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2526 | bool wasLeUnicastActive = isLeUnicastActive(); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2527 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2528 | // increment usage count for this stream on the requested output: |
| 2529 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 2530 | // 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] | 2531 | outputDesc->setClientActive(client, true); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2532 | |
| 2533 | if (client->hasPreferredDevice(true)) { |
Eric Laurent | 72af801 | 2023-03-15 17:36:22 +0100 | [diff] [blame] | 2534 | if (outputDesc->sameExclusivePreferredDevicesCount() > 0) { |
François Gaffie | f96e543 | 2019-04-09 17:13:56 +0200 | [diff] [blame] | 2535 | // Preferred device may be exclusive, use only if no other active clients on this output |
| 2536 | devices = DeviceVector( |
| 2537 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId())); |
| 2538 | } else { |
| 2539 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
| 2540 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2541 | if (devices != outputDesc->devices()) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2542 | checkStrategyRoute(clientStrategy, outputDesc->mIoHandle); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2543 | } |
| 2544 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2545 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2546 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2547 | selectOutputForMusicEffects(); |
| 2548 | } |
| 2549 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2550 | if (outputDesc->getActivityCount(clientVolSrc) == 1 || !devices.isEmpty()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2551 | // starting an output being rerouted? |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2552 | if (devices.isEmpty()) { |
| 2553 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2554 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2555 | bool shouldWait = |
| 2556 | (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM)) || |
| 2557 | followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_NOTIFICATION)) || |
| 2558 | (beaconMuteLatency > 0)); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2559 | uint32_t waitMs = beaconMuteLatency; |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 2560 | const bool needToCloseBitPerfectOutput = |
| 2561 | (com::android::media::audioserver:: |
| 2562 | fix_concurrent_playback_behavior_with_bit_perfect_client() && |
| 2563 | gHighPriorityUseCases.count(clientAttr.usage) != 0); |
| 2564 | std::vector<sp<SwAudioOutputDescriptor>> outputsToReopen; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2565 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2566 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2567 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2568 | // An output has a shared device if |
| 2569 | // - managed by the same hw module |
| 2570 | // - supports the currently selected device |
| 2571 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2572 | && (!desc->filterSupportedDevices(devices).isEmpty()); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2573 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2574 | // force a device change if any other output is: |
| 2575 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 2576 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2577 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2578 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2579 | // 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] | 2580 | // change the device currently selected by the other output. |
| 2581 | if (sharedDevice && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2582 | desc->devices() != devices && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2583 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2584 | force = true; |
| 2585 | } |
| 2586 | // 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] | 2587 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 2588 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2589 | const uint32_t latencyMs = desc->latency(); |
| 2590 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 2591 | |
| 2592 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 2593 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2594 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2595 | |
| 2596 | // Require mute check if another output is on a shared device |
| 2597 | // and currently active to have proper drain and avoid pops. |
| 2598 | // Note restoring AudioTracks onto this output needs to invoke |
| 2599 | // a volume ramp if there is no mute. |
| 2600 | requiresMuteCheck |= sharedDevice && isActive; |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 2601 | |
| 2602 | if (needToCloseBitPerfectOutput && desc->isBitPerfect()) { |
| 2603 | outputsToReopen.push_back(desc); |
| 2604 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2605 | } |
| 2606 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2607 | |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 2608 | if (outputDesc->mPreferredAttrInfo != nullptr && devices != outputDesc->devices()) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2609 | // If the output is open with preferred mixer attributes, but the routed device is |
| 2610 | // changed when calling this function, returning DEAD_OBJECT to indicate routing |
| 2611 | // changed. |
| 2612 | return DEAD_OBJECT; |
| 2613 | } |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 2614 | for (auto& outputToReopen : outputsToReopen) { |
| 2615 | reopenOutput(outputToReopen, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 2616 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2617 | const uint32_t muteWaitMs = |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 2618 | setOutputDevices(__func__, outputDesc, devices, force, 0, nullptr, |
| 2619 | requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2620 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2621 | // apply volume rules for current stream and device if necessary |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2622 | auto &curves = getVolumeCurves(client->attributes()); |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2623 | if (NO_ERROR != checkAndSetVolume(curves, client->volumeSource(), |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2624 | curves.getVolumeIndex(outputDesc->devices().types()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2625 | outputDesc, |
Francois Gaffie | d11442b | 2020-04-27 11:51:09 +0200 | [diff] [blame] | 2626 | outputDesc->devices().types(), 0 /*delay*/, |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2627 | outputDesc->useHwGain() /*force*/)) { |
| 2628 | // request AudioService to reinitialize the volume curves asynchronously |
| 2629 | ALOGE("checkAndSetVolume failed, requesting volume range init"); |
| 2630 | mpClientInterface->onVolumeRangeInitRequest(); |
| 2631 | }; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2632 | |
| 2633 | // update the outputs if starting an output with a stream that can affect notification |
| 2634 | // routing |
| 2635 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2636 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 2637 | // force reevaluating accessibility routing when ringtone or alarm starts |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2638 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM))) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 2639 | invalidateStreams({AUDIO_STREAM_ACCESSIBILITY}); |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 2640 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2641 | |
| 2642 | if (waitMs > muteWaitMs) { |
| 2643 | *delayMs = waitMs - muteWaitMs; |
| 2644 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2645 | |
| 2646 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 2647 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 2648 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 2649 | // change occurs after the MixerThread starts and causes a stream volume |
| 2650 | // glitch. |
| 2651 | // |
| 2652 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2653 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2654 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2655 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2656 | mEngine->getForceUse( |
| 2657 | AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2658 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), true, outputDesc); |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2659 | } |
| 2660 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2661 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 2662 | // of type MIX_TYPE_RECORDERS |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2663 | if (isSingleDeviceType(devices.types(), &audio_is_remote_submix_device) && |
| 2664 | policyMix != NULL && policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2665 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2666 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2667 | address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2668 | "remote-submix", |
| 2669 | AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2670 | } |
| 2671 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2672 | checkLeBroadcastRoutes(wasLeUnicastActive, outputDesc, *delayMs); |
| 2673 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2674 | return NO_ERROR; |
| 2675 | } |
| 2676 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2677 | void AudioPolicyManager::checkLeBroadcastRoutes(bool wasUnicastActive, |
| 2678 | sp<SwAudioOutputDescriptor> ignoredOutput, uint32_t delayMs) { |
| 2679 | bool isUnicastActive = isLeUnicastActive(); |
| 2680 | |
| 2681 | if (wasUnicastActive != isUnicastActive) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2682 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2683 | //reroute all outputs routed to LE broadcast if LE unicast activy changed on any output |
| 2684 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2685 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2686 | if (desc != ignoredOutput && desc->isActive() |
| 2687 | && ((isUnicastActive && |
| 2688 | !desc->devices(). |
| 2689 | getDevicesFromType(AUDIO_DEVICE_OUT_BLE_BROADCAST).isEmpty()) |
| 2690 | || (wasUnicastActive && |
| 2691 | !desc->devices().getDevicesFromTypes( |
| 2692 | getAudioDeviceOutLeAudioUnicastSet()).isEmpty()))) { |
| 2693 | DeviceVector newDevices = getNewOutputDevices(desc, false /*fromCache*/); |
| 2694 | bool force = desc->devices() != newDevices; |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 2695 | if (desc->mPreferredAttrInfo != nullptr && force) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2696 | // If the device is using preferred mixer attributes, the output need to reopen |
| 2697 | // with default configuration when the new selected devices are different from |
| 2698 | // current routing devices. |
| 2699 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices); |
| 2700 | continue; |
| 2701 | } |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 2702 | setOutputDevices(__func__, desc, newDevices, force, delayMs); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2703 | // re-apply device specific volume if not done by setOutputDevice() |
| 2704 | if (!force) { |
| 2705 | applyStreamVolumes(desc, newDevices.types(), delayMs); |
| 2706 | } |
| 2707 | } |
| 2708 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2709 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2710 | } |
| 2711 | } |
| 2712 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2713 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2714 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2715 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2716 | |
| 2717 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2718 | if (outputDesc == 0) { |
| 2719 | ALOGW("stopOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2720 | return BAD_VALUE; |
| 2721 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2722 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2723 | |
Jean-Michel Trivi | b1f6e64 | 2023-02-07 20:49:04 +0000 | [diff] [blame] | 2724 | if (client->hasPreferredDevice(true)) { |
| 2725 | // playback activity with preferred device impacts routing occurred, inform upper layers |
| 2726 | mpClientInterface->onRoutingUpdated(); |
| 2727 | } |
| 2728 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2729 | ALOGV("stopOutput() output %d, stream %d, session %d", |
| 2730 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2731 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2732 | status_t status = stopSource(outputDesc, client); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2733 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2734 | if (status == NO_ERROR ) { |
| 2735 | outputDesc->stop(); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2736 | } else { |
| 2737 | return status; |
| 2738 | } |
| 2739 | |
| 2740 | if (outputDesc->devices().size() == 1) { |
| 2741 | sp<PreferredMixerAttributesInfo> info = getPreferredMixerAttributesInfo( |
| 2742 | outputDesc->devices()[0]->getId(), client->strategy()); |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 2743 | bool outputReopened = false; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2744 | if (info != nullptr && info->getUid() == client->uid()) { |
| 2745 | info->decreaseActiveClient(); |
| 2746 | if (info->getActiveClientCount() == 0) { |
| 2747 | reopenOutput(outputDesc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 2748 | outputReopened = true; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2749 | } |
| 2750 | } |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 2751 | if (com::android::media::audioserver:: |
| 2752 | fix_concurrent_playback_behavior_with_bit_perfect_client() && |
| 2753 | !outputReopened && outputDesc->isBitPerfect()) { |
| 2754 | // Only need to update the clients' internal mute when the output is bit-perfect and it |
| 2755 | // is not reopened. |
| 2756 | updateClientsInternalMute(outputDesc); |
| 2757 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2758 | } |
| 2759 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2760 | } |
| 2761 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2762 | status_t AudioPolicyManager::stopSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 2763 | const sp<TrackClientDescriptor>& client) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2764 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2765 | // always handle stream stop, check which stream type is stopping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2766 | audio_stream_type_t stream = client->stream(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2767 | auto clientVolSrc = client->volumeSource(); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2768 | bool wasLeUnicastActive = isLeUnicastActive(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2769 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2770 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 2771 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2772 | if (outputDesc->getActivityCount(clientVolSrc) > 0) { |
| 2773 | if (outputDesc->getActivityCount(clientVolSrc) == 1) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2774 | // Automatically disable the remote submix input when output is stopped on a |
| 2775 | // re routing mix of type MIX_TYPE_RECORDERS |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2776 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2777 | if (isSingleDeviceType( |
| 2778 | outputDesc->devices().types(), &audio_is_remote_submix_device) && |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2779 | policyMix != nullptr && |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2780 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2781 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2782 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2783 | policyMix->mDeviceAddress, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2784 | "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2785 | } |
| 2786 | } |
| 2787 | bool forceDeviceUpdate = false; |
Eric Laurent | 72af801 | 2023-03-15 17:36:22 +0100 | [diff] [blame] | 2788 | if (client->hasPreferredDevice(true) && |
| 2789 | outputDesc->sameExclusivePreferredDevicesCount() < 2) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2790 | checkStrategyRoute(client->strategy(), AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2791 | forceDeviceUpdate = true; |
| 2792 | } |
| 2793 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2794 | // decrement usage count of this stream on the output |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 2795 | outputDesc->setClientActive(client, false); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 2796 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2797 | // store time at which the stream was stopped - see isStreamActive() |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2798 | if (outputDesc->getActivityCount(clientVolSrc) == 0 || forceDeviceUpdate) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2799 | outputDesc->setStopTime(client, systemTime()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2800 | DeviceVector newDevices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 2801 | |
| 2802 | // If the routing does not change, if an output is routed on a device using HwGain |
| 2803 | // (aka setAudioPortConfig) and there are still active clients following different |
| 2804 | // volume group(s), force reapply volume |
| 2805 | bool requiresVolumeCheck = outputDesc->getActivityCount(clientVolSrc) == 0 && |
| 2806 | outputDesc->useHwGain() && outputDesc->isAnyActive(VOLUME_SOURCE_NONE); |
| 2807 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2808 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 2809 | // the track stop() command is received and at that time the audio track buffer can |
| 2810 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 2811 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 2812 | // audio path (audio DSP, CODEC ...) |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 2813 | setOutputDevices(__func__, outputDesc, newDevices, false, outputDesc->latency()*2, |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 2814 | nullptr, true /*requiresMuteCheck*/, requiresVolumeCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2815 | |
| 2816 | // force restoring the device selection on other active outputs if it differs from the |
| 2817 | // one being selected for this output |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2818 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2819 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2820 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2821 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2822 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2823 | desc->isActive() && |
| 2824 | outputDesc->sharesHwModuleWith(desc) && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2825 | (newDevices != desc->devices())) { |
| 2826 | DeviceVector newDevices2 = getNewOutputDevices(desc, false /*fromCache*/); |
| 2827 | bool force = desc->devices() != newDevices2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 2828 | |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 2829 | if (desc->mPreferredAttrInfo != nullptr && force) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2830 | // If the device is using preferred mixer attributes, the output need to |
| 2831 | // reopen with default configuration when the new selected devices are |
| 2832 | // different from current routing devices. |
| 2833 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices2); |
| 2834 | continue; |
| 2835 | } |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 2836 | setOutputDevices(__func__, desc, newDevices2, force, delayMs); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2837 | |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2838 | // re-apply device specific volume if not done by setOutputDevice() |
| 2839 | if (!force) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2840 | applyStreamVolumes(desc, newDevices2.types(), delayMs); |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2841 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2842 | } |
| 2843 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2844 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2845 | // update the outputs if stopping one with a stream that can affect notification routing |
| 2846 | handleNotificationRoutingForStream(stream); |
| 2847 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2848 | |
| 2849 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 2850 | 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] | 2851 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), false, outputDesc); |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2852 | } |
| 2853 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2854 | if (followsSameRouting(client->attributes(), attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2855 | selectOutputForMusicEffects(); |
| 2856 | } |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2857 | |
| 2858 | checkLeBroadcastRoutes(wasLeUnicastActive, outputDesc, outputDesc->latency()*2); |
| 2859 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2860 | return NO_ERROR; |
| 2861 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2862 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2863 | return INVALID_OPERATION; |
| 2864 | } |
| 2865 | } |
| 2866 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2867 | bool AudioPolicyManager::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2868 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2869 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2870 | |
| 2871 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2872 | if (outputDesc == 0) { |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2873 | // If an output descriptor is closed due to a device routing change, |
| 2874 | // then there are race conditions with releaseOutput from tracks |
| 2875 | // that may be destroyed (with no PlaybackThread) or a PlaybackThread |
| 2876 | // destroyed shortly thereafter. |
| 2877 | // |
| 2878 | // Here we just log a warning, instead of a fatal error. |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2879 | ALOGW("releaseOutput() no output for client %d", portId); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2880 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2881 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2882 | |
| 2883 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2884 | |
Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2885 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
| 2886 | if (outputDesc->isClientActive(client)) { |
| 2887 | ALOGW("releaseOutput() inactivates portId %d in good faith", portId); |
| 2888 | stopOutput(portId); |
| 2889 | } |
| 2890 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2891 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 2892 | if (outputDesc->mDirectOpenCount <= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2893 | ALOGW("releaseOutput() invalid open count %d for output %d", |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2894 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2895 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2896 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2897 | if (--outputDesc->mDirectOpenCount == 0) { |
| 2898 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2899 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2900 | } |
| 2901 | } |
Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2902 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2903 | outputDesc->removeClient(portId); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2904 | if (outputDesc->mPendingReopenToQueryProfiles && outputDesc->getClientCount() == 0) { |
| 2905 | // The output is pending reopened to query dynamic profiles and |
| 2906 | // there is no active clients |
| 2907 | closeOutput(outputDesc->mIoHandle); |
| 2908 | sp<SwAudioOutputDescriptor> newOutputDesc = openOutputWithProfileAndDevice( |
| 2909 | outputDesc->mProfile, mEngine->getActiveMediaDevices(mAvailableOutputDevices)); |
| 2910 | if (newOutputDesc == nullptr) { |
| 2911 | ALOGE("%s failed to open output", __func__); |
| 2912 | } |
| 2913 | return true; |
| 2914 | } |
| 2915 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2916 | } |
| 2917 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2918 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 2919 | audio_io_handle_t *input, |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2920 | audio_unique_id_t riid, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2921 | audio_session_t session, |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2922 | const AttributionSourceState& attributionSource, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2923 | audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2924 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2925 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2926 | input_type_t *inputType, |
Marvin Ramin | e5a122d | 2023-12-07 13:57:59 +0100 | [diff] [blame] | 2927 | audio_port_handle_t *portId, |
| 2928 | uint32_t *virtualDeviceId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2929 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2930 | 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] | 2931 | "flags %#x attributes=%s requested device ID %d", |
| 2932 | __func__, attr->source, config->sample_rate, config->format, config->channel_mask, |
| 2933 | session, flags, toString(*attr).c_str(), *selectedDeviceId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2934 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2935 | status_t status = NO_ERROR; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2936 | audio_attributes_t attributes = *attr; |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2937 | sp<AudioPolicyMix> policyMix; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2938 | sp<DeviceDescriptor> device; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2939 | sp<AudioInputDescriptor> inputDesc; |
François Gaffie | 1b4753e | 2023-02-06 10:36:33 +0100 | [diff] [blame] | 2940 | sp<AudioInputDescriptor> previousInputDesc; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2941 | sp<RecordClientDescriptor> clientDesc; |
| 2942 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2943 | 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] | 2944 | bool isSoundTrigger; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2945 | |
| 2946 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 2947 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 2948 | return INVALID_OPERATION; |
| 2949 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2950 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2951 | if (attr->source == AUDIO_SOURCE_DEFAULT) { |
| 2952 | attributes.source = AUDIO_SOURCE_MIC; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2953 | } |
| 2954 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2955 | // Explicit routing? |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 2956 | sp<DeviceDescriptor> explicitRoutingDevice = |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2957 | mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2958 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2959 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 2960 | // possible |
| 2961 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 2962 | *input != AUDIO_IO_HANDLE_NONE) { |
| 2963 | ssize_t index = mInputs.indexOfKey(*input); |
| 2964 | if (index < 0) { |
| 2965 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 2966 | status = BAD_VALUE; |
| 2967 | goto error; |
| 2968 | } |
| 2969 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2970 | RecordClientVector clients = inputDesc->getClientsForSession(session); |
| 2971 | if (clients.size() == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2972 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 2973 | status = BAD_VALUE; |
| 2974 | goto error; |
| 2975 | } |
| 2976 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 2977 | // 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] | 2978 | // corresponds to a new client and is only permitted from the same UID. |
| 2979 | // 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] | 2980 | if (clients.size() > 1) { |
| 2981 | for (const auto& client : clients) { |
| 2982 | // The client map is ordered by key values (portId) and portIds are allocated |
| 2983 | // incrementaly. So the first client in this list is the one opened by audio flinger |
| 2984 | // when the mmap stream is created and should be ignored as it does not correspond |
| 2985 | // to an actual client |
| 2986 | if (client == *clients.cbegin()) { |
| 2987 | continue; |
| 2988 | } |
| 2989 | if (uid != client->uid() && !client->isSilenced()) { |
| 2990 | ALOGW("getInputForAttr() bad uid %d for client %d uid %d", |
| 2991 | uid, client->portId(), client->uid()); |
| 2992 | status = INVALID_OPERATION; |
| 2993 | goto error; |
| 2994 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 2995 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2996 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2997 | *inputType = API_INPUT_LEGACY; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2998 | device = inputDesc->getDevice(); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2999 | |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3000 | ALOGV("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3001 | goto exit; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | *input = AUDIO_IO_HANDLE_NONE; |
| 3005 | *inputType = API_INPUT_INVALID; |
| 3006 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 3007 | if (attributes.source == AUDIO_SOURCE_REMOTE_SUBMIX && |
Jan Sebechlebsky | bc56bcd | 2022-09-26 13:15:19 +0200 | [diff] [blame] | 3008 | extractAddressFromAudioAttributes(attributes).has_value()) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 3009 | status = mPolicyMixes.getInputMixForAttr(attributes, &policyMix); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 3010 | if (status != NO_ERROR) { |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 3011 | ALOGW("%s could not find input mix for attr %s", |
| 3012 | __func__, toString(attributes).c_str()); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 3013 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3014 | } |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 3015 | device = mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 3016 | String8(attr->tags + strlen("addr=")), |
| 3017 | AUDIO_FORMAT_DEFAULT); |
| 3018 | if (device == nullptr) { |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 3019 | 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] | 3020 | __func__, attributes.source, attributes.tags); |
| 3021 | status = BAD_VALUE; |
| 3022 | goto error; |
| 3023 | } |
| 3024 | |
Kevin Rocard | 25f9b05 | 2019-02-27 15:08:54 -0800 | [diff] [blame] | 3025 | if (is_mix_loopback_render(policyMix->mRouteFlags)) { |
| 3026 | *inputType = API_INPUT_MIX_PUBLIC_CAPTURE_PLAYBACK; |
| 3027 | } else { |
| 3028 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 3029 | } |
Marvin Ramin | e5a122d | 2023-12-07 13:57:59 +0100 | [diff] [blame] | 3030 | if (virtualDeviceId) { |
| 3031 | *virtualDeviceId = policyMix->mVirtualDeviceId; |
| 3032 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3033 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3034 | if (explicitRoutingDevice != nullptr) { |
| 3035 | device = explicitRoutingDevice; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3036 | } else { |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 3037 | // Prevent from storing invalid requested device id in clients |
| 3038 | requestedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 3039 | device = mEngine->getInputDeviceForAttributes(attributes, uid, session, &policyMix); |
yuanjiahsu | 4069d5d | 2021-04-19 07:54:27 +0800 | [diff] [blame] | 3040 | ALOGV_IF(device != nullptr, "%s found device type is 0x%X", |
| 3041 | __FUNCTION__, device->type()); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3042 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3043 | if (device == nullptr) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 3044 | ALOGW("getInputForAttr() could not find device for source %d", attributes.source); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 3045 | status = BAD_VALUE; |
| 3046 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3047 | } |
Alden DSouza | b7d2078 | 2021-02-08 08:51:42 -0800 | [diff] [blame] | 3048 | if (device->type() == AUDIO_DEVICE_IN_ECHO_REFERENCE) { |
| 3049 | *inputType = API_INPUT_MIX_CAPTURE; |
| 3050 | } else if (policyMix) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3051 | ALOG_ASSERT(policyMix->mMixType == MIX_TYPE_RECORDERS, "Invalid Mix Type"); |
| 3052 | // there is an external policy, but this input is attached to a mix of recorders, |
| 3053 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 3054 | // know about it and is therefore considered "legacy" |
| 3055 | *inputType = API_INPUT_LEGACY; |
Marvin Ramin | e5a122d | 2023-12-07 13:57:59 +0100 | [diff] [blame] | 3056 | |
| 3057 | if (virtualDeviceId) { |
| 3058 | *virtualDeviceId = policyMix->mVirtualDeviceId; |
| 3059 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3060 | } else if (audio_is_remote_submix_device(device->type())) { |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 3061 | *inputType = API_INPUT_MIX_CAPTURE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3062 | } else if (device->type() == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 3063 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 3064 | } else { |
| 3065 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3066 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 3067 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3068 | } |
| 3069 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3070 | *input = getInputForDevice(device, session, attributes, config, flags, policyMix); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3071 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 3072 | status = INVALID_OPERATION; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 3073 | AudioProfileVector profiles; |
| 3074 | status_t ret = getProfilesForDevices( |
| 3075 | DeviceVector(device), profiles, flags, true /*isInput*/); |
| 3076 | if (ret == NO_ERROR && !profiles.empty()) { |
Robert Wu | b98ff1b | 2023-06-15 22:53:58 +0000 | [diff] [blame] | 3077 | const auto channels = profiles[0]->getChannels(); |
| 3078 | if (!channels.empty() && (channels.find(config->channel_mask) == channels.end())) { |
| 3079 | config->channel_mask = *channels.begin(); |
| 3080 | } |
| 3081 | const auto sampleRates = profiles[0]->getSampleRates(); |
| 3082 | if (!sampleRates.empty() && |
| 3083 | (sampleRates.find(config->sample_rate) == sampleRates.end())) { |
| 3084 | config->sample_rate = *sampleRates.begin(); |
| 3085 | } |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 3086 | config->format = profiles[0]->getFormat(); |
| 3087 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 3088 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3089 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 3090 | |
Marvin Ramin | e5a122d | 2023-12-07 13:57:59 +0100 | [diff] [blame] | 3091 | |
| 3092 | if (policyMix != nullptr && virtualDeviceId != nullptr) { |
| 3093 | *virtualDeviceId = policyMix->mVirtualDeviceId; |
| 3094 | } |
| 3095 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3096 | exit: |
| 3097 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3098 | *selectedDeviceId = mAvailableInputDevices.contains(device) ? |
| 3099 | device->getId() : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 3100 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 3101 | isSoundTrigger = attributes.source == AUDIO_SOURCE_HOTWORD && |
Carter Hsu | d0cce2e | 2019-05-03 17:36:28 +0800 | [diff] [blame] | 3102 | mSoundTriggerSessions.indexOfKey(session) >= 0; |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 3103 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3104 | |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 3105 | clientDesc = new RecordClientDescriptor(*portId, riid, uid, session, attributes, *config, |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 3106 | requestedDeviceId, attributes.source, flags, |
| 3107 | isSoundTrigger); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3108 | inputDesc = mInputs.valueFor(*input); |
François Gaffie | 1b4753e | 2023-02-06 10:36:33 +0100 | [diff] [blame] | 3109 | // Move (if found) effect for the client session to its input |
| 3110 | mEffects.moveEffectsForIo(session, *input, &mInputs, mpClientInterface); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3111 | inputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3112 | |
| 3113 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", |
| 3114 | *input, *inputType, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 3115 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3116 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 3117 | |
| 3118 | error: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 3119 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3123 | audio_io_handle_t AudioPolicyManager::getInputForDevice(const sp<DeviceDescriptor> &device, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3124 | audio_session_t session, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3125 | const audio_attributes_t &attributes, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 3126 | audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3127 | audio_input_flags_t flags, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3128 | const sp<AudioPolicyMix> &policyMix) |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3129 | { |
| 3130 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3131 | audio_source_t halInputSource = attributes.source; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3132 | bool isSoundTrigger = false; |
| 3133 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3134 | if (attributes.source == AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3135 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 3136 | if (index >= 0) { |
| 3137 | input = mSoundTriggerSessions.valueFor(session); |
| 3138 | isSoundTrigger = true; |
| 3139 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 3140 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 3141 | } else { |
| 3142 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 3143 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3144 | } else if (attributes.source == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3145 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 3146 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 3147 | } |
| 3148 | |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 3149 | if (attributes.source == AUDIO_SOURCE_ULTRASOUND) { |
| 3150 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_ULTRASOUND); |
| 3151 | } |
| 3152 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3153 | // sampling rate and flags may be updated by getInputProfile |
| 3154 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 3155 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 3156 | audio_format_t profileFormat = config->format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3157 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3158 | audio_input_flags_t profileFlags = flags; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 3159 | // find a compatible input profile (not necessarily identical in parameters) |
| 3160 | sp<IOProfile> profile = getInputProfile( |
| 3161 | device, profileSamplingRate, profileFormat, profileChannelMask, profileFlags); |
| 3162 | if (profile == nullptr) { |
| 3163 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3164 | } |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 3165 | |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 3166 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3167 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 3168 | if (samplingRate == 0) { |
| 3169 | samplingRate = profileSamplingRate; |
| 3170 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3171 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3172 | if (profile->getModuleHandle() == 0) { |
| 3173 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3174 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3175 | } |
| 3176 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 3177 | // Reuse an already opened input if a client with the same session ID already exists |
| 3178 | // on that input |
| 3179 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3180 | sp <AudioInputDescriptor> desc = mInputs.valueAt(i); |
| 3181 | if (desc->mProfile != profile) { |
| 3182 | continue; |
| 3183 | } |
| 3184 | RecordClientVector clients = desc->clientsList(); |
| 3185 | for (const auto &client : clients) { |
| 3186 | if (session == client->session()) { |
| 3187 | return desc->mIoHandle; |
| 3188 | } |
| 3189 | } |
| 3190 | } |
| 3191 | |
Eric Laurent | c71b11b | 2024-06-03 12:54:53 +0000 | [diff] [blame] | 3192 | bool isPreemptor = false; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3193 | if (!profile->canOpenNewIo()) { |
Eric Laurent | c71b11b | 2024-06-03 12:54:53 +0000 | [diff] [blame] | 3194 | if (com::android::media::audioserver::fix_input_sharing_logic()) { |
| 3195 | // First pick best candidate for preemption (there may not be any): |
| 3196 | // - Preempt and input if: |
| 3197 | // - It has only strictly lower priority use cases than the new client |
| 3198 | // - It has equal priority use cases than the new client, was not |
| 3199 | // opened thanks to preemption or has been active since opened. |
| 3200 | // - Order the preemption candidates by inactive first and priority second |
| 3201 | sp<AudioInputDescriptor> closeCandidate; |
| 3202 | int leastCloseRank = INT_MAX; |
| 3203 | static const int sCloseActive = 0x100; |
| 3204 | |
| 3205 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3206 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
| 3207 | if (desc->mProfile != profile) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3208 | continue; |
| 3209 | } |
Eric Laurent | c71b11b | 2024-06-03 12:54:53 +0000 | [diff] [blame] | 3210 | sp<RecordClientDescriptor> topPrioClient = desc->getHighestPriorityClient(); |
| 3211 | if (topPrioClient == nullptr) { |
| 3212 | continue; |
| 3213 | } |
| 3214 | int topPrio = source_priority(topPrioClient->source()); |
| 3215 | if (topPrio < source_priority(attributes.source) |
| 3216 | || (topPrio == source_priority(attributes.source) |
| 3217 | && !desc->isPreemptor())) { |
| 3218 | int closeRank = (desc->isActive() ? sCloseActive : 0) + topPrio; |
| 3219 | if (closeRank < leastCloseRank) { |
| 3220 | leastCloseRank = closeRank; |
| 3221 | closeCandidate = desc; |
| 3222 | } |
| 3223 | } |
| 3224 | } |
| 3225 | |
| 3226 | if (closeCandidate != nullptr) { |
| 3227 | closeInput(closeCandidate->mIoHandle); |
| 3228 | // Mark the new input as being issued from a preemption |
| 3229 | // so that is will not be preempted later |
| 3230 | isPreemptor = true; |
| 3231 | } else { |
| 3232 | // Then pick the best reusable input (There is always one) |
| 3233 | // The order of preference is: |
| 3234 | // 1) active inputs with same use case as the new client |
| 3235 | // 2) inactive inputs with same use case |
| 3236 | // 3) active inputs with different use cases |
| 3237 | // 4) inactive inputs with different use cases |
| 3238 | sp<AudioInputDescriptor> reuseCandidate; |
| 3239 | int leastReuseRank = INT_MAX; |
| 3240 | static const int sReuseDifferentUseCase = 0x100; |
| 3241 | |
| 3242 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3243 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
| 3244 | if (desc->mProfile != profile) { |
| 3245 | continue; |
| 3246 | } |
| 3247 | int reuseRank = sReuseDifferentUseCase; |
| 3248 | for (const auto& client: desc->getClientIterable()) { |
| 3249 | if (client->source() == attributes.source) { |
| 3250 | reuseRank = 0; |
| 3251 | break; |
| 3252 | } |
| 3253 | } |
| 3254 | reuseRank += desc->isActive() ? 0 : 1; |
| 3255 | if (reuseRank < leastReuseRank) { |
| 3256 | leastReuseRank = reuseRank; |
| 3257 | reuseCandidate = desc; |
| 3258 | } |
| 3259 | } |
| 3260 | return reuseCandidate->mIoHandle; |
| 3261 | } |
| 3262 | } else { // fix_input_sharing_logic() |
| 3263 | for (size_t i = 0; i < mInputs.size(); ) { |
| 3264 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
| 3265 | if (desc->mProfile != profile) { |
| 3266 | i++; |
| 3267 | continue; |
| 3268 | } |
| 3269 | // if sound trigger, reuse input if used by other sound trigger on same session |
| 3270 | // else |
| 3271 | // reuse input if active client app is not in IDLE state |
| 3272 | // |
| 3273 | RecordClientVector clients = desc->clientsList(); |
| 3274 | bool doClose = false; |
| 3275 | for (const auto& client : clients) { |
| 3276 | if (isSoundTrigger != client->isSoundTrigger()) { |
| 3277 | continue; |
| 3278 | } |
| 3279 | if (client->isSoundTrigger()) { |
| 3280 | if (session == client->session()) { |
| 3281 | return desc->mIoHandle; |
| 3282 | } |
| 3283 | continue; |
| 3284 | } |
| 3285 | if (client->active() && client->appState() != APP_STATE_IDLE) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3286 | return desc->mIoHandle; |
| 3287 | } |
Eric Laurent | c71b11b | 2024-06-03 12:54:53 +0000 | [diff] [blame] | 3288 | doClose = true; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3289 | } |
Eric Laurent | c71b11b | 2024-06-03 12:54:53 +0000 | [diff] [blame] | 3290 | if (doClose) { |
| 3291 | closeInput(desc->mIoHandle); |
| 3292 | } else { |
| 3293 | i++; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3294 | } |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3295 | } |
| 3296 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3297 | } |
| 3298 | |
Eric Laurent | c71b11b | 2024-06-03 12:54:53 +0000 | [diff] [blame] | 3299 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor( |
| 3300 | profile, mpClientInterface, isPreemptor); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3301 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3302 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 3303 | lConfig.sample_rate = profileSamplingRate; |
| 3304 | lConfig.channel_mask = profileChannelMask; |
| 3305 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 3306 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3307 | status_t status = inputDesc->open(&lConfig, device, halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3308 | |
| 3309 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3310 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3311 | (profileSamplingRate != lConfig.sample_rate) || |
| 3312 | !audio_formats_match(profileFormat, lConfig.format) || |
| 3313 | (profileChannelMask != lConfig.channel_mask)) { |
| 3314 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 3315 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3316 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3317 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3318 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3319 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3320 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3321 | } |
| 3322 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3323 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3324 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3325 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3326 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 3327 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3328 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3329 | } |
| 3330 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3331 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId) |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 3332 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3333 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 3334 | |
| 3335 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 3336 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3337 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 3338 | return DEAD_OBJECT; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3339 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3340 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3341 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3342 | if (client->active()) { |
| 3343 | ALOGW("%s input %d client %d already started", __FUNCTION__, input, client->portId()); |
| 3344 | return INVALID_OPERATION; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 3345 | } |
| 3346 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3347 | audio_session_t session = client->session(); |
| 3348 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3349 | ALOGV("%s input:%d, session:%d)", __FUNCTION__, input, session); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3350 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3351 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 3352 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3353 | status_t status = inputDesc->start(); |
| 3354 | if (status != NO_ERROR) { |
| 3355 | return status; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 3356 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3357 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 3358 | // increment activity count before calling getNewInputDevice() below as only active sessions |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 3359 | // are considered for device selection |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3360 | inputDesc->setClientActive(client, true); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 3361 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3362 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 3363 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3364 | sp<DeviceDescriptor> device = getNewInputDevice(inputDesc); |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 3365 | if (device != nullptr) { |
| 3366 | status = setInputDevice(input, device, true /* force */); |
| 3367 | } else { |
| 3368 | ALOGW("%s no new input device can be found for descriptor %d", |
| 3369 | __FUNCTION__, inputDesc->getId()); |
| 3370 | status = BAD_VALUE; |
| 3371 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3372 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 3373 | if (status == NO_ERROR && inputDesc->activeCount() == 1) { |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3374 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3375 | // 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] | 3376 | if ((policyMix != nullptr) |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3377 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 3378 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3379 | MIX_STATE_MIXING); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3380 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3381 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3382 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 3383 | if (primaryInputDevices.contains(device) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3384 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 3385 | mpClientInterface->setSoundTriggerCaptureState(true); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3386 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 3387 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3388 | // automatically enable the remote submix output when input is started if not |
| 3389 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 3390 | // 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] | 3391 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3392 | String8 address = String8(""); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 3393 | if (policyMix == nullptr) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3394 | address = String8("0"); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3395 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 3396 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 3397 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3398 | if (address != "") { |
| 3399 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 3400 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 3401 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3402 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 3403 | } |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 3404 | } else if (status != NO_ERROR) { |
| 3405 | // Restore client activity state. |
| 3406 | inputDesc->setClientActive(client, false); |
| 3407 | inputDesc->stop(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3408 | } |
| 3409 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 3410 | ALOGV("%s input %d source = %d status = %d exit", |
| 3411 | __FUNCTION__, input, client->source(), status); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3412 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 3413 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3414 | } |
| 3415 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3416 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3417 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3418 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 3419 | |
| 3420 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 3421 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3422 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3423 | return BAD_VALUE; |
| 3424 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3425 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3426 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3427 | if (!client->active()) { |
| 3428 | ALOGW("%s input %d client %d already stopped", __FUNCTION__, input, client->portId()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3429 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3430 | } |
Carter Hsu | e6139d5 | 2021-07-08 10:30:20 +0800 | [diff] [blame] | 3431 | auto old_source = inputDesc->source(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3432 | inputDesc->setClientActive(client, false); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 3433 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3434 | inputDesc->stop(); |
| 3435 | if (inputDesc->isActive()) { |
Carter Hsu | e6139d5 | 2021-07-08 10:30:20 +0800 | [diff] [blame] | 3436 | auto current_source = inputDesc->source(); |
| 3437 | setInputDevice(input, getNewInputDevice(inputDesc), |
| 3438 | old_source != current_source /* force */); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3439 | } else { |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3440 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3441 | // 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] | 3442 | if ((policyMix != nullptr) |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3443 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 3444 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3445 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 3446 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3447 | |
| 3448 | // automatically disable the remote submix output when input is stopped if not |
| 3449 | // used by a policy mix of type MIX_TYPE_RECORDERS |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3450 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3451 | String8 address = String8(""); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 3452 | if (policyMix == nullptr) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3453 | address = String8("0"); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3454 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 3455 | address = policyMix->mDeviceAddress; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3456 | } |
| 3457 | if (address != "") { |
| 3458 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 3459 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 3460 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3461 | } |
| 3462 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3463 | resetInputDevice(input); |
| 3464 | |
| 3465 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 3466 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3467 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 3468 | if (primaryInputDevices.contains(inputDesc->getDevice()) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3469 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 3470 | mpClientInterface->setSoundTriggerCaptureState(false); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3471 | } |
| 3472 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3473 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3474 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3475 | } |
| 3476 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3477 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3478 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3479 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 3480 | |
| 3481 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 3482 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3483 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3484 | return; |
| 3485 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3486 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3487 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 3488 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3489 | ALOGV("%s %d", __FUNCTION__, input); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 3490 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3491 | inputDesc->removeClient(portId); |
Eric Laurent | c03ada6 | 2024-03-21 14:02:22 +0000 | [diff] [blame] | 3492 | |
| 3493 | // If no more clients are present in this session, park effects to an orphan chain |
| 3494 | RecordClientVector clientsOnSession = inputDesc->getClientsForSession(client->session()); |
| 3495 | if (clientsOnSession.size() == 0) { |
| 3496 | mEffects.putOrphanEffects(client->session(), input, &mInputs, mpClientInterface); |
| 3497 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3498 | if (inputDesc->getClientCount() > 0) { |
| 3499 | ALOGV("%s(%d) %zu clients remaining", __func__, portId, inputDesc->getClientCount()); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3500 | return; |
| 3501 | } |
| 3502 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3503 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3504 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3505 | ALOGV("%s exit", __FUNCTION__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3506 | } |
| 3507 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3508 | void AudioPolicyManager::closeActiveClients(const sp<AudioInputDescriptor>& input) |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3509 | { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3510 | RecordClientVector clients = input->clientsList(true); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3511 | |
| 3512 | for (const auto& client : clients) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3513 | closeClient(client->portId()); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3514 | } |
| 3515 | } |
| 3516 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3517 | void AudioPolicyManager::closeClient(audio_port_handle_t portId) |
| 3518 | { |
| 3519 | stopInput(portId); |
| 3520 | releaseInput(portId); |
| 3521 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3522 | |
Mikhail Naganov | c66ffc1 | 2024-05-30 16:56:25 -0700 | [diff] [blame] | 3523 | bool AudioPolicyManager::checkCloseInput(const sp<AudioInputDescriptor>& input) { |
| 3524 | if (input->clientsList().size() == 0 |
| 3525 | || !mAvailableInputDevices.containsAtLeastOne(input->supportedDevices())) { |
| 3526 | return true; |
| 3527 | } |
| 3528 | for (const auto& client : input->clientsList()) { |
| 3529 | sp<DeviceDescriptor> device = |
| 3530 | mEngine->getInputDeviceForAttributes(client->attributes(), client->uid(), |
| 3531 | client->session()); |
| 3532 | if (!input->supportedDevices().contains(device)) { |
| 3533 | return true; |
| 3534 | } |
| 3535 | } |
| 3536 | setInputDevice(input->mIoHandle, getNewInputDevice(input)); |
| 3537 | return false; |
| 3538 | } |
| 3539 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3540 | void AudioPolicyManager::checkCloseInputs() { |
| 3541 | // After connecting or disconnecting an input device, close input if: |
| 3542 | // - it has no client (was just opened to check profile) OR |
| 3543 | // - none of its supported devices are connected anymore OR |
| 3544 | // - one of its clients cannot be routed to one of its supported |
| 3545 | // devices anymore. Otherwise update device selection |
| 3546 | std::vector<audio_io_handle_t> inputsToClose; |
| 3547 | for (size_t i = 0; i < mInputs.size(); i++) { |
Mikhail Naganov | c66ffc1 | 2024-05-30 16:56:25 -0700 | [diff] [blame] | 3548 | if (checkCloseInput(mInputs.valueAt(i))) { |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3549 | inputsToClose.push_back(mInputs.keyAt(i)); |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3550 | } |
| 3551 | } |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3552 | for (const audio_io_handle_t handle : inputsToClose) { |
| 3553 | ALOGV("%s closing input %d", __func__, handle); |
| 3554 | closeInput(handle); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3555 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3556 | } |
| 3557 | |
Vlad Popa | 87e0e58 | 2024-05-20 18:49:20 -0700 | [diff] [blame] | 3558 | status_t AudioPolicyManager::setDeviceAbsoluteVolumeEnabled(audio_devices_t deviceType, |
| 3559 | const char *address __unused, |
| 3560 | bool enabled, |
| 3561 | audio_stream_type_t streamToDriveAbs) |
| 3562 | { |
Vlad Popa | a536eb3 | 2024-07-18 16:00:35 -0700 | [diff] [blame] | 3563 | if (!enabled) { |
| 3564 | mAbsoluteVolumeDrivingStreams.erase(deviceType); |
| 3565 | return NO_ERROR; |
| 3566 | } |
| 3567 | |
Vlad Popa | 87e0e58 | 2024-05-20 18:49:20 -0700 | [diff] [blame] | 3568 | audio_attributes_t attributesToDriveAbs = mEngine->getAttributesForStreamType(streamToDriveAbs); |
| 3569 | if (attributesToDriveAbs == AUDIO_ATTRIBUTES_INITIALIZER) { |
| 3570 | ALOGW("%s: no attributes for stream %s, bailing out", __func__, |
| 3571 | toString(streamToDriveAbs).c_str()); |
| 3572 | return BAD_VALUE; |
| 3573 | } |
| 3574 | |
Vlad Popa | a536eb3 | 2024-07-18 16:00:35 -0700 | [diff] [blame] | 3575 | mAbsoluteVolumeDrivingStreams[deviceType] = attributesToDriveAbs; |
Vlad Popa | 87e0e58 | 2024-05-20 18:49:20 -0700 | [diff] [blame] | 3576 | return NO_ERROR; |
| 3577 | } |
| 3578 | |
François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 3579 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, int indexMin, int indexMax) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3580 | { |
| 3581 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 3582 | if (indexMin < 0 || indexMax < 0) { |
| 3583 | ALOGE("%s for stream %d: invalid min %d or max %d", __func__, stream , indexMin, indexMax); |
| 3584 | return; |
| 3585 | } |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 3586 | getVolumeCurves(stream).initVolume(indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3587 | |
| 3588 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3589 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 3590 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3591 | continue; |
| 3592 | } |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 3593 | getVolumeCurves((audio_stream_type_t)curStream).initVolume(indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3594 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3595 | } |
| 3596 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3597 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3598 | int index, |
| 3599 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3600 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3601 | auto attributes = mEngine->getAttributesForStreamType(stream); |
Eric Laurent | 242a9f8 | 2020-03-23 15:57:04 -0700 | [diff] [blame] | 3602 | if (attributes == AUDIO_ATTRIBUTES_INITIALIZER) { |
| 3603 | ALOGW("%s: no group for stream %s, bailing out", __func__, toString(stream).c_str()); |
| 3604 | return NO_ERROR; |
| 3605 | } |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 3606 | ALOGV("%s: stream %s attributes=%s, index %d , device 0x%X", __func__, |
| 3607 | toString(stream).c_str(), toString(attributes).c_str(), index, device); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3608 | return setVolumeIndexForAttributes(attributes, index, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3609 | } |
| 3610 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3611 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3612 | int *index, |
| 3613 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3614 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3615 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this |
| 3616 | // stream by the engine. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3617 | DeviceTypeSet deviceTypes = {device}; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 3618 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3619 | deviceTypes = mEngine->getOutputDevicesForStream( |
| 3620 | stream, true /*fromCache*/).types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3621 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3622 | return getVolumeIndex(getVolumeCurves(stream), *index, deviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3623 | } |
| 3624 | |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3625 | status_t AudioPolicyManager::setVolumeIndexForAttributes(const audio_attributes_t &attributes, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3626 | int index, |
| 3627 | audio_devices_t device) |
| 3628 | { |
| 3629 | // Get Volume group matching the Audio Attributes |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3630 | auto group = mEngine->getVolumeGroupForAttributes(attributes); |
| 3631 | if (group == VOLUME_GROUP_NONE) { |
| 3632 | 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] | 3633 | return BAD_VALUE; |
| 3634 | } |
Eric Laurent | ae6e88c | 2024-01-10 14:42:57 +0100 | [diff] [blame] | 3635 | ALOGV("%s: group %d matching with %s index %d", |
| 3636 | __FUNCTION__, group, toString(attributes).c_str(), index); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3637 | status_t status = NO_ERROR; |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3638 | IVolumeCurves &curves = getVolumeCurves(attributes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3639 | VolumeSource vs = toVolumeSource(group); |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3640 | // AUDIO_STREAM_BLUETOOTH_SCO is only used for volume control so we remap |
| 3641 | // to AUDIO_STREAM_VOICE_CALL to match with relevant playback activity |
| 3642 | VolumeSource activityVs = (vs == toVolumeSource(AUDIO_STREAM_BLUETOOTH_SCO, false)) ? |
| 3643 | toVolumeSource(AUDIO_STREAM_VOICE_CALL, false) : vs; |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3644 | product_strategy_t strategy = mEngine->getProductStrategyForAttributes(attributes); |
| 3645 | |
| 3646 | status = setVolumeCurveIndex(index, device, curves); |
| 3647 | if (status != NO_ERROR) { |
| 3648 | ALOGE("%s failed to set curve index for group %d device 0x%X", __func__, group, device); |
| 3649 | return status; |
| 3650 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3651 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3652 | DeviceTypeSet curSrcDevices; |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3653 | auto curCurvAttrs = curves.getAttributes(); |
| 3654 | if (!curCurvAttrs.empty() && curCurvAttrs.front() != defaultAttr) { |
| 3655 | auto attr = curCurvAttrs.front(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3656 | curSrcDevices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false).types(); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3657 | } else if (!curves.getStreamTypes().empty()) { |
| 3658 | auto stream = curves.getStreamTypes().front(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3659 | curSrcDevices = mEngine->getOutputDevicesForStream(stream, false).types(); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3660 | } else { |
| 3661 | ALOGE("%s: Invalid src %d: no valid attributes nor stream",__func__, vs); |
| 3662 | return BAD_VALUE; |
| 3663 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3664 | audio_devices_t curSrcDevice = Volume::getDeviceForVolume(curSrcDevices); |
| 3665 | resetDeviceTypes(curSrcDevices, curSrcDevice); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3666 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3667 | // update volume on all outputs and streams matching the following: |
| 3668 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 3669 | // - The device (or devices) selected by the engine for this stream includes |
| 3670 | // the requested device |
| 3671 | // - For non default requested device, currently selected device on the output is either the |
| 3672 | // requested device or one of the devices selected by the engine for this stream |
| 3673 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 3674 | // no specific device volume value exists for currently selected device. |
Henrik Backlund | 18373a3 | 2024-01-24 10:26:42 +0100 | [diff] [blame] | 3675 | // - 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] | 3676 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3677 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3678 | DeviceTypeSet curDevices = desc->devices().types(); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3679 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3680 | if (curDevices.erase(AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { |
| 3681 | curDevices.insert(AUDIO_DEVICE_OUT_SPEAKER); |
Robert Lee | 5e66e79 | 2019-04-03 18:37:15 +0800 | [diff] [blame] | 3682 | } |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3683 | |
| 3684 | if (!(desc->isActive(activityVs) || isInCallOrScreening())) { |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3685 | continue; |
| 3686 | } |
| 3687 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME && |
| 3688 | curDevices.find(device) == curDevices.end()) { |
| 3689 | continue; |
| 3690 | } |
| 3691 | bool applyVolume = false; |
| 3692 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
| 3693 | curSrcDevices.insert(device); |
| 3694 | applyVolume = (curSrcDevices.find( |
Henrik Backlund | 18373a3 | 2024-01-24 10:26:42 +0100 | [diff] [blame] | 3695 | Volume::getDeviceForVolume(curDevices)) != curSrcDevices.end()) |
| 3696 | && Volume::getDeviceForVolume(curSrcDevices) == device; |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3697 | } else { |
| 3698 | applyVolume = !curves.hasVolumeIndexForDevice(curSrcDevice); |
| 3699 | } |
| 3700 | if (!applyVolume) { |
| 3701 | continue; // next output |
| 3702 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3703 | // Inter / intra volume group priority management: Loop on strategies arranged by priority |
| 3704 | // If a higher priority strategy is active, and the output is routed to a device with a |
| 3705 | // HW Gain management, do not change the volume |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3706 | if (desc->useHwGain()) { |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3707 | applyVolume = false; |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 3708 | // If the volume source is active with higher priority source, ensure at least Sw Muted |
| 3709 | desc->setSwMute((index == 0), vs, curves.getStreamTypes(), curDevices, 0 /*delayMs*/); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3710 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { |
| 3711 | auto activeClients = desc->clientsList(true /*activeOnly*/, productStrategy, |
| 3712 | false /*preferredDevice*/); |
| 3713 | if (activeClients.empty()) { |
| 3714 | continue; |
| 3715 | } |
| 3716 | bool isPreempted = false; |
| 3717 | bool isHigherPriority = productStrategy < strategy; |
| 3718 | for (const auto &client : activeClients) { |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3719 | if (isHigherPriority && (client->volumeSource() != activityVs)) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3720 | ALOGV("%s: Strategy=%d (\nrequester:\n" |
| 3721 | " group %d, volumeGroup=%d attributes=%s)\n" |
| 3722 | " higher priority source active:\n" |
| 3723 | " volumeGroup=%d attributes=%s) \n" |
| 3724 | " on output %zu, bailing out", __func__, productStrategy, |
| 3725 | group, group, toString(attributes).c_str(), |
| 3726 | client->volumeSource(), toString(client->attributes()).c_str(), i); |
| 3727 | applyVolume = false; |
| 3728 | isPreempted = true; |
| 3729 | break; |
| 3730 | } |
| 3731 | // 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] | 3732 | if (client->volumeSource() == activityVs) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3733 | applyVolume = true; |
| 3734 | } |
| 3735 | } |
| 3736 | if (isPreempted || applyVolume) { |
| 3737 | break; |
| 3738 | } |
| 3739 | } |
| 3740 | if (!applyVolume) { |
| 3741 | continue; // next output |
| 3742 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3743 | } |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3744 | //FIXME: workaround for truncated touch sounds |
| 3745 | // delayed volume change for system stream to be removed when the problem is |
| 3746 | // handled by system UI |
| 3747 | status_t volStatus = checkAndSetVolume( |
| 3748 | curves, vs, index, desc, curDevices, |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3749 | ((vs == toVolumeSource(AUDIO_STREAM_SYSTEM, false))? |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3750 | TOUCH_SOUND_FIXED_DELAY_MS : 0)); |
| 3751 | if (volStatus != NO_ERROR) { |
| 3752 | status = volStatus; |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3753 | } |
| 3754 | } |
Eric Laurent | ae6e88c | 2024-01-10 14:42:57 +0100 | [diff] [blame] | 3755 | |
| 3756 | // update voice volume if the an active call route exists |
| 3757 | if (mCallRxSourceClient != nullptr && mCallRxSourceClient->isConnected() |
| 3758 | && (curSrcDevices.find( |
| 3759 | Volume::getDeviceForVolume({mCallRxSourceClient->sinkDevice()->type()})) |
| 3760 | != curSrcDevices.end())) { |
| 3761 | bool isVoiceVolSrc; |
| 3762 | bool isBtScoVolSrc; |
| 3763 | if (isVolumeConsistentForCalls(vs, {mCallRxSourceClient->sinkDevice()->type()}, |
| 3764 | isVoiceVolSrc, isBtScoVolSrc, __func__) |
| 3765 | && (isVoiceVolSrc || isBtScoVolSrc)) { |
chenxin20 | 9555903 | 2024-06-15 13:59:29 +0800 | [diff] [blame] | 3766 | bool voiceVolumeManagedByHost = isVoiceVolSrc && |
| 3767 | !audio_is_ble_out_device(mCallRxSourceClient->sinkDevice()->type()); |
| 3768 | setVoiceVolume(index, curves, voiceVolumeManagedByHost, 0); |
Eric Laurent | ae6e88c | 2024-01-10 14:42:57 +0100 | [diff] [blame] | 3769 | } |
| 3770 | } |
| 3771 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3772 | mpClientInterface->onAudioVolumeGroupChanged(group, 0 /*flags*/); |
| 3773 | return status; |
| 3774 | } |
| 3775 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3776 | status_t AudioPolicyManager::setVolumeCurveIndex(int index, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3777 | audio_devices_t device, |
| 3778 | IVolumeCurves &volumeCurves) |
| 3779 | { |
| 3780 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 3781 | // app that has MODIFY_PHONE_STATE permission. |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3782 | bool hasVoice = hasVoiceStream(volumeCurves.getStreamTypes()); |
| 3783 | if (((index < volumeCurves.getVolumeIndexMin()) && !(hasVoice && index == 0)) || |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3784 | (index > volumeCurves.getVolumeIndexMax())) { |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 3785 | ALOGE("%s: wrong index %d min=%d max=%d, device 0x%X", __FUNCTION__, index, |
| 3786 | volumeCurves.getVolumeIndexMin(), volumeCurves.getVolumeIndexMax(), device); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3787 | return BAD_VALUE; |
| 3788 | } |
| 3789 | if (!audio_is_output_device(device)) { |
| 3790 | return BAD_VALUE; |
| 3791 | } |
| 3792 | |
| 3793 | // Force max volume if stream cannot be muted |
| 3794 | if (!volumeCurves.canBeMuted()) index = volumeCurves.getVolumeIndexMax(); |
| 3795 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3796 | ALOGV("%s device %08x, index %d", __FUNCTION__ , device, index); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3797 | volumeCurves.addCurrentVolumeIndex(device, index); |
| 3798 | return NO_ERROR; |
| 3799 | } |
| 3800 | |
| 3801 | status_t AudioPolicyManager::getVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3802 | int &index, |
| 3803 | audio_devices_t device) |
| 3804 | { |
| 3805 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this |
| 3806 | // stream by the engine. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3807 | DeviceTypeSet deviceTypes = {device}; |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3808 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Mikhail Naganov | bb990f2 | 2022-06-15 00:46:43 +0000 | [diff] [blame] | 3809 | deviceTypes = mEngine->getOutputDevicesForAttributes( |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3810 | attr, nullptr, true /*fromCache*/).types(); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3811 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3812 | return getVolumeIndex(getVolumeCurves(attr), index, deviceTypes); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3813 | } |
| 3814 | |
| 3815 | status_t AudioPolicyManager::getVolumeIndex(const IVolumeCurves &curves, |
| 3816 | int &index, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3817 | const DeviceTypeSet& deviceTypes) const |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3818 | { |
Mikhail Naganov | bb990f2 | 2022-06-15 00:46:43 +0000 | [diff] [blame] | 3819 | if (!isSingleDeviceType(deviceTypes, audio_is_output_device)) { |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3820 | return BAD_VALUE; |
| 3821 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3822 | index = curves.getVolumeIndex(deviceTypes); |
| 3823 | 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] | 3824 | return NO_ERROR; |
| 3825 | } |
| 3826 | |
| 3827 | status_t AudioPolicyManager::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3828 | int &index) |
| 3829 | { |
| 3830 | index = getVolumeCurves(attr).getVolumeIndexMin(); |
| 3831 | return NO_ERROR; |
| 3832 | } |
| 3833 | |
| 3834 | status_t AudioPolicyManager::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3835 | int &index) |
| 3836 | { |
| 3837 | index = getVolumeCurves(attr).getVolumeIndexMax(); |
| 3838 | return NO_ERROR; |
| 3839 | } |
| 3840 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3841 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3842 | { |
| 3843 | // select one output among several suitable for global effects. |
| 3844 | // The priority is as follows: |
| 3845 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 3846 | // AudioFlinger will invalidate the track and the offloaded output |
| 3847 | // will be closed causing the effect to be moved to a PCM output. |
Shunkai Yao | 2dcd60c | 2024-08-27 21:08:53 +0000 | [diff] [blame] | 3848 | // 2: Spatializer output if the stereo spatializer feature enabled |
| 3849 | // 3: A deep buffer output |
| 3850 | // 4: The primary output |
| 3851 | // 5: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3852 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3853 | DeviceVector devices = mEngine->getOutputDevicesForAttributes( |
| 3854 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3855 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3856 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3857 | if (outputs.size() == 0) { |
| 3858 | return AUDIO_IO_HANDLE_NONE; |
| 3859 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3860 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3861 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3862 | bool activeOnly = true; |
| 3863 | |
| 3864 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 3865 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
Shunkai Yao | 2dcd60c | 2024-08-27 21:08:53 +0000 | [diff] [blame] | 3866 | audio_io_handle_t outputSpatializer = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3867 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 3868 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 3869 | |
Shunkai Yao | 2dcd60c | 2024-08-27 21:08:53 +0000 | [diff] [blame] | 3870 | for (audio_io_handle_t outputLoop : outputs) { |
| 3871 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputLoop); |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 3872 | if (activeOnly && !desc->isActive(toVolumeSource(AUDIO_STREAM_MUSIC))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3873 | continue; |
| 3874 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3875 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
Shunkai Yao | 2dcd60c | 2024-08-27 21:08:53 +0000 | [diff] [blame] | 3876 | activeOnly, outputLoop, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3877 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Shunkai Yao | 2dcd60c | 2024-08-27 21:08:53 +0000 | [diff] [blame] | 3878 | outputOffloaded = outputLoop; |
| 3879 | } |
| 3880 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) { |
| 3881 | if (SpatializerHelper::isStereoSpatializationFeatureEnabled()) { |
| 3882 | outputSpatializer = outputLoop; |
| 3883 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3884 | } |
| 3885 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Shunkai Yao | 2dcd60c | 2024-08-27 21:08:53 +0000 | [diff] [blame] | 3886 | outputDeepBuffer = outputLoop; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3887 | } |
| 3888 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Shunkai Yao | 2dcd60c | 2024-08-27 21:08:53 +0000 | [diff] [blame] | 3889 | outputPrimary = outputLoop; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3890 | } |
| 3891 | } |
| 3892 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 3893 | output = outputOffloaded; |
Shunkai Yao | 2dcd60c | 2024-08-27 21:08:53 +0000 | [diff] [blame] | 3894 | } else if (outputSpatializer != AUDIO_IO_HANDLE_NONE) { |
| 3895 | output = outputSpatializer; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3896 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 3897 | output = outputDeepBuffer; |
| 3898 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 3899 | output = outputPrimary; |
| 3900 | } else { |
| 3901 | output = outputs[0]; |
| 3902 | } |
| 3903 | activeOnly = false; |
| 3904 | } |
| 3905 | |
| 3906 | if (output != mMusicEffectOutput) { |
François Gaffie | 1b4753e | 2023-02-06 10:36:33 +0100 | [diff] [blame] | 3907 | mEffects.moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output, |
| 3908 | mpClientInterface); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3909 | mMusicEffectOutput = output; |
| 3910 | } |
| 3911 | |
| 3912 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3913 | return output; |
| 3914 | } |
| 3915 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3916 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 3917 | { |
| 3918 | return selectOutputForMusicEffects(); |
| 3919 | } |
| 3920 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3921 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3922 | audio_io_handle_t io, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 3923 | product_strategy_t strategy, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3924 | int session, |
| 3925 | int id) |
| 3926 | { |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 3927 | if (session != AUDIO_SESSION_DEVICE && io != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3928 | ssize_t index = mOutputs.indexOfKey(io); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3929 | if (index < 0) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3930 | index = mInputs.indexOfKey(io); |
| 3931 | if (index < 0) { |
| 3932 | ALOGW("registerEffect() unknown io %d", io); |
| 3933 | return INVALID_OPERATION; |
| 3934 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3935 | } |
| 3936 | } |
Eric Laurent | bf8f69f | 2022-03-25 17:48:38 +0100 | [diff] [blame] | 3937 | bool isMusicEffect = (session != AUDIO_SESSION_OUTPUT_STAGE) |
| 3938 | && ((strategy == streamToStrategy(AUDIO_STREAM_MUSIC) |
| 3939 | || strategy == PRODUCT_STRATEGY_NONE)); |
| 3940 | return mEffects.registerEffect(desc, io, session, id, isMusicEffect); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3941 | } |
| 3942 | |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 3943 | status_t AudioPolicyManager::unregisterEffect(int id) |
| 3944 | { |
| 3945 | if (mEffects.getEffect(id) == nullptr) { |
| 3946 | return INVALID_OPERATION; |
| 3947 | } |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 3948 | if (mEffects.isEffectEnabled(id)) { |
| 3949 | ALOGW("%s effect %d enabled", __FUNCTION__, id); |
| 3950 | setEffectEnabled(id, false); |
| 3951 | } |
| 3952 | return mEffects.unregisterEffect(id); |
| 3953 | } |
| 3954 | |
| 3955 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) |
| 3956 | { |
| 3957 | sp<EffectDescriptor> effect = mEffects.getEffect(id); |
| 3958 | if (effect == nullptr) { |
| 3959 | return INVALID_OPERATION; |
| 3960 | } |
| 3961 | |
| 3962 | status_t status = mEffects.setEffectEnabled(id, enabled); |
| 3963 | if (status == NO_ERROR) { |
| 3964 | mInputs.trackEffectEnabled(effect, enabled); |
| 3965 | } |
| 3966 | return status; |
| 3967 | } |
| 3968 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3969 | |
| 3970 | status_t AudioPolicyManager::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) |
| 3971 | { |
| 3972 | mEffects.moveEffects(ids, io); |
| 3973 | return NO_ERROR; |
| 3974 | } |
| 3975 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3976 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 3977 | { |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3978 | auto vs = toVolumeSource(stream, false); |
| 3979 | return vs != VOLUME_SOURCE_NONE ? mOutputs.isActive(vs, inPastMs) : false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3980 | } |
| 3981 | |
| 3982 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 3983 | { |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3984 | auto vs = toVolumeSource(stream, false); |
| 3985 | return vs != VOLUME_SOURCE_NONE ? mOutputs.isActiveRemotely(vs, inPastMs) : false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3986 | } |
| 3987 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3988 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3989 | { |
| 3990 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3991 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3992 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3993 | return true; |
| 3994 | } |
| 3995 | } |
| 3996 | return false; |
| 3997 | } |
| 3998 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3999 | // Register a list of custom mixes with their attributes and format. |
| 4000 | // When a mix is registered, corresponding input and output profiles are |
| 4001 | // added to the remote submix hw module. The profile contains only the |
| 4002 | // parameters (sampling rate, format...) specified by the mix. |
| 4003 | // The corresponding input remote submix device is also connected. |
| 4004 | // |
| 4005 | // When a remote submix device is connected, the address is checked to select the |
| 4006 | // appropriate profile and the corresponding input or output stream is opened. |
| 4007 | // |
| 4008 | // When capture starts, getInputForAttr() will: |
| 4009 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 4010 | // - 2 if none found, getDeviceForInputSource() will: |
| 4011 | // - 2.1 look for a mix matching the attributes source |
| 4012 | // - 2.2 if none found, default to device selection by policy rules |
| 4013 | // At this time, the corresponding output remote submix device is also connected |
| 4014 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 4015 | // after AudioTracks are invalidated |
| 4016 | // |
| 4017 | // When playback starts, getOutputForAttr() will: |
| 4018 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 4019 | // - 2 if none found, look for a mix matching the attributes usage |
| 4020 | // - 3 if none found, default to device and output selection by policy rules. |
| 4021 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4022 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4023 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4024 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 4025 | status_t res = NO_ERROR; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 4026 | bool checkOutputs = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4027 | sp<HwModule> rSubmixModule; |
Marvin Ramin | abd9b89 | 2023-11-17 16:36:27 +0100 | [diff] [blame] | 4028 | Vector<AudioMix> registeredMixes; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4029 | // examine each mix's route type |
| 4030 | for (size_t i = 0; i < mixes.size(); i++) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4031 | AudioMix mix = mixes[i]; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 4032 | // Only capture of playback is allowed in LOOP_BACK & RENDER mode |
| 4033 | if (is_mix_loopback_render(mix.mRouteFlags) && mix.mMixType != MIX_TYPE_PLAYERS) { |
| 4034 | ALOGE("Unsupported Policy Mix %zu of %zu: " |
| 4035 | "Only capture of playback is allowed in LOOP_BACK & RENDER mode", |
| 4036 | i, mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4037 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4038 | break; |
| 4039 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 4040 | // LOOP_BACK and LOOP_BACK | RENDER have the same remote submix backend and are handled |
| 4041 | // in the same way. |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4042 | 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] | 4043 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK %d", i, mixes.size(), |
| 4044 | mix.mRouteFlags); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4045 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4046 | rSubmixModule = mHwModules.getModuleFromName( |
| 4047 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 4048 | if (rSubmixModule == 0) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 4049 | ALOGE("Unable to find audio module for submix, aborting mix %zu registration", |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4050 | i); |
| 4051 | res = INVALID_OPERATION; |
| 4052 | break; |
| 4053 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4054 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4055 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4056 | String8 address = mix.mDeviceAddress; |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 4057 | audio_devices_t deviceTypeToMakeAvailable; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4058 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4059 | mix.mDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 4060 | deviceTypeToMakeAvailable = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 4061 | } else { |
| 4062 | mix.mDeviceType = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 4063 | deviceTypeToMakeAvailable = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4064 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4065 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 4066 | if (mPolicyMixes.registerMix(mix, 0 /*output desc*/) != NO_ERROR) { |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 4067 | 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] | 4068 | res = INVALID_OPERATION; |
| 4069 | break; |
| 4070 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4071 | audio_config_t outputConfig = mix.mFormat; |
| 4072 | audio_config_t inputConfig = mix.mFormat; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4073 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL |
| 4074 | // 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] | 4075 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 4076 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 4077 | rSubmixModule->addOutputProfile(address.c_str(), &outputConfig, |
Dean Wheatley | 8055186 | 2023-11-17 01:32:22 +1100 | [diff] [blame] | 4078 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address, |
| 4079 | audio_is_linear_pcm(outputConfig.format) |
| 4080 | ? AUDIO_OUTPUT_FLAG_NONE : AUDIO_OUTPUT_FLAG_DIRECT); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 4081 | rSubmixModule->addInputProfile(address.c_str(), &inputConfig, |
Dean Wheatley | 8055186 | 2023-11-17 01:32:22 +1100 | [diff] [blame] | 4082 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address, |
| 4083 | audio_is_linear_pcm(inputConfig.format) |
| 4084 | ? AUDIO_INPUT_FLAG_NONE : AUDIO_INPUT_FLAG_DIRECT); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4085 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 4086 | if ((res = setDeviceConnectionStateInt(deviceTypeToMakeAvailable, |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 4087 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 4088 | address.c_str(), "remote-submix", AUDIO_FORMAT_DEFAULT)) != NO_ERROR) { |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 4089 | ALOGE("Failed to set remote submix device available, type %u, address %s", |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 4090 | mix.mDeviceType, address.c_str()); |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 4091 | break; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4092 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4093 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 4094 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 4095 | audio_devices_t type = mix.mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 4096 | 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] | 4097 | i, mixes.size(), type, address.c_str()); |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 4098 | |
| 4099 | sp<DeviceDescriptor> device = mHwModules.getDeviceDescriptor( |
| 4100 | mix.mDeviceType, mix.mDeviceAddress, |
| 4101 | String8(), AUDIO_FORMAT_DEFAULT); |
| 4102 | if (device == nullptr) { |
| 4103 | res = INVALID_OPERATION; |
| 4104 | break; |
| 4105 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4106 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 4107 | bool foundOutput = false; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4108 | // First try to find an already opened output supporting the device |
| 4109 | 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] | 4110 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 4111 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4112 | if (!desc->isDuplicated() && desc->supportedDevices().contains(device)) { |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 4113 | if (mPolicyMixes.registerMix(mix, desc) != NO_ERROR) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 4114 | ALOGE("Could not register mix RENDER, dev=0x%X addr=%s", type, |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 4115 | address.c_str()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4116 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 4117 | } else { |
| 4118 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4119 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4120 | } |
| 4121 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4122 | // If no output found, try to find a direct output profile supporting the device |
| 4123 | for (size_t i = 0; i < mHwModules.size() && !foundOutput && res == NO_ERROR; i++) { |
| 4124 | sp<HwModule> module = mHwModules[i]; |
| 4125 | for (size_t j = 0; |
| 4126 | j < module->getOutputProfiles().size() && !foundOutput && res == NO_ERROR; |
| 4127 | j++) { |
| 4128 | sp<IOProfile> profile = module->getOutputProfiles()[j]; |
| 4129 | if (profile->isDirectOutput() && profile->supportsDevice(device)) { |
| 4130 | if (mPolicyMixes.registerMix(mix, nullptr) != NO_ERROR) { |
| 4131 | ALOGE("Could not register mix RENDER, dev=0x%X addr=%s", type, |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 4132 | address.c_str()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4133 | res = INVALID_OPERATION; |
| 4134 | } else { |
| 4135 | foundOutput = true; |
| 4136 | } |
| 4137 | } |
| 4138 | } |
| 4139 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4140 | if (res != NO_ERROR) { |
| 4141 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 4142 | i, type, address.c_str()); |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 4143 | res = INVALID_OPERATION; |
| 4144 | break; |
| 4145 | } else if (!foundOutput) { |
| 4146 | 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] | 4147 | i, type, address.c_str()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4148 | res = INVALID_OPERATION; |
| 4149 | break; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 4150 | } else { |
| 4151 | checkOutputs = true; |
Marvin Ramin | abd9b89 | 2023-11-17 16:36:27 +0100 | [diff] [blame] | 4152 | registeredMixes.add(mix); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4153 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4154 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4155 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4156 | if (res != NO_ERROR) { |
Marvin Ramin | abd9b89 | 2023-11-17 16:36:27 +0100 | [diff] [blame] | 4157 | if (audio_flags::audio_mix_ownership()) { |
| 4158 | // Only unregister mixes that were actually registered to not accidentally unregister |
| 4159 | // mixes that already existed previously. |
| 4160 | unregisterPolicyMixes(registeredMixes); |
| 4161 | registeredMixes.clear(); |
| 4162 | } else { |
| 4163 | unregisterPolicyMixes(mixes); |
| 4164 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 4165 | } else if (checkOutputs) { |
| 4166 | checkForDeviceAndOutputChanges(); |
| 4167 | updateCallAndOutputRouting(); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4168 | } |
| 4169 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 4173 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 4174 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4175 | status_t res = NO_ERROR; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 4176 | bool checkOutputs = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4177 | sp<HwModule> rSubmixModule; |
| 4178 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4179 | for (const auto& mix : mixes) { |
| 4180 | 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] | 4181 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4182 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4183 | rSubmixModule = mHwModules.getModuleFromName( |
| 4184 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 4185 | if (rSubmixModule == 0) { |
| 4186 | res = INVALID_OPERATION; |
| 4187 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4188 | } |
| 4189 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4190 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4191 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4192 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 4193 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4194 | res = INVALID_OPERATION; |
| 4195 | continue; |
| 4196 | } |
| 4197 | |
Marvin Ramin | 0783e20 | 2024-03-05 12:45:50 +0100 | [diff] [blame] | 4198 | 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] | 4199 | if (getDeviceConnectionState(device, address.c_str()) == |
Marvin Ramin | 0783e20 | 2024-03-05 12:45:50 +0100 | [diff] [blame] | 4200 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 4201 | status_t currentRes = |
| 4202 | setDeviceConnectionStateInt(device, |
| 4203 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 4204 | address.c_str(), |
| 4205 | "remote-submix", |
| 4206 | AUDIO_FORMAT_DEFAULT); |
| 4207 | if (!audio_flags::audio_mix_ownership()) { |
| 4208 | res = currentRes; |
| 4209 | } |
| 4210 | if (currentRes != OK) { |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 4211 | ALOGE("Error making RemoteSubmix device unavailable for mix " |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 4212 | "with type %d, address %s", device, address.c_str()); |
Marvin Ramin | 0783e20 | 2024-03-05 12:45:50 +0100 | [diff] [blame] | 4213 | res = INVALID_OPERATION; |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 4214 | } |
| 4215 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4216 | } |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 4217 | rSubmixModule->removeOutputProfile(address.c_str()); |
| 4218 | rSubmixModule->removeInputProfile(address.c_str()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4219 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 4220 | } 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] | 4221 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4222 | res = INVALID_OPERATION; |
| 4223 | continue; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 4224 | } else { |
| 4225 | checkOutputs = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4226 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4227 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4228 | } |
Marvin Ramin | 0783e20 | 2024-03-05 12:45:50 +0100 | [diff] [blame] | 4229 | |
| 4230 | if (res == NO_ERROR && checkOutputs) { |
| 4231 | checkForDeviceAndOutputChanges(); |
| 4232 | updateCallAndOutputRouting(); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 4233 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 4234 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4235 | } |
| 4236 | |
Marvin Ramin | bdefaf0 | 2023-11-01 09:10:32 +0100 | [diff] [blame] | 4237 | status_t AudioPolicyManager::getRegisteredPolicyMixes(std::vector<AudioMix>& _aidl_return) { |
| 4238 | if (!audio_flags::audio_mix_test_api()) { |
| 4239 | return INVALID_OPERATION; |
| 4240 | } |
| 4241 | |
| 4242 | _aidl_return.clear(); |
| 4243 | _aidl_return.reserve(mPolicyMixes.size()); |
| 4244 | for (const auto &policyMix: mPolicyMixes) { |
| 4245 | _aidl_return.emplace_back(policyMix->mCriteria, policyMix->mMixType, |
| 4246 | policyMix->mFormat, policyMix->mRouteFlags, policyMix->mDeviceAddress, |
| 4247 | policyMix->mCbFlags); |
| 4248 | _aidl_return.back().mDeviceType = policyMix->mDeviceType; |
Marvin Ramin | abd9b89 | 2023-11-17 16:36:27 +0100 | [diff] [blame] | 4249 | _aidl_return.back().mToken = policyMix->mToken; |
Marvin Ramin | e5a122d | 2023-12-07 13:57:59 +0100 | [diff] [blame] | 4250 | _aidl_return.back().mVirtualDeviceId = policyMix->mVirtualDeviceId; |
Marvin Ramin | bdefaf0 | 2023-11-01 09:10:32 +0100 | [diff] [blame] | 4251 | } |
| 4252 | |
Vlad Popa | a5d73f3 | 2024-03-08 16:05:38 -0800 | [diff] [blame] | 4253 | ALOGVV("%s() returning %zu registered mixes", __func__, _aidl_return.size()); |
Marvin Ramin | bdefaf0 | 2023-11-01 09:10:32 +0100 | [diff] [blame] | 4254 | return OK; |
| 4255 | } |
| 4256 | |
Jan Sebechlebsky | 0af8e87 | 2023-08-11 14:45:08 +0200 | [diff] [blame] | 4257 | status_t AudioPolicyManager::updatePolicyMix( |
| 4258 | const AudioMix& mix, |
| 4259 | const std::vector<AudioMixMatchCriterion>& updatedCriteria) { |
| 4260 | status_t res = mPolicyMixes.updateMix(mix, updatedCriteria); |
| 4261 | if (res == NO_ERROR) { |
| 4262 | checkForDeviceAndOutputChanges(); |
| 4263 | updateCallAndOutputRouting(); |
| 4264 | } |
| 4265 | return res; |
| 4266 | } |
| 4267 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4268 | void AudioPolicyManager::dumpManualSurroundFormats(String8 *dst) const |
| 4269 | { |
| 4270 | size_t i = 0; |
| 4271 | constexpr size_t audioFormatPrefixLen = sizeof("AUDIO_FORMAT_"); |
| 4272 | for (const auto& fmt : mManualSurroundFormats) { |
| 4273 | if (i++ != 0) dst->append(", "); |
| 4274 | std::string sfmt; |
| 4275 | FormatConverter::toString(fmt, sfmt); |
| 4276 | dst->append(sfmt.size() >= audioFormatPrefixLen ? |
| 4277 | sfmt.c_str() + audioFormatPrefixLen - 1 : sfmt.c_str()); |
| 4278 | } |
| 4279 | } |
| 4280 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4281 | // Returns true if all devices types match the predicate and are supported by one HW module |
| 4282 | bool AudioPolicyManager::areAllDevicesSupported( |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 4283 | const AudioDeviceTypeAddrVector& devices, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4284 | std::function<bool(audio_devices_t)> predicate, |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 4285 | const char *context, |
| 4286 | bool matchAddress) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4287 | for (size_t i = 0; i < devices.size(); i++) { |
| 4288 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor( |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 4289 | devices[i].mType, devices[i].getAddress(), String8(), |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 4290 | AUDIO_FORMAT_DEFAULT, false /*allowToCreate*/, matchAddress); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4291 | if (devDesc == nullptr || (predicate != nullptr && !predicate(devices[i].mType))) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4292 | ALOGE("%s: device type %#x address %s not supported or not match predicate", |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 4293 | context, devices[i].mType, devices[i].getAddress()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4294 | return false; |
| 4295 | } |
| 4296 | } |
| 4297 | return true; |
| 4298 | } |
| 4299 | |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 4300 | void AudioPolicyManager::changeOutputDevicesMuteState( |
| 4301 | const AudioDeviceTypeAddrVector& devices) { |
| 4302 | ALOGVV("%s() num devices %zu", __func__, devices.size()); |
| 4303 | |
| 4304 | std::vector<sp<SwAudioOutputDescriptor>> outputs = |
| 4305 | getSoftwareOutputsForDevices(devices); |
| 4306 | |
| 4307 | for (size_t i = 0; i < outputs.size(); i++) { |
| 4308 | sp<SwAudioOutputDescriptor> outputDesc = outputs[i]; |
| 4309 | DeviceVector prevDevices = outputDesc->devices(); |
| 4310 | checkDeviceMuteStrategies(outputDesc, prevDevices, 0 /* delayMs */); |
| 4311 | } |
| 4312 | } |
| 4313 | |
| 4314 | std::vector<sp<SwAudioOutputDescriptor>> AudioPolicyManager::getSoftwareOutputsForDevices( |
| 4315 | const AudioDeviceTypeAddrVector& devices) const |
| 4316 | { |
| 4317 | std::vector<sp<SwAudioOutputDescriptor>> outputs; |
| 4318 | DeviceVector deviceDescriptors; |
| 4319 | for (size_t j = 0; j < devices.size(); j++) { |
| 4320 | sp<DeviceDescriptor> desc = mHwModules.getDeviceDescriptor( |
| 4321 | devices[j].mType, devices[j].getAddress(), String8(), AUDIO_FORMAT_DEFAULT); |
| 4322 | if (desc == nullptr || !audio_is_output_device(devices[j].mType)) { |
| 4323 | ALOGE("%s: device type %#x address %s not supported or not an output device", |
| 4324 | __func__, devices[j].mType, devices[j].getAddress()); |
| 4325 | continue; |
| 4326 | } |
| 4327 | deviceDescriptors.add(desc); |
| 4328 | } |
| 4329 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4330 | if (!mOutputs.valueAt(i)->supportsAtLeastOne(deviceDescriptors)) { |
| 4331 | continue; |
| 4332 | } |
| 4333 | outputs.push_back(mOutputs.valueAt(i)); |
| 4334 | } |
| 4335 | return outputs; |
| 4336 | } |
| 4337 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 4338 | status_t AudioPolicyManager::setUidDeviceAffinities(uid_t uid, |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 4339 | const AudioDeviceTypeAddrVector& devices) { |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 4340 | ALOGV("%s() uid=%d num devices %zu", __FUNCTION__, uid, devices.size()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4341 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
| 4342 | return BAD_VALUE; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 4343 | } |
| 4344 | status_t res = mPolicyMixes.setUidDeviceAffinities(uid, devices); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4345 | if (res != NO_ERROR) { |
| 4346 | ALOGE("%s() Could not set all device affinities for uid = %d", __FUNCTION__, uid); |
| 4347 | return res; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 4348 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4349 | |
| 4350 | checkForDeviceAndOutputChanges(); |
| 4351 | updateCallAndOutputRouting(); |
| 4352 | |
| 4353 | return NO_ERROR; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 4354 | } |
| 4355 | |
| 4356 | status_t AudioPolicyManager::removeUidDeviceAffinities(uid_t uid) { |
| 4357 | ALOGV("%s() uid=%d", __FUNCTION__, uid); |
Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 4358 | status_t res = mPolicyMixes.removeUidDeviceAffinities(uid); |
| 4359 | if (res != NO_ERROR) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4360 | ALOGE("%s() Could not remove all device affinities for uid = %d", |
Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 4361 | __FUNCTION__, uid); |
| 4362 | return INVALID_OPERATION; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 4363 | } |
| 4364 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4365 | checkForDeviceAndOutputChanges(); |
| 4366 | updateCallAndOutputRouting(); |
| 4367 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 4368 | return res; |
| 4369 | } |
| 4370 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4371 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 4372 | status_t AudioPolicyManager::setDevicesRoleForStrategy(product_strategy_t strategy, |
| 4373 | device_role_t role, |
| 4374 | const AudioDeviceTypeAddrVector &devices) { |
| 4375 | ALOGV("%s() strategy=%d role=%d %s", __func__, strategy, role, |
| 4376 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4377 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4378 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4379 | return BAD_VALUE; |
| 4380 | } |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 4381 | status_t status = mEngine->setDevicesRoleForStrategy(strategy, role, devices); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4382 | if (status != NO_ERROR) { |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 4383 | ALOGW("Engine could not set preferred devices %s for strategy %d role %d", |
| 4384 | dumpAudioDeviceTypeAddrVector(devices).c_str(), strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4385 | return status; |
| 4386 | } |
| 4387 | |
| 4388 | checkForDeviceAndOutputChanges(); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4389 | |
| 4390 | bool forceVolumeReeval = false; |
| 4391 | // FIXME: workaround for truncated touch sounds |
| 4392 | // to be removed when the problem is handled by system UI |
| 4393 | uint32_t delayMs = 0; |
| 4394 | if (strategy == mCommunnicationStrategy) { |
| 4395 | forceVolumeReeval = true; |
| 4396 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 4397 | updateInputRouting(); |
| 4398 | } |
| 4399 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4400 | |
| 4401 | return NO_ERROR; |
| 4402 | } |
| 4403 | |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 4404 | void AudioPolicyManager::updateCallAndOutputRouting(bool forceVolumeReeval, uint32_t delayMs, |
| 4405 | bool skipDelays) |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4406 | { |
| 4407 | uint32_t waitMs = 0; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 4408 | bool wasLeUnicastActive = isLeUnicastActive(); |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 4409 | if (updateCallRouting(true /*fromCache*/, delayMs, &waitMs) == NO_ERROR) { |
Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 4410 | // Only apply special touch sound delay once |
| 4411 | delayMs = 0; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4412 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 4413 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4414 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4415 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 4416 | DeviceVector newDevices = getNewOutputDevices(outputDesc, true /*fromCache*/); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 4417 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || |
| 4418 | (outputDesc != mPrimaryOutput && !isTelephonyRxOrTx(outputDesc))) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4419 | // As done in setDeviceConnectionState, we could also fix default device issue by |
| 4420 | // preventing the force re-routing in case of default dev that distinguishes on address. |
| 4421 | // Let's give back to engine full device choice decision however. |
jiabin | 2361ed8 | 2024-09-20 17:36:31 +0000 | [diff] [blame] | 4422 | bool newDevicesNotEmpty = !newDevices.isEmpty(); |
| 4423 | if (outputDesc->mPreferredAttrInfo != nullptr && newDevices != outputDesc->devices() |
| 4424 | && newDevicesNotEmpty) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 4425 | // If the device is using preferred mixer attributes, the output need to reopen |
| 4426 | // with default configuration when the new selected devices are different from |
| 4427 | // current routing devices. |
| 4428 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices); |
| 4429 | continue; |
| 4430 | } |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 4431 | |
jiabin | 2361ed8 | 2024-09-20 17:36:31 +0000 | [diff] [blame] | 4432 | waitMs = setOutputDevices(__func__, outputDesc, newDevices, |
| 4433 | newDevicesNotEmpty /*force*/, delayMs, |
| 4434 | nullptr /*patchHandle*/, !skipDelays /*requiresMuteCheck*/, |
| 4435 | !newDevicesNotEmpty /*requiresVolumeCheck*/, skipDelays); |
Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 4436 | // Only apply special touch sound delay once |
| 4437 | delayMs = 0; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4438 | } |
| 4439 | if (forceVolumeReeval && !newDevices.isEmpty()) { |
| 4440 | applyStreamVolumes(outputDesc, newDevices.types(), waitMs, true); |
| 4441 | } |
| 4442 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 4443 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 4444 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4445 | } |
| 4446 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4447 | void AudioPolicyManager::updateInputRouting() { |
| 4448 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Jaideep Sharma | 408349a | 2020-11-27 14:47:17 +0530 | [diff] [blame] | 4449 | // Skip for hotword recording as the input device switch |
| 4450 | // is handled within sound trigger HAL |
| 4451 | if (activeDesc->isSoundTrigger() && activeDesc->source() == AUDIO_SOURCE_HOTWORD) { |
| 4452 | continue; |
| 4453 | } |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4454 | auto newDevice = getNewInputDevice(activeDesc); |
| 4455 | // Force new input selection if the new device can not be reached via current input |
| 4456 | if (activeDesc->mProfile->getSupportedDevices().contains(newDevice)) { |
| 4457 | setInputDevice(activeDesc->mIoHandle, newDevice); |
| 4458 | } else { |
| 4459 | closeInput(activeDesc->mIoHandle); |
| 4460 | } |
| 4461 | } |
| 4462 | } |
| 4463 | |
Paul Wang | 5d7cdb5 | 2022-11-22 09:45:06 +0000 | [diff] [blame] | 4464 | status_t |
| 4465 | AudioPolicyManager::removeDevicesRoleForStrategy(product_strategy_t strategy, |
| 4466 | device_role_t role, |
| 4467 | const AudioDeviceTypeAddrVector &devices) { |
| 4468 | ALOGV("%s() strategy=%d role=%d %s", __func__, strategy, role, |
| 4469 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 4470 | |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 4471 | if (!areAllDevicesSupported( |
| 4472 | devices, audio_is_output_device, __func__, /*matchAddress*/false)) { |
Paul Wang | 5d7cdb5 | 2022-11-22 09:45:06 +0000 | [diff] [blame] | 4473 | return BAD_VALUE; |
| 4474 | } |
| 4475 | status_t status = mEngine->removeDevicesRoleForStrategy(strategy, role, devices); |
| 4476 | if (status != NO_ERROR) { |
| 4477 | ALOGW("Engine could not remove devices %s for strategy %d role %d", |
| 4478 | dumpAudioDeviceTypeAddrVector(devices).c_str(), strategy, role); |
| 4479 | return status; |
| 4480 | } |
| 4481 | |
| 4482 | checkForDeviceAndOutputChanges(); |
| 4483 | |
| 4484 | bool forceVolumeReeval = false; |
| 4485 | // TODO(b/263479999): workaround for truncated touch sounds |
| 4486 | // to be removed when the problem is handled by system UI |
| 4487 | uint32_t delayMs = 0; |
| 4488 | if (strategy == mCommunnicationStrategy) { |
| 4489 | forceVolumeReeval = true; |
| 4490 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 4491 | updateInputRouting(); |
| 4492 | } |
| 4493 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
| 4494 | |
| 4495 | return NO_ERROR; |
| 4496 | } |
| 4497 | |
| 4498 | status_t AudioPolicyManager::clearDevicesRoleForStrategy(product_strategy_t strategy, |
| 4499 | device_role_t role) |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4500 | { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 4501 | ALOGV("%s() strategy=%d role=%d", __func__, strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4502 | |
Paul Wang | 5d7cdb5 | 2022-11-22 09:45:06 +0000 | [diff] [blame] | 4503 | status_t status = mEngine->clearDevicesRoleForStrategy(strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4504 | if (status != NO_ERROR) { |
Eric Laurent | 9ae44f3 | 2022-12-14 16:17:02 +0100 | [diff] [blame] | 4505 | ALOGW_IF(status != NAME_NOT_FOUND, |
| 4506 | "Engine could not remove device role for strategy %d status %d", |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4507 | strategy, status); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4508 | return status; |
| 4509 | } |
| 4510 | |
| 4511 | checkForDeviceAndOutputChanges(); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4512 | |
| 4513 | bool forceVolumeReeval = false; |
| 4514 | // FIXME: workaround for truncated touch sounds |
| 4515 | // to be removed when the problem is handled by system UI |
| 4516 | uint32_t delayMs = 0; |
| 4517 | if (strategy == mCommunnicationStrategy) { |
| 4518 | forceVolumeReeval = true; |
| 4519 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 4520 | updateInputRouting(); |
| 4521 | } |
| 4522 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4523 | |
| 4524 | return NO_ERROR; |
| 4525 | } |
| 4526 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 4527 | status_t AudioPolicyManager::getDevicesForRoleAndStrategy(product_strategy_t strategy, |
| 4528 | device_role_t role, |
| 4529 | AudioDeviceTypeAddrVector &devices) { |
| 4530 | return mEngine->getDevicesForRoleAndStrategy(strategy, role, devices); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 4531 | } |
| 4532 | |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4533 | status_t AudioPolicyManager::setDevicesRoleForCapturePreset( |
| 4534 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { |
| 4535 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, |
| 4536 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 4537 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 4538 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4539 | return BAD_VALUE; |
| 4540 | } |
| 4541 | status_t status = mEngine->setDevicesRoleForCapturePreset(audioSource, role, devices); |
| 4542 | ALOGW_IF(status != NO_ERROR, |
| 4543 | "Engine could not set preferred devices %s for audio source %d role %d", |
| 4544 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); |
| 4545 | |
Wenyu Zhang | 47655d2 | 2024-10-01 12:24:53 +0000 | [diff] [blame] | 4546 | if (status == NO_ERROR) { |
| 4547 | updateInputRouting(); |
| 4548 | } |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4549 | return status; |
| 4550 | } |
| 4551 | |
| 4552 | status_t AudioPolicyManager::addDevicesRoleForCapturePreset( |
| 4553 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { |
| 4554 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, |
| 4555 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 4556 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 4557 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4558 | return BAD_VALUE; |
| 4559 | } |
| 4560 | status_t status = mEngine->addDevicesRoleForCapturePreset(audioSource, role, devices); |
| 4561 | ALOGW_IF(status != NO_ERROR, |
| 4562 | "Engine could not add preferred devices %s for audio source %d role %d", |
| 4563 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); |
| 4564 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4565 | updateInputRouting(); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4566 | return status; |
| 4567 | } |
| 4568 | |
| 4569 | status_t AudioPolicyManager::removeDevicesRoleForCapturePreset( |
| 4570 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) |
| 4571 | { |
| 4572 | ALOGV("%s() audioSource=%d role=%d devices=%s", __func__, audioSource, role, |
| 4573 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 4574 | |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 4575 | if (!areAllDevicesSupported( |
| 4576 | devices, audio_call_is_input_device, __func__, /*matchAddress*/false)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4577 | return BAD_VALUE; |
| 4578 | } |
| 4579 | |
| 4580 | status_t status = mEngine->removeDevicesRoleForCapturePreset( |
| 4581 | audioSource, role, devices); |
Eric Laurent | 9ae44f3 | 2022-12-14 16:17:02 +0100 | [diff] [blame] | 4582 | ALOGW_IF(status != NO_ERROR && status != NAME_NOT_FOUND, |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4583 | "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] | 4584 | if (status == NO_ERROR) { |
| 4585 | updateInputRouting(); |
| 4586 | } |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4587 | return status; |
| 4588 | } |
| 4589 | |
| 4590 | status_t AudioPolicyManager::clearDevicesRoleForCapturePreset(audio_source_t audioSource, |
| 4591 | device_role_t role) { |
| 4592 | ALOGV("%s() audioSource=%d role=%d", __func__, audioSource, role); |
| 4593 | |
| 4594 | status_t status = mEngine->clearDevicesRoleForCapturePreset(audioSource, role); |
Eric Laurent | 9ae44f3 | 2022-12-14 16:17:02 +0100 | [diff] [blame] | 4595 | ALOGW_IF(status != NO_ERROR && status != NAME_NOT_FOUND, |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4596 | "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] | 4597 | if (status == NO_ERROR) { |
| 4598 | updateInputRouting(); |
| 4599 | } |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4600 | return status; |
| 4601 | } |
| 4602 | |
| 4603 | status_t AudioPolicyManager::getDevicesForRoleAndCapturePreset( |
| 4604 | audio_source_t audioSource, device_role_t role, AudioDeviceTypeAddrVector &devices) { |
| 4605 | return mEngine->getDevicesForRoleAndCapturePreset(audioSource, role, devices); |
| 4606 | } |
| 4607 | |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4608 | status_t AudioPolicyManager::setUserIdDeviceAffinities(int userId, |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 4609 | const AudioDeviceTypeAddrVector& devices) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 4610 | ALOGV("%s() userId=%d num devices %zu", __func__, userId, devices.size()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4611 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
| 4612 | return BAD_VALUE; |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4613 | } |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4614 | status_t status = mPolicyMixes.setUserIdDeviceAffinities(userId, devices); |
| 4615 | if (status != NO_ERROR) { |
| 4616 | ALOGE("%s() could not set device affinity for userId %d", |
| 4617 | __FUNCTION__, userId); |
| 4618 | return status; |
| 4619 | } |
| 4620 | |
| 4621 | // reevaluate outputs for all devices |
| 4622 | checkForDeviceAndOutputChanges(); |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 4623 | changeOutputDevicesMuteState(devices); |
| 4624 | updateCallAndOutputRouting(false /* forceVolumeReeval */, 0 /* delayMs */, |
| 4625 | true /* skipDelays */); |
| 4626 | changeOutputDevicesMuteState(devices); |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4627 | |
| 4628 | return NO_ERROR; |
| 4629 | } |
| 4630 | |
| 4631 | status_t AudioPolicyManager::removeUserIdDeviceAffinities(int userId) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 4632 | ALOGV("%s() userId=%d", __FUNCTION__, userId); |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 4633 | AudioDeviceTypeAddrVector devices; |
| 4634 | mPolicyMixes.getDevicesForUserId(userId, devices); |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4635 | status_t status = mPolicyMixes.removeUserIdDeviceAffinities(userId); |
| 4636 | if (status != NO_ERROR) { |
| 4637 | ALOGE("%s() Could not remove all device affinities fo userId = %d", |
| 4638 | __FUNCTION__, userId); |
| 4639 | return status; |
| 4640 | } |
| 4641 | |
| 4642 | // reevaluate outputs for all devices |
| 4643 | checkForDeviceAndOutputChanges(); |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 4644 | changeOutputDevicesMuteState(devices); |
| 4645 | updateCallAndOutputRouting(false /* forceVolumeReeval */, 0 /* delayMs */, |
| 4646 | true /* skipDelays */); |
| 4647 | changeOutputDevicesMuteState(devices); |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4648 | |
| 4649 | return NO_ERROR; |
| 4650 | } |
| 4651 | |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4652 | void AudioPolicyManager::dump(String8 *dst) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4653 | { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4654 | dst->appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
Mikhail Naganov | 0dbe87b | 2021-12-01 02:03:31 +0000 | [diff] [blame] | 4655 | dst->appendFormat(" Primary Output I/O handle: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4656 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 4657 | std::string stateLiteral; |
| 4658 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4659 | dst->appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 4660 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 4661 | "communications", "media", "record", "dock", "system", |
| 4662 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 4663 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 4664 | 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] | 4665 | audio_policy_forced_cfg_t forceUseValue = mEngine->getForceUse(i); |
| 4666 | dst->appendFormat(" Force use for %s: %d", forceUses[i], forceUseValue); |
| 4667 | if (i == AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND && |
| 4668 | forceUseValue == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 4669 | dst->append(" (MANUAL: "); |
| 4670 | dumpManualSurroundFormats(dst); |
| 4671 | dst->append(")"); |
| 4672 | } |
| 4673 | dst->append("\n"); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 4674 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4675 | dst->appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 4676 | dst->appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
Mikhail Naganov | b3bcb4f | 2022-02-23 23:46:56 +0000 | [diff] [blame] | 4677 | dst->appendFormat(" Communication Strategy id: %d\n", mCommunnicationStrategy); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 4678 | dst->appendFormat(" Config source: %s\n", mConfig->getSource().c_str()); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4679 | |
Mikhail Naganov | 0f413b2 | 2021-12-02 05:29:27 +0000 | [diff] [blame] | 4680 | dst->append("\n"); |
| 4681 | mAvailableOutputDevices.dump(dst, String8("Available output"), 1); |
| 4682 | dst->append("\n"); |
| 4683 | mAvailableInputDevices.dump(dst, String8("Available input"), 1); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 4684 | mHwModules.dump(dst); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4685 | mOutputs.dump(dst); |
| 4686 | mInputs.dump(dst); |
Mikhail Naganov | 0f413b2 | 2021-12-02 05:29:27 +0000 | [diff] [blame] | 4687 | mEffects.dump(dst, 1); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4688 | mAudioPatches.dump(dst); |
| 4689 | mPolicyMixes.dump(dst); |
| 4690 | mAudioSources.dump(dst); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4691 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 4692 | dst->appendFormat(" AllowedCapturePolicies:\n"); |
| 4693 | for (auto& policy : mAllowedCapturePolicies) { |
| 4694 | dst->appendFormat(" - uid=%d flag_mask=%#x\n", policy.first, policy.second); |
| 4695 | } |
| 4696 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4697 | dst->appendFormat(" Preferred mixer audio configuration:\n"); |
| 4698 | for (const auto it : mPreferredMixerAttrInfos) { |
| 4699 | dst->appendFormat(" - device port id: %d\n", it.first); |
| 4700 | for (const auto preferredMixerInfoIt : it.second) { |
| 4701 | dst->appendFormat(" - strategy: %d; ", preferredMixerInfoIt.first); |
| 4702 | preferredMixerInfoIt.second->dump(dst); |
| 4703 | } |
| 4704 | } |
| 4705 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4706 | dst->appendFormat("\nPolicy Engine dump:\n"); |
| 4707 | mEngine->dump(dst); |
Vlad Popa | 87e0e58 | 2024-05-20 18:49:20 -0700 | [diff] [blame] | 4708 | |
| 4709 | dst->appendFormat("\nAbsolute volume devices with driving streams:\n"); |
| 4710 | for (const auto it : mAbsoluteVolumeDrivingStreams) { |
| 4711 | dst->appendFormat(" - device type: %s, driving stream %d\n", |
| 4712 | dumpDeviceTypes({it.first}).c_str(), |
| 4713 | mEngine->getVolumeGroupForAttributes(it.second)); |
| 4714 | } |
jiabin | 7ed22ff | 2024-10-04 20:51:21 +0000 | [diff] [blame^] | 4715 | |
| 4716 | // dump mmap policy by device |
| 4717 | dst->appendFormat("\nMmap policy:\n"); |
| 4718 | for (const auto& [policyType, policyByDevice] : mMmapPolicyByDeviceType) { |
| 4719 | std::stringstream ss; |
| 4720 | ss << '{'; |
| 4721 | for (const auto& [deviceType, policy] : policyByDevice) { |
| 4722 | ss << deviceType.toString() << ":" << toString(policy) << " "; |
| 4723 | } |
| 4724 | ss << '}'; |
| 4725 | dst->appendFormat(" - %s: %s\n", toString(policyType).c_str(), ss.str().c_str()); |
| 4726 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4727 | } |
| 4728 | |
| 4729 | status_t AudioPolicyManager::dump(int fd) |
| 4730 | { |
| 4731 | String8 result; |
| 4732 | dump(&result); |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 4733 | write(fd, result.c_str(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4734 | return NO_ERROR; |
| 4735 | } |
| 4736 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 4737 | status_t AudioPolicyManager::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) |
| 4738 | { |
| 4739 | mAllowedCapturePolicies[uid] = capturePolicy; |
| 4740 | return NO_ERROR; |
| 4741 | } |
| 4742 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4743 | // This function checks for the parameters which can be offloaded. |
| 4744 | // This can be enhanced depending on the capability of the DSP and policy |
| 4745 | // of the system. |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4746 | audio_offload_mode_t AudioPolicyManager::getOffloadSupport(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4747 | { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4748 | ALOGV("%s: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4749 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4750 | __func__, offloadInfo.sample_rate, offloadInfo.channel_mask, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4751 | offloadInfo.format, |
| 4752 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 4753 | offloadInfo.has_video); |
| 4754 | |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4755 | if (!isOffloadPossible(offloadInfo)) { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4756 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4757 | } |
| 4758 | |
| 4759 | // See if there is a profile to support this. |
| 4760 | // AUDIO_DEVICE_NONE |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4761 | sp<IOProfile> profile = getProfileForOutput(DeviceVector() /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4762 | offloadInfo.sample_rate, |
| 4763 | offloadInfo.format, |
| 4764 | offloadInfo.channel_mask, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4765 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, |
| 4766 | true /* directOnly */); |
Eric Laurent | 9436544 | 2021-01-08 18:36:05 +0100 | [diff] [blame] | 4767 | ALOGV("%s: profile %sfound%s", __func__, profile != nullptr ? "" : "NOT ", |
| 4768 | (profile != nullptr && (profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) |
| 4769 | ? ", supports gapless" : ""); |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4770 | if (profile == nullptr) { |
| 4771 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
| 4772 | } |
| 4773 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) { |
| 4774 | return AUDIO_OFFLOAD_GAPLESS_SUPPORTED; |
| 4775 | } |
| 4776 | return AUDIO_OFFLOAD_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4777 | } |
| 4778 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4779 | bool AudioPolicyManager::isDirectOutputSupported(const audio_config_base_t& config, |
| 4780 | const audio_attributes_t& attributes) { |
| 4781 | audio_output_flags_t output_flags = AUDIO_OUTPUT_FLAG_NONE; |
François Gaffie | 58d4be5 | 2018-11-06 15:30:12 +0100 | [diff] [blame] | 4782 | audio_flags_to_audio_output_flags(attributes.flags, &output_flags); |
Dorin Drimus | 9901eb0 | 2022-01-14 11:36:51 +0000 | [diff] [blame] | 4783 | DeviceVector outputDevices = mEngine->getOutputDevicesForAttributes(attributes); |
| 4784 | sp<IOProfile> profile = getProfileForOutput(outputDevices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4785 | config.sample_rate, |
| 4786 | config.format, |
| 4787 | config.channel_mask, |
| 4788 | output_flags, |
| 4789 | true /* directOnly */); |
| 4790 | ALOGV("%s() profile %sfound with name: %s, " |
| 4791 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 4792 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 4793 | (profile != 0 ? profile->getTagName().c_str() : "null"), |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4794 | config.sample_rate, config.format, config.channel_mask, output_flags); |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 4795 | |
| 4796 | // also try the MSD module if compatible profile not found |
| 4797 | if (profile == nullptr) { |
| 4798 | profile = getMsdProfileForOutput(outputDevices, |
| 4799 | config.sample_rate, |
| 4800 | config.format, |
| 4801 | config.channel_mask, |
| 4802 | output_flags, |
| 4803 | true /* directOnly */); |
| 4804 | ALOGV("%s() MSD profile %sfound with name: %s, " |
| 4805 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 4806 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
| 4807 | (profile != 0 ? profile->getTagName().c_str() : "null"), |
| 4808 | config.sample_rate, config.format, config.channel_mask, output_flags); |
| 4809 | } |
| 4810 | return (profile != nullptr); |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4811 | } |
| 4812 | |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4813 | bool AudioPolicyManager::isOffloadPossible(const audio_offload_info_t &offloadInfo, |
| 4814 | bool durationIgnored) { |
| 4815 | if (mMasterMono) { |
| 4816 | return false; // no offloading if mono is set. |
| 4817 | } |
| 4818 | |
| 4819 | // Check if offload has been disabled |
| 4820 | if (property_get_bool("audio.offload.disable", false /* default_value */)) { |
| 4821 | ALOGV("%s: offload disabled by audio.offload.disable", __func__); |
| 4822 | return false; |
| 4823 | } |
| 4824 | |
| 4825 | // Check if stream type is music, then only allow offload as of now. |
| 4826 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 4827 | { |
| 4828 | ALOGV("%s: stream_type != MUSIC, returning false", __func__); |
| 4829 | return false; |
| 4830 | } |
| 4831 | |
| 4832 | //TODO: enable audio offloading with video when ready |
| 4833 | const bool allowOffloadWithVideo = |
| 4834 | property_get_bool("audio.offload.video", false /* default_value */); |
| 4835 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
| 4836 | ALOGV("%s: has_video == true, returning false", __func__); |
| 4837 | return false; |
| 4838 | } |
| 4839 | |
| 4840 | //If duration is less than minimum value defined in property, return false |
| 4841 | const int min_duration_secs = property_get_int32( |
| 4842 | "audio.offload.min.duration.secs", -1 /* default_value */); |
| 4843 | if (!durationIgnored) { |
| 4844 | if (min_duration_secs >= 0) { |
| 4845 | if (offloadInfo.duration_us < min_duration_secs * 1000000LL) { |
| 4846 | ALOGV("%s: Offload denied by duration < audio.offload.min.duration.secs(=%d)", |
| 4847 | __func__, min_duration_secs); |
| 4848 | return false; |
| 4849 | } |
| 4850 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 4851 | ALOGV("%s: Offload denied by duration < default min(=%u)", |
| 4852 | __func__, OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 4853 | return false; |
| 4854 | } |
| 4855 | } |
| 4856 | |
| 4857 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 4858 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 4859 | // detects there is an active non offloadable effect. |
| 4860 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 4861 | // This may prevent offloading in rare situations where effects are left active by apps |
| 4862 | // in the background. |
| 4863 | if (mEffects.isNonOffloadableEffectEnabled()) { |
| 4864 | return false; |
| 4865 | } |
| 4866 | |
| 4867 | return true; |
| 4868 | } |
| 4869 | |
| 4870 | audio_direct_mode_t AudioPolicyManager::getDirectPlaybackSupport(const audio_attributes_t *attr, |
| 4871 | const audio_config_t *config) { |
| 4872 | audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER; |
| 4873 | offloadInfo.format = config->format; |
| 4874 | offloadInfo.sample_rate = config->sample_rate; |
| 4875 | offloadInfo.channel_mask = config->channel_mask; |
| 4876 | offloadInfo.stream_type = mEngine->getStreamTypeForAttributes(*attr); |
| 4877 | offloadInfo.has_video = false; |
| 4878 | offloadInfo.is_streaming = false; |
| 4879 | const bool offloadPossible = isOffloadPossible(offloadInfo, true /*durationIgnored*/); |
| 4880 | |
| 4881 | audio_direct_mode_t directMode = AUDIO_DIRECT_NOT_SUPPORTED; |
| 4882 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 4883 | audio_flags_to_audio_output_flags(attr->flags, &flags); |
| 4884 | // only retain flags that will drive compressed offload or passthrough |
| 4885 | uint32_t relevantFlags = AUDIO_OUTPUT_FLAG_HW_AV_SYNC; |
| 4886 | if (offloadPossible) { |
| 4887 | relevantFlags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD; |
| 4888 | } |
| 4889 | flags = (audio_output_flags_t)((flags & relevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 4890 | |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4891 | DeviceVector engineOutputDevices = mEngine->getOutputDevicesForAttributes(*attr); |
jiabin | 8a09667 | 2024-09-18 18:20:24 +0000 | [diff] [blame] | 4892 | if (std::any_of(engineOutputDevices.begin(), engineOutputDevices.end(), |
| 4893 | [this, attr](sp<DeviceDescriptor> device) { |
| 4894 | return getPreferredMixerAttributesInfo( |
| 4895 | device->getId(), |
| 4896 | mEngine->getProductStrategyForAttributes(*attr), |
| 4897 | true /*activeBitPerfectPreferred*/) != nullptr; |
| 4898 | })) { |
| 4899 | // Bit-perfect playback is active on one of the selected devices, direct output will |
| 4900 | // be rejected at this instant. |
| 4901 | return AUDIO_DIRECT_NOT_SUPPORTED; |
| 4902 | } |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4903 | for (const auto& hwModule : mHwModules) { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4904 | DeviceVector outputDevices = engineOutputDevices; |
| 4905 | // the MSD module checks for different conditions and output devices |
| 4906 | if (strcmp(hwModule->getName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0) { |
| 4907 | if (!msdHasPatchesToAllDevices(engineOutputDevices.toTypeAddrVector())) { |
| 4908 | continue; |
| 4909 | } |
| 4910 | outputDevices = getMsdAudioOutDevices(); |
| 4911 | } |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4912 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
jiabin | 66acc43 | 2024-02-06 00:57:36 +0000 | [diff] [blame] | 4913 | if (curProfile->getCompatibilityScore(outputDevices, |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4914 | config->sample_rate, nullptr /*updatedSamplingRate*/, |
| 4915 | config->format, nullptr /*updatedFormat*/, |
| 4916 | config->channel_mask, nullptr /*updatedChannelMask*/, |
jiabin | 66acc43 | 2024-02-06 00:57:36 +0000 | [diff] [blame] | 4917 | flags) == IOProfile::NO_MATCH) { |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4918 | continue; |
| 4919 | } |
| 4920 | // reject profiles not corresponding to a device currently available |
| 4921 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
| 4922 | continue; |
| 4923 | } |
Yinchu Chen | 9f66758 | 2023-10-10 09:44:54 +0000 | [diff] [blame] | 4924 | if (offloadPossible && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) |
| 4925 | != AUDIO_OUTPUT_FLAG_NONE)) { |
jiabin | c6132d6 | 2022-01-01 07:36:31 +0000 | [diff] [blame] | 4926 | if ((directMode & AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED) |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4927 | != AUDIO_DIRECT_NOT_SUPPORTED) { |
| 4928 | // Already reports offload gapless supported. No need to report offload support. |
| 4929 | continue; |
| 4930 | } |
| 4931 | if ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) |
| 4932 | != AUDIO_OUTPUT_FLAG_NONE) { |
| 4933 | // If offload gapless is reported, no need to report offload support. |
| 4934 | directMode = (audio_direct_mode_t) ((directMode & |
| 4935 | ~AUDIO_DIRECT_OFFLOAD_SUPPORTED) | |
| 4936 | AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED); |
| 4937 | } else { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4938 | directMode = (audio_direct_mode_t)(directMode | AUDIO_DIRECT_OFFLOAD_SUPPORTED); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4939 | } |
| 4940 | } else { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4941 | directMode = (audio_direct_mode_t) (directMode | AUDIO_DIRECT_BITSTREAM_SUPPORTED); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4942 | } |
| 4943 | } |
| 4944 | } |
| 4945 | return directMode; |
| 4946 | } |
| 4947 | |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4948 | status_t AudioPolicyManager::getDirectProfilesForAttributes(const audio_attributes_t* attr, |
| 4949 | AudioProfileVector& audioProfilesVector) { |
Dorin Drimus | 1711263 | 2022-09-23 15:28:51 +0000 | [diff] [blame] | 4950 | if (mEffects.isNonOffloadableEffectEnabled()) { |
| 4951 | return OK; |
| 4952 | } |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 4953 | DeviceVector devices; |
| 4954 | status_t status = getDevicesForAttributes(*attr, devices, false /* forVolume */); |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4955 | if (status != OK) { |
| 4956 | return status; |
| 4957 | } |
| 4958 | ALOGV("%s: found %zu output devices for attributes.", __func__, devices.size()); |
| 4959 | if (devices.empty()) { |
| 4960 | return OK; // no output devices for the attributes |
| 4961 | } |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 4962 | return getProfilesForDevices(devices, audioProfilesVector, |
| 4963 | AUDIO_OUTPUT_FLAG_DIRECT /*flags*/, false /*isInput*/); |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4964 | } |
| 4965 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4966 | status_t AudioPolicyManager::getSupportedMixerAttributes( |
| 4967 | audio_port_handle_t portId, std::vector<audio_mixer_attributes_t> &mixerAttrs) { |
| 4968 | ALOGV("%s, portId=%d", __func__, portId); |
| 4969 | sp<DeviceDescriptor> deviceDescriptor = mAvailableOutputDevices.getDeviceFromId(portId); |
| 4970 | if (deviceDescriptor == nullptr) { |
| 4971 | ALOGE("%s the requested device is currently unavailable", __func__); |
| 4972 | return BAD_VALUE; |
| 4973 | } |
jiabin | 96daffc | 2023-05-11 17:51:55 +0000 | [diff] [blame] | 4974 | if (!audio_is_usb_out_device(deviceDescriptor->type())) { |
| 4975 | ALOGE("%s the requested device(type=%#x) is not usb device", __func__, |
| 4976 | deviceDescriptor->type()); |
| 4977 | return BAD_VALUE; |
| 4978 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4979 | for (const auto& hwModule : mHwModules) { |
| 4980 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
| 4981 | if (curProfile->supportsDevice(deviceDescriptor)) { |
| 4982 | curProfile->toSupportedMixerAttributes(&mixerAttrs); |
| 4983 | } |
| 4984 | } |
| 4985 | } |
| 4986 | return NO_ERROR; |
| 4987 | } |
| 4988 | |
| 4989 | status_t AudioPolicyManager::setPreferredMixerAttributes( |
| 4990 | const audio_attributes_t *attr, |
| 4991 | audio_port_handle_t portId, |
| 4992 | uid_t uid, |
| 4993 | const audio_mixer_attributes_t *mixerAttributes) { |
| 4994 | ALOGV("%s, attr=%s, mixerAttributes={format=%#x, channelMask=%#x, samplingRate=%u, " |
| 4995 | "mixerBehavior=%d}, uid=%d, portId=%u", |
| 4996 | __func__, toString(*attr).c_str(), mixerAttributes->config.format, |
| 4997 | mixerAttributes->config.channel_mask, mixerAttributes->config.sample_rate, |
| 4998 | mixerAttributes->mixer_behavior, uid, portId); |
| 4999 | if (attr->usage != AUDIO_USAGE_MEDIA) { |
| 5000 | ALOGE("%s failed, only media is allowed, the given usage is %d", __func__, attr->usage); |
| 5001 | return BAD_VALUE; |
| 5002 | } |
| 5003 | sp<DeviceDescriptor> deviceDescriptor = mAvailableOutputDevices.getDeviceFromId(portId); |
| 5004 | if (deviceDescriptor == nullptr) { |
| 5005 | ALOGE("%s the requested device is currently unavailable", __func__); |
| 5006 | return BAD_VALUE; |
| 5007 | } |
| 5008 | if (!audio_is_usb_out_device(deviceDescriptor->type())) { |
| 5009 | ALOGE("%s(%d), type=%d, is not a usb output device", |
| 5010 | __func__, portId, deviceDescriptor->type()); |
| 5011 | return BAD_VALUE; |
| 5012 | } |
| 5013 | |
| 5014 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 5015 | audio_flags_to_audio_output_flags(attr->flags, &flags); |
| 5016 | flags = (audio_output_flags_t) (flags | |
| 5017 | audio_output_flags_from_mixer_behavior(mixerAttributes->mixer_behavior)); |
| 5018 | sp<IOProfile> profile = nullptr; |
| 5019 | DeviceVector devices(deviceDescriptor); |
| 5020 | for (const auto& hwModule : mHwModules) { |
| 5021 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
| 5022 | if (curProfile->hasDynamicAudioProfile() |
jiabin | 66acc43 | 2024-02-06 00:57:36 +0000 | [diff] [blame] | 5023 | && curProfile->getCompatibilityScore( |
| 5024 | devices, |
| 5025 | mixerAttributes->config.sample_rate, |
| 5026 | nullptr /*updatedSamplingRate*/, |
| 5027 | mixerAttributes->config.format, |
| 5028 | nullptr /*updatedFormat*/, |
| 5029 | mixerAttributes->config.channel_mask, |
| 5030 | nullptr /*updatedChannelMask*/, |
| 5031 | flags, |
| 5032 | false /*exactMatchRequiredForInputFlags*/) |
| 5033 | != IOProfile::NO_MATCH) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 5034 | profile = curProfile; |
| 5035 | break; |
| 5036 | } |
| 5037 | } |
| 5038 | } |
| 5039 | if (profile == nullptr) { |
| 5040 | ALOGE("%s, there is no compatible profile found", __func__); |
| 5041 | return BAD_VALUE; |
| 5042 | } |
| 5043 | |
| 5044 | sp<PreferredMixerAttributesInfo> mixerAttrInfo = |
| 5045 | sp<PreferredMixerAttributesInfo>::make( |
| 5046 | uid, portId, profile, flags, *mixerAttributes); |
| 5047 | const product_strategy_t strategy = mEngine->getProductStrategyForAttributes(*attr); |
| 5048 | mPreferredMixerAttrInfos[portId][strategy] = mixerAttrInfo; |
| 5049 | |
| 5050 | // If 1) there is any client from the preferred mixer configuration owner that is currently |
| 5051 | // active and matches the strategy and 2) current output is on the preferred device and the |
| 5052 | // mixer configuration doesn't match the preferred one, reopen output with preferred mixer |
| 5053 | // configuration. |
| 5054 | std::vector<audio_io_handle_t> outputsToReopen; |
| 5055 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5056 | const auto output = mOutputs.valueAt(i); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5057 | if (output->mProfile == profile && output->devices().onlyContainsDevice(deviceDescriptor)) { |
| 5058 | if (output->isConfigurationMatched(mixerAttributes->config, flags)) { |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 5059 | output->mPreferredAttrInfo = mixerAttrInfo; |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5060 | } else { |
| 5061 | for (const auto &client: output->getActiveClients()) { |
| 5062 | if (client->uid() == uid && client->strategy() == strategy) { |
| 5063 | client->setIsInvalid(); |
| 5064 | outputsToReopen.push_back(output->mIoHandle); |
| 5065 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 5066 | } |
| 5067 | } |
| 5068 | } |
| 5069 | } |
| 5070 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 5071 | config.sample_rate = mixerAttributes->config.sample_rate; |
| 5072 | config.channel_mask = mixerAttributes->config.channel_mask; |
| 5073 | config.format = mixerAttributes->config.format; |
| 5074 | for (const auto output : outputsToReopen) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5075 | sp<SwAudioOutputDescriptor> desc = |
| 5076 | reopenOutput(mOutputs.valueFor(output), &config, flags, __func__); |
| 5077 | if (desc == nullptr) { |
| 5078 | ALOGE("%s, failed to reopen output with preferred mixer attributes", __func__); |
| 5079 | continue; |
| 5080 | } |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 5081 | desc->mPreferredAttrInfo = mixerAttrInfo; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 5082 | } |
| 5083 | |
| 5084 | return NO_ERROR; |
| 5085 | } |
| 5086 | |
| 5087 | sp<PreferredMixerAttributesInfo> AudioPolicyManager::getPreferredMixerAttributesInfo( |
jiabin | d9a58d3 | 2023-06-01 17:57:30 +0000 | [diff] [blame] | 5088 | audio_port_handle_t devicePortId, |
| 5089 | product_strategy_t strategy, |
| 5090 | bool activeBitPerfectPreferred) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 5091 | auto it = mPreferredMixerAttrInfos.find(devicePortId); |
| 5092 | if (it == mPreferredMixerAttrInfos.end()) { |
| 5093 | return nullptr; |
| 5094 | } |
jiabin | d9a58d3 | 2023-06-01 17:57:30 +0000 | [diff] [blame] | 5095 | if (activeBitPerfectPreferred) { |
| 5096 | for (auto [strategy, info] : it->second) { |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 5097 | if (info->isBitPerfect() && info->getActiveClientCount() != 0) { |
jiabin | d9a58d3 | 2023-06-01 17:57:30 +0000 | [diff] [blame] | 5098 | return info; |
| 5099 | } |
| 5100 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 5101 | } |
jiabin | d9a58d3 | 2023-06-01 17:57:30 +0000 | [diff] [blame] | 5102 | auto strategyMatchedMixerAttrInfoIt = it->second.find(strategy); |
| 5103 | return strategyMatchedMixerAttrInfoIt == it->second.end() |
| 5104 | ? nullptr : strategyMatchedMixerAttrInfoIt->second; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 5105 | } |
| 5106 | |
| 5107 | status_t AudioPolicyManager::getPreferredMixerAttributes( |
| 5108 | const audio_attributes_t *attr, |
| 5109 | audio_port_handle_t portId, |
| 5110 | audio_mixer_attributes_t* mixerAttributes) { |
| 5111 | sp<PreferredMixerAttributesInfo> info = getPreferredMixerAttributesInfo( |
| 5112 | portId, mEngine->getProductStrategyForAttributes(*attr)); |
| 5113 | if (info == nullptr) { |
| 5114 | return NAME_NOT_FOUND; |
| 5115 | } |
| 5116 | *mixerAttributes = info->getMixerAttributes(); |
| 5117 | return NO_ERROR; |
| 5118 | } |
| 5119 | |
| 5120 | status_t AudioPolicyManager::clearPreferredMixerAttributes(const audio_attributes_t *attr, |
| 5121 | audio_port_handle_t portId, |
| 5122 | uid_t uid) { |
| 5123 | const product_strategy_t strategy = mEngine->getProductStrategyForAttributes(*attr); |
| 5124 | const auto preferredMixerAttrInfo = getPreferredMixerAttributesInfo(portId, strategy); |
| 5125 | if (preferredMixerAttrInfo == nullptr) { |
| 5126 | return NAME_NOT_FOUND; |
| 5127 | } |
| 5128 | if (preferredMixerAttrInfo->getUid() != uid) { |
| 5129 | ALOGE("%s, requested uid=%d, owned uid=%d", |
| 5130 | __func__, uid, preferredMixerAttrInfo->getUid()); |
| 5131 | return PERMISSION_DENIED; |
| 5132 | } |
| 5133 | mPreferredMixerAttrInfos[portId].erase(strategy); |
| 5134 | if (mPreferredMixerAttrInfos[portId].empty()) { |
| 5135 | mPreferredMixerAttrInfos.erase(portId); |
| 5136 | } |
| 5137 | |
| 5138 | // Reconfig existing output |
| 5139 | std::vector<audio_io_handle_t> potentialOutputsToReopen; |
| 5140 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5141 | if (mOutputs.valueAt(i)->mProfile == preferredMixerAttrInfo->getProfile()) { |
| 5142 | potentialOutputsToReopen.push_back(mOutputs.keyAt(i)); |
| 5143 | } |
| 5144 | } |
| 5145 | for (const auto output : potentialOutputsToReopen) { |
| 5146 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 5147 | if (desc->isConfigurationMatched(preferredMixerAttrInfo->getConfigBase(), |
| 5148 | preferredMixerAttrInfo->getFlags())) { |
| 5149 | reopenOutput(desc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 5150 | } |
| 5151 | } |
| 5152 | return NO_ERROR; |
| 5153 | } |
| 5154 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5155 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 5156 | audio_port_type_t type, |
| 5157 | unsigned int *num_ports, |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 5158 | struct audio_port_v7 *ports, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5159 | unsigned int *generation) |
| 5160 | { |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 5161 | if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) || |
| 5162 | generation == nullptr) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5163 | return BAD_VALUE; |
| 5164 | } |
| 5165 | 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] | 5166 | if (ports == nullptr) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5167 | *num_ports = 0; |
| 5168 | } |
| 5169 | |
| 5170 | size_t portsWritten = 0; |
| 5171 | size_t portsMax = *num_ports; |
| 5172 | *num_ports = 0; |
| 5173 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 5174 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 5175 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5176 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5177 | for (const auto& dev : mAvailableOutputDevices) { |
| 5178 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 5179 | continue; |
| 5180 | } |
| 5181 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5182 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 5183 | } |
| 5184 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5185 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5186 | } |
| 5187 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5188 | for (const auto& dev : mAvailableInputDevices) { |
| 5189 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 5190 | continue; |
| 5191 | } |
| 5192 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5193 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 5194 | } |
| 5195 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5196 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5197 | } |
| 5198 | } |
| 5199 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 5200 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 5201 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 5202 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 5203 | } |
| 5204 | *num_ports += mInputs.size(); |
| 5205 | } |
| 5206 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5207 | size_t numOutputs = 0; |
| 5208 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5209 | if (!mOutputs[i]->isDuplicated()) { |
| 5210 | numOutputs++; |
| 5211 | if (portsWritten < portsMax) { |
| 5212 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 5213 | } |
| 5214 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5215 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5216 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5217 | } |
| 5218 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 5219 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5220 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 5221 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5222 | return NO_ERROR; |
| 5223 | } |
| 5224 | |
Mikhail Naganov | 5edc5ed | 2023-03-23 14:52:15 -0700 | [diff] [blame] | 5225 | status_t AudioPolicyManager::listDeclaredDevicePorts(media::AudioPortRole role, |
| 5226 | std::vector<media::AudioPortFw>* _aidl_return) { |
| 5227 | auto pushPort = [&](const sp<DeviceDescriptor>& dev) -> status_t { |
| 5228 | audio_port_v7 port; |
| 5229 | dev->toAudioPort(&port); |
| 5230 | auto aidlPort = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_v7_AudioPortFw(port)); |
| 5231 | _aidl_return->push_back(std::move(aidlPort)); |
| 5232 | return OK; |
| 5233 | }; |
| 5234 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5235 | for (const auto& module : mHwModules) { |
Mikhail Naganov | 5edc5ed | 2023-03-23 14:52:15 -0700 | [diff] [blame] | 5236 | for (const auto& dev : module->getDeclaredDevices()) { |
| 5237 | if (role == media::AudioPortRole::NONE || |
| 5238 | ((role == media::AudioPortRole::SOURCE) |
| 5239 | == audio_is_input_device(dev->type()))) { |
| 5240 | RETURN_STATUS_IF_ERROR(pushPort(dev)); |
| 5241 | } |
| 5242 | } |
| 5243 | } |
| 5244 | return OK; |
| 5245 | } |
| 5246 | |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 5247 | status_t AudioPolicyManager::getAudioPort(struct audio_port_v7 *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5248 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 5249 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 5250 | return BAD_VALUE; |
| 5251 | } |
| 5252 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 5253 | if (dev != 0) { |
| 5254 | dev->toAudioPort(port); |
| 5255 | return NO_ERROR; |
| 5256 | } |
| 5257 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 5258 | if (dev != 0) { |
| 5259 | dev->toAudioPort(port); |
| 5260 | return NO_ERROR; |
| 5261 | } |
| 5262 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 5263 | if (out != 0) { |
| 5264 | out->toAudioPort(port); |
| 5265 | return NO_ERROR; |
| 5266 | } |
| 5267 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 5268 | if (in != 0) { |
| 5269 | in->toAudioPort(port); |
| 5270 | return NO_ERROR; |
| 5271 | } |
| 5272 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5273 | } |
| 5274 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5275 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 5276 | audio_patch_handle_t *handle, |
| 5277 | uid_t uid) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5278 | { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5279 | ALOGV("%s", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5280 | if (handle == NULL || patch == NULL) { |
| 5281 | return BAD_VALUE; |
| 5282 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5283 | 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] | 5284 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5285 | return BAD_VALUE; |
| 5286 | } |
| 5287 | // only one source per audio patch supported for now |
| 5288 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5289 | return INVALID_OPERATION; |
| 5290 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5291 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5292 | return INVALID_OPERATION; |
| 5293 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5294 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 5295 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 5296 | return INVALID_OPERATION; |
| 5297 | } |
| 5298 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5299 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5300 | sp<DeviceDescriptor> srcDevice = mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 5301 | sp<DeviceDescriptor> sinkDevice = mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id); |
| 5302 | if (srcDevice == nullptr || sinkDevice == nullptr) { |
| 5303 | ALOGW("%s could not create patch, invalid sink and/or source device(s)", __func__); |
| 5304 | return BAD_VALUE; |
| 5305 | } |
| 5306 | ALOGV("%s between source %s and sink %s", __func__, |
| 5307 | srcDevice->toString().c_str(), sinkDevice->toString().c_str()); |
| 5308 | audio_port_handle_t portId = PolicyAudioPort::getNextUniqueId(); |
| 5309 | // Default attributes, default volume priority, not to infer with non raw audio patches. |
| 5310 | audio_attributes_t attributes = attributes_initializer(AUDIO_USAGE_MEDIA); |
| 5311 | const struct audio_port_config *source = &patch->sources[0]; |
| 5312 | sp<SourceClientDescriptor> sourceDesc = |
Eric Laurent | 541a200 | 2024-01-15 18:11:42 +0100 | [diff] [blame] | 5313 | new SourceClientDescriptor( |
| 5314 | portId, uid, attributes, *source, srcDevice, AUDIO_STREAM_PATCH, |
| 5315 | mEngine->getProductStrategyForAttributes(attributes), toVolumeSource(attributes), |
Eric Laurent | ccbd787 | 2024-06-20 12:34:15 +0000 | [diff] [blame] | 5316 | true, false /*isCallRx*/, false /*isCallTx*/); |
Eric Laurent | 541a200 | 2024-01-15 18:11:42 +0100 | [diff] [blame] | 5317 | sourceDesc->setPreferredDeviceId(sinkDevice->getId()); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5318 | |
| 5319 | status_t status = |
| 5320 | connectAudioSourceToSink(sourceDesc, sinkDevice, patch, *handle, uid, 0 /* delayMs */); |
| 5321 | |
| 5322 | if (status != NO_ERROR) { |
| 5323 | return INVALID_OPERATION; |
| 5324 | } |
| 5325 | mAudioSources.add(portId, sourceDesc); |
| 5326 | return NO_ERROR; |
| 5327 | } |
| 5328 | |
| 5329 | status_t AudioPolicyManager::connectAudioSourceToSink( |
| 5330 | const sp<SourceClientDescriptor>& sourceDesc, const sp<DeviceDescriptor> &sinkDevice, |
| 5331 | const struct audio_patch *patch, |
| 5332 | audio_patch_handle_t &handle, |
| 5333 | uid_t uid, uint32_t delayMs) |
| 5334 | { |
| 5335 | status_t status = createAudioPatchInternal(patch, &handle, uid, delayMs, sourceDesc); |
| 5336 | if (status != NO_ERROR || mAudioPatches.indexOfKey(handle) < 0) { |
| 5337 | ALOGW("%s patch panel could not connect device patch, error %d", __func__, status); |
| 5338 | return INVALID_OPERATION; |
| 5339 | } |
| 5340 | sourceDesc->connect(handle, sinkDevice); |
| 5341 | if (isMsdPatch(handle)) { |
| 5342 | return NO_ERROR; |
| 5343 | } |
| 5344 | // SW Bridge? (@todo: HW bridge, keep track of HwOutput for device selection "reconsideration") |
| 5345 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); |
| 5346 | ALOG_ASSERT(swOutput != nullptr, "%s: a swOutput shall always be associated", __func__); |
| 5347 | if (swOutput->getClient(sourceDesc->portId()) != nullptr) { |
| 5348 | ALOGW("%s source portId has already been attached to outputDesc", __func__); |
| 5349 | goto FailurePatchAdded; |
| 5350 | } |
| 5351 | status = swOutput->start(); |
| 5352 | if (status != NO_ERROR) { |
| 5353 | goto FailureSourceAdded; |
| 5354 | } |
| 5355 | swOutput->addClient(sourceDesc); |
| 5356 | status = startSource(swOutput, sourceDesc, &delayMs); |
| 5357 | if (status != NO_ERROR) { |
| 5358 | ALOGW("%s failed to start source, error %d", __FUNCTION__, status); |
| 5359 | goto FailureSourceActive; |
| 5360 | } |
| 5361 | if (delayMs != 0) { |
| 5362 | usleep(delayMs * 1000); |
| 5363 | } |
| 5364 | return NO_ERROR; |
| 5365 | |
| 5366 | FailureSourceActive: |
| 5367 | swOutput->stop(); |
| 5368 | releaseOutput(sourceDesc->portId()); |
| 5369 | FailureSourceAdded: |
| 5370 | sourceDesc->setSwOutput(nullptr); |
| 5371 | FailurePatchAdded: |
| 5372 | releaseAudioPatchInternal(handle); |
| 5373 | return INVALID_OPERATION; |
| 5374 | } |
| 5375 | |
| 5376 | status_t AudioPolicyManager::createAudioPatchInternal(const struct audio_patch *patch, |
| 5377 | audio_patch_handle_t *handle, |
| 5378 | uid_t uid, uint32_t delayMs, |
| 5379 | const sp<SourceClientDescriptor>& sourceDesc) |
| 5380 | { |
| 5381 | 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] | 5382 | sp<AudioPatch> patchDesc; |
| 5383 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 5384 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5385 | ALOGV("%s source id %d role %d type %d", __func__, patch->sources[0].id, |
| 5386 | patch->sources[0].role, |
| 5387 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5388 | #if LOG_NDEBUG == 0 |
| 5389 | for (size_t i = 0; i < patch->num_sinks; i++) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5390 | ALOGV("%s sink %zu: id %d role %d type %d", __func__ ,i, patch->sinks[i].id, |
| 5391 | patch->sinks[i].role, |
| 5392 | patch->sinks[i].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5393 | } |
| 5394 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5395 | |
| 5396 | if (index >= 0) { |
| 5397 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5398 | ALOGV("%s mUidCached %d patchDesc->mUid %d uid %d", |
| 5399 | __func__, mUidCached, patchDesc->getUid(), uid); |
| 5400 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5401 | return INVALID_OPERATION; |
| 5402 | } |
| 5403 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5404 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5405 | } |
| 5406 | |
| 5407 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5408 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5409 | if (outputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5410 | 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] | 5411 | return BAD_VALUE; |
| 5412 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5413 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 5414 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5415 | if (patchDesc != 0) { |
| 5416 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5417 | ALOGV("%s source id differs for patch current id %d new id %d", |
| 5418 | __func__, patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5419 | return BAD_VALUE; |
| 5420 | } |
| 5421 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5422 | DeviceVector devices; |
| 5423 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 5424 | // Only support mix to devices connection |
| 5425 | // TODO add support for mix to mix connection |
| 5426 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5427 | ALOGV("%s source mix but sink is not a device", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5428 | return INVALID_OPERATION; |
| 5429 | } |
| 5430 | sp<DeviceDescriptor> devDesc = |
| 5431 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 5432 | if (devDesc == 0) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5433 | 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] | 5434 | return BAD_VALUE; |
| 5435 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5436 | |
jiabin | 66acc43 | 2024-02-06 00:57:36 +0000 | [diff] [blame] | 5437 | if (outputDesc->mProfile->getCompatibilityScore( |
| 5438 | DeviceVector(devDesc), |
| 5439 | patch->sources[0].sample_rate, |
| 5440 | nullptr, // updatedSamplingRate |
| 5441 | patch->sources[0].format, |
| 5442 | nullptr, // updatedFormat |
| 5443 | patch->sources[0].channel_mask, |
| 5444 | nullptr, // updatedChannelMask |
| 5445 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/) == IOProfile::NO_MATCH) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5446 | ALOGV("%s profile not supported for device %08x", __func__, devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5447 | return INVALID_OPERATION; |
| 5448 | } |
| 5449 | devices.add(devDesc); |
| 5450 | } |
| 5451 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5452 | return INVALID_OPERATION; |
| 5453 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5454 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5455 | // TODO: reconfigure output format and channels here |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5456 | ALOGV("%s setting device %s on output %d", |
| 5457 | __func__, dumpDeviceTypes(devices.types()).c_str(), outputDesc->mIoHandle); |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 5458 | setOutputDevices(__func__, outputDesc, devices, true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5459 | index = mAudioPatches.indexOfKey(*handle); |
| 5460 | if (index >= 0) { |
| 5461 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5462 | ALOGW("%s setOutputDevice() did not reuse the patch provided", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5463 | } |
| 5464 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5465 | patchDesc->setUid(uid); |
| 5466 | ALOGV("%s success", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5467 | } else { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5468 | ALOGW("%s setOutputDevice() failed to create a patch", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5469 | return INVALID_OPERATION; |
| 5470 | } |
| 5471 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 5472 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 5473 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5474 | // only one sink supported when connecting an input device to a mix |
| 5475 | if (patch->num_sinks > 1) { |
| 5476 | return INVALID_OPERATION; |
| 5477 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5478 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5479 | if (inputDesc == NULL) { |
| 5480 | return BAD_VALUE; |
| 5481 | } |
| 5482 | if (patchDesc != 0) { |
| 5483 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 5484 | return BAD_VALUE; |
| 5485 | } |
| 5486 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5487 | sp<DeviceDescriptor> device = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5488 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5489 | if (device == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5490 | return BAD_VALUE; |
| 5491 | } |
| 5492 | |
jiabin | 66acc43 | 2024-02-06 00:57:36 +0000 | [diff] [blame] | 5493 | if (inputDesc->mProfile->getCompatibilityScore( |
| 5494 | DeviceVector(device), |
| 5495 | patch->sinks[0].sample_rate, |
| 5496 | nullptr, /*updatedSampleRate*/ |
| 5497 | patch->sinks[0].format, |
| 5498 | nullptr, /*updatedFormat*/ |
| 5499 | patch->sinks[0].channel_mask, |
| 5500 | nullptr, /*updatedChannelMask*/ |
| 5501 | // FIXME for the parameter type, |
| 5502 | // and the NONE |
| 5503 | (audio_output_flags_t) |
| 5504 | AUDIO_INPUT_FLAG_NONE) == IOProfile::NO_MATCH) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5505 | return INVALID_OPERATION; |
| 5506 | } |
| 5507 | // TODO: reconfigure output format and channels here |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5508 | ALOGV("%s setting device %s on output %d", __func__, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5509 | device->toString().c_str(), inputDesc->mIoHandle); |
| 5510 | setInputDevice(inputDesc->mIoHandle, device, true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5511 | index = mAudioPatches.indexOfKey(*handle); |
| 5512 | if (index >= 0) { |
| 5513 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5514 | ALOGW("%s setInputDevice() did not reuse the patch provided", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5515 | } |
| 5516 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5517 | patchDesc->setUid(uid); |
| 5518 | ALOGV("%s success", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5519 | } else { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5520 | ALOGW("%s setInputDevice() failed to create a patch", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5521 | return INVALID_OPERATION; |
| 5522 | } |
| 5523 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 5524 | // device to device connection |
| 5525 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5526 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5527 | return BAD_VALUE; |
| 5528 | } |
| 5529 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5530 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5531 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5532 | if (srcDevice == 0) { |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 5533 | return BAD_VALUE; |
| 5534 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5535 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5536 | //update source and sink with our own data as the data passed in the patch may |
| 5537 | // be incomplete. |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5538 | PatchBuilder patchBuilder; |
| 5539 | audio_port_config sourcePortConfig = {}; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 5540 | |
| 5541 | // if first sink is to MSD, establish single MSD patch |
| 5542 | if (getMsdAudioOutDevices().contains( |
| 5543 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id))) { |
| 5544 | ALOGV("%s patching to MSD", __FUNCTION__); |
| 5545 | patchBuilder = buildMsdPatch(false /*msdIsSource*/, srcDevice); |
| 5546 | goto installPatch; |
| 5547 | } |
| 5548 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5549 | srcDevice->toAudioPortConfig(&sourcePortConfig, &patch->sources[0]); |
| 5550 | patchBuilder.addSource(sourcePortConfig); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5551 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5552 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 5553 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5554 | ALOGV("%s source device but one sink is not a device", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5555 | return INVALID_OPERATION; |
| 5556 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5557 | sp<DeviceDescriptor> sinkDevice = |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5558 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5559 | if (sinkDevice == 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5560 | return BAD_VALUE; |
| 5561 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5562 | audio_port_config sinkPortConfig = {}; |
| 5563 | sinkDevice->toAudioPortConfig(&sinkPortConfig, &patch->sinks[i]); |
| 5564 | patchBuilder.addSink(sinkPortConfig); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5565 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5566 | // Whatever Sw or Hw bridge, we do attach an SwOutput to an Audio Source for |
| 5567 | // volume management purpose (tracking activity) |
| 5568 | // In case of Hw bridge, it is a Work Around. The mixPort used is the one declared |
| 5569 | // in config XML to reach the sink so that is can be declared as available. |
| 5570 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 5571 | sp<SwAudioOutputDescriptor> outputDesc; |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5572 | if (!sourceDesc->isInternal()) { |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5573 | // take care of dynamic routing for SwOutput selection, |
| 5574 | audio_attributes_t attributes = sourceDesc->attributes(); |
| 5575 | audio_stream_type_t stream = sourceDesc->stream(); |
| 5576 | audio_attributes_t resultAttr; |
| 5577 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 5578 | config.sample_rate = sourceDesc->config().sample_rate; |
François Gaffie | 2a24db4 | 2022-04-04 16:45:02 +0200 | [diff] [blame] | 5579 | audio_channel_mask_t sourceMask = sourceDesc->config().channel_mask; |
| 5580 | config.channel_mask = |
| 5581 | (audio_channel_mask_get_representation(sourceMask) |
| 5582 | == AUDIO_CHANNEL_REPRESENTATION_INDEX) ? sourceMask |
| 5583 | : audio_channel_mask_in_to_out(sourceMask); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5584 | config.format = sourceDesc->config().format; |
| 5585 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 5586 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 5587 | bool isRequestedDeviceForExclusiveUse = false; |
| 5588 | output_type_t outputType; |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 5589 | bool isSpatialized; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 5590 | bool isBitPerfect; |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5591 | getOutputForAttrInt(&resultAttr, &output, AUDIO_SESSION_NONE, &attributes, |
| 5592 | &stream, sourceDesc->uid(), &config, &flags, |
| 5593 | &selectedDeviceId, &isRequestedDeviceForExclusiveUse, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 5594 | nullptr, &outputType, &isSpatialized, &isBitPerfect); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5595 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 5596 | ALOGV("%s no output for device %s", |
| 5597 | __FUNCTION__, sinkDevice->toString().c_str()); |
| 5598 | return INVALID_OPERATION; |
| 5599 | } |
| 5600 | outputDesc = mOutputs.valueFor(output); |
| 5601 | if (outputDesc->isDuplicated()) { |
| 5602 | ALOGE("%s output is duplicated", __func__); |
| 5603 | return INVALID_OPERATION; |
| 5604 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5605 | bool closeOutput = outputDesc->mDirectOpenCount != 0; |
| 5606 | sourceDesc->setSwOutput(outputDesc, closeOutput); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5607 | } else { |
| 5608 | // Same for "raw patches" aka created from createAudioPatch API |
| 5609 | SortedVector<audio_io_handle_t> outputs = |
| 5610 | getOutputsForDevices(DeviceVector(sinkDevice), mOutputs); |
| 5611 | // if the sink device is reachable via an opened output stream, request to |
| 5612 | // go via this output stream by adding a second source to the patch |
| 5613 | // description |
| 5614 | output = selectOutput(outputs); |
| 5615 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 5616 | ALOGE("%s no output available for internal patch sink", __func__); |
| 5617 | return INVALID_OPERATION; |
| 5618 | } |
| 5619 | outputDesc = mOutputs.valueFor(output); |
| 5620 | if (outputDesc->isDuplicated()) { |
| 5621 | ALOGV("%s output for device %s is duplicated", |
| 5622 | __func__, sinkDevice->toString().c_str()); |
| 5623 | return INVALID_OPERATION; |
| 5624 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5625 | sourceDesc->setSwOutput(outputDesc, /* closeOutput= */ false); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5626 | } |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 5627 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 5628 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 5629 | // - audio HAL version is < 3.0 |
Francois Gaffie | 99896da | 2018-04-09 11:05:33 +0200 | [diff] [blame] | 5630 | // - 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] | 5631 | // - called from startAudioSource (aka sourceDesc is not internal) and source device |
| 5632 | // does not have a gain controller |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5633 | if (!srcDevice->hasSameHwModuleAs(sinkDevice) || |
| 5634 | (srcDevice->getModuleVersionMajor() < 3) || |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5635 | !srcDevice->getModule()->supportsPatch(srcDevice, sinkDevice) || |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5636 | (!sourceDesc->isInternal() && |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5637 | srcDevice->getAudioPort()->getGains().size() == 0)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 5638 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5639 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5640 | return INVALID_OPERATION; |
| 5641 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5642 | sourceDesc->setUseSwBridge(); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5643 | if (outputDesc != nullptr) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5644 | audio_port_config srcMixPortConfig = {}; |
Ytai Ben-Tsvi | c9d2a91 | 2021-11-22 16:43:09 -0800 | [diff] [blame] | 5645 | outputDesc->toAudioPortConfig(&srcMixPortConfig, nullptr); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5646 | // for volume control, we may need a valid stream |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 5647 | srcMixPortConfig.ext.mix.usecase.stream = |
Eric Laurent | ccbd787 | 2024-06-20 12:34:15 +0000 | [diff] [blame] | 5648 | (!sourceDesc->isInternal() || sourceDesc->isCallTx()) ? |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5649 | mEngine->getStreamTypeForAttributes(sourceDesc->attributes()) : |
| 5650 | AUDIO_STREAM_PATCH; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5651 | patchBuilder.addSource(srcMixPortConfig); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5652 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5653 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5654 | } |
| 5655 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 5656 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 5657 | installPatch: |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5658 | status_t status = installPatch( |
| 5659 | __func__, index, handle, patchBuilder.patch(), delayMs, uid, &patchDesc); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5660 | if (status != NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5661 | 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] | 5662 | return INVALID_OPERATION; |
| 5663 | } |
| 5664 | } else { |
| 5665 | return BAD_VALUE; |
| 5666 | } |
| 5667 | } else { |
| 5668 | return BAD_VALUE; |
| 5669 | } |
| 5670 | return NO_ERROR; |
| 5671 | } |
| 5672 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5673 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, uid_t uid) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5674 | { |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5675 | ALOGV("%s patch %d", __func__, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5676 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 5677 | |
| 5678 | if (index < 0) { |
| 5679 | return BAD_VALUE; |
| 5680 | } |
| 5681 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5682 | ALOGV("%s() mUidCached %d patchDesc->mUid %d uid %d", |
| 5683 | __func__, mUidCached, patchDesc->getUid(), uid); |
| 5684 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5685 | return INVALID_OPERATION; |
| 5686 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5687 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 5688 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 5689 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5690 | if (sourceDesc != nullptr && sourceDesc->getPatchHandle() == handle) { |
| 5691 | portId = sourceDesc->portId(); |
| 5692 | break; |
| 5693 | } |
| 5694 | } |
| 5695 | return portId != AUDIO_PORT_HANDLE_NONE ? |
| 5696 | stopAudioSource(portId) : releaseAudioPatchInternal(handle); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5697 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5698 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5699 | status_t AudioPolicyManager::releaseAudioPatchInternal(audio_patch_handle_t handle, |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5700 | uint32_t delayMs, |
| 5701 | const sp<SourceClientDescriptor>& sourceDesc) |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5702 | { |
| 5703 | ALOGV("%s patch %d", __func__, handle); |
| 5704 | if (mAudioPatches.indexOfKey(handle) < 0) { |
| 5705 | ALOGE("%s: no patch found with handle=%d", __func__, handle); |
| 5706 | return BAD_VALUE; |
| 5707 | } |
| 5708 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5709 | struct audio_patch *patch = &patchDesc->mPatch; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5710 | patchDesc->setUid(mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5711 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5712 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5713 | if (outputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5714 | 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] | 5715 | return BAD_VALUE; |
| 5716 | } |
| 5717 | |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 5718 | setOutputDevices(__func__, outputDesc, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5719 | getNewOutputDevices(outputDesc, true /*fromCache*/), |
| 5720 | true, |
| 5721 | 0, |
| 5722 | NULL); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5723 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 5724 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5725 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5726 | if (inputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5727 | 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] | 5728 | return BAD_VALUE; |
| 5729 | } |
| 5730 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 5731 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5732 | true, |
| 5733 | NULL); |
| 5734 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5735 | status_t status = |
| 5736 | mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); |
| 5737 | ALOGV("%s patch panel returned %d patchHandle %d", |
| 5738 | __func__, status, patchDesc->getAfHandle()); |
| 5739 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5740 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5741 | mpClientInterface->onAudioPatchListUpdate(); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5742 | // SW or HW Bridge |
| 5743 | sp<SwAudioOutputDescriptor> outputDesc = nullptr; |
| 5744 | audio_patch_handle_t patchHandle = AUDIO_PATCH_HANDLE_NONE; |
Francois Gaffie | 7feb854 | 2020-04-06 17:39:47 +0200 | [diff] [blame] | 5745 | 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] | 5746 | outputDesc = mOutputs.getOutputFromId(patch->sources[1].id); |
| 5747 | } else if (patch->num_sources == 1 && sourceDesc != nullptr) { |
| 5748 | outputDesc = sourceDesc->swOutput().promote(); |
| 5749 | } |
| 5750 | if (outputDesc == nullptr) { |
| 5751 | ALOGW("%s no output for id %d", __func__, patch->sources[0].id); |
| 5752 | // releaseOutput has already called closeOutput in case of direct output |
| 5753 | return NO_ERROR; |
| 5754 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5755 | patchHandle = outputDesc->getPatchHandle(); |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5756 | // While using a HwBridge, force reconsidering device only if not reusing an existing |
| 5757 | // output and no more activity on output (will force to close). |
François Gaffie | 150fcc6 | 2023-09-15 11:02:39 +0200 | [diff] [blame] | 5758 | const bool force = sourceDesc->canCloseOutput() && !outputDesc->isActive(); |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5759 | // APM pattern is to have always outputs opened / patch realized for reachable devices. |
| 5760 | // Update device may result to NONE (empty), coupled with force, it releases the patch. |
| 5761 | // Reconsider device only for cases: |
| 5762 | // 1 / Active Output |
| 5763 | // 2 / Inactive Output previously hosting HwBridge |
| 5764 | // 3 / Inactive Output previously hosting SwBridge that can be closed. |
| 5765 | bool updateDevice = outputDesc->isActive() || !sourceDesc->useSwBridge() || |
| 5766 | sourceDesc->canCloseOutput(); |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 5767 | setOutputDevices(__func__, outputDesc, |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5768 | updateDevice ? getNewOutputDevices(outputDesc, true /*fromCache*/) : |
| 5769 | outputDesc->devices(), |
| 5770 | force, |
| 5771 | 0, |
| 5772 | patchHandle == AUDIO_PATCH_HANDLE_NONE ? nullptr : &patchHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5773 | } else { |
| 5774 | return BAD_VALUE; |
| 5775 | } |
| 5776 | } else { |
| 5777 | return BAD_VALUE; |
| 5778 | } |
| 5779 | return NO_ERROR; |
| 5780 | } |
| 5781 | |
| 5782 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 5783 | struct audio_patch *patches, |
| 5784 | unsigned int *generation) |
| 5785 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5786 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5787 | return BAD_VALUE; |
| 5788 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5789 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5790 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5791 | } |
| 5792 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5793 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5794 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5795 | ALOGV("setAudioPortConfig()"); |
| 5796 | |
| 5797 | if (config == NULL) { |
| 5798 | return BAD_VALUE; |
| 5799 | } |
| 5800 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 5801 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5802 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 5803 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5804 | } |
| 5805 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5806 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5807 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 5808 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5809 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5810 | if (outputDesc == NULL) { |
| 5811 | return BAD_VALUE; |
| 5812 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5813 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 5814 | "setAudioPortConfig() called on duplicated output %d", |
| 5815 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5816 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5817 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5818 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5819 | if (inputDesc == NULL) { |
| 5820 | return BAD_VALUE; |
| 5821 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5822 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5823 | } else { |
| 5824 | return BAD_VALUE; |
| 5825 | } |
| 5826 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 5827 | sp<DeviceDescriptor> deviceDesc; |
| 5828 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 5829 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 5830 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 5831 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 5832 | } else { |
| 5833 | return BAD_VALUE; |
| 5834 | } |
| 5835 | if (deviceDesc == NULL) { |
| 5836 | return BAD_VALUE; |
| 5837 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5838 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5839 | } else { |
| 5840 | return BAD_VALUE; |
| 5841 | } |
| 5842 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 5843 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5844 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 5845 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 5846 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5847 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 5848 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5849 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5850 | if (status != NO_ERROR) { |
| 5851 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5852 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5853 | |
| 5854 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5855 | } |
| 5856 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5857 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 5858 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5859 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5860 | clearAudioPatches(uid); |
| 5861 | clearSessionRoutes(uid); |
| 5862 | } |
| 5863 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5864 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 5865 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 5866 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5867 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5868 | if (patchDesc->getUid() == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 5869 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5870 | } |
| 5871 | } |
| 5872 | } |
| 5873 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5874 | void AudioPolicyManager::checkStrategyRoute(product_strategy_t ps, audio_io_handle_t ouptutToSkip) |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5875 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5876 | // Take the first attributes following the product strategy as it is used to retrieve the routed |
| 5877 | // device. All attributes wihin a strategy follows the same "routing strategy" |
| 5878 | auto attributes = mEngine->getAllAttributesForProductStrategy(ps).front(); |
| 5879 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(attributes, nullptr, false); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5880 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5881 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5882 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 5883 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 5884 | continue; |
| 5885 | } |
| 5886 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5887 | if (!outputDesc->isStrategyActive(ps)) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5888 | continue; |
| 5889 | } |
| 5890 | // If the default device for this strategy is on another output mix, |
| 5891 | // invalidate all tracks in this strategy to force re connection. |
| 5892 | // Otherwise select new device on the output mix. |
| 5893 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 5894 | invalidateStreams(mEngine->getStreamTypesForProductStrategy(ps)); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5895 | } else { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5896 | DeviceVector newDevices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 5897 | if (outputDesc->mPreferredAttrInfo != nullptr && outputDesc->devices() != newDevices) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5898 | // If the device is using preferred mixer attributes, the output need to reopen |
| 5899 | // with default configuration when the new selected devices are different from |
| 5900 | // current routing devices. |
| 5901 | outputsToReopen.emplace(mOutputs.keyAt(j), newDevices); |
| 5902 | continue; |
| 5903 | } |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 5904 | setOutputDevices(__func__, outputDesc, newDevices, false); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5905 | } |
| 5906 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5907 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5908 | } |
| 5909 | |
| 5910 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 5911 | { |
| 5912 | // remove output routes associated with this uid |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5913 | std::vector<product_strategy_t> affectedStrategies; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5914 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5915 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 5916 | for (const auto& client : outputDesc->getClientIterable()) { |
| 5917 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 5918 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5919 | auto clientStrategy = client->strategy(); |
| 5920 | if (std::find(begin(affectedStrategies), end(affectedStrategies), clientStrategy) != |
| 5921 | end(affectedStrategies)) { |
| 5922 | continue; |
| 5923 | } |
| 5924 | affectedStrategies.push_back(client->strategy()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5925 | } |
| 5926 | } |
| 5927 | } |
| 5928 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5929 | for (const auto& strategy : affectedStrategies) { |
| 5930 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5931 | } |
| 5932 | |
| 5933 | // remove input routes associated with this uid |
| 5934 | SortedVector<audio_source_t> affectedSources; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5935 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5936 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 5937 | for (const auto& client : inputDesc->getClientIterable()) { |
| 5938 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 5939 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 5940 | affectedSources.add(client->source()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5941 | } |
| 5942 | } |
| 5943 | } |
| 5944 | // reroute inputs if necessary |
| 5945 | SortedVector<audio_io_handle_t> inputsToClose; |
| 5946 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5947 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 5948 | if (affectedSources.indexOf(inputDesc->source()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5949 | inputsToClose.add(inputDesc->mIoHandle); |
| 5950 | } |
| 5951 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5952 | for (const auto& input : inputsToClose) { |
| 5953 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5954 | } |
| 5955 | } |
| 5956 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5957 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 5958 | { |
| 5959 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5960 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5961 | if (sourceDesc->uid() == uid) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5962 | stopAudioSource(mAudioSources.keyAt(i)); |
| 5963 | } |
| 5964 | } |
| 5965 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5966 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5967 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 5968 | audio_io_handle_t *ioHandle, |
| 5969 | audio_devices_t *device) |
| 5970 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 5971 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 5972 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5973 | audio_attributes_t attr = { .source = AUDIO_SOURCE_HOTWORD }; |
Eric Laurent | cedd5b5 | 2023-03-22 00:03:31 +0000 | [diff] [blame] | 5974 | sp<DeviceDescriptor> deviceDesc = mEngine->getInputDeviceForAttributes(attr); |
| 5975 | if (deviceDesc == nullptr) { |
| 5976 | return INVALID_OPERATION; |
| 5977 | } |
| 5978 | *device = deviceDesc->type(); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5979 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 5980 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5981 | } |
| 5982 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5983 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5984 | const audio_attributes_t *attributes, |
| 5985 | audio_port_handle_t *portId, |
Eric Laurent | ccbd787 | 2024-06-20 12:34:15 +0000 | [diff] [blame] | 5986 | uid_t uid) { |
| 5987 | return startAudioSourceInternal(source, attributes, portId, uid, |
David Li | f85c5e3 | 2024-07-01 13:14:10 +0000 | [diff] [blame] | 5988 | false /*internal*/, false /*isCallRx*/, 0 /*delayMs*/); |
Eric Laurent | ccbd787 | 2024-06-20 12:34:15 +0000 | [diff] [blame] | 5989 | } |
| 5990 | |
| 5991 | status_t AudioPolicyManager::startAudioSourceInternal(const struct audio_port_config *source, |
| 5992 | const audio_attributes_t *attributes, |
| 5993 | audio_port_handle_t *portId, |
David Li | f85c5e3 | 2024-07-01 13:14:10 +0000 | [diff] [blame] | 5994 | uid_t uid, bool internal, bool isCallRx, |
| 5995 | uint32_t delayMs) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5996 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5997 | ALOGV("%s", __FUNCTION__); |
| 5998 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 5999 | |
| 6000 | if (source == NULL || attributes == NULL || portId == NULL) { |
| 6001 | ALOGW("%s invalid argument: source %p attributes %p handle %p", |
| 6002 | __FUNCTION__, source, attributes, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6003 | return BAD_VALUE; |
| 6004 | } |
| 6005 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6006 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 6007 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6008 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", |
| 6009 | __FUNCTION__, source->role, source->type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6010 | return INVALID_OPERATION; |
| 6011 | } |
| 6012 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6013 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6014 | mAvailableInputDevices.getDevice(source->ext.device.type, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6015 | String8(source->ext.device.address), |
| 6016 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6017 | if (srcDevice == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6018 | 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] | 6019 | return BAD_VALUE; |
| 6020 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6021 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6022 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6023 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6024 | sp<SourceClientDescriptor> sourceDesc = |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6025 | new SourceClientDescriptor(*portId, uid, *attributes, *source, srcDevice, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6026 | mEngine->getStreamTypeForAttributes(*attributes), |
| 6027 | mEngine->getProductStrategyForAttributes(*attributes), |
Eric Laurent | ccbd787 | 2024-06-20 12:34:15 +0000 | [diff] [blame] | 6028 | toVolumeSource(*attributes), internal, isCallRx, false); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6029 | |
David Li | f85c5e3 | 2024-07-01 13:14:10 +0000 | [diff] [blame] | 6030 | status_t status = connectAudioSource(sourceDesc, delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6031 | if (status == NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6032 | mAudioSources.add(*portId, sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6033 | } |
| 6034 | return status; |
| 6035 | } |
| 6036 | |
David Li | f85c5e3 | 2024-07-01 13:14:10 +0000 | [diff] [blame] | 6037 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc, |
| 6038 | uint32_t delayMs) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6039 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6040 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6041 | |
| 6042 | // make sure we only have one patch per source. |
| 6043 | disconnectAudioSource(sourceDesc); |
| 6044 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6045 | audio_attributes_t attributes = sourceDesc->attributes(); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 6046 | // May the device (dynamic) have been disconnected/reconnected, id has changed. |
| 6047 | sp<DeviceDescriptor> srcDevice = mAvailableInputDevices.getDevice( |
| 6048 | sourceDesc->srcDevice()->type(), |
| 6049 | String8(sourceDesc->srcDevice()->address().c_str()), |
| 6050 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6051 | DeviceVector sinkDevices = |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6052 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false /*fromCache*/); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6053 | ALOG_ASSERT(!sinkDevices.isEmpty(), "connectAudioSource(): no device found for attributes"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6054 | sp<DeviceDescriptor> sinkDevice = sinkDevices.itemAt(0); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 6055 | if (!mAvailableOutputDevices.contains(sinkDevice)) { |
| 6056 | ALOGE("%s Device %s not available", __func__, sinkDevice->toString().c_str()); |
| 6057 | return INVALID_OPERATION; |
| 6058 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6059 | PatchBuilder patchBuilder; |
| 6060 | patchBuilder.addSink(sinkDevice).addSource(srcDevice); |
| 6061 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6062 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6063 | return connectAudioSourceToSink( |
David Li | f85c5e3 | 2024-07-01 13:14:10 +0000 | [diff] [blame] | 6064 | sourceDesc, sinkDevice, patchBuilder.patch(), handle, mUidCached, delayMs); |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 6065 | } |
| 6066 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6067 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 6068 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6069 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); |
| 6070 | ALOGV("%s port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6071 | if (sourceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6072 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6073 | return BAD_VALUE; |
| 6074 | } |
| 6075 | status_t status = disconnectAudioSource(sourceDesc); |
| 6076 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6077 | mAudioSources.removeItem(portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6078 | return status; |
| 6079 | } |
| 6080 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 6081 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 6082 | { |
| 6083 | if (mMasterMono == mono) { |
| 6084 | return NO_ERROR; |
| 6085 | } |
| 6086 | mMasterMono = mono; |
| 6087 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 6088 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 6089 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 6090 | // |
| 6091 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 6092 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 6093 | // play back offloaded. |
| 6094 | if (mMasterMono) { |
| 6095 | Vector<audio_io_handle_t> offloaded; |
| 6096 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 6097 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 6098 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 6099 | offloaded.push(desc->mIoHandle); |
| 6100 | } |
| 6101 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 6102 | for (const auto& handle : offloaded) { |
| 6103 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 6104 | } |
| 6105 | } |
| 6106 | // update master mono for all remaining outputs |
| 6107 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 6108 | updateMono(mOutputs.keyAt(i)); |
| 6109 | } |
| 6110 | return NO_ERROR; |
| 6111 | } |
| 6112 | |
| 6113 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 6114 | { |
| 6115 | *mono = mMasterMono; |
| 6116 | return NO_ERROR; |
| 6117 | } |
| 6118 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 6119 | float AudioPolicyManager::getStreamVolumeDB( |
| 6120 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 6121 | { |
Vlad Popa | 9d48276 | 2024-06-21 16:40:23 -0700 | [diff] [blame] | 6122 | return computeVolume(getVolumeCurves(stream), toVolumeSource(stream), index, |
| 6123 | {device}, /* adjustAttenuation= */false); |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 6124 | } |
| 6125 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6126 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 6127 | audio_format_t *surroundFormats, |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 6128 | bool *surroundFormatsEnabled) |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6129 | { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 6130 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && |
| 6131 | (surroundFormats == nullptr || surroundFormatsEnabled == nullptr))) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6132 | return BAD_VALUE; |
| 6133 | } |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 6134 | ALOGV("%s() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p", |
| 6135 | __func__, *numSurroundFormats, surroundFormats, surroundFormatsEnabled); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6136 | |
| 6137 | size_t formatsWritten = 0; |
| 6138 | size_t formatsMax = *numSurroundFormats; |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 6139 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6140 | *numSurroundFormats = mConfig->getSurroundFormats().size(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6141 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 6142 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6143 | for (const auto& format: mConfig->getSurroundFormats()) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6144 | if (formatsWritten < formatsMax) { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 6145 | surroundFormats[formatsWritten] = format.first; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6146 | bool formatEnabled = true; |
| 6147 | switch (forceUse) { |
| 6148 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL: |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 6149 | formatEnabled = mManualSurroundFormats.count(format.first) != 0; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6150 | break; |
| 6151 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER: |
| 6152 | formatEnabled = false; |
| 6153 | break; |
| 6154 | default: // AUTO or ALWAYS => true |
| 6155 | break; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6156 | } |
| 6157 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 6158 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6159 | } |
| 6160 | return NO_ERROR; |
| 6161 | } |
| 6162 | |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 6163 | status_t AudioPolicyManager::getReportedSurroundFormats(unsigned int *numSurroundFormats, |
| 6164 | audio_format_t *surroundFormats) { |
| 6165 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) { |
| 6166 | return BAD_VALUE; |
| 6167 | } |
| 6168 | ALOGV("%s() numSurroundFormats %d surroundFormats %p", |
| 6169 | __func__, *numSurroundFormats, surroundFormats); |
| 6170 | |
| 6171 | size_t formatsWritten = 0; |
| 6172 | size_t formatsMax = *numSurroundFormats; |
| 6173 | std::unordered_set<audio_format_t> formats; // Uses primary surround formats only |
| 6174 | |
| 6175 | // Return formats from all device profiles that have already been resolved by |
| 6176 | // checkOutputsForDevice(). |
| 6177 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 6178 | sp<DeviceDescriptor> device = mAvailableOutputDevices[i]; |
| 6179 | audio_devices_t deviceType = device->type(); |
| 6180 | // Enabling/disabling formats are applied to only HDMI devices. So, this function |
| 6181 | // returns formats reported by HDMI devices. |
hongchao.yin | f0c8208 | 2024-07-24 19:41:02 +0800 | [diff] [blame] | 6182 | if (deviceType != AUDIO_DEVICE_OUT_HDMI && |
| 6183 | deviceType != AUDIO_DEVICE_OUT_HDMI_ARC && deviceType != AUDIO_DEVICE_OUT_HDMI_EARC) { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 6184 | continue; |
| 6185 | } |
| 6186 | // Formats reported by sink devices |
| 6187 | std::unordered_set<audio_format_t> formatset; |
| 6188 | if (auto it = mReportedFormatsMap.find(device); it != mReportedFormatsMap.end()) { |
| 6189 | formatset.insert(it->second.begin(), it->second.end()); |
| 6190 | } |
| 6191 | |
| 6192 | // Formats hard-coded in the in policy configuration file (if any). |
| 6193 | FormatVector encodedFormats = device->encodedFormats(); |
| 6194 | formatset.insert(encodedFormats.begin(), encodedFormats.end()); |
| 6195 | // Filter the formats which are supported by the vendor hardware. |
| 6196 | for (auto it = formatset.begin(); it != formatset.end(); ++it) { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6197 | if (mConfig->getSurroundFormats().count(*it) != 0) { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 6198 | formats.insert(*it); |
| 6199 | } else { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6200 | for (const auto& pair : mConfig->getSurroundFormats()) { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 6201 | if (pair.second.count(*it) != 0) { |
| 6202 | formats.insert(pair.first); |
| 6203 | break; |
| 6204 | } |
| 6205 | } |
| 6206 | } |
| 6207 | } |
| 6208 | } |
| 6209 | *numSurroundFormats = formats.size(); |
| 6210 | for (const auto& format: formats) { |
| 6211 | if (formatsWritten < formatsMax) { |
| 6212 | surroundFormats[formatsWritten++] = format; |
| 6213 | } |
| 6214 | } |
| 6215 | return NO_ERROR; |
| 6216 | } |
| 6217 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6218 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 6219 | { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 6220 | ALOGV("%s() format 0x%X enabled %d", __func__, audioFormat, enabled); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6221 | const auto& formatIter = mConfig->getSurroundFormats().find(audioFormat); |
| 6222 | if (formatIter == mConfig->getSurroundFormats().end()) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 6223 | ALOGW("%s() format 0x%X is not a known surround format", __func__, audioFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6224 | return BAD_VALUE; |
| 6225 | } |
| 6226 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6227 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND) != |
| 6228 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 6229 | ALOGW("%s() not in manual mode for surround sound format selection", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6230 | return INVALID_OPERATION; |
| 6231 | } |
| 6232 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6233 | if ((mManualSurroundFormats.count(audioFormat) != 0) == enabled) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6234 | return NO_ERROR; |
| 6235 | } |
| 6236 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6237 | std::unordered_set<audio_format_t> surroundFormatsBackup(mManualSurroundFormats); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6238 | if (enabled) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6239 | mManualSurroundFormats.insert(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 6240 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6241 | mManualSurroundFormats.insert(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6242 | } |
| 6243 | } else { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6244 | mManualSurroundFormats.erase(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 6245 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6246 | mManualSurroundFormats.erase(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6247 | } |
| 6248 | } |
| 6249 | |
| 6250 | sp<SwAudioOutputDescriptor> outputDesc; |
| 6251 | bool profileUpdated = false; |
hongchao.yin | f0c8208 | 2024-07-24 19:41:02 +0800 | [diff] [blame] | 6252 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromTypes( |
| 6253 | {AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC}); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6254 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 6255 | // Simulate reconnection to update enabled surround sound formats. |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 6256 | String8 address = String8(hdmiOutputDevices[i]->address().c_str()); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 6257 | std::string name = hdmiOutputDevices[i]->getName(); |
hongchao.yin | f0c8208 | 2024-07-24 19:41:02 +0800 | [diff] [blame] | 6258 | status_t status = setDeviceConnectionStateInt(hdmiOutputDevices[i]->type(), |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6259 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 6260 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6261 | name.c_str(), |
| 6262 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6263 | if (status != NO_ERROR) { |
| 6264 | continue; |
| 6265 | } |
hongchao.yin | f0c8208 | 2024-07-24 19:41:02 +0800 | [diff] [blame] | 6266 | status = setDeviceConnectionStateInt(hdmiOutputDevices[i]->type(), |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6267 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 6268 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6269 | name.c_str(), |
| 6270 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6271 | profileUpdated |= (status == NO_ERROR); |
| 6272 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6273 | // 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] | 6274 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromType( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6275 | AUDIO_DEVICE_IN_HDMI); |
| 6276 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 6277 | // Simulate reconnection to update enabled surround sound formats. |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 6278 | String8 address = String8(hdmiInputDevices[i]->address().c_str()); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 6279 | std::string name = hdmiInputDevices[i]->getName(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6280 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 6281 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 6282 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6283 | name.c_str(), |
| 6284 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6285 | if (status != NO_ERROR) { |
| 6286 | continue; |
| 6287 | } |
| 6288 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 6289 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 6290 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6291 | name.c_str(), |
| 6292 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6293 | profileUpdated |= (status == NO_ERROR); |
| 6294 | } |
| 6295 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6296 | if (!profileUpdated) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 6297 | ALOGW("%s() no audio profiles updated, undoing surround formats change", __func__); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6298 | mManualSurroundFormats = std::move(surroundFormatsBackup); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6299 | } |
| 6300 | |
| 6301 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 6302 | } |
| 6303 | |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 6304 | void AudioPolicyManager::setAppState(audio_port_handle_t portId, app_state_t state) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 6305 | { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 6306 | ALOGV("%s(portId:%d, state:%d)", __func__, portId, state); |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 6307 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 6308 | mInputs.valueAt(i)->setAppState(portId, state); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 6309 | } |
| 6310 | } |
| 6311 | |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 6312 | bool AudioPolicyManager::isHapticPlaybackSupported() |
| 6313 | { |
| 6314 | for (const auto& hwModule : mHwModules) { |
| 6315 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 6316 | for (const auto &outProfile : outputProfiles) { |
| 6317 | struct audio_port audioPort; |
| 6318 | outProfile->toAudioPort(&audioPort); |
| 6319 | for (size_t i = 0; i < audioPort.num_channel_masks; i++) { |
| 6320 | if (audioPort.channel_masks[i] & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 6321 | return true; |
| 6322 | } |
| 6323 | } |
| 6324 | } |
| 6325 | } |
| 6326 | return false; |
| 6327 | } |
| 6328 | |
Carter Hsu | 325a8eb | 2022-01-19 19:56:51 +0800 | [diff] [blame] | 6329 | bool AudioPolicyManager::isUltrasoundSupported() |
| 6330 | { |
| 6331 | bool hasUltrasoundOutput = false; |
| 6332 | bool hasUltrasoundInput = false; |
| 6333 | for (const auto& hwModule : mHwModules) { |
| 6334 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 6335 | if (!hasUltrasoundOutput) { |
| 6336 | for (const auto &outProfile : outputProfiles) { |
| 6337 | if (outProfile->getFlags() & AUDIO_OUTPUT_FLAG_ULTRASOUND) { |
| 6338 | hasUltrasoundOutput = true; |
| 6339 | break; |
| 6340 | } |
| 6341 | } |
| 6342 | } |
| 6343 | |
| 6344 | const InputProfileCollection &inputProfiles = hwModule->getInputProfiles(); |
| 6345 | if (!hasUltrasoundInput) { |
| 6346 | for (const auto &inputProfile : inputProfiles) { |
| 6347 | if (inputProfile->getFlags() & AUDIO_INPUT_FLAG_ULTRASOUND) { |
| 6348 | hasUltrasoundInput = true; |
| 6349 | break; |
| 6350 | } |
| 6351 | } |
| 6352 | } |
| 6353 | |
| 6354 | if (hasUltrasoundOutput && hasUltrasoundInput) |
| 6355 | return true; |
| 6356 | } |
| 6357 | return false; |
| 6358 | } |
| 6359 | |
Atneya Nair | 698f5ef | 2022-12-15 16:15:09 -0800 | [diff] [blame] | 6360 | bool AudioPolicyManager::isHotwordStreamSupported(bool lookbackAudio) |
| 6361 | { |
| 6362 | const auto mask = AUDIO_INPUT_FLAG_HOTWORD_TAP | |
| 6363 | (lookbackAudio ? AUDIO_INPUT_FLAG_HW_LOOKBACK : 0); |
| 6364 | for (const auto& hwModule : mHwModules) { |
| 6365 | const InputProfileCollection &inputProfiles = hwModule->getInputProfiles(); |
| 6366 | for (const auto &inputProfile : inputProfiles) { |
| 6367 | if ((inputProfile->getFlags() & mask) == mask) { |
| 6368 | return true; |
| 6369 | } |
| 6370 | } |
| 6371 | } |
| 6372 | return false; |
| 6373 | } |
| 6374 | |
Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 6375 | bool AudioPolicyManager::isCallScreenModeSupported() |
| 6376 | { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6377 | return mConfig->isCallScreenModeSupported(); |
Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 6378 | } |
| 6379 | |
| 6380 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6381 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6382 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6383 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 6384 | if (!sourceDesc->isConnected()) { |
| 6385 | ALOGV("%s port Id %d already disconnected", __FUNCTION__, sourceDesc->portId()); |
| 6386 | return NO_ERROR; |
| 6387 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6388 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); |
| 6389 | if (swOutput != 0) { |
| 6390 | status_t status = stopSource(swOutput, sourceDesc); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 6391 | if (status == NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6392 | swOutput->stop(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 6393 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6394 | if (releaseOutput(sourceDesc->portId())) { |
| 6395 | // The output descriptor is reopened to query dynamic profiles. In that case, there is |
| 6396 | // no need to release audio patch here but just return NO_ERROR. |
| 6397 | return NO_ERROR; |
| 6398 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6399 | } else { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6400 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6401 | if (hwOutputDesc != 0) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6402 | // close Hwoutput and remove from mHwOutputs |
| 6403 | } else { |
| 6404 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 6405 | } |
| 6406 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6407 | status_t status = releaseAudioPatchInternal(sourceDesc->getPatchHandle(), 0, sourceDesc); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 6408 | sourceDesc->disconnect(); |
| 6409 | return status; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6410 | } |
| 6411 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6412 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForAttributesOnOutput( |
| 6413 | audio_io_handle_t output, const audio_attributes_t &attr) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6414 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6415 | sp<SourceClientDescriptor> source; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6416 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6417 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6418 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6419 | if (followsSameRouting(attr, sourceDesc->attributes()) && |
| 6420 | outputDesc != 0 && outputDesc->mIoHandle == output) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6421 | source = sourceDesc; |
| 6422 | break; |
| 6423 | } |
| 6424 | } |
| 6425 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 6426 | } |
| 6427 | |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 6428 | bool AudioPolicyManager::canBeSpatializedInt(const audio_attributes_t *attr, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6429 | const audio_config_t *config, |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 6430 | const AudioDeviceTypeAddrVector &devices) const |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6431 | { |
| 6432 | // The caller can have the audio attributes criteria ignored by either passing a null ptr or |
| 6433 | // the AUDIO_ATTRIBUTES_INITIALIZER value. |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 6434 | // 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] | 6435 | // and game usages. |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6436 | if (attr != nullptr && *attr != AUDIO_ATTRIBUTES_INITIALIZER) { |
| 6437 | if (attr->usage != AUDIO_USAGE_MEDIA && attr->usage != AUDIO_USAGE_GAME) { |
| 6438 | return false; |
| 6439 | } |
| 6440 | if ((attr->flags & (AUDIO_FLAG_CONTENT_SPATIALIZED | AUDIO_FLAG_NEVER_SPATIALIZE)) != 0) { |
| 6441 | return false; |
| 6442 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6443 | } |
| 6444 | |
Eric Laurent | d332bc8 | 2023-08-04 11:45:23 +0200 | [diff] [blame] | 6445 | // The caller can have the audio config criteria ignored by either passing a null ptr or |
| 6446 | // the AUDIO_CONFIG_INITIALIZER value. |
| 6447 | // 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] | 6448 | // some positional channel masks and PCM format and for stereo if low latency performance |
| 6449 | // mode is not requested. |
Eric Laurent | d332bc8 | 2023-08-04 11:45:23 +0200 | [diff] [blame] | 6450 | |
| 6451 | if (config != nullptr && *config != AUDIO_CONFIG_INITIALIZER) { |
Andy Hung | 481bfe3 | 2023-12-18 14:00:29 -0800 | [diff] [blame] | 6452 | const bool channel_mask_spatialized = |
Shunkai Yao | 2dcd60c | 2024-08-27 21:08:53 +0000 | [diff] [blame] | 6453 | SpatializerHelper::isStereoSpatializationFeatureEnabled() |
| 6454 | ? audio_channel_mask_contains_stereo(config->channel_mask) |
| 6455 | : audio_is_channel_mask_spatialized(config->channel_mask); |
Andy Hung | 481bfe3 | 2023-12-18 14:00:29 -0800 | [diff] [blame] | 6456 | if (!channel_mask_spatialized) { |
Eric Laurent | d332bc8 | 2023-08-04 11:45:23 +0200 | [diff] [blame] | 6457 | return false; |
| 6458 | } |
| 6459 | if (!audio_is_linear_pcm(config->format)) { |
| 6460 | return false; |
| 6461 | } |
Eric Laurent | f9230d5 | 2024-01-26 18:49:09 +0100 | [diff] [blame] | 6462 | if (config->channel_mask == AUDIO_CHANNEL_OUT_STEREO |
| 6463 | && ((attr->flags & AUDIO_FLAG_LOW_LATENCY) != 0)) { |
| 6464 | return false; |
| 6465 | } |
Eric Laurent | d332bc8 | 2023-08-04 11:45:23 +0200 | [diff] [blame] | 6466 | } |
| 6467 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6468 | sp<IOProfile> profile = |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6469 | getSpatializerOutputProfile(config, devices); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6470 | if (profile == nullptr) { |
| 6471 | return false; |
| 6472 | } |
| 6473 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6474 | return true; |
| 6475 | } |
| 6476 | |
Shunkai Yao | 4c3af93 | 2024-04-26 04:12:21 +0000 | [diff] [blame] | 6477 | // The Spatializer output is compatible with Haptic use cases if: |
| 6478 | // 1. the Spatializer output thread supports Haptic, and format/sampleRate are same |
| 6479 | // with client if client haptic channel bits were set, or |
| 6480 | // 2. the Spatializer output thread does not support Haptic, and client did not ask haptic by |
| 6481 | // including the haptic bits or creating the HapticGenerator effect for same session. |
| 6482 | bool AudioPolicyManager::checkHapticCompatibilityOnSpatializerOutput( |
| 6483 | const audio_config_t* config, audio_session_t sessionId) const { |
| 6484 | const auto clientHapticChannel = |
| 6485 | audio_channel_count_from_out_mask(config->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL); |
| 6486 | const auto threadOutputHapticChannel = audio_channel_count_from_out_mask( |
| 6487 | mSpatializerOutput->getChannelMask() & AUDIO_CHANNEL_HAPTIC_ALL); |
| 6488 | |
| 6489 | if (threadOutputHapticChannel) { |
| 6490 | // check format and sampleRate match if client haptic channel mask exist |
| 6491 | if (clientHapticChannel) { |
| 6492 | return mSpatializerOutput->getFormat() == config->format && |
| 6493 | mSpatializerOutput->getSamplingRate() == config->sample_rate; |
| 6494 | } |
| 6495 | return true; |
| 6496 | } else { |
| 6497 | // in the case of the Spatializer output channel mask does not have haptic channel bits, it |
| 6498 | // means haptic use cases (either the client channelmask includes haptic bits, or created a |
| 6499 | // HapticGenerator effect for this session) are not supported. |
| 6500 | return clientHapticChannel == 0 && |
Shunkai Yao | cb21feb | 2024-07-17 00:34:54 +0000 | [diff] [blame] | 6501 | !mEffects.hasOrphansForSession(sessionId, FX_IID_HAPTICGENERATOR); |
Shunkai Yao | 4c3af93 | 2024-04-26 04:12:21 +0000 | [diff] [blame] | 6502 | } |
| 6503 | } |
| 6504 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6505 | void AudioPolicyManager::checkVirtualizerClientRoutes() { |
| 6506 | std::set<audio_stream_type_t> streamsToInvalidate; |
| 6507 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6508 | const sp<SwAudioOutputDescriptor>& desc = mOutputs[i]; |
| 6509 | for (const sp<TrackClientDescriptor>& client : desc->getClientIterable()) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6510 | audio_attributes_t attr = client->attributes(); |
| 6511 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false); |
| 6512 | AudioDeviceTypeAddrVector devicesTypeAddress = devices.toTypeAddrVector(); |
| 6513 | audio_config_base_t clientConfig = client->config(); |
| 6514 | audio_config_t config = audio_config_initializer(&clientConfig); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6515 | if (desc != mSpatializerOutput |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 6516 | && canBeSpatializedInt(&attr, &config, devicesTypeAddress)) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6517 | streamsToInvalidate.insert(client->stream()); |
| 6518 | } |
| 6519 | } |
| 6520 | } |
| 6521 | |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 6522 | invalidateStreams(StreamTypeVector(streamsToInvalidate.begin(), streamsToInvalidate.end())); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6523 | } |
| 6524 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6525 | |
| 6526 | bool AudioPolicyManager::isOutputOnlyAvailableRouteToSomeDevice( |
| 6527 | const sp<SwAudioOutputDescriptor>& outputDesc) { |
| 6528 | if (outputDesc->isDuplicated()) { |
| 6529 | return false; |
| 6530 | } |
| 6531 | DeviceVector devices = outputDesc->supportedDevices(); |
| 6532 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6533 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 6534 | if (desc == outputDesc || desc->isDuplicated()) { |
| 6535 | continue; |
| 6536 | } |
| 6537 | DeviceVector sharedDevices = desc->filterSupportedDevices(devices); |
| 6538 | if (!sharedDevices.isEmpty() |
| 6539 | && (desc->devicesSupportEncodedFormats(sharedDevices.types()) |
| 6540 | == outputDesc->devicesSupportEncodedFormats(sharedDevices.types()))) { |
| 6541 | return false; |
| 6542 | } |
| 6543 | } |
| 6544 | return true; |
| 6545 | } |
| 6546 | |
| 6547 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 6548 | status_t AudioPolicyManager::getSpatializerOutput(const audio_config_base_t *mixerConfig, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6549 | const audio_attributes_t *attr, |
| 6550 | audio_io_handle_t *output) { |
| 6551 | *output = AUDIO_IO_HANDLE_NONE; |
| 6552 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6553 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(*attr, nullptr, false); |
| 6554 | AudioDeviceTypeAddrVector devicesTypeAddress = devices.toTypeAddrVector(); |
| 6555 | audio_config_t *configPtr = nullptr; |
| 6556 | audio_config_t config; |
| 6557 | if (mixerConfig != nullptr) { |
| 6558 | config = audio_config_initializer(mixerConfig); |
| 6559 | configPtr = &config; |
| 6560 | } |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 6561 | if (!canBeSpatializedInt(attr, configPtr, devicesTypeAddress)) { |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6562 | ALOGV("%s provided attributes or mixer config cannot be spatialized", __func__); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6563 | return BAD_VALUE; |
| 6564 | } |
| 6565 | |
| 6566 | sp<IOProfile> profile = |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6567 | getSpatializerOutputProfile(configPtr, devicesTypeAddress); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6568 | if (profile == nullptr) { |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6569 | 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] | 6570 | return BAD_VALUE; |
| 6571 | } |
| 6572 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6573 | std::vector<sp<SwAudioOutputDescriptor>> spatializerOutputs; |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6574 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6575 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6576 | if (!desc->isDuplicated() |
| 6577 | && (desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) { |
| 6578 | spatializerOutputs.push_back(desc); |
| 6579 | ALOGV("%s adding opened spatializer Output %d", __func__, desc->mIoHandle); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6580 | } |
| 6581 | } |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6582 | mSpatializerOutput.clear(); |
| 6583 | bool outputsChanged = false; |
| 6584 | for (const auto& desc : spatializerOutputs) { |
| 6585 | if (desc->mProfile == profile |
| 6586 | && (configPtr == nullptr |
| 6587 | || configPtr->channel_mask == desc->mMixerChannelMask)) { |
| 6588 | mSpatializerOutput = desc; |
| 6589 | ALOGV("%s reusing current spatializer output %d", __func__, desc->mIoHandle); |
| 6590 | } else { |
| 6591 | ALOGV("%s closing spatializerOutput output %d to match channel mask %#x" |
| 6592 | " and devices %s", __func__, desc->mIoHandle, |
| 6593 | configPtr != nullptr ? configPtr->channel_mask : 0, |
| 6594 | devices.toString().c_str()); |
| 6595 | closeOutput(desc->mIoHandle); |
| 6596 | outputsChanged = true; |
| 6597 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6598 | } |
| 6599 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6600 | if (mSpatializerOutput == nullptr) { |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 6601 | sp<SwAudioOutputDescriptor> desc = |
| 6602 | openOutputWithProfileAndDevice(profile, devices, mixerConfig); |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6603 | if (desc != nullptr) { |
| 6604 | mSpatializerOutput = desc; |
| 6605 | outputsChanged = true; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6606 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6607 | } |
| 6608 | |
| 6609 | checkVirtualizerClientRoutes(); |
| 6610 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6611 | if (outputsChanged) { |
| 6612 | mPreviousOutputs = mOutputs; |
| 6613 | mpClientInterface->onAudioPortListUpdate(); |
| 6614 | } |
| 6615 | |
| 6616 | if (mSpatializerOutput == nullptr) { |
| 6617 | ALOGV("%s could not open spatializer output with requested config", __func__); |
| 6618 | return BAD_VALUE; |
| 6619 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6620 | *output = mSpatializerOutput->mIoHandle; |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6621 | ALOGV("%s returning new spatializer output %d", __func__, *output); |
| 6622 | return OK; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6623 | } |
| 6624 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 6625 | status_t AudioPolicyManager::releaseSpatializerOutput(audio_io_handle_t output) { |
| 6626 | if (mSpatializerOutput == nullptr) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6627 | return INVALID_OPERATION; |
| 6628 | } |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 6629 | if (mSpatializerOutput->mIoHandle != output) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6630 | return BAD_VALUE; |
| 6631 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6632 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6633 | if (!isOutputOnlyAvailableRouteToSomeDevice(mSpatializerOutput)) { |
| 6634 | ALOGV("%s closing spatializer output %d", __func__, mSpatializerOutput->mIoHandle); |
| 6635 | closeOutput(mSpatializerOutput->mIoHandle); |
| 6636 | //from now on mSpatializerOutput is null |
| 6637 | checkVirtualizerClientRoutes(); |
| 6638 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6639 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 6640 | return NO_ERROR; |
| 6641 | } |
| 6642 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6643 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6644 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6645 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6646 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 6647 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 6648 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6649 | } |
| 6650 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6651 | AudioPolicyManager::AudioPolicyManager(const sp<const AudioPolicyConfig>& config, |
Mikhail Naganov | f1b6d97 | 2023-05-02 13:56:01 -0700 | [diff] [blame] | 6652 | EngineInstance&& engine, |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6653 | AudioPolicyClientInterface *clientInterface) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6654 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 6655 | 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] | 6656 | mConfig(config), |
Mikhail Naganov | f1b6d97 | 2023-05-02 13:56:01 -0700 | [diff] [blame] | 6657 | mEngine(std::move(engine)), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6658 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6659 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6660 | mA2dpSuspended(false), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6661 | mAudioPortGeneration(1), |
| 6662 | mBeaconMuteRefCount(0), |
| 6663 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 6664 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 6665 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6666 | mMasterMono(false), |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 6667 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6668 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6669 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 6670 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6671 | status_t AudioPolicyManager::initialize() { |
Mikhail Naganov | f1b6d97 | 2023-05-02 13:56:01 -0700 | [diff] [blame] | 6672 | if (mEngine == nullptr) { |
| 6673 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6674 | } |
| 6675 | mEngine->setObserver(this); |
| 6676 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6677 | if (status != NO_ERROR) { |
| 6678 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 6679 | return status; |
| 6680 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6681 | |
jiabin | 2923018 | 2023-04-04 21:02:36 +0000 | [diff] [blame] | 6682 | // The actual device selection cache will be updated when calling `updateDevicesAndOutputs` |
| 6683 | // at the end of this function. |
| 6684 | mEngine->initializeDeviceSelectionCache(); |
Eric Laurent | 1d69c87 | 2021-01-11 18:53:01 +0100 | [diff] [blame] | 6685 | mCommunnicationStrategy = mEngine->getProductStrategyForAttributes( |
| 6686 | mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL)); |
| 6687 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6688 | // after parsing the config, mConfig contain all known devices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6689 | // open all output streams needed to access attached devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6690 | onNewAudioModulesAvailableInt(nullptr /*newDevices*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6691 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6692 | // make sure default device is reachable |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6693 | if (const auto defaultOutputDevice = mConfig->getDefaultOutputDevice(); |
| 6694 | defaultOutputDevice == nullptr || |
| 6695 | !mAvailableOutputDevices.contains(defaultOutputDevice)) { |
| 6696 | ALOGE_IF(defaultOutputDevice != nullptr, "Default device %s is unreachable", |
| 6697 | defaultOutputDevice->toString().c_str()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6698 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6699 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 6700 | 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] | 6701 | |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 6702 | // Silence ALOGV statements |
| 6703 | property_set("log.tag." LOG_TAG, "D"); |
| 6704 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6705 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6706 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6707 | } |
| 6708 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6709 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6710 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6711 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6712 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6713 | } |
| 6714 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6715 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6716 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6717 | mAvailableOutputDevices.clear(); |
| 6718 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6719 | mOutputs.clear(); |
| 6720 | mInputs.clear(); |
| 6721 | mHwModules.clear(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6722 | mManualSurroundFormats.clear(); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6723 | mConfig.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6724 | } |
| 6725 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6726 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6727 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 6728 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6729 | } |
| 6730 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6731 | // --- |
| 6732 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6733 | void AudioPolicyManager::onNewAudioModulesAvailable() |
| 6734 | { |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6735 | DeviceVector newDevices; |
| 6736 | onNewAudioModulesAvailableInt(&newDevices); |
| 6737 | if (!newDevices.empty()) { |
| 6738 | nextAudioPortGeneration(); |
| 6739 | mpClientInterface->onAudioPortListUpdate(); |
| 6740 | } |
| 6741 | } |
| 6742 | |
| 6743 | void AudioPolicyManager::onNewAudioModulesAvailableInt(DeviceVector *newDevices) |
| 6744 | { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6745 | for (const auto& hwModule : mConfig->getHwModules()) { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6746 | if (std::find(mHwModules.begin(), mHwModules.end(), hwModule) != mHwModules.end()) { |
| 6747 | continue; |
| 6748 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6749 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
Mikhail Naganov | ffd9771 | 2023-05-03 17:45:36 -0700 | [diff] [blame] | 6750 | if (audio_module_handle_t handle = mpClientInterface->loadHwModule(hwModule->getName()); |
| 6751 | handle != AUDIO_MODULE_HANDLE_NONE) { |
| 6752 | hwModule->setHandle(handle); |
| 6753 | } else { |
| 6754 | ALOGW("could not load HW module %s", hwModule->getName()); |
| 6755 | continue; |
| 6756 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6757 | } |
| 6758 | mHwModules.push_back(hwModule); |
Dean Wheatley | 12a8713 | 2021-04-16 10:08:49 +1000 | [diff] [blame] | 6759 | // open all output streams needed to access attached devices. |
| 6760 | // direct outputs are closed immediately after checking the availability of attached devices |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6761 | // This also validates mAvailableOutputDevices list |
| 6762 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
| 6763 | if (!outProfile->canOpenNewIo()) { |
| 6764 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 6765 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 6766 | continue; |
| 6767 | } |
| 6768 | if (!outProfile->hasSupportedDevices()) { |
| 6769 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
| 6770 | continue; |
| 6771 | } |
Carter Hsu | 1a3364a | 2022-01-21 15:32:56 +0800 | [diff] [blame] | 6772 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0 || |
| 6773 | (outProfile->getFlags() & AUDIO_OUTPUT_FLAG_ULTRASOUND) != 0) { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6774 | mTtsOutputAvailable = true; |
| 6775 | } |
| 6776 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6777 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6778 | DeviceVector availProfileDevices = supportedDevices.filter(mConfig->getOutputDevices()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6779 | sp<DeviceDescriptor> supportedDevice = 0; |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6780 | if (supportedDevices.contains(mConfig->getDefaultOutputDevice())) { |
| 6781 | supportedDevice = mConfig->getDefaultOutputDevice(); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6782 | } else { |
| 6783 | // choose first device present in profile's SupportedDevices also part of |
| 6784 | // mAvailableOutputDevices. |
| 6785 | if (availProfileDevices.isEmpty()) { |
| 6786 | continue; |
| 6787 | } |
| 6788 | supportedDevice = availProfileDevices.itemAt(0); |
| 6789 | } |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6790 | if (!mConfig->getOutputDevices().contains(supportedDevice)) { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6791 | continue; |
| 6792 | } |
Jaideep Sharma | 2cfa7ef | 2024-06-18 16:32:34 +0530 | [diff] [blame] | 6793 | |
| 6794 | if (outProfile->isMmap() && !outProfile->hasDynamicAudioProfile() |
| 6795 | && availProfileDevices.areAllDevicesAttached()) { |
| 6796 | ALOGV("%s skip opening output for mmap profile %s", __func__, |
| 6797 | outProfile->getTagName().c_str()); |
| 6798 | continue; |
| 6799 | } |
| 6800 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6801 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 6802 | mpClientInterface); |
| 6803 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Dean Wheatley | dfb67b8 | 2024-01-23 09:36:29 +1100 | [diff] [blame] | 6804 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
Haofan Wang | f6e304f | 2024-07-09 23:06:58 -0700 | [diff] [blame] | 6805 | audio_attributes_t attributes = AUDIO_ATTRIBUTES_INITIALIZER; |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 6806 | status_t status = outputDesc->open(nullptr /* halConfig */, nullptr /* mixerConfig */, |
| 6807 | DeviceVector(supportedDevice), |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6808 | AUDIO_STREAM_DEFAULT, |
Dean Wheatley | dfb67b8 | 2024-01-23 09:36:29 +1100 | [diff] [blame] | 6809 | &flags, &output, attributes); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6810 | if (status != NO_ERROR) { |
| 6811 | ALOGW("Cannot open output stream for devices %s on hw module %s", |
| 6812 | supportedDevice->toString().c_str(), hwModule->getName()); |
| 6813 | continue; |
| 6814 | } |
| 6815 | for (const auto &device : availProfileDevices) { |
| 6816 | // give a valid ID to an attached device once confirmed it is reachable |
| 6817 | if (!device->isAttached()) { |
| 6818 | device->attach(hwModule); |
| 6819 | mAvailableOutputDevices.add(device); |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 6820 | device->setEncapsulationInfoFromHal(mpClientInterface); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6821 | if (newDevices) newDevices->add(device); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6822 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 6823 | } |
| 6824 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 6825 | if (mPrimaryOutput == nullptr && |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6826 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 6827 | mPrimaryOutput = outputDesc; |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 6828 | mPrimaryModuleHandle = mPrimaryOutput->getModuleHandle(); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6829 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6830 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6831 | outputDesc->close(); |
| 6832 | } else { |
| 6833 | addOutput(output, outputDesc); |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 6834 | setOutputDevices(__func__, outputDesc, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6835 | DeviceVector(supportedDevice), |
| 6836 | true, |
| 6837 | 0, |
| 6838 | NULL); |
| 6839 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6840 | } |
| 6841 | // open input streams needed to access attached devices to validate |
| 6842 | // mAvailableInputDevices list |
| 6843 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
| 6844 | if (!inProfile->canOpenNewIo()) { |
| 6845 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 6846 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 6847 | continue; |
| 6848 | } |
| 6849 | if (!inProfile->hasSupportedDevices()) { |
| 6850 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
| 6851 | continue; |
| 6852 | } |
| 6853 | // chose first device present in profile's SupportedDevices also part of |
| 6854 | // available input devices |
| 6855 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6856 | DeviceVector availProfileDevices = supportedDevices.filter(mConfig->getInputDevices()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6857 | if (availProfileDevices.isEmpty()) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 6858 | ALOGV("%s: Input device list is empty! for profile %s", |
| 6859 | __func__, inProfile->getTagName().c_str()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6860 | continue; |
| 6861 | } |
Jaideep Sharma | 2cfa7ef | 2024-06-18 16:32:34 +0530 | [diff] [blame] | 6862 | |
| 6863 | if (inProfile->isMmap() && !inProfile->hasDynamicAudioProfile() |
| 6864 | && availProfileDevices.areAllDevicesAttached()) { |
| 6865 | ALOGV("%s skip opening input for mmap profile %s", __func__, |
| 6866 | inProfile->getTagName().c_str()); |
| 6867 | continue; |
| 6868 | } |
| 6869 | |
Eric Laurent | c71b11b | 2024-06-03 12:54:53 +0000 | [diff] [blame] | 6870 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor( |
| 6871 | inProfile, mpClientInterface, false /*isPreemptor*/); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6872 | |
| 6873 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 6874 | status_t status = inputDesc->open(nullptr, |
| 6875 | availProfileDevices.itemAt(0), |
| 6876 | AUDIO_SOURCE_MIC, |
Jaideep Sharma | 26e31c2 | 2024-06-18 14:12:50 +0530 | [diff] [blame] | 6877 | (audio_input_flags_t) inProfile->getFlags(), |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6878 | &input); |
| 6879 | if (status != NO_ERROR) { |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 6880 | ALOGW("%s: Cannot open input stream for device %s for profile %s on hw module %s", |
| 6881 | __func__, availProfileDevices.toString().c_str(), |
| 6882 | inProfile->getTagName().c_str(), hwModule->getName()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6883 | continue; |
| 6884 | } |
| 6885 | for (const auto &device : availProfileDevices) { |
| 6886 | // give a valid ID to an attached device once confirmed it is reachable |
| 6887 | if (!device->isAttached()) { |
| 6888 | device->attach(hwModule); |
| 6889 | device->importAudioPortAndPickAudioProfile(inProfile, true); |
| 6890 | mAvailableInputDevices.add(device); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6891 | if (newDevices) newDevices->add(device); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6892 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 6893 | } |
| 6894 | } |
| 6895 | inputDesc->close(); |
| 6896 | } |
| 6897 | } |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6898 | |
| 6899 | // Check if spatializer outputs can be closed until used. |
| 6900 | // mOutputs vector never contains duplicated outputs at this point. |
| 6901 | std::vector<audio_io_handle_t> outputsClosed; |
| 6902 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6903 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 6904 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0 |
| 6905 | && !isOutputOnlyAvailableRouteToSomeDevice(desc)) { |
| 6906 | outputsClosed.push_back(desc->mIoHandle); |
Eric Laurent | a70bc37 | 2024-04-30 02:10:04 +0000 | [diff] [blame] | 6907 | nextAudioPortGeneration(); |
| 6908 | ssize_t index = mAudioPatches.indexOfKey(desc->getPatchHandle()); |
| 6909 | if (index >= 0) { |
| 6910 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 6911 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 6912 | patchDesc->getAfHandle(), 0); |
| 6913 | mAudioPatches.removeItemsAt(index); |
| 6914 | mpClientInterface->onAudioPatchListUpdate(); |
| 6915 | } |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6916 | desc->close(); |
| 6917 | } |
| 6918 | } |
| 6919 | for (auto output : outputsClosed) { |
| 6920 | removeOutput(output); |
| 6921 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6922 | } |
| 6923 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 6924 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 6925 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6926 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6927 | mOutputs.add(output, outputDesc); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6928 | applyStreamVolumes(outputDesc, DeviceTypeSet(), 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 6929 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6930 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6931 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6932 | } |
| 6933 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6934 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 6935 | { |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 6936 | if (mPrimaryOutput != 0 && mPrimaryOutput == mOutputs.valueFor(output)) { |
| 6937 | ALOGV("%s: removing primary output", __func__); |
| 6938 | mPrimaryOutput = nullptr; |
| 6939 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6940 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6941 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6942 | } |
| 6943 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 6944 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 6945 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6946 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6947 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6948 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6949 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6950 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6951 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6952 | audio_policy_dev_state_t state, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6953 | SortedVector<audio_io_handle_t>& outputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6954 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6955 | audio_devices_t deviceType = device->type(); |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 6956 | const String8 &address = String8(device->address().c_str()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6957 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6958 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6959 | if (audio_device_is_digital(deviceType)) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6960 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6961 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6962 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6963 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 6964 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 6965 | // first call getAudioPort to get the supported attributes from the HAL |
| 6966 | struct audio_port_v7 port = {}; |
| 6967 | device->toAudioPort(&port); |
| 6968 | status_t status = mpClientInterface->getAudioPort(&port); |
| 6969 | if (status == NO_ERROR) { |
| 6970 | device->importAudioPort(port); |
| 6971 | } |
| 6972 | |
| 6973 | // then list already open outputs that can be routed to this device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6974 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6975 | desc = mOutputs.valueAt(i); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6976 | if (!desc->isDuplicated() && desc->supportsDevice(device) |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6977 | && desc->devicesSupportEncodedFormats({deviceType})) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6978 | ALOGV("checkOutputsForDevice(): adding opened output %d on device %s", |
| 6979 | mOutputs.keyAt(i), device->toString().c_str()); |
| 6980 | outputs.add(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6981 | } |
| 6982 | } |
| 6983 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6984 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6985 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6986 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 6987 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6988 | if (profile->supportsDevice(device)) { |
| 6989 | profiles.add(profile); |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 6990 | ALOGV("%s(): adding profile %s from module %s", |
| 6991 | __func__, profile->getTagName().c_str(), hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6992 | } |
| 6993 | } |
| 6994 | } |
| 6995 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 6996 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6997 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6998 | if (profiles.isEmpty() && outputs.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6999 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7000 | return BAD_VALUE; |
| 7001 | } |
| 7002 | |
| 7003 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 7004 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 7005 | 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] | 7006 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7007 | |
| 7008 | // nothing to do if one output is already opened for this profile |
| 7009 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 7010 | for (j = 0; j < outputs.size(); j++) { |
| 7011 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7012 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 7013 | // matching profile: save the sample rates, format and channel masks supported |
| 7014 | // by the profile in our device descriptor |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7015 | if (audio_device_is_digital(deviceType)) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 7016 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 7017 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7018 | break; |
| 7019 | } |
| 7020 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 7021 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7022 | continue; |
| 7023 | } |
Jaideep Sharma | 2cfa7ef | 2024-06-18 16:32:34 +0530 | [diff] [blame] | 7024 | if (profile->isMmap() && !profile->hasDynamicAudioProfile()) { |
| 7025 | ALOGV("%s skip opening output for mmap profile %s", |
| 7026 | __func__, profile->getTagName().c_str()); |
| 7027 | continue; |
| 7028 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 7029 | if (!profile->canOpenNewIo()) { |
| 7030 | ALOGW("Max Output number %u already opened for this profile %s", |
| 7031 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 7032 | continue; |
| 7033 | } |
| 7034 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 7035 | 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] | 7036 | deviceType, address.c_str(), profile.get(), profile->getName().c_str()); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7037 | desc = openOutputWithProfileAndDevice(profile, DeviceVector(device)); |
| 7038 | 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] | 7039 | if (output == AUDIO_IO_HANDLE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7040 | ALOGW("checkOutputsForDevice() could not open output for device %x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7041 | profiles.removeAt(profile_index); |
| 7042 | profile_index--; |
| 7043 | } else { |
| 7044 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 7045 | // Load digital format info only for digital devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7046 | if (audio_device_is_digital(deviceType)) { |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7047 | // TODO: when getAudioPort is ready, it may not be needed to import the audio |
| 7048 | // port but just pick audio profile |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 7049 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 7050 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 7051 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7052 | if (device_distinguishes_on_address(deviceType)) { |
| 7053 | ALOGV("checkOutputsForDevice(): setOutputDevices %s", |
| 7054 | device->toString().c_str()); |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 7055 | setOutputDevices(__func__, desc, DeviceVector(device), true/*force*/, |
| 7056 | 0/*delay*/, NULL/*patch handle*/); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 7057 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7058 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 7059 | } |
| 7060 | } |
| 7061 | |
| 7062 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7063 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7064 | return BAD_VALUE; |
| 7065 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7066 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7067 | // check if one opened output is not needed any more after disconnecting one device |
| 7068 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 7069 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 7070 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7071 | // exact match on device |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 7072 | if (device_distinguishes_on_address(deviceType) && desc->supportsDevice(device) |
Francois Gaffie | c7d4c22 | 2021-12-02 11:12:52 +0100 | [diff] [blame] | 7073 | && desc->containsSingleDeviceSupportingEncodedFormats(device)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7074 | outputs.add(mOutputs.keyAt(i)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7075 | } else if (!mAvailableOutputDevices.containsAtLeastOne(desc->supportedDevices())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 7076 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 7077 | mOutputs.keyAt(i)); |
| 7078 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 7079 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7080 | } |
| 7081 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7082 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 7083 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 7084 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 7085 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7086 | if (!profile->supportsDevice(device)) { |
| 7087 | continue; |
| 7088 | } |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 7089 | ALOGV("%s(): clearing direct output profile %s on module %s", |
| 7090 | __func__, profile->getTagName().c_str(), hwModule->getName()); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7091 | profile->clearAudioProfiles(); |
| 7092 | if (!profile->hasDynamicAudioProfile()) { |
| 7093 | continue; |
| 7094 | } |
| 7095 | // When a device is disconnected, if there is an IOProfile that contains dynamic |
| 7096 | // profiles and supports the disconnected device, call getAudioPort to repopulate |
| 7097 | // the capabilities of the devices that is supported by the IOProfile. |
| 7098 | for (const auto& supportedDevice : profile->getSupportedDevices()) { |
| 7099 | if (supportedDevice == device || |
| 7100 | !mAvailableOutputDevices.contains(supportedDevice)) { |
| 7101 | continue; |
| 7102 | } |
| 7103 | struct audio_port_v7 port; |
| 7104 | supportedDevice->toAudioPort(&port); |
| 7105 | status_t status = mpClientInterface->getAudioPort(&port); |
| 7106 | if (status == NO_ERROR) { |
| 7107 | supportedDevice->importAudioPort(port); |
| 7108 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7109 | } |
| 7110 | } |
| 7111 | } |
| 7112 | } |
| 7113 | return NO_ERROR; |
| 7114 | } |
| 7115 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7116 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& device, |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 7117 | audio_policy_dev_state_t state) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7118 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7119 | if (audio_device_is_digital(device->type())) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 7120 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7121 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 7122 | } |
| 7123 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7124 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Mikhail Naganov | c66ffc1 | 2024-05-30 16:56:25 -0700 | [diff] [blame] | 7125 | sp<AudioInputDescriptor> desc; |
| 7126 | |
jiabin | bf5f426 | 2023-04-12 21:48:34 +0000 | [diff] [blame] | 7127 | // first call getAudioPort to get the supported attributes from the HAL |
| 7128 | struct audio_port_v7 port = {}; |
| 7129 | device->toAudioPort(&port); |
| 7130 | status_t status = mpClientInterface->getAudioPort(&port); |
| 7131 | if (status == NO_ERROR) { |
| 7132 | device->importAudioPort(port); |
| 7133 | } |
| 7134 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 7135 | // look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7136 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 7137 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7138 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 7139 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 7140 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 7141 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7142 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7143 | if (profile->supportsDevice(device)) { |
| 7144 | profiles.add(profile); |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 7145 | ALOGV("%s : adding profile %s from module %s", __func__, |
| 7146 | profile->getTagName().c_str(), hwModule->getName()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7147 | } |
| 7148 | } |
| 7149 | } |
| 7150 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 7151 | if (profiles.isEmpty()) { |
| 7152 | ALOGW("%s: No input profile available for device %s", |
| 7153 | __func__, device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7154 | return BAD_VALUE; |
| 7155 | } |
| 7156 | |
| 7157 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 7158 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 7159 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 7160 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7161 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 7162 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7163 | // nothing to do if one input is already opened for this profile |
| 7164 | size_t input_index; |
| 7165 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 7166 | desc = mInputs.valueAt(input_index); |
| 7167 | if (desc->mProfile == profile) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7168 | if (audio_device_is_digital(device->type())) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 7169 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 7170 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7171 | break; |
| 7172 | } |
| 7173 | } |
| 7174 | if (input_index != mInputs.size()) { |
| 7175 | continue; |
| 7176 | } |
| 7177 | |
Jaideep Sharma | 2cfa7ef | 2024-06-18 16:32:34 +0530 | [diff] [blame] | 7178 | if (profile->isMmap() && !profile->hasDynamicAudioProfile()) { |
| 7179 | ALOGV("%s skip opening input for mmap profile %s", |
| 7180 | __func__, profile->getTagName().c_str()); |
| 7181 | continue; |
| 7182 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 7183 | if (!profile->canOpenNewIo()) { |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 7184 | ALOGW("%s Max Input number %u already opened for this profile %s", |
| 7185 | __func__, profile->maxOpenCount, profile->getTagName().c_str()); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 7186 | continue; |
| 7187 | } |
| 7188 | |
Eric Laurent | c71b11b | 2024-06-03 12:54:53 +0000 | [diff] [blame] | 7189 | desc = new AudioInputDescriptor(profile, mpClientInterface, false /*isPreemptor*/); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 7190 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 7191 | ALOGV("%s opening input for profile %s", __func__, profile->getTagName().c_str()); |
Jaideep Sharma | 26e31c2 | 2024-06-18 14:12:50 +0530 | [diff] [blame] | 7192 | status = desc->open(nullptr, device, AUDIO_SOURCE_MIC, |
| 7193 | (audio_input_flags_t) profile->getFlags(), &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7194 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 7195 | if (status == NO_ERROR) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 7196 | const String8& address = String8(device->address().c_str()); |
Tomasz Wasilczyk | fd9ffd1 | 2023-08-14 17:56:22 +0000 | [diff] [blame] | 7197 | if (!address.empty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7198 | char *param = audio_device_address_to_parameter(device->type(), address); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 7199 | mpClientInterface->setParameters(input, String8(param)); |
| 7200 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7201 | } |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 7202 | updateAudioProfiles(device, input, profile); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7203 | if (!profile->hasValidAudioProfile()) { |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 7204 | ALOGW("%s direct input missing param for profile %s", __func__, |
| 7205 | profile->getTagName().c_str()); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 7206 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 7207 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7208 | } |
| 7209 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 7210 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7211 | addInput(input, desc); |
| 7212 | } |
| 7213 | } // endif input != 0 |
| 7214 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 7215 | if (input == AUDIO_IO_HANDLE_NONE) { |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 7216 | ALOGW("%s could not open input for device %s on profile %s", __func__, |
| 7217 | device->toString().c_str(), profile->getTagName().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7218 | profiles.removeAt(profile_index); |
| 7219 | profile_index--; |
| 7220 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7221 | if (audio_device_is_digital(device->type())) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 7222 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 7223 | } |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 7224 | ALOGV("%s: adding input %d for profile %s", __func__, |
| 7225 | input, profile->getTagName().c_str()); |
Mikhail Naganov | c66ffc1 | 2024-05-30 16:56:25 -0700 | [diff] [blame] | 7226 | |
| 7227 | if (checkCloseInput(desc)) { |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 7228 | ALOGV("%s: closing input %d for profile %s", __func__, |
| 7229 | input, profile->getTagName().c_str()); |
Mikhail Naganov | c66ffc1 | 2024-05-30 16:56:25 -0700 | [diff] [blame] | 7230 | closeInput(input); |
| 7231 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7232 | } |
| 7233 | } // end scan profiles |
| 7234 | |
| 7235 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7236 | 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] | 7237 | return BAD_VALUE; |
| 7238 | } |
| 7239 | } else { |
| 7240 | // Disconnect |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7241 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 7242 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7243 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 7244 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7245 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 7246 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7247 | if (profile->supportsDevice(device)) { |
Jaideep Sharma | 3317320 | 2024-06-18 17:46:45 +0530 | [diff] [blame] | 7248 | ALOGV("%s: clearing direct input profile %s on module %s", __func__, |
| 7249 | profile->getTagName().c_str(), hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7250 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7251 | } |
| 7252 | } |
| 7253 | } |
| 7254 | } // end disconnect |
| 7255 | |
| 7256 | return NO_ERROR; |
| 7257 | } |
| 7258 | |
| 7259 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7260 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7261 | { |
| 7262 | ALOGV("closeOutput(%d)", output); |
| 7263 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 7264 | sp<SwAudioOutputDescriptor> closingOutput = mOutputs.valueFor(output); |
| 7265 | if (closingOutput == NULL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7266 | ALOGW("closeOutput() unknown output %d", output); |
| 7267 | return; |
| 7268 | } |
Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 7269 | const bool closingOutputWasActive = closingOutput->isActive(); |
jiabin | 24ff57a | 2023-11-27 21:06:51 +0000 | [diff] [blame] | 7270 | mPolicyMixes.closeOutput(closingOutput, mOutputs); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7271 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7272 | // look for duplicated outputs connected to the output being removed. |
| 7273 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 7274 | sp<SwAudioOutputDescriptor> dupOutput = mOutputs.valueAt(i); |
| 7275 | if (dupOutput->isDuplicated() && |
| 7276 | (dupOutput->mOutput1 == closingOutput || dupOutput->mOutput2 == closingOutput)) { |
| 7277 | sp<SwAudioOutputDescriptor> remainingOutput = |
| 7278 | dupOutput->mOutput1 == closingOutput ? dupOutput->mOutput2 : dupOutput->mOutput1; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7279 | // As all active tracks on duplicated output will be deleted, |
| 7280 | // and as they were also referenced on the other output, the reference |
| 7281 | // count for their stream type must be adjusted accordingly on |
| 7282 | // the other output. |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 7283 | const bool wasActive = remainingOutput->isActive(); |
| 7284 | // Note: no-op on the closing output where all clients has already been set inactive |
| 7285 | dupOutput->setAllClientsInactive(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 7286 | // stop() will be a no op if the output is still active but is needed in case all |
| 7287 | // active streams refcounts where cleared above |
| 7288 | if (wasActive) { |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 7289 | remainingOutput->stop(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 7290 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7291 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 7292 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 7293 | |
| 7294 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7295 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7296 | } |
| 7297 | } |
| 7298 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 7299 | nextAudioPortGeneration(); |
| 7300 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 7301 | ssize_t index = mAudioPatches.indexOfKey(closingOutput->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 7302 | if (index >= 0) { |
| 7303 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7304 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 7305 | patchDesc->getAfHandle(), 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 7306 | mAudioPatches.removeItemsAt(index); |
| 7307 | mpClientInterface->onAudioPatchListUpdate(); |
| 7308 | } |
| 7309 | |
Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 7310 | if (closingOutputWasActive) { |
| 7311 | closingOutput->stop(); |
| 7312 | } |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 7313 | closingOutput->close(); |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 7314 | if (closingOutput->isBitPerfect()) { |
jiabin | 14b50cc | 2023-12-13 19:01:52 +0000 | [diff] [blame] | 7315 | for (const auto device : closingOutput->devices()) { |
| 7316 | device->setPreferredConfig(nullptr); |
| 7317 | } |
| 7318 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7319 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7320 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7321 | mPreviousOutputs = mOutputs; |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 7322 | if (closingOutput == mSpatializerOutput) { |
| 7323 | mSpatializerOutput.clear(); |
| 7324 | } |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 7325 | |
| 7326 | // MSD patches may have been released to support a non-MSD direct output. Reset MSD patch if |
| 7327 | // no direct outputs are open. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7328 | if (!getMsdAudioOutDevices().isEmpty()) { |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 7329 | bool directOutputOpen = false; |
| 7330 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 7331 | if (mOutputs[i]->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 7332 | directOutputOpen = true; |
| 7333 | break; |
| 7334 | } |
| 7335 | } |
| 7336 | if (!directOutputOpen) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 7337 | ALOGV("no direct outputs open, reset MSD patches"); |
| 7338 | // TODO: The MSD patches to be established here may differ to current MSD patches due to |
| 7339 | // how output devices for patching are resolved. Avoid by caching and reusing the |
| 7340 | // arguments to mEngine->getOutputDevicesForAttributes() when resolving which output |
| 7341 | // devices to patch to. This may be complicated by the fact that devices may become |
| 7342 | // unavailable. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 7343 | setMsdOutputPatches(); |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 7344 | } |
| 7345 | } |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 7346 | |
| 7347 | if (closingOutput->mPreferredAttrInfo != nullptr) { |
| 7348 | closingOutput->mPreferredAttrInfo->resetActiveClient(); |
| 7349 | } |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 7350 | } |
| 7351 | |
| 7352 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 7353 | { |
| 7354 | ALOGV("closeInput(%d)", input); |
| 7355 | |
| 7356 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 7357 | if (inputDesc == NULL) { |
| 7358 | ALOGW("closeInput() unknown input %d", input); |
| 7359 | return; |
| 7360 | } |
| 7361 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7362 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 7363 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7364 | sp<DeviceDescriptor> device = inputDesc->getDevice(); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 7365 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 7366 | if (index >= 0) { |
| 7367 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7368 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 7369 | patchDesc->getAfHandle(), 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 7370 | mAudioPatches.removeItemsAt(index); |
| 7371 | mpClientInterface->onAudioPatchListUpdate(); |
| 7372 | } |
| 7373 | |
François Gaffie | 6ebbce0 | 2023-07-19 13:27:53 +0200 | [diff] [blame] | 7374 | mEffects.putOrphanEffectsForIo(input); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 7375 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 7376 | mInputs.removeItem(input); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 7377 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7378 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 7379 | if (primaryInputDevices.contains(device) && |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 7380 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 7381 | mpClientInterface->setSoundTriggerCaptureState(false); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 7382 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7383 | } |
| 7384 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7385 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevices( |
| 7386 | const DeviceVector &devices, |
| 7387 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7388 | { |
| 7389 | SortedVector<audio_io_handle_t> outputs; |
| 7390 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7391 | ALOGVV("%s() devices %s", __func__, devices.toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7392 | for (size_t i = 0; i < openOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7393 | ALOGVV("output %zu isDuplicated=%d device=%s", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 7394 | i, openOutputs.valueAt(i)->isDuplicated(), |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7395 | openOutputs.valueAt(i)->supportedDevices().toString().c_str()); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 7396 | if (openOutputs.valueAt(i)->supportsAllDevices(devices) |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7397 | && openOutputs.valueAt(i)->devicesSupportEncodedFormats(devices.types())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7398 | ALOGVV("%s() found output %d", __func__, openOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7399 | outputs.add(openOutputs.keyAt(i)); |
| 7400 | } |
| 7401 | } |
| 7402 | return outputs; |
| 7403 | } |
| 7404 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 7405 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 7406 | { |
| 7407 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 7408 | // output is suspended before any tracks are moved to it |
| 7409 | checkA2dpSuspend(); |
| 7410 | checkOutputForAllStrategies(); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 7411 | checkSecondaryOutputs(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 7412 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 7413 | updateDevicesAndOutputs(); |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 7414 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 7415 | // TODO: The MSD patches to be established here may differ to current MSD patches due to how |
| 7416 | // output devices for patching are resolved. Nevertheless, AudioTracks affected by device |
| 7417 | // configuration changes will ultimately be rerouted correctly. We can still avoid |
| 7418 | // unnecessary rerouting by caching and reusing the arguments to |
| 7419 | // mEngine->getOutputDevicesForAttributes() when resolving which output devices to patch to. |
| 7420 | // This may be complicated by the fact that devices may become unavailable. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 7421 | setMsdOutputPatches(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 7422 | } |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 7423 | // an event that changed routing likely occurred, inform upper layers |
| 7424 | mpClientInterface->onRoutingUpdated(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 7425 | } |
| 7426 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7427 | bool AudioPolicyManager::followsSameRouting(const audio_attributes_t &lAttr, |
| 7428 | const audio_attributes_t &rAttr) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7429 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7430 | return mEngine->getProductStrategyForAttributes(lAttr) == |
| 7431 | mEngine->getProductStrategyForAttributes(rAttr); |
| 7432 | } |
| 7433 | |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 7434 | void AudioPolicyManager::checkAudioSourceForAttributes(const audio_attributes_t &attr) |
| 7435 | { |
| 7436 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 7437 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 7438 | if (sourceDesc != nullptr && followsSameRouting(attr, sourceDesc->attributes()) |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 7439 | && sourceDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE |
Eric Laurent | ccbd787 | 2024-06-20 12:34:15 +0000 | [diff] [blame] | 7440 | && !sourceDesc->isCallRx() && !sourceDesc->isInternal()) { |
David Li | f85c5e3 | 2024-07-01 13:14:10 +0000 | [diff] [blame] | 7441 | connectAudioSource(sourceDesc, 0 /*delayMs*/); |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 7442 | } |
| 7443 | } |
| 7444 | } |
| 7445 | |
| 7446 | void AudioPolicyManager::clearAudioSourcesForOutput(audio_io_handle_t output) |
| 7447 | { |
| 7448 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 7449 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 7450 | if (sourceDesc != nullptr && sourceDesc->swOutput().promote() != nullptr |
| 7451 | && sourceDesc->swOutput().promote()->mIoHandle == output) { |
| 7452 | disconnectAudioSource(sourceDesc); |
| 7453 | } |
| 7454 | } |
| 7455 | } |
| 7456 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7457 | void AudioPolicyManager::checkOutputForAttributes(const audio_attributes_t &attr) |
| 7458 | { |
| 7459 | auto psId = mEngine->getProductStrategyForAttributes(attr); |
| 7460 | |
| 7461 | DeviceVector oldDevices = mEngine->getOutputDevicesForAttributes(attr, 0, true /*fromCache*/); |
| 7462 | DeviceVector newDevices = mEngine->getOutputDevicesForAttributes(attr, 0, false /*fromCache*/); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 7463 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7464 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevices(oldDevices, mPreviousOutputs); |
| 7465 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevices(newDevices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7466 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 7467 | uint32_t maxLatency = 0; |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 7468 | bool unneededUsePrimaryOutputFromPolicyMixes = false; |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 7469 | std::vector<sp<SwAudioOutputDescriptor>> invalidatedOutputs; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 7470 | // take into account dynamic audio policies related changes: if a client is now associated |
| 7471 | // to a different policy mix than at creation time, invalidate corresponding stream |
Eric Laurent | 3ec5556 | 2024-08-22 15:08:57 +0000 | [diff] [blame] | 7472 | // invalidate clients on outputs that do not support all the newly selected devices for the |
| 7473 | // strategy |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 7474 | for (size_t i = 0; i < mPreviousOutputs.size(); i++) { |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 7475 | const sp<SwAudioOutputDescriptor>& desc = mPreviousOutputs.valueAt(i); |
Eric Laurent | 3ec5556 | 2024-08-22 15:08:57 +0000 | [diff] [blame] | 7476 | if (desc->isDuplicated() || desc->getClientCount() == 0) { |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 7477 | continue; |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 7478 | } |
Eric Laurent | 3ec5556 | 2024-08-22 15:08:57 +0000 | [diff] [blame] | 7479 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 7480 | for (const sp<TrackClientDescriptor>& client : desc->getClientIterable()) { |
| 7481 | if (mEngine->getProductStrategyForAttributes(client->attributes()) != psId) { |
| 7482 | continue; |
| 7483 | } |
Eric Laurent | 3ec5556 | 2024-08-22 15:08:57 +0000 | [diff] [blame] | 7484 | if (!desc->supportsAllDevices(newDevices)) { |
| 7485 | invalidatedOutputs.push_back(desc); |
| 7486 | break; |
| 7487 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 7488 | sp<AudioPolicyMix> primaryMix; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 7489 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->config(), |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 7490 | client->uid(), client->session(), client->flags(), mAvailableOutputDevices, |
| 7491 | nullptr /* requestedDevice */, primaryMix, nullptr /* secondaryMixes */, |
| 7492 | unneededUsePrimaryOutputFromPolicyMixes); |
Eric Laurent | 3ec5556 | 2024-08-22 15:08:57 +0000 | [diff] [blame] | 7493 | if (status == OK) { |
| 7494 | if (client->getPrimaryMix() != primaryMix || client->hasLostPrimaryMix()) { |
| 7495 | if (desc->isStrategyActive(psId) && maxLatency < desc->latency()) { |
| 7496 | maxLatency = desc->latency(); |
| 7497 | } |
| 7498 | invalidatedOutputs.push_back(desc); |
| 7499 | break; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 7500 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 7501 | } |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 7502 | } |
| 7503 | } |
| 7504 | |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 7505 | if (srcOutputs != dstOutputs || !invalidatedOutputs.empty()) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 7506 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 7507 | // audio from invalidated tracks will be rendered when unmuting |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 7508 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 7509 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 7510 | if (desc == nullptr) continue; |
| 7511 | |
| 7512 | if (desc->isStrategyActive(psId) && maxLatency < desc->latency()) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 7513 | maxLatency = desc->latency(); |
| 7514 | } |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 7515 | |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 7516 | bool invalidate = false; |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 7517 | for (auto client : desc->clientsList(false /*activeOnly*/)) { |
Eric Laurent | 78aade8 | 2019-09-13 18:55:08 -0700 | [diff] [blame] | 7518 | if (desc->isDuplicated() || !desc->mProfile->isDirectOutput()) { |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 7519 | // a client on a non direct outputs has necessarily a linear PCM format |
| 7520 | // so we can call selectOutput() safely |
| 7521 | const audio_io_handle_t newOutput = selectOutput(dstOutputs, |
| 7522 | client->flags(), |
| 7523 | client->config().format, |
| 7524 | client->config().channel_mask, |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 7525 | client->config().sample_rate, |
| 7526 | client->session()); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 7527 | if (newOutput != srcOut) { |
| 7528 | invalidate = true; |
| 7529 | break; |
| 7530 | } |
| 7531 | } else { |
| 7532 | sp<IOProfile> profile = getProfileForOutput(newDevices, |
| 7533 | client->config().sample_rate, |
| 7534 | client->config().format, |
| 7535 | client->config().channel_mask, |
| 7536 | client->flags(), |
| 7537 | true /* directOnly */); |
| 7538 | if (profile != desc->mProfile) { |
| 7539 | invalidate = true; |
| 7540 | break; |
| 7541 | } |
| 7542 | } |
| 7543 | } |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 7544 | // mute strategy while moving tracks from one output to another |
| 7545 | if (invalidate) { |
| 7546 | invalidatedOutputs.push_back(desc); |
| 7547 | if (desc->isStrategyActive(psId)) { |
| 7548 | setStrategyMute(psId, true, desc); |
| 7549 | setStrategyMute(psId, false, desc, maxLatency * LATENCY_MUTE_FACTOR, |
| 7550 | newDevices.types()); |
| 7551 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7552 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7553 | sp<SourceClientDescriptor> source = getSourceForAttributesOnOutput(srcOut, attr); |
Eric Laurent | ccbd787 | 2024-06-20 12:34:15 +0000 | [diff] [blame] | 7554 | if (source != nullptr && !source->isCallRx() && !source->isInternal()) { |
David Li | f85c5e3 | 2024-07-01 13:14:10 +0000 | [diff] [blame] | 7555 | connectAudioSource(source, 0 /*delayMs*/); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7556 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7557 | } |
| 7558 | |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 7559 | ALOGV_IF(!(srcOutputs.isEmpty() || dstOutputs.isEmpty()), |
| 7560 | "%s: strategy %d, moving from output %s to output %s", __func__, psId, |
| 7561 | std::to_string(srcOutputs[0]).c_str(), |
| 7562 | std::to_string(dstOutputs[0]).c_str()); |
| 7563 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7564 | // Move effects associated to this stream from previous output to new output |
| 7565 | if (followsSameRouting(attr, attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 7566 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7567 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7568 | // 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] | 7569 | if (!invalidatedOutputs.empty()) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 7570 | invalidateStreams(mEngine->getStreamTypesForProductStrategy(psId)); |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 7571 | for (sp<SwAudioOutputDescriptor> desc : invalidatedOutputs) { |
jiabin | 4925685 | 2022-03-09 11:21:35 -0800 | [diff] [blame] | 7572 | desc->setTracksInvalidatedStatusByStrategy(psId); |
| 7573 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7574 | } |
| 7575 | } |
| 7576 | } |
| 7577 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7578 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7579 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7580 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { |
| 7581 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); |
| 7582 | checkOutputForAttributes(attributes); |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 7583 | checkAudioSourceForAttributes(attributes); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7584 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7585 | } |
| 7586 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 7587 | void AudioPolicyManager::checkSecondaryOutputs() { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 7588 | PortHandleVector clientsToInvalidate; |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 7589 | TrackSecondaryOutputsMap trackSecondaryOutputs; |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 7590 | bool unneededUsePrimaryOutputFromPolicyMixes = false; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 7591 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 7592 | const sp<SwAudioOutputDescriptor>& outputDescriptor = mOutputs[i]; |
| 7593 | for (const sp<TrackClientDescriptor>& client : outputDescriptor->getClientIterable()) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 7594 | sp<AudioPolicyMix> primaryMix; |
| 7595 | std::vector<sp<AudioPolicyMix>> secondaryMixes; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 7596 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->config(), |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 7597 | client->uid(), client->session(), client->flags(), mAvailableOutputDevices, |
| 7598 | nullptr /* requestedDevice */, primaryMix, &secondaryMixes, |
| 7599 | unneededUsePrimaryOutputFromPolicyMixes); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 7600 | std::vector<sp<SwAudioOutputDescriptor>> secondaryDescs; |
| 7601 | for (auto &secondaryMix : secondaryMixes) { |
| 7602 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); |
| 7603 | if (outputDesc != nullptr && |
| 7604 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { |
| 7605 | secondaryDescs.push_back(outputDesc); |
| 7606 | } |
| 7607 | } |
| 7608 | |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 7609 | if (status != OK && |
| 7610 | (client->flags() & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == AUDIO_OUTPUT_FLAG_NONE) { |
| 7611 | // When it failed to query secondary output, only invalidate the client that is not |
| 7612 | // MMAP. The reason is that MMAP stream will not support secondary output. |
| 7613 | clientsToInvalidate.push_back(client->portId()); |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 7614 | } else if (!std::equal( |
| 7615 | client->getSecondaryOutputs().begin(), |
| 7616 | client->getSecondaryOutputs().end(), |
| 7617 | secondaryDescs.begin(), secondaryDescs.end())) { |
Andy Hung | db27c44 | 2024-08-14 11:37:57 -0700 | [diff] [blame] | 7618 | if (client->flags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD |
| 7619 | || !audio_is_linear_pcm(client->config().format)) { |
jiabin | a528106 | 2021-11-23 00:10:23 +0000 | [diff] [blame] | 7620 | // If the format is not PCM, the tracks should be invalidated to get correct |
| 7621 | // behavior when the secondary output is changed. |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 7622 | clientsToInvalidate.push_back(client->portId()); |
jiabin | a528106 | 2021-11-23 00:10:23 +0000 | [diff] [blame] | 7623 | } else { |
| 7624 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryDescs; |
| 7625 | std::vector<audio_io_handle_t> secondaryOutputIds; |
| 7626 | for (const auto &secondaryDesc: secondaryDescs) { |
| 7627 | secondaryOutputIds.push_back(secondaryDesc->mIoHandle); |
| 7628 | weakSecondaryDescs.push_back(secondaryDesc); |
| 7629 | } |
| 7630 | trackSecondaryOutputs.emplace(client->portId(), secondaryOutputIds); |
| 7631 | client->setSecondaryOutputs(std::move(weakSecondaryDescs)); |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 7632 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 7633 | } |
| 7634 | } |
| 7635 | } |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 7636 | if (!trackSecondaryOutputs.empty()) { |
| 7637 | mpClientInterface->updateSecondaryOutputs(trackSecondaryOutputs); |
| 7638 | } |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 7639 | if (!clientsToInvalidate.empty()) { |
| 7640 | ALOGD("%s Invalidate clients due to fail getting output for attr", __func__); |
| 7641 | mpClientInterface->invalidateTracks(clientsToInvalidate); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 7642 | } |
| 7643 | } |
| 7644 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7645 | bool AudioPolicyManager::isScoRequestedForComm() const { |
| 7646 | AudioDeviceTypeAddrVector devices; |
| 7647 | mEngine->getDevicesForRoleAndStrategy(mCommunnicationStrategy, DEVICE_ROLE_PREFERRED, devices); |
| 7648 | for (const auto &device : devices) { |
| 7649 | if (audio_is_bluetooth_out_sco_device(device.mType)) { |
| 7650 | return true; |
| 7651 | } |
| 7652 | } |
| 7653 | return false; |
| 7654 | } |
| 7655 | |
Eric Laurent | 1a8b45f | 2022-04-13 16:01:47 +0200 | [diff] [blame] | 7656 | bool AudioPolicyManager::isHearingAidUsedForComm() const { |
| 7657 | DeviceVector devices = mEngine->getOutputDevicesForStream(AUDIO_STREAM_VOICE_CALL, |
| 7658 | true /*fromCache*/); |
| 7659 | for (const auto &device : devices) { |
| 7660 | if (device->type() == AUDIO_DEVICE_OUT_HEARING_AID) { |
| 7661 | return true; |
| 7662 | } |
| 7663 | } |
| 7664 | return false; |
| 7665 | } |
| 7666 | |
| 7667 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7668 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7669 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7670 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 7671 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7672 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7673 | return; |
| 7674 | } |
| 7675 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7676 | bool isScoConnected = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7677 | (mAvailableInputDevices.types().count(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) != 0 || |
| 7678 | !Intersection(mAvailableOutputDevices.types(), getAudioDeviceOutAllScoSet()).empty()); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7679 | bool isScoRequested = isScoRequestedForComm(); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7680 | |
| 7681 | // if suspended, restore A2DP output if: |
| 7682 | // ((SCO device is NOT connected) || |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7683 | // ((SCO is not requested) && |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7684 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7685 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7686 | // if not suspended, suspend A2DP output if: |
| 7687 | // (SCO device is connected) && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7688 | // ((SCO is requested) || |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7689 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7690 | // |
| 7691 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7692 | if (!isScoConnected || |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7693 | (!isScoRequested && |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7694 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7695 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7696 | |
| 7697 | mpClientInterface->restoreOutput(a2dpOutput); |
| 7698 | mA2dpSuspended = false; |
| 7699 | } |
| 7700 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7701 | if (isScoConnected && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7702 | (isScoRequested || |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 7703 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7704 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7705 | |
| 7706 | mpClientInterface->suspendOutput(a2dpOutput); |
| 7707 | mA2dpSuspended = true; |
| 7708 | } |
| 7709 | } |
| 7710 | } |
| 7711 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7712 | DeviceVector AudioPolicyManager::getNewOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 7713 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7714 | { |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 7715 | if (outputDesc == nullptr) { |
| 7716 | return DeviceVector{}; |
| 7717 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7718 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 7719 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7720 | if (index >= 0) { |
| 7721 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7722 | if (patchDesc->getUid() != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7723 | ALOGV("%s device %s forced by patch %d", __func__, |
| 7724 | outputDesc->devices().toString().c_str(), outputDesc->getPatchHandle()); |
| 7725 | return outputDesc->devices(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7726 | } |
| 7727 | } |
| 7728 | |
Dean Wheatley | 514b431 | 2020-06-17 21:45:00 +1000 | [diff] [blame] | 7729 | // Do not retrieve engine device for outputs through MSD |
| 7730 | // TODO: support explicit routing requests by resetting MSD patch to engine device. |
| 7731 | if (outputDesc->devices() == getMsdAudioOutDevices()) { |
| 7732 | return outputDesc->devices(); |
| 7733 | } |
| 7734 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 7735 | // Honor explicit routing requests only if no client using default routing is active on this |
| 7736 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 7737 | bool active; // unused |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7738 | sp<DeviceDescriptor> device = |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7739 | findPreferredDevice(outputDesc, PRODUCT_STRATEGY_NONE, active, mAvailableOutputDevices); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7740 | if (device != nullptr) { |
| 7741 | return DeviceVector(device); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 7742 | } |
| 7743 | |
François Gaffie | a807ef9 | 2018-11-05 10:44:33 +0100 | [diff] [blame] | 7744 | // Legacy Engine cannot take care of bus devices and mix, so we need to handle the conflict |
| 7745 | // of setForceUse / Default Bus device here |
| 7746 | device = mPolicyMixes.getDeviceAndMixForOutput(outputDesc, mAvailableOutputDevices); |
| 7747 | if (device != nullptr) { |
| 7748 | return DeviceVector(device); |
| 7749 | } |
| 7750 | |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 7751 | DeviceVector devices; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7752 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { |
| 7753 | StreamTypeVector streams = mEngine->getStreamTypesForProductStrategy(productStrategy); |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7754 | auto hasStreamActive = [&](auto stream) { |
| 7755 | return hasStream(streams, stream) && isStreamActive(stream, 0); |
| 7756 | }; |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 7757 | |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7758 | auto doGetOutputDevicesForVoice = [&]() { |
| 7759 | return hasVoiceStream(streams) && (outputDesc == mPrimaryOutput || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7760 | outputDesc->isActive(toVolumeSource(AUDIO_STREAM_VOICE_CALL, false))) && |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7761 | (isInCall() || |
Henrik Backlund | 07c654a | 2021-10-14 15:57:10 +0200 | [diff] [blame] | 7762 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc)) && |
| 7763 | !isStreamActive(AUDIO_STREAM_ENFORCED_AUDIBLE, 0); |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7764 | }; |
| 7765 | |
| 7766 | // With low-latency playing on speaker, music on WFD, when the first low-latency |
| 7767 | // output is stopped, getNewOutputDevices checks for a product strategy |
| 7768 | // from the list, as STRATEGY_SONIFICATION comes prior to STRATEGY_MEDIA. |
Carter Hsu | cc58a6b | 2021-07-20 08:44:50 +0000 | [diff] [blame] | 7769 | // 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] | 7770 | // devices are returned for STRATEGY_SONIFICATION without checking whether the |
| 7771 | // stream is associated to the output descriptor. |
| 7772 | if (doGetOutputDevicesForVoice() || outputDesc->isStrategyActive(productStrategy) || |
| 7773 | ((hasStreamActive(AUDIO_STREAM_ALARM) || |
| 7774 | hasStreamActive(AUDIO_STREAM_ENFORCED_AUDIBLE)) && |
| 7775 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc))) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7776 | // Retrieval of devices for voice DL is done on primary output profile, cannot |
| 7777 | // check the route (would force modifying configuration file for this profile) |
jiangyao | 9478094 | 2024-03-05 10:43:14 +0800 | [diff] [blame] | 7778 | auto attr = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7779 | devices = mEngine->getOutputDevicesForAttributes(attr, nullptr, fromCache); |
| 7780 | break; |
| 7781 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7782 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7783 | ALOGV("%s selected devices %s", __func__, devices.toString().c_str()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7784 | return devices; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7785 | } |
| 7786 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7787 | sp<DeviceDescriptor> AudioPolicyManager::getNewInputDevice( |
| 7788 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7789 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7790 | sp<DeviceDescriptor> device; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7791 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 7792 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7793 | if (index >= 0) { |
| 7794 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7795 | if (patchDesc->getUid() != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7796 | ALOGV("getNewInputDevice() device %s forced by patch %d", |
| 7797 | inputDesc->getDevice()->toString().c_str(), inputDesc->getPatchHandle()); |
| 7798 | return inputDesc->getDevice(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7799 | } |
| 7800 | } |
| 7801 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 7802 | // Honor explicit routing requests only if no client using default routing is active on this |
| 7803 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 7804 | bool active; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7805 | device = findPreferredDevice(inputDesc, AUDIO_SOURCE_DEFAULT, active, mAvailableInputDevices); |
| 7806 | if (device != nullptr) { |
| 7807 | return device; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 7808 | } |
| 7809 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 7810 | // 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] | 7811 | // a null sp<>, causing the patch on the input stream to be released. |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7812 | audio_attributes_t attributes; |
| 7813 | uid_t uid; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7814 | audio_session_t session; |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7815 | sp<RecordClientDescriptor> topClient = inputDesc->getHighestPriorityClient(); |
| 7816 | if (topClient != nullptr) { |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 7817 | attributes = topClient->attributes(); |
| 7818 | uid = topClient->uid(); |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7819 | session = topClient->session(); |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7820 | } else { |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 7821 | attributes = { .source = AUDIO_SOURCE_DEFAULT }; |
| 7822 | uid = 0; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7823 | session = AUDIO_SESSION_NONE; |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7824 | } |
| 7825 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7826 | if (attributes.source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 7827 | attributes.source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 7828 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7829 | if (attributes.source != AUDIO_SOURCE_DEFAULT) { |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7830 | device = mEngine->getInputDeviceForAttributes(attributes, uid, session); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 7831 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7832 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7833 | return device; |
| 7834 | } |
| 7835 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 7836 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 7837 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 7838 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 7839 | } |
| 7840 | |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 7841 | status_t AudioPolicyManager::getDevicesForAttributes( |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7842 | const audio_attributes_t &attr, AudioDeviceTypeAddrVector *devices, bool forVolume) { |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 7843 | if (devices == nullptr) { |
| 7844 | return BAD_VALUE; |
| 7845 | } |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7846 | |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7847 | DeviceVector curDevices; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 7848 | if (status_t status = getDevicesForAttributes(attr, curDevices, forVolume); status != OK) { |
| 7849 | return status; |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7850 | } |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 7851 | for (const auto& device : curDevices) { |
| 7852 | devices->push_back(device->getDeviceTypeAddr()); |
| 7853 | } |
| 7854 | return NO_ERROR; |
| 7855 | } |
| 7856 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7857 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7858 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 7859 | case AUDIO_STREAM_MUSIC: |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7860 | checkOutputForAttributes(attributes_initializer(AUDIO_USAGE_NOTIFICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7861 | updateDevicesAndOutputs(); |
| 7862 | break; |
| 7863 | default: |
| 7864 | break; |
| 7865 | } |
| 7866 | } |
| 7867 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7868 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 7869 | |
| 7870 | // skip beacon mute management if a dedicated TTS output is available |
| 7871 | if (mTtsOutputAvailable) { |
| 7872 | return 0; |
| 7873 | } |
| 7874 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7875 | switch(event) { |
| 7876 | case STARTING_OUTPUT: |
| 7877 | mBeaconMuteRefCount++; |
| 7878 | break; |
| 7879 | case STOPPING_OUTPUT: |
| 7880 | if (mBeaconMuteRefCount > 0) { |
| 7881 | mBeaconMuteRefCount--; |
| 7882 | } |
| 7883 | break; |
| 7884 | case STARTING_BEACON: |
| 7885 | mBeaconPlayingRefCount++; |
| 7886 | break; |
| 7887 | case STOPPING_BEACON: |
| 7888 | if (mBeaconPlayingRefCount > 0) { |
| 7889 | mBeaconPlayingRefCount--; |
| 7890 | } |
| 7891 | break; |
| 7892 | } |
| 7893 | |
| 7894 | if (mBeaconMuteRefCount > 0) { |
| 7895 | // any playback causes beacon to be muted |
| 7896 | return setBeaconMute(true); |
| 7897 | } else { |
| 7898 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 7899 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 7900 | } |
| 7901 | } |
| 7902 | |
| 7903 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 7904 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 7905 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 7906 | // keep track of muted state to avoid repeating mute/unmute operations |
| 7907 | if (mBeaconMuted != mute) { |
| 7908 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 7909 | ALOGV("\t muting %d", mute); |
| 7910 | uint32_t maxLatency = 0; |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7911 | auto ttsVolumeSource = toVolumeSource(AUDIO_STREAM_TTS, false); |
| 7912 | if (ttsVolumeSource == VOLUME_SOURCE_NONE) { |
| 7913 | ALOGV("\t no tts volume source available"); |
| 7914 | return 0; |
| 7915 | } |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7916 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7917 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7918 | setVolumeSourceMute(ttsVolumeSource, mute/*on*/, desc, 0 /*delay*/, DeviceTypeSet()); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7919 | const uint32_t latency = desc->latency() * 2; |
Eric Laurent | cdb2b35 | 2020-07-23 10:57:02 -0700 | [diff] [blame] | 7920 | if (desc->isActive(latency * 2) && latency > maxLatency) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7921 | maxLatency = latency; |
| 7922 | } |
| 7923 | } |
| 7924 | mBeaconMuted = mute; |
| 7925 | return maxLatency; |
| 7926 | } |
| 7927 | return 0; |
| 7928 | } |
| 7929 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7930 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7931 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7932 | mEngine->updateDeviceSelectionCache(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7933 | mPreviousOutputs = mOutputs; |
| 7934 | } |
| 7935 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 7936 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7937 | const DeviceVector &prevDevices, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7938 | uint32_t delayMs) |
| 7939 | { |
| 7940 | // mute/unmute strategies using an incompatible device combination |
| 7941 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 7942 | // if unmuting, unmute only after the specified delay |
| 7943 | if (outputDesc->isDuplicated()) { |
| 7944 | return 0; |
| 7945 | } |
| 7946 | |
| 7947 | uint32_t muteWaitMs = 0; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7948 | DeviceVector devices = outputDesc->devices(); |
| 7949 | bool shouldMute = outputDesc->isActive() && (devices.size() >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7950 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7951 | auto productStrategies = mEngine->getOrderedProductStrategies(); |
| 7952 | for (const auto &productStrategy : productStrategies) { |
| 7953 | auto attributes = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); |
| 7954 | DeviceVector curDevices = |
| 7955 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false/*fromCache*/); |
| 7956 | curDevices = curDevices.filter(outputDesc->supportedDevices()); |
| 7957 | bool mute = shouldMute && curDevices.containsAtLeastOne(devices) && curDevices != devices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7958 | bool doMute = false; |
| 7959 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7960 | if (mute && !outputDesc->isStrategyMutedByDevice(productStrategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7961 | doMute = true; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7962 | outputDesc->setStrategyMutedByDevice(productStrategy, true); |
| 7963 | } else if (!mute && outputDesc->isStrategyMutedByDevice(productStrategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7964 | doMute = true; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7965 | outputDesc->setStrategyMutedByDevice(productStrategy, false); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7966 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7967 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7968 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7969 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7970 | // 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] | 7971 | if (!desc->supportedDevices().containsAtLeastOne(outputDesc->supportedDevices())) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7972 | continue; |
| 7973 | } |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 7974 | 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] | 7975 | mute ? "muting" : "unmuting", curDevices.toString().c_str()); |
| 7976 | setStrategyMute(productStrategy, mute, desc, mute ? 0 : delayMs); |
| 7977 | if (desc->isStrategyActive(productStrategy)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7978 | if (mute) { |
| 7979 | // FIXME: should not need to double latency if volume could be applied |
| 7980 | // immediately by the audioflinger mixer. We must account for the delay |
| 7981 | // between now and the next time the audioflinger thread for this output |
| 7982 | // will process a buffer (which corresponds to one buffer size, |
| 7983 | // usually 1/2 or 1/4 of the latency). |
| 7984 | if (muteWaitMs < desc->latency() * 2) { |
| 7985 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7986 | } |
| 7987 | } |
| 7988 | } |
| 7989 | } |
| 7990 | } |
| 7991 | } |
| 7992 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7993 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 7994 | // different per device volumes |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7995 | if (outputDesc->isActive() && (devices != prevDevices)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 7996 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
Jasmine Cha | f6074fe | 2021-08-17 13:44:31 +0800 | [diff] [blame] | 7997 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 7998 | if (muteWaitMs < tempMuteWaitMs) { |
| 7999 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 8000 | } |
Jasmine Cha | f6074fe | 2021-08-17 13:44:31 +0800 | [diff] [blame] | 8001 | |
| 8002 | // If recommended duration is defined, replace temporary mute duration to avoid |
| 8003 | // truncated notifications at beginning, which depends on duration of changing path in HAL. |
| 8004 | // Otherwise, temporary mute duration is conservatively set to 4 times the reported latency. |
| 8005 | uint32_t tempRecommendedMuteDuration = outputDesc->getRecommendedMuteDurationMs(); |
| 8006 | uint32_t tempMuteDurationMs = tempRecommendedMuteDuration > 0 ? |
| 8007 | tempRecommendedMuteDuration : outputDesc->latency() * 4; |
| 8008 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8009 | for (const auto &activeVs : outputDesc->getActiveVolumeSources()) { |
| 8010 | // make sure that we do not start the temporary mute period too early in case of |
| 8011 | // delayed device change |
| 8012 | setVolumeSourceMute(activeVs, true, outputDesc, delayMs); |
| 8013 | setVolumeSourceMute(activeVs, false, outputDesc, delayMs + tempMuteDurationMs, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 8014 | devices.types()); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 8015 | } |
| 8016 | } |
| 8017 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8018 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 8019 | if (muteWaitMs > delayMs) { |
| 8020 | muteWaitMs -= delayMs; |
| 8021 | usleep(muteWaitMs * 1000); |
| 8022 | return muteWaitMs; |
| 8023 | } |
| 8024 | return 0; |
| 8025 | } |
| 8026 | |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 8027 | uint32_t AudioPolicyManager::setOutputDevices(const char *caller, |
| 8028 | const sp<SwAudioOutputDescriptor>& outputDesc, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 8029 | const DeviceVector &devices, |
| 8030 | bool force, |
| 8031 | int delayMs, |
| 8032 | audio_patch_handle_t *patchHandle, |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 8033 | bool requiresMuteCheck, bool requiresVolumeCheck, |
| 8034 | bool skipMuteDelay) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8035 | { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 8036 | // TODO(b/262404095): Consider if the output need to be reopened. |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 8037 | std::string logPrefix = std::string("caller ") + caller + outputDesc->info(); |
| 8038 | ALOGV("%s %s device %s delayMs %d", __func__, logPrefix.c_str(), |
| 8039 | devices.toString().c_str(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8040 | uint32_t muteWaitMs; |
| 8041 | |
| 8042 | if (outputDesc->isDuplicated()) { |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 8043 | muteWaitMs = setOutputDevices(__func__, outputDesc->subOutput1(), devices, force, delayMs, |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 8044 | nullptr /* patchHandle */, requiresMuteCheck, skipMuteDelay); |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 8045 | muteWaitMs += setOutputDevices(__func__, outputDesc->subOutput2(), devices, force, delayMs, |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 8046 | nullptr /* patchHandle */, requiresMuteCheck, skipMuteDelay); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8047 | return muteWaitMs; |
| 8048 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8049 | |
| 8050 | // filter devices according to output selected |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 8051 | DeviceVector filteredDevices = outputDesc->filterSupportedDevices(devices); |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 8052 | DeviceVector prevDevices = outputDesc->devices(); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 8053 | DeviceVector availPrevDevices = mAvailableOutputDevices.filter(prevDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8054 | |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 8055 | ALOGV("%s %s prevDevice %s", __func__, logPrefix.c_str(), |
| 8056 | prevDevices.toString().c_str()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 8057 | |
| 8058 | if (!filteredDevices.isEmpty()) { |
| 8059 | outputDesc->setDevices(filteredDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8060 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 8061 | |
| 8062 | // if the outputs are not materially active, there is no need to mute. |
| 8063 | if (requiresMuteCheck) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 8064 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevices, delayMs); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 8065 | } else { |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 8066 | ALOGV("%s: %s suppressing checkDeviceMuteStrategies", __func__, |
| 8067 | logPrefix.c_str()); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 8068 | muteWaitMs = 0; |
| 8069 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8070 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 8071 | bool outputRouted = outputDesc->isRouted(); |
| 8072 | |
Eric Laurent | 79ea958 | 2020-06-11 18:49:24 -0700 | [diff] [blame] | 8073 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 8074 | // output profile or if new device is not supported AND previous device(s) is(are) still |
| 8075 | // available (otherwise reset device must be done on the output) |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 8076 | if (!devices.isEmpty() && filteredDevices.isEmpty() && !availPrevDevices.empty()) { |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 8077 | ALOGV("%s: %s unsupported device %s for output", __func__, logPrefix.c_str(), |
| 8078 | devices.toString().c_str()); |
Eric Laurent | 79ea958 | 2020-06-11 18:49:24 -0700 | [diff] [blame] | 8079 | // restore previous device after evaluating strategy mute state |
| 8080 | outputDesc->setDevices(prevDevices); |
| 8081 | return muteWaitMs; |
| 8082 | } |
| 8083 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8084 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 8085 | // the requested device is AUDIO_DEVICE_NONE |
| 8086 | // OR the requested device is the same as current device |
| 8087 | // AND force is not specified |
| 8088 | // AND the output is connected by a valid audio patch. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 8089 | // Doing this check here allows the caller to call setOutputDevices() without conditions |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 8090 | if ((filteredDevices.isEmpty() || filteredDevices == prevDevices) && !force && outputRouted) { |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 8091 | ALOGV("%s %s setting same device %s or null device, force=%d, patch handle=%d", |
| 8092 | __func__, logPrefix.c_str(), filteredDevices.toString().c_str(), force, |
| 8093 | outputDesc->getPatchHandle()); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 8094 | if (requiresVolumeCheck && !filteredDevices.isEmpty()) { |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 8095 | ALOGV("%s %s setting same device on routed output, force apply volumes", |
| 8096 | __func__, logPrefix.c_str()); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 8097 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs, true /*force*/); |
| 8098 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8099 | return muteWaitMs; |
| 8100 | } |
| 8101 | |
Jaideep Sharma | 4b5c425 | 2023-07-27 14:47:32 +0530 | [diff] [blame] | 8102 | ALOGV("%s %s changing device to %s", __func__, logPrefix.c_str(), |
| 8103 | filteredDevices.toString().c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8104 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8105 | // do the routing |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 8106 | if (filteredDevices.isEmpty() || mAvailableOutputDevices.filter(filteredDevices).empty()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 8107 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8108 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 8109 | PatchBuilder patchBuilder; |
| 8110 | patchBuilder.addSource(outputDesc); |
| 8111 | ALOG_ASSERT(filteredDevices.size() <= AUDIO_PATCH_PORTS_MAX, "Too many sink ports"); |
| 8112 | for (const auto &filteredDevice : filteredDevices) { |
| 8113 | patchBuilder.addSink(filteredDevice); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 8114 | } |
| 8115 | |
Jasmine Cha | 7f82d1a | 2020-03-16 13:21:47 +0800 | [diff] [blame] | 8116 | // Add half reported latency to delayMs when muteWaitMs is null in order |
| 8117 | // to avoid disordered sequence of muting volume and changing devices. |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 8118 | int actualDelayMs = !skipMuteDelay && muteWaitMs == 0 |
| 8119 | ? (delayMs + (outputDesc->latency() / 2)) : delayMs; |
| 8120 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), actualDelayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8121 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8122 | |
Oscar Azucena | 6acf34b | 2023-04-27 16:32:09 -0700 | [diff] [blame] | 8123 | // Since the mute is skip, also skip the apply stream volume as that will be applied externally |
| 8124 | if (!skipMuteDelay) { |
| 8125 | // update stream volumes according to new device |
| 8126 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs); |
| 8127 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8128 | |
| 8129 | return muteWaitMs; |
| 8130 | } |
| 8131 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 8132 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 8133 | int delayMs, |
| 8134 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8135 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 8136 | ssize_t index; |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 8137 | if (patchHandle == nullptr && !outputDesc->isRouted()) { |
| 8138 | return INVALID_OPERATION; |
| 8139 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 8140 | if (patchHandle) { |
| 8141 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 8142 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 8143 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 8144 | } |
| 8145 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8146 | return INVALID_OPERATION; |
| 8147 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 8148 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8149 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8150 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 8151 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8152 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 8153 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 8154 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8155 | return status; |
| 8156 | } |
| 8157 | |
| 8158 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 8159 | const sp<DeviceDescriptor> &device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 8160 | bool force, |
| 8161 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8162 | { |
| 8163 | status_t status = NO_ERROR; |
| 8164 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 8165 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 8166 | if ((device != nullptr) && ((device != inputDesc->getDevice()) || force)) { |
| 8167 | inputDesc->setDevice(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8168 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 8169 | if (mAvailableInputDevices.contains(device)) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8170 | PatchBuilder patchBuilder; |
| 8171 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 8172 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 8173 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8174 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 8175 | auto result = usecase; |
| 8176 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 8177 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 8178 | } |
Dean Wheatley | b984183 | 2024-10-01 14:56:29 +1000 | [diff] [blame] | 8179 | return result; }); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8180 | //only one input device for now |
Dean Wheatley | b984183 | 2024-10-01 14:56:29 +1000 | [diff] [blame] | 8181 | if (audio_is_remote_submix_device(device->type())) { |
| 8182 | // remote submix HAL does not support audio conversion, need source device |
| 8183 | // audio config to match the sink input descriptor audio config, otherwise AIDL |
| 8184 | // HAL patching will fail |
| 8185 | audio_port_config srcDevicePortConfig = {}; |
| 8186 | device->toAudioPortConfig(&srcDevicePortConfig, nullptr); |
| 8187 | srcDevicePortConfig.sample_rate = inputDesc->getSamplingRate(); |
| 8188 | srcDevicePortConfig.channel_mask = inputDesc->getChannelMask(); |
| 8189 | srcDevicePortConfig.format = inputDesc->getFormat(); |
| 8190 | patchBuilder.addSource(srcDevicePortConfig); |
| 8191 | } else { |
| 8192 | patchBuilder.addSource(device); |
| 8193 | } |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8194 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8195 | } |
| 8196 | } |
| 8197 | return status; |
| 8198 | } |
| 8199 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 8200 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 8201 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8202 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 8203 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 8204 | ssize_t index; |
| 8205 | if (patchHandle) { |
| 8206 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 8207 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 8208 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 8209 | } |
| 8210 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8211 | return INVALID_OPERATION; |
| 8212 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 8213 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8214 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8215 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 8216 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8217 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 8218 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 8219 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8220 | return status; |
| 8221 | } |
| 8222 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 8223 | sp<IOProfile> AudioPolicyManager::getInputProfile(const sp<DeviceDescriptor> &device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 8224 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 8225 | audio_format_t& format, |
| 8226 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 8227 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8228 | { |
| 8229 | // Choose an input profile based on the requested capture parameters: select the first available |
| 8230 | // profile supporting all requested parameters. |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 8231 | // 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] | 8232 | |
Atneya Nair | 0f0a803 | 2022-12-12 16:20:12 -0800 | [diff] [blame] | 8233 | using underlying_input_flag_t = std::underlying_type_t<audio_input_flags_t>; |
| 8234 | const underlying_input_flag_t mustMatchFlag = AUDIO_INPUT_FLAG_MMAP_NOIRQ | |
| 8235 | AUDIO_INPUT_FLAG_HOTWORD_TAP | AUDIO_INPUT_FLAG_HW_LOOKBACK; |
| 8236 | |
| 8237 | const underlying_input_flag_t oriFlags = flags; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 8238 | |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 8239 | for (;;) { |
| 8240 | sp<IOProfile> firstInexact = nullptr; |
jiabin | 5e02d2b | 2024-09-23 19:26:19 +0000 | [diff] [blame] | 8241 | uint32_t inexactSamplingRate = 0; |
| 8242 | audio_format_t inexactFormat = AUDIO_FORMAT_INVALID; |
| 8243 | audio_channel_mask_t inexactChannelMask = AUDIO_CHANNEL_INVALID; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 8244 | uint32_t updatedSamplingRate = 0; |
| 8245 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 8246 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
| 8247 | for (const auto& hwModule : mHwModules) { |
| 8248 | for (const auto& profile : hwModule->getInputProfiles()) { |
| 8249 | // profile->log(); |
| 8250 | //updatedFormat = format; |
jiabin | 66acc43 | 2024-02-06 00:57:36 +0000 | [diff] [blame] | 8251 | if (profile->getCompatibilityScore( |
| 8252 | DeviceVector(device), |
| 8253 | samplingRate, |
| 8254 | &updatedSamplingRate, |
| 8255 | format, |
| 8256 | &updatedFormat, |
| 8257 | channelMask, |
| 8258 | &updatedChannelMask, |
| 8259 | // FIXME ugly cast |
| 8260 | (audio_output_flags_t) flags, |
| 8261 | true /*exactMatchRequiredForInputFlags*/) == IOProfile::EXACT_MATCH) { |
| 8262 | samplingRate = updatedSamplingRate; |
| 8263 | format = updatedFormat; |
| 8264 | channelMask = updatedChannelMask; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 8265 | return profile; |
| 8266 | } |
jiabin | 66acc43 | 2024-02-06 00:57:36 +0000 | [diff] [blame] | 8267 | if (firstInexact == nullptr |
| 8268 | && profile->getCompatibilityScore( |
| 8269 | DeviceVector(device), |
| 8270 | samplingRate, |
| 8271 | &updatedSamplingRate, |
| 8272 | format, |
| 8273 | &updatedFormat, |
| 8274 | channelMask, |
| 8275 | &updatedChannelMask, |
| 8276 | // FIXME ugly cast |
| 8277 | (audio_output_flags_t) flags, |
| 8278 | false /*exactMatchRequiredForInputFlags*/) |
| 8279 | != IOProfile::NO_MATCH) { |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 8280 | firstInexact = profile; |
jiabin | 5e02d2b | 2024-09-23 19:26:19 +0000 | [diff] [blame] | 8281 | inexactSamplingRate = updatedSamplingRate; |
| 8282 | inexactFormat = updatedFormat; |
| 8283 | inexactChannelMask = updatedChannelMask; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 8284 | } |
| 8285 | } |
| 8286 | } |
| 8287 | |
| 8288 | if (firstInexact != nullptr) { |
jiabin | 5e02d2b | 2024-09-23 19:26:19 +0000 | [diff] [blame] | 8289 | samplingRate = inexactSamplingRate; |
| 8290 | format = inexactFormat; |
| 8291 | channelMask = inexactChannelMask; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 8292 | return firstInexact; |
| 8293 | } else if (flags & AUDIO_INPUT_FLAG_RAW) { |
| 8294 | flags = (audio_input_flags_t) (flags & ~AUDIO_INPUT_FLAG_RAW); // retry |
| 8295 | } else if ((flags & mustMatchFlag) == AUDIO_INPUT_FLAG_NONE && |
| 8296 | flags != AUDIO_INPUT_FLAG_NONE && audio_is_linear_pcm(format)) { |
| 8297 | flags = AUDIO_INPUT_FLAG_NONE; |
| 8298 | } else { // fail |
| 8299 | ALOGW("%s could not find profile for device %s, sampling rate %u, format %#x, " |
| 8300 | "channel mask 0x%X, flags %#x", __func__, device->toString().c_str(), |
| 8301 | samplingRate, format, channelMask, oriFlags); |
| 8302 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8303 | } |
| 8304 | } |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 8305 | |
| 8306 | return nullptr; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8307 | } |
| 8308 | |
Vlad Popa | 87e0e58 | 2024-05-20 18:49:20 -0700 | [diff] [blame] | 8309 | float AudioPolicyManager::adjustDeviceAttenuationForAbsVolume(IVolumeCurves &curves, |
| 8310 | VolumeSource volumeSource, |
| 8311 | int index, |
| 8312 | const DeviceTypeSet &deviceTypes) |
| 8313 | { |
| 8314 | audio_devices_t volumeDevice = Volume::getDeviceForVolume(deviceTypes); |
| 8315 | device_category deviceCategory = Volume::getDeviceCategory({volumeDevice}); |
| 8316 | float volumeDb = curves.volIndexToDb(deviceCategory, index); |
| 8317 | |
| 8318 | if (com_android_media_audio_abs_volume_index_fix()) { |
| 8319 | if (mAbsoluteVolumeDrivingStreams.find(volumeDevice) != |
| 8320 | mAbsoluteVolumeDrivingStreams.end()) { |
| 8321 | audio_attributes_t attributesToDriveAbs = mAbsoluteVolumeDrivingStreams[volumeDevice]; |
| 8322 | auto groupToDriveAbs = mEngine->getVolumeGroupForAttributes(attributesToDriveAbs); |
| 8323 | if (groupToDriveAbs == VOLUME_GROUP_NONE) { |
| 8324 | ALOGD("%s: no group matching with %s", __FUNCTION__, |
| 8325 | toString(attributesToDriveAbs).c_str()); |
| 8326 | return volumeDb; |
| 8327 | } |
| 8328 | |
| 8329 | float volumeDbMax = curves.volIndexToDb(deviceCategory, curves.getVolumeIndexMax()); |
| 8330 | VolumeSource vsToDriveAbs = toVolumeSource(groupToDriveAbs); |
| 8331 | if (vsToDriveAbs == volumeSource) { |
| 8332 | // attenuation is applied by the abs volume controller |
Eric Laurent | 64e868f | 2024-06-28 16:42:49 +0000 | [diff] [blame] | 8333 | return (index != 0) ? volumeDbMax : volumeDb; |
Vlad Popa | 87e0e58 | 2024-05-20 18:49:20 -0700 | [diff] [blame] | 8334 | } else { |
| 8335 | IVolumeCurves &curvesAbs = getVolumeCurves(vsToDriveAbs); |
| 8336 | int indexAbs = curvesAbs.getVolumeIndex({volumeDevice}); |
| 8337 | float volumeDbAbs = curvesAbs.volIndexToDb(deviceCategory, indexAbs); |
| 8338 | float volumeDbAbsMax = curvesAbs.volIndexToDb(deviceCategory, |
| 8339 | curvesAbs.getVolumeIndexMax()); |
| 8340 | float newVolumeDb = fminf(volumeDb + volumeDbAbsMax - volumeDbAbs, volumeDbMax); |
| 8341 | ALOGV("%s: abs vol stream %d with attenuation %f is adjusting stream %d from " |
| 8342 | "attenuation %f to attenuation %f %f", __func__, vsToDriveAbs, volumeDbAbs, |
| 8343 | volumeSource, volumeDb, newVolumeDb, volumeDbMax); |
| 8344 | return newVolumeDb; |
| 8345 | } |
| 8346 | } |
| 8347 | return volumeDb; |
| 8348 | } else { |
| 8349 | return volumeDb; |
| 8350 | } |
| 8351 | } |
| 8352 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8353 | float AudioPolicyManager::computeVolume(IVolumeCurves &curves, |
| 8354 | VolumeSource volumeSource, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 8355 | int index, |
Oscar Azucena | e763f7a | 2024-03-27 18:56:02 -0700 | [diff] [blame] | 8356 | const DeviceTypeSet& deviceTypes, |
Vlad Popa | 9d48276 | 2024-06-21 16:40:23 -0700 | [diff] [blame] | 8357 | bool adjustAttenuation, |
Oscar Azucena | e763f7a | 2024-03-27 18:56:02 -0700 | [diff] [blame] | 8358 | bool computeInternalInteraction) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8359 | { |
Vlad Popa | 9d48276 | 2024-06-21 16:40:23 -0700 | [diff] [blame] | 8360 | float volumeDb; |
| 8361 | if (adjustAttenuation) { |
| 8362 | volumeDb = adjustDeviceAttenuationForAbsVolume(curves, volumeSource, index, deviceTypes); |
| 8363 | } else { |
| 8364 | volumeDb = curves.volIndexToDb(Volume::getDeviceCategory(deviceTypes), index); |
| 8365 | } |
Oscar Azucena | e763f7a | 2024-03-27 18:56:02 -0700 | [diff] [blame] | 8366 | ALOGV("%s volume source %d, index %d, devices %s, compute internal %b ", __func__, |
| 8367 | volumeSource, index, dumpDeviceTypes(deviceTypes).c_str(), computeInternalInteraction); |
| 8368 | |
| 8369 | if (!computeInternalInteraction) { |
| 8370 | return volumeDb; |
| 8371 | } |
| 8372 | |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 8373 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 8374 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 8375 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 8376 | // the ringtone volume |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 8377 | const auto callVolumeSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL, false); |
| 8378 | const auto ringVolumeSrc = toVolumeSource(AUDIO_STREAM_RING, false); |
| 8379 | const auto musicVolumeSrc = toVolumeSource(AUDIO_STREAM_MUSIC, false); |
| 8380 | const auto alarmVolumeSrc = toVolumeSource(AUDIO_STREAM_ALARM, false); |
| 8381 | const auto a11yVolumeSrc = toVolumeSource(AUDIO_STREAM_ACCESSIBILITY, false); |
Oscar Azucena | e763f7a | 2024-03-27 18:56:02 -0700 | [diff] [blame] | 8382 | if (AUDIO_MODE_RINGTONE == mEngine->getPhoneState() && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8383 | mOutputs.isActive(ringVolumeSrc, 0)) { |
| 8384 | auto &ringCurves = getVolumeCurves(AUDIO_STREAM_RING); |
Oscar Azucena | e763f7a | 2024-03-27 18:56:02 -0700 | [diff] [blame] | 8385 | const float ringVolumeDb = computeVolume(ringCurves, ringVolumeSrc, index, deviceTypes, |
Vlad Popa | 9d48276 | 2024-06-21 16:40:23 -0700 | [diff] [blame] | 8386 | adjustAttenuation, |
| 8387 | /* computeInternalInteraction= */false); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8388 | return ringVolumeDb - 4 > volumeDb ? ringVolumeDb - 4 : volumeDb; |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 8389 | } |
| 8390 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 8391 | // in-call: always cap volume by voice volume + some low headroom |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8392 | if ((volumeSource != callVolumeSrc && (isInCall() || |
| 8393 | mOutputs.isActiveLocally(callVolumeSrc))) && |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 8394 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM, false) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8395 | volumeSource == ringVolumeSrc || volumeSource == musicVolumeSrc || |
| 8396 | volumeSource == alarmVolumeSrc || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 8397 | volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION, false) || |
| 8398 | volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false) || |
| 8399 | volumeSource == toVolumeSource(AUDIO_STREAM_DTMF, false) || |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 8400 | volumeSource == a11yVolumeSrc)) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8401 | auto &voiceCurves = getVolumeCurves(callVolumeSrc); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8402 | int voiceVolumeIndex = voiceCurves.getVolumeIndex(deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8403 | const float maxVoiceVolDb = |
Oscar Azucena | e763f7a | 2024-03-27 18:56:02 -0700 | [diff] [blame] | 8404 | computeVolume(voiceCurves, callVolumeSrc, voiceVolumeIndex, deviceTypes, |
Vlad Popa | 9d48276 | 2024-06-21 16:40:23 -0700 | [diff] [blame] | 8405 | adjustAttenuation, /* computeInternalInteraction= */false) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 8406 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 8407 | // FIXME: Workaround for call screening applications until a proper audio mode is defined |
| 8408 | // to support this scenario : Exempt the RING stream from the audio cap if the audio was |
| 8409 | // programmatically muted. |
| 8410 | // VOICE_CALL stream has minVolumeIndex > 0 : Users cannot set the volume of voice calls to |
| 8411 | // 0. We don't want to cap volume when the system has programmatically muted the voice call |
| 8412 | // stream. See setVolumeCurveIndex() for more information. |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 8413 | bool exemptFromCapping = |
| 8414 | ((volumeSource == ringVolumeSrc) || (volumeSource == a11yVolumeSrc)) |
| 8415 | && (voiceVolumeIndex == 0); |
Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 8416 | ALOGV_IF(exemptFromCapping, "%s volume source %d at vol=%f not capped", __func__, |
| 8417 | volumeSource, volumeDb); |
| 8418 | if ((volumeDb > maxVoiceVolDb) && !exemptFromCapping) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8419 | ALOGV("%s volume source %d at vol=%f overriden by volume group %d at vol=%f", __func__, |
| 8420 | volumeSource, volumeDb, callVolumeSrc, maxVoiceVolDb); |
| 8421 | volumeDb = maxVoiceVolDb; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 8422 | } |
| 8423 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8424 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 8425 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 8426 | // - always attenuate notifications volume by 6dB |
| 8427 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 8428 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8429 | // - if music is playing, always limit the volume to current music volume, |
| 8430 | // with a minimum threshold at -36dB so that notification is always perceived. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8431 | if (!Intersection(deviceTypes, |
| 8432 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, |
| 8433 | AUDIO_DEVICE_OUT_WIRED_HEADSET, AUDIO_DEVICE_OUT_WIRED_HEADPHONE, |
Eric Laurent | c42df45 | 2020-08-07 10:51:53 -0700 | [diff] [blame] | 8434 | AUDIO_DEVICE_OUT_USB_HEADSET, AUDIO_DEVICE_OUT_HEARING_AID, |
| 8435 | AUDIO_DEVICE_OUT_BLE_HEADSET}).empty() && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8436 | ((volumeSource == alarmVolumeSrc || |
| 8437 | volumeSource == ringVolumeSrc) || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 8438 | (volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION, false)) || |
| 8439 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM, false)) || |
| 8440 | ((volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false)) && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8441 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
| 8442 | curves.canBeMuted()) { |
| 8443 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8444 | // when the phone is ringing we must consider that music could have been paused just before |
| 8445 | // by the music application and behave as if music was active if the last music track was |
| 8446 | // just stopped |
Oscar Azucena | e763f7a | 2024-03-27 18:56:02 -0700 | [diff] [blame] | 8447 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) |
| 8448 | || mLimitRingtoneVolume) { |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 8449 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8450 | DeviceTypeSet musicDevice = |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 8451 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 8452 | nullptr, true /*fromCache*/).types(); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8453 | auto &musicCurves = getVolumeCurves(AUDIO_STREAM_MUSIC); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8454 | float musicVolDb = computeVolume(musicCurves, |
| 8455 | musicVolumeSrc, |
| 8456 | musicCurves.getVolumeIndex(musicDevice), |
Oscar Azucena | e763f7a | 2024-03-27 18:56:02 -0700 | [diff] [blame] | 8457 | musicDevice, |
Vlad Popa | 9d48276 | 2024-06-21 16:40:23 -0700 | [diff] [blame] | 8458 | adjustAttenuation, |
| 8459 | /* computeInternalInteraction= */ false); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8460 | float minVolDb = (musicVolDb > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 8461 | musicVolDb : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
| 8462 | if (volumeDb > minVolDb) { |
| 8463 | volumeDb = minVolDb; |
| 8464 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDb, musicVolDb); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8465 | } |
Eric Laurent | 7b6385c | 2021-05-12 17:55:36 +0200 | [diff] [blame] | 8466 | if (Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER |
| 8467 | && !Intersection(deviceTypes, {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, |
chenxin20 | 58f15fd | 2024-06-13 22:04:29 +0800 | [diff] [blame] | 8468 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, |
| 8469 | AUDIO_DEVICE_OUT_BLE_HEADSET}).empty()) { |
| 8470 | // on A2DP/BLE, also ensure notification volume is not too low compared to media |
| 8471 | // when intended to be played. |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 8472 | if ((volumeDb > -96.0f) && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8473 | (musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDb)) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8474 | ALOGV("%s increasing volume for volume source=%d device=%s from %f to %f", |
| 8475 | __func__, volumeSource, dumpDeviceTypes(deviceTypes).c_str(), volumeDb, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8476 | musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 8477 | volumeDb = musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 8478 | } |
| 8479 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8480 | } else if ((Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8481 | (!(volumeSource == alarmVolumeSrc || volumeSource == ringVolumeSrc))) { |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 8482 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8483 | } |
| 8484 | } |
| 8485 | |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 8486 | return volumeDb; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8487 | } |
| 8488 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 8489 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 8490 | VolumeSource fromVolumeSource, |
| 8491 | VolumeSource toVolumeSource) |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 8492 | { |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 8493 | if (fromVolumeSource == toVolumeSource) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 8494 | return srcIndex; |
| 8495 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 8496 | auto &srcCurves = getVolumeCurves(fromVolumeSource); |
| 8497 | auto &dstCurves = getVolumeCurves(toVolumeSource); |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 8498 | float minSrc = (float)srcCurves.getVolumeIndexMin(); |
| 8499 | float maxSrc = (float)srcCurves.getVolumeIndexMax(); |
| 8500 | float minDst = (float)dstCurves.getVolumeIndexMin(); |
| 8501 | float maxDst = (float)dstCurves.getVolumeIndexMax(); |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 8502 | |
Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 8503 | // preserve mute request or correct range |
| 8504 | if (srcIndex < minSrc) { |
| 8505 | if (srcIndex == 0) { |
| 8506 | return 0; |
| 8507 | } |
| 8508 | srcIndex = minSrc; |
| 8509 | } else if (srcIndex > maxSrc) { |
| 8510 | srcIndex = maxSrc; |
| 8511 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 8512 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 8513 | } |
| 8514 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8515 | status_t AudioPolicyManager::checkAndSetVolume(IVolumeCurves &curves, |
| 8516 | VolumeSource volumeSource, |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 8517 | int index, |
| 8518 | const sp<AudioOutputDescriptor>& outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8519 | DeviceTypeSet deviceTypes, |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 8520 | int delayMs, |
| 8521 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8522 | { |
Mikhail Naganov | 8b648e5 | 2024-09-06 11:22:13 -0700 | [diff] [blame] | 8523 | // APM is single threaded, and single instance. |
| 8524 | static std::set<IVolumeCurves*> invalidCurvesReported; |
| 8525 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8526 | // do not change actual attributes volume if the attributes is muted |
| 8527 | if (outputDesc->isMuted(volumeSource)) { |
| 8528 | ALOGVV("%s: volume source %d muted count %d active=%d", __func__, volumeSource, |
| 8529 | outputDesc->getMuteCount(volumeSource), outputDesc->isActive(volumeSource)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8530 | return NO_ERROR; |
| 8531 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8532 | |
Eric Laurent | ae6e88c | 2024-01-10 14:42:57 +0100 | [diff] [blame] | 8533 | bool isVoiceVolSrc; |
| 8534 | bool isBtScoVolSrc; |
| 8535 | if (!isVolumeConsistentForCalls( |
| 8536 | volumeSource, deviceTypes, isVoiceVolSrc, isBtScoVolSrc, __func__)) { |
Eric Laurent | 571ef96 | 2020-07-24 11:43:48 -0700 | [diff] [blame] | 8537 | // 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] | 8538 | // and Bluetooth SCO volumes due to stream aliasing. |
Eric Laurent | 571ef96 | 2020-07-24 11:43:48 -0700 | [diff] [blame] | 8539 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8540 | } |
Eric Laurent | ae6e88c | 2024-01-10 14:42:57 +0100 | [diff] [blame] | 8541 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8542 | if (deviceTypes.empty()) { |
| 8543 | deviceTypes = outputDesc->devices().types(); |
chenxin20 | 80986da | 2023-07-17 11:45:21 +0800 | [diff] [blame] | 8544 | index = curves.getVolumeIndex(deviceTypes); |
Mikhail Naganov | 0621c04 | 2024-06-05 11:43:22 -0700 | [diff] [blame] | 8545 | ALOGV("%s if deviceTypes is change from none to device %s, need get index %d", |
chenxin20 | 80986da | 2023-07-17 11:45:21 +0800 | [diff] [blame] | 8546 | __func__, dumpDeviceTypes(deviceTypes).c_str(), index); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 8547 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 8548 | |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 8549 | if (curves.getVolumeIndexMin() < 0 || curves.getVolumeIndexMax() < 0) { |
Mikhail Naganov | 8b648e5 | 2024-09-06 11:22:13 -0700 | [diff] [blame] | 8550 | if (!invalidCurvesReported.count(&curves)) { |
| 8551 | invalidCurvesReported.insert(&curves); |
| 8552 | String8 dump; |
| 8553 | curves.dump(&dump); |
| 8554 | ALOGE("invalid volume index range in the curve:\n%s", dump.c_str()); |
| 8555 | } |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 8556 | return BAD_VALUE; |
| 8557 | } |
| 8558 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8559 | float volumeDb = computeVolume(curves, volumeSource, index, deviceTypes); |
Eric Laurent | c32e669 | 2024-10-04 14:32:28 +0000 | [diff] [blame] | 8560 | const VolumeSource dtmfVolSrc = toVolumeSource(AUDIO_STREAM_DTMF, false); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8561 | if (outputDesc->isFixedVolume(deviceTypes) || |
chenxin20 | 9555903 | 2024-06-15 13:59:29 +0800 | [diff] [blame] | 8562 | // Force VoIP volume to max for bluetooth SCO/BLE device except if muted |
Eric Laurent | c32e669 | 2024-10-04 14:32:28 +0000 | [diff] [blame] | 8563 | (index != 0 && (isVoiceVolSrc || isBtScoVolSrc |
| 8564 | || (isInCall() && (dtmfVolSrc == volumeSource))) && |
chenxin20 | 9555903 | 2024-06-15 13:59:29 +0800 | [diff] [blame] | 8565 | (isSingleDeviceType(deviceTypes, audio_is_bluetooth_out_sco_device) |
| 8566 | || isSingleDeviceType(deviceTypes, audio_is_ble_out_device)))) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 8567 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 8568 | } |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 8569 | const bool muted = (index == 0) && (volumeDb != 0.0f); |
Eric Laurent | 31a428a | 2023-08-11 12:16:28 +0200 | [diff] [blame] | 8570 | outputDesc->setVolume(volumeDb, muted, volumeSource, curves.getStreamTypes(), |
| 8571 | deviceTypes, delayMs, force, isVoiceVolSrc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 8572 | |
Eric Laurent | e8f2c0f | 2021-08-17 11:17:19 +0200 | [diff] [blame] | 8573 | if (outputDesc == mPrimaryOutput && (isVoiceVolSrc || isBtScoVolSrc)) { |
chenxin20 | 9555903 | 2024-06-15 13:59:29 +0800 | [diff] [blame] | 8574 | bool voiceVolumeManagedByHost = isVoiceVolSrc && |
| 8575 | !isSingleDeviceType(deviceTypes, audio_is_ble_out_device); |
| 8576 | setVoiceVolume(index, curves, voiceVolumeManagedByHost, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8577 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8578 | return NO_ERROR; |
| 8579 | } |
| 8580 | |
Eric Laurent | ae6e88c | 2024-01-10 14:42:57 +0100 | [diff] [blame] | 8581 | void AudioPolicyManager::setVoiceVolume( |
chenxin20 | 9555903 | 2024-06-15 13:59:29 +0800 | [diff] [blame] | 8582 | int index, IVolumeCurves &curves, bool voiceVolumeManagedByHost, int delayMs) { |
Eric Laurent | ae6e88c | 2024-01-10 14:42:57 +0100 | [diff] [blame] | 8583 | float voiceVolume; |
chenxin20 | 9555903 | 2024-06-15 13:59:29 +0800 | [diff] [blame] | 8584 | // Force voice volume to max or mute for Bluetooth SCO/BLE as other attenuations are managed |
Eric Laurent | ae6e88c | 2024-01-10 14:42:57 +0100 | [diff] [blame] | 8585 | // by the headset |
chenxin20 | 9555903 | 2024-06-15 13:59:29 +0800 | [diff] [blame] | 8586 | if (voiceVolumeManagedByHost) { |
Eric Laurent | ae6e88c | 2024-01-10 14:42:57 +0100 | [diff] [blame] | 8587 | voiceVolume = (float)index/(float)curves.getVolumeIndexMax(); |
| 8588 | } else { |
| 8589 | voiceVolume = index == 0 ? 0.0 : 1.0; |
| 8590 | } |
| 8591 | if (voiceVolume != mLastVoiceVolume) { |
| 8592 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 8593 | mLastVoiceVolume = voiceVolume; |
| 8594 | } |
| 8595 | } |
| 8596 | |
| 8597 | bool AudioPolicyManager::isVolumeConsistentForCalls(VolumeSource volumeSource, |
| 8598 | const DeviceTypeSet& deviceTypes, |
| 8599 | bool& isVoiceVolSrc, |
| 8600 | bool& isBtScoVolSrc, |
| 8601 | const char* caller) { |
| 8602 | const VolumeSource callVolSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL, false); |
Vlad Popa | 695b76b | 2024-06-14 16:49:25 -0700 | [diff] [blame] | 8603 | isVoiceVolSrc = (volumeSource != VOLUME_SOURCE_NONE) && (callVolSrc == volumeSource); |
| 8604 | |
Eric Laurent | ae6e88c | 2024-01-10 14:42:57 +0100 | [diff] [blame] | 8605 | const bool isScoRequested = isScoRequestedForComm(); |
| 8606 | const bool isHAUsed = isHearingAidUsedForComm(); |
| 8607 | |
Vlad Popa | 695b76b | 2024-06-14 16:49:25 -0700 | [diff] [blame] | 8608 | if (com_android_media_audio_replace_stream_bt_sco()) { |
| 8609 | ALOGV("%s stream bt sco is replaced, no volume consistency check for calls", __func__); |
| 8610 | isBtScoVolSrc = (volumeSource != VOLUME_SOURCE_NONE) && (callVolSrc == volumeSource) && |
| 8611 | (isScoRequested || isHAUsed); |
| 8612 | return true; |
| 8613 | } |
| 8614 | |
| 8615 | const VolumeSource btScoVolSrc = toVolumeSource(AUDIO_STREAM_BLUETOOTH_SCO, false); |
Eric Laurent | ae6e88c | 2024-01-10 14:42:57 +0100 | [diff] [blame] | 8616 | isBtScoVolSrc = (volumeSource != VOLUME_SOURCE_NONE) && (btScoVolSrc == volumeSource); |
| 8617 | |
| 8618 | if ((callVolSrc != btScoVolSrc) && |
| 8619 | ((isVoiceVolSrc && isScoRequested) || |
| 8620 | (isBtScoVolSrc && !(isScoRequested || isHAUsed))) && |
| 8621 | !isSingleDeviceType(deviceTypes, AUDIO_DEVICE_OUT_TELEPHONY_TX)) { |
| 8622 | ALOGV("%s cannot set volume group %d volume when is%srequested for comm", caller, |
| 8623 | volumeSource, isScoRequested ? " " : " not "); |
| 8624 | return false; |
| 8625 | } |
| 8626 | return true; |
| 8627 | } |
| 8628 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 8629 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8630 | const DeviceTypeSet& deviceTypes, |
| 8631 | int delayMs, |
| 8632 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8633 | { |
jiabin | cd51052 | 2020-01-22 09:40:55 -0800 | [diff] [blame] | 8634 | ALOGVV("applyStreamVolumes() for device %s", dumpDeviceTypes(deviceTypes).c_str()); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8635 | for (const auto &volumeGroup : mEngine->getVolumeGroups()) { |
| 8636 | auto &curves = getVolumeCurves(toVolumeSource(volumeGroup)); |
| 8637 | checkAndSetVolume(curves, toVolumeSource(volumeGroup), |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8638 | curves.getVolumeIndex(deviceTypes), |
| 8639 | outputDesc, deviceTypes, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8640 | } |
| 8641 | } |
| 8642 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 8643 | void AudioPolicyManager::setStrategyMute(product_strategy_t strategy, |
| 8644 | bool on, |
| 8645 | const sp<AudioOutputDescriptor>& outputDesc, |
| 8646 | int delayMs, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8647 | DeviceTypeSet deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8648 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8649 | std::vector<VolumeSource> sourcesToMute; |
| 8650 | for (auto attributes: mEngine->getAllAttributesForProductStrategy(strategy)) { |
| 8651 | ALOGVV("%s() attributes %s, mute %d, output ID %d", __func__, |
| 8652 | toString(attributes).c_str(), on, outputDesc->getId()); |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 8653 | VolumeSource source = toVolumeSource(attributes, false); |
| 8654 | if ((source != VOLUME_SOURCE_NONE) && |
| 8655 | (std::find(begin(sourcesToMute), end(sourcesToMute), source) |
| 8656 | == end(sourcesToMute))) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8657 | sourcesToMute.push_back(source); |
| 8658 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8659 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8660 | for (auto source : sourcesToMute) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8661 | setVolumeSourceMute(source, on, outputDesc, delayMs, deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8662 | } |
| 8663 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8664 | } |
| 8665 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8666 | void AudioPolicyManager::setVolumeSourceMute(VolumeSource volumeSource, |
| 8667 | bool on, |
| 8668 | const sp<AudioOutputDescriptor>& outputDesc, |
| 8669 | int delayMs, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8670 | DeviceTypeSet deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8671 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8672 | if (deviceTypes.empty()) { |
| 8673 | deviceTypes = outputDesc->devices().types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8674 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8675 | auto &curves = getVolumeCurves(volumeSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8676 | if (on) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8677 | if (!outputDesc->isMuted(volumeSource)) { |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 8678 | if (curves.canBeMuted() && |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 8679 | (volumeSource != toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8680 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == |
| 8681 | AUDIO_POLICY_FORCE_NONE))) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8682 | checkAndSetVolume(curves, volumeSource, 0, outputDesc, deviceTypes, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8683 | } |
| 8684 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8685 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not |
| 8686 | // ignored |
| 8687 | outputDesc->incMuteCount(volumeSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8688 | } else { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8689 | if (!outputDesc->isMuted(volumeSource)) { |
| 8690 | ALOGV("%s unmuting non muted attributes!", __func__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8691 | return; |
| 8692 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 8693 | if (outputDesc->decMuteCount(volumeSource) == 0) { |
| 8694 | checkAndSetVolume(curves, volumeSource, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8695 | curves.getVolumeIndex(deviceTypes), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 8696 | outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 8697 | deviceTypes, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 8698 | delayMs); |
| 8699 | } |
| 8700 | } |
| 8701 | } |
| 8702 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 8703 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 8704 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 8705 | // has flags that map to a stream type? |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 8706 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 8707 | return true; |
| 8708 | } |
| 8709 | |
| 8710 | // has known usage? |
| 8711 | switch (paa->usage) { |
| 8712 | case AUDIO_USAGE_UNKNOWN: |
| 8713 | case AUDIO_USAGE_MEDIA: |
| 8714 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 8715 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 8716 | case AUDIO_USAGE_ALARM: |
| 8717 | case AUDIO_USAGE_NOTIFICATION: |
| 8718 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 8719 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 8720 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 8721 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 8722 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 8723 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 8724 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 8725 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 8726 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 8727 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 8728 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | 21777f8 | 2019-12-06 18:12:06 -0800 | [diff] [blame] | 8729 | case AUDIO_USAGE_CALL_ASSISTANT: |
Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 8730 | case AUDIO_USAGE_EMERGENCY: |
| 8731 | case AUDIO_USAGE_SAFETY: |
| 8732 | case AUDIO_USAGE_VEHICLE_STATUS: |
| 8733 | case AUDIO_USAGE_ANNOUNCEMENT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 8734 | break; |
| 8735 | default: |
| 8736 | return false; |
| 8737 | } |
| 8738 | return true; |
| 8739 | } |
| 8740 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 8741 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 8742 | { |
| 8743 | return mEngine->getForceUse(usage); |
| 8744 | } |
| 8745 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 8746 | bool AudioPolicyManager::isInCall() const { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 8747 | return isStateInCall(mEngine->getPhoneState()); |
| 8748 | } |
| 8749 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 8750 | bool AudioPolicyManager::isStateInCall(int state) const { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 8751 | return is_state_in_call(state); |
| 8752 | } |
| 8753 | |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 8754 | bool AudioPolicyManager::isCallAudioAccessible() const { |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 8755 | audio_mode_t mode = mEngine->getPhoneState(); |
| 8756 | return (mode == AUDIO_MODE_IN_CALL) |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 8757 | || (mode == AUDIO_MODE_CALL_SCREEN) |
| 8758 | || (mode == AUDIO_MODE_CALL_REDIRECT); |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 8759 | } |
| 8760 | |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 8761 | bool AudioPolicyManager::isInCallOrScreening() const { |
| 8762 | audio_mode_t mode = mEngine->getPhoneState(); |
| 8763 | return isStateInCall(mode) || mode == AUDIO_MODE_CALL_SCREEN; |
| 8764 | } |
| 8765 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8766 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 8767 | { |
| 8768 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 8769 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 8770 | if (sourceDesc->isConnected() && (sourceDesc->srcDevice()->equals(deviceDesc) || |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 8771 | sourceDesc->sinkDevice()->equals(deviceDesc)) |
Eric Laurent | ccbd787 | 2024-06-20 12:34:15 +0000 | [diff] [blame] | 8772 | && !sourceDesc->isCallRx()) { |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 8773 | disconnectAudioSource(sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8774 | } |
| 8775 | } |
| 8776 | |
| 8777 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 8778 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 8779 | bool release = false; |
| 8780 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 8781 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 8782 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 8783 | source->ext.device.type == deviceDesc->type()) { |
| 8784 | release = true; |
| 8785 | } |
| 8786 | } |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 8787 | const char *address = deviceDesc->address().c_str(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8788 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 8789 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 8790 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 8791 | sink->ext.device.type == deviceDesc->type() && |
| 8792 | (strnlen(address, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0 |
| 8793 | || strncmp(sink->ext.device.address, address, |
| 8794 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8795 | release = true; |
| 8796 | } |
| 8797 | } |
| 8798 | if (release) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8799 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->getHandle()); |
| 8800 | releaseAudioPatch(patchDesc->getHandle(), patchDesc->getUid()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8801 | } |
| 8802 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 8803 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 8804 | mInputs.clearSessionRoutesForDevice(deviceDesc); |
| 8805 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 8806 | mHwModules.cleanUpForDevice(deviceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8807 | } |
| 8808 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8809 | void AudioPolicyManager::modifySurroundFormats( |
| 8810 | const sp<DeviceDescriptor>& devDesc, FormatVector *formatsPtr) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8811 | std::unordered_set<audio_format_t> enforcedSurround( |
| 8812 | devDesc->encodedFormats().begin(), devDesc->encodedFormats().end()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8813 | 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] | 8814 | for (const auto& pair : mConfig->getSurroundFormats()) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8815 | allSurround.insert(pair.first); |
| 8816 | for (const auto& subformat : pair.second) allSurround.insert(subformat); |
| 8817 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8818 | |
| 8819 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 8820 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8821 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8822 | // This is the resulting set of formats depending on the surround mode: |
| 8823 | // 'all surround' = allSurround |
| 8824 | // 'enforced surround' = enforcedSurround [may include IEC69137 which isn't raw surround fmt] |
| 8825 | // 'non-surround' = not in 'all surround' and not in 'enforced surround' |
| 8826 | // 'manual surround' = mManualSurroundFormats |
| 8827 | // AUTO: formats v 'enforced surround' |
| 8828 | // ALWAYS: formats v 'all surround' v 'enforced surround' |
| 8829 | // NEVER: formats ^ 'non-surround' |
| 8830 | // MANUAL: formats ^ ('non-surround' v 'manual surround' v (IEC69137 ^ 'enforced surround')) |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8831 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8832 | std::unordered_set<audio_format_t> formatSet; |
| 8833 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL |
| 8834 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8835 | // formatSet is (formats ^ 'non-surround') |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8836 | for (auto formatIter = formatsPtr->begin(); formatIter != formatsPtr->end(); ++formatIter) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8837 | if (allSurround.count(*formatIter) == 0 && enforcedSurround.count(*formatIter) == 0) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8838 | formatSet.insert(*formatIter); |
| 8839 | } |
| 8840 | } |
| 8841 | } else { |
| 8842 | formatSet.insert(formatsPtr->begin(), formatsPtr->end()); |
| 8843 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8844 | formatsPtr->clear(); // Re-filled from the formatSet at the end. |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8845 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 8846 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8847 | formatSet.insert(mManualSurroundFormats.begin(), mManualSurroundFormats.end()); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8848 | // Enable IEC61937 when in MANUAL mode if it's enforced for this device. |
| 8849 | if (enforcedSurround.count(AUDIO_FORMAT_IEC61937) != 0) { |
| 8850 | formatSet.insert(AUDIO_FORMAT_IEC61937); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8851 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8852 | } else if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { // AUTO or ALWAYS |
| 8853 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 8854 | formatSet.insert(allSurround.begin(), allSurround.end()); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 8855 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8856 | formatSet.insert(enforcedSurround.begin(), enforcedSurround.end()); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8857 | } |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8858 | for (const auto& format : formatSet) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8859 | formatsPtr->push_back(format); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8860 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8861 | } |
| 8862 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8863 | void AudioPolicyManager::modifySurroundChannelMasks(ChannelMaskSet *channelMasksPtr) { |
| 8864 | ChannelMaskSet &channelMasks = *channelMasksPtr; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8865 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 8866 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 8867 | |
| 8868 | // If NEVER, then remove support for channelMasks > stereo. |
| 8869 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8870 | for (auto it = channelMasks.begin(); it != channelMasks.end();) { |
| 8871 | audio_channel_mask_t channelMask = *it; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8872 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 8873 | ALOGV("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8874 | it = channelMasks.erase(it); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8875 | } else { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8876 | ++it; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8877 | } |
| 8878 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 8879 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 8880 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 8881 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8882 | bool supports5dot1 = false; |
| 8883 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 8884 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8885 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 8886 | supports5dot1 = true; |
| 8887 | break; |
| 8888 | } |
| 8889 | } |
| 8890 | // If not then add 5.1 support. |
| 8891 | if (!supports5dot1) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8892 | channelMasks.insert(AUDIO_CHANNEL_OUT_5POINT1); |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 8893 | 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] | 8894 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8895 | } |
| 8896 | } |
| 8897 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8898 | void AudioPolicyManager::updateAudioProfiles(const sp<DeviceDescriptor>& devDesc, |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 8899 | audio_io_handle_t ioHandle, |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 8900 | const sp<IOProfile>& profile) { |
| 8901 | if (!profile->hasDynamicAudioProfile()) { |
| 8902 | return; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8903 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8904 | |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 8905 | audio_port_v7 devicePort; |
| 8906 | devDesc->toAudioPort(&devicePort); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8907 | |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 8908 | audio_port_v7 mixPort; |
| 8909 | profile->toAudioPort(&mixPort); |
| 8910 | mixPort.ext.mix.handle = ioHandle; |
| 8911 | |
| 8912 | status_t status = mpClientInterface->getAudioMixPort(&devicePort, &mixPort); |
| 8913 | if (status != NO_ERROR) { |
| 8914 | ALOGE("%s failed to query the attributes of the mix port", __func__); |
| 8915 | return; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8916 | } |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 8917 | |
| 8918 | std::set<audio_format_t> supportedFormats; |
| 8919 | for (size_t i = 0; i < mixPort.num_audio_profiles; ++i) { |
| 8920 | supportedFormats.insert(mixPort.audio_profiles[i].format); |
| 8921 | } |
| 8922 | FormatVector formats(supportedFormats.begin(), supportedFormats.end()); |
| 8923 | mReportedFormatsMap[devDesc] = formats; |
| 8924 | |
| 8925 | if (devDesc->type() == AUDIO_DEVICE_OUT_HDMI || |
hongchao.yin | f0c8208 | 2024-07-24 19:41:02 +0800 | [diff] [blame] | 8926 | devDesc->type() == AUDIO_DEVICE_OUT_HDMI_ARC || |
| 8927 | devDesc->type() == AUDIO_DEVICE_OUT_HDMI_EARC || |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 8928 | isDeviceOfModule(devDesc,AUDIO_HARDWARE_MODULE_ID_MSD)) { |
| 8929 | modifySurroundFormats(devDesc, &formats); |
| 8930 | size_t modifiedNumProfiles = 0; |
| 8931 | for (size_t i = 0; i < mixPort.num_audio_profiles; ++i) { |
| 8932 | if (std::find(formats.begin(), formats.end(), mixPort.audio_profiles[i].format) == |
| 8933 | formats.end()) { |
| 8934 | // Skip the format that is not present after modifying surround formats. |
| 8935 | continue; |
| 8936 | } |
| 8937 | memcpy(&mixPort.audio_profiles[modifiedNumProfiles], &mixPort.audio_profiles[i], |
| 8938 | sizeof(struct audio_profile)); |
| 8939 | ChannelMaskSet channels(mixPort.audio_profiles[modifiedNumProfiles].channel_masks, |
| 8940 | mixPort.audio_profiles[modifiedNumProfiles].channel_masks + |
| 8941 | mixPort.audio_profiles[modifiedNumProfiles].num_channel_masks); |
| 8942 | modifySurroundChannelMasks(&channels); |
| 8943 | std::copy(channels.begin(), channels.end(), |
| 8944 | std::begin(mixPort.audio_profiles[modifiedNumProfiles].channel_masks)); |
| 8945 | mixPort.audio_profiles[modifiedNumProfiles++].num_channel_masks = channels.size(); |
| 8946 | } |
| 8947 | mixPort.num_audio_profiles = modifiedNumProfiles; |
| 8948 | } |
| 8949 | profile->importAudioPort(mixPort); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8950 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8951 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8952 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 8953 | audio_patch_handle_t *patchHandle, |
| 8954 | AudioIODescriptorInterface *ioDescriptor, |
| 8955 | const struct audio_patch *patch, |
| 8956 | int delayMs) |
| 8957 | { |
| 8958 | ssize_t index = mAudioPatches.indexOfKey( |
| 8959 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 8960 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 8961 | sp<AudioPatch> patchDesc; |
| 8962 | status_t status = installPatch( |
| 8963 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 8964 | if (status == NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8965 | ioDescriptor->setPatchHandle(patchDesc->getHandle()); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8966 | } |
| 8967 | return status; |
| 8968 | } |
| 8969 | |
| 8970 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 8971 | ssize_t index, |
| 8972 | audio_patch_handle_t *patchHandle, |
| 8973 | const struct audio_patch *patch, |
| 8974 | int delayMs, |
| 8975 | uid_t uid, |
| 8976 | sp<AudioPatch> *patchDescPtr) |
| 8977 | { |
| 8978 | sp<AudioPatch> patchDesc; |
| 8979 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 8980 | if (index >= 0) { |
| 8981 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8982 | afPatchHandle = patchDesc->getAfHandle(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8983 | } |
| 8984 | |
| 8985 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 8986 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 8987 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 8988 | if (status == NO_ERROR) { |
| 8989 | if (index < 0) { |
| 8990 | patchDesc = new AudioPatch(patch, uid); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8991 | addAudioPatch(patchDesc->getHandle(), patchDesc); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8992 | } else { |
| 8993 | patchDesc->mPatch = *patch; |
| 8994 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8995 | patchDesc->setAfHandle(afPatchHandle); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8996 | if (patchHandle) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8997 | *patchHandle = patchDesc->getHandle(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8998 | } |
| 8999 | nextAudioPortGeneration(); |
| 9000 | mpClientInterface->onAudioPatchListUpdate(); |
| 9001 | } |
| 9002 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 9003 | return status; |
| 9004 | } |
| 9005 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 9006 | bool AudioPolicyManager::areAllActiveTracksRerouted(const sp<SwAudioOutputDescriptor>& output) |
| 9007 | { |
| 9008 | const TrackClientVector activeClients = output->getActiveClients(); |
| 9009 | if (activeClients.empty()) { |
| 9010 | return true; |
| 9011 | } |
| 9012 | ssize_t index = mAudioPatches.indexOfKey(output->getPatchHandle()); |
| 9013 | if (index < 0) { |
| 9014 | ALOGE("%s, no audio patch found while there are active clients on output %d", |
| 9015 | __func__, output->getId()); |
| 9016 | return false; |
| 9017 | } |
| 9018 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 9019 | DeviceVector routedDevices; |
| 9020 | for (int i = 0; i < patchDesc->mPatch.num_sinks; ++i) { |
| 9021 | sp<DeviceDescriptor> device = mAvailableOutputDevices.getDeviceFromId( |
| 9022 | patchDesc->mPatch.sinks[i].id); |
| 9023 | if (device == nullptr) { |
| 9024 | ALOGE("%s, no audio device found with id(%d)", |
| 9025 | __func__, patchDesc->mPatch.sinks[i].id); |
| 9026 | return false; |
| 9027 | } |
| 9028 | routedDevices.add(device); |
| 9029 | } |
| 9030 | for (const auto& client : activeClients) { |
jiabin | 4925685 | 2022-03-09 11:21:35 -0800 | [diff] [blame] | 9031 | if (client->isInvalid()) { |
| 9032 | // No need to take care about invalidated clients. |
| 9033 | continue; |
| 9034 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 9035 | sp<DeviceDescriptor> preferredDevice = |
| 9036 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId()); |
| 9037 | if (mEngine->getOutputDevicesForAttributes( |
| 9038 | client->attributes(), preferredDevice, false) == routedDevices) { |
| 9039 | return false; |
| 9040 | } |
| 9041 | } |
| 9042 | return true; |
| 9043 | } |
| 9044 | |
| 9045 | sp<SwAudioOutputDescriptor> AudioPolicyManager::openOutputWithProfileAndDevice( |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 9046 | const sp<IOProfile>& profile, const DeviceVector& devices, |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 9047 | const audio_config_base_t *mixerConfig, const audio_config_t *halConfig, |
| 9048 | audio_output_flags_t flags) |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 9049 | { |
| 9050 | for (const auto& device : devices) { |
| 9051 | // TODO: This should be checking if the profile supports the device combo. |
| 9052 | if (!profile->supportsDevice(device)) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 9053 | ALOGE("%s profile(%s) doesn't support device %#x", __func__, profile->getName().c_str(), |
| 9054 | device->type()); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 9055 | return nullptr; |
| 9056 | } |
| 9057 | } |
| 9058 | sp<SwAudioOutputDescriptor> desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 9059 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Haofan Wang | f6e304f | 2024-07-09 23:06:58 -0700 | [diff] [blame] | 9060 | audio_attributes_t attributes = AUDIO_ATTRIBUTES_INITIALIZER; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 9061 | status_t status = desc->open(halConfig, mixerConfig, devices, |
Dean Wheatley | dfb67b8 | 2024-01-23 09:36:29 +1100 | [diff] [blame] | 9062 | AUDIO_STREAM_DEFAULT, &flags, &output, attributes); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 9063 | if (status != NO_ERROR) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 9064 | ALOGE("%s failed to open output %d", __func__, status); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 9065 | return nullptr; |
| 9066 | } |
jiabin | 14b50cc | 2023-12-13 19:01:52 +0000 | [diff] [blame] | 9067 | if ((flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) == AUDIO_OUTPUT_FLAG_BIT_PERFECT) { |
| 9068 | auto portConfig = desc->getConfig(); |
| 9069 | for (const auto& device : devices) { |
| 9070 | device->setPreferredConfig(&portConfig); |
| 9071 | } |
| 9072 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 9073 | |
| 9074 | // Here is where the out_set_parameters() for card & device gets called |
| 9075 | sp<DeviceDescriptor> device = devices.getDeviceForOpening(); |
| 9076 | const audio_devices_t deviceType = device->type(); |
| 9077 | const String8 &address = String8(device->address().c_str()); |
Tomasz Wasilczyk | fd9ffd1 | 2023-08-14 17:56:22 +0000 | [diff] [blame] | 9078 | if (!address.empty()) { |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 9079 | char *param = audio_device_address_to_parameter(deviceType, address.c_str()); |
| 9080 | mpClientInterface->setParameters(output, String8(param)); |
| 9081 | free(param); |
| 9082 | } |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 9083 | updateAudioProfiles(device, output, profile); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 9084 | if (!profile->hasValidAudioProfile()) { |
| 9085 | ALOGW("%s() missing param", __func__); |
| 9086 | desc->close(); |
| 9087 | return nullptr; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 9088 | } else if (profile->hasDynamicAudioProfile() && halConfig == nullptr) { |
| 9089 | // Reopen the output with the best audio profile picked by APM when the profile supports |
| 9090 | // dynamic audio profile and the hal config is not specified. |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 9091 | desc->close(); |
| 9092 | output = AUDIO_IO_HANDLE_NONE; |
| 9093 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 9094 | profile->pickAudioProfile( |
| 9095 | config.sample_rate, config.channel_mask, config.format); |
| 9096 | config.offload_info.sample_rate = config.sample_rate; |
| 9097 | config.offload_info.channel_mask = config.channel_mask; |
| 9098 | config.offload_info.format = config.format; |
| 9099 | |
Dean Wheatley | dfb67b8 | 2024-01-23 09:36:29 +1100 | [diff] [blame] | 9100 | status = desc->open(&config, mixerConfig, devices, AUDIO_STREAM_DEFAULT, &flags, &output, |
Haofan Wang | f6e304f | 2024-07-09 23:06:58 -0700 | [diff] [blame] | 9101 | attributes); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 9102 | if (status != NO_ERROR) { |
| 9103 | return nullptr; |
| 9104 | } |
| 9105 | } |
| 9106 | |
| 9107 | addOutput(output, desc); |
Mikhail Naganov | ccd149c | 2024-09-26 14:16:13 -0700 | [diff] [blame] | 9108 | // The version check is essentially to avoid making this call in the case of the HIDL HAL. |
| 9109 | if (auto hwModule = mHwModules.getModuleFromHandle(mPrimaryModuleHandle); hwModule && |
| 9110 | hwModule->getHalVersionMajor() >= 3) { |
| 9111 | setOutputDevices(__func__, desc, devices, true, 0, NULL); |
| 9112 | } |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 9113 | sp<DeviceDescriptor> speaker = mAvailableOutputDevices.getDevice( |
| 9114 | AUDIO_DEVICE_OUT_SPEAKER, String8(""), AUDIO_FORMAT_DEFAULT); |
| 9115 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 9116 | if (audio_is_remote_submix_device(deviceType) && address != "0") { |
| 9117 | sp<AudioPolicyMix> policyMix; |
| 9118 | if (mPolicyMixes.getAudioPolicyMix(deviceType, address, policyMix) == NO_ERROR) { |
| 9119 | policyMix->setOutput(desc); |
| 9120 | desc->mPolicyMix = policyMix; |
| 9121 | } else { |
| 9122 | ALOGW("checkOutputsForDevice() cannot find policy for address %s", |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 9123 | address.c_str()); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 9124 | } |
| 9125 | |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 9126 | } else if (hasPrimaryOutput() && speaker != nullptr |
| 9127 | && mPrimaryOutput->supportsDevice(speaker) && !desc->supportsDevice(speaker) |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 9128 | && ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) { |
| 9129 | // no duplicated output for: |
| 9130 | // - direct outputs |
| 9131 | // - outputs used by dynamic policy mixes |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 9132 | // - outputs that supports SPEAKER while the primary output does not. |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 9133 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
| 9134 | |
| 9135 | //TODO: configure audio effect output stage here |
| 9136 | |
| 9137 | // open a duplicating output thread for the new output and the primary output |
| 9138 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 9139 | new SwAudioOutputDescriptor(nullptr, mpClientInterface); |
| 9140 | status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, &duplicatedOutput); |
| 9141 | if (status == NO_ERROR) { |
| 9142 | // add duplicated output descriptor |
| 9143 | addOutput(duplicatedOutput, dupOutputDesc); |
| 9144 | } else { |
| 9145 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
| 9146 | mPrimaryOutput->mIoHandle, output); |
| 9147 | desc->close(); |
| 9148 | removeOutput(output); |
| 9149 | nextAudioPortGeneration(); |
| 9150 | return nullptr; |
| 9151 | } |
| 9152 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 9153 | if (mPrimaryOutput == nullptr && profile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 9154 | ALOGV("%s(): re-assigning mPrimaryOutput", __func__); |
| 9155 | mPrimaryOutput = desc; |
François Gaffie | db1755b | 2023-09-01 11:50:35 +0200 | [diff] [blame] | 9156 | mPrimaryModuleHandle = mPrimaryOutput->getModuleHandle(); |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 9157 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 9158 | return desc; |
| 9159 | } |
| 9160 | |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 9161 | status_t AudioPolicyManager::getDevicesForAttributes( |
| 9162 | const audio_attributes_t &attr, DeviceVector &devices, bool forVolume) { |
wenyu zhang | 8558a33 | 2024-09-09 15:12:48 +0000 | [diff] [blame] | 9163 | // attr containing source set by AudioAttributes.Builder.setCapturePreset() has precedence |
| 9164 | // over any usage or content type also present in attr. |
| 9165 | if (com::android::media::audioserver::enable_audio_input_device_routing() && |
| 9166 | attr.source != AUDIO_SOURCE_INVALID) { |
| 9167 | return getInputDevicesForAttributes(attr, devices); |
| 9168 | } |
| 9169 | |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 9170 | // Devices are determined in the following precedence: |
| 9171 | // |
| 9172 | // 1) Devices associated with a dynamic policy matching the attributes. This is often |
| 9173 | // a remote submix from MIX_ROUTE_FLAG_LOOP_BACK. |
| 9174 | // |
| 9175 | // If no such dynamic policy then |
| 9176 | // 2) Devices containing an active client using setPreferredDevice |
| 9177 | // with same strategy as the attributes. |
| 9178 | // (from the default Engine::getOutputDevicesForAttributes() implementation). |
| 9179 | // |
| 9180 | // If no corresponding active client with setPreferredDevice then |
| 9181 | // 3) Devices associated with the strategy determined by the attributes |
| 9182 | // (from the default Engine::getOutputDevicesForAttributes() implementation). |
| 9183 | // |
| 9184 | // See related getOutputForAttrInt(). |
| 9185 | |
| 9186 | // check dynamic policies but only for primary descriptors (secondary not used for audible |
| 9187 | // audio routing, only used for duplication for playback capture) |
| 9188 | sp<AudioPolicyMix> policyMix; |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 9189 | bool unneededUsePrimaryOutputFromPolicyMixes = false; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 9190 | status_t status = mPolicyMixes.getOutputForAttr(attr, AUDIO_CONFIG_BASE_INITIALIZER, |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 9191 | 0 /*uid unknown here*/, AUDIO_SESSION_NONE, AUDIO_OUTPUT_FLAG_NONE, |
| 9192 | mAvailableOutputDevices, nullptr /* requestedDevice */, policyMix, |
| 9193 | nullptr /* secondaryMixes */, unneededUsePrimaryOutputFromPolicyMixes); |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 9194 | if (status != OK) { |
| 9195 | return status; |
| 9196 | } |
| 9197 | |
| 9198 | if (policyMix != nullptr && policyMix->getOutput() != nullptr && |
| 9199 | // For volume control, skip LOOPBACK mixes which use AUDIO_DEVICE_OUT_REMOTE_SUBMIX |
| 9200 | // as they are unaffected by device/stream volume |
| 9201 | // (per SwAudioOutputDescriptor::isFixedVolume()). |
| 9202 | (!forVolume || policyMix->mDeviceType != AUDIO_DEVICE_OUT_REMOTE_SUBMIX) |
| 9203 | ) { |
| 9204 | sp<DeviceDescriptor> deviceDesc = mAvailableOutputDevices.getDevice( |
| 9205 | policyMix->mDeviceType, policyMix->mDeviceAddress, AUDIO_FORMAT_DEFAULT); |
| 9206 | devices.add(deviceDesc); |
| 9207 | } else { |
| 9208 | // The default Engine::getOutputDevicesForAttributes() uses findPreferredDevice() |
| 9209 | // which selects setPreferredDevice if active. This means forVolume call |
| 9210 | // will take an active setPreferredDevice, if such exists. |
| 9211 | |
| 9212 | devices = mEngine->getOutputDevicesForAttributes( |
| 9213 | attr, nullptr /* preferredDevice */, false /* fromCache */); |
| 9214 | } |
| 9215 | |
| 9216 | if (forVolume) { |
| 9217 | // We alias the device AUDIO_DEVICE_OUT_SPEAKER_SAFE to AUDIO_DEVICE_OUT_SPEAKER |
| 9218 | // for single volume control in AudioService (such relationship should exist if |
| 9219 | // SPEAKER_SAFE is present). |
| 9220 | // |
| 9221 | // (This is unrelated to a different device grouping as Volume::getDeviceCategory) |
| 9222 | DeviceVector speakerSafeDevices = |
| 9223 | devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER_SAFE); |
| 9224 | if (!speakerSafeDevices.isEmpty()) { |
| 9225 | devices.merge(mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER)); |
| 9226 | devices.remove(speakerSafeDevices); |
| 9227 | } |
| 9228 | } |
| 9229 | |
| 9230 | return NO_ERROR; |
| 9231 | } |
| 9232 | |
wenyu zhang | 8558a33 | 2024-09-09 15:12:48 +0000 | [diff] [blame] | 9233 | status_t AudioPolicyManager::getInputDevicesForAttributes( |
| 9234 | const audio_attributes_t &attr, DeviceVector &devices) { |
| 9235 | devices = DeviceVector( |
| 9236 | mEngine->getInputDeviceForAttributes(attr, 0 /*uid unknown here*/, |
| 9237 | AUDIO_SESSION_NONE, |
| 9238 | nullptr /* mix */)); |
| 9239 | return NO_ERROR; |
| 9240 | } |
| 9241 | |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 9242 | status_t AudioPolicyManager::getProfilesForDevices(const DeviceVector& devices, |
| 9243 | AudioProfileVector& audioProfiles, |
| 9244 | uint32_t flags, |
| 9245 | bool isInput) { |
| 9246 | for (const auto& hwModule : mHwModules) { |
| 9247 | // the MSD module checks for different conditions |
| 9248 | if (strcmp(hwModule->getName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0) { |
| 9249 | continue; |
| 9250 | } |
| 9251 | IOProfileCollection ioProfiles = isInput ? hwModule->getInputProfiles() |
| 9252 | : hwModule->getOutputProfiles(); |
| 9253 | for (const auto& profile : ioProfiles) { |
| 9254 | if (!profile->areAllDevicesSupported(devices) || |
| 9255 | !profile->isCompatibleProfileForFlags( |
| 9256 | flags, false /*exactMatchRequiredForInputFlags*/)) { |
| 9257 | continue; |
| 9258 | } |
| 9259 | audioProfiles.addAllValidProfiles(profile->asAudioPort()->getAudioProfiles()); |
| 9260 | } |
| 9261 | } |
| 9262 | |
| 9263 | if (!isInput) { |
| 9264 | // add the direct profiles from MSD if present and has audio patches to all the output(s) |
| 9265 | const auto &msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 9266 | if (msdModule != nullptr) { |
| 9267 | if (msdHasPatchesToAllDevices(devices.toTypeAddrVector())) { |
| 9268 | ALOGV("%s: MSD audio patches set to all output devices.", __func__); |
| 9269 | for (const auto &profile: msdModule->getOutputProfiles()) { |
| 9270 | if (!profile->asAudioPort()->isDirectOutput()) { |
| 9271 | continue; |
| 9272 | } |
| 9273 | audioProfiles.addAllValidProfiles(profile->asAudioPort()->getAudioProfiles()); |
| 9274 | } |
| 9275 | } else { |
| 9276 | ALOGV("%s: MSD audio patches NOT set to all output devices.", __func__); |
| 9277 | } |
| 9278 | } |
| 9279 | } |
| 9280 | |
| 9281 | return NO_ERROR; |
| 9282 | } |
| 9283 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 9284 | sp<SwAudioOutputDescriptor> AudioPolicyManager::reopenOutput(sp<SwAudioOutputDescriptor> outputDesc, |
| 9285 | const audio_config_t *config, |
| 9286 | audio_output_flags_t flags, |
| 9287 | const char* caller) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 9288 | closeOutput(outputDesc->mIoHandle); |
| 9289 | sp<SwAudioOutputDescriptor> preferredOutput = openOutputWithProfileAndDevice( |
| 9290 | outputDesc->mProfile, outputDesc->devices(), nullptr /*mixerConfig*/, config, flags); |
| 9291 | if (preferredOutput == nullptr) { |
| 9292 | ALOGE("%s failed to reopen output device=%d, caller=%s", |
| 9293 | __func__, outputDesc->devices()[0]->getId(), caller); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 9294 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 9295 | return preferredOutput; |
| 9296 | } |
| 9297 | |
| 9298 | void AudioPolicyManager::reopenOutputsWithDevices( |
| 9299 | const std::map<audio_io_handle_t, DeviceVector> &outputsToReopen) { |
| 9300 | for (const auto& [output, devices] : outputsToReopen) { |
| 9301 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 9302 | closeOutput(output); |
| 9303 | openOutputWithProfileAndDevice(desc->mProfile, devices); |
| 9304 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 9305 | } |
| 9306 | |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 9307 | PortHandleVector AudioPolicyManager::getClientsForStream( |
| 9308 | audio_stream_type_t streamType) const { |
| 9309 | PortHandleVector clients; |
| 9310 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 9311 | PortHandleVector clientsForStream = mOutputs.valueAt(i)->getClientsForStream(streamType); |
| 9312 | clients.insert(clients.end(), clientsForStream.begin(), clientsForStream.end()); |
| 9313 | } |
| 9314 | return clients; |
| 9315 | } |
| 9316 | |
| 9317 | void AudioPolicyManager::invalidateStreams(StreamTypeVector streams) const { |
| 9318 | PortHandleVector clients; |
| 9319 | for (auto stream : streams) { |
| 9320 | PortHandleVector clientsForStream = getClientsForStream(stream); |
| 9321 | clients.insert(clients.end(), clientsForStream.begin(), clientsForStream.end()); |
| 9322 | } |
| 9323 | mpClientInterface->invalidateTracks(clients); |
| 9324 | } |
| 9325 | |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 9326 | void AudioPolicyManager::updateClientsInternalMute( |
| 9327 | const sp<android::SwAudioOutputDescriptor> &desc) { |
| 9328 | if (!desc->isBitPerfect() || |
| 9329 | !com::android::media::audioserver:: |
| 9330 | fix_concurrent_playback_behavior_with_bit_perfect_client()) { |
| 9331 | // This is only used for bit perfect output now. |
| 9332 | return; |
| 9333 | } |
| 9334 | sp<TrackClientDescriptor> bitPerfectClient = nullptr; |
| 9335 | bool bitPerfectClientInternalMute = false; |
| 9336 | std::vector<media::TrackInternalMuteInfo> clientsInternalMute; |
| 9337 | for (const sp<TrackClientDescriptor>& client : desc->getActiveClients()) { |
| 9338 | if ((client->flags() & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != AUDIO_OUTPUT_FLAG_NONE) { |
| 9339 | bitPerfectClient = client; |
| 9340 | continue; |
| 9341 | } |
| 9342 | bool muted = false; |
| 9343 | if (client->stream() == AUDIO_STREAM_SYSTEM) { |
| 9344 | // System sound is muted. |
| 9345 | muted = true; |
| 9346 | } else { |
| 9347 | bitPerfectClientInternalMute = true; |
| 9348 | } |
| 9349 | if (client->setInternalMute(muted)) { |
| 9350 | auto result = legacy2aidl_audio_port_handle_t_int32_t(client->portId()); |
| 9351 | if (!result.ok()) { |
| 9352 | ALOGE("%s, failed to convert port id(%d) to aidl", __func__, client->portId()); |
| 9353 | continue; |
| 9354 | } |
| 9355 | media::TrackInternalMuteInfo info; |
| 9356 | info.portId = result.value(); |
| 9357 | info.muted = client->getInternalMute(); |
| 9358 | clientsInternalMute.push_back(std::move(info)); |
| 9359 | } |
| 9360 | } |
| 9361 | if (bitPerfectClient != nullptr && |
| 9362 | bitPerfectClient->setInternalMute(bitPerfectClientInternalMute)) { |
| 9363 | auto result = legacy2aidl_audio_port_handle_t_int32_t(bitPerfectClient->portId()); |
| 9364 | if (result.ok()) { |
| 9365 | media::TrackInternalMuteInfo info; |
| 9366 | info.portId = result.value(); |
| 9367 | info.muted = bitPerfectClient->getInternalMute(); |
| 9368 | clientsInternalMute.push_back(std::move(info)); |
| 9369 | } else { |
| 9370 | ALOGE("%s, failed to convert port id(%d) of bit perfect client to aidl", |
| 9371 | __func__, bitPerfectClient->portId()); |
| 9372 | } |
| 9373 | } |
| 9374 | if (!clientsInternalMute.empty()) { |
| 9375 | if (status_t status = mpClientInterface->setTracksInternalMute(clientsInternalMute); |
| 9376 | status != NO_ERROR) { |
| 9377 | ALOGE("%s, failed to update tracks internal mute, err=%d", __func__, status); |
| 9378 | } |
| 9379 | } |
| 9380 | } |
| 9381 | |
jiabin | 7ed22ff | 2024-10-04 20:51:21 +0000 | [diff] [blame^] | 9382 | status_t AudioPolicyManager::getMmapPolicyInfos(AudioMMapPolicyType policyType, |
| 9383 | std::vector<AudioMMapPolicyInfo> *policyInfos) { |
| 9384 | if (policyType != AudioMMapPolicyType::DEFAULT && |
| 9385 | policyType != AudioMMapPolicyType::EXCLUSIVE) { |
| 9386 | return BAD_VALUE; |
| 9387 | } |
| 9388 | if (mMmapPolicyByDeviceType.count(policyType) == 0) { |
| 9389 | if (status_t status = updateMmapPolicyInfos(policyType); status != NO_ERROR) { |
| 9390 | return status; |
| 9391 | } |
| 9392 | } |
| 9393 | *policyInfos = mMmapPolicyInfos[policyType]; |
| 9394 | return NO_ERROR; |
| 9395 | } |
| 9396 | |
| 9397 | status_t AudioPolicyManager::getMmapPolicyForDevice( |
| 9398 | AudioMMapPolicyType policyType, AudioMMapPolicyInfo *policyInfo) { |
| 9399 | if (policyType != AudioMMapPolicyType::DEFAULT && |
| 9400 | policyType != AudioMMapPolicyType::EXCLUSIVE) { |
| 9401 | return BAD_VALUE; |
| 9402 | } |
| 9403 | if (mMmapPolicyByDeviceType.count(policyType) == 0) { |
| 9404 | if (status_t status = updateMmapPolicyInfos(policyType); status != NO_ERROR) { |
| 9405 | return status; |
| 9406 | } |
| 9407 | } |
| 9408 | auto it = mMmapPolicyByDeviceType[policyType].find(policyInfo->device.type); |
| 9409 | policyInfo->mmapPolicy = it == mMmapPolicyByDeviceType[policyType].end() |
| 9410 | ? AudioMMapPolicy::NEVER : it->second; |
| 9411 | return NO_ERROR; |
| 9412 | } |
| 9413 | |
| 9414 | status_t AudioPolicyManager::updateMmapPolicyInfos(AudioMMapPolicyType policyType) { |
| 9415 | std::vector<AudioMMapPolicyInfo> policyInfos; |
| 9416 | if (status_t status = mpClientInterface->getMmapPolicyInfos(policyType, &policyInfos); |
| 9417 | status != NO_ERROR) { |
| 9418 | ALOGE("%s, failed, error = %d", __func__, status); |
| 9419 | return status; |
| 9420 | } |
| 9421 | std::map<AudioDeviceDescription, AudioMMapPolicy> mmapPolicyByDeviceType; |
| 9422 | if (policyInfos.size() == 1 && policyInfos[0].device == AudioDevice()) { |
| 9423 | // When there is only one AudioMMapPolicyInfo instance and the device is a default value, |
| 9424 | // it indicates the mmap policy is reported via system property. In that case, use the |
| 9425 | // routing information to fill details for how mmap is supported for a particular device. |
| 9426 | for (const auto &hwModule: mHwModules) { |
| 9427 | for (const auto &profile: hwModule->getInputProfiles()) { |
| 9428 | if ((profile->getFlags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) |
| 9429 | != AUDIO_INPUT_FLAG_MMAP_NOIRQ) { |
| 9430 | continue; |
| 9431 | } |
| 9432 | for (const auto &device: profile->getSupportedDevices()) { |
| 9433 | auto deviceDesc = |
| 9434 | legacy2aidl_audio_devices_t_AudioDeviceDescription(device->type()); |
| 9435 | if (deviceDesc.ok()) { |
| 9436 | mmapPolicyByDeviceType.emplace( |
| 9437 | deviceDesc.value(), policyInfos[0].mmapPolicy); |
| 9438 | } |
| 9439 | } |
| 9440 | } |
| 9441 | for (const auto &profile: hwModule->getOutputProfiles()) { |
| 9442 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) |
| 9443 | != AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) { |
| 9444 | continue; |
| 9445 | } |
| 9446 | for (const auto &device: profile->getSupportedDevices()) { |
| 9447 | auto deviceDesc = |
| 9448 | legacy2aidl_audio_devices_t_AudioDeviceDescription(device->type()); |
| 9449 | if (deviceDesc.ok()) { |
| 9450 | mmapPolicyByDeviceType.emplace( |
| 9451 | deviceDesc.value(), policyInfos[0].mmapPolicy); |
| 9452 | } |
| 9453 | } |
| 9454 | } |
| 9455 | } |
| 9456 | } else { |
| 9457 | for (const auto &info: policyInfos) { |
| 9458 | mmapPolicyByDeviceType[info.device.type] = info.mmapPolicy; |
| 9459 | } |
| 9460 | } |
| 9461 | mMmapPolicyByDeviceType.emplace(policyType, mmapPolicyByDeviceType); |
| 9462 | mMmapPolicyInfos.emplace(policyType, policyInfos); |
| 9463 | return NO_ERROR; |
| 9464 | } |
| 9465 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 9466 | } // namespace android |